blob: a2270310196e2451a391dbe65a5a1bcc280972a8 [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 Moolenaara9b579f2016-07-17 18:29:19 +02001731 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001732 static int
1733get_func_arguments(
1734 char_u **arg,
1735 evalarg_T *evalarg,
1736 int partial_argc,
1737 typval_T *argvars,
1738 int *argcount)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001739{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001740 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001741 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001742 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001743 int evaluate = evalarg == NULL
1744 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001745
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001746 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001747 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001748 // skip the '(' or ',' and possibly line breaks
1749 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1750
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001751 if (*argp == ')' || *argp == ',' || *argp == NUL)
1752 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001753 if (eval1(&argp, &argvars[*argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001754 {
1755 ret = FAIL;
1756 break;
1757 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001758 ++*argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001759 // The comma should come right after the argument, but this wasn't
1760 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001761 if (vim9script)
1762 {
1763 if (*argp != ',' && *skipwhite(argp) == ',')
1764 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001765 if (evaluate)
1766 semsg(_(e_no_white_space_allowed_before_str_str),
1767 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001768 ret = FAIL;
1769 break;
1770 }
1771 }
1772 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001773 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001774 if (*argp != ',')
1775 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001776 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
1777 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001778 if (evaluate)
1779 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001780 ret = FAIL;
1781 break;
1782 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001783 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001784
Bram Moolenaare6b53242020-07-01 17:28:33 +02001785 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001786 if (*argp == ')')
1787 ++argp;
1788 else
1789 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001790 *arg = argp;
1791 return ret;
1792}
1793
1794/*
1795 * Call a function and put the result in "rettv".
1796 * Return OK or FAIL.
1797 */
1798 int
1799get_func_tv(
1800 char_u *name, // name of the function
1801 int len, // length of "name" or -1 to use strlen()
1802 typval_T *rettv,
1803 char_u **arg, // argument, pointing to the '('
1804 evalarg_T *evalarg, // for line continuation
1805 funcexe_T *funcexe) // various values
1806{
1807 char_u *argp;
1808 int ret = OK;
1809 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1810 int argcount = 0; // number of arguments found
1811 int vim9script = in_vim9script();
1812 int evaluate = evalarg == NULL
1813 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
1814
1815 argp = *arg;
1816 ret = get_func_arguments(&argp, evalarg,
1817 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
1818 argvars, &argcount);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001819
1820 if (ret == OK)
1821 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001822 int i = 0;
1823 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001824
1825 if (get_vim_var_nr(VV_TESTING))
1826 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001827 // Prepare for calling test_garbagecollect_now(), need to know
1828 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001829 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001830 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001831 for (i = 0; i < argcount; ++i)
1832 if (ga_grow(&funcargs, 1) == OK)
1833 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1834 &argvars[i];
1835 }
1836
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001837 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00001838 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001839 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001840 // An error in a builtin function does not return FAIL, but we do
1841 // want to abort further processing if an error was given.
1842 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001843 clear_tv(rettv);
1844 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001845
1846 funcargs.ga_len -= i;
1847 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001848 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001849 {
1850 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001851 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001852 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001853 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001854 }
1855
1856 while (--argcount >= 0)
1857 clear_tv(&argvars[argcount]);
1858
Bram Moolenaar4525a572022-02-13 11:57:33 +00001859 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02001860 *arg = argp;
1861 else
1862 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001863 return ret;
1864}
1865
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001866/*
1867 * Return TRUE if "p" starts with "<SID>" or "s:".
1868 * Only works if eval_fname_script() returned non-zero for "p"!
1869 */
1870 static int
1871eval_fname_sid(char_u *p)
1872{
1873 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
1874}
1875
1876/*
1877 * In a script change <SID>name() and s:name() to K_SNR 123_name().
1878 * Change <SNR>123_name() to K_SNR 123_name().
1879 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
1880 * (slow).
1881 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001882 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001883fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
1884{
1885 int llen;
1886 char_u *fname;
1887 int i;
1888
1889 llen = eval_fname_script(name);
1890 if (llen > 0)
1891 {
1892 fname_buf[0] = K_SPECIAL;
1893 fname_buf[1] = KS_EXTRA;
1894 fname_buf[2] = (int)KE_SNR;
1895 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001896 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001897 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001898 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001899 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001900 else
1901 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001902 sprintf((char *)fname_buf + 3, "%ld_",
1903 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001904 i = (int)STRLEN(fname_buf);
1905 }
1906 }
1907 if (i + STRLEN(name + llen) < FLEN_FIXED)
1908 {
1909 STRCPY(fname_buf + i, name + llen);
1910 fname = fname_buf;
1911 }
1912 else
1913 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001914 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001915 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +01001916 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001917 else
1918 {
1919 *tofree = fname;
1920 mch_memmove(fname, fname_buf, (size_t)i);
1921 STRCPY(fname + i, name + llen);
1922 }
1923 }
1924 }
1925 else
1926 fname = name;
1927 return fname;
1928}
1929
1930/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001931 * Concatenate the script ID and function name into "<SNR>99_name".
1932 * "buffer" must have size MAX_FUNC_NAME_LEN.
1933 */
1934 void
1935func_name_with_sid(char_u *name, int sid, char_u *buffer)
1936{
1937 // A script-local function is stored as "<SNR>99_name".
1938 buffer[0] = K_SPECIAL;
1939 buffer[1] = KS_EXTRA;
1940 buffer[2] = (int)KE_SNR;
1941 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
1942 (long)sid, name);
1943}
1944
1945/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001946 * Find a function "name" in script "sid".
1947 */
1948 static ufunc_T *
1949find_func_with_sid(char_u *name, int sid)
1950{
Bram Moolenaarb8822442022-01-11 15:24:05 +00001951 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001952 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001953
Bram Moolenaarb8822442022-01-11 15:24:05 +00001954 if (!SCRIPT_ID_VALID(sid))
1955 return NULL; // not in a script
1956
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001957 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001958 hi = hash_find(&func_hashtab, buffer);
1959 if (!HASHITEM_EMPTY(hi))
1960 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00001961 return NULL;
1962}
1963
1964/*
1965 * Find a function "name" in script "sid" prefixing the autoload prefix.
1966 */
1967 static ufunc_T *
1968find_func_with_prefix(char_u *name, int sid)
1969{
1970 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001971 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00001972 scriptitem_T *si;
1973
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001974 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00001975 return NULL; // already has the prefix
1976 if (!SCRIPT_ID_VALID(sid))
1977 return NULL; // not in a script
1978 si = SCRIPT_ITEM(sid);
1979 if (si->sn_autoload_prefix != NULL)
1980 {
1981 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
1982 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00001983 char_u *namep;
1984
1985 // skip a "<SNR>99_" prefix
1986 namep = untrans_function_name(name);
1987 if (namep == NULL)
1988 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00001989
1990 // An exported function in an autoload script is stored as
1991 // "dir#path#name".
1992 if (len < sizeof(buffer))
1993 auto_name = buffer;
1994 else
1995 auto_name = alloc(len);
1996 if (auto_name != NULL)
1997 {
1998 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00001999 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002000 hi = hash_find(&func_hashtab, auto_name);
2001 if (auto_name != buffer)
2002 vim_free(auto_name);
2003 if (!HASHITEM_EMPTY(hi))
2004 return HI2UF(hi);
2005 }
2006 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007
2008 return NULL;
2009}
2010
2011/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002012 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002013 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2014 * functions.
2015 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002016 * Return NULL for unknown function.
2017 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002018 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002019find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002020{
2021 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002024 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002026 // Find script-local function before global one.
2027 if (in_vim9script() && eval_isnamec1(*name)
2028 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002030 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2031 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002032 if (func != NULL)
2033 return func;
2034 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002035 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2036 {
2037 char_u *p = name + 5;
2038 long sid;
2039
2040 // printable "<SNR>123_Name" form
2041 sid = getdigits(&p);
2042 if (*p == '_')
2043 {
2044 func = find_func_with_sid(p + 1, (int)sid);
2045 if (func != NULL)
2046 return func;
2047 }
2048 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002049 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002050
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002051 if ((flags & FFED_NO_GLOBAL) == 0)
2052 {
2053 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002054 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002055 if (!HASHITEM_EMPTY(hi))
2056 return HI2UF(hi);
2057 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058
Bram Moolenaarb8822442022-01-11 15:24:05 +00002059 // Find autoload function if this is an autoload script.
2060 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2061 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002062}
2063
2064/*
2065 * Find a function by name, return pointer to it in ufuncs.
2066 * "cctx" is passed in a :def function to find imported functions.
2067 * Return NULL for unknown or dead function.
2068 */
2069 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002070find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002072 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002073
2074 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2075 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002076 return NULL;
2077}
2078
2079/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002080 * Return TRUE if "ufunc" is a global function.
2081 */
2082 int
2083func_is_global(ufunc_T *ufunc)
2084{
2085 return ufunc->uf_name[0] != K_SPECIAL;
2086}
2087
2088/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002089 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2090 */
2091 int
2092func_requires_g_prefix(ufunc_T *ufunc)
2093{
2094 return ufunc->uf_name[0] != K_SPECIAL
2095 && (ufunc->uf_flags & FC_LAMBDA) == 0
2096 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL;
2097}
2098
2099/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002100 * Copy the function name of "fp" to buffer "buf".
2101 * "buf" must be able to hold the function name plus three bytes.
2102 * Takes care of script-local function names.
2103 */
2104 static void
2105cat_func_name(char_u *buf, ufunc_T *fp)
2106{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002107 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002108 {
2109 STRCPY(buf, "<SNR>");
2110 STRCAT(buf, fp->uf_name + 3);
2111 }
2112 else
2113 STRCPY(buf, fp->uf_name);
2114}
2115
2116/*
2117 * Add a number variable "name" to dict "dp" with value "nr".
2118 */
2119 static void
2120add_nr_var(
2121 dict_T *dp,
2122 dictitem_T *v,
2123 char *name,
2124 varnumber_T nr)
2125{
2126 STRCPY(v->di_key, name);
2127 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2128 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
2129 v->di_tv.v_type = VAR_NUMBER;
2130 v->di_tv.v_lock = VAR_FIXED;
2131 v->di_tv.vval.v_number = nr;
2132}
2133
2134/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002135 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002136 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002137 static void
2138free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002139{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002140 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002141
2142 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
2143 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002144 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002145
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002146 // When garbage collecting a funccall_T may be freed before the
2147 // function that references it, clear its uf_scoped field.
2148 // The function may have been redefined and point to another
2149 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002150 if (fp != NULL && fp->uf_scoped == fc)
2151 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002152 }
Bram Moolenaar58016442016-07-31 18:30:22 +02002153 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002154
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002155 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002156 vim_free(fc);
2157}
2158
2159/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002160 * Free "fc" and what it contains.
2161 * Can be called only when "fc" is kept beyond the period of it called,
2162 * i.e. after cleanup_function_call(fc).
2163 */
2164 static void
2165free_funccal_contents(funccall_T *fc)
2166{
2167 listitem_T *li;
2168
2169 // Free all l: variables.
2170 vars_clear(&fc->l_vars.dv_hashtab);
2171
2172 // Free all a: variables.
2173 vars_clear(&fc->l_avars.dv_hashtab);
2174
2175 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002176 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002177 clear_tv(&li->li_tv);
2178
2179 free_funccal(fc);
2180}
2181
2182/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002183 * Handle the last part of returning from a function: free the local hashtable.
2184 * Unless it is still in use by a closure.
2185 */
2186 static void
2187cleanup_function_call(funccall_T *fc)
2188{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002189 int may_free_fc = fc->fc_refcount <= 0;
2190 int free_fc = TRUE;
2191
Bram Moolenaar6914c642017-04-01 21:21:30 +02002192 current_funccal = fc->caller;
2193
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002194 // Free all l: variables if not referred.
2195 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
2196 vars_clear(&fc->l_vars.dv_hashtab);
2197 else
2198 free_fc = FALSE;
2199
2200 // If the a:000 list and the l: and a: dicts are not referenced and
2201 // there is no closure using it, we can free the funccall_T and what's
2202 // in it.
2203 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
2204 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002205 else
2206 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002207 int todo;
2208 hashitem_T *hi;
2209 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002210
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002211 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002212
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002213 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +02002214 todo = (int)fc->l_avars.dv_hashtab.ht_used;
2215 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
2216 {
2217 if (!HASHITEM_EMPTY(hi))
2218 {
2219 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002220 di = HI2DI(hi);
2221 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002222 }
2223 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002224 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002225
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002226 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2227 fc->l_varlist.lv_first = NULL;
2228 else
2229 {
2230 listitem_T *li;
2231
2232 free_fc = FALSE;
2233
2234 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002235 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002236 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002237 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002238
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002239 if (free_fc)
2240 free_funccal(fc);
2241 else
2242 {
2243 static int made_copy = 0;
2244
2245 // "fc" is still in use. This can happen when returning "a:000",
2246 // assigning "l:" to a global variable or defining a closure.
2247 // Link "fc" in the list for garbage collection later.
2248 fc->caller = previous_funccal;
2249 previous_funccal = fc;
2250
2251 if (want_garbage_collect)
2252 // If garbage collector is ready, clear count.
2253 made_copy = 0;
2254 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002255 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002256 // We have made a lot of copies, worth 4 Mbyte. This can happen
2257 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002258 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002259 // too much memory.
2260 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002261 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002262 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002263 }
2264}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002265
2266/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002267 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2268 */
2269 static int
2270numbered_function(char_u *name)
2271{
2272 return isdigit(*name)
2273 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2274}
2275
2276/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002277 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002278 * 1. ordinary names, function defined with :function or :def;
2279 * can start with "<SNR>123_" literally or with K_SPECIAL.
2280 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002281 * For the first we only count the name stored in func_hashtab as a reference,
2282 * using function() does not count as a reference, because the function is
2283 * looked up by name.
2284 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002285 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002286func_name_refcount(char_u *name)
2287{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002288 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002289}
2290
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002291/*
2292 * Unreference "fc": decrement the reference count and free it when it
2293 * becomes zero. "fp" is detached from "fc".
2294 * When "force" is TRUE we are exiting.
2295 */
2296 static void
2297funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2298{
2299 funccall_T **pfc;
2300 int i;
2301
2302 if (fc == NULL)
2303 return;
2304
2305 if (--fc->fc_refcount <= 0 && (force || (
2306 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
2307 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
2308 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2309 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
2310 {
2311 if (fc == *pfc)
2312 {
2313 *pfc = fc->caller;
2314 free_funccal_contents(fc);
2315 return;
2316 }
2317 }
2318 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
2319 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
2320 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
2321}
2322
2323/*
2324 * Remove the function from the function hashtable. If the function was
2325 * deleted while it still has references this was already done.
2326 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2327 */
2328 static int
2329func_remove(ufunc_T *fp)
2330{
2331 hashitem_T *hi;
2332
2333 // Return if it was already virtually deleted.
2334 if (fp->uf_flags & FC_DEAD)
2335 return FALSE;
2336
2337 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
2338 if (!HASHITEM_EMPTY(hi))
2339 {
2340 // When there is a def-function index do not actually remove the
2341 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002342 // Do remove it when it's a copy.
2343 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaarc970e422021-03-17 15:03:04 +01002344 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01002346 return FALSE;
2347 }
2348 hash_remove(&func_hashtab, hi);
2349 fp->uf_flags |= FC_DELETED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002350 return TRUE;
2351 }
2352 return FALSE;
2353}
2354
2355 static void
2356func_clear_items(ufunc_T *fp)
2357{
2358 ga_clear_strings(&(fp->uf_args));
2359 ga_clear_strings(&(fp->uf_def_args));
2360 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002362 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002363 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002364 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002365
2366 // Increment the refcount of this function to avoid it being freed
2367 // recursively when the partial is freed.
2368 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002369 partial_unref(fp->uf_partial);
2370 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002371 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002372
2373#ifdef FEAT_LUA
2374 if (fp->uf_cb_free != NULL)
2375 {
2376 fp->uf_cb_free(fp->uf_cb_state);
2377 fp->uf_cb_free = NULL;
2378 }
2379
2380 fp->uf_cb_state = NULL;
2381 fp->uf_cb = NULL;
2382#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383#ifdef FEAT_PROFILE
2384 VIM_CLEAR(fp->uf_tml_count);
2385 VIM_CLEAR(fp->uf_tml_total);
2386 VIM_CLEAR(fp->uf_tml_self);
2387#endif
2388}
2389
2390/*
2391 * Free all things that a function contains. Does not free the function
2392 * itself, use func_free() for that.
2393 * When "force" is TRUE we are exiting.
2394 */
2395 static void
2396func_clear(ufunc_T *fp, int force)
2397{
2398 if (fp->uf_cleared)
2399 return;
2400 fp->uf_cleared = TRUE;
2401
2402 // clear this function
2403 func_clear_items(fp);
2404 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002405 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406}
2407
2408/*
2409 * Free a function and remove it from the list of functions. Does not free
2410 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002411 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002412 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002414 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002415func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416{
2417 // Only remove it when not done already, otherwise we would remove a newer
2418 // version of the function with the same name.
2419 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2420 func_remove(fp);
2421
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002422 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002423 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002424 if (fp->uf_dfunc_idx > 0)
2425 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002426 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002427 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002428 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002429 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002430 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002431}
2432
2433/*
2434 * Free all things that a function contains and free the function itself.
2435 * When "force" is TRUE we are exiting.
2436 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002437 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438func_clear_free(ufunc_T *fp, int force)
2439{
2440 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002441 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2442 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002443 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002444 else
2445 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446}
2447
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002448/*
2449 * Copy already defined function "lambda" to a new function with name "global".
2450 * This is for when a compiled function defines a global function.
2451 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002452 int
2453copy_func(char_u *lambda, char_u *global, ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002454{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002455 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002456 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002457
2458 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002459 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002460 semsg(_(e_lambda_function_not_found_str), lambda);
2461 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002462 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002463
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002464 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002465 if (fp != NULL)
2466 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002467 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002468 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002469 return FAIL;
2470 }
2471
2472 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
2473 if (fp == NULL)
2474 return FAIL;
2475
2476 fp->uf_varargs = ufunc->uf_varargs;
2477 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2478 fp->uf_def_status = ufunc->uf_def_status;
2479 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2480 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2481 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2482 == FAIL
2483 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2484 goto failed;
2485
2486 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
2487 : vim_strsave(ufunc->uf_name_exp);
2488 if (ufunc->uf_arg_types != NULL)
2489 {
2490 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2491 if (fp->uf_arg_types == NULL)
2492 goto failed;
2493 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2494 sizeof(type_T *) * fp->uf_args.ga_len);
2495 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002496 if (ufunc->uf_va_name != NULL)
2497 {
2498 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2499 if (fp->uf_va_name == NULL)
2500 goto failed;
2501 }
2502 fp->uf_ret_type = ufunc->uf_ret_type;
2503
2504 fp->uf_refcount = 1;
2505 STRCPY(fp->uf_name, global);
2506 hash_add(&func_hashtab, UF2HIKEY(fp));
2507
2508 // the referenced dfunc_T is now used one more time
2509 link_def_function(fp);
2510
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002511 // Create a partial to store the context of the function where it was
2512 // instantiated. Only needs to be done once. Do this on the original
2513 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002514 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2515 {
2516 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2517
2518 if (pt == NULL)
2519 goto failed;
2520 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002521 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002522 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002523 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002524 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002525 ufunc->uf_partial = pt;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002526 --pt->pt_refcount; // not actually referenced here
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002527 }
2528
2529 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002530
2531failed:
2532 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002533 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002534}
2535
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002536static int funcdepth = 0;
2537
2538/*
2539 * Increment the function call depth count.
2540 * Return FAIL when going over 'maxfuncdepth'.
2541 * Otherwise return OK, must call funcdepth_decrement() later!
2542 */
2543 int
2544funcdepth_increment(void)
2545{
2546 if (funcdepth >= p_mfd)
2547 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002548 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002549 return FAIL;
2550 }
2551 ++funcdepth;
2552 return OK;
2553}
2554
2555 void
2556funcdepth_decrement(void)
2557{
2558 --funcdepth;
2559}
2560
2561/*
2562 * Get the current function call depth.
2563 */
2564 int
2565funcdepth_get(void)
2566{
2567 return funcdepth;
2568}
2569
2570/*
2571 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002572 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002573 * times as funcdepth_increment().
2574 */
2575 void
2576funcdepth_restore(int depth)
2577{
2578 funcdepth = depth;
2579}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002580
2581/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002582 * Call a user function.
2583 */
2584 static void
2585call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002586 ufunc_T *fp, // pointer to function
2587 int argcount, // nr of args
2588 typval_T *argvars, // arguments
2589 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002590 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002591 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002592{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002593 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002594 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002595 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002596 funccall_T *fc;
2597 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002598 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002599 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002600 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002601 int i;
2602 int ai;
2603 int islambda = FALSE;
2604 char_u numbuf[NUMBUFLEN];
2605 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002606 typval_T *tv_to_free[MAX_FUNC_ARGS];
2607 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002608#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002609 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002610#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01002611 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002612
Bram Moolenaarb2049902021-01-24 12:53:53 +01002613#ifdef FEAT_PROFILE
2614 CLEAR_FIELD(profile_info);
2615#endif
2616
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002617 // If depth of calling is getting too high, don't execute the function.
2618 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002619 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002620 rettv->v_type = VAR_NUMBER;
2621 rettv->vval.v_number = -1;
2622 return;
2623 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002624
Bram Moolenaare38eab22019-12-05 21:50:01 +01002625 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002626
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002627 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002628 if (fc == NULL)
2629 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002630 fc->caller = current_funccal;
2631 current_funccal = fc;
2632 fc->func = fp;
2633 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002634 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002635 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002636 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2637 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002638 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002639 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002640 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002641
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002642 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002644#ifdef FEAT_PROFILE
2645 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2646#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002647 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002648#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002649 if (do_profiling == PROF_YES)
2650 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002651#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002652 sticky_cmdmod_flags = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002653 call_def_function(fp, argcount, argvars, funcexe->fe_partial, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002654 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002655#ifdef FEAT_PROFILE
2656 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002657 || (caller != NULL && caller->uf_profiling)))
2658 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002659#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 current_funccal = fc->caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 free_funccal(fc);
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002662 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663 return;
2664 }
2665
Bram Moolenaar38453522021-11-28 22:00:12 +00002666 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002667
2668 /*
2669 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
2670 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
2671 * each argument variable and saves a lot of time.
2672 */
2673 /*
2674 * Init l: variables.
2675 */
2676 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
2677 if (selfdict != NULL)
2678 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002679 // Set l:self to "selfdict". Use "name" to avoid a warning from
2680 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002681 v = &fc->fixvar[fixvar_idx++].var;
2682 name = v->di_key;
2683 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002684 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002685 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
2686 v->di_tv.v_type = VAR_DICT;
2687 v->di_tv.v_lock = 0;
2688 v->di_tv.vval.v_dict = selfdict;
2689 ++selfdict->dv_refcount;
2690 }
2691
2692 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002693 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002694 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002695 * Set a:000 to a list with room for the "..." arguments.
2696 */
2697 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002698 if ((fp->uf_flags & FC_NOARGS) == 0)
2699 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002700 (varnumber_T)(argcount >= fp->uf_args.ga_len
2701 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01002702 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002703 if ((fp->uf_flags & FC_NOARGS) == 0)
2704 {
2705 // Use "name" to avoid a warning from some compiler that checks the
2706 // destination size.
2707 v = &fc->fixvar[fixvar_idx++].var;
2708 name = v->di_key;
2709 STRCPY(name, "000");
2710 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2711 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
2712 v->di_tv.v_type = VAR_LIST;
2713 v->di_tv.v_lock = VAR_FIXED;
2714 v->di_tv.vval.v_list = &fc->l_varlist;
2715 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02002716 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002717 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2718 fc->l_varlist.lv_lock = VAR_FIXED;
2719
2720 /*
2721 * Set a:firstline to "firstline" and a:lastline to "lastline".
2722 * Set a:name to named arguments.
2723 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002724 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002725 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002726 if ((fp->uf_flags & FC_NOARGS) == 0)
2727 {
2728 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002729 (varnumber_T)funcexe->fe_firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002730 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002731 (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002732 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002733 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002734 {
2735 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002736 typval_T def_rettv;
2737 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002738
2739 ai = i - fp->uf_args.ga_len;
2740 if (ai < 0)
2741 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002742 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002743 name = FUNCARG(fp, i);
2744 if (islambda)
2745 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002746
2747 // evaluate named argument default expression
2748 isdefault = ai + fp->uf_def_args.ga_len >= 0
2749 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2750 && argvars[i].vval.v_number == VVAL_NONE));
2751 if (isdefault)
2752 {
2753 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002754
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002755 def_rettv.v_type = VAR_NUMBER;
2756 def_rettv.vval.v_number = -1;
2757
2758 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2759 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002760 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002761 {
2762 default_arg_err = 1;
2763 break;
2764 }
2765 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002766 }
2767 else
2768 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002769 if ((fp->uf_flags & FC_NOARGS) != 0)
2770 // Bail out if no a: arguments used (in lambda).
2771 break;
2772
Bram Moolenaare38eab22019-12-05 21:50:01 +01002773 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002774 sprintf((char *)numbuf, "%d", ai + 1);
2775 name = numbuf;
2776 }
2777 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2778 {
2779 v = &fc->fixvar[fixvar_idx++].var;
2780 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002781 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002782 }
2783 else
2784 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002785 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002786 if (v == NULL)
2787 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002788 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002789 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002790
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002791 // Note: the values are copied directly to avoid alloc/free.
2792 // "argvars" must have VAR_FIXED for v_lock.
2793 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002794 v->di_tv.v_lock = VAR_FIXED;
2795
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002796 if (isdefault)
2797 // Need to free this later, no matter where it's stored.
2798 tv_to_free[tv_to_free_len++] = &v->di_tv;
2799
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002800 if (addlocal)
2801 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002802 // Named arguments should be accessed without the "a:" prefix in
2803 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002804 copy_tv(&v->di_tv, &v->di_tv);
2805 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002806 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002807 else
2808 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002809
2810 if (ai >= 0 && ai < MAX_FUNC_ARGS)
2811 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002812 listitem_T *li = &fc->l_listitems[ai];
2813
2814 li->li_tv = argvars[i];
2815 li->li_tv.v_lock = VAR_FIXED;
2816 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002817 }
2818 }
2819
Bram Moolenaare38eab22019-12-05 21:50:01 +01002820 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002821 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02002822
2823 if (fp->uf_flags & FC_SANDBOX)
2824 {
2825 using_sandbox = TRUE;
2826 ++sandbox;
2827 }
2828
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002829 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002830 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002831 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002832 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002833 ++no_wait_return;
2834 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002835
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002836 smsg(_("calling %s"), SOURCING_NAME);
2837 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002838 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002839 char_u buf[MSG_BUF_LEN];
2840 char_u numbuf2[NUMBUFLEN];
2841 char_u *tofree;
2842 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002843
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002844 msg_puts("(");
2845 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002846 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002847 if (i > 0)
2848 msg_puts(", ");
2849 if (argvars[i].v_type == VAR_NUMBER)
2850 msg_outnum((long)argvars[i].vval.v_number);
2851 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002852 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002853 // Do not want errors such as E724 here.
2854 ++emsg_off;
2855 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
2856 --emsg_off;
2857 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002858 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002859 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002860 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002861 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2862 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002863 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002864 msg_puts((char *)s);
2865 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002866 }
2867 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002868 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002869 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002870 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002871 msg_puts("\n"); // don't overwrite this either
2872
2873 verbose_leave_scroll();
2874 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002875 }
2876#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002877 if (do_profiling == PROF_YES)
2878 profile_may_start_func(&profile_info, fp,
2879 fc->caller == NULL ? NULL : fc->caller->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002880#endif
2881
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002882 // "legacy" does not apply to commands in the function
2883 sticky_cmdmod_flags = 0;
2884
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002885 save_current_sctx = current_sctx;
2886 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002887 save_did_emsg = did_emsg;
2888 did_emsg = FALSE;
2889
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002890 if (default_arg_err && (fp->uf_flags & FC_ABORT))
2891 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002892 else if (islambda)
2893 {
2894 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
2895
2896 // A Lambda always has the command "return {expr}". It is much faster
2897 // to evaluate {expr} directly.
2898 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002899 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002900 --ex_nesting_level;
2901 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002902 else
2903 // call do_cmdline() to execute the lines
2904 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002905 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
2906
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002907 // Invoke functions added with ":defer".
2908 handle_defer();
2909
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002910 --RedrawingDisabled;
2911
Bram Moolenaare38eab22019-12-05 21:50:01 +01002912 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002913 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
2914 {
2915 clear_tv(rettv);
2916 rettv->v_type = VAR_NUMBER;
2917 rettv->vval.v_number = -1;
2918 }
2919
2920#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002921 if (do_profiling == PROF_YES)
2922 {
2923 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2924
2925 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
2926 profile_may_end_func(&profile_info, fp, caller);
2927 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002928#endif
2929
Bram Moolenaare38eab22019-12-05 21:50:01 +01002930 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002931 if (p_verbose >= 12)
2932 {
2933 ++no_wait_return;
2934 verbose_enter_scroll();
2935
2936 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002937 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002938 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002939 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002940 (long)fc->rettv->vval.v_number);
2941 else
2942 {
2943 char_u buf[MSG_BUF_LEN];
2944 char_u numbuf2[NUMBUFLEN];
2945 char_u *tofree;
2946 char_u *s;
2947
Bram Moolenaare38eab22019-12-05 21:50:01 +01002948 // The value may be very long. Skip the middle part, so that we
2949 // have some idea how it starts and ends. smsg() would always
2950 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002951 ++emsg_off;
2952 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
2953 --emsg_off;
2954 if (s != NULL)
2955 {
2956 if (vim_strsize(s) > MSG_BUF_CLEN)
2957 {
2958 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2959 s = buf;
2960 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002961 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002962 vim_free(tofree);
2963 }
2964 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002965 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002966
2967 verbose_leave_scroll();
2968 --no_wait_return;
2969 }
2970
Bram Moolenaare31ee862020-01-07 20:59:34 +01002971 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002972 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002973 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002974#ifdef FEAT_PROFILE
2975 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002976 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002977#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02002978 if (using_sandbox)
2979 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002980 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002981
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002982 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002983 {
2984 ++no_wait_return;
2985 verbose_enter_scroll();
2986
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002987 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002988 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002989
2990 verbose_leave_scroll();
2991 --no_wait_return;
2992 }
2993
2994 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002995 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002996 for (i = 0; i < tv_to_free_len; ++i)
2997 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002998 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002999}
3000
3001/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003002 * Check the argument count for user function "fp".
3003 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3004 */
3005 int
3006check_user_func_argcount(ufunc_T *fp, int argcount)
3007{
3008 int regular_args = fp->uf_args.ga_len;
3009
3010 if (argcount < regular_args - fp->uf_def_args.ga_len)
3011 return FCERR_TOOFEW;
3012 else if (!has_varargs(fp) && argcount > regular_args)
3013 return FCERR_TOOMANY;
3014 return FCERR_UNKNOWN;
3015}
3016
3017/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003019 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 int
3021call_user_func_check(
3022 ufunc_T *fp,
3023 int argcount,
3024 typval_T *argvars,
3025 typval_T *rettv,
3026 funcexe_T *funcexe,
3027 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003028{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003030
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003031#ifdef FEAT_LUA
3032 if (fp->uf_flags & FC_CFUNC)
3033 {
3034 cfunc_T cb = fp->uf_cb;
3035
3036 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3037 }
3038#endif
3039
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003040 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3041 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003042 error = check_user_func_argcount(fp, argcount);
3043 if (error != FCERR_UNKNOWN)
3044 return error;
3045 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 error = FCERR_DICT;
3047 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003048 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 int did_save_redo = FALSE;
3050 save_redo_T save_redo;
3051
3052 /*
3053 * Call the user function.
3054 * Save and restore search patterns, script variables and
3055 * redo buffer.
3056 */
3057 save_search_patterns();
3058 if (!ins_compl_active())
3059 {
3060 saveRedobuff(&save_redo);
3061 did_save_redo = TRUE;
3062 }
3063 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003064 call_user_func(fp, argcount, argvars, rettv, funcexe,
3065 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3067 // Function was unreferenced while being used, free it now.
3068 func_clear_free(fp, FALSE);
3069 if (did_save_redo)
3070 restoreRedobuff(&save_redo);
3071 restore_search_patterns();
3072 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003073 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003075}
3076
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003077static funccal_entry_T *funccal_stack = NULL;
3078
3079/*
3080 * Save the current function call pointer, and set it to NULL.
3081 * Used when executing autocommands and for ":source".
3082 */
3083 void
3084save_funccal(funccal_entry_T *entry)
3085{
3086 entry->top_funccal = current_funccal;
3087 entry->next = funccal_stack;
3088 funccal_stack = entry;
3089 current_funccal = NULL;
3090}
3091
3092 void
3093restore_funccal(void)
3094{
3095 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003096 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003097 else
3098 {
3099 current_funccal = funccal_stack->top_funccal;
3100 funccal_stack = funccal_stack->next;
3101 }
3102}
3103
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003104 funccall_T *
3105get_current_funccal(void)
3106{
3107 return current_funccal;
3108}
3109
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003110/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003111 * Return TRUE when currently at the script level:
3112 * - not in a function
3113 * - not executing an autocommand
3114 * Note that when an autocommand sources a script the result is FALSE;
3115 */
3116 int
3117at_script_level(void)
3118{
3119 return current_funccal == NULL && autocmd_match == NULL;
3120}
3121
3122/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003123 * Mark all functions of script "sid" as deleted.
3124 */
3125 void
3126delete_script_functions(int sid)
3127{
3128 hashitem_T *hi;
3129 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003130 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003131 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003132 size_t len;
3133
3134 buf[0] = K_SPECIAL;
3135 buf[1] = KS_EXTRA;
3136 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003137 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003138 len = STRLEN(buf);
3139
Bram Moolenaarce658352020-07-31 23:47:12 +02003140 while (todo > 0)
3141 {
3142 todo = func_hashtab.ht_used;
3143 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3144 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003145 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003146 fp = HI2UF(hi);
3147 if (STRNCMP(fp->uf_name, buf, len) == 0)
3148 {
3149 int changed = func_hashtab.ht_changed;
3150
3151 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003152
3153 if (fp->uf_calls > 0)
3154 {
3155 // Function is executing, don't free it but do remove
3156 // it from the hashtable.
3157 if (func_remove(fp))
3158 fp->uf_refcount--;
3159 }
3160 else
3161 {
3162 func_clear(fp, TRUE);
3163 // When clearing a function another function can be
3164 // cleared as a side effect. When that happens start
3165 // over.
3166 if (changed != func_hashtab.ht_changed)
3167 break;
3168 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003169 }
3170 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003171 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003172 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003173}
3174
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003175#if defined(EXITFREE) || defined(PROTO)
3176 void
3177free_all_functions(void)
3178{
3179 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003180 ufunc_T *fp;
3181 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003182 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003183 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003184
Bram Moolenaare38eab22019-12-05 21:50:01 +01003185 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003186 while (current_funccal != NULL)
3187 {
3188 clear_tv(current_funccal->rettv);
3189 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003190 if (current_funccal == NULL && funccal_stack != NULL)
3191 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003192 }
3193
Bram Moolenaare38eab22019-12-05 21:50:01 +01003194 // First clear what the functions contain. Since this may lower the
3195 // reference count of a function, it may also free a function and change
3196 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003197 while (todo > 0)
3198 {
3199 todo = func_hashtab.ht_used;
3200 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3201 if (!HASHITEM_EMPTY(hi))
3202 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 // clear the def function index now
3204 fp = HI2UF(hi);
3205 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003206 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207
Bram Moolenaare38eab22019-12-05 21:50:01 +01003208 // Only free functions that are not refcounted, those are
3209 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003210 if (func_name_refcount(fp->uf_name))
3211 ++skipped;
3212 else
3213 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003214 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003215 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003216 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003217 {
3218 skipped = 0;
3219 break;
3220 }
3221 }
3222 --todo;
3223 }
3224 }
3225
Bram Moolenaare38eab22019-12-05 21:50:01 +01003226 // Now actually free the functions. Need to start all over every time,
3227 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003228 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003229 while (func_hashtab.ht_used > skipped)
3230 {
3231 todo = func_hashtab.ht_used;
3232 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003233 if (!HASHITEM_EMPTY(hi))
3234 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003235 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003236 // Only free functions that are not refcounted, those are
3237 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003238 fp = HI2UF(hi);
3239 if (func_name_refcount(fp->uf_name))
3240 ++skipped;
3241 else
3242 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003243 if (func_free(fp, FALSE) == OK)
3244 {
3245 skipped = 0;
3246 break;
3247 }
3248 // did not actually free it
3249 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003250 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003251 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003252 }
3253 if (skipped == 0)
3254 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255
3256 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003257}
3258#endif
3259
3260/*
3261 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003262 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3263 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003264 * "len" is the length of "name", or -1 for NUL terminated.
3265 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003267builtin_function(char_u *name, int len)
3268{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003269 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003270
Bram Moolenaara26b9702020-04-18 19:53:28 +02003271 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003272 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003273 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3274 {
3275 if (name[i] == AUTOLOAD_CHAR)
3276 return FALSE;
3277 if (!eval_isnamec(name[i]))
3278 {
3279 // "name.something" is not a builtin function
3280 if (name[i] == '.')
3281 return FALSE;
3282 break;
3283 }
3284 }
3285 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003286}
3287
3288 int
3289func_call(
3290 char_u *name,
3291 typval_T *args,
3292 partial_T *partial,
3293 dict_T *selfdict,
3294 typval_T *rettv)
3295{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003296 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003297 listitem_T *item;
3298 typval_T argv[MAX_FUNC_ARGS + 1];
3299 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003300 int r = 0;
3301
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003302 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003303 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003304 {
3305 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3306 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003307 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003308 break;
3309 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003310 // Make a copy of each argument. This is needed to be able to set
3311 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003312 copy_tv(&item->li_tv, &argv[argc++]);
3313 }
3314
3315 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003316 {
3317 funcexe_T funcexe;
3318
Bram Moolenaara80faa82020-04-12 19:37:17 +02003319 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003320 funcexe.fe_firstline = curwin->w_cursor.lnum;
3321 funcexe.fe_lastline = curwin->w_cursor.lnum;
3322 funcexe.fe_evaluate = TRUE;
3323 funcexe.fe_partial = partial;
3324 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003325 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3326 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003327
Bram Moolenaare38eab22019-12-05 21:50:01 +01003328 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003329 while (argc > 0)
3330 clear_tv(&argv[--argc]);
3331
3332 return r;
3333}
3334
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003335static int callback_depth = 0;
3336
3337 int
3338get_callback_depth(void)
3339{
3340 return callback_depth;
3341}
3342
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003343/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003344 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003345 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003346 */
3347 int
3348call_callback(
3349 callback_T *callback,
3350 int len, // length of "name" or -1 to use strlen()
3351 typval_T *rettv, // return value goes here
3352 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003353 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003354 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003355{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003356 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003357 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003358
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003359 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3360 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02003361 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003362 funcexe.fe_evaluate = TRUE;
3363 funcexe.fe_partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003364 ++callback_depth;
3365 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3366 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003367
3368 // When a :def function was called that uses :try an error would be turned
3369 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003370 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003371 {
3372 need_rethrow = FALSE;
3373 handle_did_throw();
3374 }
3375
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003376 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003377}
3378
3379/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003380 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003381 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003382 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3383 */
3384 varnumber_T
3385call_callback_retnr(
3386 callback_T *callback,
3387 int argcount, // number of "argvars"
3388 typval_T *argvars) // vars for arguments, must have "argcount"
3389 // PLUS ONE elements!
3390{
3391 typval_T rettv;
3392 varnumber_T retval;
3393
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003394 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003395 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003396
3397 retval = tv_get_number_chk(&rettv, NULL);
3398 clear_tv(&rettv);
3399 return retval;
3400}
3401
3402/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403 * Give an error message for the result of a function.
3404 * Nothing if "error" is FCERR_NONE.
3405 */
3406 void
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003407user_func_error(int error, char_u *name, funcexe_T *funcexe)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408{
3409 switch (error)
3410 {
3411 case FCERR_UNKNOWN:
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003412 if (funcexe->fe_found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003413 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003414 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003415 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 break;
3417 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003418 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 break;
3420 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003421 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 break;
3423 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003424 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 break;
3426 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003427 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 break;
3429 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003430 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 break;
3432 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003433 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3434 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 break;
3436 }
3437}
3438
3439/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003440 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003441 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003442 * Return FAIL when the function can't be called, OK otherwise.
3443 * Also returns OK when an error was encountered while executing the function.
3444 */
3445 int
3446call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003447 char_u *funcname, // name of the function
3448 int len, // length of "name" or -1 to use strlen()
3449 typval_T *rettv, // return value goes here
3450 int argcount_in, // number of "argvars"
3451 typval_T *argvars_in, // vars for arguments, must have "argcount"
3452 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003453 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003454{
3455 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01003456 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003457 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003458 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003459 char_u fname_buf[FLEN_FIXED + 1];
3460 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003461 char_u *fname = NULL;
3462 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003463 int argcount = argcount_in;
3464 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003465 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003466 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003467 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003468 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003469 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003470 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003471 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003472 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003473
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003474 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3475 // even when call_func() returns FAIL.
3476 rettv->v_type = VAR_UNKNOWN;
3477
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003478 if (partial != NULL)
3479 fp = partial->pt_func;
3480 if (fp == NULL)
3481 {
3482 // Make a copy of the name, if it comes from a funcref variable it
3483 // could be changed or deleted in the called function.
3484 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3485 if (name == NULL)
3486 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003487
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003488 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3489 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003490
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003491 if (funcexe->fe_doesrange != NULL)
3492 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003493
3494 if (partial != NULL)
3495 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003496 // When the function has a partial with a dict and there is a dict
3497 // argument, use the dict argument. That is backwards compatible.
3498 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003499 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003500 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003501 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003502 {
3503 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003504 {
3505 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3506 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003507 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003508 goto theend;
3509 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003510 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003511 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003512 for (i = 0; i < argcount_in; ++i)
3513 argv[i + argv_clear] = argvars_in[i];
3514 argvars = argv;
3515 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003516
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003517 if (funcexe->fe_check_type != NULL
3518 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003519 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003520 // Now funcexe->fe_check_type is missing the added arguments,
3521 // make a copy of the type with the correction.
3522 check_type = *funcexe->fe_check_type;
3523 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003524 check_type.tt_args = check_type_args;
3525 CLEAR_FIELD(check_type_args);
3526 for (i = 0; i < check_type.tt_argcount; ++i)
3527 check_type_args[i + partial->pt_argc] =
3528 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003529 check_type.tt_argcount += partial->pt_argc;
3530 check_type.tt_min_argcount += partial->pt_argc;
3531 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003532 }
3533 }
3534
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003535 if (error == FCERR_NONE && funcexe->fe_check_type != NULL
3536 && funcexe->fe_evaluate)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003537 {
3538 // Check that the argument types are OK for the types of the funcref.
Bram Moolenaarc84287d2022-01-16 18:06:21 +00003539 if (check_argument_types(funcexe->fe_check_type,
3540 argvars, argcount, funcexe->fe_basetv,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003541 (name != NULL) ? name : funcname) == FAIL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003542 error = FCERR_OTHER;
3543 }
3544
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003545 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003546 {
3547 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003548 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003549
Bram Moolenaar333894b2020-08-01 18:53:07 +02003550 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003551 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003552 {
3553 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003554 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003555 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003556
Bram Moolenaare38eab22019-12-05 21:50:01 +01003557 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003558 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003559 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003560
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003561 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003562 {
3563 /*
3564 * User defined function.
3565 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003566 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003567 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003568 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003569 if (fp != NULL && !is_global && in_vim9script()
3570 && func_requires_g_prefix(fp))
3571 // In Vim9 script g: is required to find a global
3572 // non-autoload function.
3573 fp = NULL;
3574 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003575
Bram Moolenaare38eab22019-12-05 21:50:01 +01003576 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003577 if (fp == NULL
3578 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003579 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003580 && !aborting())
3581 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003582 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003583 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003584 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003585 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003586 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3587 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003588 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003589 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003590 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003591 if (fp == NULL)
3592 {
3593 char_u *p = untrans_function_name(rfname);
3594
3595 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003596 // Don't do this if the name starts with "s:".
3597 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003598 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003599 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003600
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003601 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003602 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003603 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003604 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003605 if (funcexe->fe_argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003606 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003607 argcount = funcexe->fe_argv_func(argcount, argvars,
3608 argv_clear, fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003609
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003610 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003611 {
3612 // Method call: base->Method()
3613 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003614 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003615 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003616 argvars = argv;
3617 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003618 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003619
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 error = call_user_func_check(fp, argcount, argvars, rettv,
3621 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003622 }
3623 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003624 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003625 {
3626 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003627 * expr->method(): Find the method name in the table, call its
3628 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003629 */
3630 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003631 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003632 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003633 else
3634 {
3635 /*
3636 * Find the function name in the table, call its implementation.
3637 */
3638 error = call_internal_func(fname, argcount, argvars, rettv);
3639 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003640
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003641 /*
3642 * The function call (or "FuncUndefined" autocommand sequence) might
3643 * have been aborted by an error, an interrupt, or an explicitly thrown
3644 * exception that has not been caught so far. This situation can be
3645 * tested for by calling aborting(). For an error in an internal
3646 * function or for the "E132" error in call_user_func(), however, the
3647 * throw point at which the "force_abort" flag (temporarily reset by
3648 * emsg()) is normally updated has not been reached yet. We need to
3649 * update that flag first to make aborting() reliable.
3650 */
3651 update_force_abort();
3652 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003653 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003654 ret = OK;
3655
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003656theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003657 /*
3658 * Report an error unless the argument evaluation or function call has been
3659 * cancelled due to an aborting error, an interrupt, or an exception.
3660 */
3661 if (!aborting())
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003662 user_func_error(error, (name != NULL) ? name : funcname, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003663
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003664 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003665 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003666 clear_tv(&argv[--argv_clear + argv_base]);
3667
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003668 vim_free(tofree);
3669 vim_free(name);
3670
3671 return ret;
3672}
3673
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003674 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675printable_func_name(ufunc_T *fp)
3676{
3677 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
3678}
3679
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003680/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003681 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003682 */
3683 static void
3684list_func_head(ufunc_T *fp, int indent)
3685{
3686 int j;
3687
3688 msg_start();
3689 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003690 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003691 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003692 msg_puts("def ");
3693 else
3694 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003696 msg_putchar('(');
3697 for (j = 0; j < fp->uf_args.ga_len; ++j)
3698 {
3699 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003700 msg_puts(", ");
3701 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 if (fp->uf_arg_types != NULL)
3703 {
3704 char *tofree;
3705
3706 msg_puts(": ");
3707 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
3708 vim_free(tofree);
3709 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003710 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
3711 {
3712 msg_puts(" = ");
3713 msg_puts(((char **)(fp->uf_def_args.ga_data))
3714 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
3715 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003716 }
3717 if (fp->uf_varargs)
3718 {
3719 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003720 msg_puts(", ");
3721 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003722 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723 if (fp->uf_va_name != NULL)
3724 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01003725 if (!fp->uf_varargs)
3726 {
3727 if (j)
3728 msg_puts(", ");
3729 msg_puts("...");
3730 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02003732 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 {
3734 char *tofree;
3735
3736 msg_puts(": ");
3737 msg_puts(type_name(fp->uf_va_type, &tofree));
3738 vim_free(tofree);
3739 }
3740 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003741 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003742
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003743 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003744 {
3745 if (fp->uf_ret_type != &t_void)
3746 {
3747 char *tofree;
3748
3749 msg_puts(": ");
3750 msg_puts(type_name(fp->uf_ret_type, &tofree));
3751 vim_free(tofree);
3752 }
3753 }
3754 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003755 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003756 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003757 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003758 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003759 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003760 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003761 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003762 msg_clr_eos();
3763 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003764 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003765}
3766
3767/*
3768 * Get a function name, translating "<SID>" and "<SNR>".
3769 * Also handles a Funcref in a List or Dictionary.
3770 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003771 * Set "*is_global" to TRUE when the function must be global, unless
3772 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003773 * flags:
3774 * TFN_INT: internal function name OK
3775 * TFN_QUIET: be quiet
3776 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003777 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003778 * Advances "pp" to just after the function name (if no error).
3779 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003780 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003781trans_function_name(
3782 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003783 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003784 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003785 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003786 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003787 partial_T **partial, // return: partial of a FuncRef
3788 type_T **type) // return: type of funcref if not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003789{
3790 char_u *name = NULL;
3791 char_u *start;
3792 char_u *end;
3793 int lead;
3794 char_u sid_buf[20];
3795 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003797 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003798 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003799 int vim9script = in_vim9script();
3800 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003801
3802 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003803 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003804 start = *pp;
3805
Bram Moolenaare38eab22019-12-05 21:50:01 +01003806 // Check for hard coded <SNR>: already translated function ID (from a user
3807 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003808 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
3809 && (*pp)[2] == (int)KE_SNR)
3810 {
3811 *pp += 3;
3812 len = get_id_len(pp) + 3;
3813 return vim_strnsave(start, len);
3814 }
3815
Bram Moolenaare38eab22019-12-05 21:50:01 +01003816 // A name starting with "<SID>" or "<SNR>" is local to a script. But
3817 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003818 lead = eval_fname_script(start);
3819 if (lead > 2)
3820 start += lead;
3821
Bram Moolenaare38eab22019-12-05 21:50:01 +01003822 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01003823 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003824 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003825 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00003826 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003827 {
3828 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00003829 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003830 goto theend;
3831 }
3832 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
3833 {
3834 /*
3835 * Report an invalid expression in braces, unless the expression
3836 * evaluation has been cancelled due to an aborting error, an
3837 * interrupt, or an exception.
3838 */
3839 if (!aborting())
3840 {
3841 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003842 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003843 }
3844 else
3845 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
3846 goto theend;
3847 }
3848
3849 if (lv.ll_tv != NULL)
3850 {
3851 if (fdp != NULL)
3852 {
3853 fdp->fd_dict = lv.ll_dict;
3854 fdp->fd_newkey = lv.ll_newkey;
3855 lv.ll_newkey = NULL;
3856 fdp->fd_di = lv.ll_di;
3857 }
3858 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
3859 {
3860 name = vim_strsave(lv.ll_tv->vval.v_string);
3861 *pp = end;
3862 }
3863 else if (lv.ll_tv->v_type == VAR_PARTIAL
3864 && lv.ll_tv->vval.v_partial != NULL)
3865 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003866 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003867 *pp = end;
3868 if (partial != NULL)
3869 *partial = lv.ll_tv->vval.v_partial;
3870 }
3871 else
3872 {
3873 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
3874 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00003875 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003876 else
3877 *pp = end;
3878 name = NULL;
3879 }
3880 goto theend;
3881 }
3882
3883 if (lv.ll_name == NULL)
3884 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003885 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003886 *pp = end;
3887 goto theend;
3888 }
3889
Bram Moolenaare38eab22019-12-05 21:50:01 +01003890 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003891 if (lv.ll_exp_name != NULL)
3892 {
3893 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003894 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00003895 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003896 if (name == lv.ll_exp_name)
3897 name = NULL;
3898 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00003899 else if (lv.ll_sid > 0)
3900 {
3901 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
3902 int cc = *lv.ll_name_end;
3903
3904 // function in another script. Prefix <SNR>99_ or the autoload prefix.
3905 *lv.ll_name_end = NUL;
3906 if (si->sn_autoload_prefix != NULL)
3907 {
3908 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
3909 }
3910 else
3911 {
3912 sid_buf[0] = K_SPECIAL;
3913 sid_buf[1] = KS_EXTRA;
3914 sid_buf[2] = (int)KE_SNR;
3915 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00003916 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00003917 name = concat_str(sid_buf, lv.ll_name);
3918 }
3919 *lv.ll_name_end = cc;
3920 *pp = end;
3921 goto theend;
3922 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003923 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003924 {
3925 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003926 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00003927 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003928 if (name == *pp)
3929 name = NULL;
3930 }
3931 if (name != NULL)
3932 {
3933 name = vim_strsave(name);
3934 *pp = end;
3935 if (STRNCMP(name, "<SNR>", 5) == 0)
3936 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003937 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003938 name[0] = K_SPECIAL;
3939 name[1] = KS_EXTRA;
3940 name[2] = (int)KE_SNR;
3941 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
3942 }
3943 goto theend;
3944 }
3945
3946 if (lv.ll_exp_name != NULL)
3947 {
3948 len = (int)STRLEN(lv.ll_exp_name);
3949 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
3950 && STRNCMP(lv.ll_name, "s:", 2) == 0)
3951 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003952 // When there was "s:" already or the name expanded to get a
3953 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003954 lv.ll_name += 2;
3955 len -= 2;
3956 lead = 2;
3957 }
3958 }
3959 else
3960 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003961 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003962 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003963 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003964 if (lv.ll_name[0] == 'g')
3965 {
3966 if (is_global != NULL)
3967 {
3968 *is_global = TRUE;
3969 }
3970 else
3971 {
3972 // dropping "g:" without setting "is_global" won't work in
3973 // Vim9script, put it back later
3974 prefix_g = TRUE;
3975 extra = 2;
3976 }
3977 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003978 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003979 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003980 len = (int)(end - lv.ll_name);
3981 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003982 if (len <= 0)
3983 {
3984 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00003985 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003986 goto theend;
3987 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003988
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003989 // In Vim9 script a user function is script-local by default, unless it
3990 // starts with a lower case character: dict.func().
Bram Moolenaar4525a572022-02-13 11:57:33 +00003991 vim9_local = ASCII_ISUPPER(*start) && vim9script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003993 /*
3994 * Copy the function name to allocated memory.
3995 * Accept <SID>name() inside a script, translate into <SNR>123_name().
3996 * Accept <SNR>123_name() outside a script.
3997 */
3998 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003999 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004000 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004001 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004002 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004003 {
Kota Kato948a3892022-08-16 16:09:59 +01004004 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4005 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004006 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004007 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004008 goto theend;
4009 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004011 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004012 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004014 || eval_fname_sid(*pp))
4015 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004017 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004018 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004019 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004020 goto theend;
4021 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004022 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004023 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 extra = 3 + (int)STRLEN(sid_buf);
4025 else
4026 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004027 }
4028 }
Bram Moolenaar22f17a22021-06-21 20:48:58 +02004029 else if (!(flags & TFN_INT) && (builtin_function(lv.ll_name, len)
Bram Moolenaar4525a572022-02-13 11:57:33 +00004030 || (vim9script && *lv.ll_name == '_')))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004031 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004032 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004033 : e_function_name_must_start_with_capital_or_s_str),
4034 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004035 goto theend;
4036 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004037 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004038 {
4039 char_u *cp = vim_strchr(lv.ll_name, ':');
4040
4041 if (cp != NULL && cp < end)
4042 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004043 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004044 goto theend;
4045 }
4046 }
4047
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004049 if (name != NULL)
4050 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004051 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004052 {
4053 name[0] = K_SPECIAL;
4054 name[1] = KS_EXTRA;
4055 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004056 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004057 STRCPY(name + 3, sid_buf);
4058 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004059 else if (prefix_g)
4060 {
4061 name[0] = 'g';
4062 name[1] = ':';
4063 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4065 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004066 }
4067 *pp = end;
4068
4069theend:
4070 clear_lval(&lv);
4071 return name;
4072}
4073
4074/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004075 * Assuming "name" is the result of trans_function_name() and it was prefixed
4076 * to use the script-local name, return the unmodified name (points into
4077 * "name"). Otherwise return NULL.
4078 * This can be used to first search for a script-local function and fall back
4079 * to the global function if not found.
4080 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004081 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004082untrans_function_name(char_u *name)
4083{
4084 char_u *p;
4085
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004086 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004087 {
4088 p = vim_strchr(name, '_');
4089 if (p != NULL)
4090 return p + 1;
4091 }
4092 return NULL;
4093}
4094
4095/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004096 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4097 * current script ID and returns the expanded function name. The caller should
4098 * free the returned name. If not called from a script context or the function
4099 * name doesn't start with these prefixes, then returns NULL.
4100 * This doesn't check whether the script-local function exists or not.
4101 */
4102 char_u *
4103get_scriptlocal_funcname(char_u *funcname)
4104{
4105 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004106 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004107 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004108 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004109
4110 if (funcname == NULL)
4111 return NULL;
4112
4113 if (STRNCMP(funcname, "s:", 2) != 0
4114 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004115 {
4116 ufunc_T *ufunc;
4117
4118 // The function name does not have a script-local prefix. Try finding
4119 // it when in a Vim9 script and there is no "g:" prefix.
4120 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4121 return NULL;
4122 ufunc = find_func(funcname, FALSE);
4123 if (ufunc == NULL || func_is_global(ufunc)
4124 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4125 return NULL;
4126 ++p;
4127 off = 0;
4128 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004129 else
4130 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004131
4132 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4133 {
4134 emsg(_(e_using_sid_not_in_script_context));
4135 return NULL;
4136 }
4137 // Expand s: prefix into <SNR>nr_<name>
4138 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4139 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004140 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004141 if (newname == NULL)
4142 return NULL;
4143 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004144 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004145
4146 return newname;
4147}
4148
4149/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004150 * Return script-local "fname" with the 3-byte sequence replaced by
4151 * printable <SNR> in allocated memory.
4152 */
4153 char_u *
4154alloc_printable_func_name(char_u *fname)
4155{
4156 char_u *n = alloc(STRLEN(fname + 3) + 6);
4157
4158 if (n != NULL)
4159 {
4160 STRCPY(n, "<SNR>");
4161 STRCPY(n + 5, fname + 3);
4162 }
4163 return n;
4164}
4165
4166/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004167 * Call trans_function_name(), except that a lambda is returned as-is.
4168 * Returns the name in allocated memory.
4169 */
4170 char_u *
4171save_function_name(
4172 char_u **name,
4173 int *is_global,
4174 int skip,
4175 int flags,
4176 funcdict_T *fudi)
4177{
4178 char_u *p = *name;
4179 char_u *saved;
4180
4181 if (STRNCMP(p, "<lambda>", 8) == 0)
4182 {
4183 p += 8;
4184 (void)getdigits(&p);
4185 saved = vim_strnsave(*name, p - *name);
4186 if (fudi != NULL)
4187 CLEAR_POINTER(fudi);
4188 }
4189 else
4190 saved = trans_function_name(&p, is_global, skip,
4191 flags, fudi, NULL, NULL);
4192 *name = p;
4193 return saved;
4194}
4195
4196/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004197 * List functions. When "regmatch" is NULL all of then.
4198 * Otherwise functions matching "regmatch".
4199 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004200 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004201list_functions(regmatch_T *regmatch)
4202{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004203 int changed = func_hashtab.ht_changed;
4204 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004205 hashitem_T *hi;
4206
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004207 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004208 {
4209 if (!HASHITEM_EMPTY(hi))
4210 {
4211 ufunc_T *fp = HI2UF(hi);
4212
4213 --todo;
4214 if ((fp->uf_flags & FC_DEAD) == 0
4215 && (regmatch == NULL
4216 ? !message_filtered(fp->uf_name)
4217 && !func_name_refcount(fp->uf_name)
4218 : !isdigit(*fp->uf_name)
4219 && vim_regexec(regmatch, fp->uf_name, 0)))
4220 {
4221 list_func_head(fp, FALSE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004222 if (changed != func_hashtab.ht_changed)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004223 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00004224 emsg(_(e_function_list_was_modified));
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004225 return;
4226 }
4227 }
4228 }
4229 }
4230}
4231
4232/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004233 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004234 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4235 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004236 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004237 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004238 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004239 ufunc_T *
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004240define_function(exarg_T *eap, char_u *name_arg, garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004241{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004242 int j;
4243 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004244 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004245 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004246 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004247 char_u *p;
4248 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004249 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004250 char_u *line_arg = NULL;
4251 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004253 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004254 garray_T newlines;
4255 int varargs = FALSE;
4256 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004258 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004259 int fp_allocated = FALSE;
4260 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004261 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004262 dictitem_T *v;
4263 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004264 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004265 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004266 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004267 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004268 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004269 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004270
4271 /*
4272 * ":function" without argument: list functions.
4273 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004274 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004275 {
4276 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004277 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004278 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004279 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004280 }
4281
4282 /*
4283 * ":function /pat": list functions matching pattern.
4284 */
4285 if (*eap->arg == '/')
4286 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004287 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004288 if (!eap->skip)
4289 {
4290 regmatch_T regmatch;
4291
4292 c = *p;
4293 *p = NUL;
4294 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4295 *p = c;
4296 if (regmatch.regprog != NULL)
4297 {
4298 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004299 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004300 vim_regfree(regmatch.regprog);
4301 }
4302 }
4303 if (*p == '/')
4304 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004305 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004306 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004307 }
4308
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309 ga_init(&newargs);
4310 ga_init(&argtypes);
4311 ga_init(&default_args);
4312
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004313 /*
4314 * Get the function name. There are these situations:
4315 * func normal function name
4316 * "name" == func, "fudi.fd_dict" == NULL
4317 * dict.func new dictionary entry
4318 * "name" == NULL, "fudi.fd_dict" set,
4319 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4320 * dict.func existing dict entry with a Funcref
4321 * "name" == func, "fudi.fd_dict" set,
4322 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4323 * dict.func existing dict entry that's not a Funcref
4324 * "name" == NULL, "fudi.fd_dict" set,
4325 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4326 * s:func script-local function name
4327 * g:func global function name, same as "func"
4328 */
4329 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004330 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004331 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004332 // nested function, argument is (args).
4333 paren = TRUE;
4334 CLEAR_FIELD(fudi);
4335 }
4336 else
4337 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004338 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004339 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004340 if (p[0] == 's' && p[1] == ':')
4341 {
4342 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4343 return NULL;
4344 }
4345 p = to_name_end(p, TRUE);
4346 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4347 {
4348 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4349 eap->arg);
4350 return NULL;
4351 }
4352 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004353 }
4354
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004355 name = save_function_name(&p, &is_global, eap->skip,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004356 TFN_NO_AUTOLOAD | TFN_NEW_FUNC, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004357 paren = (vim_strchr(p, '(') != NULL);
4358 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004359 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004360 /*
4361 * Return on an invalid expression in braces, unless the expression
4362 * evaluation has been cancelled due to an aborting error, an
4363 * interrupt, or an exception.
4364 */
4365 if (!aborting())
4366 {
4367 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004368 semsg(_(e_key_not_present_in_dictionary), fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004369 vim_free(fudi.fd_newkey);
4370 return NULL;
4371 }
4372 else
4373 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004374 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004375
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004376 // For "export def FuncName()" in an autoload script the function name
4377 // is stored with the legacy autoload name "dir#script#FuncName" so
4378 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004379 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004380 {
4381 char_u *prefixed = may_prefix_autoload(name);
4382
4383 if (prefixed != NULL && prefixed != name)
4384 {
4385 vim_free(name);
4386 name = prefixed;
4387 }
4388 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004389 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004390 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004391 {
4392 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4393 goto ret_free;
4394 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004395 }
4396
Bram Moolenaare38eab22019-12-05 21:50:01 +01004397 // An error in a function call during evaluation of an expression in magic
4398 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004399 saved_did_emsg = did_emsg;
4400 did_emsg = FALSE;
4401
4402 /*
4403 * ":function func" with only function name: list function.
4404 */
4405 if (!paren)
4406 {
4407 if (!ends_excmd(*skipwhite(p)))
4408 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004409 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004410 goto ret_free;
4411 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004412 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004413 if (eap->nextcmd != NULL)
4414 *p = NUL;
4415 if (!eap->skip && !got_int)
4416 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004417 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004418 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4419 {
4420 char_u *up = untrans_function_name(name);
4421
4422 // With Vim9 script the name was made script-local, if not
4423 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004424 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004425 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004426 }
4427
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004428 if (fp != NULL)
4429 {
4430 list_func_head(fp, TRUE);
4431 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4432 {
4433 if (FUNCLINE(fp, j) == NULL)
4434 continue;
4435 msg_putchar('\n');
4436 msg_outnum((long)(j + 1));
4437 if (j < 9)
4438 msg_putchar(' ');
4439 if (j < 99)
4440 msg_putchar(' ');
4441 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004442 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004443 ui_breakcheck();
4444 }
4445 if (!got_int)
4446 {
4447 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004448 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449 msg_puts(" enddef");
4450 else
4451 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004452 }
4453 }
4454 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004455 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004456 }
4457 goto ret_free;
4458 }
4459
4460 /*
4461 * ":function name(arg1, arg2)" Define function.
4462 */
4463 p = skipwhite(p);
4464 if (*p != '(')
4465 {
4466 if (!eap->skip)
4467 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004468 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004469 goto ret_free;
4470 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004471 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004472 if (vim_strchr(p, '(') != NULL)
4473 p = vim_strchr(p, '(');
4474 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004475
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004476 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4477 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004478 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004479 goto ret_free;
4480 }
4481
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004482 // In Vim9 script only global functions can be redefined.
4483 if (vim9script && eap->forceit && !is_global)
4484 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004485 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004486 goto ret_free;
4487 }
4488
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004489 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004490
Bram Moolenaar04b12692020-05-04 23:24:44 +02004491 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004492 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004493 // Check the name of the function. Unless it's a dictionary function
4494 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004495 if (name != NULL)
4496 arg = name;
4497 else
4498 arg = fudi.fd_newkey;
4499 if (arg != NULL && (fudi.fd_di == NULL
4500 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4501 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4502 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004503 char_u *name_base = arg;
4504 int i;
4505
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004506 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004507 {
4508 name_base = vim_strchr(arg, '_');
4509 if (name_base == NULL)
4510 name_base = arg + 3;
4511 else
4512 ++name_base;
4513 }
4514 for (i = 0; name_base[i] != NUL && (i == 0
4515 ? eval_isnamec1(name_base[i])
4516 : eval_isnamec(name_base[i])); ++i)
4517 ;
4518 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004519 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004520
4521 // In Vim9 script a function cannot have the same name as a
4522 // variable.
4523 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004524 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4525 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004526 + EVAL_VAR_NO_FUNC) == OK)
4527 {
4528 semsg(_(e_redefining_script_item_str), name_base);
4529 goto ret_free;
4530 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004531 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004532 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004533 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004534 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004535 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004536 goto ret_free;
4537 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004538 }
4539
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004540 // This may get more lines and make the pointers into the first line
4541 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004542 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004544 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004545 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004546 eap, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004547 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004548 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004549
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004550 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004551 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004553 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004554 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004555 if (*p != ':')
4556 {
4557 semsg(_(e_no_white_space_allowed_before_colon_str), p);
4558 p = skipwhite(p);
4559 }
4560 else if (!IS_WHITE_OR_NUL(p[1]))
4561 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02004563 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004565 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004566 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01004567 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004569 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004571 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004572 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02004573 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004574 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004575 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02004576 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004577 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578 else
4579 // find extra arguments "range", "dict", "abort" and "closure"
4580 for (;;)
4581 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01004582 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583 p = skipwhite(p);
4584 if (STRNCMP(p, "range", 5) == 0)
4585 {
4586 flags |= FC_RANGE;
4587 p += 5;
4588 }
4589 else if (STRNCMP(p, "dict", 4) == 0)
4590 {
4591 flags |= FC_DICT;
4592 p += 4;
4593 }
4594 else if (STRNCMP(p, "abort", 5) == 0)
4595 {
4596 flags |= FC_ABORT;
4597 p += 5;
4598 }
4599 else if (STRNCMP(p, "closure", 7) == 0)
4600 {
4601 flags |= FC_CLOSURE;
4602 p += 7;
4603 if (current_funccal == NULL)
4604 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004605 emsg_funcname(e_closure_function_should_not_be_at_top_level,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606 name == NULL ? (char_u *)"" : name);
4607 goto erret;
4608 }
4609 }
4610 else
4611 break;
4612 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004613
Bram Moolenaare38eab22019-12-05 21:50:01 +01004614 // When there is a line break use what follows for the function body.
4615 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004616 if (*p == '\n')
4617 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004618 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02004619 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
4620 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01004621 && !(VIM_ISWHITE(*whitep) && *p == '#'
4622 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02004623 && !eap->skip
4624 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004625 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004626
4627 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
4629 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004630 */
4631 if (KeyTyped)
4632 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004633 // Check if the function already exists, don't let the user type the
4634 // whole function before telling him it doesn't work! For a script we
4635 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004636 if (!eap->skip && !eap->forceit)
4637 {
4638 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004639 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004640 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00004641 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004642 }
4643
4644 if (!eap->skip && did_emsg)
4645 goto erret;
4646
Bram Moolenaare38eab22019-12-05 21:50:01 +01004647 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004648 cmdline_row = msg_row;
4649 }
4650
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004651 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004652 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004653
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004654 // Do not define the function when getting the body fails and when
4655 // skipping.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004656 if (get_function_body(eap, &newlines, line_arg, lines_to_free) == FAIL
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004657 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004658 goto erret;
4659
4660 /*
4661 * If there are no errors, add the function
4662 */
4663 if (fudi.fd_dict == NULL)
4664 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004666 char_u *find_name = name;
4667 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004668 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004669
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004670 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004671 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004672 var_conflict = TRUE;
4673
4674 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
4675 {
4676 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
4677
4678 if (si->sn_autoload_prefix != NULL)
4679 {
4680 if (is_export)
4681 {
4682 find_name = name + STRLEN(si->sn_autoload_prefix);
4683 v = find_var(find_name, &ht, TRUE);
4684 if (v != NULL)
4685 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004686 // Only check if the function already exists in the script,
4687 // global functions can be shadowed.
4688 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004689 }
4690 else
4691 {
4692 char_u *prefixed = may_prefix_autoload(name);
4693
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00004694 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004695 {
4696 v = find_var(prefixed, &ht, TRUE);
4697 if (v != NULL)
4698 var_conflict = TRUE;
4699 vim_free(prefixed);
4700 }
4701 }
4702 }
4703 }
4704 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004705 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004706 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004707 goto erret;
4708 }
4709
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004710 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02004711 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004712 {
Bram Moolenaareef21022020-08-01 22:16:43 +02004713 char_u *uname = untrans_function_name(name);
4714
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004715 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004716 }
4717
4718 if (fp != NULL || import != NULL)
4719 {
4720 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004722 // Function can be replaced with "function!" and when sourcing the
4723 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02004724 // A name that is used by an import can not be overruled.
4725 if (import != NULL
4726 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004727 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02004728 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004729 {
Bram Moolenaard604d782021-11-20 21:46:20 +00004730 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02004731 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004732 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02004733 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00004734 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004735 goto erret;
4736 }
4737 if (fp->uf_calls > 0)
4738 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004739 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00004740 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004741 goto erret;
4742 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004743 if (fp->uf_refcount > 1)
4744 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004745 // This function is referenced somewhere, don't redefine it but
4746 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004747 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004748 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004749 fp = NULL;
4750 overwrite = TRUE;
4751 }
4752 else
4753 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004754 char_u *exp_name = fp->uf_name_exp;
4755
4756 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01004757 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004758 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004759 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004760 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004761 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004762#ifdef FEAT_PROFILE
4763 fp->uf_profiling = FALSE;
4764 fp->uf_prof_initialized = FALSE;
4765#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01004766 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004767 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004768 }
4769 }
4770 else
4771 {
4772 char numbuf[20];
4773
4774 fp = NULL;
4775 if (fudi.fd_newkey == NULL && !eap->forceit)
4776 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004777 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004778 goto erret;
4779 }
4780 if (fudi.fd_di == NULL)
4781 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004782 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02004783 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004784 goto erret;
4785 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004786 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02004787 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004788 goto erret;
4789
Bram Moolenaare38eab22019-12-05 21:50:01 +01004790 // Give the function a sequential number. Can only be used with a
4791 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004792 vim_free(name);
4793 sprintf(numbuf, "%d", ++func_nr);
4794 name = vim_strsave((char_u *)numbuf);
4795 if (name == NULL)
4796 goto erret;
4797 }
4798
4799 if (fp == NULL)
4800 {
4801 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
4802 {
4803 int slen, plen;
4804 char_u *scriptname;
4805
Bram Moolenaare38eab22019-12-05 21:50:01 +01004806 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004807 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004808 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004809 {
4810 scriptname = autoload_name(name);
4811 if (scriptname != NULL)
4812 {
4813 p = vim_strchr(scriptname, '/');
4814 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004815 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004816 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004817 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004818 j = OK;
4819 vim_free(scriptname);
4820 }
4821 }
4822 if (j == FAIL)
4823 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004824 linenr_T save_lnum = SOURCING_LNUM;
4825
4826 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00004827 semsg(_(e_function_name_does_not_match_script_file_name_str),
4828 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004829 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004830 goto erret;
4831 }
4832 }
4833
Bram Moolenaar47ed5532019-08-08 20:49:14 +02004834 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004835 if (fp == NULL)
4836 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004837 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004838
4839 if (fudi.fd_dict != NULL)
4840 {
4841 if (fudi.fd_di == NULL)
4842 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004843 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004844 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
4845 if (fudi.fd_di == NULL)
4846 {
4847 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004848 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004849 goto erret;
4850 }
4851 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
4852 {
4853 vim_free(fudi.fd_di);
4854 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004855 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004856 goto erret;
4857 }
4858 }
4859 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004860 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004861 clear_tv(&fudi.fd_di->di_tv);
4862 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004863 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004864
Bram Moolenaare38eab22019-12-05 21:50:01 +01004865 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004866 flags |= FC_DICT;
4867 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004868 }
4869 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004870 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004872 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004873
4874 if (eap->cmdidx == CMD_def)
4875 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02004876 int lnum_save = SOURCING_LNUM;
4877 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004878
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004879 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004880
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004881 // error messages are for the first function line
4882 SOURCING_LNUM = sourcing_lnum_top;
4883
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02004884 // The function may use script variables from the context.
4885 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004886
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004887 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004888 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004889 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004890 free_fp = fp_allocated;
4891 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004893 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894
4895 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004896 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004898 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004899 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004900 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004901 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02004902 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004903 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02004904 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004905 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004906
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004907 if (fp_allocated)
4908 {
4909 // insert the new function in the function list
4910 set_ufunc_name(fp, name);
4911 if (overwrite)
4912 {
4913 hi = hash_find(&func_hashtab, name);
4914 hi->hi_key = UF2HIKEY(fp);
4915 }
4916 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
4917 {
4918 free_fp = TRUE;
4919 goto erret;
4920 }
4921 fp->uf_refcount = 1;
4922 }
4923
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004924 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004925 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004926 if ((flags & FC_CLOSURE) != 0)
4927 {
Bram Moolenaar58016442016-07-31 18:30:22 +02004928 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004929 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004930 }
4931 else
4932 fp->uf_scoped = NULL;
4933
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004934#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004935 if (prof_def_func())
4936 func_do_profile(fp);
4937#endif
4938 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02004939 if (sandbox)
4940 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004941 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004942 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004943 fp->uf_flags = flags;
4944 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01004945 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004946 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004947 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004948 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004949 if (is_export)
4950 {
4951 fp->uf_flags |= FC_EXPORT;
4952 // let ex_export() know the export worked.
4953 is_export = FALSE;
4954 }
4955
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004956 if (eap->cmdidx == CMD_def)
4957 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02004958 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
4959 // :func does not use Vim9 script syntax, even in a Vim9 script file
4960 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004961
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004962 goto ret_free;
4963
4964erret:
4965 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004966 ga_clear_strings(&default_args);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004967 if (fp != NULL)
4968 {
4969 ga_init(&fp->uf_args);
4970 ga_init(&fp->uf_def_args);
4971 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004972errret_2:
4973 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004974 if (fp != NULL)
4975 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004976 if (free_fp)
4977 {
4978 vim_free(fp);
4979 fp = NULL;
4980 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004981ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01004982 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004983 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004984 if (name != name_arg)
4985 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004986 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004987 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004988
4989 return fp;
4990}
4991
4992/*
4993 * ":function"
4994 */
4995 void
4996ex_function(exarg_T *eap)
4997{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004998 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00004999
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005000 ga_init2(&lines_to_free, sizeof(char_u *), 50);
5001 (void)define_function(eap, NULL, &lines_to_free);
5002 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005003}
5004
5005/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005006 * Find a function by name, including "<lambda>123".
5007 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005008 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005009 * Return NULL if not found.
5010 */
5011 ufunc_T *
5012find_func_by_name(char_u *name, compiletype_T *compile_type)
5013{
5014 char_u *arg = name;
5015 char_u *fname;
5016 ufunc_T *ufunc;
5017 int is_global = FALSE;
5018
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005019 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5020 {
5021 *compile_type = CT_PROFILE;
5022 arg = skipwhite(arg + 7);
5023 }
5024 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5025 {
5026 *compile_type = CT_DEBUG;
5027 arg = skipwhite(arg + 5);
5028 }
5029
5030 if (STRNCMP(arg, "<lambda>", 8) == 0)
5031 {
5032 arg += 8;
5033 (void)getdigits(&arg);
5034 fname = vim_strnsave(name, arg - name);
5035 }
5036 else
5037 fname = trans_function_name(&arg, &is_global, FALSE,
5038 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
5039 if (fname == NULL)
5040 {
5041 semsg(_(e_invalid_argument_str), name);
5042 return NULL;
5043 }
5044 if (!ends_excmd2(name, arg))
5045 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005046 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005047 emsg(ex_errmsg(e_trailing_characters_str, arg));
5048 return NULL;
5049 }
5050
5051 ufunc = find_func(fname, is_global);
5052 if (ufunc == NULL)
5053 {
5054 char_u *p = untrans_function_name(fname);
5055
5056 if (p != NULL)
5057 // Try again without making it script-local.
5058 ufunc = find_func(p, FALSE);
5059 }
5060 vim_free(fname);
5061 if (ufunc == NULL)
5062 semsg(_(e_cannot_find_function_str), name);
5063 return ufunc;
5064}
5065
5066/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005067 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005068 * be compiled or the one specified by the argument.
5069 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005070 */
5071 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005072ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005073{
Bram Moolenaar822ba242020-05-24 23:00:18 +02005074 ufunc_T *ufunc;
5075
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005076 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005077 {
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005078 compiletype_T compile_type = CT_NONE;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005079
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005080 ufunc = find_func_by_name(eap->arg, &compile_type);
5081 if (ufunc != NULL)
5082 {
5083 if (func_needs_compiling(ufunc, compile_type))
5084 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5085 else
5086 smsg(_("Function %s does not need compiling"), eap->arg);
5087 }
5088 }
5089 else
5090 {
5091 long todo = (long)func_hashtab.ht_used;
5092 int changed = func_hashtab.ht_changed;
5093 hashitem_T *hi;
5094
5095 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5096 {
5097 if (!HASHITEM_EMPTY(hi))
5098 {
5099 --todo;
5100 ufunc = HI2UF(hi);
5101 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5102 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5103 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005104 {
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005105 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5106
5107 if (func_hashtab.ht_changed != changed)
5108 {
5109 // a function has been added or removed, need to start
5110 // over
5111 todo = (long)func_hashtab.ht_used;
5112 changed = func_hashtab.ht_changed;
5113 hi = func_hashtab.ht_array;
5114 --hi;
5115 }
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005116 }
5117 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005118 }
5119 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005120}
5121
5122/*
5123 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5124 * Return 2 if "p" starts with "s:".
5125 * Return 0 otherwise.
5126 */
5127 int
5128eval_fname_script(char_u *p)
5129{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005130 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5131 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005132 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5133 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5134 return 5;
5135 if (p[0] == 's' && p[1] == ':')
5136 return 2;
5137 return 0;
5138}
5139
5140 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005141translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005142{
5143 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005144 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005145 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005146}
5147
5148/*
5149 * Return TRUE when "ufunc" has old-style "..." varargs
5150 * or named varargs "...name: type".
5151 */
5152 int
5153has_varargs(ufunc_T *ufunc)
5154{
5155 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005156}
5157
5158/*
5159 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005160 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005161 */
5162 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005163function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005164{
5165 char_u *nm = name;
5166 char_u *p;
5167 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005168 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005169 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005170
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005171 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5172 if (no_deref)
5173 flag |= TFN_NO_DEREF;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005174 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005175 nm = skipwhite(nm);
5176
Bram Moolenaare38eab22019-12-05 21:50:01 +01005177 // Only accept "funcname", "funcname ", "funcname (..." and
5178 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005179 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005180 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005181 vim_free(p);
5182 return n;
5183}
5184
Bram Moolenaar113e1072019-01-20 15:30:40 +01005185#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005186 char_u *
5187get_expanded_name(char_u *name, int check)
5188{
5189 char_u *nm = name;
5190 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005191 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005192
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005193 p = trans_function_name(&nm, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005194 TFN_INT|TFN_QUIET, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005195
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005196 if (p != NULL && *nm == NUL
5197 && (!check || translated_function_exists(p, is_global)))
5198 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005199
5200 vim_free(p);
5201 return NULL;
5202}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005203#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005204
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005205/*
5206 * Function given to ExpandGeneric() to obtain the list of user defined
5207 * function names.
5208 */
5209 char_u *
5210get_user_func_name(expand_T *xp, int idx)
5211{
5212 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005213 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005214 static hashitem_T *hi;
5215 ufunc_T *fp;
5216
5217 if (idx == 0)
5218 {
5219 done = 0;
5220 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005221 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005222 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005223 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005224 {
5225 if (done++ > 0)
5226 ++hi;
5227 while (HASHITEM_EMPTY(hi))
5228 ++hi;
5229 fp = HI2UF(hi);
5230
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005231 // don't show dead, dict and lambda functions
5232 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005233 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005234 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005235
5236 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005237 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005238
5239 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005240 if (xp->xp_context != EXPAND_USER_FUNC
5241 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005242 {
5243 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005244 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005245 STRCAT(IObuff, ")");
5246 }
5247 return IObuff;
5248 }
5249 return NULL;
5250}
5251
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005252/*
5253 * ":delfunction {name}"
5254 */
5255 void
5256ex_delfunction(exarg_T *eap)
5257{
5258 ufunc_T *fp = NULL;
5259 char_u *p;
5260 char_u *name;
5261 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005262 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005263
5264 p = eap->arg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005265 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi,
5266 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005267 vim_free(fudi.fd_newkey);
5268 if (name == NULL)
5269 {
5270 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005271 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005272 return;
5273 }
5274 if (!ends_excmd(*skipwhite(p)))
5275 {
5276 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005277 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005278 return;
5279 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005280 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005281 if (eap->nextcmd != NULL)
5282 *p = NUL;
5283
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005284 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005285 {
5286 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005287 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005288 vim_free(name);
5289 return;
5290 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005291 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005292 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005293 vim_free(name);
5294
5295 if (!eap->skip)
5296 {
5297 if (fp == NULL)
5298 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005299 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005300 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005301 return;
5302 }
5303 if (fp->uf_calls > 0)
5304 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005305 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005306 return;
5307 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005308 if (fp->uf_flags & FC_VIM9)
5309 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005310 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005311 return;
5312 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005313
5314 if (fudi.fd_dict != NULL)
5315 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005316 // Delete the dict item that refers to the function, it will
5317 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005318 dictitem_remove(fudi.fd_dict, fudi.fd_di);
5319 }
5320 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005321 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005322 // A normal function (not a numbered function or lambda) has a
5323 // refcount of 1 for the entry in the hashtable. When deleting
5324 // it and the refcount is more than one, it should be kept.
5325 // A numbered function and lambda should be kept if the refcount is
5326 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005327 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005328 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005329 // Function is still referenced somewhere. Don't free it but
5330 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005331 if (func_remove(fp))
5332 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005333 }
5334 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005335 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005336 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005337 }
5338}
5339
5340/*
5341 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005342 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005343 */
5344 void
5345func_unref(char_u *name)
5346{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005347 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005348
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005349 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005350 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005351 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005352 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005353 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005354#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005355 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005356#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005357 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005358 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005359 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005360}
5361
5362/*
5363 * Unreference a Function: decrement the reference count and free it when it
5364 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005365 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005366 */
5367 void
5368func_ptr_unref(ufunc_T *fp)
5369{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005370 if (fp != NULL && (--fp->uf_refcount <= 0
5371 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5372 && fp->uf_partial->pt_refcount <= 1
5373 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005374 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005375 // Only delete it when it's not being used. Otherwise it's done
5376 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005377 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005378 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005379 }
5380}
5381
5382/*
5383 * Count a reference to a Function.
5384 */
5385 void
5386func_ref(char_u *name)
5387{
5388 ufunc_T *fp;
5389
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005390 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005391 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005392 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005393 if (fp != NULL)
5394 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005395 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005396 // Only give an error for a numbered function.
5397 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005398 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005399}
5400
5401/*
5402 * Count a reference to a Function.
5403 */
5404 void
5405func_ptr_ref(ufunc_T *fp)
5406{
5407 if (fp != NULL)
5408 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005409}
5410
5411/*
5412 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5413 * referenced from anywhere that is in use.
5414 */
5415 static int
5416can_free_funccal(funccall_T *fc, int copyID)
5417{
5418 return (fc->l_varlist.lv_copyID != copyID
5419 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005420 && fc->l_avars.dv_copyID != copyID
5421 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005422}
5423
5424/*
5425 * ":return [expr]"
5426 */
5427 void
5428ex_return(exarg_T *eap)
5429{
5430 char_u *arg = eap->arg;
5431 typval_T rettv;
5432 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005433 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005434
5435 if (current_funccal == NULL)
5436 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005437 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005438 return;
5439 }
5440
Bram Moolenaar844fb642021-10-23 13:32:30 +01005441 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005442 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5443
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005444 if (eap->skip)
5445 ++emsg_skip;
5446
5447 eap->nextcmd = NULL;
5448 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005449 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005450 {
5451 if (!eap->skip)
5452 returning = do_return(eap, FALSE, TRUE, &rettv);
5453 else
5454 clear_tv(&rettv);
5455 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005456 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005457 else if (!eap->skip)
5458 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005459 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005460 update_force_abort();
5461
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005462 /*
5463 * Return unless the expression evaluation has been cancelled due to an
5464 * aborting error, an interrupt, or an exception.
5465 */
5466 if (!aborting())
5467 returning = do_return(eap, FALSE, TRUE, NULL);
5468 }
5469
Bram Moolenaare38eab22019-12-05 21:50:01 +01005470 // When skipping or the return gets pending, advance to the next command
5471 // in this line (!returning). Otherwise, ignore the rest of the line.
5472 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005473 if (returning)
5474 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005475 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02005476 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005477
5478 if (eap->skip)
5479 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02005480 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005481}
5482
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005483 static int
5484ex_call_inner(
5485 exarg_T *eap,
5486 char_u *name,
5487 char_u **arg,
5488 char_u *startarg,
5489 funcexe_T *funcexe_init,
5490 evalarg_T *evalarg)
5491{
5492 linenr_T lnum;
5493 int doesrange;
5494 typval_T rettv;
5495 int failed = FALSE;
5496
5497 /*
5498 * When skipping, evaluate the function once, to find the end of the
5499 * arguments.
5500 * When the function takes a range, this is discovered after the first
5501 * call, and the loop is broken.
5502 */
5503 if (eap->skip)
5504 {
5505 ++emsg_skip;
5506 lnum = eap->line2; // do it once, also with an invalid range
5507 }
5508 else
5509 lnum = eap->line1;
5510 for ( ; lnum <= eap->line2; ++lnum)
5511 {
5512 funcexe_T funcexe;
5513
5514 if (!eap->skip && eap->addr_count > 0)
5515 {
5516 if (lnum > curbuf->b_ml.ml_line_count)
5517 {
5518 // If the function deleted lines or switched to another buffer
5519 // the line number may become invalid.
5520 emsg(_(e_invalid_range));
5521 break;
5522 }
5523 curwin->w_cursor.lnum = lnum;
5524 curwin->w_cursor.col = 0;
5525 curwin->w_cursor.coladd = 0;
5526 }
5527 *arg = startarg;
5528
5529 funcexe = *funcexe_init;
5530 funcexe.fe_doesrange = &doesrange;
5531 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
5532 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
5533 {
5534 failed = TRUE;
5535 break;
5536 }
5537 if (has_watchexpr())
5538 dbg_check_breakpoint(eap);
5539
5540 // Handle a function returning a Funcref, Dictionary or List.
5541 if (handle_subscript(arg, NULL, &rettv,
5542 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
5543 {
5544 failed = TRUE;
5545 break;
5546 }
5547
5548 clear_tv(&rettv);
5549 if (doesrange || eap->skip)
5550 break;
5551
5552 // Stop when immediately aborting on error, or when an interrupt
5553 // occurred or an exception was thrown but not caught.
5554 // get_func_tv() returned OK, so that the check for trailing
5555 // characters below is executed.
5556 if (aborting())
5557 break;
5558 }
5559 if (eap->skip)
5560 --emsg_skip;
5561 return failed;
5562}
5563
5564/*
5565 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
5566 * Returns FAIL or OK.
5567 */
5568 static int
5569ex_defer_inner(char_u *name, char_u **arg, evalarg_T *evalarg)
5570{
5571 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
5572 int argcount = 0; // number of arguments found
5573 defer_T *dr;
5574 int ret = FAIL;
5575 char_u *saved_name;
5576
5577 if (current_funccal == NULL)
5578 {
5579 semsg(_(e_str_not_inside_function), "defer");
5580 return FAIL;
5581 }
5582 if (get_func_arguments(arg, evalarg, FALSE, argvars, &argcount) == FAIL)
5583 goto theend;
5584 saved_name = vim_strsave(name);
5585 if (saved_name == NULL)
5586 goto theend;
5587
5588 if (current_funccal->fc_defer.ga_itemsize == 0)
5589 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
5590 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
5591 goto theend;
5592 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
5593 + current_funccal->fc_defer.ga_len++;
5594 dr->dr_name = saved_name;
5595 dr->dr_argcount = argcount;
5596 while (argcount > 0)
5597 {
5598 --argcount;
5599 dr->dr_argvars[argcount] = argvars[argcount];
5600 }
5601 ret = OK;
5602
5603theend:
5604 while (--argcount >= 0)
5605 clear_tv(&argvars[argcount]);
5606 return ret;
5607}
5608
5609/*
5610 * Invoked after a functions has finished: invoke ":defer" functions.
5611 */
5612 void
5613handle_defer(void)
5614{
5615 int idx;
5616
5617 for (idx = current_funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
5618 {
5619 funcexe_T funcexe;
5620 typval_T rettv;
5621 defer_T *dr = ((defer_T *)current_funccal->fc_defer.ga_data) + idx;
5622 int i;
5623
5624 CLEAR_FIELD(funcexe);
5625 funcexe.fe_evaluate = TRUE;
5626
5627 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
5628 call_func(dr->dr_name, -1, &rettv,
5629 dr->dr_argcount, dr->dr_argvars, &funcexe);
5630 clear_tv(&rettv);
5631 vim_free(dr->dr_name);
5632 for (i = dr->dr_argcount - 1; i >= 0; --i)
5633 clear_tv(&dr->dr_argvars[i]);
5634 }
5635 ga_clear(&current_funccal->fc_defer);
5636}
5637
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005638/*
5639 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005640 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005641 */
5642 void
5643ex_call(exarg_T *eap)
5644{
5645 char_u *arg = eap->arg;
5646 char_u *startarg;
5647 char_u *name;
5648 char_u *tofree;
5649 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005650 int failed = FALSE;
5651 funcdict_T fudi;
5652 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005653 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005654 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005655 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005656 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005657
Bram Moolenaare6b53242020-07-01 17:28:33 +02005658 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005659 if (eap->skip)
5660 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005661 typval_T rettv;
5662
Bram Moolenaare38eab22019-12-05 21:50:01 +01005663 // trans_function_name() doesn't work well when skipping, use eval0()
5664 // instead to skip to any following command, e.g. for:
5665 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005666 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005667 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005668 clear_tv(&rettv);
5669 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005670 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005671 return;
5672 }
5673
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005674 tofree = trans_function_name(&arg, NULL, eap->skip, TFN_INT,
Bram Moolenaar4525a572022-02-13 11:57:33 +00005675 &fudi, &partial, vim9script ? &type : NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005676 if (fudi.fd_newkey != NULL)
5677 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005678 // Still need to give an error message for missing key.
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005679 semsg(_(e_key_not_present_in_dictionary), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005680 vim_free(fudi.fd_newkey);
5681 }
5682 if (tofree == NULL)
5683 return;
5684
Bram Moolenaare38eab22019-12-05 21:50:01 +01005685 // Increase refcount on dictionary, it could get deleted when evaluating
5686 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005687 if (fudi.fd_dict != NULL)
5688 ++fudi.fd_dict->dv_refcount;
5689
Bram Moolenaare38eab22019-12-05 21:50:01 +01005690 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
5691 // contents. For VAR_PARTIAL get its partial, unless we already have one
5692 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005693 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005694 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00005695 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00005696 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005697
Bram Moolenaare38eab22019-12-05 21:50:01 +01005698 // Skip white space to allow ":call func ()". Not good, but required for
5699 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005700 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005701 if (*startarg != '(')
5702 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005703 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005704 goto end;
5705 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00005706 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02005707 {
5708 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
5709 goto end;
5710 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005711
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005712 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005713 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005714 arg = startarg;
5715 failed = ex_defer_inner(name, &arg, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005716 }
5717 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005718 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005719 funcexe_T funcexe;
5720
Bram Moolenaara80faa82020-04-12 19:37:17 +02005721 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005722 funcexe.fe_check_type = type;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00005723 funcexe.fe_partial = partial;
5724 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005725 funcexe.fe_firstline = eap->line1;
5726 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005727 funcexe.fe_found_var = found_var;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005728 funcexe.fe_evaluate = !eap->skip;
5729 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005730 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005731
Bram Moolenaar1d341892021-09-18 15:25:52 +02005732 // When inside :try we need to check for following "| catch" or "| endtry".
5733 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005734 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005735 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005736 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02005737 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02005738 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005739 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02005740 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01005741 {
5742 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00005743 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01005744 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005745 }
5746 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02005747 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005748 }
Bram Moolenaara929c922022-04-18 15:21:17 +01005749 // Must be after using "arg", it may point into memory cleared here.
5750 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005751
5752end:
5753 dict_unref(fudi.fd_dict);
5754 vim_free(tofree);
5755}
5756
5757/*
5758 * Return from a function. Possibly makes the return pending. Also called
5759 * for a pending return at the ":endtry" or after returning from an extra
5760 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
5761 * when called due to a ":return" command. "rettv" may point to a typval_T
5762 * with the return rettv. Returns TRUE when the return can be carried out,
5763 * FALSE when the return gets pending.
5764 */
5765 int
5766do_return(
5767 exarg_T *eap,
5768 int reanimate,
5769 int is_cmd,
5770 void *rettv)
5771{
5772 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01005773 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005774
5775 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005776 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005777 current_funccal->returned = FALSE;
5778
5779 /*
5780 * Cleanup (and inactivate) conditionals, but stop when a try conditional
5781 * not in its finally clause (which then is to be executed next) is found.
5782 * In this case, make the ":return" pending for execution at the ":endtry".
5783 * Otherwise, return normally.
5784 */
5785 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
5786 if (idx >= 0)
5787 {
5788 cstack->cs_pending[idx] = CSTP_RETURN;
5789
5790 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005791 // A pending return again gets pending. "rettv" points to an
5792 // allocated variable with the rettv of the original ":return"'s
5793 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005794 cstack->cs_rettv[idx] = rettv;
5795 else
5796 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005797 // When undoing a return in order to make it pending, get the stored
5798 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005799 if (reanimate)
5800 rettv = current_funccal->rettv;
5801
5802 if (rettv != NULL)
5803 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005804 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005805 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
5806 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
5807 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005808 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005809 }
5810 else
5811 cstack->cs_rettv[idx] = NULL;
5812
5813 if (reanimate)
5814 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005815 // The pending return value could be overwritten by a ":return"
5816 // without argument in a finally clause; reset the default
5817 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005818 current_funccal->rettv->v_type = VAR_NUMBER;
5819 current_funccal->rettv->vval.v_number = 0;
5820 }
5821 }
5822 report_make_pending(CSTP_RETURN, rettv);
5823 }
5824 else
5825 {
5826 current_funccal->returned = TRUE;
5827
Bram Moolenaare38eab22019-12-05 21:50:01 +01005828 // If the return is carried out now, store the return value. For
5829 // a return immediately after reanimation, the value is already
5830 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005831 if (!reanimate && rettv != NULL)
5832 {
5833 clear_tv(current_funccal->rettv);
5834 *current_funccal->rettv = *(typval_T *)rettv;
5835 if (!is_cmd)
5836 vim_free(rettv);
5837 }
5838 }
5839
5840 return idx < 0;
5841}
5842
5843/*
5844 * Free the variable with a pending return value.
5845 */
5846 void
5847discard_pending_return(void *rettv)
5848{
5849 free_tv((typval_T *)rettv);
5850}
5851
5852/*
5853 * Generate a return command for producing the value of "rettv". The result
5854 * is an allocated string. Used by report_pending() for verbose messages.
5855 */
5856 char_u *
5857get_return_cmd(void *rettv)
5858{
5859 char_u *s = NULL;
5860 char_u *tofree = NULL;
5861 char_u numbuf[NUMBUFLEN];
5862
5863 if (rettv != NULL)
5864 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
5865 if (s == NULL)
5866 s = (char_u *)"";
5867
5868 STRCPY(IObuff, ":return ");
5869 STRNCPY(IObuff + 8, s, IOSIZE - 8);
5870 if (STRLEN(s) + 8 >= IOSIZE)
5871 STRCPY(IObuff + IOSIZE - 4, "...");
5872 vim_free(tofree);
5873 return vim_strsave(IObuff);
5874}
5875
5876/*
5877 * Get next function line.
5878 * Called by do_cmdline() to get the next line.
5879 * Returns allocated string, or NULL for end of function.
5880 */
5881 char_u *
5882get_func_line(
5883 int c UNUSED,
5884 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02005885 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005886 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005887{
5888 funccall_T *fcp = (funccall_T *)cookie;
5889 ufunc_T *fp = fcp->func;
5890 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005891 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005892
Bram Moolenaare38eab22019-12-05 21:50:01 +01005893 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005894 if (fcp->dbg_tick != debug_tick)
5895 {
5896 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005897 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005898 fcp->dbg_tick = debug_tick;
5899 }
5900#ifdef FEAT_PROFILE
5901 if (do_profiling == PROF_YES)
5902 func_line_end(cookie);
5903#endif
5904
5905 gap = &fp->uf_lines;
5906 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5907 || fcp->returned)
5908 retval = NULL;
5909 else
5910 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005911 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005912 while (fcp->linenr < gap->ga_len
5913 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
5914 ++fcp->linenr;
5915 if (fcp->linenr >= gap->ga_len)
5916 retval = NULL;
5917 else
5918 {
5919 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005920 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005921#ifdef FEAT_PROFILE
5922 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005923 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005924#endif
5925 }
5926 }
5927
Bram Moolenaare38eab22019-12-05 21:50:01 +01005928 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005929 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005930 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005931 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01005932 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005933 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005934 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005935 fcp->dbg_tick = debug_tick;
5936 }
5937
5938 return retval;
5939}
5940
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005941/*
5942 * Return TRUE if the currently active function should be ended, because a
5943 * return was encountered or an error occurred. Used inside a ":while".
5944 */
5945 int
5946func_has_ended(void *cookie)
5947{
5948 funccall_T *fcp = (funccall_T *)cookie;
5949
Bram Moolenaare38eab22019-12-05 21:50:01 +01005950 // Ignore the "abort" flag if the abortion behavior has been changed due to
5951 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005952 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5953 || fcp->returned);
5954}
5955
5956/*
5957 * return TRUE if cookie indicates a function which "abort"s on errors.
5958 */
5959 int
5960func_has_abort(
5961 void *cookie)
5962{
5963 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
5964}
5965
5966
5967/*
5968 * Turn "dict.Func" into a partial for "Func" bound to "dict".
5969 * Don't do this when "Func" is already a partial that was bound
5970 * explicitly (pt_auto is FALSE).
5971 * Changes "rettv" in-place.
5972 * Returns the updated "selfdict_in".
5973 */
5974 dict_T *
5975make_partial(dict_T *selfdict_in, typval_T *rettv)
5976{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005977 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005978 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005979 char_u fname_buf[FLEN_FIXED + 1];
5980 int error;
5981 dict_T *selfdict = selfdict_in;
5982
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005983 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
5984 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005985 fp = rettv->vval.v_partial->pt_func;
5986 else
5987 {
5988 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005989 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005990 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005991 if (fname == NULL)
5992 {
5993 // There is no point binding a dict to a NULL function, just create
5994 // a function reference.
5995 rettv->v_type = VAR_FUNC;
5996 rettv->vval.v_string = NULL;
5997 }
5998 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00005999 {
6000 char_u *tofree = NULL;
6001
6002 // Translate "s:func" to the stored function name.
6003 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6004 fp = find_func(fname, FALSE);
6005 vim_free(tofree);
6006 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006007 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006008
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006009 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006010 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006011 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006012
6013 if (pt != NULL)
6014 {
6015 pt->pt_refcount = 1;
6016 pt->pt_dict = selfdict;
6017 pt->pt_auto = TRUE;
6018 selfdict = NULL;
6019 if (rettv->v_type == VAR_FUNC)
6020 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006021 // Just a function: Take over the function name and use
6022 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006023 pt->pt_name = rettv->vval.v_string;
6024 }
6025 else
6026 {
6027 partial_T *ret_pt = rettv->vval.v_partial;
6028 int i;
6029
Bram Moolenaare38eab22019-12-05 21:50:01 +01006030 // Partial: copy the function name, use selfdict and copy
6031 // args. Can't take over name or args, the partial might
6032 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006033 if (ret_pt->pt_name != NULL)
6034 {
6035 pt->pt_name = vim_strsave(ret_pt->pt_name);
6036 func_ref(pt->pt_name);
6037 }
6038 else
6039 {
6040 pt->pt_func = ret_pt->pt_func;
6041 func_ptr_ref(pt->pt_func);
6042 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006043 if (ret_pt->pt_argc > 0)
6044 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006045 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006046 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006047 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006048 pt->pt_argc = 0;
6049 else
6050 {
6051 pt->pt_argc = ret_pt->pt_argc;
6052 for (i = 0; i < pt->pt_argc; i++)
6053 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6054 }
6055 }
6056 partial_unref(ret_pt);
6057 }
6058 rettv->v_type = VAR_PARTIAL;
6059 rettv->vval.v_partial = pt;
6060 }
6061 }
6062 return selfdict;
6063}
6064
6065/*
6066 * Return the name of the executed function.
6067 */
6068 char_u *
6069func_name(void *cookie)
6070{
6071 return ((funccall_T *)cookie)->func->uf_name;
6072}
6073
6074/*
6075 * Return the address holding the next breakpoint line for a funccall cookie.
6076 */
6077 linenr_T *
6078func_breakpoint(void *cookie)
6079{
6080 return &((funccall_T *)cookie)->breakpoint;
6081}
6082
6083/*
6084 * Return the address holding the debug tick for a funccall cookie.
6085 */
6086 int *
6087func_dbg_tick(void *cookie)
6088{
6089 return &((funccall_T *)cookie)->dbg_tick;
6090}
6091
6092/*
6093 * Return the nesting level for a funccall cookie.
6094 */
6095 int
6096func_level(void *cookie)
6097{
6098 return ((funccall_T *)cookie)->level;
6099}
6100
6101/*
6102 * Return TRUE when a function was ended by a ":return" command.
6103 */
6104 int
6105current_func_returned(void)
6106{
6107 return current_funccal->returned;
6108}
6109
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006110 int
6111free_unref_funccal(int copyID, int testing)
6112{
6113 int did_free = FALSE;
6114 int did_free_funccal = FALSE;
6115 funccall_T *fc, **pfc;
6116
6117 for (pfc = &previous_funccal; *pfc != NULL; )
6118 {
6119 if (can_free_funccal(*pfc, copyID))
6120 {
6121 fc = *pfc;
6122 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006123 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006124 did_free = TRUE;
6125 did_free_funccal = TRUE;
6126 }
6127 else
6128 pfc = &(*pfc)->caller;
6129 }
6130 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006131 // When a funccal was freed some more items might be garbage
6132 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006133 (void)garbage_collect(testing);
6134
6135 return did_free;
6136}
6137
6138/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006139 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006140 */
6141 static funccall_T *
6142get_funccal(void)
6143{
6144 int i;
6145 funccall_T *funccal;
6146 funccall_T *temp_funccal;
6147
6148 funccal = current_funccal;
6149 if (debug_backtrace_level > 0)
6150 {
6151 for (i = 0; i < debug_backtrace_level; i++)
6152 {
6153 temp_funccal = funccal->caller;
6154 if (temp_funccal)
6155 funccal = temp_funccal;
6156 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006157 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006158 debug_backtrace_level = i;
6159 }
6160 }
6161 return funccal;
6162}
6163
6164/*
6165 * Return the hashtable used for local variables in the current funccal.
6166 * Return NULL if there is no current funccal.
6167 */
6168 hashtab_T *
6169get_funccal_local_ht()
6170{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006171 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006172 return NULL;
6173 return &get_funccal()->l_vars.dv_hashtab;
6174}
6175
6176/*
6177 * Return the l: scope variable.
6178 * Return NULL if there is no current funccal.
6179 */
6180 dictitem_T *
6181get_funccal_local_var()
6182{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006183 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006184 return NULL;
6185 return &get_funccal()->l_vars_var;
6186}
6187
6188/*
6189 * Return the hashtable used for argument in the current funccal.
6190 * Return NULL if there is no current funccal.
6191 */
6192 hashtab_T *
6193get_funccal_args_ht()
6194{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006195 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006196 return NULL;
6197 return &get_funccal()->l_avars.dv_hashtab;
6198}
6199
6200/*
6201 * Return the a: scope variable.
6202 * Return NULL if there is no current funccal.
6203 */
6204 dictitem_T *
6205get_funccal_args_var()
6206{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006207 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006208 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01006209 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006210}
6211
6212/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006213 * List function variables, if there is a function.
6214 */
6215 void
6216list_func_vars(int *first)
6217{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006218 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006219 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006220 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006221}
6222
6223/*
6224 * If "ht" is the hashtable for local variables in the current funccal, return
6225 * the dict that contains it.
6226 * Otherwise return NULL.
6227 */
6228 dict_T *
6229get_current_funccal_dict(hashtab_T *ht)
6230{
6231 if (current_funccal != NULL
6232 && ht == &current_funccal->l_vars.dv_hashtab)
6233 return &current_funccal->l_vars;
6234 return NULL;
6235}
6236
6237/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006238 * Search hashitem in parent scope.
6239 */
6240 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006241find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006242{
6243 funccall_T *old_current_funccal = current_funccal;
6244 hashtab_T *ht;
6245 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006246 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006247
6248 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
6249 return NULL;
6250
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006251 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006252 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006253 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006254 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006255 ht = find_var_ht(name, &varname);
6256 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006257 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006258 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006259 if (!HASHITEM_EMPTY(hi))
6260 {
6261 *pht = ht;
6262 break;
6263 }
6264 }
6265 if (current_funccal == current_funccal->func->uf_scoped)
6266 break;
6267 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006268 }
6269 current_funccal = old_current_funccal;
6270
6271 return hi;
6272}
6273
6274/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006275 * Search variable in parent scope.
6276 */
6277 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006278find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006279{
6280 dictitem_T *v = NULL;
6281 funccall_T *old_current_funccal = current_funccal;
6282 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006283 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006284
6285 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
6286 return NULL;
6287
Bram Moolenaare38eab22019-12-05 21:50:01 +01006288 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006289 current_funccal = current_funccal->func->uf_scoped;
6290 while (current_funccal)
6291 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006292 ht = find_var_ht(name, &varname);
6293 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006294 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006295 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006296 if (v != NULL)
6297 break;
6298 }
6299 if (current_funccal == current_funccal->func->uf_scoped)
6300 break;
6301 current_funccal = current_funccal->func->uf_scoped;
6302 }
6303 current_funccal = old_current_funccal;
6304
6305 return v;
6306}
6307
6308/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006309 * Set "copyID + 1" in previous_funccal and callers.
6310 */
6311 int
6312set_ref_in_previous_funccal(int copyID)
6313{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006314 funccall_T *fc;
6315
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006316 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006317 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006318 fc->fc_copyID = copyID + 1;
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006319 if (set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
6320 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
6321 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL))
6322 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006323 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006324 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006325}
6326
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006327 static int
6328set_ref_in_funccal(funccall_T *fc, int copyID)
6329{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006330 if (fc->fc_copyID != copyID)
6331 {
6332 fc->fc_copyID = copyID;
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006333 if (set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
6334 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
6335 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
6336 || set_ref_in_func(NULL, fc->func, copyID))
6337 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006338 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006339 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006340}
6341
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006342/*
6343 * Set "copyID" in all local vars and arguments in the call stack.
6344 */
6345 int
6346set_ref_in_call_stack(int copyID)
6347{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006348 funccall_T *fc;
6349 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006350
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006351 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6352 if (set_ref_in_funccal(fc, copyID))
6353 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006354
6355 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006356 for (entry = funccal_stack; entry != NULL; entry = entry->next)
6357 for (fc = entry->top_funccal; fc != NULL; fc = fc->caller)
6358 if (set_ref_in_funccal(fc, copyID))
6359 return TRUE;
6360 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006361}
6362
6363/*
6364 * Set "copyID" in all functions available by name.
6365 */
6366 int
6367set_ref_in_functions(int copyID)
6368{
6369 int todo;
6370 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006371 ufunc_T *fp;
6372
6373 todo = (int)func_hashtab.ht_used;
6374 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006375 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006376 if (!HASHITEM_EMPTY(hi))
6377 {
6378 --todo;
6379 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006380 if (!func_name_refcount(fp->uf_name)
6381 && set_ref_in_func(NULL, fp, copyID))
6382 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006383 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006384 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006385 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006386}
6387
6388/*
6389 * Set "copyID" in all function arguments.
6390 */
6391 int
6392set_ref_in_func_args(int copyID)
6393{
6394 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006395
6396 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006397 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
6398 copyID, NULL, NULL))
6399 return TRUE;
6400 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006401}
6402
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006403/*
6404 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006405 * Returns TRUE if setting references failed somehow.
6406 */
6407 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006408set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006409{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006410 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006411 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01006412 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006413 char_u fname_buf[FLEN_FIXED + 1];
6414 char_u *tofree = NULL;
6415 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006416 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006417
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006418 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006419 return FALSE;
6420
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006421 if (fp_in == NULL)
6422 {
6423 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006424 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006425 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006426 if (fp != NULL)
6427 {
6428 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006429 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006430 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02006431
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006432 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006433 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006434}
6435
Bram Moolenaare38eab22019-12-05 21:50:01 +01006436#endif // FEAT_EVAL