blob: e0c1d5f9196f6f0a23fc22c751013ca9c839200e [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 Moolenaar58779852022-09-06 18:31:14 +010036static void handle_defer_one(funccall_T *funccal);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020037
38 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +000039func_init(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020040{
41 hash_init(&func_hashtab);
42}
43
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020044/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020045 * Return the function hash table
46 */
47 hashtab_T *
48func_tbl_get(void)
49{
50 return &func_hashtab;
51}
52
53/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020054 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020055 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010056 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010057 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaardce24412022-02-08 20:35:30 +000058 * If "eap" is not NULL use it to check for an already declared name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010059 * Return a pointer to after the type.
60 * When something is wrong return "arg".
61 */
62 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010063one_function_arg(
64 char_u *arg,
65 garray_T *newargs,
66 garray_T *argtypes,
67 int types_optional,
h-eastb895b0f2023-09-24 15:46:31 +020068 garray_T *arg_objm,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010069 evalarg_T *evalarg,
Bram Moolenaardce24412022-02-08 20:35:30 +000070 exarg_T *eap,
Bram Moolenaar2a389082021-04-09 20:24:31 +020071 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010072 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073{
Bram Moolenaar6e949782020-04-13 17:21:00 +020074 char_u *p = arg;
75 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020076 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077
78 while (ASCII_ISALNUM(*p) || *p == '_')
79 ++p;
80 if (arg == p || isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020081 || (argtypes == NULL
82 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
83 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010084 {
85 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000086 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087 return arg;
88 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010089
Bram Moolenaarce723f32023-06-10 19:00:12 +010090 // Extra checks in Vim9 script.
91 if (!skip && argtypes != NULL)
92 {
93 int c = *p;
94 *p = NUL;
95 int r = check_reserved_name(arg, FALSE);
96 *p = c;
97 if (r == FAIL)
98 return arg;
99
100 // Cannot use script var name for argument. In function: also check
101 // local vars and arguments.
102 if (check_defined(arg, p - arg,
103 evalarg == NULL ? NULL : evalarg->eval_cctx,
Bram Moolenaardce24412022-02-08 20:35:30 +0000104 eap == NULL ? NULL : eap->cstack, TRUE) == FAIL)
Bram Moolenaarce723f32023-06-10 19:00:12 +0100105 return arg;
106 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100108 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
109 return arg;
110 if (newargs != NULL)
111 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100112 int c;
113 int i;
114
115 c = *p;
116 *p = NUL;
117 arg_copy = vim_strsave(arg);
118 if (arg_copy == NULL)
119 {
120 *p = c;
121 return arg;
122 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200123 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200124 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200125 // Check for duplicate argument name.
126 for (i = 0; i < newargs->ga_len; ++i)
127 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
128 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000129 semsg(_(e_duplicate_argument_name_str), arg_copy);
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200130 vim_free(arg_copy);
131 return arg;
132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100133 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
134 newargs->ga_len++;
135
136 *p = c;
137 }
138
139 // get any type from "arg: type"
h-eastb895b0f2023-09-24 15:46:31 +0200140 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK)
141 && arg_objm != NULL && (skip || ga_grow(arg_objm, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 {
143 char_u *type = NULL;
144
Bram Moolenaar6e949782020-04-13 17:21:00 +0200145 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
146 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200147 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200148 arg_copy == NULL ? arg : arg_copy);
149 p = skipwhite(p);
150 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 if (*p == ':')
152 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200153 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100154 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200155 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100156 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200157 return arg;
158 }
159 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200160 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100161 if (!skip)
162 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100163 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200164 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200165 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200166 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200167 arg_copy == NULL ? arg : arg_copy);
168 return arg;
169 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100170 if (!skip)
171 {
172 if (type == NULL && types_optional)
173 // lambda arguments default to "any" type
Bram Moolenaar2a389082021-04-09 20:24:31 +0200174 type = vim_strsave((char_u *)
175 (is_vararg ? "list<any>" : "any"));
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100176 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
h-eastb895b0f2023-09-24 15:46:31 +0200177 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = FALSE;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 }
180
181 return p;
182}
183
184/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000185 * Handle line continuation in function arguments or body.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000186 * Get a next line, store it in "eap" if appropriate and put the line in
187 * "lines_to_free" to free the line later.
Bram Moolenaar7473a842021-12-28 17:55:26 +0000188 */
189 static char_u *
190get_function_line(
191 exarg_T *eap,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000192 garray_T *lines_to_free,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000193 int indent,
194 getline_opt_T getline_options)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000195{
196 char_u *theline;
197
198 if (eap->getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +0000199 theline = getcmdline(':', 0L, indent, 0);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000200 else
201 theline = eap->getline(':', eap->cookie, indent, getline_options);
202 if (theline != NULL)
203 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000204 if (lines_to_free->ga_len > 0
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000205 && eap->cmdlinep != NULL
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000206 && *eap->cmdlinep == ((char_u **)lines_to_free->ga_data)
207 [lines_to_free->ga_len - 1])
Bram Moolenaar7473a842021-12-28 17:55:26 +0000208 *eap->cmdlinep = theline;
Bram Moolenaarb5820102023-01-25 12:27:13 +0000209 (void)ga_add_string(lines_to_free, theline);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000210 }
211
212 return theline;
213}
214
215/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200216 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100217 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100218 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200219 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100220 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200221get_function_args(
222 char_u **argp,
223 char_u endchar,
224 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100225 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100226 int types_optional, // types optional if "argtypes" is not NULL
h-eastb895b0f2023-09-24 15:46:31 +0200227 garray_T *arg_objm, // NULL unless using :def
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100228 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200229 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200230 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200231 int skip,
Bram Moolenaardce24412022-02-08 20:35:30 +0000232 exarg_T *eap, // can be NULL
Bram Moolenaar554d0312023-01-05 19:59:18 +0000233 int in_class, // non-zero when inside a class or interface
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000234 garray_T *newlines, // function body lines
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000235 garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200236{
237 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100238 char_u *arg;
239 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200240 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200241 int any_default = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100242 char_u *whitep = *argp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200243
244 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000245 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000247 ga_init2(argtypes, sizeof(char_u *), 3);
h-eastb895b0f2023-09-24 15:46:31 +0200248 if (arg_objm != NULL)
249 ga_init2(arg_objm, sizeof(int8_T), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200250 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000251 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200252
253 if (varargs != NULL)
254 *varargs = FALSE;
255
256 /*
257 * Isolate the arguments: "arg1, arg2, ...)"
258 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100259 arg = skipwhite(*argp);
260 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200261 while (*p != endchar)
262 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200263 while (eap != NULL && eap->getline != NULL
264 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200265 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200266 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000267 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000268 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000269
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200270 if (theline == NULL)
271 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200272 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200273 p = skipwhite(theline);
274 }
275
276 if (mustend && *p != endchar)
277 {
278 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000279 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100280 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200281 }
282 if (*p == endchar)
283 break;
284
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200285 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
286 {
287 if (varargs != NULL)
288 *varargs = TRUE;
289 p += 3;
290 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100291
292 if (argtypes != NULL)
293 {
294 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200295 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100296 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100297 if (!skip)
298 emsg(_(e_missing_name_after_dots));
299 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100300 }
301
302 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100303 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200304 arg_objm, evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305 if (p == arg)
306 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100307 if (*skipwhite(p) == '=')
308 {
309 emsg(_(e_cannot_use_default_for_variable_arguments));
310 break;
311 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100312 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200313 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000314 else if (in_class && STRNCMP(p, "this.", 5) == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000315 {
316 // this.memberName
317 p += 5;
318 arg = p;
319 while (ASCII_ISALNUM(*p) || *p == '_')
320 ++p;
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000321 char_u *argend = p;
322
323 if (*skipwhite(p) == '=')
324 {
325 char_u *defval = skipwhite(skipwhite(p) + 1);
326 if (STRNCMP(defval, "v:none", 6) != 0)
327 {
328 semsg(_(e_constructor_default_value_must_be_vnone_str), p);
329 goto err_ret;
330 }
331 any_default = TRUE;
332 p = defval + 6;
333
334 if (ga_grow(default_args, 1) == FAIL)
335 goto err_ret;
336
337 char_u *expr = vim_strsave((char_u *)"v:none");
338 if (expr == NULL)
339 goto err_ret;
340 ((char_u **)(default_args->ga_data))
341 [default_args->ga_len] = expr;
342 default_args->ga_len++;
343 }
344 else if (any_default)
345 {
346 emsg(_(e_non_default_argument_follows_default_argument));
347 goto err_ret;
348 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000349
350 // TODO: check the argument is indeed a member
351 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
352 return FAIL;
353 if (newargs != NULL)
354 {
355 ((char_u **)(newargs->ga_data))[newargs->ga_len] =
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000356 vim_strnsave(arg, argend - arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000357 newargs->ga_len++;
358
h-eastb895b0f2023-09-24 15:46:31 +0200359 if (argtypes != NULL && ga_grow(argtypes, 1) == OK
360 && arg_objm != NULL && ga_grow(arg_objm, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000361 {
362 // TODO: use the actual type
363 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] =
364 vim_strsave((char_u *)"any");
h-eastb895b0f2023-09-24 15:46:31 +0200365 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000366
367 // Add a line to the function body for the assignment.
368 if (ga_grow(newlines, 1) == OK)
369 {
370 // "this.name = name"
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000371 int len = 5 + (argend - arg) + 3 + (argend - arg) + 1;
372 if (any_default)
373 len += 14 + 10;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000374 char_u *assignment = alloc(len);
375 if (assignment != NULL)
376 {
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000377 c = *argend;
378 *argend = NUL;
379 if (any_default)
380 vim_snprintf((char *)assignment, len,
381 "ifargisset %d this.%s = %s",
382 default_args->ga_len - 1, arg, arg);
383 else
384 vim_snprintf((char *)assignment, len,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000385 "this.%s = %s", arg, arg);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000386 *argend = c;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000387 ((char_u **)(newlines->ga_data))[
388 newlines->ga_len++] = assignment;
389 }
390 }
391 }
392 }
393 if (*p == ',')
394 ++p;
395 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200396 else
397 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200398 char_u *np;
399
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200400 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100401 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200402 arg_objm, evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100403 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200404 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200405
Bram Moolenaar015cf102021-06-26 21:52:02 +0200406 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200407 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200408 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200409 if (*np == '=' && np[1] != '=' && np[1] != '~'
410 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200411 {
412 typval_T rettv;
413
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100414 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200415 any_default = TRUE;
Bram Moolenaareb5adf12022-09-04 13:41:37 +0100416 p = skipwhite(np + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000417 char_u *expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200418 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200419 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200420 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200421 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200422 if (ga_grow(default_args, 1) == FAIL)
423 goto err_ret;
424
425 // trim trailing whitespace
426 while (p > expr && VIM_ISWHITE(p[-1]))
427 p--;
428 c = *p;
429 *p = NUL;
430 expr = vim_strsave(expr);
431 if (expr == NULL)
432 {
433 *p = c;
434 goto err_ret;
435 }
436 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200437 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200438 default_args->ga_len++;
439 *p = c;
440 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200441 }
442 else
443 mustend = TRUE;
444 }
445 else if (any_default)
446 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000447 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200448 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200449 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200450
451 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
452 {
453 // Be tolerant when skipping
454 if (!skip)
455 {
456 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
457 goto err_ret;
458 }
459 p = skipwhite(p);
460 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200461 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200462 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200463 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200464 // Don't give this error when skipping, it makes the "->" not
465 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100466 // Allow missing space after comma in legacy functions.
467 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200468 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
469 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100470 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200471 goto err_ret;
472 }
473 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200474 else
475 mustend = TRUE;
476 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200477 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200478 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200479 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200480
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200481 if (*p != endchar)
482 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100483 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200484
485 *argp = p;
486 return OK;
487
488err_ret:
489 if (newargs != NULL)
490 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200491 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200492 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200493 return FAIL;
494}
495
496/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100497 * Parse the argument types, filling "fp->uf_arg_types".
498 * Return OK or FAIL.
499 */
500 static int
h-eastb895b0f2023-09-24 15:46:31 +0200501parse_argument_types(
502 ufunc_T *fp,
503 garray_T *argtypes,
504 int varargs,
505 garray_T *arg_objm,
506 ocmember_T *obj_members,
507 int obj_member_count)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100508{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200509 int len = 0;
510
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100511 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
512 if (argtypes->ga_len > 0)
513 {
514 // When "varargs" is set the last name/type goes into uf_va_name
515 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200516 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100517
518 if (len > 0)
519 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
520 if (fp->uf_arg_types != NULL)
521 {
522 int i;
523 type_T *type;
524
525 for (i = 0; i < len; ++ i)
526 {
527 char_u *p = ((char_u **)argtypes->ga_data)[i];
528
529 if (p == NULL)
530 // will get the type from the default value
531 type = &t_unknown;
532 else
h-eastb895b0f2023-09-24 15:46:31 +0200533 {
534 if (arg_objm != NULL && ((int8_T *)arg_objm->ga_data)[i])
535 {
536 char_u *aname = ((char_u **)fp->uf_args.ga_data)[i];
537
538 type = &t_any;
539 for (int om = 0; om < obj_member_count; ++om)
540 {
541 if (STRCMP(aname, obj_members[om].ocm_name) == 0)
542 {
543 type = obj_members[om].ocm_type;
544 break;
545 }
546 }
547 }
548 else
549 type = parse_type(&p, &fp->uf_type_list, TRUE);
550 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100551 if (type == NULL)
552 return FAIL;
553 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000554 if (i < fp->uf_args.ga_len
555 && (type->tt_type == VAR_FUNC
556 || type->tt_type == VAR_PARTIAL)
557 && var_wrong_func_name(
558 ((char_u **)fp->uf_args.ga_data)[i], TRUE))
559 return FAIL;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100560 }
561 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100562 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200563
564 if (varargs)
565 {
566 char_u *p;
567
568 // Move the last argument "...name: type" to uf_va_name and
569 // uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200570 --fp->uf_args.ga_len;
Bram Moolenaarf0571712023-01-04 13:16:20 +0000571 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len];
572 ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len] = NULL;
Bram Moolenaar2a389082021-04-09 20:24:31 +0200573 p = ((char_u **)argtypes->ga_data)[len];
574 if (p == NULL)
575 // TODO: get type from default value
576 fp->uf_va_type = &t_list_any;
577 else
578 {
579 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
580 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
581 {
582 semsg(_(e_variable_arguments_type_must_be_list_str),
583 ((char_u **)argtypes->ga_data)[len]);
584 return FAIL;
585 }
586 }
587 if (fp->uf_va_type == NULL)
588 return FAIL;
589 }
590
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100591 return OK;
592}
593
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100594 static int
595parse_return_type(ufunc_T *fp, char_u *ret_type)
596{
597 if (ret_type == NULL)
598 fp->uf_ret_type = &t_void;
599 else
600 {
601 char_u *p = ret_type;
602
603 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
604 if (fp->uf_ret_type == NULL)
605 {
606 fp->uf_ret_type = &t_void;
607 return FAIL;
608 }
609 }
610 return OK;
611}
612
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100613/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200614 * Register function "fp" as using "current_funccal" as its scope.
615 */
616 static int
617register_closure(ufunc_T *fp)
618{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200619 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100620 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200621 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200622 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200623 fp->uf_scoped = current_funccal;
624 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200625
Bram Moolenaarca16c602022-09-06 18:57:08 +0100626 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200627 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100628 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
629 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200630 return OK;
631}
632
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100633 static void
634set_ufunc_name(ufunc_T *fp, char_u *name)
635{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100636 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
637 // actually extends beyond the struct.
638 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100639
640 if (name[0] == K_SPECIAL)
641 {
642 fp->uf_name_exp = alloc(STRLEN(name) + 3);
643 if (fp->uf_name_exp != NULL)
644 {
645 STRCPY(fp->uf_name_exp, "<SNR>");
646 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
647 }
648 }
649}
650
Bram Moolenaar58016442016-07-31 18:30:22 +0200651/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100652 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
653 * return "buf" filled with a readable function name.
654 * Otherwise just return "name", thus the return value can always be used.
655 * "name" and "buf" may be equal.
656 */
657 char_u *
658make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
659{
660 size_t len;
661
662 if (name[0] != K_SPECIAL)
663 return name;
664 len = STRLEN(name);
665 if (len + 3 > bufsize)
666 return name;
667
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100668 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100669 mch_memmove(buf, "<SNR>", 5);
670 return buf;
671}
672
673/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200674 * Get a name for a lambda. Returned in static memory.
675 */
676 char_u *
677get_lambda_name(void)
678{
679 static char_u name[30];
680 static int lambda_no = 0;
681
682 sprintf((char*)name, "<lambda>%d", ++lambda_no);
683 return name;
684}
685
Bram Moolenaare01e5212023-01-08 20:31:18 +0000686/*
687 * Allocate a "ufunc_T" for a function called "name".
688 * Makes sure the size is right.
689 */
690 static ufunc_T *
691alloc_ufunc(char_u *name)
692{
693 // When the name is short we need to make sure we allocate enough bytes for
694 // the whole struct, including any padding.
695 size_t len = offsetof(ufunc_T, uf_name) + STRLEN(name) + 1;
696 return alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
697}
698
Bram Moolenaar801ab062020-06-25 19:27:56 +0200699#if defined(FEAT_LUA) || defined(PROTO)
700/*
701 * Registers a native C callback which can be called from Vim script.
702 * Returns the name of the Vim script function.
703 */
704 char_u *
705register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
706{
707 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200708 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200709
Bram Moolenaare01e5212023-01-08 20:31:18 +0000710 fp = alloc_ufunc(name);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200711 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200712 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200713
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200714 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200715 fp->uf_refcount = 1;
716 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000717 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200718 fp->uf_calls = 0;
719 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200720 fp->uf_cb = cb;
721 fp->uf_cb_free = cb_free;
722 fp->uf_cb_state = state;
723
724 set_ufunc_name(fp, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000725 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200726
727 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200728}
729#endif
730
Bram Moolenaar04b12692020-05-04 23:24:44 +0200731/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100732 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100733 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100734 * If "white_error" is not NULL check for correct use of white space and set
735 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100736 * Return NULL if no valid arrow found.
737 */
738 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100739skip_arrow(
740 char_u *start,
741 int equal_arrow,
742 char_u **ret_type,
743 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100744{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100745 char_u *s = start;
746 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100747
748 if (equal_arrow)
749 {
750 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100751 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100752 if (white_error != NULL && !VIM_ISWHITE(s[1]))
753 {
754 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100755 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100756 return NULL;
757 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100758 s = skipwhite(s + 1);
759 *ret_type = s;
760 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100761 if (s == *ret_type)
762 {
763 emsg(_(e_missing_return_type));
764 return NULL;
765 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100766 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100767 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100768 s = skipwhite(s);
769 if (*s != '=')
770 return NULL;
771 ++s;
772 }
773 if (*s != '>')
774 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100775 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
776 || !IS_WHITE_OR_NUL(s[1])))
777 {
778 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100779 semsg(_(e_white_space_required_before_and_after_str_at_str),
780 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100781 return NULL;
782 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100783 return skipwhite(s + 1);
784}
785
786/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100787 * Check if "*cmd" points to a function command and if so advance "*cmd" and
788 * return TRUE.
789 * Otherwise return FALSE;
790 * Do not consider "function(" to be a command.
791 */
792 static int
793is_function_cmd(char_u **cmd)
794{
795 char_u *p = *cmd;
796
797 if (checkforcmd(&p, "function", 2))
798 {
799 if (*p == '(')
800 return FALSE;
801 *cmd = p;
802 return TRUE;
803 }
804 return FALSE;
805}
806
807/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200808 * Called when defining a function: The context may be needed for script
809 * variables declared in a block that is visible now but not when the function
810 * is compiled or called later.
811 */
812 static void
813function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
814{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000815 if (cstack == NULL || cstack->cs_idx < 0)
816 return;
817
818 int count = cstack->cs_idx + 1;
819 int i;
820
821 fp->uf_block_ids = ALLOC_MULT(int, count);
822 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200823 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000824 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
825 sizeof(int) * count);
826 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200827 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000828
829 // Set flag in each block to indicate a function was defined. This
830 // is used to keep the variable when leaving the block, see
831 // hide_script_var().
832 for (i = 0; i <= cstack->cs_idx; ++i)
833 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200834}
835
836/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100837 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200838 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100839 * "newlines" must already have been initialized.
840 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
841 */
842 static int
843get_function_body(
844 exarg_T *eap,
845 garray_T *newlines,
846 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000847 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100848{
849 linenr_T sourcing_lnum_top = SOURCING_LNUM;
850 linenr_T sourcing_lnum_off;
851 int saved_wait_return = need_wait_return;
852 char_u *line_arg = line_arg_in;
853 int vim9_function = eap->cmdidx == CMD_def
854 || eap->cmdidx == CMD_block;
855#define MAX_FUNC_NESTING 50
856 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200857 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100858 int nesting = 0;
859 getline_opt_T getline_options;
860 int indent = 2;
861 char_u *skip_until = NULL;
862 int ret = FAIL;
863 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200864 int heredoc_concat_len = 0;
865 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100866 char_u *heredoc_trimmed = NULL;
867
Bram Moolenaar20677332021-06-06 17:02:53 +0200868 ga_init2(&heredoc_ga, 1, 500);
869
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100870 // Detect having skipped over comment lines to find the return
871 // type. Add NULL lines to keep the line count correct.
872 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
873 if (SOURCING_LNUM < sourcing_lnum_off)
874 {
875 sourcing_lnum_off -= SOURCING_LNUM;
876 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
877 goto theend;
878 while (sourcing_lnum_off-- > 0)
879 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
880 }
881
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200882 nesting_def[0] = vim9_function;
883 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100884 getline_options = vim9_function
885 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
886 for (;;)
887 {
888 char_u *theline;
889 char_u *p;
890 char_u *arg;
891
892 if (KeyTyped)
893 {
894 msg_scroll = TRUE;
895 saved_wait_return = FALSE;
896 }
897 need_wait_return = FALSE;
898
899 if (line_arg != NULL)
900 {
901 // Use eap->arg, split up in parts by line breaks.
902 theline = line_arg;
903 p = vim_strchr(theline, '\n');
904 if (p == NULL)
905 line_arg += STRLEN(line_arg);
906 else
907 {
908 *p = NUL;
909 line_arg = p + 1;
910 }
911 }
912 else
913 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000914 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100915 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100916 }
917 if (KeyTyped)
918 lines_left = Rows - 1;
919 if (theline == NULL)
920 {
921 // Use the start of the function for the line number.
922 SOURCING_LNUM = sourcing_lnum_top;
923 if (skip_until != NULL)
924 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200925 else if (nesting_inline[nesting])
926 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100927 else if (eap->cmdidx == CMD_def)
928 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100929 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000930 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100931 goto theend;
932 }
933
934 // Detect line continuation: SOURCING_LNUM increased more than one.
935 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
936 if (SOURCING_LNUM < sourcing_lnum_off)
937 sourcing_lnum_off -= SOURCING_LNUM;
938 else
939 sourcing_lnum_off = 0;
940
941 if (skip_until != NULL)
942 {
943 // Don't check for ":endfunc"/":enddef" between
944 // * ":append" and "."
945 // * ":python <<EOF" and "EOF"
946 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
947 if (heredoc_trimmed == NULL
948 || (is_heredoc && skipwhite(theline) == theline)
949 || STRNCMP(theline, heredoc_trimmed,
950 STRLEN(heredoc_trimmed)) == 0)
951 {
952 if (heredoc_trimmed == NULL)
953 p = theline;
954 else if (is_heredoc)
955 p = skipwhite(theline) == theline
956 ? theline : theline + STRLEN(heredoc_trimmed);
957 else
958 p = theline + STRLEN(heredoc_trimmed);
959 if (STRCMP(p, skip_until) == 0)
960 {
961 VIM_CLEAR(skip_until);
962 VIM_CLEAR(heredoc_trimmed);
963 getline_options = vim9_function
964 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
965 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200966
967 if (heredoc_concat_len > 0)
968 {
969 // Replace the starting line with all the concatenated
970 // lines.
971 ga_concat(&heredoc_ga, theline);
972 vim_free(((char_u **)(newlines->ga_data))[
973 heredoc_concat_len - 1]);
974 ((char_u **)(newlines->ga_data))[
975 heredoc_concat_len - 1] = heredoc_ga.ga_data;
976 ga_init(&heredoc_ga);
977 heredoc_concat_len = 0;
978 theline += STRLEN(theline); // skip the "EOF"
979 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100980 }
981 }
982 }
983 else
984 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200985 int c;
986 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +0000987 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100988
989 // skip ':' and blanks
990 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
991 ;
992
993 // Check for "endfunction", "enddef" or "}".
994 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +0000995 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200996 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100997 ? *p == '}'
998 : (checkforcmd(&p, nesting_def[nesting]
999 ? "enddef" : "endfunction", 4)
1000 && *p != ':'))
1001 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00001002 if (!nesting_inline[nesting] && nesting_def[nesting]
1003 && p < cmd + 6)
1004 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001005 if (nesting-- == 0)
1006 {
1007 char_u *nextcmd = NULL;
1008
1009 if (*p == '|' || *p == '}')
1010 nextcmd = p + 1;
1011 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
1012 nextcmd = line_arg;
1013 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001014 && (vim9_function || p_verbose > 0))
1015 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +02001016 SOURCING_LNUM = sourcing_lnum_top
1017 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001018 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +00001019 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001020 else
1021 give_warning2((char_u *)
1022 _("W22: Text found after :endfunction: %s"),
1023 p, TRUE);
1024 }
1025 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001026 {
1027 // Another command follows. If the line came from "eap"
1028 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001029 // change "eap->cmdlinep" to point to the last fetched
1030 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001031 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001032 if (lines_to_free->ga_len > 0
1033 && *eap->cmdlinep !=
1034 ((char_u **)lines_to_free->ga_data)
1035 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001036 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001037 // *cmdlinep will be freed later, thus remove the
1038 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001039 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001040 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
1041 [lines_to_free->ga_len - 1];
1042 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001043 }
1044 }
1045 break;
1046 }
1047 }
1048
1049 // Check for mismatched "endfunc" or "enddef".
1050 // We don't check for "def" inside "func" thus we also can't check
1051 // for "enddef".
1052 // We continue to find the end of the function, although we might
1053 // not find it.
1054 else if (nesting_def[nesting])
1055 {
1056 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1057 emsg(_(e_mismatched_endfunction));
1058 }
1059 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1060 emsg(_(e_mismatched_enddef));
1061
1062 // Increase indent inside "if", "while", "for" and "try", decrease
1063 // at "end".
1064 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1065 indent -= 2;
1066 else if (STRNCMP(p, "if", 2) == 0
1067 || STRNCMP(p, "wh", 2) == 0
1068 || STRNCMP(p, "for", 3) == 0
1069 || STRNCMP(p, "try", 3) == 0)
1070 indent += 2;
1071
1072 // Check for defining a function inside this function.
1073 // Only recognize "def" inside "def", not inside "function",
1074 // For backwards compatibility, see Test_function_python().
1075 c = *p;
1076 if (is_function_cmd(&p)
1077 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1078 {
1079 if (*p == '!')
1080 p = skipwhite(p + 1);
1081 p += eval_fname_script(p);
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001082 vim_free(trans_function_name(&p, NULL, TRUE, 0));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001083 if (*skipwhite(p) == '(')
1084 {
1085 if (nesting == MAX_FUNC_NESTING - 1)
1086 emsg(_(e_function_nesting_too_deep));
1087 else
1088 {
1089 ++nesting;
1090 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001091 nesting_inline[nesting] = FALSE;
1092 indent += 2;
1093 }
1094 }
1095 }
1096
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001097 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001098 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001099 // Not a comment line: check for nested inline function.
1100 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001101 while (end > p && VIM_ISWHITE(*end))
1102 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001103 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001104 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001105 int is_block;
1106
1107 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001108 --end;
1109 while (end > p && VIM_ISWHITE(*end))
1110 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001111 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1112 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001113 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001114 char_u *s = p;
1115
1116 // check for line starting with "au" for :autocmd or
1117 // "com" for :command, these can use a {} block
1118 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1119 || checkforcmd_noparen(&s, "command", 3);
1120 }
1121
1122 if (is_block)
1123 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001124 if (nesting == MAX_FUNC_NESTING - 1)
1125 emsg(_(e_function_nesting_too_deep));
1126 else
1127 {
1128 ++nesting;
1129 nesting_def[nesting] = TRUE;
1130 nesting_inline[nesting] = TRUE;
1131 indent += 2;
1132 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001133 }
1134 }
1135 }
1136
1137 // Check for ":append", ":change", ":insert". Not for :def.
1138 p = skip_range(p, FALSE, NULL);
1139 if (!vim9_function
1140 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1141 || (p[0] == 'c'
1142 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1143 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1144 && (STRNCMP(&p[3], "nge", 3) != 0
1145 || !ASCII_ISALPHA(p[6])))))))
1146 || (p[0] == 'i'
1147 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1148 && (!ASCII_ISALPHA(p[2])
1149 || (p[2] == 's'
1150 && (!ASCII_ISALPHA(p[3])
1151 || p[3] == 'e'))))))))
1152 skip_until = vim_strsave((char_u *)".");
1153
1154 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1155 arg = skipwhite(skiptowhite(p));
1156 if (arg[0] == '<' && arg[1] =='<'
1157 && ((p[0] == 'p' && p[1] == 'y'
1158 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1159 || ((p[2] == '3' || p[2] == 'x')
1160 && !ASCII_ISALPHA(p[3]))))
1161 || (p[0] == 'p' && p[1] == 'e'
1162 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1163 || (p[0] == 't' && p[1] == 'c'
1164 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1165 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1166 && !ASCII_ISALPHA(p[3]))
1167 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1168 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1169 || (p[0] == 'm' && p[1] == 'z'
1170 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1171 ))
1172 {
1173 // ":python <<" continues until a dot, like ":append"
1174 p = skipwhite(arg + 2);
1175 if (STRNCMP(p, "trim", 4) == 0)
1176 {
1177 // Ignore leading white space.
1178 p = skipwhite(p + 4);
1179 heredoc_trimmed = vim_strnsave(theline,
1180 skipwhite(theline) - theline);
1181 }
1182 if (*p == NUL)
1183 skip_until = vim_strsave((char_u *)".");
1184 else
1185 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1186 getline_options = GETLINE_NONE;
1187 is_heredoc = TRUE;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001188 if (vim9_function && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001189 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001190 }
1191
Bram Moolenaard881d152022-05-13 13:50:36 +01001192 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001193 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001194 // Check for ":cmd v =<< [trim] EOF"
1195 // and ":cmd [a, b] =<< [trim] EOF"
1196 // and "lines =<< [trim] EOF" for Vim9
1197 // Where "cmd" can be "let", "var", "final" or "const".
zeertzjqa93d9cd2023-05-02 16:25:47 +01001198 arg = p;
1199 if (checkforcmd(&arg, "let", 2)
1200 || checkforcmd(&arg, "var", 3)
1201 || checkforcmd(&arg, "final", 5)
1202 || checkforcmd(&arg, "const", 5)
1203 || vim9_function)
Bram Moolenaard881d152022-05-13 13:50:36 +01001204 {
zeertzjqa93d9cd2023-05-02 16:25:47 +01001205 while (vim_strchr((char_u *)"$@&", *arg) != NULL)
1206 ++arg;
1207 arg = skipwhite(find_name_end(arg, NULL, NULL,
1208 FNE_INCL_BR | FNE_ALLOW_CURLY));
1209 if (vim9_function && *arg == ':')
1210 arg = skipwhite(skip_type(skipwhite(arg + 1), FALSE));
1211 if (arg[0] == '=' && arg[1] == '<' && arg[2] =='<')
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001212 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001213 p = skipwhite(arg + 3);
1214 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001215 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001216 if (STRNCMP(p, "trim", 4) == 0)
1217 {
1218 // Ignore leading white space.
1219 p = skipwhite(p + 4);
1220 heredoc_trimmed = vim_strnsave(theline,
1221 skipwhite(theline) - theline);
1222 continue;
1223 }
1224 if (STRNCMP(p, "eval", 4) == 0)
1225 {
1226 // Ignore leading white space.
1227 p = skipwhite(p + 4);
1228 continue;
1229 }
1230 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001231 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001232 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1233 getline_options = GETLINE_NONE;
1234 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001235 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001236 }
1237 }
1238 }
1239
1240 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001241 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001242 goto theend;
1243
Bram Moolenaar20677332021-06-06 17:02:53 +02001244 if (heredoc_concat_len > 0)
1245 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001246 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001247 // to be used for the instruction later.
1248 ga_concat(&heredoc_ga, theline);
1249 ga_concat(&heredoc_ga, (char_u *)"\n");
1250 p = vim_strsave((char_u *)"");
1251 }
1252 else
1253 {
1254 // Copy the line to newly allocated memory. get_one_sourceline()
1255 // allocates 250 bytes per line, this saves 80% on average. The
1256 // cost is an extra alloc/free.
1257 p = vim_strsave(theline);
1258 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001259 if (p == NULL)
1260 goto theend;
1261 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1262
1263 // Add NULL lines for continuation lines, so that the line count is
1264 // equal to the index in the growarray.
1265 while (sourcing_lnum_off-- > 0)
1266 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1267
1268 // Check for end of eap->arg.
1269 if (line_arg != NULL && *line_arg == NUL)
1270 line_arg = NULL;
1271 }
1272
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001273 // Return OK when no error was detected.
1274 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001275 ret = OK;
1276
1277theend:
1278 vim_free(skip_until);
1279 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001280 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001281 need_wait_return |= saved_wait_return;
1282 return ret;
1283}
1284
1285/*
1286 * Handle the body of a lambda. *arg points to the "{", process statements
1287 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001288 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001289 * When successful "rettv" is set to a funcref.
1290 */
1291 static int
1292lambda_function_body(
1293 char_u **arg,
1294 typval_T *rettv,
1295 evalarg_T *evalarg,
1296 garray_T *newargs,
1297 garray_T *argtypes,
1298 int varargs,
1299 garray_T *default_args,
1300 char_u *ret_type)
1301{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001302 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001303 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001304 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001305 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001306 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001307 exarg_T eap;
1308 garray_T newlines;
1309 char_u *cmdline = NULL;
1310 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001311 partial_T *pt;
1312 char_u *name;
1313 int lnum_save = -1;
1314 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1315
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001316 *arg = skipwhite(*arg + 1);
1317 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001318 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001319 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001320 return FAIL;
1321 }
1322
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001323 CLEAR_FIELD(eap);
1324 eap.cmdidx = CMD_block;
1325 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001326 eap.cmdlinep = &cmdline;
1327 eap.skip = !evaluate;
1328 if (evalarg->eval_cctx != NULL)
1329 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1330 else
1331 {
1332 eap.getline = evalarg->eval_getline;
1333 eap.cookie = evalarg->eval_cookie;
1334 }
1335
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001336 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001337 if (get_function_body(&eap, &newlines, NULL,
1338 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001339 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001340
1341 // When inside a lambda must add the function lines to evalarg.eval_ga.
1342 evalarg->eval_break_count += newlines.ga_len;
1343 if (gap->ga_itemsize > 0)
1344 {
1345 int idx;
1346 char_u *last;
1347 size_t plen;
1348 char_u *pnl;
1349
1350 for (idx = 0; idx < newlines.ga_len; ++idx)
1351 {
1352 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1353
Bram Moolenaarecb66452021-05-18 15:09:18 +02001354 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001355 goto erret;
1356
1357 // Going to concatenate the lines after parsing. For an empty or
1358 // comment line use an empty string.
1359 // Insert NL characters at the start of each line, the string will
1360 // be split again later in .get_lambda_tv().
1361 if (*p == NUL || vim9_comment_start(p))
1362 p = (char_u *)"";
1363 plen = STRLEN(p);
1364 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1365 if (pnl != NULL)
1366 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001367 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1368 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001369 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001370 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001371 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001372 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001373 // more is following after the "}", which was skipped
1374 last = cmdline;
1375 else
1376 // nothing is following the "}"
1377 last = (char_u *)"}";
1378 plen = STRLEN(last);
1379 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1380 if (pnl != NULL)
1381 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001382 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1383 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001384 }
1385
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001386 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001387 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001388 garray_T *tfgap = &evalarg->eval_tofree_ga;
1389
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001390 // Something comes after the "}".
1391 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001392
1393 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001394 if (ga_grow(tfgap, 1) == OK)
1395 {
1396 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1397 evalarg->eval_using_cmdline = TRUE;
1398 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001399 }
1400 else
1401 *arg = (char_u *)"";
1402
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001403 if (!evaluate)
1404 {
1405 ret = OK;
1406 goto erret;
1407 }
1408
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001409 name = get_lambda_name();
Bram Moolenaare01e5212023-01-08 20:31:18 +00001410 ufunc = alloc_ufunc(name);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001411 if (ufunc == NULL)
1412 goto erret;
1413 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001414 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001415 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001416 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001417 ufunc->uf_refcount = 1;
1418 ufunc->uf_args = *newargs;
1419 newargs->ga_data = NULL;
1420 ufunc->uf_def_args = *default_args;
1421 default_args->ga_data = NULL;
1422 ufunc->uf_func_type = &t_func_any;
1423
1424 // error messages are for the first function line
1425 lnum_save = SOURCING_LNUM;
1426 SOURCING_LNUM = sourcing_lnum_top;
1427
1428 // parse argument types
h-eastb895b0f2023-09-24 15:46:31 +02001429 if (parse_argument_types(ufunc, argtypes, varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001430 {
1431 SOURCING_LNUM = lnum_save;
1432 goto erret;
1433 }
1434
1435 // parse the return type, if any
1436 if (parse_return_type(ufunc, ret_type) == FAIL)
1437 goto erret;
1438
1439 pt = ALLOC_CLEAR_ONE(partial_T);
1440 if (pt == NULL)
1441 goto erret;
1442 pt->pt_func = ufunc;
1443 pt->pt_refcount = 1;
1444
1445 ufunc->uf_lines = newlines;
1446 newlines.ga_data = NULL;
1447 if (sandbox)
1448 ufunc->uf_flags |= FC_SANDBOX;
1449 if (!ASCII_ISUPPER(*ufunc->uf_name))
1450 ufunc->uf_flags |= FC_VIM9;
1451 ufunc->uf_script_ctx = current_sctx;
1452 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1453 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1454 set_function_type(ufunc);
1455
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001456 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1457
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001458 rettv->vval.v_partial = pt;
1459 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001460 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001461 ret = OK;
1462
1463erret:
1464 if (lnum_save >= 0)
1465 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001466 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001467 if (newargs != NULL)
1468 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001469 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001470 if (ufunc != NULL)
1471 {
1472 func_clear(ufunc, TRUE);
1473 func_free(ufunc, TRUE);
1474 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001475 return ret;
1476}
1477
1478/*
1479 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001480 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001481 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001482 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1483 */
1484 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001485get_lambda_tv(
1486 char_u **arg,
1487 typval_T *rettv,
1488 int types_optional,
1489 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001490{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001491 int evaluate = evalarg != NULL
1492 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001493 garray_T newargs;
1494 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001495 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001496 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001497 garray_T default_args;
h-eastb895b0f2023-09-24 15:46:31 +02001498 garray_T arg_objm;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001499 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001500 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001501 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001502 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001503 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001504 char_u *s;
1505 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001506 int *old_eval_lavars = eval_lavars_used;
1507 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001508 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001509 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001510 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001511 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001512 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001513 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001514
Bram Moolenaar4525a572022-02-13 11:57:33 +00001515 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001516 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001517
1518 ga_init(&newargs);
1519 ga_init(&newlines);
1520
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001521 // First, check if this is really a lambda expression. "->" or "=>" must
1522 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001523 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001524 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001525 types_optional ? &argtypes : NULL, types_optional,
1526 types_optional ? &arg_objm : NULL, evalarg,
1527 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001528 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001529 {
1530 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001531 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001532 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001533 ga_clear(&arg_objm);
1534 }
Bram Moolenaar0346b792021-01-31 22:18:29 +01001535 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001536 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001537
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001538 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001539 if (evaluate)
1540 pnewargs = &newargs;
1541 else
1542 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001543 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001544 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
h-eastb895b0f2023-09-24 15:46:31 +02001545 types_optional ? &argtypes : NULL, types_optional,
1546 types_optional ? &arg_objm : NULL, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001547 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001548 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001549 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001550 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001551 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001552 {
1553 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001554 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001555 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001556 ga_clear(&arg_objm);
1557 }
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001558 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001559 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001560 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001561 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001562
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001563 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1564 if (ret_type != NULL)
1565 {
1566 ret_type = vim_strsave(ret_type);
1567 tofree2 = ret_type;
1568 }
1569
Bram Moolenaare38eab22019-12-05 21:50:01 +01001570 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001571 if (evaluate)
1572 eval_lavars_used = &eval_lavars;
1573
Bram Moolenaar65c44152020-12-24 15:14:01 +01001574 *arg = skipwhite_and_linebreak(*arg, evalarg);
1575
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001576 // Recognize "{" as the start of a function body.
1577 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001578 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001579 if (evalarg == NULL)
1580 // cannot happen?
1581 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001582 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001583 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1584 types_optional ? &argtypes : NULL, varargs,
1585 &default_args, ret_type) == FAIL)
1586 goto errret;
1587 goto theend;
1588 }
1589 if (default_args.ga_len > 0)
1590 {
1591 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001592 goto errret;
1593 }
1594
Bram Moolenaare38eab22019-12-05 21:50:01 +01001595 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001596 start = *arg;
1597 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001598 if (ret == FAIL)
1599 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001600
Bram Moolenaar65c44152020-12-24 15:14:01 +01001601 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001602 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001603 *arg = skipwhite_and_linebreak(*arg, evalarg);
1604 if (**arg != '}')
1605 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001606 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001607 goto errret;
1608 }
1609 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001610 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001611
1612 if (evaluate)
1613 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001614 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001615 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001616 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001617 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001618 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001619
Bram Moolenaare01e5212023-01-08 20:31:18 +00001620 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001621 if (fp == NULL)
1622 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001623 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001624 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001625 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001626 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001627
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001628 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001629 if (ga_grow(&newlines, 1) == FAIL)
1630 goto errret;
1631
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001632 // If there are line breaks, we need to split up the string.
1633 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001634 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001635 line_end = end;
1636
1637 // Add "return " before the expression (or the first line).
1638 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001639 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001640 if (p == NULL)
1641 goto errret;
1642 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1643 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001644 vim_strncpy(p + 7, start, line_end - start);
1645
1646 if (line_end != end)
1647 {
1648 // Add more lines, split by line breaks. Thus is used when a
1649 // lambda with { cmds } is encountered.
1650 while (*line_end == '\n')
1651 {
1652 if (ga_grow(&newlines, 1) == FAIL)
1653 goto errret;
1654 start = line_end + 1;
1655 line_end = vim_strchr(start, '\n');
1656 if (line_end == NULL)
1657 line_end = end;
1658 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1659 vim_strnsave(start, line_end - start);
1660 }
1661 }
1662
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001663 if (strstr((char *)p + 7, "a:") == NULL)
1664 // No a: variables are used for sure.
1665 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001666
1667 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001668 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001669 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001670 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001671 if (types_optional)
1672 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001673 if (parse_argument_types(fp, &argtypes,
h-eastb895b0f2023-09-24 15:46:31 +02001674 vim9script && varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001675 goto errret;
1676 if (ret_type != NULL)
1677 {
1678 fp->uf_ret_type = parse_type(&ret_type,
1679 &fp->uf_type_list, TRUE);
1680 if (fp->uf_ret_type == NULL)
1681 goto errret;
1682 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001683 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001684 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001685 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001686
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001687 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001688 if (current_funccal != NULL && eval_lavars)
1689 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001690 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001691 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001692 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001693 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001694
1695#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001696 if (prof_def_func())
1697 func_do_profile(fp);
1698#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001699 if (sandbox)
1700 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001701 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001702 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001703 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001704 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001705 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001706 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001707 // Use the line number of the arguments.
1708 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001709
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001710 function_using_block_scopes(fp, evalarg->eval_cstack);
1711
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001712 pt->pt_func = fp;
1713 pt->pt_refcount = 1;
1714 rettv->vval.v_partial = pt;
1715 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001716
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001717 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001718 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001719
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001720theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001721 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001722 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001723 if (types_optional)
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001724 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001725 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001726 ga_clear(&arg_objm);
1727 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001728
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001729 return OK;
1730
1731errret:
1732 ga_clear_strings(&newargs);
1733 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001734 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001735 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001736 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001737 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001738 ga_clear(&arg_objm);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001739 if (fp != NULL)
1740 vim_free(fp->uf_arg_types);
1741 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001742 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001743 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001744 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001745 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001746 return FAIL;
1747}
1748
1749/*
1750 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1751 * name it contains, otherwise return "name".
1752 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1753 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001754 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1755 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001756 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001757 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001758 */
1759 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001760deref_func_name(
1761 char_u *name,
1762 int *lenp,
1763 partial_T **partialp,
1764 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001765 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001766 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001767 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001768{
1769 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001770 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001771 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001772 char_u *s = NULL;
1773 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001774 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001775
1776 if (partialp != NULL)
1777 *partialp = NULL;
1778
1779 cc = name[*lenp];
1780 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001781
Bram Moolenaar71f21932022-01-07 18:20:55 +00001782 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001783 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001784 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001785 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001786 tv = &v->di_tv;
1787 }
1788 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1789 {
1790 imported_T *import;
1791 char_u *p = name;
1792 int len = *lenp;
1793
1794 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001795 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001796 p = name + 2;
1797 len -= 2;
1798 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001799 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001800
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001801 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001802 if (import != NULL)
1803 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001804 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001805 if (new_function)
1806 semsg(_(e_redefining_imported_item_str), name);
1807 else
1808 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001809 name[len] = cc;
1810 *lenp = 0;
1811 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001812 }
1813 }
1814
1815 if (tv != NULL)
1816 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001817 if (found_var != NULL)
1818 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001819 if (tv->v_type == VAR_FUNC)
1820 {
1821 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001822 {
1823 *lenp = 0;
1824 return (char_u *)""; // just in case
1825 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001826 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001827 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001828 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001829
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001830 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001831 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001832 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001833
1834 if (pt == NULL)
1835 {
1836 *lenp = 0;
1837 return (char_u *)""; // just in case
1838 }
1839 if (partialp != NULL)
1840 *partialp = pt;
1841 s = partial_name(pt);
1842 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001843 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001844
1845 if (s != NULL)
1846 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001847 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001848 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001849 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001850
1851 if (sv != NULL)
1852 *type = sv->sv_type;
1853 }
1854 return s;
1855 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001856 }
1857
1858 return name;
1859}
1860
1861/*
1862 * Give an error message with a function name. Handle <SNR> things.
1863 * "ermsg" is to be passed without translation, use N_() instead of _().
1864 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001865 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001866emsg_funcname(char *ermsg, char_u *name)
1867{
Bram Moolenaar54598062022-01-13 12:05:09 +00001868 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001869
Bram Moolenaar54598062022-01-13 12:05:09 +00001870 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001871 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001872 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001873 if (p != name)
1874 vim_free(p);
1875}
1876
1877/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001878 * Get function arguments at "*arg" and advance it.
1879 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001880 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001881 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001882 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001883get_func_arguments(
1884 char_u **arg,
1885 evalarg_T *evalarg,
1886 int partial_argc,
1887 typval_T *argvars,
1888 int *argcount)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001889{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001890 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001891 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001892 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001893 int evaluate = evalarg == NULL
1894 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001895
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001896 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001897 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001898 // skip the '(' or ',' and possibly line breaks
1899 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1900
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001901 if (*argp == ')' || *argp == ',' || *argp == NUL)
1902 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001903 if (eval1(&argp, &argvars[*argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001904 {
1905 ret = FAIL;
1906 break;
1907 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001908 ++*argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001909 // The comma should come right after the argument, but this wasn't
1910 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001911 if (vim9script)
1912 {
1913 if (*argp != ',' && *skipwhite(argp) == ',')
1914 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001915 if (evaluate)
1916 semsg(_(e_no_white_space_allowed_before_str_str),
1917 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001918 ret = FAIL;
1919 break;
1920 }
1921 }
1922 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001923 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001924 if (*argp != ',')
1925 break;
Christian Brabandt00cb2472023-09-05 20:46:25 +02001926 if (vim9script && !IS_WHITE_NL_OR_NUL(argp[1]))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001927 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001928 if (evaluate)
1929 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001930 ret = FAIL;
1931 break;
1932 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001933 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001934
Bram Moolenaare6b53242020-07-01 17:28:33 +02001935 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001936 if (*argp == ')')
1937 ++argp;
1938 else
1939 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001940 *arg = argp;
1941 return ret;
1942}
1943
1944/*
1945 * Call a function and put the result in "rettv".
1946 * Return OK or FAIL.
1947 */
1948 int
1949get_func_tv(
1950 char_u *name, // name of the function
1951 int len, // length of "name" or -1 to use strlen()
1952 typval_T *rettv,
1953 char_u **arg, // argument, pointing to the '('
1954 evalarg_T *evalarg, // for line continuation
1955 funcexe_T *funcexe) // various values
1956{
1957 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001958 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001959 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1960 int argcount = 0; // number of arguments found
1961 int vim9script = in_vim9script();
1962 int evaluate = evalarg == NULL
1963 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
1964
1965 argp = *arg;
1966 ret = get_func_arguments(&argp, evalarg,
1967 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
1968 argvars, &argcount);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001969
1970 if (ret == OK)
1971 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001972 int i = 0;
1973 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001974
1975 if (get_vim_var_nr(VV_TESTING))
1976 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001977 // Prepare for calling test_garbagecollect_now(), need to know
1978 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001979 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001980 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001981 for (i = 0; i < argcount; ++i)
1982 if (ga_grow(&funcargs, 1) == OK)
1983 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1984 &argvars[i];
1985 }
1986
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001987 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00001988 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001989 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001990 // An error in a builtin function does not return FAIL, but we do
1991 // want to abort further processing if an error was given.
1992 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001993 clear_tv(rettv);
1994 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001995
1996 funcargs.ga_len -= i;
1997 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001998 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001999 {
2000 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002001 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002002 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00002003 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002004 }
2005
2006 while (--argcount >= 0)
2007 clear_tv(&argvars[argcount]);
2008
Bram Moolenaar4525a572022-02-13 11:57:33 +00002009 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02002010 *arg = argp;
2011 else
2012 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002013 return ret;
2014}
2015
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002016/*
2017 * Return TRUE if "p" starts with "<SID>" or "s:".
2018 * Only works if eval_fname_script() returned non-zero for "p"!
2019 */
2020 static int
2021eval_fname_sid(char_u *p)
2022{
2023 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
2024}
2025
2026/*
2027 * In a script change <SID>name() and s:name() to K_SNR 123_name().
2028 * Change <SNR>123_name() to K_SNR 123_name().
2029 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002030 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002031 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002032 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00002033fname_trans_sid(
2034 char_u *name,
2035 char_u *fname_buf,
2036 char_u **tofree,
2037 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002038{
2039 int llen;
2040 char_u *fname;
2041 int i;
2042
2043 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002044 if (llen == 0)
2045 return name; // no prefix
2046
2047 fname_buf[0] = K_SPECIAL;
2048 fname_buf[1] = KS_EXTRA;
2049 fname_buf[2] = (int)KE_SNR;
2050 i = 3;
2051 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002052 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002053 if (current_sctx.sc_sid <= 0)
2054 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002055 else
2056 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002057 sprintf((char *)fname_buf + 3, "%ld_",
2058 (long)current_sctx.sc_sid);
2059 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002060 }
2061 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002062 if (i + STRLEN(name + llen) < FLEN_FIXED)
2063 {
2064 STRCPY(fname_buf + i, name + llen);
2065 fname = fname_buf;
2066 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002067 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002068 {
2069 fname = alloc(i + STRLEN(name + llen) + 1);
2070 if (fname == NULL)
2071 *error = FCERR_OTHER;
2072 else
2073 {
2074 *tofree = fname;
2075 mch_memmove(fname, fname_buf, (size_t)i);
2076 STRCPY(fname + i, name + llen);
2077 }
2078 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002079 return fname;
2080}
2081
2082/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002083 * Concatenate the script ID and function name into "<SNR>99_name".
2084 * "buffer" must have size MAX_FUNC_NAME_LEN.
2085 */
2086 void
2087func_name_with_sid(char_u *name, int sid, char_u *buffer)
2088{
2089 // A script-local function is stored as "<SNR>99_name".
2090 buffer[0] = K_SPECIAL;
2091 buffer[1] = KS_EXTRA;
2092 buffer[2] = (int)KE_SNR;
2093 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2094 (long)sid, name);
2095}
2096
2097/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098 * Find a function "name" in script "sid".
2099 */
2100 static ufunc_T *
2101find_func_with_sid(char_u *name, int sid)
2102{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002103 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002104 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002105
Bram Moolenaarb8822442022-01-11 15:24:05 +00002106 if (!SCRIPT_ID_VALID(sid))
2107 return NULL; // not in a script
2108
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002109 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002110 hi = hash_find(&func_hashtab, buffer);
2111 if (!HASHITEM_EMPTY(hi))
2112 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002113 return NULL;
2114}
2115
2116/*
2117 * Find a function "name" in script "sid" prefixing the autoload prefix.
2118 */
2119 static ufunc_T *
2120find_func_with_prefix(char_u *name, int sid)
2121{
2122 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002123 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002124 scriptitem_T *si;
2125
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002126 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002127 return NULL; // already has the prefix
2128 if (!SCRIPT_ID_VALID(sid))
2129 return NULL; // not in a script
2130 si = SCRIPT_ITEM(sid);
2131 if (si->sn_autoload_prefix != NULL)
2132 {
2133 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2134 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002135 char_u *namep;
2136
2137 // skip a "<SNR>99_" prefix
2138 namep = untrans_function_name(name);
2139 if (namep == NULL)
2140 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002141
2142 // An exported function in an autoload script is stored as
2143 // "dir#path#name".
2144 if (len < sizeof(buffer))
2145 auto_name = buffer;
2146 else
2147 auto_name = alloc(len);
2148 if (auto_name != NULL)
2149 {
2150 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002151 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002152 hi = hash_find(&func_hashtab, auto_name);
2153 if (auto_name != buffer)
2154 vim_free(auto_name);
2155 if (!HASHITEM_EMPTY(hi))
2156 return HI2UF(hi);
2157 }
2158 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159
2160 return NULL;
2161}
2162
2163/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002164 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002165 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2166 * functions.
2167 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002168 * Return NULL for unknown function.
2169 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002170 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002171find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002172{
2173 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002174 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002176 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002178 // Find script-local function before global one.
2179 if (in_vim9script() && eval_isnamec1(*name)
2180 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002182 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2183 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002184 if (func != NULL)
2185 return func;
2186 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002187 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2188 {
2189 char_u *p = name + 5;
2190 long sid;
2191
2192 // printable "<SNR>123_Name" form
2193 sid = getdigits(&p);
2194 if (*p == '_')
2195 {
2196 func = find_func_with_sid(p + 1, (int)sid);
2197 if (func != NULL)
2198 return func;
2199 }
2200 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002202
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002203 if ((flags & FFED_NO_GLOBAL) == 0)
2204 {
2205 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002206 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002207 if (!HASHITEM_EMPTY(hi))
2208 return HI2UF(hi);
2209 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210
Bram Moolenaarb8822442022-01-11 15:24:05 +00002211 // Find autoload function if this is an autoload script.
2212 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2213 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002214}
2215
2216/*
2217 * Find a function by name, return pointer to it in ufuncs.
2218 * "cctx" is passed in a :def function to find imported functions.
2219 * Return NULL for unknown or dead function.
2220 */
2221 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002222find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002223{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002224 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002225
2226 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2227 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002228 return NULL;
2229}
2230
2231/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002232 * Return TRUE if "ufunc" is a global function.
2233 */
2234 int
2235func_is_global(ufunc_T *ufunc)
2236{
2237 return ufunc->uf_name[0] != K_SPECIAL;
2238}
2239
2240/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002241 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2242 */
2243 int
2244func_requires_g_prefix(ufunc_T *ufunc)
2245{
2246 return ufunc->uf_name[0] != K_SPECIAL
2247 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002248 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
2249 && !isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002250}
2251
2252/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002253 * Copy the function name of "fp" to buffer "buf".
2254 * "buf" must be able to hold the function name plus three bytes.
2255 * Takes care of script-local function names.
2256 */
2257 static void
2258cat_func_name(char_u *buf, ufunc_T *fp)
2259{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002260 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002261 {
2262 STRCPY(buf, "<SNR>");
2263 STRCAT(buf, fp->uf_name + 3);
2264 }
2265 else
2266 STRCPY(buf, fp->uf_name);
2267}
2268
2269/*
2270 * Add a number variable "name" to dict "dp" with value "nr".
2271 */
2272 static void
2273add_nr_var(
2274 dict_T *dp,
2275 dictitem_T *v,
2276 char *name,
2277 varnumber_T nr)
2278{
2279 STRCPY(v->di_key, name);
2280 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002281 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002282 v->di_tv.v_type = VAR_NUMBER;
2283 v->di_tv.v_lock = VAR_FIXED;
2284 v->di_tv.vval.v_number = nr;
2285}
2286
2287/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002288 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002289 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002290 static void
2291free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002292{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002293 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002294
Bram Moolenaarca16c602022-09-06 18:57:08 +01002295 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002296 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002297 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002298
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002299 // When garbage collecting a funccall_T may be freed before the
2300 // function that references it, clear its uf_scoped field.
2301 // The function may have been redefined and point to another
2302 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002303 if (fp != NULL && fp->uf_scoped == fc)
2304 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002305 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002306 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002307
Bram Moolenaarca16c602022-09-06 18:57:08 +01002308 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002309 vim_free(fc);
2310}
2311
2312/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002313 * Free "fc" and what it contains.
2314 * Can be called only when "fc" is kept beyond the period of it called,
2315 * i.e. after cleanup_function_call(fc).
2316 */
2317 static void
2318free_funccal_contents(funccall_T *fc)
2319{
2320 listitem_T *li;
2321
2322 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002323 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002324
2325 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002326 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002327
2328 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002329 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002330 clear_tv(&li->li_tv);
2331
2332 free_funccal(fc);
2333}
2334
2335/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002336 * Handle the last part of returning from a function: free the local hashtable.
2337 * Unless it is still in use by a closure.
2338 */
2339 static void
2340cleanup_function_call(funccall_T *fc)
2341{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002342 int may_free_fc = fc->fc_refcount <= 0;
2343 int free_fc = TRUE;
2344
Bram Moolenaarca16c602022-09-06 18:57:08 +01002345 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002346
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002347 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002348 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2349 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002350 else
2351 free_fc = FALSE;
2352
2353 // If the a:000 list and the l: and a: dicts are not referenced and
2354 // there is no closure using it, we can free the funccall_T and what's
2355 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002356 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2357 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002358 else
2359 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002360 int todo;
2361 hashitem_T *hi;
2362 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002363
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002364 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002365
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002366 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002367 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002368 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002369 {
2370 if (!HASHITEM_EMPTY(hi))
2371 {
2372 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002373 di = HI2DI(hi);
2374 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002375 }
2376 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002377 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002378
Bram Moolenaarca16c602022-09-06 18:57:08 +01002379 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2380 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002381 else
2382 {
2383 listitem_T *li;
2384
2385 free_fc = FALSE;
2386
2387 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002388 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002389 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002390 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002391
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002392 if (free_fc)
2393 free_funccal(fc);
2394 else
2395 {
2396 static int made_copy = 0;
2397
2398 // "fc" is still in use. This can happen when returning "a:000",
2399 // assigning "l:" to a global variable or defining a closure.
2400 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002401 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002402 previous_funccal = fc;
2403
2404 if (want_garbage_collect)
2405 // If garbage collector is ready, clear count.
2406 made_copy = 0;
2407 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002408 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002409 // We have made a lot of copies, worth 4 Mbyte. This can happen
2410 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002411 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002412 // too much memory.
2413 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002414 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002415 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002416 }
2417}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002418
2419/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002420 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2421 */
2422 static int
2423numbered_function(char_u *name)
2424{
2425 return isdigit(*name)
2426 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2427}
2428
2429/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002430 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002431 * 1. ordinary names, function defined with :function or :def;
2432 * can start with "<SNR>123_" literally or with K_SPECIAL.
2433 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002434 * For the first we only count the name stored in func_hashtab as a reference,
2435 * using function() does not count as a reference, because the function is
2436 * looked up by name.
2437 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002438 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002439func_name_refcount(char_u *name)
2440{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002441 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002442}
2443
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002444/*
2445 * Unreference "fc": decrement the reference count and free it when it
2446 * becomes zero. "fp" is detached from "fc".
2447 * When "force" is TRUE we are exiting.
2448 */
2449 static void
2450funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2451{
2452 funccall_T **pfc;
2453 int i;
2454
2455 if (fc == NULL)
2456 return;
2457
2458 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002459 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2460 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2461 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2462 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 {
2464 if (fc == *pfc)
2465 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002466 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 free_funccal_contents(fc);
2468 return;
2469 }
2470 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002471 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2472 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2473 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474}
2475
2476/*
2477 * Remove the function from the function hashtable. If the function was
2478 * deleted while it still has references this was already done.
2479 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2480 */
2481 static int
2482func_remove(ufunc_T *fp)
2483{
2484 hashitem_T *hi;
2485
2486 // Return if it was already virtually deleted.
2487 if (fp->uf_flags & FC_DEAD)
2488 return FALSE;
2489
2490 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002491 if (HASHITEM_EMPTY(hi))
2492 return FALSE;
2493
2494 // When there is a def-function index do not actually remove the
2495 // function, so we can find the index when defining the function again.
2496 // Do remove it when it's a copy.
2497 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002499 fp->uf_flags |= FC_DEAD;
2500 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002501 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002502 hash_remove(&func_hashtab, hi, "remove function");
2503 fp->uf_flags |= FC_DELETED;
2504 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505}
2506
2507 static void
2508func_clear_items(ufunc_T *fp)
2509{
2510 ga_clear_strings(&(fp->uf_args));
2511 ga_clear_strings(&(fp->uf_def_args));
2512 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002514 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002515 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002516 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002517
2518 // Increment the refcount of this function to avoid it being freed
2519 // recursively when the partial is freed.
2520 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002521 partial_unref(fp->uf_partial);
2522 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002523 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002524
2525#ifdef FEAT_LUA
2526 if (fp->uf_cb_free != NULL)
2527 {
2528 fp->uf_cb_free(fp->uf_cb_state);
2529 fp->uf_cb_free = NULL;
2530 }
2531
2532 fp->uf_cb_state = NULL;
2533 fp->uf_cb = NULL;
2534#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535#ifdef FEAT_PROFILE
2536 VIM_CLEAR(fp->uf_tml_count);
2537 VIM_CLEAR(fp->uf_tml_total);
2538 VIM_CLEAR(fp->uf_tml_self);
2539#endif
2540}
2541
2542/*
2543 * Free all things that a function contains. Does not free the function
2544 * itself, use func_free() for that.
2545 * When "force" is TRUE we are exiting.
2546 */
2547 static void
2548func_clear(ufunc_T *fp, int force)
2549{
2550 if (fp->uf_cleared)
2551 return;
2552 fp->uf_cleared = TRUE;
2553
2554 // clear this function
2555 func_clear_items(fp);
2556 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002557 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558}
2559
2560/*
2561 * Free a function and remove it from the list of functions. Does not free
2562 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002563 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002564 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002565 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002566 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002567func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568{
2569 // Only remove it when not done already, otherwise we would remove a newer
2570 // version of the function with the same name.
2571 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2572 func_remove(fp);
2573
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002574 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002575 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002576 if (fp->uf_dfunc_idx > 0)
2577 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002578 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002580 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002581 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002582 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583}
2584
2585/*
2586 * Free all things that a function contains and free the function itself.
2587 * When "force" is TRUE we are exiting.
2588 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002589 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590func_clear_free(ufunc_T *fp, int force)
2591{
2592 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002593 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2594 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002595 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002596 else
2597 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598}
2599
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002600/*
2601 * Copy already defined function "lambda" to a new function with name "global".
2602 * This is for when a compiled function defines a global function.
2603 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002604 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002605copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002606 char_u *lambda,
2607 char_u *global,
2608 loopvarinfo_T *loopvarinfo,
2609 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002610{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002611 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002612 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002613
2614 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002615 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002616 semsg(_(e_lambda_function_not_found_str), lambda);
2617 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002618 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002619
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002620 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002621 if (fp != NULL)
2622 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002623 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002624 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002625 return FAIL;
2626 }
2627
Bram Moolenaare01e5212023-01-08 20:31:18 +00002628 fp = alloc_ufunc(global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002629 if (fp == NULL)
2630 return FAIL;
2631
2632 fp->uf_varargs = ufunc->uf_varargs;
2633 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2634 fp->uf_def_status = ufunc->uf_def_status;
2635 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2636 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2637 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2638 == FAIL
2639 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2640 goto failed;
2641
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002642 if (ufunc->uf_arg_types != NULL)
2643 {
2644 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2645 if (fp->uf_arg_types == NULL)
2646 goto failed;
2647 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2648 sizeof(type_T *) * fp->uf_args.ga_len);
2649 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002650 if (ufunc->uf_va_name != NULL)
2651 {
2652 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2653 if (fp->uf_va_name == NULL)
2654 goto failed;
2655 }
2656 fp->uf_ret_type = ufunc->uf_ret_type;
2657
2658 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002659
2660 fp->uf_name_exp = NULL;
2661 set_ufunc_name(fp, global);
2662
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002663 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002664
2665 // the referenced dfunc_T is now used one more time
2666 link_def_function(fp);
2667
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002668 // Create a partial to store the context of the function where it was
2669 // instantiated. Only needs to be done once. Do this on the original
2670 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002671 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2672 {
2673 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2674
2675 if (pt == NULL)
2676 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002677 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002678 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002679 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002680 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002681 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002682 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002683 }
2684
2685 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002686
2687failed:
2688 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002689 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002690}
2691
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002692static int funcdepth = 0;
2693
2694/*
2695 * Increment the function call depth count.
2696 * Return FAIL when going over 'maxfuncdepth'.
2697 * Otherwise return OK, must call funcdepth_decrement() later!
2698 */
2699 int
2700funcdepth_increment(void)
2701{
2702 if (funcdepth >= p_mfd)
2703 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002704 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002705 return FAIL;
2706 }
2707 ++funcdepth;
2708 return OK;
2709}
2710
2711 void
2712funcdepth_decrement(void)
2713{
2714 --funcdepth;
2715}
2716
2717/*
2718 * Get the current function call depth.
2719 */
2720 int
2721funcdepth_get(void)
2722{
2723 return funcdepth;
2724}
2725
2726/*
2727 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002728 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002729 * times as funcdepth_increment().
2730 */
2731 void
2732funcdepth_restore(int depth)
2733{
2734 funcdepth = depth;
2735}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002736
2737/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002738 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2739 * "rettv".
2740 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2741 * Returns NULL when allocation fails.
2742 */
2743 funccall_T *
2744create_funccal(ufunc_T *fp, typval_T *rettv)
2745{
2746 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2747
2748 if (fc == NULL)
2749 return NULL;
2750 fc->fc_caller = current_funccal;
2751 current_funccal = fc;
2752 fc->fc_func = fp;
2753 func_ptr_ref(fp);
2754 fc->fc_rettv = rettv;
2755 return fc;
2756}
2757
2758/*
2759 * To be called when returning from a compiled function; restores
2760 * current_funccal.
2761 */
2762 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002763remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002764{
2765 funccall_T *fc = current_funccal;
2766
2767 current_funccal = fc->fc_caller;
2768 free_funccal(fc);
2769}
2770
2771/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002772 * Call a user function.
2773 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002774 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002775call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002776 ufunc_T *fp, // pointer to function
2777 int argcount, // nr of args
2778 typval_T *argvars, // arguments
2779 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002780 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002781 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002782{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002783 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002784 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002785 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002786 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002787 funccall_T *fc;
2788 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002789 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002790 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002791 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002792 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002793 int i;
2794 int ai;
2795 int islambda = FALSE;
2796 char_u numbuf[NUMBUFLEN];
2797 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002798 typval_T *tv_to_free[MAX_FUNC_ARGS];
2799 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002800#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002801 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002802#endif
ichizok7e5fe382023-04-15 13:17:50 +01002803 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002804
Bram Moolenaarb2049902021-01-24 12:53:53 +01002805#ifdef FEAT_PROFILE
2806 CLEAR_FIELD(profile_info);
2807#endif
2808
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002809 // If depth of calling is getting too high, don't execute the function.
2810 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002811 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002812 rettv->v_type = VAR_NUMBER;
2813 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002814 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002815 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002816
Bram Moolenaare38eab22019-12-05 21:50:01 +01002817 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002818
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002819 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002820 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00002821 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002822 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002823 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002824 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2825 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002826 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002827 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002828
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002829 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002831#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002832 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002833#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002834 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002835#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002836 if (do_profiling == PROF_YES)
2837 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002838#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002839 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002840 if (call_def_function(fp, argcount, argvars, 0,
2841 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
2842 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002843 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002844#ifdef FEAT_PROFILE
2845 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002846 || (caller != NULL && caller->uf_profiling)))
2847 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002848#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002849 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002850 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002851 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 }
2853
Bram Moolenaar38453522021-11-28 22:00:12 +00002854 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002855
2856 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002857 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2858 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2859 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002860 */
2861 /*
2862 * Init l: variables.
2863 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002864 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002865 if (selfdict != NULL)
2866 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002867 // Set l:self to "selfdict". Use "name" to avoid a warning from
2868 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002869 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002870 name = v->di_key;
2871 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002872 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002873 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002874 v->di_tv.v_type = VAR_DICT;
2875 v->di_tv.v_lock = 0;
2876 v->di_tv.vval.v_dict = selfdict;
2877 ++selfdict->dv_refcount;
2878 }
2879
2880 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002881 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002882 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002883 * Set a:000 to a list with room for the "..." arguments.
2884 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002885 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002886 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002887 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002888 (varnumber_T)(argcount >= fp->uf_args.ga_len
2889 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002890 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002891 if ((fp->uf_flags & FC_NOARGS) == 0)
2892 {
2893 // Use "name" to avoid a warning from some compiler that checks the
2894 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002895 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002896 name = v->di_key;
2897 STRCPY(name, "000");
2898 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002899 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002900 v->di_tv.v_type = VAR_LIST;
2901 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002902 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002903 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002904 CLEAR_FIELD(fc->fc_l_varlist);
2905 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2906 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002907
2908 /*
2909 * Set a:firstline to "firstline" and a:lastline to "lastline".
2910 * Set a:name to named arguments.
2911 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002912 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002913 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002914 if ((fp->uf_flags & FC_NOARGS) == 0)
2915 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002916 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2917 "firstline", (varnumber_T)funcexe->fe_firstline);
2918 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2919 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002920 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002921 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002922 {
2923 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002924 typval_T def_rettv;
2925 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002926
2927 ai = i - fp->uf_args.ga_len;
2928 if (ai < 0)
2929 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002930 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002931 name = FUNCARG(fp, i);
2932 if (islambda)
2933 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002934
2935 // evaluate named argument default expression
2936 isdefault = ai + fp->uf_def_args.ga_len >= 0
2937 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2938 && argvars[i].vval.v_number == VVAL_NONE));
2939 if (isdefault)
2940 {
2941 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002942
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002943 def_rettv.v_type = VAR_NUMBER;
2944 def_rettv.vval.v_number = -1;
2945
2946 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2947 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002948 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002949 {
2950 default_arg_err = 1;
2951 break;
2952 }
2953 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002954 }
2955 else
2956 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002957 if ((fp->uf_flags & FC_NOARGS) != 0)
2958 // Bail out if no a: arguments used (in lambda).
2959 break;
2960
Bram Moolenaare38eab22019-12-05 21:50:01 +01002961 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002962 sprintf((char *)numbuf, "%d", ai + 1);
2963 name = numbuf;
2964 }
2965 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2966 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002967 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002968 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002969 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002970 }
2971 else
2972 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002973 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002974 if (v == NULL)
2975 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002976 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002977 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002978
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002979 // Note: the values are copied directly to avoid alloc/free.
2980 // "argvars" must have VAR_FIXED for v_lock.
2981 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002982 v->di_tv.v_lock = VAR_FIXED;
2983
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002984 if (isdefault)
2985 // Need to free this later, no matter where it's stored.
2986 tv_to_free[tv_to_free_len++] = &v->di_tv;
2987
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002988 if (addlocal)
2989 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002990 // Named arguments should be accessed without the "a:" prefix in
2991 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002992 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002993 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002994 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002995 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002996 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002997
2998 if (ai >= 0 && ai < MAX_FUNC_ARGS)
2999 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003000 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003001
3002 li->li_tv = argvars[i];
3003 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003004 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003005 }
3006 }
3007
Bram Moolenaare38eab22019-12-05 21:50:01 +01003008 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003009 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003010
3011 if (fp->uf_flags & FC_SANDBOX)
3012 {
3013 using_sandbox = TRUE;
3014 ++sandbox;
3015 }
3016
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003017 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003018 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003019 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003020 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003021 ++no_wait_return;
3022 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003023
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003024 smsg(_("calling %s"), SOURCING_NAME);
3025 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003026 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003027 char_u buf[MSG_BUF_LEN];
3028 char_u numbuf2[NUMBUFLEN];
3029 char_u *tofree;
3030 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003031
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003032 msg_puts("(");
3033 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003034 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003035 if (i > 0)
3036 msg_puts(", ");
3037 if (argvars[i].v_type == VAR_NUMBER)
3038 msg_outnum((long)argvars[i].vval.v_number);
3039 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003040 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003041 // Do not want errors such as E724 here.
3042 ++emsg_off;
3043 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3044 --emsg_off;
3045 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003046 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003047 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003048 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003049 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3050 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003051 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003052 msg_puts((char *)s);
3053 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003054 }
3055 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003056 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003057 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003058 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003059 msg_puts("\n"); // don't overwrite this either
3060
3061 verbose_leave_scroll();
3062 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003063 }
3064#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003065 if (do_profiling == PROF_YES)
3066 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003067 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003068#endif
3069
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003070 // "legacy" does not apply to commands in the function
3071 sticky_cmdmod_flags = 0;
3072
Bram Moolenaar98aff652022-09-06 21:02:35 +01003073 // If called from a compiled :def function the execution context must be
3074 // hidden, any deferred functions need to be added to the function being
3075 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003076 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003077
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003078 save_current_sctx = current_sctx;
3079 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003080 save_did_emsg = did_emsg;
3081 did_emsg = FALSE;
3082
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003083 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003084 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003085 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003086 retval = FCERR_FAILED;
3087 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003088 else if (islambda)
3089 {
3090 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3091
3092 // A Lambda always has the command "return {expr}". It is much faster
3093 // to evaluate {expr} directly.
3094 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003095 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003096 --ex_nesting_level;
3097 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003098 else
3099 // call do_cmdline() to execute the lines
3100 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003101 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3102
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003103 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003104 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003105
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003106 if (RedrawingDisabled > 0)
3107 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003108
Bram Moolenaare38eab22019-12-05 21:50:01 +01003109 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003110 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3111 {
3112 clear_tv(rettv);
3113 rettv->v_type = VAR_NUMBER;
3114 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003115
3116 // In corner cases returning a "failed" value is not backwards
3117 // compatible. Only do this for Vim9 script.
3118 if (in_vim9script())
3119 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003120 }
3121
3122#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003123 if (do_profiling == PROF_YES)
3124 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003125 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003126
3127 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3128 profile_may_end_func(&profile_info, fp, caller);
3129 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003130#endif
3131
Bram Moolenaare38eab22019-12-05 21:50:01 +01003132 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003133 if (p_verbose >= 12)
3134 {
3135 ++no_wait_return;
3136 verbose_enter_scroll();
3137
3138 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003139 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003140 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003141 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003142 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003143 else
3144 {
3145 char_u buf[MSG_BUF_LEN];
3146 char_u numbuf2[NUMBUFLEN];
3147 char_u *tofree;
3148 char_u *s;
3149
Bram Moolenaare38eab22019-12-05 21:50:01 +01003150 // The value may be very long. Skip the middle part, so that we
3151 // have some idea how it starts and ends. smsg() would always
3152 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003153 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003154 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003155 --emsg_off;
3156 if (s != NULL)
3157 {
3158 if (vim_strsize(s) > MSG_BUF_CLEN)
3159 {
3160 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3161 s = buf;
3162 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003163 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003164 vim_free(tofree);
3165 }
3166 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003167 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003168
3169 verbose_leave_scroll();
3170 --no_wait_return;
3171 }
3172
ichizok7e5fe382023-04-15 13:17:50 +01003173 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003174 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003175 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003176 restore_current_ectx(save_current_ectx);
3177
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003178#ifdef FEAT_PROFILE
3179 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003180 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003181#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003182 if (using_sandbox)
3183 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003184 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003185
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003186 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003187 {
3188 ++no_wait_return;
3189 verbose_enter_scroll();
3190
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003191 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003192 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003193
3194 verbose_leave_scroll();
3195 --no_wait_return;
3196 }
3197
3198 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003199 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003200 for (i = 0; i < tv_to_free_len; ++i)
3201 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003202 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003203
3204 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003205}
3206
3207/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003208 * Check the argument count for user function "fp".
3209 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3210 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003211 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003212check_user_func_argcount(ufunc_T *fp, int argcount)
3213{
3214 int regular_args = fp->uf_args.ga_len;
3215
3216 if (argcount < regular_args - fp->uf_def_args.ga_len)
3217 return FCERR_TOOFEW;
3218 else if (!has_varargs(fp) && argcount > regular_args)
3219 return FCERR_TOOMANY;
3220 return FCERR_UNKNOWN;
3221}
3222
3223/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003225 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003226 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003227call_user_func_check(
3228 ufunc_T *fp,
3229 int argcount,
3230 typval_T *argvars,
3231 typval_T *rettv,
3232 funcexe_T *funcexe,
3233 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003234{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003235 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003236
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003237#ifdef FEAT_LUA
3238 if (fp->uf_flags & FC_CFUNC)
3239 {
3240 cfunc_T cb = fp->uf_cb;
3241
3242 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3243 }
3244#endif
3245
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003246 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3247 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003248 error = check_user_func_argcount(fp, argcount);
3249 if (error != FCERR_UNKNOWN)
3250 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003251
Bram Moolenaar50824712020-12-20 21:10:17 +01003252 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003253 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003255 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003257 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 int did_save_redo = FALSE;
3259 save_redo_T save_redo;
3260
3261 /*
3262 * Call the user function.
3263 * Save and restore search patterns, script variables and
3264 * redo buffer.
3265 */
3266 save_search_patterns();
3267 if (!ins_compl_active())
3268 {
3269 saveRedobuff(&save_redo);
3270 did_save_redo = TRUE;
3271 }
3272 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003273 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003274 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3276 // Function was unreferenced while being used, free it now.
3277 func_clear_free(fp, FALSE);
3278 if (did_save_redo)
3279 restoreRedobuff(&save_redo);
3280 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003281 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003282
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003284}
3285
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003286static funccal_entry_T *funccal_stack = NULL;
3287
3288/*
3289 * Save the current function call pointer, and set it to NULL.
3290 * Used when executing autocommands and for ":source".
3291 */
3292 void
3293save_funccal(funccal_entry_T *entry)
3294{
3295 entry->top_funccal = current_funccal;
3296 entry->next = funccal_stack;
3297 funccal_stack = entry;
3298 current_funccal = NULL;
3299}
3300
3301 void
3302restore_funccal(void)
3303{
3304 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003305 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003306 else
3307 {
3308 current_funccal = funccal_stack->top_funccal;
3309 funccal_stack = funccal_stack->next;
3310 }
3311}
3312
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003313 funccall_T *
3314get_current_funccal(void)
3315{
3316 return current_funccal;
3317}
3318
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003319/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003320 * Return TRUE when currently at the script level:
3321 * - not in a function
3322 * - not executing an autocommand
3323 * Note that when an autocommand sources a script the result is FALSE;
3324 */
3325 int
3326at_script_level(void)
3327{
3328 return current_funccal == NULL && autocmd_match == NULL;
3329}
3330
3331/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003332 * Mark all functions of script "sid" as deleted.
3333 */
3334 void
3335delete_script_functions(int sid)
3336{
3337 hashitem_T *hi;
3338 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003339 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003340 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003341 size_t len;
3342
3343 buf[0] = K_SPECIAL;
3344 buf[1] = KS_EXTRA;
3345 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003346 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003347 len = STRLEN(buf);
3348
Bram Moolenaarce658352020-07-31 23:47:12 +02003349 while (todo > 0)
3350 {
3351 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003352 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003353 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003354 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003355 fp = HI2UF(hi);
3356 if (STRNCMP(fp->uf_name, buf, len) == 0)
3357 {
3358 int changed = func_hashtab.ht_changed;
3359
3360 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003361
3362 if (fp->uf_calls > 0)
3363 {
3364 // Function is executing, don't free it but do remove
3365 // it from the hashtable.
3366 if (func_remove(fp))
3367 fp->uf_refcount--;
3368 }
3369 else
3370 {
3371 func_clear(fp, TRUE);
3372 // When clearing a function another function can be
3373 // cleared as a side effect. When that happens start
3374 // over.
3375 if (changed != func_hashtab.ht_changed)
3376 break;
3377 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003378 }
3379 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003380 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003381 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003382}
3383
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003384#if defined(EXITFREE) || defined(PROTO)
3385 void
3386free_all_functions(void)
3387{
3388 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003389 ufunc_T *fp;
3390 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003391 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003392 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003393
Bram Moolenaare38eab22019-12-05 21:50:01 +01003394 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003395 while (current_funccal != NULL)
3396 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003397 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003398 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003399 if (current_funccal == NULL && funccal_stack != NULL)
3400 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003401 }
3402
Bram Moolenaare38eab22019-12-05 21:50:01 +01003403 // First clear what the functions contain. Since this may lower the
3404 // reference count of a function, it may also free a function and change
3405 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003406 while (todo > 0)
3407 {
3408 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003409 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003410 if (!HASHITEM_EMPTY(hi))
3411 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 // clear the def function index now
3413 fp = HI2UF(hi);
3414 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003415 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416
Bram Moolenaare38eab22019-12-05 21:50:01 +01003417 // Only free functions that are not refcounted, those are
3418 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003419 if (func_name_refcount(fp->uf_name))
3420 ++skipped;
3421 else
3422 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003423 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003424 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003425 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003426 {
3427 skipped = 0;
3428 break;
3429 }
3430 }
3431 --todo;
3432 }
3433 }
3434
Bram Moolenaare38eab22019-12-05 21:50:01 +01003435 // Now actually free the functions. Need to start all over every time,
3436 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003437 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003438 while (func_hashtab.ht_used > skipped)
3439 {
3440 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003441 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003442 if (!HASHITEM_EMPTY(hi))
3443 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003444 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003445 // Only free functions that are not refcounted, those are
3446 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003447 fp = HI2UF(hi);
3448 if (func_name_refcount(fp->uf_name))
3449 ++skipped;
3450 else
3451 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003452 if (func_free(fp, FALSE) == OK)
3453 {
3454 skipped = 0;
3455 break;
3456 }
3457 // did not actually free it
3458 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003459 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003460 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003461 }
3462 if (skipped == 0)
3463 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464
3465 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003466}
3467#endif
3468
3469/*
3470 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003471 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3472 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003473 * "len" is the length of "name", or -1 for NUL terminated.
3474 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003476builtin_function(char_u *name, int len)
3477{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003478 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003479
Bram Moolenaara26b9702020-04-18 19:53:28 +02003480 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003481 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003482 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3483 {
3484 if (name[i] == AUTOLOAD_CHAR)
3485 return FALSE;
3486 if (!eval_isnamec(name[i]))
3487 {
3488 // "name.something" is not a builtin function
3489 if (name[i] == '.')
3490 return FALSE;
3491 break;
3492 }
3493 }
3494 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003495}
3496
3497 int
3498func_call(
3499 char_u *name,
3500 typval_T *args,
3501 partial_T *partial,
3502 dict_T *selfdict,
3503 typval_T *rettv)
3504{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003505 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003506 listitem_T *item;
3507 typval_T argv[MAX_FUNC_ARGS + 1];
3508 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003509 int r = 0;
3510
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003511 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003512 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003513 {
3514 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3515 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003516 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003517 break;
3518 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003519 // Make a copy of each argument. This is needed to be able to set
3520 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003521 copy_tv(&item->li_tv, &argv[argc++]);
3522 }
3523
3524 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003525 {
3526 funcexe_T funcexe;
3527
Bram Moolenaara80faa82020-04-12 19:37:17 +02003528 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003529 funcexe.fe_firstline = curwin->w_cursor.lnum;
3530 funcexe.fe_lastline = curwin->w_cursor.lnum;
3531 funcexe.fe_evaluate = TRUE;
3532 funcexe.fe_partial = partial;
3533 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003534 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3535 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003536
Bram Moolenaare38eab22019-12-05 21:50:01 +01003537 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003538 while (argc > 0)
3539 clear_tv(&argv[--argc]);
3540
3541 return r;
3542}
3543
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003544static int callback_depth = 0;
3545
3546 int
3547get_callback_depth(void)
3548{
3549 return callback_depth;
3550}
3551
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003552/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003553 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003554 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003555 */
3556 int
3557call_callback(
3558 callback_T *callback,
3559 int len, // length of "name" or -1 to use strlen()
3560 typval_T *rettv, // return value goes here
3561 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003562 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003563 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003564{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003565 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003566 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003567
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003568 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3569 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02003570 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003571 funcexe.fe_evaluate = TRUE;
3572 funcexe.fe_partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003573 ++callback_depth;
3574 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3575 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003576
3577 // When a :def function was called that uses :try an error would be turned
3578 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003579 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003580 {
3581 need_rethrow = FALSE;
3582 handle_did_throw();
3583 }
3584
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003585 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003586}
3587
3588/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003589 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003590 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003591 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3592 */
3593 varnumber_T
3594call_callback_retnr(
3595 callback_T *callback,
3596 int argcount, // number of "argvars"
3597 typval_T *argvars) // vars for arguments, must have "argcount"
3598 // PLUS ONE elements!
3599{
3600 typval_T rettv;
3601 varnumber_T retval;
3602
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003603 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003604 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003605
3606 retval = tv_get_number_chk(&rettv, NULL);
3607 clear_tv(&rettv);
3608 return retval;
3609}
3610
3611/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003612 * Give an error message for the result of a function.
3613 * Nothing if "error" is FCERR_NONE.
3614 */
3615 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003616user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617{
3618 switch (error)
3619 {
3620 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003621 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003622 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003623 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003624 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 break;
3626 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003627 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 break;
3629 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003630 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631 break;
3632 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003633 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 break;
3635 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003636 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 break;
3638 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003639 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 break;
3641 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003642 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3643 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003645 case FCERR_OTHER:
3646 case FCERR_FAILED:
3647 // assume the error message was already given
3648 break;
3649 case FCERR_NONE:
3650 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 }
3652}
3653
3654/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003655 * Check the argument types "argvars[argcount]" for "name" using the
3656 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3657 * is already included in "argvars[]".
3658 * Will do nothing if "funcexe->fe_check_type" is NULL or
3659 * "funcexe->fe_evaluate" is FALSE;
3660 * Returns an FCERR_ value.
3661 */
3662 static funcerror_T
3663may_check_argument_types(
3664 funcexe_T *funcexe,
3665 typval_T *argvars,
3666 int argcount,
3667 int base_included,
3668 char_u *name)
3669{
3670 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3671 {
3672 // Check that the argument types are OK for the types of the funcref.
3673 if (check_argument_types(funcexe->fe_check_type,
3674 argvars, argcount,
3675 base_included ? NULL : funcexe->fe_basetv,
3676 name) == FAIL)
3677 return FCERR_OTHER;
3678 }
3679 return FCERR_NONE;
3680}
3681
3682/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003683 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003684 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003685 * Return FAIL when the function can't be called, OK otherwise.
3686 * Also returns OK when an error was encountered while executing the function.
3687 */
3688 int
3689call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003690 char_u *funcname, // name of the function
3691 int len, // length of "name" or -1 to use strlen()
3692 typval_T *rettv, // return value goes here
3693 int argcount_in, // number of "argvars"
3694 typval_T *argvars_in, // vars for arguments, must have "argcount"
3695 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003696 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003697{
3698 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003699 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003700 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003701 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003702 char_u fname_buf[FLEN_FIXED + 1];
3703 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003704 char_u *fname = NULL;
3705 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003706 int argcount = argcount_in;
3707 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003708 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003709 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003710 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003711 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003712 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003713 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003714 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003715 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003716
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003717 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3718 // even when call_func() returns FAIL.
3719 rettv->v_type = VAR_UNKNOWN;
3720
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003721 if (partial != NULL)
3722 fp = partial->pt_func;
3723 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003724 fp = funcexe->fe_ufunc;
3725
3726 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003727 {
3728 // Make a copy of the name, if it comes from a funcref variable it
3729 // could be changed or deleted in the called function.
3730 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3731 if (name == NULL)
3732 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003733
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003734 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3735 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003736
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003737 if (funcexe->fe_doesrange != NULL)
3738 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003739
3740 if (partial != NULL)
3741 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003742 // When the function has a partial with a dict and there is a dict
3743 // argument, use the dict argument. That is backwards compatible.
3744 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003745 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003746 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003747 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003748 {
3749 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003750 {
3751 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3752 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003753 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003754 goto theend;
3755 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003756 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003757 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003758 for (i = 0; i < argcount_in; ++i)
3759 argv[i + argv_clear] = argvars_in[i];
3760 argvars = argv;
3761 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003762
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003763 if (funcexe->fe_check_type != NULL
3764 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003765 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003766 // Now funcexe->fe_check_type is missing the added arguments,
3767 // make a copy of the type with the correction.
3768 check_type = *funcexe->fe_check_type;
3769 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003770 check_type.tt_args = check_type_args;
3771 CLEAR_FIELD(check_type_args);
3772 for (i = 0; i < check_type.tt_argcount; ++i)
3773 check_type_args[i + partial->pt_argc] =
3774 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003775 check_type.tt_argcount += partial->pt_argc;
3776 check_type.tt_min_argcount += partial->pt_argc;
3777 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003778 }
3779 }
3780
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003781 if (error == FCERR_NONE)
3782 // check the argument types if possible
3783 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
3784 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003785
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003786 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003787 {
3788 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003789 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003790
Bram Moolenaar333894b2020-08-01 18:53:07 +02003791 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003792 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003793 {
3794 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003795 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003796 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003797
Bram Moolenaare38eab22019-12-05 21:50:01 +01003798 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003799 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003800 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003801
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003802 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003803 {
3804 /*
3805 * User defined function.
3806 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003807 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003808 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003809 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003810 if (fp != NULL && !is_global && in_vim9script()
3811 && func_requires_g_prefix(fp))
3812 // In Vim9 script g: is required to find a global
3813 // non-autoload function.
3814 fp = NULL;
3815 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003816
Bram Moolenaare38eab22019-12-05 21:50:01 +01003817 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003818 if (fp == NULL
3819 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003820 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003821 && !aborting())
3822 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003823 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003824 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003825 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003826 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003827 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3828 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003829 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003830 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003831 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003832 if (fp == NULL)
3833 {
3834 char_u *p = untrans_function_name(rfname);
3835
3836 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003837 // Don't do this if the name starts with "s:".
3838 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003839 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003840 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003841
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003842 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003843 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003844 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003845 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003846 int need_arg_check = FALSE;
3847 if (funcexe->fe_check_type == NULL)
3848 {
3849 funcexe->fe_check_type = fp->uf_func_type;
3850 need_arg_check = TRUE;
3851 }
3852
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003853 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003854 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003855 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003856 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003857 argv_clear, fp);
3858 need_arg_check = TRUE;
3859 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003860
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003861 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003862 {
3863 // Method call: base->Method()
3864 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003865 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003866 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003867 argvars = argv;
3868 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003869 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003870 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003871
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003872 // Check the argument types now that the function type and all
3873 // argument values are known, if not done above.
3874 if (need_arg_check)
3875 error = may_check_argument_types(funcexe, argvars, argcount,
3876 TRUE, (name != NULL) ? name : funcname);
3877 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
3878 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003880 }
3881 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003882 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003883 {
3884 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003885 * expr->method(): Find the method name in the table, call its
3886 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003887 */
3888 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003889 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003890 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003891 else
3892 {
3893 /*
3894 * Find the function name in the table, call its implementation.
3895 */
3896 error = call_internal_func(fname, argcount, argvars, rettv);
3897 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003898
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003899 /*
3900 * The function call (or "FuncUndefined" autocommand sequence) might
3901 * have been aborted by an error, an interrupt, or an explicitly thrown
3902 * exception that has not been caught so far. This situation can be
3903 * tested for by calling aborting(). For an error in an internal
3904 * function or for the "E132" error in call_user_func(), however, the
3905 * throw point at which the "force_abort" flag (temporarily reset by
3906 * emsg()) is normally updated has not been reached yet. We need to
3907 * update that flag first to make aborting() reliable.
3908 */
3909 update_force_abort();
3910 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003911 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003912 ret = OK;
3913
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003914theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003915 /*
3916 * Report an error unless the argument evaluation or function call has been
3917 * cancelled due to an aborting error, an interrupt, or an exception.
3918 */
3919 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003920 user_func_error(error, (name != NULL) ? name : funcname,
3921 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003922
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003923 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003924 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003925 clear_tv(&argv[--argv_clear + argv_base]);
3926
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003927 vim_free(tofree);
3928 vim_free(name);
3929
3930 return ret;
3931}
3932
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003933/*
3934 * Call a function without arguments, partial or dict.
3935 * This is like call_func() when the call is only "FuncName()".
3936 * To be used by "expr" options.
3937 * Returns NOTDONE when the function could not be found.
3938 */
3939 int
3940call_simple_func(
3941 char_u *funcname, // name of the function
3942 int len, // length of "name" or -1 to use strlen()
3943 typval_T *rettv) // return value goes here
3944{
3945 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003946 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003947 char_u fname_buf[FLEN_FIXED + 1];
3948 char_u *tofree = NULL;
3949 char_u *name;
3950 char_u *fname;
3951 char_u *rfname;
3952 int is_global = FALSE;
3953 ufunc_T *fp;
3954
3955 rettv->v_type = VAR_NUMBER; // default rettv is number zero
3956 rettv->vval.v_number = 0;
3957
3958 // Make a copy of the name, an option can be changed in the function.
3959 name = vim_strnsave(funcname, len);
3960 if (name == NULL)
3961 return ret;
3962
3963 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3964
3965 // Skip "g:" before a function name.
3966 if (fname[0] == 'g' && fname[1] == ':')
3967 {
3968 is_global = TRUE;
3969 rfname = fname + 2;
3970 }
3971 else
3972 rfname = fname;
3973 fp = find_func(rfname, is_global);
3974 if (fp != NULL && !is_global && in_vim9script()
3975 && func_requires_g_prefix(fp))
3976 // In Vim9 script g: is required to find a global non-autoload
3977 // function.
3978 fp = NULL;
3979 if (fp == NULL)
3980 ret = NOTDONE;
3981 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
3982 error = FCERR_DELETED;
3983 else if (fp != NULL)
3984 {
3985 typval_T argvars[1];
3986 funcexe_T funcexe;
3987
3988 argvars[0].v_type = VAR_UNKNOWN;
3989 CLEAR_FIELD(funcexe);
3990 funcexe.fe_evaluate = TRUE;
3991
3992 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
3993 if (error == FCERR_NONE)
3994 ret = OK;
3995 }
3996
3997 user_func_error(error, name, FALSE);
3998 vim_free(tofree);
3999 vim_free(name);
4000
4001 return ret;
4002}
4003
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004004 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005printable_func_name(ufunc_T *fp)
4006{
4007 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4008}
4009
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004010/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004011 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4012 * TRUE. Otherwise return FALSE.
4013 */
4014 static int
4015function_list_modified(int prev_ht_changed)
4016{
4017 if (prev_ht_changed != func_hashtab.ht_changed)
4018 {
4019 emsg(_(e_function_list_was_modified));
4020 return TRUE;
4021 }
4022 return FALSE;
4023}
4024
4025/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004026 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004027 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004028 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004029list_func_head(ufunc_T *fp, int indent)
4030{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004031 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004032 int j;
4033
4034 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004035
4036 // a timer at the more prompt may have deleted the function
4037 if (function_list_modified(prev_ht_changed))
4038 return FAIL;
4039
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004040 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004041 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004042 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004043 msg_puts("def ");
4044 else
4045 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004047 msg_putchar('(');
4048 for (j = 0; j < fp->uf_args.ga_len; ++j)
4049 {
4050 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004051 msg_puts(", ");
4052 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 if (fp->uf_arg_types != NULL)
4054 {
4055 char *tofree;
4056
4057 msg_puts(": ");
4058 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4059 vim_free(tofree);
4060 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004061 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4062 {
4063 msg_puts(" = ");
4064 msg_puts(((char **)(fp->uf_def_args.ga_data))
4065 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4066 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004067 }
4068 if (fp->uf_varargs)
4069 {
4070 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004071 msg_puts(", ");
4072 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004073 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074 if (fp->uf_va_name != NULL)
4075 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004076 if (!fp->uf_varargs)
4077 {
4078 if (j)
4079 msg_puts(", ");
4080 msg_puts("...");
4081 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004083 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 {
4085 char *tofree;
4086
4087 msg_puts(": ");
4088 msg_puts(type_name(fp->uf_va_type, &tofree));
4089 vim_free(tofree);
4090 }
4091 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004092 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004093
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004094 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004095 {
4096 if (fp->uf_ret_type != &t_void)
4097 {
4098 char *tofree;
4099
4100 msg_puts(": ");
4101 msg_puts(type_name(fp->uf_ret_type, &tofree));
4102 vim_free(tofree);
4103 }
4104 }
4105 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004106 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004107 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004108 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004109 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004110 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004111 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004112 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004113 msg_clr_eos();
4114 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004115 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004116
4117 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004118}
4119
4120/*
4121 * Get a function name, translating "<SID>" and "<SNR>".
4122 * Also handles a Funcref in a List or Dictionary.
4123 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004124 * Set "*is_global" to TRUE when the function must be global, unless
4125 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004126 * flags:
4127 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004128 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004129 * TFN_QUIET: be quiet
4130 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004131 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004132 * Advances "pp" to just after the function name (if no error).
4133 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004134 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004135trans_function_name(
4136 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004137 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004138 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004139 int flags)
4140{
4141 return trans_function_name_ext(pp, is_global, skip, flags,
4142 NULL, NULL, NULL, NULL);
4143}
4144
4145/*
4146 * trans_function_name() with extra arguments.
4147 * "fdp", "partial", "type" and "ufunc" can be NULL.
4148 */
4149 char_u *
4150trans_function_name_ext(
4151 char_u **pp,
4152 int *is_global,
4153 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004154 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004155 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004156 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004157 type_T **type, // return: type of funcref
4158 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004159{
4160 char_u *name = NULL;
4161 char_u *start;
4162 char_u *end;
4163 int lead;
4164 char_u sid_buf[20];
4165 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004167 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004168 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004169 int vim9script = in_vim9script();
4170 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004171
4172 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004173 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004174 start = *pp;
4175
Bram Moolenaare38eab22019-12-05 21:50:01 +01004176 // Check for hard coded <SNR>: already translated function ID (from a user
4177 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004178 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4179 && (*pp)[2] == (int)KE_SNR)
4180 {
4181 *pp += 3;
4182 len = get_id_len(pp) + 3;
4183 return vim_strnsave(start, len);
4184 }
4185
Bram Moolenaare38eab22019-12-05 21:50:01 +01004186 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4187 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004188 lead = eval_fname_script(start);
4189 if (lead > 2)
4190 start += lead;
4191
Bram Moolenaare38eab22019-12-05 21:50:01 +01004192 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004193 end = get_lval(start, NULL, &lv, FALSE, skip,
4194 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004195 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004196 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004197 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004198 {
4199 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004200 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004201 goto theend;
4202 }
4203 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4204 {
4205 /*
4206 * Report an invalid expression in braces, unless the expression
4207 * evaluation has been cancelled due to an aborting error, an
4208 * interrupt, or an exception.
4209 */
4210 if (!aborting())
4211 {
4212 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004213 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004214 }
4215 else
4216 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4217 goto theend;
4218 }
4219
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004220 if (lv.ll_ufunc != NULL)
4221 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004222 if (ufunc != NULL)
4223 *ufunc = lv.ll_ufunc;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004224 name = vim_strsave(lv.ll_ufunc->uf_name);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004225 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004226 goto theend;
4227 }
4228
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004229 if (lv.ll_tv != NULL)
4230 {
4231 if (fdp != NULL)
4232 {
4233 fdp->fd_dict = lv.ll_dict;
4234 fdp->fd_newkey = lv.ll_newkey;
4235 lv.ll_newkey = NULL;
4236 fdp->fd_di = lv.ll_di;
4237 }
4238 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4239 {
4240 name = vim_strsave(lv.ll_tv->vval.v_string);
4241 *pp = end;
4242 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004243 else if (lv.ll_tv->v_type == VAR_CLASS
4244 && lv.ll_tv->vval.v_class != NULL)
4245 {
4246 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4247 *pp = end;
4248 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004249 else if (lv.ll_tv->v_type == VAR_PARTIAL
4250 && lv.ll_tv->vval.v_partial != NULL)
4251 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004252 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004253 *pp = end;
4254 if (partial != NULL)
4255 *partial = lv.ll_tv->vval.v_partial;
4256 }
4257 else
4258 {
4259 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4260 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004261 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004262 else
4263 *pp = end;
4264 name = NULL;
4265 }
4266 goto theend;
4267 }
4268
4269 if (lv.ll_name == NULL)
4270 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004271 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004272 *pp = end;
4273 goto theend;
4274 }
4275
Bram Moolenaare38eab22019-12-05 21:50:01 +01004276 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004277 if (lv.ll_exp_name != NULL)
4278 {
4279 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004280 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004281 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004282 if (name == lv.ll_exp_name)
4283 name = NULL;
4284 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004285 else if (lv.ll_sid > 0)
4286 {
4287 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4288 int cc = *lv.ll_name_end;
4289
4290 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4291 *lv.ll_name_end = NUL;
4292 if (si->sn_autoload_prefix != NULL)
4293 {
4294 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4295 }
4296 else
4297 {
4298 sid_buf[0] = K_SPECIAL;
4299 sid_buf[1] = KS_EXTRA;
4300 sid_buf[2] = (int)KE_SNR;
4301 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004302 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004303 name = concat_str(sid_buf, lv.ll_name);
4304 }
4305 *lv.ll_name_end = cc;
4306 *pp = end;
4307 goto theend;
4308 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004309 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004310 {
4311 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004312 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004313 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004314 if (name == *pp)
4315 name = NULL;
4316 }
4317 if (name != NULL)
4318 {
4319 name = vim_strsave(name);
4320 *pp = end;
4321 if (STRNCMP(name, "<SNR>", 5) == 0)
4322 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004323 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004324 name[0] = K_SPECIAL;
4325 name[1] = KS_EXTRA;
4326 name[2] = (int)KE_SNR;
4327 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4328 }
4329 goto theend;
4330 }
4331
4332 if (lv.ll_exp_name != NULL)
4333 {
4334 len = (int)STRLEN(lv.ll_exp_name);
4335 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4336 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4337 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004338 // When there was "s:" already or the name expanded to get a
4339 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004340 lv.ll_name += 2;
4341 len -= 2;
4342 lead = 2;
4343 }
4344 }
4345 else
4346 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004347 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004348 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004349 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004350 if (lv.ll_name[0] == 'g')
4351 {
4352 if (is_global != NULL)
4353 {
4354 *is_global = TRUE;
4355 }
4356 else
4357 {
4358 // dropping "g:" without setting "is_global" won't work in
4359 // Vim9script, put it back later
4360 prefix_g = TRUE;
4361 extra = 2;
4362 }
4363 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004364 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004365 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004366 len = (int)(end - lv.ll_name);
4367 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004368 if (len <= 0)
4369 {
4370 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004371 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004372 goto theend;
4373 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004374
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004375 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004376 // starts with a lower case character: dict.func(). Or when in a class.
4377 vim9_local = ASCII_ISUPPER(*start) && vim9script
4378 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004380 /*
4381 * Copy the function name to allocated memory.
4382 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4383 * Accept <SNR>123_name() outside a script.
4384 */
4385 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004386 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004387 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004388 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004389 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004390 {
Kota Kato948a3892022-08-16 16:09:59 +01004391 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4392 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004393 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004394 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004395 goto theend;
4396 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004397 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004398 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004399 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004401 || eval_fname_sid(*pp))
4402 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004404 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004405 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004406 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004407 goto theend;
4408 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004409 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004410 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411 extra = 3 + (int)STRLEN(sid_buf);
4412 else
4413 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004414 }
4415 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004416 // The function name must start with an upper case letter (unless it is a
4417 // Vim9 class new() function or a Vim9 class private method)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004418 else if (!(flags & TFN_INT)
4419 && (builtin_function(lv.ll_name, len)
4420 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004421 && !((flags & TFN_IN_CLASS)
4422 && (STRNCMP(lv.ll_name, "new", 3) == 0
4423 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004424 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004425 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004426 : e_function_name_must_start_with_capital_or_s_str),
4427 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004428 goto theend;
4429 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004430 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004431 {
4432 char_u *cp = vim_strchr(lv.ll_name, ':');
4433
4434 if (cp != NULL && cp < end)
4435 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004436 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004437 goto theend;
4438 }
4439 }
4440
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004442 if (name != NULL)
4443 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004444 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004445 {
4446 name[0] = K_SPECIAL;
4447 name[1] = KS_EXTRA;
4448 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004449 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004450 STRCPY(name + 3, sid_buf);
4451 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004452 else if (prefix_g)
4453 {
4454 name[0] = 'g';
4455 name[1] = ':';
4456 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4458 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004459 }
4460 *pp = end;
4461
4462theend:
4463 clear_lval(&lv);
4464 return name;
4465}
4466
4467/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004468 * Assuming "name" is the result of trans_function_name() and it was prefixed
4469 * to use the script-local name, return the unmodified name (points into
4470 * "name"). Otherwise return NULL.
4471 * This can be used to first search for a script-local function and fall back
4472 * to the global function if not found.
4473 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004474 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004475untrans_function_name(char_u *name)
4476{
4477 char_u *p;
4478
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004479 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004480 {
4481 p = vim_strchr(name, '_');
4482 if (p != NULL)
4483 return p + 1;
4484 }
4485 return NULL;
4486}
4487
4488/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004489 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4490 * current script ID and returns the expanded function name. The caller should
4491 * free the returned name. If not called from a script context or the function
4492 * name doesn't start with these prefixes, then returns NULL.
4493 * This doesn't check whether the script-local function exists or not.
4494 */
4495 char_u *
4496get_scriptlocal_funcname(char_u *funcname)
4497{
4498 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004499 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004500 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004501 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004502
4503 if (funcname == NULL)
4504 return NULL;
4505
4506 if (STRNCMP(funcname, "s:", 2) != 0
4507 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004508 {
4509 ufunc_T *ufunc;
4510
4511 // The function name does not have a script-local prefix. Try finding
4512 // it when in a Vim9 script and there is no "g:" prefix.
4513 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4514 return NULL;
4515 ufunc = find_func(funcname, FALSE);
4516 if (ufunc == NULL || func_is_global(ufunc)
4517 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4518 return NULL;
4519 ++p;
4520 off = 0;
4521 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004522 else
4523 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004524
4525 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4526 {
4527 emsg(_(e_using_sid_not_in_script_context));
4528 return NULL;
4529 }
4530 // Expand s: prefix into <SNR>nr_<name>
4531 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4532 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004533 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004534 if (newname == NULL)
4535 return NULL;
4536 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004537 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004538
4539 return newname;
4540}
4541
4542/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004543 * Return script-local "fname" with the 3-byte sequence replaced by
4544 * printable <SNR> in allocated memory.
4545 */
4546 char_u *
4547alloc_printable_func_name(char_u *fname)
4548{
4549 char_u *n = alloc(STRLEN(fname + 3) + 6);
4550
4551 if (n != NULL)
4552 {
4553 STRCPY(n, "<SNR>");
4554 STRCPY(n + 5, fname + 3);
4555 }
4556 return n;
4557}
4558
4559/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004560 * Call trans_function_name(), except that a lambda is returned as-is.
4561 * Returns the name in allocated memory.
4562 */
4563 char_u *
4564save_function_name(
4565 char_u **name,
4566 int *is_global,
4567 int skip,
4568 int flags,
4569 funcdict_T *fudi)
4570{
4571 char_u *p = *name;
4572 char_u *saved;
4573
4574 if (STRNCMP(p, "<lambda>", 8) == 0)
4575 {
4576 p += 8;
4577 (void)getdigits(&p);
4578 saved = vim_strnsave(*name, p - *name);
4579 if (fudi != NULL)
4580 CLEAR_POINTER(fudi);
4581 }
4582 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004583 saved = trans_function_name_ext(&p, is_global, skip,
4584 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004585 *name = p;
4586 return saved;
4587}
4588
4589/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004590 * List functions. When "regmatch" is NULL all of then.
4591 * Otherwise functions matching "regmatch".
4592 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004593 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004594list_functions(regmatch_T *regmatch)
4595{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004596 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004597 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004598 hashitem_T *hi;
4599
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004600 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004601 {
4602 if (!HASHITEM_EMPTY(hi))
4603 {
4604 ufunc_T *fp = HI2UF(hi);
4605
4606 --todo;
4607 if ((fp->uf_flags & FC_DEAD) == 0
4608 && (regmatch == NULL
4609 ? !message_filtered(fp->uf_name)
4610 && !func_name_refcount(fp->uf_name)
4611 : !isdigit(*fp->uf_name)
4612 && vim_regexec(regmatch, fp->uf_name, 0)))
4613 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004614 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004615 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004616 if (function_list_modified(prev_ht_changed))
4617 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004618 }
4619 }
4620 }
4621}
4622
4623/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004624 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004625 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4626 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004627 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004628 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4629 * With CF_INTERFACE the function is define inside an interface, only the
4630 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004631 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004632 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004633 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004634define_function(
4635 exarg_T *eap,
4636 char_u *name_arg,
4637 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004638 int class_flags,
4639 ocmember_T *obj_members,
4640 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004641{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004642 int j;
4643 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004644 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004645 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004646 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004647 char_u *p;
4648 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004649 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004650 char_u *line_arg = NULL;
4651 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004653 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004654 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004655 garray_T newlines;
4656 int varargs = FALSE;
4657 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004658 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004659 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004660 int fp_allocated = FALSE;
4661 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004662 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004663 dictitem_T *v;
4664 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004665 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004666 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004667 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004668 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004669 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004670 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004671
4672 /*
4673 * ":function" without argument: list functions.
4674 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004675 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004676 {
4677 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004678 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004679 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004680 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004681 }
4682
4683 /*
4684 * ":function /pat": list functions matching pattern.
4685 */
4686 if (*eap->arg == '/')
4687 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004688 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004689 if (!eap->skip)
4690 {
4691 regmatch_T regmatch;
4692
4693 c = *p;
4694 *p = NUL;
4695 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4696 *p = c;
4697 if (regmatch.regprog != NULL)
4698 {
4699 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004700 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004701 vim_regfree(regmatch.regprog);
4702 }
4703 }
4704 if (*p == '/')
4705 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004706 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004707 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004708 }
4709
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710 ga_init(&newargs);
4711 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004712 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 ga_init(&default_args);
4714
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004715 /*
4716 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004717 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004718 * "name" == func, "fudi.fd_dict" == NULL
4719 * dict.func new dictionary entry
4720 * "name" == NULL, "fudi.fd_dict" set,
4721 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4722 * dict.func existing dict entry with a Funcref
4723 * "name" == func, "fudi.fd_dict" set,
4724 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4725 * dict.func existing dict entry that's not a Funcref
4726 * "name" == NULL, "fudi.fd_dict" set,
4727 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4728 * s:func script-local function name
4729 * g:func global function name, same as "func"
4730 */
4731 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004732 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004733 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004734 // nested function, argument is (args).
4735 paren = TRUE;
4736 CLEAR_FIELD(fudi);
4737 }
4738 else
4739 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004740 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004741 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004742 if (p[0] == 's' && p[1] == ':')
4743 {
4744 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4745 return NULL;
4746 }
4747 p = to_name_end(p, TRUE);
4748 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4749 {
4750 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4751 eap->arg);
4752 return NULL;
4753 }
4754 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004755 }
4756
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004757 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004758 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004759 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004760 paren = (vim_strchr(p, '(') != NULL);
4761 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004762 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004763 /*
4764 * Return on an invalid expression in braces, unless the expression
4765 * evaluation has been cancelled due to an aborting error, an
4766 * interrupt, or an exception.
4767 */
4768 if (!aborting())
4769 {
4770 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004771 semsg(_(e_key_not_present_in_dictionary_str),
4772 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004773 vim_free(fudi.fd_newkey);
4774 return NULL;
4775 }
4776 else
4777 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004778 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004779
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004780 // For "export def FuncName()" in an autoload script the function name
4781 // is stored with the legacy autoload name "dir#script#FuncName" so
4782 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004783 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004784 {
4785 char_u *prefixed = may_prefix_autoload(name);
4786
4787 if (prefixed != NULL && prefixed != name)
4788 {
4789 vim_free(name);
4790 name = prefixed;
4791 }
4792 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004793 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004794 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004795 {
4796 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4797 goto ret_free;
4798 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004799 }
4800
Bram Moolenaare38eab22019-12-05 21:50:01 +01004801 // An error in a function call during evaluation of an expression in magic
4802 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004803 saved_did_emsg = did_emsg;
4804 did_emsg = FALSE;
4805
4806 /*
4807 * ":function func" with only function name: list function.
4808 */
4809 if (!paren)
4810 {
4811 if (!ends_excmd(*skipwhite(p)))
4812 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004813 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004814 goto ret_free;
4815 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004816 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004817 if (eap->nextcmd != NULL)
4818 *p = NUL;
4819 if (!eap->skip && !got_int)
4820 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004821 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004822 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4823 {
4824 char_u *up = untrans_function_name(name);
4825
4826 // With Vim9 script the name was made script-local, if not
4827 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004828 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004829 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004830 }
4831
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004832 if (fp != NULL)
4833 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004834 // Check no function was added or removed from a timer, e.g. at
4835 // the more prompt. "fp" may then be invalid.
4836 int prev_ht_changed = func_hashtab.ht_changed;
4837
4838 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004839 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004840 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4841 {
4842 if (FUNCLINE(fp, j) == NULL)
4843 continue;
4844 msg_putchar('\n');
4845 msg_outnum((long)(j + 1));
4846 if (j < 9)
4847 msg_putchar(' ');
4848 if (j < 99)
4849 msg_putchar(' ');
4850 if (function_list_modified(prev_ht_changed))
4851 break;
4852 msg_prt_line(FUNCLINE(fp, j), FALSE);
4853 out_flush(); // show a line at a time
4854 ui_breakcheck();
4855 }
4856 if (!got_int)
4857 {
4858 msg_putchar('\n');
4859 if (!function_list_modified(prev_ht_changed))
4860 {
4861 if (fp->uf_def_status != UF_NOT_COMPILED)
4862 msg_puts(" enddef");
4863 else
4864 msg_puts(" endfunction");
4865 }
4866 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004867 }
4868 }
4869 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004870 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004871 }
4872 goto ret_free;
4873 }
4874
4875 /*
4876 * ":function name(arg1, arg2)" Define function.
4877 */
4878 p = skipwhite(p);
4879 if (*p != '(')
4880 {
4881 if (!eap->skip)
4882 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004883 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004884 goto ret_free;
4885 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004886 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004887 if (vim_strchr(p, '(') != NULL)
4888 p = vim_strchr(p, '(');
4889 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004890
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004891 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4892 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004893 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004894 goto ret_free;
4895 }
4896
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004897 // In Vim9 script only global functions can be redefined.
4898 if (vim9script && eap->forceit && !is_global)
4899 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004900 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004901 goto ret_free;
4902 }
4903
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004904 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004905
Bram Moolenaar04b12692020-05-04 23:24:44 +02004906 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004907 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004908 // Check the name of the function. Unless it's a dictionary function
4909 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004910 if (name != NULL)
4911 arg = name;
4912 else
4913 arg = fudi.fd_newkey;
4914 if (arg != NULL && (fudi.fd_di == NULL
4915 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4916 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4917 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004918 char_u *name_base = arg;
4919 int i;
4920
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004921 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004922 {
4923 name_base = vim_strchr(arg, '_');
4924 if (name_base == NULL)
4925 name_base = arg + 3;
4926 else
4927 ++name_base;
4928 }
4929 for (i = 0; name_base[i] != NUL && (i == 0
4930 ? eval_isnamec1(name_base[i])
4931 : eval_isnamec(name_base[i])); ++i)
4932 ;
4933 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004934 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004935
4936 // In Vim9 script a function cannot have the same name as a
4937 // variable.
4938 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004939 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4940 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004941 + EVAL_VAR_NO_FUNC) == OK)
4942 {
4943 semsg(_(e_redefining_script_item_str), name_base);
4944 goto ret_free;
4945 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004946 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004947 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004948 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004949 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004950 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004951 goto ret_free;
4952 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004953 }
4954
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004955 // This may get more lines and make the pointers into the first line
4956 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004957 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004958 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00004959 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02004960 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004961 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00004962 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004963 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004964 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004965
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004966 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004967 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004968 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004969 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004970 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004971 if (*p != ':')
4972 {
4973 semsg(_(e_no_white_space_allowed_before_colon_str), p);
4974 p = skipwhite(p);
4975 }
4976 else if (!IS_WHITE_OR_NUL(p[1]))
4977 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004978 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02004979 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004980 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004981 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004982 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01004983 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004984 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004985 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004986 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004987 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004988 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02004989 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004990 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004991 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02004992 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004993 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004994 else
4995 // find extra arguments "range", "dict", "abort" and "closure"
4996 for (;;)
4997 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01004998 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004999 p = skipwhite(p);
5000 if (STRNCMP(p, "range", 5) == 0)
5001 {
5002 flags |= FC_RANGE;
5003 p += 5;
5004 }
5005 else if (STRNCMP(p, "dict", 4) == 0)
5006 {
5007 flags |= FC_DICT;
5008 p += 4;
5009 }
5010 else if (STRNCMP(p, "abort", 5) == 0)
5011 {
5012 flags |= FC_ABORT;
5013 p += 5;
5014 }
5015 else if (STRNCMP(p, "closure", 7) == 0)
5016 {
5017 flags |= FC_CLOSURE;
5018 p += 7;
5019 if (current_funccal == NULL)
5020 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005021 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005022 name == NULL ? (char_u *)"" : name);
5023 goto erret;
5024 }
5025 }
5026 else
5027 break;
5028 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005029
Bram Moolenaare38eab22019-12-05 21:50:01 +01005030 // When there is a line break use what follows for the function body.
5031 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005032 if (*p == '\n')
5033 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005034 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005035 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5036 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005037 && !(VIM_ISWHITE(*whitep) && *p == '#'
5038 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005039 && !eap->skip
5040 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005041 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005042
5043 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005044 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5045 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005046 */
5047 if (KeyTyped)
5048 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005049 // Check if the function already exists, don't let the user type the
5050 // whole function before telling him it doesn't work! For a script we
5051 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005052 if (!eap->skip && !eap->forceit)
5053 {
5054 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005055 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005056 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005057 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005058 }
5059
5060 if (!eap->skip && did_emsg)
5061 goto erret;
5062
Bram Moolenaare38eab22019-12-05 21:50:01 +01005063 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005064 cmdline_row = msg_row;
5065 }
5066
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005067 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005068 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005069
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005070 // Do not define the function when getting the body fails and when
5071 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005072 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005073 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005074 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5075 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005076 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005077 goto erret;
5078
5079 /*
5080 * If there are no errors, add the function
5081 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005082 if (fudi.fd_dict != NULL)
5083 {
5084 char numbuf[20];
5085
5086 fp = NULL;
5087 if (fudi.fd_newkey == NULL && !eap->forceit)
5088 {
5089 emsg(_(e_dictionary_entry_already_exists));
5090 goto erret;
5091 }
5092 if (fudi.fd_di == NULL)
5093 {
5094 // Can't add a function to a locked dictionary
5095 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5096 goto erret;
5097 }
5098 // Can't change an existing function if it is locked
5099 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5100 goto erret;
5101
5102 // Give the function a sequential number. Can only be used with a
5103 // Funcref!
5104 vim_free(name);
5105 sprintf(numbuf, "%d", ++func_nr);
5106 name = vim_strsave((char_u *)numbuf);
5107 if (name == NULL)
5108 goto erret;
5109 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005110 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005111 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005113 char_u *find_name = name;
5114 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005115 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005117 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005118 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005119 var_conflict = TRUE;
5120
5121 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5122 {
5123 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5124
5125 if (si->sn_autoload_prefix != NULL)
5126 {
5127 if (is_export)
5128 {
5129 find_name = name + STRLEN(si->sn_autoload_prefix);
5130 v = find_var(find_name, &ht, TRUE);
5131 if (v != NULL)
5132 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005133 // Only check if the function already exists in the script,
5134 // global functions can be shadowed.
5135 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005136 }
5137 else
5138 {
5139 char_u *prefixed = may_prefix_autoload(name);
5140
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005141 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005142 {
5143 v = find_var(prefixed, &ht, TRUE);
5144 if (v != NULL)
5145 var_conflict = TRUE;
5146 vim_free(prefixed);
5147 }
5148 }
5149 }
5150 }
5151 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005152 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005153 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005154 goto erret;
5155 }
5156
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005157 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005158 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005159 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005160 char_u *uname = untrans_function_name(name);
5161
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005162 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005163 }
5164
5165 if (fp != NULL || import != NULL)
5166 {
5167 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005168
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005169 // Function can be replaced with "function!" and when sourcing the
5170 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005171 // A name that is used by an import can not be overruled.
5172 if (import != NULL
5173 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005174 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005175 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005176 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005177 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005178 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005179 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005180 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005181 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005182 goto erret;
5183 }
5184 if (fp->uf_calls > 0)
5185 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005186 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005187 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005188 goto erret;
5189 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005190 if (fp->uf_refcount > 1)
5191 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005192 // This function is referenced somewhere, don't redefine it but
5193 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005194 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005195 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005196 fp = NULL;
5197 overwrite = TRUE;
5198 }
5199 else
5200 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005201 char_u *exp_name = fp->uf_name_exp;
5202
5203 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005204 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005205 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005206 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005207 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005208 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005209#ifdef FEAT_PROFILE
5210 fp->uf_profiling = FALSE;
5211 fp->uf_prof_initialized = FALSE;
5212#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005213 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005214 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005215 }
5216 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005217
5218 if (fp == NULL)
5219 {
5220 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5221 {
5222 int slen, plen;
5223 char_u *scriptname;
5224
Bram Moolenaare38eab22019-12-05 21:50:01 +01005225 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005226 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005227 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005228 {
5229 scriptname = autoload_name(name);
5230 if (scriptname != NULL)
5231 {
5232 p = vim_strchr(scriptname, '/');
5233 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005234 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005235 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005236 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005237 j = OK;
5238 vim_free(scriptname);
5239 }
5240 }
5241 if (j == FAIL)
5242 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005243 linenr_T save_lnum = SOURCING_LNUM;
5244
5245 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005246 semsg(_(e_function_name_does_not_match_script_file_name_str),
5247 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005248 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005249 goto erret;
5250 }
5251 }
5252
Bram Moolenaare01e5212023-01-08 20:31:18 +00005253 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005254 if (fp == NULL)
5255 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005256 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005257
5258 if (fudi.fd_dict != NULL)
5259 {
5260 if (fudi.fd_di == NULL)
5261 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005262 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005263 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5264 if (fudi.fd_di == NULL)
5265 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005266 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005267 goto erret;
5268 }
5269 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5270 {
5271 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005272 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005273 goto erret;
5274 }
5275 }
5276 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005277 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005278 clear_tv(&fudi.fd_di->di_tv);
5279 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005280 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005281
Bram Moolenaare38eab22019-12-05 21:50:01 +01005282 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005283 flags |= FC_DICT;
5284 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005285 }
5286 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005287 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005288 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005289 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005290
5291 if (eap->cmdidx == CMD_def)
5292 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005293 int lnum_save = SOURCING_LNUM;
5294 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005295
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005296 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005297
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005298 // error messages are for the first function line
5299 SOURCING_LNUM = sourcing_lnum_top;
5300
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005301 // The function may use script variables from the context.
5302 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005303
h-eastb895b0f2023-09-24 15:46:31 +02005304 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5305 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005306 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005307 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005308 free_fp = fp_allocated;
5309 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005310 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005311 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005312
5313 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005314 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005315 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005316 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005317 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005318 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005319 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005320 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005321 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005322 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005323 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005324
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005325 if (fp_allocated)
5326 {
5327 // insert the new function in the function list
5328 set_ufunc_name(fp, name);
5329 if (overwrite)
5330 {
5331 hi = hash_find(&func_hashtab, name);
5332 hi->hi_key = UF2HIKEY(fp);
5333 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005334 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005335 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005336 {
5337 free_fp = TRUE;
5338 goto erret;
5339 }
5340 fp->uf_refcount = 1;
5341 }
5342
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005343 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005344 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005345 if ((flags & FC_CLOSURE) != 0)
5346 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005347 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005348 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005349 }
5350 else
5351 fp->uf_scoped = NULL;
5352
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005353#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005354 if (prof_def_func())
5355 func_do_profile(fp);
5356#endif
5357 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005358 if (sandbox)
5359 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005360 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005361 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005362 fp->uf_flags = flags;
5363 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005364 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005365 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005366 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005367 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005368 if (is_export)
5369 {
5370 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005371 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005372 is_export = FALSE;
5373 }
5374
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005375 if (eap->cmdidx == CMD_def)
5376 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005377 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5378 // :func does not use Vim9 script syntax, even in a Vim9 script file
5379 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005380
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005381 goto ret_free;
5382
5383erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005384 if (fp != NULL)
5385 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005386 // these were set to "newargs" and "default_args", which are cleared
5387 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005388 ga_init(&fp->uf_args);
5389 ga_init(&fp->uf_def_args);
5390 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005391errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005392 ga_clear_strings(&newargs);
5393 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005394 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005395 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005396 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005397 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005398 VIM_CLEAR(fp->uf_va_name);
5399 clear_type_list(&fp->uf_type_list);
5400 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005401 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005402 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005403ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005404 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005405 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005406 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005407 if (name != name_arg)
5408 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005409 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005410 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005411
5412 return fp;
5413}
5414
5415/*
5416 * ":function"
5417 */
5418 void
5419ex_function(exarg_T *eap)
5420{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005421 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005422
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005423 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005424 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005425 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005426}
5427
5428/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005429 * Find a function by name, including "<lambda>123".
5430 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005431 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005432 * Return NULL if not found.
5433 */
5434 ufunc_T *
5435find_func_by_name(char_u *name, compiletype_T *compile_type)
5436{
5437 char_u *arg = name;
5438 char_u *fname;
5439 ufunc_T *ufunc;
5440 int is_global = FALSE;
5441
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005442 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5443 {
5444 *compile_type = CT_PROFILE;
5445 arg = skipwhite(arg + 7);
5446 }
5447 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5448 {
5449 *compile_type = CT_DEBUG;
5450 arg = skipwhite(arg + 5);
5451 }
5452
5453 if (STRNCMP(arg, "<lambda>", 8) == 0)
5454 {
5455 arg += 8;
5456 (void)getdigits(&arg);
5457 fname = vim_strnsave(name, arg - name);
5458 }
5459 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005460 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005461 // First try finding a method in a class, trans_function_name() will
5462 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005463 ufunc = find_class_func(&arg);
5464 if (ufunc != NULL)
5465 return ufunc;
5466
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005467 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005468 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005469 NULL, NULL, NULL, &ufunc);
5470 if (ufunc != NULL)
5471 {
5472 vim_free(fname);
5473 return ufunc;
5474 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005475 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005476 if (fname == NULL)
5477 {
5478 semsg(_(e_invalid_argument_str), name);
5479 return NULL;
5480 }
5481 if (!ends_excmd2(name, arg))
5482 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005483 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005484 emsg(ex_errmsg(e_trailing_characters_str, arg));
5485 return NULL;
5486 }
5487
5488 ufunc = find_func(fname, is_global);
5489 if (ufunc == NULL)
5490 {
5491 char_u *p = untrans_function_name(fname);
5492
5493 if (p != NULL)
5494 // Try again without making it script-local.
5495 ufunc = find_func(p, FALSE);
5496 }
5497 vim_free(fname);
5498 if (ufunc == NULL)
5499 semsg(_(e_cannot_find_function_str), name);
5500 return ufunc;
5501}
5502
5503/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005504 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005505 * be compiled or the one specified by the argument.
5506 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005507 */
5508 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005509ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005510{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005511 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005512 {
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005513 compiletype_T compile_type = CT_NONE;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005514 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005515 if (ufunc != NULL)
5516 {
5517 if (func_needs_compiling(ufunc, compile_type))
5518 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5519 else
5520 smsg(_("Function %s does not need compiling"), eap->arg);
5521 }
5522 }
5523 else
5524 {
5525 long todo = (long)func_hashtab.ht_used;
5526 int changed = func_hashtab.ht_changed;
5527 hashitem_T *hi;
5528
5529 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5530 {
5531 if (!HASHITEM_EMPTY(hi))
5532 {
5533 --todo;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005534 ufunc_T *ufunc = HI2UF(hi);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005535 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5536 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5537 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005538 {
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005539 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5540
5541 if (func_hashtab.ht_changed != changed)
5542 {
5543 // a function has been added or removed, need to start
5544 // over
5545 todo = (long)func_hashtab.ht_used;
5546 changed = func_hashtab.ht_changed;
5547 hi = func_hashtab.ht_array;
5548 --hi;
5549 }
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005550 }
5551 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005552 }
5553 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005554}
5555
5556/*
5557 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5558 * Return 2 if "p" starts with "s:".
5559 * Return 0 otherwise.
5560 */
5561 int
5562eval_fname_script(char_u *p)
5563{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005564 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5565 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005566 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5567 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5568 return 5;
5569 if (p[0] == 's' && p[1] == ':')
5570 return 2;
5571 return 0;
5572}
5573
5574 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005575translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005576{
5577 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005578 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005579 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005580}
5581
5582/*
5583 * Return TRUE when "ufunc" has old-style "..." varargs
5584 * or named varargs "...name: type".
5585 */
5586 int
5587has_varargs(ufunc_T *ufunc)
5588{
5589 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005590}
5591
5592/*
5593 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005594 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005595 */
5596 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005597function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005598{
5599 char_u *nm = name;
5600 char_u *p;
5601 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005602 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005603 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005604
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005605 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5606 if (no_deref)
5607 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005608 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005609 nm = skipwhite(nm);
5610
Bram Moolenaare38eab22019-12-05 21:50:01 +01005611 // Only accept "funcname", "funcname ", "funcname (..." and
5612 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005613 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005614 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005615 vim_free(p);
5616 return n;
5617}
5618
Bram Moolenaar113e1072019-01-20 15:30:40 +01005619#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005620 char_u *
5621get_expanded_name(char_u *name, int check)
5622{
5623 char_u *nm = name;
5624 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005625 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005626
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005627 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005628
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005629 if (p != NULL && *nm == NUL
5630 && (!check || translated_function_exists(p, is_global)))
5631 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005632
5633 vim_free(p);
5634 return NULL;
5635}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005636#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005637
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005638/*
5639 * Function given to ExpandGeneric() to obtain the list of user defined
5640 * function names.
5641 */
5642 char_u *
5643get_user_func_name(expand_T *xp, int idx)
5644{
5645 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005646 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005647 static hashitem_T *hi;
5648 ufunc_T *fp;
5649
5650 if (idx == 0)
5651 {
5652 done = 0;
5653 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005654 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005655 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005656 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005657 {
5658 if (done++ > 0)
5659 ++hi;
5660 while (HASHITEM_EMPTY(hi))
5661 ++hi;
5662 fp = HI2UF(hi);
5663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005664 // don't show dead, dict and lambda functions
5665 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005666 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005667 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005668
5669 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005670 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005671
5672 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005673 if (xp->xp_context != EXPAND_USER_FUNC
5674 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005675 {
5676 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005677 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005678 STRCAT(IObuff, ")");
5679 }
5680 return IObuff;
5681 }
5682 return NULL;
5683}
5684
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005685/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005686 * Make a copy of a function.
5687 * Intended to be used for a function defined on a base class that has a copy
5688 * on the child class.
5689 * The copy has uf_refcount set to one.
5690 * Returns NULL when out of memory.
5691 */
5692 ufunc_T *
5693copy_function(ufunc_T *fp)
5694{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005695 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005696 if (ufunc == NULL)
5697 return NULL;
5698
5699 // Most things can just be copied.
5700 *ufunc = *fp;
5701
5702 ufunc->uf_def_status = UF_TO_BE_COMPILED;
5703 ufunc->uf_dfunc_idx = 0;
5704 ufunc->uf_class = NULL;
5705
5706 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
5707 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
5708
5709 if (ufunc->uf_arg_types != NULL)
5710 {
5711 // "uf_arg_types" is an allocated array, make a copy.
5712 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
5713 if (at != NULL)
5714 {
5715 mch_memmove(at, ufunc->uf_arg_types,
5716 sizeof(type_T *) * ufunc->uf_args.ga_len);
5717 ufunc->uf_arg_types = at;
5718 }
5719 }
5720
5721 // TODO: how about the types themselves? they can be freed when the
5722 // original function is freed:
5723 // type_T **uf_arg_types;
5724 // type_T *uf_ret_type;
5725
Ernie Rael114ec812023-06-04 18:11:35 +01005726 // make uf_type_list empty
5727 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00005728
5729 // TODO: partial_T *uf_partial;
5730
5731 if (ufunc->uf_va_name != NULL)
5732 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
5733
5734 // TODO:
5735 // type_T *uf_va_type;
5736 // type_T *uf_func_type;
5737
5738 ufunc->uf_block_depth = 0;
5739 ufunc->uf_block_ids = NULL;
5740
5741 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
5742
5743 ufunc->uf_refcount = 1;
5744 ufunc->uf_name_exp = NULL;
5745 STRCPY(ufunc->uf_name, fp->uf_name);
5746
5747 return ufunc;
5748}
5749
5750/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005751 * ":delfunction {name}"
5752 */
5753 void
5754ex_delfunction(exarg_T *eap)
5755{
5756 ufunc_T *fp = NULL;
5757 char_u *p;
5758 char_u *name;
5759 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005760 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005761
5762 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005763 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
5764 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005765 vim_free(fudi.fd_newkey);
5766 if (name == NULL)
5767 {
5768 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005769 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005770 return;
5771 }
5772 if (!ends_excmd(*skipwhite(p)))
5773 {
5774 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005775 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005776 return;
5777 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005778 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005779 if (eap->nextcmd != NULL)
5780 *p = NUL;
5781
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005782 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005783 {
5784 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005785 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005786 vim_free(name);
5787 return;
5788 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005789 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005790 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005791 vim_free(name);
5792
5793 if (!eap->skip)
5794 {
5795 if (fp == NULL)
5796 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005797 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005798 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005799 return;
5800 }
5801 if (fp->uf_calls > 0)
5802 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005803 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005804 return;
5805 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005806 if (fp->uf_flags & FC_VIM9)
5807 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005808 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005809 return;
5810 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005811
5812 if (fudi.fd_dict != NULL)
5813 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005814 // Delete the dict item that refers to the function, it will
5815 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00005816 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005817 }
5818 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005819 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005820 // A normal function (not a numbered function or lambda) has a
5821 // refcount of 1 for the entry in the hashtable. When deleting
5822 // it and the refcount is more than one, it should be kept.
5823 // A numbered function and lambda should be kept if the refcount is
5824 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005825 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005826 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005827 // Function is still referenced somewhere. Don't free it but
5828 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005829 if (func_remove(fp))
5830 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005831 }
5832 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005833 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005834 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005835 }
5836}
5837
5838/*
5839 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005840 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005841 */
5842 void
5843func_unref(char_u *name)
5844{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005845 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005846
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005847 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005848 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005849 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005850 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005851 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005852#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005853 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005854#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005855 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005856 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005857 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005858}
5859
5860/*
5861 * Unreference a Function: decrement the reference count and free it when it
5862 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005863 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005864 */
5865 void
5866func_ptr_unref(ufunc_T *fp)
5867{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005868 if (fp != NULL && (--fp->uf_refcount <= 0
5869 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5870 && fp->uf_partial->pt_refcount <= 1
5871 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005872 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005873 // Only delete it when it's not being used. Otherwise it's done
5874 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005875 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005876 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005877 }
5878}
5879
5880/*
5881 * Count a reference to a Function.
5882 */
5883 void
5884func_ref(char_u *name)
5885{
5886 ufunc_T *fp;
5887
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005888 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005889 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005890 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005891 if (fp != NULL)
5892 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005893 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005894 // Only give an error for a numbered function.
5895 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005896 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005897}
5898
5899/*
5900 * Count a reference to a Function.
5901 */
5902 void
5903func_ptr_ref(ufunc_T *fp)
5904{
5905 if (fp != NULL)
5906 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005907}
5908
5909/*
5910 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5911 * referenced from anywhere that is in use.
5912 */
5913 static int
5914can_free_funccal(funccall_T *fc, int copyID)
5915{
Bram Moolenaarca16c602022-09-06 18:57:08 +01005916 return (fc->fc_l_varlist.lv_copyID != copyID
5917 && fc->fc_l_vars.dv_copyID != copyID
5918 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005919 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005920}
5921
5922/*
5923 * ":return [expr]"
5924 */
5925 void
5926ex_return(exarg_T *eap)
5927{
5928 char_u *arg = eap->arg;
5929 typval_T rettv;
5930 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005931 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005932
5933 if (current_funccal == NULL)
5934 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005935 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005936 return;
5937 }
5938
Bram Moolenaar844fb642021-10-23 13:32:30 +01005939 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005940 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5941
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005942 if (eap->skip)
5943 ++emsg_skip;
5944
5945 eap->nextcmd = NULL;
5946 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005947 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005948 {
5949 if (!eap->skip)
5950 returning = do_return(eap, FALSE, TRUE, &rettv);
5951 else
5952 clear_tv(&rettv);
5953 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005954 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005955 else if (!eap->skip)
5956 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005957 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005958 update_force_abort();
5959
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005960 /*
5961 * Return unless the expression evaluation has been cancelled due to an
5962 * aborting error, an interrupt, or an exception.
5963 */
5964 if (!aborting())
5965 returning = do_return(eap, FALSE, TRUE, NULL);
5966 }
5967
Bram Moolenaare38eab22019-12-05 21:50:01 +01005968 // When skipping or the return gets pending, advance to the next command
5969 // in this line (!returning). Otherwise, ignore the rest of the line.
5970 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005971 if (returning)
5972 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005973 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02005974 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005975
5976 if (eap->skip)
5977 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02005978 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005979}
5980
zeertzjq5299c092023-04-12 20:48:16 +01005981/*
5982 * Lower level implementation of "call". Only called when not skipping.
5983 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005984 static int
5985ex_call_inner(
5986 exarg_T *eap,
5987 char_u *name,
5988 char_u **arg,
5989 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01005990 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005991 evalarg_T *evalarg)
5992{
5993 linenr_T lnum;
5994 int doesrange;
5995 typval_T rettv;
5996 int failed = FALSE;
5997
zeertzjq5299c092023-04-12 20:48:16 +01005998 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005999 for ( ; lnum <= eap->line2; ++lnum)
6000 {
6001 funcexe_T funcexe;
6002
zeertzjq5299c092023-04-12 20:48:16 +01006003 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006004 {
6005 if (lnum > curbuf->b_ml.ml_line_count)
6006 {
6007 // If the function deleted lines or switched to another buffer
6008 // the line number may become invalid.
6009 emsg(_(e_invalid_range));
6010 break;
6011 }
6012 curwin->w_cursor.lnum = lnum;
6013 curwin->w_cursor.col = 0;
6014 curwin->w_cursor.coladd = 0;
6015 }
6016 *arg = startarg;
6017
6018 funcexe = *funcexe_init;
6019 funcexe.fe_doesrange = &doesrange;
6020 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6021 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6022 {
6023 failed = TRUE;
6024 break;
6025 }
6026 if (has_watchexpr())
6027 dbg_check_breakpoint(eap);
6028
6029 // Handle a function returning a Funcref, Dictionary or List.
6030 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006031 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006032 {
6033 failed = TRUE;
6034 break;
6035 }
6036
6037 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006038 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006039 break;
6040
6041 // Stop when immediately aborting on error, or when an interrupt
6042 // occurred or an exception was thrown but not caught.
6043 // get_func_tv() returned OK, so that the check for trailing
6044 // characters below is executed.
6045 if (aborting())
6046 break;
6047 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006048 return failed;
6049}
6050
6051/*
6052 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6053 * Returns FAIL or OK.
6054 */
6055 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006056ex_defer_inner(
6057 char_u *name,
6058 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006059 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006060 partial_T *partial,
6061 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006062{
6063 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006064 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006065 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006066 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006067
6068 if (current_funccal == NULL)
6069 {
6070 semsg(_(e_str_not_inside_function), "defer");
6071 return FAIL;
6072 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006073 if (partial != NULL)
6074 {
6075 if (partial->pt_dict != NULL)
6076 {
6077 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6078 return FAIL;
6079 }
6080 if (partial->pt_argc > 0)
6081 {
6082 int i;
6083
6084 partial_argc = partial->pt_argc;
6085 for (i = 0; i < partial_argc; ++i)
6086 copy_tv(&partial->pt_argv[i], &argvars[i]);
6087 }
6088 }
6089 r = get_func_arguments(arg, evalarg, FALSE,
6090 argvars + partial_argc, &argcount);
6091 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006092
6093 if (r == OK)
6094 {
6095 if (type != NULL)
6096 {
6097 // Check that the arguments are OK for the types of the funcref.
6098 r = check_argument_types(type, argvars, argcount, NULL, name);
6099 }
6100 else if (builtin_function(name, -1))
6101 {
6102 int idx = find_internal_func(name);
6103
6104 if (idx < 0)
6105 {
6106 emsg_funcname(e_unknown_function_str, name);
6107 r = FAIL;
6108 }
6109 else if (check_internal_func(idx, argcount) == -1)
6110 r = FAIL;
6111 }
6112 else
6113 {
6114 ufunc_T *ufunc = find_func(name, FALSE);
6115
6116 // we tolerate an unknown function here, it might be defined later
6117 if (ufunc != NULL)
6118 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006119 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006120 if (error != FCERR_UNKNOWN)
6121 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006122 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006123 r = FAIL;
6124 }
6125 }
6126 }
6127 }
6128
Bram Moolenaar86d87252022-09-05 21:21:25 +01006129 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006130 {
6131 while (--argcount >= 0)
6132 clear_tv(&argvars[argcount]);
6133 return FAIL;
6134 }
6135 return add_defer(name, argcount, argvars);
6136}
6137
6138/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006139 * Return TRUE if currently inside a function call.
6140 * Give an error message and return FALSE when not.
6141 */
6142 int
6143can_add_defer(void)
6144{
6145 if (!in_def_function() && get_current_funccal() == NULL)
6146 {
6147 semsg(_(e_str_not_inside_function), "defer");
6148 return FALSE;
6149 }
6150 return TRUE;
6151}
6152
6153/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006154 * Add a deferred call for "name" with arguments "argvars[argcount]".
6155 * Consumes "argvars[]".
6156 * Caller must check that in_def_function() returns TRUE or current_funccal is
6157 * not NULL.
6158 * Returns OK or FAIL.
6159 */
6160 int
6161add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6162{
6163 char_u *saved_name = vim_strsave(name);
6164 int argcount = argcount_arg;
6165 defer_T *dr;
6166 int ret = FAIL;
6167
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006168 if (saved_name == NULL)
6169 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006170 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006171 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006172 if (add_defer_function(saved_name, argcount, argvars) == OK)
6173 argcount = 0;
6174 }
6175 else
6176 {
6177 if (current_funccal->fc_defer.ga_itemsize == 0)
6178 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6179 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6180 goto theend;
6181 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6182 + current_funccal->fc_defer.ga_len++;
6183 dr->dr_name = saved_name;
6184 dr->dr_argcount = argcount;
6185 while (argcount > 0)
6186 {
6187 --argcount;
6188 dr->dr_argvars[argcount] = argvars[argcount];
6189 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006190 }
6191 ret = OK;
6192
6193theend:
6194 while (--argcount >= 0)
6195 clear_tv(&argvars[argcount]);
6196 return ret;
6197}
6198
6199/*
6200 * Invoked after a functions has finished: invoke ":defer" functions.
6201 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006202 static void
6203handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006204{
6205 int idx;
6206
Bram Moolenaar58779852022-09-06 18:31:14 +01006207 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006208 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006209 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006210
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006211 if (dr->dr_name == NULL)
6212 // already being called, can happen if function does ":qa"
6213 continue;
6214
6215 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006216 CLEAR_FIELD(funcexe);
6217 funcexe.fe_evaluate = TRUE;
6218
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006219 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006220 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006221
6222 char_u *name = dr->dr_name;
6223 dr->dr_name = NULL;
6224
6225 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6226
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006227 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006228 vim_free(name);
6229 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006230 clear_tv(&dr->dr_argvars[i]);
6231 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006232 ga_clear(&funccal->fc_defer);
6233}
6234
zeertzjq960cf912023-04-18 21:52:54 +01006235 static void
6236invoke_funccall_defer(funccall_T *fc)
6237{
6238 if (fc->fc_ectx != NULL)
6239 {
6240 // :def function
6241 unwind_def_callstack(fc->fc_ectx);
6242 may_invoke_defer_funcs(fc->fc_ectx);
6243 }
6244 else
6245 {
6246 // legacy function
6247 handle_defer_one(fc);
6248 }
6249}
6250
Bram Moolenaar58779852022-09-06 18:31:14 +01006251/*
6252 * Called when exiting: call all defer functions.
6253 */
6254 void
6255invoke_all_defer(void)
6256{
zeertzjq1be4b812023-04-19 14:21:24 +01006257 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6258 invoke_funccall_defer(fc);
6259
zeertzjq960cf912023-04-18 21:52:54 +01006260 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6261 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6262 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006263}
6264
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006265/*
6266 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006267 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006268 */
6269 void
6270ex_call(exarg_T *eap)
6271{
6272 char_u *arg = eap->arg;
6273 char_u *startarg;
6274 char_u *name;
6275 char_u *tofree;
6276 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006277 int failed = FALSE;
6278 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006279 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006280 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006281 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006282 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006283 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006284 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006285
Bram Moolenaare6b53242020-07-01 17:28:33 +02006286 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006287 if (eap->skip)
6288 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006289 typval_T rettv;
6290
Bram Moolenaare38eab22019-12-05 21:50:01 +01006291 // trans_function_name() doesn't work well when skipping, use eval0()
6292 // instead to skip to any following command, e.g. for:
6293 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006294 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006295 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006296 clear_tv(&rettv);
6297 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006298 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006299 return;
6300 }
6301
zeertzjq5299c092023-04-12 20:48:16 +01006302 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006303 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006304 if (fudi.fd_newkey != NULL)
6305 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006306 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006307 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006308 vim_free(fudi.fd_newkey);
6309 }
6310 if (tofree == NULL)
6311 return;
6312
Bram Moolenaare38eab22019-12-05 21:50:01 +01006313 // Increase refcount on dictionary, it could get deleted when evaluating
6314 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006315 if (fudi.fd_dict != NULL)
6316 ++fudi.fd_dict->dv_refcount;
6317
Bram Moolenaare38eab22019-12-05 21:50:01 +01006318 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6319 // contents. For VAR_PARTIAL get its partial, unless we already have one
6320 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006321 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006322 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006323 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006324 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006325
Bram Moolenaare38eab22019-12-05 21:50:01 +01006326 // Skip white space to allow ":call func ()". Not good, but required for
6327 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006328 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006329 if (*startarg != '(')
6330 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006331 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006332 goto end;
6333 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006334 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006335 {
6336 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6337 goto end;
6338 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006339
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006340 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006341 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006342 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006343 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006344 }
6345 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006346 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006347 funcexe_T funcexe;
6348
Bram Moolenaara80faa82020-04-12 19:37:17 +02006349 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006350 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006351 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006352 funcexe.fe_partial = partial;
6353 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006354 funcexe.fe_firstline = eap->line1;
6355 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006356 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006357 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006358 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006359 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006360
Bram Moolenaar1d341892021-09-18 15:25:52 +02006361 // When inside :try we need to check for following "| catch" or "| endtry".
6362 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006363 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006364 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006365 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006366 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006367 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006368 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006369 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006370 {
6371 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006372 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006373 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006374 }
6375 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006376 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006377 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006378 // Must be after using "arg", it may point into memory cleared here.
6379 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006380
6381end:
6382 dict_unref(fudi.fd_dict);
6383 vim_free(tofree);
6384}
6385
6386/*
6387 * Return from a function. Possibly makes the return pending. Also called
6388 * for a pending return at the ":endtry" or after returning from an extra
6389 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6390 * when called due to a ":return" command. "rettv" may point to a typval_T
6391 * with the return rettv. Returns TRUE when the return can be carried out,
6392 * FALSE when the return gets pending.
6393 */
6394 int
6395do_return(
6396 exarg_T *eap,
6397 int reanimate,
6398 int is_cmd,
6399 void *rettv)
6400{
6401 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006402 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006403
6404 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006405 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006406 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006407
6408 /*
6409 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6410 * not in its finally clause (which then is to be executed next) is found.
6411 * In this case, make the ":return" pending for execution at the ":endtry".
6412 * Otherwise, return normally.
6413 */
6414 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6415 if (idx >= 0)
6416 {
6417 cstack->cs_pending[idx] = CSTP_RETURN;
6418
6419 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006420 // A pending return again gets pending. "rettv" points to an
6421 // allocated variable with the rettv of the original ":return"'s
6422 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006423 cstack->cs_rettv[idx] = rettv;
6424 else
6425 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006426 // When undoing a return in order to make it pending, get the stored
6427 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006428 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006429 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006430
6431 if (rettv != NULL)
6432 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006433 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006434 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6435 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6436 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006437 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006438 }
6439 else
6440 cstack->cs_rettv[idx] = NULL;
6441
6442 if (reanimate)
6443 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006444 // The pending return value could be overwritten by a ":return"
6445 // without argument in a finally clause; reset the default
6446 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006447 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6448 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006449 }
6450 }
6451 report_make_pending(CSTP_RETURN, rettv);
6452 }
6453 else
6454 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006455 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006456
Bram Moolenaare38eab22019-12-05 21:50:01 +01006457 // If the return is carried out now, store the return value. For
6458 // a return immediately after reanimation, the value is already
6459 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006460 if (!reanimate && rettv != NULL)
6461 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006462 clear_tv(current_funccal->fc_rettv);
6463 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006464 if (!is_cmd)
6465 vim_free(rettv);
6466 }
6467 }
6468
6469 return idx < 0;
6470}
6471
6472/*
6473 * Free the variable with a pending return value.
6474 */
6475 void
6476discard_pending_return(void *rettv)
6477{
6478 free_tv((typval_T *)rettv);
6479}
6480
6481/*
6482 * Generate a return command for producing the value of "rettv". The result
6483 * is an allocated string. Used by report_pending() for verbose messages.
6484 */
6485 char_u *
6486get_return_cmd(void *rettv)
6487{
6488 char_u *s = NULL;
6489 char_u *tofree = NULL;
6490 char_u numbuf[NUMBUFLEN];
6491
6492 if (rettv != NULL)
6493 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6494 if (s == NULL)
6495 s = (char_u *)"";
6496
6497 STRCPY(IObuff, ":return ");
6498 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6499 if (STRLEN(s) + 8 >= IOSIZE)
6500 STRCPY(IObuff + IOSIZE - 4, "...");
6501 vim_free(tofree);
6502 return vim_strsave(IObuff);
6503}
6504
6505/*
6506 * Get next function line.
6507 * Called by do_cmdline() to get the next line.
6508 * Returns allocated string, or NULL for end of function.
6509 */
6510 char_u *
6511get_func_line(
6512 int c UNUSED,
6513 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006514 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006515 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006516{
6517 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006518 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006519 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006520 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006521
Bram Moolenaare38eab22019-12-05 21:50:01 +01006522 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006523 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006524 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006525 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006526 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006527 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006528 }
6529#ifdef FEAT_PROFILE
6530 if (do_profiling == PROF_YES)
6531 func_line_end(cookie);
6532#endif
6533
6534 gap = &fp->uf_lines;
6535 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006536 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006537 retval = NULL;
6538 else
6539 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006540 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006541 while (fcp->fc_linenr < gap->ga_len
6542 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6543 ++fcp->fc_linenr;
6544 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006545 retval = NULL;
6546 else
6547 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006548 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6549 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006550#ifdef FEAT_PROFILE
6551 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006552 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006553#endif
6554 }
6555 }
6556
Bram Moolenaare38eab22019-12-05 21:50:01 +01006557 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006558 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006559 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006560 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006561 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006562 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006563 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006564 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006565 }
6566
6567 return retval;
6568}
6569
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006570/*
6571 * Return TRUE if the currently active function should be ended, because a
6572 * return was encountered or an error occurred. Used inside a ":while".
6573 */
6574 int
6575func_has_ended(void *cookie)
6576{
6577 funccall_T *fcp = (funccall_T *)cookie;
6578
Bram Moolenaare38eab22019-12-05 21:50:01 +01006579 // Ignore the "abort" flag if the abortion behavior has been changed due to
6580 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006581 return (((fcp->fc_func->uf_flags & FC_ABORT)
6582 && did_emsg && !aborted_in_try())
6583 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006584}
6585
6586/*
6587 * return TRUE if cookie indicates a function which "abort"s on errors.
6588 */
6589 int
6590func_has_abort(
6591 void *cookie)
6592{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006593 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006594}
6595
6596
6597/*
6598 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6599 * Don't do this when "Func" is already a partial that was bound
6600 * explicitly (pt_auto is FALSE).
6601 * Changes "rettv" in-place.
6602 * Returns the updated "selfdict_in".
6603 */
6604 dict_T *
6605make_partial(dict_T *selfdict_in, typval_T *rettv)
6606{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006607 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006608 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006609 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006610 dict_T *selfdict = selfdict_in;
6611
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006612 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6613 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006614 fp = rettv->vval.v_partial->pt_func;
6615 else
6616 {
6617 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006618 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006619 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006620 if (fname == NULL)
6621 {
6622 // There is no point binding a dict to a NULL function, just create
6623 // a function reference.
6624 rettv->v_type = VAR_FUNC;
6625 rettv->vval.v_string = NULL;
6626 }
6627 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006628 {
6629 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006630 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006631
6632 // Translate "s:func" to the stored function name.
6633 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6634 fp = find_func(fname, FALSE);
6635 vim_free(tofree);
6636 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006637 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006638
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006639 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006640 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006641 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006642
6643 if (pt != NULL)
6644 {
6645 pt->pt_refcount = 1;
6646 pt->pt_dict = selfdict;
6647 pt->pt_auto = TRUE;
6648 selfdict = NULL;
6649 if (rettv->v_type == VAR_FUNC)
6650 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006651 // Just a function: Take over the function name and use
6652 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006653 pt->pt_name = rettv->vval.v_string;
6654 }
6655 else
6656 {
6657 partial_T *ret_pt = rettv->vval.v_partial;
6658 int i;
6659
Bram Moolenaare38eab22019-12-05 21:50:01 +01006660 // Partial: copy the function name, use selfdict and copy
6661 // args. Can't take over name or args, the partial might
6662 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006663 if (ret_pt->pt_name != NULL)
6664 {
6665 pt->pt_name = vim_strsave(ret_pt->pt_name);
6666 func_ref(pt->pt_name);
6667 }
6668 else
6669 {
6670 pt->pt_func = ret_pt->pt_func;
6671 func_ptr_ref(pt->pt_func);
6672 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006673 if (ret_pt->pt_argc > 0)
6674 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006675 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006676 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006677 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006678 pt->pt_argc = 0;
6679 else
6680 {
6681 pt->pt_argc = ret_pt->pt_argc;
6682 for (i = 0; i < pt->pt_argc; i++)
6683 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6684 }
6685 }
6686 partial_unref(ret_pt);
6687 }
6688 rettv->v_type = VAR_PARTIAL;
6689 rettv->vval.v_partial = pt;
6690 }
6691 }
6692 return selfdict;
6693}
6694
6695/*
6696 * Return the name of the executed function.
6697 */
6698 char_u *
6699func_name(void *cookie)
6700{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006701 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006702}
6703
6704/*
6705 * Return the address holding the next breakpoint line for a funccall cookie.
6706 */
6707 linenr_T *
6708func_breakpoint(void *cookie)
6709{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006710 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006711}
6712
6713/*
6714 * Return the address holding the debug tick for a funccall cookie.
6715 */
6716 int *
6717func_dbg_tick(void *cookie)
6718{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006719 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006720}
6721
6722/*
6723 * Return the nesting level for a funccall cookie.
6724 */
6725 int
6726func_level(void *cookie)
6727{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006728 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006729}
6730
6731/*
6732 * Return TRUE when a function was ended by a ":return" command.
6733 */
6734 int
6735current_func_returned(void)
6736{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006737 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006738}
6739
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006740 int
6741free_unref_funccal(int copyID, int testing)
6742{
6743 int did_free = FALSE;
6744 int did_free_funccal = FALSE;
6745 funccall_T *fc, **pfc;
6746
6747 for (pfc = &previous_funccal; *pfc != NULL; )
6748 {
6749 if (can_free_funccal(*pfc, copyID))
6750 {
6751 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006752 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006753 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006754 did_free = TRUE;
6755 did_free_funccal = TRUE;
6756 }
6757 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006758 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006759 }
6760 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006761 // When a funccal was freed some more items might be garbage
6762 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006763 (void)garbage_collect(testing);
6764
6765 return did_free;
6766}
6767
6768/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006769 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006770 */
6771 static funccall_T *
6772get_funccal(void)
6773{
6774 int i;
6775 funccall_T *funccal;
6776 funccall_T *temp_funccal;
6777
6778 funccal = current_funccal;
6779 if (debug_backtrace_level > 0)
6780 {
6781 for (i = 0; i < debug_backtrace_level; i++)
6782 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006783 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006784 if (temp_funccal)
6785 funccal = temp_funccal;
6786 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006787 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006788 debug_backtrace_level = i;
6789 }
6790 }
6791 return funccal;
6792}
6793
6794/*
6795 * Return the hashtable used for local variables in the current funccal.
6796 * Return NULL if there is no current funccal.
6797 */
6798 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006799get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006800{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006801 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006802 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006803 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006804}
6805
6806/*
6807 * Return the l: scope variable.
6808 * Return NULL if there is no current funccal.
6809 */
6810 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006811get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006812{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006813 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006814 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006815 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006816}
6817
6818/*
6819 * Return the hashtable used for argument in the current funccal.
6820 * Return NULL if there is no current funccal.
6821 */
6822 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006823get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006824{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006825 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006826 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006827 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006828}
6829
6830/*
6831 * Return the a: scope variable.
6832 * Return NULL if there is no current funccal.
6833 */
6834 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006835get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006836{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006837 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006838 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006839 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006840}
6841
6842/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006843 * List function variables, if there is a function.
6844 */
6845 void
6846list_func_vars(int *first)
6847{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006848 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
6849 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006850 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006851}
6852
6853/*
6854 * If "ht" is the hashtable for local variables in the current funccal, return
6855 * the dict that contains it.
6856 * Otherwise return NULL.
6857 */
6858 dict_T *
6859get_current_funccal_dict(hashtab_T *ht)
6860{
6861 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01006862 && ht == &current_funccal->fc_l_vars.dv_hashtab)
6863 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006864 return NULL;
6865}
6866
6867/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006868 * Search hashitem in parent scope.
6869 */
6870 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006871find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006872{
6873 funccall_T *old_current_funccal = current_funccal;
6874 hashtab_T *ht;
6875 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006876 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006877
Bram Moolenaarca16c602022-09-06 18:57:08 +01006878 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006879 return NULL;
6880
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006881 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006882 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006883 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006884 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006885 ht = find_var_ht(name, &varname);
6886 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006887 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006888 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006889 if (!HASHITEM_EMPTY(hi))
6890 {
6891 *pht = ht;
6892 break;
6893 }
6894 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006895 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02006896 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006897 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006898 }
6899 current_funccal = old_current_funccal;
6900
6901 return hi;
6902}
6903
6904/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006905 * Search variable in parent scope.
6906 */
6907 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006908find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006909{
6910 dictitem_T *v = NULL;
6911 funccall_T *old_current_funccal = current_funccal;
6912 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006913 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006914
Bram Moolenaarca16c602022-09-06 18:57:08 +01006915 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006916 return NULL;
6917
Bram Moolenaare38eab22019-12-05 21:50:01 +01006918 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01006919 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006920 while (current_funccal)
6921 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006922 ht = find_var_ht(name, &varname);
6923 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006924 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006925 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006926 if (v != NULL)
6927 break;
6928 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006929 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006930 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006931 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006932 }
6933 current_funccal = old_current_funccal;
6934
6935 return v;
6936}
6937
6938/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006939 * Set "copyID + 1" in previous_funccal and callers.
6940 */
6941 int
6942set_ref_in_previous_funccal(int copyID)
6943{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006944 funccall_T *fc;
6945
Bram Moolenaarca16c602022-09-06 18:57:08 +01006946 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006947 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006948 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006949 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
6950 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
6951 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006952 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006953 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006954 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006955}
6956
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006957 static int
6958set_ref_in_funccal(funccall_T *fc, int copyID)
6959{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006960 if (fc->fc_copyID != copyID)
6961 {
6962 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006963 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
6964 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
6965 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
6966 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006967 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006968 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006969 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006970}
6971
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006972/*
6973 * Set "copyID" in all local vars and arguments in the call stack.
6974 */
6975 int
6976set_ref_in_call_stack(int copyID)
6977{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006978 funccall_T *fc;
6979 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006980
Bram Moolenaarca16c602022-09-06 18:57:08 +01006981 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006982 if (set_ref_in_funccal(fc, copyID))
6983 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006984
6985 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006986 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006987 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006988 if (set_ref_in_funccal(fc, copyID))
6989 return TRUE;
6990 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006991}
6992
6993/*
6994 * Set "copyID" in all functions available by name.
6995 */
6996 int
6997set_ref_in_functions(int copyID)
6998{
6999 int todo;
7000 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007001 ufunc_T *fp;
7002
7003 todo = (int)func_hashtab.ht_used;
7004 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007005 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007006 if (!HASHITEM_EMPTY(hi))
7007 {
7008 --todo;
7009 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007010 if (!func_name_refcount(fp->uf_name)
7011 && set_ref_in_func(NULL, fp, copyID))
7012 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007013 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007014 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007015 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007016}
7017
7018/*
7019 * Set "copyID" in all function arguments.
7020 */
7021 int
7022set_ref_in_func_args(int copyID)
7023{
7024 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007025
7026 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007027 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7028 copyID, NULL, NULL))
7029 return TRUE;
7030 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007031}
7032
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007033/*
7034 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007035 * Returns TRUE if setting references failed somehow.
7036 */
7037 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007038set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007039{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007040 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007041 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007042 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007043 char_u fname_buf[FLEN_FIXED + 1];
7044 char_u *tofree = NULL;
7045 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007046 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007047
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007048 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007049 return FALSE;
7050
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007051 if (fp_in == NULL)
7052 {
7053 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007054 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007055 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007056 if (fp != NULL)
7057 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007058 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007059 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007060 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007061
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007062 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007063 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007064}
7065
Bram Moolenaare38eab22019-12-05 21:50:01 +01007066#endif // FEAT_EVAL