blob: 9ac681f202af83dc6ffc69333163e2a7b2297253 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +020011 * userfunc.c: User defined function support
Bram Moolenaara9b579f2016-07-17 18:29:19 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020017/*
18 * All user-defined functions are found in this hashtable.
19 */
20static hashtab_T func_hashtab;
21
Bram Moolenaare38eab22019-12-05 21:50:01 +010022// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020023static garray_T funcargs = GA_EMPTY;
24
Bram Moolenaar209b8e32019-03-14 13:43:24 +010025// pointer to funccal for currently active function
26static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020027
Bram Moolenaar209b8e32019-03-14 13:43:24 +010028// Pointer to list of previously used funccal, still around because some
29// item in it is still being used.
30static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020031
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020032static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +010033static void func_clear(ufunc_T *fp, int force);
34static int func_free(ufunc_T *fp, int force);
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +010035static char_u *untrans_function_name(char_u *name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020036
37 void
38func_init()
39{
40 hash_init(&func_hashtab);
41}
42
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}
51
52/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020053 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020054 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010055 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010056 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaardce24412022-02-08 20:35:30 +000057 * If "eap" is not NULL use it to check for an already declared name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010058 * Return a pointer to after the type.
59 * When something is wrong return "arg".
60 */
61 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010062one_function_arg(
63 char_u *arg,
64 garray_T *newargs,
65 garray_T *argtypes,
66 int types_optional,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010067 evalarg_T *evalarg,
Bram Moolenaardce24412022-02-08 20:35:30 +000068 exarg_T *eap,
Bram Moolenaar2a389082021-04-09 20:24:31 +020069 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010070 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010071{
Bram Moolenaar6e949782020-04-13 17:21:00 +020072 char_u *p = arg;
73 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020074 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010075
76 while (ASCII_ISALNUM(*p) || *p == '_')
77 ++p;
78 if (arg == p || isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020079 || (argtypes == NULL
80 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
81 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082 {
83 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000084 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085 return arg;
86 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010087
Bram Moolenaar057e84a2021-02-28 16:55:11 +010088 // Vim9 script: cannot use script var name for argument. In function: also
89 // check local vars and arguments.
90 if (!skip && argtypes != NULL && check_defined(arg, p - arg,
Bram Moolenaardce24412022-02-08 20:35:30 +000091 evalarg == NULL ? NULL : evalarg->eval_cctx,
92 eap == NULL ? NULL : eap->cstack, TRUE) == FAIL)
Bram Moolenaarb4893b82021-02-21 22:20:24 +010093 return arg;
Bram Moolenaarb4893b82021-02-21 22:20:24 +010094
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010095 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
96 return arg;
97 if (newargs != NULL)
98 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010099 int c;
100 int i;
101
102 c = *p;
103 *p = NUL;
104 arg_copy = vim_strsave(arg);
105 if (arg_copy == NULL)
106 {
107 *p = c;
108 return arg;
109 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200110 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200111 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200112 // Check for duplicate argument name.
113 for (i = 0; i < newargs->ga_len; ++i)
114 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
115 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000116 semsg(_(e_duplicate_argument_name_str), arg_copy);
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200117 vim_free(arg_copy);
118 return arg;
119 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100120 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
121 newargs->ga_len++;
122
123 *p = c;
124 }
125
126 // get any type from "arg: type"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100127 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100128 {
129 char_u *type = NULL;
130
Bram Moolenaar6e949782020-04-13 17:21:00 +0200131 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
132 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200133 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200134 arg_copy == NULL ? arg : arg_copy);
135 p = skipwhite(p);
136 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100137 if (*p == ':')
138 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200139 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100140 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200141 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100142 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200143 return arg;
144 }
145 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200146 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100147 if (!skip)
148 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200150 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200151 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200152 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200153 arg_copy == NULL ? arg : arg_copy);
154 return arg;
155 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100156 if (!skip)
157 {
158 if (type == NULL && types_optional)
159 // lambda arguments default to "any" type
Bram Moolenaar2a389082021-04-09 20:24:31 +0200160 type = vim_strsave((char_u *)
161 (is_vararg ? "list<any>" : "any"));
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100162 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
163 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 }
165
166 return p;
167}
168
169/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000170 * Handle line continuation in function arguments or body.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000171 * Get a next line, store it in "eap" if appropriate and put the line in
172 * "lines_to_free" to free the line later.
Bram Moolenaar7473a842021-12-28 17:55:26 +0000173 */
174 static char_u *
175get_function_line(
176 exarg_T *eap,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000177 garray_T *lines_to_free,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000178 int indent,
179 getline_opt_T getline_options)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000180{
181 char_u *theline;
182
183 if (eap->getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +0000184 theline = getcmdline(':', 0L, indent, 0);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000185 else
186 theline = eap->getline(':', eap->cookie, indent, getline_options);
187 if (theline != NULL)
188 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000189 if (lines_to_free->ga_len > 0
190 && *eap->cmdlinep == ((char_u **)lines_to_free->ga_data)
191 [lines_to_free->ga_len - 1])
Bram Moolenaar7473a842021-12-28 17:55:26 +0000192 *eap->cmdlinep = theline;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000193 ga_add_string(lines_to_free, theline);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000194 }
195
196 return theline;
197}
198
199/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200200 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100201 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100202 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200203 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100204 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200205get_function_args(
206 char_u **argp,
207 char_u endchar,
208 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100209 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100210 int types_optional, // types optional if "argtypes" is not NULL
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100211 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200212 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200213 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200214 int skip,
Bram Moolenaardce24412022-02-08 20:35:30 +0000215 exarg_T *eap, // can be NULL
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000216 garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200217{
218 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100219 char_u *arg;
220 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200221 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200222 int any_default = FALSE;
223 char_u *expr;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100224 char_u *whitep = *argp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200225
226 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000227 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000229 ga_init2(argtypes, sizeof(char_u *), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200230 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000231 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200232
233 if (varargs != NULL)
234 *varargs = FALSE;
235
236 /*
237 * Isolate the arguments: "arg1, arg2, ...)"
238 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100239 arg = skipwhite(*argp);
240 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200241 while (*p != endchar)
242 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200243 while (eap != NULL && eap->getline != NULL
244 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200245 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200246 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000247 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000248 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000249
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200250 if (theline == NULL)
251 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200252 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200253 p = skipwhite(theline);
254 }
255
256 if (mustend && *p != endchar)
257 {
258 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000259 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100260 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200261 }
262 if (*p == endchar)
263 break;
264
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200265 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
266 {
267 if (varargs != NULL)
268 *varargs = TRUE;
269 p += 3;
270 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271
272 if (argtypes != NULL)
273 {
274 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200275 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100276 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100277 if (!skip)
278 emsg(_(e_missing_name_after_dots));
279 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100280 }
281
282 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100283 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaardce24412022-02-08 20:35:30 +0000284 evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100285 if (p == arg)
286 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100287 if (*skipwhite(p) == '=')
288 {
289 emsg(_(e_cannot_use_default_for_variable_arguments));
290 break;
291 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100292 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200293 }
294 else
295 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200296 char_u *np;
297
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200298 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100299 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaardce24412022-02-08 20:35:30 +0000300 evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100301 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200302 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200303
Bram Moolenaar015cf102021-06-26 21:52:02 +0200304 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200305 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200306 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200307 if (*np == '=' && np[1] != '=' && np[1] != '~'
308 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200309 {
310 typval_T rettv;
311
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100312 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200313 any_default = TRUE;
Bram Moolenaareb5adf12022-09-04 13:41:37 +0100314 p = skipwhite(np + 1);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200315 expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200316 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200317 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200318 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200319 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200320 if (ga_grow(default_args, 1) == FAIL)
321 goto err_ret;
322
323 // trim trailing whitespace
324 while (p > expr && VIM_ISWHITE(p[-1]))
325 p--;
326 c = *p;
327 *p = NUL;
328 expr = vim_strsave(expr);
329 if (expr == NULL)
330 {
331 *p = c;
332 goto err_ret;
333 }
334 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200335 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200336 default_args->ga_len++;
337 *p = c;
338 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200339 }
340 else
341 mustend = TRUE;
342 }
343 else if (any_default)
344 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000345 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200346 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200347 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200348
349 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
350 {
351 // Be tolerant when skipping
352 if (!skip)
353 {
354 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
355 goto err_ret;
356 }
357 p = skipwhite(p);
358 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200359 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200360 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200361 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200362 // Don't give this error when skipping, it makes the "->" not
363 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100364 // Allow missing space after comma in legacy functions.
365 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200366 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
367 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100368 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200369 goto err_ret;
370 }
371 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200372 else
373 mustend = TRUE;
374 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200375 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200376 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200377 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200378
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200379 if (*p != endchar)
380 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100381 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200382
383 *argp = p;
384 return OK;
385
386err_ret:
387 if (newargs != NULL)
388 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200389 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200390 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200391 return FAIL;
392}
393
394/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100395 * Parse the argument types, filling "fp->uf_arg_types".
396 * Return OK or FAIL.
397 */
398 static int
399parse_argument_types(ufunc_T *fp, garray_T *argtypes, int varargs)
400{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200401 int len = 0;
402
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100403 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
404 if (argtypes->ga_len > 0)
405 {
406 // When "varargs" is set the last name/type goes into uf_va_name
407 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200408 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100409
410 if (len > 0)
411 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
412 if (fp->uf_arg_types != NULL)
413 {
414 int i;
415 type_T *type;
416
417 for (i = 0; i < len; ++ i)
418 {
419 char_u *p = ((char_u **)argtypes->ga_data)[i];
420
421 if (p == NULL)
422 // will get the type from the default value
423 type = &t_unknown;
424 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100425 type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100426 if (type == NULL)
427 return FAIL;
428 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000429 if (i < fp->uf_args.ga_len
430 && (type->tt_type == VAR_FUNC
431 || type->tt_type == VAR_PARTIAL)
432 && var_wrong_func_name(
433 ((char_u **)fp->uf_args.ga_data)[i], TRUE))
434 return FAIL;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100435 }
436 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100437 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200438
439 if (varargs)
440 {
441 char_u *p;
442
443 // Move the last argument "...name: type" to uf_va_name and
444 // uf_va_type.
445 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
446 [fp->uf_args.ga_len - 1];
447 --fp->uf_args.ga_len;
448 p = ((char_u **)argtypes->ga_data)[len];
449 if (p == NULL)
450 // TODO: get type from default value
451 fp->uf_va_type = &t_list_any;
452 else
453 {
454 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
455 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
456 {
457 semsg(_(e_variable_arguments_type_must_be_list_str),
458 ((char_u **)argtypes->ga_data)[len]);
459 return FAIL;
460 }
461 }
462 if (fp->uf_va_type == NULL)
463 return FAIL;
464 }
465
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100466 return OK;
467}
468
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100469 static int
470parse_return_type(ufunc_T *fp, char_u *ret_type)
471{
472 if (ret_type == NULL)
473 fp->uf_ret_type = &t_void;
474 else
475 {
476 char_u *p = ret_type;
477
478 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
479 if (fp->uf_ret_type == NULL)
480 {
481 fp->uf_ret_type = &t_void;
482 return FAIL;
483 }
484 }
485 return OK;
486}
487
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100488/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200489 * Register function "fp" as using "current_funccal" as its scope.
490 */
491 static int
492register_closure(ufunc_T *fp)
493{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200494 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100495 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200496 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200497 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200498 fp->uf_scoped = current_funccal;
499 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200500
Bram Moolenaar58016442016-07-31 18:30:22 +0200501 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
502 return FAIL;
503 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
504 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200505 return OK;
506}
507
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100508 static void
509set_ufunc_name(ufunc_T *fp, char_u *name)
510{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100511 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
512 // actually extends beyond the struct.
513 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100514
515 if (name[0] == K_SPECIAL)
516 {
517 fp->uf_name_exp = alloc(STRLEN(name) + 3);
518 if (fp->uf_name_exp != NULL)
519 {
520 STRCPY(fp->uf_name_exp, "<SNR>");
521 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
522 }
523 }
524}
525
Bram Moolenaar58016442016-07-31 18:30:22 +0200526/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100527 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
528 * return "buf" filled with a readable function name.
529 * Otherwise just return "name", thus the return value can always be used.
530 * "name" and "buf" may be equal.
531 */
532 char_u *
533make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
534{
535 size_t len;
536
537 if (name[0] != K_SPECIAL)
538 return name;
539 len = STRLEN(name);
540 if (len + 3 > bufsize)
541 return name;
542
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100543 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100544 mch_memmove(buf, "<SNR>", 5);
545 return buf;
546}
547
548/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200549 * Get a name for a lambda. Returned in static memory.
550 */
551 char_u *
552get_lambda_name(void)
553{
554 static char_u name[30];
555 static int lambda_no = 0;
556
557 sprintf((char*)name, "<lambda>%d", ++lambda_no);
558 return name;
559}
560
Bram Moolenaar801ab062020-06-25 19:27:56 +0200561#if defined(FEAT_LUA) || defined(PROTO)
562/*
563 * Registers a native C callback which can be called from Vim script.
564 * Returns the name of the Vim script function.
565 */
566 char_u *
567register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
568{
569 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200570 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200571
572 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
573 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200574 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200575
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200576 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200577 fp->uf_refcount = 1;
578 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000579 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200580 fp->uf_calls = 0;
581 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200582 fp->uf_cb = cb;
583 fp->uf_cb_free = cb_free;
584 fp->uf_cb_state = state;
585
586 set_ufunc_name(fp, name);
587 hash_add(&func_hashtab, UF2HIKEY(fp));
588
589 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200590}
591#endif
592
Bram Moolenaar04b12692020-05-04 23:24:44 +0200593/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100594 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100595 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100596 * If "white_error" is not NULL check for correct use of white space and set
597 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100598 * Return NULL if no valid arrow found.
599 */
600 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100601skip_arrow(
602 char_u *start,
603 int equal_arrow,
604 char_u **ret_type,
605 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100606{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100607 char_u *s = start;
608 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100609
610 if (equal_arrow)
611 {
612 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100613 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100614 if (white_error != NULL && !VIM_ISWHITE(s[1]))
615 {
616 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100617 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100618 return NULL;
619 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100620 s = skipwhite(s + 1);
621 *ret_type = s;
622 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100623 if (s == *ret_type)
624 {
625 emsg(_(e_missing_return_type));
626 return NULL;
627 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100628 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100629 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100630 s = skipwhite(s);
631 if (*s != '=')
632 return NULL;
633 ++s;
634 }
635 if (*s != '>')
636 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100637 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
638 || !IS_WHITE_OR_NUL(s[1])))
639 {
640 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100641 semsg(_(e_white_space_required_before_and_after_str_at_str),
642 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100643 return NULL;
644 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100645 return skipwhite(s + 1);
646}
647
648/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100649 * Check if "*cmd" points to a function command and if so advance "*cmd" and
650 * return TRUE.
651 * Otherwise return FALSE;
652 * Do not consider "function(" to be a command.
653 */
654 static int
655is_function_cmd(char_u **cmd)
656{
657 char_u *p = *cmd;
658
659 if (checkforcmd(&p, "function", 2))
660 {
661 if (*p == '(')
662 return FALSE;
663 *cmd = p;
664 return TRUE;
665 }
666 return FALSE;
667}
668
669/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200670 * Called when defining a function: The context may be needed for script
671 * variables declared in a block that is visible now but not when the function
672 * is compiled or called later.
673 */
674 static void
675function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
676{
677 if (cstack != NULL && cstack->cs_idx >= 0)
678 {
679 int count = cstack->cs_idx + 1;
680 int i;
681
682 fp->uf_block_ids = ALLOC_MULT(int, count);
683 if (fp->uf_block_ids != NULL)
684 {
685 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
686 sizeof(int) * count);
687 fp->uf_block_depth = count;
688 }
689
690 // Set flag in each block to indicate a function was defined. This
691 // is used to keep the variable when leaving the block, see
692 // hide_script_var().
693 for (i = 0; i <= cstack->cs_idx; ++i)
694 cstack->cs_flags[i] |= CSF_FUNC_DEF;
695 }
696}
697
698/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100699 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200700 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100701 * "newlines" must already have been initialized.
702 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
703 */
704 static int
705get_function_body(
706 exarg_T *eap,
707 garray_T *newlines,
708 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000709 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100710{
711 linenr_T sourcing_lnum_top = SOURCING_LNUM;
712 linenr_T sourcing_lnum_off;
713 int saved_wait_return = need_wait_return;
714 char_u *line_arg = line_arg_in;
715 int vim9_function = eap->cmdidx == CMD_def
716 || eap->cmdidx == CMD_block;
717#define MAX_FUNC_NESTING 50
718 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200719 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100720 int nesting = 0;
721 getline_opt_T getline_options;
722 int indent = 2;
723 char_u *skip_until = NULL;
724 int ret = FAIL;
725 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200726 int heredoc_concat_len = 0;
727 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100728 char_u *heredoc_trimmed = NULL;
729
Bram Moolenaar20677332021-06-06 17:02:53 +0200730 ga_init2(&heredoc_ga, 1, 500);
731
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100732 // Detect having skipped over comment lines to find the return
733 // type. Add NULL lines to keep the line count correct.
734 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
735 if (SOURCING_LNUM < sourcing_lnum_off)
736 {
737 sourcing_lnum_off -= SOURCING_LNUM;
738 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
739 goto theend;
740 while (sourcing_lnum_off-- > 0)
741 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
742 }
743
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200744 nesting_def[0] = vim9_function;
745 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100746 getline_options = vim9_function
747 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
748 for (;;)
749 {
750 char_u *theline;
751 char_u *p;
752 char_u *arg;
753
754 if (KeyTyped)
755 {
756 msg_scroll = TRUE;
757 saved_wait_return = FALSE;
758 }
759 need_wait_return = FALSE;
760
761 if (line_arg != NULL)
762 {
763 // Use eap->arg, split up in parts by line breaks.
764 theline = line_arg;
765 p = vim_strchr(theline, '\n');
766 if (p == NULL)
767 line_arg += STRLEN(line_arg);
768 else
769 {
770 *p = NUL;
771 line_arg = p + 1;
772 }
773 }
774 else
775 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000776 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100777 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100778 }
779 if (KeyTyped)
780 lines_left = Rows - 1;
781 if (theline == NULL)
782 {
783 // Use the start of the function for the line number.
784 SOURCING_LNUM = sourcing_lnum_top;
785 if (skip_until != NULL)
786 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200787 else if (nesting_inline[nesting])
788 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100789 else if (eap->cmdidx == CMD_def)
790 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100791 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000792 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100793 goto theend;
794 }
795
796 // Detect line continuation: SOURCING_LNUM increased more than one.
797 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
798 if (SOURCING_LNUM < sourcing_lnum_off)
799 sourcing_lnum_off -= SOURCING_LNUM;
800 else
801 sourcing_lnum_off = 0;
802
803 if (skip_until != NULL)
804 {
805 // Don't check for ":endfunc"/":enddef" between
806 // * ":append" and "."
807 // * ":python <<EOF" and "EOF"
808 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
809 if (heredoc_trimmed == NULL
810 || (is_heredoc && skipwhite(theline) == theline)
811 || STRNCMP(theline, heredoc_trimmed,
812 STRLEN(heredoc_trimmed)) == 0)
813 {
814 if (heredoc_trimmed == NULL)
815 p = theline;
816 else if (is_heredoc)
817 p = skipwhite(theline) == theline
818 ? theline : theline + STRLEN(heredoc_trimmed);
819 else
820 p = theline + STRLEN(heredoc_trimmed);
821 if (STRCMP(p, skip_until) == 0)
822 {
823 VIM_CLEAR(skip_until);
824 VIM_CLEAR(heredoc_trimmed);
825 getline_options = vim9_function
826 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
827 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200828
829 if (heredoc_concat_len > 0)
830 {
831 // Replace the starting line with all the concatenated
832 // lines.
833 ga_concat(&heredoc_ga, theline);
834 vim_free(((char_u **)(newlines->ga_data))[
835 heredoc_concat_len - 1]);
836 ((char_u **)(newlines->ga_data))[
837 heredoc_concat_len - 1] = heredoc_ga.ga_data;
838 ga_init(&heredoc_ga);
839 heredoc_concat_len = 0;
840 theline += STRLEN(theline); // skip the "EOF"
841 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100842 }
843 }
844 }
845 else
846 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200847 int c;
848 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +0000849 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100850
851 // skip ':' and blanks
852 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
853 ;
854
855 // Check for "endfunction", "enddef" or "}".
856 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +0000857 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200858 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100859 ? *p == '}'
860 : (checkforcmd(&p, nesting_def[nesting]
861 ? "enddef" : "endfunction", 4)
862 && *p != ':'))
863 {
Bram Moolenaarb2175222022-03-05 20:24:41 +0000864 if (!nesting_inline[nesting] && nesting_def[nesting]
865 && p < cmd + 6)
866 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100867 if (nesting-- == 0)
868 {
869 char_u *nextcmd = NULL;
870
871 if (*p == '|' || *p == '}')
872 nextcmd = p + 1;
873 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
874 nextcmd = line_arg;
875 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100876 && (vim9_function || p_verbose > 0))
877 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200878 SOURCING_LNUM = sourcing_lnum_top
879 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100880 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000881 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100882 else
883 give_warning2((char_u *)
884 _("W22: Text found after :endfunction: %s"),
885 p, TRUE);
886 }
887 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100888 {
889 // Another command follows. If the line came from "eap"
890 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000891 // change "eap->cmdlinep" to point to the last fetched
892 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100893 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000894 if (lines_to_free->ga_len > 0
895 && *eap->cmdlinep !=
896 ((char_u **)lines_to_free->ga_data)
897 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100898 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000899 // *cmdlinep will be freed later, thus remove the
900 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100901 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000902 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
903 [lines_to_free->ga_len - 1];
904 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100905 }
906 }
907 break;
908 }
909 }
910
911 // Check for mismatched "endfunc" or "enddef".
912 // We don't check for "def" inside "func" thus we also can't check
913 // for "enddef".
914 // We continue to find the end of the function, although we might
915 // not find it.
916 else if (nesting_def[nesting])
917 {
918 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
919 emsg(_(e_mismatched_endfunction));
920 }
921 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
922 emsg(_(e_mismatched_enddef));
923
924 // Increase indent inside "if", "while", "for" and "try", decrease
925 // at "end".
926 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
927 indent -= 2;
928 else if (STRNCMP(p, "if", 2) == 0
929 || STRNCMP(p, "wh", 2) == 0
930 || STRNCMP(p, "for", 3) == 0
931 || STRNCMP(p, "try", 3) == 0)
932 indent += 2;
933
934 // Check for defining a function inside this function.
935 // Only recognize "def" inside "def", not inside "function",
936 // For backwards compatibility, see Test_function_python().
937 c = *p;
938 if (is_function_cmd(&p)
939 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
940 {
941 if (*p == '!')
942 p = skipwhite(p + 1);
943 p += eval_fname_script(p);
944 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL,
945 NULL, NULL));
946 if (*skipwhite(p) == '(')
947 {
948 if (nesting == MAX_FUNC_NESTING - 1)
949 emsg(_(e_function_nesting_too_deep));
950 else
951 {
952 ++nesting;
953 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200954 nesting_inline[nesting] = FALSE;
955 indent += 2;
956 }
957 }
958 }
959
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200960 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200961 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200962 // Not a comment line: check for nested inline function.
963 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200964 while (end > p && VIM_ISWHITE(*end))
965 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +0200966 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200967 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200968 int is_block;
969
970 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200971 --end;
972 while (end > p && VIM_ISWHITE(*end))
973 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200974 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
975 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200976 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200977 char_u *s = p;
978
979 // check for line starting with "au" for :autocmd or
980 // "com" for :command, these can use a {} block
981 is_block = checkforcmd_noparen(&s, "autocmd", 2)
982 || checkforcmd_noparen(&s, "command", 3);
983 }
984
985 if (is_block)
986 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200987 if (nesting == MAX_FUNC_NESTING - 1)
988 emsg(_(e_function_nesting_too_deep));
989 else
990 {
991 ++nesting;
992 nesting_def[nesting] = TRUE;
993 nesting_inline[nesting] = TRUE;
994 indent += 2;
995 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100996 }
997 }
998 }
999
1000 // Check for ":append", ":change", ":insert". Not for :def.
1001 p = skip_range(p, FALSE, NULL);
1002 if (!vim9_function
1003 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1004 || (p[0] == 'c'
1005 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1006 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1007 && (STRNCMP(&p[3], "nge", 3) != 0
1008 || !ASCII_ISALPHA(p[6])))))))
1009 || (p[0] == 'i'
1010 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1011 && (!ASCII_ISALPHA(p[2])
1012 || (p[2] == 's'
1013 && (!ASCII_ISALPHA(p[3])
1014 || p[3] == 'e'))))))))
1015 skip_until = vim_strsave((char_u *)".");
1016
1017 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1018 arg = skipwhite(skiptowhite(p));
1019 if (arg[0] == '<' && arg[1] =='<'
1020 && ((p[0] == 'p' && p[1] == 'y'
1021 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1022 || ((p[2] == '3' || p[2] == 'x')
1023 && !ASCII_ISALPHA(p[3]))))
1024 || (p[0] == 'p' && p[1] == 'e'
1025 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1026 || (p[0] == 't' && p[1] == 'c'
1027 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1028 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1029 && !ASCII_ISALPHA(p[3]))
1030 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1031 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1032 || (p[0] == 'm' && p[1] == 'z'
1033 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1034 ))
1035 {
1036 // ":python <<" continues until a dot, like ":append"
1037 p = skipwhite(arg + 2);
1038 if (STRNCMP(p, "trim", 4) == 0)
1039 {
1040 // Ignore leading white space.
1041 p = skipwhite(p + 4);
1042 heredoc_trimmed = vim_strnsave(theline,
1043 skipwhite(theline) - theline);
1044 }
1045 if (*p == NUL)
1046 skip_until = vim_strsave((char_u *)".");
1047 else
1048 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1049 getline_options = GETLINE_NONE;
1050 is_heredoc = TRUE;
Bram Moolenaard881d152022-05-13 13:50:36 +01001051 if (eap->cmdidx == CMD_def && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001052 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001053 }
1054
Bram Moolenaard881d152022-05-13 13:50:36 +01001055 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001056 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001057 // Check for ":cmd v =<< [trim] EOF"
1058 // and ":cmd [a, b] =<< [trim] EOF"
1059 // and "lines =<< [trim] EOF" for Vim9
1060 // Where "cmd" can be "let", "var", "final" or "const".
1061 arg = skipwhite(skiptowhite(p));
1062 if (*arg == '[')
1063 arg = vim_strchr(arg, ']');
1064 if (arg != NULL)
1065 {
1066 int found = (eap->cmdidx == CMD_def && arg[0] == '='
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001067 && arg[1] == '<' && arg[2] =='<');
1068
Bram Moolenaard881d152022-05-13 13:50:36 +01001069 if (!found)
1070 // skip over the argument after "cmd"
1071 arg = skipwhite(skiptowhite(arg));
1072 if (found || (arg[0] == '=' && arg[1] == '<'
1073 && arg[2] =='<'
1074 && (checkforcmd(&p, "let", 2)
1075 || checkforcmd(&p, "var", 3)
1076 || checkforcmd(&p, "final", 5)
1077 || checkforcmd(&p, "const", 5))))
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001078 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001079 p = skipwhite(arg + 3);
1080 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001081 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001082 if (STRNCMP(p, "trim", 4) == 0)
1083 {
1084 // Ignore leading white space.
1085 p = skipwhite(p + 4);
1086 heredoc_trimmed = vim_strnsave(theline,
1087 skipwhite(theline) - theline);
1088 continue;
1089 }
1090 if (STRNCMP(p, "eval", 4) == 0)
1091 {
1092 // Ignore leading white space.
1093 p = skipwhite(p + 4);
1094 continue;
1095 }
1096 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001097 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001098 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1099 getline_options = GETLINE_NONE;
1100 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001101 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001102 }
1103 }
1104 }
1105
1106 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001107 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001108 goto theend;
1109
Bram Moolenaar20677332021-06-06 17:02:53 +02001110 if (heredoc_concat_len > 0)
1111 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001112 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001113 // to be used for the instruction later.
1114 ga_concat(&heredoc_ga, theline);
1115 ga_concat(&heredoc_ga, (char_u *)"\n");
1116 p = vim_strsave((char_u *)"");
1117 }
1118 else
1119 {
1120 // Copy the line to newly allocated memory. get_one_sourceline()
1121 // allocates 250 bytes per line, this saves 80% on average. The
1122 // cost is an extra alloc/free.
1123 p = vim_strsave(theline);
1124 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001125 if (p == NULL)
1126 goto theend;
1127 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1128
1129 // Add NULL lines for continuation lines, so that the line count is
1130 // equal to the index in the growarray.
1131 while (sourcing_lnum_off-- > 0)
1132 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1133
1134 // Check for end of eap->arg.
1135 if (line_arg != NULL && *line_arg == NUL)
1136 line_arg = NULL;
1137 }
1138
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001139 // Return OK when no error was detected.
1140 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001141 ret = OK;
1142
1143theend:
1144 vim_free(skip_until);
1145 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001146 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001147 need_wait_return |= saved_wait_return;
1148 return ret;
1149}
1150
1151/*
1152 * Handle the body of a lambda. *arg points to the "{", process statements
1153 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001154 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001155 * When successful "rettv" is set to a funcref.
1156 */
1157 static int
1158lambda_function_body(
1159 char_u **arg,
1160 typval_T *rettv,
1161 evalarg_T *evalarg,
1162 garray_T *newargs,
1163 garray_T *argtypes,
1164 int varargs,
1165 garray_T *default_args,
1166 char_u *ret_type)
1167{
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001168 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001169 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001170 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001171 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001172 exarg_T eap;
1173 garray_T newlines;
1174 char_u *cmdline = NULL;
1175 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001176 partial_T *pt;
1177 char_u *name;
1178 int lnum_save = -1;
1179 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1180
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001181 if (!ends_excmd2(*arg, skipwhite(*arg + 1)))
1182 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001183 semsg(_(e_trailing_characters_str), *arg + 1);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001184 return FAIL;
1185 }
1186
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001187 CLEAR_FIELD(eap);
1188 eap.cmdidx = CMD_block;
1189 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001190 eap.cmdlinep = &cmdline;
1191 eap.skip = !evaluate;
1192 if (evalarg->eval_cctx != NULL)
1193 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1194 else
1195 {
1196 eap.getline = evalarg->eval_getline;
1197 eap.cookie = evalarg->eval_cookie;
1198 }
1199
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001200 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001201 if (get_function_body(&eap, &newlines, NULL,
1202 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001203 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001204
1205 // When inside a lambda must add the function lines to evalarg.eval_ga.
1206 evalarg->eval_break_count += newlines.ga_len;
1207 if (gap->ga_itemsize > 0)
1208 {
1209 int idx;
1210 char_u *last;
1211 size_t plen;
1212 char_u *pnl;
1213
1214 for (idx = 0; idx < newlines.ga_len; ++idx)
1215 {
1216 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1217
Bram Moolenaarecb66452021-05-18 15:09:18 +02001218 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001219 goto erret;
1220
1221 // Going to concatenate the lines after parsing. For an empty or
1222 // comment line use an empty string.
1223 // Insert NL characters at the start of each line, the string will
1224 // be split again later in .get_lambda_tv().
1225 if (*p == NUL || vim9_comment_start(p))
1226 p = (char_u *)"";
1227 plen = STRLEN(p);
1228 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1229 if (pnl != NULL)
1230 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001231 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1232 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001233 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001234 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001235 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001236 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001237 // more is following after the "}", which was skipped
1238 last = cmdline;
1239 else
1240 // nothing is following the "}"
1241 last = (char_u *)"}";
1242 plen = STRLEN(last);
1243 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1244 if (pnl != NULL)
1245 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001246 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1247 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001248 }
1249
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001250 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001251 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001252 garray_T *tfgap = &evalarg->eval_tofree_ga;
1253
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001254 // Something comes after the "}".
1255 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001256
1257 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001258 if (ga_grow(tfgap, 1) == OK)
1259 {
1260 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1261 evalarg->eval_using_cmdline = TRUE;
1262 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001263 }
1264 else
1265 *arg = (char_u *)"";
1266
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001267 if (!evaluate)
1268 {
1269 ret = OK;
1270 goto erret;
1271 }
1272
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001273 name = get_lambda_name();
1274 ufunc = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
1275 if (ufunc == NULL)
1276 goto erret;
1277 set_ufunc_name(ufunc, name);
1278 if (hash_add(&func_hashtab, UF2HIKEY(ufunc)) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001279 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001280 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001281 ufunc->uf_refcount = 1;
1282 ufunc->uf_args = *newargs;
1283 newargs->ga_data = NULL;
1284 ufunc->uf_def_args = *default_args;
1285 default_args->ga_data = NULL;
1286 ufunc->uf_func_type = &t_func_any;
1287
1288 // error messages are for the first function line
1289 lnum_save = SOURCING_LNUM;
1290 SOURCING_LNUM = sourcing_lnum_top;
1291
1292 // parse argument types
1293 if (parse_argument_types(ufunc, argtypes, varargs) == FAIL)
1294 {
1295 SOURCING_LNUM = lnum_save;
1296 goto erret;
1297 }
1298
1299 // parse the return type, if any
1300 if (parse_return_type(ufunc, ret_type) == FAIL)
1301 goto erret;
1302
1303 pt = ALLOC_CLEAR_ONE(partial_T);
1304 if (pt == NULL)
1305 goto erret;
1306 pt->pt_func = ufunc;
1307 pt->pt_refcount = 1;
1308
1309 ufunc->uf_lines = newlines;
1310 newlines.ga_data = NULL;
1311 if (sandbox)
1312 ufunc->uf_flags |= FC_SANDBOX;
1313 if (!ASCII_ISUPPER(*ufunc->uf_name))
1314 ufunc->uf_flags |= FC_VIM9;
1315 ufunc->uf_script_ctx = current_sctx;
1316 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1317 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1318 set_function_type(ufunc);
1319
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001320 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1321
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001322 rettv->vval.v_partial = pt;
1323 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001324 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001325 ret = OK;
1326
1327erret:
1328 if (lnum_save >= 0)
1329 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001330 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001331 if (newargs != NULL)
1332 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001333 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001334 if (ufunc != NULL)
1335 {
1336 func_clear(ufunc, TRUE);
1337 func_free(ufunc, TRUE);
1338 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001339 return ret;
1340}
1341
1342/*
1343 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001344 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001345 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001346 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1347 */
1348 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001349get_lambda_tv(
1350 char_u **arg,
1351 typval_T *rettv,
1352 int types_optional,
1353 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001354{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001355 int evaluate = evalarg != NULL
1356 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001357 garray_T newargs;
1358 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001359 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001360 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001361 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001362 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001363 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001364 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001365 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001366 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001367 char_u *s;
1368 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001369 int *old_eval_lavars = eval_lavars_used;
1370 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001371 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001372 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001373 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001374 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001375 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001376 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001377
Bram Moolenaar4525a572022-02-13 11:57:33 +00001378 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001379 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001380
1381 ga_init(&newargs);
1382 ga_init(&newlines);
1383
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001384 // First, check if this is really a lambda expression. "->" or "=>" must
1385 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001386 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001387 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001388 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar14ded112021-06-26 19:25:49 +02001389 NULL, &default_args, TRUE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001390 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001391 {
1392 if (types_optional)
1393 ga_clear_strings(&argtypes);
Bram Moolenaar0346b792021-01-31 22:18:29 +01001394 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001395 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001396
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001397 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001398 if (evaluate)
1399 pnewargs = &newargs;
1400 else
1401 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001402 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001403 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001404 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001405 &varargs, &default_args,
1406 FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001407 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001408 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001409 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001410 {
1411 if (types_optional)
1412 ga_clear_strings(&argtypes);
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001413 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001414 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001415 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001416 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001417
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001418 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1419 if (ret_type != NULL)
1420 {
1421 ret_type = vim_strsave(ret_type);
1422 tofree2 = ret_type;
1423 }
1424
Bram Moolenaare38eab22019-12-05 21:50:01 +01001425 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001426 if (evaluate)
1427 eval_lavars_used = &eval_lavars;
1428
Bram Moolenaar65c44152020-12-24 15:14:01 +01001429 *arg = skipwhite_and_linebreak(*arg, evalarg);
1430
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001431 // Recognize "{" as the start of a function body.
1432 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001433 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001434 if (evalarg == NULL)
1435 // cannot happen?
1436 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001437 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001438 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1439 types_optional ? &argtypes : NULL, varargs,
1440 &default_args, ret_type) == FAIL)
1441 goto errret;
1442 goto theend;
1443 }
1444 if (default_args.ga_len > 0)
1445 {
1446 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001447 goto errret;
1448 }
1449
Bram Moolenaare38eab22019-12-05 21:50:01 +01001450 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001451 start = *arg;
1452 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001453 if (ret == FAIL)
1454 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001455
Bram Moolenaar65c44152020-12-24 15:14:01 +01001456 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001457 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001458 *arg = skipwhite_and_linebreak(*arg, evalarg);
1459 if (**arg != '}')
1460 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001461 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001462 goto errret;
1463 }
1464 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001465 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001466
1467 if (evaluate)
1468 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001469 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001470 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001471 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001472 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001473 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001474
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001475 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001476 if (fp == NULL)
1477 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001478 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001479 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001480 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001481 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001482
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001483 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001484 if (ga_grow(&newlines, 1) == FAIL)
1485 goto errret;
1486
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001487 // If there are line breaks, we need to split up the string.
1488 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001489 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001490 line_end = end;
1491
1492 // Add "return " before the expression (or the first line).
1493 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001494 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001495 if (p == NULL)
1496 goto errret;
1497 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1498 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001499 vim_strncpy(p + 7, start, line_end - start);
1500
1501 if (line_end != end)
1502 {
1503 // Add more lines, split by line breaks. Thus is used when a
1504 // lambda with { cmds } is encountered.
1505 while (*line_end == '\n')
1506 {
1507 if (ga_grow(&newlines, 1) == FAIL)
1508 goto errret;
1509 start = line_end + 1;
1510 line_end = vim_strchr(start, '\n');
1511 if (line_end == NULL)
1512 line_end = end;
1513 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1514 vim_strnsave(start, line_end - start);
1515 }
1516 }
1517
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001518 if (strstr((char *)p + 7, "a:") == NULL)
1519 // No a: variables are used for sure.
1520 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001521
1522 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001523 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001524 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001525 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001526 if (types_optional)
1527 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001528 if (parse_argument_types(fp, &argtypes,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001529 vim9script && varargs) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001530 goto errret;
1531 if (ret_type != NULL)
1532 {
1533 fp->uf_ret_type = parse_type(&ret_type,
1534 &fp->uf_type_list, TRUE);
1535 if (fp->uf_ret_type == NULL)
1536 goto errret;
1537 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001538 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001539 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001540 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001541
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001542 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001543 if (current_funccal != NULL && eval_lavars)
1544 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001545 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001546 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001547 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001548 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001549
1550#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001551 if (prof_def_func())
1552 func_do_profile(fp);
1553#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001554 if (sandbox)
1555 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001556 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001557 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001558 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001559 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001560 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001561 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001562 // Use the line number of the arguments.
1563 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001564
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001565 function_using_block_scopes(fp, evalarg->eval_cstack);
1566
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001567 pt->pt_func = fp;
1568 pt->pt_refcount = 1;
1569 rettv->vval.v_partial = pt;
1570 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001571
1572 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001573 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001574
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001575theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001576 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001577 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001578 if (types_optional)
1579 ga_clear_strings(&argtypes);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001580
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001581 return OK;
1582
1583errret:
1584 ga_clear_strings(&newargs);
1585 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001586 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001587 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001588 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001589 ga_clear_strings(&argtypes);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001590 if (fp != NULL)
1591 vim_free(fp->uf_arg_types);
1592 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001593 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001594 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001595 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/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001729 * Get function arguments at "*arg" and advance it.
1730 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001731 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001732 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001733 static int
1734get_func_arguments(
1735 char_u **arg,
1736 evalarg_T *evalarg,
1737 int partial_argc,
1738 typval_T *argvars,
1739 int *argcount)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001740{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001741 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001742 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001743 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001744 int evaluate = evalarg == NULL
1745 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001746
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001747 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001748 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001749 // skip the '(' or ',' and possibly line breaks
1750 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1751
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001752 if (*argp == ')' || *argp == ',' || *argp == NUL)
1753 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001754 if (eval1(&argp, &argvars[*argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001755 {
1756 ret = FAIL;
1757 break;
1758 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001759 ++*argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001760 // The comma should come right after the argument, but this wasn't
1761 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001762 if (vim9script)
1763 {
1764 if (*argp != ',' && *skipwhite(argp) == ',')
1765 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001766 if (evaluate)
1767 semsg(_(e_no_white_space_allowed_before_str_str),
1768 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001769 ret = FAIL;
1770 break;
1771 }
1772 }
1773 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001774 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001775 if (*argp != ',')
1776 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001777 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
1778 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001779 if (evaluate)
1780 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001781 ret = FAIL;
1782 break;
1783 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001784 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001785
Bram Moolenaare6b53242020-07-01 17:28:33 +02001786 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001787 if (*argp == ')')
1788 ++argp;
1789 else
1790 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001791 *arg = argp;
1792 return ret;
1793}
1794
1795/*
1796 * Call a function and put the result in "rettv".
1797 * Return OK or FAIL.
1798 */
1799 int
1800get_func_tv(
1801 char_u *name, // name of the function
1802 int len, // length of "name" or -1 to use strlen()
1803 typval_T *rettv,
1804 char_u **arg, // argument, pointing to the '('
1805 evalarg_T *evalarg, // for line continuation
1806 funcexe_T *funcexe) // various values
1807{
1808 char_u *argp;
1809 int ret = OK;
1810 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1811 int argcount = 0; // number of arguments found
1812 int vim9script = in_vim9script();
1813 int evaluate = evalarg == NULL
1814 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
1815
1816 argp = *arg;
1817 ret = get_func_arguments(&argp, evalarg,
1818 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
1819 argvars, &argcount);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001820
1821 if (ret == OK)
1822 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001823 int i = 0;
1824 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001825
1826 if (get_vim_var_nr(VV_TESTING))
1827 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001828 // Prepare for calling test_garbagecollect_now(), need to know
1829 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001830 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001831 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001832 for (i = 0; i < argcount; ++i)
1833 if (ga_grow(&funcargs, 1) == OK)
1834 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1835 &argvars[i];
1836 }
1837
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001838 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00001839 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001840 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001841 // An error in a builtin function does not return FAIL, but we do
1842 // want to abort further processing if an error was given.
1843 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001844 clear_tv(rettv);
1845 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001846
1847 funcargs.ga_len -= i;
1848 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001849 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001850 {
1851 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001852 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001853 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001854 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001855 }
1856
1857 while (--argcount >= 0)
1858 clear_tv(&argvars[argcount]);
1859
Bram Moolenaar4525a572022-02-13 11:57:33 +00001860 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02001861 *arg = argp;
1862 else
1863 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001864 return ret;
1865}
1866
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001867/*
1868 * Return TRUE if "p" starts with "<SID>" or "s:".
1869 * Only works if eval_fname_script() returned non-zero for "p"!
1870 */
1871 static int
1872eval_fname_sid(char_u *p)
1873{
1874 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
1875}
1876
1877/*
1878 * In a script change <SID>name() and s:name() to K_SNR 123_name().
1879 * Change <SNR>123_name() to K_SNR 123_name().
1880 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
1881 * (slow).
1882 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001883 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001884fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
1885{
1886 int llen;
1887 char_u *fname;
1888 int i;
1889
1890 llen = eval_fname_script(name);
1891 if (llen > 0)
1892 {
1893 fname_buf[0] = K_SPECIAL;
1894 fname_buf[1] = KS_EXTRA;
1895 fname_buf[2] = (int)KE_SNR;
1896 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001897 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001898 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001899 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001900 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001901 else
1902 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001903 sprintf((char *)fname_buf + 3, "%ld_",
1904 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001905 i = (int)STRLEN(fname_buf);
1906 }
1907 }
1908 if (i + STRLEN(name + llen) < FLEN_FIXED)
1909 {
1910 STRCPY(fname_buf + i, name + llen);
1911 fname = fname_buf;
1912 }
1913 else
1914 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001915 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001916 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +01001917 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001918 else
1919 {
1920 *tofree = fname;
1921 mch_memmove(fname, fname_buf, (size_t)i);
1922 STRCPY(fname + i, name + llen);
1923 }
1924 }
1925 }
1926 else
1927 fname = name;
1928 return fname;
1929}
1930
1931/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001932 * Concatenate the script ID and function name into "<SNR>99_name".
1933 * "buffer" must have size MAX_FUNC_NAME_LEN.
1934 */
1935 void
1936func_name_with_sid(char_u *name, int sid, char_u *buffer)
1937{
1938 // A script-local function is stored as "<SNR>99_name".
1939 buffer[0] = K_SPECIAL;
1940 buffer[1] = KS_EXTRA;
1941 buffer[2] = (int)KE_SNR;
1942 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
1943 (long)sid, name);
1944}
1945
1946/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947 * Find a function "name" in script "sid".
1948 */
1949 static ufunc_T *
1950find_func_with_sid(char_u *name, int sid)
1951{
Bram Moolenaarb8822442022-01-11 15:24:05 +00001952 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001953 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001954
Bram Moolenaarb8822442022-01-11 15:24:05 +00001955 if (!SCRIPT_ID_VALID(sid))
1956 return NULL; // not in a script
1957
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001958 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959 hi = hash_find(&func_hashtab, buffer);
1960 if (!HASHITEM_EMPTY(hi))
1961 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00001962 return NULL;
1963}
1964
1965/*
1966 * Find a function "name" in script "sid" prefixing the autoload prefix.
1967 */
1968 static ufunc_T *
1969find_func_with_prefix(char_u *name, int sid)
1970{
1971 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001972 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00001973 scriptitem_T *si;
1974
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001975 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00001976 return NULL; // already has the prefix
1977 if (!SCRIPT_ID_VALID(sid))
1978 return NULL; // not in a script
1979 si = SCRIPT_ITEM(sid);
1980 if (si->sn_autoload_prefix != NULL)
1981 {
1982 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
1983 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00001984 char_u *namep;
1985
1986 // skip a "<SNR>99_" prefix
1987 namep = untrans_function_name(name);
1988 if (namep == NULL)
1989 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00001990
1991 // An exported function in an autoload script is stored as
1992 // "dir#path#name".
1993 if (len < sizeof(buffer))
1994 auto_name = buffer;
1995 else
1996 auto_name = alloc(len);
1997 if (auto_name != NULL)
1998 {
1999 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002000 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002001 hi = hash_find(&func_hashtab, auto_name);
2002 if (auto_name != buffer)
2003 vim_free(auto_name);
2004 if (!HASHITEM_EMPTY(hi))
2005 return HI2UF(hi);
2006 }
2007 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008
2009 return NULL;
2010}
2011
2012/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002013 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002014 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2015 * functions.
2016 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002017 * Return NULL for unknown function.
2018 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002019 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002020find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002021{
2022 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002025 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002027 // Find script-local function before global one.
2028 if (in_vim9script() && eval_isnamec1(*name)
2029 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002030 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002031 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2032 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002033 if (func != NULL)
2034 return func;
2035 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002036 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2037 {
2038 char_u *p = name + 5;
2039 long sid;
2040
2041 // printable "<SNR>123_Name" form
2042 sid = getdigits(&p);
2043 if (*p == '_')
2044 {
2045 func = find_func_with_sid(p + 1, (int)sid);
2046 if (func != NULL)
2047 return func;
2048 }
2049 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002051
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002052 if ((flags & FFED_NO_GLOBAL) == 0)
2053 {
2054 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002055 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002056 if (!HASHITEM_EMPTY(hi))
2057 return HI2UF(hi);
2058 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002059
Bram Moolenaarb8822442022-01-11 15:24:05 +00002060 // Find autoload function if this is an autoload script.
2061 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2062 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063}
2064
2065/*
2066 * Find a function by name, return pointer to it in ufuncs.
2067 * "cctx" is passed in a :def function to find imported functions.
2068 * Return NULL for unknown or dead function.
2069 */
2070 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002071find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002073 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074
2075 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2076 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002077 return NULL;
2078}
2079
2080/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002081 * Return TRUE if "ufunc" is a global function.
2082 */
2083 int
2084func_is_global(ufunc_T *ufunc)
2085{
2086 return ufunc->uf_name[0] != K_SPECIAL;
2087}
2088
2089/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002090 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2091 */
2092 int
2093func_requires_g_prefix(ufunc_T *ufunc)
2094{
2095 return ufunc->uf_name[0] != K_SPECIAL
2096 && (ufunc->uf_flags & FC_LAMBDA) == 0
2097 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL;
2098}
2099
2100/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002101 * Copy the function name of "fp" to buffer "buf".
2102 * "buf" must be able to hold the function name plus three bytes.
2103 * Takes care of script-local function names.
2104 */
2105 static void
2106cat_func_name(char_u *buf, ufunc_T *fp)
2107{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002108 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002109 {
2110 STRCPY(buf, "<SNR>");
2111 STRCAT(buf, fp->uf_name + 3);
2112 }
2113 else
2114 STRCPY(buf, fp->uf_name);
2115}
2116
2117/*
2118 * Add a number variable "name" to dict "dp" with value "nr".
2119 */
2120 static void
2121add_nr_var(
2122 dict_T *dp,
2123 dictitem_T *v,
2124 char *name,
2125 varnumber_T nr)
2126{
2127 STRCPY(v->di_key, name);
2128 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2129 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
2130 v->di_tv.v_type = VAR_NUMBER;
2131 v->di_tv.v_lock = VAR_FIXED;
2132 v->di_tv.vval.v_number = nr;
2133}
2134
2135/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002136 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002137 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002138 static void
2139free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002140{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002141 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002142
2143 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
2144 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002145 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002146
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002147 // When garbage collecting a funccall_T may be freed before the
2148 // function that references it, clear its uf_scoped field.
2149 // The function may have been redefined and point to another
2150 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002151 if (fp != NULL && fp->uf_scoped == fc)
2152 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002153 }
Bram Moolenaar58016442016-07-31 18:30:22 +02002154 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002155
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002156 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002157 vim_free(fc);
2158}
2159
2160/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002161 * Free "fc" and what it contains.
2162 * Can be called only when "fc" is kept beyond the period of it called,
2163 * i.e. after cleanup_function_call(fc).
2164 */
2165 static void
2166free_funccal_contents(funccall_T *fc)
2167{
2168 listitem_T *li;
2169
2170 // Free all l: variables.
2171 vars_clear(&fc->l_vars.dv_hashtab);
2172
2173 // Free all a: variables.
2174 vars_clear(&fc->l_avars.dv_hashtab);
2175
2176 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002177 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002178 clear_tv(&li->li_tv);
2179
2180 free_funccal(fc);
2181}
2182
2183/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002184 * Handle the last part of returning from a function: free the local hashtable.
2185 * Unless it is still in use by a closure.
2186 */
2187 static void
2188cleanup_function_call(funccall_T *fc)
2189{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002190 int may_free_fc = fc->fc_refcount <= 0;
2191 int free_fc = TRUE;
2192
Bram Moolenaar6914c642017-04-01 21:21:30 +02002193 current_funccal = fc->caller;
2194
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002195 // Free all l: variables if not referred.
2196 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
2197 vars_clear(&fc->l_vars.dv_hashtab);
2198 else
2199 free_fc = FALSE;
2200
2201 // If the a:000 list and the l: and a: dicts are not referenced and
2202 // there is no closure using it, we can free the funccall_T and what's
2203 // in it.
2204 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
2205 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002206 else
2207 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002208 int todo;
2209 hashitem_T *hi;
2210 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002211
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002212 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002213
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002214 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +02002215 todo = (int)fc->l_avars.dv_hashtab.ht_used;
2216 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
2217 {
2218 if (!HASHITEM_EMPTY(hi))
2219 {
2220 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002221 di = HI2DI(hi);
2222 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002223 }
2224 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002225 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002226
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002227 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2228 fc->l_varlist.lv_first = NULL;
2229 else
2230 {
2231 listitem_T *li;
2232
2233 free_fc = FALSE;
2234
2235 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002236 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002237 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002238 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002239
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002240 if (free_fc)
2241 free_funccal(fc);
2242 else
2243 {
2244 static int made_copy = 0;
2245
2246 // "fc" is still in use. This can happen when returning "a:000",
2247 // assigning "l:" to a global variable or defining a closure.
2248 // Link "fc" in the list for garbage collection later.
2249 fc->caller = previous_funccal;
2250 previous_funccal = fc;
2251
2252 if (want_garbage_collect)
2253 // If garbage collector is ready, clear count.
2254 made_copy = 0;
2255 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002256 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002257 // We have made a lot of copies, worth 4 Mbyte. This can happen
2258 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002259 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002260 // too much memory.
2261 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002262 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002263 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002264 }
2265}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002266
2267/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002268 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2269 */
2270 static int
2271numbered_function(char_u *name)
2272{
2273 return isdigit(*name)
2274 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2275}
2276
2277/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002278 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002279 * 1. ordinary names, function defined with :function or :def;
2280 * can start with "<SNR>123_" literally or with K_SPECIAL.
2281 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002282 * For the first we only count the name stored in func_hashtab as a reference,
2283 * using function() does not count as a reference, because the function is
2284 * looked up by name.
2285 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002286 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002287func_name_refcount(char_u *name)
2288{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002289 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002290}
2291
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292/*
2293 * Unreference "fc": decrement the reference count and free it when it
2294 * becomes zero. "fp" is detached from "fc".
2295 * When "force" is TRUE we are exiting.
2296 */
2297 static void
2298funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2299{
2300 funccall_T **pfc;
2301 int i;
2302
2303 if (fc == NULL)
2304 return;
2305
2306 if (--fc->fc_refcount <= 0 && (force || (
2307 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
2308 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
2309 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2310 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
2311 {
2312 if (fc == *pfc)
2313 {
2314 *pfc = fc->caller;
2315 free_funccal_contents(fc);
2316 return;
2317 }
2318 }
2319 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
2320 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
2321 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
2322}
2323
2324/*
2325 * Remove the function from the function hashtable. If the function was
2326 * deleted while it still has references this was already done.
2327 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2328 */
2329 static int
2330func_remove(ufunc_T *fp)
2331{
2332 hashitem_T *hi;
2333
2334 // Return if it was already virtually deleted.
2335 if (fp->uf_flags & FC_DEAD)
2336 return FALSE;
2337
2338 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
2339 if (!HASHITEM_EMPTY(hi))
2340 {
2341 // When there is a def-function index do not actually remove the
2342 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002343 // Do remove it when it's a copy.
2344 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaarc970e422021-03-17 15:03:04 +01002345 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002346 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01002347 return FALSE;
2348 }
2349 hash_remove(&func_hashtab, hi);
2350 fp->uf_flags |= FC_DELETED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002351 return TRUE;
2352 }
2353 return FALSE;
2354}
2355
2356 static void
2357func_clear_items(ufunc_T *fp)
2358{
2359 ga_clear_strings(&(fp->uf_args));
2360 ga_clear_strings(&(fp->uf_def_args));
2361 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002363 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002364 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002365 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002366
2367 // Increment the refcount of this function to avoid it being freed
2368 // recursively when the partial is freed.
2369 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002370 partial_unref(fp->uf_partial);
2371 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002372 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002373
2374#ifdef FEAT_LUA
2375 if (fp->uf_cb_free != NULL)
2376 {
2377 fp->uf_cb_free(fp->uf_cb_state);
2378 fp->uf_cb_free = NULL;
2379 }
2380
2381 fp->uf_cb_state = NULL;
2382 fp->uf_cb = NULL;
2383#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384#ifdef FEAT_PROFILE
2385 VIM_CLEAR(fp->uf_tml_count);
2386 VIM_CLEAR(fp->uf_tml_total);
2387 VIM_CLEAR(fp->uf_tml_self);
2388#endif
2389}
2390
2391/*
2392 * Free all things that a function contains. Does not free the function
2393 * itself, use func_free() for that.
2394 * When "force" is TRUE we are exiting.
2395 */
2396 static void
2397func_clear(ufunc_T *fp, int force)
2398{
2399 if (fp->uf_cleared)
2400 return;
2401 fp->uf_cleared = TRUE;
2402
2403 // clear this function
2404 func_clear_items(fp);
2405 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002406 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407}
2408
2409/*
2410 * Free a function and remove it from the list of functions. Does not free
2411 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002412 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002413 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002414 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002415 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002416func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417{
2418 // Only remove it when not done already, otherwise we would remove a newer
2419 // version of the function with the same name.
2420 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2421 func_remove(fp);
2422
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002423 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002424 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002425 if (fp->uf_dfunc_idx > 0)
2426 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002427 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002429 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002430 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002431 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432}
2433
2434/*
2435 * Free all things that a function contains and free the function itself.
2436 * When "force" is TRUE we are exiting.
2437 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002438 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002439func_clear_free(ufunc_T *fp, int force)
2440{
2441 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002442 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2443 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002444 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002445 else
2446 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447}
2448
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002449/*
2450 * Copy already defined function "lambda" to a new function with name "global".
2451 * This is for when a compiled function defines a global function.
2452 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002453 int
2454copy_func(char_u *lambda, char_u *global, ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002455{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002456 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002457 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002458
2459 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002460 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002461 semsg(_(e_lambda_function_not_found_str), lambda);
2462 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002463 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002464
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002465 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002466 if (fp != NULL)
2467 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002468 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002469 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002470 return FAIL;
2471 }
2472
2473 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
2474 if (fp == NULL)
2475 return FAIL;
2476
2477 fp->uf_varargs = ufunc->uf_varargs;
2478 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2479 fp->uf_def_status = ufunc->uf_def_status;
2480 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2481 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2482 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2483 == FAIL
2484 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2485 goto failed;
2486
2487 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
2488 : vim_strsave(ufunc->uf_name_exp);
2489 if (ufunc->uf_arg_types != NULL)
2490 {
2491 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2492 if (fp->uf_arg_types == NULL)
2493 goto failed;
2494 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2495 sizeof(type_T *) * fp->uf_args.ga_len);
2496 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002497 if (ufunc->uf_va_name != NULL)
2498 {
2499 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2500 if (fp->uf_va_name == NULL)
2501 goto failed;
2502 }
2503 fp->uf_ret_type = ufunc->uf_ret_type;
2504
2505 fp->uf_refcount = 1;
2506 STRCPY(fp->uf_name, global);
2507 hash_add(&func_hashtab, UF2HIKEY(fp));
2508
2509 // the referenced dfunc_T is now used one more time
2510 link_def_function(fp);
2511
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002512 // Create a partial to store the context of the function where it was
2513 // instantiated. Only needs to be done once. Do this on the original
2514 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002515 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2516 {
2517 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2518
2519 if (pt == NULL)
2520 goto failed;
2521 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002522 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002523 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002524 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002525 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002526 ufunc->uf_partial = pt;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002527 --pt->pt_refcount; // not actually referenced here
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002528 }
2529
2530 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002531
2532failed:
2533 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002534 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002535}
2536
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002537static int funcdepth = 0;
2538
2539/*
2540 * Increment the function call depth count.
2541 * Return FAIL when going over 'maxfuncdepth'.
2542 * Otherwise return OK, must call funcdepth_decrement() later!
2543 */
2544 int
2545funcdepth_increment(void)
2546{
2547 if (funcdepth >= p_mfd)
2548 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002549 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002550 return FAIL;
2551 }
2552 ++funcdepth;
2553 return OK;
2554}
2555
2556 void
2557funcdepth_decrement(void)
2558{
2559 --funcdepth;
2560}
2561
2562/*
2563 * Get the current function call depth.
2564 */
2565 int
2566funcdepth_get(void)
2567{
2568 return funcdepth;
2569}
2570
2571/*
2572 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002573 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002574 * times as funcdepth_increment().
2575 */
2576 void
2577funcdepth_restore(int depth)
2578{
2579 funcdepth = depth;
2580}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002581
2582/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002583 * Call a user function.
2584 */
2585 static void
2586call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002587 ufunc_T *fp, // pointer to function
2588 int argcount, // nr of args
2589 typval_T *argvars, // arguments
2590 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002591 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002592 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002593{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002594 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002595 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002596 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002597 funccall_T *fc;
2598 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002599 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002600 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002601 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002602 int i;
2603 int ai;
2604 int islambda = FALSE;
2605 char_u numbuf[NUMBUFLEN];
2606 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002607 typval_T *tv_to_free[MAX_FUNC_ARGS];
2608 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002609#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002610 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002611#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01002612 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002613
Bram Moolenaarb2049902021-01-24 12:53:53 +01002614#ifdef FEAT_PROFILE
2615 CLEAR_FIELD(profile_info);
2616#endif
2617
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002618 // If depth of calling is getting too high, don't execute the function.
2619 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002620 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002621 rettv->v_type = VAR_NUMBER;
2622 rettv->vval.v_number = -1;
2623 return;
2624 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002625
Bram Moolenaare38eab22019-12-05 21:50:01 +01002626 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002627
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002628 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002629 if (fc == NULL)
2630 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002631 fc->caller = current_funccal;
2632 current_funccal = fc;
2633 fc->func = fp;
2634 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002635 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002636 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002637 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2638 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002639 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002640 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002641 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002642
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002643 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002645#ifdef FEAT_PROFILE
2646 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2647#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002648 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002649#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002650 if (do_profiling == PROF_YES)
2651 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002652#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002653 sticky_cmdmod_flags = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002654 call_def_function(fp, argcount, argvars, funcexe->fe_partial, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002655 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002656#ifdef FEAT_PROFILE
2657 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002658 || (caller != NULL && caller->uf_profiling)))
2659 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002660#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 current_funccal = fc->caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 free_funccal(fc);
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002663 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664 return;
2665 }
2666
Bram Moolenaar38453522021-11-28 22:00:12 +00002667 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002668
2669 /*
2670 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
2671 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
2672 * each argument variable and saves a lot of time.
2673 */
2674 /*
2675 * Init l: variables.
2676 */
2677 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
2678 if (selfdict != NULL)
2679 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002680 // Set l:self to "selfdict". Use "name" to avoid a warning from
2681 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002682 v = &fc->fixvar[fixvar_idx++].var;
2683 name = v->di_key;
2684 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002685 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002686 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
2687 v->di_tv.v_type = VAR_DICT;
2688 v->di_tv.v_lock = 0;
2689 v->di_tv.vval.v_dict = selfdict;
2690 ++selfdict->dv_refcount;
2691 }
2692
2693 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002694 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002695 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002696 * Set a:000 to a list with room for the "..." arguments.
2697 */
2698 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002699 if ((fp->uf_flags & FC_NOARGS) == 0)
2700 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002701 (varnumber_T)(argcount >= fp->uf_args.ga_len
2702 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01002703 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002704 if ((fp->uf_flags & FC_NOARGS) == 0)
2705 {
2706 // Use "name" to avoid a warning from some compiler that checks the
2707 // destination size.
2708 v = &fc->fixvar[fixvar_idx++].var;
2709 name = v->di_key;
2710 STRCPY(name, "000");
2711 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2712 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
2713 v->di_tv.v_type = VAR_LIST;
2714 v->di_tv.v_lock = VAR_FIXED;
2715 v->di_tv.vval.v_list = &fc->l_varlist;
2716 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02002717 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002718 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2719 fc->l_varlist.lv_lock = VAR_FIXED;
2720
2721 /*
2722 * Set a:firstline to "firstline" and a:lastline to "lastline".
2723 * Set a:name to named arguments.
2724 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002725 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002726 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002727 if ((fp->uf_flags & FC_NOARGS) == 0)
2728 {
2729 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002730 (varnumber_T)funcexe->fe_firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002731 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002732 (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002733 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002734 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002735 {
2736 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002737 typval_T def_rettv;
2738 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002739
2740 ai = i - fp->uf_args.ga_len;
2741 if (ai < 0)
2742 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002743 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002744 name = FUNCARG(fp, i);
2745 if (islambda)
2746 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002747
2748 // evaluate named argument default expression
2749 isdefault = ai + fp->uf_def_args.ga_len >= 0
2750 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2751 && argvars[i].vval.v_number == VVAL_NONE));
2752 if (isdefault)
2753 {
2754 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002755
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002756 def_rettv.v_type = VAR_NUMBER;
2757 def_rettv.vval.v_number = -1;
2758
2759 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2760 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002761 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002762 {
2763 default_arg_err = 1;
2764 break;
2765 }
2766 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002767 }
2768 else
2769 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002770 if ((fp->uf_flags & FC_NOARGS) != 0)
2771 // Bail out if no a: arguments used (in lambda).
2772 break;
2773
Bram Moolenaare38eab22019-12-05 21:50:01 +01002774 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002775 sprintf((char *)numbuf, "%d", ai + 1);
2776 name = numbuf;
2777 }
2778 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2779 {
2780 v = &fc->fixvar[fixvar_idx++].var;
2781 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002782 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002783 }
2784 else
2785 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002786 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002787 if (v == NULL)
2788 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002789 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002790 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002791
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002792 // Note: the values are copied directly to avoid alloc/free.
2793 // "argvars" must have VAR_FIXED for v_lock.
2794 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002795 v->di_tv.v_lock = VAR_FIXED;
2796
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002797 if (isdefault)
2798 // Need to free this later, no matter where it's stored.
2799 tv_to_free[tv_to_free_len++] = &v->di_tv;
2800
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002801 if (addlocal)
2802 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002803 // Named arguments should be accessed without the "a:" prefix in
2804 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002805 copy_tv(&v->di_tv, &v->di_tv);
2806 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002807 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002808 else
2809 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002810
2811 if (ai >= 0 && ai < MAX_FUNC_ARGS)
2812 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002813 listitem_T *li = &fc->l_listitems[ai];
2814
2815 li->li_tv = argvars[i];
2816 li->li_tv.v_lock = VAR_FIXED;
2817 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002818 }
2819 }
2820
Bram Moolenaare38eab22019-12-05 21:50:01 +01002821 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002822 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02002823
2824 if (fp->uf_flags & FC_SANDBOX)
2825 {
2826 using_sandbox = TRUE;
2827 ++sandbox;
2828 }
2829
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002830 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002831 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002832 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002833 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002834 ++no_wait_return;
2835 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002836
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002837 smsg(_("calling %s"), SOURCING_NAME);
2838 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002839 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002840 char_u buf[MSG_BUF_LEN];
2841 char_u numbuf2[NUMBUFLEN];
2842 char_u *tofree;
2843 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002844
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002845 msg_puts("(");
2846 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002847 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002848 if (i > 0)
2849 msg_puts(", ");
2850 if (argvars[i].v_type == VAR_NUMBER)
2851 msg_outnum((long)argvars[i].vval.v_number);
2852 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002853 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002854 // Do not want errors such as E724 here.
2855 ++emsg_off;
2856 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
2857 --emsg_off;
2858 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002859 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002860 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002861 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002862 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2863 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002864 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002865 msg_puts((char *)s);
2866 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002867 }
2868 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002869 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002870 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002871 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002872 msg_puts("\n"); // don't overwrite this either
2873
2874 verbose_leave_scroll();
2875 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002876 }
2877#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002878 if (do_profiling == PROF_YES)
2879 profile_may_start_func(&profile_info, fp,
2880 fc->caller == NULL ? NULL : fc->caller->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002881#endif
2882
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002883 // "legacy" does not apply to commands in the function
2884 sticky_cmdmod_flags = 0;
2885
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002886 save_current_sctx = current_sctx;
2887 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002888 save_did_emsg = did_emsg;
2889 did_emsg = FALSE;
2890
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002891 if (default_arg_err && (fp->uf_flags & FC_ABORT))
2892 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002893 else if (islambda)
2894 {
2895 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
2896
2897 // A Lambda always has the command "return {expr}". It is much faster
2898 // to evaluate {expr} directly.
2899 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002900 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002901 --ex_nesting_level;
2902 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002903 else
2904 // call do_cmdline() to execute the lines
2905 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002906 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
2907
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002908 // Invoke functions added with ":defer".
2909 handle_defer();
2910
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002911 --RedrawingDisabled;
2912
Bram Moolenaare38eab22019-12-05 21:50:01 +01002913 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002914 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
2915 {
2916 clear_tv(rettv);
2917 rettv->v_type = VAR_NUMBER;
2918 rettv->vval.v_number = -1;
2919 }
2920
2921#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002922 if (do_profiling == PROF_YES)
2923 {
2924 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2925
2926 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
2927 profile_may_end_func(&profile_info, fp, caller);
2928 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002929#endif
2930
Bram Moolenaare38eab22019-12-05 21:50:01 +01002931 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002932 if (p_verbose >= 12)
2933 {
2934 ++no_wait_return;
2935 verbose_enter_scroll();
2936
2937 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002938 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002939 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002940 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002941 (long)fc->rettv->vval.v_number);
2942 else
2943 {
2944 char_u buf[MSG_BUF_LEN];
2945 char_u numbuf2[NUMBUFLEN];
2946 char_u *tofree;
2947 char_u *s;
2948
Bram Moolenaare38eab22019-12-05 21:50:01 +01002949 // The value may be very long. Skip the middle part, so that we
2950 // have some idea how it starts and ends. smsg() would always
2951 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002952 ++emsg_off;
2953 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
2954 --emsg_off;
2955 if (s != NULL)
2956 {
2957 if (vim_strsize(s) > MSG_BUF_CLEN)
2958 {
2959 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2960 s = buf;
2961 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002962 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002963 vim_free(tofree);
2964 }
2965 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002966 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002967
2968 verbose_leave_scroll();
2969 --no_wait_return;
2970 }
2971
Bram Moolenaare31ee862020-01-07 20:59:34 +01002972 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002973 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002974 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002975#ifdef FEAT_PROFILE
2976 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002977 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002978#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02002979 if (using_sandbox)
2980 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002981 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002982
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002983 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002984 {
2985 ++no_wait_return;
2986 verbose_enter_scroll();
2987
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002988 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002989 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002990
2991 verbose_leave_scroll();
2992 --no_wait_return;
2993 }
2994
2995 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002996 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002997 for (i = 0; i < tv_to_free_len; ++i)
2998 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002999 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003000}
3001
3002/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003003 * Check the argument count for user function "fp".
3004 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3005 */
3006 int
3007check_user_func_argcount(ufunc_T *fp, int argcount)
3008{
3009 int regular_args = fp->uf_args.ga_len;
3010
3011 if (argcount < regular_args - fp->uf_def_args.ga_len)
3012 return FCERR_TOOFEW;
3013 else if (!has_varargs(fp) && argcount > regular_args)
3014 return FCERR_TOOMANY;
3015 return FCERR_UNKNOWN;
3016}
3017
3018/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003020 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 int
3022call_user_func_check(
3023 ufunc_T *fp,
3024 int argcount,
3025 typval_T *argvars,
3026 typval_T *rettv,
3027 funcexe_T *funcexe,
3028 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003029{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003031
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003032#ifdef FEAT_LUA
3033 if (fp->uf_flags & FC_CFUNC)
3034 {
3035 cfunc_T cb = fp->uf_cb;
3036
3037 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3038 }
3039#endif
3040
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003041 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3042 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003043 error = check_user_func_argcount(fp, argcount);
3044 if (error != FCERR_UNKNOWN)
3045 return error;
3046 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 error = FCERR_DICT;
3048 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003049 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 int did_save_redo = FALSE;
3051 save_redo_T save_redo;
3052
3053 /*
3054 * Call the user function.
3055 * Save and restore search patterns, script variables and
3056 * redo buffer.
3057 */
3058 save_search_patterns();
3059 if (!ins_compl_active())
3060 {
3061 saveRedobuff(&save_redo);
3062 did_save_redo = TRUE;
3063 }
3064 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003065 call_user_func(fp, argcount, argvars, rettv, funcexe,
3066 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3068 // Function was unreferenced while being used, free it now.
3069 func_clear_free(fp, FALSE);
3070 if (did_save_redo)
3071 restoreRedobuff(&save_redo);
3072 restore_search_patterns();
3073 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003074 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003076}
3077
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003078static funccal_entry_T *funccal_stack = NULL;
3079
3080/*
3081 * Save the current function call pointer, and set it to NULL.
3082 * Used when executing autocommands and for ":source".
3083 */
3084 void
3085save_funccal(funccal_entry_T *entry)
3086{
3087 entry->top_funccal = current_funccal;
3088 entry->next = funccal_stack;
3089 funccal_stack = entry;
3090 current_funccal = NULL;
3091}
3092
3093 void
3094restore_funccal(void)
3095{
3096 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003097 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003098 else
3099 {
3100 current_funccal = funccal_stack->top_funccal;
3101 funccal_stack = funccal_stack->next;
3102 }
3103}
3104
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003105 funccall_T *
3106get_current_funccal(void)
3107{
3108 return current_funccal;
3109}
3110
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003111/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003112 * Return TRUE when currently at the script level:
3113 * - not in a function
3114 * - not executing an autocommand
3115 * Note that when an autocommand sources a script the result is FALSE;
3116 */
3117 int
3118at_script_level(void)
3119{
3120 return current_funccal == NULL && autocmd_match == NULL;
3121}
3122
3123/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003124 * Mark all functions of script "sid" as deleted.
3125 */
3126 void
3127delete_script_functions(int sid)
3128{
3129 hashitem_T *hi;
3130 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003131 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003132 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003133 size_t len;
3134
3135 buf[0] = K_SPECIAL;
3136 buf[1] = KS_EXTRA;
3137 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003138 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003139 len = STRLEN(buf);
3140
Bram Moolenaarce658352020-07-31 23:47:12 +02003141 while (todo > 0)
3142 {
3143 todo = func_hashtab.ht_used;
3144 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3145 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003146 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003147 fp = HI2UF(hi);
3148 if (STRNCMP(fp->uf_name, buf, len) == 0)
3149 {
3150 int changed = func_hashtab.ht_changed;
3151
3152 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003153
3154 if (fp->uf_calls > 0)
3155 {
3156 // Function is executing, don't free it but do remove
3157 // it from the hashtable.
3158 if (func_remove(fp))
3159 fp->uf_refcount--;
3160 }
3161 else
3162 {
3163 func_clear(fp, TRUE);
3164 // When clearing a function another function can be
3165 // cleared as a side effect. When that happens start
3166 // over.
3167 if (changed != func_hashtab.ht_changed)
3168 break;
3169 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003170 }
3171 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003172 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003173 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003174}
3175
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003176#if defined(EXITFREE) || defined(PROTO)
3177 void
3178free_all_functions(void)
3179{
3180 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003181 ufunc_T *fp;
3182 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003183 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003184 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003185
Bram Moolenaare38eab22019-12-05 21:50:01 +01003186 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003187 while (current_funccal != NULL)
3188 {
3189 clear_tv(current_funccal->rettv);
3190 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003191 if (current_funccal == NULL && funccal_stack != NULL)
3192 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003193 }
3194
Bram Moolenaare38eab22019-12-05 21:50:01 +01003195 // First clear what the functions contain. Since this may lower the
3196 // reference count of a function, it may also free a function and change
3197 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003198 while (todo > 0)
3199 {
3200 todo = func_hashtab.ht_used;
3201 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3202 if (!HASHITEM_EMPTY(hi))
3203 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 // clear the def function index now
3205 fp = HI2UF(hi);
3206 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003207 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208
Bram Moolenaare38eab22019-12-05 21:50:01 +01003209 // Only free functions that are not refcounted, those are
3210 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003211 if (func_name_refcount(fp->uf_name))
3212 ++skipped;
3213 else
3214 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003215 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003216 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003217 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003218 {
3219 skipped = 0;
3220 break;
3221 }
3222 }
3223 --todo;
3224 }
3225 }
3226
Bram Moolenaare38eab22019-12-05 21:50:01 +01003227 // Now actually free the functions. Need to start all over every time,
3228 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003229 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003230 while (func_hashtab.ht_used > skipped)
3231 {
3232 todo = func_hashtab.ht_used;
3233 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003234 if (!HASHITEM_EMPTY(hi))
3235 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003236 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003237 // Only free functions that are not refcounted, those are
3238 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003239 fp = HI2UF(hi);
3240 if (func_name_refcount(fp->uf_name))
3241 ++skipped;
3242 else
3243 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003244 if (func_free(fp, FALSE) == OK)
3245 {
3246 skipped = 0;
3247 break;
3248 }
3249 // did not actually free it
3250 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003251 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003252 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003253 }
3254 if (skipped == 0)
3255 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256
3257 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003258}
3259#endif
3260
3261/*
3262 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003263 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3264 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003265 * "len" is the length of "name", or -1 for NUL terminated.
3266 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003268builtin_function(char_u *name, int len)
3269{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003270 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003271
Bram Moolenaara26b9702020-04-18 19:53:28 +02003272 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003273 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003274 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3275 {
3276 if (name[i] == AUTOLOAD_CHAR)
3277 return FALSE;
3278 if (!eval_isnamec(name[i]))
3279 {
3280 // "name.something" is not a builtin function
3281 if (name[i] == '.')
3282 return FALSE;
3283 break;
3284 }
3285 }
3286 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003287}
3288
3289 int
3290func_call(
3291 char_u *name,
3292 typval_T *args,
3293 partial_T *partial,
3294 dict_T *selfdict,
3295 typval_T *rettv)
3296{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003297 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003298 listitem_T *item;
3299 typval_T argv[MAX_FUNC_ARGS + 1];
3300 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003301 int r = 0;
3302
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003303 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003304 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003305 {
3306 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3307 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003308 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003309 break;
3310 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003311 // Make a copy of each argument. This is needed to be able to set
3312 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003313 copy_tv(&item->li_tv, &argv[argc++]);
3314 }
3315
3316 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003317 {
3318 funcexe_T funcexe;
3319
Bram Moolenaara80faa82020-04-12 19:37:17 +02003320 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003321 funcexe.fe_firstline = curwin->w_cursor.lnum;
3322 funcexe.fe_lastline = curwin->w_cursor.lnum;
3323 funcexe.fe_evaluate = TRUE;
3324 funcexe.fe_partial = partial;
3325 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003326 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3327 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003328
Bram Moolenaare38eab22019-12-05 21:50:01 +01003329 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003330 while (argc > 0)
3331 clear_tv(&argv[--argc]);
3332
3333 return r;
3334}
3335
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003336static int callback_depth = 0;
3337
3338 int
3339get_callback_depth(void)
3340{
3341 return callback_depth;
3342}
3343
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003344/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003345 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003346 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003347 */
3348 int
3349call_callback(
3350 callback_T *callback,
3351 int len, // length of "name" or -1 to use strlen()
3352 typval_T *rettv, // return value goes here
3353 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003354 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003355 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003356{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003357 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003358 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003359
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003360 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3361 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02003362 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003363 funcexe.fe_evaluate = TRUE;
3364 funcexe.fe_partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003365 ++callback_depth;
3366 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3367 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003368
3369 // When a :def function was called that uses :try an error would be turned
3370 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003371 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003372 {
3373 need_rethrow = FALSE;
3374 handle_did_throw();
3375 }
3376
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003377 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003378}
3379
3380/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003381 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003382 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003383 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3384 */
3385 varnumber_T
3386call_callback_retnr(
3387 callback_T *callback,
3388 int argcount, // number of "argvars"
3389 typval_T *argvars) // vars for arguments, must have "argcount"
3390 // PLUS ONE elements!
3391{
3392 typval_T rettv;
3393 varnumber_T retval;
3394
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003395 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003396 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003397
3398 retval = tv_get_number_chk(&rettv, NULL);
3399 clear_tv(&rettv);
3400 return retval;
3401}
3402
3403/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404 * Give an error message for the result of a function.
3405 * Nothing if "error" is FCERR_NONE.
3406 */
3407 void
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003408user_func_error(int error, char_u *name, funcexe_T *funcexe)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409{
3410 switch (error)
3411 {
3412 case FCERR_UNKNOWN:
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003413 if (funcexe->fe_found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003414 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003415 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003416 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 break;
3418 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003419 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 break;
3421 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003422 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 break;
3424 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003425 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 break;
3427 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003428 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 break;
3430 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003431 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 break;
3433 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003434 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3435 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 break;
3437 }
3438}
3439
3440/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003441 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003442 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003443 * Return FAIL when the function can't be called, OK otherwise.
3444 * Also returns OK when an error was encountered while executing the function.
3445 */
3446 int
3447call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003448 char_u *funcname, // name of the function
3449 int len, // length of "name" or -1 to use strlen()
3450 typval_T *rettv, // return value goes here
3451 int argcount_in, // number of "argvars"
3452 typval_T *argvars_in, // vars for arguments, must have "argcount"
3453 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003454 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003455{
3456 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01003457 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003458 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003459 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003460 char_u fname_buf[FLEN_FIXED + 1];
3461 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003462 char_u *fname = NULL;
3463 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003464 int argcount = argcount_in;
3465 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003466 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003467 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003468 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003469 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003470 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003471 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003472 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003473 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003474
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003475 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3476 // even when call_func() returns FAIL.
3477 rettv->v_type = VAR_UNKNOWN;
3478
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003479 if (partial != NULL)
3480 fp = partial->pt_func;
3481 if (fp == NULL)
3482 {
3483 // Make a copy of the name, if it comes from a funcref variable it
3484 // could be changed or deleted in the called function.
3485 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3486 if (name == NULL)
3487 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003488
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003489 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3490 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003491
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003492 if (funcexe->fe_doesrange != NULL)
3493 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003494
3495 if (partial != NULL)
3496 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003497 // When the function has a partial with a dict and there is a dict
3498 // argument, use the dict argument. That is backwards compatible.
3499 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003500 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003501 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003502 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003503 {
3504 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003505 {
3506 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3507 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003508 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003509 goto theend;
3510 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003511 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003512 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003513 for (i = 0; i < argcount_in; ++i)
3514 argv[i + argv_clear] = argvars_in[i];
3515 argvars = argv;
3516 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003517
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003518 if (funcexe->fe_check_type != NULL
3519 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003520 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003521 // Now funcexe->fe_check_type is missing the added arguments,
3522 // make a copy of the type with the correction.
3523 check_type = *funcexe->fe_check_type;
3524 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003525 check_type.tt_args = check_type_args;
3526 CLEAR_FIELD(check_type_args);
3527 for (i = 0; i < check_type.tt_argcount; ++i)
3528 check_type_args[i + partial->pt_argc] =
3529 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003530 check_type.tt_argcount += partial->pt_argc;
3531 check_type.tt_min_argcount += partial->pt_argc;
3532 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003533 }
3534 }
3535
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003536 if (error == FCERR_NONE && funcexe->fe_check_type != NULL
3537 && funcexe->fe_evaluate)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003538 {
3539 // Check that the argument types are OK for the types of the funcref.
Bram Moolenaarc84287d2022-01-16 18:06:21 +00003540 if (check_argument_types(funcexe->fe_check_type,
3541 argvars, argcount, funcexe->fe_basetv,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003542 (name != NULL) ? name : funcname) == FAIL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003543 error = FCERR_OTHER;
3544 }
3545
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003546 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003547 {
3548 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003549 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003550
Bram Moolenaar333894b2020-08-01 18:53:07 +02003551 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003552 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003553 {
3554 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003555 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003556 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003557
Bram Moolenaare38eab22019-12-05 21:50:01 +01003558 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003559 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003560 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003561
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003562 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003563 {
3564 /*
3565 * User defined function.
3566 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003567 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003568 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003569 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003570 if (fp != NULL && !is_global && in_vim9script()
3571 && func_requires_g_prefix(fp))
3572 // In Vim9 script g: is required to find a global
3573 // non-autoload function.
3574 fp = NULL;
3575 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003576
Bram Moolenaare38eab22019-12-05 21:50:01 +01003577 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003578 if (fp == NULL
3579 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003580 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003581 && !aborting())
3582 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003583 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003584 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003585 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003586 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003587 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3588 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003589 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003590 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003591 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003592 if (fp == NULL)
3593 {
3594 char_u *p = untrans_function_name(rfname);
3595
3596 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003597 // Don't do this if the name starts with "s:".
3598 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003599 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003600 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003601
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003602 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003603 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003604 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003605 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003606 if (funcexe->fe_argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003607 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003608 argcount = funcexe->fe_argv_func(argcount, argvars,
3609 argv_clear, fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003610
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003611 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003612 {
3613 // Method call: base->Method()
3614 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003615 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003616 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003617 argvars = argv;
3618 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003619 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003620
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 error = call_user_func_check(fp, argcount, argvars, rettv,
3622 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003623 }
3624 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003625 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003626 {
3627 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003628 * expr->method(): Find the method name in the table, call its
3629 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003630 */
3631 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003632 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003633 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003634 else
3635 {
3636 /*
3637 * Find the function name in the table, call its implementation.
3638 */
3639 error = call_internal_func(fname, argcount, argvars, rettv);
3640 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003641
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003642 /*
3643 * The function call (or "FuncUndefined" autocommand sequence) might
3644 * have been aborted by an error, an interrupt, or an explicitly thrown
3645 * exception that has not been caught so far. This situation can be
3646 * tested for by calling aborting(). For an error in an internal
3647 * function or for the "E132" error in call_user_func(), however, the
3648 * throw point at which the "force_abort" flag (temporarily reset by
3649 * emsg()) is normally updated has not been reached yet. We need to
3650 * update that flag first to make aborting() reliable.
3651 */
3652 update_force_abort();
3653 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003654 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003655 ret = OK;
3656
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003657theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003658 /*
3659 * Report an error unless the argument evaluation or function call has been
3660 * cancelled due to an aborting error, an interrupt, or an exception.
3661 */
3662 if (!aborting())
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003663 user_func_error(error, (name != NULL) ? name : funcname, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003664
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003665 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003666 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003667 clear_tv(&argv[--argv_clear + argv_base]);
3668
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003669 vim_free(tofree);
3670 vim_free(name);
3671
3672 return ret;
3673}
3674
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003675 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676printable_func_name(ufunc_T *fp)
3677{
3678 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
3679}
3680
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003681/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003682 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003683 */
3684 static void
3685list_func_head(ufunc_T *fp, int indent)
3686{
3687 int j;
3688
3689 msg_start();
3690 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003691 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003692 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003693 msg_puts("def ");
3694 else
3695 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003697 msg_putchar('(');
3698 for (j = 0; j < fp->uf_args.ga_len; ++j)
3699 {
3700 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003701 msg_puts(", ");
3702 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 if (fp->uf_arg_types != NULL)
3704 {
3705 char *tofree;
3706
3707 msg_puts(": ");
3708 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
3709 vim_free(tofree);
3710 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003711 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
3712 {
3713 msg_puts(" = ");
3714 msg_puts(((char **)(fp->uf_def_args.ga_data))
3715 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
3716 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003717 }
3718 if (fp->uf_varargs)
3719 {
3720 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003721 msg_puts(", ");
3722 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003723 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724 if (fp->uf_va_name != NULL)
3725 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01003726 if (!fp->uf_varargs)
3727 {
3728 if (j)
3729 msg_puts(", ");
3730 msg_puts("...");
3731 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02003733 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 {
3735 char *tofree;
3736
3737 msg_puts(": ");
3738 msg_puts(type_name(fp->uf_va_type, &tofree));
3739 vim_free(tofree);
3740 }
3741 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003742 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003743
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003744 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003745 {
3746 if (fp->uf_ret_type != &t_void)
3747 {
3748 char *tofree;
3749
3750 msg_puts(": ");
3751 msg_puts(type_name(fp->uf_ret_type, &tofree));
3752 vim_free(tofree);
3753 }
3754 }
3755 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003756 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003757 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003758 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003759 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003760 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003761 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003762 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003763 msg_clr_eos();
3764 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003765 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003766}
3767
3768/*
3769 * Get a function name, translating "<SID>" and "<SNR>".
3770 * Also handles a Funcref in a List or Dictionary.
3771 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003772 * Set "*is_global" to TRUE when the function must be global, unless
3773 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003774 * flags:
3775 * TFN_INT: internal function name OK
3776 * TFN_QUIET: be quiet
3777 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003778 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003779 * Advances "pp" to just after the function name (if no error).
3780 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003781 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003782trans_function_name(
3783 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003784 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003785 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003786 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003787 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003788 partial_T **partial, // return: partial of a FuncRef
3789 type_T **type) // return: type of funcref if not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003790{
3791 char_u *name = NULL;
3792 char_u *start;
3793 char_u *end;
3794 int lead;
3795 char_u sid_buf[20];
3796 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003798 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003799 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003800 int vim9script = in_vim9script();
3801 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003802
3803 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003804 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003805 start = *pp;
3806
Bram Moolenaare38eab22019-12-05 21:50:01 +01003807 // Check for hard coded <SNR>: already translated function ID (from a user
3808 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003809 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
3810 && (*pp)[2] == (int)KE_SNR)
3811 {
3812 *pp += 3;
3813 len = get_id_len(pp) + 3;
3814 return vim_strnsave(start, len);
3815 }
3816
Bram Moolenaare38eab22019-12-05 21:50:01 +01003817 // A name starting with "<SID>" or "<SNR>" is local to a script. But
3818 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003819 lead = eval_fname_script(start);
3820 if (lead > 2)
3821 start += lead;
3822
Bram Moolenaare38eab22019-12-05 21:50:01 +01003823 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01003824 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003825 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003826 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00003827 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003828 {
3829 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00003830 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003831 goto theend;
3832 }
3833 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
3834 {
3835 /*
3836 * Report an invalid expression in braces, unless the expression
3837 * evaluation has been cancelled due to an aborting error, an
3838 * interrupt, or an exception.
3839 */
3840 if (!aborting())
3841 {
3842 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003843 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003844 }
3845 else
3846 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
3847 goto theend;
3848 }
3849
3850 if (lv.ll_tv != NULL)
3851 {
3852 if (fdp != NULL)
3853 {
3854 fdp->fd_dict = lv.ll_dict;
3855 fdp->fd_newkey = lv.ll_newkey;
3856 lv.ll_newkey = NULL;
3857 fdp->fd_di = lv.ll_di;
3858 }
3859 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
3860 {
3861 name = vim_strsave(lv.ll_tv->vval.v_string);
3862 *pp = end;
3863 }
3864 else if (lv.ll_tv->v_type == VAR_PARTIAL
3865 && lv.ll_tv->vval.v_partial != NULL)
3866 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003867 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003868 *pp = end;
3869 if (partial != NULL)
3870 *partial = lv.ll_tv->vval.v_partial;
3871 }
3872 else
3873 {
3874 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
3875 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00003876 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003877 else
3878 *pp = end;
3879 name = NULL;
3880 }
3881 goto theend;
3882 }
3883
3884 if (lv.ll_name == NULL)
3885 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003886 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003887 *pp = end;
3888 goto theend;
3889 }
3890
Bram Moolenaare38eab22019-12-05 21:50:01 +01003891 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003892 if (lv.ll_exp_name != NULL)
3893 {
3894 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003895 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00003896 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003897 if (name == lv.ll_exp_name)
3898 name = NULL;
3899 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00003900 else if (lv.ll_sid > 0)
3901 {
3902 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
3903 int cc = *lv.ll_name_end;
3904
3905 // function in another script. Prefix <SNR>99_ or the autoload prefix.
3906 *lv.ll_name_end = NUL;
3907 if (si->sn_autoload_prefix != NULL)
3908 {
3909 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
3910 }
3911 else
3912 {
3913 sid_buf[0] = K_SPECIAL;
3914 sid_buf[1] = KS_EXTRA;
3915 sid_buf[2] = (int)KE_SNR;
3916 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00003917 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00003918 name = concat_str(sid_buf, lv.ll_name);
3919 }
3920 *lv.ll_name_end = cc;
3921 *pp = end;
3922 goto theend;
3923 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003924 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003925 {
3926 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003927 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00003928 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003929 if (name == *pp)
3930 name = NULL;
3931 }
3932 if (name != NULL)
3933 {
3934 name = vim_strsave(name);
3935 *pp = end;
3936 if (STRNCMP(name, "<SNR>", 5) == 0)
3937 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003938 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003939 name[0] = K_SPECIAL;
3940 name[1] = KS_EXTRA;
3941 name[2] = (int)KE_SNR;
3942 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
3943 }
3944 goto theend;
3945 }
3946
3947 if (lv.ll_exp_name != NULL)
3948 {
3949 len = (int)STRLEN(lv.ll_exp_name);
3950 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
3951 && STRNCMP(lv.ll_name, "s:", 2) == 0)
3952 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003953 // When there was "s:" already or the name expanded to get a
3954 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003955 lv.ll_name += 2;
3956 len -= 2;
3957 lead = 2;
3958 }
3959 }
3960 else
3961 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003962 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003963 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003964 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003965 if (lv.ll_name[0] == 'g')
3966 {
3967 if (is_global != NULL)
3968 {
3969 *is_global = TRUE;
3970 }
3971 else
3972 {
3973 // dropping "g:" without setting "is_global" won't work in
3974 // Vim9script, put it back later
3975 prefix_g = TRUE;
3976 extra = 2;
3977 }
3978 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003979 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003980 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003981 len = (int)(end - lv.ll_name);
3982 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003983 if (len <= 0)
3984 {
3985 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00003986 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003987 goto theend;
3988 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003989
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003990 // In Vim9 script a user function is script-local by default, unless it
3991 // starts with a lower case character: dict.func().
Bram Moolenaar4525a572022-02-13 11:57:33 +00003992 vim9_local = ASCII_ISUPPER(*start) && vim9script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003994 /*
3995 * Copy the function name to allocated memory.
3996 * Accept <SID>name() inside a script, translate into <SNR>123_name().
3997 * Accept <SNR>123_name() outside a script.
3998 */
3999 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004000 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004001 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004002 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004003 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004004 {
Kota Kato948a3892022-08-16 16:09:59 +01004005 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4006 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004007 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004008 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004009 goto theend;
4010 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004012 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004013 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004015 || eval_fname_sid(*pp))
4016 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004018 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004019 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004020 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004021 goto theend;
4022 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004023 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004024 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 extra = 3 + (int)STRLEN(sid_buf);
4026 else
4027 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004028 }
4029 }
Bram Moolenaar22f17a22021-06-21 20:48:58 +02004030 else if (!(flags & TFN_INT) && (builtin_function(lv.ll_name, len)
Bram Moolenaar4525a572022-02-13 11:57:33 +00004031 || (vim9script && *lv.ll_name == '_')))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004032 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004033 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004034 : e_function_name_must_start_with_capital_or_s_str),
4035 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004036 goto theend;
4037 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004038 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004039 {
4040 char_u *cp = vim_strchr(lv.ll_name, ':');
4041
4042 if (cp != NULL && cp < end)
4043 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004044 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004045 goto theend;
4046 }
4047 }
4048
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004050 if (name != NULL)
4051 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004052 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004053 {
4054 name[0] = K_SPECIAL;
4055 name[1] = KS_EXTRA;
4056 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004057 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004058 STRCPY(name + 3, sid_buf);
4059 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004060 else if (prefix_g)
4061 {
4062 name[0] = 'g';
4063 name[1] = ':';
4064 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4066 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004067 }
4068 *pp = end;
4069
4070theend:
4071 clear_lval(&lv);
4072 return name;
4073}
4074
4075/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004076 * Assuming "name" is the result of trans_function_name() and it was prefixed
4077 * to use the script-local name, return the unmodified name (points into
4078 * "name"). Otherwise return NULL.
4079 * This can be used to first search for a script-local function and fall back
4080 * to the global function if not found.
4081 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004082 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004083untrans_function_name(char_u *name)
4084{
4085 char_u *p;
4086
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004087 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004088 {
4089 p = vim_strchr(name, '_');
4090 if (p != NULL)
4091 return p + 1;
4092 }
4093 return NULL;
4094}
4095
4096/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004097 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4098 * current script ID and returns the expanded function name. The caller should
4099 * free the returned name. If not called from a script context or the function
4100 * name doesn't start with these prefixes, then returns NULL.
4101 * This doesn't check whether the script-local function exists or not.
4102 */
4103 char_u *
4104get_scriptlocal_funcname(char_u *funcname)
4105{
4106 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004107 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004108 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004109 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004110
4111 if (funcname == NULL)
4112 return NULL;
4113
4114 if (STRNCMP(funcname, "s:", 2) != 0
4115 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004116 {
4117 ufunc_T *ufunc;
4118
4119 // The function name does not have a script-local prefix. Try finding
4120 // it when in a Vim9 script and there is no "g:" prefix.
4121 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4122 return NULL;
4123 ufunc = find_func(funcname, FALSE);
4124 if (ufunc == NULL || func_is_global(ufunc)
4125 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4126 return NULL;
4127 ++p;
4128 off = 0;
4129 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004130 else
4131 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004132
4133 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4134 {
4135 emsg(_(e_using_sid_not_in_script_context));
4136 return NULL;
4137 }
4138 // Expand s: prefix into <SNR>nr_<name>
4139 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4140 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004141 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004142 if (newname == NULL)
4143 return NULL;
4144 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004145 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004146
4147 return newname;
4148}
4149
4150/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004151 * Return script-local "fname" with the 3-byte sequence replaced by
4152 * printable <SNR> in allocated memory.
4153 */
4154 char_u *
4155alloc_printable_func_name(char_u *fname)
4156{
4157 char_u *n = alloc(STRLEN(fname + 3) + 6);
4158
4159 if (n != NULL)
4160 {
4161 STRCPY(n, "<SNR>");
4162 STRCPY(n + 5, fname + 3);
4163 }
4164 return n;
4165}
4166
4167/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004168 * Call trans_function_name(), except that a lambda is returned as-is.
4169 * Returns the name in allocated memory.
4170 */
4171 char_u *
4172save_function_name(
4173 char_u **name,
4174 int *is_global,
4175 int skip,
4176 int flags,
4177 funcdict_T *fudi)
4178{
4179 char_u *p = *name;
4180 char_u *saved;
4181
4182 if (STRNCMP(p, "<lambda>", 8) == 0)
4183 {
4184 p += 8;
4185 (void)getdigits(&p);
4186 saved = vim_strnsave(*name, p - *name);
4187 if (fudi != NULL)
4188 CLEAR_POINTER(fudi);
4189 }
4190 else
4191 saved = trans_function_name(&p, is_global, skip,
4192 flags, fudi, NULL, NULL);
4193 *name = p;
4194 return saved;
4195}
4196
4197/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004198 * List functions. When "regmatch" is NULL all of then.
4199 * Otherwise functions matching "regmatch".
4200 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004201 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004202list_functions(regmatch_T *regmatch)
4203{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004204 int changed = func_hashtab.ht_changed;
4205 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004206 hashitem_T *hi;
4207
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004208 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004209 {
4210 if (!HASHITEM_EMPTY(hi))
4211 {
4212 ufunc_T *fp = HI2UF(hi);
4213
4214 --todo;
4215 if ((fp->uf_flags & FC_DEAD) == 0
4216 && (regmatch == NULL
4217 ? !message_filtered(fp->uf_name)
4218 && !func_name_refcount(fp->uf_name)
4219 : !isdigit(*fp->uf_name)
4220 && vim_regexec(regmatch, fp->uf_name, 0)))
4221 {
4222 list_func_head(fp, FALSE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004223 if (changed != func_hashtab.ht_changed)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004224 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00004225 emsg(_(e_function_list_was_modified));
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004226 return;
4227 }
4228 }
4229 }
4230 }
4231}
4232
4233/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004234 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004235 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4236 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004237 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004238 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004239 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004240 ufunc_T *
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004241define_function(exarg_T *eap, char_u *name_arg, garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004242{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004243 int j;
4244 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004245 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004246 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004247 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004248 char_u *p;
4249 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004250 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004251 char_u *line_arg = NULL;
4252 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004254 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004255 garray_T newlines;
4256 int varargs = FALSE;
4257 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004259 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004260 int fp_allocated = FALSE;
4261 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004262 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004263 dictitem_T *v;
4264 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004265 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004266 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004267 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004268 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004269 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004270 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004271
4272 /*
4273 * ":function" without argument: list functions.
4274 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004275 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004276 {
4277 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004278 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004279 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004280 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004281 }
4282
4283 /*
4284 * ":function /pat": list functions matching pattern.
4285 */
4286 if (*eap->arg == '/')
4287 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004288 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004289 if (!eap->skip)
4290 {
4291 regmatch_T regmatch;
4292
4293 c = *p;
4294 *p = NUL;
4295 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4296 *p = c;
4297 if (regmatch.regprog != NULL)
4298 {
4299 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004300 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004301 vim_regfree(regmatch.regprog);
4302 }
4303 }
4304 if (*p == '/')
4305 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004306 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004307 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004308 }
4309
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310 ga_init(&newargs);
4311 ga_init(&argtypes);
4312 ga_init(&default_args);
4313
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004314 /*
4315 * Get the function name. There are these situations:
4316 * func normal function name
4317 * "name" == func, "fudi.fd_dict" == NULL
4318 * dict.func new dictionary entry
4319 * "name" == NULL, "fudi.fd_dict" set,
4320 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4321 * dict.func existing dict entry with a Funcref
4322 * "name" == func, "fudi.fd_dict" set,
4323 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4324 * dict.func existing dict entry that's not a Funcref
4325 * "name" == NULL, "fudi.fd_dict" set,
4326 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4327 * s:func script-local function name
4328 * g:func global function name, same as "func"
4329 */
4330 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004331 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004332 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004333 // nested function, argument is (args).
4334 paren = TRUE;
4335 CLEAR_FIELD(fudi);
4336 }
4337 else
4338 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004339 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004340 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004341 if (p[0] == 's' && p[1] == ':')
4342 {
4343 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4344 return NULL;
4345 }
4346 p = to_name_end(p, TRUE);
4347 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4348 {
4349 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4350 eap->arg);
4351 return NULL;
4352 }
4353 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004354 }
4355
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004356 name = save_function_name(&p, &is_global, eap->skip,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004357 TFN_NO_AUTOLOAD | TFN_NEW_FUNC, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004358 paren = (vim_strchr(p, '(') != NULL);
4359 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004360 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004361 /*
4362 * Return on an invalid expression in braces, unless the expression
4363 * evaluation has been cancelled due to an aborting error, an
4364 * interrupt, or an exception.
4365 */
4366 if (!aborting())
4367 {
4368 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004369 semsg(_(e_key_not_present_in_dictionary), fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004370 vim_free(fudi.fd_newkey);
4371 return NULL;
4372 }
4373 else
4374 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004375 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004376
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004377 // For "export def FuncName()" in an autoload script the function name
4378 // is stored with the legacy autoload name "dir#script#FuncName" so
4379 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004380 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004381 {
4382 char_u *prefixed = may_prefix_autoload(name);
4383
4384 if (prefixed != NULL && prefixed != name)
4385 {
4386 vim_free(name);
4387 name = prefixed;
4388 }
4389 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004390 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004391 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004392 {
4393 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4394 goto ret_free;
4395 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004396 }
4397
Bram Moolenaare38eab22019-12-05 21:50:01 +01004398 // An error in a function call during evaluation of an expression in magic
4399 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004400 saved_did_emsg = did_emsg;
4401 did_emsg = FALSE;
4402
4403 /*
4404 * ":function func" with only function name: list function.
4405 */
4406 if (!paren)
4407 {
4408 if (!ends_excmd(*skipwhite(p)))
4409 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004410 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004411 goto ret_free;
4412 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004413 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004414 if (eap->nextcmd != NULL)
4415 *p = NUL;
4416 if (!eap->skip && !got_int)
4417 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004418 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004419 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4420 {
4421 char_u *up = untrans_function_name(name);
4422
4423 // With Vim9 script the name was made script-local, if not
4424 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004425 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004426 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004427 }
4428
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004429 if (fp != NULL)
4430 {
4431 list_func_head(fp, TRUE);
4432 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4433 {
4434 if (FUNCLINE(fp, j) == NULL)
4435 continue;
4436 msg_putchar('\n');
4437 msg_outnum((long)(j + 1));
4438 if (j < 9)
4439 msg_putchar(' ');
4440 if (j < 99)
4441 msg_putchar(' ');
4442 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004443 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004444 ui_breakcheck();
4445 }
4446 if (!got_int)
4447 {
4448 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004449 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450 msg_puts(" enddef");
4451 else
4452 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004453 }
4454 }
4455 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004456 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004457 }
4458 goto ret_free;
4459 }
4460
4461 /*
4462 * ":function name(arg1, arg2)" Define function.
4463 */
4464 p = skipwhite(p);
4465 if (*p != '(')
4466 {
4467 if (!eap->skip)
4468 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004469 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004470 goto ret_free;
4471 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004472 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004473 if (vim_strchr(p, '(') != NULL)
4474 p = vim_strchr(p, '(');
4475 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004476
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004477 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4478 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004479 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004480 goto ret_free;
4481 }
4482
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004483 // In Vim9 script only global functions can be redefined.
4484 if (vim9script && eap->forceit && !is_global)
4485 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004486 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004487 goto ret_free;
4488 }
4489
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004490 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004491
Bram Moolenaar04b12692020-05-04 23:24:44 +02004492 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004493 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004494 // Check the name of the function. Unless it's a dictionary function
4495 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004496 if (name != NULL)
4497 arg = name;
4498 else
4499 arg = fudi.fd_newkey;
4500 if (arg != NULL && (fudi.fd_di == NULL
4501 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4502 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4503 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004504 char_u *name_base = arg;
4505 int i;
4506
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004507 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004508 {
4509 name_base = vim_strchr(arg, '_');
4510 if (name_base == NULL)
4511 name_base = arg + 3;
4512 else
4513 ++name_base;
4514 }
4515 for (i = 0; name_base[i] != NUL && (i == 0
4516 ? eval_isnamec1(name_base[i])
4517 : eval_isnamec(name_base[i])); ++i)
4518 ;
4519 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004520 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004521
4522 // In Vim9 script a function cannot have the same name as a
4523 // variable.
4524 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004525 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4526 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004527 + EVAL_VAR_NO_FUNC) == OK)
4528 {
4529 semsg(_(e_redefining_script_item_str), name_base);
4530 goto ret_free;
4531 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004532 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004533 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004534 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004535 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004536 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004537 goto ret_free;
4538 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004539 }
4540
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004541 // This may get more lines and make the pointers into the first line
4542 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004543 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004544 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004545 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004546 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004547 eap, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004548 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004549 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004550
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004551 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004552 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004554 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004555 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004556 if (*p != ':')
4557 {
4558 semsg(_(e_no_white_space_allowed_before_colon_str), p);
4559 p = skipwhite(p);
4560 }
4561 else if (!IS_WHITE_OR_NUL(p[1]))
4562 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02004564 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004566 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004567 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01004568 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004570 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004572 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004573 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02004574 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004575 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004576 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02004577 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004578 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004579 else
4580 // find extra arguments "range", "dict", "abort" and "closure"
4581 for (;;)
4582 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01004583 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 p = skipwhite(p);
4585 if (STRNCMP(p, "range", 5) == 0)
4586 {
4587 flags |= FC_RANGE;
4588 p += 5;
4589 }
4590 else if (STRNCMP(p, "dict", 4) == 0)
4591 {
4592 flags |= FC_DICT;
4593 p += 4;
4594 }
4595 else if (STRNCMP(p, "abort", 5) == 0)
4596 {
4597 flags |= FC_ABORT;
4598 p += 5;
4599 }
4600 else if (STRNCMP(p, "closure", 7) == 0)
4601 {
4602 flags |= FC_CLOSURE;
4603 p += 7;
4604 if (current_funccal == NULL)
4605 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004606 emsg_funcname(e_closure_function_should_not_be_at_top_level,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 name == NULL ? (char_u *)"" : name);
4608 goto erret;
4609 }
4610 }
4611 else
4612 break;
4613 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004614
Bram Moolenaare38eab22019-12-05 21:50:01 +01004615 // When there is a line break use what follows for the function body.
4616 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004617 if (*p == '\n')
4618 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004619 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02004620 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
4621 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01004622 && !(VIM_ISWHITE(*whitep) && *p == '#'
4623 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02004624 && !eap->skip
4625 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004626 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004627
4628 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
4630 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004631 */
4632 if (KeyTyped)
4633 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004634 // Check if the function already exists, don't let the user type the
4635 // whole function before telling him it doesn't work! For a script we
4636 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004637 if (!eap->skip && !eap->forceit)
4638 {
4639 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004640 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004641 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00004642 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004643 }
4644
4645 if (!eap->skip && did_emsg)
4646 goto erret;
4647
Bram Moolenaare38eab22019-12-05 21:50:01 +01004648 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004649 cmdline_row = msg_row;
4650 }
4651
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004652 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004653 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004654
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004655 // Do not define the function when getting the body fails and when
4656 // skipping.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004657 if (get_function_body(eap, &newlines, line_arg, lines_to_free) == FAIL
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004658 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004659 goto erret;
4660
4661 /*
4662 * If there are no errors, add the function
4663 */
4664 if (fudi.fd_dict == NULL)
4665 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004667 char_u *find_name = name;
4668 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004669 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004671 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004672 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004673 var_conflict = TRUE;
4674
4675 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
4676 {
4677 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
4678
4679 if (si->sn_autoload_prefix != NULL)
4680 {
4681 if (is_export)
4682 {
4683 find_name = name + STRLEN(si->sn_autoload_prefix);
4684 v = find_var(find_name, &ht, TRUE);
4685 if (v != NULL)
4686 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004687 // Only check if the function already exists in the script,
4688 // global functions can be shadowed.
4689 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004690 }
4691 else
4692 {
4693 char_u *prefixed = may_prefix_autoload(name);
4694
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00004695 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004696 {
4697 v = find_var(prefixed, &ht, TRUE);
4698 if (v != NULL)
4699 var_conflict = TRUE;
4700 vim_free(prefixed);
4701 }
4702 }
4703 }
4704 }
4705 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004706 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004707 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004708 goto erret;
4709 }
4710
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004711 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02004712 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004713 {
Bram Moolenaareef21022020-08-01 22:16:43 +02004714 char_u *uname = untrans_function_name(name);
4715
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004716 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004717 }
4718
4719 if (fp != NULL || import != NULL)
4720 {
4721 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004722
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004723 // Function can be replaced with "function!" and when sourcing the
4724 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02004725 // A name that is used by an import can not be overruled.
4726 if (import != NULL
4727 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004728 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02004729 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004730 {
Bram Moolenaard604d782021-11-20 21:46:20 +00004731 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02004732 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004733 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02004734 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00004735 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004736 goto erret;
4737 }
4738 if (fp->uf_calls > 0)
4739 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004740 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00004741 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004742 goto erret;
4743 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004744 if (fp->uf_refcount > 1)
4745 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004746 // This function is referenced somewhere, don't redefine it but
4747 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004748 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004749 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004750 fp = NULL;
4751 overwrite = TRUE;
4752 }
4753 else
4754 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004755 char_u *exp_name = fp->uf_name_exp;
4756
4757 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01004758 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004759 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004760 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004761 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004763#ifdef FEAT_PROFILE
4764 fp->uf_profiling = FALSE;
4765 fp->uf_prof_initialized = FALSE;
4766#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01004767 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004768 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004769 }
4770 }
4771 else
4772 {
4773 char numbuf[20];
4774
4775 fp = NULL;
4776 if (fudi.fd_newkey == NULL && !eap->forceit)
4777 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004778 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004779 goto erret;
4780 }
4781 if (fudi.fd_di == NULL)
4782 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004783 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02004784 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004785 goto erret;
4786 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004787 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02004788 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004789 goto erret;
4790
Bram Moolenaare38eab22019-12-05 21:50:01 +01004791 // Give the function a sequential number. Can only be used with a
4792 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004793 vim_free(name);
4794 sprintf(numbuf, "%d", ++func_nr);
4795 name = vim_strsave((char_u *)numbuf);
4796 if (name == NULL)
4797 goto erret;
4798 }
4799
4800 if (fp == NULL)
4801 {
4802 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
4803 {
4804 int slen, plen;
4805 char_u *scriptname;
4806
Bram Moolenaare38eab22019-12-05 21:50:01 +01004807 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004808 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004809 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004810 {
4811 scriptname = autoload_name(name);
4812 if (scriptname != NULL)
4813 {
4814 p = vim_strchr(scriptname, '/');
4815 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004816 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004817 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004818 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004819 j = OK;
4820 vim_free(scriptname);
4821 }
4822 }
4823 if (j == FAIL)
4824 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004825 linenr_T save_lnum = SOURCING_LNUM;
4826
4827 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00004828 semsg(_(e_function_name_does_not_match_script_file_name_str),
4829 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004830 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004831 goto erret;
4832 }
4833 }
4834
Bram Moolenaar47ed5532019-08-08 20:49:14 +02004835 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004836 if (fp == NULL)
4837 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004838 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004839
4840 if (fudi.fd_dict != NULL)
4841 {
4842 if (fudi.fd_di == NULL)
4843 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004844 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004845 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
4846 if (fudi.fd_di == NULL)
4847 {
4848 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004849 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004850 goto erret;
4851 }
4852 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
4853 {
4854 vim_free(fudi.fd_di);
4855 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004856 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004857 goto erret;
4858 }
4859 }
4860 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004861 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004862 clear_tv(&fudi.fd_di->di_tv);
4863 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004864 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004865
Bram Moolenaare38eab22019-12-05 21:50:01 +01004866 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004867 flags |= FC_DICT;
4868 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004869 }
4870 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004871 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004872 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004873 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874
4875 if (eap->cmdidx == CMD_def)
4876 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02004877 int lnum_save = SOURCING_LNUM;
4878 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004879
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004880 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004881
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004882 // error messages are for the first function line
4883 SOURCING_LNUM = sourcing_lnum_top;
4884
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02004885 // The function may use script variables from the context.
4886 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004887
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004888 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004890 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004891 free_fp = fp_allocated;
4892 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004894 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895
4896 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004897 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004898 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004899 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004900 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004901 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02004903 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004904 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02004905 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004906 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004907
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004908 if (fp_allocated)
4909 {
4910 // insert the new function in the function list
4911 set_ufunc_name(fp, name);
4912 if (overwrite)
4913 {
4914 hi = hash_find(&func_hashtab, name);
4915 hi->hi_key = UF2HIKEY(fp);
4916 }
4917 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
4918 {
4919 free_fp = TRUE;
4920 goto erret;
4921 }
4922 fp->uf_refcount = 1;
4923 }
4924
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004925 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004926 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004927 if ((flags & FC_CLOSURE) != 0)
4928 {
Bram Moolenaar58016442016-07-31 18:30:22 +02004929 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004930 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004931 }
4932 else
4933 fp->uf_scoped = NULL;
4934
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004935#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004936 if (prof_def_func())
4937 func_do_profile(fp);
4938#endif
4939 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02004940 if (sandbox)
4941 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004942 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004943 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004944 fp->uf_flags = flags;
4945 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01004946 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004947 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004948 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004949 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004950 if (is_export)
4951 {
4952 fp->uf_flags |= FC_EXPORT;
4953 // let ex_export() know the export worked.
4954 is_export = FALSE;
4955 }
4956
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004957 if (eap->cmdidx == CMD_def)
4958 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02004959 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
4960 // :func does not use Vim9 script syntax, even in a Vim9 script file
4961 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004962
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004963 goto ret_free;
4964
4965erret:
4966 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004967 ga_clear_strings(&default_args);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004968 if (fp != NULL)
4969 {
4970 ga_init(&fp->uf_args);
4971 ga_init(&fp->uf_def_args);
4972 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004973errret_2:
4974 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004975 if (fp != NULL)
4976 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004977 if (free_fp)
4978 {
4979 vim_free(fp);
4980 fp = NULL;
4981 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004982ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01004983 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004984 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004985 if (name != name_arg)
4986 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004987 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004988 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004989
4990 return fp;
4991}
4992
4993/*
4994 * ":function"
4995 */
4996 void
4997ex_function(exarg_T *eap)
4998{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004999 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005000
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005001 ga_init2(&lines_to_free, sizeof(char_u *), 50);
5002 (void)define_function(eap, NULL, &lines_to_free);
5003 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005004}
5005
5006/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005007 * Find a function by name, including "<lambda>123".
5008 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005009 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005010 * Return NULL if not found.
5011 */
5012 ufunc_T *
5013find_func_by_name(char_u *name, compiletype_T *compile_type)
5014{
5015 char_u *arg = name;
5016 char_u *fname;
5017 ufunc_T *ufunc;
5018 int is_global = FALSE;
5019
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005020 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5021 {
5022 *compile_type = CT_PROFILE;
5023 arg = skipwhite(arg + 7);
5024 }
5025 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5026 {
5027 *compile_type = CT_DEBUG;
5028 arg = skipwhite(arg + 5);
5029 }
5030
5031 if (STRNCMP(arg, "<lambda>", 8) == 0)
5032 {
5033 arg += 8;
5034 (void)getdigits(&arg);
5035 fname = vim_strnsave(name, arg - name);
5036 }
5037 else
5038 fname = trans_function_name(&arg, &is_global, FALSE,
5039 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
5040 if (fname == NULL)
5041 {
5042 semsg(_(e_invalid_argument_str), name);
5043 return NULL;
5044 }
5045 if (!ends_excmd2(name, arg))
5046 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005047 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005048 emsg(ex_errmsg(e_trailing_characters_str, arg));
5049 return NULL;
5050 }
5051
5052 ufunc = find_func(fname, is_global);
5053 if (ufunc == NULL)
5054 {
5055 char_u *p = untrans_function_name(fname);
5056
5057 if (p != NULL)
5058 // Try again without making it script-local.
5059 ufunc = find_func(p, FALSE);
5060 }
5061 vim_free(fname);
5062 if (ufunc == NULL)
5063 semsg(_(e_cannot_find_function_str), name);
5064 return ufunc;
5065}
5066
5067/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005068 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005069 * be compiled or the one specified by the argument.
5070 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005071 */
5072 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005073ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005074{
Bram Moolenaar822ba242020-05-24 23:00:18 +02005075 ufunc_T *ufunc;
5076
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005077 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005078 {
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005079 compiletype_T compile_type = CT_NONE;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005080
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005081 ufunc = find_func_by_name(eap->arg, &compile_type);
5082 if (ufunc != NULL)
5083 {
5084 if (func_needs_compiling(ufunc, compile_type))
5085 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5086 else
5087 smsg(_("Function %s does not need compiling"), eap->arg);
5088 }
5089 }
5090 else
5091 {
5092 long todo = (long)func_hashtab.ht_used;
5093 int changed = func_hashtab.ht_changed;
5094 hashitem_T *hi;
5095
5096 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5097 {
5098 if (!HASHITEM_EMPTY(hi))
5099 {
5100 --todo;
5101 ufunc = HI2UF(hi);
5102 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5103 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5104 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005105 {
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005106 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5107
5108 if (func_hashtab.ht_changed != changed)
5109 {
5110 // a function has been added or removed, need to start
5111 // over
5112 todo = (long)func_hashtab.ht_used;
5113 changed = func_hashtab.ht_changed;
5114 hi = func_hashtab.ht_array;
5115 --hi;
5116 }
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005117 }
5118 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005119 }
5120 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005121}
5122
5123/*
5124 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5125 * Return 2 if "p" starts with "s:".
5126 * Return 0 otherwise.
5127 */
5128 int
5129eval_fname_script(char_u *p)
5130{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005131 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5132 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005133 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5134 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5135 return 5;
5136 if (p[0] == 's' && p[1] == ':')
5137 return 2;
5138 return 0;
5139}
5140
5141 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005142translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005143{
5144 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005145 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005146 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005147}
5148
5149/*
5150 * Return TRUE when "ufunc" has old-style "..." varargs
5151 * or named varargs "...name: type".
5152 */
5153 int
5154has_varargs(ufunc_T *ufunc)
5155{
5156 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005157}
5158
5159/*
5160 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005161 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005162 */
5163 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005164function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005165{
5166 char_u *nm = name;
5167 char_u *p;
5168 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005169 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005170 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005171
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005172 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5173 if (no_deref)
5174 flag |= TFN_NO_DEREF;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005175 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005176 nm = skipwhite(nm);
5177
Bram Moolenaare38eab22019-12-05 21:50:01 +01005178 // Only accept "funcname", "funcname ", "funcname (..." and
5179 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005180 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005181 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005182 vim_free(p);
5183 return n;
5184}
5185
Bram Moolenaar113e1072019-01-20 15:30:40 +01005186#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005187 char_u *
5188get_expanded_name(char_u *name, int check)
5189{
5190 char_u *nm = name;
5191 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005192 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005193
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005194 p = trans_function_name(&nm, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005195 TFN_INT|TFN_QUIET, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005196
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005197 if (p != NULL && *nm == NUL
5198 && (!check || translated_function_exists(p, is_global)))
5199 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005200
5201 vim_free(p);
5202 return NULL;
5203}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005204#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005205
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005206/*
5207 * Function given to ExpandGeneric() to obtain the list of user defined
5208 * function names.
5209 */
5210 char_u *
5211get_user_func_name(expand_T *xp, int idx)
5212{
5213 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005214 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005215 static hashitem_T *hi;
5216 ufunc_T *fp;
5217
5218 if (idx == 0)
5219 {
5220 done = 0;
5221 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005222 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005223 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005224 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005225 {
5226 if (done++ > 0)
5227 ++hi;
5228 while (HASHITEM_EMPTY(hi))
5229 ++hi;
5230 fp = HI2UF(hi);
5231
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005232 // don't show dead, dict and lambda functions
5233 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005234 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005235 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005236
5237 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005238 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005239
5240 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005241 if (xp->xp_context != EXPAND_USER_FUNC
5242 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005243 {
5244 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005245 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005246 STRCAT(IObuff, ")");
5247 }
5248 return IObuff;
5249 }
5250 return NULL;
5251}
5252
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005253/*
5254 * ":delfunction {name}"
5255 */
5256 void
5257ex_delfunction(exarg_T *eap)
5258{
5259 ufunc_T *fp = NULL;
5260 char_u *p;
5261 char_u *name;
5262 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005263 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005264
5265 p = eap->arg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005266 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi,
5267 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005268 vim_free(fudi.fd_newkey);
5269 if (name == NULL)
5270 {
5271 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005272 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005273 return;
5274 }
5275 if (!ends_excmd(*skipwhite(p)))
5276 {
5277 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005278 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005279 return;
5280 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005281 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005282 if (eap->nextcmd != NULL)
5283 *p = NUL;
5284
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005285 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005286 {
5287 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005288 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005289 vim_free(name);
5290 return;
5291 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005292 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005293 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005294 vim_free(name);
5295
5296 if (!eap->skip)
5297 {
5298 if (fp == NULL)
5299 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005300 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005301 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005302 return;
5303 }
5304 if (fp->uf_calls > 0)
5305 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005306 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005307 return;
5308 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005309 if (fp->uf_flags & FC_VIM9)
5310 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005311 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005312 return;
5313 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005314
5315 if (fudi.fd_dict != NULL)
5316 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005317 // Delete the dict item that refers to the function, it will
5318 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005319 dictitem_remove(fudi.fd_dict, fudi.fd_di);
5320 }
5321 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005322 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005323 // A normal function (not a numbered function or lambda) has a
5324 // refcount of 1 for the entry in the hashtable. When deleting
5325 // it and the refcount is more than one, it should be kept.
5326 // A numbered function and lambda should be kept if the refcount is
5327 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005328 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005329 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005330 // Function is still referenced somewhere. Don't free it but
5331 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005332 if (func_remove(fp))
5333 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005334 }
5335 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005336 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005337 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005338 }
5339}
5340
5341/*
5342 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005343 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005344 */
5345 void
5346func_unref(char_u *name)
5347{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005348 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005349
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005350 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005351 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005352 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005353 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005354 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005355#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005356 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005357#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005358 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005359 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005360 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005361}
5362
5363/*
5364 * Unreference a Function: decrement the reference count and free it when it
5365 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005366 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005367 */
5368 void
5369func_ptr_unref(ufunc_T *fp)
5370{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005371 if (fp != NULL && (--fp->uf_refcount <= 0
5372 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5373 && fp->uf_partial->pt_refcount <= 1
5374 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005375 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005376 // Only delete it when it's not being used. Otherwise it's done
5377 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005378 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005379 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005380 }
5381}
5382
5383/*
5384 * Count a reference to a Function.
5385 */
5386 void
5387func_ref(char_u *name)
5388{
5389 ufunc_T *fp;
5390
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005391 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005392 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005393 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005394 if (fp != NULL)
5395 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005396 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005397 // Only give an error for a numbered function.
5398 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005399 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005400}
5401
5402/*
5403 * Count a reference to a Function.
5404 */
5405 void
5406func_ptr_ref(ufunc_T *fp)
5407{
5408 if (fp != NULL)
5409 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005410}
5411
5412/*
5413 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5414 * referenced from anywhere that is in use.
5415 */
5416 static int
5417can_free_funccal(funccall_T *fc, int copyID)
5418{
5419 return (fc->l_varlist.lv_copyID != copyID
5420 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005421 && fc->l_avars.dv_copyID != copyID
5422 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005423}
5424
5425/*
5426 * ":return [expr]"
5427 */
5428 void
5429ex_return(exarg_T *eap)
5430{
5431 char_u *arg = eap->arg;
5432 typval_T rettv;
5433 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005434 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005435
5436 if (current_funccal == NULL)
5437 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005438 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005439 return;
5440 }
5441
Bram Moolenaar844fb642021-10-23 13:32:30 +01005442 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005443 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5444
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005445 if (eap->skip)
5446 ++emsg_skip;
5447
5448 eap->nextcmd = NULL;
5449 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005450 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005451 {
5452 if (!eap->skip)
5453 returning = do_return(eap, FALSE, TRUE, &rettv);
5454 else
5455 clear_tv(&rettv);
5456 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005457 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005458 else if (!eap->skip)
5459 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005460 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005461 update_force_abort();
5462
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005463 /*
5464 * Return unless the expression evaluation has been cancelled due to an
5465 * aborting error, an interrupt, or an exception.
5466 */
5467 if (!aborting())
5468 returning = do_return(eap, FALSE, TRUE, NULL);
5469 }
5470
Bram Moolenaare38eab22019-12-05 21:50:01 +01005471 // When skipping or the return gets pending, advance to the next command
5472 // in this line (!returning). Otherwise, ignore the rest of the line.
5473 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005474 if (returning)
5475 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005476 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02005477 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005478
5479 if (eap->skip)
5480 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02005481 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005482}
5483
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005484 static int
5485ex_call_inner(
5486 exarg_T *eap,
5487 char_u *name,
5488 char_u **arg,
5489 char_u *startarg,
5490 funcexe_T *funcexe_init,
5491 evalarg_T *evalarg)
5492{
5493 linenr_T lnum;
5494 int doesrange;
5495 typval_T rettv;
5496 int failed = FALSE;
5497
5498 /*
5499 * When skipping, evaluate the function once, to find the end of the
5500 * arguments.
5501 * When the function takes a range, this is discovered after the first
5502 * call, and the loop is broken.
5503 */
5504 if (eap->skip)
5505 {
5506 ++emsg_skip;
5507 lnum = eap->line2; // do it once, also with an invalid range
5508 }
5509 else
5510 lnum = eap->line1;
5511 for ( ; lnum <= eap->line2; ++lnum)
5512 {
5513 funcexe_T funcexe;
5514
5515 if (!eap->skip && eap->addr_count > 0)
5516 {
5517 if (lnum > curbuf->b_ml.ml_line_count)
5518 {
5519 // If the function deleted lines or switched to another buffer
5520 // the line number may become invalid.
5521 emsg(_(e_invalid_range));
5522 break;
5523 }
5524 curwin->w_cursor.lnum = lnum;
5525 curwin->w_cursor.col = 0;
5526 curwin->w_cursor.coladd = 0;
5527 }
5528 *arg = startarg;
5529
5530 funcexe = *funcexe_init;
5531 funcexe.fe_doesrange = &doesrange;
5532 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
5533 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
5534 {
5535 failed = TRUE;
5536 break;
5537 }
5538 if (has_watchexpr())
5539 dbg_check_breakpoint(eap);
5540
5541 // Handle a function returning a Funcref, Dictionary or List.
5542 if (handle_subscript(arg, NULL, &rettv,
5543 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
5544 {
5545 failed = TRUE;
5546 break;
5547 }
5548
5549 clear_tv(&rettv);
5550 if (doesrange || eap->skip)
5551 break;
5552
5553 // Stop when immediately aborting on error, or when an interrupt
5554 // occurred or an exception was thrown but not caught.
5555 // get_func_tv() returned OK, so that the check for trailing
5556 // characters below is executed.
5557 if (aborting())
5558 break;
5559 }
5560 if (eap->skip)
5561 --emsg_skip;
5562 return failed;
5563}
5564
5565/*
5566 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
5567 * Returns FAIL or OK.
5568 */
5569 static int
5570ex_defer_inner(char_u *name, char_u **arg, evalarg_T *evalarg)
5571{
5572 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
5573 int argcount = 0; // number of arguments found
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005574
5575 if (current_funccal == NULL)
5576 {
5577 semsg(_(e_str_not_inside_function), "defer");
5578 return FAIL;
5579 }
5580 if (get_func_arguments(arg, evalarg, FALSE, argvars, &argcount) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01005581 {
5582 while (--argcount >= 0)
5583 clear_tv(&argvars[argcount]);
5584 return FAIL;
5585 }
5586 return add_defer(name, argcount, argvars);
5587}
5588
5589/*
5590 * Add a deferred call for "name" with arguments "argvars[argcount]".
5591 * Consumes "argvars[]".
5592 * Caller must check that in_def_function() returns TRUE or current_funccal is
5593 * not NULL.
5594 * Returns OK or FAIL.
5595 */
5596 int
5597add_defer(char_u *name, int argcount_arg, typval_T *argvars)
5598{
5599 char_u *saved_name = vim_strsave(name);
5600 int argcount = argcount_arg;
5601 defer_T *dr;
5602 int ret = FAIL;
5603
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005604 if (saved_name == NULL)
5605 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01005606 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005607 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01005608 if (add_defer_function(saved_name, argcount, argvars) == OK)
5609 argcount = 0;
5610 }
5611 else
5612 {
5613 if (current_funccal->fc_defer.ga_itemsize == 0)
5614 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
5615 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
5616 goto theend;
5617 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
5618 + current_funccal->fc_defer.ga_len++;
5619 dr->dr_name = saved_name;
5620 dr->dr_argcount = argcount;
5621 while (argcount > 0)
5622 {
5623 --argcount;
5624 dr->dr_argvars[argcount] = argvars[argcount];
5625 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005626 }
5627 ret = OK;
5628
5629theend:
5630 while (--argcount >= 0)
5631 clear_tv(&argvars[argcount]);
5632 return ret;
5633}
5634
5635/*
5636 * Invoked after a functions has finished: invoke ":defer" functions.
5637 */
5638 void
5639handle_defer(void)
5640{
5641 int idx;
5642
5643 for (idx = current_funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
5644 {
5645 funcexe_T funcexe;
5646 typval_T rettv;
5647 defer_T *dr = ((defer_T *)current_funccal->fc_defer.ga_data) + idx;
5648 int i;
5649
5650 CLEAR_FIELD(funcexe);
5651 funcexe.fe_evaluate = TRUE;
5652
5653 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
5654 call_func(dr->dr_name, -1, &rettv,
5655 dr->dr_argcount, dr->dr_argvars, &funcexe);
5656 clear_tv(&rettv);
5657 vim_free(dr->dr_name);
5658 for (i = dr->dr_argcount - 1; i >= 0; --i)
5659 clear_tv(&dr->dr_argvars[i]);
5660 }
5661 ga_clear(&current_funccal->fc_defer);
5662}
5663
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005664/*
5665 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005666 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005667 */
5668 void
5669ex_call(exarg_T *eap)
5670{
5671 char_u *arg = eap->arg;
5672 char_u *startarg;
5673 char_u *name;
5674 char_u *tofree;
5675 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005676 int failed = FALSE;
5677 funcdict_T fudi;
5678 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005679 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005680 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005681 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005682 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005683
Bram Moolenaare6b53242020-07-01 17:28:33 +02005684 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005685 if (eap->skip)
5686 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005687 typval_T rettv;
5688
Bram Moolenaare38eab22019-12-05 21:50:01 +01005689 // trans_function_name() doesn't work well when skipping, use eval0()
5690 // instead to skip to any following command, e.g. for:
5691 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005692 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005693 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005694 clear_tv(&rettv);
5695 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005696 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005697 return;
5698 }
5699
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005700 tofree = trans_function_name(&arg, NULL, eap->skip, TFN_INT,
Bram Moolenaar4525a572022-02-13 11:57:33 +00005701 &fudi, &partial, vim9script ? &type : NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005702 if (fudi.fd_newkey != NULL)
5703 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005704 // Still need to give an error message for missing key.
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005705 semsg(_(e_key_not_present_in_dictionary), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005706 vim_free(fudi.fd_newkey);
5707 }
5708 if (tofree == NULL)
5709 return;
5710
Bram Moolenaare38eab22019-12-05 21:50:01 +01005711 // Increase refcount on dictionary, it could get deleted when evaluating
5712 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005713 if (fudi.fd_dict != NULL)
5714 ++fudi.fd_dict->dv_refcount;
5715
Bram Moolenaare38eab22019-12-05 21:50:01 +01005716 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
5717 // contents. For VAR_PARTIAL get its partial, unless we already have one
5718 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005719 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005720 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00005721 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00005722 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005723
Bram Moolenaare38eab22019-12-05 21:50:01 +01005724 // Skip white space to allow ":call func ()". Not good, but required for
5725 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005726 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005727 if (*startarg != '(')
5728 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005729 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005730 goto end;
5731 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00005732 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02005733 {
5734 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
5735 goto end;
5736 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005737
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005738 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005739 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005740 arg = startarg;
5741 failed = ex_defer_inner(name, &arg, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005742 }
5743 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005744 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005745 funcexe_T funcexe;
5746
Bram Moolenaara80faa82020-04-12 19:37:17 +02005747 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005748 funcexe.fe_check_type = type;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00005749 funcexe.fe_partial = partial;
5750 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005751 funcexe.fe_firstline = eap->line1;
5752 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005753 funcexe.fe_found_var = found_var;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005754 funcexe.fe_evaluate = !eap->skip;
5755 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005756 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005757
Bram Moolenaar1d341892021-09-18 15:25:52 +02005758 // When inside :try we need to check for following "| catch" or "| endtry".
5759 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005760 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005761 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005762 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02005763 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02005764 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005765 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02005766 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01005767 {
5768 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00005769 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01005770 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005771 }
5772 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02005773 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005774 }
Bram Moolenaara929c922022-04-18 15:21:17 +01005775 // Must be after using "arg", it may point into memory cleared here.
5776 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005777
5778end:
5779 dict_unref(fudi.fd_dict);
5780 vim_free(tofree);
5781}
5782
5783/*
5784 * Return from a function. Possibly makes the return pending. Also called
5785 * for a pending return at the ":endtry" or after returning from an extra
5786 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
5787 * when called due to a ":return" command. "rettv" may point to a typval_T
5788 * with the return rettv. Returns TRUE when the return can be carried out,
5789 * FALSE when the return gets pending.
5790 */
5791 int
5792do_return(
5793 exarg_T *eap,
5794 int reanimate,
5795 int is_cmd,
5796 void *rettv)
5797{
5798 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01005799 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005800
5801 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005802 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005803 current_funccal->returned = FALSE;
5804
5805 /*
5806 * Cleanup (and inactivate) conditionals, but stop when a try conditional
5807 * not in its finally clause (which then is to be executed next) is found.
5808 * In this case, make the ":return" pending for execution at the ":endtry".
5809 * Otherwise, return normally.
5810 */
5811 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
5812 if (idx >= 0)
5813 {
5814 cstack->cs_pending[idx] = CSTP_RETURN;
5815
5816 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005817 // A pending return again gets pending. "rettv" points to an
5818 // allocated variable with the rettv of the original ":return"'s
5819 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005820 cstack->cs_rettv[idx] = rettv;
5821 else
5822 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005823 // When undoing a return in order to make it pending, get the stored
5824 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005825 if (reanimate)
5826 rettv = current_funccal->rettv;
5827
5828 if (rettv != NULL)
5829 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005830 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005831 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
5832 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
5833 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005834 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005835 }
5836 else
5837 cstack->cs_rettv[idx] = NULL;
5838
5839 if (reanimate)
5840 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005841 // The pending return value could be overwritten by a ":return"
5842 // without argument in a finally clause; reset the default
5843 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005844 current_funccal->rettv->v_type = VAR_NUMBER;
5845 current_funccal->rettv->vval.v_number = 0;
5846 }
5847 }
5848 report_make_pending(CSTP_RETURN, rettv);
5849 }
5850 else
5851 {
5852 current_funccal->returned = TRUE;
5853
Bram Moolenaare38eab22019-12-05 21:50:01 +01005854 // If the return is carried out now, store the return value. For
5855 // a return immediately after reanimation, the value is already
5856 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005857 if (!reanimate && rettv != NULL)
5858 {
5859 clear_tv(current_funccal->rettv);
5860 *current_funccal->rettv = *(typval_T *)rettv;
5861 if (!is_cmd)
5862 vim_free(rettv);
5863 }
5864 }
5865
5866 return idx < 0;
5867}
5868
5869/*
5870 * Free the variable with a pending return value.
5871 */
5872 void
5873discard_pending_return(void *rettv)
5874{
5875 free_tv((typval_T *)rettv);
5876}
5877
5878/*
5879 * Generate a return command for producing the value of "rettv". The result
5880 * is an allocated string. Used by report_pending() for verbose messages.
5881 */
5882 char_u *
5883get_return_cmd(void *rettv)
5884{
5885 char_u *s = NULL;
5886 char_u *tofree = NULL;
5887 char_u numbuf[NUMBUFLEN];
5888
5889 if (rettv != NULL)
5890 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
5891 if (s == NULL)
5892 s = (char_u *)"";
5893
5894 STRCPY(IObuff, ":return ");
5895 STRNCPY(IObuff + 8, s, IOSIZE - 8);
5896 if (STRLEN(s) + 8 >= IOSIZE)
5897 STRCPY(IObuff + IOSIZE - 4, "...");
5898 vim_free(tofree);
5899 return vim_strsave(IObuff);
5900}
5901
5902/*
5903 * Get next function line.
5904 * Called by do_cmdline() to get the next line.
5905 * Returns allocated string, or NULL for end of function.
5906 */
5907 char_u *
5908get_func_line(
5909 int c UNUSED,
5910 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02005911 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005912 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005913{
5914 funccall_T *fcp = (funccall_T *)cookie;
5915 ufunc_T *fp = fcp->func;
5916 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005917 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005918
Bram Moolenaare38eab22019-12-05 21:50:01 +01005919 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005920 if (fcp->dbg_tick != debug_tick)
5921 {
5922 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005923 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005924 fcp->dbg_tick = debug_tick;
5925 }
5926#ifdef FEAT_PROFILE
5927 if (do_profiling == PROF_YES)
5928 func_line_end(cookie);
5929#endif
5930
5931 gap = &fp->uf_lines;
5932 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5933 || fcp->returned)
5934 retval = NULL;
5935 else
5936 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005937 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005938 while (fcp->linenr < gap->ga_len
5939 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
5940 ++fcp->linenr;
5941 if (fcp->linenr >= gap->ga_len)
5942 retval = NULL;
5943 else
5944 {
5945 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005946 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005947#ifdef FEAT_PROFILE
5948 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005949 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005950#endif
5951 }
5952 }
5953
Bram Moolenaare38eab22019-12-05 21:50:01 +01005954 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005955 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005956 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005957 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01005958 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005959 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005960 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005961 fcp->dbg_tick = debug_tick;
5962 }
5963
5964 return retval;
5965}
5966
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005967/*
5968 * Return TRUE if the currently active function should be ended, because a
5969 * return was encountered or an error occurred. Used inside a ":while".
5970 */
5971 int
5972func_has_ended(void *cookie)
5973{
5974 funccall_T *fcp = (funccall_T *)cookie;
5975
Bram Moolenaare38eab22019-12-05 21:50:01 +01005976 // Ignore the "abort" flag if the abortion behavior has been changed due to
5977 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005978 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5979 || fcp->returned);
5980}
5981
5982/*
5983 * return TRUE if cookie indicates a function which "abort"s on errors.
5984 */
5985 int
5986func_has_abort(
5987 void *cookie)
5988{
5989 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
5990}
5991
5992
5993/*
5994 * Turn "dict.Func" into a partial for "Func" bound to "dict".
5995 * Don't do this when "Func" is already a partial that was bound
5996 * explicitly (pt_auto is FALSE).
5997 * Changes "rettv" in-place.
5998 * Returns the updated "selfdict_in".
5999 */
6000 dict_T *
6001make_partial(dict_T *selfdict_in, typval_T *rettv)
6002{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006003 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006004 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006005 char_u fname_buf[FLEN_FIXED + 1];
6006 int error;
6007 dict_T *selfdict = selfdict_in;
6008
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006009 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6010 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006011 fp = rettv->vval.v_partial->pt_func;
6012 else
6013 {
6014 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006015 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006016 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006017 if (fname == NULL)
6018 {
6019 // There is no point binding a dict to a NULL function, just create
6020 // a function reference.
6021 rettv->v_type = VAR_FUNC;
6022 rettv->vval.v_string = NULL;
6023 }
6024 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006025 {
6026 char_u *tofree = NULL;
6027
6028 // Translate "s:func" to the stored function name.
6029 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6030 fp = find_func(fname, FALSE);
6031 vim_free(tofree);
6032 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006033 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006034
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006035 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006036 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006037 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006038
6039 if (pt != NULL)
6040 {
6041 pt->pt_refcount = 1;
6042 pt->pt_dict = selfdict;
6043 pt->pt_auto = TRUE;
6044 selfdict = NULL;
6045 if (rettv->v_type == VAR_FUNC)
6046 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006047 // Just a function: Take over the function name and use
6048 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006049 pt->pt_name = rettv->vval.v_string;
6050 }
6051 else
6052 {
6053 partial_T *ret_pt = rettv->vval.v_partial;
6054 int i;
6055
Bram Moolenaare38eab22019-12-05 21:50:01 +01006056 // Partial: copy the function name, use selfdict and copy
6057 // args. Can't take over name or args, the partial might
6058 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006059 if (ret_pt->pt_name != NULL)
6060 {
6061 pt->pt_name = vim_strsave(ret_pt->pt_name);
6062 func_ref(pt->pt_name);
6063 }
6064 else
6065 {
6066 pt->pt_func = ret_pt->pt_func;
6067 func_ptr_ref(pt->pt_func);
6068 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006069 if (ret_pt->pt_argc > 0)
6070 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006071 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006072 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006073 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006074 pt->pt_argc = 0;
6075 else
6076 {
6077 pt->pt_argc = ret_pt->pt_argc;
6078 for (i = 0; i < pt->pt_argc; i++)
6079 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6080 }
6081 }
6082 partial_unref(ret_pt);
6083 }
6084 rettv->v_type = VAR_PARTIAL;
6085 rettv->vval.v_partial = pt;
6086 }
6087 }
6088 return selfdict;
6089}
6090
6091/*
6092 * Return the name of the executed function.
6093 */
6094 char_u *
6095func_name(void *cookie)
6096{
6097 return ((funccall_T *)cookie)->func->uf_name;
6098}
6099
6100/*
6101 * Return the address holding the next breakpoint line for a funccall cookie.
6102 */
6103 linenr_T *
6104func_breakpoint(void *cookie)
6105{
6106 return &((funccall_T *)cookie)->breakpoint;
6107}
6108
6109/*
6110 * Return the address holding the debug tick for a funccall cookie.
6111 */
6112 int *
6113func_dbg_tick(void *cookie)
6114{
6115 return &((funccall_T *)cookie)->dbg_tick;
6116}
6117
6118/*
6119 * Return the nesting level for a funccall cookie.
6120 */
6121 int
6122func_level(void *cookie)
6123{
6124 return ((funccall_T *)cookie)->level;
6125}
6126
6127/*
6128 * Return TRUE when a function was ended by a ":return" command.
6129 */
6130 int
6131current_func_returned(void)
6132{
6133 return current_funccal->returned;
6134}
6135
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006136 int
6137free_unref_funccal(int copyID, int testing)
6138{
6139 int did_free = FALSE;
6140 int did_free_funccal = FALSE;
6141 funccall_T *fc, **pfc;
6142
6143 for (pfc = &previous_funccal; *pfc != NULL; )
6144 {
6145 if (can_free_funccal(*pfc, copyID))
6146 {
6147 fc = *pfc;
6148 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006149 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006150 did_free = TRUE;
6151 did_free_funccal = TRUE;
6152 }
6153 else
6154 pfc = &(*pfc)->caller;
6155 }
6156 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006157 // When a funccal was freed some more items might be garbage
6158 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006159 (void)garbage_collect(testing);
6160
6161 return did_free;
6162}
6163
6164/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006165 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006166 */
6167 static funccall_T *
6168get_funccal(void)
6169{
6170 int i;
6171 funccall_T *funccal;
6172 funccall_T *temp_funccal;
6173
6174 funccal = current_funccal;
6175 if (debug_backtrace_level > 0)
6176 {
6177 for (i = 0; i < debug_backtrace_level; i++)
6178 {
6179 temp_funccal = funccal->caller;
6180 if (temp_funccal)
6181 funccal = temp_funccal;
6182 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006183 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006184 debug_backtrace_level = i;
6185 }
6186 }
6187 return funccal;
6188}
6189
6190/*
6191 * Return the hashtable used for local variables in the current funccal.
6192 * Return NULL if there is no current funccal.
6193 */
6194 hashtab_T *
6195get_funccal_local_ht()
6196{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006197 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006198 return NULL;
6199 return &get_funccal()->l_vars.dv_hashtab;
6200}
6201
6202/*
6203 * Return the l: scope variable.
6204 * Return NULL if there is no current funccal.
6205 */
6206 dictitem_T *
6207get_funccal_local_var()
6208{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006209 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006210 return NULL;
6211 return &get_funccal()->l_vars_var;
6212}
6213
6214/*
6215 * Return the hashtable used for argument in the current funccal.
6216 * Return NULL if there is no current funccal.
6217 */
6218 hashtab_T *
6219get_funccal_args_ht()
6220{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006221 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006222 return NULL;
6223 return &get_funccal()->l_avars.dv_hashtab;
6224}
6225
6226/*
6227 * Return the a: scope variable.
6228 * Return NULL if there is no current funccal.
6229 */
6230 dictitem_T *
6231get_funccal_args_var()
6232{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006233 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006234 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01006235 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006236}
6237
6238/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006239 * List function variables, if there is a function.
6240 */
6241 void
6242list_func_vars(int *first)
6243{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006244 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006245 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006246 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006247}
6248
6249/*
6250 * If "ht" is the hashtable for local variables in the current funccal, return
6251 * the dict that contains it.
6252 * Otherwise return NULL.
6253 */
6254 dict_T *
6255get_current_funccal_dict(hashtab_T *ht)
6256{
6257 if (current_funccal != NULL
6258 && ht == &current_funccal->l_vars.dv_hashtab)
6259 return &current_funccal->l_vars;
6260 return NULL;
6261}
6262
6263/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006264 * Search hashitem in parent scope.
6265 */
6266 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006267find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006268{
6269 funccall_T *old_current_funccal = current_funccal;
6270 hashtab_T *ht;
6271 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006272 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006273
6274 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
6275 return NULL;
6276
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006277 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006278 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006279 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006280 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006281 ht = find_var_ht(name, &varname);
6282 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006283 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006284 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006285 if (!HASHITEM_EMPTY(hi))
6286 {
6287 *pht = ht;
6288 break;
6289 }
6290 }
6291 if (current_funccal == current_funccal->func->uf_scoped)
6292 break;
6293 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006294 }
6295 current_funccal = old_current_funccal;
6296
6297 return hi;
6298}
6299
6300/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006301 * Search variable in parent scope.
6302 */
6303 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006304find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006305{
6306 dictitem_T *v = NULL;
6307 funccall_T *old_current_funccal = current_funccal;
6308 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006309 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006310
6311 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
6312 return NULL;
6313
Bram Moolenaare38eab22019-12-05 21:50:01 +01006314 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006315 current_funccal = current_funccal->func->uf_scoped;
6316 while (current_funccal)
6317 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006318 ht = find_var_ht(name, &varname);
6319 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006320 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006321 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006322 if (v != NULL)
6323 break;
6324 }
6325 if (current_funccal == current_funccal->func->uf_scoped)
6326 break;
6327 current_funccal = current_funccal->func->uf_scoped;
6328 }
6329 current_funccal = old_current_funccal;
6330
6331 return v;
6332}
6333
6334/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006335 * Set "copyID + 1" in previous_funccal and callers.
6336 */
6337 int
6338set_ref_in_previous_funccal(int copyID)
6339{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006340 funccall_T *fc;
6341
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006342 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006343 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006344 fc->fc_copyID = copyID + 1;
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006345 if (set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
6346 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
6347 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL))
6348 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006349 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006350 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006351}
6352
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006353 static int
6354set_ref_in_funccal(funccall_T *fc, int copyID)
6355{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006356 if (fc->fc_copyID != copyID)
6357 {
6358 fc->fc_copyID = copyID;
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006359 if (set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
6360 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
6361 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
6362 || set_ref_in_func(NULL, fc->func, copyID))
6363 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006364 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006365 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006366}
6367
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006368/*
6369 * Set "copyID" in all local vars and arguments in the call stack.
6370 */
6371 int
6372set_ref_in_call_stack(int copyID)
6373{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006374 funccall_T *fc;
6375 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006376
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006377 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6378 if (set_ref_in_funccal(fc, copyID))
6379 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006380
6381 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006382 for (entry = funccal_stack; entry != NULL; entry = entry->next)
6383 for (fc = entry->top_funccal; fc != NULL; fc = fc->caller)
6384 if (set_ref_in_funccal(fc, copyID))
6385 return TRUE;
6386 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006387}
6388
6389/*
6390 * Set "copyID" in all functions available by name.
6391 */
6392 int
6393set_ref_in_functions(int copyID)
6394{
6395 int todo;
6396 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006397 ufunc_T *fp;
6398
6399 todo = (int)func_hashtab.ht_used;
6400 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006401 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006402 if (!HASHITEM_EMPTY(hi))
6403 {
6404 --todo;
6405 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006406 if (!func_name_refcount(fp->uf_name)
6407 && set_ref_in_func(NULL, fp, copyID))
6408 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006409 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006410 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006411 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006412}
6413
6414/*
6415 * Set "copyID" in all function arguments.
6416 */
6417 int
6418set_ref_in_func_args(int copyID)
6419{
6420 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006421
6422 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006423 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
6424 copyID, NULL, NULL))
6425 return TRUE;
6426 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006427}
6428
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006429/*
6430 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006431 * Returns TRUE if setting references failed somehow.
6432 */
6433 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006434set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006435{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006436 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006437 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01006438 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006439 char_u fname_buf[FLEN_FIXED + 1];
6440 char_u *tofree = NULL;
6441 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006442 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006443
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006444 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006445 return FALSE;
6446
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006447 if (fp_in == NULL)
6448 {
6449 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006450 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006451 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006452 if (fp != NULL)
6453 {
6454 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006455 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006456 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02006457
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006458 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006459 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006460}
6461
Bram Moolenaare38eab22019-12-05 21:50:01 +01006462#endif // FEAT_EVAL