blob: c33ec63f9a91415d694ddbb05d8e7f95bbe85735 [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;
314 p = skipwhite(p) + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200315 whitep = p;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200316 p = skipwhite(p);
317 expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200318 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200319 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200320 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200321 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200322 if (ga_grow(default_args, 1) == FAIL)
323 goto err_ret;
324
325 // trim trailing whitespace
326 while (p > expr && VIM_ISWHITE(p[-1]))
327 p--;
328 c = *p;
329 *p = NUL;
330 expr = vim_strsave(expr);
331 if (expr == NULL)
332 {
333 *p = c;
334 goto err_ret;
335 }
336 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200337 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200338 default_args->ga_len++;
339 *p = c;
340 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200341 }
342 else
343 mustend = TRUE;
344 }
345 else if (any_default)
346 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000347 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200348 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200349 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200350
351 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
352 {
353 // Be tolerant when skipping
354 if (!skip)
355 {
356 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
357 goto err_ret;
358 }
359 p = skipwhite(p);
360 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200361 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200362 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200363 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200364 // Don't give this error when skipping, it makes the "->" not
365 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100366 // Allow missing space after comma in legacy functions.
367 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200368 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
369 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100370 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200371 goto err_ret;
372 }
373 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200374 else
375 mustend = TRUE;
376 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200377 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200378 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200379 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200380
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200381 if (*p != endchar)
382 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100383 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200384
385 *argp = p;
386 return OK;
387
388err_ret:
389 if (newargs != NULL)
390 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200391 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200392 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200393 return FAIL;
394}
395
396/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100397 * Parse the argument types, filling "fp->uf_arg_types".
398 * Return OK or FAIL.
399 */
400 static int
401parse_argument_types(ufunc_T *fp, garray_T *argtypes, int varargs)
402{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200403 int len = 0;
404
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100405 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
406 if (argtypes->ga_len > 0)
407 {
408 // When "varargs" is set the last name/type goes into uf_va_name
409 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200410 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100411
412 if (len > 0)
413 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
414 if (fp->uf_arg_types != NULL)
415 {
416 int i;
417 type_T *type;
418
419 for (i = 0; i < len; ++ i)
420 {
421 char_u *p = ((char_u **)argtypes->ga_data)[i];
422
423 if (p == NULL)
424 // will get the type from the default value
425 type = &t_unknown;
426 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100427 type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100428 if (type == NULL)
429 return FAIL;
430 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000431 if (i < fp->uf_args.ga_len
432 && (type->tt_type == VAR_FUNC
433 || type->tt_type == VAR_PARTIAL)
434 && var_wrong_func_name(
435 ((char_u **)fp->uf_args.ga_data)[i], TRUE))
436 return FAIL;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100437 }
438 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100439 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200440
441 if (varargs)
442 {
443 char_u *p;
444
445 // Move the last argument "...name: type" to uf_va_name and
446 // uf_va_type.
447 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
448 [fp->uf_args.ga_len - 1];
449 --fp->uf_args.ga_len;
450 p = ((char_u **)argtypes->ga_data)[len];
451 if (p == NULL)
452 // TODO: get type from default value
453 fp->uf_va_type = &t_list_any;
454 else
455 {
456 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
457 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
458 {
459 semsg(_(e_variable_arguments_type_must_be_list_str),
460 ((char_u **)argtypes->ga_data)[len]);
461 return FAIL;
462 }
463 }
464 if (fp->uf_va_type == NULL)
465 return FAIL;
466 }
467
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100468 return OK;
469}
470
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100471 static int
472parse_return_type(ufunc_T *fp, char_u *ret_type)
473{
474 if (ret_type == NULL)
475 fp->uf_ret_type = &t_void;
476 else
477 {
478 char_u *p = ret_type;
479
480 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
481 if (fp->uf_ret_type == NULL)
482 {
483 fp->uf_ret_type = &t_void;
484 return FAIL;
485 }
486 }
487 return OK;
488}
489
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100490/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200491 * Register function "fp" as using "current_funccal" as its scope.
492 */
493 static int
494register_closure(ufunc_T *fp)
495{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200496 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100497 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200498 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200499 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200500 fp->uf_scoped = current_funccal;
501 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200502
Bram Moolenaar58016442016-07-31 18:30:22 +0200503 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
504 return FAIL;
505 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
506 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200507 return OK;
508}
509
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100510 static void
511set_ufunc_name(ufunc_T *fp, char_u *name)
512{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100513 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
514 // actually extends beyond the struct.
515 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100516
517 if (name[0] == K_SPECIAL)
518 {
519 fp->uf_name_exp = alloc(STRLEN(name) + 3);
520 if (fp->uf_name_exp != NULL)
521 {
522 STRCPY(fp->uf_name_exp, "<SNR>");
523 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
524 }
525 }
526}
527
Bram Moolenaar58016442016-07-31 18:30:22 +0200528/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100529 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
530 * return "buf" filled with a readable function name.
531 * Otherwise just return "name", thus the return value can always be used.
532 * "name" and "buf" may be equal.
533 */
534 char_u *
535make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
536{
537 size_t len;
538
539 if (name[0] != K_SPECIAL)
540 return name;
541 len = STRLEN(name);
542 if (len + 3 > bufsize)
543 return name;
544
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100545 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100546 mch_memmove(buf, "<SNR>", 5);
547 return buf;
548}
549
550/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200551 * Get a name for a lambda. Returned in static memory.
552 */
553 char_u *
554get_lambda_name(void)
555{
556 static char_u name[30];
557 static int lambda_no = 0;
558
559 sprintf((char*)name, "<lambda>%d", ++lambda_no);
560 return name;
561}
562
Bram Moolenaar801ab062020-06-25 19:27:56 +0200563#if defined(FEAT_LUA) || defined(PROTO)
564/*
565 * Registers a native C callback which can be called from Vim script.
566 * Returns the name of the Vim script function.
567 */
568 char_u *
569register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
570{
571 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200572 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200573
574 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
575 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200576 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200577
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200578 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200579 fp->uf_refcount = 1;
580 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000581 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200582 fp->uf_calls = 0;
583 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200584 fp->uf_cb = cb;
585 fp->uf_cb_free = cb_free;
586 fp->uf_cb_state = state;
587
588 set_ufunc_name(fp, name);
589 hash_add(&func_hashtab, UF2HIKEY(fp));
590
591 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200592}
593#endif
594
Bram Moolenaar04b12692020-05-04 23:24:44 +0200595/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100596 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100597 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100598 * If "white_error" is not NULL check for correct use of white space and set
599 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100600 * Return NULL if no valid arrow found.
601 */
602 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100603skip_arrow(
604 char_u *start,
605 int equal_arrow,
606 char_u **ret_type,
607 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100608{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100609 char_u *s = start;
610 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100611
612 if (equal_arrow)
613 {
614 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100615 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100616 if (white_error != NULL && !VIM_ISWHITE(s[1]))
617 {
618 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100619 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100620 return NULL;
621 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100622 s = skipwhite(s + 1);
623 *ret_type = s;
624 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100625 if (s == *ret_type)
626 {
627 emsg(_(e_missing_return_type));
628 return NULL;
629 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100630 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100631 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100632 s = skipwhite(s);
633 if (*s != '=')
634 return NULL;
635 ++s;
636 }
637 if (*s != '>')
638 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100639 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
640 || !IS_WHITE_OR_NUL(s[1])))
641 {
642 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100643 semsg(_(e_white_space_required_before_and_after_str_at_str),
644 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100645 return NULL;
646 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100647 return skipwhite(s + 1);
648}
649
650/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100651 * Check if "*cmd" points to a function command and if so advance "*cmd" and
652 * return TRUE.
653 * Otherwise return FALSE;
654 * Do not consider "function(" to be a command.
655 */
656 static int
657is_function_cmd(char_u **cmd)
658{
659 char_u *p = *cmd;
660
661 if (checkforcmd(&p, "function", 2))
662 {
663 if (*p == '(')
664 return FALSE;
665 *cmd = p;
666 return TRUE;
667 }
668 return FALSE;
669}
670
671/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200672 * Called when defining a function: The context may be needed for script
673 * variables declared in a block that is visible now but not when the function
674 * is compiled or called later.
675 */
676 static void
677function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
678{
679 if (cstack != NULL && cstack->cs_idx >= 0)
680 {
681 int count = cstack->cs_idx + 1;
682 int i;
683
684 fp->uf_block_ids = ALLOC_MULT(int, count);
685 if (fp->uf_block_ids != NULL)
686 {
687 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
688 sizeof(int) * count);
689 fp->uf_block_depth = count;
690 }
691
692 // Set flag in each block to indicate a function was defined. This
693 // is used to keep the variable when leaving the block, see
694 // hide_script_var().
695 for (i = 0; i <= cstack->cs_idx; ++i)
696 cstack->cs_flags[i] |= CSF_FUNC_DEF;
697 }
698}
699
700/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100701 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200702 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100703 * "newlines" must already have been initialized.
704 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
705 */
706 static int
707get_function_body(
708 exarg_T *eap,
709 garray_T *newlines,
710 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000711 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100712{
713 linenr_T sourcing_lnum_top = SOURCING_LNUM;
714 linenr_T sourcing_lnum_off;
715 int saved_wait_return = need_wait_return;
716 char_u *line_arg = line_arg_in;
717 int vim9_function = eap->cmdidx == CMD_def
718 || eap->cmdidx == CMD_block;
719#define MAX_FUNC_NESTING 50
720 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200721 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100722 int nesting = 0;
723 getline_opt_T getline_options;
724 int indent = 2;
725 char_u *skip_until = NULL;
726 int ret = FAIL;
727 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200728 int heredoc_concat_len = 0;
729 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100730 char_u *heredoc_trimmed = NULL;
731
Bram Moolenaar20677332021-06-06 17:02:53 +0200732 ga_init2(&heredoc_ga, 1, 500);
733
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100734 // Detect having skipped over comment lines to find the return
735 // type. Add NULL lines to keep the line count correct.
736 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
737 if (SOURCING_LNUM < sourcing_lnum_off)
738 {
739 sourcing_lnum_off -= SOURCING_LNUM;
740 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
741 goto theend;
742 while (sourcing_lnum_off-- > 0)
743 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
744 }
745
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200746 nesting_def[0] = vim9_function;
747 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100748 getline_options = vim9_function
749 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
750 for (;;)
751 {
752 char_u *theline;
753 char_u *p;
754 char_u *arg;
755
756 if (KeyTyped)
757 {
758 msg_scroll = TRUE;
759 saved_wait_return = FALSE;
760 }
761 need_wait_return = FALSE;
762
763 if (line_arg != NULL)
764 {
765 // Use eap->arg, split up in parts by line breaks.
766 theline = line_arg;
767 p = vim_strchr(theline, '\n');
768 if (p == NULL)
769 line_arg += STRLEN(line_arg);
770 else
771 {
772 *p = NUL;
773 line_arg = p + 1;
774 }
775 }
776 else
777 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000778 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100779 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100780 }
781 if (KeyTyped)
782 lines_left = Rows - 1;
783 if (theline == NULL)
784 {
785 // Use the start of the function for the line number.
786 SOURCING_LNUM = sourcing_lnum_top;
787 if (skip_until != NULL)
788 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200789 else if (nesting_inline[nesting])
790 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100791 else if (eap->cmdidx == CMD_def)
792 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100793 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000794 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100795 goto theend;
796 }
797
798 // Detect line continuation: SOURCING_LNUM increased more than one.
799 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
800 if (SOURCING_LNUM < sourcing_lnum_off)
801 sourcing_lnum_off -= SOURCING_LNUM;
802 else
803 sourcing_lnum_off = 0;
804
805 if (skip_until != NULL)
806 {
807 // Don't check for ":endfunc"/":enddef" between
808 // * ":append" and "."
809 // * ":python <<EOF" and "EOF"
810 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
811 if (heredoc_trimmed == NULL
812 || (is_heredoc && skipwhite(theline) == theline)
813 || STRNCMP(theline, heredoc_trimmed,
814 STRLEN(heredoc_trimmed)) == 0)
815 {
816 if (heredoc_trimmed == NULL)
817 p = theline;
818 else if (is_heredoc)
819 p = skipwhite(theline) == theline
820 ? theline : theline + STRLEN(heredoc_trimmed);
821 else
822 p = theline + STRLEN(heredoc_trimmed);
823 if (STRCMP(p, skip_until) == 0)
824 {
825 VIM_CLEAR(skip_until);
826 VIM_CLEAR(heredoc_trimmed);
827 getline_options = vim9_function
828 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
829 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200830
831 if (heredoc_concat_len > 0)
832 {
833 // Replace the starting line with all the concatenated
834 // lines.
835 ga_concat(&heredoc_ga, theline);
836 vim_free(((char_u **)(newlines->ga_data))[
837 heredoc_concat_len - 1]);
838 ((char_u **)(newlines->ga_data))[
839 heredoc_concat_len - 1] = heredoc_ga.ga_data;
840 ga_init(&heredoc_ga);
841 heredoc_concat_len = 0;
842 theline += STRLEN(theline); // skip the "EOF"
843 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100844 }
845 }
846 }
847 else
848 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200849 int c;
850 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +0000851 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100852
853 // skip ':' and blanks
854 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
855 ;
856
857 // Check for "endfunction", "enddef" or "}".
858 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +0000859 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200860 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100861 ? *p == '}'
862 : (checkforcmd(&p, nesting_def[nesting]
863 ? "enddef" : "endfunction", 4)
864 && *p != ':'))
865 {
Bram Moolenaarb2175222022-03-05 20:24:41 +0000866 if (!nesting_inline[nesting] && nesting_def[nesting]
867 && p < cmd + 6)
868 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100869 if (nesting-- == 0)
870 {
871 char_u *nextcmd = NULL;
872
873 if (*p == '|' || *p == '}')
874 nextcmd = p + 1;
875 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
876 nextcmd = line_arg;
877 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100878 && (vim9_function || p_verbose > 0))
879 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200880 SOURCING_LNUM = sourcing_lnum_top
881 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100882 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000883 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100884 else
885 give_warning2((char_u *)
886 _("W22: Text found after :endfunction: %s"),
887 p, TRUE);
888 }
889 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100890 {
891 // Another command follows. If the line came from "eap"
892 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000893 // change "eap->cmdlinep" to point to the last fetched
894 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100895 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000896 if (lines_to_free->ga_len > 0
897 && *eap->cmdlinep !=
898 ((char_u **)lines_to_free->ga_data)
899 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100900 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000901 // *cmdlinep will be freed later, thus remove the
902 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100903 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000904 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
905 [lines_to_free->ga_len - 1];
906 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100907 }
908 }
909 break;
910 }
911 }
912
913 // Check for mismatched "endfunc" or "enddef".
914 // We don't check for "def" inside "func" thus we also can't check
915 // for "enddef".
916 // We continue to find the end of the function, although we might
917 // not find it.
918 else if (nesting_def[nesting])
919 {
920 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
921 emsg(_(e_mismatched_endfunction));
922 }
923 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
924 emsg(_(e_mismatched_enddef));
925
926 // Increase indent inside "if", "while", "for" and "try", decrease
927 // at "end".
928 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
929 indent -= 2;
930 else if (STRNCMP(p, "if", 2) == 0
931 || STRNCMP(p, "wh", 2) == 0
932 || STRNCMP(p, "for", 3) == 0
933 || STRNCMP(p, "try", 3) == 0)
934 indent += 2;
935
936 // Check for defining a function inside this function.
937 // Only recognize "def" inside "def", not inside "function",
938 // For backwards compatibility, see Test_function_python().
939 c = *p;
940 if (is_function_cmd(&p)
941 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
942 {
943 if (*p == '!')
944 p = skipwhite(p + 1);
945 p += eval_fname_script(p);
946 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL,
947 NULL, NULL));
948 if (*skipwhite(p) == '(')
949 {
950 if (nesting == MAX_FUNC_NESTING - 1)
951 emsg(_(e_function_nesting_too_deep));
952 else
953 {
954 ++nesting;
955 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200956 nesting_inline[nesting] = FALSE;
957 indent += 2;
958 }
959 }
960 }
961
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200962 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200963 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200964 // Not a comment line: check for nested inline function.
965 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200966 while (end > p && VIM_ISWHITE(*end))
967 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +0200968 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200969 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200970 int is_block;
971
972 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200973 --end;
974 while (end > p && VIM_ISWHITE(*end))
975 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200976 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
977 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200978 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200979 char_u *s = p;
980
981 // check for line starting with "au" for :autocmd or
982 // "com" for :command, these can use a {} block
983 is_block = checkforcmd_noparen(&s, "autocmd", 2)
984 || checkforcmd_noparen(&s, "command", 3);
985 }
986
987 if (is_block)
988 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200989 if (nesting == MAX_FUNC_NESTING - 1)
990 emsg(_(e_function_nesting_too_deep));
991 else
992 {
993 ++nesting;
994 nesting_def[nesting] = TRUE;
995 nesting_inline[nesting] = TRUE;
996 indent += 2;
997 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100998 }
999 }
1000 }
1001
1002 // Check for ":append", ":change", ":insert". Not for :def.
1003 p = skip_range(p, FALSE, NULL);
1004 if (!vim9_function
1005 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1006 || (p[0] == 'c'
1007 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1008 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1009 && (STRNCMP(&p[3], "nge", 3) != 0
1010 || !ASCII_ISALPHA(p[6])))))))
1011 || (p[0] == 'i'
1012 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1013 && (!ASCII_ISALPHA(p[2])
1014 || (p[2] == 's'
1015 && (!ASCII_ISALPHA(p[3])
1016 || p[3] == 'e'))))))))
1017 skip_until = vim_strsave((char_u *)".");
1018
1019 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1020 arg = skipwhite(skiptowhite(p));
1021 if (arg[0] == '<' && arg[1] =='<'
1022 && ((p[0] == 'p' && p[1] == 'y'
1023 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1024 || ((p[2] == '3' || p[2] == 'x')
1025 && !ASCII_ISALPHA(p[3]))))
1026 || (p[0] == 'p' && p[1] == 'e'
1027 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1028 || (p[0] == 't' && p[1] == 'c'
1029 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1030 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1031 && !ASCII_ISALPHA(p[3]))
1032 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1033 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1034 || (p[0] == 'm' && p[1] == 'z'
1035 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1036 ))
1037 {
1038 // ":python <<" continues until a dot, like ":append"
1039 p = skipwhite(arg + 2);
1040 if (STRNCMP(p, "trim", 4) == 0)
1041 {
1042 // Ignore leading white space.
1043 p = skipwhite(p + 4);
1044 heredoc_trimmed = vim_strnsave(theline,
1045 skipwhite(theline) - theline);
1046 }
1047 if (*p == NUL)
1048 skip_until = vim_strsave((char_u *)".");
1049 else
1050 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1051 getline_options = GETLINE_NONE;
1052 is_heredoc = TRUE;
Bram Moolenaard881d152022-05-13 13:50:36 +01001053 if (eap->cmdidx == CMD_def && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001054 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001055 }
1056
Bram Moolenaard881d152022-05-13 13:50:36 +01001057 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001058 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001059 // Check for ":cmd v =<< [trim] EOF"
1060 // and ":cmd [a, b] =<< [trim] EOF"
1061 // and "lines =<< [trim] EOF" for Vim9
1062 // Where "cmd" can be "let", "var", "final" or "const".
1063 arg = skipwhite(skiptowhite(p));
1064 if (*arg == '[')
1065 arg = vim_strchr(arg, ']');
1066 if (arg != NULL)
1067 {
1068 int found = (eap->cmdidx == CMD_def && arg[0] == '='
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001069 && arg[1] == '<' && arg[2] =='<');
1070
Bram Moolenaard881d152022-05-13 13:50:36 +01001071 if (!found)
1072 // skip over the argument after "cmd"
1073 arg = skipwhite(skiptowhite(arg));
1074 if (found || (arg[0] == '=' && arg[1] == '<'
1075 && arg[2] =='<'
1076 && (checkforcmd(&p, "let", 2)
1077 || checkforcmd(&p, "var", 3)
1078 || checkforcmd(&p, "final", 5)
1079 || checkforcmd(&p, "const", 5))))
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001080 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001081 p = skipwhite(arg + 3);
1082 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001083 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001084 if (STRNCMP(p, "trim", 4) == 0)
1085 {
1086 // Ignore leading white space.
1087 p = skipwhite(p + 4);
1088 heredoc_trimmed = vim_strnsave(theline,
1089 skipwhite(theline) - theline);
1090 continue;
1091 }
1092 if (STRNCMP(p, "eval", 4) == 0)
1093 {
1094 // Ignore leading white space.
1095 p = skipwhite(p + 4);
1096 continue;
1097 }
1098 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001099 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001100 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1101 getline_options = GETLINE_NONE;
1102 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001103 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001104 }
1105 }
1106 }
1107
1108 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001109 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001110 goto theend;
1111
Bram Moolenaar20677332021-06-06 17:02:53 +02001112 if (heredoc_concat_len > 0)
1113 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001114 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001115 // to be used for the instruction later.
1116 ga_concat(&heredoc_ga, theline);
1117 ga_concat(&heredoc_ga, (char_u *)"\n");
1118 p = vim_strsave((char_u *)"");
1119 }
1120 else
1121 {
1122 // Copy the line to newly allocated memory. get_one_sourceline()
1123 // allocates 250 bytes per line, this saves 80% on average. The
1124 // cost is an extra alloc/free.
1125 p = vim_strsave(theline);
1126 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001127 if (p == NULL)
1128 goto theend;
1129 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1130
1131 // Add NULL lines for continuation lines, so that the line count is
1132 // equal to the index in the growarray.
1133 while (sourcing_lnum_off-- > 0)
1134 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1135
1136 // Check for end of eap->arg.
1137 if (line_arg != NULL && *line_arg == NUL)
1138 line_arg = NULL;
1139 }
1140
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001141 // Return OK when no error was detected.
1142 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001143 ret = OK;
1144
1145theend:
1146 vim_free(skip_until);
1147 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001148 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001149 need_wait_return |= saved_wait_return;
1150 return ret;
1151}
1152
1153/*
1154 * Handle the body of a lambda. *arg points to the "{", process statements
1155 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001156 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001157 * When successful "rettv" is set to a funcref.
1158 */
1159 static int
1160lambda_function_body(
1161 char_u **arg,
1162 typval_T *rettv,
1163 evalarg_T *evalarg,
1164 garray_T *newargs,
1165 garray_T *argtypes,
1166 int varargs,
1167 garray_T *default_args,
1168 char_u *ret_type)
1169{
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001170 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001171 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001172 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001173 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001174 exarg_T eap;
1175 garray_T newlines;
1176 char_u *cmdline = NULL;
1177 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001178 partial_T *pt;
1179 char_u *name;
1180 int lnum_save = -1;
1181 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1182
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001183 if (!ends_excmd2(*arg, skipwhite(*arg + 1)))
1184 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001185 semsg(_(e_trailing_characters_str), *arg + 1);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001186 return FAIL;
1187 }
1188
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001189 CLEAR_FIELD(eap);
1190 eap.cmdidx = CMD_block;
1191 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001192 eap.cmdlinep = &cmdline;
1193 eap.skip = !evaluate;
1194 if (evalarg->eval_cctx != NULL)
1195 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1196 else
1197 {
1198 eap.getline = evalarg->eval_getline;
1199 eap.cookie = evalarg->eval_cookie;
1200 }
1201
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001202 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001203 if (get_function_body(&eap, &newlines, NULL,
1204 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001205 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001206
1207 // When inside a lambda must add the function lines to evalarg.eval_ga.
1208 evalarg->eval_break_count += newlines.ga_len;
1209 if (gap->ga_itemsize > 0)
1210 {
1211 int idx;
1212 char_u *last;
1213 size_t plen;
1214 char_u *pnl;
1215
1216 for (idx = 0; idx < newlines.ga_len; ++idx)
1217 {
1218 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1219
Bram Moolenaarecb66452021-05-18 15:09:18 +02001220 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001221 goto erret;
1222
1223 // Going to concatenate the lines after parsing. For an empty or
1224 // comment line use an empty string.
1225 // Insert NL characters at the start of each line, the string will
1226 // be split again later in .get_lambda_tv().
1227 if (*p == NUL || vim9_comment_start(p))
1228 p = (char_u *)"";
1229 plen = STRLEN(p);
1230 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1231 if (pnl != NULL)
1232 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001233 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1234 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001235 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001236 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001237 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001238 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001239 // more is following after the "}", which was skipped
1240 last = cmdline;
1241 else
1242 // nothing is following the "}"
1243 last = (char_u *)"}";
1244 plen = STRLEN(last);
1245 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1246 if (pnl != NULL)
1247 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001248 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1249 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001250 }
1251
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001252 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001253 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001254 garray_T *tfgap = &evalarg->eval_tofree_ga;
1255
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001256 // Something comes after the "}".
1257 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001258
1259 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001260 if (ga_grow(tfgap, 1) == OK)
1261 {
1262 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1263 evalarg->eval_using_cmdline = TRUE;
1264 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001265 }
1266 else
1267 *arg = (char_u *)"";
1268
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001269 if (!evaluate)
1270 {
1271 ret = OK;
1272 goto erret;
1273 }
1274
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001275 name = get_lambda_name();
1276 ufunc = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
1277 if (ufunc == NULL)
1278 goto erret;
1279 set_ufunc_name(ufunc, name);
1280 if (hash_add(&func_hashtab, UF2HIKEY(ufunc)) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001281 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001282 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001283 ufunc->uf_refcount = 1;
1284 ufunc->uf_args = *newargs;
1285 newargs->ga_data = NULL;
1286 ufunc->uf_def_args = *default_args;
1287 default_args->ga_data = NULL;
1288 ufunc->uf_func_type = &t_func_any;
1289
1290 // error messages are for the first function line
1291 lnum_save = SOURCING_LNUM;
1292 SOURCING_LNUM = sourcing_lnum_top;
1293
1294 // parse argument types
1295 if (parse_argument_types(ufunc, argtypes, varargs) == FAIL)
1296 {
1297 SOURCING_LNUM = lnum_save;
1298 goto erret;
1299 }
1300
1301 // parse the return type, if any
1302 if (parse_return_type(ufunc, ret_type) == FAIL)
1303 goto erret;
1304
1305 pt = ALLOC_CLEAR_ONE(partial_T);
1306 if (pt == NULL)
1307 goto erret;
1308 pt->pt_func = ufunc;
1309 pt->pt_refcount = 1;
1310
1311 ufunc->uf_lines = newlines;
1312 newlines.ga_data = NULL;
1313 if (sandbox)
1314 ufunc->uf_flags |= FC_SANDBOX;
1315 if (!ASCII_ISUPPER(*ufunc->uf_name))
1316 ufunc->uf_flags |= FC_VIM9;
1317 ufunc->uf_script_ctx = current_sctx;
1318 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1319 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1320 set_function_type(ufunc);
1321
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001322 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1323
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001324 rettv->vval.v_partial = pt;
1325 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001326 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001327 ret = OK;
1328
1329erret:
1330 if (lnum_save >= 0)
1331 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001332 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001333 if (newargs != NULL)
1334 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001335 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001336 if (ufunc != NULL)
1337 {
1338 func_clear(ufunc, TRUE);
1339 func_free(ufunc, TRUE);
1340 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001341 return ret;
1342}
1343
1344/*
1345 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001346 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001347 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001348 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1349 */
1350 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001351get_lambda_tv(
1352 char_u **arg,
1353 typval_T *rettv,
1354 int types_optional,
1355 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001356{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001357 int evaluate = evalarg != NULL
1358 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001359 garray_T newargs;
1360 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001361 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001362 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001363 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001364 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001365 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001366 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001367 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001368 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001369 char_u *s;
1370 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001371 int *old_eval_lavars = eval_lavars_used;
1372 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001373 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001374 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001375 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001376 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001377 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001378 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001379
Bram Moolenaar4525a572022-02-13 11:57:33 +00001380 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001381 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001382
1383 ga_init(&newargs);
1384 ga_init(&newlines);
1385
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001386 // First, check if this is really a lambda expression. "->" or "=>" must
1387 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001388 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001389 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001390 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar14ded112021-06-26 19:25:49 +02001391 NULL, &default_args, TRUE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001392 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001393 {
1394 if (types_optional)
1395 ga_clear_strings(&argtypes);
Bram Moolenaar0346b792021-01-31 22:18:29 +01001396 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001397 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001398
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001399 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001400 if (evaluate)
1401 pnewargs = &newargs;
1402 else
1403 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001404 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001405 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001406 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001407 &varargs, &default_args,
1408 FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001409 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001410 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001411 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001412 {
1413 if (types_optional)
1414 ga_clear_strings(&argtypes);
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001415 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001416 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001417 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001418 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001419
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001420 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1421 if (ret_type != NULL)
1422 {
1423 ret_type = vim_strsave(ret_type);
1424 tofree2 = ret_type;
1425 }
1426
Bram Moolenaare38eab22019-12-05 21:50:01 +01001427 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001428 if (evaluate)
1429 eval_lavars_used = &eval_lavars;
1430
Bram Moolenaar65c44152020-12-24 15:14:01 +01001431 *arg = skipwhite_and_linebreak(*arg, evalarg);
1432
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001433 // Recognize "{" as the start of a function body.
1434 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001435 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001436 if (evalarg == NULL)
1437 // cannot happen?
1438 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001439 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001440 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1441 types_optional ? &argtypes : NULL, varargs,
1442 &default_args, ret_type) == FAIL)
1443 goto errret;
1444 goto theend;
1445 }
1446 if (default_args.ga_len > 0)
1447 {
1448 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001449 goto errret;
1450 }
1451
Bram Moolenaare38eab22019-12-05 21:50:01 +01001452 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001453 start = *arg;
1454 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001455 if (ret == FAIL)
1456 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001457
Bram Moolenaar65c44152020-12-24 15:14:01 +01001458 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001459 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001460 *arg = skipwhite_and_linebreak(*arg, evalarg);
1461 if (**arg != '}')
1462 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001463 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001464 goto errret;
1465 }
1466 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001467 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001468
1469 if (evaluate)
1470 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001471 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001472 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001473 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001474 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001475 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001476
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001477 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001478 if (fp == NULL)
1479 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001480 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001481 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001482 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001483 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001484
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001485 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001486 if (ga_grow(&newlines, 1) == FAIL)
1487 goto errret;
1488
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001489 // If there are line breaks, we need to split up the string.
1490 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001491 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001492 line_end = end;
1493
1494 // Add "return " before the expression (or the first line).
1495 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001496 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001497 if (p == NULL)
1498 goto errret;
1499 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1500 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001501 vim_strncpy(p + 7, start, line_end - start);
1502
1503 if (line_end != end)
1504 {
1505 // Add more lines, split by line breaks. Thus is used when a
1506 // lambda with { cmds } is encountered.
1507 while (*line_end == '\n')
1508 {
1509 if (ga_grow(&newlines, 1) == FAIL)
1510 goto errret;
1511 start = line_end + 1;
1512 line_end = vim_strchr(start, '\n');
1513 if (line_end == NULL)
1514 line_end = end;
1515 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1516 vim_strnsave(start, line_end - start);
1517 }
1518 }
1519
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001520 if (strstr((char *)p + 7, "a:") == NULL)
1521 // No a: variables are used for sure.
1522 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001523
1524 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001525 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001526 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001527 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001528 if (types_optional)
1529 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001530 if (parse_argument_types(fp, &argtypes,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001531 vim9script && varargs) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001532 goto errret;
1533 if (ret_type != NULL)
1534 {
1535 fp->uf_ret_type = parse_type(&ret_type,
1536 &fp->uf_type_list, TRUE);
1537 if (fp->uf_ret_type == NULL)
1538 goto errret;
1539 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001540 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001541 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001542 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001543
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001544 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001545 if (current_funccal != NULL && eval_lavars)
1546 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001547 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001548 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001549 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001550 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001551
1552#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001553 if (prof_def_func())
1554 func_do_profile(fp);
1555#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001556 if (sandbox)
1557 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001558 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001559 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001560 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001561 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001562 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001563 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001564 // Use the line number of the arguments.
1565 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001566
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001567 function_using_block_scopes(fp, evalarg->eval_cstack);
1568
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001569 pt->pt_func = fp;
1570 pt->pt_refcount = 1;
1571 rettv->vval.v_partial = pt;
1572 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001573
1574 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001575 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001576
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001577theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001578 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001579 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001580 if (types_optional)
1581 ga_clear_strings(&argtypes);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001582
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001583 return OK;
1584
1585errret:
1586 ga_clear_strings(&newargs);
1587 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001588 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001589 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001590 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001591 ga_clear_strings(&argtypes);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001592 if (fp != NULL)
1593 vim_free(fp->uf_arg_types);
1594 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001595 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001596 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001597 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001598 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001599 return FAIL;
1600}
1601
1602/*
1603 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1604 * name it contains, otherwise return "name".
1605 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1606 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001607 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1608 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001609 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001610 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001611 */
1612 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001613deref_func_name(
1614 char_u *name,
1615 int *lenp,
1616 partial_T **partialp,
1617 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001618 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001619 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001620 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001621{
1622 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001623 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001624 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001625 char_u *s = NULL;
1626 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001627 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001628
1629 if (partialp != NULL)
1630 *partialp = NULL;
1631
1632 cc = name[*lenp];
1633 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001634
Bram Moolenaar71f21932022-01-07 18:20:55 +00001635 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001636 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001637 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001638 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001639 tv = &v->di_tv;
1640 }
1641 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1642 {
1643 imported_T *import;
1644 char_u *p = name;
1645 int len = *lenp;
1646
1647 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001648 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001649 p = name + 2;
1650 len -= 2;
1651 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001652 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001653
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001654 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001655 if (import != NULL)
1656 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001657 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001658 if (new_function)
1659 semsg(_(e_redefining_imported_item_str), name);
1660 else
1661 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001662 name[len] = cc;
1663 *lenp = 0;
1664 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001665 }
1666 }
1667
1668 if (tv != NULL)
1669 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001670 if (found_var != NULL)
1671 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001672 if (tv->v_type == VAR_FUNC)
1673 {
1674 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001675 {
1676 *lenp = 0;
1677 return (char_u *)""; // just in case
1678 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001679 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001680 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001681 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001682
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001683 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001684 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001685 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001686
1687 if (pt == NULL)
1688 {
1689 *lenp = 0;
1690 return (char_u *)""; // just in case
1691 }
1692 if (partialp != NULL)
1693 *partialp = pt;
1694 s = partial_name(pt);
1695 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001696 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001697
1698 if (s != NULL)
1699 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001700 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001701 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001702 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001703
1704 if (sv != NULL)
1705 *type = sv->sv_type;
1706 }
1707 return s;
1708 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001709 }
1710
1711 return name;
1712}
1713
1714/*
1715 * Give an error message with a function name. Handle <SNR> things.
1716 * "ermsg" is to be passed without translation, use N_() instead of _().
1717 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001718 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001719emsg_funcname(char *ermsg, char_u *name)
1720{
Bram Moolenaar54598062022-01-13 12:05:09 +00001721 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001722
Bram Moolenaar54598062022-01-13 12:05:09 +00001723 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001724 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001725 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001726 if (p != name)
1727 vim_free(p);
1728}
1729
1730/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001731 * Get function arguments at "*arg" and advance it.
1732 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001733 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001734 static int
1735get_func_arguments(
1736 char_u **arg,
1737 evalarg_T *evalarg,
1738 int partial_argc,
1739 typval_T *argvars,
1740 int *argcount)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001741{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001742 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001743 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001744 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001745 int evaluate = evalarg == NULL
1746 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001747
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001748 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001749 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001750 // skip the '(' or ',' and possibly line breaks
1751 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1752
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001753 if (*argp == ')' || *argp == ',' || *argp == NUL)
1754 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001755 if (eval1(&argp, &argvars[*argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001756 {
1757 ret = FAIL;
1758 break;
1759 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001760 ++*argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001761 // The comma should come right after the argument, but this wasn't
1762 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001763 if (vim9script)
1764 {
1765 if (*argp != ',' && *skipwhite(argp) == ',')
1766 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001767 if (evaluate)
1768 semsg(_(e_no_white_space_allowed_before_str_str),
1769 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001770 ret = FAIL;
1771 break;
1772 }
1773 }
1774 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001775 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001776 if (*argp != ',')
1777 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001778 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
1779 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001780 if (evaluate)
1781 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001782 ret = FAIL;
1783 break;
1784 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001785 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001786
Bram Moolenaare6b53242020-07-01 17:28:33 +02001787 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001788 if (*argp == ')')
1789 ++argp;
1790 else
1791 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001792 *arg = argp;
1793 return ret;
1794}
1795
1796/*
1797 * Call a function and put the result in "rettv".
1798 * Return OK or FAIL.
1799 */
1800 int
1801get_func_tv(
1802 char_u *name, // name of the function
1803 int len, // length of "name" or -1 to use strlen()
1804 typval_T *rettv,
1805 char_u **arg, // argument, pointing to the '('
1806 evalarg_T *evalarg, // for line continuation
1807 funcexe_T *funcexe) // various values
1808{
1809 char_u *argp;
1810 int ret = OK;
1811 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1812 int argcount = 0; // number of arguments found
1813 int vim9script = in_vim9script();
1814 int evaluate = evalarg == NULL
1815 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
1816
1817 argp = *arg;
1818 ret = get_func_arguments(&argp, evalarg,
1819 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
1820 argvars, &argcount);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001821
1822 if (ret == OK)
1823 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001824 int i = 0;
1825 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001826
1827 if (get_vim_var_nr(VV_TESTING))
1828 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001829 // Prepare for calling test_garbagecollect_now(), need to know
1830 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001831 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001832 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001833 for (i = 0; i < argcount; ++i)
1834 if (ga_grow(&funcargs, 1) == OK)
1835 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1836 &argvars[i];
1837 }
1838
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001839 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00001840 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001841 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001842 // An error in a builtin function does not return FAIL, but we do
1843 // want to abort further processing if an error was given.
1844 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001845 clear_tv(rettv);
1846 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001847
1848 funcargs.ga_len -= i;
1849 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001850 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001851 {
1852 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001853 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001854 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001855 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001856 }
1857
1858 while (--argcount >= 0)
1859 clear_tv(&argvars[argcount]);
1860
Bram Moolenaar4525a572022-02-13 11:57:33 +00001861 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02001862 *arg = argp;
1863 else
1864 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001865 return ret;
1866}
1867
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001868/*
1869 * Return TRUE if "p" starts with "<SID>" or "s:".
1870 * Only works if eval_fname_script() returned non-zero for "p"!
1871 */
1872 static int
1873eval_fname_sid(char_u *p)
1874{
1875 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
1876}
1877
1878/*
1879 * In a script change <SID>name() and s:name() to K_SNR 123_name().
1880 * Change <SNR>123_name() to K_SNR 123_name().
1881 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
1882 * (slow).
1883 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001884 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001885fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
1886{
1887 int llen;
1888 char_u *fname;
1889 int i;
1890
1891 llen = eval_fname_script(name);
1892 if (llen > 0)
1893 {
1894 fname_buf[0] = K_SPECIAL;
1895 fname_buf[1] = KS_EXTRA;
1896 fname_buf[2] = (int)KE_SNR;
1897 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001898 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001899 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001900 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001901 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001902 else
1903 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001904 sprintf((char *)fname_buf + 3, "%ld_",
1905 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001906 i = (int)STRLEN(fname_buf);
1907 }
1908 }
1909 if (i + STRLEN(name + llen) < FLEN_FIXED)
1910 {
1911 STRCPY(fname_buf + i, name + llen);
1912 fname = fname_buf;
1913 }
1914 else
1915 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001916 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001917 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +01001918 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001919 else
1920 {
1921 *tofree = fname;
1922 mch_memmove(fname, fname_buf, (size_t)i);
1923 STRCPY(fname + i, name + llen);
1924 }
1925 }
1926 }
1927 else
1928 fname = name;
1929 return fname;
1930}
1931
1932/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001933 * Concatenate the script ID and function name into "<SNR>99_name".
1934 * "buffer" must have size MAX_FUNC_NAME_LEN.
1935 */
1936 void
1937func_name_with_sid(char_u *name, int sid, char_u *buffer)
1938{
1939 // A script-local function is stored as "<SNR>99_name".
1940 buffer[0] = K_SPECIAL;
1941 buffer[1] = KS_EXTRA;
1942 buffer[2] = (int)KE_SNR;
1943 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
1944 (long)sid, name);
1945}
1946
1947/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001948 * Find a function "name" in script "sid".
1949 */
1950 static ufunc_T *
1951find_func_with_sid(char_u *name, int sid)
1952{
Bram Moolenaarb8822442022-01-11 15:24:05 +00001953 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001954 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955
Bram Moolenaarb8822442022-01-11 15:24:05 +00001956 if (!SCRIPT_ID_VALID(sid))
1957 return NULL; // not in a script
1958
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001959 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960 hi = hash_find(&func_hashtab, buffer);
1961 if (!HASHITEM_EMPTY(hi))
1962 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00001963 return NULL;
1964}
1965
1966/*
1967 * Find a function "name" in script "sid" prefixing the autoload prefix.
1968 */
1969 static ufunc_T *
1970find_func_with_prefix(char_u *name, int sid)
1971{
1972 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001973 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00001974 scriptitem_T *si;
1975
Bram Moolenaar848fadd2022-01-30 15:28:30 +00001976 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00001977 return NULL; // already has the prefix
1978 if (!SCRIPT_ID_VALID(sid))
1979 return NULL; // not in a script
1980 si = SCRIPT_ITEM(sid);
1981 if (si->sn_autoload_prefix != NULL)
1982 {
1983 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
1984 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00001985 char_u *namep;
1986
1987 // skip a "<SNR>99_" prefix
1988 namep = untrans_function_name(name);
1989 if (namep == NULL)
1990 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00001991
1992 // An exported function in an autoload script is stored as
1993 // "dir#path#name".
1994 if (len < sizeof(buffer))
1995 auto_name = buffer;
1996 else
1997 auto_name = alloc(len);
1998 if (auto_name != NULL)
1999 {
2000 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002001 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002002 hi = hash_find(&func_hashtab, auto_name);
2003 if (auto_name != buffer)
2004 vim_free(auto_name);
2005 if (!HASHITEM_EMPTY(hi))
2006 return HI2UF(hi);
2007 }
2008 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009
2010 return NULL;
2011}
2012
2013/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002014 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002015 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2016 * functions.
2017 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002018 * Return NULL for unknown function.
2019 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002020 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002021find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002022{
2023 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002026 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002028 // Find script-local function before global one.
2029 if (in_vim9script() && eval_isnamec1(*name)
2030 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002032 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2033 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002034 if (func != NULL)
2035 return func;
2036 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002037 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2038 {
2039 char_u *p = name + 5;
2040 long sid;
2041
2042 // printable "<SNR>123_Name" form
2043 sid = getdigits(&p);
2044 if (*p == '_')
2045 {
2046 func = find_func_with_sid(p + 1, (int)sid);
2047 if (func != NULL)
2048 return func;
2049 }
2050 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002051 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002052
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002053 if ((flags & FFED_NO_GLOBAL) == 0)
2054 {
2055 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002056 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002057 if (!HASHITEM_EMPTY(hi))
2058 return HI2UF(hi);
2059 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002060
Bram Moolenaarb8822442022-01-11 15:24:05 +00002061 // Find autoload function if this is an autoload script.
2062 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2063 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064}
2065
2066/*
2067 * Find a function by name, return pointer to it in ufuncs.
2068 * "cctx" is passed in a :def function to find imported functions.
2069 * Return NULL for unknown or dead function.
2070 */
2071 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002072find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002073{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002074 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075
2076 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2077 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002078 return NULL;
2079}
2080
2081/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002082 * Return TRUE if "ufunc" is a global function.
2083 */
2084 int
2085func_is_global(ufunc_T *ufunc)
2086{
2087 return ufunc->uf_name[0] != K_SPECIAL;
2088}
2089
2090/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002091 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2092 */
2093 int
2094func_requires_g_prefix(ufunc_T *ufunc)
2095{
2096 return ufunc->uf_name[0] != K_SPECIAL
2097 && (ufunc->uf_flags & FC_LAMBDA) == 0
2098 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL;
2099}
2100
2101/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002102 * Copy the function name of "fp" to buffer "buf".
2103 * "buf" must be able to hold the function name plus three bytes.
2104 * Takes care of script-local function names.
2105 */
2106 static void
2107cat_func_name(char_u *buf, ufunc_T *fp)
2108{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002109 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002110 {
2111 STRCPY(buf, "<SNR>");
2112 STRCAT(buf, fp->uf_name + 3);
2113 }
2114 else
2115 STRCPY(buf, fp->uf_name);
2116}
2117
2118/*
2119 * Add a number variable "name" to dict "dp" with value "nr".
2120 */
2121 static void
2122add_nr_var(
2123 dict_T *dp,
2124 dictitem_T *v,
2125 char *name,
2126 varnumber_T nr)
2127{
2128 STRCPY(v->di_key, name);
2129 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2130 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
2131 v->di_tv.v_type = VAR_NUMBER;
2132 v->di_tv.v_lock = VAR_FIXED;
2133 v->di_tv.vval.v_number = nr;
2134}
2135
2136/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002137 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002138 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002139 static void
2140free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002141{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002142 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002143
2144 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
2145 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002146 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002147
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002148 // When garbage collecting a funccall_T may be freed before the
2149 // function that references it, clear its uf_scoped field.
2150 // The function may have been redefined and point to another
2151 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002152 if (fp != NULL && fp->uf_scoped == fc)
2153 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002154 }
Bram Moolenaar58016442016-07-31 18:30:22 +02002155 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002156
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002157 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002158 vim_free(fc);
2159}
2160
2161/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002162 * Free "fc" and what it contains.
2163 * Can be called only when "fc" is kept beyond the period of it called,
2164 * i.e. after cleanup_function_call(fc).
2165 */
2166 static void
2167free_funccal_contents(funccall_T *fc)
2168{
2169 listitem_T *li;
2170
2171 // Free all l: variables.
2172 vars_clear(&fc->l_vars.dv_hashtab);
2173
2174 // Free all a: variables.
2175 vars_clear(&fc->l_avars.dv_hashtab);
2176
2177 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002178 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002179 clear_tv(&li->li_tv);
2180
2181 free_funccal(fc);
2182}
2183
2184/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002185 * Handle the last part of returning from a function: free the local hashtable.
2186 * Unless it is still in use by a closure.
2187 */
2188 static void
2189cleanup_function_call(funccall_T *fc)
2190{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002191 int may_free_fc = fc->fc_refcount <= 0;
2192 int free_fc = TRUE;
2193
Bram Moolenaar6914c642017-04-01 21:21:30 +02002194 current_funccal = fc->caller;
2195
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002196 // Free all l: variables if not referred.
2197 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
2198 vars_clear(&fc->l_vars.dv_hashtab);
2199 else
2200 free_fc = FALSE;
2201
2202 // If the a:000 list and the l: and a: dicts are not referenced and
2203 // there is no closure using it, we can free the funccall_T and what's
2204 // in it.
2205 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
2206 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002207 else
2208 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002209 int todo;
2210 hashitem_T *hi;
2211 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002212
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002213 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002214
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002215 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +02002216 todo = (int)fc->l_avars.dv_hashtab.ht_used;
2217 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
2218 {
2219 if (!HASHITEM_EMPTY(hi))
2220 {
2221 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002222 di = HI2DI(hi);
2223 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002224 }
2225 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002226 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002227
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002228 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2229 fc->l_varlist.lv_first = NULL;
2230 else
2231 {
2232 listitem_T *li;
2233
2234 free_fc = FALSE;
2235
2236 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002237 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002238 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002239 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002240
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002241 if (free_fc)
2242 free_funccal(fc);
2243 else
2244 {
2245 static int made_copy = 0;
2246
2247 // "fc" is still in use. This can happen when returning "a:000",
2248 // assigning "l:" to a global variable or defining a closure.
2249 // Link "fc" in the list for garbage collection later.
2250 fc->caller = previous_funccal;
2251 previous_funccal = fc;
2252
2253 if (want_garbage_collect)
2254 // If garbage collector is ready, clear count.
2255 made_copy = 0;
2256 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002257 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002258 // We have made a lot of copies, worth 4 Mbyte. This can happen
2259 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002260 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002261 // too much memory.
2262 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002263 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002264 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002265 }
2266}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002267
2268/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002269 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2270 */
2271 static int
2272numbered_function(char_u *name)
2273{
2274 return isdigit(*name)
2275 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2276}
2277
2278/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002279 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002280 * 1. ordinary names, function defined with :function or :def;
2281 * can start with "<SNR>123_" literally or with K_SPECIAL.
2282 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002283 * For the first we only count the name stored in func_hashtab as a reference,
2284 * using function() does not count as a reference, because the function is
2285 * looked up by name.
2286 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002287 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002288func_name_refcount(char_u *name)
2289{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002290 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002291}
2292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002293/*
2294 * Unreference "fc": decrement the reference count and free it when it
2295 * becomes zero. "fp" is detached from "fc".
2296 * When "force" is TRUE we are exiting.
2297 */
2298 static void
2299funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2300{
2301 funccall_T **pfc;
2302 int i;
2303
2304 if (fc == NULL)
2305 return;
2306
2307 if (--fc->fc_refcount <= 0 && (force || (
2308 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
2309 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
2310 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2311 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
2312 {
2313 if (fc == *pfc)
2314 {
2315 *pfc = fc->caller;
2316 free_funccal_contents(fc);
2317 return;
2318 }
2319 }
2320 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
2321 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
2322 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
2323}
2324
2325/*
2326 * Remove the function from the function hashtable. If the function was
2327 * deleted while it still has references this was already done.
2328 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2329 */
2330 static int
2331func_remove(ufunc_T *fp)
2332{
2333 hashitem_T *hi;
2334
2335 // Return if it was already virtually deleted.
2336 if (fp->uf_flags & FC_DEAD)
2337 return FALSE;
2338
2339 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
2340 if (!HASHITEM_EMPTY(hi))
2341 {
2342 // When there is a def-function index do not actually remove the
2343 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002344 // Do remove it when it's a copy.
2345 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaarc970e422021-03-17 15:03:04 +01002346 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01002348 return FALSE;
2349 }
2350 hash_remove(&func_hashtab, hi);
2351 fp->uf_flags |= FC_DELETED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002352 return TRUE;
2353 }
2354 return FALSE;
2355}
2356
2357 static void
2358func_clear_items(ufunc_T *fp)
2359{
2360 ga_clear_strings(&(fp->uf_args));
2361 ga_clear_strings(&(fp->uf_def_args));
2362 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002363 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002364 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002365 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002366 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002367
2368 // Increment the refcount of this function to avoid it being freed
2369 // recursively when the partial is freed.
2370 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002371 partial_unref(fp->uf_partial);
2372 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002373 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002374
2375#ifdef FEAT_LUA
2376 if (fp->uf_cb_free != NULL)
2377 {
2378 fp->uf_cb_free(fp->uf_cb_state);
2379 fp->uf_cb_free = NULL;
2380 }
2381
2382 fp->uf_cb_state = NULL;
2383 fp->uf_cb = NULL;
2384#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385#ifdef FEAT_PROFILE
2386 VIM_CLEAR(fp->uf_tml_count);
2387 VIM_CLEAR(fp->uf_tml_total);
2388 VIM_CLEAR(fp->uf_tml_self);
2389#endif
2390}
2391
2392/*
2393 * Free all things that a function contains. Does not free the function
2394 * itself, use func_free() for that.
2395 * When "force" is TRUE we are exiting.
2396 */
2397 static void
2398func_clear(ufunc_T *fp, int force)
2399{
2400 if (fp->uf_cleared)
2401 return;
2402 fp->uf_cleared = TRUE;
2403
2404 // clear this function
2405 func_clear_items(fp);
2406 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002407 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408}
2409
2410/*
2411 * Free a function and remove it from the list of functions. Does not free
2412 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002413 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002414 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002416 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002417func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418{
2419 // Only remove it when not done already, otherwise we would remove a newer
2420 // version of the function with the same name.
2421 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2422 func_remove(fp);
2423
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002424 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002425 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002426 if (fp->uf_dfunc_idx > 0)
2427 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002428 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002429 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002430 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002431 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002432 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002433}
2434
2435/*
2436 * Free all things that a function contains and free the function itself.
2437 * When "force" is TRUE we are exiting.
2438 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002439 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002440func_clear_free(ufunc_T *fp, int force)
2441{
2442 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002443 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2444 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002445 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002446 else
2447 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448}
2449
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002450/*
2451 * Copy already defined function "lambda" to a new function with name "global".
2452 * This is for when a compiled function defines a global function.
2453 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002454 int
2455copy_func(char_u *lambda, char_u *global, ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002456{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002457 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002458 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002459
2460 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002461 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002462 semsg(_(e_lambda_function_not_found_str), lambda);
2463 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002464 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002465
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002466 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002467 if (fp != NULL)
2468 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002469 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002470 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002471 return FAIL;
2472 }
2473
2474 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
2475 if (fp == NULL)
2476 return FAIL;
2477
2478 fp->uf_varargs = ufunc->uf_varargs;
2479 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2480 fp->uf_def_status = ufunc->uf_def_status;
2481 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2482 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2483 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2484 == FAIL
2485 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2486 goto failed;
2487
2488 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
2489 : vim_strsave(ufunc->uf_name_exp);
2490 if (ufunc->uf_arg_types != NULL)
2491 {
2492 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2493 if (fp->uf_arg_types == NULL)
2494 goto failed;
2495 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2496 sizeof(type_T *) * fp->uf_args.ga_len);
2497 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002498 if (ufunc->uf_va_name != NULL)
2499 {
2500 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2501 if (fp->uf_va_name == NULL)
2502 goto failed;
2503 }
2504 fp->uf_ret_type = ufunc->uf_ret_type;
2505
2506 fp->uf_refcount = 1;
2507 STRCPY(fp->uf_name, global);
2508 hash_add(&func_hashtab, UF2HIKEY(fp));
2509
2510 // the referenced dfunc_T is now used one more time
2511 link_def_function(fp);
2512
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002513 // Create a partial to store the context of the function where it was
2514 // instantiated. Only needs to be done once. Do this on the original
2515 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002516 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2517 {
2518 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2519
2520 if (pt == NULL)
2521 goto failed;
2522 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002523 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002524 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002525 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002526 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002527 ufunc->uf_partial = pt;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002528 --pt->pt_refcount; // not actually referenced here
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002529 }
2530
2531 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002532
2533failed:
2534 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002535 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002536}
2537
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002538static int funcdepth = 0;
2539
2540/*
2541 * Increment the function call depth count.
2542 * Return FAIL when going over 'maxfuncdepth'.
2543 * Otherwise return OK, must call funcdepth_decrement() later!
2544 */
2545 int
2546funcdepth_increment(void)
2547{
2548 if (funcdepth >= p_mfd)
2549 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002550 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002551 return FAIL;
2552 }
2553 ++funcdepth;
2554 return OK;
2555}
2556
2557 void
2558funcdepth_decrement(void)
2559{
2560 --funcdepth;
2561}
2562
2563/*
2564 * Get the current function call depth.
2565 */
2566 int
2567funcdepth_get(void)
2568{
2569 return funcdepth;
2570}
2571
2572/*
2573 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002574 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002575 * times as funcdepth_increment().
2576 */
2577 void
2578funcdepth_restore(int depth)
2579{
2580 funcdepth = depth;
2581}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002582
2583/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002584 * Call a user function.
2585 */
2586 static void
2587call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002588 ufunc_T *fp, // pointer to function
2589 int argcount, // nr of args
2590 typval_T *argvars, // arguments
2591 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002592 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002593 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002594{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002595 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002596 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002597 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002598 funccall_T *fc;
2599 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002600 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002601 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002602 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002603 int i;
2604 int ai;
2605 int islambda = FALSE;
2606 char_u numbuf[NUMBUFLEN];
2607 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002608 typval_T *tv_to_free[MAX_FUNC_ARGS];
2609 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002610#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002611 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002612#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01002613 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002614
Bram Moolenaarb2049902021-01-24 12:53:53 +01002615#ifdef FEAT_PROFILE
2616 CLEAR_FIELD(profile_info);
2617#endif
2618
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002619 // If depth of calling is getting too high, don't execute the function.
2620 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002621 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002622 rettv->v_type = VAR_NUMBER;
2623 rettv->vval.v_number = -1;
2624 return;
2625 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002626
Bram Moolenaare38eab22019-12-05 21:50:01 +01002627 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002628
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002629 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002630 if (fc == NULL)
2631 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002632 fc->caller = current_funccal;
2633 current_funccal = fc;
2634 fc->func = fp;
2635 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002636 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002637 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002638 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2639 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002640 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002641 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002642 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002643
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002644 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002646#ifdef FEAT_PROFILE
2647 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2648#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002649 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002650#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002651 if (do_profiling == PROF_YES)
2652 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002653#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002654 sticky_cmdmod_flags = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002655 call_def_function(fp, argcount, argvars, funcexe->fe_partial, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002656 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002657#ifdef FEAT_PROFILE
2658 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002659 || (caller != NULL && caller->uf_profiling)))
2660 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002661#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 current_funccal = fc->caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663 free_funccal(fc);
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002664 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 return;
2666 }
2667
Bram Moolenaar38453522021-11-28 22:00:12 +00002668 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002669
2670 /*
2671 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
2672 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
2673 * each argument variable and saves a lot of time.
2674 */
2675 /*
2676 * Init l: variables.
2677 */
2678 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
2679 if (selfdict != NULL)
2680 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002681 // Set l:self to "selfdict". Use "name" to avoid a warning from
2682 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002683 v = &fc->fixvar[fixvar_idx++].var;
2684 name = v->di_key;
2685 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002686 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002687 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
2688 v->di_tv.v_type = VAR_DICT;
2689 v->di_tv.v_lock = 0;
2690 v->di_tv.vval.v_dict = selfdict;
2691 ++selfdict->dv_refcount;
2692 }
2693
2694 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002695 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002696 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002697 * Set a:000 to a list with room for the "..." arguments.
2698 */
2699 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002700 if ((fp->uf_flags & FC_NOARGS) == 0)
2701 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002702 (varnumber_T)(argcount >= fp->uf_args.ga_len
2703 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01002704 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002705 if ((fp->uf_flags & FC_NOARGS) == 0)
2706 {
2707 // Use "name" to avoid a warning from some compiler that checks the
2708 // destination size.
2709 v = &fc->fixvar[fixvar_idx++].var;
2710 name = v->di_key;
2711 STRCPY(name, "000");
2712 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2713 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
2714 v->di_tv.v_type = VAR_LIST;
2715 v->di_tv.v_lock = VAR_FIXED;
2716 v->di_tv.vval.v_list = &fc->l_varlist;
2717 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02002718 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002719 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2720 fc->l_varlist.lv_lock = VAR_FIXED;
2721
2722 /*
2723 * Set a:firstline to "firstline" and a:lastline to "lastline".
2724 * Set a:name to named arguments.
2725 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002726 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002727 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002728 if ((fp->uf_flags & FC_NOARGS) == 0)
2729 {
2730 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002731 (varnumber_T)funcexe->fe_firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002732 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002733 (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002734 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002735 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002736 {
2737 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002738 typval_T def_rettv;
2739 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002740
2741 ai = i - fp->uf_args.ga_len;
2742 if (ai < 0)
2743 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002744 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002745 name = FUNCARG(fp, i);
2746 if (islambda)
2747 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002748
2749 // evaluate named argument default expression
2750 isdefault = ai + fp->uf_def_args.ga_len >= 0
2751 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2752 && argvars[i].vval.v_number == VVAL_NONE));
2753 if (isdefault)
2754 {
2755 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002756
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002757 def_rettv.v_type = VAR_NUMBER;
2758 def_rettv.vval.v_number = -1;
2759
2760 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2761 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002762 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002763 {
2764 default_arg_err = 1;
2765 break;
2766 }
2767 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002768 }
2769 else
2770 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002771 if ((fp->uf_flags & FC_NOARGS) != 0)
2772 // Bail out if no a: arguments used (in lambda).
2773 break;
2774
Bram Moolenaare38eab22019-12-05 21:50:01 +01002775 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002776 sprintf((char *)numbuf, "%d", ai + 1);
2777 name = numbuf;
2778 }
2779 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2780 {
2781 v = &fc->fixvar[fixvar_idx++].var;
2782 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002783 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002784 }
2785 else
2786 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002787 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002788 if (v == NULL)
2789 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002790 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002791 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002792
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002793 // Note: the values are copied directly to avoid alloc/free.
2794 // "argvars" must have VAR_FIXED for v_lock.
2795 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002796 v->di_tv.v_lock = VAR_FIXED;
2797
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002798 if (isdefault)
2799 // Need to free this later, no matter where it's stored.
2800 tv_to_free[tv_to_free_len++] = &v->di_tv;
2801
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002802 if (addlocal)
2803 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002804 // Named arguments should be accessed without the "a:" prefix in
2805 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002806 copy_tv(&v->di_tv, &v->di_tv);
2807 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002808 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002809 else
2810 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002811
2812 if (ai >= 0 && ai < MAX_FUNC_ARGS)
2813 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002814 listitem_T *li = &fc->l_listitems[ai];
2815
2816 li->li_tv = argvars[i];
2817 li->li_tv.v_lock = VAR_FIXED;
2818 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002819 }
2820 }
2821
Bram Moolenaare38eab22019-12-05 21:50:01 +01002822 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002823 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02002824
2825 if (fp->uf_flags & FC_SANDBOX)
2826 {
2827 using_sandbox = TRUE;
2828 ++sandbox;
2829 }
2830
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002831 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002832 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002833 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002834 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002835 ++no_wait_return;
2836 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002837
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002838 smsg(_("calling %s"), SOURCING_NAME);
2839 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002840 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002841 char_u buf[MSG_BUF_LEN];
2842 char_u numbuf2[NUMBUFLEN];
2843 char_u *tofree;
2844 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002845
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002846 msg_puts("(");
2847 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002848 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002849 if (i > 0)
2850 msg_puts(", ");
2851 if (argvars[i].v_type == VAR_NUMBER)
2852 msg_outnum((long)argvars[i].vval.v_number);
2853 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002854 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002855 // Do not want errors such as E724 here.
2856 ++emsg_off;
2857 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
2858 --emsg_off;
2859 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002860 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002861 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002862 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002863 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2864 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002865 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002866 msg_puts((char *)s);
2867 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002868 }
2869 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002870 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002871 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002872 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002873 msg_puts("\n"); // don't overwrite this either
2874
2875 verbose_leave_scroll();
2876 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002877 }
2878#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002879 if (do_profiling == PROF_YES)
2880 profile_may_start_func(&profile_info, fp,
2881 fc->caller == NULL ? NULL : fc->caller->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002882#endif
2883
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002884 // "legacy" does not apply to commands in the function
2885 sticky_cmdmod_flags = 0;
2886
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002887 save_current_sctx = current_sctx;
2888 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002889 save_did_emsg = did_emsg;
2890 did_emsg = FALSE;
2891
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002892 if (default_arg_err && (fp->uf_flags & FC_ABORT))
2893 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002894 else if (islambda)
2895 {
2896 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
2897
2898 // A Lambda always has the command "return {expr}". It is much faster
2899 // to evaluate {expr} directly.
2900 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002901 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002902 --ex_nesting_level;
2903 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002904 else
2905 // call do_cmdline() to execute the lines
2906 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002907 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
2908
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002909 // Invoke functions added with ":defer".
2910 handle_defer();
2911
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002912 --RedrawingDisabled;
2913
Bram Moolenaare38eab22019-12-05 21:50:01 +01002914 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002915 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
2916 {
2917 clear_tv(rettv);
2918 rettv->v_type = VAR_NUMBER;
2919 rettv->vval.v_number = -1;
2920 }
2921
2922#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002923 if (do_profiling == PROF_YES)
2924 {
2925 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2926
2927 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
2928 profile_may_end_func(&profile_info, fp, caller);
2929 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002930#endif
2931
Bram Moolenaare38eab22019-12-05 21:50:01 +01002932 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002933 if (p_verbose >= 12)
2934 {
2935 ++no_wait_return;
2936 verbose_enter_scroll();
2937
2938 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002939 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002940 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002941 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002942 (long)fc->rettv->vval.v_number);
2943 else
2944 {
2945 char_u buf[MSG_BUF_LEN];
2946 char_u numbuf2[NUMBUFLEN];
2947 char_u *tofree;
2948 char_u *s;
2949
Bram Moolenaare38eab22019-12-05 21:50:01 +01002950 // The value may be very long. Skip the middle part, so that we
2951 // have some idea how it starts and ends. smsg() would always
2952 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002953 ++emsg_off;
2954 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
2955 --emsg_off;
2956 if (s != NULL)
2957 {
2958 if (vim_strsize(s) > MSG_BUF_CLEN)
2959 {
2960 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2961 s = buf;
2962 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002963 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002964 vim_free(tofree);
2965 }
2966 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002967 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002968
2969 verbose_leave_scroll();
2970 --no_wait_return;
2971 }
2972
Bram Moolenaare31ee862020-01-07 20:59:34 +01002973 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002974 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002975 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002976#ifdef FEAT_PROFILE
2977 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002978 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002979#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02002980 if (using_sandbox)
2981 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002982 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002983
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002984 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002985 {
2986 ++no_wait_return;
2987 verbose_enter_scroll();
2988
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002989 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002990 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002991
2992 verbose_leave_scroll();
2993 --no_wait_return;
2994 }
2995
2996 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002997 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002998 for (i = 0; i < tv_to_free_len; ++i)
2999 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003000 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003001}
3002
3003/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003004 * Check the argument count for user function "fp".
3005 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3006 */
3007 int
3008check_user_func_argcount(ufunc_T *fp, int argcount)
3009{
3010 int regular_args = fp->uf_args.ga_len;
3011
3012 if (argcount < regular_args - fp->uf_def_args.ga_len)
3013 return FCERR_TOOFEW;
3014 else if (!has_varargs(fp) && argcount > regular_args)
3015 return FCERR_TOOMANY;
3016 return FCERR_UNKNOWN;
3017}
3018
3019/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003021 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 int
3023call_user_func_check(
3024 ufunc_T *fp,
3025 int argcount,
3026 typval_T *argvars,
3027 typval_T *rettv,
3028 funcexe_T *funcexe,
3029 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003030{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003032
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003033#ifdef FEAT_LUA
3034 if (fp->uf_flags & FC_CFUNC)
3035 {
3036 cfunc_T cb = fp->uf_cb;
3037
3038 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3039 }
3040#endif
3041
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003042 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3043 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003044 error = check_user_func_argcount(fp, argcount);
3045 if (error != FCERR_UNKNOWN)
3046 return error;
3047 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 error = FCERR_DICT;
3049 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003050 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 int did_save_redo = FALSE;
3052 save_redo_T save_redo;
3053
3054 /*
3055 * Call the user function.
3056 * Save and restore search patterns, script variables and
3057 * redo buffer.
3058 */
3059 save_search_patterns();
3060 if (!ins_compl_active())
3061 {
3062 saveRedobuff(&save_redo);
3063 did_save_redo = TRUE;
3064 }
3065 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003066 call_user_func(fp, argcount, argvars, rettv, funcexe,
3067 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3069 // Function was unreferenced while being used, free it now.
3070 func_clear_free(fp, FALSE);
3071 if (did_save_redo)
3072 restoreRedobuff(&save_redo);
3073 restore_search_patterns();
3074 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003075 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003077}
3078
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003079static funccal_entry_T *funccal_stack = NULL;
3080
3081/*
3082 * Save the current function call pointer, and set it to NULL.
3083 * Used when executing autocommands and for ":source".
3084 */
3085 void
3086save_funccal(funccal_entry_T *entry)
3087{
3088 entry->top_funccal = current_funccal;
3089 entry->next = funccal_stack;
3090 funccal_stack = entry;
3091 current_funccal = NULL;
3092}
3093
3094 void
3095restore_funccal(void)
3096{
3097 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003098 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003099 else
3100 {
3101 current_funccal = funccal_stack->top_funccal;
3102 funccal_stack = funccal_stack->next;
3103 }
3104}
3105
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003106 funccall_T *
3107get_current_funccal(void)
3108{
3109 return current_funccal;
3110}
3111
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003112/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003113 * Return TRUE when currently at the script level:
3114 * - not in a function
3115 * - not executing an autocommand
3116 * Note that when an autocommand sources a script the result is FALSE;
3117 */
3118 int
3119at_script_level(void)
3120{
3121 return current_funccal == NULL && autocmd_match == NULL;
3122}
3123
3124/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003125 * Mark all functions of script "sid" as deleted.
3126 */
3127 void
3128delete_script_functions(int sid)
3129{
3130 hashitem_T *hi;
3131 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003132 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003133 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003134 size_t len;
3135
3136 buf[0] = K_SPECIAL;
3137 buf[1] = KS_EXTRA;
3138 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003139 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003140 len = STRLEN(buf);
3141
Bram Moolenaarce658352020-07-31 23:47:12 +02003142 while (todo > 0)
3143 {
3144 todo = func_hashtab.ht_used;
3145 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3146 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003147 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003148 fp = HI2UF(hi);
3149 if (STRNCMP(fp->uf_name, buf, len) == 0)
3150 {
3151 int changed = func_hashtab.ht_changed;
3152
3153 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003154
3155 if (fp->uf_calls > 0)
3156 {
3157 // Function is executing, don't free it but do remove
3158 // it from the hashtable.
3159 if (func_remove(fp))
3160 fp->uf_refcount--;
3161 }
3162 else
3163 {
3164 func_clear(fp, TRUE);
3165 // When clearing a function another function can be
3166 // cleared as a side effect. When that happens start
3167 // over.
3168 if (changed != func_hashtab.ht_changed)
3169 break;
3170 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003171 }
3172 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003173 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003174 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003175}
3176
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003177#if defined(EXITFREE) || defined(PROTO)
3178 void
3179free_all_functions(void)
3180{
3181 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003182 ufunc_T *fp;
3183 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003184 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003185 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003186
Bram Moolenaare38eab22019-12-05 21:50:01 +01003187 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003188 while (current_funccal != NULL)
3189 {
3190 clear_tv(current_funccal->rettv);
3191 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003192 if (current_funccal == NULL && funccal_stack != NULL)
3193 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003194 }
3195
Bram Moolenaare38eab22019-12-05 21:50:01 +01003196 // First clear what the functions contain. Since this may lower the
3197 // reference count of a function, it may also free a function and change
3198 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003199 while (todo > 0)
3200 {
3201 todo = func_hashtab.ht_used;
3202 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3203 if (!HASHITEM_EMPTY(hi))
3204 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 // clear the def function index now
3206 fp = HI2UF(hi);
3207 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003208 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209
Bram Moolenaare38eab22019-12-05 21:50:01 +01003210 // Only free functions that are not refcounted, those are
3211 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003212 if (func_name_refcount(fp->uf_name))
3213 ++skipped;
3214 else
3215 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003216 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003217 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003218 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003219 {
3220 skipped = 0;
3221 break;
3222 }
3223 }
3224 --todo;
3225 }
3226 }
3227
Bram Moolenaare38eab22019-12-05 21:50:01 +01003228 // Now actually free the functions. Need to start all over every time,
3229 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003230 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003231 while (func_hashtab.ht_used > skipped)
3232 {
3233 todo = func_hashtab.ht_used;
3234 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003235 if (!HASHITEM_EMPTY(hi))
3236 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003237 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003238 // Only free functions that are not refcounted, those are
3239 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003240 fp = HI2UF(hi);
3241 if (func_name_refcount(fp->uf_name))
3242 ++skipped;
3243 else
3244 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003245 if (func_free(fp, FALSE) == OK)
3246 {
3247 skipped = 0;
3248 break;
3249 }
3250 // did not actually free it
3251 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003252 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003253 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003254 }
3255 if (skipped == 0)
3256 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257
3258 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003259}
3260#endif
3261
3262/*
3263 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003264 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3265 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003266 * "len" is the length of "name", or -1 for NUL terminated.
3267 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003269builtin_function(char_u *name, int len)
3270{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003271 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003272
Bram Moolenaara26b9702020-04-18 19:53:28 +02003273 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003274 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003275 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3276 {
3277 if (name[i] == AUTOLOAD_CHAR)
3278 return FALSE;
3279 if (!eval_isnamec(name[i]))
3280 {
3281 // "name.something" is not a builtin function
3282 if (name[i] == '.')
3283 return FALSE;
3284 break;
3285 }
3286 }
3287 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003288}
3289
3290 int
3291func_call(
3292 char_u *name,
3293 typval_T *args,
3294 partial_T *partial,
3295 dict_T *selfdict,
3296 typval_T *rettv)
3297{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003298 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003299 listitem_T *item;
3300 typval_T argv[MAX_FUNC_ARGS + 1];
3301 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003302 int r = 0;
3303
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003304 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003305 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003306 {
3307 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3308 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003309 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003310 break;
3311 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003312 // Make a copy of each argument. This is needed to be able to set
3313 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003314 copy_tv(&item->li_tv, &argv[argc++]);
3315 }
3316
3317 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003318 {
3319 funcexe_T funcexe;
3320
Bram Moolenaara80faa82020-04-12 19:37:17 +02003321 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003322 funcexe.fe_firstline = curwin->w_cursor.lnum;
3323 funcexe.fe_lastline = curwin->w_cursor.lnum;
3324 funcexe.fe_evaluate = TRUE;
3325 funcexe.fe_partial = partial;
3326 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003327 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3328 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003329
Bram Moolenaare38eab22019-12-05 21:50:01 +01003330 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003331 while (argc > 0)
3332 clear_tv(&argv[--argc]);
3333
3334 return r;
3335}
3336
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003337static int callback_depth = 0;
3338
3339 int
3340get_callback_depth(void)
3341{
3342 return callback_depth;
3343}
3344
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003345/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003346 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003347 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003348 */
3349 int
3350call_callback(
3351 callback_T *callback,
3352 int len, // length of "name" or -1 to use strlen()
3353 typval_T *rettv, // return value goes here
3354 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003355 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003356 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003357{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003358 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003359 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003360
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003361 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3362 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02003363 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003364 funcexe.fe_evaluate = TRUE;
3365 funcexe.fe_partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003366 ++callback_depth;
3367 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3368 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003369
3370 // When a :def function was called that uses :try an error would be turned
3371 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003372 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003373 {
3374 need_rethrow = FALSE;
3375 handle_did_throw();
3376 }
3377
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003378 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003379}
3380
3381/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003382 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003383 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003384 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3385 */
3386 varnumber_T
3387call_callback_retnr(
3388 callback_T *callback,
3389 int argcount, // number of "argvars"
3390 typval_T *argvars) // vars for arguments, must have "argcount"
3391 // PLUS ONE elements!
3392{
3393 typval_T rettv;
3394 varnumber_T retval;
3395
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003396 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003397 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003398
3399 retval = tv_get_number_chk(&rettv, NULL);
3400 clear_tv(&rettv);
3401 return retval;
3402}
3403
3404/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 * Give an error message for the result of a function.
3406 * Nothing if "error" is FCERR_NONE.
3407 */
3408 void
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003409user_func_error(int error, char_u *name, funcexe_T *funcexe)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410{
3411 switch (error)
3412 {
3413 case FCERR_UNKNOWN:
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003414 if (funcexe->fe_found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003415 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003416 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003417 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 break;
3419 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003420 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421 break;
3422 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003423 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424 break;
3425 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003426 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 break;
3428 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003429 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 break;
3431 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003432 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433 break;
3434 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003435 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3436 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 break;
3438 }
3439}
3440
3441/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003442 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003443 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003444 * Return FAIL when the function can't be called, OK otherwise.
3445 * Also returns OK when an error was encountered while executing the function.
3446 */
3447 int
3448call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003449 char_u *funcname, // name of the function
3450 int len, // length of "name" or -1 to use strlen()
3451 typval_T *rettv, // return value goes here
3452 int argcount_in, // number of "argvars"
3453 typval_T *argvars_in, // vars for arguments, must have "argcount"
3454 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003455 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003456{
3457 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01003458 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003459 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003460 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003461 char_u fname_buf[FLEN_FIXED + 1];
3462 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003463 char_u *fname = NULL;
3464 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003465 int argcount = argcount_in;
3466 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003467 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003468 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003469 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003470 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003471 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003472 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003473 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003474 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003475
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003476 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3477 // even when call_func() returns FAIL.
3478 rettv->v_type = VAR_UNKNOWN;
3479
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003480 if (partial != NULL)
3481 fp = partial->pt_func;
3482 if (fp == NULL)
3483 {
3484 // Make a copy of the name, if it comes from a funcref variable it
3485 // could be changed or deleted in the called function.
3486 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3487 if (name == NULL)
3488 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003489
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003490 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3491 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003492
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003493 if (funcexe->fe_doesrange != NULL)
3494 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003495
3496 if (partial != NULL)
3497 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003498 // When the function has a partial with a dict and there is a dict
3499 // argument, use the dict argument. That is backwards compatible.
3500 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003501 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003502 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003503 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003504 {
3505 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003506 {
3507 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3508 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003509 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003510 goto theend;
3511 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003512 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003513 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003514 for (i = 0; i < argcount_in; ++i)
3515 argv[i + argv_clear] = argvars_in[i];
3516 argvars = argv;
3517 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003518
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003519 if (funcexe->fe_check_type != NULL
3520 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003521 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003522 // Now funcexe->fe_check_type is missing the added arguments,
3523 // make a copy of the type with the correction.
3524 check_type = *funcexe->fe_check_type;
3525 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003526 check_type.tt_args = check_type_args;
3527 CLEAR_FIELD(check_type_args);
3528 for (i = 0; i < check_type.tt_argcount; ++i)
3529 check_type_args[i + partial->pt_argc] =
3530 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003531 check_type.tt_argcount += partial->pt_argc;
3532 check_type.tt_min_argcount += partial->pt_argc;
3533 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003534 }
3535 }
3536
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003537 if (error == FCERR_NONE && funcexe->fe_check_type != NULL
3538 && funcexe->fe_evaluate)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003539 {
3540 // Check that the argument types are OK for the types of the funcref.
Bram Moolenaarc84287d2022-01-16 18:06:21 +00003541 if (check_argument_types(funcexe->fe_check_type,
3542 argvars, argcount, funcexe->fe_basetv,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003543 (name != NULL) ? name : funcname) == FAIL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003544 error = FCERR_OTHER;
3545 }
3546
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003547 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003548 {
3549 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003550 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003551
Bram Moolenaar333894b2020-08-01 18:53:07 +02003552 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003553 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003554 {
3555 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003556 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003557 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003558
Bram Moolenaare38eab22019-12-05 21:50:01 +01003559 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003560 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003561 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003562
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003563 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003564 {
3565 /*
3566 * User defined function.
3567 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003568 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003569 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003570 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003571 if (fp != NULL && !is_global && in_vim9script()
3572 && func_requires_g_prefix(fp))
3573 // In Vim9 script g: is required to find a global
3574 // non-autoload function.
3575 fp = NULL;
3576 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003577
Bram Moolenaare38eab22019-12-05 21:50:01 +01003578 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003579 if (fp == NULL
3580 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003581 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003582 && !aborting())
3583 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003584 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003585 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003586 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003587 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003588 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3589 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003590 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003591 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003592 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003593 if (fp == NULL)
3594 {
3595 char_u *p = untrans_function_name(rfname);
3596
3597 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003598 // Don't do this if the name starts with "s:".
3599 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003600 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003601 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003602
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003603 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003604 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003605 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003606 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003607 if (funcexe->fe_argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003608 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003609 argcount = funcexe->fe_argv_func(argcount, argvars,
3610 argv_clear, fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003611
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003612 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003613 {
3614 // Method call: base->Method()
3615 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003616 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003617 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003618 argvars = argv;
3619 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003620 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622 error = call_user_func_check(fp, argcount, argvars, rettv,
3623 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003624 }
3625 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003626 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003627 {
3628 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003629 * expr->method(): Find the method name in the table, call its
3630 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003631 */
3632 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003633 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003634 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003635 else
3636 {
3637 /*
3638 * Find the function name in the table, call its implementation.
3639 */
3640 error = call_internal_func(fname, argcount, argvars, rettv);
3641 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003642
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003643 /*
3644 * The function call (or "FuncUndefined" autocommand sequence) might
3645 * have been aborted by an error, an interrupt, or an explicitly thrown
3646 * exception that has not been caught so far. This situation can be
3647 * tested for by calling aborting(). For an error in an internal
3648 * function or for the "E132" error in call_user_func(), however, the
3649 * throw point at which the "force_abort" flag (temporarily reset by
3650 * emsg()) is normally updated has not been reached yet. We need to
3651 * update that flag first to make aborting() reliable.
3652 */
3653 update_force_abort();
3654 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003655 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003656 ret = OK;
3657
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003658theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003659 /*
3660 * Report an error unless the argument evaluation or function call has been
3661 * cancelled due to an aborting error, an interrupt, or an exception.
3662 */
3663 if (!aborting())
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003664 user_func_error(error, (name != NULL) ? name : funcname, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003665
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003666 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003667 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003668 clear_tv(&argv[--argv_clear + argv_base]);
3669
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003670 vim_free(tofree);
3671 vim_free(name);
3672
3673 return ret;
3674}
3675
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003676 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677printable_func_name(ufunc_T *fp)
3678{
3679 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
3680}
3681
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003682/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003683 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003684 */
3685 static void
3686list_func_head(ufunc_T *fp, int indent)
3687{
3688 int j;
3689
3690 msg_start();
3691 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003692 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003693 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003694 msg_puts("def ");
3695 else
3696 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003698 msg_putchar('(');
3699 for (j = 0; j < fp->uf_args.ga_len; ++j)
3700 {
3701 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003702 msg_puts(", ");
3703 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 if (fp->uf_arg_types != NULL)
3705 {
3706 char *tofree;
3707
3708 msg_puts(": ");
3709 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
3710 vim_free(tofree);
3711 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003712 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
3713 {
3714 msg_puts(" = ");
3715 msg_puts(((char **)(fp->uf_def_args.ga_data))
3716 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
3717 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003718 }
3719 if (fp->uf_varargs)
3720 {
3721 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003722 msg_puts(", ");
3723 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003724 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 if (fp->uf_va_name != NULL)
3726 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01003727 if (!fp->uf_varargs)
3728 {
3729 if (j)
3730 msg_puts(", ");
3731 msg_puts("...");
3732 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02003734 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 {
3736 char *tofree;
3737
3738 msg_puts(": ");
3739 msg_puts(type_name(fp->uf_va_type, &tofree));
3740 vim_free(tofree);
3741 }
3742 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003743 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003744
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003745 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003746 {
3747 if (fp->uf_ret_type != &t_void)
3748 {
3749 char *tofree;
3750
3751 msg_puts(": ");
3752 msg_puts(type_name(fp->uf_ret_type, &tofree));
3753 vim_free(tofree);
3754 }
3755 }
3756 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003757 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003758 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003759 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003760 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003761 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003762 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003763 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003764 msg_clr_eos();
3765 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003766 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003767}
3768
3769/*
3770 * Get a function name, translating "<SID>" and "<SNR>".
3771 * Also handles a Funcref in a List or Dictionary.
3772 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003773 * Set "*is_global" to TRUE when the function must be global, unless
3774 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003775 * flags:
3776 * TFN_INT: internal function name OK
3777 * TFN_QUIET: be quiet
3778 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003779 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003780 * Advances "pp" to just after the function name (if no error).
3781 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003782 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003783trans_function_name(
3784 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003785 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003786 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003787 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003788 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003789 partial_T **partial, // return: partial of a FuncRef
3790 type_T **type) // return: type of funcref if not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003791{
3792 char_u *name = NULL;
3793 char_u *start;
3794 char_u *end;
3795 int lead;
3796 char_u sid_buf[20];
3797 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003799 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003800 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003801 int vim9script = in_vim9script();
3802 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003803
3804 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003805 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003806 start = *pp;
3807
Bram Moolenaare38eab22019-12-05 21:50:01 +01003808 // Check for hard coded <SNR>: already translated function ID (from a user
3809 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003810 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
3811 && (*pp)[2] == (int)KE_SNR)
3812 {
3813 *pp += 3;
3814 len = get_id_len(pp) + 3;
3815 return vim_strnsave(start, len);
3816 }
3817
Bram Moolenaare38eab22019-12-05 21:50:01 +01003818 // A name starting with "<SID>" or "<SNR>" is local to a script. But
3819 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003820 lead = eval_fname_script(start);
3821 if (lead > 2)
3822 start += lead;
3823
Bram Moolenaare38eab22019-12-05 21:50:01 +01003824 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01003825 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003826 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003827 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00003828 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003829 {
3830 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00003831 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003832 goto theend;
3833 }
3834 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
3835 {
3836 /*
3837 * Report an invalid expression in braces, unless the expression
3838 * evaluation has been cancelled due to an aborting error, an
3839 * interrupt, or an exception.
3840 */
3841 if (!aborting())
3842 {
3843 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003844 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003845 }
3846 else
3847 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
3848 goto theend;
3849 }
3850
3851 if (lv.ll_tv != NULL)
3852 {
3853 if (fdp != NULL)
3854 {
3855 fdp->fd_dict = lv.ll_dict;
3856 fdp->fd_newkey = lv.ll_newkey;
3857 lv.ll_newkey = NULL;
3858 fdp->fd_di = lv.ll_di;
3859 }
3860 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
3861 {
3862 name = vim_strsave(lv.ll_tv->vval.v_string);
3863 *pp = end;
3864 }
3865 else if (lv.ll_tv->v_type == VAR_PARTIAL
3866 && lv.ll_tv->vval.v_partial != NULL)
3867 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003868 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003869 *pp = end;
3870 if (partial != NULL)
3871 *partial = lv.ll_tv->vval.v_partial;
3872 }
3873 else
3874 {
3875 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
3876 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00003877 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003878 else
3879 *pp = end;
3880 name = NULL;
3881 }
3882 goto theend;
3883 }
3884
3885 if (lv.ll_name == NULL)
3886 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003887 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003888 *pp = end;
3889 goto theend;
3890 }
3891
Bram Moolenaare38eab22019-12-05 21:50:01 +01003892 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003893 if (lv.ll_exp_name != NULL)
3894 {
3895 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003896 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00003897 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003898 if (name == lv.ll_exp_name)
3899 name = NULL;
3900 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00003901 else if (lv.ll_sid > 0)
3902 {
3903 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
3904 int cc = *lv.ll_name_end;
3905
3906 // function in another script. Prefix <SNR>99_ or the autoload prefix.
3907 *lv.ll_name_end = NUL;
3908 if (si->sn_autoload_prefix != NULL)
3909 {
3910 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
3911 }
3912 else
3913 {
3914 sid_buf[0] = K_SPECIAL;
3915 sid_buf[1] = KS_EXTRA;
3916 sid_buf[2] = (int)KE_SNR;
3917 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00003918 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00003919 name = concat_str(sid_buf, lv.ll_name);
3920 }
3921 *lv.ll_name_end = cc;
3922 *pp = end;
3923 goto theend;
3924 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003925 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003926 {
3927 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003928 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00003929 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003930 if (name == *pp)
3931 name = NULL;
3932 }
3933 if (name != NULL)
3934 {
3935 name = vim_strsave(name);
3936 *pp = end;
3937 if (STRNCMP(name, "<SNR>", 5) == 0)
3938 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003939 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003940 name[0] = K_SPECIAL;
3941 name[1] = KS_EXTRA;
3942 name[2] = (int)KE_SNR;
3943 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
3944 }
3945 goto theend;
3946 }
3947
3948 if (lv.ll_exp_name != NULL)
3949 {
3950 len = (int)STRLEN(lv.ll_exp_name);
3951 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
3952 && STRNCMP(lv.ll_name, "s:", 2) == 0)
3953 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003954 // When there was "s:" already or the name expanded to get a
3955 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003956 lv.ll_name += 2;
3957 len -= 2;
3958 lead = 2;
3959 }
3960 }
3961 else
3962 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003963 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003964 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003965 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003966 if (lv.ll_name[0] == 'g')
3967 {
3968 if (is_global != NULL)
3969 {
3970 *is_global = TRUE;
3971 }
3972 else
3973 {
3974 // dropping "g:" without setting "is_global" won't work in
3975 // Vim9script, put it back later
3976 prefix_g = TRUE;
3977 extra = 2;
3978 }
3979 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003980 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003981 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003982 len = (int)(end - lv.ll_name);
3983 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003984 if (len <= 0)
3985 {
3986 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00003987 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003988 goto theend;
3989 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003990
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003991 // In Vim9 script a user function is script-local by default, unless it
3992 // starts with a lower case character: dict.func().
Bram Moolenaar4525a572022-02-13 11:57:33 +00003993 vim9_local = ASCII_ISUPPER(*start) && vim9script;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003995 /*
3996 * Copy the function name to allocated memory.
3997 * Accept <SID>name() inside a script, translate into <SNR>123_name().
3998 * Accept <SNR>123_name() outside a script.
3999 */
4000 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004001 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004002 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004003 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004004 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004005 {
Kota Kato948a3892022-08-16 16:09:59 +01004006 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4007 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004008 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004009 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004010 goto theend;
4011 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004013 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004014 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004016 || eval_fname_sid(*pp))
4017 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004019 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004020 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004021 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004022 goto theend;
4023 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004024 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004025 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 extra = 3 + (int)STRLEN(sid_buf);
4027 else
4028 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004029 }
4030 }
Bram Moolenaar22f17a22021-06-21 20:48:58 +02004031 else if (!(flags & TFN_INT) && (builtin_function(lv.ll_name, len)
Bram Moolenaar4525a572022-02-13 11:57:33 +00004032 || (vim9script && *lv.ll_name == '_')))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004033 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004034 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004035 : e_function_name_must_start_with_capital_or_s_str),
4036 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004037 goto theend;
4038 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004039 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004040 {
4041 char_u *cp = vim_strchr(lv.ll_name, ':');
4042
4043 if (cp != NULL && cp < end)
4044 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004045 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004046 goto theend;
4047 }
4048 }
4049
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004051 if (name != NULL)
4052 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004053 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004054 {
4055 name[0] = K_SPECIAL;
4056 name[1] = KS_EXTRA;
4057 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004058 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004059 STRCPY(name + 3, sid_buf);
4060 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004061 else if (prefix_g)
4062 {
4063 name[0] = 'g';
4064 name[1] = ':';
4065 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004066 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4067 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004068 }
4069 *pp = end;
4070
4071theend:
4072 clear_lval(&lv);
4073 return name;
4074}
4075
4076/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004077 * Assuming "name" is the result of trans_function_name() and it was prefixed
4078 * to use the script-local name, return the unmodified name (points into
4079 * "name"). Otherwise return NULL.
4080 * This can be used to first search for a script-local function and fall back
4081 * to the global function if not found.
4082 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004083 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004084untrans_function_name(char_u *name)
4085{
4086 char_u *p;
4087
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004088 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004089 {
4090 p = vim_strchr(name, '_');
4091 if (p != NULL)
4092 return p + 1;
4093 }
4094 return NULL;
4095}
4096
4097/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004098 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4099 * current script ID and returns the expanded function name. The caller should
4100 * free the returned name. If not called from a script context or the function
4101 * name doesn't start with these prefixes, then returns NULL.
4102 * This doesn't check whether the script-local function exists or not.
4103 */
4104 char_u *
4105get_scriptlocal_funcname(char_u *funcname)
4106{
4107 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004108 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004109 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004110 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004111
4112 if (funcname == NULL)
4113 return NULL;
4114
4115 if (STRNCMP(funcname, "s:", 2) != 0
4116 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004117 {
4118 ufunc_T *ufunc;
4119
4120 // The function name does not have a script-local prefix. Try finding
4121 // it when in a Vim9 script and there is no "g:" prefix.
4122 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4123 return NULL;
4124 ufunc = find_func(funcname, FALSE);
4125 if (ufunc == NULL || func_is_global(ufunc)
4126 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4127 return NULL;
4128 ++p;
4129 off = 0;
4130 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004131 else
4132 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004133
4134 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4135 {
4136 emsg(_(e_using_sid_not_in_script_context));
4137 return NULL;
4138 }
4139 // Expand s: prefix into <SNR>nr_<name>
4140 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4141 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004142 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004143 if (newname == NULL)
4144 return NULL;
4145 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004146 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004147
4148 return newname;
4149}
4150
4151/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004152 * Return script-local "fname" with the 3-byte sequence replaced by
4153 * printable <SNR> in allocated memory.
4154 */
4155 char_u *
4156alloc_printable_func_name(char_u *fname)
4157{
4158 char_u *n = alloc(STRLEN(fname + 3) + 6);
4159
4160 if (n != NULL)
4161 {
4162 STRCPY(n, "<SNR>");
4163 STRCPY(n + 5, fname + 3);
4164 }
4165 return n;
4166}
4167
4168/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004169 * Call trans_function_name(), except that a lambda is returned as-is.
4170 * Returns the name in allocated memory.
4171 */
4172 char_u *
4173save_function_name(
4174 char_u **name,
4175 int *is_global,
4176 int skip,
4177 int flags,
4178 funcdict_T *fudi)
4179{
4180 char_u *p = *name;
4181 char_u *saved;
4182
4183 if (STRNCMP(p, "<lambda>", 8) == 0)
4184 {
4185 p += 8;
4186 (void)getdigits(&p);
4187 saved = vim_strnsave(*name, p - *name);
4188 if (fudi != NULL)
4189 CLEAR_POINTER(fudi);
4190 }
4191 else
4192 saved = trans_function_name(&p, is_global, skip,
4193 flags, fudi, NULL, NULL);
4194 *name = p;
4195 return saved;
4196}
4197
4198/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004199 * List functions. When "regmatch" is NULL all of then.
4200 * Otherwise functions matching "regmatch".
4201 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004202 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004203list_functions(regmatch_T *regmatch)
4204{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004205 int changed = func_hashtab.ht_changed;
4206 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004207 hashitem_T *hi;
4208
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004209 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004210 {
4211 if (!HASHITEM_EMPTY(hi))
4212 {
4213 ufunc_T *fp = HI2UF(hi);
4214
4215 --todo;
4216 if ((fp->uf_flags & FC_DEAD) == 0
4217 && (regmatch == NULL
4218 ? !message_filtered(fp->uf_name)
4219 && !func_name_refcount(fp->uf_name)
4220 : !isdigit(*fp->uf_name)
4221 && vim_regexec(regmatch, fp->uf_name, 0)))
4222 {
4223 list_func_head(fp, FALSE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004224 if (changed != func_hashtab.ht_changed)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004225 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00004226 emsg(_(e_function_list_was_modified));
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004227 return;
4228 }
4229 }
4230 }
4231 }
4232}
4233
4234/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004235 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004236 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4237 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004238 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004239 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004240 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004241 ufunc_T *
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004242define_function(exarg_T *eap, char_u *name_arg, garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004243{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004244 int j;
4245 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004246 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004247 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004248 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004249 char_u *p;
4250 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004251 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004252 char_u *line_arg = NULL;
4253 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004255 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004256 garray_T newlines;
4257 int varargs = FALSE;
4258 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004260 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004261 int fp_allocated = FALSE;
4262 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004263 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004264 dictitem_T *v;
4265 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004266 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004267 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004268 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004269 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004270 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004271 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004272
4273 /*
4274 * ":function" without argument: list functions.
4275 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004276 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004277 {
4278 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004279 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004280 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004281 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004282 }
4283
4284 /*
4285 * ":function /pat": list functions matching pattern.
4286 */
4287 if (*eap->arg == '/')
4288 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004289 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004290 if (!eap->skip)
4291 {
4292 regmatch_T regmatch;
4293
4294 c = *p;
4295 *p = NUL;
4296 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4297 *p = c;
4298 if (regmatch.regprog != NULL)
4299 {
4300 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004301 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004302 vim_regfree(regmatch.regprog);
4303 }
4304 }
4305 if (*p == '/')
4306 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004307 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004308 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004309 }
4310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 ga_init(&newargs);
4312 ga_init(&argtypes);
4313 ga_init(&default_args);
4314
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004315 /*
4316 * Get the function name. There are these situations:
4317 * func normal function name
4318 * "name" == func, "fudi.fd_dict" == NULL
4319 * dict.func new dictionary entry
4320 * "name" == NULL, "fudi.fd_dict" set,
4321 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4322 * dict.func existing dict entry with a Funcref
4323 * "name" == func, "fudi.fd_dict" set,
4324 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4325 * dict.func existing dict entry that's not a Funcref
4326 * "name" == NULL, "fudi.fd_dict" set,
4327 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4328 * s:func script-local function name
4329 * g:func global function name, same as "func"
4330 */
4331 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004332 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004333 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004334 // nested function, argument is (args).
4335 paren = TRUE;
4336 CLEAR_FIELD(fudi);
4337 }
4338 else
4339 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004340 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004341 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004342 if (p[0] == 's' && p[1] == ':')
4343 {
4344 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4345 return NULL;
4346 }
4347 p = to_name_end(p, TRUE);
4348 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4349 {
4350 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4351 eap->arg);
4352 return NULL;
4353 }
4354 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004355 }
4356
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004357 name = save_function_name(&p, &is_global, eap->skip,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004358 TFN_NO_AUTOLOAD | TFN_NEW_FUNC, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004359 paren = (vim_strchr(p, '(') != NULL);
4360 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004361 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004362 /*
4363 * Return on an invalid expression in braces, unless the expression
4364 * evaluation has been cancelled due to an aborting error, an
4365 * interrupt, or an exception.
4366 */
4367 if (!aborting())
4368 {
4369 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004370 semsg(_(e_key_not_present_in_dictionary), fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004371 vim_free(fudi.fd_newkey);
4372 return NULL;
4373 }
4374 else
4375 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004376 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004377
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004378 // For "export def FuncName()" in an autoload script the function name
4379 // is stored with the legacy autoload name "dir#script#FuncName" so
4380 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004381 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004382 {
4383 char_u *prefixed = may_prefix_autoload(name);
4384
4385 if (prefixed != NULL && prefixed != name)
4386 {
4387 vim_free(name);
4388 name = prefixed;
4389 }
4390 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004391 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004392 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004393 {
4394 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4395 goto ret_free;
4396 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004397 }
4398
Bram Moolenaare38eab22019-12-05 21:50:01 +01004399 // An error in a function call during evaluation of an expression in magic
4400 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004401 saved_did_emsg = did_emsg;
4402 did_emsg = FALSE;
4403
4404 /*
4405 * ":function func" with only function name: list function.
4406 */
4407 if (!paren)
4408 {
4409 if (!ends_excmd(*skipwhite(p)))
4410 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004411 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004412 goto ret_free;
4413 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004414 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004415 if (eap->nextcmd != NULL)
4416 *p = NUL;
4417 if (!eap->skip && !got_int)
4418 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004419 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004420 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4421 {
4422 char_u *up = untrans_function_name(name);
4423
4424 // With Vim9 script the name was made script-local, if not
4425 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004426 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004427 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004428 }
4429
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004430 if (fp != NULL)
4431 {
4432 list_func_head(fp, TRUE);
4433 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4434 {
4435 if (FUNCLINE(fp, j) == NULL)
4436 continue;
4437 msg_putchar('\n');
4438 msg_outnum((long)(j + 1));
4439 if (j < 9)
4440 msg_putchar(' ');
4441 if (j < 99)
4442 msg_putchar(' ');
4443 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004444 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004445 ui_breakcheck();
4446 }
4447 if (!got_int)
4448 {
4449 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004450 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 msg_puts(" enddef");
4452 else
4453 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004454 }
4455 }
4456 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004457 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004458 }
4459 goto ret_free;
4460 }
4461
4462 /*
4463 * ":function name(arg1, arg2)" Define function.
4464 */
4465 p = skipwhite(p);
4466 if (*p != '(')
4467 {
4468 if (!eap->skip)
4469 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004470 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004471 goto ret_free;
4472 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004473 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004474 if (vim_strchr(p, '(') != NULL)
4475 p = vim_strchr(p, '(');
4476 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004477
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004478 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4479 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004480 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004481 goto ret_free;
4482 }
4483
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004484 // In Vim9 script only global functions can be redefined.
4485 if (vim9script && eap->forceit && !is_global)
4486 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004487 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004488 goto ret_free;
4489 }
4490
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004491 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004492
Bram Moolenaar04b12692020-05-04 23:24:44 +02004493 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004494 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004495 // Check the name of the function. Unless it's a dictionary function
4496 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004497 if (name != NULL)
4498 arg = name;
4499 else
4500 arg = fudi.fd_newkey;
4501 if (arg != NULL && (fudi.fd_di == NULL
4502 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4503 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4504 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004505 char_u *name_base = arg;
4506 int i;
4507
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004508 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004509 {
4510 name_base = vim_strchr(arg, '_');
4511 if (name_base == NULL)
4512 name_base = arg + 3;
4513 else
4514 ++name_base;
4515 }
4516 for (i = 0; name_base[i] != NUL && (i == 0
4517 ? eval_isnamec1(name_base[i])
4518 : eval_isnamec(name_base[i])); ++i)
4519 ;
4520 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004521 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004522
4523 // In Vim9 script a function cannot have the same name as a
4524 // variable.
4525 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004526 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4527 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004528 + EVAL_VAR_NO_FUNC) == OK)
4529 {
4530 semsg(_(e_redefining_script_item_str), name_base);
4531 goto ret_free;
4532 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004533 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004534 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004535 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004536 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004537 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004538 goto ret_free;
4539 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004540 }
4541
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004542 // This may get more lines and make the pointers into the first line
4543 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004544 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004546 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004547 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004548 eap, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004549 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004550 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004551
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004553 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004555 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004556 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004557 if (*p != ':')
4558 {
4559 semsg(_(e_no_white_space_allowed_before_colon_str), p);
4560 p = skipwhite(p);
4561 }
4562 else if (!IS_WHITE_OR_NUL(p[1]))
4563 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004564 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02004565 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004567 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004568 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01004569 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004571 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004573 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004574 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02004575 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004576 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004577 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02004578 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004579 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580 else
4581 // find extra arguments "range", "dict", "abort" and "closure"
4582 for (;;)
4583 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01004584 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 p = skipwhite(p);
4586 if (STRNCMP(p, "range", 5) == 0)
4587 {
4588 flags |= FC_RANGE;
4589 p += 5;
4590 }
4591 else if (STRNCMP(p, "dict", 4) == 0)
4592 {
4593 flags |= FC_DICT;
4594 p += 4;
4595 }
4596 else if (STRNCMP(p, "abort", 5) == 0)
4597 {
4598 flags |= FC_ABORT;
4599 p += 5;
4600 }
4601 else if (STRNCMP(p, "closure", 7) == 0)
4602 {
4603 flags |= FC_CLOSURE;
4604 p += 7;
4605 if (current_funccal == NULL)
4606 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004607 emsg_funcname(e_closure_function_should_not_be_at_top_level,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 name == NULL ? (char_u *)"" : name);
4609 goto erret;
4610 }
4611 }
4612 else
4613 break;
4614 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004615
Bram Moolenaare38eab22019-12-05 21:50:01 +01004616 // When there is a line break use what follows for the function body.
4617 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004618 if (*p == '\n')
4619 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004620 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02004621 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
4622 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01004623 && !(VIM_ISWHITE(*whitep) && *p == '#'
4624 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02004625 && !eap->skip
4626 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004627 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004628
4629 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
4631 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004632 */
4633 if (KeyTyped)
4634 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004635 // Check if the function already exists, don't let the user type the
4636 // whole function before telling him it doesn't work! For a script we
4637 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004638 if (!eap->skip && !eap->forceit)
4639 {
4640 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004641 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004642 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00004643 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004644 }
4645
4646 if (!eap->skip && did_emsg)
4647 goto erret;
4648
Bram Moolenaare38eab22019-12-05 21:50:01 +01004649 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004650 cmdline_row = msg_row;
4651 }
4652
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004653 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004654 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004655
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004656 // Do not define the function when getting the body fails and when
4657 // skipping.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004658 if (get_function_body(eap, &newlines, line_arg, lines_to_free) == FAIL
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004659 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004660 goto erret;
4661
4662 /*
4663 * If there are no errors, add the function
4664 */
4665 if (fudi.fd_dict == NULL)
4666 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004667 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004668 char_u *find_name = name;
4669 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004670 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004672 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004673 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004674 var_conflict = TRUE;
4675
4676 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
4677 {
4678 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
4679
4680 if (si->sn_autoload_prefix != NULL)
4681 {
4682 if (is_export)
4683 {
4684 find_name = name + STRLEN(si->sn_autoload_prefix);
4685 v = find_var(find_name, &ht, TRUE);
4686 if (v != NULL)
4687 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004688 // Only check if the function already exists in the script,
4689 // global functions can be shadowed.
4690 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004691 }
4692 else
4693 {
4694 char_u *prefixed = may_prefix_autoload(name);
4695
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00004696 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004697 {
4698 v = find_var(prefixed, &ht, TRUE);
4699 if (v != NULL)
4700 var_conflict = TRUE;
4701 vim_free(prefixed);
4702 }
4703 }
4704 }
4705 }
4706 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004707 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004708 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004709 goto erret;
4710 }
4711
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004712 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02004713 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004714 {
Bram Moolenaareef21022020-08-01 22:16:43 +02004715 char_u *uname = untrans_function_name(name);
4716
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004717 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004718 }
4719
4720 if (fp != NULL || import != NULL)
4721 {
4722 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004723
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004724 // Function can be replaced with "function!" and when sourcing the
4725 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02004726 // A name that is used by an import can not be overruled.
4727 if (import != NULL
4728 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004729 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02004730 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004731 {
Bram Moolenaard604d782021-11-20 21:46:20 +00004732 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02004733 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004734 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02004735 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00004736 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004737 goto erret;
4738 }
4739 if (fp->uf_calls > 0)
4740 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004741 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00004742 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004743 goto erret;
4744 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004745 if (fp->uf_refcount > 1)
4746 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004747 // This function is referenced somewhere, don't redefine it but
4748 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004749 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004750 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004751 fp = NULL;
4752 overwrite = TRUE;
4753 }
4754 else
4755 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004756 char_u *exp_name = fp->uf_name_exp;
4757
4758 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01004759 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004760 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004761 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004762 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004763 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004764#ifdef FEAT_PROFILE
4765 fp->uf_profiling = FALSE;
4766 fp->uf_prof_initialized = FALSE;
4767#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01004768 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004769 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004770 }
4771 }
4772 else
4773 {
4774 char numbuf[20];
4775
4776 fp = NULL;
4777 if (fudi.fd_newkey == NULL && !eap->forceit)
4778 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004779 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004780 goto erret;
4781 }
4782 if (fudi.fd_di == NULL)
4783 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004784 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02004785 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004786 goto erret;
4787 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004788 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02004789 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004790 goto erret;
4791
Bram Moolenaare38eab22019-12-05 21:50:01 +01004792 // Give the function a sequential number. Can only be used with a
4793 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004794 vim_free(name);
4795 sprintf(numbuf, "%d", ++func_nr);
4796 name = vim_strsave((char_u *)numbuf);
4797 if (name == NULL)
4798 goto erret;
4799 }
4800
4801 if (fp == NULL)
4802 {
4803 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
4804 {
4805 int slen, plen;
4806 char_u *scriptname;
4807
Bram Moolenaare38eab22019-12-05 21:50:01 +01004808 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004809 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004810 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004811 {
4812 scriptname = autoload_name(name);
4813 if (scriptname != NULL)
4814 {
4815 p = vim_strchr(scriptname, '/');
4816 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004817 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004818 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004819 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004820 j = OK;
4821 vim_free(scriptname);
4822 }
4823 }
4824 if (j == FAIL)
4825 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004826 linenr_T save_lnum = SOURCING_LNUM;
4827
4828 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00004829 semsg(_(e_function_name_does_not_match_script_file_name_str),
4830 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004831 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004832 goto erret;
4833 }
4834 }
4835
Bram Moolenaar47ed5532019-08-08 20:49:14 +02004836 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004837 if (fp == NULL)
4838 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004839 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004840
4841 if (fudi.fd_dict != NULL)
4842 {
4843 if (fudi.fd_di == NULL)
4844 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004845 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004846 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
4847 if (fudi.fd_di == NULL)
4848 {
4849 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004850 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004851 goto erret;
4852 }
4853 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
4854 {
4855 vim_free(fudi.fd_di);
4856 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004857 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004858 goto erret;
4859 }
4860 }
4861 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004862 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004863 clear_tv(&fudi.fd_di->di_tv);
4864 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004865 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004866
Bram Moolenaare38eab22019-12-05 21:50:01 +01004867 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004868 flags |= FC_DICT;
4869 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004870 }
4871 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004872 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004873 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004874 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875
4876 if (eap->cmdidx == CMD_def)
4877 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02004878 int lnum_save = SOURCING_LNUM;
4879 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004880
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004881 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004882
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004883 // error messages are for the first function line
4884 SOURCING_LNUM = sourcing_lnum_top;
4885
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02004886 // The function may use script variables from the context.
4887 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004888
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004889 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004890 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004891 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004892 free_fp = fp_allocated;
4893 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004895 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004896
4897 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004898 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004899 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004900 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004901 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004902 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004903 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02004904 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02004906 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004907 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004909 if (fp_allocated)
4910 {
4911 // insert the new function in the function list
4912 set_ufunc_name(fp, name);
4913 if (overwrite)
4914 {
4915 hi = hash_find(&func_hashtab, name);
4916 hi->hi_key = UF2HIKEY(fp);
4917 }
4918 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
4919 {
4920 free_fp = TRUE;
4921 goto erret;
4922 }
4923 fp->uf_refcount = 1;
4924 }
4925
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004926 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004927 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004928 if ((flags & FC_CLOSURE) != 0)
4929 {
Bram Moolenaar58016442016-07-31 18:30:22 +02004930 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004931 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004932 }
4933 else
4934 fp->uf_scoped = NULL;
4935
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004936#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004937 if (prof_def_func())
4938 func_do_profile(fp);
4939#endif
4940 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02004941 if (sandbox)
4942 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004943 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004944 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004945 fp->uf_flags = flags;
4946 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01004947 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004948 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004949 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004950 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004951 if (is_export)
4952 {
4953 fp->uf_flags |= FC_EXPORT;
4954 // let ex_export() know the export worked.
4955 is_export = FALSE;
4956 }
4957
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004958 if (eap->cmdidx == CMD_def)
4959 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02004960 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
4961 // :func does not use Vim9 script syntax, even in a Vim9 script file
4962 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004963
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004964 goto ret_free;
4965
4966erret:
4967 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004968 ga_clear_strings(&default_args);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004969 if (fp != NULL)
4970 {
4971 ga_init(&fp->uf_args);
4972 ga_init(&fp->uf_def_args);
4973 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004974errret_2:
4975 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004976 if (fp != NULL)
4977 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004978 if (free_fp)
4979 {
4980 vim_free(fp);
4981 fp = NULL;
4982 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004983ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01004984 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004985 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004986 if (name != name_arg)
4987 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004988 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004989 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004990
4991 return fp;
4992}
4993
4994/*
4995 * ":function"
4996 */
4997 void
4998ex_function(exarg_T *eap)
4999{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005000 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005001
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005002 ga_init2(&lines_to_free, sizeof(char_u *), 50);
5003 (void)define_function(eap, NULL, &lines_to_free);
5004 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005005}
5006
5007/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005008 * Find a function by name, including "<lambda>123".
5009 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005010 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005011 * Return NULL if not found.
5012 */
5013 ufunc_T *
5014find_func_by_name(char_u *name, compiletype_T *compile_type)
5015{
5016 char_u *arg = name;
5017 char_u *fname;
5018 ufunc_T *ufunc;
5019 int is_global = FALSE;
5020
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005021 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5022 {
5023 *compile_type = CT_PROFILE;
5024 arg = skipwhite(arg + 7);
5025 }
5026 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5027 {
5028 *compile_type = CT_DEBUG;
5029 arg = skipwhite(arg + 5);
5030 }
5031
5032 if (STRNCMP(arg, "<lambda>", 8) == 0)
5033 {
5034 arg += 8;
5035 (void)getdigits(&arg);
5036 fname = vim_strnsave(name, arg - name);
5037 }
5038 else
5039 fname = trans_function_name(&arg, &is_global, FALSE,
5040 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
5041 if (fname == NULL)
5042 {
5043 semsg(_(e_invalid_argument_str), name);
5044 return NULL;
5045 }
5046 if (!ends_excmd2(name, arg))
5047 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005048 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005049 emsg(ex_errmsg(e_trailing_characters_str, arg));
5050 return NULL;
5051 }
5052
5053 ufunc = find_func(fname, is_global);
5054 if (ufunc == NULL)
5055 {
5056 char_u *p = untrans_function_name(fname);
5057
5058 if (p != NULL)
5059 // Try again without making it script-local.
5060 ufunc = find_func(p, FALSE);
5061 }
5062 vim_free(fname);
5063 if (ufunc == NULL)
5064 semsg(_(e_cannot_find_function_str), name);
5065 return ufunc;
5066}
5067
5068/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005069 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005070 * be compiled or the one specified by the argument.
5071 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005072 */
5073 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005074ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005075{
Bram Moolenaar822ba242020-05-24 23:00:18 +02005076 ufunc_T *ufunc;
5077
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005078 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005079 {
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005080 compiletype_T compile_type = CT_NONE;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005081
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005082 ufunc = find_func_by_name(eap->arg, &compile_type);
5083 if (ufunc != NULL)
5084 {
5085 if (func_needs_compiling(ufunc, compile_type))
5086 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5087 else
5088 smsg(_("Function %s does not need compiling"), eap->arg);
5089 }
5090 }
5091 else
5092 {
5093 long todo = (long)func_hashtab.ht_used;
5094 int changed = func_hashtab.ht_changed;
5095 hashitem_T *hi;
5096
5097 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5098 {
5099 if (!HASHITEM_EMPTY(hi))
5100 {
5101 --todo;
5102 ufunc = HI2UF(hi);
5103 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5104 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5105 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005106 {
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005107 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5108
5109 if (func_hashtab.ht_changed != changed)
5110 {
5111 // a function has been added or removed, need to start
5112 // over
5113 todo = (long)func_hashtab.ht_used;
5114 changed = func_hashtab.ht_changed;
5115 hi = func_hashtab.ht_array;
5116 --hi;
5117 }
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005118 }
5119 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005120 }
5121 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005122}
5123
5124/*
5125 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5126 * Return 2 if "p" starts with "s:".
5127 * Return 0 otherwise.
5128 */
5129 int
5130eval_fname_script(char_u *p)
5131{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005132 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5133 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005134 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5135 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5136 return 5;
5137 if (p[0] == 's' && p[1] == ':')
5138 return 2;
5139 return 0;
5140}
5141
5142 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005143translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005144{
5145 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005146 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005147 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005148}
5149
5150/*
5151 * Return TRUE when "ufunc" has old-style "..." varargs
5152 * or named varargs "...name: type".
5153 */
5154 int
5155has_varargs(ufunc_T *ufunc)
5156{
5157 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005158}
5159
5160/*
5161 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005162 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005163 */
5164 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005165function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005166{
5167 char_u *nm = name;
5168 char_u *p;
5169 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005170 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005171 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005172
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005173 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5174 if (no_deref)
5175 flag |= TFN_NO_DEREF;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005176 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005177 nm = skipwhite(nm);
5178
Bram Moolenaare38eab22019-12-05 21:50:01 +01005179 // Only accept "funcname", "funcname ", "funcname (..." and
5180 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005181 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005182 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005183 vim_free(p);
5184 return n;
5185}
5186
Bram Moolenaar113e1072019-01-20 15:30:40 +01005187#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005188 char_u *
5189get_expanded_name(char_u *name, int check)
5190{
5191 char_u *nm = name;
5192 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005193 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005194
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005195 p = trans_function_name(&nm, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005196 TFN_INT|TFN_QUIET, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005197
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005198 if (p != NULL && *nm == NUL
5199 && (!check || translated_function_exists(p, is_global)))
5200 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005201
5202 vim_free(p);
5203 return NULL;
5204}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005205#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005206
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005207/*
5208 * Function given to ExpandGeneric() to obtain the list of user defined
5209 * function names.
5210 */
5211 char_u *
5212get_user_func_name(expand_T *xp, int idx)
5213{
5214 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005215 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005216 static hashitem_T *hi;
5217 ufunc_T *fp;
5218
5219 if (idx == 0)
5220 {
5221 done = 0;
5222 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005223 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005224 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005225 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005226 {
5227 if (done++ > 0)
5228 ++hi;
5229 while (HASHITEM_EMPTY(hi))
5230 ++hi;
5231 fp = HI2UF(hi);
5232
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005233 // don't show dead, dict and lambda functions
5234 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005235 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005236 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005237
5238 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005239 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005240
5241 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005242 if (xp->xp_context != EXPAND_USER_FUNC
5243 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005244 {
5245 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005246 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005247 STRCAT(IObuff, ")");
5248 }
5249 return IObuff;
5250 }
5251 return NULL;
5252}
5253
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005254/*
5255 * ":delfunction {name}"
5256 */
5257 void
5258ex_delfunction(exarg_T *eap)
5259{
5260 ufunc_T *fp = NULL;
5261 char_u *p;
5262 char_u *name;
5263 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005264 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005265
5266 p = eap->arg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005267 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi,
5268 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005269 vim_free(fudi.fd_newkey);
5270 if (name == NULL)
5271 {
5272 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005273 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005274 return;
5275 }
5276 if (!ends_excmd(*skipwhite(p)))
5277 {
5278 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005279 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005280 return;
5281 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005282 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005283 if (eap->nextcmd != NULL)
5284 *p = NUL;
5285
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005286 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005287 {
5288 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005289 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005290 vim_free(name);
5291 return;
5292 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005293 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005294 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005295 vim_free(name);
5296
5297 if (!eap->skip)
5298 {
5299 if (fp == NULL)
5300 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005301 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005302 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005303 return;
5304 }
5305 if (fp->uf_calls > 0)
5306 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005307 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005308 return;
5309 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005310 if (fp->uf_flags & FC_VIM9)
5311 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005312 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005313 return;
5314 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005315
5316 if (fudi.fd_dict != NULL)
5317 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005318 // Delete the dict item that refers to the function, it will
5319 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005320 dictitem_remove(fudi.fd_dict, fudi.fd_di);
5321 }
5322 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005323 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005324 // A normal function (not a numbered function or lambda) has a
5325 // refcount of 1 for the entry in the hashtable. When deleting
5326 // it and the refcount is more than one, it should be kept.
5327 // A numbered function and lambda should be kept if the refcount is
5328 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005329 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005330 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005331 // Function is still referenced somewhere. Don't free it but
5332 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005333 if (func_remove(fp))
5334 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005335 }
5336 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005337 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005338 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005339 }
5340}
5341
5342/*
5343 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005344 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005345 */
5346 void
5347func_unref(char_u *name)
5348{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005349 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005350
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005351 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005352 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005353 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005354 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005355 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005356#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005357 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005358#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005359 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005360 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005361 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005362}
5363
5364/*
5365 * Unreference a Function: decrement the reference count and free it when it
5366 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005367 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005368 */
5369 void
5370func_ptr_unref(ufunc_T *fp)
5371{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005372 if (fp != NULL && (--fp->uf_refcount <= 0
5373 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5374 && fp->uf_partial->pt_refcount <= 1
5375 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005376 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005377 // Only delete it when it's not being used. Otherwise it's done
5378 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005379 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005380 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005381 }
5382}
5383
5384/*
5385 * Count a reference to a Function.
5386 */
5387 void
5388func_ref(char_u *name)
5389{
5390 ufunc_T *fp;
5391
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005392 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005393 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005394 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005395 if (fp != NULL)
5396 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005397 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005398 // Only give an error for a numbered function.
5399 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005400 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005401}
5402
5403/*
5404 * Count a reference to a Function.
5405 */
5406 void
5407func_ptr_ref(ufunc_T *fp)
5408{
5409 if (fp != NULL)
5410 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005411}
5412
5413/*
5414 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5415 * referenced from anywhere that is in use.
5416 */
5417 static int
5418can_free_funccal(funccall_T *fc, int copyID)
5419{
5420 return (fc->l_varlist.lv_copyID != copyID
5421 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005422 && fc->l_avars.dv_copyID != copyID
5423 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005424}
5425
5426/*
5427 * ":return [expr]"
5428 */
5429 void
5430ex_return(exarg_T *eap)
5431{
5432 char_u *arg = eap->arg;
5433 typval_T rettv;
5434 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005435 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005436
5437 if (current_funccal == NULL)
5438 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005439 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005440 return;
5441 }
5442
Bram Moolenaar844fb642021-10-23 13:32:30 +01005443 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005444 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5445
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005446 if (eap->skip)
5447 ++emsg_skip;
5448
5449 eap->nextcmd = NULL;
5450 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005451 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005452 {
5453 if (!eap->skip)
5454 returning = do_return(eap, FALSE, TRUE, &rettv);
5455 else
5456 clear_tv(&rettv);
5457 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005458 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005459 else if (!eap->skip)
5460 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005461 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005462 update_force_abort();
5463
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005464 /*
5465 * Return unless the expression evaluation has been cancelled due to an
5466 * aborting error, an interrupt, or an exception.
5467 */
5468 if (!aborting())
5469 returning = do_return(eap, FALSE, TRUE, NULL);
5470 }
5471
Bram Moolenaare38eab22019-12-05 21:50:01 +01005472 // When skipping or the return gets pending, advance to the next command
5473 // in this line (!returning). Otherwise, ignore the rest of the line.
5474 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005475 if (returning)
5476 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005477 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02005478 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005479
5480 if (eap->skip)
5481 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02005482 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005483}
5484
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005485 static int
5486ex_call_inner(
5487 exarg_T *eap,
5488 char_u *name,
5489 char_u **arg,
5490 char_u *startarg,
5491 funcexe_T *funcexe_init,
5492 evalarg_T *evalarg)
5493{
5494 linenr_T lnum;
5495 int doesrange;
5496 typval_T rettv;
5497 int failed = FALSE;
5498
5499 /*
5500 * When skipping, evaluate the function once, to find the end of the
5501 * arguments.
5502 * When the function takes a range, this is discovered after the first
5503 * call, and the loop is broken.
5504 */
5505 if (eap->skip)
5506 {
5507 ++emsg_skip;
5508 lnum = eap->line2; // do it once, also with an invalid range
5509 }
5510 else
5511 lnum = eap->line1;
5512 for ( ; lnum <= eap->line2; ++lnum)
5513 {
5514 funcexe_T funcexe;
5515
5516 if (!eap->skip && eap->addr_count > 0)
5517 {
5518 if (lnum > curbuf->b_ml.ml_line_count)
5519 {
5520 // If the function deleted lines or switched to another buffer
5521 // the line number may become invalid.
5522 emsg(_(e_invalid_range));
5523 break;
5524 }
5525 curwin->w_cursor.lnum = lnum;
5526 curwin->w_cursor.col = 0;
5527 curwin->w_cursor.coladd = 0;
5528 }
5529 *arg = startarg;
5530
5531 funcexe = *funcexe_init;
5532 funcexe.fe_doesrange = &doesrange;
5533 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
5534 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
5535 {
5536 failed = TRUE;
5537 break;
5538 }
5539 if (has_watchexpr())
5540 dbg_check_breakpoint(eap);
5541
5542 // Handle a function returning a Funcref, Dictionary or List.
5543 if (handle_subscript(arg, NULL, &rettv,
5544 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
5545 {
5546 failed = TRUE;
5547 break;
5548 }
5549
5550 clear_tv(&rettv);
5551 if (doesrange || eap->skip)
5552 break;
5553
5554 // Stop when immediately aborting on error, or when an interrupt
5555 // occurred or an exception was thrown but not caught.
5556 // get_func_tv() returned OK, so that the check for trailing
5557 // characters below is executed.
5558 if (aborting())
5559 break;
5560 }
5561 if (eap->skip)
5562 --emsg_skip;
5563 return failed;
5564}
5565
5566/*
5567 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
5568 * Returns FAIL or OK.
5569 */
5570 static int
5571ex_defer_inner(char_u *name, char_u **arg, evalarg_T *evalarg)
5572{
5573 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
5574 int argcount = 0; // number of arguments found
5575 defer_T *dr;
5576 int ret = FAIL;
5577 char_u *saved_name;
5578
5579 if (current_funccal == NULL)
5580 {
5581 semsg(_(e_str_not_inside_function), "defer");
5582 return FAIL;
5583 }
5584 if (get_func_arguments(arg, evalarg, FALSE, argvars, &argcount) == FAIL)
5585 goto theend;
5586 saved_name = vim_strsave(name);
5587 if (saved_name == NULL)
5588 goto theend;
5589
5590 if (current_funccal->fc_defer.ga_itemsize == 0)
5591 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
5592 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
5593 goto theend;
5594 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
5595 + current_funccal->fc_defer.ga_len++;
5596 dr->dr_name = saved_name;
5597 dr->dr_argcount = argcount;
5598 while (argcount > 0)
5599 {
5600 --argcount;
5601 dr->dr_argvars[argcount] = argvars[argcount];
5602 }
5603 ret = OK;
5604
5605theend:
5606 while (--argcount >= 0)
5607 clear_tv(&argvars[argcount]);
5608 return ret;
5609}
5610
5611/*
5612 * Invoked after a functions has finished: invoke ":defer" functions.
5613 */
5614 void
5615handle_defer(void)
5616{
5617 int idx;
5618
5619 for (idx = current_funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
5620 {
5621 funcexe_T funcexe;
5622 typval_T rettv;
5623 defer_T *dr = ((defer_T *)current_funccal->fc_defer.ga_data) + idx;
5624 int i;
5625
5626 CLEAR_FIELD(funcexe);
5627 funcexe.fe_evaluate = TRUE;
5628
5629 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
5630 call_func(dr->dr_name, -1, &rettv,
5631 dr->dr_argcount, dr->dr_argvars, &funcexe);
5632 clear_tv(&rettv);
5633 vim_free(dr->dr_name);
5634 for (i = dr->dr_argcount - 1; i >= 0; --i)
5635 clear_tv(&dr->dr_argvars[i]);
5636 }
5637 ga_clear(&current_funccal->fc_defer);
5638}
5639
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005640/*
5641 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005642 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005643 */
5644 void
5645ex_call(exarg_T *eap)
5646{
5647 char_u *arg = eap->arg;
5648 char_u *startarg;
5649 char_u *name;
5650 char_u *tofree;
5651 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005652 int failed = FALSE;
5653 funcdict_T fudi;
5654 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005655 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005656 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005657 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00005658 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005659
Bram Moolenaare6b53242020-07-01 17:28:33 +02005660 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005661 if (eap->skip)
5662 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005663 typval_T rettv;
5664
Bram Moolenaare38eab22019-12-05 21:50:01 +01005665 // trans_function_name() doesn't work well when skipping, use eval0()
5666 // instead to skip to any following command, e.g. for:
5667 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005668 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005669 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005670 clear_tv(&rettv);
5671 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005672 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005673 return;
5674 }
5675
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005676 tofree = trans_function_name(&arg, NULL, eap->skip, TFN_INT,
Bram Moolenaar4525a572022-02-13 11:57:33 +00005677 &fudi, &partial, vim9script ? &type : NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005678 if (fudi.fd_newkey != NULL)
5679 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005680 // Still need to give an error message for missing key.
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005681 semsg(_(e_key_not_present_in_dictionary), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005682 vim_free(fudi.fd_newkey);
5683 }
5684 if (tofree == NULL)
5685 return;
5686
Bram Moolenaare38eab22019-12-05 21:50:01 +01005687 // Increase refcount on dictionary, it could get deleted when evaluating
5688 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005689 if (fudi.fd_dict != NULL)
5690 ++fudi.fd_dict->dv_refcount;
5691
Bram Moolenaare38eab22019-12-05 21:50:01 +01005692 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
5693 // contents. For VAR_PARTIAL get its partial, unless we already have one
5694 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005695 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005696 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00005697 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00005698 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005699
Bram Moolenaare38eab22019-12-05 21:50:01 +01005700 // Skip white space to allow ":call func ()". Not good, but required for
5701 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005702 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005703 if (*startarg != '(')
5704 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005705 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005706 goto end;
5707 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00005708 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02005709 {
5710 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
5711 goto end;
5712 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005713
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005714 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005715 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005716 arg = startarg;
5717 failed = ex_defer_inner(name, &arg, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005718 }
5719 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005720 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005721 funcexe_T funcexe;
5722
Bram Moolenaara80faa82020-04-12 19:37:17 +02005723 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005724 funcexe.fe_check_type = type;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00005725 funcexe.fe_partial = partial;
5726 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005727 funcexe.fe_firstline = eap->line1;
5728 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005729 funcexe.fe_found_var = found_var;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005730 funcexe.fe_evaluate = !eap->skip;
5731 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005732 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005733
Bram Moolenaar1d341892021-09-18 15:25:52 +02005734 // When inside :try we need to check for following "| catch" or "| endtry".
5735 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005736 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005737 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005738 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02005739 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02005740 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005741 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02005742 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01005743 {
5744 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00005745 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01005746 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005747 }
5748 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02005749 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005750 }
Bram Moolenaara929c922022-04-18 15:21:17 +01005751 // Must be after using "arg", it may point into memory cleared here.
5752 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005753
5754end:
5755 dict_unref(fudi.fd_dict);
5756 vim_free(tofree);
5757}
5758
5759/*
5760 * Return from a function. Possibly makes the return pending. Also called
5761 * for a pending return at the ":endtry" or after returning from an extra
5762 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
5763 * when called due to a ":return" command. "rettv" may point to a typval_T
5764 * with the return rettv. Returns TRUE when the return can be carried out,
5765 * FALSE when the return gets pending.
5766 */
5767 int
5768do_return(
5769 exarg_T *eap,
5770 int reanimate,
5771 int is_cmd,
5772 void *rettv)
5773{
5774 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01005775 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005776
5777 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005778 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005779 current_funccal->returned = FALSE;
5780
5781 /*
5782 * Cleanup (and inactivate) conditionals, but stop when a try conditional
5783 * not in its finally clause (which then is to be executed next) is found.
5784 * In this case, make the ":return" pending for execution at the ":endtry".
5785 * Otherwise, return normally.
5786 */
5787 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
5788 if (idx >= 0)
5789 {
5790 cstack->cs_pending[idx] = CSTP_RETURN;
5791
5792 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005793 // A pending return again gets pending. "rettv" points to an
5794 // allocated variable with the rettv of the original ":return"'s
5795 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005796 cstack->cs_rettv[idx] = rettv;
5797 else
5798 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005799 // When undoing a return in order to make it pending, get the stored
5800 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005801 if (reanimate)
5802 rettv = current_funccal->rettv;
5803
5804 if (rettv != NULL)
5805 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005806 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005807 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
5808 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
5809 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005810 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005811 }
5812 else
5813 cstack->cs_rettv[idx] = NULL;
5814
5815 if (reanimate)
5816 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005817 // The pending return value could be overwritten by a ":return"
5818 // without argument in a finally clause; reset the default
5819 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005820 current_funccal->rettv->v_type = VAR_NUMBER;
5821 current_funccal->rettv->vval.v_number = 0;
5822 }
5823 }
5824 report_make_pending(CSTP_RETURN, rettv);
5825 }
5826 else
5827 {
5828 current_funccal->returned = TRUE;
5829
Bram Moolenaare38eab22019-12-05 21:50:01 +01005830 // If the return is carried out now, store the return value. For
5831 // a return immediately after reanimation, the value is already
5832 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005833 if (!reanimate && rettv != NULL)
5834 {
5835 clear_tv(current_funccal->rettv);
5836 *current_funccal->rettv = *(typval_T *)rettv;
5837 if (!is_cmd)
5838 vim_free(rettv);
5839 }
5840 }
5841
5842 return idx < 0;
5843}
5844
5845/*
5846 * Free the variable with a pending return value.
5847 */
5848 void
5849discard_pending_return(void *rettv)
5850{
5851 free_tv((typval_T *)rettv);
5852}
5853
5854/*
5855 * Generate a return command for producing the value of "rettv". The result
5856 * is an allocated string. Used by report_pending() for verbose messages.
5857 */
5858 char_u *
5859get_return_cmd(void *rettv)
5860{
5861 char_u *s = NULL;
5862 char_u *tofree = NULL;
5863 char_u numbuf[NUMBUFLEN];
5864
5865 if (rettv != NULL)
5866 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
5867 if (s == NULL)
5868 s = (char_u *)"";
5869
5870 STRCPY(IObuff, ":return ");
5871 STRNCPY(IObuff + 8, s, IOSIZE - 8);
5872 if (STRLEN(s) + 8 >= IOSIZE)
5873 STRCPY(IObuff + IOSIZE - 4, "...");
5874 vim_free(tofree);
5875 return vim_strsave(IObuff);
5876}
5877
5878/*
5879 * Get next function line.
5880 * Called by do_cmdline() to get the next line.
5881 * Returns allocated string, or NULL for end of function.
5882 */
5883 char_u *
5884get_func_line(
5885 int c UNUSED,
5886 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02005887 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005888 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005889{
5890 funccall_T *fcp = (funccall_T *)cookie;
5891 ufunc_T *fp = fcp->func;
5892 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005893 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005894
Bram Moolenaare38eab22019-12-05 21:50:01 +01005895 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005896 if (fcp->dbg_tick != debug_tick)
5897 {
5898 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005899 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005900 fcp->dbg_tick = debug_tick;
5901 }
5902#ifdef FEAT_PROFILE
5903 if (do_profiling == PROF_YES)
5904 func_line_end(cookie);
5905#endif
5906
5907 gap = &fp->uf_lines;
5908 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5909 || fcp->returned)
5910 retval = NULL;
5911 else
5912 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005913 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005914 while (fcp->linenr < gap->ga_len
5915 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
5916 ++fcp->linenr;
5917 if (fcp->linenr >= gap->ga_len)
5918 retval = NULL;
5919 else
5920 {
5921 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005922 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005923#ifdef FEAT_PROFILE
5924 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005925 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005926#endif
5927 }
5928 }
5929
Bram Moolenaare38eab22019-12-05 21:50:01 +01005930 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005931 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005932 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005933 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01005934 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005935 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005936 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005937 fcp->dbg_tick = debug_tick;
5938 }
5939
5940 return retval;
5941}
5942
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005943/*
5944 * Return TRUE if the currently active function should be ended, because a
5945 * return was encountered or an error occurred. Used inside a ":while".
5946 */
5947 int
5948func_has_ended(void *cookie)
5949{
5950 funccall_T *fcp = (funccall_T *)cookie;
5951
Bram Moolenaare38eab22019-12-05 21:50:01 +01005952 // Ignore the "abort" flag if the abortion behavior has been changed due to
5953 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005954 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5955 || fcp->returned);
5956}
5957
5958/*
5959 * return TRUE if cookie indicates a function which "abort"s on errors.
5960 */
5961 int
5962func_has_abort(
5963 void *cookie)
5964{
5965 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
5966}
5967
5968
5969/*
5970 * Turn "dict.Func" into a partial for "Func" bound to "dict".
5971 * Don't do this when "Func" is already a partial that was bound
5972 * explicitly (pt_auto is FALSE).
5973 * Changes "rettv" in-place.
5974 * Returns the updated "selfdict_in".
5975 */
5976 dict_T *
5977make_partial(dict_T *selfdict_in, typval_T *rettv)
5978{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005979 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005980 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005981 char_u fname_buf[FLEN_FIXED + 1];
5982 int error;
5983 dict_T *selfdict = selfdict_in;
5984
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005985 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
5986 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005987 fp = rettv->vval.v_partial->pt_func;
5988 else
5989 {
5990 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005991 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005992 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00005993 if (fname == NULL)
5994 {
5995 // There is no point binding a dict to a NULL function, just create
5996 // a function reference.
5997 rettv->v_type = VAR_FUNC;
5998 rettv->vval.v_string = NULL;
5999 }
6000 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006001 {
6002 char_u *tofree = NULL;
6003
6004 // Translate "s:func" to the stored function name.
6005 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6006 fp = find_func(fname, FALSE);
6007 vim_free(tofree);
6008 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006009 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006010
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006011 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006012 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006013 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006014
6015 if (pt != NULL)
6016 {
6017 pt->pt_refcount = 1;
6018 pt->pt_dict = selfdict;
6019 pt->pt_auto = TRUE;
6020 selfdict = NULL;
6021 if (rettv->v_type == VAR_FUNC)
6022 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006023 // Just a function: Take over the function name and use
6024 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006025 pt->pt_name = rettv->vval.v_string;
6026 }
6027 else
6028 {
6029 partial_T *ret_pt = rettv->vval.v_partial;
6030 int i;
6031
Bram Moolenaare38eab22019-12-05 21:50:01 +01006032 // Partial: copy the function name, use selfdict and copy
6033 // args. Can't take over name or args, the partial might
6034 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006035 if (ret_pt->pt_name != NULL)
6036 {
6037 pt->pt_name = vim_strsave(ret_pt->pt_name);
6038 func_ref(pt->pt_name);
6039 }
6040 else
6041 {
6042 pt->pt_func = ret_pt->pt_func;
6043 func_ptr_ref(pt->pt_func);
6044 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006045 if (ret_pt->pt_argc > 0)
6046 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006047 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006048 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006049 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006050 pt->pt_argc = 0;
6051 else
6052 {
6053 pt->pt_argc = ret_pt->pt_argc;
6054 for (i = 0; i < pt->pt_argc; i++)
6055 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6056 }
6057 }
6058 partial_unref(ret_pt);
6059 }
6060 rettv->v_type = VAR_PARTIAL;
6061 rettv->vval.v_partial = pt;
6062 }
6063 }
6064 return selfdict;
6065}
6066
6067/*
6068 * Return the name of the executed function.
6069 */
6070 char_u *
6071func_name(void *cookie)
6072{
6073 return ((funccall_T *)cookie)->func->uf_name;
6074}
6075
6076/*
6077 * Return the address holding the next breakpoint line for a funccall cookie.
6078 */
6079 linenr_T *
6080func_breakpoint(void *cookie)
6081{
6082 return &((funccall_T *)cookie)->breakpoint;
6083}
6084
6085/*
6086 * Return the address holding the debug tick for a funccall cookie.
6087 */
6088 int *
6089func_dbg_tick(void *cookie)
6090{
6091 return &((funccall_T *)cookie)->dbg_tick;
6092}
6093
6094/*
6095 * Return the nesting level for a funccall cookie.
6096 */
6097 int
6098func_level(void *cookie)
6099{
6100 return ((funccall_T *)cookie)->level;
6101}
6102
6103/*
6104 * Return TRUE when a function was ended by a ":return" command.
6105 */
6106 int
6107current_func_returned(void)
6108{
6109 return current_funccal->returned;
6110}
6111
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006112 int
6113free_unref_funccal(int copyID, int testing)
6114{
6115 int did_free = FALSE;
6116 int did_free_funccal = FALSE;
6117 funccall_T *fc, **pfc;
6118
6119 for (pfc = &previous_funccal; *pfc != NULL; )
6120 {
6121 if (can_free_funccal(*pfc, copyID))
6122 {
6123 fc = *pfc;
6124 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006125 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006126 did_free = TRUE;
6127 did_free_funccal = TRUE;
6128 }
6129 else
6130 pfc = &(*pfc)->caller;
6131 }
6132 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006133 // When a funccal was freed some more items might be garbage
6134 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006135 (void)garbage_collect(testing);
6136
6137 return did_free;
6138}
6139
6140/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006141 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006142 */
6143 static funccall_T *
6144get_funccal(void)
6145{
6146 int i;
6147 funccall_T *funccal;
6148 funccall_T *temp_funccal;
6149
6150 funccal = current_funccal;
6151 if (debug_backtrace_level > 0)
6152 {
6153 for (i = 0; i < debug_backtrace_level; i++)
6154 {
6155 temp_funccal = funccal->caller;
6156 if (temp_funccal)
6157 funccal = temp_funccal;
6158 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006159 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006160 debug_backtrace_level = i;
6161 }
6162 }
6163 return funccal;
6164}
6165
6166/*
6167 * Return the hashtable used for local variables in the current funccal.
6168 * Return NULL if there is no current funccal.
6169 */
6170 hashtab_T *
6171get_funccal_local_ht()
6172{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006173 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006174 return NULL;
6175 return &get_funccal()->l_vars.dv_hashtab;
6176}
6177
6178/*
6179 * Return the l: scope variable.
6180 * Return NULL if there is no current funccal.
6181 */
6182 dictitem_T *
6183get_funccal_local_var()
6184{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006185 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006186 return NULL;
6187 return &get_funccal()->l_vars_var;
6188}
6189
6190/*
6191 * Return the hashtable used for argument in the current funccal.
6192 * Return NULL if there is no current funccal.
6193 */
6194 hashtab_T *
6195get_funccal_args_ht()
6196{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006197 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006198 return NULL;
6199 return &get_funccal()->l_avars.dv_hashtab;
6200}
6201
6202/*
6203 * Return the a: scope variable.
6204 * Return NULL if there is no current funccal.
6205 */
6206 dictitem_T *
6207get_funccal_args_var()
6208{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006209 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006210 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01006211 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006212}
6213
6214/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006215 * List function variables, if there is a function.
6216 */
6217 void
6218list_func_vars(int *first)
6219{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006220 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006221 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006222 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006223}
6224
6225/*
6226 * If "ht" is the hashtable for local variables in the current funccal, return
6227 * the dict that contains it.
6228 * Otherwise return NULL.
6229 */
6230 dict_T *
6231get_current_funccal_dict(hashtab_T *ht)
6232{
6233 if (current_funccal != NULL
6234 && ht == &current_funccal->l_vars.dv_hashtab)
6235 return &current_funccal->l_vars;
6236 return NULL;
6237}
6238
6239/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006240 * Search hashitem in parent scope.
6241 */
6242 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006243find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006244{
6245 funccall_T *old_current_funccal = current_funccal;
6246 hashtab_T *ht;
6247 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006248 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006249
6250 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
6251 return NULL;
6252
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006253 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006254 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006255 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006256 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006257 ht = find_var_ht(name, &varname);
6258 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006259 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006260 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006261 if (!HASHITEM_EMPTY(hi))
6262 {
6263 *pht = ht;
6264 break;
6265 }
6266 }
6267 if (current_funccal == current_funccal->func->uf_scoped)
6268 break;
6269 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006270 }
6271 current_funccal = old_current_funccal;
6272
6273 return hi;
6274}
6275
6276/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006277 * Search variable in parent scope.
6278 */
6279 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006280find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006281{
6282 dictitem_T *v = NULL;
6283 funccall_T *old_current_funccal = current_funccal;
6284 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006285 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006286
6287 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
6288 return NULL;
6289
Bram Moolenaare38eab22019-12-05 21:50:01 +01006290 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006291 current_funccal = current_funccal->func->uf_scoped;
6292 while (current_funccal)
6293 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006294 ht = find_var_ht(name, &varname);
6295 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006296 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006297 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006298 if (v != NULL)
6299 break;
6300 }
6301 if (current_funccal == current_funccal->func->uf_scoped)
6302 break;
6303 current_funccal = current_funccal->func->uf_scoped;
6304 }
6305 current_funccal = old_current_funccal;
6306
6307 return v;
6308}
6309
6310/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006311 * Set "copyID + 1" in previous_funccal and callers.
6312 */
6313 int
6314set_ref_in_previous_funccal(int copyID)
6315{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006316 funccall_T *fc;
6317
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006318 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006319 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006320 fc->fc_copyID = copyID + 1;
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006321 if (set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
6322 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
6323 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL))
6324 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006325 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006326 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006327}
6328
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006329 static int
6330set_ref_in_funccal(funccall_T *fc, int copyID)
6331{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006332 if (fc->fc_copyID != copyID)
6333 {
6334 fc->fc_copyID = copyID;
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006335 if (set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
6336 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
6337 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
6338 || set_ref_in_func(NULL, fc->func, copyID))
6339 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006340 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006341 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006342}
6343
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006344/*
6345 * Set "copyID" in all local vars and arguments in the call stack.
6346 */
6347 int
6348set_ref_in_call_stack(int copyID)
6349{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006350 funccall_T *fc;
6351 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006352
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006353 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6354 if (set_ref_in_funccal(fc, copyID))
6355 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006356
6357 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006358 for (entry = funccal_stack; entry != NULL; entry = entry->next)
6359 for (fc = entry->top_funccal; fc != NULL; fc = fc->caller)
6360 if (set_ref_in_funccal(fc, copyID))
6361 return TRUE;
6362 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006363}
6364
6365/*
6366 * Set "copyID" in all functions available by name.
6367 */
6368 int
6369set_ref_in_functions(int copyID)
6370{
6371 int todo;
6372 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006373 ufunc_T *fp;
6374
6375 todo = (int)func_hashtab.ht_used;
6376 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006377 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006378 if (!HASHITEM_EMPTY(hi))
6379 {
6380 --todo;
6381 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006382 if (!func_name_refcount(fp->uf_name)
6383 && set_ref_in_func(NULL, fp, copyID))
6384 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006385 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006386 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006387 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006388}
6389
6390/*
6391 * Set "copyID" in all function arguments.
6392 */
6393 int
6394set_ref_in_func_args(int copyID)
6395{
6396 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006397
6398 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006399 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
6400 copyID, NULL, NULL))
6401 return TRUE;
6402 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006403}
6404
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006405/*
6406 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006407 * Returns TRUE if setting references failed somehow.
6408 */
6409 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006410set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006411{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006412 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006413 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01006414 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006415 char_u fname_buf[FLEN_FIXED + 1];
6416 char_u *tofree = NULL;
6417 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006418 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006419
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006420 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006421 return FALSE;
6422
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006423 if (fp_in == NULL)
6424 {
6425 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006426 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006427 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006428 if (fp != NULL)
6429 {
6430 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006431 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006432 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02006433
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006434 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006435 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006436}
6437
Bram Moolenaare38eab22019-12-05 21:50:01 +01006438#endif // FEAT_EVAL