blob: 0f487fc1207021fcfa6009a214d83d1bc11a8f46 [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
h-eastdb385522023-09-28 22:18:19 +0200323 // object variable this. can be used only in a constructor
324 if (STRNCMP(eap->arg, "new", 3) != 0)
325 {
326 c = *argend;
327 *argend = NUL;
328 semsg(_(e_cannot_use_an_object_variable_except_with_the_new_method_str), arg);
329 *argend = c;
330 break;
331 }
332
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000333 if (*skipwhite(p) == '=')
334 {
335 char_u *defval = skipwhite(skipwhite(p) + 1);
336 if (STRNCMP(defval, "v:none", 6) != 0)
337 {
338 semsg(_(e_constructor_default_value_must_be_vnone_str), p);
339 goto err_ret;
340 }
341 any_default = TRUE;
342 p = defval + 6;
343
344 if (ga_grow(default_args, 1) == FAIL)
345 goto err_ret;
346
347 char_u *expr = vim_strsave((char_u *)"v:none");
348 if (expr == NULL)
349 goto err_ret;
350 ((char_u **)(default_args->ga_data))
351 [default_args->ga_len] = expr;
352 default_args->ga_len++;
353 }
354 else if (any_default)
355 {
356 emsg(_(e_non_default_argument_follows_default_argument));
357 goto err_ret;
358 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000359
360 // TODO: check the argument is indeed a member
361 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
362 return FAIL;
363 if (newargs != NULL)
364 {
365 ((char_u **)(newargs->ga_data))[newargs->ga_len] =
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000366 vim_strnsave(arg, argend - arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000367 newargs->ga_len++;
368
h-eastb895b0f2023-09-24 15:46:31 +0200369 if (argtypes != NULL && ga_grow(argtypes, 1) == OK
370 && arg_objm != NULL && ga_grow(arg_objm, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000371 {
372 // TODO: use the actual type
373 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] =
374 vim_strsave((char_u *)"any");
h-eastb895b0f2023-09-24 15:46:31 +0200375 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000376
377 // Add a line to the function body for the assignment.
378 if (ga_grow(newlines, 1) == OK)
379 {
380 // "this.name = name"
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000381 int len = 5 + (argend - arg) + 3 + (argend - arg) + 1;
382 if (any_default)
383 len += 14 + 10;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000384 char_u *assignment = alloc(len);
385 if (assignment != NULL)
386 {
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000387 c = *argend;
388 *argend = NUL;
389 if (any_default)
390 vim_snprintf((char *)assignment, len,
391 "ifargisset %d this.%s = %s",
392 default_args->ga_len - 1, arg, arg);
393 else
394 vim_snprintf((char *)assignment, len,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000395 "this.%s = %s", arg, arg);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000396 *argend = c;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000397 ((char_u **)(newlines->ga_data))[
398 newlines->ga_len++] = assignment;
399 }
400 }
401 }
402 }
403 if (*p == ',')
404 ++p;
405 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200406 else
407 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200408 char_u *np;
409
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200410 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100411 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200412 arg_objm, evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200414 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200415
Bram Moolenaar015cf102021-06-26 21:52:02 +0200416 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200417 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200418 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200419 if (*np == '=' && np[1] != '=' && np[1] != '~'
420 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200421 {
422 typval_T rettv;
423
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100424 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200425 any_default = TRUE;
Bram Moolenaareb5adf12022-09-04 13:41:37 +0100426 p = skipwhite(np + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000427 char_u *expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200428 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200429 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200430 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200431 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200432 if (ga_grow(default_args, 1) == FAIL)
433 goto err_ret;
434
435 // trim trailing whitespace
436 while (p > expr && VIM_ISWHITE(p[-1]))
437 p--;
438 c = *p;
439 *p = NUL;
440 expr = vim_strsave(expr);
441 if (expr == NULL)
442 {
443 *p = c;
444 goto err_ret;
445 }
446 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200447 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200448 default_args->ga_len++;
449 *p = c;
450 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200451 }
452 else
453 mustend = TRUE;
454 }
455 else if (any_default)
456 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000457 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200458 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200459 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200460
461 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
462 {
463 // Be tolerant when skipping
464 if (!skip)
465 {
466 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
467 goto err_ret;
468 }
469 p = skipwhite(p);
470 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200471 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200472 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200473 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200474 // Don't give this error when skipping, it makes the "->" not
475 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100476 // Allow missing space after comma in legacy functions.
477 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200478 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
479 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100480 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200481 goto err_ret;
482 }
483 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200484 else
485 mustend = TRUE;
486 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200487 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200488 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200489 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200490
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200491 if (*p != endchar)
492 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100493 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200494
495 *argp = p;
496 return OK;
497
498err_ret:
499 if (newargs != NULL)
500 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200501 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200502 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200503 return FAIL;
504}
505
506/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100507 * Parse the argument types, filling "fp->uf_arg_types".
508 * Return OK or FAIL.
509 */
510 static int
h-eastb895b0f2023-09-24 15:46:31 +0200511parse_argument_types(
512 ufunc_T *fp,
513 garray_T *argtypes,
514 int varargs,
515 garray_T *arg_objm,
516 ocmember_T *obj_members,
517 int obj_member_count)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100518{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200519 int len = 0;
520
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100521 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
522 if (argtypes->ga_len > 0)
523 {
524 // When "varargs" is set the last name/type goes into uf_va_name
525 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200526 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100527
528 if (len > 0)
529 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
530 if (fp->uf_arg_types != NULL)
531 {
532 int i;
533 type_T *type;
534
535 for (i = 0; i < len; ++ i)
536 {
537 char_u *p = ((char_u **)argtypes->ga_data)[i];
538
539 if (p == NULL)
540 // will get the type from the default value
541 type = &t_unknown;
542 else
h-eastb895b0f2023-09-24 15:46:31 +0200543 {
544 if (arg_objm != NULL && ((int8_T *)arg_objm->ga_data)[i])
545 {
546 char_u *aname = ((char_u **)fp->uf_args.ga_data)[i];
547
548 type = &t_any;
549 for (int om = 0; om < obj_member_count; ++om)
550 {
551 if (STRCMP(aname, obj_members[om].ocm_name) == 0)
552 {
553 type = obj_members[om].ocm_type;
554 break;
555 }
556 }
557 }
558 else
559 type = parse_type(&p, &fp->uf_type_list, TRUE);
560 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100561 if (type == NULL)
562 return FAIL;
563 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000564 if (i < fp->uf_args.ga_len
565 && (type->tt_type == VAR_FUNC
566 || type->tt_type == VAR_PARTIAL)
567 && var_wrong_func_name(
568 ((char_u **)fp->uf_args.ga_data)[i], TRUE))
569 return FAIL;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100570 }
571 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100572 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200573
574 if (varargs)
575 {
576 char_u *p;
577
578 // Move the last argument "...name: type" to uf_va_name and
579 // uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200580 --fp->uf_args.ga_len;
Bram Moolenaarf0571712023-01-04 13:16:20 +0000581 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len];
582 ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len] = NULL;
Bram Moolenaar2a389082021-04-09 20:24:31 +0200583 p = ((char_u **)argtypes->ga_data)[len];
584 if (p == NULL)
585 // TODO: get type from default value
586 fp->uf_va_type = &t_list_any;
587 else
588 {
589 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
590 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
591 {
592 semsg(_(e_variable_arguments_type_must_be_list_str),
593 ((char_u **)argtypes->ga_data)[len]);
594 return FAIL;
595 }
596 }
597 if (fp->uf_va_type == NULL)
598 return FAIL;
599 }
600
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100601 return OK;
602}
603
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100604 static int
605parse_return_type(ufunc_T *fp, char_u *ret_type)
606{
607 if (ret_type == NULL)
608 fp->uf_ret_type = &t_void;
609 else
610 {
611 char_u *p = ret_type;
612
613 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
614 if (fp->uf_ret_type == NULL)
615 {
616 fp->uf_ret_type = &t_void;
617 return FAIL;
618 }
619 }
620 return OK;
621}
622
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100623/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200624 * Register function "fp" as using "current_funccal" as its scope.
625 */
626 static int
627register_closure(ufunc_T *fp)
628{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200629 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100630 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200631 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200632 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200633 fp->uf_scoped = current_funccal;
634 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200635
Bram Moolenaarca16c602022-09-06 18:57:08 +0100636 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200637 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100638 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
639 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200640 return OK;
641}
642
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100643 static void
644set_ufunc_name(ufunc_T *fp, char_u *name)
645{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100646 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
647 // actually extends beyond the struct.
648 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100649
650 if (name[0] == K_SPECIAL)
651 {
652 fp->uf_name_exp = alloc(STRLEN(name) + 3);
653 if (fp->uf_name_exp != NULL)
654 {
655 STRCPY(fp->uf_name_exp, "<SNR>");
656 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
657 }
658 }
659}
660
Bram Moolenaar58016442016-07-31 18:30:22 +0200661/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100662 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
663 * return "buf" filled with a readable function name.
664 * Otherwise just return "name", thus the return value can always be used.
665 * "name" and "buf" may be equal.
666 */
667 char_u *
668make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
669{
670 size_t len;
671
672 if (name[0] != K_SPECIAL)
673 return name;
674 len = STRLEN(name);
675 if (len + 3 > bufsize)
676 return name;
677
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100678 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100679 mch_memmove(buf, "<SNR>", 5);
680 return buf;
681}
682
683/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200684 * Get a name for a lambda. Returned in static memory.
685 */
686 char_u *
687get_lambda_name(void)
688{
689 static char_u name[30];
690 static int lambda_no = 0;
691
692 sprintf((char*)name, "<lambda>%d", ++lambda_no);
693 return name;
694}
695
Bram Moolenaare01e5212023-01-08 20:31:18 +0000696/*
697 * Allocate a "ufunc_T" for a function called "name".
698 * Makes sure the size is right.
699 */
700 static ufunc_T *
701alloc_ufunc(char_u *name)
702{
703 // When the name is short we need to make sure we allocate enough bytes for
704 // the whole struct, including any padding.
705 size_t len = offsetof(ufunc_T, uf_name) + STRLEN(name) + 1;
706 return alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
707}
708
Bram Moolenaar801ab062020-06-25 19:27:56 +0200709#if defined(FEAT_LUA) || defined(PROTO)
710/*
711 * Registers a native C callback which can be called from Vim script.
712 * Returns the name of the Vim script function.
713 */
714 char_u *
715register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
716{
717 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200718 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200719
Bram Moolenaare01e5212023-01-08 20:31:18 +0000720 fp = alloc_ufunc(name);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200721 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200722 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200723
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200724 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200725 fp->uf_refcount = 1;
726 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000727 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200728 fp->uf_calls = 0;
729 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200730 fp->uf_cb = cb;
731 fp->uf_cb_free = cb_free;
732 fp->uf_cb_state = state;
733
734 set_ufunc_name(fp, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000735 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200736
737 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200738}
739#endif
740
Bram Moolenaar04b12692020-05-04 23:24:44 +0200741/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100742 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100743 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100744 * If "white_error" is not NULL check for correct use of white space and set
745 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100746 * Return NULL if no valid arrow found.
747 */
748 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100749skip_arrow(
750 char_u *start,
751 int equal_arrow,
752 char_u **ret_type,
753 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100754{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100755 char_u *s = start;
756 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100757
758 if (equal_arrow)
759 {
760 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100761 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100762 if (white_error != NULL && !VIM_ISWHITE(s[1]))
763 {
764 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100765 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100766 return NULL;
767 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100768 s = skipwhite(s + 1);
769 *ret_type = s;
770 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100771 if (s == *ret_type)
772 {
773 emsg(_(e_missing_return_type));
774 return NULL;
775 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100776 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100777 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100778 s = skipwhite(s);
779 if (*s != '=')
780 return NULL;
781 ++s;
782 }
783 if (*s != '>')
784 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100785 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
786 || !IS_WHITE_OR_NUL(s[1])))
787 {
788 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100789 semsg(_(e_white_space_required_before_and_after_str_at_str),
790 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100791 return NULL;
792 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100793 return skipwhite(s + 1);
794}
795
796/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100797 * Check if "*cmd" points to a function command and if so advance "*cmd" and
798 * return TRUE.
799 * Otherwise return FALSE;
800 * Do not consider "function(" to be a command.
801 */
802 static int
803is_function_cmd(char_u **cmd)
804{
805 char_u *p = *cmd;
806
807 if (checkforcmd(&p, "function", 2))
808 {
809 if (*p == '(')
810 return FALSE;
811 *cmd = p;
812 return TRUE;
813 }
814 return FALSE;
815}
816
817/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200818 * Called when defining a function: The context may be needed for script
819 * variables declared in a block that is visible now but not when the function
820 * is compiled or called later.
821 */
822 static void
823function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
824{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000825 if (cstack == NULL || cstack->cs_idx < 0)
826 return;
827
828 int count = cstack->cs_idx + 1;
829 int i;
830
831 fp->uf_block_ids = ALLOC_MULT(int, count);
832 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200833 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000834 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
835 sizeof(int) * count);
836 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200837 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000838
839 // Set flag in each block to indicate a function was defined. This
840 // is used to keep the variable when leaving the block, see
841 // hide_script_var().
842 for (i = 0; i <= cstack->cs_idx; ++i)
843 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200844}
845
846/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100847 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200848 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100849 * "newlines" must already have been initialized.
850 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
851 */
852 static int
853get_function_body(
854 exarg_T *eap,
855 garray_T *newlines,
856 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000857 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100858{
859 linenr_T sourcing_lnum_top = SOURCING_LNUM;
860 linenr_T sourcing_lnum_off;
861 int saved_wait_return = need_wait_return;
862 char_u *line_arg = line_arg_in;
863 int vim9_function = eap->cmdidx == CMD_def
864 || eap->cmdidx == CMD_block;
865#define MAX_FUNC_NESTING 50
866 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200867 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100868 int nesting = 0;
869 getline_opt_T getline_options;
870 int indent = 2;
871 char_u *skip_until = NULL;
872 int ret = FAIL;
873 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200874 int heredoc_concat_len = 0;
875 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100876 char_u *heredoc_trimmed = NULL;
877
Bram Moolenaar20677332021-06-06 17:02:53 +0200878 ga_init2(&heredoc_ga, 1, 500);
879
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100880 // Detect having skipped over comment lines to find the return
881 // type. Add NULL lines to keep the line count correct.
882 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
883 if (SOURCING_LNUM < sourcing_lnum_off)
884 {
885 sourcing_lnum_off -= SOURCING_LNUM;
886 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
887 goto theend;
888 while (sourcing_lnum_off-- > 0)
889 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
890 }
891
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200892 nesting_def[0] = vim9_function;
893 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100894 getline_options = vim9_function
895 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
896 for (;;)
897 {
898 char_u *theline;
899 char_u *p;
900 char_u *arg;
901
902 if (KeyTyped)
903 {
904 msg_scroll = TRUE;
905 saved_wait_return = FALSE;
906 }
907 need_wait_return = FALSE;
908
909 if (line_arg != NULL)
910 {
911 // Use eap->arg, split up in parts by line breaks.
912 theline = line_arg;
913 p = vim_strchr(theline, '\n');
914 if (p == NULL)
915 line_arg += STRLEN(line_arg);
916 else
917 {
918 *p = NUL;
919 line_arg = p + 1;
920 }
921 }
922 else
923 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000924 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100925 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100926 }
927 if (KeyTyped)
928 lines_left = Rows - 1;
929 if (theline == NULL)
930 {
931 // Use the start of the function for the line number.
932 SOURCING_LNUM = sourcing_lnum_top;
933 if (skip_until != NULL)
934 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200935 else if (nesting_inline[nesting])
936 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100937 else if (eap->cmdidx == CMD_def)
938 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100939 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000940 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100941 goto theend;
942 }
943
944 // Detect line continuation: SOURCING_LNUM increased more than one.
945 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
946 if (SOURCING_LNUM < sourcing_lnum_off)
947 sourcing_lnum_off -= SOURCING_LNUM;
948 else
949 sourcing_lnum_off = 0;
950
951 if (skip_until != NULL)
952 {
953 // Don't check for ":endfunc"/":enddef" between
954 // * ":append" and "."
955 // * ":python <<EOF" and "EOF"
956 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
957 if (heredoc_trimmed == NULL
958 || (is_heredoc && skipwhite(theline) == theline)
959 || STRNCMP(theline, heredoc_trimmed,
960 STRLEN(heredoc_trimmed)) == 0)
961 {
962 if (heredoc_trimmed == NULL)
963 p = theline;
964 else if (is_heredoc)
965 p = skipwhite(theline) == theline
966 ? theline : theline + STRLEN(heredoc_trimmed);
967 else
968 p = theline + STRLEN(heredoc_trimmed);
969 if (STRCMP(p, skip_until) == 0)
970 {
971 VIM_CLEAR(skip_until);
972 VIM_CLEAR(heredoc_trimmed);
973 getline_options = vim9_function
974 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
975 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200976
977 if (heredoc_concat_len > 0)
978 {
979 // Replace the starting line with all the concatenated
980 // lines.
981 ga_concat(&heredoc_ga, theline);
982 vim_free(((char_u **)(newlines->ga_data))[
983 heredoc_concat_len - 1]);
984 ((char_u **)(newlines->ga_data))[
985 heredoc_concat_len - 1] = heredoc_ga.ga_data;
986 ga_init(&heredoc_ga);
987 heredoc_concat_len = 0;
988 theline += STRLEN(theline); // skip the "EOF"
989 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100990 }
991 }
992 }
993 else
994 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200995 int c;
996 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +0000997 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100998
999 // skip ':' and blanks
1000 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
1001 ;
1002
1003 // Check for "endfunction", "enddef" or "}".
1004 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +00001005 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001006 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001007 ? *p == '}'
1008 : (checkforcmd(&p, nesting_def[nesting]
1009 ? "enddef" : "endfunction", 4)
1010 && *p != ':'))
1011 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00001012 if (!nesting_inline[nesting] && nesting_def[nesting]
1013 && p < cmd + 6)
1014 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001015 if (nesting-- == 0)
1016 {
1017 char_u *nextcmd = NULL;
1018
1019 if (*p == '|' || *p == '}')
1020 nextcmd = p + 1;
1021 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
1022 nextcmd = line_arg;
1023 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001024 && (vim9_function || p_verbose > 0))
1025 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +02001026 SOURCING_LNUM = sourcing_lnum_top
1027 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001028 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +00001029 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001030 else
1031 give_warning2((char_u *)
1032 _("W22: Text found after :endfunction: %s"),
1033 p, TRUE);
1034 }
1035 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001036 {
1037 // Another command follows. If the line came from "eap"
1038 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001039 // change "eap->cmdlinep" to point to the last fetched
1040 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001041 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001042 if (lines_to_free->ga_len > 0
1043 && *eap->cmdlinep !=
1044 ((char_u **)lines_to_free->ga_data)
1045 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001046 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001047 // *cmdlinep will be freed later, thus remove the
1048 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001049 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001050 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
1051 [lines_to_free->ga_len - 1];
1052 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001053 }
1054 }
1055 break;
1056 }
1057 }
1058
1059 // Check for mismatched "endfunc" or "enddef".
1060 // We don't check for "def" inside "func" thus we also can't check
1061 // for "enddef".
1062 // We continue to find the end of the function, although we might
1063 // not find it.
1064 else if (nesting_def[nesting])
1065 {
1066 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1067 emsg(_(e_mismatched_endfunction));
1068 }
1069 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1070 emsg(_(e_mismatched_enddef));
1071
1072 // Increase indent inside "if", "while", "for" and "try", decrease
1073 // at "end".
1074 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1075 indent -= 2;
1076 else if (STRNCMP(p, "if", 2) == 0
1077 || STRNCMP(p, "wh", 2) == 0
1078 || STRNCMP(p, "for", 3) == 0
1079 || STRNCMP(p, "try", 3) == 0)
1080 indent += 2;
1081
1082 // Check for defining a function inside this function.
1083 // Only recognize "def" inside "def", not inside "function",
1084 // For backwards compatibility, see Test_function_python().
1085 c = *p;
1086 if (is_function_cmd(&p)
1087 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1088 {
1089 if (*p == '!')
1090 p = skipwhite(p + 1);
1091 p += eval_fname_script(p);
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001092 vim_free(trans_function_name(&p, NULL, TRUE, 0));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001093 if (*skipwhite(p) == '(')
1094 {
1095 if (nesting == MAX_FUNC_NESTING - 1)
1096 emsg(_(e_function_nesting_too_deep));
1097 else
1098 {
1099 ++nesting;
1100 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001101 nesting_inline[nesting] = FALSE;
1102 indent += 2;
1103 }
1104 }
1105 }
1106
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001107 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001108 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001109 // Not a comment line: check for nested inline function.
1110 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001111 while (end > p && VIM_ISWHITE(*end))
1112 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001113 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001114 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001115 int is_block;
1116
1117 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001118 --end;
1119 while (end > p && VIM_ISWHITE(*end))
1120 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001121 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1122 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001123 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001124 char_u *s = p;
1125
1126 // check for line starting with "au" for :autocmd or
1127 // "com" for :command, these can use a {} block
1128 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1129 || checkforcmd_noparen(&s, "command", 3);
1130 }
1131
1132 if (is_block)
1133 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001134 if (nesting == MAX_FUNC_NESTING - 1)
1135 emsg(_(e_function_nesting_too_deep));
1136 else
1137 {
1138 ++nesting;
1139 nesting_def[nesting] = TRUE;
1140 nesting_inline[nesting] = TRUE;
1141 indent += 2;
1142 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001143 }
1144 }
1145 }
1146
1147 // Check for ":append", ":change", ":insert". Not for :def.
1148 p = skip_range(p, FALSE, NULL);
1149 if (!vim9_function
1150 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1151 || (p[0] == 'c'
1152 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1153 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1154 && (STRNCMP(&p[3], "nge", 3) != 0
1155 || !ASCII_ISALPHA(p[6])))))))
1156 || (p[0] == 'i'
1157 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1158 && (!ASCII_ISALPHA(p[2])
1159 || (p[2] == 's'
1160 && (!ASCII_ISALPHA(p[3])
1161 || p[3] == 'e'))))))))
1162 skip_until = vim_strsave((char_u *)".");
1163
1164 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1165 arg = skipwhite(skiptowhite(p));
1166 if (arg[0] == '<' && arg[1] =='<'
1167 && ((p[0] == 'p' && p[1] == 'y'
1168 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1169 || ((p[2] == '3' || p[2] == 'x')
1170 && !ASCII_ISALPHA(p[3]))))
1171 || (p[0] == 'p' && p[1] == 'e'
1172 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1173 || (p[0] == 't' && p[1] == 'c'
1174 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1175 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1176 && !ASCII_ISALPHA(p[3]))
1177 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1178 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1179 || (p[0] == 'm' && p[1] == 'z'
1180 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1181 ))
1182 {
1183 // ":python <<" continues until a dot, like ":append"
1184 p = skipwhite(arg + 2);
1185 if (STRNCMP(p, "trim", 4) == 0)
1186 {
1187 // Ignore leading white space.
1188 p = skipwhite(p + 4);
1189 heredoc_trimmed = vim_strnsave(theline,
1190 skipwhite(theline) - theline);
1191 }
1192 if (*p == NUL)
1193 skip_until = vim_strsave((char_u *)".");
1194 else
1195 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1196 getline_options = GETLINE_NONE;
1197 is_heredoc = TRUE;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001198 if (vim9_function && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001199 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001200 }
1201
Bram Moolenaard881d152022-05-13 13:50:36 +01001202 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001203 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001204 // Check for ":cmd v =<< [trim] EOF"
1205 // and ":cmd [a, b] =<< [trim] EOF"
1206 // and "lines =<< [trim] EOF" for Vim9
1207 // Where "cmd" can be "let", "var", "final" or "const".
zeertzjqa93d9cd2023-05-02 16:25:47 +01001208 arg = p;
1209 if (checkforcmd(&arg, "let", 2)
1210 || checkforcmd(&arg, "var", 3)
1211 || checkforcmd(&arg, "final", 5)
1212 || checkforcmd(&arg, "const", 5)
1213 || vim9_function)
Bram Moolenaard881d152022-05-13 13:50:36 +01001214 {
zeertzjqa93d9cd2023-05-02 16:25:47 +01001215 while (vim_strchr((char_u *)"$@&", *arg) != NULL)
1216 ++arg;
1217 arg = skipwhite(find_name_end(arg, NULL, NULL,
1218 FNE_INCL_BR | FNE_ALLOW_CURLY));
1219 if (vim9_function && *arg == ':')
1220 arg = skipwhite(skip_type(skipwhite(arg + 1), FALSE));
1221 if (arg[0] == '=' && arg[1] == '<' && arg[2] =='<')
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001222 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001223 p = skipwhite(arg + 3);
1224 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001225 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001226 if (STRNCMP(p, "trim", 4) == 0)
1227 {
1228 // Ignore leading white space.
1229 p = skipwhite(p + 4);
1230 heredoc_trimmed = vim_strnsave(theline,
1231 skipwhite(theline) - theline);
1232 continue;
1233 }
1234 if (STRNCMP(p, "eval", 4) == 0)
1235 {
1236 // Ignore leading white space.
1237 p = skipwhite(p + 4);
1238 continue;
1239 }
1240 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001241 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001242 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1243 getline_options = GETLINE_NONE;
1244 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001245 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001246 }
1247 }
1248 }
1249
1250 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001251 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001252 goto theend;
1253
Bram Moolenaar20677332021-06-06 17:02:53 +02001254 if (heredoc_concat_len > 0)
1255 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001256 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001257 // to be used for the instruction later.
1258 ga_concat(&heredoc_ga, theline);
1259 ga_concat(&heredoc_ga, (char_u *)"\n");
1260 p = vim_strsave((char_u *)"");
1261 }
1262 else
1263 {
1264 // Copy the line to newly allocated memory. get_one_sourceline()
1265 // allocates 250 bytes per line, this saves 80% on average. The
1266 // cost is an extra alloc/free.
1267 p = vim_strsave(theline);
1268 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001269 if (p == NULL)
1270 goto theend;
1271 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1272
1273 // Add NULL lines for continuation lines, so that the line count is
1274 // equal to the index in the growarray.
1275 while (sourcing_lnum_off-- > 0)
1276 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1277
1278 // Check for end of eap->arg.
1279 if (line_arg != NULL && *line_arg == NUL)
1280 line_arg = NULL;
1281 }
1282
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001283 // Return OK when no error was detected.
1284 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001285 ret = OK;
1286
1287theend:
1288 vim_free(skip_until);
1289 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001290 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001291 need_wait_return |= saved_wait_return;
1292 return ret;
1293}
1294
1295/*
1296 * Handle the body of a lambda. *arg points to the "{", process statements
1297 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001298 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001299 * When successful "rettv" is set to a funcref.
1300 */
1301 static int
1302lambda_function_body(
1303 char_u **arg,
1304 typval_T *rettv,
1305 evalarg_T *evalarg,
1306 garray_T *newargs,
1307 garray_T *argtypes,
1308 int varargs,
1309 garray_T *default_args,
1310 char_u *ret_type)
1311{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001312 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001313 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001314 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001315 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001316 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001317 exarg_T eap;
1318 garray_T newlines;
1319 char_u *cmdline = NULL;
1320 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001321 partial_T *pt;
1322 char_u *name;
1323 int lnum_save = -1;
1324 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1325
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001326 *arg = skipwhite(*arg + 1);
1327 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001328 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001329 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001330 return FAIL;
1331 }
1332
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001333 CLEAR_FIELD(eap);
1334 eap.cmdidx = CMD_block;
1335 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001336 eap.cmdlinep = &cmdline;
1337 eap.skip = !evaluate;
1338 if (evalarg->eval_cctx != NULL)
1339 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1340 else
1341 {
1342 eap.getline = evalarg->eval_getline;
1343 eap.cookie = evalarg->eval_cookie;
1344 }
1345
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001346 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001347 if (get_function_body(&eap, &newlines, NULL,
1348 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001349 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001350
1351 // When inside a lambda must add the function lines to evalarg.eval_ga.
1352 evalarg->eval_break_count += newlines.ga_len;
1353 if (gap->ga_itemsize > 0)
1354 {
1355 int idx;
1356 char_u *last;
1357 size_t plen;
1358 char_u *pnl;
1359
1360 for (idx = 0; idx < newlines.ga_len; ++idx)
1361 {
1362 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1363
Bram Moolenaarecb66452021-05-18 15:09:18 +02001364 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001365 goto erret;
1366
1367 // Going to concatenate the lines after parsing. For an empty or
1368 // comment line use an empty string.
1369 // Insert NL characters at the start of each line, the string will
1370 // be split again later in .get_lambda_tv().
1371 if (*p == NUL || vim9_comment_start(p))
1372 p = (char_u *)"";
1373 plen = STRLEN(p);
1374 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1375 if (pnl != NULL)
1376 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001377 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1378 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001379 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001380 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001381 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001382 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001383 // more is following after the "}", which was skipped
1384 last = cmdline;
1385 else
1386 // nothing is following the "}"
1387 last = (char_u *)"}";
1388 plen = STRLEN(last);
1389 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1390 if (pnl != NULL)
1391 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001392 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1393 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001394 }
1395
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001396 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001397 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001398 garray_T *tfgap = &evalarg->eval_tofree_ga;
1399
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001400 // Something comes after the "}".
1401 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001402
1403 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001404 if (ga_grow(tfgap, 1) == OK)
1405 {
1406 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1407 evalarg->eval_using_cmdline = TRUE;
1408 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001409 }
1410 else
1411 *arg = (char_u *)"";
1412
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001413 if (!evaluate)
1414 {
1415 ret = OK;
1416 goto erret;
1417 }
1418
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001419 name = get_lambda_name();
Bram Moolenaare01e5212023-01-08 20:31:18 +00001420 ufunc = alloc_ufunc(name);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001421 if (ufunc == NULL)
1422 goto erret;
1423 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001424 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001425 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001426 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001427 ufunc->uf_refcount = 1;
1428 ufunc->uf_args = *newargs;
1429 newargs->ga_data = NULL;
1430 ufunc->uf_def_args = *default_args;
1431 default_args->ga_data = NULL;
1432 ufunc->uf_func_type = &t_func_any;
1433
1434 // error messages are for the first function line
1435 lnum_save = SOURCING_LNUM;
1436 SOURCING_LNUM = sourcing_lnum_top;
1437
1438 // parse argument types
h-eastb895b0f2023-09-24 15:46:31 +02001439 if (parse_argument_types(ufunc, argtypes, varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001440 {
1441 SOURCING_LNUM = lnum_save;
1442 goto erret;
1443 }
1444
1445 // parse the return type, if any
1446 if (parse_return_type(ufunc, ret_type) == FAIL)
1447 goto erret;
1448
1449 pt = ALLOC_CLEAR_ONE(partial_T);
1450 if (pt == NULL)
1451 goto erret;
1452 pt->pt_func = ufunc;
1453 pt->pt_refcount = 1;
1454
1455 ufunc->uf_lines = newlines;
1456 newlines.ga_data = NULL;
1457 if (sandbox)
1458 ufunc->uf_flags |= FC_SANDBOX;
1459 if (!ASCII_ISUPPER(*ufunc->uf_name))
1460 ufunc->uf_flags |= FC_VIM9;
1461 ufunc->uf_script_ctx = current_sctx;
1462 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1463 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1464 set_function_type(ufunc);
1465
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001466 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1467
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001468 rettv->vval.v_partial = pt;
1469 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001470 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001471 ret = OK;
1472
1473erret:
1474 if (lnum_save >= 0)
1475 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001476 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001477 if (newargs != NULL)
1478 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001479 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001480 if (ufunc != NULL)
1481 {
1482 func_clear(ufunc, TRUE);
1483 func_free(ufunc, TRUE);
1484 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001485 return ret;
1486}
1487
1488/*
1489 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001490 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001491 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001492 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1493 */
1494 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001495get_lambda_tv(
1496 char_u **arg,
1497 typval_T *rettv,
1498 int types_optional,
1499 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001500{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001501 int evaluate = evalarg != NULL
1502 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001503 garray_T newargs;
1504 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001505 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001506 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001507 garray_T default_args;
h-eastb895b0f2023-09-24 15:46:31 +02001508 garray_T arg_objm;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001509 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001510 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001511 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001512 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001513 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001514 char_u *s;
1515 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001516 int *old_eval_lavars = eval_lavars_used;
1517 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001518 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001519 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001520 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001521 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001522 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001523 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001524
Bram Moolenaar4525a572022-02-13 11:57:33 +00001525 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001526 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001527
1528 ga_init(&newargs);
1529 ga_init(&newlines);
1530
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001531 // First, check if this is really a lambda expression. "->" or "=>" must
1532 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001533 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001534 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001535 types_optional ? &argtypes : NULL, types_optional,
1536 types_optional ? &arg_objm : NULL, evalarg,
1537 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001538 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001539 {
1540 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001541 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001542 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001543 ga_clear(&arg_objm);
1544 }
Bram Moolenaar0346b792021-01-31 22:18:29 +01001545 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001546 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001547
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001548 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001549 if (evaluate)
1550 pnewargs = &newargs;
1551 else
1552 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001553 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001554 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
h-eastb895b0f2023-09-24 15:46:31 +02001555 types_optional ? &argtypes : NULL, types_optional,
1556 types_optional ? &arg_objm : NULL, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001557 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001558 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001559 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001560 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001561 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001562 {
1563 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001564 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001565 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001566 ga_clear(&arg_objm);
1567 }
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001568 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001569 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001570 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001571 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001572
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001573 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1574 if (ret_type != NULL)
1575 {
1576 ret_type = vim_strsave(ret_type);
1577 tofree2 = ret_type;
1578 }
1579
Bram Moolenaare38eab22019-12-05 21:50:01 +01001580 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001581 if (evaluate)
1582 eval_lavars_used = &eval_lavars;
1583
Bram Moolenaar65c44152020-12-24 15:14:01 +01001584 *arg = skipwhite_and_linebreak(*arg, evalarg);
1585
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001586 // Recognize "{" as the start of a function body.
1587 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001588 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001589 if (evalarg == NULL)
1590 // cannot happen?
1591 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001592 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001593 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1594 types_optional ? &argtypes : NULL, varargs,
1595 &default_args, ret_type) == FAIL)
1596 goto errret;
1597 goto theend;
1598 }
1599 if (default_args.ga_len > 0)
1600 {
1601 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001602 goto errret;
1603 }
1604
Bram Moolenaare38eab22019-12-05 21:50:01 +01001605 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001606 start = *arg;
1607 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001608 if (ret == FAIL)
1609 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001610
Bram Moolenaar65c44152020-12-24 15:14:01 +01001611 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001612 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001613 *arg = skipwhite_and_linebreak(*arg, evalarg);
1614 if (**arg != '}')
1615 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001616 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001617 goto errret;
1618 }
1619 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001620 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001621
1622 if (evaluate)
1623 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001624 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001625 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001626 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001627 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001628 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001629
Bram Moolenaare01e5212023-01-08 20:31:18 +00001630 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001631 if (fp == NULL)
1632 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001633 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001634 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001635 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001636 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001637
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001638 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001639 if (ga_grow(&newlines, 1) == FAIL)
1640 goto errret;
1641
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001642 // If there are line breaks, we need to split up the string.
1643 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001644 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001645 line_end = end;
1646
1647 // Add "return " before the expression (or the first line).
1648 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001649 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001650 if (p == NULL)
1651 goto errret;
1652 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1653 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001654 vim_strncpy(p + 7, start, line_end - start);
1655
1656 if (line_end != end)
1657 {
1658 // Add more lines, split by line breaks. Thus is used when a
1659 // lambda with { cmds } is encountered.
1660 while (*line_end == '\n')
1661 {
1662 if (ga_grow(&newlines, 1) == FAIL)
1663 goto errret;
1664 start = line_end + 1;
1665 line_end = vim_strchr(start, '\n');
1666 if (line_end == NULL)
1667 line_end = end;
1668 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1669 vim_strnsave(start, line_end - start);
1670 }
1671 }
1672
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001673 if (strstr((char *)p + 7, "a:") == NULL)
1674 // No a: variables are used for sure.
1675 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001676
1677 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001678 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001679 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001680 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001681 if (types_optional)
1682 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001683 if (parse_argument_types(fp, &argtypes,
h-eastb895b0f2023-09-24 15:46:31 +02001684 vim9script && varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001685 goto errret;
1686 if (ret_type != NULL)
1687 {
1688 fp->uf_ret_type = parse_type(&ret_type,
1689 &fp->uf_type_list, TRUE);
1690 if (fp->uf_ret_type == NULL)
1691 goto errret;
1692 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001693 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001694 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001695 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001696
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001697 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001698 if (current_funccal != NULL && eval_lavars)
1699 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001700 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001701 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001702 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001703 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001704
1705#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001706 if (prof_def_func())
1707 func_do_profile(fp);
1708#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001709 if (sandbox)
1710 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001711 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001712 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001713 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001714 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001715 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001716 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001717 // Use the line number of the arguments.
1718 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001719
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001720 function_using_block_scopes(fp, evalarg->eval_cstack);
1721
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001722 pt->pt_func = fp;
1723 pt->pt_refcount = 1;
1724 rettv->vval.v_partial = pt;
1725 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001726
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001727 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001728 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001729
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001730theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001731 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001732 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001733 if (types_optional)
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001734 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001735 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001736 ga_clear(&arg_objm);
1737 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001738
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001739 return OK;
1740
1741errret:
1742 ga_clear_strings(&newargs);
1743 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001744 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001745 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001746 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001747 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001748 ga_clear(&arg_objm);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001749 if (fp != NULL)
1750 vim_free(fp->uf_arg_types);
1751 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001752 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001753 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001754 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001755 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001756 return FAIL;
1757}
1758
1759/*
1760 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1761 * name it contains, otherwise return "name".
1762 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1763 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001764 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1765 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001766 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001767 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001768 */
1769 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001770deref_func_name(
1771 char_u *name,
1772 int *lenp,
1773 partial_T **partialp,
1774 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001775 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001776 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001777 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001778{
1779 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001780 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001781 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001782 char_u *s = NULL;
1783 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001784 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001785
1786 if (partialp != NULL)
1787 *partialp = NULL;
1788
1789 cc = name[*lenp];
1790 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001791
Bram Moolenaar71f21932022-01-07 18:20:55 +00001792 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001793 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001794 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001795 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001796 tv = &v->di_tv;
1797 }
1798 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1799 {
1800 imported_T *import;
1801 char_u *p = name;
1802 int len = *lenp;
1803
1804 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001805 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001806 p = name + 2;
1807 len -= 2;
1808 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001809 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001810
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001811 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001812 if (import != NULL)
1813 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001814 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001815 if (new_function)
1816 semsg(_(e_redefining_imported_item_str), name);
1817 else
1818 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001819 name[len] = cc;
1820 *lenp = 0;
1821 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001822 }
1823 }
1824
1825 if (tv != NULL)
1826 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001827 if (found_var != NULL)
1828 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001829 if (tv->v_type == VAR_FUNC)
1830 {
1831 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001832 {
1833 *lenp = 0;
1834 return (char_u *)""; // just in case
1835 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001836 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001837 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001838 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001839
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001840 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001841 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001842 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001843
1844 if (pt == NULL)
1845 {
1846 *lenp = 0;
1847 return (char_u *)""; // just in case
1848 }
1849 if (partialp != NULL)
1850 *partialp = pt;
1851 s = partial_name(pt);
1852 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001853 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001854
1855 if (s != NULL)
1856 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001857 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001858 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001859 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001860
1861 if (sv != NULL)
1862 *type = sv->sv_type;
1863 }
1864 return s;
1865 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001866 }
1867
1868 return name;
1869}
1870
1871/*
1872 * Give an error message with a function name. Handle <SNR> things.
1873 * "ermsg" is to be passed without translation, use N_() instead of _().
1874 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001875 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001876emsg_funcname(char *ermsg, char_u *name)
1877{
Bram Moolenaar54598062022-01-13 12:05:09 +00001878 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001879
Bram Moolenaar54598062022-01-13 12:05:09 +00001880 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001881 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001882 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001883 if (p != name)
1884 vim_free(p);
1885}
1886
1887/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001888 * Get function arguments at "*arg" and advance it.
1889 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001890 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001891 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001892 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001893get_func_arguments(
1894 char_u **arg,
1895 evalarg_T *evalarg,
1896 int partial_argc,
1897 typval_T *argvars,
1898 int *argcount)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001899{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001900 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001901 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001902 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001903 int evaluate = evalarg == NULL
1904 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001905
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001906 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001907 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001908 // skip the '(' or ',' and possibly line breaks
1909 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1910
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001911 if (*argp == ')' || *argp == ',' || *argp == NUL)
1912 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001913 if (eval1(&argp, &argvars[*argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001914 {
1915 ret = FAIL;
1916 break;
1917 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001918 ++*argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001919 // The comma should come right after the argument, but this wasn't
1920 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001921 if (vim9script)
1922 {
1923 if (*argp != ',' && *skipwhite(argp) == ',')
1924 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001925 if (evaluate)
1926 semsg(_(e_no_white_space_allowed_before_str_str),
1927 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001928 ret = FAIL;
1929 break;
1930 }
1931 }
1932 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001933 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001934 if (*argp != ',')
1935 break;
Christian Brabandt00cb2472023-09-05 20:46:25 +02001936 if (vim9script && !IS_WHITE_NL_OR_NUL(argp[1]))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001937 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001938 if (evaluate)
1939 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001940 ret = FAIL;
1941 break;
1942 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001943 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001944
Bram Moolenaare6b53242020-07-01 17:28:33 +02001945 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001946 if (*argp == ')')
1947 ++argp;
1948 else
1949 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001950 *arg = argp;
1951 return ret;
1952}
1953
1954/*
1955 * Call a function and put the result in "rettv".
1956 * Return OK or FAIL.
1957 */
1958 int
1959get_func_tv(
1960 char_u *name, // name of the function
1961 int len, // length of "name" or -1 to use strlen()
1962 typval_T *rettv,
1963 char_u **arg, // argument, pointing to the '('
1964 evalarg_T *evalarg, // for line continuation
1965 funcexe_T *funcexe) // various values
1966{
1967 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001968 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001969 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1970 int argcount = 0; // number of arguments found
1971 int vim9script = in_vim9script();
1972 int evaluate = evalarg == NULL
1973 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
1974
1975 argp = *arg;
1976 ret = get_func_arguments(&argp, evalarg,
1977 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
1978 argvars, &argcount);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001979
1980 if (ret == OK)
1981 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001982 int i = 0;
1983 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001984
1985 if (get_vim_var_nr(VV_TESTING))
1986 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001987 // Prepare for calling test_garbagecollect_now(), need to know
1988 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001989 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001990 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001991 for (i = 0; i < argcount; ++i)
1992 if (ga_grow(&funcargs, 1) == OK)
1993 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1994 &argvars[i];
1995 }
1996
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001997 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00001998 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001999 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002000 // An error in a builtin function does not return FAIL, but we do
2001 // want to abort further processing if an error was given.
2002 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002003 clear_tv(rettv);
2004 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002005
2006 funcargs.ga_len -= i;
2007 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002008 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002009 {
2010 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002011 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002012 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00002013 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002014 }
2015
2016 while (--argcount >= 0)
2017 clear_tv(&argvars[argcount]);
2018
Bram Moolenaar4525a572022-02-13 11:57:33 +00002019 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02002020 *arg = argp;
2021 else
2022 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002023 return ret;
2024}
2025
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002026/*
2027 * Return TRUE if "p" starts with "<SID>" or "s:".
2028 * Only works if eval_fname_script() returned non-zero for "p"!
2029 */
2030 static int
2031eval_fname_sid(char_u *p)
2032{
2033 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
2034}
2035
2036/*
2037 * In a script change <SID>name() and s:name() to K_SNR 123_name().
2038 * Change <SNR>123_name() to K_SNR 123_name().
2039 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002040 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002041 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002042 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00002043fname_trans_sid(
2044 char_u *name,
2045 char_u *fname_buf,
2046 char_u **tofree,
2047 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002048{
2049 int llen;
2050 char_u *fname;
2051 int i;
2052
2053 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002054 if (llen == 0)
2055 return name; // no prefix
2056
2057 fname_buf[0] = K_SPECIAL;
2058 fname_buf[1] = KS_EXTRA;
2059 fname_buf[2] = (int)KE_SNR;
2060 i = 3;
2061 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002062 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002063 if (current_sctx.sc_sid <= 0)
2064 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002065 else
2066 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002067 sprintf((char *)fname_buf + 3, "%ld_",
2068 (long)current_sctx.sc_sid);
2069 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002070 }
2071 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002072 if (i + STRLEN(name + llen) < FLEN_FIXED)
2073 {
2074 STRCPY(fname_buf + i, name + llen);
2075 fname = fname_buf;
2076 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002077 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002078 {
2079 fname = alloc(i + STRLEN(name + llen) + 1);
2080 if (fname == NULL)
2081 *error = FCERR_OTHER;
2082 else
2083 {
2084 *tofree = fname;
2085 mch_memmove(fname, fname_buf, (size_t)i);
2086 STRCPY(fname + i, name + llen);
2087 }
2088 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002089 return fname;
2090}
2091
2092/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002093 * Concatenate the script ID and function name into "<SNR>99_name".
2094 * "buffer" must have size MAX_FUNC_NAME_LEN.
2095 */
2096 void
2097func_name_with_sid(char_u *name, int sid, char_u *buffer)
2098{
2099 // A script-local function is stored as "<SNR>99_name".
2100 buffer[0] = K_SPECIAL;
2101 buffer[1] = KS_EXTRA;
2102 buffer[2] = (int)KE_SNR;
2103 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2104 (long)sid, name);
2105}
2106
2107/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108 * Find a function "name" in script "sid".
2109 */
2110 static ufunc_T *
2111find_func_with_sid(char_u *name, int sid)
2112{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002113 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002114 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002115
Bram Moolenaarb8822442022-01-11 15:24:05 +00002116 if (!SCRIPT_ID_VALID(sid))
2117 return NULL; // not in a script
2118
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002119 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002120 hi = hash_find(&func_hashtab, buffer);
2121 if (!HASHITEM_EMPTY(hi))
2122 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002123 return NULL;
2124}
2125
2126/*
2127 * Find a function "name" in script "sid" prefixing the autoload prefix.
2128 */
2129 static ufunc_T *
2130find_func_with_prefix(char_u *name, int sid)
2131{
2132 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002133 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002134 scriptitem_T *si;
2135
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002136 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002137 return NULL; // already has the prefix
2138 if (!SCRIPT_ID_VALID(sid))
2139 return NULL; // not in a script
2140 si = SCRIPT_ITEM(sid);
2141 if (si->sn_autoload_prefix != NULL)
2142 {
2143 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2144 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002145 char_u *namep;
2146
2147 // skip a "<SNR>99_" prefix
2148 namep = untrans_function_name(name);
2149 if (namep == NULL)
2150 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002151
2152 // An exported function in an autoload script is stored as
2153 // "dir#path#name".
2154 if (len < sizeof(buffer))
2155 auto_name = buffer;
2156 else
2157 auto_name = alloc(len);
2158 if (auto_name != NULL)
2159 {
2160 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002161 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002162 hi = hash_find(&func_hashtab, auto_name);
2163 if (auto_name != buffer)
2164 vim_free(auto_name);
2165 if (!HASHITEM_EMPTY(hi))
2166 return HI2UF(hi);
2167 }
2168 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169
2170 return NULL;
2171}
2172
2173/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002174 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002175 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2176 * functions.
2177 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002178 * Return NULL for unknown function.
2179 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002180 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002181find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002182{
2183 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002184 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002186 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002188 // Find script-local function before global one.
2189 if (in_vim9script() && eval_isnamec1(*name)
2190 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002192 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2193 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002194 if (func != NULL)
2195 return func;
2196 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002197 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2198 {
2199 char_u *p = name + 5;
2200 long sid;
2201
2202 // printable "<SNR>123_Name" form
2203 sid = getdigits(&p);
2204 if (*p == '_')
2205 {
2206 func = find_func_with_sid(p + 1, (int)sid);
2207 if (func != NULL)
2208 return func;
2209 }
2210 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002211 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002212
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002213 if ((flags & FFED_NO_GLOBAL) == 0)
2214 {
2215 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002216 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002217 if (!HASHITEM_EMPTY(hi))
2218 return HI2UF(hi);
2219 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220
Bram Moolenaarb8822442022-01-11 15:24:05 +00002221 // Find autoload function if this is an autoload script.
2222 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2223 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224}
2225
2226/*
2227 * Find a function by name, return pointer to it in ufuncs.
2228 * "cctx" is passed in a :def function to find imported functions.
2229 * Return NULL for unknown or dead function.
2230 */
2231 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002232find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002233{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002234 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235
2236 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2237 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002238 return NULL;
2239}
2240
2241/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002242 * Return TRUE if "ufunc" is a global function.
2243 */
2244 int
2245func_is_global(ufunc_T *ufunc)
2246{
2247 return ufunc->uf_name[0] != K_SPECIAL;
2248}
2249
2250/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002251 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2252 */
2253 int
2254func_requires_g_prefix(ufunc_T *ufunc)
2255{
2256 return ufunc->uf_name[0] != K_SPECIAL
2257 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002258 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
2259 && !isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002260}
2261
2262/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002263 * Copy the function name of "fp" to buffer "buf".
2264 * "buf" must be able to hold the function name plus three bytes.
2265 * Takes care of script-local function names.
2266 */
2267 static void
2268cat_func_name(char_u *buf, ufunc_T *fp)
2269{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002270 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002271 {
2272 STRCPY(buf, "<SNR>");
2273 STRCAT(buf, fp->uf_name + 3);
2274 }
2275 else
2276 STRCPY(buf, fp->uf_name);
2277}
2278
2279/*
2280 * Add a number variable "name" to dict "dp" with value "nr".
2281 */
2282 static void
2283add_nr_var(
2284 dict_T *dp,
2285 dictitem_T *v,
2286 char *name,
2287 varnumber_T nr)
2288{
2289 STRCPY(v->di_key, name);
2290 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002291 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002292 v->di_tv.v_type = VAR_NUMBER;
2293 v->di_tv.v_lock = VAR_FIXED;
2294 v->di_tv.vval.v_number = nr;
2295}
2296
2297/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002298 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002299 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002300 static void
2301free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002302{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002303 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002304
Bram Moolenaarca16c602022-09-06 18:57:08 +01002305 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002306 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002307 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002308
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002309 // When garbage collecting a funccall_T may be freed before the
2310 // function that references it, clear its uf_scoped field.
2311 // The function may have been redefined and point to another
2312 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002313 if (fp != NULL && fp->uf_scoped == fc)
2314 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002315 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002316 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002317
Bram Moolenaarca16c602022-09-06 18:57:08 +01002318 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002319 vim_free(fc);
2320}
2321
2322/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002323 * Free "fc" and what it contains.
2324 * Can be called only when "fc" is kept beyond the period of it called,
2325 * i.e. after cleanup_function_call(fc).
2326 */
2327 static void
2328free_funccal_contents(funccall_T *fc)
2329{
2330 listitem_T *li;
2331
2332 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002333 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002334
2335 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002336 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002337
2338 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002339 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002340 clear_tv(&li->li_tv);
2341
2342 free_funccal(fc);
2343}
2344
2345/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002346 * Handle the last part of returning from a function: free the local hashtable.
2347 * Unless it is still in use by a closure.
2348 */
2349 static void
2350cleanup_function_call(funccall_T *fc)
2351{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002352 int may_free_fc = fc->fc_refcount <= 0;
2353 int free_fc = TRUE;
2354
Bram Moolenaarca16c602022-09-06 18:57:08 +01002355 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002356
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002357 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002358 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2359 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002360 else
2361 free_fc = FALSE;
2362
2363 // If the a:000 list and the l: and a: dicts are not referenced and
2364 // there is no closure using it, we can free the funccall_T and what's
2365 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002366 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2367 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002368 else
2369 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002370 int todo;
2371 hashitem_T *hi;
2372 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002373
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002374 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002375
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002376 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002377 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002378 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002379 {
2380 if (!HASHITEM_EMPTY(hi))
2381 {
2382 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002383 di = HI2DI(hi);
2384 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002385 }
2386 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002387 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002388
Bram Moolenaarca16c602022-09-06 18:57:08 +01002389 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2390 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002391 else
2392 {
2393 listitem_T *li;
2394
2395 free_fc = FALSE;
2396
2397 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002398 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002399 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002400 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002401
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002402 if (free_fc)
2403 free_funccal(fc);
2404 else
2405 {
2406 static int made_copy = 0;
2407
2408 // "fc" is still in use. This can happen when returning "a:000",
2409 // assigning "l:" to a global variable or defining a closure.
2410 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002411 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002412 previous_funccal = fc;
2413
2414 if (want_garbage_collect)
2415 // If garbage collector is ready, clear count.
2416 made_copy = 0;
2417 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002418 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002419 // We have made a lot of copies, worth 4 Mbyte. This can happen
2420 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002421 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002422 // too much memory.
2423 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002424 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002425 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002426 }
2427}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002428
2429/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002430 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2431 */
2432 static int
2433numbered_function(char_u *name)
2434{
2435 return isdigit(*name)
2436 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2437}
2438
2439/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002440 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002441 * 1. ordinary names, function defined with :function or :def;
2442 * can start with "<SNR>123_" literally or with K_SPECIAL.
2443 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002444 * For the first we only count the name stored in func_hashtab as a reference,
2445 * using function() does not count as a reference, because the function is
2446 * looked up by name.
2447 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002448 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002449func_name_refcount(char_u *name)
2450{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002451 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002452}
2453
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454/*
2455 * Unreference "fc": decrement the reference count and free it when it
2456 * becomes zero. "fp" is detached from "fc".
2457 * When "force" is TRUE we are exiting.
2458 */
2459 static void
2460funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2461{
2462 funccall_T **pfc;
2463 int i;
2464
2465 if (fc == NULL)
2466 return;
2467
2468 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002469 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2470 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2471 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2472 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 {
2474 if (fc == *pfc)
2475 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002476 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477 free_funccal_contents(fc);
2478 return;
2479 }
2480 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002481 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2482 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2483 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484}
2485
2486/*
2487 * Remove the function from the function hashtable. If the function was
2488 * deleted while it still has references this was already done.
2489 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2490 */
2491 static int
2492func_remove(ufunc_T *fp)
2493{
2494 hashitem_T *hi;
2495
2496 // Return if it was already virtually deleted.
2497 if (fp->uf_flags & FC_DEAD)
2498 return FALSE;
2499
2500 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002501 if (HASHITEM_EMPTY(hi))
2502 return FALSE;
2503
2504 // When there is a def-function index do not actually remove the
2505 // function, so we can find the index when defining the function again.
2506 // Do remove it when it's a copy.
2507 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002509 fp->uf_flags |= FC_DEAD;
2510 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002512 hash_remove(&func_hashtab, hi, "remove function");
2513 fp->uf_flags |= FC_DELETED;
2514 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515}
2516
2517 static void
2518func_clear_items(ufunc_T *fp)
2519{
2520 ga_clear_strings(&(fp->uf_args));
2521 ga_clear_strings(&(fp->uf_def_args));
2522 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002524 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002525 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002526 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002527
2528 // Increment the refcount of this function to avoid it being freed
2529 // recursively when the partial is freed.
2530 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002531 partial_unref(fp->uf_partial);
2532 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002533 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002534
2535#ifdef FEAT_LUA
2536 if (fp->uf_cb_free != NULL)
2537 {
2538 fp->uf_cb_free(fp->uf_cb_state);
2539 fp->uf_cb_free = NULL;
2540 }
2541
2542 fp->uf_cb_state = NULL;
2543 fp->uf_cb = NULL;
2544#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545#ifdef FEAT_PROFILE
2546 VIM_CLEAR(fp->uf_tml_count);
2547 VIM_CLEAR(fp->uf_tml_total);
2548 VIM_CLEAR(fp->uf_tml_self);
2549#endif
2550}
2551
2552/*
2553 * Free all things that a function contains. Does not free the function
2554 * itself, use func_free() for that.
2555 * When "force" is TRUE we are exiting.
2556 */
2557 static void
2558func_clear(ufunc_T *fp, int force)
2559{
2560 if (fp->uf_cleared)
2561 return;
2562 fp->uf_cleared = TRUE;
2563
2564 // clear this function
2565 func_clear_items(fp);
2566 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002567 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568}
2569
2570/*
2571 * Free a function and remove it from the list of functions. Does not free
2572 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002573 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002574 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002576 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002577func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578{
2579 // Only remove it when not done already, otherwise we would remove a newer
2580 // version of the function with the same name.
2581 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2582 func_remove(fp);
2583
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002584 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002585 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002586 if (fp->uf_dfunc_idx > 0)
2587 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002588 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002590 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002591 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002592 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593}
2594
2595/*
2596 * Free all things that a function contains and free the function itself.
2597 * When "force" is TRUE we are exiting.
2598 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002599 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600func_clear_free(ufunc_T *fp, int force)
2601{
2602 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002603 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2604 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002605 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002606 else
2607 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608}
2609
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002610/*
2611 * Copy already defined function "lambda" to a new function with name "global".
2612 * This is for when a compiled function defines a global function.
2613 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002614 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002615copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002616 char_u *lambda,
2617 char_u *global,
2618 loopvarinfo_T *loopvarinfo,
2619 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002620{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002621 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002622 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002623
2624 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002625 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002626 semsg(_(e_lambda_function_not_found_str), lambda);
2627 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002628 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002629
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002630 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002631 if (fp != NULL)
2632 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002633 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002634 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002635 return FAIL;
2636 }
2637
Bram Moolenaare01e5212023-01-08 20:31:18 +00002638 fp = alloc_ufunc(global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002639 if (fp == NULL)
2640 return FAIL;
2641
2642 fp->uf_varargs = ufunc->uf_varargs;
2643 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2644 fp->uf_def_status = ufunc->uf_def_status;
2645 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2646 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2647 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2648 == FAIL
2649 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2650 goto failed;
2651
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002652 if (ufunc->uf_arg_types != NULL)
2653 {
2654 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2655 if (fp->uf_arg_types == NULL)
2656 goto failed;
2657 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2658 sizeof(type_T *) * fp->uf_args.ga_len);
2659 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002660 if (ufunc->uf_va_name != NULL)
2661 {
2662 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2663 if (fp->uf_va_name == NULL)
2664 goto failed;
2665 }
2666 fp->uf_ret_type = ufunc->uf_ret_type;
2667
2668 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002669
2670 fp->uf_name_exp = NULL;
2671 set_ufunc_name(fp, global);
2672
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002673 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002674
2675 // the referenced dfunc_T is now used one more time
2676 link_def_function(fp);
2677
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002678 // Create a partial to store the context of the function where it was
2679 // instantiated. Only needs to be done once. Do this on the original
2680 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002681 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2682 {
2683 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2684
2685 if (pt == NULL)
2686 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002687 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002688 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002689 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002690 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002691 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002692 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002693 }
2694
2695 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002696
2697failed:
2698 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002699 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002700}
2701
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002702static int funcdepth = 0;
2703
2704/*
2705 * Increment the function call depth count.
2706 * Return FAIL when going over 'maxfuncdepth'.
2707 * Otherwise return OK, must call funcdepth_decrement() later!
2708 */
2709 int
2710funcdepth_increment(void)
2711{
2712 if (funcdepth >= p_mfd)
2713 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002714 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002715 return FAIL;
2716 }
2717 ++funcdepth;
2718 return OK;
2719}
2720
2721 void
2722funcdepth_decrement(void)
2723{
2724 --funcdepth;
2725}
2726
2727/*
2728 * Get the current function call depth.
2729 */
2730 int
2731funcdepth_get(void)
2732{
2733 return funcdepth;
2734}
2735
2736/*
2737 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002738 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002739 * times as funcdepth_increment().
2740 */
2741 void
2742funcdepth_restore(int depth)
2743{
2744 funcdepth = depth;
2745}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002746
2747/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002748 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2749 * "rettv".
2750 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2751 * Returns NULL when allocation fails.
2752 */
2753 funccall_T *
2754create_funccal(ufunc_T *fp, typval_T *rettv)
2755{
2756 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2757
2758 if (fc == NULL)
2759 return NULL;
2760 fc->fc_caller = current_funccal;
2761 current_funccal = fc;
2762 fc->fc_func = fp;
2763 func_ptr_ref(fp);
2764 fc->fc_rettv = rettv;
2765 return fc;
2766}
2767
2768/*
2769 * To be called when returning from a compiled function; restores
2770 * current_funccal.
2771 */
2772 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002773remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002774{
2775 funccall_T *fc = current_funccal;
2776
2777 current_funccal = fc->fc_caller;
2778 free_funccal(fc);
2779}
2780
2781/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002782 * Call a user function.
2783 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002784 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002785call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002786 ufunc_T *fp, // pointer to function
2787 int argcount, // nr of args
2788 typval_T *argvars, // arguments
2789 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002790 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002791 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002792{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002793 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002794 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002795 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002796 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002797 funccall_T *fc;
2798 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002799 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002800 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002801 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002802 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002803 int i;
2804 int ai;
2805 int islambda = FALSE;
2806 char_u numbuf[NUMBUFLEN];
2807 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002808 typval_T *tv_to_free[MAX_FUNC_ARGS];
2809 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002810#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002811 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002812#endif
ichizok7e5fe382023-04-15 13:17:50 +01002813 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002814
Bram Moolenaarb2049902021-01-24 12:53:53 +01002815#ifdef FEAT_PROFILE
2816 CLEAR_FIELD(profile_info);
2817#endif
2818
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002819 // If depth of calling is getting too high, don't execute the function.
2820 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002821 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002822 rettv->v_type = VAR_NUMBER;
2823 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002824 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002825 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002826
Bram Moolenaare38eab22019-12-05 21:50:01 +01002827 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002828
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002829 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002830 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00002831 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002832 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002833 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002834 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2835 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002836 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002837 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002838
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002839 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002841#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002842 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002843#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002844 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002845#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002846 if (do_profiling == PROF_YES)
2847 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002848#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002849 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002850 if (call_def_function(fp, argcount, argvars, 0,
2851 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
2852 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002853 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002854#ifdef FEAT_PROFILE
2855 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002856 || (caller != NULL && caller->uf_profiling)))
2857 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002858#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002859 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002860 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002861 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 }
2863
Bram Moolenaar38453522021-11-28 22:00:12 +00002864 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002865
2866 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002867 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2868 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2869 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002870 */
2871 /*
2872 * Init l: variables.
2873 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002874 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002875 if (selfdict != NULL)
2876 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002877 // Set l:self to "selfdict". Use "name" to avoid a warning from
2878 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002879 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002880 name = v->di_key;
2881 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002882 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002883 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002884 v->di_tv.v_type = VAR_DICT;
2885 v->di_tv.v_lock = 0;
2886 v->di_tv.vval.v_dict = selfdict;
2887 ++selfdict->dv_refcount;
2888 }
2889
2890 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002891 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002892 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002893 * Set a:000 to a list with room for the "..." arguments.
2894 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002895 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002896 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002897 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002898 (varnumber_T)(argcount >= fp->uf_args.ga_len
2899 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002900 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002901 if ((fp->uf_flags & FC_NOARGS) == 0)
2902 {
2903 // Use "name" to avoid a warning from some compiler that checks the
2904 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002905 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002906 name = v->di_key;
2907 STRCPY(name, "000");
2908 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002909 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002910 v->di_tv.v_type = VAR_LIST;
2911 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002912 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002913 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002914 CLEAR_FIELD(fc->fc_l_varlist);
2915 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2916 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002917
2918 /*
2919 * Set a:firstline to "firstline" and a:lastline to "lastline".
2920 * Set a:name to named arguments.
2921 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002922 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002923 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002924 if ((fp->uf_flags & FC_NOARGS) == 0)
2925 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002926 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2927 "firstline", (varnumber_T)funcexe->fe_firstline);
2928 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2929 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002930 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002931 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002932 {
2933 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002934 typval_T def_rettv;
2935 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002936
2937 ai = i - fp->uf_args.ga_len;
2938 if (ai < 0)
2939 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002940 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002941 name = FUNCARG(fp, i);
2942 if (islambda)
2943 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002944
2945 // evaluate named argument default expression
2946 isdefault = ai + fp->uf_def_args.ga_len >= 0
2947 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2948 && argvars[i].vval.v_number == VVAL_NONE));
2949 if (isdefault)
2950 {
2951 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002952
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002953 def_rettv.v_type = VAR_NUMBER;
2954 def_rettv.vval.v_number = -1;
2955
2956 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2957 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002958 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002959 {
2960 default_arg_err = 1;
2961 break;
2962 }
2963 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002964 }
2965 else
2966 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002967 if ((fp->uf_flags & FC_NOARGS) != 0)
2968 // Bail out if no a: arguments used (in lambda).
2969 break;
2970
Bram Moolenaare38eab22019-12-05 21:50:01 +01002971 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002972 sprintf((char *)numbuf, "%d", ai + 1);
2973 name = numbuf;
2974 }
2975 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2976 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002977 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002978 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002979 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002980 }
2981 else
2982 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002983 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002984 if (v == NULL)
2985 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002986 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002987 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002988
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002989 // Note: the values are copied directly to avoid alloc/free.
2990 // "argvars" must have VAR_FIXED for v_lock.
2991 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002992 v->di_tv.v_lock = VAR_FIXED;
2993
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002994 if (isdefault)
2995 // Need to free this later, no matter where it's stored.
2996 tv_to_free[tv_to_free_len++] = &v->di_tv;
2997
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002998 if (addlocal)
2999 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003000 // Named arguments should be accessed without the "a:" prefix in
3001 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003002 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003003 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003004 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003005 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003006 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003007
3008 if (ai >= 0 && ai < MAX_FUNC_ARGS)
3009 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003010 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003011
3012 li->li_tv = argvars[i];
3013 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003014 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003015 }
3016 }
3017
Bram Moolenaare38eab22019-12-05 21:50:01 +01003018 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003019 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003020
3021 if (fp->uf_flags & FC_SANDBOX)
3022 {
3023 using_sandbox = TRUE;
3024 ++sandbox;
3025 }
3026
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003027 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003028 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003029 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003030 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003031 ++no_wait_return;
3032 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003033
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003034 smsg(_("calling %s"), SOURCING_NAME);
3035 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003036 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003037 char_u buf[MSG_BUF_LEN];
3038 char_u numbuf2[NUMBUFLEN];
3039 char_u *tofree;
3040 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003041
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003042 msg_puts("(");
3043 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003044 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003045 if (i > 0)
3046 msg_puts(", ");
3047 if (argvars[i].v_type == VAR_NUMBER)
3048 msg_outnum((long)argvars[i].vval.v_number);
3049 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003050 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003051 // Do not want errors such as E724 here.
3052 ++emsg_off;
3053 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3054 --emsg_off;
3055 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003056 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003057 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003058 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003059 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3060 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003061 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003062 msg_puts((char *)s);
3063 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003064 }
3065 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003066 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003067 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003068 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003069 msg_puts("\n"); // don't overwrite this either
3070
3071 verbose_leave_scroll();
3072 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003073 }
3074#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003075 if (do_profiling == PROF_YES)
3076 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003077 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003078#endif
3079
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003080 // "legacy" does not apply to commands in the function
3081 sticky_cmdmod_flags = 0;
3082
Bram Moolenaar98aff652022-09-06 21:02:35 +01003083 // If called from a compiled :def function the execution context must be
3084 // hidden, any deferred functions need to be added to the function being
3085 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003086 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003087
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003088 save_current_sctx = current_sctx;
3089 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003090 save_did_emsg = did_emsg;
3091 did_emsg = FALSE;
3092
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003093 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003094 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003095 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003096 retval = FCERR_FAILED;
3097 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003098 else if (islambda)
3099 {
3100 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3101
3102 // A Lambda always has the command "return {expr}". It is much faster
3103 // to evaluate {expr} directly.
3104 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003105 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003106 --ex_nesting_level;
3107 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003108 else
3109 // call do_cmdline() to execute the lines
3110 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003111 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3112
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003113 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003114 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003115
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003116 if (RedrawingDisabled > 0)
3117 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003118
Bram Moolenaare38eab22019-12-05 21:50:01 +01003119 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003120 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3121 {
3122 clear_tv(rettv);
3123 rettv->v_type = VAR_NUMBER;
3124 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003125
3126 // In corner cases returning a "failed" value is not backwards
3127 // compatible. Only do this for Vim9 script.
3128 if (in_vim9script())
3129 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003130 }
3131
3132#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003133 if (do_profiling == PROF_YES)
3134 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003135 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003136
3137 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3138 profile_may_end_func(&profile_info, fp, caller);
3139 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003140#endif
3141
Bram Moolenaare38eab22019-12-05 21:50:01 +01003142 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003143 if (p_verbose >= 12)
3144 {
3145 ++no_wait_return;
3146 verbose_enter_scroll();
3147
3148 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003149 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003150 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003151 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003152 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003153 else
3154 {
3155 char_u buf[MSG_BUF_LEN];
3156 char_u numbuf2[NUMBUFLEN];
3157 char_u *tofree;
3158 char_u *s;
3159
Bram Moolenaare38eab22019-12-05 21:50:01 +01003160 // The value may be very long. Skip the middle part, so that we
3161 // have some idea how it starts and ends. smsg() would always
3162 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003163 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003164 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003165 --emsg_off;
3166 if (s != NULL)
3167 {
3168 if (vim_strsize(s) > MSG_BUF_CLEN)
3169 {
3170 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3171 s = buf;
3172 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003173 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003174 vim_free(tofree);
3175 }
3176 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003177 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003178
3179 verbose_leave_scroll();
3180 --no_wait_return;
3181 }
3182
ichizok7e5fe382023-04-15 13:17:50 +01003183 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003184 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003185 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003186 restore_current_ectx(save_current_ectx);
3187
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003188#ifdef FEAT_PROFILE
3189 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003190 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003191#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003192 if (using_sandbox)
3193 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003194 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003195
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003196 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003197 {
3198 ++no_wait_return;
3199 verbose_enter_scroll();
3200
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003201 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003202 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003203
3204 verbose_leave_scroll();
3205 --no_wait_return;
3206 }
3207
3208 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003209 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003210 for (i = 0; i < tv_to_free_len; ++i)
3211 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003212 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003213
3214 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003215}
3216
3217/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003218 * Check the argument count for user function "fp".
3219 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3220 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003221 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003222check_user_func_argcount(ufunc_T *fp, int argcount)
3223{
3224 int regular_args = fp->uf_args.ga_len;
3225
3226 if (argcount < regular_args - fp->uf_def_args.ga_len)
3227 return FCERR_TOOFEW;
3228 else if (!has_varargs(fp) && argcount > regular_args)
3229 return FCERR_TOOMANY;
3230 return FCERR_UNKNOWN;
3231}
3232
3233/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003235 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003236 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237call_user_func_check(
3238 ufunc_T *fp,
3239 int argcount,
3240 typval_T *argvars,
3241 typval_T *rettv,
3242 funcexe_T *funcexe,
3243 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003244{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003245 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003246
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003247#ifdef FEAT_LUA
3248 if (fp->uf_flags & FC_CFUNC)
3249 {
3250 cfunc_T cb = fp->uf_cb;
3251
3252 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3253 }
3254#endif
3255
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003256 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3257 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003258 error = check_user_func_argcount(fp, argcount);
3259 if (error != FCERR_UNKNOWN)
3260 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003261
Bram Moolenaar50824712020-12-20 21:10:17 +01003262 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003263 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003265 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003267 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 int did_save_redo = FALSE;
3269 save_redo_T save_redo;
3270
3271 /*
3272 * Call the user function.
3273 * Save and restore search patterns, script variables and
3274 * redo buffer.
3275 */
3276 save_search_patterns();
3277 if (!ins_compl_active())
3278 {
3279 saveRedobuff(&save_redo);
3280 did_save_redo = TRUE;
3281 }
3282 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003283 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003284 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3286 // Function was unreferenced while being used, free it now.
3287 func_clear_free(fp, FALSE);
3288 if (did_save_redo)
3289 restoreRedobuff(&save_redo);
3290 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003291 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003294}
3295
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003296static funccal_entry_T *funccal_stack = NULL;
3297
3298/*
3299 * Save the current function call pointer, and set it to NULL.
3300 * Used when executing autocommands and for ":source".
3301 */
3302 void
3303save_funccal(funccal_entry_T *entry)
3304{
3305 entry->top_funccal = current_funccal;
3306 entry->next = funccal_stack;
3307 funccal_stack = entry;
3308 current_funccal = NULL;
3309}
3310
3311 void
3312restore_funccal(void)
3313{
3314 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003315 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003316 else
3317 {
3318 current_funccal = funccal_stack->top_funccal;
3319 funccal_stack = funccal_stack->next;
3320 }
3321}
3322
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003323 funccall_T *
3324get_current_funccal(void)
3325{
3326 return current_funccal;
3327}
3328
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003329/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003330 * Return TRUE when currently at the script level:
3331 * - not in a function
3332 * - not executing an autocommand
3333 * Note that when an autocommand sources a script the result is FALSE;
3334 */
3335 int
3336at_script_level(void)
3337{
3338 return current_funccal == NULL && autocmd_match == NULL;
3339}
3340
3341/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003342 * Mark all functions of script "sid" as deleted.
3343 */
3344 void
3345delete_script_functions(int sid)
3346{
3347 hashitem_T *hi;
3348 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003349 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003350 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003351 size_t len;
3352
3353 buf[0] = K_SPECIAL;
3354 buf[1] = KS_EXTRA;
3355 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003356 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003357 len = STRLEN(buf);
3358
Bram Moolenaarce658352020-07-31 23:47:12 +02003359 while (todo > 0)
3360 {
3361 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003362 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003363 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003364 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003365 fp = HI2UF(hi);
3366 if (STRNCMP(fp->uf_name, buf, len) == 0)
3367 {
3368 int changed = func_hashtab.ht_changed;
3369
3370 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003371
3372 if (fp->uf_calls > 0)
3373 {
3374 // Function is executing, don't free it but do remove
3375 // it from the hashtable.
3376 if (func_remove(fp))
3377 fp->uf_refcount--;
3378 }
3379 else
3380 {
3381 func_clear(fp, TRUE);
3382 // When clearing a function another function can be
3383 // cleared as a side effect. When that happens start
3384 // over.
3385 if (changed != func_hashtab.ht_changed)
3386 break;
3387 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003388 }
3389 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003390 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003391 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003392}
3393
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003394#if defined(EXITFREE) || defined(PROTO)
3395 void
3396free_all_functions(void)
3397{
3398 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003399 ufunc_T *fp;
3400 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003401 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003402 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003403
Bram Moolenaare38eab22019-12-05 21:50:01 +01003404 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003405 while (current_funccal != NULL)
3406 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003407 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003408 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003409 if (current_funccal == NULL && funccal_stack != NULL)
3410 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003411 }
3412
Bram Moolenaare38eab22019-12-05 21:50:01 +01003413 // First clear what the functions contain. Since this may lower the
3414 // reference count of a function, it may also free a function and change
3415 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003416 while (todo > 0)
3417 {
3418 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003419 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003420 if (!HASHITEM_EMPTY(hi))
3421 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 // clear the def function index now
3423 fp = HI2UF(hi);
3424 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003425 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426
Bram Moolenaare38eab22019-12-05 21:50:01 +01003427 // Only free functions that are not refcounted, those are
3428 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003429 if (func_name_refcount(fp->uf_name))
3430 ++skipped;
3431 else
3432 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003433 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003434 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003435 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003436 {
3437 skipped = 0;
3438 break;
3439 }
3440 }
3441 --todo;
3442 }
3443 }
3444
Bram Moolenaare38eab22019-12-05 21:50:01 +01003445 // Now actually free the functions. Need to start all over every time,
3446 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003447 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003448 while (func_hashtab.ht_used > skipped)
3449 {
3450 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003451 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003452 if (!HASHITEM_EMPTY(hi))
3453 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003454 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003455 // Only free functions that are not refcounted, those are
3456 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003457 fp = HI2UF(hi);
3458 if (func_name_refcount(fp->uf_name))
3459 ++skipped;
3460 else
3461 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003462 if (func_free(fp, FALSE) == OK)
3463 {
3464 skipped = 0;
3465 break;
3466 }
3467 // did not actually free it
3468 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003469 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003470 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003471 }
3472 if (skipped == 0)
3473 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474
3475 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003476}
3477#endif
3478
3479/*
3480 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003481 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3482 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003483 * "len" is the length of "name", or -1 for NUL terminated.
3484 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003486builtin_function(char_u *name, int len)
3487{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003488 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003489
Bram Moolenaara26b9702020-04-18 19:53:28 +02003490 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003491 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003492 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3493 {
3494 if (name[i] == AUTOLOAD_CHAR)
3495 return FALSE;
3496 if (!eval_isnamec(name[i]))
3497 {
3498 // "name.something" is not a builtin function
3499 if (name[i] == '.')
3500 return FALSE;
3501 break;
3502 }
3503 }
3504 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003505}
3506
3507 int
3508func_call(
3509 char_u *name,
3510 typval_T *args,
3511 partial_T *partial,
3512 dict_T *selfdict,
3513 typval_T *rettv)
3514{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003515 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003516 listitem_T *item;
3517 typval_T argv[MAX_FUNC_ARGS + 1];
3518 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003519 int r = 0;
3520
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003521 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003522 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003523 {
3524 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3525 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003526 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003527 break;
3528 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003529 // Make a copy of each argument. This is needed to be able to set
3530 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003531 copy_tv(&item->li_tv, &argv[argc++]);
3532 }
3533
3534 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003535 {
3536 funcexe_T funcexe;
3537
Bram Moolenaara80faa82020-04-12 19:37:17 +02003538 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003539 funcexe.fe_firstline = curwin->w_cursor.lnum;
3540 funcexe.fe_lastline = curwin->w_cursor.lnum;
3541 funcexe.fe_evaluate = TRUE;
3542 funcexe.fe_partial = partial;
3543 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003544 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3545 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003546
Bram Moolenaare38eab22019-12-05 21:50:01 +01003547 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003548 while (argc > 0)
3549 clear_tv(&argv[--argc]);
3550
3551 return r;
3552}
3553
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003554static int callback_depth = 0;
3555
3556 int
3557get_callback_depth(void)
3558{
3559 return callback_depth;
3560}
3561
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003562/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003563 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003564 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003565 */
3566 int
3567call_callback(
3568 callback_T *callback,
3569 int len, // length of "name" or -1 to use strlen()
3570 typval_T *rettv, // return value goes here
3571 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003572 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003573 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003574{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003575 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003576 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003577
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003578 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3579 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02003580 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003581 funcexe.fe_evaluate = TRUE;
3582 funcexe.fe_partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003583 ++callback_depth;
3584 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3585 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003586
3587 // When a :def function was called that uses :try an error would be turned
3588 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003589 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003590 {
3591 need_rethrow = FALSE;
3592 handle_did_throw();
3593 }
3594
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003595 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003596}
3597
3598/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003599 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003600 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003601 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3602 */
3603 varnumber_T
3604call_callback_retnr(
3605 callback_T *callback,
3606 int argcount, // number of "argvars"
3607 typval_T *argvars) // vars for arguments, must have "argcount"
3608 // PLUS ONE elements!
3609{
3610 typval_T rettv;
3611 varnumber_T retval;
3612
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003613 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003614 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003615
3616 retval = tv_get_number_chk(&rettv, NULL);
3617 clear_tv(&rettv);
3618 return retval;
3619}
3620
3621/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622 * Give an error message for the result of a function.
3623 * Nothing if "error" is FCERR_NONE.
3624 */
3625 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003626user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627{
3628 switch (error)
3629 {
3630 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003631 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003632 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003633 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003634 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 break;
3636 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003637 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 break;
3639 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003640 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 break;
3642 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003643 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 break;
3645 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003646 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 break;
3648 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003649 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 break;
3651 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003652 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3653 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003655 case FCERR_OTHER:
3656 case FCERR_FAILED:
3657 // assume the error message was already given
3658 break;
3659 case FCERR_NONE:
3660 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 }
3662}
3663
3664/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003665 * Check the argument types "argvars[argcount]" for "name" using the
3666 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3667 * is already included in "argvars[]".
3668 * Will do nothing if "funcexe->fe_check_type" is NULL or
3669 * "funcexe->fe_evaluate" is FALSE;
3670 * Returns an FCERR_ value.
3671 */
3672 static funcerror_T
3673may_check_argument_types(
3674 funcexe_T *funcexe,
3675 typval_T *argvars,
3676 int argcount,
3677 int base_included,
3678 char_u *name)
3679{
3680 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3681 {
3682 // Check that the argument types are OK for the types of the funcref.
3683 if (check_argument_types(funcexe->fe_check_type,
3684 argvars, argcount,
3685 base_included ? NULL : funcexe->fe_basetv,
3686 name) == FAIL)
3687 return FCERR_OTHER;
3688 }
3689 return FCERR_NONE;
3690}
3691
3692/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003693 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003694 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003695 * Return FAIL when the function can't be called, OK otherwise.
3696 * Also returns OK when an error was encountered while executing the function.
3697 */
3698 int
3699call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003700 char_u *funcname, // name of the function
3701 int len, // length of "name" or -1 to use strlen()
3702 typval_T *rettv, // return value goes here
3703 int argcount_in, // number of "argvars"
3704 typval_T *argvars_in, // vars for arguments, must have "argcount"
3705 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003706 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003707{
3708 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003709 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003710 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003711 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003712 char_u fname_buf[FLEN_FIXED + 1];
3713 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003714 char_u *fname = NULL;
3715 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003716 int argcount = argcount_in;
3717 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003718 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003719 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003720 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003721 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003722 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003723 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003724 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003725 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003726
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003727 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3728 // even when call_func() returns FAIL.
3729 rettv->v_type = VAR_UNKNOWN;
3730
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003731 if (partial != NULL)
3732 fp = partial->pt_func;
3733 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003734 fp = funcexe->fe_ufunc;
3735
3736 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003737 {
3738 // Make a copy of the name, if it comes from a funcref variable it
3739 // could be changed or deleted in the called function.
3740 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3741 if (name == NULL)
3742 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003743
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003744 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3745 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003746
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003747 if (funcexe->fe_doesrange != NULL)
3748 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003749
3750 if (partial != NULL)
3751 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003752 // When the function has a partial with a dict and there is a dict
3753 // argument, use the dict argument. That is backwards compatible.
3754 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003755 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003756 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003757 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003758 {
3759 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003760 {
3761 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3762 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003763 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003764 goto theend;
3765 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003766 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003767 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003768 for (i = 0; i < argcount_in; ++i)
3769 argv[i + argv_clear] = argvars_in[i];
3770 argvars = argv;
3771 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003772
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003773 if (funcexe->fe_check_type != NULL
3774 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003775 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003776 // Now funcexe->fe_check_type is missing the added arguments,
3777 // make a copy of the type with the correction.
3778 check_type = *funcexe->fe_check_type;
3779 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003780 check_type.tt_args = check_type_args;
3781 CLEAR_FIELD(check_type_args);
3782 for (i = 0; i < check_type.tt_argcount; ++i)
3783 check_type_args[i + partial->pt_argc] =
3784 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003785 check_type.tt_argcount += partial->pt_argc;
3786 check_type.tt_min_argcount += partial->pt_argc;
3787 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003788 }
3789 }
3790
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003791 if (error == FCERR_NONE)
3792 // check the argument types if possible
3793 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
3794 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003795
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003796 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003797 {
3798 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003799 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003800
Bram Moolenaar333894b2020-08-01 18:53:07 +02003801 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003802 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003803 {
3804 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003805 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003806 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003807
Bram Moolenaare38eab22019-12-05 21:50:01 +01003808 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003809 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003810 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003811
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003812 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003813 {
3814 /*
3815 * User defined function.
3816 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003817 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003818 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003819 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003820 if (fp != NULL && !is_global && in_vim9script()
3821 && func_requires_g_prefix(fp))
3822 // In Vim9 script g: is required to find a global
3823 // non-autoload function.
3824 fp = NULL;
3825 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003826
Bram Moolenaare38eab22019-12-05 21:50:01 +01003827 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003828 if (fp == NULL
3829 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003830 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003831 && !aborting())
3832 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003833 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003834 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003835 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003836 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003837 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3838 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003839 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003840 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003841 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003842 if (fp == NULL)
3843 {
3844 char_u *p = untrans_function_name(rfname);
3845
3846 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003847 // Don't do this if the name starts with "s:".
3848 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003849 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003850 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003851
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003852 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003853 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003854 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003855 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003856 int need_arg_check = FALSE;
3857 if (funcexe->fe_check_type == NULL)
3858 {
3859 funcexe->fe_check_type = fp->uf_func_type;
3860 need_arg_check = TRUE;
3861 }
3862
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003863 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003864 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003865 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003866 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003867 argv_clear, fp);
3868 need_arg_check = TRUE;
3869 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003870
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003871 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003872 {
3873 // Method call: base->Method()
3874 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003875 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003876 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003877 argvars = argv;
3878 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003879 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003880 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003881
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003882 // Check the argument types now that the function type and all
3883 // argument values are known, if not done above.
3884 if (need_arg_check)
3885 error = may_check_argument_types(funcexe, argvars, argcount,
3886 TRUE, (name != NULL) ? name : funcname);
3887 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
3888 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003890 }
3891 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003892 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003893 {
3894 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003895 * expr->method(): Find the method name in the table, call its
3896 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003897 */
3898 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003899 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003900 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003901 else
3902 {
3903 /*
3904 * Find the function name in the table, call its implementation.
3905 */
3906 error = call_internal_func(fname, argcount, argvars, rettv);
3907 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003908
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003909 /*
3910 * The function call (or "FuncUndefined" autocommand sequence) might
3911 * have been aborted by an error, an interrupt, or an explicitly thrown
3912 * exception that has not been caught so far. This situation can be
3913 * tested for by calling aborting(). For an error in an internal
3914 * function or for the "E132" error in call_user_func(), however, the
3915 * throw point at which the "force_abort" flag (temporarily reset by
3916 * emsg()) is normally updated has not been reached yet. We need to
3917 * update that flag first to make aborting() reliable.
3918 */
3919 update_force_abort();
3920 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003921 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003922 ret = OK;
3923
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003924theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003925 /*
3926 * Report an error unless the argument evaluation or function call has been
3927 * cancelled due to an aborting error, an interrupt, or an exception.
3928 */
3929 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003930 user_func_error(error, (name != NULL) ? name : funcname,
3931 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003932
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003933 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003934 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003935 clear_tv(&argv[--argv_clear + argv_base]);
3936
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003937 vim_free(tofree);
3938 vim_free(name);
3939
3940 return ret;
3941}
3942
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003943/*
3944 * Call a function without arguments, partial or dict.
3945 * This is like call_func() when the call is only "FuncName()".
3946 * To be used by "expr" options.
3947 * Returns NOTDONE when the function could not be found.
3948 */
3949 int
3950call_simple_func(
3951 char_u *funcname, // name of the function
3952 int len, // length of "name" or -1 to use strlen()
3953 typval_T *rettv) // return value goes here
3954{
3955 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003956 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003957 char_u fname_buf[FLEN_FIXED + 1];
3958 char_u *tofree = NULL;
3959 char_u *name;
3960 char_u *fname;
3961 char_u *rfname;
3962 int is_global = FALSE;
3963 ufunc_T *fp;
3964
3965 rettv->v_type = VAR_NUMBER; // default rettv is number zero
3966 rettv->vval.v_number = 0;
3967
3968 // Make a copy of the name, an option can be changed in the function.
3969 name = vim_strnsave(funcname, len);
3970 if (name == NULL)
3971 return ret;
3972
3973 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3974
3975 // Skip "g:" before a function name.
3976 if (fname[0] == 'g' && fname[1] == ':')
3977 {
3978 is_global = TRUE;
3979 rfname = fname + 2;
3980 }
3981 else
3982 rfname = fname;
3983 fp = find_func(rfname, is_global);
3984 if (fp != NULL && !is_global && in_vim9script()
3985 && func_requires_g_prefix(fp))
3986 // In Vim9 script g: is required to find a global non-autoload
3987 // function.
3988 fp = NULL;
3989 if (fp == NULL)
3990 ret = NOTDONE;
3991 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
3992 error = FCERR_DELETED;
3993 else if (fp != NULL)
3994 {
3995 typval_T argvars[1];
3996 funcexe_T funcexe;
3997
3998 argvars[0].v_type = VAR_UNKNOWN;
3999 CLEAR_FIELD(funcexe);
4000 funcexe.fe_evaluate = TRUE;
4001
4002 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
4003 if (error == FCERR_NONE)
4004 ret = OK;
4005 }
4006
4007 user_func_error(error, name, FALSE);
4008 vim_free(tofree);
4009 vim_free(name);
4010
4011 return ret;
4012}
4013
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004014 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015printable_func_name(ufunc_T *fp)
4016{
4017 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4018}
4019
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004020/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004021 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4022 * TRUE. Otherwise return FALSE.
4023 */
4024 static int
4025function_list_modified(int prev_ht_changed)
4026{
4027 if (prev_ht_changed != func_hashtab.ht_changed)
4028 {
4029 emsg(_(e_function_list_was_modified));
4030 return TRUE;
4031 }
4032 return FALSE;
4033}
4034
4035/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004036 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004037 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004038 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004039list_func_head(ufunc_T *fp, int indent)
4040{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004041 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004042 int j;
4043
4044 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004045
4046 // a timer at the more prompt may have deleted the function
4047 if (function_list_modified(prev_ht_changed))
4048 return FAIL;
4049
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004050 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004051 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004052 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004053 msg_puts("def ");
4054 else
4055 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004057 msg_putchar('(');
4058 for (j = 0; j < fp->uf_args.ga_len; ++j)
4059 {
4060 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004061 msg_puts(", ");
4062 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004063 if (fp->uf_arg_types != NULL)
4064 {
4065 char *tofree;
4066
4067 msg_puts(": ");
4068 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4069 vim_free(tofree);
4070 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004071 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4072 {
4073 msg_puts(" = ");
4074 msg_puts(((char **)(fp->uf_def_args.ga_data))
4075 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4076 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004077 }
4078 if (fp->uf_varargs)
4079 {
4080 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004081 msg_puts(", ");
4082 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004083 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 if (fp->uf_va_name != NULL)
4085 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004086 if (!fp->uf_varargs)
4087 {
4088 if (j)
4089 msg_puts(", ");
4090 msg_puts("...");
4091 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004093 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004094 {
4095 char *tofree;
4096
4097 msg_puts(": ");
4098 msg_puts(type_name(fp->uf_va_type, &tofree));
4099 vim_free(tofree);
4100 }
4101 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004102 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004103
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004104 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004105 {
4106 if (fp->uf_ret_type != &t_void)
4107 {
4108 char *tofree;
4109
4110 msg_puts(": ");
4111 msg_puts(type_name(fp->uf_ret_type, &tofree));
4112 vim_free(tofree);
4113 }
4114 }
4115 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004116 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004117 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004118 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004119 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004120 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004121 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004122 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004123 msg_clr_eos();
4124 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004125 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004126
4127 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004128}
4129
4130/*
4131 * Get a function name, translating "<SID>" and "<SNR>".
4132 * Also handles a Funcref in a List or Dictionary.
4133 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004134 * Set "*is_global" to TRUE when the function must be global, unless
4135 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004136 * flags:
4137 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004138 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004139 * TFN_QUIET: be quiet
4140 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004141 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004142 * Advances "pp" to just after the function name (if no error).
4143 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004144 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004145trans_function_name(
4146 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004147 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004148 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004149 int flags)
4150{
4151 return trans_function_name_ext(pp, is_global, skip, flags,
4152 NULL, NULL, NULL, NULL);
4153}
4154
4155/*
4156 * trans_function_name() with extra arguments.
4157 * "fdp", "partial", "type" and "ufunc" can be NULL.
4158 */
4159 char_u *
4160trans_function_name_ext(
4161 char_u **pp,
4162 int *is_global,
4163 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004164 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004165 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004166 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004167 type_T **type, // return: type of funcref
4168 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004169{
4170 char_u *name = NULL;
4171 char_u *start;
4172 char_u *end;
4173 int lead;
4174 char_u sid_buf[20];
4175 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004177 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004178 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004179 int vim9script = in_vim9script();
4180 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004181
4182 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004183 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004184 start = *pp;
4185
Bram Moolenaare38eab22019-12-05 21:50:01 +01004186 // Check for hard coded <SNR>: already translated function ID (from a user
4187 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004188 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4189 && (*pp)[2] == (int)KE_SNR)
4190 {
4191 *pp += 3;
4192 len = get_id_len(pp) + 3;
4193 return vim_strnsave(start, len);
4194 }
4195
Bram Moolenaare38eab22019-12-05 21:50:01 +01004196 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4197 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004198 lead = eval_fname_script(start);
4199 if (lead > 2)
4200 start += lead;
4201
Bram Moolenaare38eab22019-12-05 21:50:01 +01004202 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004203 end = get_lval(start, NULL, &lv, FALSE, skip,
4204 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004205 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004206 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004207 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004208 {
4209 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004210 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004211 goto theend;
4212 }
4213 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4214 {
4215 /*
4216 * Report an invalid expression in braces, unless the expression
4217 * evaluation has been cancelled due to an aborting error, an
4218 * interrupt, or an exception.
4219 */
4220 if (!aborting())
4221 {
4222 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004223 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004224 }
4225 else
4226 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4227 goto theend;
4228 }
4229
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004230 if (lv.ll_ufunc != NULL)
4231 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004232 if (ufunc != NULL)
4233 *ufunc = lv.ll_ufunc;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004234 name = vim_strsave(lv.ll_ufunc->uf_name);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004235 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004236 goto theend;
4237 }
4238
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004239 if (lv.ll_tv != NULL)
4240 {
4241 if (fdp != NULL)
4242 {
4243 fdp->fd_dict = lv.ll_dict;
4244 fdp->fd_newkey = lv.ll_newkey;
4245 lv.ll_newkey = NULL;
4246 fdp->fd_di = lv.ll_di;
4247 }
4248 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4249 {
4250 name = vim_strsave(lv.ll_tv->vval.v_string);
4251 *pp = end;
4252 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004253 else if (lv.ll_tv->v_type == VAR_CLASS
4254 && lv.ll_tv->vval.v_class != NULL)
4255 {
4256 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4257 *pp = end;
4258 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004259 else if (lv.ll_tv->v_type == VAR_PARTIAL
4260 && lv.ll_tv->vval.v_partial != NULL)
4261 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004262 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004263 *pp = end;
4264 if (partial != NULL)
4265 *partial = lv.ll_tv->vval.v_partial;
4266 }
4267 else
4268 {
4269 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4270 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004271 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004272 else
4273 *pp = end;
4274 name = NULL;
4275 }
4276 goto theend;
4277 }
4278
4279 if (lv.ll_name == NULL)
4280 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004281 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004282 *pp = end;
4283 goto theend;
4284 }
4285
Bram Moolenaare38eab22019-12-05 21:50:01 +01004286 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004287 if (lv.ll_exp_name != NULL)
4288 {
4289 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004290 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004291 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004292 if (name == lv.ll_exp_name)
4293 name = NULL;
4294 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004295 else if (lv.ll_sid > 0)
4296 {
4297 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4298 int cc = *lv.ll_name_end;
4299
4300 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4301 *lv.ll_name_end = NUL;
4302 if (si->sn_autoload_prefix != NULL)
4303 {
4304 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4305 }
4306 else
4307 {
4308 sid_buf[0] = K_SPECIAL;
4309 sid_buf[1] = KS_EXTRA;
4310 sid_buf[2] = (int)KE_SNR;
4311 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004312 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004313 name = concat_str(sid_buf, lv.ll_name);
4314 }
4315 *lv.ll_name_end = cc;
4316 *pp = end;
4317 goto theend;
4318 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004319 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004320 {
4321 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004322 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004323 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004324 if (name == *pp)
4325 name = NULL;
4326 }
4327 if (name != NULL)
4328 {
4329 name = vim_strsave(name);
4330 *pp = end;
4331 if (STRNCMP(name, "<SNR>", 5) == 0)
4332 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004333 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004334 name[0] = K_SPECIAL;
4335 name[1] = KS_EXTRA;
4336 name[2] = (int)KE_SNR;
4337 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4338 }
4339 goto theend;
4340 }
4341
4342 if (lv.ll_exp_name != NULL)
4343 {
4344 len = (int)STRLEN(lv.ll_exp_name);
4345 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4346 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4347 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004348 // When there was "s:" already or the name expanded to get a
4349 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004350 lv.ll_name += 2;
4351 len -= 2;
4352 lead = 2;
4353 }
4354 }
4355 else
4356 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004357 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004358 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004359 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004360 if (lv.ll_name[0] == 'g')
4361 {
4362 if (is_global != NULL)
4363 {
4364 *is_global = TRUE;
4365 }
4366 else
4367 {
4368 // dropping "g:" without setting "is_global" won't work in
4369 // Vim9script, put it back later
4370 prefix_g = TRUE;
4371 extra = 2;
4372 }
4373 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004374 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004375 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004376 len = (int)(end - lv.ll_name);
4377 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004378 if (len <= 0)
4379 {
4380 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004381 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004382 goto theend;
4383 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004384
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004385 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004386 // starts with a lower case character: dict.func(). Or when in a class.
4387 vim9_local = ASCII_ISUPPER(*start) && vim9script
4388 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004390 /*
4391 * Copy the function name to allocated memory.
4392 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4393 * Accept <SNR>123_name() outside a script.
4394 */
4395 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004396 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004397 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004398 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004399 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004400 {
Kota Kato948a3892022-08-16 16:09:59 +01004401 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4402 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004403 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004404 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004405 goto theend;
4406 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004408 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004409 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004411 || eval_fname_sid(*pp))
4412 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004414 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004415 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004416 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004417 goto theend;
4418 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004419 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004420 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 extra = 3 + (int)STRLEN(sid_buf);
4422 else
4423 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004424 }
4425 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004426 // The function name must start with an upper case letter (unless it is a
4427 // Vim9 class new() function or a Vim9 class private method)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004428 else if (!(flags & TFN_INT)
4429 && (builtin_function(lv.ll_name, len)
4430 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004431 && !((flags & TFN_IN_CLASS)
4432 && (STRNCMP(lv.ll_name, "new", 3) == 0
4433 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004434 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004435 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004436 : e_function_name_must_start_with_capital_or_s_str),
4437 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004438 goto theend;
4439 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004440 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004441 {
4442 char_u *cp = vim_strchr(lv.ll_name, ':');
4443
4444 if (cp != NULL && cp < end)
4445 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004446 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004447 goto theend;
4448 }
4449 }
4450
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004452 if (name != NULL)
4453 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004454 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004455 {
4456 name[0] = K_SPECIAL;
4457 name[1] = KS_EXTRA;
4458 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004459 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004460 STRCPY(name + 3, sid_buf);
4461 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004462 else if (prefix_g)
4463 {
4464 name[0] = 'g';
4465 name[1] = ':';
4466 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4468 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004469 }
4470 *pp = end;
4471
4472theend:
4473 clear_lval(&lv);
4474 return name;
4475}
4476
4477/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004478 * Assuming "name" is the result of trans_function_name() and it was prefixed
4479 * to use the script-local name, return the unmodified name (points into
4480 * "name"). Otherwise return NULL.
4481 * This can be used to first search for a script-local function and fall back
4482 * to the global function if not found.
4483 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004484 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004485untrans_function_name(char_u *name)
4486{
4487 char_u *p;
4488
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004489 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004490 {
4491 p = vim_strchr(name, '_');
4492 if (p != NULL)
4493 return p + 1;
4494 }
4495 return NULL;
4496}
4497
4498/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004499 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4500 * current script ID and returns the expanded function name. The caller should
4501 * free the returned name. If not called from a script context or the function
4502 * name doesn't start with these prefixes, then returns NULL.
4503 * This doesn't check whether the script-local function exists or not.
4504 */
4505 char_u *
4506get_scriptlocal_funcname(char_u *funcname)
4507{
4508 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004509 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004510 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004511 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004512
4513 if (funcname == NULL)
4514 return NULL;
4515
4516 if (STRNCMP(funcname, "s:", 2) != 0
4517 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004518 {
4519 ufunc_T *ufunc;
4520
4521 // The function name does not have a script-local prefix. Try finding
4522 // it when in a Vim9 script and there is no "g:" prefix.
4523 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4524 return NULL;
4525 ufunc = find_func(funcname, FALSE);
4526 if (ufunc == NULL || func_is_global(ufunc)
4527 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4528 return NULL;
4529 ++p;
4530 off = 0;
4531 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004532 else
4533 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004534
4535 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4536 {
4537 emsg(_(e_using_sid_not_in_script_context));
4538 return NULL;
4539 }
4540 // Expand s: prefix into <SNR>nr_<name>
4541 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4542 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004543 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004544 if (newname == NULL)
4545 return NULL;
4546 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004547 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004548
4549 return newname;
4550}
4551
4552/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004553 * Return script-local "fname" with the 3-byte sequence replaced by
4554 * printable <SNR> in allocated memory.
4555 */
4556 char_u *
4557alloc_printable_func_name(char_u *fname)
4558{
4559 char_u *n = alloc(STRLEN(fname + 3) + 6);
4560
4561 if (n != NULL)
4562 {
4563 STRCPY(n, "<SNR>");
4564 STRCPY(n + 5, fname + 3);
4565 }
4566 return n;
4567}
4568
4569/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004570 * Call trans_function_name(), except that a lambda is returned as-is.
4571 * Returns the name in allocated memory.
4572 */
4573 char_u *
4574save_function_name(
4575 char_u **name,
4576 int *is_global,
4577 int skip,
4578 int flags,
4579 funcdict_T *fudi)
4580{
4581 char_u *p = *name;
4582 char_u *saved;
4583
4584 if (STRNCMP(p, "<lambda>", 8) == 0)
4585 {
4586 p += 8;
4587 (void)getdigits(&p);
4588 saved = vim_strnsave(*name, p - *name);
4589 if (fudi != NULL)
4590 CLEAR_POINTER(fudi);
4591 }
4592 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004593 saved = trans_function_name_ext(&p, is_global, skip,
4594 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004595 *name = p;
4596 return saved;
4597}
4598
4599/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004600 * List functions. When "regmatch" is NULL all of then.
4601 * Otherwise functions matching "regmatch".
4602 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004603 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004604list_functions(regmatch_T *regmatch)
4605{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004606 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004607 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004608 hashitem_T *hi;
4609
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004610 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004611 {
4612 if (!HASHITEM_EMPTY(hi))
4613 {
4614 ufunc_T *fp = HI2UF(hi);
4615
4616 --todo;
4617 if ((fp->uf_flags & FC_DEAD) == 0
4618 && (regmatch == NULL
4619 ? !message_filtered(fp->uf_name)
4620 && !func_name_refcount(fp->uf_name)
4621 : !isdigit(*fp->uf_name)
4622 && vim_regexec(regmatch, fp->uf_name, 0)))
4623 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004624 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004625 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004626 if (function_list_modified(prev_ht_changed))
4627 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004628 }
4629 }
4630 }
4631}
4632
4633/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004634 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004635 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4636 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004637 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004638 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4639 * With CF_INTERFACE the function is define inside an interface, only the
4640 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004641 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004642 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004643 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004644define_function(
4645 exarg_T *eap,
4646 char_u *name_arg,
4647 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004648 int class_flags,
4649 ocmember_T *obj_members,
4650 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004651{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004652 int j;
4653 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004654 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004655 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004656 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004657 char_u *p;
4658 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004659 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004660 char_u *line_arg = NULL;
4661 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004663 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004664 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004665 garray_T newlines;
4666 int varargs = FALSE;
4667 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004669 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004670 int fp_allocated = FALSE;
4671 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004672 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004673 dictitem_T *v;
4674 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004675 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004676 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004677 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004678 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004679 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004680 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004681
4682 /*
4683 * ":function" without argument: list functions.
4684 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004685 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004686 {
4687 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004688 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004689 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004690 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004691 }
4692
4693 /*
4694 * ":function /pat": list functions matching pattern.
4695 */
4696 if (*eap->arg == '/')
4697 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004698 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004699 if (!eap->skip)
4700 {
4701 regmatch_T regmatch;
4702
4703 c = *p;
4704 *p = NUL;
4705 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4706 *p = c;
4707 if (regmatch.regprog != NULL)
4708 {
4709 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004710 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004711 vim_regfree(regmatch.regprog);
4712 }
4713 }
4714 if (*p == '/')
4715 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004716 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004717 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004718 }
4719
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004720 ga_init(&newargs);
4721 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004722 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004723 ga_init(&default_args);
4724
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004725 /*
4726 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004727 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004728 * "name" == func, "fudi.fd_dict" == NULL
4729 * dict.func new dictionary entry
4730 * "name" == NULL, "fudi.fd_dict" set,
4731 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4732 * dict.func existing dict entry with a Funcref
4733 * "name" == func, "fudi.fd_dict" set,
4734 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4735 * dict.func existing dict entry that's not a Funcref
4736 * "name" == NULL, "fudi.fd_dict" set,
4737 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4738 * s:func script-local function name
4739 * g:func global function name, same as "func"
4740 */
4741 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004742 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004743 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004744 // nested function, argument is (args).
4745 paren = TRUE;
4746 CLEAR_FIELD(fudi);
4747 }
4748 else
4749 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004750 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004751 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004752 if (p[0] == 's' && p[1] == ':')
4753 {
4754 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4755 return NULL;
4756 }
4757 p = to_name_end(p, TRUE);
4758 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4759 {
4760 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4761 eap->arg);
4762 return NULL;
4763 }
4764 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004765 }
4766
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004767 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004768 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004769 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004770 paren = (vim_strchr(p, '(') != NULL);
4771 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004772 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004773 /*
4774 * Return on an invalid expression in braces, unless the expression
4775 * evaluation has been cancelled due to an aborting error, an
4776 * interrupt, or an exception.
4777 */
4778 if (!aborting())
4779 {
4780 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004781 semsg(_(e_key_not_present_in_dictionary_str),
4782 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004783 vim_free(fudi.fd_newkey);
4784 return NULL;
4785 }
4786 else
4787 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004788 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004789
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004790 // For "export def FuncName()" in an autoload script the function name
4791 // is stored with the legacy autoload name "dir#script#FuncName" so
4792 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004793 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004794 {
4795 char_u *prefixed = may_prefix_autoload(name);
4796
4797 if (prefixed != NULL && prefixed != name)
4798 {
4799 vim_free(name);
4800 name = prefixed;
4801 }
4802 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004803 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004804 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004805 {
4806 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4807 goto ret_free;
4808 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004809 }
4810
Bram Moolenaare38eab22019-12-05 21:50:01 +01004811 // An error in a function call during evaluation of an expression in magic
4812 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004813 saved_did_emsg = did_emsg;
4814 did_emsg = FALSE;
4815
4816 /*
4817 * ":function func" with only function name: list function.
4818 */
4819 if (!paren)
4820 {
4821 if (!ends_excmd(*skipwhite(p)))
4822 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004823 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004824 goto ret_free;
4825 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004826 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004827 if (eap->nextcmd != NULL)
4828 *p = NUL;
4829 if (!eap->skip && !got_int)
4830 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004831 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004832 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4833 {
4834 char_u *up = untrans_function_name(name);
4835
4836 // With Vim9 script the name was made script-local, if not
4837 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004838 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004839 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004840 }
4841
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004842 if (fp != NULL)
4843 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004844 // Check no function was added or removed from a timer, e.g. at
4845 // the more prompt. "fp" may then be invalid.
4846 int prev_ht_changed = func_hashtab.ht_changed;
4847
4848 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004849 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004850 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4851 {
4852 if (FUNCLINE(fp, j) == NULL)
4853 continue;
4854 msg_putchar('\n');
4855 msg_outnum((long)(j + 1));
4856 if (j < 9)
4857 msg_putchar(' ');
4858 if (j < 99)
4859 msg_putchar(' ');
4860 if (function_list_modified(prev_ht_changed))
4861 break;
4862 msg_prt_line(FUNCLINE(fp, j), FALSE);
4863 out_flush(); // show a line at a time
4864 ui_breakcheck();
4865 }
4866 if (!got_int)
4867 {
4868 msg_putchar('\n');
4869 if (!function_list_modified(prev_ht_changed))
4870 {
4871 if (fp->uf_def_status != UF_NOT_COMPILED)
4872 msg_puts(" enddef");
4873 else
4874 msg_puts(" endfunction");
4875 }
4876 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004877 }
4878 }
4879 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004880 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004881 }
4882 goto ret_free;
4883 }
4884
4885 /*
4886 * ":function name(arg1, arg2)" Define function.
4887 */
4888 p = skipwhite(p);
4889 if (*p != '(')
4890 {
4891 if (!eap->skip)
4892 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004893 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004894 goto ret_free;
4895 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004896 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004897 if (vim_strchr(p, '(') != NULL)
4898 p = vim_strchr(p, '(');
4899 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004900
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004901 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4902 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004903 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004904 goto ret_free;
4905 }
4906
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004907 // In Vim9 script only global functions can be redefined.
4908 if (vim9script && eap->forceit && !is_global)
4909 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004910 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004911 goto ret_free;
4912 }
4913
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004914 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004915
Bram Moolenaar04b12692020-05-04 23:24:44 +02004916 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004917 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004918 // Check the name of the function. Unless it's a dictionary function
4919 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004920 if (name != NULL)
4921 arg = name;
4922 else
4923 arg = fudi.fd_newkey;
4924 if (arg != NULL && (fudi.fd_di == NULL
4925 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4926 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4927 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004928 char_u *name_base = arg;
4929 int i;
4930
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004931 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004932 {
4933 name_base = vim_strchr(arg, '_');
4934 if (name_base == NULL)
4935 name_base = arg + 3;
4936 else
4937 ++name_base;
4938 }
4939 for (i = 0; name_base[i] != NUL && (i == 0
4940 ? eval_isnamec1(name_base[i])
4941 : eval_isnamec(name_base[i])); ++i)
4942 ;
4943 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004944 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004945
4946 // In Vim9 script a function cannot have the same name as a
4947 // variable.
4948 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004949 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4950 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004951 + EVAL_VAR_NO_FUNC) == OK)
4952 {
4953 semsg(_(e_redefining_script_item_str), name_base);
4954 goto ret_free;
4955 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004956 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004957 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004958 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004959 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004960 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004961 goto ret_free;
4962 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004963 }
4964
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004965 // This may get more lines and make the pointers into the first line
4966 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004967 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004968 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00004969 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02004970 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004971 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00004972 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004973 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004974 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004975
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004976 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004977 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004978 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004979 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004980 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004981 if (*p != ':')
4982 {
4983 semsg(_(e_no_white_space_allowed_before_colon_str), p);
4984 p = skipwhite(p);
4985 }
4986 else if (!IS_WHITE_OR_NUL(p[1]))
4987 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02004989 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004990 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004991 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004992 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01004993 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004994 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004995 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004997 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004998 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02004999 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005000 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005001 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02005002 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005003 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005004 else
5005 // find extra arguments "range", "dict", "abort" and "closure"
5006 for (;;)
5007 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01005008 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005009 p = skipwhite(p);
5010 if (STRNCMP(p, "range", 5) == 0)
5011 {
5012 flags |= FC_RANGE;
5013 p += 5;
5014 }
5015 else if (STRNCMP(p, "dict", 4) == 0)
5016 {
5017 flags |= FC_DICT;
5018 p += 4;
5019 }
5020 else if (STRNCMP(p, "abort", 5) == 0)
5021 {
5022 flags |= FC_ABORT;
5023 p += 5;
5024 }
5025 else if (STRNCMP(p, "closure", 7) == 0)
5026 {
5027 flags |= FC_CLOSURE;
5028 p += 7;
5029 if (current_funccal == NULL)
5030 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005031 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005032 name == NULL ? (char_u *)"" : name);
5033 goto erret;
5034 }
5035 }
5036 else
5037 break;
5038 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005039
Bram Moolenaare38eab22019-12-05 21:50:01 +01005040 // When there is a line break use what follows for the function body.
5041 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005042 if (*p == '\n')
5043 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005044 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005045 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5046 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005047 && !(VIM_ISWHITE(*whitep) && *p == '#'
5048 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005049 && !eap->skip
5050 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005051 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005052
5053 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005054 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5055 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005056 */
5057 if (KeyTyped)
5058 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005059 // Check if the function already exists, don't let the user type the
5060 // whole function before telling him it doesn't work! For a script we
5061 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005062 if (!eap->skip && !eap->forceit)
5063 {
5064 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005065 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005066 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005067 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005068 }
5069
5070 if (!eap->skip && did_emsg)
5071 goto erret;
5072
Bram Moolenaare38eab22019-12-05 21:50:01 +01005073 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005074 cmdline_row = msg_row;
5075 }
5076
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005077 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005078 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005079
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005080 // Do not define the function when getting the body fails and when
5081 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005082 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005083 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005084 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5085 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005086 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005087 goto erret;
5088
5089 /*
5090 * If there are no errors, add the function
5091 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005092 if (fudi.fd_dict != NULL)
5093 {
5094 char numbuf[20];
5095
5096 fp = NULL;
5097 if (fudi.fd_newkey == NULL && !eap->forceit)
5098 {
5099 emsg(_(e_dictionary_entry_already_exists));
5100 goto erret;
5101 }
5102 if (fudi.fd_di == NULL)
5103 {
5104 // Can't add a function to a locked dictionary
5105 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5106 goto erret;
5107 }
5108 // Can't change an existing function if it is locked
5109 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5110 goto erret;
5111
5112 // Give the function a sequential number. Can only be used with a
5113 // Funcref!
5114 vim_free(name);
5115 sprintf(numbuf, "%d", ++func_nr);
5116 name = vim_strsave((char_u *)numbuf);
5117 if (name == NULL)
5118 goto erret;
5119 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005120 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005121 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005122 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005123 char_u *find_name = name;
5124 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005125 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005127 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005128 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005129 var_conflict = TRUE;
5130
5131 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5132 {
5133 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5134
5135 if (si->sn_autoload_prefix != NULL)
5136 {
5137 if (is_export)
5138 {
5139 find_name = name + STRLEN(si->sn_autoload_prefix);
5140 v = find_var(find_name, &ht, TRUE);
5141 if (v != NULL)
5142 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005143 // Only check if the function already exists in the script,
5144 // global functions can be shadowed.
5145 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005146 }
5147 else
5148 {
5149 char_u *prefixed = may_prefix_autoload(name);
5150
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005151 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005152 {
5153 v = find_var(prefixed, &ht, TRUE);
5154 if (v != NULL)
5155 var_conflict = TRUE;
5156 vim_free(prefixed);
5157 }
5158 }
5159 }
5160 }
5161 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005162 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005163 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005164 goto erret;
5165 }
5166
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005167 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005168 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005169 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005170 char_u *uname = untrans_function_name(name);
5171
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005172 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005173 }
5174
5175 if (fp != NULL || import != NULL)
5176 {
5177 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005178
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005179 // Function can be replaced with "function!" and when sourcing the
5180 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005181 // A name that is used by an import can not be overruled.
5182 if (import != NULL
5183 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005184 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005185 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005186 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005187 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005188 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005189 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005190 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005191 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005192 goto erret;
5193 }
5194 if (fp->uf_calls > 0)
5195 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005196 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005197 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005198 goto erret;
5199 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005200 if (fp->uf_refcount > 1)
5201 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005202 // This function is referenced somewhere, don't redefine it but
5203 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005204 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005205 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005206 fp = NULL;
5207 overwrite = TRUE;
5208 }
5209 else
5210 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005211 char_u *exp_name = fp->uf_name_exp;
5212
5213 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005214 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005215 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005216 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005217 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005219#ifdef FEAT_PROFILE
5220 fp->uf_profiling = FALSE;
5221 fp->uf_prof_initialized = FALSE;
5222#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005223 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005224 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005225 }
5226 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005227
5228 if (fp == NULL)
5229 {
5230 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5231 {
5232 int slen, plen;
5233 char_u *scriptname;
5234
Bram Moolenaare38eab22019-12-05 21:50:01 +01005235 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005236 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005237 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005238 {
5239 scriptname = autoload_name(name);
5240 if (scriptname != NULL)
5241 {
5242 p = vim_strchr(scriptname, '/');
5243 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005244 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005245 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005246 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005247 j = OK;
5248 vim_free(scriptname);
5249 }
5250 }
5251 if (j == FAIL)
5252 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005253 linenr_T save_lnum = SOURCING_LNUM;
5254
5255 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005256 semsg(_(e_function_name_does_not_match_script_file_name_str),
5257 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005258 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005259 goto erret;
5260 }
5261 }
5262
Bram Moolenaare01e5212023-01-08 20:31:18 +00005263 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005264 if (fp == NULL)
5265 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005266 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005267
5268 if (fudi.fd_dict != NULL)
5269 {
5270 if (fudi.fd_di == NULL)
5271 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005272 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005273 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5274 if (fudi.fd_di == NULL)
5275 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005276 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005277 goto erret;
5278 }
5279 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5280 {
5281 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005282 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005283 goto erret;
5284 }
5285 }
5286 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005287 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005288 clear_tv(&fudi.fd_di->di_tv);
5289 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005290 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005291
Bram Moolenaare38eab22019-12-05 21:50:01 +01005292 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005293 flags |= FC_DICT;
5294 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005295 }
5296 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005297 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005298 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005299 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005300
5301 if (eap->cmdidx == CMD_def)
5302 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005303 int lnum_save = SOURCING_LNUM;
5304 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005305
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005306 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005307
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005308 // error messages are for the first function line
5309 SOURCING_LNUM = sourcing_lnum_top;
5310
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005311 // The function may use script variables from the context.
5312 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005313
h-eastb895b0f2023-09-24 15:46:31 +02005314 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5315 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005316 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005317 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005318 free_fp = fp_allocated;
5319 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005320 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005321 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005322
5323 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005324 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005325 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005326 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005327 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005328 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005329 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005330 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005331 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005332 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005333 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005334
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005335 if (fp_allocated)
5336 {
5337 // insert the new function in the function list
5338 set_ufunc_name(fp, name);
5339 if (overwrite)
5340 {
5341 hi = hash_find(&func_hashtab, name);
5342 hi->hi_key = UF2HIKEY(fp);
5343 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005344 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005345 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005346 {
5347 free_fp = TRUE;
5348 goto erret;
5349 }
5350 fp->uf_refcount = 1;
5351 }
5352
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005353 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005354 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005355 if ((flags & FC_CLOSURE) != 0)
5356 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005357 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005358 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005359 }
5360 else
5361 fp->uf_scoped = NULL;
5362
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005363#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005364 if (prof_def_func())
5365 func_do_profile(fp);
5366#endif
5367 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005368 if (sandbox)
5369 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005370 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005371 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005372 fp->uf_flags = flags;
5373 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005374 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005375 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005376 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005377 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005378 if (is_export)
5379 {
5380 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005381 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005382 is_export = FALSE;
5383 }
5384
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005385 if (eap->cmdidx == CMD_def)
5386 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005387 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5388 // :func does not use Vim9 script syntax, even in a Vim9 script file
5389 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005390
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005391 goto ret_free;
5392
5393erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005394 if (fp != NULL)
5395 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005396 // these were set to "newargs" and "default_args", which are cleared
5397 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005398 ga_init(&fp->uf_args);
5399 ga_init(&fp->uf_def_args);
5400 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005401errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005402 ga_clear_strings(&newargs);
5403 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005404 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005405 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005406 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005407 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005408 VIM_CLEAR(fp->uf_va_name);
5409 clear_type_list(&fp->uf_type_list);
5410 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005411 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005412 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005413ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005414 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005415 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005416 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005417 if (name != name_arg)
5418 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005419 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005420 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005421
5422 return fp;
5423}
5424
5425/*
5426 * ":function"
5427 */
5428 void
5429ex_function(exarg_T *eap)
5430{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005431 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005432
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005433 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005434 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005435 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005436}
5437
5438/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005439 * Find a function by name, including "<lambda>123".
5440 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005441 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005442 * Return NULL if not found.
5443 */
5444 ufunc_T *
5445find_func_by_name(char_u *name, compiletype_T *compile_type)
5446{
5447 char_u *arg = name;
5448 char_u *fname;
5449 ufunc_T *ufunc;
5450 int is_global = FALSE;
5451
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005452 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5453 {
5454 *compile_type = CT_PROFILE;
5455 arg = skipwhite(arg + 7);
5456 }
5457 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5458 {
5459 *compile_type = CT_DEBUG;
5460 arg = skipwhite(arg + 5);
5461 }
5462
5463 if (STRNCMP(arg, "<lambda>", 8) == 0)
5464 {
5465 arg += 8;
5466 (void)getdigits(&arg);
5467 fname = vim_strnsave(name, arg - name);
5468 }
5469 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005470 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005471 // First try finding a method in a class, trans_function_name() will
5472 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005473 ufunc = find_class_func(&arg);
5474 if (ufunc != NULL)
5475 return ufunc;
5476
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005477 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005478 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005479 NULL, NULL, NULL, &ufunc);
5480 if (ufunc != NULL)
5481 {
5482 vim_free(fname);
5483 return ufunc;
5484 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005485 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005486 if (fname == NULL)
5487 {
5488 semsg(_(e_invalid_argument_str), name);
5489 return NULL;
5490 }
5491 if (!ends_excmd2(name, arg))
5492 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005493 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005494 emsg(ex_errmsg(e_trailing_characters_str, arg));
5495 return NULL;
5496 }
5497
5498 ufunc = find_func(fname, is_global);
5499 if (ufunc == NULL)
5500 {
5501 char_u *p = untrans_function_name(fname);
5502
5503 if (p != NULL)
5504 // Try again without making it script-local.
5505 ufunc = find_func(p, FALSE);
5506 }
5507 vim_free(fname);
5508 if (ufunc == NULL)
5509 semsg(_(e_cannot_find_function_str), name);
5510 return ufunc;
5511}
5512
5513/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005514 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005515 * be compiled or the one specified by the argument.
5516 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005517 */
5518 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005519ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005520{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005521 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005522 {
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005523 compiletype_T compile_type = CT_NONE;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005524 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005525 if (ufunc != NULL)
5526 {
5527 if (func_needs_compiling(ufunc, compile_type))
5528 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5529 else
5530 smsg(_("Function %s does not need compiling"), eap->arg);
5531 }
5532 }
5533 else
5534 {
5535 long todo = (long)func_hashtab.ht_used;
5536 int changed = func_hashtab.ht_changed;
5537 hashitem_T *hi;
5538
5539 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5540 {
5541 if (!HASHITEM_EMPTY(hi))
5542 {
5543 --todo;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005544 ufunc_T *ufunc = HI2UF(hi);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005545 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5546 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5547 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005548 {
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005549 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5550
5551 if (func_hashtab.ht_changed != changed)
5552 {
5553 // a function has been added or removed, need to start
5554 // over
5555 todo = (long)func_hashtab.ht_used;
5556 changed = func_hashtab.ht_changed;
5557 hi = func_hashtab.ht_array;
5558 --hi;
5559 }
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005560 }
5561 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005562 }
5563 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005564}
5565
5566/*
5567 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5568 * Return 2 if "p" starts with "s:".
5569 * Return 0 otherwise.
5570 */
5571 int
5572eval_fname_script(char_u *p)
5573{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005574 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5575 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005576 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5577 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5578 return 5;
5579 if (p[0] == 's' && p[1] == ':')
5580 return 2;
5581 return 0;
5582}
5583
5584 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005585translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005586{
5587 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005588 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005589 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005590}
5591
5592/*
5593 * Return TRUE when "ufunc" has old-style "..." varargs
5594 * or named varargs "...name: type".
5595 */
5596 int
5597has_varargs(ufunc_T *ufunc)
5598{
5599 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005600}
5601
5602/*
5603 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005604 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005605 */
5606 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005607function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005608{
5609 char_u *nm = name;
5610 char_u *p;
5611 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005612 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005613 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005614
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005615 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5616 if (no_deref)
5617 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005618 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005619 nm = skipwhite(nm);
5620
Bram Moolenaare38eab22019-12-05 21:50:01 +01005621 // Only accept "funcname", "funcname ", "funcname (..." and
5622 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005623 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005624 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005625 vim_free(p);
5626 return n;
5627}
5628
Bram Moolenaar113e1072019-01-20 15:30:40 +01005629#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005630 char_u *
5631get_expanded_name(char_u *name, int check)
5632{
5633 char_u *nm = name;
5634 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005635 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005636
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005637 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005638
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005639 if (p != NULL && *nm == NUL
5640 && (!check || translated_function_exists(p, is_global)))
5641 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005642
5643 vim_free(p);
5644 return NULL;
5645}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005646#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005647
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005648/*
5649 * Function given to ExpandGeneric() to obtain the list of user defined
5650 * function names.
5651 */
5652 char_u *
5653get_user_func_name(expand_T *xp, int idx)
5654{
5655 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005656 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005657 static hashitem_T *hi;
5658 ufunc_T *fp;
5659
5660 if (idx == 0)
5661 {
5662 done = 0;
5663 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005664 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005665 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005666 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005667 {
5668 if (done++ > 0)
5669 ++hi;
5670 while (HASHITEM_EMPTY(hi))
5671 ++hi;
5672 fp = HI2UF(hi);
5673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005674 // don't show dead, dict and lambda functions
5675 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005676 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005677 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005678
5679 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005680 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005681
5682 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005683 if (xp->xp_context != EXPAND_USER_FUNC
5684 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005685 {
5686 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005687 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005688 STRCAT(IObuff, ")");
5689 }
5690 return IObuff;
5691 }
5692 return NULL;
5693}
5694
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005695/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005696 * Make a copy of a function.
5697 * Intended to be used for a function defined on a base class that has a copy
5698 * on the child class.
5699 * The copy has uf_refcount set to one.
5700 * Returns NULL when out of memory.
5701 */
5702 ufunc_T *
5703copy_function(ufunc_T *fp)
5704{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005705 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005706 if (ufunc == NULL)
5707 return NULL;
5708
5709 // Most things can just be copied.
5710 *ufunc = *fp;
5711
5712 ufunc->uf_def_status = UF_TO_BE_COMPILED;
5713 ufunc->uf_dfunc_idx = 0;
5714 ufunc->uf_class = NULL;
5715
5716 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
5717 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
5718
5719 if (ufunc->uf_arg_types != NULL)
5720 {
5721 // "uf_arg_types" is an allocated array, make a copy.
5722 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
5723 if (at != NULL)
5724 {
5725 mch_memmove(at, ufunc->uf_arg_types,
5726 sizeof(type_T *) * ufunc->uf_args.ga_len);
5727 ufunc->uf_arg_types = at;
5728 }
5729 }
5730
5731 // TODO: how about the types themselves? they can be freed when the
5732 // original function is freed:
5733 // type_T **uf_arg_types;
5734 // type_T *uf_ret_type;
5735
Ernie Rael114ec812023-06-04 18:11:35 +01005736 // make uf_type_list empty
5737 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00005738
5739 // TODO: partial_T *uf_partial;
5740
5741 if (ufunc->uf_va_name != NULL)
5742 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
5743
5744 // TODO:
5745 // type_T *uf_va_type;
5746 // type_T *uf_func_type;
5747
5748 ufunc->uf_block_depth = 0;
5749 ufunc->uf_block_ids = NULL;
5750
5751 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
5752
5753 ufunc->uf_refcount = 1;
5754 ufunc->uf_name_exp = NULL;
5755 STRCPY(ufunc->uf_name, fp->uf_name);
5756
5757 return ufunc;
5758}
5759
5760/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005761 * ":delfunction {name}"
5762 */
5763 void
5764ex_delfunction(exarg_T *eap)
5765{
5766 ufunc_T *fp = NULL;
5767 char_u *p;
5768 char_u *name;
5769 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005770 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005771
5772 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005773 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
5774 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005775 vim_free(fudi.fd_newkey);
5776 if (name == NULL)
5777 {
5778 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005779 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005780 return;
5781 }
5782 if (!ends_excmd(*skipwhite(p)))
5783 {
5784 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005785 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005786 return;
5787 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005788 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005789 if (eap->nextcmd != NULL)
5790 *p = NUL;
5791
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005792 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005793 {
5794 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005795 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005796 vim_free(name);
5797 return;
5798 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005799 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005800 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005801 vim_free(name);
5802
5803 if (!eap->skip)
5804 {
5805 if (fp == NULL)
5806 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005807 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005808 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005809 return;
5810 }
5811 if (fp->uf_calls > 0)
5812 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005813 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005814 return;
5815 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005816 if (fp->uf_flags & FC_VIM9)
5817 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005818 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005819 return;
5820 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005821
5822 if (fudi.fd_dict != NULL)
5823 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005824 // Delete the dict item that refers to the function, it will
5825 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00005826 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005827 }
5828 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005829 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005830 // A normal function (not a numbered function or lambda) has a
5831 // refcount of 1 for the entry in the hashtable. When deleting
5832 // it and the refcount is more than one, it should be kept.
5833 // A numbered function and lambda should be kept if the refcount is
5834 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005835 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005836 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005837 // Function is still referenced somewhere. Don't free it but
5838 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005839 if (func_remove(fp))
5840 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005841 }
5842 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005843 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005844 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005845 }
5846}
5847
5848/*
5849 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005850 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005851 */
5852 void
5853func_unref(char_u *name)
5854{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005855 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005856
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005857 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005858 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005859 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005860 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005861 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005862#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005863 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005864#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005865 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005866 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005867 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005868}
5869
5870/*
5871 * Unreference a Function: decrement the reference count and free it when it
5872 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005873 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005874 */
5875 void
5876func_ptr_unref(ufunc_T *fp)
5877{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005878 if (fp != NULL && (--fp->uf_refcount <= 0
5879 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5880 && fp->uf_partial->pt_refcount <= 1
5881 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005882 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005883 // Only delete it when it's not being used. Otherwise it's done
5884 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005885 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005886 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005887 }
5888}
5889
5890/*
5891 * Count a reference to a Function.
5892 */
5893 void
5894func_ref(char_u *name)
5895{
5896 ufunc_T *fp;
5897
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005898 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005899 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005900 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005901 if (fp != NULL)
5902 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005903 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005904 // Only give an error for a numbered function.
5905 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005906 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005907}
5908
5909/*
5910 * Count a reference to a Function.
5911 */
5912 void
5913func_ptr_ref(ufunc_T *fp)
5914{
5915 if (fp != NULL)
5916 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005917}
5918
5919/*
5920 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5921 * referenced from anywhere that is in use.
5922 */
5923 static int
5924can_free_funccal(funccall_T *fc, int copyID)
5925{
Bram Moolenaarca16c602022-09-06 18:57:08 +01005926 return (fc->fc_l_varlist.lv_copyID != copyID
5927 && fc->fc_l_vars.dv_copyID != copyID
5928 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005929 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005930}
5931
5932/*
5933 * ":return [expr]"
5934 */
5935 void
5936ex_return(exarg_T *eap)
5937{
5938 char_u *arg = eap->arg;
5939 typval_T rettv;
5940 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005941 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005942
5943 if (current_funccal == NULL)
5944 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005945 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005946 return;
5947 }
5948
Bram Moolenaar844fb642021-10-23 13:32:30 +01005949 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005950 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5951
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005952 if (eap->skip)
5953 ++emsg_skip;
5954
5955 eap->nextcmd = NULL;
5956 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005957 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005958 {
5959 if (!eap->skip)
5960 returning = do_return(eap, FALSE, TRUE, &rettv);
5961 else
5962 clear_tv(&rettv);
5963 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005964 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005965 else if (!eap->skip)
5966 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005967 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005968 update_force_abort();
5969
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005970 /*
5971 * Return unless the expression evaluation has been cancelled due to an
5972 * aborting error, an interrupt, or an exception.
5973 */
5974 if (!aborting())
5975 returning = do_return(eap, FALSE, TRUE, NULL);
5976 }
5977
Bram Moolenaare38eab22019-12-05 21:50:01 +01005978 // When skipping or the return gets pending, advance to the next command
5979 // in this line (!returning). Otherwise, ignore the rest of the line.
5980 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005981 if (returning)
5982 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005983 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02005984 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005985
5986 if (eap->skip)
5987 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02005988 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005989}
5990
zeertzjq5299c092023-04-12 20:48:16 +01005991/*
5992 * Lower level implementation of "call". Only called when not skipping.
5993 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005994 static int
5995ex_call_inner(
5996 exarg_T *eap,
5997 char_u *name,
5998 char_u **arg,
5999 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006000 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006001 evalarg_T *evalarg)
6002{
6003 linenr_T lnum;
6004 int doesrange;
6005 typval_T rettv;
6006 int failed = FALSE;
6007
zeertzjq5299c092023-04-12 20:48:16 +01006008 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006009 for ( ; lnum <= eap->line2; ++lnum)
6010 {
6011 funcexe_T funcexe;
6012
zeertzjq5299c092023-04-12 20:48:16 +01006013 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006014 {
6015 if (lnum > curbuf->b_ml.ml_line_count)
6016 {
6017 // If the function deleted lines or switched to another buffer
6018 // the line number may become invalid.
6019 emsg(_(e_invalid_range));
6020 break;
6021 }
6022 curwin->w_cursor.lnum = lnum;
6023 curwin->w_cursor.col = 0;
6024 curwin->w_cursor.coladd = 0;
6025 }
6026 *arg = startarg;
6027
6028 funcexe = *funcexe_init;
6029 funcexe.fe_doesrange = &doesrange;
6030 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6031 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6032 {
6033 failed = TRUE;
6034 break;
6035 }
6036 if (has_watchexpr())
6037 dbg_check_breakpoint(eap);
6038
6039 // Handle a function returning a Funcref, Dictionary or List.
6040 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006041 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006042 {
6043 failed = TRUE;
6044 break;
6045 }
6046
6047 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006048 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006049 break;
6050
6051 // Stop when immediately aborting on error, or when an interrupt
6052 // occurred or an exception was thrown but not caught.
6053 // get_func_tv() returned OK, so that the check for trailing
6054 // characters below is executed.
6055 if (aborting())
6056 break;
6057 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006058 return failed;
6059}
6060
6061/*
6062 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6063 * Returns FAIL or OK.
6064 */
6065 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006066ex_defer_inner(
6067 char_u *name,
6068 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006069 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006070 partial_T *partial,
6071 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006072{
6073 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006074 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006075 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006076 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006077
6078 if (current_funccal == NULL)
6079 {
6080 semsg(_(e_str_not_inside_function), "defer");
6081 return FAIL;
6082 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006083 if (partial != NULL)
6084 {
6085 if (partial->pt_dict != NULL)
6086 {
6087 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6088 return FAIL;
6089 }
6090 if (partial->pt_argc > 0)
6091 {
6092 int i;
6093
6094 partial_argc = partial->pt_argc;
6095 for (i = 0; i < partial_argc; ++i)
6096 copy_tv(&partial->pt_argv[i], &argvars[i]);
6097 }
6098 }
6099 r = get_func_arguments(arg, evalarg, FALSE,
6100 argvars + partial_argc, &argcount);
6101 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006102
6103 if (r == OK)
6104 {
6105 if (type != NULL)
6106 {
6107 // Check that the arguments are OK for the types of the funcref.
6108 r = check_argument_types(type, argvars, argcount, NULL, name);
6109 }
6110 else if (builtin_function(name, -1))
6111 {
6112 int idx = find_internal_func(name);
6113
6114 if (idx < 0)
6115 {
6116 emsg_funcname(e_unknown_function_str, name);
6117 r = FAIL;
6118 }
6119 else if (check_internal_func(idx, argcount) == -1)
6120 r = FAIL;
6121 }
6122 else
6123 {
6124 ufunc_T *ufunc = find_func(name, FALSE);
6125
6126 // we tolerate an unknown function here, it might be defined later
6127 if (ufunc != NULL)
6128 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006129 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006130 if (error != FCERR_UNKNOWN)
6131 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006132 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006133 r = FAIL;
6134 }
6135 }
6136 }
6137 }
6138
Bram Moolenaar86d87252022-09-05 21:21:25 +01006139 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006140 {
6141 while (--argcount >= 0)
6142 clear_tv(&argvars[argcount]);
6143 return FAIL;
6144 }
6145 return add_defer(name, argcount, argvars);
6146}
6147
6148/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006149 * Return TRUE if currently inside a function call.
6150 * Give an error message and return FALSE when not.
6151 */
6152 int
6153can_add_defer(void)
6154{
6155 if (!in_def_function() && get_current_funccal() == NULL)
6156 {
6157 semsg(_(e_str_not_inside_function), "defer");
6158 return FALSE;
6159 }
6160 return TRUE;
6161}
6162
6163/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006164 * Add a deferred call for "name" with arguments "argvars[argcount]".
6165 * Consumes "argvars[]".
6166 * Caller must check that in_def_function() returns TRUE or current_funccal is
6167 * not NULL.
6168 * Returns OK or FAIL.
6169 */
6170 int
6171add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6172{
6173 char_u *saved_name = vim_strsave(name);
6174 int argcount = argcount_arg;
6175 defer_T *dr;
6176 int ret = FAIL;
6177
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006178 if (saved_name == NULL)
6179 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006180 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006181 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006182 if (add_defer_function(saved_name, argcount, argvars) == OK)
6183 argcount = 0;
6184 }
6185 else
6186 {
6187 if (current_funccal->fc_defer.ga_itemsize == 0)
6188 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6189 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6190 goto theend;
6191 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6192 + current_funccal->fc_defer.ga_len++;
6193 dr->dr_name = saved_name;
6194 dr->dr_argcount = argcount;
6195 while (argcount > 0)
6196 {
6197 --argcount;
6198 dr->dr_argvars[argcount] = argvars[argcount];
6199 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006200 }
6201 ret = OK;
6202
6203theend:
6204 while (--argcount >= 0)
6205 clear_tv(&argvars[argcount]);
6206 return ret;
6207}
6208
6209/*
6210 * Invoked after a functions has finished: invoke ":defer" functions.
6211 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006212 static void
6213handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006214{
6215 int idx;
6216
Bram Moolenaar58779852022-09-06 18:31:14 +01006217 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006218 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006219 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006220
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006221 if (dr->dr_name == NULL)
6222 // already being called, can happen if function does ":qa"
6223 continue;
6224
6225 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006226 CLEAR_FIELD(funcexe);
6227 funcexe.fe_evaluate = TRUE;
6228
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006229 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006230 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006231
6232 char_u *name = dr->dr_name;
6233 dr->dr_name = NULL;
6234
6235 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6236
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006237 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006238 vim_free(name);
6239 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006240 clear_tv(&dr->dr_argvars[i]);
6241 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006242 ga_clear(&funccal->fc_defer);
6243}
6244
zeertzjq960cf912023-04-18 21:52:54 +01006245 static void
6246invoke_funccall_defer(funccall_T *fc)
6247{
6248 if (fc->fc_ectx != NULL)
6249 {
6250 // :def function
6251 unwind_def_callstack(fc->fc_ectx);
6252 may_invoke_defer_funcs(fc->fc_ectx);
6253 }
6254 else
6255 {
6256 // legacy function
6257 handle_defer_one(fc);
6258 }
6259}
6260
Bram Moolenaar58779852022-09-06 18:31:14 +01006261/*
6262 * Called when exiting: call all defer functions.
6263 */
6264 void
6265invoke_all_defer(void)
6266{
zeertzjq1be4b812023-04-19 14:21:24 +01006267 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6268 invoke_funccall_defer(fc);
6269
zeertzjq960cf912023-04-18 21:52:54 +01006270 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6271 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6272 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006273}
6274
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006275/*
6276 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006277 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006278 */
6279 void
6280ex_call(exarg_T *eap)
6281{
6282 char_u *arg = eap->arg;
6283 char_u *startarg;
6284 char_u *name;
6285 char_u *tofree;
6286 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006287 int failed = FALSE;
6288 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006289 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006290 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006291 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006292 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006293 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006294 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006295
Bram Moolenaare6b53242020-07-01 17:28:33 +02006296 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006297 if (eap->skip)
6298 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006299 typval_T rettv;
6300
Bram Moolenaare38eab22019-12-05 21:50:01 +01006301 // trans_function_name() doesn't work well when skipping, use eval0()
6302 // instead to skip to any following command, e.g. for:
6303 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006304 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006305 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006306 clear_tv(&rettv);
6307 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006308 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006309 return;
6310 }
6311
zeertzjq5299c092023-04-12 20:48:16 +01006312 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006313 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006314 if (fudi.fd_newkey != NULL)
6315 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006316 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006317 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006318 vim_free(fudi.fd_newkey);
6319 }
6320 if (tofree == NULL)
6321 return;
6322
Bram Moolenaare38eab22019-12-05 21:50:01 +01006323 // Increase refcount on dictionary, it could get deleted when evaluating
6324 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006325 if (fudi.fd_dict != NULL)
6326 ++fudi.fd_dict->dv_refcount;
6327
Bram Moolenaare38eab22019-12-05 21:50:01 +01006328 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6329 // contents. For VAR_PARTIAL get its partial, unless we already have one
6330 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006331 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006332 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006333 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006334 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006335
Bram Moolenaare38eab22019-12-05 21:50:01 +01006336 // Skip white space to allow ":call func ()". Not good, but required for
6337 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006338 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006339 if (*startarg != '(')
6340 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006341 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006342 goto end;
6343 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006344 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006345 {
6346 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6347 goto end;
6348 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006349
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006350 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006351 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006352 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006353 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006354 }
6355 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006356 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006357 funcexe_T funcexe;
6358
Bram Moolenaara80faa82020-04-12 19:37:17 +02006359 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006360 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006361 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006362 funcexe.fe_partial = partial;
6363 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006364 funcexe.fe_firstline = eap->line1;
6365 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006366 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006367 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006368 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006369 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006370
Bram Moolenaar1d341892021-09-18 15:25:52 +02006371 // When inside :try we need to check for following "| catch" or "| endtry".
6372 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006373 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006374 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006375 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006376 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006377 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006378 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006379 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006380 {
6381 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006382 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006383 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006384 }
6385 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006386 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006387 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006388 // Must be after using "arg", it may point into memory cleared here.
6389 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006390
6391end:
6392 dict_unref(fudi.fd_dict);
6393 vim_free(tofree);
6394}
6395
6396/*
6397 * Return from a function. Possibly makes the return pending. Also called
6398 * for a pending return at the ":endtry" or after returning from an extra
6399 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6400 * when called due to a ":return" command. "rettv" may point to a typval_T
6401 * with the return rettv. Returns TRUE when the return can be carried out,
6402 * FALSE when the return gets pending.
6403 */
6404 int
6405do_return(
6406 exarg_T *eap,
6407 int reanimate,
6408 int is_cmd,
6409 void *rettv)
6410{
6411 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006412 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006413
6414 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006415 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006416 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006417
6418 /*
6419 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6420 * not in its finally clause (which then is to be executed next) is found.
6421 * In this case, make the ":return" pending for execution at the ":endtry".
6422 * Otherwise, return normally.
6423 */
6424 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6425 if (idx >= 0)
6426 {
6427 cstack->cs_pending[idx] = CSTP_RETURN;
6428
6429 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006430 // A pending return again gets pending. "rettv" points to an
6431 // allocated variable with the rettv of the original ":return"'s
6432 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006433 cstack->cs_rettv[idx] = rettv;
6434 else
6435 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006436 // When undoing a return in order to make it pending, get the stored
6437 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006438 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006439 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006440
6441 if (rettv != NULL)
6442 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006443 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006444 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6445 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6446 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006447 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006448 }
6449 else
6450 cstack->cs_rettv[idx] = NULL;
6451
6452 if (reanimate)
6453 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006454 // The pending return value could be overwritten by a ":return"
6455 // without argument in a finally clause; reset the default
6456 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006457 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6458 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006459 }
6460 }
6461 report_make_pending(CSTP_RETURN, rettv);
6462 }
6463 else
6464 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006465 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006466
Bram Moolenaare38eab22019-12-05 21:50:01 +01006467 // If the return is carried out now, store the return value. For
6468 // a return immediately after reanimation, the value is already
6469 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006470 if (!reanimate && rettv != NULL)
6471 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006472 clear_tv(current_funccal->fc_rettv);
6473 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006474 if (!is_cmd)
6475 vim_free(rettv);
6476 }
6477 }
6478
6479 return idx < 0;
6480}
6481
6482/*
6483 * Free the variable with a pending return value.
6484 */
6485 void
6486discard_pending_return(void *rettv)
6487{
6488 free_tv((typval_T *)rettv);
6489}
6490
6491/*
6492 * Generate a return command for producing the value of "rettv". The result
6493 * is an allocated string. Used by report_pending() for verbose messages.
6494 */
6495 char_u *
6496get_return_cmd(void *rettv)
6497{
6498 char_u *s = NULL;
6499 char_u *tofree = NULL;
6500 char_u numbuf[NUMBUFLEN];
6501
6502 if (rettv != NULL)
6503 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6504 if (s == NULL)
6505 s = (char_u *)"";
6506
6507 STRCPY(IObuff, ":return ");
6508 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6509 if (STRLEN(s) + 8 >= IOSIZE)
6510 STRCPY(IObuff + IOSIZE - 4, "...");
6511 vim_free(tofree);
6512 return vim_strsave(IObuff);
6513}
6514
6515/*
6516 * Get next function line.
6517 * Called by do_cmdline() to get the next line.
6518 * Returns allocated string, or NULL for end of function.
6519 */
6520 char_u *
6521get_func_line(
6522 int c UNUSED,
6523 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006524 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006525 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006526{
6527 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006528 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006529 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006530 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006531
Bram Moolenaare38eab22019-12-05 21:50:01 +01006532 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006533 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006534 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006535 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006536 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006537 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006538 }
6539#ifdef FEAT_PROFILE
6540 if (do_profiling == PROF_YES)
6541 func_line_end(cookie);
6542#endif
6543
6544 gap = &fp->uf_lines;
6545 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006546 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006547 retval = NULL;
6548 else
6549 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006550 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006551 while (fcp->fc_linenr < gap->ga_len
6552 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6553 ++fcp->fc_linenr;
6554 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006555 retval = NULL;
6556 else
6557 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006558 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6559 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006560#ifdef FEAT_PROFILE
6561 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006562 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006563#endif
6564 }
6565 }
6566
Bram Moolenaare38eab22019-12-05 21:50:01 +01006567 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006568 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006569 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006570 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006571 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006572 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006573 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006574 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006575 }
6576
6577 return retval;
6578}
6579
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006580/*
6581 * Return TRUE if the currently active function should be ended, because a
6582 * return was encountered or an error occurred. Used inside a ":while".
6583 */
6584 int
6585func_has_ended(void *cookie)
6586{
6587 funccall_T *fcp = (funccall_T *)cookie;
6588
Bram Moolenaare38eab22019-12-05 21:50:01 +01006589 // Ignore the "abort" flag if the abortion behavior has been changed due to
6590 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006591 return (((fcp->fc_func->uf_flags & FC_ABORT)
6592 && did_emsg && !aborted_in_try())
6593 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006594}
6595
6596/*
6597 * return TRUE if cookie indicates a function which "abort"s on errors.
6598 */
6599 int
6600func_has_abort(
6601 void *cookie)
6602{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006603 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006604}
6605
6606
6607/*
6608 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6609 * Don't do this when "Func" is already a partial that was bound
6610 * explicitly (pt_auto is FALSE).
6611 * Changes "rettv" in-place.
6612 * Returns the updated "selfdict_in".
6613 */
6614 dict_T *
6615make_partial(dict_T *selfdict_in, typval_T *rettv)
6616{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006617 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006618 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006619 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006620 dict_T *selfdict = selfdict_in;
6621
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006622 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6623 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006624 fp = rettv->vval.v_partial->pt_func;
6625 else
6626 {
6627 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006628 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006629 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006630 if (fname == NULL)
6631 {
6632 // There is no point binding a dict to a NULL function, just create
6633 // a function reference.
6634 rettv->v_type = VAR_FUNC;
6635 rettv->vval.v_string = NULL;
6636 }
6637 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006638 {
6639 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006640 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006641
6642 // Translate "s:func" to the stored function name.
6643 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6644 fp = find_func(fname, FALSE);
6645 vim_free(tofree);
6646 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006647 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006648
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006649 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006650 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006651 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006652
6653 if (pt != NULL)
6654 {
6655 pt->pt_refcount = 1;
6656 pt->pt_dict = selfdict;
6657 pt->pt_auto = TRUE;
6658 selfdict = NULL;
6659 if (rettv->v_type == VAR_FUNC)
6660 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006661 // Just a function: Take over the function name and use
6662 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006663 pt->pt_name = rettv->vval.v_string;
6664 }
6665 else
6666 {
6667 partial_T *ret_pt = rettv->vval.v_partial;
6668 int i;
6669
Bram Moolenaare38eab22019-12-05 21:50:01 +01006670 // Partial: copy the function name, use selfdict and copy
6671 // args. Can't take over name or args, the partial might
6672 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006673 if (ret_pt->pt_name != NULL)
6674 {
6675 pt->pt_name = vim_strsave(ret_pt->pt_name);
6676 func_ref(pt->pt_name);
6677 }
6678 else
6679 {
6680 pt->pt_func = ret_pt->pt_func;
6681 func_ptr_ref(pt->pt_func);
6682 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006683 if (ret_pt->pt_argc > 0)
6684 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006685 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006686 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006687 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006688 pt->pt_argc = 0;
6689 else
6690 {
6691 pt->pt_argc = ret_pt->pt_argc;
6692 for (i = 0; i < pt->pt_argc; i++)
6693 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6694 }
6695 }
6696 partial_unref(ret_pt);
6697 }
6698 rettv->v_type = VAR_PARTIAL;
6699 rettv->vval.v_partial = pt;
6700 }
6701 }
6702 return selfdict;
6703}
6704
6705/*
6706 * Return the name of the executed function.
6707 */
6708 char_u *
6709func_name(void *cookie)
6710{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006711 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006712}
6713
6714/*
6715 * Return the address holding the next breakpoint line for a funccall cookie.
6716 */
6717 linenr_T *
6718func_breakpoint(void *cookie)
6719{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006720 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006721}
6722
6723/*
6724 * Return the address holding the debug tick for a funccall cookie.
6725 */
6726 int *
6727func_dbg_tick(void *cookie)
6728{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006729 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006730}
6731
6732/*
6733 * Return the nesting level for a funccall cookie.
6734 */
6735 int
6736func_level(void *cookie)
6737{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006738 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006739}
6740
6741/*
6742 * Return TRUE when a function was ended by a ":return" command.
6743 */
6744 int
6745current_func_returned(void)
6746{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006747 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006748}
6749
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006750 int
6751free_unref_funccal(int copyID, int testing)
6752{
6753 int did_free = FALSE;
6754 int did_free_funccal = FALSE;
6755 funccall_T *fc, **pfc;
6756
6757 for (pfc = &previous_funccal; *pfc != NULL; )
6758 {
6759 if (can_free_funccal(*pfc, copyID))
6760 {
6761 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006762 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006763 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006764 did_free = TRUE;
6765 did_free_funccal = TRUE;
6766 }
6767 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006768 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006769 }
6770 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006771 // When a funccal was freed some more items might be garbage
6772 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006773 (void)garbage_collect(testing);
6774
6775 return did_free;
6776}
6777
6778/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006779 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006780 */
6781 static funccall_T *
6782get_funccal(void)
6783{
6784 int i;
6785 funccall_T *funccal;
6786 funccall_T *temp_funccal;
6787
6788 funccal = current_funccal;
6789 if (debug_backtrace_level > 0)
6790 {
6791 for (i = 0; i < debug_backtrace_level; i++)
6792 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006793 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006794 if (temp_funccal)
6795 funccal = temp_funccal;
6796 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006797 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006798 debug_backtrace_level = i;
6799 }
6800 }
6801 return funccal;
6802}
6803
6804/*
6805 * Return the hashtable used for local variables in the current funccal.
6806 * Return NULL if there is no current funccal.
6807 */
6808 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006809get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006810{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006811 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006812 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006813 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006814}
6815
6816/*
6817 * Return the l: scope variable.
6818 * Return NULL if there is no current funccal.
6819 */
6820 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006821get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006822{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006823 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006824 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006825 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006826}
6827
6828/*
6829 * Return the hashtable used for argument in the current funccal.
6830 * Return NULL if there is no current funccal.
6831 */
6832 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006833get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006834{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006835 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006836 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006837 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006838}
6839
6840/*
6841 * Return the a: scope variable.
6842 * Return NULL if there is no current funccal.
6843 */
6844 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006845get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006846{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006847 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006848 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006849 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006850}
6851
6852/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006853 * List function variables, if there is a function.
6854 */
6855 void
6856list_func_vars(int *first)
6857{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006858 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
6859 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006860 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006861}
6862
6863/*
6864 * If "ht" is the hashtable for local variables in the current funccal, return
6865 * the dict that contains it.
6866 * Otherwise return NULL.
6867 */
6868 dict_T *
6869get_current_funccal_dict(hashtab_T *ht)
6870{
6871 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01006872 && ht == &current_funccal->fc_l_vars.dv_hashtab)
6873 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006874 return NULL;
6875}
6876
6877/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006878 * Search hashitem in parent scope.
6879 */
6880 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006881find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006882{
6883 funccall_T *old_current_funccal = current_funccal;
6884 hashtab_T *ht;
6885 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006886 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006887
Bram Moolenaarca16c602022-09-06 18:57:08 +01006888 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006889 return NULL;
6890
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006891 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006892 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006893 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006894 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006895 ht = find_var_ht(name, &varname);
6896 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006897 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006898 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006899 if (!HASHITEM_EMPTY(hi))
6900 {
6901 *pht = ht;
6902 break;
6903 }
6904 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006905 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02006906 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006907 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006908 }
6909 current_funccal = old_current_funccal;
6910
6911 return hi;
6912}
6913
6914/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006915 * Search variable in parent scope.
6916 */
6917 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006918find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006919{
6920 dictitem_T *v = NULL;
6921 funccall_T *old_current_funccal = current_funccal;
6922 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006923 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006924
Bram Moolenaarca16c602022-09-06 18:57:08 +01006925 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006926 return NULL;
6927
Bram Moolenaare38eab22019-12-05 21:50:01 +01006928 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01006929 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006930 while (current_funccal)
6931 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006932 ht = find_var_ht(name, &varname);
6933 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006934 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006935 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006936 if (v != NULL)
6937 break;
6938 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006939 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006940 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006941 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006942 }
6943 current_funccal = old_current_funccal;
6944
6945 return v;
6946}
6947
6948/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006949 * Set "copyID + 1" in previous_funccal and callers.
6950 */
6951 int
6952set_ref_in_previous_funccal(int copyID)
6953{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006954 funccall_T *fc;
6955
Bram Moolenaarca16c602022-09-06 18:57:08 +01006956 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006957 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006958 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006959 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
6960 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
6961 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006962 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006963 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006964 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006965}
6966
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006967 static int
6968set_ref_in_funccal(funccall_T *fc, int copyID)
6969{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006970 if (fc->fc_copyID != copyID)
6971 {
6972 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006973 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
6974 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
6975 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
6976 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006977 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006978 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006979 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006980}
6981
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006982/*
6983 * Set "copyID" in all local vars and arguments in the call stack.
6984 */
6985 int
6986set_ref_in_call_stack(int copyID)
6987{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006988 funccall_T *fc;
6989 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006990
Bram Moolenaarca16c602022-09-06 18:57:08 +01006991 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006992 if (set_ref_in_funccal(fc, copyID))
6993 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006994
6995 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006996 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006997 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006998 if (set_ref_in_funccal(fc, copyID))
6999 return TRUE;
7000 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007001}
7002
7003/*
7004 * Set "copyID" in all functions available by name.
7005 */
7006 int
7007set_ref_in_functions(int copyID)
7008{
7009 int todo;
7010 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007011 ufunc_T *fp;
7012
7013 todo = (int)func_hashtab.ht_used;
7014 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007015 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007016 if (!HASHITEM_EMPTY(hi))
7017 {
7018 --todo;
7019 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007020 if (!func_name_refcount(fp->uf_name)
7021 && set_ref_in_func(NULL, fp, copyID))
7022 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007023 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007024 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007025 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007026}
7027
7028/*
7029 * Set "copyID" in all function arguments.
7030 */
7031 int
7032set_ref_in_func_args(int copyID)
7033{
7034 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007035
7036 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007037 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7038 copyID, NULL, NULL))
7039 return TRUE;
7040 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007041}
7042
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007043/*
7044 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007045 * Returns TRUE if setting references failed somehow.
7046 */
7047 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007048set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007049{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007050 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007051 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007052 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007053 char_u fname_buf[FLEN_FIXED + 1];
7054 char_u *tofree = NULL;
7055 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007056 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007057
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007058 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007059 return FALSE;
7060
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007061 if (fp_in == NULL)
7062 {
7063 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007064 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007065 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007066 if (fp != NULL)
7067 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007068 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007069 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007070 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007071
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007072 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007073 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007074}
7075
Bram Moolenaare38eab22019-12-05 21:50:01 +01007076#endif // FEAT_EVAL