blob: c80a49051ce2932796ee546f0dbf64a4b6791451 [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);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020035
36 void
37func_init()
38{
39 hash_init(&func_hashtab);
40}
41
Dominique Pelle748b3082022-01-08 12:41:16 +000042#if defined(FEAT_PROFILE) || defined(PROTO)
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020043/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020044 * Return the function hash table
45 */
46 hashtab_T *
47func_tbl_get(void)
48{
49 return &func_hashtab;
50}
Dominique Pelle748b3082022-01-08 12:41:16 +000051#endif
Bram Moolenaar660a10a2019-07-14 15:48:38 +020052
53/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020054 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020055 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010056 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010057 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaardce24412022-02-08 20:35:30 +000058 * If "eap" is not NULL use it to check for an already declared name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010059 * Return a pointer to after the type.
60 * When something is wrong return "arg".
61 */
62 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010063one_function_arg(
64 char_u *arg,
65 garray_T *newargs,
66 garray_T *argtypes,
67 int types_optional,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010068 evalarg_T *evalarg,
Bram Moolenaardce24412022-02-08 20:35:30 +000069 exarg_T *eap,
Bram Moolenaar2a389082021-04-09 20:24:31 +020070 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010071 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010072{
Bram Moolenaar6e949782020-04-13 17:21:00 +020073 char_u *p = arg;
74 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020075 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010076
77 while (ASCII_ISALNUM(*p) || *p == '_')
78 ++p;
79 if (arg == p || isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020080 || (argtypes == NULL
81 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
82 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083 {
84 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000085 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 return arg;
87 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010088
Bram Moolenaar057e84a2021-02-28 16:55:11 +010089 // Vim9 script: cannot use script var name for argument. In function: also
90 // check local vars and arguments.
91 if (!skip && argtypes != NULL && check_defined(arg, p - arg,
Bram Moolenaardce24412022-02-08 20:35:30 +000092 evalarg == NULL ? NULL : evalarg->eval_cctx,
93 eap == NULL ? NULL : eap->cstack, TRUE) == FAIL)
Bram Moolenaarb4893b82021-02-21 22:20:24 +010094 return arg;
Bram Moolenaarb4893b82021-02-21 22:20:24 +010095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
97 return arg;
98 if (newargs != NULL)
99 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100100 int c;
101 int i;
102
103 c = *p;
104 *p = NUL;
105 arg_copy = vim_strsave(arg);
106 if (arg_copy == NULL)
107 {
108 *p = c;
109 return arg;
110 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200111 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200112 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200113 // Check for duplicate argument name.
114 for (i = 0; i < newargs->ga_len; ++i)
115 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
116 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000117 semsg(_(e_duplicate_argument_name_str), arg_copy);
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200118 vim_free(arg_copy);
119 return arg;
120 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100121 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
122 newargs->ga_len++;
123
124 *p = c;
125 }
126
127 // get any type from "arg: type"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100128 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100129 {
130 char_u *type = NULL;
131
Bram Moolenaar6e949782020-04-13 17:21:00 +0200132 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
133 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200134 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200135 arg_copy == NULL ? arg : arg_copy);
136 p = skipwhite(p);
137 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138 if (*p == ':')
139 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200140 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100141 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200142 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100143 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200144 return arg;
145 }
146 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200147 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100148 if (!skip)
149 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100150 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200151 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200152 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200153 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200154 arg_copy == NULL ? arg : arg_copy);
155 return arg;
156 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100157 if (!skip)
158 {
159 if (type == NULL && types_optional)
160 // lambda arguments default to "any" type
Bram Moolenaar2a389082021-04-09 20:24:31 +0200161 type = vim_strsave((char_u *)
162 (is_vararg ? "list<any>" : "any"));
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100163 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
164 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100165 }
166
167 return p;
168}
169
170/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000171 * Handle line continuation in function arguments or body.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000172 * Get a next line, store it in "eap" if appropriate and put the line in
173 * "lines_to_free" to free the line later.
Bram Moolenaar7473a842021-12-28 17:55:26 +0000174 */
175 static char_u *
176get_function_line(
177 exarg_T *eap,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000178 garray_T *lines_to_free,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000179 int indent,
180 getline_opt_T getline_options)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000181{
182 char_u *theline;
183
184 if (eap->getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +0000185 theline = getcmdline(':', 0L, indent, 0);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000186 else
187 theline = eap->getline(':', eap->cookie, indent, getline_options);
188 if (theline != NULL)
189 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000190 if (lines_to_free->ga_len > 0
191 && *eap->cmdlinep == ((char_u **)lines_to_free->ga_data)
192 [lines_to_free->ga_len - 1])
Bram Moolenaar7473a842021-12-28 17:55:26 +0000193 *eap->cmdlinep = theline;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000194 ga_add_string(lines_to_free, theline);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000195 }
196
197 return theline;
198}
199
200/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200201 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100202 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100203 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200204 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100205 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200206get_function_args(
207 char_u **argp,
208 char_u endchar,
209 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100210 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100211 int types_optional, // types optional if "argtypes" is not NULL
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100212 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200213 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200214 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200215 int skip,
Bram Moolenaardce24412022-02-08 20:35:30 +0000216 exarg_T *eap, // can be NULL
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000217 garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200218{
219 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100220 char_u *arg;
221 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200222 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200223 int any_default = FALSE;
224 char_u *expr;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100225 char_u *whitep = *argp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200226
227 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000228 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100229 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000230 ga_init2(argtypes, sizeof(char_u *), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200231 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000232 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200233
234 if (varargs != NULL)
235 *varargs = FALSE;
236
237 /*
238 * Isolate the arguments: "arg1, arg2, ...)"
239 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100240 arg = skipwhite(*argp);
241 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200242 while (*p != endchar)
243 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200244 while (eap != NULL && eap->getline != NULL
245 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200246 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200247 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000248 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000249 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000250
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200251 if (theline == NULL)
252 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200253 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200254 p = skipwhite(theline);
255 }
256
257 if (mustend && *p != endchar)
258 {
259 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000260 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100261 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200262 }
263 if (*p == endchar)
264 break;
265
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200266 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
267 {
268 if (varargs != NULL)
269 *varargs = TRUE;
270 p += 3;
271 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100272
273 if (argtypes != NULL)
274 {
275 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200276 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100278 if (!skip)
279 emsg(_(e_missing_name_after_dots));
280 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100281 }
282
283 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100284 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaardce24412022-02-08 20:35:30 +0000285 evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100286 if (p == arg)
287 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100288 if (*skipwhite(p) == '=')
289 {
290 emsg(_(e_cannot_use_default_for_variable_arguments));
291 break;
292 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100293 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200294 }
295 else
296 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200297 char_u *np;
298
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200299 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100300 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaardce24412022-02-08 20:35:30 +0000301 evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100302 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200303 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200304
Bram Moolenaar015cf102021-06-26 21:52:02 +0200305 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200306 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200307 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200308 if (*np == '=' && np[1] != '=' && np[1] != '~'
309 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200310 {
311 typval_T rettv;
312
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100313 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200314 any_default = TRUE;
315 p = skipwhite(p) + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200316 whitep = p;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200317 p = skipwhite(p);
318 expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200319 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200320 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200321 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200322 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200323 if (ga_grow(default_args, 1) == FAIL)
324 goto err_ret;
325
326 // trim trailing whitespace
327 while (p > expr && VIM_ISWHITE(p[-1]))
328 p--;
329 c = *p;
330 *p = NUL;
331 expr = vim_strsave(expr);
332 if (expr == NULL)
333 {
334 *p = c;
335 goto err_ret;
336 }
337 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200338 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200339 default_args->ga_len++;
340 *p = c;
341 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200342 }
343 else
344 mustend = TRUE;
345 }
346 else if (any_default)
347 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000348 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200349 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200350 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200351
352 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
353 {
354 // Be tolerant when skipping
355 if (!skip)
356 {
357 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
358 goto err_ret;
359 }
360 p = skipwhite(p);
361 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200362 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200363 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200364 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200365 // Don't give this error when skipping, it makes the "->" not
366 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100367 // Allow missing space after comma in legacy functions.
368 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200369 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
370 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100371 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200372 goto err_ret;
373 }
374 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200375 else
376 mustend = TRUE;
377 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200378 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200379 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200380 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200381
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200382 if (*p != endchar)
383 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100384 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200385
386 *argp = p;
387 return OK;
388
389err_ret:
390 if (newargs != NULL)
391 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200392 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200393 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200394 return FAIL;
395}
396
397/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100398 * Parse the argument types, filling "fp->uf_arg_types".
399 * Return OK or FAIL.
400 */
401 static int
402parse_argument_types(ufunc_T *fp, garray_T *argtypes, int varargs)
403{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200404 int len = 0;
405
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100406 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
407 if (argtypes->ga_len > 0)
408 {
409 // When "varargs" is set the last name/type goes into uf_va_name
410 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200411 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100412
413 if (len > 0)
414 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
415 if (fp->uf_arg_types != NULL)
416 {
417 int i;
418 type_T *type;
419
420 for (i = 0; i < len; ++ i)
421 {
422 char_u *p = ((char_u **)argtypes->ga_data)[i];
423
424 if (p == NULL)
425 // will get the type from the default value
426 type = &t_unknown;
427 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100428 type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100429 if (type == NULL)
430 return FAIL;
431 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000432 if (i < fp->uf_args.ga_len
433 && (type->tt_type == VAR_FUNC
434 || type->tt_type == VAR_PARTIAL)
435 && var_wrong_func_name(
436 ((char_u **)fp->uf_args.ga_data)[i], TRUE))
437 return FAIL;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100438 }
439 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100440 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200441
442 if (varargs)
443 {
444 char_u *p;
445
446 // Move the last argument "...name: type" to uf_va_name and
447 // uf_va_type.
448 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
449 [fp->uf_args.ga_len - 1];
450 --fp->uf_args.ga_len;
451 p = ((char_u **)argtypes->ga_data)[len];
452 if (p == NULL)
453 // TODO: get type from default value
454 fp->uf_va_type = &t_list_any;
455 else
456 {
457 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
458 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
459 {
460 semsg(_(e_variable_arguments_type_must_be_list_str),
461 ((char_u **)argtypes->ga_data)[len]);
462 return FAIL;
463 }
464 }
465 if (fp->uf_va_type == NULL)
466 return FAIL;
467 }
468
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100469 return OK;
470}
471
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100472 static int
473parse_return_type(ufunc_T *fp, char_u *ret_type)
474{
475 if (ret_type == NULL)
476 fp->uf_ret_type = &t_void;
477 else
478 {
479 char_u *p = ret_type;
480
481 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
482 if (fp->uf_ret_type == NULL)
483 {
484 fp->uf_ret_type = &t_void;
485 return FAIL;
486 }
487 }
488 return OK;
489}
490
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100491/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200492 * Register function "fp" as using "current_funccal" as its scope.
493 */
494 static int
495register_closure(ufunc_T *fp)
496{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200497 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100498 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200499 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200500 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200501 fp->uf_scoped = current_funccal;
502 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200503
Bram Moolenaar58016442016-07-31 18:30:22 +0200504 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
505 return FAIL;
506 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
507 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200508 return OK;
509}
510
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100511 static void
512set_ufunc_name(ufunc_T *fp, char_u *name)
513{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100514 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
515 // actually extends beyond the struct.
516 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100517
518 if (name[0] == K_SPECIAL)
519 {
520 fp->uf_name_exp = alloc(STRLEN(name) + 3);
521 if (fp->uf_name_exp != NULL)
522 {
523 STRCPY(fp->uf_name_exp, "<SNR>");
524 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
525 }
526 }
527}
528
Bram Moolenaar58016442016-07-31 18:30:22 +0200529/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100530 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
531 * return "buf" filled with a readable function name.
532 * Otherwise just return "name", thus the return value can always be used.
533 * "name" and "buf" may be equal.
534 */
535 char_u *
536make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
537{
538 size_t len;
539
540 if (name[0] != K_SPECIAL)
541 return name;
542 len = STRLEN(name);
543 if (len + 3 > bufsize)
544 return name;
545
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100546 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100547 mch_memmove(buf, "<SNR>", 5);
548 return buf;
549}
550
551/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200552 * Get a name for a lambda. Returned in static memory.
553 */
554 char_u *
555get_lambda_name(void)
556{
557 static char_u name[30];
558 static int lambda_no = 0;
559
560 sprintf((char*)name, "<lambda>%d", ++lambda_no);
561 return name;
562}
563
Bram Moolenaar801ab062020-06-25 19:27:56 +0200564#if defined(FEAT_LUA) || defined(PROTO)
565/*
566 * Registers a native C callback which can be called from Vim script.
567 * Returns the name of the Vim script function.
568 */
569 char_u *
570register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
571{
572 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200573 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200574
575 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
576 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200577 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200578
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200579 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200580 fp->uf_refcount = 1;
581 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000582 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200583 fp->uf_calls = 0;
584 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200585 fp->uf_cb = cb;
586 fp->uf_cb_free = cb_free;
587 fp->uf_cb_state = state;
588
589 set_ufunc_name(fp, name);
590 hash_add(&func_hashtab, UF2HIKEY(fp));
591
592 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200593}
594#endif
595
Bram Moolenaar04b12692020-05-04 23:24:44 +0200596/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100597 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100598 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100599 * If "white_error" is not NULL check for correct use of white space and set
600 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100601 * Return NULL if no valid arrow found.
602 */
603 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100604skip_arrow(
605 char_u *start,
606 int equal_arrow,
607 char_u **ret_type,
608 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100609{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100610 char_u *s = start;
611 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100612
613 if (equal_arrow)
614 {
615 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100616 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100617 if (white_error != NULL && !VIM_ISWHITE(s[1]))
618 {
619 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100620 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100621 return NULL;
622 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100623 s = skipwhite(s + 1);
624 *ret_type = s;
625 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100626 if (s == *ret_type)
627 {
628 emsg(_(e_missing_return_type));
629 return NULL;
630 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100631 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100632 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100633 s = skipwhite(s);
634 if (*s != '=')
635 return NULL;
636 ++s;
637 }
638 if (*s != '>')
639 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100640 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
641 || !IS_WHITE_OR_NUL(s[1])))
642 {
643 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100644 semsg(_(e_white_space_required_before_and_after_str_at_str),
645 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100646 return NULL;
647 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100648 return skipwhite(s + 1);
649}
650
651/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100652 * Check if "*cmd" points to a function command and if so advance "*cmd" and
653 * return TRUE.
654 * Otherwise return FALSE;
655 * Do not consider "function(" to be a command.
656 */
657 static int
658is_function_cmd(char_u **cmd)
659{
660 char_u *p = *cmd;
661
662 if (checkforcmd(&p, "function", 2))
663 {
664 if (*p == '(')
665 return FALSE;
666 *cmd = p;
667 return TRUE;
668 }
669 return FALSE;
670}
671
672/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200673 * Called when defining a function: The context may be needed for script
674 * variables declared in a block that is visible now but not when the function
675 * is compiled or called later.
676 */
677 static void
678function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
679{
680 if (cstack != NULL && cstack->cs_idx >= 0)
681 {
682 int count = cstack->cs_idx + 1;
683 int i;
684
685 fp->uf_block_ids = ALLOC_MULT(int, count);
686 if (fp->uf_block_ids != NULL)
687 {
688 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
689 sizeof(int) * count);
690 fp->uf_block_depth = count;
691 }
692
693 // Set flag in each block to indicate a function was defined. This
694 // is used to keep the variable when leaving the block, see
695 // hide_script_var().
696 for (i = 0; i <= cstack->cs_idx; ++i)
697 cstack->cs_flags[i] |= CSF_FUNC_DEF;
698 }
699}
700
701/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100702 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200703 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100704 * "newlines" must already have been initialized.
705 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
706 */
707 static int
708get_function_body(
709 exarg_T *eap,
710 garray_T *newlines,
711 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000712 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100713{
714 linenr_T sourcing_lnum_top = SOURCING_LNUM;
715 linenr_T sourcing_lnum_off;
716 int saved_wait_return = need_wait_return;
717 char_u *line_arg = line_arg_in;
718 int vim9_function = eap->cmdidx == CMD_def
719 || eap->cmdidx == CMD_block;
720#define MAX_FUNC_NESTING 50
721 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200722 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100723 int nesting = 0;
724 getline_opt_T getline_options;
725 int indent = 2;
726 char_u *skip_until = NULL;
727 int ret = FAIL;
728 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200729 int heredoc_concat_len = 0;
730 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100731 char_u *heredoc_trimmed = NULL;
732
Bram Moolenaar20677332021-06-06 17:02:53 +0200733 ga_init2(&heredoc_ga, 1, 500);
734
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100735 // Detect having skipped over comment lines to find the return
736 // type. Add NULL lines to keep the line count correct.
737 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
738 if (SOURCING_LNUM < sourcing_lnum_off)
739 {
740 sourcing_lnum_off -= SOURCING_LNUM;
741 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
742 goto theend;
743 while (sourcing_lnum_off-- > 0)
744 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
745 }
746
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200747 nesting_def[0] = vim9_function;
748 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100749 getline_options = vim9_function
750 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
751 for (;;)
752 {
753 char_u *theline;
754 char_u *p;
755 char_u *arg;
756
757 if (KeyTyped)
758 {
759 msg_scroll = TRUE;
760 saved_wait_return = FALSE;
761 }
762 need_wait_return = FALSE;
763
764 if (line_arg != NULL)
765 {
766 // Use eap->arg, split up in parts by line breaks.
767 theline = line_arg;
768 p = vim_strchr(theline, '\n');
769 if (p == NULL)
770 line_arg += STRLEN(line_arg);
771 else
772 {
773 *p = NUL;
774 line_arg = p + 1;
775 }
776 }
777 else
778 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000779 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100780 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100781 }
782 if (KeyTyped)
783 lines_left = Rows - 1;
784 if (theline == NULL)
785 {
786 // Use the start of the function for the line number.
787 SOURCING_LNUM = sourcing_lnum_top;
788 if (skip_until != NULL)
789 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200790 else if (nesting_inline[nesting])
791 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100792 else if (eap->cmdidx == CMD_def)
793 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100794 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000795 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100796 goto theend;
797 }
798
799 // Detect line continuation: SOURCING_LNUM increased more than one.
800 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
801 if (SOURCING_LNUM < sourcing_lnum_off)
802 sourcing_lnum_off -= SOURCING_LNUM;
803 else
804 sourcing_lnum_off = 0;
805
806 if (skip_until != NULL)
807 {
808 // Don't check for ":endfunc"/":enddef" between
809 // * ":append" and "."
810 // * ":python <<EOF" and "EOF"
811 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
812 if (heredoc_trimmed == NULL
813 || (is_heredoc && skipwhite(theline) == theline)
814 || STRNCMP(theline, heredoc_trimmed,
815 STRLEN(heredoc_trimmed)) == 0)
816 {
817 if (heredoc_trimmed == NULL)
818 p = theline;
819 else if (is_heredoc)
820 p = skipwhite(theline) == theline
821 ? theline : theline + STRLEN(heredoc_trimmed);
822 else
823 p = theline + STRLEN(heredoc_trimmed);
824 if (STRCMP(p, skip_until) == 0)
825 {
826 VIM_CLEAR(skip_until);
827 VIM_CLEAR(heredoc_trimmed);
828 getline_options = vim9_function
829 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
830 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200831
832 if (heredoc_concat_len > 0)
833 {
834 // Replace the starting line with all the concatenated
835 // lines.
836 ga_concat(&heredoc_ga, theline);
837 vim_free(((char_u **)(newlines->ga_data))[
838 heredoc_concat_len - 1]);
839 ((char_u **)(newlines->ga_data))[
840 heredoc_concat_len - 1] = heredoc_ga.ga_data;
841 ga_init(&heredoc_ga);
842 heredoc_concat_len = 0;
843 theline += STRLEN(theline); // skip the "EOF"
844 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100845 }
846 }
847 }
848 else
849 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200850 int c;
851 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +0000852 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100853
854 // skip ':' and blanks
855 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
856 ;
857
858 // Check for "endfunction", "enddef" or "}".
859 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +0000860 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200861 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100862 ? *p == '}'
863 : (checkforcmd(&p, nesting_def[nesting]
864 ? "enddef" : "endfunction", 4)
865 && *p != ':'))
866 {
Bram Moolenaarb2175222022-03-05 20:24:41 +0000867 if (!nesting_inline[nesting] && nesting_def[nesting]
868 && p < cmd + 6)
869 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100870 if (nesting-- == 0)
871 {
872 char_u *nextcmd = NULL;
873
874 if (*p == '|' || *p == '}')
875 nextcmd = p + 1;
876 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
877 nextcmd = line_arg;
878 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100879 && (vim9_function || p_verbose > 0))
880 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200881 SOURCING_LNUM = sourcing_lnum_top
882 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100883 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000884 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100885 else
886 give_warning2((char_u *)
887 _("W22: Text found after :endfunction: %s"),
888 p, TRUE);
889 }
890 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100891 {
892 // Another command follows. If the line came from "eap"
893 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000894 // change "eap->cmdlinep" to point to the last fetched
895 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100896 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000897 if (lines_to_free->ga_len > 0
898 && *eap->cmdlinep !=
899 ((char_u **)lines_to_free->ga_data)
900 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100901 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000902 // *cmdlinep will be freed later, thus remove the
903 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100904 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000905 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
906 [lines_to_free->ga_len - 1];
907 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100908 }
909 }
910 break;
911 }
912 }
913
914 // Check for mismatched "endfunc" or "enddef".
915 // We don't check for "def" inside "func" thus we also can't check
916 // for "enddef".
917 // We continue to find the end of the function, although we might
918 // not find it.
919 else if (nesting_def[nesting])
920 {
921 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
922 emsg(_(e_mismatched_endfunction));
923 }
924 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
925 emsg(_(e_mismatched_enddef));
926
927 // Increase indent inside "if", "while", "for" and "try", decrease
928 // at "end".
929 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
930 indent -= 2;
931 else if (STRNCMP(p, "if", 2) == 0
932 || STRNCMP(p, "wh", 2) == 0
933 || STRNCMP(p, "for", 3) == 0
934 || STRNCMP(p, "try", 3) == 0)
935 indent += 2;
936
937 // Check for defining a function inside this function.
938 // Only recognize "def" inside "def", not inside "function",
939 // For backwards compatibility, see Test_function_python().
940 c = *p;
941 if (is_function_cmd(&p)
942 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
943 {
944 if (*p == '!')
945 p = skipwhite(p + 1);
946 p += eval_fname_script(p);
947 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL,
948 NULL, NULL));
949 if (*skipwhite(p) == '(')
950 {
951 if (nesting == MAX_FUNC_NESTING - 1)
952 emsg(_(e_function_nesting_too_deep));
953 else
954 {
955 ++nesting;
956 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200957 nesting_inline[nesting] = FALSE;
958 indent += 2;
959 }
960 }
961 }
962
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200963 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200964 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200965 // Not a comment line: check for nested inline function.
966 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200967 while (end > p && VIM_ISWHITE(*end))
968 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +0200969 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200970 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200971 int is_block;
972
973 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200974 --end;
975 while (end > p && VIM_ISWHITE(*end))
976 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200977 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
978 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200979 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200980 char_u *s = p;
981
982 // check for line starting with "au" for :autocmd or
983 // "com" for :command, these can use a {} block
984 is_block = checkforcmd_noparen(&s, "autocmd", 2)
985 || checkforcmd_noparen(&s, "command", 3);
986 }
987
988 if (is_block)
989 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200990 if (nesting == MAX_FUNC_NESTING - 1)
991 emsg(_(e_function_nesting_too_deep));
992 else
993 {
994 ++nesting;
995 nesting_def[nesting] = TRUE;
996 nesting_inline[nesting] = TRUE;
997 indent += 2;
998 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100999 }
1000 }
1001 }
1002
1003 // Check for ":append", ":change", ":insert". Not for :def.
1004 p = skip_range(p, FALSE, NULL);
1005 if (!vim9_function
1006 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1007 || (p[0] == 'c'
1008 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1009 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1010 && (STRNCMP(&p[3], "nge", 3) != 0
1011 || !ASCII_ISALPHA(p[6])))))))
1012 || (p[0] == 'i'
1013 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1014 && (!ASCII_ISALPHA(p[2])
1015 || (p[2] == 's'
1016 && (!ASCII_ISALPHA(p[3])
1017 || p[3] == 'e'))))))))
1018 skip_until = vim_strsave((char_u *)".");
1019
1020 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1021 arg = skipwhite(skiptowhite(p));
1022 if (arg[0] == '<' && arg[1] =='<'
1023 && ((p[0] == 'p' && p[1] == 'y'
1024 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1025 || ((p[2] == '3' || p[2] == 'x')
1026 && !ASCII_ISALPHA(p[3]))))
1027 || (p[0] == 'p' && p[1] == 'e'
1028 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1029 || (p[0] == 't' && p[1] == 'c'
1030 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1031 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1032 && !ASCII_ISALPHA(p[3]))
1033 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1034 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1035 || (p[0] == 'm' && p[1] == 'z'
1036 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1037 ))
1038 {
1039 // ":python <<" continues until a dot, like ":append"
1040 p = skipwhite(arg + 2);
1041 if (STRNCMP(p, "trim", 4) == 0)
1042 {
1043 // Ignore leading white space.
1044 p = skipwhite(p + 4);
1045 heredoc_trimmed = vim_strnsave(theline,
1046 skipwhite(theline) - theline);
1047 }
1048 if (*p == NUL)
1049 skip_until = vim_strsave((char_u *)".");
1050 else
1051 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1052 getline_options = GETLINE_NONE;
1053 is_heredoc = TRUE;
Bram Moolenaar20677332021-06-06 17:02:53 +02001054 if (eap->cmdidx == CMD_def)
1055 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001056 }
1057
1058 // Check for ":cmd v =<< [trim] EOF"
1059 // and ":cmd [a, b] =<< [trim] EOF"
1060 // and "lines =<< [trim] EOF" for Vim9
1061 // Where "cmd" can be "let", "var", "final" or "const".
1062 arg = skipwhite(skiptowhite(p));
1063 if (*arg == '[')
1064 arg = vim_strchr(arg, ']');
1065 if (arg != NULL)
1066 {
1067 int found = (eap->cmdidx == CMD_def && arg[0] == '='
1068 && arg[1] == '<' && arg[2] =='<');
1069
1070 if (!found)
1071 // skip over the argument after "cmd"
1072 arg = skipwhite(skiptowhite(arg));
1073 if (found || (arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
1074 && (checkforcmd(&p, "let", 2)
1075 || checkforcmd(&p, "var", 3)
1076 || checkforcmd(&p, "final", 5)
1077 || checkforcmd(&p, "const", 5))))
1078 {
1079 p = skipwhite(arg + 3);
1080 if (STRNCMP(p, "trim", 4) == 0)
1081 {
1082 // Ignore leading white space.
1083 p = skipwhite(p + 4);
1084 heredoc_trimmed = vim_strnsave(theline,
1085 skipwhite(theline) - theline);
1086 }
1087 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1088 getline_options = GETLINE_NONE;
1089 is_heredoc = TRUE;
1090 }
1091 }
1092 }
1093
1094 // Add the line to the function.
1095 if (ga_grow(newlines, 1 + sourcing_lnum_off) == FAIL)
1096 goto theend;
1097
Bram Moolenaar20677332021-06-06 17:02:53 +02001098 if (heredoc_concat_len > 0)
1099 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001100 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001101 // to be used for the instruction later.
1102 ga_concat(&heredoc_ga, theline);
1103 ga_concat(&heredoc_ga, (char_u *)"\n");
1104 p = vim_strsave((char_u *)"");
1105 }
1106 else
1107 {
1108 // Copy the line to newly allocated memory. get_one_sourceline()
1109 // allocates 250 bytes per line, this saves 80% on average. The
1110 // cost is an extra alloc/free.
1111 p = vim_strsave(theline);
1112 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001113 if (p == NULL)
1114 goto theend;
1115 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1116
1117 // Add NULL lines for continuation lines, so that the line count is
1118 // equal to the index in the growarray.
1119 while (sourcing_lnum_off-- > 0)
1120 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1121
1122 // Check for end of eap->arg.
1123 if (line_arg != NULL && *line_arg == NUL)
1124 line_arg = NULL;
1125 }
1126
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001127 // Return OK when no error was detected.
1128 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001129 ret = OK;
1130
1131theend:
1132 vim_free(skip_until);
1133 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001134 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001135 need_wait_return |= saved_wait_return;
1136 return ret;
1137}
1138
1139/*
1140 * Handle the body of a lambda. *arg points to the "{", process statements
1141 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001142 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001143 * When successful "rettv" is set to a funcref.
1144 */
1145 static int
1146lambda_function_body(
1147 char_u **arg,
1148 typval_T *rettv,
1149 evalarg_T *evalarg,
1150 garray_T *newargs,
1151 garray_T *argtypes,
1152 int varargs,
1153 garray_T *default_args,
1154 char_u *ret_type)
1155{
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001156 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001157 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001158 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001159 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001160 exarg_T eap;
1161 garray_T newlines;
1162 char_u *cmdline = NULL;
1163 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001164 partial_T *pt;
1165 char_u *name;
1166 int lnum_save = -1;
1167 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1168
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001169 if (!ends_excmd2(*arg, skipwhite(*arg + 1)))
1170 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001171 semsg(_(e_trailing_characters_str), *arg + 1);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001172 return FAIL;
1173 }
1174
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001175 CLEAR_FIELD(eap);
1176 eap.cmdidx = CMD_block;
1177 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001178 eap.cmdlinep = &cmdline;
1179 eap.skip = !evaluate;
1180 if (evalarg->eval_cctx != NULL)
1181 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1182 else
1183 {
1184 eap.getline = evalarg->eval_getline;
1185 eap.cookie = evalarg->eval_cookie;
1186 }
1187
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001188 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001189 if (get_function_body(&eap, &newlines, NULL,
1190 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001191 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001192
1193 // When inside a lambda must add the function lines to evalarg.eval_ga.
1194 evalarg->eval_break_count += newlines.ga_len;
1195 if (gap->ga_itemsize > 0)
1196 {
1197 int idx;
1198 char_u *last;
1199 size_t plen;
1200 char_u *pnl;
1201
1202 for (idx = 0; idx < newlines.ga_len; ++idx)
1203 {
1204 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1205
Bram Moolenaarecb66452021-05-18 15:09:18 +02001206 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001207 goto erret;
1208
1209 // Going to concatenate the lines after parsing. For an empty or
1210 // comment line use an empty string.
1211 // Insert NL characters at the start of each line, the string will
1212 // be split again later in .get_lambda_tv().
1213 if (*p == NUL || vim9_comment_start(p))
1214 p = (char_u *)"";
1215 plen = STRLEN(p);
1216 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1217 if (pnl != NULL)
1218 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001219 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1220 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001221 }
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;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001224 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001225 // more is following after the "}", which was skipped
1226 last = cmdline;
1227 else
1228 // nothing is following the "}"
1229 last = (char_u *)"}";
1230 plen = STRLEN(last);
1231 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1232 if (pnl != NULL)
1233 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001234 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1235 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001236 }
1237
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001238 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001239 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001240 garray_T *tfgap = &evalarg->eval_tofree_ga;
1241
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001242 // Something comes after the "}".
1243 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001244
1245 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001246 if (ga_grow(tfgap, 1) == OK)
1247 {
1248 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1249 evalarg->eval_using_cmdline = TRUE;
1250 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001251 }
1252 else
1253 *arg = (char_u *)"";
1254
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001255 if (!evaluate)
1256 {
1257 ret = OK;
1258 goto erret;
1259 }
1260
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001261 name = get_lambda_name();
1262 ufunc = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
1263 if (ufunc == NULL)
1264 goto erret;
1265 set_ufunc_name(ufunc, name);
1266 if (hash_add(&func_hashtab, UF2HIKEY(ufunc)) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001267 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001268 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001269 ufunc->uf_refcount = 1;
1270 ufunc->uf_args = *newargs;
1271 newargs->ga_data = NULL;
1272 ufunc->uf_def_args = *default_args;
1273 default_args->ga_data = NULL;
1274 ufunc->uf_func_type = &t_func_any;
1275
1276 // error messages are for the first function line
1277 lnum_save = SOURCING_LNUM;
1278 SOURCING_LNUM = sourcing_lnum_top;
1279
1280 // parse argument types
1281 if (parse_argument_types(ufunc, argtypes, varargs) == FAIL)
1282 {
1283 SOURCING_LNUM = lnum_save;
1284 goto erret;
1285 }
1286
1287 // parse the return type, if any
1288 if (parse_return_type(ufunc, ret_type) == FAIL)
1289 goto erret;
1290
1291 pt = ALLOC_CLEAR_ONE(partial_T);
1292 if (pt == NULL)
1293 goto erret;
1294 pt->pt_func = ufunc;
1295 pt->pt_refcount = 1;
1296
1297 ufunc->uf_lines = newlines;
1298 newlines.ga_data = NULL;
1299 if (sandbox)
1300 ufunc->uf_flags |= FC_SANDBOX;
1301 if (!ASCII_ISUPPER(*ufunc->uf_name))
1302 ufunc->uf_flags |= FC_VIM9;
1303 ufunc->uf_script_ctx = current_sctx;
1304 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1305 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1306 set_function_type(ufunc);
1307
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001308 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1309
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001310 rettv->vval.v_partial = pt;
1311 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001312 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001313 ret = OK;
1314
1315erret:
1316 if (lnum_save >= 0)
1317 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001318 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001319 if (newargs != NULL)
1320 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001321 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001322 if (ufunc != NULL)
1323 {
1324 func_clear(ufunc, TRUE);
1325 func_free(ufunc, TRUE);
1326 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001327 return ret;
1328}
1329
1330/*
1331 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001332 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001333 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001334 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1335 */
1336 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001337get_lambda_tv(
1338 char_u **arg,
1339 typval_T *rettv,
1340 int types_optional,
1341 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001342{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001343 int evaluate = evalarg != NULL
1344 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001345 garray_T newargs;
1346 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001347 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001348 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001349 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001350 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001351 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001352 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001353 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001354 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001355 char_u *s;
1356 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001357 int *old_eval_lavars = eval_lavars_used;
1358 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001359 char_u *tofree1 = NULL;
1360 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001361 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001362 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001363 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001364 int vim9script = in_vim9script();
Bram Moolenaar65c44152020-12-24 15:14:01 +01001365
Bram Moolenaar4525a572022-02-13 11:57:33 +00001366 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001367 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001368
1369 ga_init(&newargs);
1370 ga_init(&newlines);
1371
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001372 // First, check if this is really a lambda expression. "->" or "=>" must
1373 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001374 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001375 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001376 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar14ded112021-06-26 19:25:49 +02001377 NULL, &default_args, TRUE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001378 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001379 {
1380 if (types_optional)
1381 ga_clear_strings(&argtypes);
Bram Moolenaar0346b792021-01-31 22:18:29 +01001382 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001383 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001384
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001385 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001386 if (evaluate)
1387 pnewargs = &newargs;
1388 else
1389 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001390 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001391 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001392 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001393 &varargs, &default_args,
1394 FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001395 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001396 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001397 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001398 {
1399 if (types_optional)
1400 ga_clear_strings(&argtypes);
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001401 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001402 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001403 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001404 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001405
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001406 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1407 if (ret_type != NULL)
1408 {
1409 ret_type = vim_strsave(ret_type);
1410 tofree2 = ret_type;
1411 }
1412
Bram Moolenaare38eab22019-12-05 21:50:01 +01001413 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001414 if (evaluate)
1415 eval_lavars_used = &eval_lavars;
1416
Bram Moolenaar65c44152020-12-24 15:14:01 +01001417 *arg = skipwhite_and_linebreak(*arg, evalarg);
1418
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001419 // Recognize "{" as the start of a function body.
1420 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001421 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001422 if (evalarg == NULL)
1423 // cannot happen?
1424 goto theend;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001425 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1426 types_optional ? &argtypes : NULL, varargs,
1427 &default_args, ret_type) == FAIL)
1428 goto errret;
1429 goto theend;
1430 }
1431 if (default_args.ga_len > 0)
1432 {
1433 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001434 goto errret;
1435 }
1436
Bram Moolenaare38eab22019-12-05 21:50:01 +01001437 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001438 start = *arg;
1439 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001440 if (ret == FAIL)
1441 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001442 if (evalarg != NULL)
1443 {
1444 // avoid that the expression gets freed when another line break follows
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001445 tofree1 = evalarg->eval_tofree;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001446 evalarg->eval_tofree = NULL;
1447 }
1448
Bram Moolenaar65c44152020-12-24 15:14:01 +01001449 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001450 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001451 *arg = skipwhite_and_linebreak(*arg, evalarg);
1452 if (**arg != '}')
1453 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001454 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001455 goto errret;
1456 }
1457 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001458 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001459
1460 if (evaluate)
1461 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001462 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001463 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001464 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001465 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001466 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001467
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001468 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001469 if (fp == NULL)
1470 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001471 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001472 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001473 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001474 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001475
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001476 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001477 if (ga_grow(&newlines, 1) == FAIL)
1478 goto errret;
1479
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001480 // If there are line breaks, we need to split up the string.
1481 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001482 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001483 line_end = end;
1484
1485 // Add "return " before the expression (or the first line).
1486 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001487 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001488 if (p == NULL)
1489 goto errret;
1490 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1491 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001492 vim_strncpy(p + 7, start, line_end - start);
1493
1494 if (line_end != end)
1495 {
1496 // Add more lines, split by line breaks. Thus is used when a
1497 // lambda with { cmds } is encountered.
1498 while (*line_end == '\n')
1499 {
1500 if (ga_grow(&newlines, 1) == FAIL)
1501 goto errret;
1502 start = line_end + 1;
1503 line_end = vim_strchr(start, '\n');
1504 if (line_end == NULL)
1505 line_end = end;
1506 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1507 vim_strnsave(start, line_end - start);
1508 }
1509 }
1510
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001511 if (strstr((char *)p + 7, "a:") == NULL)
1512 // No a: variables are used for sure.
1513 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001514
1515 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001516 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001517 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001518 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001519 if (types_optional)
1520 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001521 if (parse_argument_types(fp, &argtypes,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001522 vim9script && varargs) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001523 goto errret;
1524 if (ret_type != NULL)
1525 {
1526 fp->uf_ret_type = parse_type(&ret_type,
1527 &fp->uf_type_list, TRUE);
1528 if (fp->uf_ret_type == NULL)
1529 goto errret;
1530 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001531 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001532 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001533 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001534
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001535 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001536 if (current_funccal != NULL && eval_lavars)
1537 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001538 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001539 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001540 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001541 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001542
1543#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001544 if (prof_def_func())
1545 func_do_profile(fp);
1546#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001547 if (sandbox)
1548 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001549 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001550 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001551 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001552 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001553 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001554 fp->uf_script_ctx = current_sctx;
Bram Moolenaara755fdb2021-11-20 21:35:41 +00001555 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len + 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001556
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001557 function_using_block_scopes(fp, evalarg->eval_cstack);
1558
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001559 pt->pt_func = fp;
1560 pt->pt_refcount = 1;
1561 rettv->vval.v_partial = pt;
1562 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001563
1564 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001565 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001566
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001567theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001568 eval_lavars_used = old_eval_lavars;
Bram Moolenaarefaaaa62020-07-08 22:24:09 +02001569 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001570 evalarg->eval_tofree = tofree1;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001571 else
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001572 vim_free(tofree1);
1573 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001574 if (types_optional)
1575 ga_clear_strings(&argtypes);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001576
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001577 return OK;
1578
1579errret:
1580 ga_clear_strings(&newargs);
1581 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001582 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001583 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001584 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001585 ga_clear_strings(&argtypes);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001586 if (fp != NULL)
1587 vim_free(fp->uf_arg_types);
1588 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001589 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001590 vim_free(pt);
Bram Moolenaarefaaaa62020-07-08 22:24:09 +02001591 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001592 evalarg->eval_tofree = tofree1;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001593 else
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001594 vim_free(tofree1);
1595 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001596 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001597 return FAIL;
1598}
1599
1600/*
1601 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1602 * name it contains, otherwise return "name".
1603 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1604 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001605 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1606 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001607 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001608 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001609 */
1610 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001611deref_func_name(
1612 char_u *name,
1613 int *lenp,
1614 partial_T **partialp,
1615 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001616 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001617 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001618 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001619{
1620 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001621 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001622 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001623 char_u *s = NULL;
1624 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001625 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001626
1627 if (partialp != NULL)
1628 *partialp = NULL;
1629
1630 cc = name[*lenp];
1631 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001632
Bram Moolenaar71f21932022-01-07 18:20:55 +00001633 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001634 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001635 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001636 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001637 tv = &v->di_tv;
1638 }
1639 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1640 {
1641 imported_T *import;
1642 char_u *p = name;
1643 int len = *lenp;
1644
1645 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001646 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001647 p = name + 2;
1648 len -= 2;
1649 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001650 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001651
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001652 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001653 if (import != NULL)
1654 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001655 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001656 if (new_function)
1657 semsg(_(e_redefining_imported_item_str), name);
1658 else
1659 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001660 name[len] = cc;
1661 *lenp = 0;
1662 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001663 }
1664 }
1665
1666 if (tv != NULL)
1667 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001668 if (found_var != NULL)
1669 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001670 if (tv->v_type == VAR_FUNC)
1671 {
1672 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001673 {
1674 *lenp = 0;
1675 return (char_u *)""; // just in case
1676 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001677 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001678 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001679 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001680
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001681 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001682 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001683 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001684
1685 if (pt == NULL)
1686 {
1687 *lenp = 0;
1688 return (char_u *)""; // just in case
1689 }
1690 if (partialp != NULL)
1691 *partialp = pt;
1692 s = partial_name(pt);
1693 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001694 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001695
1696 if (s != NULL)
1697 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001698 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001699 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001700 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001701
1702 if (sv != NULL)
1703 *type = sv->sv_type;
1704 }
1705 return s;
1706 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001707 }
1708
1709 return name;
1710}
1711
1712/*
1713 * Give an error message with a function name. Handle <SNR> things.
1714 * "ermsg" is to be passed without translation, use N_() instead of _().
1715 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001716 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001717emsg_funcname(char *ermsg, char_u *name)
1718{
Bram Moolenaar54598062022-01-13 12:05:09 +00001719 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001720
Bram Moolenaar54598062022-01-13 12:05:09 +00001721 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001722 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001723 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001724 if (p != name)
1725 vim_free(p);
1726}
1727
1728/*
1729 * Allocate a variable for the result of a function.
1730 * Return OK or FAIL.
1731 */
1732 int
1733get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001734 char_u *name, // name of the function
1735 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001736 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001737 char_u **arg, // argument, pointing to the '('
Bram Moolenaare6b53242020-07-01 17:28:33 +02001738 evalarg_T *evalarg, // for line continuation
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001739 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001740{
1741 char_u *argp;
1742 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001743 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1744 int argcount = 0; // number of arguments found
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001745 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001746 int evaluate = evalarg == NULL
1747 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001748
1749 /*
1750 * Get the arguments.
1751 */
1752 argp = *arg;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001753 while (argcount < MAX_FUNC_ARGS - (funcexe->fe_partial == NULL ? 0
1754 : funcexe->fe_partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001755 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001756 // skip the '(' or ',' and possibly line breaks
1757 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1758
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001759 if (*argp == ')' || *argp == ',' || *argp == NUL)
1760 break;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001761 if (eval1(&argp, &argvars[argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001762 {
1763 ret = FAIL;
1764 break;
1765 }
1766 ++argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001767 // The comma should come right after the argument, but this wasn't
1768 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001769 if (vim9script)
1770 {
1771 if (*argp != ',' && *skipwhite(argp) == ',')
1772 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001773 if (evaluate)
1774 semsg(_(e_no_white_space_allowed_before_str_str),
1775 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001776 ret = FAIL;
1777 break;
1778 }
1779 }
1780 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001781 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001782 if (*argp != ',')
1783 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001784 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
1785 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001786 if (evaluate)
1787 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001788 ret = FAIL;
1789 break;
1790 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001791 }
Bram Moolenaare6b53242020-07-01 17:28:33 +02001792 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001793 if (*argp == ')')
1794 ++argp;
1795 else
1796 ret = FAIL;
1797
1798 if (ret == OK)
1799 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001800 int i = 0;
1801 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001802
1803 if (get_vim_var_nr(VV_TESTING))
1804 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001805 // Prepare for calling test_garbagecollect_now(), need to know
1806 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001807 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001808 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001809 for (i = 0; i < argcount; ++i)
1810 if (ga_grow(&funcargs, 1) == OK)
1811 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1812 &argvars[i];
1813 }
1814
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001815 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00001816 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001817 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001818 // An error in a builtin function does not return FAIL, but we do
1819 // want to abort further processing if an error was given.
1820 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001821 clear_tv(rettv);
1822 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001823
1824 funcargs.ga_len -= i;
1825 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001826 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001827 {
1828 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001829 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001830 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001831 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001832 }
1833
1834 while (--argcount >= 0)
1835 clear_tv(&argvars[argcount]);
1836
Bram Moolenaar4525a572022-02-13 11:57:33 +00001837 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02001838 *arg = argp;
1839 else
1840 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001841 return ret;
1842}
1843
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001844/*
1845 * Return TRUE if "p" starts with "<SID>" or "s:".
1846 * Only works if eval_fname_script() returned non-zero for "p"!
1847 */
1848 static int
1849eval_fname_sid(char_u *p)
1850{
1851 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
1852}
1853
1854/*
1855 * In a script change <SID>name() and s:name() to K_SNR 123_name().
1856 * Change <SNR>123_name() to K_SNR 123_name().
1857 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
1858 * (slow).
1859 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001860 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001861fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
1862{
1863 int llen;
1864 char_u *fname;
1865 int i;
1866
1867 llen = eval_fname_script(name);
1868 if (llen > 0)
1869 {
1870 fname_buf[0] = K_SPECIAL;
1871 fname_buf[1] = KS_EXTRA;
1872 fname_buf[2] = (int)KE_SNR;
1873 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001874 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001875 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001876 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001877 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001878 else
1879 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001880 sprintf((char *)fname_buf + 3, "%ld_",
1881 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001882 i = (int)STRLEN(fname_buf);
1883 }
1884 }
1885 if (i + STRLEN(name + llen) < FLEN_FIXED)
1886 {
1887 STRCPY(fname_buf + i, name + llen);
1888 fname = fname_buf;
1889 }
1890 else
1891 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001892 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001893 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +01001894 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001895 else
1896 {
1897 *tofree = fname;
1898 mch_memmove(fname, fname_buf, (size_t)i);
1899 STRCPY(fname + i, name + llen);
1900 }
1901 }
1902 }
1903 else
1904 fname = name;
1905 return fname;
1906}
1907
1908/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001909 * Concatenate the script ID and function name into "<SNR>99_name".
1910 * "buffer" must have size MAX_FUNC_NAME_LEN.
1911 */
1912 void
1913func_name_with_sid(char_u *name, int sid, char_u *buffer)
1914{
1915 // A script-local function is stored as "<SNR>99_name".
1916 buffer[0] = K_SPECIAL;
1917 buffer[1] = KS_EXTRA;
1918 buffer[2] = (int)KE_SNR;
1919 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
1920 (long)sid, name);
1921}
1922
1923/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 * Find a function "name" in script "sid".
1925 */
1926 static ufunc_T *
1927find_func_with_sid(char_u *name, int sid)
1928{
Bram Moolenaarb8822442022-01-11 15:24:05 +00001929 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001930 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931
Bram Moolenaarb8822442022-01-11 15:24:05 +00001932 if (!SCRIPT_ID_VALID(sid))
1933 return NULL; // not in a script
1934
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001935 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001936 hi = hash_find(&func_hashtab, buffer);
1937 if (!HASHITEM_EMPTY(hi))
1938 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00001939 return NULL;
1940}
1941
1942/*
1943 * Find a function "name" in script "sid" prefixing the autoload prefix.
1944 */
1945 static ufunc_T *
1946find_func_with_prefix(char_u *name, int sid)
1947{
1948 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001949 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00001950 scriptitem_T *si;
1951
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001952 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00001953 return NULL; // already has the prefix
1954 if (!SCRIPT_ID_VALID(sid))
1955 return NULL; // not in a script
1956 si = SCRIPT_ITEM(sid);
1957 if (si->sn_autoload_prefix != NULL)
1958 {
1959 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
1960 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00001961 char_u *namep;
1962
1963 // skip a "<SNR>99_" prefix
1964 namep = untrans_function_name(name);
1965 if (namep == NULL)
1966 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00001967
1968 // An exported function in an autoload script is stored as
1969 // "dir#path#name".
1970 if (len < sizeof(buffer))
1971 auto_name = buffer;
1972 else
1973 auto_name = alloc(len);
1974 if (auto_name != NULL)
1975 {
1976 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00001977 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00001978 hi = hash_find(&func_hashtab, auto_name);
1979 if (auto_name != buffer)
1980 vim_free(auto_name);
1981 if (!HASHITEM_EMPTY(hi))
1982 return HI2UF(hi);
1983 }
1984 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985
1986 return NULL;
1987}
1988
1989/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001990 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001991 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
1992 * functions.
1993 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001994 * Return NULL for unknown function.
1995 */
Bram Moolenaarce658352020-07-31 23:47:12 +02001996 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00001997find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001998{
1999 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002001
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002002 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002004 // Find script-local function before global one.
2005 if (in_vim9script() && eval_isnamec1(*name)
2006 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002008 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2009 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002010 if (func != NULL)
2011 return func;
2012 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002013 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2014 {
2015 char_u *p = name + 5;
2016 long sid;
2017
2018 // printable "<SNR>123_Name" form
2019 sid = getdigits(&p);
2020 if (*p == '_')
2021 {
2022 func = find_func_with_sid(p + 1, (int)sid);
2023 if (func != NULL)
2024 return func;
2025 }
2026 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002028
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002029 if ((flags & FFED_NO_GLOBAL) == 0)
2030 {
2031 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002032 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002033 if (!HASHITEM_EMPTY(hi))
2034 return HI2UF(hi);
2035 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002036
Bram Moolenaarb8822442022-01-11 15:24:05 +00002037 // Find autoload function if this is an autoload script.
2038 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2039 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040}
2041
2042/*
2043 * Find a function by name, return pointer to it in ufuncs.
2044 * "cctx" is passed in a :def function to find imported functions.
2045 * Return NULL for unknown or dead function.
2046 */
2047 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002048find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002049{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002050 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002051
2052 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2053 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002054 return NULL;
2055}
2056
2057/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002058 * Return TRUE if "ufunc" is a global function.
2059 */
2060 int
2061func_is_global(ufunc_T *ufunc)
2062{
2063 return ufunc->uf_name[0] != K_SPECIAL;
2064}
2065
2066/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002067 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2068 */
2069 int
2070func_requires_g_prefix(ufunc_T *ufunc)
2071{
2072 return ufunc->uf_name[0] != K_SPECIAL
2073 && (ufunc->uf_flags & FC_LAMBDA) == 0
2074 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL;
2075}
2076
2077/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002078 * Copy the function name of "fp" to buffer "buf".
2079 * "buf" must be able to hold the function name plus three bytes.
2080 * Takes care of script-local function names.
2081 */
2082 static void
2083cat_func_name(char_u *buf, ufunc_T *fp)
2084{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002085 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002086 {
2087 STRCPY(buf, "<SNR>");
2088 STRCAT(buf, fp->uf_name + 3);
2089 }
2090 else
2091 STRCPY(buf, fp->uf_name);
2092}
2093
2094/*
2095 * Add a number variable "name" to dict "dp" with value "nr".
2096 */
2097 static void
2098add_nr_var(
2099 dict_T *dp,
2100 dictitem_T *v,
2101 char *name,
2102 varnumber_T nr)
2103{
2104 STRCPY(v->di_key, name);
2105 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2106 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
2107 v->di_tv.v_type = VAR_NUMBER;
2108 v->di_tv.v_lock = VAR_FIXED;
2109 v->di_tv.vval.v_number = nr;
2110}
2111
2112/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002113 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002114 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002115 static void
2116free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002117{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002118 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002119
2120 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
2121 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002122 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002123
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002124 // When garbage collecting a funccall_T may be freed before the
2125 // function that references it, clear its uf_scoped field.
2126 // The function may have been redefined and point to another
2127 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002128 if (fp != NULL && fp->uf_scoped == fc)
2129 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002130 }
Bram Moolenaar58016442016-07-31 18:30:22 +02002131 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002132
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002133 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002134 vim_free(fc);
2135}
2136
2137/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002138 * Free "fc" and what it contains.
2139 * Can be called only when "fc" is kept beyond the period of it called,
2140 * i.e. after cleanup_function_call(fc).
2141 */
2142 static void
2143free_funccal_contents(funccall_T *fc)
2144{
2145 listitem_T *li;
2146
2147 // Free all l: variables.
2148 vars_clear(&fc->l_vars.dv_hashtab);
2149
2150 // Free all a: variables.
2151 vars_clear(&fc->l_avars.dv_hashtab);
2152
2153 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002154 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002155 clear_tv(&li->li_tv);
2156
2157 free_funccal(fc);
2158}
2159
2160/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002161 * Handle the last part of returning from a function: free the local hashtable.
2162 * Unless it is still in use by a closure.
2163 */
2164 static void
2165cleanup_function_call(funccall_T *fc)
2166{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002167 int may_free_fc = fc->fc_refcount <= 0;
2168 int free_fc = TRUE;
2169
Bram Moolenaar6914c642017-04-01 21:21:30 +02002170 current_funccal = fc->caller;
2171
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002172 // Free all l: variables if not referred.
2173 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
2174 vars_clear(&fc->l_vars.dv_hashtab);
2175 else
2176 free_fc = FALSE;
2177
2178 // If the a:000 list and the l: and a: dicts are not referenced and
2179 // there is no closure using it, we can free the funccall_T and what's
2180 // in it.
2181 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
2182 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002183 else
2184 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002185 int todo;
2186 hashitem_T *hi;
2187 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002188
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002189 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002190
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002191 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +02002192 todo = (int)fc->l_avars.dv_hashtab.ht_used;
2193 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
2194 {
2195 if (!HASHITEM_EMPTY(hi))
2196 {
2197 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002198 di = HI2DI(hi);
2199 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002200 }
2201 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002202 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002203
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002204 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2205 fc->l_varlist.lv_first = NULL;
2206 else
2207 {
2208 listitem_T *li;
2209
2210 free_fc = FALSE;
2211
2212 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002213 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002214 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002215 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002216
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002217 if (free_fc)
2218 free_funccal(fc);
2219 else
2220 {
2221 static int made_copy = 0;
2222
2223 // "fc" is still in use. This can happen when returning "a:000",
2224 // assigning "l:" to a global variable or defining a closure.
2225 // Link "fc" in the list for garbage collection later.
2226 fc->caller = previous_funccal;
2227 previous_funccal = fc;
2228
2229 if (want_garbage_collect)
2230 // If garbage collector is ready, clear count.
2231 made_copy = 0;
2232 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002233 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002234 // We have made a lot of copies, worth 4 Mbyte. This can happen
2235 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002236 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002237 // too much memory.
2238 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002239 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002240 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002241 }
2242}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002243
2244/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002245 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2246 */
2247 static int
2248numbered_function(char_u *name)
2249{
2250 return isdigit(*name)
2251 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2252}
2253
2254/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002255 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002256 * 1. ordinary names, function defined with :function or :def;
2257 * can start with "<SNR>123_" literally or with K_SPECIAL.
2258 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002259 * For the first we only count the name stored in func_hashtab as a reference,
2260 * using function() does not count as a reference, because the function is
2261 * looked up by name.
2262 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002263 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002264func_name_refcount(char_u *name)
2265{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002266 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002267}
2268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269/*
2270 * Unreference "fc": decrement the reference count and free it when it
2271 * becomes zero. "fp" is detached from "fc".
2272 * When "force" is TRUE we are exiting.
2273 */
2274 static void
2275funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2276{
2277 funccall_T **pfc;
2278 int i;
2279
2280 if (fc == NULL)
2281 return;
2282
2283 if (--fc->fc_refcount <= 0 && (force || (
2284 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
2285 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
2286 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2287 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
2288 {
2289 if (fc == *pfc)
2290 {
2291 *pfc = fc->caller;
2292 free_funccal_contents(fc);
2293 return;
2294 }
2295 }
2296 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
2297 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
2298 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
2299}
2300
2301/*
2302 * Remove the function from the function hashtable. If the function was
2303 * deleted while it still has references this was already done.
2304 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2305 */
2306 static int
2307func_remove(ufunc_T *fp)
2308{
2309 hashitem_T *hi;
2310
2311 // Return if it was already virtually deleted.
2312 if (fp->uf_flags & FC_DEAD)
2313 return FALSE;
2314
2315 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
2316 if (!HASHITEM_EMPTY(hi))
2317 {
2318 // When there is a def-function index do not actually remove the
2319 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002320 // Do remove it when it's a copy.
2321 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaarc970e422021-03-17 15:03:04 +01002322 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01002324 return FALSE;
2325 }
2326 hash_remove(&func_hashtab, hi);
2327 fp->uf_flags |= FC_DELETED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002328 return TRUE;
2329 }
2330 return FALSE;
2331}
2332
2333 static void
2334func_clear_items(ufunc_T *fp)
2335{
2336 ga_clear_strings(&(fp->uf_args));
2337 ga_clear_strings(&(fp->uf_def_args));
2338 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002340 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002341 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002342 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002343
2344 // Increment the refcount of this function to avoid it being freed
2345 // recursively when the partial is freed.
2346 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002347 partial_unref(fp->uf_partial);
2348 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002349 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002350
2351#ifdef FEAT_LUA
2352 if (fp->uf_cb_free != NULL)
2353 {
2354 fp->uf_cb_free(fp->uf_cb_state);
2355 fp->uf_cb_free = NULL;
2356 }
2357
2358 fp->uf_cb_state = NULL;
2359 fp->uf_cb = NULL;
2360#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361#ifdef FEAT_PROFILE
2362 VIM_CLEAR(fp->uf_tml_count);
2363 VIM_CLEAR(fp->uf_tml_total);
2364 VIM_CLEAR(fp->uf_tml_self);
2365#endif
2366}
2367
2368/*
2369 * Free all things that a function contains. Does not free the function
2370 * itself, use func_free() for that.
2371 * When "force" is TRUE we are exiting.
2372 */
2373 static void
2374func_clear(ufunc_T *fp, int force)
2375{
2376 if (fp->uf_cleared)
2377 return;
2378 fp->uf_cleared = TRUE;
2379
2380 // clear this function
2381 func_clear_items(fp);
2382 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002383 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384}
2385
2386/*
2387 * Free a function and remove it from the list of functions. Does not free
2388 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002389 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002390 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002392 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002393func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394{
2395 // Only remove it when not done already, otherwise we would remove a newer
2396 // version of the function with the same name.
2397 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2398 func_remove(fp);
2399
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002400 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002401 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002402 if (fp->uf_dfunc_idx > 0)
2403 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002404 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002406 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002407 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002408 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002409}
2410
2411/*
2412 * Free all things that a function contains and free the function itself.
2413 * When "force" is TRUE we are exiting.
2414 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002415 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416func_clear_free(ufunc_T *fp, int force)
2417{
2418 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002419 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2420 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002421 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002422 else
2423 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424}
2425
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002426/*
2427 * Copy already defined function "lambda" to a new function with name "global".
2428 * This is for when a compiled function defines a global function.
2429 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002430 int
2431copy_func(char_u *lambda, char_u *global, ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002432{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002433 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002434 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002435
2436 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002437 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002438 semsg(_(e_lambda_function_not_found_str), lambda);
2439 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002440 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002441
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002442 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002443 if (fp != NULL)
2444 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002445 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002446 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002447 return FAIL;
2448 }
2449
2450 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
2451 if (fp == NULL)
2452 return FAIL;
2453
2454 fp->uf_varargs = ufunc->uf_varargs;
2455 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2456 fp->uf_def_status = ufunc->uf_def_status;
2457 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2458 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2459 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2460 == FAIL
2461 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2462 goto failed;
2463
2464 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
2465 : vim_strsave(ufunc->uf_name_exp);
2466 if (ufunc->uf_arg_types != NULL)
2467 {
2468 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2469 if (fp->uf_arg_types == NULL)
2470 goto failed;
2471 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2472 sizeof(type_T *) * fp->uf_args.ga_len);
2473 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002474 if (ufunc->uf_va_name != NULL)
2475 {
2476 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2477 if (fp->uf_va_name == NULL)
2478 goto failed;
2479 }
2480 fp->uf_ret_type = ufunc->uf_ret_type;
2481
2482 fp->uf_refcount = 1;
2483 STRCPY(fp->uf_name, global);
2484 hash_add(&func_hashtab, UF2HIKEY(fp));
2485
2486 // the referenced dfunc_T is now used one more time
2487 link_def_function(fp);
2488
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002489 // Create a partial to store the context of the function where it was
2490 // instantiated. Only needs to be done once. Do this on the original
2491 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002492 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2493 {
2494 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2495
2496 if (pt == NULL)
2497 goto failed;
2498 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002499 {
2500 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002501 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002502 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002503 ufunc->uf_partial = pt;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002504 --pt->pt_refcount; // not actually referenced here
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002505 }
2506
2507 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002508
2509failed:
2510 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002511 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002512}
2513
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002514static int funcdepth = 0;
2515
2516/*
2517 * Increment the function call depth count.
2518 * Return FAIL when going over 'maxfuncdepth'.
2519 * Otherwise return OK, must call funcdepth_decrement() later!
2520 */
2521 int
2522funcdepth_increment(void)
2523{
2524 if (funcdepth >= p_mfd)
2525 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002526 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002527 return FAIL;
2528 }
2529 ++funcdepth;
2530 return OK;
2531}
2532
2533 void
2534funcdepth_decrement(void)
2535{
2536 --funcdepth;
2537}
2538
2539/*
2540 * Get the current function call depth.
2541 */
2542 int
2543funcdepth_get(void)
2544{
2545 return funcdepth;
2546}
2547
2548/*
2549 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002550 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002551 * times as funcdepth_increment().
2552 */
2553 void
2554funcdepth_restore(int depth)
2555{
2556 funcdepth = depth;
2557}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002558
2559/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002560 * Call a user function.
2561 */
2562 static void
2563call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002564 ufunc_T *fp, // pointer to function
2565 int argcount, // nr of args
2566 typval_T *argvars, // arguments
2567 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002568 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002569 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002570{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002571 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002572 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002573 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002574 funccall_T *fc;
2575 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002576 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002577 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002578 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002579 int i;
2580 int ai;
2581 int islambda = FALSE;
2582 char_u numbuf[NUMBUFLEN];
2583 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002584 typval_T *tv_to_free[MAX_FUNC_ARGS];
2585 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002586#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002587 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002588#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01002589 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002590
Bram Moolenaarb2049902021-01-24 12:53:53 +01002591#ifdef FEAT_PROFILE
2592 CLEAR_FIELD(profile_info);
2593#endif
2594
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002595 // If depth of calling is getting too high, don't execute the function.
2596 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002597 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002598 rettv->v_type = VAR_NUMBER;
2599 rettv->vval.v_number = -1;
2600 return;
2601 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002602
Bram Moolenaare38eab22019-12-05 21:50:01 +01002603 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002604
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002605 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002606 if (fc == NULL)
2607 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002608 fc->caller = current_funccal;
2609 current_funccal = fc;
2610 fc->func = fp;
2611 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002612 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002613 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002614 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2615 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002616 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002617 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002618 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002619
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002620 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002622#ifdef FEAT_PROFILE
2623 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2624#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002625 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002626#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002627 if (do_profiling == PROF_YES)
2628 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002629#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002630 sticky_cmdmod_flags = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002631 call_def_function(fp, argcount, argvars, funcexe->fe_partial, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002632 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002633#ifdef FEAT_PROFILE
2634 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002635 || (caller != NULL && caller->uf_profiling)))
2636 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002637#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638 current_funccal = fc->caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639 free_funccal(fc);
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002640 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002641 return;
2642 }
2643
Bram Moolenaar38453522021-11-28 22:00:12 +00002644 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002645
2646 /*
2647 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
2648 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
2649 * each argument variable and saves a lot of time.
2650 */
2651 /*
2652 * Init l: variables.
2653 */
2654 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
2655 if (selfdict != NULL)
2656 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002657 // Set l:self to "selfdict". Use "name" to avoid a warning from
2658 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002659 v = &fc->fixvar[fixvar_idx++].var;
2660 name = v->di_key;
2661 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002662 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002663 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
2664 v->di_tv.v_type = VAR_DICT;
2665 v->di_tv.v_lock = 0;
2666 v->di_tv.vval.v_dict = selfdict;
2667 ++selfdict->dv_refcount;
2668 }
2669
2670 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002671 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002672 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002673 * Set a:000 to a list with room for the "..." arguments.
2674 */
2675 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002676 if ((fp->uf_flags & FC_NOARGS) == 0)
2677 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002678 (varnumber_T)(argcount >= fp->uf_args.ga_len
2679 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01002680 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002681 if ((fp->uf_flags & FC_NOARGS) == 0)
2682 {
2683 // Use "name" to avoid a warning from some compiler that checks the
2684 // destination size.
2685 v = &fc->fixvar[fixvar_idx++].var;
2686 name = v->di_key;
2687 STRCPY(name, "000");
2688 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2689 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
2690 v->di_tv.v_type = VAR_LIST;
2691 v->di_tv.v_lock = VAR_FIXED;
2692 v->di_tv.vval.v_list = &fc->l_varlist;
2693 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02002694 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002695 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2696 fc->l_varlist.lv_lock = VAR_FIXED;
2697
2698 /*
2699 * Set a:firstline to "firstline" and a:lastline to "lastline".
2700 * Set a:name to named arguments.
2701 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002702 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002703 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002704 if ((fp->uf_flags & FC_NOARGS) == 0)
2705 {
2706 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002707 (varnumber_T)funcexe->fe_firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002708 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002709 (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002710 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002711 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002712 {
2713 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002714 typval_T def_rettv;
2715 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002716
2717 ai = i - fp->uf_args.ga_len;
2718 if (ai < 0)
2719 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002720 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002721 name = FUNCARG(fp, i);
2722 if (islambda)
2723 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002724
2725 // evaluate named argument default expression
2726 isdefault = ai + fp->uf_def_args.ga_len >= 0
2727 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2728 && argvars[i].vval.v_number == VVAL_NONE));
2729 if (isdefault)
2730 {
2731 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002732
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002733 def_rettv.v_type = VAR_NUMBER;
2734 def_rettv.vval.v_number = -1;
2735
2736 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2737 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002738 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002739 {
2740 default_arg_err = 1;
2741 break;
2742 }
2743 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002744 }
2745 else
2746 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002747 if ((fp->uf_flags & FC_NOARGS) != 0)
2748 // Bail out if no a: arguments used (in lambda).
2749 break;
2750
Bram Moolenaare38eab22019-12-05 21:50:01 +01002751 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002752 sprintf((char *)numbuf, "%d", ai + 1);
2753 name = numbuf;
2754 }
2755 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2756 {
2757 v = &fc->fixvar[fixvar_idx++].var;
2758 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002759 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002760 }
2761 else
2762 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002763 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002764 if (v == NULL)
2765 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002766 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002767 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002768
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002769 // Note: the values are copied directly to avoid alloc/free.
2770 // "argvars" must have VAR_FIXED for v_lock.
2771 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002772 v->di_tv.v_lock = VAR_FIXED;
2773
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002774 if (isdefault)
2775 // Need to free this later, no matter where it's stored.
2776 tv_to_free[tv_to_free_len++] = &v->di_tv;
2777
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002778 if (addlocal)
2779 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002780 // Named arguments should be accessed without the "a:" prefix in
2781 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002782 copy_tv(&v->di_tv, &v->di_tv);
2783 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002784 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002785 else
2786 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002787
2788 if (ai >= 0 && ai < MAX_FUNC_ARGS)
2789 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002790 listitem_T *li = &fc->l_listitems[ai];
2791
2792 li->li_tv = argvars[i];
2793 li->li_tv.v_lock = VAR_FIXED;
2794 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002795 }
2796 }
2797
Bram Moolenaare38eab22019-12-05 21:50:01 +01002798 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002799 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02002800
2801 if (fp->uf_flags & FC_SANDBOX)
2802 {
2803 using_sandbox = TRUE;
2804 ++sandbox;
2805 }
2806
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002807 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002808 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002809 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002810 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002811 ++no_wait_return;
2812 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002813
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002814 smsg(_("calling %s"), SOURCING_NAME);
2815 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002816 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002817 char_u buf[MSG_BUF_LEN];
2818 char_u numbuf2[NUMBUFLEN];
2819 char_u *tofree;
2820 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002821
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002822 msg_puts("(");
2823 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002824 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002825 if (i > 0)
2826 msg_puts(", ");
2827 if (argvars[i].v_type == VAR_NUMBER)
2828 msg_outnum((long)argvars[i].vval.v_number);
2829 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002830 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002831 // Do not want errors such as E724 here.
2832 ++emsg_off;
2833 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
2834 --emsg_off;
2835 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002836 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002837 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002838 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002839 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2840 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002841 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002842 msg_puts((char *)s);
2843 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002844 }
2845 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002846 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002847 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002848 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002849 msg_puts("\n"); // don't overwrite this either
2850
2851 verbose_leave_scroll();
2852 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002853 }
2854#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002855 if (do_profiling == PROF_YES)
2856 profile_may_start_func(&profile_info, fp,
2857 fc->caller == NULL ? NULL : fc->caller->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002858#endif
2859
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002860 // "legacy" does not apply to commands in the function
2861 sticky_cmdmod_flags = 0;
2862
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002863 save_current_sctx = current_sctx;
2864 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002865 save_did_emsg = did_emsg;
2866 did_emsg = FALSE;
2867
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002868 if (default_arg_err && (fp->uf_flags & FC_ABORT))
2869 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002870 else if (islambda)
2871 {
2872 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
2873
2874 // A Lambda always has the command "return {expr}". It is much faster
2875 // to evaluate {expr} directly.
2876 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002877 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002878 --ex_nesting_level;
2879 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002880 else
2881 // call do_cmdline() to execute the lines
2882 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002883 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
2884
2885 --RedrawingDisabled;
2886
Bram Moolenaare38eab22019-12-05 21:50:01 +01002887 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002888 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
2889 {
2890 clear_tv(rettv);
2891 rettv->v_type = VAR_NUMBER;
2892 rettv->vval.v_number = -1;
2893 }
2894
2895#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002896 if (do_profiling == PROF_YES)
2897 {
2898 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2899
2900 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
2901 profile_may_end_func(&profile_info, fp, caller);
2902 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002903#endif
2904
Bram Moolenaare38eab22019-12-05 21:50:01 +01002905 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002906 if (p_verbose >= 12)
2907 {
2908 ++no_wait_return;
2909 verbose_enter_scroll();
2910
2911 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002912 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002913 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002914 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002915 (long)fc->rettv->vval.v_number);
2916 else
2917 {
2918 char_u buf[MSG_BUF_LEN];
2919 char_u numbuf2[NUMBUFLEN];
2920 char_u *tofree;
2921 char_u *s;
2922
Bram Moolenaare38eab22019-12-05 21:50:01 +01002923 // The value may be very long. Skip the middle part, so that we
2924 // have some idea how it starts and ends. smsg() would always
2925 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002926 ++emsg_off;
2927 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
2928 --emsg_off;
2929 if (s != NULL)
2930 {
2931 if (vim_strsize(s) > MSG_BUF_CLEN)
2932 {
2933 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2934 s = buf;
2935 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002936 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002937 vim_free(tofree);
2938 }
2939 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002940 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002941
2942 verbose_leave_scroll();
2943 --no_wait_return;
2944 }
2945
Bram Moolenaare31ee862020-01-07 20:59:34 +01002946 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002947 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002948 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002949#ifdef FEAT_PROFILE
2950 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002951 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002952#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02002953 if (using_sandbox)
2954 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002955 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002956
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002957 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002958 {
2959 ++no_wait_return;
2960 verbose_enter_scroll();
2961
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002962 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002963 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002964
2965 verbose_leave_scroll();
2966 --no_wait_return;
2967 }
2968
2969 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002970 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002971 for (i = 0; i < tv_to_free_len; ++i)
2972 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002973 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002974}
2975
2976/*
Bram Moolenaar50824712020-12-20 21:10:17 +01002977 * Check the argument count for user function "fp".
2978 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
2979 */
2980 int
2981check_user_func_argcount(ufunc_T *fp, int argcount)
2982{
2983 int regular_args = fp->uf_args.ga_len;
2984
2985 if (argcount < regular_args - fp->uf_def_args.ga_len)
2986 return FCERR_TOOFEW;
2987 else if (!has_varargs(fp) && argcount > regular_args)
2988 return FCERR_TOOMANY;
2989 return FCERR_UNKNOWN;
2990}
2991
2992/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002994 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 int
2996call_user_func_check(
2997 ufunc_T *fp,
2998 int argcount,
2999 typval_T *argvars,
3000 typval_T *rettv,
3001 funcexe_T *funcexe,
3002 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003003{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003005
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003006 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3007 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003008 error = check_user_func_argcount(fp, argcount);
3009 if (error != FCERR_UNKNOWN)
3010 return error;
3011 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 error = FCERR_DICT;
3013 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003014 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 int did_save_redo = FALSE;
3016 save_redo_T save_redo;
3017
3018 /*
3019 * Call the user function.
3020 * Save and restore search patterns, script variables and
3021 * redo buffer.
3022 */
3023 save_search_patterns();
3024 if (!ins_compl_active())
3025 {
3026 saveRedobuff(&save_redo);
3027 did_save_redo = TRUE;
3028 }
3029 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003030 call_user_func(fp, argcount, argvars, rettv, funcexe,
3031 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3033 // Function was unreferenced while being used, free it now.
3034 func_clear_free(fp, FALSE);
3035 if (did_save_redo)
3036 restoreRedobuff(&save_redo);
3037 restore_search_patterns();
3038 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003039 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003041}
3042
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003043static funccal_entry_T *funccal_stack = NULL;
3044
3045/*
3046 * Save the current function call pointer, and set it to NULL.
3047 * Used when executing autocommands and for ":source".
3048 */
3049 void
3050save_funccal(funccal_entry_T *entry)
3051{
3052 entry->top_funccal = current_funccal;
3053 entry->next = funccal_stack;
3054 funccal_stack = entry;
3055 current_funccal = NULL;
3056}
3057
3058 void
3059restore_funccal(void)
3060{
3061 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003062 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003063 else
3064 {
3065 current_funccal = funccal_stack->top_funccal;
3066 funccal_stack = funccal_stack->next;
3067 }
3068}
3069
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003070 funccall_T *
3071get_current_funccal(void)
3072{
3073 return current_funccal;
3074}
3075
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003076/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003077 * Return TRUE when currently at the script level:
3078 * - not in a function
3079 * - not executing an autocommand
3080 * Note that when an autocommand sources a script the result is FALSE;
3081 */
3082 int
3083at_script_level(void)
3084{
3085 return current_funccal == NULL && autocmd_match == NULL;
3086}
3087
3088/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003089 * Mark all functions of script "sid" as deleted.
3090 */
3091 void
3092delete_script_functions(int sid)
3093{
3094 hashitem_T *hi;
3095 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003096 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003097 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003098 size_t len;
3099
3100 buf[0] = K_SPECIAL;
3101 buf[1] = KS_EXTRA;
3102 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003103 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003104 len = STRLEN(buf);
3105
Bram Moolenaarce658352020-07-31 23:47:12 +02003106 while (todo > 0)
3107 {
3108 todo = func_hashtab.ht_used;
3109 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3110 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003111 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003112 fp = HI2UF(hi);
3113 if (STRNCMP(fp->uf_name, buf, len) == 0)
3114 {
3115 int changed = func_hashtab.ht_changed;
3116
3117 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003118
3119 if (fp->uf_calls > 0)
3120 {
3121 // Function is executing, don't free it but do remove
3122 // it from the hashtable.
3123 if (func_remove(fp))
3124 fp->uf_refcount--;
3125 }
3126 else
3127 {
3128 func_clear(fp, TRUE);
3129 // When clearing a function another function can be
3130 // cleared as a side effect. When that happens start
3131 // over.
3132 if (changed != func_hashtab.ht_changed)
3133 break;
3134 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003135 }
3136 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003137 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003138 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003139}
3140
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003141#if defined(EXITFREE) || defined(PROTO)
3142 void
3143free_all_functions(void)
3144{
3145 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003146 ufunc_T *fp;
3147 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003148 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003149 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003150
Bram Moolenaare38eab22019-12-05 21:50:01 +01003151 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003152 while (current_funccal != NULL)
3153 {
3154 clear_tv(current_funccal->rettv);
3155 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003156 if (current_funccal == NULL && funccal_stack != NULL)
3157 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003158 }
3159
Bram Moolenaare38eab22019-12-05 21:50:01 +01003160 // First clear what the functions contain. Since this may lower the
3161 // reference count of a function, it may also free a function and change
3162 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003163 while (todo > 0)
3164 {
3165 todo = func_hashtab.ht_used;
3166 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3167 if (!HASHITEM_EMPTY(hi))
3168 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 // clear the def function index now
3170 fp = HI2UF(hi);
3171 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003172 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173
Bram Moolenaare38eab22019-12-05 21:50:01 +01003174 // Only free functions that are not refcounted, those are
3175 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003176 if (func_name_refcount(fp->uf_name))
3177 ++skipped;
3178 else
3179 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003180 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003181 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003182 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003183 {
3184 skipped = 0;
3185 break;
3186 }
3187 }
3188 --todo;
3189 }
3190 }
3191
Bram Moolenaare38eab22019-12-05 21:50:01 +01003192 // Now actually free the functions. Need to start all over every time,
3193 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003194 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003195 while (func_hashtab.ht_used > skipped)
3196 {
3197 todo = func_hashtab.ht_used;
3198 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003199 if (!HASHITEM_EMPTY(hi))
3200 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003201 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003202 // Only free functions that are not refcounted, those are
3203 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003204 fp = HI2UF(hi);
3205 if (func_name_refcount(fp->uf_name))
3206 ++skipped;
3207 else
3208 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003209 if (func_free(fp, FALSE) == OK)
3210 {
3211 skipped = 0;
3212 break;
3213 }
3214 // did not actually free it
3215 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003216 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003217 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003218 }
3219 if (skipped == 0)
3220 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221
3222 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003223}
3224#endif
3225
3226/*
3227 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003228 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3229 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003230 * "len" is the length of "name", or -1 for NUL terminated.
3231 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003233builtin_function(char_u *name, int len)
3234{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003235 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003236
Bram Moolenaara26b9702020-04-18 19:53:28 +02003237 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003238 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003239 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3240 {
3241 if (name[i] == AUTOLOAD_CHAR)
3242 return FALSE;
3243 if (!eval_isnamec(name[i]))
3244 {
3245 // "name.something" is not a builtin function
3246 if (name[i] == '.')
3247 return FALSE;
3248 break;
3249 }
3250 }
3251 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003252}
3253
3254 int
3255func_call(
3256 char_u *name,
3257 typval_T *args,
3258 partial_T *partial,
3259 dict_T *selfdict,
3260 typval_T *rettv)
3261{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003262 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003263 listitem_T *item;
3264 typval_T argv[MAX_FUNC_ARGS + 1];
3265 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003266 int r = 0;
3267
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003268 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003269 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003270 {
3271 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3272 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003273 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003274 break;
3275 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003276 // Make a copy of each argument. This is needed to be able to set
3277 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003278 copy_tv(&item->li_tv, &argv[argc++]);
3279 }
3280
3281 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003282 {
3283 funcexe_T funcexe;
3284
Bram Moolenaara80faa82020-04-12 19:37:17 +02003285 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003286 funcexe.fe_firstline = curwin->w_cursor.lnum;
3287 funcexe.fe_lastline = curwin->w_cursor.lnum;
3288 funcexe.fe_evaluate = TRUE;
3289 funcexe.fe_partial = partial;
3290 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003291 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3292 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003293
Bram Moolenaare38eab22019-12-05 21:50:01 +01003294 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003295 while (argc > 0)
3296 clear_tv(&argv[--argc]);
3297
3298 return r;
3299}
3300
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003301static int callback_depth = 0;
3302
3303 int
3304get_callback_depth(void)
3305{
3306 return callback_depth;
3307}
3308
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003309/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003310 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003311 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003312 */
3313 int
3314call_callback(
3315 callback_T *callback,
3316 int len, // length of "name" or -1 to use strlen()
3317 typval_T *rettv, // return value goes here
3318 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003319 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003320 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003321{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003322 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003323 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003324
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003325 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3326 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02003327 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003328 funcexe.fe_evaluate = TRUE;
3329 funcexe.fe_partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003330 ++callback_depth;
3331 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3332 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003333
3334 // When a :def function was called that uses :try an error would be turned
3335 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003336 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003337 {
3338 need_rethrow = FALSE;
3339 handle_did_throw();
3340 }
3341
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003342 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003343}
3344
3345/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003346 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003347 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003348 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3349 */
3350 varnumber_T
3351call_callback_retnr(
3352 callback_T *callback,
3353 int argcount, // number of "argvars"
3354 typval_T *argvars) // vars for arguments, must have "argcount"
3355 // PLUS ONE elements!
3356{
3357 typval_T rettv;
3358 varnumber_T retval;
3359
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003360 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003361 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003362
3363 retval = tv_get_number_chk(&rettv, NULL);
3364 clear_tv(&rettv);
3365 return retval;
3366}
3367
3368/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003369 * Give an error message for the result of a function.
3370 * Nothing if "error" is FCERR_NONE.
3371 */
3372 void
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003373user_func_error(int error, char_u *name, funcexe_T *funcexe)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374{
3375 switch (error)
3376 {
3377 case FCERR_UNKNOWN:
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003378 if (funcexe->fe_found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003379 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003380 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003381 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 break;
3383 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003384 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 break;
3386 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003387 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 break;
3389 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003390 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 break;
3392 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003393 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 break;
3395 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003396 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 break;
3398 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003399 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3400 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 break;
3402 }
3403}
3404
3405/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003406 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003407 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003408 * Return FAIL when the function can't be called, OK otherwise.
3409 * Also returns OK when an error was encountered while executing the function.
3410 */
3411 int
3412call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003413 char_u *funcname, // name of the function
3414 int len, // length of "name" or -1 to use strlen()
3415 typval_T *rettv, // return value goes here
3416 int argcount_in, // number of "argvars"
3417 typval_T *argvars_in, // vars for arguments, must have "argcount"
3418 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003419 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003420{
3421 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01003422 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003423 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003424 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003425 char_u fname_buf[FLEN_FIXED + 1];
3426 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003427 char_u *fname = NULL;
3428 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003429 int argcount = argcount_in;
3430 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003431 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003432 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003433 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003434 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003435 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003436 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003437 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003438 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003439
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003440 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3441 // even when call_func() returns FAIL.
3442 rettv->v_type = VAR_UNKNOWN;
3443
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003444 if (partial != NULL)
3445 fp = partial->pt_func;
3446 if (fp == NULL)
3447 {
3448 // Make a copy of the name, if it comes from a funcref variable it
3449 // could be changed or deleted in the called function.
3450 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3451 if (name == NULL)
3452 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003453
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003454 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3455 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003456
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003457 if (funcexe->fe_doesrange != NULL)
3458 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003459
3460 if (partial != NULL)
3461 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003462 // When the function has a partial with a dict and there is a dict
3463 // argument, use the dict argument. That is backwards compatible.
3464 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003465 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003466 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003467 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003468 {
3469 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003470 {
3471 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3472 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003473 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003474 goto theend;
3475 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003476 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003477 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003478 for (i = 0; i < argcount_in; ++i)
3479 argv[i + argv_clear] = argvars_in[i];
3480 argvars = argv;
3481 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003482
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003483 if (funcexe->fe_check_type != NULL
3484 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003485 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003486 // Now funcexe->fe_check_type is missing the added arguments,
3487 // make a copy of the type with the correction.
3488 check_type = *funcexe->fe_check_type;
3489 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003490 check_type.tt_args = check_type_args;
3491 CLEAR_FIELD(check_type_args);
3492 for (i = 0; i < check_type.tt_argcount; ++i)
3493 check_type_args[i + partial->pt_argc] =
3494 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003495 check_type.tt_argcount += partial->pt_argc;
3496 check_type.tt_min_argcount += partial->pt_argc;
3497 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003498 }
3499 }
3500
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003501 if (error == FCERR_NONE && funcexe->fe_check_type != NULL
3502 && funcexe->fe_evaluate)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003503 {
3504 // Check that the argument types are OK for the types of the funcref.
Bram Moolenaarc84287d2022-01-16 18:06:21 +00003505 if (check_argument_types(funcexe->fe_check_type,
3506 argvars, argcount, funcexe->fe_basetv,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003507 (name != NULL) ? name : funcname) == FAIL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003508 error = FCERR_OTHER;
3509 }
3510
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003511 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003512 {
3513 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003514 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003515
Bram Moolenaar333894b2020-08-01 18:53:07 +02003516 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003517 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003518 {
3519 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003520 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003521 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003522
Bram Moolenaare38eab22019-12-05 21:50:01 +01003523 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003524 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003525 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003526
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003527 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003528 {
3529 /*
3530 * User defined function.
3531 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003532 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003533 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003534 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003535 if (fp != NULL && !is_global && in_vim9script()
3536 && func_requires_g_prefix(fp))
3537 // In Vim9 script g: is required to find a global
3538 // non-autoload function.
3539 fp = NULL;
3540 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003541
Bram Moolenaare38eab22019-12-05 21:50:01 +01003542 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003543 if (fp == NULL
3544 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003545 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003546 && !aborting())
3547 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003548 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003549 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003550 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003551 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003552 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3553 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003554 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003555 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003556 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003557 if (fp == NULL)
3558 {
3559 char_u *p = untrans_function_name(rfname);
3560
3561 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003562 // Don't do this if the name starts with "s:".
3563 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003564 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003565 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003566
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003567 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003568 error = FCERR_DELETED;
Bram Moolenaar801ab062020-06-25 19:27:56 +02003569#ifdef FEAT_LUA
3570 else if (fp != NULL && (fp->uf_flags & FC_CFUNC))
3571 {
3572 cfunc_T cb = fp->uf_cb;
3573
3574 error = (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3575 }
3576#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003577 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003578 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003579 if (funcexe->fe_argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003580 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003581 argcount = funcexe->fe_argv_func(argcount, argvars,
3582 argv_clear, fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003583
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003584 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003585 {
3586 // Method call: base->Method()
3587 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003588 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003589 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003590 argvars = argv;
3591 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003592 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003593
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 error = call_user_func_check(fp, argcount, argvars, rettv,
3595 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003596 }
3597 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003598 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003599 {
3600 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003601 * expr->method(): Find the method name in the table, call its
3602 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003603 */
3604 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003605 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003606 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003607 else
3608 {
3609 /*
3610 * Find the function name in the table, call its implementation.
3611 */
3612 error = call_internal_func(fname, argcount, argvars, rettv);
3613 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003614
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003615 /*
3616 * The function call (or "FuncUndefined" autocommand sequence) might
3617 * have been aborted by an error, an interrupt, or an explicitly thrown
3618 * exception that has not been caught so far. This situation can be
3619 * tested for by calling aborting(). For an error in an internal
3620 * function or for the "E132" error in call_user_func(), however, the
3621 * throw point at which the "force_abort" flag (temporarily reset by
3622 * emsg()) is normally updated has not been reached yet. We need to
3623 * update that flag first to make aborting() reliable.
3624 */
3625 update_force_abort();
3626 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003627 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003628 ret = OK;
3629
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003630theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003631 /*
3632 * Report an error unless the argument evaluation or function call has been
3633 * cancelled due to an aborting error, an interrupt, or an exception.
3634 */
3635 if (!aborting())
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003636 user_func_error(error, (name != NULL) ? name : funcname, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003637
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003638 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003639 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003640 clear_tv(&argv[--argv_clear + argv_base]);
3641
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003642 vim_free(tofree);
3643 vim_free(name);
3644
3645 return ret;
3646}
3647
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003648 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649printable_func_name(ufunc_T *fp)
3650{
3651 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
3652}
3653
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003654/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003655 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003656 */
3657 static void
3658list_func_head(ufunc_T *fp, int indent)
3659{
3660 int j;
3661
3662 msg_start();
3663 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003664 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003665 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003666 msg_puts("def ");
3667 else
3668 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003670 msg_putchar('(');
3671 for (j = 0; j < fp->uf_args.ga_len; ++j)
3672 {
3673 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003674 msg_puts(", ");
3675 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 if (fp->uf_arg_types != NULL)
3677 {
3678 char *tofree;
3679
3680 msg_puts(": ");
3681 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
3682 vim_free(tofree);
3683 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003684 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
3685 {
3686 msg_puts(" = ");
3687 msg_puts(((char **)(fp->uf_def_args.ga_data))
3688 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
3689 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003690 }
3691 if (fp->uf_varargs)
3692 {
3693 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003694 msg_puts(", ");
3695 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003696 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 if (fp->uf_va_name != NULL)
3698 {
3699 if (j)
3700 msg_puts(", ");
3701 msg_puts("...");
3702 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02003703 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 {
3705 char *tofree;
3706
3707 msg_puts(": ");
3708 msg_puts(type_name(fp->uf_va_type, &tofree));
3709 vim_free(tofree);
3710 }
3711 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003712 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003713
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003714 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003715 {
3716 if (fp->uf_ret_type != &t_void)
3717 {
3718 char *tofree;
3719
3720 msg_puts(": ");
3721 msg_puts(type_name(fp->uf_ret_type, &tofree));
3722 vim_free(tofree);
3723 }
3724 }
3725 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003726 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003727 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003728 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003729 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003730 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003731 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003732 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003733 msg_clr_eos();
3734 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003735 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003736}
3737
3738/*
3739 * Get a function name, translating "<SID>" and "<SNR>".
3740 * Also handles a Funcref in a List or Dictionary.
3741 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003742 * Set "*is_global" to TRUE when the function must be global, unless
3743 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003744 * flags:
3745 * TFN_INT: internal function name OK
3746 * TFN_QUIET: be quiet
3747 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003748 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003749 * Advances "pp" to just after the function name (if no error).
3750 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003751 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003752trans_function_name(
3753 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003754 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003755 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003756 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003757 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003758 partial_T **partial, // return: partial of a FuncRef
3759 type_T **type) // return: type of funcref if not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003760{
3761 char_u *name = NULL;
3762 char_u *start;
3763 char_u *end;
3764 int lead;
3765 char_u sid_buf[20];
3766 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003768 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003769 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003770 int vim9script = in_vim9script();
3771 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003772
3773 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003774 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003775 start = *pp;
3776
Bram Moolenaare38eab22019-12-05 21:50:01 +01003777 // Check for hard coded <SNR>: already translated function ID (from a user
3778 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003779 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
3780 && (*pp)[2] == (int)KE_SNR)
3781 {
3782 *pp += 3;
3783 len = get_id_len(pp) + 3;
3784 return vim_strnsave(start, len);
3785 }
3786
Bram Moolenaare38eab22019-12-05 21:50:01 +01003787 // A name starting with "<SID>" or "<SNR>" is local to a script. But
3788 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003789 lead = eval_fname_script(start);
3790 if (lead > 2)
3791 start += lead;
3792
Bram Moolenaare38eab22019-12-05 21:50:01 +01003793 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01003794 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003795 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003796 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00003797 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003798 {
3799 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00003800 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003801 goto theend;
3802 }
3803 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
3804 {
3805 /*
3806 * Report an invalid expression in braces, unless the expression
3807 * evaluation has been cancelled due to an aborting error, an
3808 * interrupt, or an exception.
3809 */
3810 if (!aborting())
3811 {
3812 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003813 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003814 }
3815 else
3816 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
3817 goto theend;
3818 }
3819
3820 if (lv.ll_tv != NULL)
3821 {
3822 if (fdp != NULL)
3823 {
3824 fdp->fd_dict = lv.ll_dict;
3825 fdp->fd_newkey = lv.ll_newkey;
3826 lv.ll_newkey = NULL;
3827 fdp->fd_di = lv.ll_di;
3828 }
3829 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
3830 {
3831 name = vim_strsave(lv.ll_tv->vval.v_string);
3832 *pp = end;
3833 }
3834 else if (lv.ll_tv->v_type == VAR_PARTIAL
3835 && lv.ll_tv->vval.v_partial != NULL)
3836 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003837 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003838 *pp = end;
3839 if (partial != NULL)
3840 *partial = lv.ll_tv->vval.v_partial;
3841 }
3842 else
3843 {
3844 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
3845 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00003846 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003847 else
3848 *pp = end;
3849 name = NULL;
3850 }
3851 goto theend;
3852 }
3853
3854 if (lv.ll_name == NULL)
3855 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003856 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003857 *pp = end;
3858 goto theend;
3859 }
3860
Bram Moolenaare38eab22019-12-05 21:50:01 +01003861 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003862 if (lv.ll_exp_name != NULL)
3863 {
3864 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003865 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00003866 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003867 if (name == lv.ll_exp_name)
3868 name = NULL;
3869 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00003870 else if (lv.ll_sid > 0)
3871 {
3872 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
3873 int cc = *lv.ll_name_end;
3874
3875 // function in another script. Prefix <SNR>99_ or the autoload prefix.
3876 *lv.ll_name_end = NUL;
3877 if (si->sn_autoload_prefix != NULL)
3878 {
3879 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
3880 }
3881 else
3882 {
3883 sid_buf[0] = K_SPECIAL;
3884 sid_buf[1] = KS_EXTRA;
3885 sid_buf[2] = (int)KE_SNR;
3886 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00003887 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00003888 name = concat_str(sid_buf, lv.ll_name);
3889 }
3890 *lv.ll_name_end = cc;
3891 *pp = end;
3892 goto theend;
3893 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003894 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003895 {
3896 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003897 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00003898 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003899 if (name == *pp)
3900 name = NULL;
3901 }
3902 if (name != NULL)
3903 {
3904 name = vim_strsave(name);
3905 *pp = end;
3906 if (STRNCMP(name, "<SNR>", 5) == 0)
3907 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003908 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003909 name[0] = K_SPECIAL;
3910 name[1] = KS_EXTRA;
3911 name[2] = (int)KE_SNR;
3912 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
3913 }
3914 goto theend;
3915 }
3916
3917 if (lv.ll_exp_name != NULL)
3918 {
3919 len = (int)STRLEN(lv.ll_exp_name);
3920 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
3921 && STRNCMP(lv.ll_name, "s:", 2) == 0)
3922 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003923 // When there was "s:" already or the name expanded to get a
3924 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003925 lv.ll_name += 2;
3926 len -= 2;
3927 lead = 2;
3928 }
3929 }
3930 else
3931 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003932 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003933 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003934 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003935 if (lv.ll_name[0] == 'g')
3936 {
3937 if (is_global != NULL)
3938 {
3939 *is_global = TRUE;
3940 }
3941 else
3942 {
3943 // dropping "g:" without setting "is_global" won't work in
3944 // Vim9script, put it back later
3945 prefix_g = TRUE;
3946 extra = 2;
3947 }
3948 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003949 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003950 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003951 len = (int)(end - lv.ll_name);
3952 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003953 if (len <= 0)
3954 {
3955 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00003956 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003957 goto theend;
3958 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003959
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003960 // In Vim9 script a user function is script-local by default, unless it
3961 // starts with a lower case character: dict.func().
Bram Moolenaar4525a572022-02-13 11:57:33 +00003962 vim9_local = ASCII_ISUPPER(*start) && vim9script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003964 /*
3965 * Copy the function name to allocated memory.
3966 * Accept <SID>name() inside a script, translate into <SNR>123_name().
3967 * Accept <SNR>123_name() outside a script.
3968 */
3969 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003970 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00003971 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003972 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003973 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00003974 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003975 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name))
Bram Moolenaar3787f262022-02-07 21:54:01 +00003976 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003977 semsg(_(vim9script
Bram Moolenaar3787f262022-02-07 21:54:01 +00003978 ? e_function_name_must_start_with_capital_str
3979 : e_function_name_must_start_with_capital_or_s_str),
3980 start);
3981 goto theend;
3982 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003983 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00003984 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00003985 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003987 || eval_fname_sid(*pp))
3988 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003990 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003991 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00003992 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003993 goto theend;
3994 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003995 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003996 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003997 extra = 3 + (int)STRLEN(sid_buf);
3998 else
3999 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004000 }
4001 }
Bram Moolenaar22f17a22021-06-21 20:48:58 +02004002 else if (!(flags & TFN_INT) && (builtin_function(lv.ll_name, len)
Bram Moolenaar4525a572022-02-13 11:57:33 +00004003 || (vim9script && *lv.ll_name == '_')))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004004 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004005 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004006 : e_function_name_must_start_with_capital_or_s_str),
4007 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004008 goto theend;
4009 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004010 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004011 {
4012 char_u *cp = vim_strchr(lv.ll_name, ':');
4013
4014 if (cp != NULL && cp < end)
4015 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004016 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004017 goto theend;
4018 }
4019 }
4020
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004022 if (name != NULL)
4023 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004024 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004025 {
4026 name[0] = K_SPECIAL;
4027 name[1] = KS_EXTRA;
4028 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004029 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004030 STRCPY(name + 3, sid_buf);
4031 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004032 else if (prefix_g)
4033 {
4034 name[0] = 'g';
4035 name[1] = ':';
4036 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4038 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004039 }
4040 *pp = end;
4041
4042theend:
4043 clear_lval(&lv);
4044 return name;
4045}
4046
4047/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004048 * Assuming "name" is the result of trans_function_name() and it was prefixed
4049 * to use the script-local name, return the unmodified name (points into
4050 * "name"). Otherwise return NULL.
4051 * This can be used to first search for a script-local function and fall back
4052 * to the global function if not found.
4053 */
4054 char_u *
4055untrans_function_name(char_u *name)
4056{
4057 char_u *p;
4058
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004059 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004060 {
4061 p = vim_strchr(name, '_');
4062 if (p != NULL)
4063 return p + 1;
4064 }
4065 return NULL;
4066}
4067
4068/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004069 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4070 * current script ID and returns the expanded function name. The caller should
4071 * free the returned name. If not called from a script context or the function
4072 * name doesn't start with these prefixes, then returns NULL.
4073 * This doesn't check whether the script-local function exists or not.
4074 */
4075 char_u *
4076get_scriptlocal_funcname(char_u *funcname)
4077{
4078 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004079 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004080 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004081 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004082
4083 if (funcname == NULL)
4084 return NULL;
4085
4086 if (STRNCMP(funcname, "s:", 2) != 0
4087 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004088 {
4089 ufunc_T *ufunc;
4090
4091 // The function name does not have a script-local prefix. Try finding
4092 // it when in a Vim9 script and there is no "g:" prefix.
4093 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4094 return NULL;
4095 ufunc = find_func(funcname, FALSE);
4096 if (ufunc == NULL || func_is_global(ufunc)
4097 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4098 return NULL;
4099 ++p;
4100 off = 0;
4101 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004102 else
4103 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004104
4105 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4106 {
4107 emsg(_(e_using_sid_not_in_script_context));
4108 return NULL;
4109 }
4110 // Expand s: prefix into <SNR>nr_<name>
4111 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4112 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004113 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004114 if (newname == NULL)
4115 return NULL;
4116 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004117 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004118
4119 return newname;
4120}
4121
4122/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004123 * Return script-local "fname" with the 3-byte sequence replaced by
4124 * printable <SNR> in allocated memory.
4125 */
4126 char_u *
4127alloc_printable_func_name(char_u *fname)
4128{
4129 char_u *n = alloc(STRLEN(fname + 3) + 6);
4130
4131 if (n != NULL)
4132 {
4133 STRCPY(n, "<SNR>");
4134 STRCPY(n + 5, fname + 3);
4135 }
4136 return n;
4137}
4138
4139/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004140 * Call trans_function_name(), except that a lambda is returned as-is.
4141 * Returns the name in allocated memory.
4142 */
4143 char_u *
4144save_function_name(
4145 char_u **name,
4146 int *is_global,
4147 int skip,
4148 int flags,
4149 funcdict_T *fudi)
4150{
4151 char_u *p = *name;
4152 char_u *saved;
4153
4154 if (STRNCMP(p, "<lambda>", 8) == 0)
4155 {
4156 p += 8;
4157 (void)getdigits(&p);
4158 saved = vim_strnsave(*name, p - *name);
4159 if (fudi != NULL)
4160 CLEAR_POINTER(fudi);
4161 }
4162 else
4163 saved = trans_function_name(&p, is_global, skip,
4164 flags, fudi, NULL, NULL);
4165 *name = p;
4166 return saved;
4167}
4168
4169/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004170 * List functions. When "regmatch" is NULL all of then.
4171 * Otherwise functions matching "regmatch".
4172 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004173 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004174list_functions(regmatch_T *regmatch)
4175{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004176 int changed = func_hashtab.ht_changed;
4177 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004178 hashitem_T *hi;
4179
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004180 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004181 {
4182 if (!HASHITEM_EMPTY(hi))
4183 {
4184 ufunc_T *fp = HI2UF(hi);
4185
4186 --todo;
4187 if ((fp->uf_flags & FC_DEAD) == 0
4188 && (regmatch == NULL
4189 ? !message_filtered(fp->uf_name)
4190 && !func_name_refcount(fp->uf_name)
4191 : !isdigit(*fp->uf_name)
4192 && vim_regexec(regmatch, fp->uf_name, 0)))
4193 {
4194 list_func_head(fp, FALSE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004195 if (changed != func_hashtab.ht_changed)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004196 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00004197 emsg(_(e_function_list_was_modified));
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004198 return;
4199 }
4200 }
4201 }
4202 }
4203}
4204
4205/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004206 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004207 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4208 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004209 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004210 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004211 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004212 ufunc_T *
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004213define_function(exarg_T *eap, char_u *name_arg, garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004214{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004215 int j;
4216 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004217 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004218 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004219 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004220 char_u *p;
4221 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004222 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004223 char_u *line_arg = NULL;
4224 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004226 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004227 garray_T newlines;
4228 int varargs = FALSE;
4229 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004230 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004231 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004232 int fp_allocated = FALSE;
4233 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004234 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004235 dictitem_T *v;
4236 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004237 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004238 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004239 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004240 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004241 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004242 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004243
4244 /*
4245 * ":function" without argument: list functions.
4246 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004247 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004248 {
4249 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004250 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004251 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004252 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004253 }
4254
4255 /*
4256 * ":function /pat": list functions matching pattern.
4257 */
4258 if (*eap->arg == '/')
4259 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004260 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004261 if (!eap->skip)
4262 {
4263 regmatch_T regmatch;
4264
4265 c = *p;
4266 *p = NUL;
4267 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4268 *p = c;
4269 if (regmatch.regprog != NULL)
4270 {
4271 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004272 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004273 vim_regfree(regmatch.regprog);
4274 }
4275 }
4276 if (*p == '/')
4277 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004278 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004279 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004280 }
4281
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 ga_init(&newargs);
4283 ga_init(&argtypes);
4284 ga_init(&default_args);
4285
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004286 /*
4287 * Get the function name. There are these situations:
4288 * func normal function name
4289 * "name" == func, "fudi.fd_dict" == NULL
4290 * dict.func new dictionary entry
4291 * "name" == NULL, "fudi.fd_dict" set,
4292 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4293 * dict.func existing dict entry with a Funcref
4294 * "name" == func, "fudi.fd_dict" set,
4295 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4296 * dict.func existing dict entry that's not a Funcref
4297 * "name" == NULL, "fudi.fd_dict" set,
4298 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4299 * s:func script-local function name
4300 * g:func global function name, same as "func"
4301 */
4302 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004303 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004304 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004305 // nested function, argument is (args).
4306 paren = TRUE;
4307 CLEAR_FIELD(fudi);
4308 }
4309 else
4310 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004311 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004312 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004313 if (p[0] == 's' && p[1] == ':')
4314 {
4315 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4316 return NULL;
4317 }
4318 p = to_name_end(p, TRUE);
4319 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4320 {
4321 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4322 eap->arg);
4323 return NULL;
4324 }
4325 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004326 }
4327
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004328 name = save_function_name(&p, &is_global, eap->skip,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004329 TFN_NO_AUTOLOAD | TFN_NEW_FUNC, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004330 paren = (vim_strchr(p, '(') != NULL);
4331 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004332 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004333 /*
4334 * Return on an invalid expression in braces, unless the expression
4335 * evaluation has been cancelled due to an aborting error, an
4336 * interrupt, or an exception.
4337 */
4338 if (!aborting())
4339 {
4340 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004341 semsg(_(e_key_not_present_in_dictionary), fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004342 vim_free(fudi.fd_newkey);
4343 return NULL;
4344 }
4345 else
4346 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004347 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004348
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004349 // For "export def FuncName()" in an autoload script the function name
4350 // is stored with the legacy autoload name "dir#script#FuncName" so
4351 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004352 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004353 {
4354 char_u *prefixed = may_prefix_autoload(name);
4355
4356 if (prefixed != NULL && prefixed != name)
4357 {
4358 vim_free(name);
4359 name = prefixed;
4360 }
4361 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004362 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004363 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004364 {
4365 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4366 goto ret_free;
4367 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004368 }
4369
Bram Moolenaare38eab22019-12-05 21:50:01 +01004370 // An error in a function call during evaluation of an expression in magic
4371 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004372 saved_did_emsg = did_emsg;
4373 did_emsg = FALSE;
4374
4375 /*
4376 * ":function func" with only function name: list function.
4377 */
4378 if (!paren)
4379 {
4380 if (!ends_excmd(*skipwhite(p)))
4381 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004382 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004383 goto ret_free;
4384 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004385 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004386 if (eap->nextcmd != NULL)
4387 *p = NUL;
4388 if (!eap->skip && !got_int)
4389 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004390 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004391 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4392 {
4393 char_u *up = untrans_function_name(name);
4394
4395 // With Vim9 script the name was made script-local, if not
4396 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004397 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004398 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004399 }
4400
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004401 if (fp != NULL)
4402 {
4403 list_func_head(fp, TRUE);
4404 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4405 {
4406 if (FUNCLINE(fp, j) == NULL)
4407 continue;
4408 msg_putchar('\n');
4409 msg_outnum((long)(j + 1));
4410 if (j < 9)
4411 msg_putchar(' ');
4412 if (j < 99)
4413 msg_putchar(' ');
4414 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004415 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004416 ui_breakcheck();
4417 }
4418 if (!got_int)
4419 {
4420 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004421 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 msg_puts(" enddef");
4423 else
4424 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004425 }
4426 }
4427 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004428 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004429 }
4430 goto ret_free;
4431 }
4432
4433 /*
4434 * ":function name(arg1, arg2)" Define function.
4435 */
4436 p = skipwhite(p);
4437 if (*p != '(')
4438 {
4439 if (!eap->skip)
4440 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004441 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004442 goto ret_free;
4443 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004444 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004445 if (vim_strchr(p, '(') != NULL)
4446 p = vim_strchr(p, '(');
4447 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004448
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004449 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4450 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004451 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004452 goto ret_free;
4453 }
4454
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004455 // In Vim9 script only global functions can be redefined.
4456 if (vim9script && eap->forceit && !is_global)
4457 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004458 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004459 goto ret_free;
4460 }
4461
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004462 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004463
Bram Moolenaar04b12692020-05-04 23:24:44 +02004464 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004465 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004466 // Check the name of the function. Unless it's a dictionary function
4467 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004468 if (name != NULL)
4469 arg = name;
4470 else
4471 arg = fudi.fd_newkey;
4472 if (arg != NULL && (fudi.fd_di == NULL
4473 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4474 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4475 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004476 char_u *name_base = arg;
4477 int i;
4478
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004479 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004480 {
4481 name_base = vim_strchr(arg, '_');
4482 if (name_base == NULL)
4483 name_base = arg + 3;
4484 else
4485 ++name_base;
4486 }
4487 for (i = 0; name_base[i] != NUL && (i == 0
4488 ? eval_isnamec1(name_base[i])
4489 : eval_isnamec(name_base[i])); ++i)
4490 ;
4491 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004492 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004493
4494 // In Vim9 script a function cannot have the same name as a
4495 // variable.
4496 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004497 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4498 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004499 + EVAL_VAR_NO_FUNC) == OK)
4500 {
4501 semsg(_(e_redefining_script_item_str), name_base);
4502 goto ret_free;
4503 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004504 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004505 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004506 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004507 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004508 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004509 goto ret_free;
4510 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004511 }
4512
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004513 // This may get more lines and make the pointers into the first line
4514 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004515 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004517 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004518 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004519 eap, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004520 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004521 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004522
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004524 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004526 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004527 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004528 if (*p != ':')
4529 {
4530 semsg(_(e_no_white_space_allowed_before_colon_str), p);
4531 p = skipwhite(p);
4532 }
4533 else if (!IS_WHITE_OR_NUL(p[1]))
4534 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02004536 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004538 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004539 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01004540 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004541 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004542 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004544 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004545 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02004546 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004547 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004548 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02004549 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004550 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004551 else
4552 // find extra arguments "range", "dict", "abort" and "closure"
4553 for (;;)
4554 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01004555 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004556 p = skipwhite(p);
4557 if (STRNCMP(p, "range", 5) == 0)
4558 {
4559 flags |= FC_RANGE;
4560 p += 5;
4561 }
4562 else if (STRNCMP(p, "dict", 4) == 0)
4563 {
4564 flags |= FC_DICT;
4565 p += 4;
4566 }
4567 else if (STRNCMP(p, "abort", 5) == 0)
4568 {
4569 flags |= FC_ABORT;
4570 p += 5;
4571 }
4572 else if (STRNCMP(p, "closure", 7) == 0)
4573 {
4574 flags |= FC_CLOSURE;
4575 p += 7;
4576 if (current_funccal == NULL)
4577 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004578 emsg_funcname(e_closure_function_should_not_be_at_top_level,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004579 name == NULL ? (char_u *)"" : name);
4580 goto erret;
4581 }
4582 }
4583 else
4584 break;
4585 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004586
Bram Moolenaare38eab22019-12-05 21:50:01 +01004587 // When there is a line break use what follows for the function body.
4588 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004589 if (*p == '\n')
4590 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004591 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02004592 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
4593 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01004594 && !(VIM_ISWHITE(*whitep) && *p == '#'
4595 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02004596 && !eap->skip
4597 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004598 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004599
4600 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
4602 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004603 */
4604 if (KeyTyped)
4605 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004606 // Check if the function already exists, don't let the user type the
4607 // whole function before telling him it doesn't work! For a script we
4608 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004609 if (!eap->skip && !eap->forceit)
4610 {
4611 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004612 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004613 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00004614 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004615 }
4616
4617 if (!eap->skip && did_emsg)
4618 goto erret;
4619
Bram Moolenaare38eab22019-12-05 21:50:01 +01004620 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004621 cmdline_row = msg_row;
4622 }
4623
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004624 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004625 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004626
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004627 // Do not define the function when getting the body fails and when
4628 // skipping.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004629 if (get_function_body(eap, &newlines, line_arg, lines_to_free) == FAIL
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004630 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004631 goto erret;
4632
4633 /*
4634 * If there are no errors, add the function
4635 */
4636 if (fudi.fd_dict == NULL)
4637 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004639 char_u *find_name = name;
4640 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004641 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004643 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004644 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004645 var_conflict = TRUE;
4646
4647 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
4648 {
4649 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
4650
4651 if (si->sn_autoload_prefix != NULL)
4652 {
4653 if (is_export)
4654 {
4655 find_name = name + STRLEN(si->sn_autoload_prefix);
4656 v = find_var(find_name, &ht, TRUE);
4657 if (v != NULL)
4658 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004659 // Only check if the function already exists in the script,
4660 // global functions can be shadowed.
4661 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004662 }
4663 else
4664 {
4665 char_u *prefixed = may_prefix_autoload(name);
4666
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00004667 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004668 {
4669 v = find_var(prefixed, &ht, TRUE);
4670 if (v != NULL)
4671 var_conflict = TRUE;
4672 vim_free(prefixed);
4673 }
4674 }
4675 }
4676 }
4677 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004678 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004679 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004680 goto erret;
4681 }
4682
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004683 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02004684 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004685 {
Bram Moolenaareef21022020-08-01 22:16:43 +02004686 char_u *uname = untrans_function_name(name);
4687
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004688 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004689 }
4690
4691 if (fp != NULL || import != NULL)
4692 {
4693 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004695 // Function can be replaced with "function!" and when sourcing the
4696 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02004697 // A name that is used by an import can not be overruled.
4698 if (import != NULL
4699 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004700 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02004701 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004702 {
Bram Moolenaard604d782021-11-20 21:46:20 +00004703 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02004704 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004705 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02004706 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00004707 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004708 goto erret;
4709 }
4710 if (fp->uf_calls > 0)
4711 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004712 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00004713 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004714 goto erret;
4715 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004716 if (fp->uf_refcount > 1)
4717 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004718 // This function is referenced somewhere, don't redefine it but
4719 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004720 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004721 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004722 fp = NULL;
4723 overwrite = TRUE;
4724 }
4725 else
4726 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004727 char_u *exp_name = fp->uf_name_exp;
4728
4729 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01004730 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004731 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004732 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004733 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004735#ifdef FEAT_PROFILE
4736 fp->uf_profiling = FALSE;
4737 fp->uf_prof_initialized = FALSE;
4738#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01004739 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004740 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004741 }
4742 }
4743 else
4744 {
4745 char numbuf[20];
4746
4747 fp = NULL;
4748 if (fudi.fd_newkey == NULL && !eap->forceit)
4749 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004750 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004751 goto erret;
4752 }
4753 if (fudi.fd_di == NULL)
4754 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004755 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02004756 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004757 goto erret;
4758 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004759 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02004760 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004761 goto erret;
4762
Bram Moolenaare38eab22019-12-05 21:50:01 +01004763 // Give the function a sequential number. Can only be used with a
4764 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004765 vim_free(name);
4766 sprintf(numbuf, "%d", ++func_nr);
4767 name = vim_strsave((char_u *)numbuf);
4768 if (name == NULL)
4769 goto erret;
4770 }
4771
4772 if (fp == NULL)
4773 {
4774 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
4775 {
4776 int slen, plen;
4777 char_u *scriptname;
4778
Bram Moolenaare38eab22019-12-05 21:50:01 +01004779 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004780 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004781 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004782 {
4783 scriptname = autoload_name(name);
4784 if (scriptname != NULL)
4785 {
4786 p = vim_strchr(scriptname, '/');
4787 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004788 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004789 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004790 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004791 j = OK;
4792 vim_free(scriptname);
4793 }
4794 }
4795 if (j == FAIL)
4796 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004797 linenr_T save_lnum = SOURCING_LNUM;
4798
4799 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00004800 semsg(_(e_function_name_does_not_match_script_file_name_str),
4801 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004802 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004803 goto erret;
4804 }
4805 }
4806
Bram Moolenaar47ed5532019-08-08 20:49:14 +02004807 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004808 if (fp == NULL)
4809 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004810 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004811
4812 if (fudi.fd_dict != NULL)
4813 {
4814 if (fudi.fd_di == NULL)
4815 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004816 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004817 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
4818 if (fudi.fd_di == NULL)
4819 {
4820 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004821 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004822 goto erret;
4823 }
4824 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
4825 {
4826 vim_free(fudi.fd_di);
4827 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004828 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004829 goto erret;
4830 }
4831 }
4832 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004833 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004834 clear_tv(&fudi.fd_di->di_tv);
4835 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004836 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004837
Bram Moolenaare38eab22019-12-05 21:50:01 +01004838 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004839 flags |= FC_DICT;
4840 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004841 }
4842 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004843 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004844 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004845 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846
4847 if (eap->cmdidx == CMD_def)
4848 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02004849 int lnum_save = SOURCING_LNUM;
4850 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004851
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004852 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004853
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004854 // error messages are for the first function line
4855 SOURCING_LNUM = sourcing_lnum_top;
4856
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02004857 // The function may use script variables from the context.
4858 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004859
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004860 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004862 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004863 free_fp = fp_allocated;
4864 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004865 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004866 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004867
4868 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004869 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004871 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004872 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004873 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02004875 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004876 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02004877 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004878 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004879
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004880 if (fp_allocated)
4881 {
4882 // insert the new function in the function list
4883 set_ufunc_name(fp, name);
4884 if (overwrite)
4885 {
4886 hi = hash_find(&func_hashtab, name);
4887 hi->hi_key = UF2HIKEY(fp);
4888 }
4889 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
4890 {
4891 free_fp = TRUE;
4892 goto erret;
4893 }
4894 fp->uf_refcount = 1;
4895 }
4896
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004897 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004898 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004899 if ((flags & FC_CLOSURE) != 0)
4900 {
Bram Moolenaar58016442016-07-31 18:30:22 +02004901 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004902 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004903 }
4904 else
4905 fp->uf_scoped = NULL;
4906
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004907#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004908 if (prof_def_func())
4909 func_do_profile(fp);
4910#endif
4911 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02004912 if (sandbox)
4913 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004914 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004915 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004916 fp->uf_flags = flags;
4917 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01004918 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004919 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004920 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004921 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004922 if (is_export)
4923 {
4924 fp->uf_flags |= FC_EXPORT;
4925 // let ex_export() know the export worked.
4926 is_export = FALSE;
4927 }
4928
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004929 if (eap->cmdidx == CMD_def)
4930 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02004931 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
4932 // :func does not use Vim9 script syntax, even in a Vim9 script file
4933 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004934
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004935 goto ret_free;
4936
4937erret:
4938 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004939 ga_clear_strings(&default_args);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004940 if (fp != NULL)
4941 {
4942 ga_init(&fp->uf_args);
4943 ga_init(&fp->uf_def_args);
4944 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004945errret_2:
4946 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004947 if (fp != NULL)
4948 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004949 if (free_fp)
4950 {
4951 vim_free(fp);
4952 fp = NULL;
4953 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004954ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01004955 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004956 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004957 if (name != name_arg)
4958 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004959 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004960 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004961
4962 return fp;
4963}
4964
4965/*
4966 * ":function"
4967 */
4968 void
4969ex_function(exarg_T *eap)
4970{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004971 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00004972
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004973 ga_init2(&lines_to_free, sizeof(char_u *), 50);
4974 (void)define_function(eap, NULL, &lines_to_free);
4975 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02004976}
4977
4978/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02004979 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarb2049902021-01-24 12:53:53 +01004980 * be compiled. Except dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02004981 */
4982 void
4983ex_defcompile(exarg_T *eap UNUSED)
4984{
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004985 long todo = (long)func_hashtab.ht_used;
4986 int changed = func_hashtab.ht_changed;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004987 hashitem_T *hi;
4988 ufunc_T *ufunc;
4989
4990 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
4991 {
4992 if (!HASHITEM_EMPTY(hi))
4993 {
4994 --todo;
4995 ufunc = HI2UF(hi);
4996 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
Bram Moolenaar96f8f492020-09-09 17:08:51 +02004997 && ufunc->uf_def_status == UF_TO_BE_COMPILED
4998 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004999 {
Bram Moolenaare99d4222021-06-13 14:01:26 +02005000 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005001
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02005002 if (func_hashtab.ht_changed != changed)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005003 {
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02005004 // a function has been added or removed, need to start over
5005 todo = (long)func_hashtab.ht_used;
5006 changed = func_hashtab.ht_changed;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005007 hi = func_hashtab.ht_array;
Bram Moolenaar285b1892020-05-26 11:37:26 +02005008 --hi;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005009 }
5010 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005011 }
5012 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005013}
5014
5015/*
5016 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5017 * Return 2 if "p" starts with "s:".
5018 * Return 0 otherwise.
5019 */
5020 int
5021eval_fname_script(char_u *p)
5022{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005023 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5024 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005025 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5026 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5027 return 5;
5028 if (p[0] == 's' && p[1] == ':')
5029 return 2;
5030 return 0;
5031}
5032
5033 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005034translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005035{
5036 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005037 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005038 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005039}
5040
5041/*
5042 * Return TRUE when "ufunc" has old-style "..." varargs
5043 * or named varargs "...name: type".
5044 */
5045 int
5046has_varargs(ufunc_T *ufunc)
5047{
5048 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005049}
5050
5051/*
5052 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005053 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005054 */
5055 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005056function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005057{
5058 char_u *nm = name;
5059 char_u *p;
5060 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005061 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005062 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005063
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005064 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5065 if (no_deref)
5066 flag |= TFN_NO_DEREF;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005067 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005068 nm = skipwhite(nm);
5069
Bram Moolenaare38eab22019-12-05 21:50:01 +01005070 // Only accept "funcname", "funcname ", "funcname (..." and
5071 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005072 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005073 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005074 vim_free(p);
5075 return n;
5076}
5077
Bram Moolenaar113e1072019-01-20 15:30:40 +01005078#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005079 char_u *
5080get_expanded_name(char_u *name, int check)
5081{
5082 char_u *nm = name;
5083 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005084 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005085
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005086 p = trans_function_name(&nm, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005087 TFN_INT|TFN_QUIET, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005088
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005089 if (p != NULL && *nm == NUL
5090 && (!check || translated_function_exists(p, is_global)))
5091 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005092
5093 vim_free(p);
5094 return NULL;
5095}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005096#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005097
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005098/*
5099 * Function given to ExpandGeneric() to obtain the list of user defined
5100 * function names.
5101 */
5102 char_u *
5103get_user_func_name(expand_T *xp, int idx)
5104{
5105 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005106 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005107 static hashitem_T *hi;
5108 ufunc_T *fp;
5109
5110 if (idx == 0)
5111 {
5112 done = 0;
5113 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005114 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005115 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005116 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005117 {
5118 if (done++ > 0)
5119 ++hi;
5120 while (HASHITEM_EMPTY(hi))
5121 ++hi;
5122 fp = HI2UF(hi);
5123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005124 // don't show dead, dict and lambda functions
5125 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005126 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005127 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005128
5129 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005130 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005131
5132 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005133 if (xp->xp_context != EXPAND_USER_FUNC
5134 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005135 {
5136 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005137 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005138 STRCAT(IObuff, ")");
5139 }
5140 return IObuff;
5141 }
5142 return NULL;
5143}
5144
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005145/*
5146 * ":delfunction {name}"
5147 */
5148 void
5149ex_delfunction(exarg_T *eap)
5150{
5151 ufunc_T *fp = NULL;
5152 char_u *p;
5153 char_u *name;
5154 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005155 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005156
5157 p = eap->arg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005158 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi,
5159 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005160 vim_free(fudi.fd_newkey);
5161 if (name == NULL)
5162 {
5163 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005164 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005165 return;
5166 }
5167 if (!ends_excmd(*skipwhite(p)))
5168 {
5169 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005170 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005171 return;
5172 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005173 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005174 if (eap->nextcmd != NULL)
5175 *p = NUL;
5176
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005177 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005178 {
5179 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005180 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005181 vim_free(name);
5182 return;
5183 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005184 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005185 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005186 vim_free(name);
5187
5188 if (!eap->skip)
5189 {
5190 if (fp == NULL)
5191 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005192 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005193 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005194 return;
5195 }
5196 if (fp->uf_calls > 0)
5197 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005198 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005199 return;
5200 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005201 if (fp->uf_flags & FC_VIM9)
5202 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005203 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005204 return;
5205 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005206
5207 if (fudi.fd_dict != NULL)
5208 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005209 // Delete the dict item that refers to the function, it will
5210 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005211 dictitem_remove(fudi.fd_dict, fudi.fd_di);
5212 }
5213 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005214 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005215 // A normal function (not a numbered function or lambda) has a
5216 // refcount of 1 for the entry in the hashtable. When deleting
5217 // it and the refcount is more than one, it should be kept.
5218 // A numbered function and lambda should be kept if the refcount is
5219 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005220 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005221 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005222 // Function is still referenced somewhere. Don't free it but
5223 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005224 if (func_remove(fp))
5225 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005226 }
5227 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005228 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005229 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005230 }
5231}
5232
5233/*
5234 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005235 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005236 */
5237 void
5238func_unref(char_u *name)
5239{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005240 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005241
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005242 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005243 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005244 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005245 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005246 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005247#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005248 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005249#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005250 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005251 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005252 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005253}
5254
5255/*
5256 * Unreference a Function: decrement the reference count and free it when it
5257 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005258 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005259 */
5260 void
5261func_ptr_unref(ufunc_T *fp)
5262{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005263 if (fp != NULL && (--fp->uf_refcount <= 0
5264 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5265 && fp->uf_partial->pt_refcount <= 1
5266 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005267 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005268 // Only delete it when it's not being used. Otherwise it's done
5269 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005270 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005271 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005272 }
5273}
5274
5275/*
5276 * Count a reference to a Function.
5277 */
5278 void
5279func_ref(char_u *name)
5280{
5281 ufunc_T *fp;
5282
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005283 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005284 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005285 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005286 if (fp != NULL)
5287 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005288 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005289 // Only give an error for a numbered function.
5290 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005291 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005292}
5293
5294/*
5295 * Count a reference to a Function.
5296 */
5297 void
5298func_ptr_ref(ufunc_T *fp)
5299{
5300 if (fp != NULL)
5301 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005302}
5303
5304/*
5305 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5306 * referenced from anywhere that is in use.
5307 */
5308 static int
5309can_free_funccal(funccall_T *fc, int copyID)
5310{
5311 return (fc->l_varlist.lv_copyID != copyID
5312 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005313 && fc->l_avars.dv_copyID != copyID
5314 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005315}
5316
5317/*
5318 * ":return [expr]"
5319 */
5320 void
5321ex_return(exarg_T *eap)
5322{
5323 char_u *arg = eap->arg;
5324 typval_T rettv;
5325 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005326 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005327
5328 if (current_funccal == NULL)
5329 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005330 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005331 return;
5332 }
5333
Bram Moolenaar844fb642021-10-23 13:32:30 +01005334 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005335 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5336
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005337 if (eap->skip)
5338 ++emsg_skip;
5339
5340 eap->nextcmd = NULL;
5341 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005342 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005343 {
5344 if (!eap->skip)
5345 returning = do_return(eap, FALSE, TRUE, &rettv);
5346 else
5347 clear_tv(&rettv);
5348 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005349 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005350 else if (!eap->skip)
5351 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005352 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005353 update_force_abort();
5354
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005355 /*
5356 * Return unless the expression evaluation has been cancelled due to an
5357 * aborting error, an interrupt, or an exception.
5358 */
5359 if (!aborting())
5360 returning = do_return(eap, FALSE, TRUE, NULL);
5361 }
5362
Bram Moolenaare38eab22019-12-05 21:50:01 +01005363 // When skipping or the return gets pending, advance to the next command
5364 // in this line (!returning). Otherwise, ignore the rest of the line.
5365 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005366 if (returning)
5367 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005368 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02005369 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005370
5371 if (eap->skip)
5372 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02005373 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005374}
5375
5376/*
5377 * ":1,25call func(arg1, arg2)" function call.
5378 */
5379 void
5380ex_call(exarg_T *eap)
5381{
5382 char_u *arg = eap->arg;
5383 char_u *startarg;
5384 char_u *name;
5385 char_u *tofree;
5386 int len;
5387 typval_T rettv;
5388 linenr_T lnum;
5389 int doesrange;
5390 int failed = FALSE;
5391 funcdict_T fudi;
5392 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005393 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005394 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005395 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005396 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005397
Bram Moolenaare6b53242020-07-01 17:28:33 +02005398 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005399 if (eap->skip)
5400 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005401 // trans_function_name() doesn't work well when skipping, use eval0()
5402 // instead to skip to any following command, e.g. for:
5403 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005404 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005405 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005406 clear_tv(&rettv);
5407 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005408 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005409 return;
5410 }
5411
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005412 tofree = trans_function_name(&arg, NULL, eap->skip, TFN_INT,
Bram Moolenaar4525a572022-02-13 11:57:33 +00005413 &fudi, &partial, vim9script ? &type : NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005414 if (fudi.fd_newkey != NULL)
5415 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005416 // Still need to give an error message for missing key.
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005417 semsg(_(e_key_not_present_in_dictionary), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005418 vim_free(fudi.fd_newkey);
5419 }
5420 if (tofree == NULL)
5421 return;
5422
Bram Moolenaare38eab22019-12-05 21:50:01 +01005423 // Increase refcount on dictionary, it could get deleted when evaluating
5424 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005425 if (fudi.fd_dict != NULL)
5426 ++fudi.fd_dict->dv_refcount;
5427
Bram Moolenaare38eab22019-12-05 21:50:01 +01005428 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
5429 // contents. For VAR_PARTIAL get its partial, unless we already have one
5430 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005431 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005432 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00005433 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00005434 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005435
Bram Moolenaare38eab22019-12-05 21:50:01 +01005436 // Skip white space to allow ":call func ()". Not good, but required for
5437 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005438 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005439 if (*startarg != '(')
5440 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005441 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005442 goto end;
5443 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00005444 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02005445 {
5446 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
5447 goto end;
5448 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005449
5450 /*
5451 * When skipping, evaluate the function once, to find the end of the
5452 * arguments.
5453 * When the function takes a range, this is discovered after the first
5454 * call, and the loop is broken.
5455 */
5456 if (eap->skip)
5457 {
5458 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005459 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005460 }
5461 else
5462 lnum = eap->line1;
5463 for ( ; lnum <= eap->line2; ++lnum)
5464 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005465 funcexe_T funcexe;
5466
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005467 if (!eap->skip && eap->addr_count > 0)
5468 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01005469 if (lnum > curbuf->b_ml.ml_line_count)
5470 {
5471 // If the function deleted lines or switched to another buffer
5472 // the line number may become invalid.
Bram Moolenaar108010a2021-06-27 22:03:33 +02005473 emsg(_(e_invalid_range));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01005474 break;
5475 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005476 curwin->w_cursor.lnum = lnum;
5477 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005478 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005479 }
5480 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005481
Bram Moolenaara80faa82020-04-12 19:37:17 +02005482 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00005483 funcexe.fe_firstline = eap->line1;
5484 funcexe.fe_lastline = eap->line2;
5485 funcexe.fe_doesrange = &doesrange;
5486 funcexe.fe_evaluate = !eap->skip;
5487 funcexe.fe_partial = partial;
5488 funcexe.fe_selfdict = fudi.fd_dict;
5489 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005490 funcexe.fe_found_var = found_var;
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02005491 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaare6b53242020-07-01 17:28:33 +02005492 if (get_func_tv(name, -1, &rettv, &arg, &evalarg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005493 {
5494 failed = TRUE;
5495 break;
5496 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01005497 if (has_watchexpr())
5498 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005499
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005500 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaar32884ad2022-01-07 12:45:29 +00005501 if (handle_subscript(&arg, NULL, &rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005502 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005503 {
5504 failed = TRUE;
5505 break;
5506 }
5507
5508 clear_tv(&rettv);
5509 if (doesrange || eap->skip)
5510 break;
5511
Bram Moolenaare38eab22019-12-05 21:50:01 +01005512 // Stop when immediately aborting on error, or when an interrupt
5513 // occurred or an exception was thrown but not caught.
5514 // get_func_tv() returned OK, so that the check for trailing
5515 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005516 if (aborting())
5517 break;
5518 }
5519 if (eap->skip)
5520 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005521 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005522
Bram Moolenaar1d341892021-09-18 15:25:52 +02005523 // When inside :try we need to check for following "| catch" or "| endtry".
5524 // Not when there was an error, but do check if an exception was thrown.
5525 if ((!aborting() || did_throw)
5526 && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005527 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005528 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02005529 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02005530 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005531 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02005532 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01005533 {
5534 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00005535 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01005536 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005537 }
5538 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02005539 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005540 }
5541
5542end:
5543 dict_unref(fudi.fd_dict);
5544 vim_free(tofree);
5545}
5546
5547/*
5548 * Return from a function. Possibly makes the return pending. Also called
5549 * for a pending return at the ":endtry" or after returning from an extra
5550 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
5551 * when called due to a ":return" command. "rettv" may point to a typval_T
5552 * with the return rettv. Returns TRUE when the return can be carried out,
5553 * FALSE when the return gets pending.
5554 */
5555 int
5556do_return(
5557 exarg_T *eap,
5558 int reanimate,
5559 int is_cmd,
5560 void *rettv)
5561{
5562 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01005563 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005564
5565 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005566 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005567 current_funccal->returned = FALSE;
5568
5569 /*
5570 * Cleanup (and inactivate) conditionals, but stop when a try conditional
5571 * not in its finally clause (which then is to be executed next) is found.
5572 * In this case, make the ":return" pending for execution at the ":endtry".
5573 * Otherwise, return normally.
5574 */
5575 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
5576 if (idx >= 0)
5577 {
5578 cstack->cs_pending[idx] = CSTP_RETURN;
5579
5580 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005581 // A pending return again gets pending. "rettv" points to an
5582 // allocated variable with the rettv of the original ":return"'s
5583 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005584 cstack->cs_rettv[idx] = rettv;
5585 else
5586 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005587 // When undoing a return in order to make it pending, get the stored
5588 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005589 if (reanimate)
5590 rettv = current_funccal->rettv;
5591
5592 if (rettv != NULL)
5593 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005594 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005595 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
5596 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
5597 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005598 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005599 }
5600 else
5601 cstack->cs_rettv[idx] = NULL;
5602
5603 if (reanimate)
5604 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005605 // The pending return value could be overwritten by a ":return"
5606 // without argument in a finally clause; reset the default
5607 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005608 current_funccal->rettv->v_type = VAR_NUMBER;
5609 current_funccal->rettv->vval.v_number = 0;
5610 }
5611 }
5612 report_make_pending(CSTP_RETURN, rettv);
5613 }
5614 else
5615 {
5616 current_funccal->returned = TRUE;
5617
Bram Moolenaare38eab22019-12-05 21:50:01 +01005618 // If the return is carried out now, store the return value. For
5619 // a return immediately after reanimation, the value is already
5620 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005621 if (!reanimate && rettv != NULL)
5622 {
5623 clear_tv(current_funccal->rettv);
5624 *current_funccal->rettv = *(typval_T *)rettv;
5625 if (!is_cmd)
5626 vim_free(rettv);
5627 }
5628 }
5629
5630 return idx < 0;
5631}
5632
5633/*
5634 * Free the variable with a pending return value.
5635 */
5636 void
5637discard_pending_return(void *rettv)
5638{
5639 free_tv((typval_T *)rettv);
5640}
5641
5642/*
5643 * Generate a return command for producing the value of "rettv". The result
5644 * is an allocated string. Used by report_pending() for verbose messages.
5645 */
5646 char_u *
5647get_return_cmd(void *rettv)
5648{
5649 char_u *s = NULL;
5650 char_u *tofree = NULL;
5651 char_u numbuf[NUMBUFLEN];
5652
5653 if (rettv != NULL)
5654 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
5655 if (s == NULL)
5656 s = (char_u *)"";
5657
5658 STRCPY(IObuff, ":return ");
5659 STRNCPY(IObuff + 8, s, IOSIZE - 8);
5660 if (STRLEN(s) + 8 >= IOSIZE)
5661 STRCPY(IObuff + IOSIZE - 4, "...");
5662 vim_free(tofree);
5663 return vim_strsave(IObuff);
5664}
5665
5666/*
5667 * Get next function line.
5668 * Called by do_cmdline() to get the next line.
5669 * Returns allocated string, or NULL for end of function.
5670 */
5671 char_u *
5672get_func_line(
5673 int c UNUSED,
5674 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02005675 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005676 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005677{
5678 funccall_T *fcp = (funccall_T *)cookie;
5679 ufunc_T *fp = fcp->func;
5680 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005681 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005682
Bram Moolenaare38eab22019-12-05 21:50:01 +01005683 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005684 if (fcp->dbg_tick != debug_tick)
5685 {
5686 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005687 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005688 fcp->dbg_tick = debug_tick;
5689 }
5690#ifdef FEAT_PROFILE
5691 if (do_profiling == PROF_YES)
5692 func_line_end(cookie);
5693#endif
5694
5695 gap = &fp->uf_lines;
5696 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5697 || fcp->returned)
5698 retval = NULL;
5699 else
5700 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005701 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005702 while (fcp->linenr < gap->ga_len
5703 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
5704 ++fcp->linenr;
5705 if (fcp->linenr >= gap->ga_len)
5706 retval = NULL;
5707 else
5708 {
5709 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005710 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005711#ifdef FEAT_PROFILE
5712 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005713 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005714#endif
5715 }
5716 }
5717
Bram Moolenaare38eab22019-12-05 21:50:01 +01005718 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005719 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005720 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005721 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01005722 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005723 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005724 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005725 fcp->dbg_tick = debug_tick;
5726 }
5727
5728 return retval;
5729}
5730
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005731/*
5732 * Return TRUE if the currently active function should be ended, because a
5733 * return was encountered or an error occurred. Used inside a ":while".
5734 */
5735 int
5736func_has_ended(void *cookie)
5737{
5738 funccall_T *fcp = (funccall_T *)cookie;
5739
Bram Moolenaare38eab22019-12-05 21:50:01 +01005740 // Ignore the "abort" flag if the abortion behavior has been changed due to
5741 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005742 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5743 || fcp->returned);
5744}
5745
5746/*
5747 * return TRUE if cookie indicates a function which "abort"s on errors.
5748 */
5749 int
5750func_has_abort(
5751 void *cookie)
5752{
5753 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
5754}
5755
5756
5757/*
5758 * Turn "dict.Func" into a partial for "Func" bound to "dict".
5759 * Don't do this when "Func" is already a partial that was bound
5760 * explicitly (pt_auto is FALSE).
5761 * Changes "rettv" in-place.
5762 * Returns the updated "selfdict_in".
5763 */
5764 dict_T *
5765make_partial(dict_T *selfdict_in, typval_T *rettv)
5766{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005767 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005768 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005769 char_u fname_buf[FLEN_FIXED + 1];
5770 int error;
5771 dict_T *selfdict = selfdict_in;
5772
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005773 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
5774 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005775 fp = rettv->vval.v_partial->pt_func;
5776 else
5777 {
5778 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005779 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005780 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005781 if (fname == NULL)
5782 {
5783 // There is no point binding a dict to a NULL function, just create
5784 // a function reference.
5785 rettv->v_type = VAR_FUNC;
5786 rettv->vval.v_string = NULL;
5787 }
5788 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00005789 {
5790 char_u *tofree = NULL;
5791
5792 // Translate "s:func" to the stored function name.
5793 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
5794 fp = find_func(fname, FALSE);
5795 vim_free(tofree);
5796 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005797 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005798
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005799 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005800 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005801 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005802
5803 if (pt != NULL)
5804 {
5805 pt->pt_refcount = 1;
5806 pt->pt_dict = selfdict;
5807 pt->pt_auto = TRUE;
5808 selfdict = NULL;
5809 if (rettv->v_type == VAR_FUNC)
5810 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005811 // Just a function: Take over the function name and use
5812 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005813 pt->pt_name = rettv->vval.v_string;
5814 }
5815 else
5816 {
5817 partial_T *ret_pt = rettv->vval.v_partial;
5818 int i;
5819
Bram Moolenaare38eab22019-12-05 21:50:01 +01005820 // Partial: copy the function name, use selfdict and copy
5821 // args. Can't take over name or args, the partial might
5822 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005823 if (ret_pt->pt_name != NULL)
5824 {
5825 pt->pt_name = vim_strsave(ret_pt->pt_name);
5826 func_ref(pt->pt_name);
5827 }
5828 else
5829 {
5830 pt->pt_func = ret_pt->pt_func;
5831 func_ptr_ref(pt->pt_func);
5832 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005833 if (ret_pt->pt_argc > 0)
5834 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005835 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005836 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005837 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005838 pt->pt_argc = 0;
5839 else
5840 {
5841 pt->pt_argc = ret_pt->pt_argc;
5842 for (i = 0; i < pt->pt_argc; i++)
5843 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
5844 }
5845 }
5846 partial_unref(ret_pt);
5847 }
5848 rettv->v_type = VAR_PARTIAL;
5849 rettv->vval.v_partial = pt;
5850 }
5851 }
5852 return selfdict;
5853}
5854
5855/*
5856 * Return the name of the executed function.
5857 */
5858 char_u *
5859func_name(void *cookie)
5860{
5861 return ((funccall_T *)cookie)->func->uf_name;
5862}
5863
5864/*
5865 * Return the address holding the next breakpoint line for a funccall cookie.
5866 */
5867 linenr_T *
5868func_breakpoint(void *cookie)
5869{
5870 return &((funccall_T *)cookie)->breakpoint;
5871}
5872
5873/*
5874 * Return the address holding the debug tick for a funccall cookie.
5875 */
5876 int *
5877func_dbg_tick(void *cookie)
5878{
5879 return &((funccall_T *)cookie)->dbg_tick;
5880}
5881
5882/*
5883 * Return the nesting level for a funccall cookie.
5884 */
5885 int
5886func_level(void *cookie)
5887{
5888 return ((funccall_T *)cookie)->level;
5889}
5890
5891/*
5892 * Return TRUE when a function was ended by a ":return" command.
5893 */
5894 int
5895current_func_returned(void)
5896{
5897 return current_funccal->returned;
5898}
5899
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005900 int
5901free_unref_funccal(int copyID, int testing)
5902{
5903 int did_free = FALSE;
5904 int did_free_funccal = FALSE;
5905 funccall_T *fc, **pfc;
5906
5907 for (pfc = &previous_funccal; *pfc != NULL; )
5908 {
5909 if (can_free_funccal(*pfc, copyID))
5910 {
5911 fc = *pfc;
5912 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01005913 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005914 did_free = TRUE;
5915 did_free_funccal = TRUE;
5916 }
5917 else
5918 pfc = &(*pfc)->caller;
5919 }
5920 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005921 // When a funccal was freed some more items might be garbage
5922 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005923 (void)garbage_collect(testing);
5924
5925 return did_free;
5926}
5927
5928/*
Bram Moolenaarba209902016-08-24 22:06:38 +02005929 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005930 */
5931 static funccall_T *
5932get_funccal(void)
5933{
5934 int i;
5935 funccall_T *funccal;
5936 funccall_T *temp_funccal;
5937
5938 funccal = current_funccal;
5939 if (debug_backtrace_level > 0)
5940 {
5941 for (i = 0; i < debug_backtrace_level; i++)
5942 {
5943 temp_funccal = funccal->caller;
5944 if (temp_funccal)
5945 funccal = temp_funccal;
5946 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005947 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005948 debug_backtrace_level = i;
5949 }
5950 }
5951 return funccal;
5952}
5953
5954/*
5955 * Return the hashtable used for local variables in the current funccal.
5956 * Return NULL if there is no current funccal.
5957 */
5958 hashtab_T *
5959get_funccal_local_ht()
5960{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005961 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005962 return NULL;
5963 return &get_funccal()->l_vars.dv_hashtab;
5964}
5965
5966/*
5967 * Return the l: scope variable.
5968 * Return NULL if there is no current funccal.
5969 */
5970 dictitem_T *
5971get_funccal_local_var()
5972{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005973 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005974 return NULL;
5975 return &get_funccal()->l_vars_var;
5976}
5977
5978/*
5979 * Return the hashtable used for argument in the current funccal.
5980 * Return NULL if there is no current funccal.
5981 */
5982 hashtab_T *
5983get_funccal_args_ht()
5984{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005985 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005986 return NULL;
5987 return &get_funccal()->l_avars.dv_hashtab;
5988}
5989
5990/*
5991 * Return the a: scope variable.
5992 * Return NULL if there is no current funccal.
5993 */
5994 dictitem_T *
5995get_funccal_args_var()
5996{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005997 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005998 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01005999 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006000}
6001
6002/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006003 * List function variables, if there is a function.
6004 */
6005 void
6006list_func_vars(int *first)
6007{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006008 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006009 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006010 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006011}
6012
6013/*
6014 * If "ht" is the hashtable for local variables in the current funccal, return
6015 * the dict that contains it.
6016 * Otherwise return NULL.
6017 */
6018 dict_T *
6019get_current_funccal_dict(hashtab_T *ht)
6020{
6021 if (current_funccal != NULL
6022 && ht == &current_funccal->l_vars.dv_hashtab)
6023 return &current_funccal->l_vars;
6024 return NULL;
6025}
6026
6027/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006028 * Search hashitem in parent scope.
6029 */
6030 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006031find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006032{
6033 funccall_T *old_current_funccal = current_funccal;
6034 hashtab_T *ht;
6035 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006036 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006037
6038 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
6039 return NULL;
6040
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006041 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006042 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006043 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006044 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006045 ht = find_var_ht(name, &varname);
6046 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006047 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006048 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006049 if (!HASHITEM_EMPTY(hi))
6050 {
6051 *pht = ht;
6052 break;
6053 }
6054 }
6055 if (current_funccal == current_funccal->func->uf_scoped)
6056 break;
6057 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006058 }
6059 current_funccal = old_current_funccal;
6060
6061 return hi;
6062}
6063
6064/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006065 * Search variable in parent scope.
6066 */
6067 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006068find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006069{
6070 dictitem_T *v = NULL;
6071 funccall_T *old_current_funccal = current_funccal;
6072 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006073 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006074
6075 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
6076 return NULL;
6077
Bram Moolenaare38eab22019-12-05 21:50:01 +01006078 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006079 current_funccal = current_funccal->func->uf_scoped;
6080 while (current_funccal)
6081 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006082 ht = find_var_ht(name, &varname);
6083 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006084 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006085 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006086 if (v != NULL)
6087 break;
6088 }
6089 if (current_funccal == current_funccal->func->uf_scoped)
6090 break;
6091 current_funccal = current_funccal->func->uf_scoped;
6092 }
6093 current_funccal = old_current_funccal;
6094
6095 return v;
6096}
6097
6098/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006099 * Set "copyID + 1" in previous_funccal and callers.
6100 */
6101 int
6102set_ref_in_previous_funccal(int copyID)
6103{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006104 funccall_T *fc;
6105
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006106 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006107 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006108 fc->fc_copyID = copyID + 1;
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006109 if (set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
6110 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
6111 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL))
6112 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006113 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006114 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006115}
6116
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006117 static int
6118set_ref_in_funccal(funccall_T *fc, int copyID)
6119{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006120 if (fc->fc_copyID != copyID)
6121 {
6122 fc->fc_copyID = copyID;
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006123 if (set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
6124 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
6125 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
6126 || set_ref_in_func(NULL, fc->func, copyID))
6127 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006128 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006129 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006130}
6131
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006132/*
6133 * Set "copyID" in all local vars and arguments in the call stack.
6134 */
6135 int
6136set_ref_in_call_stack(int copyID)
6137{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006138 funccall_T *fc;
6139 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006140
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006141 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6142 if (set_ref_in_funccal(fc, copyID))
6143 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006144
6145 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006146 for (entry = funccal_stack; entry != NULL; entry = entry->next)
6147 for (fc = entry->top_funccal; fc != NULL; fc = fc->caller)
6148 if (set_ref_in_funccal(fc, copyID))
6149 return TRUE;
6150 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006151}
6152
6153/*
6154 * Set "copyID" in all functions available by name.
6155 */
6156 int
6157set_ref_in_functions(int copyID)
6158{
6159 int todo;
6160 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006161 ufunc_T *fp;
6162
6163 todo = (int)func_hashtab.ht_used;
6164 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006165 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006166 if (!HASHITEM_EMPTY(hi))
6167 {
6168 --todo;
6169 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006170 if (!func_name_refcount(fp->uf_name)
6171 && set_ref_in_func(NULL, fp, copyID))
6172 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006173 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006174 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006175 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006176}
6177
6178/*
6179 * Set "copyID" in all function arguments.
6180 */
6181 int
6182set_ref_in_func_args(int copyID)
6183{
6184 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006185
6186 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006187 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
6188 copyID, NULL, NULL))
6189 return TRUE;
6190 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006191}
6192
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006193/*
6194 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006195 * Returns TRUE if setting references failed somehow.
6196 */
6197 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006198set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006199{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006200 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006201 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01006202 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006203 char_u fname_buf[FLEN_FIXED + 1];
6204 char_u *tofree = NULL;
6205 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006206 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006207
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006208 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006209 return FALSE;
6210
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006211 if (fp_in == NULL)
6212 {
6213 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006214 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006215 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006216 if (fp != NULL)
6217 {
6218 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006219 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006220 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02006221
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006222 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006223 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006224}
6225
Bram Moolenaare38eab22019-12-05 21:50:01 +01006226#endif // FEAT_EVAL