blob: e0bdc3fda9113064ee8346e9d32d795d4d3be7fd [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 *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001376 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001377 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001378 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001379 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001380 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001381
Bram Moolenaar4525a572022-02-13 11:57:33 +00001382 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001383 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001384
1385 ga_init(&newargs);
1386 ga_init(&newlines);
1387
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001388 // First, check if this is really a lambda expression. "->" or "=>" must
1389 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001390 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001391 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001392 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar14ded112021-06-26 19:25:49 +02001393 NULL, &default_args, TRUE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001394 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001395 {
1396 if (types_optional)
1397 ga_clear_strings(&argtypes);
Bram Moolenaar0346b792021-01-31 22:18:29 +01001398 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001399 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001400
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001401 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001402 if (evaluate)
1403 pnewargs = &newargs;
1404 else
1405 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001406 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001407 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001408 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001409 &varargs, &default_args,
1410 FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001411 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001412 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001413 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001414 {
1415 if (types_optional)
1416 ga_clear_strings(&argtypes);
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001417 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001418 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001419 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001420 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001421
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001422 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1423 if (ret_type != NULL)
1424 {
1425 ret_type = vim_strsave(ret_type);
1426 tofree2 = ret_type;
1427 }
1428
Bram Moolenaare38eab22019-12-05 21:50:01 +01001429 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001430 if (evaluate)
1431 eval_lavars_used = &eval_lavars;
1432
Bram Moolenaar65c44152020-12-24 15:14:01 +01001433 *arg = skipwhite_and_linebreak(*arg, evalarg);
1434
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001435 // Recognize "{" as the start of a function body.
1436 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001437 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001438 if (evalarg == NULL)
1439 // cannot happen?
1440 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001441 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001442 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1443 types_optional ? &argtypes : NULL, varargs,
1444 &default_args, ret_type) == FAIL)
1445 goto errret;
1446 goto theend;
1447 }
1448 if (default_args.ga_len > 0)
1449 {
1450 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001451 goto errret;
1452 }
1453
Bram Moolenaare38eab22019-12-05 21:50:01 +01001454 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001455 start = *arg;
1456 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001457 if (ret == FAIL)
1458 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001459
Bram Moolenaar65c44152020-12-24 15:14:01 +01001460 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001461 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001462 *arg = skipwhite_and_linebreak(*arg, evalarg);
1463 if (**arg != '}')
1464 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001465 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001466 goto errret;
1467 }
1468 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001469 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001470
1471 if (evaluate)
1472 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001473 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001474 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001475 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001476 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001477 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001478
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001479 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001480 if (fp == NULL)
1481 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001482 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001483 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001484 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001485 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001486
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001487 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001488 if (ga_grow(&newlines, 1) == FAIL)
1489 goto errret;
1490
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001491 // If there are line breaks, we need to split up the string.
1492 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001493 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001494 line_end = end;
1495
1496 // Add "return " before the expression (or the first line).
1497 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001498 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001499 if (p == NULL)
1500 goto errret;
1501 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1502 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001503 vim_strncpy(p + 7, start, line_end - start);
1504
1505 if (line_end != end)
1506 {
1507 // Add more lines, split by line breaks. Thus is used when a
1508 // lambda with { cmds } is encountered.
1509 while (*line_end == '\n')
1510 {
1511 if (ga_grow(&newlines, 1) == FAIL)
1512 goto errret;
1513 start = line_end + 1;
1514 line_end = vim_strchr(start, '\n');
1515 if (line_end == NULL)
1516 line_end = end;
1517 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1518 vim_strnsave(start, line_end - start);
1519 }
1520 }
1521
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001522 if (strstr((char *)p + 7, "a:") == NULL)
1523 // No a: variables are used for sure.
1524 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001525
1526 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001527 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001528 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001529 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001530 if (types_optional)
1531 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001532 if (parse_argument_types(fp, &argtypes,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001533 vim9script && varargs) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001534 goto errret;
1535 if (ret_type != NULL)
1536 {
1537 fp->uf_ret_type = parse_type(&ret_type,
1538 &fp->uf_type_list, TRUE);
1539 if (fp->uf_ret_type == NULL)
1540 goto errret;
1541 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001542 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001543 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001544 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001545
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001546 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001547 if (current_funccal != NULL && eval_lavars)
1548 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001549 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001550 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001551 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001552 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001553
1554#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001555 if (prof_def_func())
1556 func_do_profile(fp);
1557#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001558 if (sandbox)
1559 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001560 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001561 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001562 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001563 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001564 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001565 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001566 // Use the line number of the arguments.
1567 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001568
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001569 function_using_block_scopes(fp, evalarg->eval_cstack);
1570
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001571 pt->pt_func = fp;
1572 pt->pt_refcount = 1;
1573 rettv->vval.v_partial = pt;
1574 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001575
1576 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001577 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001578
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001579theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001580 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001581 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001582 if (types_optional)
1583 ga_clear_strings(&argtypes);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001584
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001585 return OK;
1586
1587errret:
1588 ga_clear_strings(&newargs);
1589 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001590 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001591 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001592 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001593 ga_clear_strings(&argtypes);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001594 if (fp != NULL)
1595 vim_free(fp->uf_arg_types);
1596 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001597 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001598 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001599 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001600 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001601 return FAIL;
1602}
1603
1604/*
1605 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1606 * name it contains, otherwise return "name".
1607 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1608 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001609 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1610 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001611 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001612 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001613 */
1614 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001615deref_func_name(
1616 char_u *name,
1617 int *lenp,
1618 partial_T **partialp,
1619 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001620 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001621 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001622 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001623{
1624 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001625 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001626 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001627 char_u *s = NULL;
1628 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001629 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001630
1631 if (partialp != NULL)
1632 *partialp = NULL;
1633
1634 cc = name[*lenp];
1635 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001636
Bram Moolenaar71f21932022-01-07 18:20:55 +00001637 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001638 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001639 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001640 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001641 tv = &v->di_tv;
1642 }
1643 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1644 {
1645 imported_T *import;
1646 char_u *p = name;
1647 int len = *lenp;
1648
1649 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001650 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001651 p = name + 2;
1652 len -= 2;
1653 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001654 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001655
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001656 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001657 if (import != NULL)
1658 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001659 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001660 if (new_function)
1661 semsg(_(e_redefining_imported_item_str), name);
1662 else
1663 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001664 name[len] = cc;
1665 *lenp = 0;
1666 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001667 }
1668 }
1669
1670 if (tv != NULL)
1671 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001672 if (found_var != NULL)
1673 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001674 if (tv->v_type == VAR_FUNC)
1675 {
1676 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001677 {
1678 *lenp = 0;
1679 return (char_u *)""; // just in case
1680 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001681 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001682 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001683 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001684
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001685 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001686 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001687 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001688
1689 if (pt == NULL)
1690 {
1691 *lenp = 0;
1692 return (char_u *)""; // just in case
1693 }
1694 if (partialp != NULL)
1695 *partialp = pt;
1696 s = partial_name(pt);
1697 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001698 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001699
1700 if (s != NULL)
1701 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001702 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001703 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001704 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001705
1706 if (sv != NULL)
1707 *type = sv->sv_type;
1708 }
1709 return s;
1710 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001711 }
1712
1713 return name;
1714}
1715
1716/*
1717 * Give an error message with a function name. Handle <SNR> things.
1718 * "ermsg" is to be passed without translation, use N_() instead of _().
1719 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001720 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001721emsg_funcname(char *ermsg, char_u *name)
1722{
Bram Moolenaar54598062022-01-13 12:05:09 +00001723 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001724
Bram Moolenaar54598062022-01-13 12:05:09 +00001725 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001726 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001727 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001728 if (p != name)
1729 vim_free(p);
1730}
1731
1732/*
1733 * Allocate a variable for the result of a function.
1734 * Return OK or FAIL.
1735 */
1736 int
1737get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001738 char_u *name, // name of the function
1739 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001740 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001741 char_u **arg, // argument, pointing to the '('
Bram Moolenaare6b53242020-07-01 17:28:33 +02001742 evalarg_T *evalarg, // for line continuation
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001743 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001744{
1745 char_u *argp;
1746 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001747 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1748 int argcount = 0; // number of arguments found
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001749 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001750 int evaluate = evalarg == NULL
1751 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001752
1753 /*
1754 * Get the arguments.
1755 */
1756 argp = *arg;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001757 while (argcount < MAX_FUNC_ARGS - (funcexe->fe_partial == NULL ? 0
1758 : funcexe->fe_partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001759 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001760 // skip the '(' or ',' and possibly line breaks
1761 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1762
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001763 if (*argp == ')' || *argp == ',' || *argp == NUL)
1764 break;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001765 if (eval1(&argp, &argvars[argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001766 {
1767 ret = FAIL;
1768 break;
1769 }
1770 ++argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001771 // The comma should come right after the argument, but this wasn't
1772 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001773 if (vim9script)
1774 {
1775 if (*argp != ',' && *skipwhite(argp) == ',')
1776 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001777 if (evaluate)
1778 semsg(_(e_no_white_space_allowed_before_str_str),
1779 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001780 ret = FAIL;
1781 break;
1782 }
1783 }
1784 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001785 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001786 if (*argp != ',')
1787 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001788 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
1789 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001790 if (evaluate)
1791 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001792 ret = FAIL;
1793 break;
1794 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001795 }
Bram Moolenaare6b53242020-07-01 17:28:33 +02001796 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001797 if (*argp == ')')
1798 ++argp;
1799 else
1800 ret = FAIL;
1801
1802 if (ret == OK)
1803 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001804 int i = 0;
1805 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001806
1807 if (get_vim_var_nr(VV_TESTING))
1808 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001809 // Prepare for calling test_garbagecollect_now(), need to know
1810 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001811 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001812 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001813 for (i = 0; i < argcount; ++i)
1814 if (ga_grow(&funcargs, 1) == OK)
1815 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1816 &argvars[i];
1817 }
1818
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001819 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00001820 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001821 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001822 // An error in a builtin function does not return FAIL, but we do
1823 // want to abort further processing if an error was given.
1824 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001825 clear_tv(rettv);
1826 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001827
1828 funcargs.ga_len -= i;
1829 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001830 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001831 {
1832 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001833 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001834 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001835 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001836 }
1837
1838 while (--argcount >= 0)
1839 clear_tv(&argvars[argcount]);
1840
Bram Moolenaar4525a572022-02-13 11:57:33 +00001841 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02001842 *arg = argp;
1843 else
1844 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001845 return ret;
1846}
1847
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001848/*
1849 * Return TRUE if "p" starts with "<SID>" or "s:".
1850 * Only works if eval_fname_script() returned non-zero for "p"!
1851 */
1852 static int
1853eval_fname_sid(char_u *p)
1854{
1855 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
1856}
1857
1858/*
1859 * In a script change <SID>name() and s:name() to K_SNR 123_name().
1860 * Change <SNR>123_name() to K_SNR 123_name().
1861 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
1862 * (slow).
1863 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001864 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001865fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
1866{
1867 int llen;
1868 char_u *fname;
1869 int i;
1870
1871 llen = eval_fname_script(name);
1872 if (llen > 0)
1873 {
1874 fname_buf[0] = K_SPECIAL;
1875 fname_buf[1] = KS_EXTRA;
1876 fname_buf[2] = (int)KE_SNR;
1877 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001878 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001879 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001880 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001881 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001882 else
1883 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001884 sprintf((char *)fname_buf + 3, "%ld_",
1885 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001886 i = (int)STRLEN(fname_buf);
1887 }
1888 }
1889 if (i + STRLEN(name + llen) < FLEN_FIXED)
1890 {
1891 STRCPY(fname_buf + i, name + llen);
1892 fname = fname_buf;
1893 }
1894 else
1895 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001896 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001897 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +01001898 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001899 else
1900 {
1901 *tofree = fname;
1902 mch_memmove(fname, fname_buf, (size_t)i);
1903 STRCPY(fname + i, name + llen);
1904 }
1905 }
1906 }
1907 else
1908 fname = name;
1909 return fname;
1910}
1911
1912/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001913 * Concatenate the script ID and function name into "<SNR>99_name".
1914 * "buffer" must have size MAX_FUNC_NAME_LEN.
1915 */
1916 void
1917func_name_with_sid(char_u *name, int sid, char_u *buffer)
1918{
1919 // A script-local function is stored as "<SNR>99_name".
1920 buffer[0] = K_SPECIAL;
1921 buffer[1] = KS_EXTRA;
1922 buffer[2] = (int)KE_SNR;
1923 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
1924 (long)sid, name);
1925}
1926
1927/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 * Find a function "name" in script "sid".
1929 */
1930 static ufunc_T *
1931find_func_with_sid(char_u *name, int sid)
1932{
Bram Moolenaarb8822442022-01-11 15:24:05 +00001933 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001934 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935
Bram Moolenaarb8822442022-01-11 15:24:05 +00001936 if (!SCRIPT_ID_VALID(sid))
1937 return NULL; // not in a script
1938
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001939 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001940 hi = hash_find(&func_hashtab, buffer);
1941 if (!HASHITEM_EMPTY(hi))
1942 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00001943 return NULL;
1944}
1945
1946/*
1947 * Find a function "name" in script "sid" prefixing the autoload prefix.
1948 */
1949 static ufunc_T *
1950find_func_with_prefix(char_u *name, int sid)
1951{
1952 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001953 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00001954 scriptitem_T *si;
1955
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001956 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00001957 return NULL; // already has the prefix
1958 if (!SCRIPT_ID_VALID(sid))
1959 return NULL; // not in a script
1960 si = SCRIPT_ITEM(sid);
1961 if (si->sn_autoload_prefix != NULL)
1962 {
1963 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
1964 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00001965 char_u *namep;
1966
1967 // skip a "<SNR>99_" prefix
1968 namep = untrans_function_name(name);
1969 if (namep == NULL)
1970 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00001971
1972 // An exported function in an autoload script is stored as
1973 // "dir#path#name".
1974 if (len < sizeof(buffer))
1975 auto_name = buffer;
1976 else
1977 auto_name = alloc(len);
1978 if (auto_name != NULL)
1979 {
1980 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00001981 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00001982 hi = hash_find(&func_hashtab, auto_name);
1983 if (auto_name != buffer)
1984 vim_free(auto_name);
1985 if (!HASHITEM_EMPTY(hi))
1986 return HI2UF(hi);
1987 }
1988 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989
1990 return NULL;
1991}
1992
1993/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001994 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001995 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
1996 * functions.
1997 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001998 * Return NULL for unknown function.
1999 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002000 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002001find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002002{
2003 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002006 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002008 // Find script-local function before global one.
2009 if (in_vim9script() && eval_isnamec1(*name)
2010 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002012 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2013 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002014 if (func != NULL)
2015 return func;
2016 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002017 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2018 {
2019 char_u *p = name + 5;
2020 long sid;
2021
2022 // printable "<SNR>123_Name" form
2023 sid = getdigits(&p);
2024 if (*p == '_')
2025 {
2026 func = find_func_with_sid(p + 1, (int)sid);
2027 if (func != NULL)
2028 return func;
2029 }
2030 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002032
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002033 if ((flags & FFED_NO_GLOBAL) == 0)
2034 {
2035 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002036 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002037 if (!HASHITEM_EMPTY(hi))
2038 return HI2UF(hi);
2039 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040
Bram Moolenaarb8822442022-01-11 15:24:05 +00002041 // Find autoload function if this is an autoload script.
2042 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2043 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044}
2045
2046/*
2047 * Find a function by name, return pointer to it in ufuncs.
2048 * "cctx" is passed in a :def function to find imported functions.
2049 * Return NULL for unknown or dead function.
2050 */
2051 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002052find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002054 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002055
2056 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2057 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002058 return NULL;
2059}
2060
2061/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002062 * Return TRUE if "ufunc" is a global function.
2063 */
2064 int
2065func_is_global(ufunc_T *ufunc)
2066{
2067 return ufunc->uf_name[0] != K_SPECIAL;
2068}
2069
2070/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002071 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2072 */
2073 int
2074func_requires_g_prefix(ufunc_T *ufunc)
2075{
2076 return ufunc->uf_name[0] != K_SPECIAL
2077 && (ufunc->uf_flags & FC_LAMBDA) == 0
2078 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL;
2079}
2080
2081/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002082 * Copy the function name of "fp" to buffer "buf".
2083 * "buf" must be able to hold the function name plus three bytes.
2084 * Takes care of script-local function names.
2085 */
2086 static void
2087cat_func_name(char_u *buf, ufunc_T *fp)
2088{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002089 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002090 {
2091 STRCPY(buf, "<SNR>");
2092 STRCAT(buf, fp->uf_name + 3);
2093 }
2094 else
2095 STRCPY(buf, fp->uf_name);
2096}
2097
2098/*
2099 * Add a number variable "name" to dict "dp" with value "nr".
2100 */
2101 static void
2102add_nr_var(
2103 dict_T *dp,
2104 dictitem_T *v,
2105 char *name,
2106 varnumber_T nr)
2107{
2108 STRCPY(v->di_key, name);
2109 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2110 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
2111 v->di_tv.v_type = VAR_NUMBER;
2112 v->di_tv.v_lock = VAR_FIXED;
2113 v->di_tv.vval.v_number = nr;
2114}
2115
2116/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002117 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002118 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002119 static void
2120free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002121{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002122 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002123
2124 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
2125 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002126 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002127
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002128 // When garbage collecting a funccall_T may be freed before the
2129 // function that references it, clear its uf_scoped field.
2130 // The function may have been redefined and point to another
2131 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002132 if (fp != NULL && fp->uf_scoped == fc)
2133 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002134 }
Bram Moolenaar58016442016-07-31 18:30:22 +02002135 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002136
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002137 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002138 vim_free(fc);
2139}
2140
2141/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002142 * Free "fc" and what it contains.
2143 * Can be called only when "fc" is kept beyond the period of it called,
2144 * i.e. after cleanup_function_call(fc).
2145 */
2146 static void
2147free_funccal_contents(funccall_T *fc)
2148{
2149 listitem_T *li;
2150
2151 // Free all l: variables.
2152 vars_clear(&fc->l_vars.dv_hashtab);
2153
2154 // Free all a: variables.
2155 vars_clear(&fc->l_avars.dv_hashtab);
2156
2157 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002158 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002159 clear_tv(&li->li_tv);
2160
2161 free_funccal(fc);
2162}
2163
2164/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002165 * Handle the last part of returning from a function: free the local hashtable.
2166 * Unless it is still in use by a closure.
2167 */
2168 static void
2169cleanup_function_call(funccall_T *fc)
2170{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002171 int may_free_fc = fc->fc_refcount <= 0;
2172 int free_fc = TRUE;
2173
Bram Moolenaar6914c642017-04-01 21:21:30 +02002174 current_funccal = fc->caller;
2175
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002176 // Free all l: variables if not referred.
2177 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
2178 vars_clear(&fc->l_vars.dv_hashtab);
2179 else
2180 free_fc = FALSE;
2181
2182 // If the a:000 list and the l: and a: dicts are not referenced and
2183 // there is no closure using it, we can free the funccall_T and what's
2184 // in it.
2185 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
2186 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002187 else
2188 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002189 int todo;
2190 hashitem_T *hi;
2191 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002192
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002193 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002194
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002195 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +02002196 todo = (int)fc->l_avars.dv_hashtab.ht_used;
2197 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
2198 {
2199 if (!HASHITEM_EMPTY(hi))
2200 {
2201 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002202 di = HI2DI(hi);
2203 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002204 }
2205 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002206 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002207
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002208 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2209 fc->l_varlist.lv_first = NULL;
2210 else
2211 {
2212 listitem_T *li;
2213
2214 free_fc = FALSE;
2215
2216 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002217 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002218 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002219 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002220
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002221 if (free_fc)
2222 free_funccal(fc);
2223 else
2224 {
2225 static int made_copy = 0;
2226
2227 // "fc" is still in use. This can happen when returning "a:000",
2228 // assigning "l:" to a global variable or defining a closure.
2229 // Link "fc" in the list for garbage collection later.
2230 fc->caller = previous_funccal;
2231 previous_funccal = fc;
2232
2233 if (want_garbage_collect)
2234 // If garbage collector is ready, clear count.
2235 made_copy = 0;
2236 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002237 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002238 // We have made a lot of copies, worth 4 Mbyte. This can happen
2239 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002240 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002241 // too much memory.
2242 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002243 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002244 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002245 }
2246}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002247
2248/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002249 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2250 */
2251 static int
2252numbered_function(char_u *name)
2253{
2254 return isdigit(*name)
2255 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2256}
2257
2258/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002259 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002260 * 1. ordinary names, function defined with :function or :def;
2261 * can start with "<SNR>123_" literally or with K_SPECIAL.
2262 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002263 * For the first we only count the name stored in func_hashtab as a reference,
2264 * using function() does not count as a reference, because the function is
2265 * looked up by name.
2266 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002267 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002268func_name_refcount(char_u *name)
2269{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002270 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002271}
2272
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273/*
2274 * Unreference "fc": decrement the reference count and free it when it
2275 * becomes zero. "fp" is detached from "fc".
2276 * When "force" is TRUE we are exiting.
2277 */
2278 static void
2279funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2280{
2281 funccall_T **pfc;
2282 int i;
2283
2284 if (fc == NULL)
2285 return;
2286
2287 if (--fc->fc_refcount <= 0 && (force || (
2288 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
2289 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
2290 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2291 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
2292 {
2293 if (fc == *pfc)
2294 {
2295 *pfc = fc->caller;
2296 free_funccal_contents(fc);
2297 return;
2298 }
2299 }
2300 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
2301 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
2302 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
2303}
2304
2305/*
2306 * Remove the function from the function hashtable. If the function was
2307 * deleted while it still has references this was already done.
2308 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2309 */
2310 static int
2311func_remove(ufunc_T *fp)
2312{
2313 hashitem_T *hi;
2314
2315 // Return if it was already virtually deleted.
2316 if (fp->uf_flags & FC_DEAD)
2317 return FALSE;
2318
2319 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
2320 if (!HASHITEM_EMPTY(hi))
2321 {
2322 // When there is a def-function index do not actually remove the
2323 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002324 // Do remove it when it's a copy.
2325 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaarc970e422021-03-17 15:03:04 +01002326 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002327 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01002328 return FALSE;
2329 }
2330 hash_remove(&func_hashtab, hi);
2331 fp->uf_flags |= FC_DELETED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332 return TRUE;
2333 }
2334 return FALSE;
2335}
2336
2337 static void
2338func_clear_items(ufunc_T *fp)
2339{
2340 ga_clear_strings(&(fp->uf_args));
2341 ga_clear_strings(&(fp->uf_def_args));
2342 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002343 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002344 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002345 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002346 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002347
2348 // Increment the refcount of this function to avoid it being freed
2349 // recursively when the partial is freed.
2350 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002351 partial_unref(fp->uf_partial);
2352 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002353 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002354
2355#ifdef FEAT_LUA
2356 if (fp->uf_cb_free != NULL)
2357 {
2358 fp->uf_cb_free(fp->uf_cb_state);
2359 fp->uf_cb_free = NULL;
2360 }
2361
2362 fp->uf_cb_state = NULL;
2363 fp->uf_cb = NULL;
2364#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365#ifdef FEAT_PROFILE
2366 VIM_CLEAR(fp->uf_tml_count);
2367 VIM_CLEAR(fp->uf_tml_total);
2368 VIM_CLEAR(fp->uf_tml_self);
2369#endif
2370}
2371
2372/*
2373 * Free all things that a function contains. Does not free the function
2374 * itself, use func_free() for that.
2375 * When "force" is TRUE we are exiting.
2376 */
2377 static void
2378func_clear(ufunc_T *fp, int force)
2379{
2380 if (fp->uf_cleared)
2381 return;
2382 fp->uf_cleared = TRUE;
2383
2384 // clear this function
2385 func_clear_items(fp);
2386 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002387 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388}
2389
2390/*
2391 * Free a function and remove it from the list of functions. Does not free
2392 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002393 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002394 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002396 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002397func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398{
2399 // Only remove it when not done already, otherwise we would remove a newer
2400 // version of the function with the same name.
2401 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2402 func_remove(fp);
2403
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002404 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002405 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002406 if (fp->uf_dfunc_idx > 0)
2407 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002408 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002409 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002410 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002411 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002412 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413}
2414
2415/*
2416 * Free all things that a function contains and free the function itself.
2417 * When "force" is TRUE we are exiting.
2418 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002419 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420func_clear_free(ufunc_T *fp, int force)
2421{
2422 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002423 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2424 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002425 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002426 else
2427 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428}
2429
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002430/*
2431 * Copy already defined function "lambda" to a new function with name "global".
2432 * This is for when a compiled function defines a global function.
2433 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002434 int
2435copy_func(char_u *lambda, char_u *global, ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002436{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002437 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002438 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002439
2440 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002441 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002442 semsg(_(e_lambda_function_not_found_str), lambda);
2443 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002444 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002445
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002446 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002447 if (fp != NULL)
2448 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002449 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002450 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002451 return FAIL;
2452 }
2453
2454 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
2455 if (fp == NULL)
2456 return FAIL;
2457
2458 fp->uf_varargs = ufunc->uf_varargs;
2459 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2460 fp->uf_def_status = ufunc->uf_def_status;
2461 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2462 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2463 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2464 == FAIL
2465 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2466 goto failed;
2467
2468 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
2469 : vim_strsave(ufunc->uf_name_exp);
2470 if (ufunc->uf_arg_types != NULL)
2471 {
2472 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2473 if (fp->uf_arg_types == NULL)
2474 goto failed;
2475 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2476 sizeof(type_T *) * fp->uf_args.ga_len);
2477 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002478 if (ufunc->uf_va_name != NULL)
2479 {
2480 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2481 if (fp->uf_va_name == NULL)
2482 goto failed;
2483 }
2484 fp->uf_ret_type = ufunc->uf_ret_type;
2485
2486 fp->uf_refcount = 1;
2487 STRCPY(fp->uf_name, global);
2488 hash_add(&func_hashtab, UF2HIKEY(fp));
2489
2490 // the referenced dfunc_T is now used one more time
2491 link_def_function(fp);
2492
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002493 // Create a partial to store the context of the function where it was
2494 // instantiated. Only needs to be done once. Do this on the original
2495 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002496 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2497 {
2498 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2499
2500 if (pt == NULL)
2501 goto failed;
2502 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002503 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002504 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002505 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002506 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002507 ufunc->uf_partial = pt;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002508 --pt->pt_refcount; // not actually referenced here
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002509 }
2510
2511 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002512
2513failed:
2514 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002515 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002516}
2517
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002518static int funcdepth = 0;
2519
2520/*
2521 * Increment the function call depth count.
2522 * Return FAIL when going over 'maxfuncdepth'.
2523 * Otherwise return OK, must call funcdepth_decrement() later!
2524 */
2525 int
2526funcdepth_increment(void)
2527{
2528 if (funcdepth >= p_mfd)
2529 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002530 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002531 return FAIL;
2532 }
2533 ++funcdepth;
2534 return OK;
2535}
2536
2537 void
2538funcdepth_decrement(void)
2539{
2540 --funcdepth;
2541}
2542
2543/*
2544 * Get the current function call depth.
2545 */
2546 int
2547funcdepth_get(void)
2548{
2549 return funcdepth;
2550}
2551
2552/*
2553 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002554 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002555 * times as funcdepth_increment().
2556 */
2557 void
2558funcdepth_restore(int depth)
2559{
2560 funcdepth = depth;
2561}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002562
2563/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002564 * Call a user function.
2565 */
2566 static void
2567call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002568 ufunc_T *fp, // pointer to function
2569 int argcount, // nr of args
2570 typval_T *argvars, // arguments
2571 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002572 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002573 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002574{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002575 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002576 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002577 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002578 funccall_T *fc;
2579 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002580 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002581 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002582 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002583 int i;
2584 int ai;
2585 int islambda = FALSE;
2586 char_u numbuf[NUMBUFLEN];
2587 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002588 typval_T *tv_to_free[MAX_FUNC_ARGS];
2589 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002590#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002591 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002592#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01002593 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002594
Bram Moolenaarb2049902021-01-24 12:53:53 +01002595#ifdef FEAT_PROFILE
2596 CLEAR_FIELD(profile_info);
2597#endif
2598
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002599 // If depth of calling is getting too high, don't execute the function.
2600 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002601 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002602 rettv->v_type = VAR_NUMBER;
2603 rettv->vval.v_number = -1;
2604 return;
2605 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002606
Bram Moolenaare38eab22019-12-05 21:50:01 +01002607 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002608
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002609 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002610 if (fc == NULL)
2611 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002612 fc->caller = current_funccal;
2613 current_funccal = fc;
2614 fc->func = fp;
2615 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002616 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002617 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002618 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2619 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002620 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002621 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002622 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002623
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002624 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002626#ifdef FEAT_PROFILE
2627 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2628#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002629 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002630#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002631 if (do_profiling == PROF_YES)
2632 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002633#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002634 sticky_cmdmod_flags = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002635 call_def_function(fp, argcount, argvars, funcexe->fe_partial, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002636 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002637#ifdef FEAT_PROFILE
2638 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002639 || (caller != NULL && caller->uf_profiling)))
2640 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002641#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642 current_funccal = fc->caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643 free_funccal(fc);
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002644 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 return;
2646 }
2647
Bram Moolenaar38453522021-11-28 22:00:12 +00002648 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002649
2650 /*
2651 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
2652 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
2653 * each argument variable and saves a lot of time.
2654 */
2655 /*
2656 * Init l: variables.
2657 */
2658 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
2659 if (selfdict != NULL)
2660 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002661 // Set l:self to "selfdict". Use "name" to avoid a warning from
2662 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002663 v = &fc->fixvar[fixvar_idx++].var;
2664 name = v->di_key;
2665 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002666 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002667 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
2668 v->di_tv.v_type = VAR_DICT;
2669 v->di_tv.v_lock = 0;
2670 v->di_tv.vval.v_dict = selfdict;
2671 ++selfdict->dv_refcount;
2672 }
2673
2674 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002675 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002676 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002677 * Set a:000 to a list with room for the "..." arguments.
2678 */
2679 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002680 if ((fp->uf_flags & FC_NOARGS) == 0)
2681 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002682 (varnumber_T)(argcount >= fp->uf_args.ga_len
2683 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01002684 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002685 if ((fp->uf_flags & FC_NOARGS) == 0)
2686 {
2687 // Use "name" to avoid a warning from some compiler that checks the
2688 // destination size.
2689 v = &fc->fixvar[fixvar_idx++].var;
2690 name = v->di_key;
2691 STRCPY(name, "000");
2692 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2693 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
2694 v->di_tv.v_type = VAR_LIST;
2695 v->di_tv.v_lock = VAR_FIXED;
2696 v->di_tv.vval.v_list = &fc->l_varlist;
2697 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02002698 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002699 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2700 fc->l_varlist.lv_lock = VAR_FIXED;
2701
2702 /*
2703 * Set a:firstline to "firstline" and a:lastline to "lastline".
2704 * Set a:name to named arguments.
2705 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002706 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002707 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002708 if ((fp->uf_flags & FC_NOARGS) == 0)
2709 {
2710 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002711 (varnumber_T)funcexe->fe_firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002712 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002713 (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002714 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002715 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002716 {
2717 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002718 typval_T def_rettv;
2719 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002720
2721 ai = i - fp->uf_args.ga_len;
2722 if (ai < 0)
2723 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002724 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002725 name = FUNCARG(fp, i);
2726 if (islambda)
2727 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002728
2729 // evaluate named argument default expression
2730 isdefault = ai + fp->uf_def_args.ga_len >= 0
2731 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2732 && argvars[i].vval.v_number == VVAL_NONE));
2733 if (isdefault)
2734 {
2735 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002736
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002737 def_rettv.v_type = VAR_NUMBER;
2738 def_rettv.vval.v_number = -1;
2739
2740 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2741 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002742 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002743 {
2744 default_arg_err = 1;
2745 break;
2746 }
2747 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002748 }
2749 else
2750 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002751 if ((fp->uf_flags & FC_NOARGS) != 0)
2752 // Bail out if no a: arguments used (in lambda).
2753 break;
2754
Bram Moolenaare38eab22019-12-05 21:50:01 +01002755 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002756 sprintf((char *)numbuf, "%d", ai + 1);
2757 name = numbuf;
2758 }
2759 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2760 {
2761 v = &fc->fixvar[fixvar_idx++].var;
2762 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002763 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002764 }
2765 else
2766 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002767 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002768 if (v == NULL)
2769 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002770 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002771 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002772
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002773 // Note: the values are copied directly to avoid alloc/free.
2774 // "argvars" must have VAR_FIXED for v_lock.
2775 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002776 v->di_tv.v_lock = VAR_FIXED;
2777
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002778 if (isdefault)
2779 // Need to free this later, no matter where it's stored.
2780 tv_to_free[tv_to_free_len++] = &v->di_tv;
2781
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002782 if (addlocal)
2783 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002784 // Named arguments should be accessed without the "a:" prefix in
2785 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002786 copy_tv(&v->di_tv, &v->di_tv);
2787 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002788 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002789 else
2790 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002791
2792 if (ai >= 0 && ai < MAX_FUNC_ARGS)
2793 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002794 listitem_T *li = &fc->l_listitems[ai];
2795
2796 li->li_tv = argvars[i];
2797 li->li_tv.v_lock = VAR_FIXED;
2798 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002799 }
2800 }
2801
Bram Moolenaare38eab22019-12-05 21:50:01 +01002802 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002803 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02002804
2805 if (fp->uf_flags & FC_SANDBOX)
2806 {
2807 using_sandbox = TRUE;
2808 ++sandbox;
2809 }
2810
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002811 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002812 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002813 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002814 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002815 ++no_wait_return;
2816 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002817
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002818 smsg(_("calling %s"), SOURCING_NAME);
2819 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002820 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002821 char_u buf[MSG_BUF_LEN];
2822 char_u numbuf2[NUMBUFLEN];
2823 char_u *tofree;
2824 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002825
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002826 msg_puts("(");
2827 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002828 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002829 if (i > 0)
2830 msg_puts(", ");
2831 if (argvars[i].v_type == VAR_NUMBER)
2832 msg_outnum((long)argvars[i].vval.v_number);
2833 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002834 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002835 // Do not want errors such as E724 here.
2836 ++emsg_off;
2837 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
2838 --emsg_off;
2839 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002840 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002841 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002842 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002843 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2844 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002845 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002846 msg_puts((char *)s);
2847 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002848 }
2849 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002850 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002851 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002852 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002853 msg_puts("\n"); // don't overwrite this either
2854
2855 verbose_leave_scroll();
2856 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002857 }
2858#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002859 if (do_profiling == PROF_YES)
2860 profile_may_start_func(&profile_info, fp,
2861 fc->caller == NULL ? NULL : fc->caller->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002862#endif
2863
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002864 // "legacy" does not apply to commands in the function
2865 sticky_cmdmod_flags = 0;
2866
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002867 save_current_sctx = current_sctx;
2868 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002869 save_did_emsg = did_emsg;
2870 did_emsg = FALSE;
2871
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002872 if (default_arg_err && (fp->uf_flags & FC_ABORT))
2873 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002874 else if (islambda)
2875 {
2876 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
2877
2878 // A Lambda always has the command "return {expr}". It is much faster
2879 // to evaluate {expr} directly.
2880 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002881 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002882 --ex_nesting_level;
2883 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002884 else
2885 // call do_cmdline() to execute the lines
2886 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002887 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
2888
2889 --RedrawingDisabled;
2890
Bram Moolenaare38eab22019-12-05 21:50:01 +01002891 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002892 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
2893 {
2894 clear_tv(rettv);
2895 rettv->v_type = VAR_NUMBER;
2896 rettv->vval.v_number = -1;
2897 }
2898
2899#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002900 if (do_profiling == PROF_YES)
2901 {
2902 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2903
2904 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
2905 profile_may_end_func(&profile_info, fp, caller);
2906 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002907#endif
2908
Bram Moolenaare38eab22019-12-05 21:50:01 +01002909 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002910 if (p_verbose >= 12)
2911 {
2912 ++no_wait_return;
2913 verbose_enter_scroll();
2914
2915 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002916 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002917 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002918 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002919 (long)fc->rettv->vval.v_number);
2920 else
2921 {
2922 char_u buf[MSG_BUF_LEN];
2923 char_u numbuf2[NUMBUFLEN];
2924 char_u *tofree;
2925 char_u *s;
2926
Bram Moolenaare38eab22019-12-05 21:50:01 +01002927 // The value may be very long. Skip the middle part, so that we
2928 // have some idea how it starts and ends. smsg() would always
2929 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002930 ++emsg_off;
2931 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
2932 --emsg_off;
2933 if (s != NULL)
2934 {
2935 if (vim_strsize(s) > MSG_BUF_CLEN)
2936 {
2937 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2938 s = buf;
2939 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002940 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002941 vim_free(tofree);
2942 }
2943 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002944 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002945
2946 verbose_leave_scroll();
2947 --no_wait_return;
2948 }
2949
Bram Moolenaare31ee862020-01-07 20:59:34 +01002950 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002951 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002952 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002953#ifdef FEAT_PROFILE
2954 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002955 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002956#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02002957 if (using_sandbox)
2958 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002959 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002960
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002961 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002962 {
2963 ++no_wait_return;
2964 verbose_enter_scroll();
2965
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002966 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002967 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002968
2969 verbose_leave_scroll();
2970 --no_wait_return;
2971 }
2972
2973 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002974 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002975 for (i = 0; i < tv_to_free_len; ++i)
2976 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002977 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002978}
2979
2980/*
Bram Moolenaar50824712020-12-20 21:10:17 +01002981 * Check the argument count for user function "fp".
2982 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
2983 */
2984 int
2985check_user_func_argcount(ufunc_T *fp, int argcount)
2986{
2987 int regular_args = fp->uf_args.ga_len;
2988
2989 if (argcount < regular_args - fp->uf_def_args.ga_len)
2990 return FCERR_TOOFEW;
2991 else if (!has_varargs(fp) && argcount > regular_args)
2992 return FCERR_TOOMANY;
2993 return FCERR_UNKNOWN;
2994}
2995
2996/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002998 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 int
3000call_user_func_check(
3001 ufunc_T *fp,
3002 int argcount,
3003 typval_T *argvars,
3004 typval_T *rettv,
3005 funcexe_T *funcexe,
3006 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003007{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003009
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003010#ifdef FEAT_LUA
3011 if (fp->uf_flags & FC_CFUNC)
3012 {
3013 cfunc_T cb = fp->uf_cb;
3014
3015 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3016 }
3017#endif
3018
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003019 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3020 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003021 error = check_user_func_argcount(fp, argcount);
3022 if (error != FCERR_UNKNOWN)
3023 return error;
3024 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 error = FCERR_DICT;
3026 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003027 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 int did_save_redo = FALSE;
3029 save_redo_T save_redo;
3030
3031 /*
3032 * Call the user function.
3033 * Save and restore search patterns, script variables and
3034 * redo buffer.
3035 */
3036 save_search_patterns();
3037 if (!ins_compl_active())
3038 {
3039 saveRedobuff(&save_redo);
3040 did_save_redo = TRUE;
3041 }
3042 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003043 call_user_func(fp, argcount, argvars, rettv, funcexe,
3044 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3046 // Function was unreferenced while being used, free it now.
3047 func_clear_free(fp, FALSE);
3048 if (did_save_redo)
3049 restoreRedobuff(&save_redo);
3050 restore_search_patterns();
3051 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003052 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003054}
3055
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003056static funccal_entry_T *funccal_stack = NULL;
3057
3058/*
3059 * Save the current function call pointer, and set it to NULL.
3060 * Used when executing autocommands and for ":source".
3061 */
3062 void
3063save_funccal(funccal_entry_T *entry)
3064{
3065 entry->top_funccal = current_funccal;
3066 entry->next = funccal_stack;
3067 funccal_stack = entry;
3068 current_funccal = NULL;
3069}
3070
3071 void
3072restore_funccal(void)
3073{
3074 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003075 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003076 else
3077 {
3078 current_funccal = funccal_stack->top_funccal;
3079 funccal_stack = funccal_stack->next;
3080 }
3081}
3082
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003083 funccall_T *
3084get_current_funccal(void)
3085{
3086 return current_funccal;
3087}
3088
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003089/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003090 * Return TRUE when currently at the script level:
3091 * - not in a function
3092 * - not executing an autocommand
3093 * Note that when an autocommand sources a script the result is FALSE;
3094 */
3095 int
3096at_script_level(void)
3097{
3098 return current_funccal == NULL && autocmd_match == NULL;
3099}
3100
3101/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003102 * Mark all functions of script "sid" as deleted.
3103 */
3104 void
3105delete_script_functions(int sid)
3106{
3107 hashitem_T *hi;
3108 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003109 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003110 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003111 size_t len;
3112
3113 buf[0] = K_SPECIAL;
3114 buf[1] = KS_EXTRA;
3115 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003116 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003117 len = STRLEN(buf);
3118
Bram Moolenaarce658352020-07-31 23:47:12 +02003119 while (todo > 0)
3120 {
3121 todo = func_hashtab.ht_used;
3122 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3123 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003124 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003125 fp = HI2UF(hi);
3126 if (STRNCMP(fp->uf_name, buf, len) == 0)
3127 {
3128 int changed = func_hashtab.ht_changed;
3129
3130 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003131
3132 if (fp->uf_calls > 0)
3133 {
3134 // Function is executing, don't free it but do remove
3135 // it from the hashtable.
3136 if (func_remove(fp))
3137 fp->uf_refcount--;
3138 }
3139 else
3140 {
3141 func_clear(fp, TRUE);
3142 // When clearing a function another function can be
3143 // cleared as a side effect. When that happens start
3144 // over.
3145 if (changed != func_hashtab.ht_changed)
3146 break;
3147 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003148 }
3149 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003150 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003151 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003152}
3153
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003154#if defined(EXITFREE) || defined(PROTO)
3155 void
3156free_all_functions(void)
3157{
3158 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003159 ufunc_T *fp;
3160 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003161 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003162 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003163
Bram Moolenaare38eab22019-12-05 21:50:01 +01003164 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003165 while (current_funccal != NULL)
3166 {
3167 clear_tv(current_funccal->rettv);
3168 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003169 if (current_funccal == NULL && funccal_stack != NULL)
3170 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003171 }
3172
Bram Moolenaare38eab22019-12-05 21:50:01 +01003173 // First clear what the functions contain. Since this may lower the
3174 // reference count of a function, it may also free a function and change
3175 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003176 while (todo > 0)
3177 {
3178 todo = func_hashtab.ht_used;
3179 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3180 if (!HASHITEM_EMPTY(hi))
3181 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 // clear the def function index now
3183 fp = HI2UF(hi);
3184 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003185 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186
Bram Moolenaare38eab22019-12-05 21:50:01 +01003187 // Only free functions that are not refcounted, those are
3188 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003189 if (func_name_refcount(fp->uf_name))
3190 ++skipped;
3191 else
3192 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003193 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003194 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003195 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003196 {
3197 skipped = 0;
3198 break;
3199 }
3200 }
3201 --todo;
3202 }
3203 }
3204
Bram Moolenaare38eab22019-12-05 21:50:01 +01003205 // Now actually free the functions. Need to start all over every time,
3206 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003207 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003208 while (func_hashtab.ht_used > skipped)
3209 {
3210 todo = func_hashtab.ht_used;
3211 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003212 if (!HASHITEM_EMPTY(hi))
3213 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003214 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003215 // Only free functions that are not refcounted, those are
3216 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003217 fp = HI2UF(hi);
3218 if (func_name_refcount(fp->uf_name))
3219 ++skipped;
3220 else
3221 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003222 if (func_free(fp, FALSE) == OK)
3223 {
3224 skipped = 0;
3225 break;
3226 }
3227 // did not actually free it
3228 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003229 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003230 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003231 }
3232 if (skipped == 0)
3233 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234
3235 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003236}
3237#endif
3238
3239/*
3240 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003241 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3242 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003243 * "len" is the length of "name", or -1 for NUL terminated.
3244 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003246builtin_function(char_u *name, int len)
3247{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003248 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003249
Bram Moolenaara26b9702020-04-18 19:53:28 +02003250 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003251 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003252 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3253 {
3254 if (name[i] == AUTOLOAD_CHAR)
3255 return FALSE;
3256 if (!eval_isnamec(name[i]))
3257 {
3258 // "name.something" is not a builtin function
3259 if (name[i] == '.')
3260 return FALSE;
3261 break;
3262 }
3263 }
3264 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003265}
3266
3267 int
3268func_call(
3269 char_u *name,
3270 typval_T *args,
3271 partial_T *partial,
3272 dict_T *selfdict,
3273 typval_T *rettv)
3274{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003275 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003276 listitem_T *item;
3277 typval_T argv[MAX_FUNC_ARGS + 1];
3278 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003279 int r = 0;
3280
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003281 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003282 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003283 {
3284 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3285 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003286 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003287 break;
3288 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003289 // Make a copy of each argument. This is needed to be able to set
3290 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003291 copy_tv(&item->li_tv, &argv[argc++]);
3292 }
3293
3294 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003295 {
3296 funcexe_T funcexe;
3297
Bram Moolenaara80faa82020-04-12 19:37:17 +02003298 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003299 funcexe.fe_firstline = curwin->w_cursor.lnum;
3300 funcexe.fe_lastline = curwin->w_cursor.lnum;
3301 funcexe.fe_evaluate = TRUE;
3302 funcexe.fe_partial = partial;
3303 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003304 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3305 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003306
Bram Moolenaare38eab22019-12-05 21:50:01 +01003307 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003308 while (argc > 0)
3309 clear_tv(&argv[--argc]);
3310
3311 return r;
3312}
3313
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003314static int callback_depth = 0;
3315
3316 int
3317get_callback_depth(void)
3318{
3319 return callback_depth;
3320}
3321
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003322/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003323 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003324 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003325 */
3326 int
3327call_callback(
3328 callback_T *callback,
3329 int len, // length of "name" or -1 to use strlen()
3330 typval_T *rettv, // return value goes here
3331 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003332 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003333 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003334{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003335 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003336 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003337
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003338 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3339 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02003340 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003341 funcexe.fe_evaluate = TRUE;
3342 funcexe.fe_partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003343 ++callback_depth;
3344 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3345 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003346
3347 // When a :def function was called that uses :try an error would be turned
3348 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003349 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003350 {
3351 need_rethrow = FALSE;
3352 handle_did_throw();
3353 }
3354
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003355 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003356}
3357
3358/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003359 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003360 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003361 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3362 */
3363 varnumber_T
3364call_callback_retnr(
3365 callback_T *callback,
3366 int argcount, // number of "argvars"
3367 typval_T *argvars) // vars for arguments, must have "argcount"
3368 // PLUS ONE elements!
3369{
3370 typval_T rettv;
3371 varnumber_T retval;
3372
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003373 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003374 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003375
3376 retval = tv_get_number_chk(&rettv, NULL);
3377 clear_tv(&rettv);
3378 return retval;
3379}
3380
3381/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 * Give an error message for the result of a function.
3383 * Nothing if "error" is FCERR_NONE.
3384 */
3385 void
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003386user_func_error(int error, char_u *name, funcexe_T *funcexe)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003387{
3388 switch (error)
3389 {
3390 case FCERR_UNKNOWN:
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003391 if (funcexe->fe_found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003392 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003393 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003394 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 break;
3396 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003397 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 break;
3399 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003400 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 break;
3402 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003403 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404 break;
3405 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003406 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 break;
3408 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003409 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 break;
3411 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003412 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3413 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 break;
3415 }
3416}
3417
3418/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003419 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003420 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003421 * Return FAIL when the function can't be called, OK otherwise.
3422 * Also returns OK when an error was encountered while executing the function.
3423 */
3424 int
3425call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003426 char_u *funcname, // name of the function
3427 int len, // length of "name" or -1 to use strlen()
3428 typval_T *rettv, // return value goes here
3429 int argcount_in, // number of "argvars"
3430 typval_T *argvars_in, // vars for arguments, must have "argcount"
3431 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003432 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003433{
3434 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01003435 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003436 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003437 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003438 char_u fname_buf[FLEN_FIXED + 1];
3439 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003440 char_u *fname = NULL;
3441 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003442 int argcount = argcount_in;
3443 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003444 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003445 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003446 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003447 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003448 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003449 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003450 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003451 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003452
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003453 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3454 // even when call_func() returns FAIL.
3455 rettv->v_type = VAR_UNKNOWN;
3456
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003457 if (partial != NULL)
3458 fp = partial->pt_func;
3459 if (fp == NULL)
3460 {
3461 // Make a copy of the name, if it comes from a funcref variable it
3462 // could be changed or deleted in the called function.
3463 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3464 if (name == NULL)
3465 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003466
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003467 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3468 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003469
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003470 if (funcexe->fe_doesrange != NULL)
3471 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003472
3473 if (partial != NULL)
3474 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003475 // When the function has a partial with a dict and there is a dict
3476 // argument, use the dict argument. That is backwards compatible.
3477 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003478 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003479 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003480 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003481 {
3482 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003483 {
3484 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3485 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003486 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003487 goto theend;
3488 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003489 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003490 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003491 for (i = 0; i < argcount_in; ++i)
3492 argv[i + argv_clear] = argvars_in[i];
3493 argvars = argv;
3494 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003495
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003496 if (funcexe->fe_check_type != NULL
3497 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003498 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003499 // Now funcexe->fe_check_type is missing the added arguments,
3500 // make a copy of the type with the correction.
3501 check_type = *funcexe->fe_check_type;
3502 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003503 check_type.tt_args = check_type_args;
3504 CLEAR_FIELD(check_type_args);
3505 for (i = 0; i < check_type.tt_argcount; ++i)
3506 check_type_args[i + partial->pt_argc] =
3507 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003508 check_type.tt_argcount += partial->pt_argc;
3509 check_type.tt_min_argcount += partial->pt_argc;
3510 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003511 }
3512 }
3513
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003514 if (error == FCERR_NONE && funcexe->fe_check_type != NULL
3515 && funcexe->fe_evaluate)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003516 {
3517 // Check that the argument types are OK for the types of the funcref.
Bram Moolenaarc84287d2022-01-16 18:06:21 +00003518 if (check_argument_types(funcexe->fe_check_type,
3519 argvars, argcount, funcexe->fe_basetv,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003520 (name != NULL) ? name : funcname) == FAIL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003521 error = FCERR_OTHER;
3522 }
3523
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003524 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003525 {
3526 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003527 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003528
Bram Moolenaar333894b2020-08-01 18:53:07 +02003529 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003530 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003531 {
3532 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003533 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003534 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003535
Bram Moolenaare38eab22019-12-05 21:50:01 +01003536 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003537 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003538 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003539
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003540 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003541 {
3542 /*
3543 * User defined function.
3544 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003545 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003546 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003547 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003548 if (fp != NULL && !is_global && in_vim9script()
3549 && func_requires_g_prefix(fp))
3550 // In Vim9 script g: is required to find a global
3551 // non-autoload function.
3552 fp = NULL;
3553 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003554
Bram Moolenaare38eab22019-12-05 21:50:01 +01003555 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003556 if (fp == NULL
3557 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003558 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003559 && !aborting())
3560 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003561 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003562 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003563 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003564 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003565 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3566 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003567 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003568 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003569 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003570 if (fp == NULL)
3571 {
3572 char_u *p = untrans_function_name(rfname);
3573
3574 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003575 // Don't do this if the name starts with "s:".
3576 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003577 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003578 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003579
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003580 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003581 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003582 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003583 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003584 if (funcexe->fe_argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003585 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003586 argcount = funcexe->fe_argv_func(argcount, argvars,
3587 argv_clear, fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003588
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003589 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003590 {
3591 // Method call: base->Method()
3592 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003593 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003594 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003595 argvars = argv;
3596 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003597 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003598
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599 error = call_user_func_check(fp, argcount, argvars, rettv,
3600 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003601 }
3602 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003603 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003604 {
3605 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003606 * expr->method(): Find the method name in the table, call its
3607 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003608 */
3609 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003610 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003611 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003612 else
3613 {
3614 /*
3615 * Find the function name in the table, call its implementation.
3616 */
3617 error = call_internal_func(fname, argcount, argvars, rettv);
3618 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003619
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003620 /*
3621 * The function call (or "FuncUndefined" autocommand sequence) might
3622 * have been aborted by an error, an interrupt, or an explicitly thrown
3623 * exception that has not been caught so far. This situation can be
3624 * tested for by calling aborting(). For an error in an internal
3625 * function or for the "E132" error in call_user_func(), however, the
3626 * throw point at which the "force_abort" flag (temporarily reset by
3627 * emsg()) is normally updated has not been reached yet. We need to
3628 * update that flag first to make aborting() reliable.
3629 */
3630 update_force_abort();
3631 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003632 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003633 ret = OK;
3634
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003635theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003636 /*
3637 * Report an error unless the argument evaluation or function call has been
3638 * cancelled due to an aborting error, an interrupt, or an exception.
3639 */
3640 if (!aborting())
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003641 user_func_error(error, (name != NULL) ? name : funcname, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003642
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003643 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003644 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003645 clear_tv(&argv[--argv_clear + argv_base]);
3646
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003647 vim_free(tofree);
3648 vim_free(name);
3649
3650 return ret;
3651}
3652
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003653 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654printable_func_name(ufunc_T *fp)
3655{
3656 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
3657}
3658
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003659/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003660 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003661 */
3662 static void
3663list_func_head(ufunc_T *fp, int indent)
3664{
3665 int j;
3666
3667 msg_start();
3668 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003669 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003670 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003671 msg_puts("def ");
3672 else
3673 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003675 msg_putchar('(');
3676 for (j = 0; j < fp->uf_args.ga_len; ++j)
3677 {
3678 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003679 msg_puts(", ");
3680 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 if (fp->uf_arg_types != NULL)
3682 {
3683 char *tofree;
3684
3685 msg_puts(": ");
3686 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
3687 vim_free(tofree);
3688 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003689 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
3690 {
3691 msg_puts(" = ");
3692 msg_puts(((char **)(fp->uf_def_args.ga_data))
3693 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
3694 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003695 }
3696 if (fp->uf_varargs)
3697 {
3698 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003699 msg_puts(", ");
3700 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003701 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 if (fp->uf_va_name != NULL)
3703 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01003704 if (!fp->uf_varargs)
3705 {
3706 if (j)
3707 msg_puts(", ");
3708 msg_puts("...");
3709 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02003711 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 {
3713 char *tofree;
3714
3715 msg_puts(": ");
3716 msg_puts(type_name(fp->uf_va_type, &tofree));
3717 vim_free(tofree);
3718 }
3719 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003720 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003721
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003722 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003723 {
3724 if (fp->uf_ret_type != &t_void)
3725 {
3726 char *tofree;
3727
3728 msg_puts(": ");
3729 msg_puts(type_name(fp->uf_ret_type, &tofree));
3730 vim_free(tofree);
3731 }
3732 }
3733 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003734 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003735 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003736 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003737 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003738 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003739 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003740 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003741 msg_clr_eos();
3742 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003743 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003744}
3745
3746/*
3747 * Get a function name, translating "<SID>" and "<SNR>".
3748 * Also handles a Funcref in a List or Dictionary.
3749 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003750 * Set "*is_global" to TRUE when the function must be global, unless
3751 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003752 * flags:
3753 * TFN_INT: internal function name OK
3754 * TFN_QUIET: be quiet
3755 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003756 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003757 * Advances "pp" to just after the function name (if no error).
3758 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003759 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003760trans_function_name(
3761 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003762 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003763 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003764 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003765 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003766 partial_T **partial, // return: partial of a FuncRef
3767 type_T **type) // return: type of funcref if not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003768{
3769 char_u *name = NULL;
3770 char_u *start;
3771 char_u *end;
3772 int lead;
3773 char_u sid_buf[20];
3774 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003776 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003777 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003778 int vim9script = in_vim9script();
3779 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003780
3781 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003782 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003783 start = *pp;
3784
Bram Moolenaare38eab22019-12-05 21:50:01 +01003785 // Check for hard coded <SNR>: already translated function ID (from a user
3786 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003787 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
3788 && (*pp)[2] == (int)KE_SNR)
3789 {
3790 *pp += 3;
3791 len = get_id_len(pp) + 3;
3792 return vim_strnsave(start, len);
3793 }
3794
Bram Moolenaare38eab22019-12-05 21:50:01 +01003795 // A name starting with "<SID>" or "<SNR>" is local to a script. But
3796 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003797 lead = eval_fname_script(start);
3798 if (lead > 2)
3799 start += lead;
3800
Bram Moolenaare38eab22019-12-05 21:50:01 +01003801 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01003802 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003803 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003804 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00003805 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003806 {
3807 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00003808 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003809 goto theend;
3810 }
3811 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
3812 {
3813 /*
3814 * Report an invalid expression in braces, unless the expression
3815 * evaluation has been cancelled due to an aborting error, an
3816 * interrupt, or an exception.
3817 */
3818 if (!aborting())
3819 {
3820 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003821 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003822 }
3823 else
3824 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
3825 goto theend;
3826 }
3827
3828 if (lv.ll_tv != NULL)
3829 {
3830 if (fdp != NULL)
3831 {
3832 fdp->fd_dict = lv.ll_dict;
3833 fdp->fd_newkey = lv.ll_newkey;
3834 lv.ll_newkey = NULL;
3835 fdp->fd_di = lv.ll_di;
3836 }
3837 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
3838 {
3839 name = vim_strsave(lv.ll_tv->vval.v_string);
3840 *pp = end;
3841 }
3842 else if (lv.ll_tv->v_type == VAR_PARTIAL
3843 && lv.ll_tv->vval.v_partial != NULL)
3844 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003845 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003846 *pp = end;
3847 if (partial != NULL)
3848 *partial = lv.ll_tv->vval.v_partial;
3849 }
3850 else
3851 {
3852 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
3853 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00003854 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003855 else
3856 *pp = end;
3857 name = NULL;
3858 }
3859 goto theend;
3860 }
3861
3862 if (lv.ll_name == NULL)
3863 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003864 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003865 *pp = end;
3866 goto theend;
3867 }
3868
Bram Moolenaare38eab22019-12-05 21:50:01 +01003869 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003870 if (lv.ll_exp_name != NULL)
3871 {
3872 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003873 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00003874 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003875 if (name == lv.ll_exp_name)
3876 name = NULL;
3877 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00003878 else if (lv.ll_sid > 0)
3879 {
3880 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
3881 int cc = *lv.ll_name_end;
3882
3883 // function in another script. Prefix <SNR>99_ or the autoload prefix.
3884 *lv.ll_name_end = NUL;
3885 if (si->sn_autoload_prefix != NULL)
3886 {
3887 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
3888 }
3889 else
3890 {
3891 sid_buf[0] = K_SPECIAL;
3892 sid_buf[1] = KS_EXTRA;
3893 sid_buf[2] = (int)KE_SNR;
3894 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00003895 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00003896 name = concat_str(sid_buf, lv.ll_name);
3897 }
3898 *lv.ll_name_end = cc;
3899 *pp = end;
3900 goto theend;
3901 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003902 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003903 {
3904 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003905 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00003906 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003907 if (name == *pp)
3908 name = NULL;
3909 }
3910 if (name != NULL)
3911 {
3912 name = vim_strsave(name);
3913 *pp = end;
3914 if (STRNCMP(name, "<SNR>", 5) == 0)
3915 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003916 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003917 name[0] = K_SPECIAL;
3918 name[1] = KS_EXTRA;
3919 name[2] = (int)KE_SNR;
3920 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
3921 }
3922 goto theend;
3923 }
3924
3925 if (lv.ll_exp_name != NULL)
3926 {
3927 len = (int)STRLEN(lv.ll_exp_name);
3928 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
3929 && STRNCMP(lv.ll_name, "s:", 2) == 0)
3930 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003931 // When there was "s:" already or the name expanded to get a
3932 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003933 lv.ll_name += 2;
3934 len -= 2;
3935 lead = 2;
3936 }
3937 }
3938 else
3939 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003940 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003941 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003942 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003943 if (lv.ll_name[0] == 'g')
3944 {
3945 if (is_global != NULL)
3946 {
3947 *is_global = TRUE;
3948 }
3949 else
3950 {
3951 // dropping "g:" without setting "is_global" won't work in
3952 // Vim9script, put it back later
3953 prefix_g = TRUE;
3954 extra = 2;
3955 }
3956 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003957 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003958 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003959 len = (int)(end - lv.ll_name);
3960 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003961 if (len <= 0)
3962 {
3963 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00003964 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003965 goto theend;
3966 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003967
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003968 // In Vim9 script a user function is script-local by default, unless it
3969 // starts with a lower case character: dict.func().
Bram Moolenaar4525a572022-02-13 11:57:33 +00003970 vim9_local = ASCII_ISUPPER(*start) && vim9script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003972 /*
3973 * Copy the function name to allocated memory.
3974 * Accept <SID>name() inside a script, translate into <SNR>123_name().
3975 * Accept <SNR>123_name() outside a script.
3976 */
3977 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003978 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00003979 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003980 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003981 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00003982 {
Kota Kato948a3892022-08-16 16:09:59 +01003983 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
3984 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00003985 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01003986 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00003987 goto theend;
3988 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00003990 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003991 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003993 || eval_fname_sid(*pp))
3994 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003996 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003997 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00003998 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003999 goto theend;
4000 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004001 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004002 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 extra = 3 + (int)STRLEN(sid_buf);
4004 else
4005 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004006 }
4007 }
Bram Moolenaar22f17a22021-06-21 20:48:58 +02004008 else if (!(flags & TFN_INT) && (builtin_function(lv.ll_name, len)
Bram Moolenaar4525a572022-02-13 11:57:33 +00004009 || (vim9script && *lv.ll_name == '_')))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004010 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004011 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004012 : e_function_name_must_start_with_capital_or_s_str),
4013 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004014 goto theend;
4015 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004016 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004017 {
4018 char_u *cp = vim_strchr(lv.ll_name, ':');
4019
4020 if (cp != NULL && cp < end)
4021 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004022 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004023 goto theend;
4024 }
4025 }
4026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004028 if (name != NULL)
4029 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004030 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004031 {
4032 name[0] = K_SPECIAL;
4033 name[1] = KS_EXTRA;
4034 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004035 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004036 STRCPY(name + 3, sid_buf);
4037 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004038 else if (prefix_g)
4039 {
4040 name[0] = 'g';
4041 name[1] = ':';
4042 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4044 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004045 }
4046 *pp = end;
4047
4048theend:
4049 clear_lval(&lv);
4050 return name;
4051}
4052
4053/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004054 * Assuming "name" is the result of trans_function_name() and it was prefixed
4055 * to use the script-local name, return the unmodified name (points into
4056 * "name"). Otherwise return NULL.
4057 * This can be used to first search for a script-local function and fall back
4058 * to the global function if not found.
4059 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004060 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004061untrans_function_name(char_u *name)
4062{
4063 char_u *p;
4064
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004065 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004066 {
4067 p = vim_strchr(name, '_');
4068 if (p != NULL)
4069 return p + 1;
4070 }
4071 return NULL;
4072}
4073
4074/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004075 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4076 * current script ID and returns the expanded function name. The caller should
4077 * free the returned name. If not called from a script context or the function
4078 * name doesn't start with these prefixes, then returns NULL.
4079 * This doesn't check whether the script-local function exists or not.
4080 */
4081 char_u *
4082get_scriptlocal_funcname(char_u *funcname)
4083{
4084 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004085 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004086 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004087 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004088
4089 if (funcname == NULL)
4090 return NULL;
4091
4092 if (STRNCMP(funcname, "s:", 2) != 0
4093 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004094 {
4095 ufunc_T *ufunc;
4096
4097 // The function name does not have a script-local prefix. Try finding
4098 // it when in a Vim9 script and there is no "g:" prefix.
4099 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4100 return NULL;
4101 ufunc = find_func(funcname, FALSE);
4102 if (ufunc == NULL || func_is_global(ufunc)
4103 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4104 return NULL;
4105 ++p;
4106 off = 0;
4107 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004108 else
4109 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004110
4111 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4112 {
4113 emsg(_(e_using_sid_not_in_script_context));
4114 return NULL;
4115 }
4116 // Expand s: prefix into <SNR>nr_<name>
4117 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4118 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004119 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004120 if (newname == NULL)
4121 return NULL;
4122 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004123 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004124
4125 return newname;
4126}
4127
4128/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004129 * Return script-local "fname" with the 3-byte sequence replaced by
4130 * printable <SNR> in allocated memory.
4131 */
4132 char_u *
4133alloc_printable_func_name(char_u *fname)
4134{
4135 char_u *n = alloc(STRLEN(fname + 3) + 6);
4136
4137 if (n != NULL)
4138 {
4139 STRCPY(n, "<SNR>");
4140 STRCPY(n + 5, fname + 3);
4141 }
4142 return n;
4143}
4144
4145/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004146 * Call trans_function_name(), except that a lambda is returned as-is.
4147 * Returns the name in allocated memory.
4148 */
4149 char_u *
4150save_function_name(
4151 char_u **name,
4152 int *is_global,
4153 int skip,
4154 int flags,
4155 funcdict_T *fudi)
4156{
4157 char_u *p = *name;
4158 char_u *saved;
4159
4160 if (STRNCMP(p, "<lambda>", 8) == 0)
4161 {
4162 p += 8;
4163 (void)getdigits(&p);
4164 saved = vim_strnsave(*name, p - *name);
4165 if (fudi != NULL)
4166 CLEAR_POINTER(fudi);
4167 }
4168 else
4169 saved = trans_function_name(&p, is_global, skip,
4170 flags, fudi, NULL, NULL);
4171 *name = p;
4172 return saved;
4173}
4174
4175/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004176 * List functions. When "regmatch" is NULL all of then.
4177 * Otherwise functions matching "regmatch".
4178 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004179 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004180list_functions(regmatch_T *regmatch)
4181{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004182 int changed = func_hashtab.ht_changed;
4183 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004184 hashitem_T *hi;
4185
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004186 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004187 {
4188 if (!HASHITEM_EMPTY(hi))
4189 {
4190 ufunc_T *fp = HI2UF(hi);
4191
4192 --todo;
4193 if ((fp->uf_flags & FC_DEAD) == 0
4194 && (regmatch == NULL
4195 ? !message_filtered(fp->uf_name)
4196 && !func_name_refcount(fp->uf_name)
4197 : !isdigit(*fp->uf_name)
4198 && vim_regexec(regmatch, fp->uf_name, 0)))
4199 {
4200 list_func_head(fp, FALSE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004201 if (changed != func_hashtab.ht_changed)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004202 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00004203 emsg(_(e_function_list_was_modified));
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004204 return;
4205 }
4206 }
4207 }
4208 }
4209}
4210
4211/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004212 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004213 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4214 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004215 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004216 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004217 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004218 ufunc_T *
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004219define_function(exarg_T *eap, char_u *name_arg, garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004220{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004221 int j;
4222 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004223 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004224 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004225 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004226 char_u *p;
4227 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004228 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004229 char_u *line_arg = NULL;
4230 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004232 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004233 garray_T newlines;
4234 int varargs = FALSE;
4235 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004237 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004238 int fp_allocated = FALSE;
4239 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004240 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004241 dictitem_T *v;
4242 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004243 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004244 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004245 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004246 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004247 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004248 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004249
4250 /*
4251 * ":function" without argument: list functions.
4252 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004253 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004254 {
4255 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004256 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004257 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004258 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004259 }
4260
4261 /*
4262 * ":function /pat": list functions matching pattern.
4263 */
4264 if (*eap->arg == '/')
4265 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004266 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004267 if (!eap->skip)
4268 {
4269 regmatch_T regmatch;
4270
4271 c = *p;
4272 *p = NUL;
4273 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4274 *p = c;
4275 if (regmatch.regprog != NULL)
4276 {
4277 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004278 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004279 vim_regfree(regmatch.regprog);
4280 }
4281 }
4282 if (*p == '/')
4283 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004284 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004285 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004286 }
4287
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 ga_init(&newargs);
4289 ga_init(&argtypes);
4290 ga_init(&default_args);
4291
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004292 /*
4293 * Get the function name. There are these situations:
4294 * func normal function name
4295 * "name" == func, "fudi.fd_dict" == NULL
4296 * dict.func new dictionary entry
4297 * "name" == NULL, "fudi.fd_dict" set,
4298 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4299 * dict.func existing dict entry with a Funcref
4300 * "name" == func, "fudi.fd_dict" set,
4301 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4302 * dict.func existing dict entry that's not a Funcref
4303 * "name" == NULL, "fudi.fd_dict" set,
4304 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4305 * s:func script-local function name
4306 * g:func global function name, same as "func"
4307 */
4308 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004309 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004310 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004311 // nested function, argument is (args).
4312 paren = TRUE;
4313 CLEAR_FIELD(fudi);
4314 }
4315 else
4316 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004317 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004318 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004319 if (p[0] == 's' && p[1] == ':')
4320 {
4321 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4322 return NULL;
4323 }
4324 p = to_name_end(p, TRUE);
4325 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4326 {
4327 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4328 eap->arg);
4329 return NULL;
4330 }
4331 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004332 }
4333
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004334 name = save_function_name(&p, &is_global, eap->skip,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004335 TFN_NO_AUTOLOAD | TFN_NEW_FUNC, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004336 paren = (vim_strchr(p, '(') != NULL);
4337 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004338 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004339 /*
4340 * Return on an invalid expression in braces, unless the expression
4341 * evaluation has been cancelled due to an aborting error, an
4342 * interrupt, or an exception.
4343 */
4344 if (!aborting())
4345 {
4346 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004347 semsg(_(e_key_not_present_in_dictionary), fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004348 vim_free(fudi.fd_newkey);
4349 return NULL;
4350 }
4351 else
4352 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004353 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004354
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004355 // For "export def FuncName()" in an autoload script the function name
4356 // is stored with the legacy autoload name "dir#script#FuncName" so
4357 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004358 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004359 {
4360 char_u *prefixed = may_prefix_autoload(name);
4361
4362 if (prefixed != NULL && prefixed != name)
4363 {
4364 vim_free(name);
4365 name = prefixed;
4366 }
4367 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004368 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004369 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004370 {
4371 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4372 goto ret_free;
4373 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004374 }
4375
Bram Moolenaare38eab22019-12-05 21:50:01 +01004376 // An error in a function call during evaluation of an expression in magic
4377 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004378 saved_did_emsg = did_emsg;
4379 did_emsg = FALSE;
4380
4381 /*
4382 * ":function func" with only function name: list function.
4383 */
4384 if (!paren)
4385 {
4386 if (!ends_excmd(*skipwhite(p)))
4387 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004388 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004389 goto ret_free;
4390 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004391 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004392 if (eap->nextcmd != NULL)
4393 *p = NUL;
4394 if (!eap->skip && !got_int)
4395 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004396 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004397 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4398 {
4399 char_u *up = untrans_function_name(name);
4400
4401 // With Vim9 script the name was made script-local, if not
4402 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004403 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004404 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004405 }
4406
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004407 if (fp != NULL)
4408 {
4409 list_func_head(fp, TRUE);
4410 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4411 {
4412 if (FUNCLINE(fp, j) == NULL)
4413 continue;
4414 msg_putchar('\n');
4415 msg_outnum((long)(j + 1));
4416 if (j < 9)
4417 msg_putchar(' ');
4418 if (j < 99)
4419 msg_putchar(' ');
4420 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004421 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004422 ui_breakcheck();
4423 }
4424 if (!got_int)
4425 {
4426 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004427 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004428 msg_puts(" enddef");
4429 else
4430 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004431 }
4432 }
4433 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004434 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004435 }
4436 goto ret_free;
4437 }
4438
4439 /*
4440 * ":function name(arg1, arg2)" Define function.
4441 */
4442 p = skipwhite(p);
4443 if (*p != '(')
4444 {
4445 if (!eap->skip)
4446 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004447 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004448 goto ret_free;
4449 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004450 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004451 if (vim_strchr(p, '(') != NULL)
4452 p = vim_strchr(p, '(');
4453 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004454
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004455 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4456 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004457 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004458 goto ret_free;
4459 }
4460
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004461 // In Vim9 script only global functions can be redefined.
4462 if (vim9script && eap->forceit && !is_global)
4463 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004464 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004465 goto ret_free;
4466 }
4467
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004468 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004469
Bram Moolenaar04b12692020-05-04 23:24:44 +02004470 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004471 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004472 // Check the name of the function. Unless it's a dictionary function
4473 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004474 if (name != NULL)
4475 arg = name;
4476 else
4477 arg = fudi.fd_newkey;
4478 if (arg != NULL && (fudi.fd_di == NULL
4479 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4480 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4481 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004482 char_u *name_base = arg;
4483 int i;
4484
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004485 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004486 {
4487 name_base = vim_strchr(arg, '_');
4488 if (name_base == NULL)
4489 name_base = arg + 3;
4490 else
4491 ++name_base;
4492 }
4493 for (i = 0; name_base[i] != NUL && (i == 0
4494 ? eval_isnamec1(name_base[i])
4495 : eval_isnamec(name_base[i])); ++i)
4496 ;
4497 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004498 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004499
4500 // In Vim9 script a function cannot have the same name as a
4501 // variable.
4502 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004503 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4504 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004505 + EVAL_VAR_NO_FUNC) == OK)
4506 {
4507 semsg(_(e_redefining_script_item_str), name_base);
4508 goto ret_free;
4509 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004510 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004511 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004512 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004513 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004514 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004515 goto ret_free;
4516 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004517 }
4518
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004519 // This may get more lines and make the pointers into the first line
4520 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004521 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004523 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004524 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004525 eap, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004526 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004527 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004528
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004530 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004532 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004533 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004534 if (*p != ':')
4535 {
4536 semsg(_(e_no_white_space_allowed_before_colon_str), p);
4537 p = skipwhite(p);
4538 }
4539 else if (!IS_WHITE_OR_NUL(p[1]))
4540 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004541 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02004542 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004544 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004545 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01004546 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004547 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004548 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004550 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004551 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02004552 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004553 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004554 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02004555 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004556 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557 else
4558 // find extra arguments "range", "dict", "abort" and "closure"
4559 for (;;)
4560 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01004561 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562 p = skipwhite(p);
4563 if (STRNCMP(p, "range", 5) == 0)
4564 {
4565 flags |= FC_RANGE;
4566 p += 5;
4567 }
4568 else if (STRNCMP(p, "dict", 4) == 0)
4569 {
4570 flags |= FC_DICT;
4571 p += 4;
4572 }
4573 else if (STRNCMP(p, "abort", 5) == 0)
4574 {
4575 flags |= FC_ABORT;
4576 p += 5;
4577 }
4578 else if (STRNCMP(p, "closure", 7) == 0)
4579 {
4580 flags |= FC_CLOSURE;
4581 p += 7;
4582 if (current_funccal == NULL)
4583 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004584 emsg_funcname(e_closure_function_should_not_be_at_top_level,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 name == NULL ? (char_u *)"" : name);
4586 goto erret;
4587 }
4588 }
4589 else
4590 break;
4591 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004592
Bram Moolenaare38eab22019-12-05 21:50:01 +01004593 // When there is a line break use what follows for the function body.
4594 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004595 if (*p == '\n')
4596 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004597 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02004598 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
4599 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01004600 && !(VIM_ISWHITE(*whitep) && *p == '#'
4601 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02004602 && !eap->skip
4603 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004604 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004605
4606 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
4608 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004609 */
4610 if (KeyTyped)
4611 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004612 // Check if the function already exists, don't let the user type the
4613 // whole function before telling him it doesn't work! For a script we
4614 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004615 if (!eap->skip && !eap->forceit)
4616 {
4617 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004618 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004619 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00004620 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004621 }
4622
4623 if (!eap->skip && did_emsg)
4624 goto erret;
4625
Bram Moolenaare38eab22019-12-05 21:50:01 +01004626 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004627 cmdline_row = msg_row;
4628 }
4629
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004630 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004631 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004632
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004633 // Do not define the function when getting the body fails and when
4634 // skipping.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004635 if (get_function_body(eap, &newlines, line_arg, lines_to_free) == FAIL
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004636 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004637 goto erret;
4638
4639 /*
4640 * If there are no errors, add the function
4641 */
4642 if (fudi.fd_dict == NULL)
4643 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004645 char_u *find_name = name;
4646 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004647 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004649 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004650 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004651 var_conflict = TRUE;
4652
4653 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
4654 {
4655 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
4656
4657 if (si->sn_autoload_prefix != NULL)
4658 {
4659 if (is_export)
4660 {
4661 find_name = name + STRLEN(si->sn_autoload_prefix);
4662 v = find_var(find_name, &ht, TRUE);
4663 if (v != NULL)
4664 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004665 // Only check if the function already exists in the script,
4666 // global functions can be shadowed.
4667 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004668 }
4669 else
4670 {
4671 char_u *prefixed = may_prefix_autoload(name);
4672
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00004673 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004674 {
4675 v = find_var(prefixed, &ht, TRUE);
4676 if (v != NULL)
4677 var_conflict = TRUE;
4678 vim_free(prefixed);
4679 }
4680 }
4681 }
4682 }
4683 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004684 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004685 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004686 goto erret;
4687 }
4688
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004689 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02004690 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004691 {
Bram Moolenaareef21022020-08-01 22:16:43 +02004692 char_u *uname = untrans_function_name(name);
4693
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004694 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004695 }
4696
4697 if (fp != NULL || import != NULL)
4698 {
4699 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004701 // Function can be replaced with "function!" and when sourcing the
4702 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02004703 // A name that is used by an import can not be overruled.
4704 if (import != NULL
4705 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004706 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02004707 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004708 {
Bram Moolenaard604d782021-11-20 21:46:20 +00004709 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02004710 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004711 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02004712 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00004713 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004714 goto erret;
4715 }
4716 if (fp->uf_calls > 0)
4717 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004718 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00004719 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004720 goto erret;
4721 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004722 if (fp->uf_refcount > 1)
4723 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004724 // This function is referenced somewhere, don't redefine it but
4725 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004726 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004727 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004728 fp = NULL;
4729 overwrite = TRUE;
4730 }
4731 else
4732 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004733 char_u *exp_name = fp->uf_name_exp;
4734
4735 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01004736 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004737 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004738 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004739 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004741#ifdef FEAT_PROFILE
4742 fp->uf_profiling = FALSE;
4743 fp->uf_prof_initialized = FALSE;
4744#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01004745 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004746 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004747 }
4748 }
4749 else
4750 {
4751 char numbuf[20];
4752
4753 fp = NULL;
4754 if (fudi.fd_newkey == NULL && !eap->forceit)
4755 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004756 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004757 goto erret;
4758 }
4759 if (fudi.fd_di == NULL)
4760 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004761 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02004762 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004763 goto erret;
4764 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004765 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02004766 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004767 goto erret;
4768
Bram Moolenaare38eab22019-12-05 21:50:01 +01004769 // Give the function a sequential number. Can only be used with a
4770 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004771 vim_free(name);
4772 sprintf(numbuf, "%d", ++func_nr);
4773 name = vim_strsave((char_u *)numbuf);
4774 if (name == NULL)
4775 goto erret;
4776 }
4777
4778 if (fp == NULL)
4779 {
4780 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
4781 {
4782 int slen, plen;
4783 char_u *scriptname;
4784
Bram Moolenaare38eab22019-12-05 21:50:01 +01004785 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004786 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004787 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004788 {
4789 scriptname = autoload_name(name);
4790 if (scriptname != NULL)
4791 {
4792 p = vim_strchr(scriptname, '/');
4793 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004794 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004795 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004796 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004797 j = OK;
4798 vim_free(scriptname);
4799 }
4800 }
4801 if (j == FAIL)
4802 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004803 linenr_T save_lnum = SOURCING_LNUM;
4804
4805 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00004806 semsg(_(e_function_name_does_not_match_script_file_name_str),
4807 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004808 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004809 goto erret;
4810 }
4811 }
4812
Bram Moolenaar47ed5532019-08-08 20:49:14 +02004813 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004814 if (fp == NULL)
4815 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004816 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004817
4818 if (fudi.fd_dict != NULL)
4819 {
4820 if (fudi.fd_di == NULL)
4821 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004822 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004823 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
4824 if (fudi.fd_di == NULL)
4825 {
4826 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004827 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004828 goto erret;
4829 }
4830 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
4831 {
4832 vim_free(fudi.fd_di);
4833 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004834 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004835 goto erret;
4836 }
4837 }
4838 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004839 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004840 clear_tv(&fudi.fd_di->di_tv);
4841 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004842 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004843
Bram Moolenaare38eab22019-12-05 21:50:01 +01004844 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004845 flags |= FC_DICT;
4846 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004847 }
4848 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004849 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004851 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852
4853 if (eap->cmdidx == CMD_def)
4854 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02004855 int lnum_save = SOURCING_LNUM;
4856 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004857
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004858 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004859
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004860 // error messages are for the first function line
4861 SOURCING_LNUM = sourcing_lnum_top;
4862
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02004863 // The function may use script variables from the context.
4864 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004865
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004866 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004867 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004868 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004869 free_fp = fp_allocated;
4870 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004872 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004873
4874 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004875 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004876 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004877 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004878 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004879 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004880 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02004881 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004882 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02004883 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004884 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004885
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004886 if (fp_allocated)
4887 {
4888 // insert the new function in the function list
4889 set_ufunc_name(fp, name);
4890 if (overwrite)
4891 {
4892 hi = hash_find(&func_hashtab, name);
4893 hi->hi_key = UF2HIKEY(fp);
4894 }
4895 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
4896 {
4897 free_fp = TRUE;
4898 goto erret;
4899 }
4900 fp->uf_refcount = 1;
4901 }
4902
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004903 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004904 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004905 if ((flags & FC_CLOSURE) != 0)
4906 {
Bram Moolenaar58016442016-07-31 18:30:22 +02004907 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004908 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004909 }
4910 else
4911 fp->uf_scoped = NULL;
4912
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004913#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004914 if (prof_def_func())
4915 func_do_profile(fp);
4916#endif
4917 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02004918 if (sandbox)
4919 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004920 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004921 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004922 fp->uf_flags = flags;
4923 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01004924 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004925 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004926 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004927 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928 if (is_export)
4929 {
4930 fp->uf_flags |= FC_EXPORT;
4931 // let ex_export() know the export worked.
4932 is_export = FALSE;
4933 }
4934
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004935 if (eap->cmdidx == CMD_def)
4936 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02004937 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
4938 // :func does not use Vim9 script syntax, even in a Vim9 script file
4939 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004940
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004941 goto ret_free;
4942
4943erret:
4944 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004945 ga_clear_strings(&default_args);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004946 if (fp != NULL)
4947 {
4948 ga_init(&fp->uf_args);
4949 ga_init(&fp->uf_def_args);
4950 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004951errret_2:
4952 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004953 if (fp != NULL)
4954 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004955 if (free_fp)
4956 {
4957 vim_free(fp);
4958 fp = NULL;
4959 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004960ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01004961 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004962 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004963 if (name != name_arg)
4964 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004965 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004966 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004967
4968 return fp;
4969}
4970
4971/*
4972 * ":function"
4973 */
4974 void
4975ex_function(exarg_T *eap)
4976{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004977 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00004978
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004979 ga_init2(&lines_to_free, sizeof(char_u *), 50);
4980 (void)define_function(eap, NULL, &lines_to_free);
4981 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02004982}
4983
4984/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01004985 * Find a function by name, including "<lambda>123".
4986 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01004987 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01004988 * Return NULL if not found.
4989 */
4990 ufunc_T *
4991find_func_by_name(char_u *name, compiletype_T *compile_type)
4992{
4993 char_u *arg = name;
4994 char_u *fname;
4995 ufunc_T *ufunc;
4996 int is_global = FALSE;
4997
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01004998 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
4999 {
5000 *compile_type = CT_PROFILE;
5001 arg = skipwhite(arg + 7);
5002 }
5003 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5004 {
5005 *compile_type = CT_DEBUG;
5006 arg = skipwhite(arg + 5);
5007 }
5008
5009 if (STRNCMP(arg, "<lambda>", 8) == 0)
5010 {
5011 arg += 8;
5012 (void)getdigits(&arg);
5013 fname = vim_strnsave(name, arg - name);
5014 }
5015 else
5016 fname = trans_function_name(&arg, &is_global, FALSE,
5017 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
5018 if (fname == NULL)
5019 {
5020 semsg(_(e_invalid_argument_str), name);
5021 return NULL;
5022 }
5023 if (!ends_excmd2(name, arg))
5024 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005025 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005026 emsg(ex_errmsg(e_trailing_characters_str, arg));
5027 return NULL;
5028 }
5029
5030 ufunc = find_func(fname, is_global);
5031 if (ufunc == NULL)
5032 {
5033 char_u *p = untrans_function_name(fname);
5034
5035 if (p != NULL)
5036 // Try again without making it script-local.
5037 ufunc = find_func(p, FALSE);
5038 }
5039 vim_free(fname);
5040 if (ufunc == NULL)
5041 semsg(_(e_cannot_find_function_str), name);
5042 return ufunc;
5043}
5044
5045/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005046 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005047 * be compiled or the one specified by the argument.
5048 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005049 */
5050 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005051ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005052{
Bram Moolenaar822ba242020-05-24 23:00:18 +02005053 ufunc_T *ufunc;
5054
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005055 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005056 {
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005057 compiletype_T compile_type = CT_NONE;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005058
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005059 ufunc = find_func_by_name(eap->arg, &compile_type);
5060 if (ufunc != NULL)
5061 {
5062 if (func_needs_compiling(ufunc, compile_type))
5063 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5064 else
5065 smsg(_("Function %s does not need compiling"), eap->arg);
5066 }
5067 }
5068 else
5069 {
5070 long todo = (long)func_hashtab.ht_used;
5071 int changed = func_hashtab.ht_changed;
5072 hashitem_T *hi;
5073
5074 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5075 {
5076 if (!HASHITEM_EMPTY(hi))
5077 {
5078 --todo;
5079 ufunc = HI2UF(hi);
5080 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5081 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5082 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005083 {
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005084 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5085
5086 if (func_hashtab.ht_changed != changed)
5087 {
5088 // a function has been added or removed, need to start
5089 // over
5090 todo = (long)func_hashtab.ht_used;
5091 changed = func_hashtab.ht_changed;
5092 hi = func_hashtab.ht_array;
5093 --hi;
5094 }
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005095 }
5096 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005097 }
5098 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005099}
5100
5101/*
5102 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5103 * Return 2 if "p" starts with "s:".
5104 * Return 0 otherwise.
5105 */
5106 int
5107eval_fname_script(char_u *p)
5108{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005109 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5110 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005111 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5112 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5113 return 5;
5114 if (p[0] == 's' && p[1] == ':')
5115 return 2;
5116 return 0;
5117}
5118
5119 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005120translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005121{
5122 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005123 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005124 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005125}
5126
5127/*
5128 * Return TRUE when "ufunc" has old-style "..." varargs
5129 * or named varargs "...name: type".
5130 */
5131 int
5132has_varargs(ufunc_T *ufunc)
5133{
5134 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005135}
5136
5137/*
5138 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005139 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005140 */
5141 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005142function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005143{
5144 char_u *nm = name;
5145 char_u *p;
5146 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005147 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005148 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005149
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005150 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5151 if (no_deref)
5152 flag |= TFN_NO_DEREF;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005153 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005154 nm = skipwhite(nm);
5155
Bram Moolenaare38eab22019-12-05 21:50:01 +01005156 // Only accept "funcname", "funcname ", "funcname (..." and
5157 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005158 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005159 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005160 vim_free(p);
5161 return n;
5162}
5163
Bram Moolenaar113e1072019-01-20 15:30:40 +01005164#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005165 char_u *
5166get_expanded_name(char_u *name, int check)
5167{
5168 char_u *nm = name;
5169 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005170 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005171
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005172 p = trans_function_name(&nm, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005173 TFN_INT|TFN_QUIET, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005174
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005175 if (p != NULL && *nm == NUL
5176 && (!check || translated_function_exists(p, is_global)))
5177 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005178
5179 vim_free(p);
5180 return NULL;
5181}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005182#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005183
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005184/*
5185 * Function given to ExpandGeneric() to obtain the list of user defined
5186 * function names.
5187 */
5188 char_u *
5189get_user_func_name(expand_T *xp, int idx)
5190{
5191 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005192 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005193 static hashitem_T *hi;
5194 ufunc_T *fp;
5195
5196 if (idx == 0)
5197 {
5198 done = 0;
5199 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005200 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005201 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005202 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005203 {
5204 if (done++ > 0)
5205 ++hi;
5206 while (HASHITEM_EMPTY(hi))
5207 ++hi;
5208 fp = HI2UF(hi);
5209
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005210 // don't show dead, dict and lambda functions
5211 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005212 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005213 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005214
5215 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005216 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005217
5218 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005219 if (xp->xp_context != EXPAND_USER_FUNC
5220 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005221 {
5222 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005223 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005224 STRCAT(IObuff, ")");
5225 }
5226 return IObuff;
5227 }
5228 return NULL;
5229}
5230
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005231/*
5232 * ":delfunction {name}"
5233 */
5234 void
5235ex_delfunction(exarg_T *eap)
5236{
5237 ufunc_T *fp = NULL;
5238 char_u *p;
5239 char_u *name;
5240 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005241 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005242
5243 p = eap->arg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005244 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi,
5245 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005246 vim_free(fudi.fd_newkey);
5247 if (name == NULL)
5248 {
5249 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005250 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005251 return;
5252 }
5253 if (!ends_excmd(*skipwhite(p)))
5254 {
5255 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005256 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005257 return;
5258 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005259 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005260 if (eap->nextcmd != NULL)
5261 *p = NUL;
5262
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005263 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005264 {
5265 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005266 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005267 vim_free(name);
5268 return;
5269 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005270 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005271 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005272 vim_free(name);
5273
5274 if (!eap->skip)
5275 {
5276 if (fp == NULL)
5277 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005278 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005279 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005280 return;
5281 }
5282 if (fp->uf_calls > 0)
5283 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005284 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005285 return;
5286 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005287 if (fp->uf_flags & FC_VIM9)
5288 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005289 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005290 return;
5291 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005292
5293 if (fudi.fd_dict != NULL)
5294 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005295 // Delete the dict item that refers to the function, it will
5296 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005297 dictitem_remove(fudi.fd_dict, fudi.fd_di);
5298 }
5299 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005300 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005301 // A normal function (not a numbered function or lambda) has a
5302 // refcount of 1 for the entry in the hashtable. When deleting
5303 // it and the refcount is more than one, it should be kept.
5304 // A numbered function and lambda should be kept if the refcount is
5305 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005306 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005307 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005308 // Function is still referenced somewhere. Don't free it but
5309 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005310 if (func_remove(fp))
5311 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005312 }
5313 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005314 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005315 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005316 }
5317}
5318
5319/*
5320 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005321 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005322 */
5323 void
5324func_unref(char_u *name)
5325{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005326 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005327
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005328 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005329 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005330 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005331 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005332 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005333#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005334 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005335#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005336 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005337 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005338 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005339}
5340
5341/*
5342 * Unreference a Function: decrement the reference count and free it when it
5343 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005344 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005345 */
5346 void
5347func_ptr_unref(ufunc_T *fp)
5348{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005349 if (fp != NULL && (--fp->uf_refcount <= 0
5350 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5351 && fp->uf_partial->pt_refcount <= 1
5352 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005353 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005354 // Only delete it when it's not being used. Otherwise it's done
5355 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005356 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005357 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005358 }
5359}
5360
5361/*
5362 * Count a reference to a Function.
5363 */
5364 void
5365func_ref(char_u *name)
5366{
5367 ufunc_T *fp;
5368
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005369 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005370 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005371 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005372 if (fp != NULL)
5373 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005374 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005375 // Only give an error for a numbered function.
5376 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005377 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005378}
5379
5380/*
5381 * Count a reference to a Function.
5382 */
5383 void
5384func_ptr_ref(ufunc_T *fp)
5385{
5386 if (fp != NULL)
5387 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005388}
5389
5390/*
5391 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5392 * referenced from anywhere that is in use.
5393 */
5394 static int
5395can_free_funccal(funccall_T *fc, int copyID)
5396{
5397 return (fc->l_varlist.lv_copyID != copyID
5398 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005399 && fc->l_avars.dv_copyID != copyID
5400 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005401}
5402
5403/*
5404 * ":return [expr]"
5405 */
5406 void
5407ex_return(exarg_T *eap)
5408{
5409 char_u *arg = eap->arg;
5410 typval_T rettv;
5411 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005412 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005413
5414 if (current_funccal == NULL)
5415 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005416 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005417 return;
5418 }
5419
Bram Moolenaar844fb642021-10-23 13:32:30 +01005420 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005421 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5422
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005423 if (eap->skip)
5424 ++emsg_skip;
5425
5426 eap->nextcmd = NULL;
5427 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005428 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005429 {
5430 if (!eap->skip)
5431 returning = do_return(eap, FALSE, TRUE, &rettv);
5432 else
5433 clear_tv(&rettv);
5434 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005435 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005436 else if (!eap->skip)
5437 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005438 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005439 update_force_abort();
5440
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005441 /*
5442 * Return unless the expression evaluation has been cancelled due to an
5443 * aborting error, an interrupt, or an exception.
5444 */
5445 if (!aborting())
5446 returning = do_return(eap, FALSE, TRUE, NULL);
5447 }
5448
Bram Moolenaare38eab22019-12-05 21:50:01 +01005449 // When skipping or the return gets pending, advance to the next command
5450 // in this line (!returning). Otherwise, ignore the rest of the line.
5451 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005452 if (returning)
5453 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005454 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02005455 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005456
5457 if (eap->skip)
5458 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02005459 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005460}
5461
5462/*
5463 * ":1,25call func(arg1, arg2)" function call.
5464 */
5465 void
5466ex_call(exarg_T *eap)
5467{
5468 char_u *arg = eap->arg;
5469 char_u *startarg;
5470 char_u *name;
5471 char_u *tofree;
5472 int len;
5473 typval_T rettv;
5474 linenr_T lnum;
5475 int doesrange;
5476 int failed = FALSE;
5477 funcdict_T fudi;
5478 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005479 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005480 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005481 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005482 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005483
Bram Moolenaare6b53242020-07-01 17:28:33 +02005484 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005485 if (eap->skip)
5486 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005487 // trans_function_name() doesn't work well when skipping, use eval0()
5488 // instead to skip to any following command, e.g. for:
5489 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005490 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005491 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005492 clear_tv(&rettv);
5493 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005494 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005495 return;
5496 }
5497
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005498 tofree = trans_function_name(&arg, NULL, eap->skip, TFN_INT,
Bram Moolenaar4525a572022-02-13 11:57:33 +00005499 &fudi, &partial, vim9script ? &type : NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005500 if (fudi.fd_newkey != NULL)
5501 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005502 // Still need to give an error message for missing key.
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005503 semsg(_(e_key_not_present_in_dictionary), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005504 vim_free(fudi.fd_newkey);
5505 }
5506 if (tofree == NULL)
5507 return;
5508
Bram Moolenaare38eab22019-12-05 21:50:01 +01005509 // Increase refcount on dictionary, it could get deleted when evaluating
5510 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005511 if (fudi.fd_dict != NULL)
5512 ++fudi.fd_dict->dv_refcount;
5513
Bram Moolenaare38eab22019-12-05 21:50:01 +01005514 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
5515 // contents. For VAR_PARTIAL get its partial, unless we already have one
5516 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005517 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005518 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00005519 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00005520 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005521
Bram Moolenaare38eab22019-12-05 21:50:01 +01005522 // Skip white space to allow ":call func ()". Not good, but required for
5523 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005524 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005525 if (*startarg != '(')
5526 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005527 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005528 goto end;
5529 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00005530 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02005531 {
5532 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
5533 goto end;
5534 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005535
5536 /*
5537 * When skipping, evaluate the function once, to find the end of the
5538 * arguments.
5539 * When the function takes a range, this is discovered after the first
5540 * call, and the loop is broken.
5541 */
5542 if (eap->skip)
5543 {
5544 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005545 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005546 }
5547 else
5548 lnum = eap->line1;
5549 for ( ; lnum <= eap->line2; ++lnum)
5550 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005551 funcexe_T funcexe;
5552
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005553 if (!eap->skip && eap->addr_count > 0)
5554 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01005555 if (lnum > curbuf->b_ml.ml_line_count)
5556 {
5557 // If the function deleted lines or switched to another buffer
5558 // the line number may become invalid.
Bram Moolenaar108010a2021-06-27 22:03:33 +02005559 emsg(_(e_invalid_range));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01005560 break;
5561 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005562 curwin->w_cursor.lnum = lnum;
5563 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005564 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005565 }
5566 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005567
Bram Moolenaara80faa82020-04-12 19:37:17 +02005568 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00005569 funcexe.fe_firstline = eap->line1;
5570 funcexe.fe_lastline = eap->line2;
5571 funcexe.fe_doesrange = &doesrange;
5572 funcexe.fe_evaluate = !eap->skip;
5573 funcexe.fe_partial = partial;
5574 funcexe.fe_selfdict = fudi.fd_dict;
5575 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005576 funcexe.fe_found_var = found_var;
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02005577 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaare6b53242020-07-01 17:28:33 +02005578 if (get_func_tv(name, -1, &rettv, &arg, &evalarg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005579 {
5580 failed = TRUE;
5581 break;
5582 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01005583 if (has_watchexpr())
5584 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005585
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005586 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005587 if (handle_subscript(&arg, NULL, &rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005588 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005589 {
5590 failed = TRUE;
5591 break;
5592 }
5593
5594 clear_tv(&rettv);
5595 if (doesrange || eap->skip)
5596 break;
5597
Bram Moolenaare38eab22019-12-05 21:50:01 +01005598 // Stop when immediately aborting on error, or when an interrupt
5599 // occurred or an exception was thrown but not caught.
5600 // get_func_tv() returned OK, so that the check for trailing
5601 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005602 if (aborting())
5603 break;
5604 }
5605 if (eap->skip)
5606 --emsg_skip;
5607
Bram Moolenaar1d341892021-09-18 15:25:52 +02005608 // When inside :try we need to check for following "| catch" or "| endtry".
5609 // Not when there was an error, but do check if an exception was thrown.
5610 if ((!aborting() || did_throw)
5611 && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005612 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005613 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02005614 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02005615 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005616 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02005617 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01005618 {
5619 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00005620 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01005621 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005622 }
5623 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02005624 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005625 }
Bram Moolenaara929c922022-04-18 15:21:17 +01005626 // Must be after using "arg", it may point into memory cleared here.
5627 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005628
5629end:
5630 dict_unref(fudi.fd_dict);
5631 vim_free(tofree);
5632}
5633
5634/*
5635 * Return from a function. Possibly makes the return pending. Also called
5636 * for a pending return at the ":endtry" or after returning from an extra
5637 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
5638 * when called due to a ":return" command. "rettv" may point to a typval_T
5639 * with the return rettv. Returns TRUE when the return can be carried out,
5640 * FALSE when the return gets pending.
5641 */
5642 int
5643do_return(
5644 exarg_T *eap,
5645 int reanimate,
5646 int is_cmd,
5647 void *rettv)
5648{
5649 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01005650 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005651
5652 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005653 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005654 current_funccal->returned = FALSE;
5655
5656 /*
5657 * Cleanup (and inactivate) conditionals, but stop when a try conditional
5658 * not in its finally clause (which then is to be executed next) is found.
5659 * In this case, make the ":return" pending for execution at the ":endtry".
5660 * Otherwise, return normally.
5661 */
5662 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
5663 if (idx >= 0)
5664 {
5665 cstack->cs_pending[idx] = CSTP_RETURN;
5666
5667 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005668 // A pending return again gets pending. "rettv" points to an
5669 // allocated variable with the rettv of the original ":return"'s
5670 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005671 cstack->cs_rettv[idx] = rettv;
5672 else
5673 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005674 // When undoing a return in order to make it pending, get the stored
5675 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005676 if (reanimate)
5677 rettv = current_funccal->rettv;
5678
5679 if (rettv != NULL)
5680 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005681 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005682 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
5683 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
5684 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005685 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005686 }
5687 else
5688 cstack->cs_rettv[idx] = NULL;
5689
5690 if (reanimate)
5691 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005692 // The pending return value could be overwritten by a ":return"
5693 // without argument in a finally clause; reset the default
5694 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005695 current_funccal->rettv->v_type = VAR_NUMBER;
5696 current_funccal->rettv->vval.v_number = 0;
5697 }
5698 }
5699 report_make_pending(CSTP_RETURN, rettv);
5700 }
5701 else
5702 {
5703 current_funccal->returned = TRUE;
5704
Bram Moolenaare38eab22019-12-05 21:50:01 +01005705 // If the return is carried out now, store the return value. For
5706 // a return immediately after reanimation, the value is already
5707 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005708 if (!reanimate && rettv != NULL)
5709 {
5710 clear_tv(current_funccal->rettv);
5711 *current_funccal->rettv = *(typval_T *)rettv;
5712 if (!is_cmd)
5713 vim_free(rettv);
5714 }
5715 }
5716
5717 return idx < 0;
5718}
5719
5720/*
5721 * Free the variable with a pending return value.
5722 */
5723 void
5724discard_pending_return(void *rettv)
5725{
5726 free_tv((typval_T *)rettv);
5727}
5728
5729/*
5730 * Generate a return command for producing the value of "rettv". The result
5731 * is an allocated string. Used by report_pending() for verbose messages.
5732 */
5733 char_u *
5734get_return_cmd(void *rettv)
5735{
5736 char_u *s = NULL;
5737 char_u *tofree = NULL;
5738 char_u numbuf[NUMBUFLEN];
5739
5740 if (rettv != NULL)
5741 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
5742 if (s == NULL)
5743 s = (char_u *)"";
5744
5745 STRCPY(IObuff, ":return ");
5746 STRNCPY(IObuff + 8, s, IOSIZE - 8);
5747 if (STRLEN(s) + 8 >= IOSIZE)
5748 STRCPY(IObuff + IOSIZE - 4, "...");
5749 vim_free(tofree);
5750 return vim_strsave(IObuff);
5751}
5752
5753/*
5754 * Get next function line.
5755 * Called by do_cmdline() to get the next line.
5756 * Returns allocated string, or NULL for end of function.
5757 */
5758 char_u *
5759get_func_line(
5760 int c UNUSED,
5761 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02005762 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005763 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005764{
5765 funccall_T *fcp = (funccall_T *)cookie;
5766 ufunc_T *fp = fcp->func;
5767 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005768 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005769
Bram Moolenaare38eab22019-12-05 21:50:01 +01005770 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005771 if (fcp->dbg_tick != debug_tick)
5772 {
5773 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005774 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005775 fcp->dbg_tick = debug_tick;
5776 }
5777#ifdef FEAT_PROFILE
5778 if (do_profiling == PROF_YES)
5779 func_line_end(cookie);
5780#endif
5781
5782 gap = &fp->uf_lines;
5783 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5784 || fcp->returned)
5785 retval = NULL;
5786 else
5787 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005788 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005789 while (fcp->linenr < gap->ga_len
5790 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
5791 ++fcp->linenr;
5792 if (fcp->linenr >= gap->ga_len)
5793 retval = NULL;
5794 else
5795 {
5796 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005797 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005798#ifdef FEAT_PROFILE
5799 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005800 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005801#endif
5802 }
5803 }
5804
Bram Moolenaare38eab22019-12-05 21:50:01 +01005805 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005806 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005807 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005808 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01005809 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005810 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005811 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005812 fcp->dbg_tick = debug_tick;
5813 }
5814
5815 return retval;
5816}
5817
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005818/*
5819 * Return TRUE if the currently active function should be ended, because a
5820 * return was encountered or an error occurred. Used inside a ":while".
5821 */
5822 int
5823func_has_ended(void *cookie)
5824{
5825 funccall_T *fcp = (funccall_T *)cookie;
5826
Bram Moolenaare38eab22019-12-05 21:50:01 +01005827 // Ignore the "abort" flag if the abortion behavior has been changed due to
5828 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005829 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5830 || fcp->returned);
5831}
5832
5833/*
5834 * return TRUE if cookie indicates a function which "abort"s on errors.
5835 */
5836 int
5837func_has_abort(
5838 void *cookie)
5839{
5840 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
5841}
5842
5843
5844/*
5845 * Turn "dict.Func" into a partial for "Func" bound to "dict".
5846 * Don't do this when "Func" is already a partial that was bound
5847 * explicitly (pt_auto is FALSE).
5848 * Changes "rettv" in-place.
5849 * Returns the updated "selfdict_in".
5850 */
5851 dict_T *
5852make_partial(dict_T *selfdict_in, typval_T *rettv)
5853{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005854 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005855 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005856 char_u fname_buf[FLEN_FIXED + 1];
5857 int error;
5858 dict_T *selfdict = selfdict_in;
5859
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005860 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
5861 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005862 fp = rettv->vval.v_partial->pt_func;
5863 else
5864 {
5865 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005866 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005867 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005868 if (fname == NULL)
5869 {
5870 // There is no point binding a dict to a NULL function, just create
5871 // a function reference.
5872 rettv->v_type = VAR_FUNC;
5873 rettv->vval.v_string = NULL;
5874 }
5875 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00005876 {
5877 char_u *tofree = NULL;
5878
5879 // Translate "s:func" to the stored function name.
5880 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
5881 fp = find_func(fname, FALSE);
5882 vim_free(tofree);
5883 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005884 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005885
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005886 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005887 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005888 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005889
5890 if (pt != NULL)
5891 {
5892 pt->pt_refcount = 1;
5893 pt->pt_dict = selfdict;
5894 pt->pt_auto = TRUE;
5895 selfdict = NULL;
5896 if (rettv->v_type == VAR_FUNC)
5897 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005898 // Just a function: Take over the function name and use
5899 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005900 pt->pt_name = rettv->vval.v_string;
5901 }
5902 else
5903 {
5904 partial_T *ret_pt = rettv->vval.v_partial;
5905 int i;
5906
Bram Moolenaare38eab22019-12-05 21:50:01 +01005907 // Partial: copy the function name, use selfdict and copy
5908 // args. Can't take over name or args, the partial might
5909 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005910 if (ret_pt->pt_name != NULL)
5911 {
5912 pt->pt_name = vim_strsave(ret_pt->pt_name);
5913 func_ref(pt->pt_name);
5914 }
5915 else
5916 {
5917 pt->pt_func = ret_pt->pt_func;
5918 func_ptr_ref(pt->pt_func);
5919 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005920 if (ret_pt->pt_argc > 0)
5921 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005922 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005923 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005924 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005925 pt->pt_argc = 0;
5926 else
5927 {
5928 pt->pt_argc = ret_pt->pt_argc;
5929 for (i = 0; i < pt->pt_argc; i++)
5930 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
5931 }
5932 }
5933 partial_unref(ret_pt);
5934 }
5935 rettv->v_type = VAR_PARTIAL;
5936 rettv->vval.v_partial = pt;
5937 }
5938 }
5939 return selfdict;
5940}
5941
5942/*
5943 * Return the name of the executed function.
5944 */
5945 char_u *
5946func_name(void *cookie)
5947{
5948 return ((funccall_T *)cookie)->func->uf_name;
5949}
5950
5951/*
5952 * Return the address holding the next breakpoint line for a funccall cookie.
5953 */
5954 linenr_T *
5955func_breakpoint(void *cookie)
5956{
5957 return &((funccall_T *)cookie)->breakpoint;
5958}
5959
5960/*
5961 * Return the address holding the debug tick for a funccall cookie.
5962 */
5963 int *
5964func_dbg_tick(void *cookie)
5965{
5966 return &((funccall_T *)cookie)->dbg_tick;
5967}
5968
5969/*
5970 * Return the nesting level for a funccall cookie.
5971 */
5972 int
5973func_level(void *cookie)
5974{
5975 return ((funccall_T *)cookie)->level;
5976}
5977
5978/*
5979 * Return TRUE when a function was ended by a ":return" command.
5980 */
5981 int
5982current_func_returned(void)
5983{
5984 return current_funccal->returned;
5985}
5986
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005987 int
5988free_unref_funccal(int copyID, int testing)
5989{
5990 int did_free = FALSE;
5991 int did_free_funccal = FALSE;
5992 funccall_T *fc, **pfc;
5993
5994 for (pfc = &previous_funccal; *pfc != NULL; )
5995 {
5996 if (can_free_funccal(*pfc, copyID))
5997 {
5998 fc = *pfc;
5999 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006000 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006001 did_free = TRUE;
6002 did_free_funccal = TRUE;
6003 }
6004 else
6005 pfc = &(*pfc)->caller;
6006 }
6007 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006008 // When a funccal was freed some more items might be garbage
6009 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006010 (void)garbage_collect(testing);
6011
6012 return did_free;
6013}
6014
6015/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006016 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006017 */
6018 static funccall_T *
6019get_funccal(void)
6020{
6021 int i;
6022 funccall_T *funccal;
6023 funccall_T *temp_funccal;
6024
6025 funccal = current_funccal;
6026 if (debug_backtrace_level > 0)
6027 {
6028 for (i = 0; i < debug_backtrace_level; i++)
6029 {
6030 temp_funccal = funccal->caller;
6031 if (temp_funccal)
6032 funccal = temp_funccal;
6033 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006034 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006035 debug_backtrace_level = i;
6036 }
6037 }
6038 return funccal;
6039}
6040
6041/*
6042 * Return the hashtable used for local variables in the current funccal.
6043 * Return NULL if there is no current funccal.
6044 */
6045 hashtab_T *
6046get_funccal_local_ht()
6047{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006048 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006049 return NULL;
6050 return &get_funccal()->l_vars.dv_hashtab;
6051}
6052
6053/*
6054 * Return the l: scope variable.
6055 * Return NULL if there is no current funccal.
6056 */
6057 dictitem_T *
6058get_funccal_local_var()
6059{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006060 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006061 return NULL;
6062 return &get_funccal()->l_vars_var;
6063}
6064
6065/*
6066 * Return the hashtable used for argument in the current funccal.
6067 * Return NULL if there is no current funccal.
6068 */
6069 hashtab_T *
6070get_funccal_args_ht()
6071{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006072 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006073 return NULL;
6074 return &get_funccal()->l_avars.dv_hashtab;
6075}
6076
6077/*
6078 * Return the a: scope variable.
6079 * Return NULL if there is no current funccal.
6080 */
6081 dictitem_T *
6082get_funccal_args_var()
6083{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006084 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006085 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01006086 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006087}
6088
6089/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006090 * List function variables, if there is a function.
6091 */
6092 void
6093list_func_vars(int *first)
6094{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006095 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006096 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006097 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006098}
6099
6100/*
6101 * If "ht" is the hashtable for local variables in the current funccal, return
6102 * the dict that contains it.
6103 * Otherwise return NULL.
6104 */
6105 dict_T *
6106get_current_funccal_dict(hashtab_T *ht)
6107{
6108 if (current_funccal != NULL
6109 && ht == &current_funccal->l_vars.dv_hashtab)
6110 return &current_funccal->l_vars;
6111 return NULL;
6112}
6113
6114/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006115 * Search hashitem in parent scope.
6116 */
6117 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006118find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006119{
6120 funccall_T *old_current_funccal = current_funccal;
6121 hashtab_T *ht;
6122 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006123 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006124
6125 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
6126 return NULL;
6127
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006128 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006129 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006130 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006131 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006132 ht = find_var_ht(name, &varname);
6133 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006134 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006135 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006136 if (!HASHITEM_EMPTY(hi))
6137 {
6138 *pht = ht;
6139 break;
6140 }
6141 }
6142 if (current_funccal == current_funccal->func->uf_scoped)
6143 break;
6144 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006145 }
6146 current_funccal = old_current_funccal;
6147
6148 return hi;
6149}
6150
6151/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006152 * Search variable in parent scope.
6153 */
6154 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006155find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006156{
6157 dictitem_T *v = NULL;
6158 funccall_T *old_current_funccal = current_funccal;
6159 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006160 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006161
6162 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
6163 return NULL;
6164
Bram Moolenaare38eab22019-12-05 21:50:01 +01006165 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006166 current_funccal = current_funccal->func->uf_scoped;
6167 while (current_funccal)
6168 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006169 ht = find_var_ht(name, &varname);
6170 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006171 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006172 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006173 if (v != NULL)
6174 break;
6175 }
6176 if (current_funccal == current_funccal->func->uf_scoped)
6177 break;
6178 current_funccal = current_funccal->func->uf_scoped;
6179 }
6180 current_funccal = old_current_funccal;
6181
6182 return v;
6183}
6184
6185/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006186 * Set "copyID + 1" in previous_funccal and callers.
6187 */
6188 int
6189set_ref_in_previous_funccal(int copyID)
6190{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006191 funccall_T *fc;
6192
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006193 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006194 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006195 fc->fc_copyID = copyID + 1;
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006196 if (set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
6197 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
6198 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL))
6199 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006200 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006201 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006202}
6203
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006204 static int
6205set_ref_in_funccal(funccall_T *fc, int copyID)
6206{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006207 if (fc->fc_copyID != copyID)
6208 {
6209 fc->fc_copyID = copyID;
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006210 if (set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
6211 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
6212 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
6213 || set_ref_in_func(NULL, fc->func, copyID))
6214 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006215 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006216 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006217}
6218
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006219/*
6220 * Set "copyID" in all local vars and arguments in the call stack.
6221 */
6222 int
6223set_ref_in_call_stack(int copyID)
6224{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006225 funccall_T *fc;
6226 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006227
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006228 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6229 if (set_ref_in_funccal(fc, copyID))
6230 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006231
6232 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006233 for (entry = funccal_stack; entry != NULL; entry = entry->next)
6234 for (fc = entry->top_funccal; fc != NULL; fc = fc->caller)
6235 if (set_ref_in_funccal(fc, copyID))
6236 return TRUE;
6237 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006238}
6239
6240/*
6241 * Set "copyID" in all functions available by name.
6242 */
6243 int
6244set_ref_in_functions(int copyID)
6245{
6246 int todo;
6247 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006248 ufunc_T *fp;
6249
6250 todo = (int)func_hashtab.ht_used;
6251 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006252 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006253 if (!HASHITEM_EMPTY(hi))
6254 {
6255 --todo;
6256 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006257 if (!func_name_refcount(fp->uf_name)
6258 && set_ref_in_func(NULL, fp, copyID))
6259 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006260 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006261 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006262 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006263}
6264
6265/*
6266 * Set "copyID" in all function arguments.
6267 */
6268 int
6269set_ref_in_func_args(int copyID)
6270{
6271 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006272
6273 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006274 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
6275 copyID, NULL, NULL))
6276 return TRUE;
6277 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006278}
6279
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006280/*
6281 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006282 * Returns TRUE if setting references failed somehow.
6283 */
6284 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006285set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006286{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006287 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006288 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01006289 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006290 char_u fname_buf[FLEN_FIXED + 1];
6291 char_u *tofree = NULL;
6292 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006293 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006294
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006295 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006296 return FALSE;
6297
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006298 if (fp_in == NULL)
6299 {
6300 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006301 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006302 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006303 if (fp != NULL)
6304 {
6305 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006306 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006307 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02006308
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006309 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006310 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006311}
6312
Bram Moolenaare38eab22019-12-05 21:50:01 +01006313#endif // FEAT_EVAL