blob: a27ff984d5358217637d6c99012b9fa49614ca05 [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)
1724 ga_clear_strings(&argtypes);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001725
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001726 return OK;
1727
1728errret:
1729 ga_clear_strings(&newargs);
1730 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001731 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001732 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001733 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001734 ga_clear_strings(&argtypes);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001735 if (fp != NULL)
1736 vim_free(fp->uf_arg_types);
1737 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001738 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001739 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001740 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001741 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001742 return FAIL;
1743}
1744
1745/*
1746 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1747 * name it contains, otherwise return "name".
1748 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1749 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001750 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1751 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001752 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001753 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001754 */
1755 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001756deref_func_name(
1757 char_u *name,
1758 int *lenp,
1759 partial_T **partialp,
1760 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001761 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001762 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001763 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001764{
1765 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001766 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001767 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001768 char_u *s = NULL;
1769 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001770 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001771
1772 if (partialp != NULL)
1773 *partialp = NULL;
1774
1775 cc = name[*lenp];
1776 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001777
Bram Moolenaar71f21932022-01-07 18:20:55 +00001778 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001779 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001780 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001781 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001782 tv = &v->di_tv;
1783 }
1784 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1785 {
1786 imported_T *import;
1787 char_u *p = name;
1788 int len = *lenp;
1789
1790 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001791 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001792 p = name + 2;
1793 len -= 2;
1794 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001795 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001796
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001797 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001798 if (import != NULL)
1799 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001800 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001801 if (new_function)
1802 semsg(_(e_redefining_imported_item_str), name);
1803 else
1804 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001805 name[len] = cc;
1806 *lenp = 0;
1807 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001808 }
1809 }
1810
1811 if (tv != NULL)
1812 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001813 if (found_var != NULL)
1814 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001815 if (tv->v_type == VAR_FUNC)
1816 {
1817 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001818 {
1819 *lenp = 0;
1820 return (char_u *)""; // just in case
1821 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001822 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001823 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001824 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001825
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001826 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001827 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001828 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001829
1830 if (pt == NULL)
1831 {
1832 *lenp = 0;
1833 return (char_u *)""; // just in case
1834 }
1835 if (partialp != NULL)
1836 *partialp = pt;
1837 s = partial_name(pt);
1838 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001839 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001840
1841 if (s != NULL)
1842 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001843 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001844 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001845 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001846
1847 if (sv != NULL)
1848 *type = sv->sv_type;
1849 }
1850 return s;
1851 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001852 }
1853
1854 return name;
1855}
1856
1857/*
1858 * Give an error message with a function name. Handle <SNR> things.
1859 * "ermsg" is to be passed without translation, use N_() instead of _().
1860 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001861 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001862emsg_funcname(char *ermsg, char_u *name)
1863{
Bram Moolenaar54598062022-01-13 12:05:09 +00001864 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001865
Bram Moolenaar54598062022-01-13 12:05:09 +00001866 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001867 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001868 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001869 if (p != name)
1870 vim_free(p);
1871}
1872
1873/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001874 * Get function arguments at "*arg" and advance it.
1875 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001876 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001877 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001878 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001879get_func_arguments(
1880 char_u **arg,
1881 evalarg_T *evalarg,
1882 int partial_argc,
1883 typval_T *argvars,
1884 int *argcount)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001885{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001886 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001887 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001888 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001889 int evaluate = evalarg == NULL
1890 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001891
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001892 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001893 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001894 // skip the '(' or ',' and possibly line breaks
1895 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1896
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001897 if (*argp == ')' || *argp == ',' || *argp == NUL)
1898 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001899 if (eval1(&argp, &argvars[*argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001900 {
1901 ret = FAIL;
1902 break;
1903 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001904 ++*argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001905 // The comma should come right after the argument, but this wasn't
1906 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001907 if (vim9script)
1908 {
1909 if (*argp != ',' && *skipwhite(argp) == ',')
1910 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001911 if (evaluate)
1912 semsg(_(e_no_white_space_allowed_before_str_str),
1913 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001914 ret = FAIL;
1915 break;
1916 }
1917 }
1918 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001919 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001920 if (*argp != ',')
1921 break;
Christian Brabandt00cb2472023-09-05 20:46:25 +02001922 if (vim9script && !IS_WHITE_NL_OR_NUL(argp[1]))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001923 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001924 if (evaluate)
1925 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001926 ret = FAIL;
1927 break;
1928 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001929 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001930
Bram Moolenaare6b53242020-07-01 17:28:33 +02001931 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001932 if (*argp == ')')
1933 ++argp;
1934 else
1935 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001936 *arg = argp;
1937 return ret;
1938}
1939
1940/*
1941 * Call a function and put the result in "rettv".
1942 * Return OK or FAIL.
1943 */
1944 int
1945get_func_tv(
1946 char_u *name, // name of the function
1947 int len, // length of "name" or -1 to use strlen()
1948 typval_T *rettv,
1949 char_u **arg, // argument, pointing to the '('
1950 evalarg_T *evalarg, // for line continuation
1951 funcexe_T *funcexe) // various values
1952{
1953 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001954 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001955 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1956 int argcount = 0; // number of arguments found
1957 int vim9script = in_vim9script();
1958 int evaluate = evalarg == NULL
1959 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
1960
1961 argp = *arg;
1962 ret = get_func_arguments(&argp, evalarg,
1963 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
1964 argvars, &argcount);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001965
1966 if (ret == OK)
1967 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001968 int i = 0;
1969 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001970
1971 if (get_vim_var_nr(VV_TESTING))
1972 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001973 // Prepare for calling test_garbagecollect_now(), need to know
1974 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001975 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001976 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001977 for (i = 0; i < argcount; ++i)
1978 if (ga_grow(&funcargs, 1) == OK)
1979 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1980 &argvars[i];
1981 }
1982
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001983 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00001984 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001985 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001986 // An error in a builtin function does not return FAIL, but we do
1987 // want to abort further processing if an error was given.
1988 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001989 clear_tv(rettv);
1990 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001991
1992 funcargs.ga_len -= i;
1993 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001994 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001995 {
1996 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001997 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001998 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001999 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002000 }
2001
2002 while (--argcount >= 0)
2003 clear_tv(&argvars[argcount]);
2004
Bram Moolenaar4525a572022-02-13 11:57:33 +00002005 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02002006 *arg = argp;
2007 else
2008 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002009 return ret;
2010}
2011
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002012/*
2013 * Return TRUE if "p" starts with "<SID>" or "s:".
2014 * Only works if eval_fname_script() returned non-zero for "p"!
2015 */
2016 static int
2017eval_fname_sid(char_u *p)
2018{
2019 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
2020}
2021
2022/*
2023 * In a script change <SID>name() and s:name() to K_SNR 123_name().
2024 * Change <SNR>123_name() to K_SNR 123_name().
2025 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002026 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002027 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002028 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00002029fname_trans_sid(
2030 char_u *name,
2031 char_u *fname_buf,
2032 char_u **tofree,
2033 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002034{
2035 int llen;
2036 char_u *fname;
2037 int i;
2038
2039 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002040 if (llen == 0)
2041 return name; // no prefix
2042
2043 fname_buf[0] = K_SPECIAL;
2044 fname_buf[1] = KS_EXTRA;
2045 fname_buf[2] = (int)KE_SNR;
2046 i = 3;
2047 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002048 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002049 if (current_sctx.sc_sid <= 0)
2050 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002051 else
2052 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002053 sprintf((char *)fname_buf + 3, "%ld_",
2054 (long)current_sctx.sc_sid);
2055 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002056 }
2057 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002058 if (i + STRLEN(name + llen) < FLEN_FIXED)
2059 {
2060 STRCPY(fname_buf + i, name + llen);
2061 fname = fname_buf;
2062 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002063 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002064 {
2065 fname = alloc(i + STRLEN(name + llen) + 1);
2066 if (fname == NULL)
2067 *error = FCERR_OTHER;
2068 else
2069 {
2070 *tofree = fname;
2071 mch_memmove(fname, fname_buf, (size_t)i);
2072 STRCPY(fname + i, name + llen);
2073 }
2074 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002075 return fname;
2076}
2077
2078/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002079 * Concatenate the script ID and function name into "<SNR>99_name".
2080 * "buffer" must have size MAX_FUNC_NAME_LEN.
2081 */
2082 void
2083func_name_with_sid(char_u *name, int sid, char_u *buffer)
2084{
2085 // A script-local function is stored as "<SNR>99_name".
2086 buffer[0] = K_SPECIAL;
2087 buffer[1] = KS_EXTRA;
2088 buffer[2] = (int)KE_SNR;
2089 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2090 (long)sid, name);
2091}
2092
2093/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002094 * Find a function "name" in script "sid".
2095 */
2096 static ufunc_T *
2097find_func_with_sid(char_u *name, int sid)
2098{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002099 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002100 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002101
Bram Moolenaarb8822442022-01-11 15:24:05 +00002102 if (!SCRIPT_ID_VALID(sid))
2103 return NULL; // not in a script
2104
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002105 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002106 hi = hash_find(&func_hashtab, buffer);
2107 if (!HASHITEM_EMPTY(hi))
2108 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002109 return NULL;
2110}
2111
2112/*
2113 * Find a function "name" in script "sid" prefixing the autoload prefix.
2114 */
2115 static ufunc_T *
2116find_func_with_prefix(char_u *name, int sid)
2117{
2118 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002119 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002120 scriptitem_T *si;
2121
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002122 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002123 return NULL; // already has the prefix
2124 if (!SCRIPT_ID_VALID(sid))
2125 return NULL; // not in a script
2126 si = SCRIPT_ITEM(sid);
2127 if (si->sn_autoload_prefix != NULL)
2128 {
2129 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2130 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002131 char_u *namep;
2132
2133 // skip a "<SNR>99_" prefix
2134 namep = untrans_function_name(name);
2135 if (namep == NULL)
2136 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002137
2138 // An exported function in an autoload script is stored as
2139 // "dir#path#name".
2140 if (len < sizeof(buffer))
2141 auto_name = buffer;
2142 else
2143 auto_name = alloc(len);
2144 if (auto_name != NULL)
2145 {
2146 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002147 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002148 hi = hash_find(&func_hashtab, auto_name);
2149 if (auto_name != buffer)
2150 vim_free(auto_name);
2151 if (!HASHITEM_EMPTY(hi))
2152 return HI2UF(hi);
2153 }
2154 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155
2156 return NULL;
2157}
2158
2159/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002160 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002161 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2162 * functions.
2163 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002164 * Return NULL for unknown function.
2165 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002166 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002167find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002168{
2169 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002170 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002172 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002173 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002174 // Find script-local function before global one.
2175 if (in_vim9script() && eval_isnamec1(*name)
2176 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002178 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2179 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002180 if (func != NULL)
2181 return func;
2182 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002183 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2184 {
2185 char_u *p = name + 5;
2186 long sid;
2187
2188 // printable "<SNR>123_Name" form
2189 sid = getdigits(&p);
2190 if (*p == '_')
2191 {
2192 func = find_func_with_sid(p + 1, (int)sid);
2193 if (func != NULL)
2194 return func;
2195 }
2196 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002198
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002199 if ((flags & FFED_NO_GLOBAL) == 0)
2200 {
2201 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002202 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002203 if (!HASHITEM_EMPTY(hi))
2204 return HI2UF(hi);
2205 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206
Bram Moolenaarb8822442022-01-11 15:24:05 +00002207 // Find autoload function if this is an autoload script.
2208 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2209 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210}
2211
2212/*
2213 * Find a function by name, return pointer to it in ufuncs.
2214 * "cctx" is passed in a :def function to find imported functions.
2215 * Return NULL for unknown or dead function.
2216 */
2217 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002218find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002220 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002221
2222 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2223 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002224 return NULL;
2225}
2226
2227/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002228 * Return TRUE if "ufunc" is a global function.
2229 */
2230 int
2231func_is_global(ufunc_T *ufunc)
2232{
2233 return ufunc->uf_name[0] != K_SPECIAL;
2234}
2235
2236/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002237 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2238 */
2239 int
2240func_requires_g_prefix(ufunc_T *ufunc)
2241{
2242 return ufunc->uf_name[0] != K_SPECIAL
2243 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002244 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
2245 && !isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002246}
2247
2248/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002249 * Copy the function name of "fp" to buffer "buf".
2250 * "buf" must be able to hold the function name plus three bytes.
2251 * Takes care of script-local function names.
2252 */
2253 static void
2254cat_func_name(char_u *buf, ufunc_T *fp)
2255{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002256 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002257 {
2258 STRCPY(buf, "<SNR>");
2259 STRCAT(buf, fp->uf_name + 3);
2260 }
2261 else
2262 STRCPY(buf, fp->uf_name);
2263}
2264
2265/*
2266 * Add a number variable "name" to dict "dp" with value "nr".
2267 */
2268 static void
2269add_nr_var(
2270 dict_T *dp,
2271 dictitem_T *v,
2272 char *name,
2273 varnumber_T nr)
2274{
2275 STRCPY(v->di_key, name);
2276 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002277 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002278 v->di_tv.v_type = VAR_NUMBER;
2279 v->di_tv.v_lock = VAR_FIXED;
2280 v->di_tv.vval.v_number = nr;
2281}
2282
2283/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002284 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002285 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002286 static void
2287free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002288{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002289 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002290
Bram Moolenaarca16c602022-09-06 18:57:08 +01002291 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002292 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002293 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002294
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002295 // When garbage collecting a funccall_T may be freed before the
2296 // function that references it, clear its uf_scoped field.
2297 // The function may have been redefined and point to another
2298 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002299 if (fp != NULL && fp->uf_scoped == fc)
2300 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002301 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002302 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002303
Bram Moolenaarca16c602022-09-06 18:57:08 +01002304 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002305 vim_free(fc);
2306}
2307
2308/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002309 * Free "fc" and what it contains.
2310 * Can be called only when "fc" is kept beyond the period of it called,
2311 * i.e. after cleanup_function_call(fc).
2312 */
2313 static void
2314free_funccal_contents(funccall_T *fc)
2315{
2316 listitem_T *li;
2317
2318 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002319 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002320
2321 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002322 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002323
2324 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002325 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002326 clear_tv(&li->li_tv);
2327
2328 free_funccal(fc);
2329}
2330
2331/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002332 * Handle the last part of returning from a function: free the local hashtable.
2333 * Unless it is still in use by a closure.
2334 */
2335 static void
2336cleanup_function_call(funccall_T *fc)
2337{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002338 int may_free_fc = fc->fc_refcount <= 0;
2339 int free_fc = TRUE;
2340
Bram Moolenaarca16c602022-09-06 18:57:08 +01002341 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002342
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002343 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002344 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2345 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002346 else
2347 free_fc = FALSE;
2348
2349 // If the a:000 list and the l: and a: dicts are not referenced and
2350 // there is no closure using it, we can free the funccall_T and what's
2351 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002352 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2353 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002354 else
2355 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002356 int todo;
2357 hashitem_T *hi;
2358 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002359
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002360 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002361
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002362 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002363 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002364 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002365 {
2366 if (!HASHITEM_EMPTY(hi))
2367 {
2368 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002369 di = HI2DI(hi);
2370 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002371 }
2372 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002373 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002374
Bram Moolenaarca16c602022-09-06 18:57:08 +01002375 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2376 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002377 else
2378 {
2379 listitem_T *li;
2380
2381 free_fc = FALSE;
2382
2383 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002384 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002385 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002386 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002387
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002388 if (free_fc)
2389 free_funccal(fc);
2390 else
2391 {
2392 static int made_copy = 0;
2393
2394 // "fc" is still in use. This can happen when returning "a:000",
2395 // assigning "l:" to a global variable or defining a closure.
2396 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002397 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002398 previous_funccal = fc;
2399
2400 if (want_garbage_collect)
2401 // If garbage collector is ready, clear count.
2402 made_copy = 0;
2403 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002404 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002405 // We have made a lot of copies, worth 4 Mbyte. This can happen
2406 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002407 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002408 // too much memory.
2409 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002410 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002411 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002412 }
2413}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002414
2415/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002416 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2417 */
2418 static int
2419numbered_function(char_u *name)
2420{
2421 return isdigit(*name)
2422 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2423}
2424
2425/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002426 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002427 * 1. ordinary names, function defined with :function or :def;
2428 * can start with "<SNR>123_" literally or with K_SPECIAL.
2429 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002430 * For the first we only count the name stored in func_hashtab as a reference,
2431 * using function() does not count as a reference, because the function is
2432 * looked up by name.
2433 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002434 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002435func_name_refcount(char_u *name)
2436{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002437 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002438}
2439
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002440/*
2441 * Unreference "fc": decrement the reference count and free it when it
2442 * becomes zero. "fp" is detached from "fc".
2443 * When "force" is TRUE we are exiting.
2444 */
2445 static void
2446funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2447{
2448 funccall_T **pfc;
2449 int i;
2450
2451 if (fc == NULL)
2452 return;
2453
2454 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002455 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2456 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2457 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2458 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002459 {
2460 if (fc == *pfc)
2461 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002462 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 free_funccal_contents(fc);
2464 return;
2465 }
2466 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002467 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2468 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2469 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470}
2471
2472/*
2473 * Remove the function from the function hashtable. If the function was
2474 * deleted while it still has references this was already done.
2475 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2476 */
2477 static int
2478func_remove(ufunc_T *fp)
2479{
2480 hashitem_T *hi;
2481
2482 // Return if it was already virtually deleted.
2483 if (fp->uf_flags & FC_DEAD)
2484 return FALSE;
2485
2486 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002487 if (HASHITEM_EMPTY(hi))
2488 return FALSE;
2489
2490 // When there is a def-function index do not actually remove the
2491 // function, so we can find the index when defining the function again.
2492 // Do remove it when it's a copy.
2493 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002495 fp->uf_flags |= FC_DEAD;
2496 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002498 hash_remove(&func_hashtab, hi, "remove function");
2499 fp->uf_flags |= FC_DELETED;
2500 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002501}
2502
2503 static void
2504func_clear_items(ufunc_T *fp)
2505{
2506 ga_clear_strings(&(fp->uf_args));
2507 ga_clear_strings(&(fp->uf_def_args));
2508 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002510 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002511 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002512 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002513
2514 // Increment the refcount of this function to avoid it being freed
2515 // recursively when the partial is freed.
2516 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002517 partial_unref(fp->uf_partial);
2518 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002519 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002520
2521#ifdef FEAT_LUA
2522 if (fp->uf_cb_free != NULL)
2523 {
2524 fp->uf_cb_free(fp->uf_cb_state);
2525 fp->uf_cb_free = NULL;
2526 }
2527
2528 fp->uf_cb_state = NULL;
2529 fp->uf_cb = NULL;
2530#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531#ifdef FEAT_PROFILE
2532 VIM_CLEAR(fp->uf_tml_count);
2533 VIM_CLEAR(fp->uf_tml_total);
2534 VIM_CLEAR(fp->uf_tml_self);
2535#endif
2536}
2537
2538/*
2539 * Free all things that a function contains. Does not free the function
2540 * itself, use func_free() for that.
2541 * When "force" is TRUE we are exiting.
2542 */
2543 static void
2544func_clear(ufunc_T *fp, int force)
2545{
2546 if (fp->uf_cleared)
2547 return;
2548 fp->uf_cleared = TRUE;
2549
2550 // clear this function
2551 func_clear_items(fp);
2552 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002553 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554}
2555
2556/*
2557 * Free a function and remove it from the list of functions. Does not free
2558 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002559 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002560 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002562 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002563func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564{
2565 // Only remove it when not done already, otherwise we would remove a newer
2566 // version of the function with the same name.
2567 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2568 func_remove(fp);
2569
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002570 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002571 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002572 if (fp->uf_dfunc_idx > 0)
2573 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002574 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002576 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002577 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002578 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579}
2580
2581/*
2582 * Free all things that a function contains and free the function itself.
2583 * When "force" is TRUE we are exiting.
2584 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002585 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586func_clear_free(ufunc_T *fp, int force)
2587{
2588 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002589 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2590 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002591 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002592 else
2593 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594}
2595
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002596/*
2597 * Copy already defined function "lambda" to a new function with name "global".
2598 * This is for when a compiled function defines a global function.
2599 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002600 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002601copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002602 char_u *lambda,
2603 char_u *global,
2604 loopvarinfo_T *loopvarinfo,
2605 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002606{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002607 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002608 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002609
2610 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002611 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002612 semsg(_(e_lambda_function_not_found_str), lambda);
2613 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002614 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002615
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002616 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002617 if (fp != NULL)
2618 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002619 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002620 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002621 return FAIL;
2622 }
2623
Bram Moolenaare01e5212023-01-08 20:31:18 +00002624 fp = alloc_ufunc(global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002625 if (fp == NULL)
2626 return FAIL;
2627
2628 fp->uf_varargs = ufunc->uf_varargs;
2629 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2630 fp->uf_def_status = ufunc->uf_def_status;
2631 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2632 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2633 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2634 == FAIL
2635 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2636 goto failed;
2637
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002638 if (ufunc->uf_arg_types != NULL)
2639 {
2640 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2641 if (fp->uf_arg_types == NULL)
2642 goto failed;
2643 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2644 sizeof(type_T *) * fp->uf_args.ga_len);
2645 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002646 if (ufunc->uf_va_name != NULL)
2647 {
2648 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2649 if (fp->uf_va_name == NULL)
2650 goto failed;
2651 }
2652 fp->uf_ret_type = ufunc->uf_ret_type;
2653
2654 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002655
2656 fp->uf_name_exp = NULL;
2657 set_ufunc_name(fp, global);
2658
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002659 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002660
2661 // the referenced dfunc_T is now used one more time
2662 link_def_function(fp);
2663
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002664 // Create a partial to store the context of the function where it was
2665 // instantiated. Only needs to be done once. Do this on the original
2666 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002667 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2668 {
2669 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2670
2671 if (pt == NULL)
2672 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002673 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002674 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002675 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002676 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002677 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002678 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002679 }
2680
2681 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002682
2683failed:
2684 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002685 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002686}
2687
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002688static int funcdepth = 0;
2689
2690/*
2691 * Increment the function call depth count.
2692 * Return FAIL when going over 'maxfuncdepth'.
2693 * Otherwise return OK, must call funcdepth_decrement() later!
2694 */
2695 int
2696funcdepth_increment(void)
2697{
2698 if (funcdepth >= p_mfd)
2699 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002700 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002701 return FAIL;
2702 }
2703 ++funcdepth;
2704 return OK;
2705}
2706
2707 void
2708funcdepth_decrement(void)
2709{
2710 --funcdepth;
2711}
2712
2713/*
2714 * Get the current function call depth.
2715 */
2716 int
2717funcdepth_get(void)
2718{
2719 return funcdepth;
2720}
2721
2722/*
2723 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002724 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002725 * times as funcdepth_increment().
2726 */
2727 void
2728funcdepth_restore(int depth)
2729{
2730 funcdepth = depth;
2731}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002732
2733/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002734 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2735 * "rettv".
2736 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2737 * Returns NULL when allocation fails.
2738 */
2739 funccall_T *
2740create_funccal(ufunc_T *fp, typval_T *rettv)
2741{
2742 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2743
2744 if (fc == NULL)
2745 return NULL;
2746 fc->fc_caller = current_funccal;
2747 current_funccal = fc;
2748 fc->fc_func = fp;
2749 func_ptr_ref(fp);
2750 fc->fc_rettv = rettv;
2751 return fc;
2752}
2753
2754/*
2755 * To be called when returning from a compiled function; restores
2756 * current_funccal.
2757 */
2758 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002759remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002760{
2761 funccall_T *fc = current_funccal;
2762
2763 current_funccal = fc->fc_caller;
2764 free_funccal(fc);
2765}
2766
2767/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002768 * Call a user function.
2769 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002770 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002771call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002772 ufunc_T *fp, // pointer to function
2773 int argcount, // nr of args
2774 typval_T *argvars, // arguments
2775 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002776 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002777 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002778{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002779 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002780 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002781 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002782 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002783 funccall_T *fc;
2784 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002785 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002786 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002787 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002788 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002789 int i;
2790 int ai;
2791 int islambda = FALSE;
2792 char_u numbuf[NUMBUFLEN];
2793 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002794 typval_T *tv_to_free[MAX_FUNC_ARGS];
2795 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002796#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002797 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002798#endif
ichizok7e5fe382023-04-15 13:17:50 +01002799 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002800
Bram Moolenaarb2049902021-01-24 12:53:53 +01002801#ifdef FEAT_PROFILE
2802 CLEAR_FIELD(profile_info);
2803#endif
2804
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002805 // If depth of calling is getting too high, don't execute the function.
2806 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002807 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002808 rettv->v_type = VAR_NUMBER;
2809 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002810 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002811 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002812
Bram Moolenaare38eab22019-12-05 21:50:01 +01002813 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002814
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002815 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002816 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00002817 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002818 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002819 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002820 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2821 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002822 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002823 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002824
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002825 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002827#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002828 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002829#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002830 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002831#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002832 if (do_profiling == PROF_YES)
2833 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002834#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002835 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002836 if (call_def_function(fp, argcount, argvars, 0,
2837 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
2838 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002839 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002840#ifdef FEAT_PROFILE
2841 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002842 || (caller != NULL && caller->uf_profiling)))
2843 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002844#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002845 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002846 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002847 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848 }
2849
Bram Moolenaar38453522021-11-28 22:00:12 +00002850 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002851
2852 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002853 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2854 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2855 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002856 */
2857 /*
2858 * Init l: variables.
2859 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002860 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002861 if (selfdict != NULL)
2862 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002863 // Set l:self to "selfdict". Use "name" to avoid a warning from
2864 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002865 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002866 name = v->di_key;
2867 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002868 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002869 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002870 v->di_tv.v_type = VAR_DICT;
2871 v->di_tv.v_lock = 0;
2872 v->di_tv.vval.v_dict = selfdict;
2873 ++selfdict->dv_refcount;
2874 }
2875
2876 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002877 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002878 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002879 * Set a:000 to a list with room for the "..." arguments.
2880 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002881 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002882 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002883 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002884 (varnumber_T)(argcount >= fp->uf_args.ga_len
2885 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002886 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002887 if ((fp->uf_flags & FC_NOARGS) == 0)
2888 {
2889 // Use "name" to avoid a warning from some compiler that checks the
2890 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002891 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002892 name = v->di_key;
2893 STRCPY(name, "000");
2894 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002895 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002896 v->di_tv.v_type = VAR_LIST;
2897 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002898 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002899 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002900 CLEAR_FIELD(fc->fc_l_varlist);
2901 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2902 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002903
2904 /*
2905 * Set a:firstline to "firstline" and a:lastline to "lastline".
2906 * Set a:name to named arguments.
2907 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002908 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002909 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002910 if ((fp->uf_flags & FC_NOARGS) == 0)
2911 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002912 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2913 "firstline", (varnumber_T)funcexe->fe_firstline);
2914 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2915 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002916 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002917 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002918 {
2919 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002920 typval_T def_rettv;
2921 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002922
2923 ai = i - fp->uf_args.ga_len;
2924 if (ai < 0)
2925 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002926 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002927 name = FUNCARG(fp, i);
2928 if (islambda)
2929 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002930
2931 // evaluate named argument default expression
2932 isdefault = ai + fp->uf_def_args.ga_len >= 0
2933 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2934 && argvars[i].vval.v_number == VVAL_NONE));
2935 if (isdefault)
2936 {
2937 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002938
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002939 def_rettv.v_type = VAR_NUMBER;
2940 def_rettv.vval.v_number = -1;
2941
2942 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2943 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002944 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002945 {
2946 default_arg_err = 1;
2947 break;
2948 }
2949 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002950 }
2951 else
2952 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002953 if ((fp->uf_flags & FC_NOARGS) != 0)
2954 // Bail out if no a: arguments used (in lambda).
2955 break;
2956
Bram Moolenaare38eab22019-12-05 21:50:01 +01002957 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002958 sprintf((char *)numbuf, "%d", ai + 1);
2959 name = numbuf;
2960 }
2961 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2962 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002963 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002964 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002965 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002966 }
2967 else
2968 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002969 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002970 if (v == NULL)
2971 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002972 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002973 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002974
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002975 // Note: the values are copied directly to avoid alloc/free.
2976 // "argvars" must have VAR_FIXED for v_lock.
2977 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002978 v->di_tv.v_lock = VAR_FIXED;
2979
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002980 if (isdefault)
2981 // Need to free this later, no matter where it's stored.
2982 tv_to_free[tv_to_free_len++] = &v->di_tv;
2983
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002984 if (addlocal)
2985 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002986 // Named arguments should be accessed without the "a:" prefix in
2987 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002988 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002989 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002990 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002991 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002992 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002993
2994 if (ai >= 0 && ai < MAX_FUNC_ARGS)
2995 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002996 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002997
2998 li->li_tv = argvars[i];
2999 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003000 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003001 }
3002 }
3003
Bram Moolenaare38eab22019-12-05 21:50:01 +01003004 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003005 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003006
3007 if (fp->uf_flags & FC_SANDBOX)
3008 {
3009 using_sandbox = TRUE;
3010 ++sandbox;
3011 }
3012
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003013 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003014 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003015 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003016 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003017 ++no_wait_return;
3018 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003019
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003020 smsg(_("calling %s"), SOURCING_NAME);
3021 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003022 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003023 char_u buf[MSG_BUF_LEN];
3024 char_u numbuf2[NUMBUFLEN];
3025 char_u *tofree;
3026 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003027
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003028 msg_puts("(");
3029 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003030 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003031 if (i > 0)
3032 msg_puts(", ");
3033 if (argvars[i].v_type == VAR_NUMBER)
3034 msg_outnum((long)argvars[i].vval.v_number);
3035 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003036 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003037 // Do not want errors such as E724 here.
3038 ++emsg_off;
3039 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3040 --emsg_off;
3041 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003042 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003043 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003044 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003045 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3046 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003047 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003048 msg_puts((char *)s);
3049 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003050 }
3051 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003052 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003053 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003054 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003055 msg_puts("\n"); // don't overwrite this either
3056
3057 verbose_leave_scroll();
3058 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003059 }
3060#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003061 if (do_profiling == PROF_YES)
3062 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003063 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003064#endif
3065
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003066 // "legacy" does not apply to commands in the function
3067 sticky_cmdmod_flags = 0;
3068
Bram Moolenaar98aff652022-09-06 21:02:35 +01003069 // If called from a compiled :def function the execution context must be
3070 // hidden, any deferred functions need to be added to the function being
3071 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003072 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003073
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003074 save_current_sctx = current_sctx;
3075 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003076 save_did_emsg = did_emsg;
3077 did_emsg = FALSE;
3078
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003079 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003080 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003081 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003082 retval = FCERR_FAILED;
3083 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003084 else if (islambda)
3085 {
3086 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3087
3088 // A Lambda always has the command "return {expr}". It is much faster
3089 // to evaluate {expr} directly.
3090 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003091 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003092 --ex_nesting_level;
3093 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003094 else
3095 // call do_cmdline() to execute the lines
3096 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003097 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3098
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003099 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003100 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003101
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003102 if (RedrawingDisabled > 0)
3103 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003104
Bram Moolenaare38eab22019-12-05 21:50:01 +01003105 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003106 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3107 {
3108 clear_tv(rettv);
3109 rettv->v_type = VAR_NUMBER;
3110 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003111
3112 // In corner cases returning a "failed" value is not backwards
3113 // compatible. Only do this for Vim9 script.
3114 if (in_vim9script())
3115 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003116 }
3117
3118#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003119 if (do_profiling == PROF_YES)
3120 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003121 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003122
3123 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3124 profile_may_end_func(&profile_info, fp, caller);
3125 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003126#endif
3127
Bram Moolenaare38eab22019-12-05 21:50:01 +01003128 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003129 if (p_verbose >= 12)
3130 {
3131 ++no_wait_return;
3132 verbose_enter_scroll();
3133
3134 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003135 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003136 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003137 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003138 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003139 else
3140 {
3141 char_u buf[MSG_BUF_LEN];
3142 char_u numbuf2[NUMBUFLEN];
3143 char_u *tofree;
3144 char_u *s;
3145
Bram Moolenaare38eab22019-12-05 21:50:01 +01003146 // The value may be very long. Skip the middle part, so that we
3147 // have some idea how it starts and ends. smsg() would always
3148 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003149 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003150 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003151 --emsg_off;
3152 if (s != NULL)
3153 {
3154 if (vim_strsize(s) > MSG_BUF_CLEN)
3155 {
3156 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3157 s = buf;
3158 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003159 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003160 vim_free(tofree);
3161 }
3162 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003163 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003164
3165 verbose_leave_scroll();
3166 --no_wait_return;
3167 }
3168
ichizok7e5fe382023-04-15 13:17:50 +01003169 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003170 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003171 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003172 restore_current_ectx(save_current_ectx);
3173
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003174#ifdef FEAT_PROFILE
3175 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003176 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003177#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003178 if (using_sandbox)
3179 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003180 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003181
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003182 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003183 {
3184 ++no_wait_return;
3185 verbose_enter_scroll();
3186
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003187 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003188 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003189
3190 verbose_leave_scroll();
3191 --no_wait_return;
3192 }
3193
3194 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003195 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003196 for (i = 0; i < tv_to_free_len; ++i)
3197 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003198 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003199
3200 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003201}
3202
3203/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003204 * Check the argument count for user function "fp".
3205 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3206 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003207 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003208check_user_func_argcount(ufunc_T *fp, int argcount)
3209{
3210 int regular_args = fp->uf_args.ga_len;
3211
3212 if (argcount < regular_args - fp->uf_def_args.ga_len)
3213 return FCERR_TOOFEW;
3214 else if (!has_varargs(fp) && argcount > regular_args)
3215 return FCERR_TOOMANY;
3216 return FCERR_UNKNOWN;
3217}
3218
3219/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003221 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003222 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223call_user_func_check(
3224 ufunc_T *fp,
3225 int argcount,
3226 typval_T *argvars,
3227 typval_T *rettv,
3228 funcexe_T *funcexe,
3229 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003230{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003231 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003232
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003233#ifdef FEAT_LUA
3234 if (fp->uf_flags & FC_CFUNC)
3235 {
3236 cfunc_T cb = fp->uf_cb;
3237
3238 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3239 }
3240#endif
3241
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003242 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3243 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003244 error = check_user_func_argcount(fp, argcount);
3245 if (error != FCERR_UNKNOWN)
3246 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003247
Bram Moolenaar50824712020-12-20 21:10:17 +01003248 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003249 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003251 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003253 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 int did_save_redo = FALSE;
3255 save_redo_T save_redo;
3256
3257 /*
3258 * Call the user function.
3259 * Save and restore search patterns, script variables and
3260 * redo buffer.
3261 */
3262 save_search_patterns();
3263 if (!ins_compl_active())
3264 {
3265 saveRedobuff(&save_redo);
3266 did_save_redo = TRUE;
3267 }
3268 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003269 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003270 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3272 // Function was unreferenced while being used, free it now.
3273 func_clear_free(fp, FALSE);
3274 if (did_save_redo)
3275 restoreRedobuff(&save_redo);
3276 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003277 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003278
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003280}
3281
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003282static funccal_entry_T *funccal_stack = NULL;
3283
3284/*
3285 * Save the current function call pointer, and set it to NULL.
3286 * Used when executing autocommands and for ":source".
3287 */
3288 void
3289save_funccal(funccal_entry_T *entry)
3290{
3291 entry->top_funccal = current_funccal;
3292 entry->next = funccal_stack;
3293 funccal_stack = entry;
3294 current_funccal = NULL;
3295}
3296
3297 void
3298restore_funccal(void)
3299{
3300 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003301 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003302 else
3303 {
3304 current_funccal = funccal_stack->top_funccal;
3305 funccal_stack = funccal_stack->next;
3306 }
3307}
3308
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003309 funccall_T *
3310get_current_funccal(void)
3311{
3312 return current_funccal;
3313}
3314
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003315/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003316 * Return TRUE when currently at the script level:
3317 * - not in a function
3318 * - not executing an autocommand
3319 * Note that when an autocommand sources a script the result is FALSE;
3320 */
3321 int
3322at_script_level(void)
3323{
3324 return current_funccal == NULL && autocmd_match == NULL;
3325}
3326
3327/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003328 * Mark all functions of script "sid" as deleted.
3329 */
3330 void
3331delete_script_functions(int sid)
3332{
3333 hashitem_T *hi;
3334 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003335 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003336 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003337 size_t len;
3338
3339 buf[0] = K_SPECIAL;
3340 buf[1] = KS_EXTRA;
3341 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003342 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003343 len = STRLEN(buf);
3344
Bram Moolenaarce658352020-07-31 23:47:12 +02003345 while (todo > 0)
3346 {
3347 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003348 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003349 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003350 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003351 fp = HI2UF(hi);
3352 if (STRNCMP(fp->uf_name, buf, len) == 0)
3353 {
3354 int changed = func_hashtab.ht_changed;
3355
3356 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003357
3358 if (fp->uf_calls > 0)
3359 {
3360 // Function is executing, don't free it but do remove
3361 // it from the hashtable.
3362 if (func_remove(fp))
3363 fp->uf_refcount--;
3364 }
3365 else
3366 {
3367 func_clear(fp, TRUE);
3368 // When clearing a function another function can be
3369 // cleared as a side effect. When that happens start
3370 // over.
3371 if (changed != func_hashtab.ht_changed)
3372 break;
3373 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003374 }
3375 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003376 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003377 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003378}
3379
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003380#if defined(EXITFREE) || defined(PROTO)
3381 void
3382free_all_functions(void)
3383{
3384 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003385 ufunc_T *fp;
3386 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003387 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003388 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003389
Bram Moolenaare38eab22019-12-05 21:50:01 +01003390 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003391 while (current_funccal != NULL)
3392 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003393 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003394 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003395 if (current_funccal == NULL && funccal_stack != NULL)
3396 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003397 }
3398
Bram Moolenaare38eab22019-12-05 21:50:01 +01003399 // First clear what the functions contain. Since this may lower the
3400 // reference count of a function, it may also free a function and change
3401 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003402 while (todo > 0)
3403 {
3404 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003405 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003406 if (!HASHITEM_EMPTY(hi))
3407 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 // clear the def function index now
3409 fp = HI2UF(hi);
3410 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003411 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412
Bram Moolenaare38eab22019-12-05 21:50:01 +01003413 // Only free functions that are not refcounted, those are
3414 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003415 if (func_name_refcount(fp->uf_name))
3416 ++skipped;
3417 else
3418 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003419 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003420 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003421 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003422 {
3423 skipped = 0;
3424 break;
3425 }
3426 }
3427 --todo;
3428 }
3429 }
3430
Bram Moolenaare38eab22019-12-05 21:50:01 +01003431 // Now actually free the functions. Need to start all over every time,
3432 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003433 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003434 while (func_hashtab.ht_used > skipped)
3435 {
3436 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003437 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003438 if (!HASHITEM_EMPTY(hi))
3439 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003440 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003441 // Only free functions that are not refcounted, those are
3442 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003443 fp = HI2UF(hi);
3444 if (func_name_refcount(fp->uf_name))
3445 ++skipped;
3446 else
3447 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003448 if (func_free(fp, FALSE) == OK)
3449 {
3450 skipped = 0;
3451 break;
3452 }
3453 // did not actually free it
3454 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003455 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003456 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003457 }
3458 if (skipped == 0)
3459 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460
3461 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003462}
3463#endif
3464
3465/*
3466 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003467 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3468 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003469 * "len" is the length of "name", or -1 for NUL terminated.
3470 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003471 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003472builtin_function(char_u *name, int len)
3473{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003474 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003475
Bram Moolenaara26b9702020-04-18 19:53:28 +02003476 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003477 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003478 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3479 {
3480 if (name[i] == AUTOLOAD_CHAR)
3481 return FALSE;
3482 if (!eval_isnamec(name[i]))
3483 {
3484 // "name.something" is not a builtin function
3485 if (name[i] == '.')
3486 return FALSE;
3487 break;
3488 }
3489 }
3490 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003491}
3492
3493 int
3494func_call(
3495 char_u *name,
3496 typval_T *args,
3497 partial_T *partial,
3498 dict_T *selfdict,
3499 typval_T *rettv)
3500{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003501 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003502 listitem_T *item;
3503 typval_T argv[MAX_FUNC_ARGS + 1];
3504 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003505 int r = 0;
3506
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003507 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003508 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003509 {
3510 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3511 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003512 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003513 break;
3514 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003515 // Make a copy of each argument. This is needed to be able to set
3516 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003517 copy_tv(&item->li_tv, &argv[argc++]);
3518 }
3519
3520 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003521 {
3522 funcexe_T funcexe;
3523
Bram Moolenaara80faa82020-04-12 19:37:17 +02003524 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003525 funcexe.fe_firstline = curwin->w_cursor.lnum;
3526 funcexe.fe_lastline = curwin->w_cursor.lnum;
3527 funcexe.fe_evaluate = TRUE;
3528 funcexe.fe_partial = partial;
3529 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003530 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3531 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003532
Bram Moolenaare38eab22019-12-05 21:50:01 +01003533 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003534 while (argc > 0)
3535 clear_tv(&argv[--argc]);
3536
3537 return r;
3538}
3539
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003540static int callback_depth = 0;
3541
3542 int
3543get_callback_depth(void)
3544{
3545 return callback_depth;
3546}
3547
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003548/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003549 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003550 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003551 */
3552 int
3553call_callback(
3554 callback_T *callback,
3555 int len, // length of "name" or -1 to use strlen()
3556 typval_T *rettv, // return value goes here
3557 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003558 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003559 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003560{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003561 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003562 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003563
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003564 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3565 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02003566 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003567 funcexe.fe_evaluate = TRUE;
3568 funcexe.fe_partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003569 ++callback_depth;
3570 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3571 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003572
3573 // When a :def function was called that uses :try an error would be turned
3574 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003575 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003576 {
3577 need_rethrow = FALSE;
3578 handle_did_throw();
3579 }
3580
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003581 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003582}
3583
3584/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003585 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003586 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003587 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3588 */
3589 varnumber_T
3590call_callback_retnr(
3591 callback_T *callback,
3592 int argcount, // number of "argvars"
3593 typval_T *argvars) // vars for arguments, must have "argcount"
3594 // PLUS ONE elements!
3595{
3596 typval_T rettv;
3597 varnumber_T retval;
3598
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003599 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003600 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003601
3602 retval = tv_get_number_chk(&rettv, NULL);
3603 clear_tv(&rettv);
3604 return retval;
3605}
3606
3607/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 * Give an error message for the result of a function.
3609 * Nothing if "error" is FCERR_NONE.
3610 */
3611 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003612user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003613{
3614 switch (error)
3615 {
3616 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003617 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003618 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003619 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003620 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 break;
3622 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003623 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 break;
3625 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003626 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 break;
3628 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003629 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630 break;
3631 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003632 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 break;
3634 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003635 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 break;
3637 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003638 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3639 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003641 case FCERR_OTHER:
3642 case FCERR_FAILED:
3643 // assume the error message was already given
3644 break;
3645 case FCERR_NONE:
3646 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 }
3648}
3649
3650/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003651 * Check the argument types "argvars[argcount]" for "name" using the
3652 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3653 * is already included in "argvars[]".
3654 * Will do nothing if "funcexe->fe_check_type" is NULL or
3655 * "funcexe->fe_evaluate" is FALSE;
3656 * Returns an FCERR_ value.
3657 */
3658 static funcerror_T
3659may_check_argument_types(
3660 funcexe_T *funcexe,
3661 typval_T *argvars,
3662 int argcount,
3663 int base_included,
3664 char_u *name)
3665{
3666 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3667 {
3668 // Check that the argument types are OK for the types of the funcref.
3669 if (check_argument_types(funcexe->fe_check_type,
3670 argvars, argcount,
3671 base_included ? NULL : funcexe->fe_basetv,
3672 name) == FAIL)
3673 return FCERR_OTHER;
3674 }
3675 return FCERR_NONE;
3676}
3677
3678/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003679 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003680 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003681 * Return FAIL when the function can't be called, OK otherwise.
3682 * Also returns OK when an error was encountered while executing the function.
3683 */
3684 int
3685call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003686 char_u *funcname, // name of the function
3687 int len, // length of "name" or -1 to use strlen()
3688 typval_T *rettv, // return value goes here
3689 int argcount_in, // number of "argvars"
3690 typval_T *argvars_in, // vars for arguments, must have "argcount"
3691 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003692 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003693{
3694 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003695 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003696 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003697 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003698 char_u fname_buf[FLEN_FIXED + 1];
3699 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003700 char_u *fname = NULL;
3701 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003702 int argcount = argcount_in;
3703 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003704 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003705 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003706 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003707 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003708 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003709 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003710 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003711 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003712
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003713 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3714 // even when call_func() returns FAIL.
3715 rettv->v_type = VAR_UNKNOWN;
3716
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003717 if (partial != NULL)
3718 fp = partial->pt_func;
3719 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003720 fp = funcexe->fe_ufunc;
3721
3722 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003723 {
3724 // Make a copy of the name, if it comes from a funcref variable it
3725 // could be changed or deleted in the called function.
3726 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3727 if (name == NULL)
3728 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003729
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003730 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3731 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003732
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003733 if (funcexe->fe_doesrange != NULL)
3734 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003735
3736 if (partial != NULL)
3737 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003738 // When the function has a partial with a dict and there is a dict
3739 // argument, use the dict argument. That is backwards compatible.
3740 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003741 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003742 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003743 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003744 {
3745 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003746 {
3747 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3748 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003749 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003750 goto theend;
3751 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003752 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003753 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003754 for (i = 0; i < argcount_in; ++i)
3755 argv[i + argv_clear] = argvars_in[i];
3756 argvars = argv;
3757 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003758
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003759 if (funcexe->fe_check_type != NULL
3760 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003761 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003762 // Now funcexe->fe_check_type is missing the added arguments,
3763 // make a copy of the type with the correction.
3764 check_type = *funcexe->fe_check_type;
3765 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003766 check_type.tt_args = check_type_args;
3767 CLEAR_FIELD(check_type_args);
3768 for (i = 0; i < check_type.tt_argcount; ++i)
3769 check_type_args[i + partial->pt_argc] =
3770 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003771 check_type.tt_argcount += partial->pt_argc;
3772 check_type.tt_min_argcount += partial->pt_argc;
3773 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003774 }
3775 }
3776
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003777 if (error == FCERR_NONE)
3778 // check the argument types if possible
3779 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
3780 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003781
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003782 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003783 {
3784 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003785 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003786
Bram Moolenaar333894b2020-08-01 18:53:07 +02003787 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003788 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003789 {
3790 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003791 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003792 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003793
Bram Moolenaare38eab22019-12-05 21:50:01 +01003794 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003795 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003796 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003797
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003798 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003799 {
3800 /*
3801 * User defined function.
3802 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003803 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003804 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003805 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003806 if (fp != NULL && !is_global && in_vim9script()
3807 && func_requires_g_prefix(fp))
3808 // In Vim9 script g: is required to find a global
3809 // non-autoload function.
3810 fp = NULL;
3811 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003812
Bram Moolenaare38eab22019-12-05 21:50:01 +01003813 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003814 if (fp == NULL
3815 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003816 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003817 && !aborting())
3818 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003819 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003820 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003821 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003822 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003823 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3824 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003825 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003826 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003827 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003828 if (fp == NULL)
3829 {
3830 char_u *p = untrans_function_name(rfname);
3831
3832 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003833 // Don't do this if the name starts with "s:".
3834 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003835 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003836 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003837
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003838 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003839 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003840 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003841 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003842 int need_arg_check = FALSE;
3843 if (funcexe->fe_check_type == NULL)
3844 {
3845 funcexe->fe_check_type = fp->uf_func_type;
3846 need_arg_check = TRUE;
3847 }
3848
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003849 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003850 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003851 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003852 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003853 argv_clear, fp);
3854 need_arg_check = TRUE;
3855 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003856
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003857 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003858 {
3859 // Method call: base->Method()
3860 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003861 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003862 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003863 argvars = argv;
3864 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003865 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003866 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003867
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003868 // Check the argument types now that the function type and all
3869 // argument values are known, if not done above.
3870 if (need_arg_check)
3871 error = may_check_argument_types(funcexe, argvars, argcount,
3872 TRUE, (name != NULL) ? name : funcname);
3873 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
3874 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003876 }
3877 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003878 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003879 {
3880 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003881 * expr->method(): Find the method name in the table, call its
3882 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003883 */
3884 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003885 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003886 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003887 else
3888 {
3889 /*
3890 * Find the function name in the table, call its implementation.
3891 */
3892 error = call_internal_func(fname, argcount, argvars, rettv);
3893 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003894
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003895 /*
3896 * The function call (or "FuncUndefined" autocommand sequence) might
3897 * have been aborted by an error, an interrupt, or an explicitly thrown
3898 * exception that has not been caught so far. This situation can be
3899 * tested for by calling aborting(). For an error in an internal
3900 * function or for the "E132" error in call_user_func(), however, the
3901 * throw point at which the "force_abort" flag (temporarily reset by
3902 * emsg()) is normally updated has not been reached yet. We need to
3903 * update that flag first to make aborting() reliable.
3904 */
3905 update_force_abort();
3906 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003907 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003908 ret = OK;
3909
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003910theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003911 /*
3912 * Report an error unless the argument evaluation or function call has been
3913 * cancelled due to an aborting error, an interrupt, or an exception.
3914 */
3915 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003916 user_func_error(error, (name != NULL) ? name : funcname,
3917 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003918
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003919 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003920 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003921 clear_tv(&argv[--argv_clear + argv_base]);
3922
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003923 vim_free(tofree);
3924 vim_free(name);
3925
3926 return ret;
3927}
3928
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003929/*
3930 * Call a function without arguments, partial or dict.
3931 * This is like call_func() when the call is only "FuncName()".
3932 * To be used by "expr" options.
3933 * Returns NOTDONE when the function could not be found.
3934 */
3935 int
3936call_simple_func(
3937 char_u *funcname, // name of the function
3938 int len, // length of "name" or -1 to use strlen()
3939 typval_T *rettv) // return value goes here
3940{
3941 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003942 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003943 char_u fname_buf[FLEN_FIXED + 1];
3944 char_u *tofree = NULL;
3945 char_u *name;
3946 char_u *fname;
3947 char_u *rfname;
3948 int is_global = FALSE;
3949 ufunc_T *fp;
3950
3951 rettv->v_type = VAR_NUMBER; // default rettv is number zero
3952 rettv->vval.v_number = 0;
3953
3954 // Make a copy of the name, an option can be changed in the function.
3955 name = vim_strnsave(funcname, len);
3956 if (name == NULL)
3957 return ret;
3958
3959 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3960
3961 // Skip "g:" before a function name.
3962 if (fname[0] == 'g' && fname[1] == ':')
3963 {
3964 is_global = TRUE;
3965 rfname = fname + 2;
3966 }
3967 else
3968 rfname = fname;
3969 fp = find_func(rfname, is_global);
3970 if (fp != NULL && !is_global && in_vim9script()
3971 && func_requires_g_prefix(fp))
3972 // In Vim9 script g: is required to find a global non-autoload
3973 // function.
3974 fp = NULL;
3975 if (fp == NULL)
3976 ret = NOTDONE;
3977 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
3978 error = FCERR_DELETED;
3979 else if (fp != NULL)
3980 {
3981 typval_T argvars[1];
3982 funcexe_T funcexe;
3983
3984 argvars[0].v_type = VAR_UNKNOWN;
3985 CLEAR_FIELD(funcexe);
3986 funcexe.fe_evaluate = TRUE;
3987
3988 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
3989 if (error == FCERR_NONE)
3990 ret = OK;
3991 }
3992
3993 user_func_error(error, name, FALSE);
3994 vim_free(tofree);
3995 vim_free(name);
3996
3997 return ret;
3998}
3999
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004000 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001printable_func_name(ufunc_T *fp)
4002{
4003 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4004}
4005
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004006/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004007 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4008 * TRUE. Otherwise return FALSE.
4009 */
4010 static int
4011function_list_modified(int prev_ht_changed)
4012{
4013 if (prev_ht_changed != func_hashtab.ht_changed)
4014 {
4015 emsg(_(e_function_list_was_modified));
4016 return TRUE;
4017 }
4018 return FALSE;
4019}
4020
4021/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004022 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004023 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004024 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004025list_func_head(ufunc_T *fp, int indent)
4026{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004027 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004028 int j;
4029
4030 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004031
4032 // a timer at the more prompt may have deleted the function
4033 if (function_list_modified(prev_ht_changed))
4034 return FAIL;
4035
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004036 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004037 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004038 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004039 msg_puts("def ");
4040 else
4041 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004043 msg_putchar('(');
4044 for (j = 0; j < fp->uf_args.ga_len; ++j)
4045 {
4046 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004047 msg_puts(", ");
4048 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 if (fp->uf_arg_types != NULL)
4050 {
4051 char *tofree;
4052
4053 msg_puts(": ");
4054 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4055 vim_free(tofree);
4056 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004057 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4058 {
4059 msg_puts(" = ");
4060 msg_puts(((char **)(fp->uf_def_args.ga_data))
4061 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4062 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004063 }
4064 if (fp->uf_varargs)
4065 {
4066 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004067 msg_puts(", ");
4068 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004069 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 if (fp->uf_va_name != NULL)
4071 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004072 if (!fp->uf_varargs)
4073 {
4074 if (j)
4075 msg_puts(", ");
4076 msg_puts("...");
4077 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004078 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004079 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004080 {
4081 char *tofree;
4082
4083 msg_puts(": ");
4084 msg_puts(type_name(fp->uf_va_type, &tofree));
4085 vim_free(tofree);
4086 }
4087 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004088 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004089
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004090 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004091 {
4092 if (fp->uf_ret_type != &t_void)
4093 {
4094 char *tofree;
4095
4096 msg_puts(": ");
4097 msg_puts(type_name(fp->uf_ret_type, &tofree));
4098 vim_free(tofree);
4099 }
4100 }
4101 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004102 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004103 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004104 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004105 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004106 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004107 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004108 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004109 msg_clr_eos();
4110 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004111 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004112
4113 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004114}
4115
4116/*
4117 * Get a function name, translating "<SID>" and "<SNR>".
4118 * Also handles a Funcref in a List or Dictionary.
4119 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004120 * Set "*is_global" to TRUE when the function must be global, unless
4121 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004122 * flags:
4123 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004124 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004125 * TFN_QUIET: be quiet
4126 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004127 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004128 * Advances "pp" to just after the function name (if no error).
4129 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004130 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004131trans_function_name(
4132 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004133 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004134 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004135 int flags)
4136{
4137 return trans_function_name_ext(pp, is_global, skip, flags,
4138 NULL, NULL, NULL, NULL);
4139}
4140
4141/*
4142 * trans_function_name() with extra arguments.
4143 * "fdp", "partial", "type" and "ufunc" can be NULL.
4144 */
4145 char_u *
4146trans_function_name_ext(
4147 char_u **pp,
4148 int *is_global,
4149 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004150 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004151 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004152 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004153 type_T **type, // return: type of funcref
4154 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004155{
4156 char_u *name = NULL;
4157 char_u *start;
4158 char_u *end;
4159 int lead;
4160 char_u sid_buf[20];
4161 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004163 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004164 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004165 int vim9script = in_vim9script();
4166 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004167
4168 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004169 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004170 start = *pp;
4171
Bram Moolenaare38eab22019-12-05 21:50:01 +01004172 // Check for hard coded <SNR>: already translated function ID (from a user
4173 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004174 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4175 && (*pp)[2] == (int)KE_SNR)
4176 {
4177 *pp += 3;
4178 len = get_id_len(pp) + 3;
4179 return vim_strnsave(start, len);
4180 }
4181
Bram Moolenaare38eab22019-12-05 21:50:01 +01004182 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4183 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004184 lead = eval_fname_script(start);
4185 if (lead > 2)
4186 start += lead;
4187
Bram Moolenaare38eab22019-12-05 21:50:01 +01004188 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004189 end = get_lval(start, NULL, &lv, FALSE, skip,
4190 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004191 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004192 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004193 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004194 {
4195 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004196 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004197 goto theend;
4198 }
4199 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4200 {
4201 /*
4202 * Report an invalid expression in braces, unless the expression
4203 * evaluation has been cancelled due to an aborting error, an
4204 * interrupt, or an exception.
4205 */
4206 if (!aborting())
4207 {
4208 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004209 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004210 }
4211 else
4212 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4213 goto theend;
4214 }
4215
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004216 if (lv.ll_ufunc != NULL)
4217 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004218 if (ufunc != NULL)
4219 *ufunc = lv.ll_ufunc;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004220 name = vim_strsave(lv.ll_ufunc->uf_name);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004221 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004222 goto theend;
4223 }
4224
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004225 if (lv.ll_tv != NULL)
4226 {
4227 if (fdp != NULL)
4228 {
4229 fdp->fd_dict = lv.ll_dict;
4230 fdp->fd_newkey = lv.ll_newkey;
4231 lv.ll_newkey = NULL;
4232 fdp->fd_di = lv.ll_di;
4233 }
4234 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4235 {
4236 name = vim_strsave(lv.ll_tv->vval.v_string);
4237 *pp = end;
4238 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004239 else if (lv.ll_tv->v_type == VAR_CLASS
4240 && lv.ll_tv->vval.v_class != NULL)
4241 {
4242 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4243 *pp = end;
4244 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004245 else if (lv.ll_tv->v_type == VAR_PARTIAL
4246 && lv.ll_tv->vval.v_partial != NULL)
4247 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004248 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004249 *pp = end;
4250 if (partial != NULL)
4251 *partial = lv.ll_tv->vval.v_partial;
4252 }
4253 else
4254 {
4255 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4256 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004257 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004258 else
4259 *pp = end;
4260 name = NULL;
4261 }
4262 goto theend;
4263 }
4264
4265 if (lv.ll_name == NULL)
4266 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004267 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004268 *pp = end;
4269 goto theend;
4270 }
4271
Bram Moolenaare38eab22019-12-05 21:50:01 +01004272 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004273 if (lv.ll_exp_name != NULL)
4274 {
4275 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004276 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004277 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004278 if (name == lv.ll_exp_name)
4279 name = NULL;
4280 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004281 else if (lv.ll_sid > 0)
4282 {
4283 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4284 int cc = *lv.ll_name_end;
4285
4286 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4287 *lv.ll_name_end = NUL;
4288 if (si->sn_autoload_prefix != NULL)
4289 {
4290 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4291 }
4292 else
4293 {
4294 sid_buf[0] = K_SPECIAL;
4295 sid_buf[1] = KS_EXTRA;
4296 sid_buf[2] = (int)KE_SNR;
4297 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004298 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004299 name = concat_str(sid_buf, lv.ll_name);
4300 }
4301 *lv.ll_name_end = cc;
4302 *pp = end;
4303 goto theend;
4304 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004305 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004306 {
4307 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004308 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004309 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004310 if (name == *pp)
4311 name = NULL;
4312 }
4313 if (name != NULL)
4314 {
4315 name = vim_strsave(name);
4316 *pp = end;
4317 if (STRNCMP(name, "<SNR>", 5) == 0)
4318 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004319 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004320 name[0] = K_SPECIAL;
4321 name[1] = KS_EXTRA;
4322 name[2] = (int)KE_SNR;
4323 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4324 }
4325 goto theend;
4326 }
4327
4328 if (lv.ll_exp_name != NULL)
4329 {
4330 len = (int)STRLEN(lv.ll_exp_name);
4331 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4332 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4333 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004334 // When there was "s:" already or the name expanded to get a
4335 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004336 lv.ll_name += 2;
4337 len -= 2;
4338 lead = 2;
4339 }
4340 }
4341 else
4342 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004343 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004344 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004345 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004346 if (lv.ll_name[0] == 'g')
4347 {
4348 if (is_global != NULL)
4349 {
4350 *is_global = TRUE;
4351 }
4352 else
4353 {
4354 // dropping "g:" without setting "is_global" won't work in
4355 // Vim9script, put it back later
4356 prefix_g = TRUE;
4357 extra = 2;
4358 }
4359 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004360 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004361 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004362 len = (int)(end - lv.ll_name);
4363 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004364 if (len <= 0)
4365 {
4366 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004367 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004368 goto theend;
4369 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004370
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004371 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004372 // starts with a lower case character: dict.func(). Or when in a class.
4373 vim9_local = ASCII_ISUPPER(*start) && vim9script
4374 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004376 /*
4377 * Copy the function name to allocated memory.
4378 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4379 * Accept <SNR>123_name() outside a script.
4380 */
4381 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004382 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004383 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004384 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004385 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004386 {
Kota Kato948a3892022-08-16 16:09:59 +01004387 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4388 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004389 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004390 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004391 goto theend;
4392 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004394 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004395 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004397 || eval_fname_sid(*pp))
4398 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004400 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004401 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004402 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004403 goto theend;
4404 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004405 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004406 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407 extra = 3 + (int)STRLEN(sid_buf);
4408 else
4409 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004410 }
4411 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004412 // The function name must start with an upper case letter (unless it is a
4413 // Vim9 class new() function or a Vim9 class private method)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004414 else if (!(flags & TFN_INT)
4415 && (builtin_function(lv.ll_name, len)
4416 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004417 && !((flags & TFN_IN_CLASS)
4418 && (STRNCMP(lv.ll_name, "new", 3) == 0
4419 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004420 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004421 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004422 : e_function_name_must_start_with_capital_or_s_str),
4423 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004424 goto theend;
4425 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004426 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004427 {
4428 char_u *cp = vim_strchr(lv.ll_name, ':');
4429
4430 if (cp != NULL && cp < end)
4431 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004432 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004433 goto theend;
4434 }
4435 }
4436
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004437 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004438 if (name != NULL)
4439 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004440 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004441 {
4442 name[0] = K_SPECIAL;
4443 name[1] = KS_EXTRA;
4444 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004445 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004446 STRCPY(name + 3, sid_buf);
4447 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004448 else if (prefix_g)
4449 {
4450 name[0] = 'g';
4451 name[1] = ':';
4452 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4454 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004455 }
4456 *pp = end;
4457
4458theend:
4459 clear_lval(&lv);
4460 return name;
4461}
4462
4463/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004464 * Assuming "name" is the result of trans_function_name() and it was prefixed
4465 * to use the script-local name, return the unmodified name (points into
4466 * "name"). Otherwise return NULL.
4467 * This can be used to first search for a script-local function and fall back
4468 * to the global function if not found.
4469 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004470 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004471untrans_function_name(char_u *name)
4472{
4473 char_u *p;
4474
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004475 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004476 {
4477 p = vim_strchr(name, '_');
4478 if (p != NULL)
4479 return p + 1;
4480 }
4481 return NULL;
4482}
4483
4484/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004485 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4486 * current script ID and returns the expanded function name. The caller should
4487 * free the returned name. If not called from a script context or the function
4488 * name doesn't start with these prefixes, then returns NULL.
4489 * This doesn't check whether the script-local function exists or not.
4490 */
4491 char_u *
4492get_scriptlocal_funcname(char_u *funcname)
4493{
4494 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004495 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004496 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004497 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004498
4499 if (funcname == NULL)
4500 return NULL;
4501
4502 if (STRNCMP(funcname, "s:", 2) != 0
4503 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004504 {
4505 ufunc_T *ufunc;
4506
4507 // The function name does not have a script-local prefix. Try finding
4508 // it when in a Vim9 script and there is no "g:" prefix.
4509 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4510 return NULL;
4511 ufunc = find_func(funcname, FALSE);
4512 if (ufunc == NULL || func_is_global(ufunc)
4513 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4514 return NULL;
4515 ++p;
4516 off = 0;
4517 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004518 else
4519 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004520
4521 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4522 {
4523 emsg(_(e_using_sid_not_in_script_context));
4524 return NULL;
4525 }
4526 // Expand s: prefix into <SNR>nr_<name>
4527 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4528 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004529 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004530 if (newname == NULL)
4531 return NULL;
4532 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004533 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004534
4535 return newname;
4536}
4537
4538/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004539 * Return script-local "fname" with the 3-byte sequence replaced by
4540 * printable <SNR> in allocated memory.
4541 */
4542 char_u *
4543alloc_printable_func_name(char_u *fname)
4544{
4545 char_u *n = alloc(STRLEN(fname + 3) + 6);
4546
4547 if (n != NULL)
4548 {
4549 STRCPY(n, "<SNR>");
4550 STRCPY(n + 5, fname + 3);
4551 }
4552 return n;
4553}
4554
4555/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004556 * Call trans_function_name(), except that a lambda is returned as-is.
4557 * Returns the name in allocated memory.
4558 */
4559 char_u *
4560save_function_name(
4561 char_u **name,
4562 int *is_global,
4563 int skip,
4564 int flags,
4565 funcdict_T *fudi)
4566{
4567 char_u *p = *name;
4568 char_u *saved;
4569
4570 if (STRNCMP(p, "<lambda>", 8) == 0)
4571 {
4572 p += 8;
4573 (void)getdigits(&p);
4574 saved = vim_strnsave(*name, p - *name);
4575 if (fudi != NULL)
4576 CLEAR_POINTER(fudi);
4577 }
4578 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004579 saved = trans_function_name_ext(&p, is_global, skip,
4580 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004581 *name = p;
4582 return saved;
4583}
4584
4585/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004586 * List functions. When "regmatch" is NULL all of then.
4587 * Otherwise functions matching "regmatch".
4588 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004589 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004590list_functions(regmatch_T *regmatch)
4591{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004592 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004593 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004594 hashitem_T *hi;
4595
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004596 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004597 {
4598 if (!HASHITEM_EMPTY(hi))
4599 {
4600 ufunc_T *fp = HI2UF(hi);
4601
4602 --todo;
4603 if ((fp->uf_flags & FC_DEAD) == 0
4604 && (regmatch == NULL
4605 ? !message_filtered(fp->uf_name)
4606 && !func_name_refcount(fp->uf_name)
4607 : !isdigit(*fp->uf_name)
4608 && vim_regexec(regmatch, fp->uf_name, 0)))
4609 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004610 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004611 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004612 if (function_list_modified(prev_ht_changed))
4613 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004614 }
4615 }
4616 }
4617}
4618
4619/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004620 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004621 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4622 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004623 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004624 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4625 * With CF_INTERFACE the function is define inside an interface, only the
4626 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004627 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004628 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004629 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004630define_function(
4631 exarg_T *eap,
4632 char_u *name_arg,
4633 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004634 int class_flags,
4635 ocmember_T *obj_members,
4636 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004637{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004638 int j;
4639 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004640 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004641 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004642 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004643 char_u *p;
4644 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004645 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004646 char_u *line_arg = NULL;
4647 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004649 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004650 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004651 garray_T newlines;
4652 int varargs = FALSE;
4653 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004655 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004656 int fp_allocated = FALSE;
4657 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004658 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004659 dictitem_T *v;
4660 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004661 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004662 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004663 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004664 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004665 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004666 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004667
4668 /*
4669 * ":function" without argument: list functions.
4670 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004671 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004672 {
4673 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004674 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004675 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004676 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004677 }
4678
4679 /*
4680 * ":function /pat": list functions matching pattern.
4681 */
4682 if (*eap->arg == '/')
4683 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004684 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004685 if (!eap->skip)
4686 {
4687 regmatch_T regmatch;
4688
4689 c = *p;
4690 *p = NUL;
4691 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4692 *p = c;
4693 if (regmatch.regprog != NULL)
4694 {
4695 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004696 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004697 vim_regfree(regmatch.regprog);
4698 }
4699 }
4700 if (*p == '/')
4701 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004702 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004703 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004704 }
4705
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706 ga_init(&newargs);
4707 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004708 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004709 ga_init(&default_args);
4710
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004711 /*
4712 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004713 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004714 * "name" == func, "fudi.fd_dict" == NULL
4715 * dict.func new dictionary entry
4716 * "name" == NULL, "fudi.fd_dict" set,
4717 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4718 * dict.func existing dict entry with a Funcref
4719 * "name" == func, "fudi.fd_dict" set,
4720 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4721 * dict.func existing dict entry that's not a Funcref
4722 * "name" == NULL, "fudi.fd_dict" set,
4723 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4724 * s:func script-local function name
4725 * g:func global function name, same as "func"
4726 */
4727 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004728 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004729 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004730 // nested function, argument is (args).
4731 paren = TRUE;
4732 CLEAR_FIELD(fudi);
4733 }
4734 else
4735 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004736 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004737 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004738 if (p[0] == 's' && p[1] == ':')
4739 {
4740 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4741 return NULL;
4742 }
4743 p = to_name_end(p, TRUE);
4744 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4745 {
4746 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4747 eap->arg);
4748 return NULL;
4749 }
4750 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004751 }
4752
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004753 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004754 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004755 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004756 paren = (vim_strchr(p, '(') != NULL);
4757 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004758 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004759 /*
4760 * Return on an invalid expression in braces, unless the expression
4761 * evaluation has been cancelled due to an aborting error, an
4762 * interrupt, or an exception.
4763 */
4764 if (!aborting())
4765 {
4766 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004767 semsg(_(e_key_not_present_in_dictionary_str),
4768 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004769 vim_free(fudi.fd_newkey);
4770 return NULL;
4771 }
4772 else
4773 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004774 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004775
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004776 // For "export def FuncName()" in an autoload script the function name
4777 // is stored with the legacy autoload name "dir#script#FuncName" so
4778 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004779 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004780 {
4781 char_u *prefixed = may_prefix_autoload(name);
4782
4783 if (prefixed != NULL && prefixed != name)
4784 {
4785 vim_free(name);
4786 name = prefixed;
4787 }
4788 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004789 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004790 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004791 {
4792 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4793 goto ret_free;
4794 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004795 }
4796
Bram Moolenaare38eab22019-12-05 21:50:01 +01004797 // An error in a function call during evaluation of an expression in magic
4798 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004799 saved_did_emsg = did_emsg;
4800 did_emsg = FALSE;
4801
4802 /*
4803 * ":function func" with only function name: list function.
4804 */
4805 if (!paren)
4806 {
4807 if (!ends_excmd(*skipwhite(p)))
4808 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004809 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004810 goto ret_free;
4811 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004812 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004813 if (eap->nextcmd != NULL)
4814 *p = NUL;
4815 if (!eap->skip && !got_int)
4816 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004817 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004818 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4819 {
4820 char_u *up = untrans_function_name(name);
4821
4822 // With Vim9 script the name was made script-local, if not
4823 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004824 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004825 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004826 }
4827
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004828 if (fp != NULL)
4829 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004830 // Check no function was added or removed from a timer, e.g. at
4831 // the more prompt. "fp" may then be invalid.
4832 int prev_ht_changed = func_hashtab.ht_changed;
4833
4834 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004835 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004836 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4837 {
4838 if (FUNCLINE(fp, j) == NULL)
4839 continue;
4840 msg_putchar('\n');
4841 msg_outnum((long)(j + 1));
4842 if (j < 9)
4843 msg_putchar(' ');
4844 if (j < 99)
4845 msg_putchar(' ');
4846 if (function_list_modified(prev_ht_changed))
4847 break;
4848 msg_prt_line(FUNCLINE(fp, j), FALSE);
4849 out_flush(); // show a line at a time
4850 ui_breakcheck();
4851 }
4852 if (!got_int)
4853 {
4854 msg_putchar('\n');
4855 if (!function_list_modified(prev_ht_changed))
4856 {
4857 if (fp->uf_def_status != UF_NOT_COMPILED)
4858 msg_puts(" enddef");
4859 else
4860 msg_puts(" endfunction");
4861 }
4862 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004863 }
4864 }
4865 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004866 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004867 }
4868 goto ret_free;
4869 }
4870
4871 /*
4872 * ":function name(arg1, arg2)" Define function.
4873 */
4874 p = skipwhite(p);
4875 if (*p != '(')
4876 {
4877 if (!eap->skip)
4878 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004879 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004880 goto ret_free;
4881 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004882 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004883 if (vim_strchr(p, '(') != NULL)
4884 p = vim_strchr(p, '(');
4885 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004886
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004887 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4888 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004889 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004890 goto ret_free;
4891 }
4892
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004893 // In Vim9 script only global functions can be redefined.
4894 if (vim9script && eap->forceit && !is_global)
4895 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004896 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004897 goto ret_free;
4898 }
4899
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004900 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004901
Bram Moolenaar04b12692020-05-04 23:24:44 +02004902 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004903 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004904 // Check the name of the function. Unless it's a dictionary function
4905 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004906 if (name != NULL)
4907 arg = name;
4908 else
4909 arg = fudi.fd_newkey;
4910 if (arg != NULL && (fudi.fd_di == NULL
4911 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4912 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4913 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004914 char_u *name_base = arg;
4915 int i;
4916
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004917 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004918 {
4919 name_base = vim_strchr(arg, '_');
4920 if (name_base == NULL)
4921 name_base = arg + 3;
4922 else
4923 ++name_base;
4924 }
4925 for (i = 0; name_base[i] != NUL && (i == 0
4926 ? eval_isnamec1(name_base[i])
4927 : eval_isnamec(name_base[i])); ++i)
4928 ;
4929 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004930 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004931
4932 // In Vim9 script a function cannot have the same name as a
4933 // variable.
4934 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004935 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4936 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004937 + EVAL_VAR_NO_FUNC) == OK)
4938 {
4939 semsg(_(e_redefining_script_item_str), name_base);
4940 goto ret_free;
4941 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004942 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004943 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004944 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004945 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004946 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004947 goto ret_free;
4948 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004949 }
4950
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004951 // This may get more lines and make the pointers into the first line
4952 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004953 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00004955 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02004956 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004957 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00004958 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004959 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004960 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004961
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004962 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004963 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004964 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004965 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004966 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004967 if (*p != ':')
4968 {
4969 semsg(_(e_no_white_space_allowed_before_colon_str), p);
4970 p = skipwhite(p);
4971 }
4972 else if (!IS_WHITE_OR_NUL(p[1]))
4973 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02004975 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004976 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004977 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004978 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01004979 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004980 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004981 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004982 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004983 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004984 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02004985 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004986 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004987 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02004988 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004989 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004990 else
4991 // find extra arguments "range", "dict", "abort" and "closure"
4992 for (;;)
4993 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01004994 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004995 p = skipwhite(p);
4996 if (STRNCMP(p, "range", 5) == 0)
4997 {
4998 flags |= FC_RANGE;
4999 p += 5;
5000 }
5001 else if (STRNCMP(p, "dict", 4) == 0)
5002 {
5003 flags |= FC_DICT;
5004 p += 4;
5005 }
5006 else if (STRNCMP(p, "abort", 5) == 0)
5007 {
5008 flags |= FC_ABORT;
5009 p += 5;
5010 }
5011 else if (STRNCMP(p, "closure", 7) == 0)
5012 {
5013 flags |= FC_CLOSURE;
5014 p += 7;
5015 if (current_funccal == NULL)
5016 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005017 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018 name == NULL ? (char_u *)"" : name);
5019 goto erret;
5020 }
5021 }
5022 else
5023 break;
5024 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005025
Bram Moolenaare38eab22019-12-05 21:50:01 +01005026 // When there is a line break use what follows for the function body.
5027 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005028 if (*p == '\n')
5029 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005030 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005031 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5032 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005033 && !(VIM_ISWHITE(*whitep) && *p == '#'
5034 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005035 && !eap->skip
5036 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005037 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005038
5039 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005040 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5041 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005042 */
5043 if (KeyTyped)
5044 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005045 // Check if the function already exists, don't let the user type the
5046 // whole function before telling him it doesn't work! For a script we
5047 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005048 if (!eap->skip && !eap->forceit)
5049 {
5050 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005051 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005052 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005053 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005054 }
5055
5056 if (!eap->skip && did_emsg)
5057 goto erret;
5058
Bram Moolenaare38eab22019-12-05 21:50:01 +01005059 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005060 cmdline_row = msg_row;
5061 }
5062
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005063 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005064 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005065
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005066 // Do not define the function when getting the body fails and when
5067 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005068 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005069 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005070 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5071 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005072 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005073 goto erret;
5074
5075 /*
5076 * If there are no errors, add the function
5077 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005078 if (fudi.fd_dict != NULL)
5079 {
5080 char numbuf[20];
5081
5082 fp = NULL;
5083 if (fudi.fd_newkey == NULL && !eap->forceit)
5084 {
5085 emsg(_(e_dictionary_entry_already_exists));
5086 goto erret;
5087 }
5088 if (fudi.fd_di == NULL)
5089 {
5090 // Can't add a function to a locked dictionary
5091 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5092 goto erret;
5093 }
5094 // Can't change an existing function if it is locked
5095 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5096 goto erret;
5097
5098 // Give the function a sequential number. Can only be used with a
5099 // Funcref!
5100 vim_free(name);
5101 sprintf(numbuf, "%d", ++func_nr);
5102 name = vim_strsave((char_u *)numbuf);
5103 if (name == NULL)
5104 goto erret;
5105 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005106 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005107 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005108 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005109 char_u *find_name = name;
5110 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005111 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005113 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005114 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005115 var_conflict = TRUE;
5116
5117 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5118 {
5119 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5120
5121 if (si->sn_autoload_prefix != NULL)
5122 {
5123 if (is_export)
5124 {
5125 find_name = name + STRLEN(si->sn_autoload_prefix);
5126 v = find_var(find_name, &ht, TRUE);
5127 if (v != NULL)
5128 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005129 // Only check if the function already exists in the script,
5130 // global functions can be shadowed.
5131 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005132 }
5133 else
5134 {
5135 char_u *prefixed = may_prefix_autoload(name);
5136
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005137 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005138 {
5139 v = find_var(prefixed, &ht, TRUE);
5140 if (v != NULL)
5141 var_conflict = TRUE;
5142 vim_free(prefixed);
5143 }
5144 }
5145 }
5146 }
5147 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005148 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005149 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005150 goto erret;
5151 }
5152
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005153 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005154 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005155 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005156 char_u *uname = untrans_function_name(name);
5157
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005158 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005159 }
5160
5161 if (fp != NULL || import != NULL)
5162 {
5163 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005164
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005165 // Function can be replaced with "function!" and when sourcing the
5166 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005167 // A name that is used by an import can not be overruled.
5168 if (import != NULL
5169 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005170 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005171 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005172 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005173 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005174 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005175 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005176 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005177 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005178 goto erret;
5179 }
5180 if (fp->uf_calls > 0)
5181 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005182 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005183 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005184 goto erret;
5185 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005186 if (fp->uf_refcount > 1)
5187 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005188 // This function is referenced somewhere, don't redefine it but
5189 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005190 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005191 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005192 fp = NULL;
5193 overwrite = TRUE;
5194 }
5195 else
5196 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005197 char_u *exp_name = fp->uf_name_exp;
5198
5199 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005200 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005201 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005202 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005203 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005204 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005205#ifdef FEAT_PROFILE
5206 fp->uf_profiling = FALSE;
5207 fp->uf_prof_initialized = FALSE;
5208#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005209 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005210 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005211 }
5212 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005213
5214 if (fp == NULL)
5215 {
5216 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5217 {
5218 int slen, plen;
5219 char_u *scriptname;
5220
Bram Moolenaare38eab22019-12-05 21:50:01 +01005221 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005222 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005223 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005224 {
5225 scriptname = autoload_name(name);
5226 if (scriptname != NULL)
5227 {
5228 p = vim_strchr(scriptname, '/');
5229 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005230 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005231 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005232 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005233 j = OK;
5234 vim_free(scriptname);
5235 }
5236 }
5237 if (j == FAIL)
5238 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005239 linenr_T save_lnum = SOURCING_LNUM;
5240
5241 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005242 semsg(_(e_function_name_does_not_match_script_file_name_str),
5243 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005244 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005245 goto erret;
5246 }
5247 }
5248
Bram Moolenaare01e5212023-01-08 20:31:18 +00005249 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005250 if (fp == NULL)
5251 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005252 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005253
5254 if (fudi.fd_dict != NULL)
5255 {
5256 if (fudi.fd_di == NULL)
5257 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005258 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005259 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5260 if (fudi.fd_di == NULL)
5261 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005262 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005263 goto erret;
5264 }
5265 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5266 {
5267 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005268 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005269 goto erret;
5270 }
5271 }
5272 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005273 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005274 clear_tv(&fudi.fd_di->di_tv);
5275 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005276 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005277
Bram Moolenaare38eab22019-12-05 21:50:01 +01005278 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005279 flags |= FC_DICT;
5280 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005281 }
5282 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005283 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005284 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005285 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005286
5287 if (eap->cmdidx == CMD_def)
5288 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005289 int lnum_save = SOURCING_LNUM;
5290 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005291
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005292 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005293
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005294 // error messages are for the first function line
5295 SOURCING_LNUM = sourcing_lnum_top;
5296
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005297 // The function may use script variables from the context.
5298 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005299
h-eastb895b0f2023-09-24 15:46:31 +02005300 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5301 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005302 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005303 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005304 free_fp = fp_allocated;
5305 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005306 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005307 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005308
5309 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005310 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005311 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005312 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005313 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005314 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005315 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005316 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005317 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005318 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005319 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005320
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005321 if (fp_allocated)
5322 {
5323 // insert the new function in the function list
5324 set_ufunc_name(fp, name);
5325 if (overwrite)
5326 {
5327 hi = hash_find(&func_hashtab, name);
5328 hi->hi_key = UF2HIKEY(fp);
5329 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005330 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005331 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005332 {
5333 free_fp = TRUE;
5334 goto erret;
5335 }
5336 fp->uf_refcount = 1;
5337 }
5338
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005339 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005340 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005341 if ((flags & FC_CLOSURE) != 0)
5342 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005343 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005344 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005345 }
5346 else
5347 fp->uf_scoped = NULL;
5348
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005349#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005350 if (prof_def_func())
5351 func_do_profile(fp);
5352#endif
5353 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005354 if (sandbox)
5355 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005356 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005357 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005358 fp->uf_flags = flags;
5359 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005360 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005361 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005362 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005363 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005364 if (is_export)
5365 {
5366 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005367 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005368 is_export = FALSE;
5369 }
5370
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005371 if (eap->cmdidx == CMD_def)
5372 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005373 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5374 // :func does not use Vim9 script syntax, even in a Vim9 script file
5375 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005376
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005377 goto ret_free;
5378
5379erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005380 if (fp != NULL)
5381 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005382 // these were set to "newargs" and "default_args", which are cleared
5383 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005384 ga_init(&fp->uf_args);
5385 ga_init(&fp->uf_def_args);
5386 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005387errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005388 ga_clear_strings(&newargs);
5389 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005390 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005391 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005392 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005393 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005394 VIM_CLEAR(fp->uf_va_name);
5395 clear_type_list(&fp->uf_type_list);
5396 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005397 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005398 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005399ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005400 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005401 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005402 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005403 if (name != name_arg)
5404 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005405 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005406 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005407
5408 return fp;
5409}
5410
5411/*
5412 * ":function"
5413 */
5414 void
5415ex_function(exarg_T *eap)
5416{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005417 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005418
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005419 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005420 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005421 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005422}
5423
5424/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005425 * Find a function by name, including "<lambda>123".
5426 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005427 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005428 * Return NULL if not found.
5429 */
5430 ufunc_T *
5431find_func_by_name(char_u *name, compiletype_T *compile_type)
5432{
5433 char_u *arg = name;
5434 char_u *fname;
5435 ufunc_T *ufunc;
5436 int is_global = FALSE;
5437
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005438 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5439 {
5440 *compile_type = CT_PROFILE;
5441 arg = skipwhite(arg + 7);
5442 }
5443 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5444 {
5445 *compile_type = CT_DEBUG;
5446 arg = skipwhite(arg + 5);
5447 }
5448
5449 if (STRNCMP(arg, "<lambda>", 8) == 0)
5450 {
5451 arg += 8;
5452 (void)getdigits(&arg);
5453 fname = vim_strnsave(name, arg - name);
5454 }
5455 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005456 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005457 // First try finding a method in a class, trans_function_name() will
5458 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005459 ufunc = find_class_func(&arg);
5460 if (ufunc != NULL)
5461 return ufunc;
5462
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005463 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005464 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005465 NULL, NULL, NULL, &ufunc);
5466 if (ufunc != NULL)
5467 {
5468 vim_free(fname);
5469 return ufunc;
5470 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005471 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005472 if (fname == NULL)
5473 {
5474 semsg(_(e_invalid_argument_str), name);
5475 return NULL;
5476 }
5477 if (!ends_excmd2(name, arg))
5478 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005479 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005480 emsg(ex_errmsg(e_trailing_characters_str, arg));
5481 return NULL;
5482 }
5483
5484 ufunc = find_func(fname, is_global);
5485 if (ufunc == NULL)
5486 {
5487 char_u *p = untrans_function_name(fname);
5488
5489 if (p != NULL)
5490 // Try again without making it script-local.
5491 ufunc = find_func(p, FALSE);
5492 }
5493 vim_free(fname);
5494 if (ufunc == NULL)
5495 semsg(_(e_cannot_find_function_str), name);
5496 return ufunc;
5497}
5498
5499/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005500 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005501 * be compiled or the one specified by the argument.
5502 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005503 */
5504 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005505ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005506{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005507 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005508 {
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005509 compiletype_T compile_type = CT_NONE;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005510 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005511 if (ufunc != NULL)
5512 {
5513 if (func_needs_compiling(ufunc, compile_type))
5514 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5515 else
5516 smsg(_("Function %s does not need compiling"), eap->arg);
5517 }
5518 }
5519 else
5520 {
5521 long todo = (long)func_hashtab.ht_used;
5522 int changed = func_hashtab.ht_changed;
5523 hashitem_T *hi;
5524
5525 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5526 {
5527 if (!HASHITEM_EMPTY(hi))
5528 {
5529 --todo;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005530 ufunc_T *ufunc = HI2UF(hi);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005531 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5532 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5533 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005534 {
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005535 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5536
5537 if (func_hashtab.ht_changed != changed)
5538 {
5539 // a function has been added or removed, need to start
5540 // over
5541 todo = (long)func_hashtab.ht_used;
5542 changed = func_hashtab.ht_changed;
5543 hi = func_hashtab.ht_array;
5544 --hi;
5545 }
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005546 }
5547 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005548 }
5549 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005550}
5551
5552/*
5553 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5554 * Return 2 if "p" starts with "s:".
5555 * Return 0 otherwise.
5556 */
5557 int
5558eval_fname_script(char_u *p)
5559{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005560 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5561 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005562 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5563 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5564 return 5;
5565 if (p[0] == 's' && p[1] == ':')
5566 return 2;
5567 return 0;
5568}
5569
5570 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005571translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005572{
5573 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005574 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005575 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005576}
5577
5578/*
5579 * Return TRUE when "ufunc" has old-style "..." varargs
5580 * or named varargs "...name: type".
5581 */
5582 int
5583has_varargs(ufunc_T *ufunc)
5584{
5585 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005586}
5587
5588/*
5589 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005590 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005591 */
5592 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005593function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005594{
5595 char_u *nm = name;
5596 char_u *p;
5597 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005598 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005599 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005600
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005601 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5602 if (no_deref)
5603 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005604 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005605 nm = skipwhite(nm);
5606
Bram Moolenaare38eab22019-12-05 21:50:01 +01005607 // Only accept "funcname", "funcname ", "funcname (..." and
5608 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005609 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005610 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005611 vim_free(p);
5612 return n;
5613}
5614
Bram Moolenaar113e1072019-01-20 15:30:40 +01005615#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005616 char_u *
5617get_expanded_name(char_u *name, int check)
5618{
5619 char_u *nm = name;
5620 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005621 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005622
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005623 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005624
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005625 if (p != NULL && *nm == NUL
5626 && (!check || translated_function_exists(p, is_global)))
5627 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005628
5629 vim_free(p);
5630 return NULL;
5631}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005632#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005633
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005634/*
5635 * Function given to ExpandGeneric() to obtain the list of user defined
5636 * function names.
5637 */
5638 char_u *
5639get_user_func_name(expand_T *xp, int idx)
5640{
5641 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005642 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005643 static hashitem_T *hi;
5644 ufunc_T *fp;
5645
5646 if (idx == 0)
5647 {
5648 done = 0;
5649 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005650 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005651 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005652 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005653 {
5654 if (done++ > 0)
5655 ++hi;
5656 while (HASHITEM_EMPTY(hi))
5657 ++hi;
5658 fp = HI2UF(hi);
5659
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005660 // don't show dead, dict and lambda functions
5661 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005662 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005663 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005664
5665 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005666 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005667
5668 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005669 if (xp->xp_context != EXPAND_USER_FUNC
5670 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005671 {
5672 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005673 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005674 STRCAT(IObuff, ")");
5675 }
5676 return IObuff;
5677 }
5678 return NULL;
5679}
5680
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005681/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005682 * Make a copy of a function.
5683 * Intended to be used for a function defined on a base class that has a copy
5684 * on the child class.
5685 * The copy has uf_refcount set to one.
5686 * Returns NULL when out of memory.
5687 */
5688 ufunc_T *
5689copy_function(ufunc_T *fp)
5690{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005691 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005692 if (ufunc == NULL)
5693 return NULL;
5694
5695 // Most things can just be copied.
5696 *ufunc = *fp;
5697
5698 ufunc->uf_def_status = UF_TO_BE_COMPILED;
5699 ufunc->uf_dfunc_idx = 0;
5700 ufunc->uf_class = NULL;
5701
5702 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
5703 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
5704
5705 if (ufunc->uf_arg_types != NULL)
5706 {
5707 // "uf_arg_types" is an allocated array, make a copy.
5708 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
5709 if (at != NULL)
5710 {
5711 mch_memmove(at, ufunc->uf_arg_types,
5712 sizeof(type_T *) * ufunc->uf_args.ga_len);
5713 ufunc->uf_arg_types = at;
5714 }
5715 }
5716
5717 // TODO: how about the types themselves? they can be freed when the
5718 // original function is freed:
5719 // type_T **uf_arg_types;
5720 // type_T *uf_ret_type;
5721
Ernie Rael114ec812023-06-04 18:11:35 +01005722 // make uf_type_list empty
5723 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00005724
5725 // TODO: partial_T *uf_partial;
5726
5727 if (ufunc->uf_va_name != NULL)
5728 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
5729
5730 // TODO:
5731 // type_T *uf_va_type;
5732 // type_T *uf_func_type;
5733
5734 ufunc->uf_block_depth = 0;
5735 ufunc->uf_block_ids = NULL;
5736
5737 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
5738
5739 ufunc->uf_refcount = 1;
5740 ufunc->uf_name_exp = NULL;
5741 STRCPY(ufunc->uf_name, fp->uf_name);
5742
5743 return ufunc;
5744}
5745
5746/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005747 * ":delfunction {name}"
5748 */
5749 void
5750ex_delfunction(exarg_T *eap)
5751{
5752 ufunc_T *fp = NULL;
5753 char_u *p;
5754 char_u *name;
5755 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005756 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005757
5758 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005759 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
5760 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005761 vim_free(fudi.fd_newkey);
5762 if (name == NULL)
5763 {
5764 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005765 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005766 return;
5767 }
5768 if (!ends_excmd(*skipwhite(p)))
5769 {
5770 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005771 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005772 return;
5773 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005774 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005775 if (eap->nextcmd != NULL)
5776 *p = NUL;
5777
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005778 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005779 {
5780 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005781 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005782 vim_free(name);
5783 return;
5784 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005785 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005786 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005787 vim_free(name);
5788
5789 if (!eap->skip)
5790 {
5791 if (fp == NULL)
5792 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005793 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005794 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005795 return;
5796 }
5797 if (fp->uf_calls > 0)
5798 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005799 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005800 return;
5801 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005802 if (fp->uf_flags & FC_VIM9)
5803 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005804 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005805 return;
5806 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005807
5808 if (fudi.fd_dict != NULL)
5809 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005810 // Delete the dict item that refers to the function, it will
5811 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00005812 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005813 }
5814 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005815 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005816 // A normal function (not a numbered function or lambda) has a
5817 // refcount of 1 for the entry in the hashtable. When deleting
5818 // it and the refcount is more than one, it should be kept.
5819 // A numbered function and lambda should be kept if the refcount is
5820 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005821 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005822 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005823 // Function is still referenced somewhere. Don't free it but
5824 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005825 if (func_remove(fp))
5826 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005827 }
5828 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005829 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005830 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005831 }
5832}
5833
5834/*
5835 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005836 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005837 */
5838 void
5839func_unref(char_u *name)
5840{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005841 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005842
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005843 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005844 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005845 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005846 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005847 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005848#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005849 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005850#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005851 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005852 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005853 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005854}
5855
5856/*
5857 * Unreference a Function: decrement the reference count and free it when it
5858 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005859 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005860 */
5861 void
5862func_ptr_unref(ufunc_T *fp)
5863{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005864 if (fp != NULL && (--fp->uf_refcount <= 0
5865 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5866 && fp->uf_partial->pt_refcount <= 1
5867 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005868 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005869 // Only delete it when it's not being used. Otherwise it's done
5870 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005871 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005872 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005873 }
5874}
5875
5876/*
5877 * Count a reference to a Function.
5878 */
5879 void
5880func_ref(char_u *name)
5881{
5882 ufunc_T *fp;
5883
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005884 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005885 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005886 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005887 if (fp != NULL)
5888 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005889 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005890 // Only give an error for a numbered function.
5891 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005892 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005893}
5894
5895/*
5896 * Count a reference to a Function.
5897 */
5898 void
5899func_ptr_ref(ufunc_T *fp)
5900{
5901 if (fp != NULL)
5902 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005903}
5904
5905/*
5906 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5907 * referenced from anywhere that is in use.
5908 */
5909 static int
5910can_free_funccal(funccall_T *fc, int copyID)
5911{
Bram Moolenaarca16c602022-09-06 18:57:08 +01005912 return (fc->fc_l_varlist.lv_copyID != copyID
5913 && fc->fc_l_vars.dv_copyID != copyID
5914 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005915 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005916}
5917
5918/*
5919 * ":return [expr]"
5920 */
5921 void
5922ex_return(exarg_T *eap)
5923{
5924 char_u *arg = eap->arg;
5925 typval_T rettv;
5926 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005927 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005928
5929 if (current_funccal == NULL)
5930 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005931 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005932 return;
5933 }
5934
Bram Moolenaar844fb642021-10-23 13:32:30 +01005935 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005936 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5937
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005938 if (eap->skip)
5939 ++emsg_skip;
5940
5941 eap->nextcmd = NULL;
5942 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005943 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005944 {
5945 if (!eap->skip)
5946 returning = do_return(eap, FALSE, TRUE, &rettv);
5947 else
5948 clear_tv(&rettv);
5949 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005950 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005951 else if (!eap->skip)
5952 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005953 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005954 update_force_abort();
5955
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005956 /*
5957 * Return unless the expression evaluation has been cancelled due to an
5958 * aborting error, an interrupt, or an exception.
5959 */
5960 if (!aborting())
5961 returning = do_return(eap, FALSE, TRUE, NULL);
5962 }
5963
Bram Moolenaare38eab22019-12-05 21:50:01 +01005964 // When skipping or the return gets pending, advance to the next command
5965 // in this line (!returning). Otherwise, ignore the rest of the line.
5966 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005967 if (returning)
5968 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005969 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02005970 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005971
5972 if (eap->skip)
5973 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02005974 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005975}
5976
zeertzjq5299c092023-04-12 20:48:16 +01005977/*
5978 * Lower level implementation of "call". Only called when not skipping.
5979 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005980 static int
5981ex_call_inner(
5982 exarg_T *eap,
5983 char_u *name,
5984 char_u **arg,
5985 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01005986 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005987 evalarg_T *evalarg)
5988{
5989 linenr_T lnum;
5990 int doesrange;
5991 typval_T rettv;
5992 int failed = FALSE;
5993
zeertzjq5299c092023-04-12 20:48:16 +01005994 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005995 for ( ; lnum <= eap->line2; ++lnum)
5996 {
5997 funcexe_T funcexe;
5998
zeertzjq5299c092023-04-12 20:48:16 +01005999 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006000 {
6001 if (lnum > curbuf->b_ml.ml_line_count)
6002 {
6003 // If the function deleted lines or switched to another buffer
6004 // the line number may become invalid.
6005 emsg(_(e_invalid_range));
6006 break;
6007 }
6008 curwin->w_cursor.lnum = lnum;
6009 curwin->w_cursor.col = 0;
6010 curwin->w_cursor.coladd = 0;
6011 }
6012 *arg = startarg;
6013
6014 funcexe = *funcexe_init;
6015 funcexe.fe_doesrange = &doesrange;
6016 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6017 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6018 {
6019 failed = TRUE;
6020 break;
6021 }
6022 if (has_watchexpr())
6023 dbg_check_breakpoint(eap);
6024
6025 // Handle a function returning a Funcref, Dictionary or List.
6026 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006027 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006028 {
6029 failed = TRUE;
6030 break;
6031 }
6032
6033 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006034 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006035 break;
6036
6037 // Stop when immediately aborting on error, or when an interrupt
6038 // occurred or an exception was thrown but not caught.
6039 // get_func_tv() returned OK, so that the check for trailing
6040 // characters below is executed.
6041 if (aborting())
6042 break;
6043 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006044 return failed;
6045}
6046
6047/*
6048 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6049 * Returns FAIL or OK.
6050 */
6051 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006052ex_defer_inner(
6053 char_u *name,
6054 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006055 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006056 partial_T *partial,
6057 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006058{
6059 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006060 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006061 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006062 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006063
6064 if (current_funccal == NULL)
6065 {
6066 semsg(_(e_str_not_inside_function), "defer");
6067 return FAIL;
6068 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006069 if (partial != NULL)
6070 {
6071 if (partial->pt_dict != NULL)
6072 {
6073 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6074 return FAIL;
6075 }
6076 if (partial->pt_argc > 0)
6077 {
6078 int i;
6079
6080 partial_argc = partial->pt_argc;
6081 for (i = 0; i < partial_argc; ++i)
6082 copy_tv(&partial->pt_argv[i], &argvars[i]);
6083 }
6084 }
6085 r = get_func_arguments(arg, evalarg, FALSE,
6086 argvars + partial_argc, &argcount);
6087 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006088
6089 if (r == OK)
6090 {
6091 if (type != NULL)
6092 {
6093 // Check that the arguments are OK for the types of the funcref.
6094 r = check_argument_types(type, argvars, argcount, NULL, name);
6095 }
6096 else if (builtin_function(name, -1))
6097 {
6098 int idx = find_internal_func(name);
6099
6100 if (idx < 0)
6101 {
6102 emsg_funcname(e_unknown_function_str, name);
6103 r = FAIL;
6104 }
6105 else if (check_internal_func(idx, argcount) == -1)
6106 r = FAIL;
6107 }
6108 else
6109 {
6110 ufunc_T *ufunc = find_func(name, FALSE);
6111
6112 // we tolerate an unknown function here, it might be defined later
6113 if (ufunc != NULL)
6114 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006115 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006116 if (error != FCERR_UNKNOWN)
6117 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006118 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006119 r = FAIL;
6120 }
6121 }
6122 }
6123 }
6124
Bram Moolenaar86d87252022-09-05 21:21:25 +01006125 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006126 {
6127 while (--argcount >= 0)
6128 clear_tv(&argvars[argcount]);
6129 return FAIL;
6130 }
6131 return add_defer(name, argcount, argvars);
6132}
6133
6134/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006135 * Return TRUE if currently inside a function call.
6136 * Give an error message and return FALSE when not.
6137 */
6138 int
6139can_add_defer(void)
6140{
6141 if (!in_def_function() && get_current_funccal() == NULL)
6142 {
6143 semsg(_(e_str_not_inside_function), "defer");
6144 return FALSE;
6145 }
6146 return TRUE;
6147}
6148
6149/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006150 * Add a deferred call for "name" with arguments "argvars[argcount]".
6151 * Consumes "argvars[]".
6152 * Caller must check that in_def_function() returns TRUE or current_funccal is
6153 * not NULL.
6154 * Returns OK or FAIL.
6155 */
6156 int
6157add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6158{
6159 char_u *saved_name = vim_strsave(name);
6160 int argcount = argcount_arg;
6161 defer_T *dr;
6162 int ret = FAIL;
6163
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006164 if (saved_name == NULL)
6165 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006166 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006167 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006168 if (add_defer_function(saved_name, argcount, argvars) == OK)
6169 argcount = 0;
6170 }
6171 else
6172 {
6173 if (current_funccal->fc_defer.ga_itemsize == 0)
6174 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6175 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6176 goto theend;
6177 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6178 + current_funccal->fc_defer.ga_len++;
6179 dr->dr_name = saved_name;
6180 dr->dr_argcount = argcount;
6181 while (argcount > 0)
6182 {
6183 --argcount;
6184 dr->dr_argvars[argcount] = argvars[argcount];
6185 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006186 }
6187 ret = OK;
6188
6189theend:
6190 while (--argcount >= 0)
6191 clear_tv(&argvars[argcount]);
6192 return ret;
6193}
6194
6195/*
6196 * Invoked after a functions has finished: invoke ":defer" functions.
6197 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006198 static void
6199handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006200{
6201 int idx;
6202
Bram Moolenaar58779852022-09-06 18:31:14 +01006203 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006204 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006205 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006206
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006207 if (dr->dr_name == NULL)
6208 // already being called, can happen if function does ":qa"
6209 continue;
6210
6211 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006212 CLEAR_FIELD(funcexe);
6213 funcexe.fe_evaluate = TRUE;
6214
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006215 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006216 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006217
6218 char_u *name = dr->dr_name;
6219 dr->dr_name = NULL;
6220
6221 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6222
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006223 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006224 vim_free(name);
6225 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006226 clear_tv(&dr->dr_argvars[i]);
6227 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006228 ga_clear(&funccal->fc_defer);
6229}
6230
zeertzjq960cf912023-04-18 21:52:54 +01006231 static void
6232invoke_funccall_defer(funccall_T *fc)
6233{
6234 if (fc->fc_ectx != NULL)
6235 {
6236 // :def function
6237 unwind_def_callstack(fc->fc_ectx);
6238 may_invoke_defer_funcs(fc->fc_ectx);
6239 }
6240 else
6241 {
6242 // legacy function
6243 handle_defer_one(fc);
6244 }
6245}
6246
Bram Moolenaar58779852022-09-06 18:31:14 +01006247/*
6248 * Called when exiting: call all defer functions.
6249 */
6250 void
6251invoke_all_defer(void)
6252{
zeertzjq1be4b812023-04-19 14:21:24 +01006253 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6254 invoke_funccall_defer(fc);
6255
zeertzjq960cf912023-04-18 21:52:54 +01006256 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6257 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6258 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006259}
6260
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006261/*
6262 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006263 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006264 */
6265 void
6266ex_call(exarg_T *eap)
6267{
6268 char_u *arg = eap->arg;
6269 char_u *startarg;
6270 char_u *name;
6271 char_u *tofree;
6272 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006273 int failed = FALSE;
6274 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006275 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006276 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006277 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006278 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006279 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006280 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006281
Bram Moolenaare6b53242020-07-01 17:28:33 +02006282 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006283 if (eap->skip)
6284 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006285 typval_T rettv;
6286
Bram Moolenaare38eab22019-12-05 21:50:01 +01006287 // trans_function_name() doesn't work well when skipping, use eval0()
6288 // instead to skip to any following command, e.g. for:
6289 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006290 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006291 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006292 clear_tv(&rettv);
6293 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006294 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006295 return;
6296 }
6297
zeertzjq5299c092023-04-12 20:48:16 +01006298 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006299 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006300 if (fudi.fd_newkey != NULL)
6301 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006302 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006303 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006304 vim_free(fudi.fd_newkey);
6305 }
6306 if (tofree == NULL)
6307 return;
6308
Bram Moolenaare38eab22019-12-05 21:50:01 +01006309 // Increase refcount on dictionary, it could get deleted when evaluating
6310 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006311 if (fudi.fd_dict != NULL)
6312 ++fudi.fd_dict->dv_refcount;
6313
Bram Moolenaare38eab22019-12-05 21:50:01 +01006314 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6315 // contents. For VAR_PARTIAL get its partial, unless we already have one
6316 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006317 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006318 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006319 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006320 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006321
Bram Moolenaare38eab22019-12-05 21:50:01 +01006322 // Skip white space to allow ":call func ()". Not good, but required for
6323 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006324 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006325 if (*startarg != '(')
6326 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006327 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006328 goto end;
6329 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006330 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006331 {
6332 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6333 goto end;
6334 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006335
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006336 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006337 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006338 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006339 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006340 }
6341 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006342 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006343 funcexe_T funcexe;
6344
Bram Moolenaara80faa82020-04-12 19:37:17 +02006345 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006346 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006347 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006348 funcexe.fe_partial = partial;
6349 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006350 funcexe.fe_firstline = eap->line1;
6351 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006352 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006353 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006354 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006355 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006356
Bram Moolenaar1d341892021-09-18 15:25:52 +02006357 // When inside :try we need to check for following "| catch" or "| endtry".
6358 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006359 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006360 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006361 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006362 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006363 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006364 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006365 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006366 {
6367 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006368 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006369 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006370 }
6371 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006372 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006373 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006374 // Must be after using "arg", it may point into memory cleared here.
6375 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006376
6377end:
6378 dict_unref(fudi.fd_dict);
6379 vim_free(tofree);
6380}
6381
6382/*
6383 * Return from a function. Possibly makes the return pending. Also called
6384 * for a pending return at the ":endtry" or after returning from an extra
6385 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6386 * when called due to a ":return" command. "rettv" may point to a typval_T
6387 * with the return rettv. Returns TRUE when the return can be carried out,
6388 * FALSE when the return gets pending.
6389 */
6390 int
6391do_return(
6392 exarg_T *eap,
6393 int reanimate,
6394 int is_cmd,
6395 void *rettv)
6396{
6397 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006398 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006399
6400 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006401 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006402 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006403
6404 /*
6405 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6406 * not in its finally clause (which then is to be executed next) is found.
6407 * In this case, make the ":return" pending for execution at the ":endtry".
6408 * Otherwise, return normally.
6409 */
6410 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6411 if (idx >= 0)
6412 {
6413 cstack->cs_pending[idx] = CSTP_RETURN;
6414
6415 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006416 // A pending return again gets pending. "rettv" points to an
6417 // allocated variable with the rettv of the original ":return"'s
6418 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006419 cstack->cs_rettv[idx] = rettv;
6420 else
6421 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006422 // When undoing a return in order to make it pending, get the stored
6423 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006424 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006425 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006426
6427 if (rettv != NULL)
6428 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006429 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006430 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6431 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6432 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006433 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006434 }
6435 else
6436 cstack->cs_rettv[idx] = NULL;
6437
6438 if (reanimate)
6439 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006440 // The pending return value could be overwritten by a ":return"
6441 // without argument in a finally clause; reset the default
6442 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006443 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6444 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006445 }
6446 }
6447 report_make_pending(CSTP_RETURN, rettv);
6448 }
6449 else
6450 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006451 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006452
Bram Moolenaare38eab22019-12-05 21:50:01 +01006453 // If the return is carried out now, store the return value. For
6454 // a return immediately after reanimation, the value is already
6455 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006456 if (!reanimate && rettv != NULL)
6457 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006458 clear_tv(current_funccal->fc_rettv);
6459 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006460 if (!is_cmd)
6461 vim_free(rettv);
6462 }
6463 }
6464
6465 return idx < 0;
6466}
6467
6468/*
6469 * Free the variable with a pending return value.
6470 */
6471 void
6472discard_pending_return(void *rettv)
6473{
6474 free_tv((typval_T *)rettv);
6475}
6476
6477/*
6478 * Generate a return command for producing the value of "rettv". The result
6479 * is an allocated string. Used by report_pending() for verbose messages.
6480 */
6481 char_u *
6482get_return_cmd(void *rettv)
6483{
6484 char_u *s = NULL;
6485 char_u *tofree = NULL;
6486 char_u numbuf[NUMBUFLEN];
6487
6488 if (rettv != NULL)
6489 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6490 if (s == NULL)
6491 s = (char_u *)"";
6492
6493 STRCPY(IObuff, ":return ");
6494 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6495 if (STRLEN(s) + 8 >= IOSIZE)
6496 STRCPY(IObuff + IOSIZE - 4, "...");
6497 vim_free(tofree);
6498 return vim_strsave(IObuff);
6499}
6500
6501/*
6502 * Get next function line.
6503 * Called by do_cmdline() to get the next line.
6504 * Returns allocated string, or NULL for end of function.
6505 */
6506 char_u *
6507get_func_line(
6508 int c UNUSED,
6509 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006510 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006511 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006512{
6513 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006514 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006515 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006516 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006517
Bram Moolenaare38eab22019-12-05 21:50:01 +01006518 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006519 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006520 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006521 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006522 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006523 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006524 }
6525#ifdef FEAT_PROFILE
6526 if (do_profiling == PROF_YES)
6527 func_line_end(cookie);
6528#endif
6529
6530 gap = &fp->uf_lines;
6531 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006532 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006533 retval = NULL;
6534 else
6535 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006536 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006537 while (fcp->fc_linenr < gap->ga_len
6538 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6539 ++fcp->fc_linenr;
6540 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006541 retval = NULL;
6542 else
6543 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006544 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6545 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006546#ifdef FEAT_PROFILE
6547 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006548 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006549#endif
6550 }
6551 }
6552
Bram Moolenaare38eab22019-12-05 21:50:01 +01006553 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006554 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006555 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006556 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006557 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006558 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006559 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006560 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006561 }
6562
6563 return retval;
6564}
6565
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006566/*
6567 * Return TRUE if the currently active function should be ended, because a
6568 * return was encountered or an error occurred. Used inside a ":while".
6569 */
6570 int
6571func_has_ended(void *cookie)
6572{
6573 funccall_T *fcp = (funccall_T *)cookie;
6574
Bram Moolenaare38eab22019-12-05 21:50:01 +01006575 // Ignore the "abort" flag if the abortion behavior has been changed due to
6576 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006577 return (((fcp->fc_func->uf_flags & FC_ABORT)
6578 && did_emsg && !aborted_in_try())
6579 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006580}
6581
6582/*
6583 * return TRUE if cookie indicates a function which "abort"s on errors.
6584 */
6585 int
6586func_has_abort(
6587 void *cookie)
6588{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006589 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006590}
6591
6592
6593/*
6594 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6595 * Don't do this when "Func" is already a partial that was bound
6596 * explicitly (pt_auto is FALSE).
6597 * Changes "rettv" in-place.
6598 * Returns the updated "selfdict_in".
6599 */
6600 dict_T *
6601make_partial(dict_T *selfdict_in, typval_T *rettv)
6602{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006603 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006604 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006605 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006606 dict_T *selfdict = selfdict_in;
6607
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006608 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6609 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006610 fp = rettv->vval.v_partial->pt_func;
6611 else
6612 {
6613 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006614 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006615 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006616 if (fname == NULL)
6617 {
6618 // There is no point binding a dict to a NULL function, just create
6619 // a function reference.
6620 rettv->v_type = VAR_FUNC;
6621 rettv->vval.v_string = NULL;
6622 }
6623 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006624 {
6625 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006626 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006627
6628 // Translate "s:func" to the stored function name.
6629 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6630 fp = find_func(fname, FALSE);
6631 vim_free(tofree);
6632 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006633 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006634
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006635 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006636 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006637 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006638
6639 if (pt != NULL)
6640 {
6641 pt->pt_refcount = 1;
6642 pt->pt_dict = selfdict;
6643 pt->pt_auto = TRUE;
6644 selfdict = NULL;
6645 if (rettv->v_type == VAR_FUNC)
6646 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006647 // Just a function: Take over the function name and use
6648 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006649 pt->pt_name = rettv->vval.v_string;
6650 }
6651 else
6652 {
6653 partial_T *ret_pt = rettv->vval.v_partial;
6654 int i;
6655
Bram Moolenaare38eab22019-12-05 21:50:01 +01006656 // Partial: copy the function name, use selfdict and copy
6657 // args. Can't take over name or args, the partial might
6658 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006659 if (ret_pt->pt_name != NULL)
6660 {
6661 pt->pt_name = vim_strsave(ret_pt->pt_name);
6662 func_ref(pt->pt_name);
6663 }
6664 else
6665 {
6666 pt->pt_func = ret_pt->pt_func;
6667 func_ptr_ref(pt->pt_func);
6668 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006669 if (ret_pt->pt_argc > 0)
6670 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006671 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006672 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006673 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006674 pt->pt_argc = 0;
6675 else
6676 {
6677 pt->pt_argc = ret_pt->pt_argc;
6678 for (i = 0; i < pt->pt_argc; i++)
6679 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6680 }
6681 }
6682 partial_unref(ret_pt);
6683 }
6684 rettv->v_type = VAR_PARTIAL;
6685 rettv->vval.v_partial = pt;
6686 }
6687 }
6688 return selfdict;
6689}
6690
6691/*
6692 * Return the name of the executed function.
6693 */
6694 char_u *
6695func_name(void *cookie)
6696{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006697 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006698}
6699
6700/*
6701 * Return the address holding the next breakpoint line for a funccall cookie.
6702 */
6703 linenr_T *
6704func_breakpoint(void *cookie)
6705{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006706 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006707}
6708
6709/*
6710 * Return the address holding the debug tick for a funccall cookie.
6711 */
6712 int *
6713func_dbg_tick(void *cookie)
6714{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006715 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006716}
6717
6718/*
6719 * Return the nesting level for a funccall cookie.
6720 */
6721 int
6722func_level(void *cookie)
6723{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006724 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006725}
6726
6727/*
6728 * Return TRUE when a function was ended by a ":return" command.
6729 */
6730 int
6731current_func_returned(void)
6732{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006733 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006734}
6735
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006736 int
6737free_unref_funccal(int copyID, int testing)
6738{
6739 int did_free = FALSE;
6740 int did_free_funccal = FALSE;
6741 funccall_T *fc, **pfc;
6742
6743 for (pfc = &previous_funccal; *pfc != NULL; )
6744 {
6745 if (can_free_funccal(*pfc, copyID))
6746 {
6747 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006748 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006749 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006750 did_free = TRUE;
6751 did_free_funccal = TRUE;
6752 }
6753 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006754 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006755 }
6756 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006757 // When a funccal was freed some more items might be garbage
6758 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006759 (void)garbage_collect(testing);
6760
6761 return did_free;
6762}
6763
6764/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006765 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006766 */
6767 static funccall_T *
6768get_funccal(void)
6769{
6770 int i;
6771 funccall_T *funccal;
6772 funccall_T *temp_funccal;
6773
6774 funccal = current_funccal;
6775 if (debug_backtrace_level > 0)
6776 {
6777 for (i = 0; i < debug_backtrace_level; i++)
6778 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006779 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006780 if (temp_funccal)
6781 funccal = temp_funccal;
6782 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006783 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006784 debug_backtrace_level = i;
6785 }
6786 }
6787 return funccal;
6788}
6789
6790/*
6791 * Return the hashtable used for local variables in the current funccal.
6792 * Return NULL if there is no current funccal.
6793 */
6794 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006795get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006796{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006797 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006798 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006799 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006800}
6801
6802/*
6803 * Return the l: scope variable.
6804 * Return NULL if there is no current funccal.
6805 */
6806 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006807get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006808{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006809 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006810 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006811 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006812}
6813
6814/*
6815 * Return the hashtable used for argument in the current funccal.
6816 * Return NULL if there is no current funccal.
6817 */
6818 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006819get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006820{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006821 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006822 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006823 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006824}
6825
6826/*
6827 * Return the a: scope variable.
6828 * Return NULL if there is no current funccal.
6829 */
6830 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006831get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006832{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006833 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006834 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006835 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006836}
6837
6838/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006839 * List function variables, if there is a function.
6840 */
6841 void
6842list_func_vars(int *first)
6843{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006844 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
6845 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006846 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006847}
6848
6849/*
6850 * If "ht" is the hashtable for local variables in the current funccal, return
6851 * the dict that contains it.
6852 * Otherwise return NULL.
6853 */
6854 dict_T *
6855get_current_funccal_dict(hashtab_T *ht)
6856{
6857 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01006858 && ht == &current_funccal->fc_l_vars.dv_hashtab)
6859 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006860 return NULL;
6861}
6862
6863/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006864 * Search hashitem in parent scope.
6865 */
6866 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006867find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006868{
6869 funccall_T *old_current_funccal = current_funccal;
6870 hashtab_T *ht;
6871 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006872 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006873
Bram Moolenaarca16c602022-09-06 18:57:08 +01006874 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006875 return NULL;
6876
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006877 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006878 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006879 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006880 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006881 ht = find_var_ht(name, &varname);
6882 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006883 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006884 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006885 if (!HASHITEM_EMPTY(hi))
6886 {
6887 *pht = ht;
6888 break;
6889 }
6890 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006891 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02006892 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006893 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006894 }
6895 current_funccal = old_current_funccal;
6896
6897 return hi;
6898}
6899
6900/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006901 * Search variable in parent scope.
6902 */
6903 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006904find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006905{
6906 dictitem_T *v = NULL;
6907 funccall_T *old_current_funccal = current_funccal;
6908 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006909 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006910
Bram Moolenaarca16c602022-09-06 18:57:08 +01006911 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006912 return NULL;
6913
Bram Moolenaare38eab22019-12-05 21:50:01 +01006914 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01006915 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006916 while (current_funccal)
6917 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006918 ht = find_var_ht(name, &varname);
6919 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006920 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006921 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006922 if (v != NULL)
6923 break;
6924 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006925 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006926 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006927 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006928 }
6929 current_funccal = old_current_funccal;
6930
6931 return v;
6932}
6933
6934/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006935 * Set "copyID + 1" in previous_funccal and callers.
6936 */
6937 int
6938set_ref_in_previous_funccal(int copyID)
6939{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006940 funccall_T *fc;
6941
Bram Moolenaarca16c602022-09-06 18:57:08 +01006942 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006943 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006944 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006945 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
6946 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
6947 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006948 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006949 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006950 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006951}
6952
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006953 static int
6954set_ref_in_funccal(funccall_T *fc, int copyID)
6955{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006956 if (fc->fc_copyID != copyID)
6957 {
6958 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006959 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
6960 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
6961 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
6962 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006963 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006964 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006965 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006966}
6967
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006968/*
6969 * Set "copyID" in all local vars and arguments in the call stack.
6970 */
6971 int
6972set_ref_in_call_stack(int copyID)
6973{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006974 funccall_T *fc;
6975 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006976
Bram Moolenaarca16c602022-09-06 18:57:08 +01006977 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006978 if (set_ref_in_funccal(fc, copyID))
6979 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006980
6981 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006982 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006983 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006984 if (set_ref_in_funccal(fc, copyID))
6985 return TRUE;
6986 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006987}
6988
6989/*
6990 * Set "copyID" in all functions available by name.
6991 */
6992 int
6993set_ref_in_functions(int copyID)
6994{
6995 int todo;
6996 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006997 ufunc_T *fp;
6998
6999 todo = (int)func_hashtab.ht_used;
7000 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007001 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007002 if (!HASHITEM_EMPTY(hi))
7003 {
7004 --todo;
7005 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007006 if (!func_name_refcount(fp->uf_name)
7007 && set_ref_in_func(NULL, fp, copyID))
7008 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007009 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007010 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007011 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007012}
7013
7014/*
7015 * Set "copyID" in all function arguments.
7016 */
7017 int
7018set_ref_in_func_args(int copyID)
7019{
7020 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007021
7022 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007023 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7024 copyID, NULL, NULL))
7025 return TRUE;
7026 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007027}
7028
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007029/*
7030 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007031 * Returns TRUE if setting references failed somehow.
7032 */
7033 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007034set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007035{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007036 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007037 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007038 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007039 char_u fname_buf[FLEN_FIXED + 1];
7040 char_u *tofree = NULL;
7041 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007042 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007043
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007044 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007045 return FALSE;
7046
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007047 if (fp_in == NULL)
7048 {
7049 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007050 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007051 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007052 if (fp != NULL)
7053 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007054 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007055 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007056 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007057
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007058 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007059 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007060}
7061
Bram Moolenaare38eab22019-12-05 21:50:01 +01007062#endif // FEAT_EVAL