blob: 092b3927b5b7cfb8dd28cfe15a74835d56fb66f9 [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;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003543 if (partial != NULL)
3544 {
3545 funcexe.fe_object = partial->pt_obj;
3546 if (funcexe.fe_object != NULL)
3547 ++funcexe.fe_object->obj_refcount;
3548 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003549 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003550 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3551 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003552
Bram Moolenaare38eab22019-12-05 21:50:01 +01003553 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003554 while (argc > 0)
3555 clear_tv(&argv[--argc]);
3556
3557 return r;
3558}
3559
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003560static int callback_depth = 0;
3561
3562 int
3563get_callback_depth(void)
3564{
3565 return callback_depth;
3566}
3567
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003568/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003569 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003570 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003571 */
3572 int
3573call_callback(
3574 callback_T *callback,
3575 int len, // length of "name" or -1 to use strlen()
3576 typval_T *rettv, // return value goes here
3577 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003578 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003579 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003580{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003581 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003582 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003583
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003584 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3585 return FAIL;
Christian Brabandt47510f32023-10-15 09:56:16 +02003586
3587 if (callback_depth > p_mfd)
3588 {
3589 emsg(_(e_command_too_recursive));
3590 return FAIL;
3591 }
3592
Bram Moolenaara80faa82020-04-12 19:37:17 +02003593 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003594 funcexe.fe_evaluate = TRUE;
3595 funcexe.fe_partial = callback->cb_partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003596 if (callback->cb_partial != NULL)
3597 {
3598 funcexe.fe_object = callback->cb_partial->pt_obj;
3599 if (funcexe.fe_object != NULL)
3600 ++funcexe.fe_object->obj_refcount;
3601 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003602 ++callback_depth;
3603 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3604 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003605
3606 // When a :def function was called that uses :try an error would be turned
3607 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003608 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003609 {
3610 need_rethrow = FALSE;
3611 handle_did_throw();
3612 }
3613
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003614 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003615}
3616
3617/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003618 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003619 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003620 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3621 */
3622 varnumber_T
3623call_callback_retnr(
3624 callback_T *callback,
3625 int argcount, // number of "argvars"
3626 typval_T *argvars) // vars for arguments, must have "argcount"
3627 // PLUS ONE elements!
3628{
3629 typval_T rettv;
3630 varnumber_T retval;
3631
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003632 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003633 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003634
3635 retval = tv_get_number_chk(&rettv, NULL);
3636 clear_tv(&rettv);
3637 return retval;
3638}
3639
3640/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 * Give an error message for the result of a function.
3642 * Nothing if "error" is FCERR_NONE.
3643 */
3644 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003645user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646{
3647 switch (error)
3648 {
3649 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003650 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003651 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003652 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003653 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 break;
3655 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003656 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 break;
3658 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003659 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 break;
3661 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003662 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 break;
3664 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003665 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 break;
3667 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003668 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 break;
3670 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003671 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3672 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003674 case FCERR_OTHER:
3675 case FCERR_FAILED:
3676 // assume the error message was already given
3677 break;
3678 case FCERR_NONE:
3679 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 }
3681}
3682
3683/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003684 * Check the argument types "argvars[argcount]" for "name" using the
3685 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3686 * is already included in "argvars[]".
3687 * Will do nothing if "funcexe->fe_check_type" is NULL or
3688 * "funcexe->fe_evaluate" is FALSE;
3689 * Returns an FCERR_ value.
3690 */
3691 static funcerror_T
3692may_check_argument_types(
3693 funcexe_T *funcexe,
3694 typval_T *argvars,
3695 int argcount,
3696 int base_included,
3697 char_u *name)
3698{
3699 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3700 {
3701 // Check that the argument types are OK for the types of the funcref.
3702 if (check_argument_types(funcexe->fe_check_type,
3703 argvars, argcount,
3704 base_included ? NULL : funcexe->fe_basetv,
3705 name) == FAIL)
3706 return FCERR_OTHER;
3707 }
3708 return FCERR_NONE;
3709}
3710
3711/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003712 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003713 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003714 * Return FAIL when the function can't be called, OK otherwise.
3715 * Also returns OK when an error was encountered while executing the function.
3716 */
3717 int
3718call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003719 char_u *funcname, // name of the function
3720 int len, // length of "name" or -1 to use strlen()
3721 typval_T *rettv, // return value goes here
3722 int argcount_in, // number of "argvars"
3723 typval_T *argvars_in, // vars for arguments, must have "argcount"
3724 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003725 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003726{
3727 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003728 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003729 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003730 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003731 char_u fname_buf[FLEN_FIXED + 1];
3732 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003733 char_u *fname = NULL;
3734 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003735 int argcount = argcount_in;
3736 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003737 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003738 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003739 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003740 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003741 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003742 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003743 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003744 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003745
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003746 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3747 // even when call_func() returns FAIL.
3748 rettv->v_type = VAR_UNKNOWN;
3749
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003750 if (partial != NULL)
3751 fp = partial->pt_func;
3752 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003753 fp = funcexe->fe_ufunc;
3754
3755 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003756 {
3757 // Make a copy of the name, if it comes from a funcref variable it
3758 // could be changed or deleted in the called function.
3759 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3760 if (name == NULL)
3761 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003762
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003763 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3764 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003765
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003766 if (funcexe->fe_doesrange != NULL)
3767 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003768
3769 if (partial != NULL)
3770 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003771 // When the function has a partial with a dict and there is a dict
3772 // argument, use the dict argument. That is backwards compatible.
3773 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003774 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003775 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003776 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003777 {
3778 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003779 {
3780 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3781 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003782 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003783 goto theend;
3784 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003785 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003786 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003787 for (i = 0; i < argcount_in; ++i)
3788 argv[i + argv_clear] = argvars_in[i];
3789 argvars = argv;
3790 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003791
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003792 if (funcexe->fe_check_type != NULL
3793 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003794 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003795 // Now funcexe->fe_check_type is missing the added arguments,
3796 // make a copy of the type with the correction.
3797 check_type = *funcexe->fe_check_type;
3798 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003799 check_type.tt_args = check_type_args;
3800 CLEAR_FIELD(check_type_args);
3801 for (i = 0; i < check_type.tt_argcount; ++i)
3802 check_type_args[i + partial->pt_argc] =
3803 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003804 check_type.tt_argcount += partial->pt_argc;
3805 check_type.tt_min_argcount += partial->pt_argc;
3806 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003807 }
3808 }
3809
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003810 if (error == FCERR_NONE)
3811 // check the argument types if possible
3812 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
3813 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003814
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003815 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003816 {
3817 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003818 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003819
Bram Moolenaar333894b2020-08-01 18:53:07 +02003820 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003821 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003822 {
3823 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003824 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003825 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003826
Bram Moolenaare38eab22019-12-05 21:50:01 +01003827 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003828 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003829 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003830
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003831 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003832 {
3833 /*
3834 * User defined function.
3835 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003836 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003837 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003838 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003839 if (fp != NULL && !is_global && in_vim9script()
3840 && func_requires_g_prefix(fp))
3841 // In Vim9 script g: is required to find a global
3842 // non-autoload function.
3843 fp = NULL;
3844 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003845
Bram Moolenaare38eab22019-12-05 21:50:01 +01003846 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003847 if (fp == NULL
3848 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003849 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003850 && !aborting())
3851 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003852 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003853 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003854 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003855 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003856 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3857 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003858 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003859 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003860 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003861 if (fp == NULL)
3862 {
3863 char_u *p = untrans_function_name(rfname);
3864
3865 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003866 // Don't do this if the name starts with "s:".
3867 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003868 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003869 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003870
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003871 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003872 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003873 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003874 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003875 int need_arg_check = FALSE;
3876 if (funcexe->fe_check_type == NULL)
3877 {
3878 funcexe->fe_check_type = fp->uf_func_type;
3879 need_arg_check = TRUE;
3880 }
3881
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003882 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003883 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003884 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003885 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003886 argv_clear, fp);
3887 need_arg_check = TRUE;
3888 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003889
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003890 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003891 {
3892 // Method call: base->Method()
3893 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003894 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003895 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003896 argvars = argv;
3897 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003898 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003899 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003900
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003901 // Check the argument types now that the function type and all
3902 // argument values are known, if not done above.
3903 if (need_arg_check)
3904 error = may_check_argument_types(funcexe, argvars, argcount,
3905 TRUE, (name != NULL) ? name : funcname);
3906 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
3907 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003909 }
3910 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003911 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003912 {
3913 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003914 * expr->method(): Find the method name in the table, call its
3915 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003916 */
3917 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003918 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003919 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003920 else
3921 {
3922 /*
3923 * Find the function name in the table, call its implementation.
3924 */
3925 error = call_internal_func(fname, argcount, argvars, rettv);
3926 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003927
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003928 /*
3929 * The function call (or "FuncUndefined" autocommand sequence) might
3930 * have been aborted by an error, an interrupt, or an explicitly thrown
3931 * exception that has not been caught so far. This situation can be
3932 * tested for by calling aborting(). For an error in an internal
3933 * function or for the "E132" error in call_user_func(), however, the
3934 * throw point at which the "force_abort" flag (temporarily reset by
3935 * emsg()) is normally updated has not been reached yet. We need to
3936 * update that flag first to make aborting() reliable.
3937 */
3938 update_force_abort();
3939 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003940 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003941 ret = OK;
3942
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003943theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003944 /*
3945 * Report an error unless the argument evaluation or function call has been
3946 * cancelled due to an aborting error, an interrupt, or an exception.
3947 */
3948 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003949 user_func_error(error, (name != NULL) ? name : funcname,
3950 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003951
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003952 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003953 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003954 clear_tv(&argv[--argv_clear + argv_base]);
3955
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003956 vim_free(tofree);
3957 vim_free(name);
3958
3959 return ret;
3960}
3961
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003962/*
3963 * Call a function without arguments, partial or dict.
3964 * This is like call_func() when the call is only "FuncName()".
3965 * To be used by "expr" options.
3966 * Returns NOTDONE when the function could not be found.
3967 */
3968 int
3969call_simple_func(
3970 char_u *funcname, // name of the function
3971 int len, // length of "name" or -1 to use strlen()
3972 typval_T *rettv) // return value goes here
3973{
3974 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003975 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003976 char_u fname_buf[FLEN_FIXED + 1];
3977 char_u *tofree = NULL;
3978 char_u *name;
3979 char_u *fname;
3980 char_u *rfname;
3981 int is_global = FALSE;
3982 ufunc_T *fp;
3983
3984 rettv->v_type = VAR_NUMBER; // default rettv is number zero
3985 rettv->vval.v_number = 0;
3986
3987 // Make a copy of the name, an option can be changed in the function.
3988 name = vim_strnsave(funcname, len);
3989 if (name == NULL)
3990 return ret;
3991
3992 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3993
3994 // Skip "g:" before a function name.
3995 if (fname[0] == 'g' && fname[1] == ':')
3996 {
3997 is_global = TRUE;
3998 rfname = fname + 2;
3999 }
4000 else
4001 rfname = fname;
4002 fp = find_func(rfname, is_global);
4003 if (fp != NULL && !is_global && in_vim9script()
4004 && func_requires_g_prefix(fp))
4005 // In Vim9 script g: is required to find a global non-autoload
4006 // function.
4007 fp = NULL;
4008 if (fp == NULL)
4009 ret = NOTDONE;
4010 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
4011 error = FCERR_DELETED;
4012 else if (fp != NULL)
4013 {
4014 typval_T argvars[1];
4015 funcexe_T funcexe;
4016
4017 argvars[0].v_type = VAR_UNKNOWN;
4018 CLEAR_FIELD(funcexe);
4019 funcexe.fe_evaluate = TRUE;
4020
4021 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
4022 if (error == FCERR_NONE)
4023 ret = OK;
4024 }
4025
4026 user_func_error(error, name, FALSE);
4027 vim_free(tofree);
4028 vim_free(name);
4029
4030 return ret;
4031}
4032
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004033 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034printable_func_name(ufunc_T *fp)
4035{
4036 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4037}
4038
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004039/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004040 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4041 * TRUE. Otherwise return FALSE.
4042 */
4043 static int
4044function_list_modified(int prev_ht_changed)
4045{
4046 if (prev_ht_changed != func_hashtab.ht_changed)
4047 {
4048 emsg(_(e_function_list_was_modified));
4049 return TRUE;
4050 }
4051 return FALSE;
4052}
4053
4054/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004055 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004056 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004057 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004058list_func_head(ufunc_T *fp, int indent)
4059{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004060 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004061 int j;
4062
4063 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004064
4065 // a timer at the more prompt may have deleted the function
4066 if (function_list_modified(prev_ht_changed))
4067 return FAIL;
4068
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004069 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004070 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004071 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004072 msg_puts("def ");
4073 else
4074 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004076 msg_putchar('(');
4077 for (j = 0; j < fp->uf_args.ga_len; ++j)
4078 {
4079 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004080 msg_puts(", ");
4081 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082 if (fp->uf_arg_types != NULL)
4083 {
4084 char *tofree;
4085
4086 msg_puts(": ");
4087 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4088 vim_free(tofree);
4089 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004090 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4091 {
4092 msg_puts(" = ");
4093 msg_puts(((char **)(fp->uf_def_args.ga_data))
4094 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4095 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004096 }
4097 if (fp->uf_varargs)
4098 {
4099 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004100 msg_puts(", ");
4101 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004102 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 if (fp->uf_va_name != NULL)
4104 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004105 if (!fp->uf_varargs)
4106 {
4107 if (j)
4108 msg_puts(", ");
4109 msg_puts("...");
4110 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004112 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 {
4114 char *tofree;
4115
4116 msg_puts(": ");
4117 msg_puts(type_name(fp->uf_va_type, &tofree));
4118 vim_free(tofree);
4119 }
4120 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004121 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004122
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004123 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004124 {
4125 if (fp->uf_ret_type != &t_void)
4126 {
4127 char *tofree;
4128
4129 msg_puts(": ");
4130 msg_puts(type_name(fp->uf_ret_type, &tofree));
4131 vim_free(tofree);
4132 }
4133 }
4134 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004135 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004136 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004137 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004138 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004139 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004140 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004141 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004142 msg_clr_eos();
4143 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004144 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004145
4146 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004147}
4148
4149/*
4150 * Get a function name, translating "<SID>" and "<SNR>".
4151 * Also handles a Funcref in a List or Dictionary.
4152 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004153 * Set "*is_global" to TRUE when the function must be global, unless
4154 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004155 * flags:
4156 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004157 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004158 * TFN_QUIET: be quiet
4159 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004160 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004161 * Advances "pp" to just after the function name (if no error).
4162 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004163 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004164trans_function_name(
4165 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004166 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004167 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004168 int flags)
4169{
4170 return trans_function_name_ext(pp, is_global, skip, flags,
4171 NULL, NULL, NULL, NULL);
4172}
4173
4174/*
4175 * trans_function_name() with extra arguments.
4176 * "fdp", "partial", "type" and "ufunc" can be NULL.
4177 */
4178 char_u *
4179trans_function_name_ext(
4180 char_u **pp,
4181 int *is_global,
4182 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004183 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004184 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004185 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004186 type_T **type, // return: type of funcref
4187 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004188{
4189 char_u *name = NULL;
4190 char_u *start;
4191 char_u *end;
4192 int lead;
4193 char_u sid_buf[20];
4194 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004195 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004196 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004197 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004198 int vim9script = in_vim9script();
4199 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004200
4201 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004202 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004203 start = *pp;
4204
Bram Moolenaare38eab22019-12-05 21:50:01 +01004205 // Check for hard coded <SNR>: already translated function ID (from a user
4206 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004207 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4208 && (*pp)[2] == (int)KE_SNR)
4209 {
4210 *pp += 3;
4211 len = get_id_len(pp) + 3;
4212 return vim_strnsave(start, len);
4213 }
4214
Bram Moolenaare38eab22019-12-05 21:50:01 +01004215 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4216 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004217 lead = eval_fname_script(start);
4218 if (lead > 2)
4219 start += lead;
4220
Bram Moolenaare38eab22019-12-05 21:50:01 +01004221 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004222 end = get_lval(start, NULL, &lv, FALSE, skip,
4223 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004224 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004225 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004226 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004227 {
4228 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004229 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004230 goto theend;
4231 }
4232 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4233 {
4234 /*
4235 * Report an invalid expression in braces, unless the expression
4236 * evaluation has been cancelled due to an aborting error, an
4237 * interrupt, or an exception.
4238 */
4239 if (!aborting())
4240 {
4241 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004242 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004243 }
4244 else
4245 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4246 goto theend;
4247 }
4248
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004249 if (lv.ll_ufunc != NULL)
4250 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004251 if (ufunc != NULL)
4252 *ufunc = lv.ll_ufunc;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004253 name = vim_strsave(lv.ll_ufunc->uf_name);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004254 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004255 goto theend;
4256 }
4257
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004258 if (lv.ll_tv != NULL)
4259 {
4260 if (fdp != NULL)
4261 {
4262 fdp->fd_dict = lv.ll_dict;
4263 fdp->fd_newkey = lv.ll_newkey;
4264 lv.ll_newkey = NULL;
4265 fdp->fd_di = lv.ll_di;
4266 }
4267 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4268 {
4269 name = vim_strsave(lv.ll_tv->vval.v_string);
4270 *pp = end;
4271 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004272 else if (lv.ll_tv->v_type == VAR_CLASS
4273 && lv.ll_tv->vval.v_class != NULL)
4274 {
4275 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4276 *pp = end;
4277 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004278 else if (lv.ll_tv->v_type == VAR_PARTIAL
4279 && lv.ll_tv->vval.v_partial != NULL)
4280 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004281 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004282 *pp = end;
4283 if (partial != NULL)
4284 *partial = lv.ll_tv->vval.v_partial;
4285 }
4286 else
4287 {
4288 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4289 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004290 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004291 else
4292 *pp = end;
4293 name = NULL;
4294 }
4295 goto theend;
4296 }
4297
4298 if (lv.ll_name == NULL)
4299 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004300 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004301 *pp = end;
4302 goto theend;
4303 }
4304
Bram Moolenaare38eab22019-12-05 21:50:01 +01004305 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004306 if (lv.ll_exp_name != NULL)
4307 {
4308 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004309 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004310 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004311 if (name == lv.ll_exp_name)
4312 name = NULL;
4313 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004314 else if (lv.ll_sid > 0)
4315 {
4316 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4317 int cc = *lv.ll_name_end;
4318
4319 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4320 *lv.ll_name_end = NUL;
4321 if (si->sn_autoload_prefix != NULL)
4322 {
4323 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4324 }
4325 else
4326 {
4327 sid_buf[0] = K_SPECIAL;
4328 sid_buf[1] = KS_EXTRA;
4329 sid_buf[2] = (int)KE_SNR;
4330 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004331 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004332 name = concat_str(sid_buf, lv.ll_name);
4333 }
4334 *lv.ll_name_end = cc;
4335 *pp = end;
4336 goto theend;
4337 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004338 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004339 {
4340 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004341 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004342 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004343 if (name == *pp)
4344 name = NULL;
4345 }
4346 if (name != NULL)
4347 {
4348 name = vim_strsave(name);
4349 *pp = end;
4350 if (STRNCMP(name, "<SNR>", 5) == 0)
4351 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004352 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004353 name[0] = K_SPECIAL;
4354 name[1] = KS_EXTRA;
4355 name[2] = (int)KE_SNR;
4356 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4357 }
4358 goto theend;
4359 }
4360
4361 if (lv.ll_exp_name != NULL)
4362 {
4363 len = (int)STRLEN(lv.ll_exp_name);
4364 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4365 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4366 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004367 // When there was "s:" already or the name expanded to get a
4368 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004369 lv.ll_name += 2;
4370 len -= 2;
4371 lead = 2;
4372 }
4373 }
4374 else
4375 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004376 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004377 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004378 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004379 if (lv.ll_name[0] == 'g')
4380 {
4381 if (is_global != NULL)
4382 {
4383 *is_global = TRUE;
4384 }
4385 else
4386 {
4387 // dropping "g:" without setting "is_global" won't work in
4388 // Vim9script, put it back later
4389 prefix_g = TRUE;
4390 extra = 2;
4391 }
4392 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004393 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004394 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004395 len = (int)(end - lv.ll_name);
4396 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004397 if (len <= 0)
4398 {
4399 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004400 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004401 goto theend;
4402 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004403
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004404 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004405 // starts with a lower case character: dict.func(). Or when in a class.
4406 vim9_local = ASCII_ISUPPER(*start) && vim9script
4407 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004409 /*
4410 * Copy the function name to allocated memory.
4411 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4412 * Accept <SNR>123_name() outside a script.
4413 */
4414 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004415 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004416 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004417 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004418 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004419 {
Kota Kato948a3892022-08-16 16:09:59 +01004420 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4421 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004422 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004423 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004424 goto theend;
4425 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004427 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004428 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004430 || eval_fname_sid(*pp))
4431 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004433 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004434 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004435 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004436 goto theend;
4437 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004438 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004439 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 extra = 3 + (int)STRLEN(sid_buf);
4441 else
4442 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004443 }
4444 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004445 // The function name must start with an upper case letter (unless it is a
4446 // Vim9 class new() function or a Vim9 class private method)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004447 else if (!(flags & TFN_INT)
4448 && (builtin_function(lv.ll_name, len)
4449 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004450 && !((flags & TFN_IN_CLASS)
4451 && (STRNCMP(lv.ll_name, "new", 3) == 0
4452 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004453 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004454 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004455 : e_function_name_must_start_with_capital_or_s_str),
4456 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004457 goto theend;
4458 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004459 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004460 {
4461 char_u *cp = vim_strchr(lv.ll_name, ':');
4462
4463 if (cp != NULL && cp < end)
4464 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004465 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004466 goto theend;
4467 }
4468 }
4469
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004471 if (name != NULL)
4472 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004473 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004474 {
4475 name[0] = K_SPECIAL;
4476 name[1] = KS_EXTRA;
4477 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004478 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004479 STRCPY(name + 3, sid_buf);
4480 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004481 else if (prefix_g)
4482 {
4483 name[0] = 'g';
4484 name[1] = ':';
4485 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4487 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004488 }
4489 *pp = end;
4490
4491theend:
4492 clear_lval(&lv);
4493 return name;
4494}
4495
4496/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004497 * Assuming "name" is the result of trans_function_name() and it was prefixed
4498 * to use the script-local name, return the unmodified name (points into
4499 * "name"). Otherwise return NULL.
4500 * This can be used to first search for a script-local function and fall back
4501 * to the global function if not found.
4502 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004503 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004504untrans_function_name(char_u *name)
4505{
4506 char_u *p;
4507
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004508 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004509 {
4510 p = vim_strchr(name, '_');
4511 if (p != NULL)
4512 return p + 1;
4513 }
4514 return NULL;
4515}
4516
4517/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004518 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4519 * current script ID and returns the expanded function name. The caller should
4520 * free the returned name. If not called from a script context or the function
4521 * name doesn't start with these prefixes, then returns NULL.
4522 * This doesn't check whether the script-local function exists or not.
4523 */
4524 char_u *
4525get_scriptlocal_funcname(char_u *funcname)
4526{
4527 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004528 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004529 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004530 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004531
4532 if (funcname == NULL)
4533 return NULL;
4534
4535 if (STRNCMP(funcname, "s:", 2) != 0
4536 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004537 {
4538 ufunc_T *ufunc;
4539
4540 // The function name does not have a script-local prefix. Try finding
4541 // it when in a Vim9 script and there is no "g:" prefix.
4542 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4543 return NULL;
4544 ufunc = find_func(funcname, FALSE);
4545 if (ufunc == NULL || func_is_global(ufunc)
4546 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4547 return NULL;
4548 ++p;
4549 off = 0;
4550 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004551 else
4552 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004553
4554 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4555 {
4556 emsg(_(e_using_sid_not_in_script_context));
4557 return NULL;
4558 }
4559 // Expand s: prefix into <SNR>nr_<name>
4560 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4561 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004562 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004563 if (newname == NULL)
4564 return NULL;
4565 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004566 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004567
4568 return newname;
4569}
4570
4571/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004572 * Return script-local "fname" with the 3-byte sequence replaced by
4573 * printable <SNR> in allocated memory.
4574 */
4575 char_u *
4576alloc_printable_func_name(char_u *fname)
4577{
4578 char_u *n = alloc(STRLEN(fname + 3) + 6);
4579
4580 if (n != NULL)
4581 {
4582 STRCPY(n, "<SNR>");
4583 STRCPY(n + 5, fname + 3);
4584 }
4585 return n;
4586}
4587
4588/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004589 * Call trans_function_name(), except that a lambda is returned as-is.
4590 * Returns the name in allocated memory.
4591 */
4592 char_u *
4593save_function_name(
4594 char_u **name,
4595 int *is_global,
4596 int skip,
4597 int flags,
4598 funcdict_T *fudi)
4599{
4600 char_u *p = *name;
4601 char_u *saved;
4602
4603 if (STRNCMP(p, "<lambda>", 8) == 0)
4604 {
4605 p += 8;
4606 (void)getdigits(&p);
4607 saved = vim_strnsave(*name, p - *name);
4608 if (fudi != NULL)
4609 CLEAR_POINTER(fudi);
4610 }
4611 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004612 saved = trans_function_name_ext(&p, is_global, skip,
4613 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004614 *name = p;
4615 return saved;
4616}
4617
4618/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004619 * List functions. When "regmatch" is NULL all of then.
4620 * Otherwise functions matching "regmatch".
4621 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004622 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004623list_functions(regmatch_T *regmatch)
4624{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004625 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004626 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004627 hashitem_T *hi;
4628
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004629 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004630 {
4631 if (!HASHITEM_EMPTY(hi))
4632 {
4633 ufunc_T *fp = HI2UF(hi);
4634
4635 --todo;
4636 if ((fp->uf_flags & FC_DEAD) == 0
4637 && (regmatch == NULL
4638 ? !message_filtered(fp->uf_name)
4639 && !func_name_refcount(fp->uf_name)
4640 : !isdigit(*fp->uf_name)
4641 && vim_regexec(regmatch, fp->uf_name, 0)))
4642 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004643 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004644 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004645 if (function_list_modified(prev_ht_changed))
4646 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004647 }
4648 }
4649 }
4650}
4651
4652/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004653 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004654 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4655 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004656 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004657 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4658 * With CF_INTERFACE the function is define inside an interface, only the
4659 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004660 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004661 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004662 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004663define_function(
4664 exarg_T *eap,
4665 char_u *name_arg,
4666 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004667 int class_flags,
4668 ocmember_T *obj_members,
4669 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004670{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004671 int j;
4672 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004673 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004674 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004675 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004676 char_u *p;
4677 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004678 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004679 char_u *line_arg = NULL;
4680 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004681 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004682 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004683 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004684 garray_T newlines;
4685 int varargs = FALSE;
4686 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004687 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004688 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004689 int fp_allocated = FALSE;
4690 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004691 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004692 dictitem_T *v;
4693 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004694 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004695 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004696 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004697 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004698 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004699 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004700
4701 /*
4702 * ":function" without argument: list functions.
4703 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004704 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004705 {
4706 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004707 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004708 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004709 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004710 }
4711
4712 /*
4713 * ":function /pat": list functions matching pattern.
4714 */
4715 if (*eap->arg == '/')
4716 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004717 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004718 if (!eap->skip)
4719 {
4720 regmatch_T regmatch;
4721
4722 c = *p;
4723 *p = NUL;
4724 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4725 *p = c;
4726 if (regmatch.regprog != NULL)
4727 {
4728 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004729 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004730 vim_regfree(regmatch.regprog);
4731 }
4732 }
4733 if (*p == '/')
4734 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004735 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004736 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004737 }
4738
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004739 ga_init(&newargs);
4740 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004741 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004742 ga_init(&default_args);
4743
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004744 /*
4745 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004746 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004747 * "name" == func, "fudi.fd_dict" == NULL
4748 * dict.func new dictionary entry
4749 * "name" == NULL, "fudi.fd_dict" set,
4750 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4751 * dict.func existing dict entry with a Funcref
4752 * "name" == func, "fudi.fd_dict" set,
4753 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4754 * dict.func existing dict entry that's not a Funcref
4755 * "name" == NULL, "fudi.fd_dict" set,
4756 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4757 * s:func script-local function name
4758 * g:func global function name, same as "func"
4759 */
4760 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004761 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004762 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004763 // nested function, argument is (args).
4764 paren = TRUE;
4765 CLEAR_FIELD(fudi);
4766 }
4767 else
4768 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004769 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004770 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004771 if (p[0] == 's' && p[1] == ':')
4772 {
4773 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4774 return NULL;
4775 }
4776 p = to_name_end(p, TRUE);
4777 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4778 {
4779 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4780 eap->arg);
4781 return NULL;
4782 }
4783 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004784 }
4785
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004786 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004787 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004788 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004789 paren = (vim_strchr(p, '(') != NULL);
4790 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004791 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004792 /*
4793 * Return on an invalid expression in braces, unless the expression
4794 * evaluation has been cancelled due to an aborting error, an
4795 * interrupt, or an exception.
4796 */
4797 if (!aborting())
4798 {
4799 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004800 semsg(_(e_key_not_present_in_dictionary_str),
4801 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004802 vim_free(fudi.fd_newkey);
4803 return NULL;
4804 }
4805 else
4806 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004807 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004808
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004809 // For "export def FuncName()" in an autoload script the function name
4810 // is stored with the legacy autoload name "dir#script#FuncName" so
4811 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004812 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004813 {
4814 char_u *prefixed = may_prefix_autoload(name);
4815
4816 if (prefixed != NULL && prefixed != name)
4817 {
4818 vim_free(name);
4819 name = prefixed;
4820 }
4821 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004822 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004823 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004824 {
4825 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4826 goto ret_free;
4827 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004828 }
4829
Bram Moolenaare38eab22019-12-05 21:50:01 +01004830 // An error in a function call during evaluation of an expression in magic
4831 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004832 saved_did_emsg = did_emsg;
4833 did_emsg = FALSE;
4834
4835 /*
4836 * ":function func" with only function name: list function.
4837 */
4838 if (!paren)
4839 {
4840 if (!ends_excmd(*skipwhite(p)))
4841 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004842 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004843 goto ret_free;
4844 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004845 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004846 if (eap->nextcmd != NULL)
4847 *p = NUL;
4848 if (!eap->skip && !got_int)
4849 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004850 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004851 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4852 {
4853 char_u *up = untrans_function_name(name);
4854
4855 // With Vim9 script the name was made script-local, if not
4856 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004857 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004858 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004859 }
4860
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004861 if (fp != NULL)
4862 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004863 // Check no function was added or removed from a timer, e.g. at
4864 // the more prompt. "fp" may then be invalid.
4865 int prev_ht_changed = func_hashtab.ht_changed;
4866
4867 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004868 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004869 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4870 {
4871 if (FUNCLINE(fp, j) == NULL)
4872 continue;
4873 msg_putchar('\n');
4874 msg_outnum((long)(j + 1));
4875 if (j < 9)
4876 msg_putchar(' ');
4877 if (j < 99)
4878 msg_putchar(' ');
4879 if (function_list_modified(prev_ht_changed))
4880 break;
4881 msg_prt_line(FUNCLINE(fp, j), FALSE);
4882 out_flush(); // show a line at a time
4883 ui_breakcheck();
4884 }
4885 if (!got_int)
4886 {
4887 msg_putchar('\n');
4888 if (!function_list_modified(prev_ht_changed))
4889 {
4890 if (fp->uf_def_status != UF_NOT_COMPILED)
4891 msg_puts(" enddef");
4892 else
4893 msg_puts(" endfunction");
4894 }
4895 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004896 }
4897 }
4898 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004899 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004900 }
4901 goto ret_free;
4902 }
4903
4904 /*
4905 * ":function name(arg1, arg2)" Define function.
4906 */
4907 p = skipwhite(p);
4908 if (*p != '(')
4909 {
4910 if (!eap->skip)
4911 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004912 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004913 goto ret_free;
4914 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004915 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004916 if (vim_strchr(p, '(') != NULL)
4917 p = vim_strchr(p, '(');
4918 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004919
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004920 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4921 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004922 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004923 goto ret_free;
4924 }
4925
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004926 // In Vim9 script only global functions can be redefined.
4927 if (vim9script && eap->forceit && !is_global)
4928 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004929 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004930 goto ret_free;
4931 }
4932
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004933 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004934
Bram Moolenaar04b12692020-05-04 23:24:44 +02004935 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004936 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004937 // Check the name of the function. Unless it's a dictionary function
4938 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004939 if (name != NULL)
4940 arg = name;
4941 else
4942 arg = fudi.fd_newkey;
4943 if (arg != NULL && (fudi.fd_di == NULL
4944 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4945 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4946 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004947 char_u *name_base = arg;
4948 int i;
4949
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004950 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004951 {
4952 name_base = vim_strchr(arg, '_');
4953 if (name_base == NULL)
4954 name_base = arg + 3;
4955 else
4956 ++name_base;
4957 }
4958 for (i = 0; name_base[i] != NUL && (i == 0
4959 ? eval_isnamec1(name_base[i])
4960 : eval_isnamec(name_base[i])); ++i)
4961 ;
4962 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004963 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004964
4965 // In Vim9 script a function cannot have the same name as a
4966 // variable.
4967 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004968 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4969 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004970 + EVAL_VAR_NO_FUNC) == OK)
4971 {
4972 semsg(_(e_redefining_script_item_str), name_base);
4973 goto ret_free;
4974 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004975 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004976 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004977 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004978 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004979 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004980 goto ret_free;
4981 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004982 }
4983
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004984 // This may get more lines and make the pointers into the first line
4985 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004986 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004987 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00004988 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02004989 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004990 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00004991 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004992 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004993 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004994
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004995 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004996 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004997 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004998 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004999 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005000 if (*p != ':')
5001 {
5002 semsg(_(e_no_white_space_allowed_before_colon_str), p);
5003 p = skipwhite(p);
5004 }
5005 else if (!IS_WHITE_OR_NUL(p[1]))
5006 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005007 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02005008 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005009 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005010 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005011 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01005012 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005014 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005015 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005016 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005017 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02005018 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005019 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005020 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02005021 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005022 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005023 else
5024 // find extra arguments "range", "dict", "abort" and "closure"
5025 for (;;)
5026 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01005027 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005028 p = skipwhite(p);
5029 if (STRNCMP(p, "range", 5) == 0)
5030 {
5031 flags |= FC_RANGE;
5032 p += 5;
5033 }
5034 else if (STRNCMP(p, "dict", 4) == 0)
5035 {
5036 flags |= FC_DICT;
5037 p += 4;
5038 }
5039 else if (STRNCMP(p, "abort", 5) == 0)
5040 {
5041 flags |= FC_ABORT;
5042 p += 5;
5043 }
5044 else if (STRNCMP(p, "closure", 7) == 0)
5045 {
5046 flags |= FC_CLOSURE;
5047 p += 7;
5048 if (current_funccal == NULL)
5049 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005050 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005051 name == NULL ? (char_u *)"" : name);
5052 goto erret;
5053 }
5054 }
5055 else
5056 break;
5057 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005058
Bram Moolenaare38eab22019-12-05 21:50:01 +01005059 // When there is a line break use what follows for the function body.
5060 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005061 if (*p == '\n')
5062 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005063 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005064 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5065 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005066 && !(VIM_ISWHITE(*whitep) && *p == '#'
5067 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005068 && !eap->skip
5069 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005070 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005071
5072 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005073 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5074 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005075 */
5076 if (KeyTyped)
5077 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005078 // Check if the function already exists, don't let the user type the
5079 // whole function before telling him it doesn't work! For a script we
5080 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005081 if (!eap->skip && !eap->forceit)
5082 {
5083 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005084 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005085 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005086 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005087 }
5088
5089 if (!eap->skip && did_emsg)
5090 goto erret;
5091
Bram Moolenaare38eab22019-12-05 21:50:01 +01005092 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005093 cmdline_row = msg_row;
5094 }
5095
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005096 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005097 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005098
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005099 // Do not define the function when getting the body fails and when
5100 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005101 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005102 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005103 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5104 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005105 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005106 goto erret;
5107
5108 /*
5109 * If there are no errors, add the function
5110 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005111 if (fudi.fd_dict != NULL)
5112 {
5113 char numbuf[20];
5114
5115 fp = NULL;
5116 if (fudi.fd_newkey == NULL && !eap->forceit)
5117 {
5118 emsg(_(e_dictionary_entry_already_exists));
5119 goto erret;
5120 }
5121 if (fudi.fd_di == NULL)
5122 {
5123 // Can't add a function to a locked dictionary
5124 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5125 goto erret;
5126 }
5127 // Can't change an existing function if it is locked
5128 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5129 goto erret;
5130
5131 // Give the function a sequential number. Can only be used with a
5132 // Funcref!
5133 vim_free(name);
5134 sprintf(numbuf, "%d", ++func_nr);
5135 name = vim_strsave((char_u *)numbuf);
5136 if (name == NULL)
5137 goto erret;
5138 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005139 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005140 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005141 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005142 char_u *find_name = name;
5143 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005144 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005145
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005146 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005147 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005148 var_conflict = TRUE;
5149
5150 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5151 {
5152 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5153
5154 if (si->sn_autoload_prefix != NULL)
5155 {
5156 if (is_export)
5157 {
5158 find_name = name + STRLEN(si->sn_autoload_prefix);
5159 v = find_var(find_name, &ht, TRUE);
5160 if (v != NULL)
5161 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005162 // Only check if the function already exists in the script,
5163 // global functions can be shadowed.
5164 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005165 }
5166 else
5167 {
5168 char_u *prefixed = may_prefix_autoload(name);
5169
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005170 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005171 {
5172 v = find_var(prefixed, &ht, TRUE);
5173 if (v != NULL)
5174 var_conflict = TRUE;
5175 vim_free(prefixed);
5176 }
5177 }
5178 }
5179 }
5180 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005181 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005182 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005183 goto erret;
5184 }
5185
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005186 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005187 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005188 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005189 char_u *uname = untrans_function_name(name);
5190
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005191 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005192 }
5193
5194 if (fp != NULL || import != NULL)
5195 {
5196 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005198 // Function can be replaced with "function!" and when sourcing the
5199 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005200 // A name that is used by an import can not be overruled.
5201 if (import != NULL
5202 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005203 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005204 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005205 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005206 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005207 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005208 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005209 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005210 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005211 goto erret;
5212 }
5213 if (fp->uf_calls > 0)
5214 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005215 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005216 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005217 goto erret;
5218 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005219 if (fp->uf_refcount > 1)
5220 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005221 // This function is referenced somewhere, don't redefine it but
5222 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005223 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005224 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005225 fp = NULL;
5226 overwrite = TRUE;
5227 }
5228 else
5229 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005230 char_u *exp_name = fp->uf_name_exp;
5231
5232 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005233 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005234 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005235 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005236 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005237 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005238#ifdef FEAT_PROFILE
5239 fp->uf_profiling = FALSE;
5240 fp->uf_prof_initialized = FALSE;
5241#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005242 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005243 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005244 }
5245 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005246
5247 if (fp == NULL)
5248 {
5249 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5250 {
5251 int slen, plen;
5252 char_u *scriptname;
5253
Bram Moolenaare38eab22019-12-05 21:50:01 +01005254 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005255 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005256 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005257 {
5258 scriptname = autoload_name(name);
5259 if (scriptname != NULL)
5260 {
5261 p = vim_strchr(scriptname, '/');
5262 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005263 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005264 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005265 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005266 j = OK;
5267 vim_free(scriptname);
5268 }
5269 }
5270 if (j == FAIL)
5271 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005272 linenr_T save_lnum = SOURCING_LNUM;
5273
5274 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005275 semsg(_(e_function_name_does_not_match_script_file_name_str),
5276 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005277 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005278 goto erret;
5279 }
5280 }
5281
Bram Moolenaare01e5212023-01-08 20:31:18 +00005282 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005283 if (fp == NULL)
5284 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005285 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005286
5287 if (fudi.fd_dict != NULL)
5288 {
5289 if (fudi.fd_di == NULL)
5290 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005291 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005292 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5293 if (fudi.fd_di == NULL)
5294 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005295 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005296 goto erret;
5297 }
5298 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5299 {
5300 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005301 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005302 goto erret;
5303 }
5304 }
5305 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005306 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005307 clear_tv(&fudi.fd_di->di_tv);
5308 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005309 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005310
Bram Moolenaare38eab22019-12-05 21:50:01 +01005311 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005312 flags |= FC_DICT;
5313 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005314 }
5315 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005316 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005317 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005318 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005319
5320 if (eap->cmdidx == CMD_def)
5321 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005322 int lnum_save = SOURCING_LNUM;
5323 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005324
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005325 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005326
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005327 // error messages are for the first function line
5328 SOURCING_LNUM = sourcing_lnum_top;
5329
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005330 // The function may use script variables from the context.
5331 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005332
h-eastb895b0f2023-09-24 15:46:31 +02005333 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5334 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005335 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005336 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005337 free_fp = fp_allocated;
5338 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005339 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005340 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005341
5342 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005343 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005344 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005345 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005346 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005347 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005348 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005349 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005350 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005351 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005352 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005353
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005354 if (fp_allocated)
5355 {
5356 // insert the new function in the function list
5357 set_ufunc_name(fp, name);
5358 if (overwrite)
5359 {
5360 hi = hash_find(&func_hashtab, name);
5361 hi->hi_key = UF2HIKEY(fp);
5362 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005363 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005364 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005365 {
5366 free_fp = TRUE;
5367 goto erret;
5368 }
5369 fp->uf_refcount = 1;
5370 }
5371
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005372 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005373 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005374 if ((flags & FC_CLOSURE) != 0)
5375 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005376 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005377 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005378 }
5379 else
5380 fp->uf_scoped = NULL;
5381
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005382#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005383 if (prof_def_func())
5384 func_do_profile(fp);
5385#endif
5386 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005387 if (sandbox)
5388 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005389 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005390 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005391 fp->uf_flags = flags;
5392 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005393 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005394 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005395 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005396 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005397 if (is_export)
5398 {
5399 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005400 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005401 is_export = FALSE;
5402 }
5403
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005404 if (eap->cmdidx == CMD_def)
5405 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005406 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5407 // :func does not use Vim9 script syntax, even in a Vim9 script file
5408 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005409
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005410 goto ret_free;
5411
5412erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005413 if (fp != NULL)
5414 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005415 // these were set to "newargs" and "default_args", which are cleared
5416 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005417 ga_init(&fp->uf_args);
5418 ga_init(&fp->uf_def_args);
5419 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005420errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005421 ga_clear_strings(&newargs);
5422 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005423 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005424 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005425 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005426 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005427 VIM_CLEAR(fp->uf_va_name);
5428 clear_type_list(&fp->uf_type_list);
5429 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005430 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005431 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005432ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005433 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005434 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005435 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005436 if (name != name_arg)
5437 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005438 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005439 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005440
5441 return fp;
5442}
5443
5444/*
5445 * ":function"
5446 */
5447 void
5448ex_function(exarg_T *eap)
5449{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005450 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005451
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005452 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005453 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005454 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005455}
5456
5457/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005458 * Find a function by name, including "<lambda>123".
5459 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005460 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005461 * Return NULL if not found.
5462 */
5463 ufunc_T *
5464find_func_by_name(char_u *name, compiletype_T *compile_type)
5465{
5466 char_u *arg = name;
5467 char_u *fname;
5468 ufunc_T *ufunc;
5469 int is_global = FALSE;
5470
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005471 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5472 {
5473 *compile_type = CT_PROFILE;
5474 arg = skipwhite(arg + 7);
5475 }
5476 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5477 {
5478 *compile_type = CT_DEBUG;
5479 arg = skipwhite(arg + 5);
5480 }
5481
5482 if (STRNCMP(arg, "<lambda>", 8) == 0)
5483 {
5484 arg += 8;
5485 (void)getdigits(&arg);
5486 fname = vim_strnsave(name, arg - name);
5487 }
5488 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005489 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005490 // First try finding a method in a class, trans_function_name() will
5491 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005492 ufunc = find_class_func(&arg);
5493 if (ufunc != NULL)
5494 return ufunc;
5495
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005496 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005497 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005498 NULL, NULL, NULL, &ufunc);
5499 if (ufunc != NULL)
5500 {
5501 vim_free(fname);
5502 return ufunc;
5503 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005504 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005505 if (fname == NULL)
5506 {
5507 semsg(_(e_invalid_argument_str), name);
5508 return NULL;
5509 }
5510 if (!ends_excmd2(name, arg))
5511 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005512 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005513 emsg(ex_errmsg(e_trailing_characters_str, arg));
5514 return NULL;
5515 }
5516
5517 ufunc = find_func(fname, is_global);
5518 if (ufunc == NULL)
5519 {
5520 char_u *p = untrans_function_name(fname);
5521
5522 if (p != NULL)
5523 // Try again without making it script-local.
5524 ufunc = find_func(p, FALSE);
5525 }
5526 vim_free(fname);
5527 if (ufunc == NULL)
5528 semsg(_(e_cannot_find_function_str), name);
5529 return ufunc;
5530}
5531
5532/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005533 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005534 * be compiled or the one specified by the argument.
5535 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005536 */
5537 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005538ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005539{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005540 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005541 {
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005542 compiletype_T compile_type = CT_NONE;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005543 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005544 if (ufunc != NULL)
5545 {
5546 if (func_needs_compiling(ufunc, compile_type))
5547 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5548 else
5549 smsg(_("Function %s does not need compiling"), eap->arg);
5550 }
5551 }
5552 else
5553 {
5554 long todo = (long)func_hashtab.ht_used;
5555 int changed = func_hashtab.ht_changed;
5556 hashitem_T *hi;
5557
5558 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5559 {
5560 if (!HASHITEM_EMPTY(hi))
5561 {
5562 --todo;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005563 ufunc_T *ufunc = HI2UF(hi);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005564 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5565 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5566 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005567 {
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005568 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5569
5570 if (func_hashtab.ht_changed != changed)
5571 {
5572 // a function has been added or removed, need to start
5573 // over
5574 todo = (long)func_hashtab.ht_used;
5575 changed = func_hashtab.ht_changed;
5576 hi = func_hashtab.ht_array;
5577 --hi;
5578 }
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005579 }
5580 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005581 }
5582 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005583}
5584
5585/*
5586 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5587 * Return 2 if "p" starts with "s:".
5588 * Return 0 otherwise.
5589 */
5590 int
5591eval_fname_script(char_u *p)
5592{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005593 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5594 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005595 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5596 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5597 return 5;
5598 if (p[0] == 's' && p[1] == ':')
5599 return 2;
5600 return 0;
5601}
5602
5603 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005604translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005605{
5606 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005607 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005608 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005609}
5610
5611/*
5612 * Return TRUE when "ufunc" has old-style "..." varargs
5613 * or named varargs "...name: type".
5614 */
5615 int
5616has_varargs(ufunc_T *ufunc)
5617{
5618 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005619}
5620
5621/*
5622 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005623 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005624 */
5625 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005626function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005627{
5628 char_u *nm = name;
5629 char_u *p;
5630 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005631 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005632 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005633
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005634 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5635 if (no_deref)
5636 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005637 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005638 nm = skipwhite(nm);
5639
Bram Moolenaare38eab22019-12-05 21:50:01 +01005640 // Only accept "funcname", "funcname ", "funcname (..." and
5641 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005642 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005643 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005644 vim_free(p);
5645 return n;
5646}
5647
Bram Moolenaar113e1072019-01-20 15:30:40 +01005648#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005649 char_u *
5650get_expanded_name(char_u *name, int check)
5651{
5652 char_u *nm = name;
5653 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005654 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005655
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005656 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005657
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005658 if (p != NULL && *nm == NUL
5659 && (!check || translated_function_exists(p, is_global)))
5660 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005661
5662 vim_free(p);
5663 return NULL;
5664}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005665#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005666
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005667/*
5668 * Function given to ExpandGeneric() to obtain the list of user defined
5669 * function names.
5670 */
5671 char_u *
5672get_user_func_name(expand_T *xp, int idx)
5673{
5674 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005675 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005676 static hashitem_T *hi;
5677 ufunc_T *fp;
5678
5679 if (idx == 0)
5680 {
5681 done = 0;
5682 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005683 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005684 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005685 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005686 {
5687 if (done++ > 0)
5688 ++hi;
5689 while (HASHITEM_EMPTY(hi))
5690 ++hi;
5691 fp = HI2UF(hi);
5692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005693 // don't show dead, dict and lambda functions
5694 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005695 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005696 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005697
5698 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005699 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005700
5701 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005702 if (xp->xp_context != EXPAND_USER_FUNC
5703 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005704 {
5705 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005706 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005707 STRCAT(IObuff, ")");
5708 }
5709 return IObuff;
5710 }
5711 return NULL;
5712}
5713
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005714/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005715 * Make a copy of a function.
5716 * Intended to be used for a function defined on a base class that has a copy
5717 * on the child class.
5718 * The copy has uf_refcount set to one.
5719 * Returns NULL when out of memory.
5720 */
5721 ufunc_T *
5722copy_function(ufunc_T *fp)
5723{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005724 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005725 if (ufunc == NULL)
5726 return NULL;
5727
5728 // Most things can just be copied.
5729 *ufunc = *fp;
5730
5731 ufunc->uf_def_status = UF_TO_BE_COMPILED;
5732 ufunc->uf_dfunc_idx = 0;
5733 ufunc->uf_class = NULL;
5734
5735 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
5736 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
5737
5738 if (ufunc->uf_arg_types != NULL)
5739 {
5740 // "uf_arg_types" is an allocated array, make a copy.
5741 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
5742 if (at != NULL)
5743 {
5744 mch_memmove(at, ufunc->uf_arg_types,
5745 sizeof(type_T *) * ufunc->uf_args.ga_len);
5746 ufunc->uf_arg_types = at;
5747 }
5748 }
5749
5750 // TODO: how about the types themselves? they can be freed when the
5751 // original function is freed:
5752 // type_T **uf_arg_types;
5753 // type_T *uf_ret_type;
5754
Ernie Rael114ec812023-06-04 18:11:35 +01005755 // make uf_type_list empty
5756 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00005757
5758 // TODO: partial_T *uf_partial;
5759
5760 if (ufunc->uf_va_name != NULL)
5761 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
5762
5763 // TODO:
5764 // type_T *uf_va_type;
5765 // type_T *uf_func_type;
5766
5767 ufunc->uf_block_depth = 0;
5768 ufunc->uf_block_ids = NULL;
5769
5770 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
5771
5772 ufunc->uf_refcount = 1;
5773 ufunc->uf_name_exp = NULL;
5774 STRCPY(ufunc->uf_name, fp->uf_name);
5775
5776 return ufunc;
5777}
5778
5779/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005780 * ":delfunction {name}"
5781 */
5782 void
5783ex_delfunction(exarg_T *eap)
5784{
5785 ufunc_T *fp = NULL;
5786 char_u *p;
5787 char_u *name;
5788 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005789 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005790
5791 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005792 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
5793 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005794 vim_free(fudi.fd_newkey);
5795 if (name == NULL)
5796 {
5797 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005798 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005799 return;
5800 }
5801 if (!ends_excmd(*skipwhite(p)))
5802 {
5803 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005804 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005805 return;
5806 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005807 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005808 if (eap->nextcmd != NULL)
5809 *p = NUL;
5810
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005811 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005812 {
5813 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005814 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005815 vim_free(name);
5816 return;
5817 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005818 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005819 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005820 vim_free(name);
5821
5822 if (!eap->skip)
5823 {
5824 if (fp == NULL)
5825 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005826 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005827 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005828 return;
5829 }
5830 if (fp->uf_calls > 0)
5831 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005832 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005833 return;
5834 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005835 if (fp->uf_flags & FC_VIM9)
5836 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005837 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005838 return;
5839 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005840
5841 if (fudi.fd_dict != NULL)
5842 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005843 // Delete the dict item that refers to the function, it will
5844 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00005845 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005846 }
5847 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005848 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005849 // A normal function (not a numbered function or lambda) has a
5850 // refcount of 1 for the entry in the hashtable. When deleting
5851 // it and the refcount is more than one, it should be kept.
5852 // A numbered function and lambda should be kept if the refcount is
5853 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005854 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005855 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005856 // Function is still referenced somewhere. Don't free it but
5857 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005858 if (func_remove(fp))
5859 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005860 }
5861 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005862 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005863 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005864 }
5865}
5866
5867/*
5868 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005869 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005870 */
5871 void
5872func_unref(char_u *name)
5873{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005874 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005875
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005876 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005877 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005878 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005879 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005880 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005881#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005882 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005883#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005884 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005885 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005886 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005887}
5888
5889/*
5890 * Unreference a Function: decrement the reference count and free it when it
5891 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005892 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005893 */
5894 void
5895func_ptr_unref(ufunc_T *fp)
5896{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005897 if (fp != NULL && (--fp->uf_refcount <= 0
5898 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5899 && fp->uf_partial->pt_refcount <= 1
5900 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005901 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005902 // Only delete it when it's not being used. Otherwise it's done
5903 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005904 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005905 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005906 }
5907}
5908
5909/*
5910 * Count a reference to a Function.
5911 */
5912 void
5913func_ref(char_u *name)
5914{
5915 ufunc_T *fp;
5916
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005917 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005918 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005919 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005920 if (fp != NULL)
5921 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005922 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005923 // Only give an error for a numbered function.
5924 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005925 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005926}
5927
5928/*
5929 * Count a reference to a Function.
5930 */
5931 void
5932func_ptr_ref(ufunc_T *fp)
5933{
5934 if (fp != NULL)
5935 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005936}
5937
5938/*
5939 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5940 * referenced from anywhere that is in use.
5941 */
5942 static int
5943can_free_funccal(funccall_T *fc, int copyID)
5944{
Bram Moolenaarca16c602022-09-06 18:57:08 +01005945 return (fc->fc_l_varlist.lv_copyID != copyID
5946 && fc->fc_l_vars.dv_copyID != copyID
5947 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005948 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005949}
5950
5951/*
5952 * ":return [expr]"
5953 */
5954 void
5955ex_return(exarg_T *eap)
5956{
5957 char_u *arg = eap->arg;
5958 typval_T rettv;
5959 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005960 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005961
5962 if (current_funccal == NULL)
5963 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005964 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005965 return;
5966 }
5967
Bram Moolenaar844fb642021-10-23 13:32:30 +01005968 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005969 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5970
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005971 if (eap->skip)
5972 ++emsg_skip;
5973
5974 eap->nextcmd = NULL;
5975 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005976 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005977 {
5978 if (!eap->skip)
5979 returning = do_return(eap, FALSE, TRUE, &rettv);
5980 else
5981 clear_tv(&rettv);
5982 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005983 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005984 else if (!eap->skip)
5985 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005986 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005987 update_force_abort();
5988
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005989 /*
5990 * Return unless the expression evaluation has been cancelled due to an
5991 * aborting error, an interrupt, or an exception.
5992 */
5993 if (!aborting())
5994 returning = do_return(eap, FALSE, TRUE, NULL);
5995 }
5996
Bram Moolenaare38eab22019-12-05 21:50:01 +01005997 // When skipping or the return gets pending, advance to the next command
5998 // in this line (!returning). Otherwise, ignore the rest of the line.
5999 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006000 if (returning)
6001 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006002 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02006003 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006004
6005 if (eap->skip)
6006 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02006007 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006008}
6009
zeertzjq5299c092023-04-12 20:48:16 +01006010/*
6011 * Lower level implementation of "call". Only called when not skipping.
6012 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006013 static int
6014ex_call_inner(
6015 exarg_T *eap,
6016 char_u *name,
6017 char_u **arg,
6018 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006019 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006020 evalarg_T *evalarg)
6021{
6022 linenr_T lnum;
6023 int doesrange;
6024 typval_T rettv;
6025 int failed = FALSE;
6026
zeertzjq5299c092023-04-12 20:48:16 +01006027 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006028 for ( ; lnum <= eap->line2; ++lnum)
6029 {
6030 funcexe_T funcexe;
6031
zeertzjq5299c092023-04-12 20:48:16 +01006032 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006033 {
6034 if (lnum > curbuf->b_ml.ml_line_count)
6035 {
6036 // If the function deleted lines or switched to another buffer
6037 // the line number may become invalid.
6038 emsg(_(e_invalid_range));
6039 break;
6040 }
6041 curwin->w_cursor.lnum = lnum;
6042 curwin->w_cursor.col = 0;
6043 curwin->w_cursor.coladd = 0;
6044 }
6045 *arg = startarg;
6046
6047 funcexe = *funcexe_init;
6048 funcexe.fe_doesrange = &doesrange;
6049 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6050 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6051 {
6052 failed = TRUE;
6053 break;
6054 }
6055 if (has_watchexpr())
6056 dbg_check_breakpoint(eap);
6057
6058 // Handle a function returning a Funcref, Dictionary or List.
6059 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006060 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006061 {
6062 failed = TRUE;
6063 break;
6064 }
6065
6066 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006067 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006068 break;
6069
6070 // Stop when immediately aborting on error, or when an interrupt
6071 // occurred or an exception was thrown but not caught.
6072 // get_func_tv() returned OK, so that the check for trailing
6073 // characters below is executed.
6074 if (aborting())
6075 break;
6076 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006077 return failed;
6078}
6079
6080/*
6081 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6082 * Returns FAIL or OK.
6083 */
6084 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006085ex_defer_inner(
6086 char_u *name,
6087 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006088 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006089 partial_T *partial,
6090 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006091{
6092 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006093 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006094 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006095 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006096
6097 if (current_funccal == NULL)
6098 {
6099 semsg(_(e_str_not_inside_function), "defer");
6100 return FAIL;
6101 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006102 if (partial != NULL)
6103 {
6104 if (partial->pt_dict != NULL)
6105 {
6106 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6107 return FAIL;
6108 }
6109 if (partial->pt_argc > 0)
6110 {
6111 int i;
6112
6113 partial_argc = partial->pt_argc;
6114 for (i = 0; i < partial_argc; ++i)
6115 copy_tv(&partial->pt_argv[i], &argvars[i]);
6116 }
6117 }
6118 r = get_func_arguments(arg, evalarg, FALSE,
6119 argvars + partial_argc, &argcount);
6120 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006121
6122 if (r == OK)
6123 {
6124 if (type != NULL)
6125 {
6126 // Check that the arguments are OK for the types of the funcref.
6127 r = check_argument_types(type, argvars, argcount, NULL, name);
6128 }
6129 else if (builtin_function(name, -1))
6130 {
6131 int idx = find_internal_func(name);
6132
6133 if (idx < 0)
6134 {
6135 emsg_funcname(e_unknown_function_str, name);
6136 r = FAIL;
6137 }
6138 else if (check_internal_func(idx, argcount) == -1)
6139 r = FAIL;
6140 }
6141 else
6142 {
6143 ufunc_T *ufunc = find_func(name, FALSE);
6144
6145 // we tolerate an unknown function here, it might be defined later
6146 if (ufunc != NULL)
6147 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006148 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006149 if (error != FCERR_UNKNOWN)
6150 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006151 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006152 r = FAIL;
6153 }
6154 }
6155 }
6156 }
6157
Bram Moolenaar86d87252022-09-05 21:21:25 +01006158 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006159 {
6160 while (--argcount >= 0)
6161 clear_tv(&argvars[argcount]);
6162 return FAIL;
6163 }
6164 return add_defer(name, argcount, argvars);
6165}
6166
6167/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006168 * Return TRUE if currently inside a function call.
6169 * Give an error message and return FALSE when not.
6170 */
6171 int
6172can_add_defer(void)
6173{
6174 if (!in_def_function() && get_current_funccal() == NULL)
6175 {
6176 semsg(_(e_str_not_inside_function), "defer");
6177 return FALSE;
6178 }
6179 return TRUE;
6180}
6181
6182/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006183 * Add a deferred call for "name" with arguments "argvars[argcount]".
6184 * Consumes "argvars[]".
6185 * Caller must check that in_def_function() returns TRUE or current_funccal is
6186 * not NULL.
6187 * Returns OK or FAIL.
6188 */
6189 int
6190add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6191{
6192 char_u *saved_name = vim_strsave(name);
6193 int argcount = argcount_arg;
6194 defer_T *dr;
6195 int ret = FAIL;
6196
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006197 if (saved_name == NULL)
6198 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006199 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006200 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006201 if (add_defer_function(saved_name, argcount, argvars) == OK)
6202 argcount = 0;
6203 }
6204 else
6205 {
6206 if (current_funccal->fc_defer.ga_itemsize == 0)
6207 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6208 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6209 goto theend;
6210 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6211 + current_funccal->fc_defer.ga_len++;
6212 dr->dr_name = saved_name;
6213 dr->dr_argcount = argcount;
6214 while (argcount > 0)
6215 {
6216 --argcount;
6217 dr->dr_argvars[argcount] = argvars[argcount];
6218 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006219 }
6220 ret = OK;
6221
6222theend:
6223 while (--argcount >= 0)
6224 clear_tv(&argvars[argcount]);
6225 return ret;
6226}
6227
6228/*
6229 * Invoked after a functions has finished: invoke ":defer" functions.
6230 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006231 static void
6232handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006233{
6234 int idx;
6235
Bram Moolenaar58779852022-09-06 18:31:14 +01006236 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006237 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006238 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006239
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006240 if (dr->dr_name == NULL)
6241 // already being called, can happen if function does ":qa"
6242 continue;
6243
6244 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006245 CLEAR_FIELD(funcexe);
6246 funcexe.fe_evaluate = TRUE;
6247
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006248 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006249 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006250
6251 char_u *name = dr->dr_name;
6252 dr->dr_name = NULL;
6253
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006254 // If the deferred function is called after an exception, then only the
6255 // first statement in the function will be executed. Save and restore
6256 // the try/catch/throw exception state.
6257 int save_trylevel = trylevel;
6258 int save_did_throw = did_throw;
6259 int save_need_rethrow = need_rethrow;
6260
6261 trylevel = 0;
6262 did_throw = FALSE;
6263 need_rethrow = FALSE;
6264
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006265 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6266
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006267 trylevel = save_trylevel;
6268 did_throw = save_did_throw;
6269 need_rethrow = save_need_rethrow;
6270
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006271 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006272 vim_free(name);
6273 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006274 clear_tv(&dr->dr_argvars[i]);
6275 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006276 ga_clear(&funccal->fc_defer);
6277}
6278
zeertzjq960cf912023-04-18 21:52:54 +01006279 static void
6280invoke_funccall_defer(funccall_T *fc)
6281{
6282 if (fc->fc_ectx != NULL)
6283 {
6284 // :def function
6285 unwind_def_callstack(fc->fc_ectx);
6286 may_invoke_defer_funcs(fc->fc_ectx);
6287 }
6288 else
6289 {
6290 // legacy function
6291 handle_defer_one(fc);
6292 }
6293}
6294
Bram Moolenaar58779852022-09-06 18:31:14 +01006295/*
6296 * Called when exiting: call all defer functions.
6297 */
6298 void
6299invoke_all_defer(void)
6300{
zeertzjq1be4b812023-04-19 14:21:24 +01006301 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6302 invoke_funccall_defer(fc);
6303
zeertzjq960cf912023-04-18 21:52:54 +01006304 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6305 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6306 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006307}
6308
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006309/*
6310 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006311 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006312 */
6313 void
6314ex_call(exarg_T *eap)
6315{
6316 char_u *arg = eap->arg;
6317 char_u *startarg;
6318 char_u *name;
6319 char_u *tofree;
6320 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006321 int failed = FALSE;
6322 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006323 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006324 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006325 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006326 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006327 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006328 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006329
Bram Moolenaare6b53242020-07-01 17:28:33 +02006330 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006331 if (eap->skip)
6332 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006333 typval_T rettv;
6334
Bram Moolenaare38eab22019-12-05 21:50:01 +01006335 // trans_function_name() doesn't work well when skipping, use eval0()
6336 // instead to skip to any following command, e.g. for:
6337 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006338 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006339 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006340 clear_tv(&rettv);
6341 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006342 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006343 return;
6344 }
6345
zeertzjq5299c092023-04-12 20:48:16 +01006346 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006347 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006348 if (fudi.fd_newkey != NULL)
6349 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006350 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006351 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006352 vim_free(fudi.fd_newkey);
6353 }
6354 if (tofree == NULL)
6355 return;
6356
Bram Moolenaare38eab22019-12-05 21:50:01 +01006357 // Increase refcount on dictionary, it could get deleted when evaluating
6358 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006359 if (fudi.fd_dict != NULL)
6360 ++fudi.fd_dict->dv_refcount;
6361
Bram Moolenaare38eab22019-12-05 21:50:01 +01006362 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6363 // contents. For VAR_PARTIAL get its partial, unless we already have one
6364 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006365 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006366 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006367 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006368 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006369
Bram Moolenaare38eab22019-12-05 21:50:01 +01006370 // Skip white space to allow ":call func ()". Not good, but required for
6371 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006372 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006373 if (*startarg != '(')
6374 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006375 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006376 goto end;
6377 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006378 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006379 {
6380 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6381 goto end;
6382 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006383
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006384 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006385 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006386 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006387 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006388 }
6389 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006390 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006391 funcexe_T funcexe;
6392
Bram Moolenaara80faa82020-04-12 19:37:17 +02006393 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006394 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006395 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006396 funcexe.fe_partial = partial;
6397 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006398 funcexe.fe_firstline = eap->line1;
6399 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006400 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006401 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006402 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006403 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006404
Bram Moolenaar1d341892021-09-18 15:25:52 +02006405 // When inside :try we need to check for following "| catch" or "| endtry".
6406 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006407 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006408 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006409 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006410 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006411 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006412 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006413 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006414 {
6415 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006416 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006417 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006418 }
6419 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006420 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006421 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006422 // Must be after using "arg", it may point into memory cleared here.
6423 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006424
6425end:
6426 dict_unref(fudi.fd_dict);
6427 vim_free(tofree);
6428}
6429
6430/*
6431 * Return from a function. Possibly makes the return pending. Also called
6432 * for a pending return at the ":endtry" or after returning from an extra
6433 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6434 * when called due to a ":return" command. "rettv" may point to a typval_T
6435 * with the return rettv. Returns TRUE when the return can be carried out,
6436 * FALSE when the return gets pending.
6437 */
6438 int
6439do_return(
6440 exarg_T *eap,
6441 int reanimate,
6442 int is_cmd,
6443 void *rettv)
6444{
6445 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006446 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006447
6448 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006449 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006450 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006451
6452 /*
6453 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6454 * not in its finally clause (which then is to be executed next) is found.
6455 * In this case, make the ":return" pending for execution at the ":endtry".
6456 * Otherwise, return normally.
6457 */
6458 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6459 if (idx >= 0)
6460 {
6461 cstack->cs_pending[idx] = CSTP_RETURN;
6462
6463 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006464 // A pending return again gets pending. "rettv" points to an
6465 // allocated variable with the rettv of the original ":return"'s
6466 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006467 cstack->cs_rettv[idx] = rettv;
6468 else
6469 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006470 // When undoing a return in order to make it pending, get the stored
6471 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006472 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006473 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006474
6475 if (rettv != NULL)
6476 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006477 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006478 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6479 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6480 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006481 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006482 }
6483 else
6484 cstack->cs_rettv[idx] = NULL;
6485
6486 if (reanimate)
6487 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006488 // The pending return value could be overwritten by a ":return"
6489 // without argument in a finally clause; reset the default
6490 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006491 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6492 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006493 }
6494 }
6495 report_make_pending(CSTP_RETURN, rettv);
6496 }
6497 else
6498 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006499 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006500
Bram Moolenaare38eab22019-12-05 21:50:01 +01006501 // If the return is carried out now, store the return value. For
6502 // a return immediately after reanimation, the value is already
6503 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006504 if (!reanimate && rettv != NULL)
6505 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006506 clear_tv(current_funccal->fc_rettv);
6507 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006508 if (!is_cmd)
6509 vim_free(rettv);
6510 }
6511 }
6512
6513 return idx < 0;
6514}
6515
6516/*
6517 * Free the variable with a pending return value.
6518 */
6519 void
6520discard_pending_return(void *rettv)
6521{
6522 free_tv((typval_T *)rettv);
6523}
6524
6525/*
6526 * Generate a return command for producing the value of "rettv". The result
6527 * is an allocated string. Used by report_pending() for verbose messages.
6528 */
6529 char_u *
6530get_return_cmd(void *rettv)
6531{
6532 char_u *s = NULL;
6533 char_u *tofree = NULL;
6534 char_u numbuf[NUMBUFLEN];
6535
6536 if (rettv != NULL)
6537 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6538 if (s == NULL)
6539 s = (char_u *)"";
6540
6541 STRCPY(IObuff, ":return ");
6542 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6543 if (STRLEN(s) + 8 >= IOSIZE)
6544 STRCPY(IObuff + IOSIZE - 4, "...");
6545 vim_free(tofree);
6546 return vim_strsave(IObuff);
6547}
6548
6549/*
6550 * Get next function line.
6551 * Called by do_cmdline() to get the next line.
6552 * Returns allocated string, or NULL for end of function.
6553 */
6554 char_u *
6555get_func_line(
6556 int c UNUSED,
6557 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006558 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006559 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006560{
6561 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006562 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006563 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006564 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006565
Bram Moolenaare38eab22019-12-05 21:50:01 +01006566 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006567 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006568 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006569 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006570 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006571 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006572 }
6573#ifdef FEAT_PROFILE
6574 if (do_profiling == PROF_YES)
6575 func_line_end(cookie);
6576#endif
6577
6578 gap = &fp->uf_lines;
6579 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006580 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006581 retval = NULL;
6582 else
6583 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006584 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006585 while (fcp->fc_linenr < gap->ga_len
6586 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6587 ++fcp->fc_linenr;
6588 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006589 retval = NULL;
6590 else
6591 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006592 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6593 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006594#ifdef FEAT_PROFILE
6595 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006596 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006597#endif
6598 }
6599 }
6600
Bram Moolenaare38eab22019-12-05 21:50:01 +01006601 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006602 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006603 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006604 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006605 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006606 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006607 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006608 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006609 }
6610
6611 return retval;
6612}
6613
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006614/*
6615 * Return TRUE if the currently active function should be ended, because a
6616 * return was encountered or an error occurred. Used inside a ":while".
6617 */
6618 int
6619func_has_ended(void *cookie)
6620{
6621 funccall_T *fcp = (funccall_T *)cookie;
6622
Bram Moolenaare38eab22019-12-05 21:50:01 +01006623 // Ignore the "abort" flag if the abortion behavior has been changed due to
6624 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006625 return (((fcp->fc_func->uf_flags & FC_ABORT)
6626 && did_emsg && !aborted_in_try())
6627 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006628}
6629
6630/*
6631 * return TRUE if cookie indicates a function which "abort"s on errors.
6632 */
6633 int
6634func_has_abort(
6635 void *cookie)
6636{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006637 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006638}
6639
6640
6641/*
6642 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6643 * Don't do this when "Func" is already a partial that was bound
6644 * explicitly (pt_auto is FALSE).
6645 * Changes "rettv" in-place.
6646 * Returns the updated "selfdict_in".
6647 */
6648 dict_T *
6649make_partial(dict_T *selfdict_in, typval_T *rettv)
6650{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006651 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006652 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006653 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006654 dict_T *selfdict = selfdict_in;
6655
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006656 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6657 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006658 fp = rettv->vval.v_partial->pt_func;
6659 else
6660 {
6661 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006662 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006663 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006664 if (fname == NULL)
6665 {
6666 // There is no point binding a dict to a NULL function, just create
6667 // a function reference.
6668 rettv->v_type = VAR_FUNC;
6669 rettv->vval.v_string = NULL;
6670 }
6671 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006672 {
6673 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006674 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006675
6676 // Translate "s:func" to the stored function name.
6677 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6678 fp = find_func(fname, FALSE);
6679 vim_free(tofree);
6680 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006681 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006682
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006683 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006684 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006685 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006686
6687 if (pt != NULL)
6688 {
6689 pt->pt_refcount = 1;
6690 pt->pt_dict = selfdict;
6691 pt->pt_auto = TRUE;
6692 selfdict = NULL;
6693 if (rettv->v_type == VAR_FUNC)
6694 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006695 // Just a function: Take over the function name and use
6696 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006697 pt->pt_name = rettv->vval.v_string;
6698 }
6699 else
6700 {
6701 partial_T *ret_pt = rettv->vval.v_partial;
6702 int i;
6703
Bram Moolenaare38eab22019-12-05 21:50:01 +01006704 // Partial: copy the function name, use selfdict and copy
6705 // args. Can't take over name or args, the partial might
6706 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006707 if (ret_pt->pt_name != NULL)
6708 {
6709 pt->pt_name = vim_strsave(ret_pt->pt_name);
6710 func_ref(pt->pt_name);
6711 }
6712 else
6713 {
6714 pt->pt_func = ret_pt->pt_func;
6715 func_ptr_ref(pt->pt_func);
6716 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006717 if (ret_pt->pt_argc > 0)
6718 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006719 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006720 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006721 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006722 pt->pt_argc = 0;
6723 else
6724 {
6725 pt->pt_argc = ret_pt->pt_argc;
6726 for (i = 0; i < pt->pt_argc; i++)
6727 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6728 }
6729 }
6730 partial_unref(ret_pt);
6731 }
6732 rettv->v_type = VAR_PARTIAL;
6733 rettv->vval.v_partial = pt;
6734 }
6735 }
6736 return selfdict;
6737}
6738
6739/*
6740 * Return the name of the executed function.
6741 */
6742 char_u *
6743func_name(void *cookie)
6744{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006745 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006746}
6747
6748/*
6749 * Return the address holding the next breakpoint line for a funccall cookie.
6750 */
6751 linenr_T *
6752func_breakpoint(void *cookie)
6753{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006754 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006755}
6756
6757/*
6758 * Return the address holding the debug tick for a funccall cookie.
6759 */
6760 int *
6761func_dbg_tick(void *cookie)
6762{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006763 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006764}
6765
6766/*
6767 * Return the nesting level for a funccall cookie.
6768 */
6769 int
6770func_level(void *cookie)
6771{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006772 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006773}
6774
6775/*
6776 * Return TRUE when a function was ended by a ":return" command.
6777 */
6778 int
6779current_func_returned(void)
6780{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006781 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006782}
6783
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006784 int
6785free_unref_funccal(int copyID, int testing)
6786{
6787 int did_free = FALSE;
6788 int did_free_funccal = FALSE;
6789 funccall_T *fc, **pfc;
6790
6791 for (pfc = &previous_funccal; *pfc != NULL; )
6792 {
6793 if (can_free_funccal(*pfc, copyID))
6794 {
6795 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006796 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006797 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006798 did_free = TRUE;
6799 did_free_funccal = TRUE;
6800 }
6801 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006802 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006803 }
6804 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006805 // When a funccal was freed some more items might be garbage
6806 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006807 (void)garbage_collect(testing);
6808
6809 return did_free;
6810}
6811
6812/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006813 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006814 */
6815 static funccall_T *
6816get_funccal(void)
6817{
6818 int i;
6819 funccall_T *funccal;
6820 funccall_T *temp_funccal;
6821
6822 funccal = current_funccal;
6823 if (debug_backtrace_level > 0)
6824 {
6825 for (i = 0; i < debug_backtrace_level; i++)
6826 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006827 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006828 if (temp_funccal)
6829 funccal = temp_funccal;
6830 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006831 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006832 debug_backtrace_level = i;
6833 }
6834 }
6835 return funccal;
6836}
6837
6838/*
6839 * Return the hashtable used for local variables in the current funccal.
6840 * Return NULL if there is no current funccal.
6841 */
6842 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006843get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006844{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006845 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006846 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006847 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006848}
6849
6850/*
6851 * Return the l: scope variable.
6852 * Return NULL if there is no current funccal.
6853 */
6854 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006855get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006856{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006857 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006858 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006859 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006860}
6861
6862/*
6863 * Return the hashtable used for argument in the current funccal.
6864 * Return NULL if there is no current funccal.
6865 */
6866 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006867get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006868{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006869 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006870 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006871 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006872}
6873
6874/*
6875 * Return the a: scope variable.
6876 * Return NULL if there is no current funccal.
6877 */
6878 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006879get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006880{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006881 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006882 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006883 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006884}
6885
6886/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006887 * List function variables, if there is a function.
6888 */
6889 void
6890list_func_vars(int *first)
6891{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006892 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
6893 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006894 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006895}
6896
6897/*
6898 * If "ht" is the hashtable for local variables in the current funccal, return
6899 * the dict that contains it.
6900 * Otherwise return NULL.
6901 */
6902 dict_T *
6903get_current_funccal_dict(hashtab_T *ht)
6904{
6905 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01006906 && ht == &current_funccal->fc_l_vars.dv_hashtab)
6907 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006908 return NULL;
6909}
6910
6911/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006912 * Search hashitem in parent scope.
6913 */
6914 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006915find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006916{
6917 funccall_T *old_current_funccal = current_funccal;
6918 hashtab_T *ht;
6919 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006920 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006921
Bram Moolenaarca16c602022-09-06 18:57:08 +01006922 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006923 return NULL;
6924
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006925 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006926 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006927 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006928 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006929 ht = find_var_ht(name, &varname);
6930 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006931 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006932 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006933 if (!HASHITEM_EMPTY(hi))
6934 {
6935 *pht = ht;
6936 break;
6937 }
6938 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006939 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02006940 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006941 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006942 }
6943 current_funccal = old_current_funccal;
6944
6945 return hi;
6946}
6947
6948/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006949 * Search variable in parent scope.
6950 */
6951 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006952find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006953{
6954 dictitem_T *v = NULL;
6955 funccall_T *old_current_funccal = current_funccal;
6956 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006957 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006958
Bram Moolenaarca16c602022-09-06 18:57:08 +01006959 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006960 return NULL;
6961
Bram Moolenaare38eab22019-12-05 21:50:01 +01006962 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01006963 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006964 while (current_funccal)
6965 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006966 ht = find_var_ht(name, &varname);
6967 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006968 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006969 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006970 if (v != NULL)
6971 break;
6972 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006973 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006974 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006975 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006976 }
6977 current_funccal = old_current_funccal;
6978
6979 return v;
6980}
6981
6982/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006983 * Set "copyID + 1" in previous_funccal and callers.
6984 */
6985 int
6986set_ref_in_previous_funccal(int copyID)
6987{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006988 funccall_T *fc;
6989
Bram Moolenaarca16c602022-09-06 18:57:08 +01006990 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006991 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006992 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006993 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
6994 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
6995 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006996 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006997 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006998 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006999}
7000
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007001 static int
7002set_ref_in_funccal(funccall_T *fc, int copyID)
7003{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007004 if (fc->fc_copyID != copyID)
7005 {
7006 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007007 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
7008 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
7009 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
7010 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007011 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007012 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007013 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007014}
7015
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007016/*
7017 * Set "copyID" in all local vars and arguments in the call stack.
7018 */
7019 int
7020set_ref_in_call_stack(int copyID)
7021{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007022 funccall_T *fc;
7023 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007024
Bram Moolenaarca16c602022-09-06 18:57:08 +01007025 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007026 if (set_ref_in_funccal(fc, copyID))
7027 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007028
7029 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007030 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007031 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007032 if (set_ref_in_funccal(fc, copyID))
7033 return TRUE;
7034 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007035}
7036
7037/*
7038 * Set "copyID" in all functions available by name.
7039 */
7040 int
7041set_ref_in_functions(int copyID)
7042{
7043 int todo;
7044 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007045 ufunc_T *fp;
7046
7047 todo = (int)func_hashtab.ht_used;
7048 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007049 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007050 if (!HASHITEM_EMPTY(hi))
7051 {
7052 --todo;
7053 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007054 if (!func_name_refcount(fp->uf_name)
7055 && set_ref_in_func(NULL, fp, copyID))
7056 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007057 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007058 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007059 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007060}
7061
7062/*
7063 * Set "copyID" in all function arguments.
7064 */
7065 int
7066set_ref_in_func_args(int copyID)
7067{
7068 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007069
7070 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007071 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7072 copyID, NULL, NULL))
7073 return TRUE;
7074 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007075}
7076
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007077/*
7078 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007079 * Returns TRUE if setting references failed somehow.
7080 */
7081 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007082set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007083{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007084 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007085 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007086 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007087 char_u fname_buf[FLEN_FIXED + 1];
7088 char_u *tofree = NULL;
7089 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007090 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007091
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007092 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007093 return FALSE;
7094
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007095 if (fp_in == NULL)
7096 {
7097 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007098 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007099 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007100 if (fp != NULL)
7101 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007102 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007103 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007104 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007105
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007106 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007107 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007108}
7109
Bram Moolenaare38eab22019-12-05 21:50:01 +01007110#endif // FEAT_EVAL