blob: 1bd1a284599e6b5c97faa8718ec8a16dff4ea7cf [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;
Keith Thompson184f71c2024-01-04 21:19:04 +010080 if (arg == p || SAFE_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
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100198 if (eap->ea_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +0000199 theline = getcmdline(':', 0L, indent, 0);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000200 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100201 theline = eap->ea_getline(':', eap->cookie, indent, getline_options);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000202 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;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100243 int need_expr = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200244
245 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000246 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100247 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000248 ga_init2(argtypes, sizeof(char_u *), 3);
h-eastb895b0f2023-09-24 15:46:31 +0200249 if (arg_objm != NULL)
250 ga_init2(arg_objm, sizeof(int8_T), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200251 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000252 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200253
254 if (varargs != NULL)
255 *varargs = FALSE;
256
257 /*
258 * Isolate the arguments: "arg1, arg2, ...)"
259 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100260 arg = skipwhite(*argp);
261 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200262 while (*p != endchar)
263 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100264 while (eap != NULL && eap->ea_getline != NULL
Bram Moolenaar2c330432020-04-13 14:41:35 +0200265 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200266 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200267 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000268 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000269 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000270
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200271 if (theline == NULL)
272 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200273 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200274 p = skipwhite(theline);
275 }
276
277 if (mustend && *p != endchar)
278 {
279 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000280 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100281 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200282 }
Christian Brabandte4a450a2023-12-08 20:57:38 +0100283 if (*p == endchar && !need_expr)
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200284 break;
285
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200286 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
287 {
288 if (varargs != NULL)
289 *varargs = TRUE;
290 p += 3;
291 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100292
293 if (argtypes != NULL)
294 {
295 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200296 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100298 if (!skip)
299 emsg(_(e_missing_name_after_dots));
300 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100301 }
302
303 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100304 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200305 arg_objm, evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100306 if (p == arg)
307 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100308 if (*skipwhite(p) == '=')
309 {
310 emsg(_(e_cannot_use_default_for_variable_arguments));
311 break;
312 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100313 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200314 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000315 else if (in_class && STRNCMP(p, "this.", 5) == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000316 {
317 // this.memberName
318 p += 5;
319 arg = p;
320 while (ASCII_ISALNUM(*p) || *p == '_')
321 ++p;
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000322 char_u *argend = p;
323
h-eastdb385522023-09-28 22:18:19 +0200324 // object variable this. can be used only in a constructor
325 if (STRNCMP(eap->arg, "new", 3) != 0)
326 {
327 c = *argend;
328 *argend = NUL;
329 semsg(_(e_cannot_use_an_object_variable_except_with_the_new_method_str), arg);
330 *argend = c;
331 break;
332 }
333
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000334 if (*skipwhite(p) == '=')
335 {
336 char_u *defval = skipwhite(skipwhite(p) + 1);
337 if (STRNCMP(defval, "v:none", 6) != 0)
338 {
339 semsg(_(e_constructor_default_value_must_be_vnone_str), p);
340 goto err_ret;
341 }
342 any_default = TRUE;
343 p = defval + 6;
344
345 if (ga_grow(default_args, 1) == FAIL)
346 goto err_ret;
347
348 char_u *expr = vim_strsave((char_u *)"v:none");
349 if (expr == NULL)
350 goto err_ret;
351 ((char_u **)(default_args->ga_data))
352 [default_args->ga_len] = expr;
353 default_args->ga_len++;
354 }
355 else if (any_default)
356 {
357 emsg(_(e_non_default_argument_follows_default_argument));
358 goto err_ret;
359 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000360
361 // TODO: check the argument is indeed a member
362 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
363 return FAIL;
364 if (newargs != NULL)
365 {
366 ((char_u **)(newargs->ga_data))[newargs->ga_len] =
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000367 vim_strnsave(arg, argend - arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000368 newargs->ga_len++;
369
h-eastb895b0f2023-09-24 15:46:31 +0200370 if (argtypes != NULL && ga_grow(argtypes, 1) == OK
371 && arg_objm != NULL && ga_grow(arg_objm, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000372 {
373 // TODO: use the actual type
374 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] =
375 vim_strsave((char_u *)"any");
h-eastb895b0f2023-09-24 15:46:31 +0200376 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000377
378 // Add a line to the function body for the assignment.
379 if (ga_grow(newlines, 1) == OK)
380 {
381 // "this.name = name"
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000382 int len = 5 + (argend - arg) + 3 + (argend - arg) + 1;
383 if (any_default)
384 len += 14 + 10;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000385 char_u *assignment = alloc(len);
386 if (assignment != NULL)
387 {
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000388 c = *argend;
389 *argend = NUL;
390 if (any_default)
391 vim_snprintf((char *)assignment, len,
392 "ifargisset %d this.%s = %s",
393 default_args->ga_len - 1, arg, arg);
394 else
395 vim_snprintf((char *)assignment, len,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000396 "this.%s = %s", arg, arg);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000397 *argend = c;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000398 ((char_u **)(newlines->ga_data))[
399 newlines->ga_len++] = assignment;
400 }
401 }
402 }
403 }
404 if (*p == ',')
405 ++p;
406 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200407 else
408 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200409 char_u *np;
410
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200411 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100412 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200413 arg_objm, evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200415 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200416
Bram Moolenaar015cf102021-06-26 21:52:02 +0200417 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200418 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200419 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200420 if (*np == '=' && np[1] != '=' && np[1] != '~'
421 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200422 {
423 typval_T rettv;
424
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100425 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200426 any_default = TRUE;
Bram Moolenaareb5adf12022-09-04 13:41:37 +0100427 p = skipwhite(np + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000428 char_u *expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200429 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200430 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200431 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200432 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200433 if (ga_grow(default_args, 1) == FAIL)
434 goto err_ret;
435
Christian Brabandte4a450a2023-12-08 20:57:38 +0100436 if (need_expr)
437 need_expr = FALSE;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200438 // trim trailing whitespace
439 while (p > expr && VIM_ISWHITE(p[-1]))
440 p--;
441 c = *p;
442 *p = NUL;
443 expr = vim_strsave(expr);
444 if (expr == NULL)
445 {
446 *p = c;
447 goto err_ret;
448 }
449 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200450 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200451 default_args->ga_len++;
452 *p = c;
453 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200454 }
455 else
Christian Brabandte4a450a2023-12-08 20:57:38 +0100456 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200457 mustend = TRUE;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100458 if (*skipwhite(p) == NUL)
459 need_expr = TRUE;
460 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200461 }
462 else if (any_default)
463 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000464 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200465 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200466 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200467
468 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
469 {
470 // Be tolerant when skipping
471 if (!skip)
472 {
473 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
474 goto err_ret;
475 }
476 p = skipwhite(p);
477 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200478 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200479 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200480 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200481 // Don't give this error when skipping, it makes the "->" not
482 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100483 // Allow missing space after comma in legacy functions.
484 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200485 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
486 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100487 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200488 goto err_ret;
489 }
490 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200491 else
492 mustend = TRUE;
493 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200494 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200495 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200496 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200497
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200498 if (*p != endchar)
499 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100500 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200501
502 *argp = p;
503 return OK;
504
505err_ret:
506 if (newargs != NULL)
507 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200508 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200509 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200510 return FAIL;
511}
512
513/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100514 * Parse the argument types, filling "fp->uf_arg_types".
515 * Return OK or FAIL.
516 */
517 static int
h-eastb895b0f2023-09-24 15:46:31 +0200518parse_argument_types(
519 ufunc_T *fp,
520 garray_T *argtypes,
521 int varargs,
522 garray_T *arg_objm,
523 ocmember_T *obj_members,
524 int obj_member_count)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100525{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200526 int len = 0;
527
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100528 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
529 if (argtypes->ga_len > 0)
530 {
531 // When "varargs" is set the last name/type goes into uf_va_name
532 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200533 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100534
535 if (len > 0)
536 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
537 if (fp->uf_arg_types != NULL)
538 {
539 int i;
540 type_T *type;
541
542 for (i = 0; i < len; ++ i)
543 {
544 char_u *p = ((char_u **)argtypes->ga_data)[i];
545
546 if (p == NULL)
547 // will get the type from the default value
548 type = &t_unknown;
549 else
h-eastb895b0f2023-09-24 15:46:31 +0200550 {
551 if (arg_objm != NULL && ((int8_T *)arg_objm->ga_data)[i])
552 {
553 char_u *aname = ((char_u **)fp->uf_args.ga_data)[i];
554
555 type = &t_any;
556 for (int om = 0; om < obj_member_count; ++om)
557 {
Christian Brabandt915f3bf2024-04-05 20:12:19 +0200558 if (obj_members != NULL
559 && STRCMP(aname,
560 obj_members[om].ocm_name) == 0)
h-eastb895b0f2023-09-24 15:46:31 +0200561 {
562 type = obj_members[om].ocm_type;
563 break;
564 }
565 }
566 }
567 else
568 type = parse_type(&p, &fp->uf_type_list, TRUE);
569 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100570 if (type == NULL)
571 return FAIL;
572 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000573 if (i < fp->uf_args.ga_len
574 && (type->tt_type == VAR_FUNC
Yegappan Lakshmanan3e336502024-04-04 19:35:59 +0200575 || type->tt_type == VAR_PARTIAL))
576 {
577 char_u *name = ((char_u **)fp->uf_args.ga_data)[i];
578 if (obj_members != NULL && *name == '_')
579 // protected object method
580 name++;
581
582 if (var_wrong_func_name(name, TRUE))
583 return FAIL;
584 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100585 }
586 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100587 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200588
589 if (varargs)
590 {
591 char_u *p;
592
593 // Move the last argument "...name: type" to uf_va_name and
594 // uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200595 --fp->uf_args.ga_len;
Bram Moolenaarf0571712023-01-04 13:16:20 +0000596 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len];
597 ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len] = NULL;
Bram Moolenaar2a389082021-04-09 20:24:31 +0200598 p = ((char_u **)argtypes->ga_data)[len];
599 if (p == NULL)
600 // TODO: get type from default value
601 fp->uf_va_type = &t_list_any;
602 else
603 {
604 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
605 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
606 {
607 semsg(_(e_variable_arguments_type_must_be_list_str),
608 ((char_u **)argtypes->ga_data)[len]);
609 return FAIL;
610 }
611 }
612 if (fp->uf_va_type == NULL)
613 return FAIL;
614 }
615
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100616 return OK;
617}
618
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100619 static int
620parse_return_type(ufunc_T *fp, char_u *ret_type)
621{
622 if (ret_type == NULL)
623 fp->uf_ret_type = &t_void;
624 else
625 {
626 char_u *p = ret_type;
627
628 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
629 if (fp->uf_ret_type == NULL)
630 {
631 fp->uf_ret_type = &t_void;
632 return FAIL;
633 }
634 }
635 return OK;
636}
637
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100638/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200639 * Register function "fp" as using "current_funccal" as its scope.
640 */
641 static int
642register_closure(ufunc_T *fp)
643{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200644 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100645 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200646 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200647 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200648 fp->uf_scoped = current_funccal;
649 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200650
Bram Moolenaarca16c602022-09-06 18:57:08 +0100651 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200652 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100653 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
654 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200655 return OK;
656}
657
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100658 static void
659set_ufunc_name(ufunc_T *fp, char_u *name)
660{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100661 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
662 // actually extends beyond the struct.
663 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100664
665 if (name[0] == K_SPECIAL)
666 {
667 fp->uf_name_exp = alloc(STRLEN(name) + 3);
668 if (fp->uf_name_exp != NULL)
669 {
670 STRCPY(fp->uf_name_exp, "<SNR>");
671 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
672 }
673 }
674}
675
Bram Moolenaar58016442016-07-31 18:30:22 +0200676/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100677 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
678 * return "buf" filled with a readable function name.
679 * Otherwise just return "name", thus the return value can always be used.
680 * "name" and "buf" may be equal.
681 */
682 char_u *
683make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
684{
685 size_t len;
686
687 if (name[0] != K_SPECIAL)
688 return name;
689 len = STRLEN(name);
690 if (len + 3 > bufsize)
691 return name;
692
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100693 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100694 mch_memmove(buf, "<SNR>", 5);
695 return buf;
696}
697
698/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200699 * Get a name for a lambda. Returned in static memory.
700 */
701 char_u *
702get_lambda_name(void)
703{
704 static char_u name[30];
705 static int lambda_no = 0;
706
707 sprintf((char*)name, "<lambda>%d", ++lambda_no);
708 return name;
709}
710
Bram Moolenaare01e5212023-01-08 20:31:18 +0000711/*
712 * Allocate a "ufunc_T" for a function called "name".
713 * Makes sure the size is right.
714 */
715 static ufunc_T *
716alloc_ufunc(char_u *name)
717{
718 // When the name is short we need to make sure we allocate enough bytes for
719 // the whole struct, including any padding.
720 size_t len = offsetof(ufunc_T, uf_name) + STRLEN(name) + 1;
721 return alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
722}
723
Bram Moolenaar801ab062020-06-25 19:27:56 +0200724#if defined(FEAT_LUA) || defined(PROTO)
725/*
726 * Registers a native C callback which can be called from Vim script.
727 * Returns the name of the Vim script function.
728 */
729 char_u *
730register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
731{
732 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200733 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200734
Bram Moolenaare01e5212023-01-08 20:31:18 +0000735 fp = alloc_ufunc(name);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200736 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200737 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200738
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200739 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200740 fp->uf_refcount = 1;
741 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000742 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200743 fp->uf_calls = 0;
744 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200745 fp->uf_cb = cb;
746 fp->uf_cb_free = cb_free;
747 fp->uf_cb_state = state;
748
749 set_ufunc_name(fp, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000750 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200751
752 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200753}
754#endif
755
Bram Moolenaar04b12692020-05-04 23:24:44 +0200756/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100757 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100758 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100759 * If "white_error" is not NULL check for correct use of white space and set
760 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100761 * Return NULL if no valid arrow found.
762 */
763 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100764skip_arrow(
765 char_u *start,
766 int equal_arrow,
767 char_u **ret_type,
768 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100769{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100770 char_u *s = start;
771 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100772
773 if (equal_arrow)
774 {
775 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100776 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100777 if (white_error != NULL && !VIM_ISWHITE(s[1]))
778 {
779 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100780 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100781 return NULL;
782 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100783 s = skipwhite(s + 1);
784 *ret_type = s;
785 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100786 if (s == *ret_type)
787 {
788 emsg(_(e_missing_return_type));
789 return NULL;
790 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100791 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100792 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100793 s = skipwhite(s);
794 if (*s != '=')
795 return NULL;
796 ++s;
797 }
798 if (*s != '>')
799 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100800 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
801 || !IS_WHITE_OR_NUL(s[1])))
802 {
803 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100804 semsg(_(e_white_space_required_before_and_after_str_at_str),
805 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100806 return NULL;
807 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100808 return skipwhite(s + 1);
809}
810
811/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100812 * Check if "*cmd" points to a function command and if so advance "*cmd" and
813 * return TRUE.
814 * Otherwise return FALSE;
815 * Do not consider "function(" to be a command.
816 */
817 static int
818is_function_cmd(char_u **cmd)
819{
820 char_u *p = *cmd;
821
822 if (checkforcmd(&p, "function", 2))
823 {
824 if (*p == '(')
825 return FALSE;
826 *cmd = p;
827 return TRUE;
828 }
829 return FALSE;
830}
831
832/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200833 * Called when defining a function: The context may be needed for script
834 * variables declared in a block that is visible now but not when the function
835 * is compiled or called later.
836 */
837 static void
838function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
839{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000840 if (cstack == NULL || cstack->cs_idx < 0)
841 return;
842
843 int count = cstack->cs_idx + 1;
844 int i;
845
846 fp->uf_block_ids = ALLOC_MULT(int, count);
847 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200848 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000849 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
850 sizeof(int) * count);
851 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200852 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000853
854 // Set flag in each block to indicate a function was defined. This
855 // is used to keep the variable when leaving the block, see
856 // hide_script_var().
857 for (i = 0; i <= cstack->cs_idx; ++i)
858 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200859}
860
861/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100862 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200863 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100864 * "newlines" must already have been initialized.
865 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
866 */
867 static int
868get_function_body(
869 exarg_T *eap,
870 garray_T *newlines,
871 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000872 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100873{
874 linenr_T sourcing_lnum_top = SOURCING_LNUM;
875 linenr_T sourcing_lnum_off;
876 int saved_wait_return = need_wait_return;
877 char_u *line_arg = line_arg_in;
878 int vim9_function = eap->cmdidx == CMD_def
879 || eap->cmdidx == CMD_block;
880#define MAX_FUNC_NESTING 50
881 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200882 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100883 int nesting = 0;
884 getline_opt_T getline_options;
885 int indent = 2;
886 char_u *skip_until = NULL;
887 int ret = FAIL;
888 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200889 int heredoc_concat_len = 0;
890 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100891 char_u *heredoc_trimmed = NULL;
892
Bram Moolenaar20677332021-06-06 17:02:53 +0200893 ga_init2(&heredoc_ga, 1, 500);
894
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100895 // Detect having skipped over comment lines to find the return
896 // type. Add NULL lines to keep the line count correct.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100897 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100898 if (SOURCING_LNUM < sourcing_lnum_off)
899 {
900 sourcing_lnum_off -= SOURCING_LNUM;
901 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
902 goto theend;
903 while (sourcing_lnum_off-- > 0)
904 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
905 }
906
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200907 nesting_def[0] = vim9_function;
908 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100909 getline_options = vim9_function
910 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
911 for (;;)
912 {
913 char_u *theline;
914 char_u *p;
915 char_u *arg;
916
917 if (KeyTyped)
918 {
919 msg_scroll = TRUE;
920 saved_wait_return = FALSE;
921 }
922 need_wait_return = FALSE;
923
924 if (line_arg != NULL)
925 {
926 // Use eap->arg, split up in parts by line breaks.
927 theline = line_arg;
928 p = vim_strchr(theline, '\n');
929 if (p == NULL)
930 line_arg += STRLEN(line_arg);
931 else
932 {
933 *p = NUL;
934 line_arg = p + 1;
935 }
936 }
937 else
938 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000939 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100940 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100941 }
942 if (KeyTyped)
943 lines_left = Rows - 1;
944 if (theline == NULL)
945 {
946 // Use the start of the function for the line number.
947 SOURCING_LNUM = sourcing_lnum_top;
948 if (skip_until != NULL)
949 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200950 else if (nesting_inline[nesting])
951 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100952 else if (eap->cmdidx == CMD_def)
953 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100954 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000955 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100956 goto theend;
957 }
958
959 // Detect line continuation: SOURCING_LNUM increased more than one.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100960 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100961 if (SOURCING_LNUM < sourcing_lnum_off)
962 sourcing_lnum_off -= SOURCING_LNUM;
963 else
964 sourcing_lnum_off = 0;
965
966 if (skip_until != NULL)
967 {
968 // Don't check for ":endfunc"/":enddef" between
969 // * ":append" and "."
970 // * ":python <<EOF" and "EOF"
971 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
972 if (heredoc_trimmed == NULL
973 || (is_heredoc && skipwhite(theline) == theline)
974 || STRNCMP(theline, heredoc_trimmed,
975 STRLEN(heredoc_trimmed)) == 0)
976 {
977 if (heredoc_trimmed == NULL)
978 p = theline;
979 else if (is_heredoc)
980 p = skipwhite(theline) == theline
981 ? theline : theline + STRLEN(heredoc_trimmed);
982 else
983 p = theline + STRLEN(heredoc_trimmed);
984 if (STRCMP(p, skip_until) == 0)
985 {
986 VIM_CLEAR(skip_until);
987 VIM_CLEAR(heredoc_trimmed);
988 getline_options = vim9_function
989 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
990 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200991
992 if (heredoc_concat_len > 0)
993 {
994 // Replace the starting line with all the concatenated
995 // lines.
996 ga_concat(&heredoc_ga, theline);
997 vim_free(((char_u **)(newlines->ga_data))[
998 heredoc_concat_len - 1]);
999 ((char_u **)(newlines->ga_data))[
1000 heredoc_concat_len - 1] = heredoc_ga.ga_data;
1001 ga_init(&heredoc_ga);
1002 heredoc_concat_len = 0;
1003 theline += STRLEN(theline); // skip the "EOF"
1004 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001005 }
1006 }
1007 }
1008 else
1009 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001010 int c;
1011 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +00001012 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001013
1014 // skip ':' and blanks
1015 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
1016 ;
1017
1018 // Check for "endfunction", "enddef" or "}".
1019 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +00001020 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001021 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001022 ? *p == '}'
1023 : (checkforcmd(&p, nesting_def[nesting]
1024 ? "enddef" : "endfunction", 4)
1025 && *p != ':'))
1026 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00001027 if (!nesting_inline[nesting] && nesting_def[nesting]
1028 && p < cmd + 6)
1029 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001030 if (nesting-- == 0)
1031 {
1032 char_u *nextcmd = NULL;
1033
1034 if (*p == '|' || *p == '}')
1035 nextcmd = p + 1;
1036 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
1037 nextcmd = line_arg;
1038 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001039 && (vim9_function || p_verbose > 0))
1040 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +02001041 SOURCING_LNUM = sourcing_lnum_top
1042 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001043 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +00001044 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001045 else
1046 give_warning2((char_u *)
1047 _("W22: Text found after :endfunction: %s"),
1048 p, TRUE);
1049 }
1050 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001051 {
1052 // Another command follows. If the line came from "eap"
1053 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001054 // change "eap->cmdlinep" to point to the last fetched
1055 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001056 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001057 if (lines_to_free->ga_len > 0
1058 && *eap->cmdlinep !=
1059 ((char_u **)lines_to_free->ga_data)
1060 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001061 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001062 // *cmdlinep will be freed later, thus remove the
1063 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001064 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001065 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
1066 [lines_to_free->ga_len - 1];
1067 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001068 }
1069 }
1070 break;
1071 }
1072 }
1073
1074 // Check for mismatched "endfunc" or "enddef".
1075 // We don't check for "def" inside "func" thus we also can't check
1076 // for "enddef".
1077 // We continue to find the end of the function, although we might
1078 // not find it.
1079 else if (nesting_def[nesting])
1080 {
1081 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1082 emsg(_(e_mismatched_endfunction));
1083 }
1084 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1085 emsg(_(e_mismatched_enddef));
1086
1087 // Increase indent inside "if", "while", "for" and "try", decrease
1088 // at "end".
1089 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1090 indent -= 2;
1091 else if (STRNCMP(p, "if", 2) == 0
1092 || STRNCMP(p, "wh", 2) == 0
1093 || STRNCMP(p, "for", 3) == 0
1094 || STRNCMP(p, "try", 3) == 0)
1095 indent += 2;
1096
1097 // Check for defining a function inside this function.
1098 // Only recognize "def" inside "def", not inside "function",
1099 // For backwards compatibility, see Test_function_python().
1100 c = *p;
1101 if (is_function_cmd(&p)
1102 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1103 {
1104 if (*p == '!')
1105 p = skipwhite(p + 1);
1106 p += eval_fname_script(p);
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001107 vim_free(trans_function_name(&p, NULL, TRUE, 0));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001108 if (*skipwhite(p) == '(')
1109 {
1110 if (nesting == MAX_FUNC_NESTING - 1)
1111 emsg(_(e_function_nesting_too_deep));
1112 else
1113 {
1114 ++nesting;
1115 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001116 nesting_inline[nesting] = FALSE;
1117 indent += 2;
1118 }
1119 }
1120 }
1121
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001122 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001123 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001124 // Not a comment line: check for nested inline function.
1125 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001126 while (end > p && VIM_ISWHITE(*end))
1127 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001128 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001129 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001130 int is_block;
1131
1132 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001133 --end;
1134 while (end > p && VIM_ISWHITE(*end))
1135 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001136 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1137 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001138 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001139 char_u *s = p;
1140
1141 // check for line starting with "au" for :autocmd or
1142 // "com" for :command, these can use a {} block
1143 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1144 || checkforcmd_noparen(&s, "command", 3);
1145 }
1146
1147 if (is_block)
1148 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001149 if (nesting == MAX_FUNC_NESTING - 1)
1150 emsg(_(e_function_nesting_too_deep));
1151 else
1152 {
1153 ++nesting;
1154 nesting_def[nesting] = TRUE;
1155 nesting_inline[nesting] = TRUE;
1156 indent += 2;
1157 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001158 }
1159 }
1160 }
1161
1162 // Check for ":append", ":change", ":insert". Not for :def.
1163 p = skip_range(p, FALSE, NULL);
1164 if (!vim9_function
1165 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1166 || (p[0] == 'c'
1167 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1168 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1169 && (STRNCMP(&p[3], "nge", 3) != 0
1170 || !ASCII_ISALPHA(p[6])))))))
1171 || (p[0] == 'i'
1172 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1173 && (!ASCII_ISALPHA(p[2])
1174 || (p[2] == 's'
1175 && (!ASCII_ISALPHA(p[3])
1176 || p[3] == 'e'))))))))
1177 skip_until = vim_strsave((char_u *)".");
1178
1179 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1180 arg = skipwhite(skiptowhite(p));
1181 if (arg[0] == '<' && arg[1] =='<'
1182 && ((p[0] == 'p' && p[1] == 'y'
1183 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1184 || ((p[2] == '3' || p[2] == 'x')
1185 && !ASCII_ISALPHA(p[3]))))
1186 || (p[0] == 'p' && p[1] == 'e'
1187 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1188 || (p[0] == 't' && p[1] == 'c'
1189 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1190 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1191 && !ASCII_ISALPHA(p[3]))
1192 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1193 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1194 || (p[0] == 'm' && p[1] == 'z'
1195 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1196 ))
1197 {
1198 // ":python <<" continues until a dot, like ":append"
1199 p = skipwhite(arg + 2);
1200 if (STRNCMP(p, "trim", 4) == 0)
1201 {
1202 // Ignore leading white space.
1203 p = skipwhite(p + 4);
1204 heredoc_trimmed = vim_strnsave(theline,
1205 skipwhite(theline) - theline);
1206 }
1207 if (*p == NUL)
1208 skip_until = vim_strsave((char_u *)".");
1209 else
1210 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1211 getline_options = GETLINE_NONE;
1212 is_heredoc = TRUE;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001213 if (vim9_function && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001214 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001215 }
1216
Bram Moolenaard881d152022-05-13 13:50:36 +01001217 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001218 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001219 // Check for ":cmd v =<< [trim] EOF"
1220 // and ":cmd [a, b] =<< [trim] EOF"
1221 // and "lines =<< [trim] EOF" for Vim9
1222 // Where "cmd" can be "let", "var", "final" or "const".
zeertzjqa93d9cd2023-05-02 16:25:47 +01001223 arg = p;
1224 if (checkforcmd(&arg, "let", 2)
1225 || checkforcmd(&arg, "var", 3)
1226 || checkforcmd(&arg, "final", 5)
1227 || checkforcmd(&arg, "const", 5)
1228 || vim9_function)
Bram Moolenaard881d152022-05-13 13:50:36 +01001229 {
zeertzjqa93d9cd2023-05-02 16:25:47 +01001230 while (vim_strchr((char_u *)"$@&", *arg) != NULL)
1231 ++arg;
1232 arg = skipwhite(find_name_end(arg, NULL, NULL,
1233 FNE_INCL_BR | FNE_ALLOW_CURLY));
1234 if (vim9_function && *arg == ':')
1235 arg = skipwhite(skip_type(skipwhite(arg + 1), FALSE));
1236 if (arg[0] == '=' && arg[1] == '<' && arg[2] =='<')
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001237 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001238 p = skipwhite(arg + 3);
1239 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001240 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001241 if (STRNCMP(p, "trim", 4) == 0)
1242 {
1243 // Ignore leading white space.
1244 p = skipwhite(p + 4);
1245 heredoc_trimmed = vim_strnsave(theline,
1246 skipwhite(theline) - theline);
1247 continue;
1248 }
1249 if (STRNCMP(p, "eval", 4) == 0)
1250 {
1251 // Ignore leading white space.
1252 p = skipwhite(p + 4);
1253 continue;
1254 }
1255 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001256 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001257 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1258 getline_options = GETLINE_NONE;
1259 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001260 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001261 }
1262 }
1263 }
1264
1265 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001266 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001267 goto theend;
1268
Bram Moolenaar20677332021-06-06 17:02:53 +02001269 if (heredoc_concat_len > 0)
1270 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001271 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001272 // to be used for the instruction later.
1273 ga_concat(&heredoc_ga, theline);
1274 ga_concat(&heredoc_ga, (char_u *)"\n");
1275 p = vim_strsave((char_u *)"");
1276 }
1277 else
1278 {
1279 // Copy the line to newly allocated memory. get_one_sourceline()
1280 // allocates 250 bytes per line, this saves 80% on average. The
1281 // cost is an extra alloc/free.
1282 p = vim_strsave(theline);
1283 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001284 if (p == NULL)
1285 goto theend;
1286 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1287
1288 // Add NULL lines for continuation lines, so that the line count is
1289 // equal to the index in the growarray.
1290 while (sourcing_lnum_off-- > 0)
1291 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1292
1293 // Check for end of eap->arg.
1294 if (line_arg != NULL && *line_arg == NUL)
1295 line_arg = NULL;
1296 }
1297
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001298 // Return OK when no error was detected.
1299 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001300 ret = OK;
1301
1302theend:
1303 vim_free(skip_until);
1304 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001305 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001306 need_wait_return |= saved_wait_return;
1307 return ret;
1308}
1309
1310/*
1311 * Handle the body of a lambda. *arg points to the "{", process statements
1312 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001313 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001314 * When successful "rettv" is set to a funcref.
1315 */
1316 static int
1317lambda_function_body(
1318 char_u **arg,
1319 typval_T *rettv,
1320 evalarg_T *evalarg,
1321 garray_T *newargs,
1322 garray_T *argtypes,
1323 int varargs,
1324 garray_T *default_args,
1325 char_u *ret_type)
1326{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001327 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001328 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001329 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001330 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001331 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001332 exarg_T eap;
1333 garray_T newlines;
1334 char_u *cmdline = NULL;
1335 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001336 partial_T *pt;
1337 char_u *name;
1338 int lnum_save = -1;
1339 linenr_T sourcing_lnum_top = SOURCING_LNUM;
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001340 char_u *line_arg = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001341
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001342 *arg = skipwhite(*arg + 1);
1343 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001344 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001345 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001346 return FAIL;
1347 }
1348
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001349 // When there is a line break use what follows for the lambda body.
1350 // Makes lambda body initializers work for object and enum member
1351 // variables.
1352 if (**arg == '\n')
1353 line_arg = *arg + 1;
1354
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001355 CLEAR_FIELD(eap);
1356 eap.cmdidx = CMD_block;
1357 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001358 eap.cmdlinep = &cmdline;
1359 eap.skip = !evaluate;
1360 if (evalarg->eval_cctx != NULL)
1361 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1362 else
1363 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001364 eap.ea_getline = evalarg->eval_getline;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001365 eap.cookie = evalarg->eval_cookie;
1366 }
1367
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001368 ga_init2(&newlines, sizeof(char_u *), 10);
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001369 if (get_function_body(&eap, &newlines, line_arg,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001370 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001371 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001372
1373 // When inside a lambda must add the function lines to evalarg.eval_ga.
1374 evalarg->eval_break_count += newlines.ga_len;
1375 if (gap->ga_itemsize > 0)
1376 {
1377 int idx;
1378 char_u *last;
1379 size_t plen;
1380 char_u *pnl;
1381
1382 for (idx = 0; idx < newlines.ga_len; ++idx)
1383 {
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001384 char_u *p = ((char_u **)newlines.ga_data)[idx];
1385 if (p == NULL)
1386 // comment line in the lambda body
1387 continue;
1388
1389 p = skipwhite(p);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001390
Bram Moolenaarecb66452021-05-18 15:09:18 +02001391 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001392 goto erret;
1393
1394 // Going to concatenate the lines after parsing. For an empty or
1395 // comment line use an empty string.
1396 // Insert NL characters at the start of each line, the string will
1397 // be split again later in .get_lambda_tv().
1398 if (*p == NUL || vim9_comment_start(p))
1399 p = (char_u *)"";
1400 plen = STRLEN(p);
1401 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1402 if (pnl != NULL)
1403 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001404 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1405 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001406 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001407 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001408 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001409 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001410 // more is following after the "}", which was skipped
1411 last = cmdline;
1412 else
1413 // nothing is following the "}"
1414 last = (char_u *)"}";
1415 plen = STRLEN(last);
1416 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1417 if (pnl != NULL)
1418 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001419 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1420 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001421 }
1422
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001423 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001424 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001425 garray_T *tfgap = &evalarg->eval_tofree_ga;
1426
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001427 // Something comes after the "}".
1428 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001429
1430 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001431 if (ga_grow(tfgap, 1) == OK)
1432 {
1433 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1434 evalarg->eval_using_cmdline = TRUE;
1435 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001436 }
1437 else
1438 *arg = (char_u *)"";
1439
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001440 if (!evaluate)
1441 {
1442 ret = OK;
1443 goto erret;
1444 }
1445
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001446 name = get_lambda_name();
Bram Moolenaare01e5212023-01-08 20:31:18 +00001447 ufunc = alloc_ufunc(name);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001448 if (ufunc == NULL)
1449 goto erret;
1450 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001451 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001452 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001453 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001454 ufunc->uf_refcount = 1;
1455 ufunc->uf_args = *newargs;
1456 newargs->ga_data = NULL;
1457 ufunc->uf_def_args = *default_args;
1458 default_args->ga_data = NULL;
1459 ufunc->uf_func_type = &t_func_any;
1460
1461 // error messages are for the first function line
1462 lnum_save = SOURCING_LNUM;
1463 SOURCING_LNUM = sourcing_lnum_top;
1464
1465 // parse argument types
h-eastb895b0f2023-09-24 15:46:31 +02001466 if (parse_argument_types(ufunc, argtypes, varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001467 {
1468 SOURCING_LNUM = lnum_save;
1469 goto erret;
1470 }
1471
1472 // parse the return type, if any
1473 if (parse_return_type(ufunc, ret_type) == FAIL)
1474 goto erret;
1475
1476 pt = ALLOC_CLEAR_ONE(partial_T);
1477 if (pt == NULL)
1478 goto erret;
1479 pt->pt_func = ufunc;
1480 pt->pt_refcount = 1;
1481
1482 ufunc->uf_lines = newlines;
1483 newlines.ga_data = NULL;
1484 if (sandbox)
1485 ufunc->uf_flags |= FC_SANDBOX;
1486 if (!ASCII_ISUPPER(*ufunc->uf_name))
1487 ufunc->uf_flags |= FC_VIM9;
1488 ufunc->uf_script_ctx = current_sctx;
1489 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1490 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1491 set_function_type(ufunc);
1492
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001493 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1494
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001495 rettv->vval.v_partial = pt;
1496 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001497 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001498 ret = OK;
1499
1500erret:
1501 if (lnum_save >= 0)
1502 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001503 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001504 if (newargs != NULL)
1505 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001506 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001507 if (ufunc != NULL)
1508 {
1509 func_clear(ufunc, TRUE);
1510 func_free(ufunc, TRUE);
1511 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001512 return ret;
1513}
1514
1515/*
1516 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001517 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001518 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001519 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1520 */
1521 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001522get_lambda_tv(
1523 char_u **arg,
1524 typval_T *rettv,
1525 int types_optional,
1526 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001527{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001528 int evaluate = evalarg != NULL
1529 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001530 garray_T newargs;
1531 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001532 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001533 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001534 garray_T default_args;
h-eastb895b0f2023-09-24 15:46:31 +02001535 garray_T arg_objm;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001536 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001537 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001538 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001539 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001540 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001541 char_u *s;
1542 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001543 int *old_eval_lavars = eval_lavars_used;
1544 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001545 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001546 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001547 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001548 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001549 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001550 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001551
Bram Moolenaar4525a572022-02-13 11:57:33 +00001552 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001553 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001554
1555 ga_init(&newargs);
1556 ga_init(&newlines);
1557
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001558 // First, check if this is really a lambda expression. "->" or "=>" must
1559 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001560 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001561 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001562 types_optional ? &argtypes : NULL, types_optional,
1563 types_optional ? &arg_objm : NULL, evalarg,
1564 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001565 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001566 {
1567 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001568 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001569 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001570 ga_clear(&arg_objm);
1571 }
Bram Moolenaar0346b792021-01-31 22:18:29 +01001572 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001573 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001574
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001575 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001576 if (evaluate)
1577 pnewargs = &newargs;
1578 else
1579 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001580 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001581 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
h-eastb895b0f2023-09-24 15:46:31 +02001582 types_optional ? &argtypes : NULL, types_optional,
1583 types_optional ? &arg_objm : NULL, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001584 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001585 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001586 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001587 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001588 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001589 {
1590 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001591 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001592 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001593 ga_clear(&arg_objm);
1594 }
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001595 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001596 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001597 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001598 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001599
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001600 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1601 if (ret_type != NULL)
1602 {
1603 ret_type = vim_strsave(ret_type);
1604 tofree2 = ret_type;
1605 }
1606
Bram Moolenaare38eab22019-12-05 21:50:01 +01001607 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001608 if (evaluate)
1609 eval_lavars_used = &eval_lavars;
1610
Bram Moolenaar65c44152020-12-24 15:14:01 +01001611 *arg = skipwhite_and_linebreak(*arg, evalarg);
1612
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001613 // Recognize "{" as the start of a function body.
1614 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001615 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001616 if (evalarg == NULL)
1617 // cannot happen?
1618 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001619 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001620 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1621 types_optional ? &argtypes : NULL, varargs,
1622 &default_args, ret_type) == FAIL)
1623 goto errret;
1624 goto theend;
1625 }
1626 if (default_args.ga_len > 0)
1627 {
1628 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001629 goto errret;
1630 }
1631
Bram Moolenaare38eab22019-12-05 21:50:01 +01001632 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001633 start = *arg;
1634 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001635 if (ret == FAIL)
1636 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001637
Bram Moolenaar65c44152020-12-24 15:14:01 +01001638 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001639 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001640 *arg = skipwhite_and_linebreak(*arg, evalarg);
1641 if (**arg != '}')
1642 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001643 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001644 goto errret;
1645 }
1646 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001647 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001648
1649 if (evaluate)
1650 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001651 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001652 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001653 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001654 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001655 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001656
Bram Moolenaare01e5212023-01-08 20:31:18 +00001657 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001658 if (fp == NULL)
1659 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001660 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001661 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001662 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001663 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001664
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001665 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001666 if (ga_grow(&newlines, 1) == FAIL)
1667 goto errret;
1668
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001669 // If there are line breaks, we need to split up the string.
1670 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001671 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001672 line_end = end;
1673
1674 // Add "return " before the expression (or the first line).
1675 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001676 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001677 if (p == NULL)
1678 goto errret;
1679 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1680 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001681 vim_strncpy(p + 7, start, line_end - start);
1682
1683 if (line_end != end)
1684 {
1685 // Add more lines, split by line breaks. Thus is used when a
1686 // lambda with { cmds } is encountered.
1687 while (*line_end == '\n')
1688 {
1689 if (ga_grow(&newlines, 1) == FAIL)
1690 goto errret;
1691 start = line_end + 1;
1692 line_end = vim_strchr(start, '\n');
1693 if (line_end == NULL)
1694 line_end = end;
1695 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1696 vim_strnsave(start, line_end - start);
1697 }
1698 }
1699
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001700 if (strstr((char *)p + 7, "a:") == NULL)
1701 // No a: variables are used for sure.
1702 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001703
1704 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001705 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001706 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001707 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001708 if (types_optional)
1709 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001710 if (parse_argument_types(fp, &argtypes,
h-eastb895b0f2023-09-24 15:46:31 +02001711 vim9script && varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001712 goto errret;
1713 if (ret_type != NULL)
1714 {
1715 fp->uf_ret_type = parse_type(&ret_type,
1716 &fp->uf_type_list, TRUE);
1717 if (fp->uf_ret_type == NULL)
1718 goto errret;
1719 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001720 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001721 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001722 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001723
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001724 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001725 if (current_funccal != NULL && eval_lavars)
1726 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001727 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001728 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001729 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001730 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001731
1732#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001733 if (prof_def_func())
1734 func_do_profile(fp);
1735#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001736 if (sandbox)
1737 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001738 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001739 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001740 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001741 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001742 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001743 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001744 // Use the line number of the arguments.
1745 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001746
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001747 function_using_block_scopes(fp, evalarg->eval_cstack);
1748
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001749 pt->pt_func = fp;
1750 pt->pt_refcount = 1;
1751 rettv->vval.v_partial = pt;
1752 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001753
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001754 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001755 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001756
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001757theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001758 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001759 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001760 if (types_optional)
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001761 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001762 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001763 ga_clear(&arg_objm);
1764 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001765
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001766 return OK;
1767
1768errret:
1769 ga_clear_strings(&newargs);
1770 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001771 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001772 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001773 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001774 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001775 ga_clear(&arg_objm);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001776 if (fp != NULL)
1777 vim_free(fp->uf_arg_types);
1778 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001779 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001780 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001781 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001782 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001783 return FAIL;
1784}
1785
1786/*
1787 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1788 * name it contains, otherwise return "name".
1789 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1790 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001791 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1792 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001793 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001794 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001795 */
1796 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001797deref_func_name(
1798 char_u *name,
1799 int *lenp,
1800 partial_T **partialp,
1801 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001802 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001803 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001804 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001805{
1806 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001807 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001808 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001809 char_u *s = NULL;
1810 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001811 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001812
1813 if (partialp != NULL)
1814 *partialp = NULL;
1815
1816 cc = name[*lenp];
1817 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001818
Bram Moolenaar71f21932022-01-07 18:20:55 +00001819 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001820 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001821 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001822 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001823 tv = &v->di_tv;
1824 }
1825 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1826 {
1827 imported_T *import;
1828 char_u *p = name;
1829 int len = *lenp;
1830
1831 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001832 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001833 p = name + 2;
1834 len -= 2;
1835 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001836 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001837
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001838 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001839 if (import != NULL)
1840 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001841 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001842 if (new_function)
1843 semsg(_(e_redefining_imported_item_str), name);
1844 else
1845 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001846 name[len] = cc;
1847 *lenp = 0;
1848 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001849 }
1850 }
1851
1852 if (tv != NULL)
1853 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001854 if (found_var != NULL)
1855 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001856 if (tv->v_type == VAR_FUNC)
1857 {
1858 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001859 {
1860 *lenp = 0;
1861 return (char_u *)""; // just in case
1862 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001863 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001864 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001865 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001866
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001867 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001868 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001869 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001870
1871 if (pt == NULL)
1872 {
1873 *lenp = 0;
1874 return (char_u *)""; // just in case
1875 }
1876 if (partialp != NULL)
1877 *partialp = pt;
1878 s = partial_name(pt);
1879 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001880 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001881
1882 if (s != NULL)
1883 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001884 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001885 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001886 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001887
1888 if (sv != NULL)
1889 *type = sv->sv_type;
1890 }
1891 return s;
1892 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001893 }
1894
1895 return name;
1896}
1897
1898/*
1899 * Give an error message with a function name. Handle <SNR> things.
1900 * "ermsg" is to be passed without translation, use N_() instead of _().
1901 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001902 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001903emsg_funcname(char *ermsg, char_u *name)
1904{
Bram Moolenaar54598062022-01-13 12:05:09 +00001905 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001906
Bram Moolenaar54598062022-01-13 12:05:09 +00001907 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001908 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001909 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001910 if (p != name)
1911 vim_free(p);
1912}
1913
1914/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001915 * Get function arguments at "*arg" and advance it.
1916 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001917 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001918 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001919 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001920get_func_arguments(
1921 char_u **arg,
1922 evalarg_T *evalarg,
1923 int partial_argc,
1924 typval_T *argvars,
Ernie Raelb077b582023-12-14 20:11:44 +01001925 int *argcount,
1926 int is_builtin)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001927{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001928 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001929 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001930 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001931 int evaluate = evalarg == NULL
1932 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001933
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001934 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001935 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001936 // skip the '(' or ',' and possibly line breaks
1937 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1938
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001939 if (*argp == ')' || *argp == ',' || *argp == NUL)
1940 break;
Ernie Raelb077b582023-12-14 20:11:44 +01001941
1942 int arg_idx = *argcount;
1943 if (eval1(&argp, &argvars[arg_idx], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001944 {
1945 ret = FAIL;
1946 break;
1947 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001948 ++*argcount;
Ernie Raelb077b582023-12-14 20:11:44 +01001949 if (!is_builtin && check_typval_is_value(&argvars[arg_idx]) == FAIL)
1950 {
1951 ret = FAIL;
1952 break;
1953 }
1954
Bram Moolenaar9d489562020-07-30 20:08:50 +02001955 // The comma should come right after the argument, but this wasn't
1956 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001957 if (vim9script)
1958 {
1959 if (*argp != ',' && *skipwhite(argp) == ',')
1960 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001961 if (evaluate)
1962 semsg(_(e_no_white_space_allowed_before_str_str),
1963 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001964 ret = FAIL;
1965 break;
1966 }
1967 }
1968 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001969 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001970 if (*argp != ',')
1971 break;
Christian Brabandt00cb2472023-09-05 20:46:25 +02001972 if (vim9script && !IS_WHITE_NL_OR_NUL(argp[1]))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001973 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001974 if (evaluate)
1975 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001976 ret = FAIL;
1977 break;
1978 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001979 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001980
Bram Moolenaare6b53242020-07-01 17:28:33 +02001981 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001982 if (*argp == ')')
1983 ++argp;
1984 else
1985 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001986 *arg = argp;
1987 return ret;
1988}
1989
1990/*
1991 * Call a function and put the result in "rettv".
1992 * Return OK or FAIL.
1993 */
1994 int
1995get_func_tv(
1996 char_u *name, // name of the function
1997 int len, // length of "name" or -1 to use strlen()
1998 typval_T *rettv,
1999 char_u **arg, // argument, pointing to the '('
2000 evalarg_T *evalarg, // for line continuation
2001 funcexe_T *funcexe) // various values
2002{
2003 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002004 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002005 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
2006 int argcount = 0; // number of arguments found
2007 int vim9script = in_vim9script();
2008 int evaluate = evalarg == NULL
2009 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
2010
2011 argp = *arg;
2012 ret = get_func_arguments(&argp, evalarg,
2013 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
Ernie Raelb077b582023-12-14 20:11:44 +01002014 argvars, &argcount, builtin_function(name, -1));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002015
2016 if (ret == OK)
2017 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002018 int i = 0;
2019 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002020
2021 if (get_vim_var_nr(VV_TESTING))
2022 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002023 // Prepare for calling test_garbagecollect_now(), need to know
2024 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002025 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002026 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002027 for (i = 0; i < argcount; ++i)
2028 if (ga_grow(&funcargs, 1) == OK)
2029 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
2030 &argvars[i];
2031 }
2032
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002033 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00002034 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002035 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002036 // An error in a builtin function does not return FAIL, but we do
2037 // want to abort further processing if an error was given.
2038 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002039 clear_tv(rettv);
2040 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002041
2042 funcargs.ga_len -= i;
2043 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002044 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002045 {
2046 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002047 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002048 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00002049 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002050 }
2051
2052 while (--argcount >= 0)
2053 clear_tv(&argvars[argcount]);
2054
Bram Moolenaar4525a572022-02-13 11:57:33 +00002055 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02002056 *arg = argp;
2057 else
2058 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002059 return ret;
2060}
2061
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002062/*
2063 * Return TRUE if "p" starts with "<SID>" or "s:".
2064 * Only works if eval_fname_script() returned non-zero for "p"!
2065 */
2066 static int
2067eval_fname_sid(char_u *p)
2068{
2069 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
2070}
2071
2072/*
2073 * In a script change <SID>name() and s:name() to K_SNR 123_name().
2074 * Change <SNR>123_name() to K_SNR 123_name().
2075 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002076 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002077 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002078 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00002079fname_trans_sid(
2080 char_u *name,
2081 char_u *fname_buf,
2082 char_u **tofree,
2083 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002084{
2085 int llen;
2086 char_u *fname;
2087 int i;
2088
2089 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002090 if (llen == 0)
2091 return name; // no prefix
2092
2093 fname_buf[0] = K_SPECIAL;
2094 fname_buf[1] = KS_EXTRA;
2095 fname_buf[2] = (int)KE_SNR;
2096 i = 3;
2097 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002098 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002099 if (current_sctx.sc_sid <= 0)
2100 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002101 else
2102 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002103 sprintf((char *)fname_buf + 3, "%ld_",
2104 (long)current_sctx.sc_sid);
2105 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002106 }
2107 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002108 if (i + STRLEN(name + llen) < FLEN_FIXED)
2109 {
2110 STRCPY(fname_buf + i, name + llen);
2111 fname = fname_buf;
2112 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002113 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002114 {
2115 fname = alloc(i + STRLEN(name + llen) + 1);
2116 if (fname == NULL)
2117 *error = FCERR_OTHER;
2118 else
2119 {
2120 *tofree = fname;
2121 mch_memmove(fname, fname_buf, (size_t)i);
2122 STRCPY(fname + i, name + llen);
2123 }
2124 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002125 return fname;
2126}
2127
2128/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002129 * Concatenate the script ID and function name into "<SNR>99_name".
2130 * "buffer" must have size MAX_FUNC_NAME_LEN.
2131 */
2132 void
2133func_name_with_sid(char_u *name, int sid, char_u *buffer)
2134{
2135 // A script-local function is stored as "<SNR>99_name".
2136 buffer[0] = K_SPECIAL;
2137 buffer[1] = KS_EXTRA;
2138 buffer[2] = (int)KE_SNR;
2139 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2140 (long)sid, name);
2141}
2142
2143/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002144 * Find a function "name" in script "sid".
2145 */
2146 static ufunc_T *
2147find_func_with_sid(char_u *name, int sid)
2148{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002149 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002150 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002151
Bram Moolenaarb8822442022-01-11 15:24:05 +00002152 if (!SCRIPT_ID_VALID(sid))
2153 return NULL; // not in a script
2154
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002155 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156 hi = hash_find(&func_hashtab, buffer);
2157 if (!HASHITEM_EMPTY(hi))
2158 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002159 return NULL;
2160}
2161
2162/*
2163 * Find a function "name" in script "sid" prefixing the autoload prefix.
2164 */
2165 static ufunc_T *
2166find_func_with_prefix(char_u *name, int sid)
2167{
2168 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002169 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002170 scriptitem_T *si;
2171
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002172 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002173 return NULL; // already has the prefix
2174 if (!SCRIPT_ID_VALID(sid))
2175 return NULL; // not in a script
2176 si = SCRIPT_ITEM(sid);
2177 if (si->sn_autoload_prefix != NULL)
2178 {
2179 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2180 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002181 char_u *namep;
2182
2183 // skip a "<SNR>99_" prefix
2184 namep = untrans_function_name(name);
2185 if (namep == NULL)
2186 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002187
2188 // An exported function in an autoload script is stored as
2189 // "dir#path#name".
2190 if (len < sizeof(buffer))
2191 auto_name = buffer;
2192 else
2193 auto_name = alloc(len);
2194 if (auto_name != NULL)
2195 {
2196 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002197 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002198 hi = hash_find(&func_hashtab, auto_name);
2199 if (auto_name != buffer)
2200 vim_free(auto_name);
2201 if (!HASHITEM_EMPTY(hi))
2202 return HI2UF(hi);
2203 }
2204 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205
2206 return NULL;
2207}
2208
2209/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002210 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002211 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2212 * functions.
2213 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002214 * Return NULL for unknown function.
2215 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002216 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002217find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002218{
2219 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002221
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002222 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002223 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002224 // Find script-local function before global one.
2225 if (in_vim9script() && eval_isnamec1(*name)
2226 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002228 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2229 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002230 if (func != NULL)
2231 return func;
2232 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002233 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2234 {
2235 char_u *p = name + 5;
2236 long sid;
2237
2238 // printable "<SNR>123_Name" form
2239 sid = getdigits(&p);
2240 if (*p == '_')
2241 {
2242 func = find_func_with_sid(p + 1, (int)sid);
2243 if (func != NULL)
2244 return func;
2245 }
2246 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002247 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002248
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002249 if ((flags & FFED_NO_GLOBAL) == 0)
2250 {
2251 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002252 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002253 if (!HASHITEM_EMPTY(hi))
2254 return HI2UF(hi);
2255 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256
Bram Moolenaarb8822442022-01-11 15:24:05 +00002257 // Find autoload function if this is an autoload script.
2258 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2259 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260}
2261
2262/*
2263 * Find a function by name, return pointer to it in ufuncs.
2264 * "cctx" is passed in a :def function to find imported functions.
2265 * Return NULL for unknown or dead function.
2266 */
2267 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002268find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002270 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271
2272 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2273 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002274 return NULL;
2275}
2276
2277/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002278 * Return TRUE if "ufunc" is a global function.
2279 */
2280 int
2281func_is_global(ufunc_T *ufunc)
2282{
2283 return ufunc->uf_name[0] != K_SPECIAL;
2284}
2285
2286/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002287 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2288 */
2289 int
2290func_requires_g_prefix(ufunc_T *ufunc)
2291{
2292 return ufunc->uf_name[0] != K_SPECIAL
2293 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002294 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01002295 && !SAFE_isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002296}
2297
2298/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002299 * Copy the function name of "fp" to buffer "buf".
2300 * "buf" must be able to hold the function name plus three bytes.
2301 * Takes care of script-local function names.
2302 */
2303 static void
2304cat_func_name(char_u *buf, ufunc_T *fp)
2305{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002306 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002307 {
2308 STRCPY(buf, "<SNR>");
2309 STRCAT(buf, fp->uf_name + 3);
2310 }
2311 else
2312 STRCPY(buf, fp->uf_name);
2313}
2314
2315/*
2316 * Add a number variable "name" to dict "dp" with value "nr".
2317 */
2318 static void
2319add_nr_var(
2320 dict_T *dp,
2321 dictitem_T *v,
2322 char *name,
2323 varnumber_T nr)
2324{
2325 STRCPY(v->di_key, name);
2326 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002327 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002328 v->di_tv.v_type = VAR_NUMBER;
2329 v->di_tv.v_lock = VAR_FIXED;
2330 v->di_tv.vval.v_number = nr;
2331}
2332
2333/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002334 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002335 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002336 static void
2337free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002338{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002339 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002340
Bram Moolenaarca16c602022-09-06 18:57:08 +01002341 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002342 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002343 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002344
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002345 // When garbage collecting a funccall_T may be freed before the
2346 // function that references it, clear its uf_scoped field.
2347 // The function may have been redefined and point to another
2348 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002349 if (fp != NULL && fp->uf_scoped == fc)
2350 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002351 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002352 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002353
Bram Moolenaarca16c602022-09-06 18:57:08 +01002354 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002355 vim_free(fc);
2356}
2357
2358/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002359 * Free "fc" and what it contains.
2360 * Can be called only when "fc" is kept beyond the period of it called,
2361 * i.e. after cleanup_function_call(fc).
2362 */
2363 static void
2364free_funccal_contents(funccall_T *fc)
2365{
2366 listitem_T *li;
2367
2368 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002369 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002370
2371 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002372 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002373
2374 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002375 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002376 clear_tv(&li->li_tv);
2377
2378 free_funccal(fc);
2379}
2380
2381/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002382 * Handle the last part of returning from a function: free the local hashtable.
2383 * Unless it is still in use by a closure.
2384 */
2385 static void
2386cleanup_function_call(funccall_T *fc)
2387{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002388 int may_free_fc = fc->fc_refcount <= 0;
2389 int free_fc = TRUE;
2390
Bram Moolenaarca16c602022-09-06 18:57:08 +01002391 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002392
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002393 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002394 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2395 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002396 else
2397 free_fc = FALSE;
2398
2399 // If the a:000 list and the l: and a: dicts are not referenced and
2400 // there is no closure using it, we can free the funccall_T and what's
2401 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002402 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2403 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002404 else
2405 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002406 int todo;
2407 hashitem_T *hi;
2408 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002409
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002410 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002411
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002412 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002413 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002414 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002415 {
2416 if (!HASHITEM_EMPTY(hi))
2417 {
2418 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002419 di = HI2DI(hi);
2420 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002421 }
2422 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002423 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002424
Bram Moolenaarca16c602022-09-06 18:57:08 +01002425 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2426 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002427 else
2428 {
2429 listitem_T *li;
2430
2431 free_fc = FALSE;
2432
2433 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002434 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002435 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002436 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002437
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002438 if (free_fc)
2439 free_funccal(fc);
2440 else
2441 {
2442 static int made_copy = 0;
2443
2444 // "fc" is still in use. This can happen when returning "a:000",
2445 // assigning "l:" to a global variable or defining a closure.
2446 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002447 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002448 previous_funccal = fc;
2449
2450 if (want_garbage_collect)
2451 // If garbage collector is ready, clear count.
2452 made_copy = 0;
2453 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002454 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002455 // We have made a lot of copies, worth 4 Mbyte. This can happen
2456 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002457 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002458 // too much memory.
2459 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002460 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002461 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002462 }
2463}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002464
2465/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002466 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2467 */
2468 static int
2469numbered_function(char_u *name)
2470{
Keith Thompson184f71c2024-01-04 21:19:04 +01002471 return SAFE_isdigit(*name)
2472 || (name[0] == 'g' && name[1] == ':' && SAFE_isdigit(name[2]));
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002473}
2474
2475/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002476 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002477 * 1. ordinary names, function defined with :function or :def;
2478 * can start with "<SNR>123_" literally or with K_SPECIAL.
2479 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002480 * For the first we only count the name stored in func_hashtab as a reference,
2481 * using function() does not count as a reference, because the function is
2482 * looked up by name.
2483 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002484 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002485func_name_refcount(char_u *name)
2486{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002487 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002488}
2489
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490/*
2491 * Unreference "fc": decrement the reference count and free it when it
2492 * becomes zero. "fp" is detached from "fc".
2493 * When "force" is TRUE we are exiting.
2494 */
2495 static void
2496funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2497{
2498 funccall_T **pfc;
2499 int i;
2500
2501 if (fc == NULL)
2502 return;
2503
2504 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002505 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2506 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2507 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2508 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 {
2510 if (fc == *pfc)
2511 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002512 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513 free_funccal_contents(fc);
2514 return;
2515 }
2516 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002517 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2518 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2519 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520}
2521
2522/*
2523 * Remove the function from the function hashtable. If the function was
2524 * deleted while it still has references this was already done.
2525 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2526 */
2527 static int
2528func_remove(ufunc_T *fp)
2529{
2530 hashitem_T *hi;
2531
2532 // Return if it was already virtually deleted.
2533 if (fp->uf_flags & FC_DEAD)
2534 return FALSE;
2535
2536 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002537 if (HASHITEM_EMPTY(hi))
2538 return FALSE;
2539
2540 // When there is a def-function index do not actually remove the
2541 // function, so we can find the index when defining the function again.
2542 // Do remove it when it's a copy.
2543 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002545 fp->uf_flags |= FC_DEAD;
2546 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002548 hash_remove(&func_hashtab, hi, "remove function");
2549 fp->uf_flags |= FC_DELETED;
2550 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551}
2552
2553 static void
2554func_clear_items(ufunc_T *fp)
2555{
2556 ga_clear_strings(&(fp->uf_args));
2557 ga_clear_strings(&(fp->uf_def_args));
2558 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002560 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002561 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01002562 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002563
2564 // Increment the refcount of this function to avoid it being freed
2565 // recursively when the partial is freed.
2566 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002567 partial_unref(fp->uf_partial);
2568 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002569 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002570
2571#ifdef FEAT_LUA
2572 if (fp->uf_cb_free != NULL)
2573 {
2574 fp->uf_cb_free(fp->uf_cb_state);
2575 fp->uf_cb_free = NULL;
2576 }
2577
2578 fp->uf_cb_state = NULL;
2579 fp->uf_cb = NULL;
2580#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581#ifdef FEAT_PROFILE
2582 VIM_CLEAR(fp->uf_tml_count);
2583 VIM_CLEAR(fp->uf_tml_total);
2584 VIM_CLEAR(fp->uf_tml_self);
2585#endif
2586}
2587
2588/*
2589 * Free all things that a function contains. Does not free the function
2590 * itself, use func_free() for that.
2591 * When "force" is TRUE we are exiting.
2592 */
2593 static void
2594func_clear(ufunc_T *fp, int force)
2595{
2596 if (fp->uf_cleared)
2597 return;
2598 fp->uf_cleared = TRUE;
2599
2600 // clear this function
2601 func_clear_items(fp);
2602 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002603 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604}
2605
2606/*
2607 * Free a function and remove it from the list of functions. Does not free
2608 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002609 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002610 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002612 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002613func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614{
2615 // Only remove it when not done already, otherwise we would remove a newer
2616 // version of the function with the same name.
2617 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2618 func_remove(fp);
2619
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002620 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002621 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002622 if (fp->uf_dfunc_idx > 0)
2623 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002624 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002626 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002627 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002628 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629}
2630
2631/*
2632 * Free all things that a function contains and free the function itself.
2633 * When "force" is TRUE we are exiting.
2634 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002635 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636func_clear_free(ufunc_T *fp, int force)
2637{
2638 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002639 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2640 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002641 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002642 else
2643 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644}
2645
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002646/*
2647 * Copy already defined function "lambda" to a new function with name "global".
2648 * This is for when a compiled function defines a global function.
2649 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002650 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002651copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002652 char_u *lambda,
2653 char_u *global,
2654 loopvarinfo_T *loopvarinfo,
2655 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002656{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002657 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002658 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002659
2660 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002661 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002662 semsg(_(e_lambda_function_not_found_str), lambda);
2663 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002664 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002665
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002666 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002667 if (fp != NULL)
2668 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002669 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002670 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002671 return FAIL;
2672 }
2673
Bram Moolenaare01e5212023-01-08 20:31:18 +00002674 fp = alloc_ufunc(global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002675 if (fp == NULL)
2676 return FAIL;
2677
2678 fp->uf_varargs = ufunc->uf_varargs;
2679 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2680 fp->uf_def_status = ufunc->uf_def_status;
2681 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2682 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2683 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2684 == FAIL
2685 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2686 goto failed;
2687
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002688 if (ufunc->uf_arg_types != NULL)
2689 {
2690 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2691 if (fp->uf_arg_types == NULL)
2692 goto failed;
2693 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2694 sizeof(type_T *) * fp->uf_args.ga_len);
2695 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002696 if (ufunc->uf_va_name != NULL)
2697 {
2698 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2699 if (fp->uf_va_name == NULL)
2700 goto failed;
2701 }
2702 fp->uf_ret_type = ufunc->uf_ret_type;
2703
2704 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002705
2706 fp->uf_name_exp = NULL;
2707 set_ufunc_name(fp, global);
2708
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002709 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002710
2711 // the referenced dfunc_T is now used one more time
2712 link_def_function(fp);
2713
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002714 // Create a partial to store the context of the function where it was
2715 // instantiated. Only needs to be done once. Do this on the original
2716 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002717 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2718 {
2719 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2720
2721 if (pt == NULL)
2722 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002723 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002724 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002725 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002726 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002727 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002728 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002729 }
2730
2731 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002732
2733failed:
2734 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002735 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002736}
2737
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002738static int funcdepth = 0;
2739
2740/*
2741 * Increment the function call depth count.
2742 * Return FAIL when going over 'maxfuncdepth'.
2743 * Otherwise return OK, must call funcdepth_decrement() later!
2744 */
2745 int
2746funcdepth_increment(void)
2747{
2748 if (funcdepth >= p_mfd)
2749 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002750 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002751 return FAIL;
2752 }
2753 ++funcdepth;
2754 return OK;
2755}
2756
2757 void
2758funcdepth_decrement(void)
2759{
2760 --funcdepth;
2761}
2762
2763/*
2764 * Get the current function call depth.
2765 */
2766 int
2767funcdepth_get(void)
2768{
2769 return funcdepth;
2770}
2771
2772/*
2773 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002774 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002775 * times as funcdepth_increment().
2776 */
2777 void
2778funcdepth_restore(int depth)
2779{
2780 funcdepth = depth;
2781}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002782
2783/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002784 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2785 * "rettv".
2786 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2787 * Returns NULL when allocation fails.
2788 */
2789 funccall_T *
2790create_funccal(ufunc_T *fp, typval_T *rettv)
2791{
2792 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2793
2794 if (fc == NULL)
2795 return NULL;
2796 fc->fc_caller = current_funccal;
2797 current_funccal = fc;
2798 fc->fc_func = fp;
2799 func_ptr_ref(fp);
2800 fc->fc_rettv = rettv;
2801 return fc;
2802}
2803
2804/*
2805 * To be called when returning from a compiled function; restores
2806 * current_funccal.
2807 */
2808 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002809remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002810{
2811 funccall_T *fc = current_funccal;
2812
2813 current_funccal = fc->fc_caller;
2814 free_funccal(fc);
2815}
2816
2817/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002818 * Call a user function.
2819 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002820 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002821call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002822 ufunc_T *fp, // pointer to function
2823 int argcount, // nr of args
2824 typval_T *argvars, // arguments
2825 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002826 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002827 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002828{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002829 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002830 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002831 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002832 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002833 funccall_T *fc;
2834 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002835 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002836 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002837 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002838 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002839 int i;
2840 int ai;
2841 int islambda = FALSE;
2842 char_u numbuf[NUMBUFLEN];
2843 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002844 typval_T *tv_to_free[MAX_FUNC_ARGS];
2845 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002846#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002847 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002848#endif
ichizok7e5fe382023-04-15 13:17:50 +01002849 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002850
Bram Moolenaarb2049902021-01-24 12:53:53 +01002851#ifdef FEAT_PROFILE
2852 CLEAR_FIELD(profile_info);
2853#endif
2854
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002855 // If depth of calling is getting too high, don't execute the function.
2856 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002857 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002858 rettv->v_type = VAR_NUMBER;
2859 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002860 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002861 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002862
Bram Moolenaare38eab22019-12-05 21:50:01 +01002863 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002864
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002865 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002866 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00002867 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002868 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002869 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002870 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2871 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002872 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002873 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002874
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002875 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002877#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002878 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002879#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002880 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002881#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002882 if (do_profiling == PROF_YES)
2883 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002884#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002885 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002886 if (call_def_function(fp, argcount, argvars, 0,
2887 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
2888 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002889 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002890#ifdef FEAT_PROFILE
2891 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002892 || (caller != NULL && caller->uf_profiling)))
2893 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002894#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002895 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002896 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002897 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898 }
2899
Bram Moolenaar38453522021-11-28 22:00:12 +00002900 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002901
2902 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002903 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2904 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2905 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002906 */
2907 /*
2908 * Init l: variables.
2909 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002910 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002911 if (selfdict != NULL)
2912 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002913 // Set l:self to "selfdict". Use "name" to avoid a warning from
2914 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002915 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002916 name = v->di_key;
2917 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002918 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002919 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002920 v->di_tv.v_type = VAR_DICT;
2921 v->di_tv.v_lock = 0;
2922 v->di_tv.vval.v_dict = selfdict;
2923 ++selfdict->dv_refcount;
2924 }
2925
2926 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002927 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002928 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002929 * Set a:000 to a list with room for the "..." arguments.
2930 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002931 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002932 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002933 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002934 (varnumber_T)(argcount >= fp->uf_args.ga_len
2935 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002936 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002937 if ((fp->uf_flags & FC_NOARGS) == 0)
2938 {
2939 // Use "name" to avoid a warning from some compiler that checks the
2940 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002941 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002942 name = v->di_key;
2943 STRCPY(name, "000");
2944 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002945 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002946 v->di_tv.v_type = VAR_LIST;
2947 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002948 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002949 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002950 CLEAR_FIELD(fc->fc_l_varlist);
2951 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2952 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002953
2954 /*
2955 * Set a:firstline to "firstline" and a:lastline to "lastline".
2956 * Set a:name to named arguments.
2957 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002958 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002959 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002960 if ((fp->uf_flags & FC_NOARGS) == 0)
2961 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002962 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2963 "firstline", (varnumber_T)funcexe->fe_firstline);
2964 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2965 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002966 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002967 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002968 {
2969 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002970 typval_T def_rettv;
2971 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002972
2973 ai = i - fp->uf_args.ga_len;
2974 if (ai < 0)
2975 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002976 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002977 name = FUNCARG(fp, i);
2978 if (islambda)
2979 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002980
2981 // evaluate named argument default expression
2982 isdefault = ai + fp->uf_def_args.ga_len >= 0
2983 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2984 && argvars[i].vval.v_number == VVAL_NONE));
2985 if (isdefault)
2986 {
2987 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002988
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002989 def_rettv.v_type = VAR_NUMBER;
2990 def_rettv.vval.v_number = -1;
2991
2992 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2993 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002994 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002995 {
2996 default_arg_err = 1;
2997 break;
2998 }
2999 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003000 }
3001 else
3002 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003003 if ((fp->uf_flags & FC_NOARGS) != 0)
3004 // Bail out if no a: arguments used (in lambda).
3005 break;
3006
Bram Moolenaare38eab22019-12-05 21:50:01 +01003007 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003008 sprintf((char *)numbuf, "%d", ai + 1);
3009 name = numbuf;
3010 }
3011 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
3012 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003013 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003014 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003015 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003016 }
3017 else
3018 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003019 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003020 if (v == NULL)
3021 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003022 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003023 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003024
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003025 // Note: the values are copied directly to avoid alloc/free.
3026 // "argvars" must have VAR_FIXED for v_lock.
3027 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003028 v->di_tv.v_lock = VAR_FIXED;
3029
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003030 if (isdefault)
3031 // Need to free this later, no matter where it's stored.
3032 tv_to_free[tv_to_free_len++] = &v->di_tv;
3033
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003034 if (addlocal)
3035 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003036 // Named arguments should be accessed without the "a:" prefix in
3037 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003038 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003039 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003040 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003041 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003042 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003043
3044 if (ai >= 0 && ai < MAX_FUNC_ARGS)
3045 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003046 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003047
3048 li->li_tv = argvars[i];
3049 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003050 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003051 }
3052 }
3053
Bram Moolenaare38eab22019-12-05 21:50:01 +01003054 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003055 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003056
3057 if (fp->uf_flags & FC_SANDBOX)
3058 {
3059 using_sandbox = TRUE;
3060 ++sandbox;
3061 }
3062
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003063 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003064 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003065 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003066 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003067 ++no_wait_return;
3068 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003069
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003070 smsg(_("calling %s"), SOURCING_NAME);
3071 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003072 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003073 char_u buf[MSG_BUF_LEN];
3074 char_u numbuf2[NUMBUFLEN];
3075 char_u *tofree;
3076 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003077
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003078 msg_puts("(");
3079 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003080 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003081 if (i > 0)
3082 msg_puts(", ");
3083 if (argvars[i].v_type == VAR_NUMBER)
3084 msg_outnum((long)argvars[i].vval.v_number);
3085 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003086 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003087 // Do not want errors such as E724 here.
3088 ++emsg_off;
3089 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3090 --emsg_off;
3091 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003092 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003093 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003094 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003095 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3096 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003097 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003098 msg_puts((char *)s);
3099 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003100 }
3101 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003102 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003103 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003104 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003105 msg_puts("\n"); // don't overwrite this either
3106
3107 verbose_leave_scroll();
3108 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003109 }
3110#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003111 if (do_profiling == PROF_YES)
3112 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003113 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003114#endif
3115
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003116 // "legacy" does not apply to commands in the function
3117 sticky_cmdmod_flags = 0;
3118
Bram Moolenaar98aff652022-09-06 21:02:35 +01003119 // If called from a compiled :def function the execution context must be
3120 // hidden, any deferred functions need to be added to the function being
3121 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003122 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003123
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003124 save_current_sctx = current_sctx;
3125 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003126 save_did_emsg = did_emsg;
3127 did_emsg = FALSE;
3128
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003129 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003130 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003131 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003132 retval = FCERR_FAILED;
3133 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003134 else if (islambda)
3135 {
3136 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3137
3138 // A Lambda always has the command "return {expr}". It is much faster
3139 // to evaluate {expr} directly.
3140 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003141 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003142 --ex_nesting_level;
3143 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003144 else
3145 // call do_cmdline() to execute the lines
3146 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003147 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3148
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003149 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003150 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003151
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003152 if (RedrawingDisabled > 0)
3153 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003154
Bram Moolenaare38eab22019-12-05 21:50:01 +01003155 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003156 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3157 {
3158 clear_tv(rettv);
3159 rettv->v_type = VAR_NUMBER;
3160 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003161
3162 // In corner cases returning a "failed" value is not backwards
3163 // compatible. Only do this for Vim9 script.
3164 if (in_vim9script())
3165 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003166 }
3167
3168#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003169 if (do_profiling == PROF_YES)
3170 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003171 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003172
3173 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3174 profile_may_end_func(&profile_info, fp, caller);
3175 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003176#endif
3177
Bram Moolenaare38eab22019-12-05 21:50:01 +01003178 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003179 if (p_verbose >= 12)
3180 {
3181 ++no_wait_return;
3182 verbose_enter_scroll();
3183
3184 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003185 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003186 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003187 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003188 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003189 else
3190 {
3191 char_u buf[MSG_BUF_LEN];
3192 char_u numbuf2[NUMBUFLEN];
3193 char_u *tofree;
3194 char_u *s;
3195
Bram Moolenaare38eab22019-12-05 21:50:01 +01003196 // The value may be very long. Skip the middle part, so that we
3197 // have some idea how it starts and ends. smsg() would always
3198 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003199 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003200 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003201 --emsg_off;
3202 if (s != NULL)
3203 {
3204 if (vim_strsize(s) > MSG_BUF_CLEN)
3205 {
3206 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3207 s = buf;
3208 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003209 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003210 vim_free(tofree);
3211 }
3212 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003213 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003214
3215 verbose_leave_scroll();
3216 --no_wait_return;
3217 }
3218
ichizok7e5fe382023-04-15 13:17:50 +01003219 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003220 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003221 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003222 restore_current_ectx(save_current_ectx);
3223
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003224#ifdef FEAT_PROFILE
3225 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003226 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003227#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003228 if (using_sandbox)
3229 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003230 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003231
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003232 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003233 {
3234 ++no_wait_return;
3235 verbose_enter_scroll();
3236
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003237 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003238 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003239
3240 verbose_leave_scroll();
3241 --no_wait_return;
3242 }
3243
3244 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003245 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003246 for (i = 0; i < tv_to_free_len; ++i)
3247 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003248 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003249
3250 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003251}
3252
3253/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003254 * Check the argument count for user function "fp".
3255 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3256 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003257 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003258check_user_func_argcount(ufunc_T *fp, int argcount)
3259{
3260 int regular_args = fp->uf_args.ga_len;
3261
3262 if (argcount < regular_args - fp->uf_def_args.ga_len)
3263 return FCERR_TOOFEW;
3264 else if (!has_varargs(fp) && argcount > regular_args)
3265 return FCERR_TOOMANY;
3266 return FCERR_UNKNOWN;
3267}
3268
3269/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003271 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003272 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273call_user_func_check(
3274 ufunc_T *fp,
3275 int argcount,
3276 typval_T *argvars,
3277 typval_T *rettv,
3278 funcexe_T *funcexe,
3279 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003280{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003281 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003282
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003283#ifdef FEAT_LUA
3284 if (fp->uf_flags & FC_CFUNC)
3285 {
3286 cfunc_T cb = fp->uf_cb;
3287
3288 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3289 }
3290#endif
3291
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003292 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3293 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003294 error = check_user_func_argcount(fp, argcount);
3295 if (error != FCERR_UNKNOWN)
3296 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003297
Bram Moolenaar50824712020-12-20 21:10:17 +01003298 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003299 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003301 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003303 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304 int did_save_redo = FALSE;
3305 save_redo_T save_redo;
3306
3307 /*
3308 * Call the user function.
3309 * Save and restore search patterns, script variables and
3310 * redo buffer.
3311 */
3312 save_search_patterns();
3313 if (!ins_compl_active())
3314 {
3315 saveRedobuff(&save_redo);
3316 did_save_redo = TRUE;
3317 }
3318 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003319 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003320 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3322 // Function was unreferenced while being used, free it now.
3323 func_clear_free(fp, FALSE);
3324 if (did_save_redo)
3325 restoreRedobuff(&save_redo);
3326 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003327 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003328
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003330}
3331
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003332static funccal_entry_T *funccal_stack = NULL;
3333
3334/*
3335 * Save the current function call pointer, and set it to NULL.
3336 * Used when executing autocommands and for ":source".
3337 */
3338 void
3339save_funccal(funccal_entry_T *entry)
3340{
3341 entry->top_funccal = current_funccal;
3342 entry->next = funccal_stack;
3343 funccal_stack = entry;
3344 current_funccal = NULL;
3345}
3346
3347 void
3348restore_funccal(void)
3349{
3350 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003351 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003352 else
3353 {
3354 current_funccal = funccal_stack->top_funccal;
3355 funccal_stack = funccal_stack->next;
3356 }
3357}
3358
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003359 funccall_T *
3360get_current_funccal(void)
3361{
3362 return current_funccal;
3363}
3364
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003365/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003366 * Return TRUE when currently at the script level:
3367 * - not in a function
3368 * - not executing an autocommand
3369 * Note that when an autocommand sources a script the result is FALSE;
3370 */
3371 int
3372at_script_level(void)
3373{
3374 return current_funccal == NULL && autocmd_match == NULL;
3375}
3376
3377/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003378 * Mark all functions of script "sid" as deleted.
3379 */
3380 void
3381delete_script_functions(int sid)
3382{
3383 hashitem_T *hi;
3384 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003385 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003386 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003387 size_t len;
3388
3389 buf[0] = K_SPECIAL;
3390 buf[1] = KS_EXTRA;
3391 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003392 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003393 len = STRLEN(buf);
3394
Bram Moolenaarce658352020-07-31 23:47:12 +02003395 while (todo > 0)
3396 {
3397 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003398 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003399 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003400 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003401 fp = HI2UF(hi);
3402 if (STRNCMP(fp->uf_name, buf, len) == 0)
3403 {
3404 int changed = func_hashtab.ht_changed;
3405
3406 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003407
3408 if (fp->uf_calls > 0)
3409 {
3410 // Function is executing, don't free it but do remove
3411 // it from the hashtable.
3412 if (func_remove(fp))
3413 fp->uf_refcount--;
3414 }
3415 else
3416 {
3417 func_clear(fp, TRUE);
3418 // When clearing a function another function can be
3419 // cleared as a side effect. When that happens start
3420 // over.
3421 if (changed != func_hashtab.ht_changed)
3422 break;
3423 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003424 }
3425 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003426 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003427 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003428}
3429
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003430#if defined(EXITFREE) || defined(PROTO)
3431 void
3432free_all_functions(void)
3433{
3434 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003435 ufunc_T *fp;
3436 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003437 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003438 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003439
Bram Moolenaare38eab22019-12-05 21:50:01 +01003440 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003441 while (current_funccal != NULL)
3442 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003443 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003444 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003445 if (current_funccal == NULL && funccal_stack != NULL)
3446 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003447 }
3448
Bram Moolenaare38eab22019-12-05 21:50:01 +01003449 // First clear what the functions contain. Since this may lower the
3450 // reference count of a function, it may also free a function and change
3451 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003452 while (todo > 0)
3453 {
3454 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003455 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003456 if (!HASHITEM_EMPTY(hi))
3457 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 // clear the def function index now
3459 fp = HI2UF(hi);
3460 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003461 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003462
Bram Moolenaare38eab22019-12-05 21:50:01 +01003463 // Only free functions that are not refcounted, those are
3464 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003465 if (func_name_refcount(fp->uf_name))
3466 ++skipped;
3467 else
3468 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003469 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003470 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003471 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003472 {
3473 skipped = 0;
3474 break;
3475 }
3476 }
3477 --todo;
3478 }
3479 }
3480
Bram Moolenaare38eab22019-12-05 21:50:01 +01003481 // Now actually free the functions. Need to start all over every time,
3482 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003483 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003484 while (func_hashtab.ht_used > skipped)
3485 {
3486 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003487 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003488 if (!HASHITEM_EMPTY(hi))
3489 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003490 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003491 // Only free functions that are not refcounted, those are
3492 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003493 fp = HI2UF(hi);
3494 if (func_name_refcount(fp->uf_name))
3495 ++skipped;
3496 else
3497 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003498 if (func_free(fp, FALSE) == OK)
3499 {
3500 skipped = 0;
3501 break;
3502 }
3503 // did not actually free it
3504 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003505 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003506 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003507 }
3508 if (skipped == 0)
3509 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510
3511 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003512}
3513#endif
3514
3515/*
3516 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003517 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3518 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003519 * "len" is the length of "name", or -1 for NUL terminated.
3520 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003522builtin_function(char_u *name, int len)
3523{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003524 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003525
Bram Moolenaara26b9702020-04-18 19:53:28 +02003526 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003527 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003528 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3529 {
3530 if (name[i] == AUTOLOAD_CHAR)
3531 return FALSE;
3532 if (!eval_isnamec(name[i]))
3533 {
3534 // "name.something" is not a builtin function
3535 if (name[i] == '.')
3536 return FALSE;
3537 break;
3538 }
3539 }
3540 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003541}
3542
3543 int
3544func_call(
3545 char_u *name,
3546 typval_T *args,
3547 partial_T *partial,
3548 dict_T *selfdict,
3549 typval_T *rettv)
3550{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003551 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003552 listitem_T *item;
3553 typval_T argv[MAX_FUNC_ARGS + 1];
3554 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003555 int r = 0;
3556
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003557 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003558 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003559 {
3560 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3561 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003562 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003563 break;
3564 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003565 // Make a copy of each argument. This is needed to be able to set
3566 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003567 copy_tv(&item->li_tv, &argv[argc++]);
3568 }
3569
3570 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003571 {
3572 funcexe_T funcexe;
3573
Bram Moolenaara80faa82020-04-12 19:37:17 +02003574 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003575 funcexe.fe_firstline = curwin->w_cursor.lnum;
3576 funcexe.fe_lastline = curwin->w_cursor.lnum;
3577 funcexe.fe_evaluate = TRUE;
3578 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003579 if (partial != NULL)
3580 {
3581 funcexe.fe_object = partial->pt_obj;
3582 if (funcexe.fe_object != NULL)
3583 ++funcexe.fe_object->obj_refcount;
3584 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003585 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003586 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3587 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003588
Bram Moolenaare38eab22019-12-05 21:50:01 +01003589 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003590 while (argc > 0)
3591 clear_tv(&argv[--argc]);
3592
3593 return r;
3594}
3595
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003596static int callback_depth = 0;
3597
3598 int
3599get_callback_depth(void)
3600{
3601 return callback_depth;
3602}
3603
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003604/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003605 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003606 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003607 */
3608 int
3609call_callback(
3610 callback_T *callback,
3611 int len, // length of "name" or -1 to use strlen()
3612 typval_T *rettv, // return value goes here
3613 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003614 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003615 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003616{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003617 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003618 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003619
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003620 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3621 return FAIL;
Christian Brabandt47510f32023-10-15 09:56:16 +02003622
zeertzjqfe583b12023-12-21 16:59:26 +01003623 if (callback_depth > p_mfd)
Christian Brabandt47510f32023-10-15 09:56:16 +02003624 {
3625 emsg(_(e_command_too_recursive));
3626 return FAIL;
3627 }
3628
Bram Moolenaara80faa82020-04-12 19:37:17 +02003629 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003630 funcexe.fe_evaluate = TRUE;
3631 funcexe.fe_partial = callback->cb_partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003632 if (callback->cb_partial != NULL)
3633 {
3634 funcexe.fe_object = callback->cb_partial->pt_obj;
3635 if (funcexe.fe_object != NULL)
3636 ++funcexe.fe_object->obj_refcount;
3637 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003638 ++callback_depth;
3639 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3640 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003641
3642 // When a :def function was called that uses :try an error would be turned
3643 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003644 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003645 {
3646 need_rethrow = FALSE;
3647 handle_did_throw();
3648 }
3649
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003650 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003651}
3652
3653/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003654 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003655 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003656 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3657 */
3658 varnumber_T
3659call_callback_retnr(
3660 callback_T *callback,
3661 int argcount, // number of "argvars"
3662 typval_T *argvars) // vars for arguments, must have "argcount"
3663 // PLUS ONE elements!
3664{
3665 typval_T rettv;
3666 varnumber_T retval;
3667
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003668 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003669 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003670
3671 retval = tv_get_number_chk(&rettv, NULL);
3672 clear_tv(&rettv);
3673 return retval;
3674}
3675
3676/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 * Give an error message for the result of a function.
3678 * Nothing if "error" is FCERR_NONE.
3679 */
3680 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003681user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682{
3683 switch (error)
3684 {
3685 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003686 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003687 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003688 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003689 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690 break;
3691 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003692 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693 break;
3694 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003695 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 break;
3697 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003698 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699 break;
3700 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003701 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 break;
3703 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003704 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 break;
3706 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003707 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3708 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003710 case FCERR_OTHER:
3711 case FCERR_FAILED:
3712 // assume the error message was already given
3713 break;
3714 case FCERR_NONE:
3715 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716 }
3717}
3718
3719/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003720 * Check the argument types "argvars[argcount]" for "name" using the
3721 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3722 * is already included in "argvars[]".
3723 * Will do nothing if "funcexe->fe_check_type" is NULL or
3724 * "funcexe->fe_evaluate" is FALSE;
3725 * Returns an FCERR_ value.
3726 */
3727 static funcerror_T
3728may_check_argument_types(
3729 funcexe_T *funcexe,
3730 typval_T *argvars,
3731 int argcount,
3732 int base_included,
3733 char_u *name)
3734{
3735 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3736 {
3737 // Check that the argument types are OK for the types of the funcref.
3738 if (check_argument_types(funcexe->fe_check_type,
3739 argvars, argcount,
3740 base_included ? NULL : funcexe->fe_basetv,
3741 name) == FAIL)
3742 return FCERR_OTHER;
3743 }
3744 return FCERR_NONE;
3745}
3746
3747/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003748 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003749 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003750 * Return FAIL when the function can't be called, OK otherwise.
3751 * Also returns OK when an error was encountered while executing the function.
3752 */
3753 int
3754call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003755 char_u *funcname, // name of the function
3756 int len, // length of "name" or -1 to use strlen()
3757 typval_T *rettv, // return value goes here
3758 int argcount_in, // number of "argvars"
3759 typval_T *argvars_in, // vars for arguments, must have "argcount"
3760 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003761 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003762{
3763 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003764 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003765 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003766 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003767 char_u fname_buf[FLEN_FIXED + 1];
3768 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003769 char_u *fname = NULL;
3770 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003771 int argcount = argcount_in;
3772 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003773 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003774 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003775 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003776 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003777 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003778 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003779 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003780 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003781
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003782 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3783 // even when call_func() returns FAIL.
3784 rettv->v_type = VAR_UNKNOWN;
3785
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003786 if (partial != NULL)
3787 fp = partial->pt_func;
3788 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003789 fp = funcexe->fe_ufunc;
3790
3791 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003792 {
3793 // Make a copy of the name, if it comes from a funcref variable it
3794 // could be changed or deleted in the called function.
3795 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3796 if (name == NULL)
3797 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003798
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003799 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3800 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003801
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003802 if (funcexe->fe_doesrange != NULL)
3803 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003804
3805 if (partial != NULL)
3806 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003807 // When the function has a partial with a dict and there is a dict
3808 // argument, use the dict argument. That is backwards compatible.
3809 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003810 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003811 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003812 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003813 {
3814 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003815 {
3816 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3817 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003818 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003819 goto theend;
3820 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003821 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003822 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003823 for (i = 0; i < argcount_in; ++i)
3824 argv[i + argv_clear] = argvars_in[i];
3825 argvars = argv;
3826 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003827
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003828 if (funcexe->fe_check_type != NULL
3829 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003830 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003831 // Now funcexe->fe_check_type is missing the added arguments,
3832 // make a copy of the type with the correction.
3833 check_type = *funcexe->fe_check_type;
3834 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003835 check_type.tt_args = check_type_args;
3836 CLEAR_FIELD(check_type_args);
3837 for (i = 0; i < check_type.tt_argcount; ++i)
3838 check_type_args[i + partial->pt_argc] =
3839 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003840 check_type.tt_argcount += partial->pt_argc;
3841 check_type.tt_min_argcount += partial->pt_argc;
3842 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003843 }
3844 }
3845
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003846 if (error == FCERR_NONE)
3847 // check the argument types if possible
3848 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
3849 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003850
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003851 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003852 {
3853 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003854 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003855
Bram Moolenaar333894b2020-08-01 18:53:07 +02003856 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003857 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003858 {
3859 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003860 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003861 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003862
Bram Moolenaare38eab22019-12-05 21:50:01 +01003863 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003864 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003865 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003866
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003867 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003868 {
3869 /*
3870 * User defined function.
3871 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003872 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003873 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003874 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003875 if (fp != NULL && !is_global && in_vim9script()
3876 && func_requires_g_prefix(fp))
3877 // In Vim9 script g: is required to find a global
3878 // non-autoload function.
3879 fp = NULL;
3880 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003881
Bram Moolenaare38eab22019-12-05 21:50:01 +01003882 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003883 if (fp == NULL
3884 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003885 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003886 && !aborting())
3887 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003888 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003889 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003890 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003891 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003892 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3893 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003894 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003895 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003896 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003897 if (fp == NULL)
3898 {
3899 char_u *p = untrans_function_name(rfname);
3900
3901 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003902 // Don't do this if the name starts with "s:".
3903 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003904 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003905 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003906
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003907 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003908 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003909 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003910 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003911 int need_arg_check = FALSE;
3912 if (funcexe->fe_check_type == NULL)
3913 {
3914 funcexe->fe_check_type = fp->uf_func_type;
3915 need_arg_check = TRUE;
3916 }
3917
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003918 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003919 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003920 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003921 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003922 argv_clear, fp);
3923 need_arg_check = TRUE;
3924 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003925
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003926 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003927 {
3928 // Method call: base->Method()
3929 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003930 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003931 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003932 argvars = argv;
3933 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003934 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003935 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003936
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003937 // Check the argument types now that the function type and all
3938 // argument values are known, if not done above.
3939 if (need_arg_check)
3940 error = may_check_argument_types(funcexe, argvars, argcount,
3941 TRUE, (name != NULL) ? name : funcname);
3942 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
3943 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003945 }
3946 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003947 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003948 {
3949 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003950 * expr->method(): Find the method name in the table, call its
3951 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003952 */
3953 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003954 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003955 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003956 else
3957 {
3958 /*
3959 * Find the function name in the table, call its implementation.
3960 */
3961 error = call_internal_func(fname, argcount, argvars, rettv);
3962 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003963
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003964 /*
3965 * The function call (or "FuncUndefined" autocommand sequence) might
3966 * have been aborted by an error, an interrupt, or an explicitly thrown
3967 * exception that has not been caught so far. This situation can be
3968 * tested for by calling aborting(). For an error in an internal
3969 * function or for the "E132" error in call_user_func(), however, the
3970 * throw point at which the "force_abort" flag (temporarily reset by
3971 * emsg()) is normally updated has not been reached yet. We need to
3972 * update that flag first to make aborting() reliable.
3973 */
3974 update_force_abort();
3975 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003976 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003977 ret = OK;
3978
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003979theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003980 /*
3981 * Report an error unless the argument evaluation or function call has been
3982 * cancelled due to an aborting error, an interrupt, or an exception.
3983 */
3984 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003985 user_func_error(error, (name != NULL) ? name : funcname,
3986 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003987
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003988 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003989 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003990 clear_tv(&argv[--argv_clear + argv_base]);
3991
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003992 vim_free(tofree);
3993 vim_free(name);
3994
3995 return ret;
3996}
3997
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003998/*
3999 * Call a function without arguments, partial or dict.
4000 * This is like call_func() when the call is only "FuncName()".
4001 * To be used by "expr" options.
4002 * Returns NOTDONE when the function could not be found.
4003 */
4004 int
4005call_simple_func(
4006 char_u *funcname, // name of the function
4007 int len, // length of "name" or -1 to use strlen()
4008 typval_T *rettv) // return value goes here
4009{
4010 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00004011 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004012 char_u fname_buf[FLEN_FIXED + 1];
4013 char_u *tofree = NULL;
4014 char_u *name;
4015 char_u *fname;
4016 char_u *rfname;
4017 int is_global = FALSE;
4018 ufunc_T *fp;
4019
4020 rettv->v_type = VAR_NUMBER; // default rettv is number zero
4021 rettv->vval.v_number = 0;
4022
4023 // Make a copy of the name, an option can be changed in the function.
4024 name = vim_strnsave(funcname, len);
4025 if (name == NULL)
4026 return ret;
4027
4028 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
4029
4030 // Skip "g:" before a function name.
4031 if (fname[0] == 'g' && fname[1] == ':')
4032 {
4033 is_global = TRUE;
4034 rfname = fname + 2;
4035 }
4036 else
4037 rfname = fname;
4038 fp = find_func(rfname, is_global);
4039 if (fp != NULL && !is_global && in_vim9script()
4040 && func_requires_g_prefix(fp))
4041 // In Vim9 script g: is required to find a global non-autoload
4042 // function.
4043 fp = NULL;
4044 if (fp == NULL)
4045 ret = NOTDONE;
4046 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
4047 error = FCERR_DELETED;
4048 else if (fp != NULL)
4049 {
4050 typval_T argvars[1];
4051 funcexe_T funcexe;
4052
4053 argvars[0].v_type = VAR_UNKNOWN;
4054 CLEAR_FIELD(funcexe);
4055 funcexe.fe_evaluate = TRUE;
4056
4057 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
4058 if (error == FCERR_NONE)
4059 ret = OK;
4060 }
4061
4062 user_func_error(error, name, FALSE);
4063 vim_free(tofree);
4064 vim_free(name);
4065
4066 return ret;
4067}
4068
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004069 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070printable_func_name(ufunc_T *fp)
4071{
4072 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4073}
4074
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004075/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004076 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4077 * TRUE. Otherwise return FALSE.
4078 */
4079 static int
4080function_list_modified(int prev_ht_changed)
4081{
4082 if (prev_ht_changed != func_hashtab.ht_changed)
4083 {
4084 emsg(_(e_function_list_was_modified));
4085 return TRUE;
4086 }
4087 return FALSE;
4088}
4089
4090/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004091 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004092 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004093 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004094list_func_head(ufunc_T *fp, int indent)
4095{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004096 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004097 int j;
4098
4099 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004100
4101 // a timer at the more prompt may have deleted the function
4102 if (function_list_modified(prev_ht_changed))
4103 return FAIL;
4104
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004105 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004106 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004107 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004108 msg_puts("def ");
4109 else
4110 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004111 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004112 msg_putchar('(');
4113 for (j = 0; j < fp->uf_args.ga_len; ++j)
4114 {
4115 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004116 msg_puts(", ");
4117 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 if (fp->uf_arg_types != NULL)
4119 {
4120 char *tofree;
4121
4122 msg_puts(": ");
4123 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4124 vim_free(tofree);
4125 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004126 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4127 {
4128 msg_puts(" = ");
4129 msg_puts(((char **)(fp->uf_def_args.ga_data))
4130 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4131 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004132 }
4133 if (fp->uf_varargs)
4134 {
4135 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004136 msg_puts(", ");
4137 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004138 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 if (fp->uf_va_name != NULL)
4140 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004141 if (!fp->uf_varargs)
4142 {
4143 if (j)
4144 msg_puts(", ");
4145 msg_puts("...");
4146 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004148 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004149 {
4150 char *tofree;
4151
4152 msg_puts(": ");
4153 msg_puts(type_name(fp->uf_va_type, &tofree));
4154 vim_free(tofree);
4155 }
4156 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004157 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004158
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004159 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004160 {
4161 if (fp->uf_ret_type != &t_void)
4162 {
4163 char *tofree;
4164
4165 msg_puts(": ");
4166 msg_puts(type_name(fp->uf_ret_type, &tofree));
4167 vim_free(tofree);
4168 }
4169 }
4170 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004171 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004172 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004173 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004174 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004175 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004176 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004177 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004178 msg_clr_eos();
4179 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004180 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004181
4182 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004183}
4184
4185/*
4186 * Get a function name, translating "<SID>" and "<SNR>".
4187 * Also handles a Funcref in a List or Dictionary.
4188 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004189 * Set "*is_global" to TRUE when the function must be global, unless
4190 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004191 * flags:
4192 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004193 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004194 * TFN_QUIET: be quiet
4195 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004196 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004197 * Advances "pp" to just after the function name (if no error).
4198 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004199 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004200trans_function_name(
4201 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004202 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004203 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004204 int flags)
4205{
4206 return trans_function_name_ext(pp, is_global, skip, flags,
4207 NULL, NULL, NULL, NULL);
4208}
4209
4210/*
4211 * trans_function_name() with extra arguments.
4212 * "fdp", "partial", "type" and "ufunc" can be NULL.
4213 */
4214 char_u *
4215trans_function_name_ext(
4216 char_u **pp,
4217 int *is_global,
4218 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004219 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004220 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004221 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004222 type_T **type, // return: type of funcref
4223 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004224{
4225 char_u *name = NULL;
4226 char_u *start;
4227 char_u *end;
4228 int lead;
4229 char_u sid_buf[20];
4230 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004232 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004233 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004234 int vim9script = in_vim9script();
4235 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004236
4237 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004238 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004239 start = *pp;
4240
Bram Moolenaare38eab22019-12-05 21:50:01 +01004241 // Check for hard coded <SNR>: already translated function ID (from a user
4242 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004243 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4244 && (*pp)[2] == (int)KE_SNR)
4245 {
4246 *pp += 3;
4247 len = get_id_len(pp) + 3;
4248 return vim_strnsave(start, len);
4249 }
4250
Bram Moolenaare38eab22019-12-05 21:50:01 +01004251 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4252 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004253 lead = eval_fname_script(start);
4254 if (lead > 2)
4255 start += lead;
4256
Bram Moolenaare38eab22019-12-05 21:50:01 +01004257 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004258 end = get_lval(start, NULL, &lv, FALSE, skip,
4259 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004260 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004261 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004262 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004263 {
4264 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004265 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004266 goto theend;
4267 }
4268 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4269 {
4270 /*
4271 * Report an invalid expression in braces, unless the expression
4272 * evaluation has been cancelled due to an aborting error, an
4273 * interrupt, or an exception.
4274 */
4275 if (!aborting())
4276 {
4277 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004278 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004279 }
4280 else
4281 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4282 goto theend;
4283 }
4284
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004285 if (lv.ll_ufunc != NULL)
4286 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004287 if (ufunc != NULL)
4288 *ufunc = lv.ll_ufunc;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004289 name = vim_strsave(lv.ll_ufunc->uf_name);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004290 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004291 goto theend;
4292 }
4293
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004294 if (lv.ll_tv != NULL)
4295 {
4296 if (fdp != NULL)
4297 {
4298 fdp->fd_dict = lv.ll_dict;
4299 fdp->fd_newkey = lv.ll_newkey;
4300 lv.ll_newkey = NULL;
4301 fdp->fd_di = lv.ll_di;
4302 }
4303 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4304 {
4305 name = vim_strsave(lv.ll_tv->vval.v_string);
4306 *pp = end;
4307 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004308 else if (lv.ll_tv->v_type == VAR_CLASS
4309 && lv.ll_tv->vval.v_class != NULL)
4310 {
4311 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4312 *pp = end;
4313 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004314 else if (lv.ll_tv->v_type == VAR_PARTIAL
4315 && lv.ll_tv->vval.v_partial != NULL)
4316 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004317 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004318 *pp = end;
4319 if (partial != NULL)
4320 *partial = lv.ll_tv->vval.v_partial;
4321 }
4322 else
4323 {
4324 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4325 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004326 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004327 else
4328 *pp = end;
4329 name = NULL;
4330 }
4331 goto theend;
4332 }
4333
4334 if (lv.ll_name == NULL)
4335 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004336 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004337 *pp = end;
4338 goto theend;
4339 }
4340
Bram Moolenaare38eab22019-12-05 21:50:01 +01004341 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004342 if (lv.ll_exp_name != NULL)
4343 {
4344 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004345 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004346 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004347 if (name == lv.ll_exp_name)
4348 name = NULL;
4349 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004350 else if (lv.ll_sid > 0)
4351 {
4352 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4353 int cc = *lv.ll_name_end;
4354
4355 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4356 *lv.ll_name_end = NUL;
4357 if (si->sn_autoload_prefix != NULL)
4358 {
4359 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4360 }
4361 else
4362 {
4363 sid_buf[0] = K_SPECIAL;
4364 sid_buf[1] = KS_EXTRA;
4365 sid_buf[2] = (int)KE_SNR;
4366 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004367 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004368 name = concat_str(sid_buf, lv.ll_name);
4369 }
4370 *lv.ll_name_end = cc;
4371 *pp = end;
4372 goto theend;
4373 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004374 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004375 {
4376 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004377 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004378 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004379 if (name == *pp)
4380 name = NULL;
4381 }
4382 if (name != NULL)
4383 {
4384 name = vim_strsave(name);
4385 *pp = end;
4386 if (STRNCMP(name, "<SNR>", 5) == 0)
4387 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004388 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004389 name[0] = K_SPECIAL;
4390 name[1] = KS_EXTRA;
4391 name[2] = (int)KE_SNR;
4392 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4393 }
4394 goto theend;
4395 }
4396
4397 if (lv.ll_exp_name != NULL)
4398 {
4399 len = (int)STRLEN(lv.ll_exp_name);
4400 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4401 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4402 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004403 // When there was "s:" already or the name expanded to get a
4404 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004405 lv.ll_name += 2;
4406 len -= 2;
4407 lead = 2;
4408 }
4409 }
4410 else
4411 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004412 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004413 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004414 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004415 if (lv.ll_name[0] == 'g')
4416 {
4417 if (is_global != NULL)
4418 {
4419 *is_global = TRUE;
4420 }
4421 else
4422 {
4423 // dropping "g:" without setting "is_global" won't work in
4424 // Vim9script, put it back later
4425 prefix_g = TRUE;
4426 extra = 2;
4427 }
4428 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004429 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004430 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004431 len = (int)(end - lv.ll_name);
4432 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004433 if (len <= 0)
4434 {
4435 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004436 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004437 goto theend;
4438 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004439
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004440 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004441 // starts with a lower case character: dict.func(). Or when in a class.
4442 vim9_local = ASCII_ISUPPER(*start) && vim9script
4443 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004445 /*
4446 * Copy the function name to allocated memory.
4447 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4448 * Accept <SNR>123_name() outside a script.
4449 */
4450 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004451 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004452 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004453 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004454 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004455 {
Kota Kato948a3892022-08-16 16:09:59 +01004456 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4457 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004458 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004459 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004460 goto theend;
4461 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004463 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004464 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004466 || eval_fname_sid(*pp))
4467 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004469 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004470 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004471 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004472 goto theend;
4473 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004474 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004475 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476 extra = 3 + (int)STRLEN(sid_buf);
4477 else
4478 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004479 }
4480 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004481 // The function name must start with an upper case letter (unless it is a
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004482 // Vim9 class new() function or a Vim9 class private method or one of the
4483 // supported Vim9 object builtin functions)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004484 else if (!(flags & TFN_INT)
4485 && (builtin_function(lv.ll_name, len)
4486 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004487 && !((flags & TFN_IN_CLASS)
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004488 && (is_valid_builtin_obj_methodname(lv.ll_name)
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004489 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004490 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004491 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004492 : e_function_name_must_start_with_capital_or_s_str),
4493 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004494 goto theend;
4495 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004496 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004497 {
4498 char_u *cp = vim_strchr(lv.ll_name, ':');
4499
4500 if (cp != NULL && cp < end)
4501 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004502 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004503 goto theend;
4504 }
4505 }
4506
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004507 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004508 if (name != NULL)
4509 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004510 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004511 {
4512 name[0] = K_SPECIAL;
4513 name[1] = KS_EXTRA;
4514 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004515 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004516 STRCPY(name + 3, sid_buf);
4517 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004518 else if (prefix_g)
4519 {
4520 name[0] = 'g';
4521 name[1] = ':';
4522 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4524 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004525 }
4526 *pp = end;
4527
4528theend:
4529 clear_lval(&lv);
4530 return name;
4531}
4532
4533/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004534 * Assuming "name" is the result of trans_function_name() and it was prefixed
4535 * to use the script-local name, return the unmodified name (points into
4536 * "name"). Otherwise return NULL.
4537 * This can be used to first search for a script-local function and fall back
4538 * to the global function if not found.
4539 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004540 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004541untrans_function_name(char_u *name)
4542{
4543 char_u *p;
4544
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004545 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004546 {
4547 p = vim_strchr(name, '_');
4548 if (p != NULL)
4549 return p + 1;
4550 }
4551 return NULL;
4552}
4553
4554/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004555 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4556 * current script ID and returns the expanded function name. The caller should
4557 * free the returned name. If not called from a script context or the function
4558 * name doesn't start with these prefixes, then returns NULL.
4559 * This doesn't check whether the script-local function exists or not.
4560 */
4561 char_u *
4562get_scriptlocal_funcname(char_u *funcname)
4563{
4564 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004565 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004566 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004567 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004568
4569 if (funcname == NULL)
4570 return NULL;
4571
4572 if (STRNCMP(funcname, "s:", 2) != 0
4573 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004574 {
4575 ufunc_T *ufunc;
4576
4577 // The function name does not have a script-local prefix. Try finding
4578 // it when in a Vim9 script and there is no "g:" prefix.
4579 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4580 return NULL;
4581 ufunc = find_func(funcname, FALSE);
4582 if (ufunc == NULL || func_is_global(ufunc)
4583 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4584 return NULL;
4585 ++p;
4586 off = 0;
4587 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004588 else
4589 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004590
4591 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4592 {
4593 emsg(_(e_using_sid_not_in_script_context));
4594 return NULL;
4595 }
4596 // Expand s: prefix into <SNR>nr_<name>
4597 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4598 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004599 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004600 if (newname == NULL)
4601 return NULL;
4602 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004603 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004604
4605 return newname;
4606}
4607
4608/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004609 * Return script-local "fname" with the 3-byte sequence replaced by
4610 * printable <SNR> in allocated memory.
4611 */
4612 char_u *
4613alloc_printable_func_name(char_u *fname)
4614{
4615 char_u *n = alloc(STRLEN(fname + 3) + 6);
4616
4617 if (n != NULL)
4618 {
4619 STRCPY(n, "<SNR>");
4620 STRCPY(n + 5, fname + 3);
4621 }
4622 return n;
4623}
4624
4625/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004626 * Call trans_function_name(), except that a lambda is returned as-is.
4627 * Returns the name in allocated memory.
4628 */
4629 char_u *
4630save_function_name(
4631 char_u **name,
4632 int *is_global,
4633 int skip,
4634 int flags,
4635 funcdict_T *fudi)
4636{
4637 char_u *p = *name;
4638 char_u *saved;
4639
4640 if (STRNCMP(p, "<lambda>", 8) == 0)
4641 {
4642 p += 8;
4643 (void)getdigits(&p);
4644 saved = vim_strnsave(*name, p - *name);
4645 if (fudi != NULL)
4646 CLEAR_POINTER(fudi);
4647 }
4648 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004649 saved = trans_function_name_ext(&p, is_global, skip,
4650 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004651 *name = p;
4652 return saved;
4653}
4654
4655/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004656 * List functions. When "regmatch" is NULL all of then.
4657 * Otherwise functions matching "regmatch".
4658 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004659 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004660list_functions(regmatch_T *regmatch)
4661{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004662 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004663 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004664 hashitem_T *hi;
4665
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004666 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004667 {
4668 if (!HASHITEM_EMPTY(hi))
4669 {
4670 ufunc_T *fp = HI2UF(hi);
4671
4672 --todo;
4673 if ((fp->uf_flags & FC_DEAD) == 0
4674 && (regmatch == NULL
4675 ? !message_filtered(fp->uf_name)
4676 && !func_name_refcount(fp->uf_name)
Keith Thompson184f71c2024-01-04 21:19:04 +01004677 : !SAFE_isdigit(*fp->uf_name)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004678 && vim_regexec(regmatch, fp->uf_name, 0)))
4679 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004680 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004681 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004682 if (function_list_modified(prev_ht_changed))
4683 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004684 }
4685 }
4686 }
4687}
4688
4689/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004690 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004691 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4692 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004693 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004694 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4695 * With CF_INTERFACE the function is define inside an interface, only the
4696 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004697 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004698 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004699 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004700define_function(
4701 exarg_T *eap,
4702 char_u *name_arg,
4703 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004704 int class_flags,
4705 ocmember_T *obj_members,
4706 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004707{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004708 int j;
4709 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004710 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004711 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004712 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004713 char_u *p;
4714 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004715 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004716 char_u *line_arg = NULL;
4717 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004718 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004719 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004720 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004721 garray_T newlines;
4722 int varargs = FALSE;
4723 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004725 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004726 int fp_allocated = FALSE;
4727 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004728 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004729 dictitem_T *v;
4730 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004731 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004732 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004733 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004734 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004735 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004736 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004737
4738 /*
4739 * ":function" without argument: list functions.
4740 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004741 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004742 {
4743 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004744 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004745 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004746 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004747 }
4748
4749 /*
4750 * ":function /pat": list functions matching pattern.
4751 */
4752 if (*eap->arg == '/')
4753 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004754 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004755 if (!eap->skip)
4756 {
4757 regmatch_T regmatch;
4758
4759 c = *p;
4760 *p = NUL;
4761 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4762 *p = c;
4763 if (regmatch.regprog != NULL)
4764 {
4765 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004766 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004767 vim_regfree(regmatch.regprog);
4768 }
4769 }
4770 if (*p == '/')
4771 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004772 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004773 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004774 }
4775
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776 ga_init(&newargs);
4777 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004778 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779 ga_init(&default_args);
4780
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004781 /*
4782 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004783 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004784 * "name" == func, "fudi.fd_dict" == NULL
4785 * dict.func new dictionary entry
4786 * "name" == NULL, "fudi.fd_dict" set,
4787 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4788 * dict.func existing dict entry with a Funcref
4789 * "name" == func, "fudi.fd_dict" set,
4790 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4791 * dict.func existing dict entry that's not a Funcref
4792 * "name" == NULL, "fudi.fd_dict" set,
4793 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4794 * s:func script-local function name
4795 * g:func global function name, same as "func"
4796 */
4797 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004798 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004799 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004800 // nested function, argument is (args).
4801 paren = TRUE;
4802 CLEAR_FIELD(fudi);
4803 }
4804 else
4805 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004806 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004807 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004808 if (p[0] == 's' && p[1] == ':')
4809 {
4810 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4811 return NULL;
4812 }
4813 p = to_name_end(p, TRUE);
4814 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4815 {
4816 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4817 eap->arg);
4818 return NULL;
4819 }
4820 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004821 }
4822
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004823 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004824 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004825 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004826 paren = (vim_strchr(p, '(') != NULL);
4827 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004828 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004829 /*
4830 * Return on an invalid expression in braces, unless the expression
4831 * evaluation has been cancelled due to an aborting error, an
4832 * interrupt, or an exception.
4833 */
4834 if (!aborting())
4835 {
4836 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004837 semsg(_(e_key_not_present_in_dictionary_str),
4838 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004839 vim_free(fudi.fd_newkey);
4840 return NULL;
4841 }
4842 else
4843 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004844 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004845
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004846 // For "export def FuncName()" in an autoload script the function name
4847 // is stored with the legacy autoload name "dir#script#FuncName" so
4848 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004849 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004850 {
4851 char_u *prefixed = may_prefix_autoload(name);
4852
4853 if (prefixed != NULL && prefixed != name)
4854 {
4855 vim_free(name);
4856 name = prefixed;
4857 }
4858 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004859 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004860 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004861 {
4862 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4863 goto ret_free;
4864 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004865 }
4866
Bram Moolenaare38eab22019-12-05 21:50:01 +01004867 // An error in a function call during evaluation of an expression in magic
4868 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004869 saved_did_emsg = did_emsg;
4870 did_emsg = FALSE;
4871
4872 /*
4873 * ":function func" with only function name: list function.
4874 */
4875 if (!paren)
4876 {
4877 if (!ends_excmd(*skipwhite(p)))
4878 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004879 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004880 goto ret_free;
4881 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004882 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004883 if (eap->nextcmd != NULL)
4884 *p = NUL;
4885 if (!eap->skip && !got_int)
4886 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004887 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004888 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4889 {
4890 char_u *up = untrans_function_name(name);
4891
4892 // With Vim9 script the name was made script-local, if not
4893 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004894 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004895 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004896 }
4897
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004898 if (fp != NULL)
4899 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004900 // Check no function was added or removed from a timer, e.g. at
4901 // the more prompt. "fp" may then be invalid.
4902 int prev_ht_changed = func_hashtab.ht_changed;
4903
4904 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004905 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004906 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4907 {
4908 if (FUNCLINE(fp, j) == NULL)
4909 continue;
4910 msg_putchar('\n');
4911 msg_outnum((long)(j + 1));
4912 if (j < 9)
4913 msg_putchar(' ');
4914 if (j < 99)
4915 msg_putchar(' ');
4916 if (function_list_modified(prev_ht_changed))
4917 break;
4918 msg_prt_line(FUNCLINE(fp, j), FALSE);
4919 out_flush(); // show a line at a time
4920 ui_breakcheck();
4921 }
4922 if (!got_int)
4923 {
4924 msg_putchar('\n');
4925 if (!function_list_modified(prev_ht_changed))
4926 {
4927 if (fp->uf_def_status != UF_NOT_COMPILED)
4928 msg_puts(" enddef");
4929 else
4930 msg_puts(" endfunction");
4931 }
4932 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004933 }
4934 }
4935 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004936 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004937 }
4938 goto ret_free;
4939 }
4940
4941 /*
4942 * ":function name(arg1, arg2)" Define function.
4943 */
4944 p = skipwhite(p);
4945 if (*p != '(')
4946 {
4947 if (!eap->skip)
4948 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004949 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004950 goto ret_free;
4951 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004952 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004953 if (vim_strchr(p, '(') != NULL)
4954 p = vim_strchr(p, '(');
4955 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004956
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004957 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4958 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004959 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004960 goto ret_free;
4961 }
4962
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004963 // In Vim9 script only global functions can be redefined.
4964 if (vim9script && eap->forceit && !is_global)
4965 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004966 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004967 goto ret_free;
4968 }
4969
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004970 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004971
Bram Moolenaar04b12692020-05-04 23:24:44 +02004972 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004973 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004974 // Check the name of the function. Unless it's a dictionary function
4975 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004976 if (name != NULL)
4977 arg = name;
4978 else
4979 arg = fudi.fd_newkey;
4980 if (arg != NULL && (fudi.fd_di == NULL
4981 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4982 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4983 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004984 char_u *name_base = arg;
4985 int i;
4986
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004987 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004988 {
4989 name_base = vim_strchr(arg, '_');
4990 if (name_base == NULL)
4991 name_base = arg + 3;
4992 else
4993 ++name_base;
4994 }
4995 for (i = 0; name_base[i] != NUL && (i == 0
4996 ? eval_isnamec1(name_base[i])
4997 : eval_isnamec(name_base[i])); ++i)
4998 ;
4999 if (name_base[i] != NUL)
zeertzjq6a04bf52024-03-16 09:39:06 +01005000 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005001 emsg_funcname(e_invalid_argument_str, arg);
zeertzjq6a04bf52024-03-16 09:39:06 +01005002 goto ret_free;
5003 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00005004
5005 // In Vim9 script a function cannot have the same name as a
5006 // variable.
5007 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00005008 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
5009 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00005010 + EVAL_VAR_NO_FUNC) == OK)
5011 {
5012 semsg(_(e_redefining_script_item_str), name_base);
5013 goto ret_free;
5014 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005015 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005016 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005017 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005018 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005019 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00005020 goto ret_free;
5021 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005022 }
5023
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005024 // This may get more lines and make the pointers into the first line
5025 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01005026 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005027 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00005028 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02005029 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005030 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00005031 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005032 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01005033 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005034
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005035 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005036 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005037 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005038 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005039 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005040 if (*p != ':')
5041 {
5042 semsg(_(e_no_white_space_allowed_before_colon_str), p);
5043 p = skipwhite(p);
5044 }
5045 else if (!IS_WHITE_OR_NUL(p[1]))
5046 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005047 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02005048 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005049 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005050 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005051 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01005052 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005053 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005054 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005055 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005056 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005057 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02005058 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005059 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005060 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02005061 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005062 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005063 else
5064 // find extra arguments "range", "dict", "abort" and "closure"
5065 for (;;)
5066 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01005067 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068 p = skipwhite(p);
5069 if (STRNCMP(p, "range", 5) == 0)
5070 {
5071 flags |= FC_RANGE;
5072 p += 5;
5073 }
5074 else if (STRNCMP(p, "dict", 4) == 0)
5075 {
5076 flags |= FC_DICT;
5077 p += 4;
5078 }
5079 else if (STRNCMP(p, "abort", 5) == 0)
5080 {
5081 flags |= FC_ABORT;
5082 p += 5;
5083 }
5084 else if (STRNCMP(p, "closure", 7) == 0)
5085 {
5086 flags |= FC_CLOSURE;
5087 p += 7;
5088 if (current_funccal == NULL)
5089 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005090 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005091 name == NULL ? (char_u *)"" : name);
5092 goto erret;
5093 }
5094 }
5095 else
5096 break;
5097 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005098
Bram Moolenaare38eab22019-12-05 21:50:01 +01005099 // When there is a line break use what follows for the function body.
5100 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005101 if (*p == '\n')
5102 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005103 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005104 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5105 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005106 && !(VIM_ISWHITE(*whitep) && *p == '#'
5107 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005108 && !eap->skip
5109 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005110 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005111
5112 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005113 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5114 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005115 */
5116 if (KeyTyped)
5117 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005118 // Check if the function already exists, don't let the user type the
5119 // whole function before telling him it doesn't work! For a script we
5120 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005121 if (!eap->skip && !eap->forceit)
5122 {
5123 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005124 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005125 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005126 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005127 }
5128
5129 if (!eap->skip && did_emsg)
5130 goto erret;
5131
Bram Moolenaare38eab22019-12-05 21:50:01 +01005132 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005133 cmdline_row = msg_row;
5134 }
5135
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005136 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005137 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005138
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005139 // Do not define the function when getting the body fails and when
5140 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005141 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005142 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005143 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5144 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005145 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005146 goto erret;
5147
5148 /*
5149 * If there are no errors, add the function
5150 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005151 if (fudi.fd_dict != NULL)
5152 {
5153 char numbuf[20];
5154
5155 fp = NULL;
5156 if (fudi.fd_newkey == NULL && !eap->forceit)
5157 {
5158 emsg(_(e_dictionary_entry_already_exists));
5159 goto erret;
5160 }
5161 if (fudi.fd_di == NULL)
5162 {
5163 // Can't add a function to a locked dictionary
5164 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5165 goto erret;
5166 }
5167 // Can't change an existing function if it is locked
5168 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5169 goto erret;
5170
5171 // Give the function a sequential number. Can only be used with a
5172 // Funcref!
5173 vim_free(name);
5174 sprintf(numbuf, "%d", ++func_nr);
5175 name = vim_strsave((char_u *)numbuf);
5176 if (name == NULL)
5177 goto erret;
5178 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005179 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005180 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005182 char_u *find_name = name;
5183 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005184 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005185
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005186 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005187 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005188 var_conflict = TRUE;
5189
5190 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5191 {
5192 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5193
5194 if (si->sn_autoload_prefix != NULL)
5195 {
5196 if (is_export)
5197 {
5198 find_name = name + STRLEN(si->sn_autoload_prefix);
5199 v = find_var(find_name, &ht, TRUE);
5200 if (v != NULL)
5201 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005202 // Only check if the function already exists in the script,
5203 // global functions can be shadowed.
5204 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005205 }
5206 else
5207 {
5208 char_u *prefixed = may_prefix_autoload(name);
5209
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005210 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005211 {
5212 v = find_var(prefixed, &ht, TRUE);
5213 if (v != NULL)
5214 var_conflict = TRUE;
5215 vim_free(prefixed);
5216 }
5217 }
5218 }
5219 }
5220 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005221 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005222 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005223 goto erret;
5224 }
5225
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005226 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005227 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005228 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005229 char_u *uname = untrans_function_name(name);
5230
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005231 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005232 }
5233
5234 if (fp != NULL || import != NULL)
5235 {
5236 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005237
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005238 // Function can be replaced with "function!" and when sourcing the
5239 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005240 // A name that is used by an import can not be overruled.
5241 if (import != NULL
5242 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005243 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005244 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005245 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005246 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005247 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005248 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005249 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005250 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005251 goto erret;
5252 }
5253 if (fp->uf_calls > 0)
5254 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005255 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005256 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005257 goto erret;
5258 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005259 if (fp->uf_refcount > 1)
5260 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005261 // This function is referenced somewhere, don't redefine it but
5262 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005263 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005264 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005265 fp = NULL;
5266 overwrite = TRUE;
5267 }
5268 else
5269 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005270 char_u *exp_name = fp->uf_name_exp;
5271
5272 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005273 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005274 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005275 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005276 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005277 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005278#ifdef FEAT_PROFILE
5279 fp->uf_profiling = FALSE;
5280 fp->uf_prof_initialized = FALSE;
5281#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005282 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005283 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005284 }
5285 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005286
5287 if (fp == NULL)
5288 {
5289 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5290 {
5291 int slen, plen;
5292 char_u *scriptname;
5293
Bram Moolenaare38eab22019-12-05 21:50:01 +01005294 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005295 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005296 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005297 {
5298 scriptname = autoload_name(name);
5299 if (scriptname != NULL)
5300 {
5301 p = vim_strchr(scriptname, '/');
5302 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005303 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005304 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005305 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005306 j = OK;
5307 vim_free(scriptname);
5308 }
5309 }
5310 if (j == FAIL)
5311 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005312 linenr_T save_lnum = SOURCING_LNUM;
5313
5314 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005315 semsg(_(e_function_name_does_not_match_script_file_name_str),
5316 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005317 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005318 goto erret;
5319 }
5320 }
5321
Bram Moolenaare01e5212023-01-08 20:31:18 +00005322 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005323 if (fp == NULL)
5324 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005325 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005326
5327 if (fudi.fd_dict != NULL)
5328 {
5329 if (fudi.fd_di == NULL)
5330 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005331 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005332 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5333 if (fudi.fd_di == NULL)
5334 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005335 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005336 goto erret;
5337 }
5338 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5339 {
5340 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005341 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005342 goto erret;
5343 }
5344 }
5345 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005346 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005347 clear_tv(&fudi.fd_di->di_tv);
5348 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005349 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005350
Bram Moolenaare38eab22019-12-05 21:50:01 +01005351 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005352 flags |= FC_DICT;
5353 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005354 }
5355 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005356 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005357 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005358 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005359
5360 if (eap->cmdidx == CMD_def)
5361 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005362 int lnum_save = SOURCING_LNUM;
5363 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005364
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005365 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005366
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005367 // error messages are for the first function line
5368 SOURCING_LNUM = sourcing_lnum_top;
5369
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005370 // The function may use script variables from the context.
5371 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005372
h-eastb895b0f2023-09-24 15:46:31 +02005373 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5374 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005375 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005376 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005377 free_fp = fp_allocated;
5378 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005379 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005380 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005381
5382 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005383 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005384 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005385 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005386 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005387 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005388 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005389 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005390 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005391 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005392 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005393
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005394 if (fp_allocated)
5395 {
5396 // insert the new function in the function list
5397 set_ufunc_name(fp, name);
5398 if (overwrite)
5399 {
5400 hi = hash_find(&func_hashtab, name);
5401 hi->hi_key = UF2HIKEY(fp);
5402 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005403 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005404 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005405 {
5406 free_fp = TRUE;
5407 goto erret;
5408 }
5409 fp->uf_refcount = 1;
5410 }
5411
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005412 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005413 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005414 if ((flags & FC_CLOSURE) != 0)
5415 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005416 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005417 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005418 }
5419 else
5420 fp->uf_scoped = NULL;
5421
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005422#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005423 if (prof_def_func())
5424 func_do_profile(fp);
5425#endif
5426 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005427 if (sandbox)
5428 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005429 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005430 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005431 fp->uf_flags = flags;
5432 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005433 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005434 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005435 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005436 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005437 if (is_export)
5438 {
5439 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005440 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005441 is_export = FALSE;
5442 }
5443
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005444 if (eap->cmdidx == CMD_def)
5445 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005446 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5447 // :func does not use Vim9 script syntax, even in a Vim9 script file
5448 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005449
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005450 goto ret_free;
5451
5452erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005453 if (fp != NULL)
5454 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005455 // these were set to "newargs" and "default_args", which are cleared
5456 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005457 ga_init(&fp->uf_args);
5458 ga_init(&fp->uf_def_args);
5459 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005460errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005461 ga_clear_strings(&newargs);
5462 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005463 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005464 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005465 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005466 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005467 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01005468 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005469 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005470 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005471 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005472ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005473 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005474 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005475 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005476 if (name != name_arg)
5477 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005478 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005479 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005480
5481 return fp;
5482}
5483
5484/*
5485 * ":function"
5486 */
5487 void
5488ex_function(exarg_T *eap)
5489{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005490 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005491
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005492 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005493 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005494 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005495}
5496
5497/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005498 * Find a function by name, including "<lambda>123".
5499 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005500 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005501 * Return NULL if not found.
5502 */
5503 ufunc_T *
5504find_func_by_name(char_u *name, compiletype_T *compile_type)
5505{
5506 char_u *arg = name;
5507 char_u *fname;
5508 ufunc_T *ufunc;
5509 int is_global = FALSE;
5510
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005511 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5512 {
5513 *compile_type = CT_PROFILE;
5514 arg = skipwhite(arg + 7);
5515 }
5516 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5517 {
5518 *compile_type = CT_DEBUG;
5519 arg = skipwhite(arg + 5);
5520 }
5521
5522 if (STRNCMP(arg, "<lambda>", 8) == 0)
5523 {
5524 arg += 8;
5525 (void)getdigits(&arg);
5526 fname = vim_strnsave(name, arg - name);
5527 }
5528 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005529 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005530 // First try finding a method in a class, trans_function_name() will
5531 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005532 ufunc = find_class_func(&arg);
5533 if (ufunc != NULL)
5534 return ufunc;
5535
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005536 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005537 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005538 NULL, NULL, NULL, &ufunc);
5539 if (ufunc != NULL)
5540 {
5541 vim_free(fname);
5542 return ufunc;
5543 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005544 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005545 if (fname == NULL)
5546 {
5547 semsg(_(e_invalid_argument_str), name);
5548 return NULL;
5549 }
5550 if (!ends_excmd2(name, arg))
5551 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005552 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005553 emsg(ex_errmsg(e_trailing_characters_str, arg));
5554 return NULL;
5555 }
5556
5557 ufunc = find_func(fname, is_global);
5558 if (ufunc == NULL)
5559 {
5560 char_u *p = untrans_function_name(fname);
5561
5562 if (p != NULL)
5563 // Try again without making it script-local.
5564 ufunc = find_func(p, FALSE);
5565 }
5566 vim_free(fname);
5567 if (ufunc == NULL)
5568 semsg(_(e_cannot_find_function_str), name);
5569 return ufunc;
5570}
5571
5572/*
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005573 * Compile the :def function "ufunc". If "cl" is not NULL, then compile the
5574 * class or object method "ufunc" in "cl".
5575 */
5576 void
5577defcompile_function(ufunc_T *ufunc, class_T *cl)
5578{
5579 compiletype_T compile_type = CT_NONE;
5580
5581 if (func_needs_compiling(ufunc, compile_type))
5582 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5583 else
5584 smsg(_("Function %s%s%s does not need compiling"),
5585 cl != NULL ? cl->class_name : (char_u *)"",
5586 cl != NULL ? (char_u *)"." : (char_u *)"",
5587 ufunc->uf_name);
5588}
5589
5590/*
5591 * Compile all the :def functions defined in the current script
5592 */
5593 static void
5594defcompile_funcs_in_script(void)
5595{
5596 long todo = (long)func_hashtab.ht_used;
5597 int changed = func_hashtab.ht_changed;
5598 hashitem_T *hi;
5599
5600 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5601 {
5602 if (!HASHITEM_EMPTY(hi))
5603 {
5604 --todo;
5605 ufunc_T *ufunc = HI2UF(hi);
5606 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5607 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5608 && (ufunc->uf_flags & FC_DEAD) == 0)
5609 {
5610 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5611
5612 if (func_hashtab.ht_changed != changed)
5613 {
5614 // a function has been added or removed, need to start
5615 // over
5616 todo = (long)func_hashtab.ht_used;
5617 changed = func_hashtab.ht_changed;
5618 hi = func_hashtab.ht_array;
5619 --hi;
5620 }
5621 }
5622 }
5623 }
5624}
5625
5626/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005627 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005628 * be compiled or the one specified by the argument.
5629 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005630 */
5631 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005632ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005633{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005634 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005635 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005636 typval_T tv;
5637
5638 if (is_class_name(eap->arg, &tv))
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005639 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005640 class_T *cl = tv.vval.v_class;
5641
5642 if (cl != NULL)
5643 defcompile_class(cl);
5644 }
5645 else
5646 {
5647 compiletype_T compile_type = CT_NONE;
5648 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
5649 if (ufunc != NULL)
5650 defcompile_function(ufunc, NULL);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005651 }
5652 }
5653 else
5654 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005655 defcompile_funcs_in_script();
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005656
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005657 // compile all the class defined in the current script
5658 defcompile_classes_in_script();
Bram Moolenaar822ba242020-05-24 23:00:18 +02005659 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005660}
5661
5662/*
5663 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5664 * Return 2 if "p" starts with "s:".
5665 * Return 0 otherwise.
5666 */
5667 int
5668eval_fname_script(char_u *p)
5669{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005670 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5671 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005672 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5673 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5674 return 5;
5675 if (p[0] == 's' && p[1] == ':')
5676 return 2;
5677 return 0;
5678}
5679
5680 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005681translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005682{
5683 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005684 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005685 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005686}
5687
5688/*
5689 * Return TRUE when "ufunc" has old-style "..." varargs
5690 * or named varargs "...name: type".
5691 */
5692 int
5693has_varargs(ufunc_T *ufunc)
5694{
5695 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005696}
5697
5698/*
5699 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005700 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005701 */
5702 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005703function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005704{
5705 char_u *nm = name;
5706 char_u *p;
5707 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005708 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005709 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005710
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005711 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5712 if (no_deref)
5713 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005714 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005715 nm = skipwhite(nm);
5716
Bram Moolenaare38eab22019-12-05 21:50:01 +01005717 // Only accept "funcname", "funcname ", "funcname (..." and
5718 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005719 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005720 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005721 vim_free(p);
5722 return n;
5723}
5724
Bram Moolenaar113e1072019-01-20 15:30:40 +01005725#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005726 char_u *
5727get_expanded_name(char_u *name, int check)
5728{
5729 char_u *nm = name;
5730 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005731 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005732
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005733 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005734
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005735 if (p != NULL && *nm == NUL
5736 && (!check || translated_function_exists(p, is_global)))
5737 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005738
5739 vim_free(p);
5740 return NULL;
5741}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005742#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005743
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005744/*
5745 * Function given to ExpandGeneric() to obtain the list of user defined
5746 * function names.
5747 */
5748 char_u *
5749get_user_func_name(expand_T *xp, int idx)
5750{
5751 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005752 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005753 static hashitem_T *hi;
5754 ufunc_T *fp;
5755
5756 if (idx == 0)
5757 {
5758 done = 0;
5759 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005760 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005761 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005762 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005763 {
5764 if (done++ > 0)
5765 ++hi;
5766 while (HASHITEM_EMPTY(hi))
5767 ++hi;
5768 fp = HI2UF(hi);
5769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005770 // don't show dead, dict and lambda functions
5771 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005772 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005773 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005774
5775 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005776 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005777
5778 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005779 if (xp->xp_context != EXPAND_USER_FUNC
5780 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005781 {
5782 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005783 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005784 STRCAT(IObuff, ")");
5785 }
5786 return IObuff;
5787 }
5788 return NULL;
5789}
5790
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005791/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005792 * Make a copy of a function.
5793 * Intended to be used for a function defined on a base class that has a copy
5794 * on the child class.
5795 * The copy has uf_refcount set to one.
5796 * Returns NULL when out of memory.
5797 */
5798 ufunc_T *
5799copy_function(ufunc_T *fp)
5800{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005801 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005802 if (ufunc == NULL)
5803 return NULL;
5804
5805 // Most things can just be copied.
5806 *ufunc = *fp;
5807
5808 ufunc->uf_def_status = UF_TO_BE_COMPILED;
5809 ufunc->uf_dfunc_idx = 0;
5810 ufunc->uf_class = NULL;
5811
5812 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
5813 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
5814
5815 if (ufunc->uf_arg_types != NULL)
5816 {
5817 // "uf_arg_types" is an allocated array, make a copy.
5818 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
5819 if (at != NULL)
5820 {
5821 mch_memmove(at, ufunc->uf_arg_types,
5822 sizeof(type_T *) * ufunc->uf_args.ga_len);
5823 ufunc->uf_arg_types = at;
5824 }
5825 }
5826
5827 // TODO: how about the types themselves? they can be freed when the
5828 // original function is freed:
5829 // type_T **uf_arg_types;
5830 // type_T *uf_ret_type;
5831
Ernie Rael114ec812023-06-04 18:11:35 +01005832 // make uf_type_list empty
5833 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00005834
5835 // TODO: partial_T *uf_partial;
5836
5837 if (ufunc->uf_va_name != NULL)
5838 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
5839
5840 // TODO:
5841 // type_T *uf_va_type;
5842 // type_T *uf_func_type;
5843
5844 ufunc->uf_block_depth = 0;
5845 ufunc->uf_block_ids = NULL;
5846
5847 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
5848
5849 ufunc->uf_refcount = 1;
5850 ufunc->uf_name_exp = NULL;
5851 STRCPY(ufunc->uf_name, fp->uf_name);
5852
5853 return ufunc;
5854}
5855
5856/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005857 * ":delfunction {name}"
5858 */
5859 void
5860ex_delfunction(exarg_T *eap)
5861{
5862 ufunc_T *fp = NULL;
5863 char_u *p;
5864 char_u *name;
5865 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005866 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005867
5868 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005869 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
5870 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005871 vim_free(fudi.fd_newkey);
5872 if (name == NULL)
5873 {
5874 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005875 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005876 return;
5877 }
5878 if (!ends_excmd(*skipwhite(p)))
5879 {
5880 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005881 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005882 return;
5883 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005884 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005885 if (eap->nextcmd != NULL)
5886 *p = NUL;
5887
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005888 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005889 {
5890 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005891 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005892 vim_free(name);
5893 return;
5894 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005895 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005896 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005897 vim_free(name);
5898
5899 if (!eap->skip)
5900 {
5901 if (fp == NULL)
5902 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005903 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005904 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005905 return;
5906 }
5907 if (fp->uf_calls > 0)
5908 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005909 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005910 return;
5911 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005912 if (fp->uf_flags & FC_VIM9)
5913 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005914 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005915 return;
5916 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005917
5918 if (fudi.fd_dict != NULL)
5919 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005920 // Delete the dict item that refers to the function, it will
5921 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00005922 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005923 }
5924 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005925 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005926 // A normal function (not a numbered function or lambda) has a
5927 // refcount of 1 for the entry in the hashtable. When deleting
5928 // it and the refcount is more than one, it should be kept.
5929 // A numbered function and lambda should be kept if the refcount is
5930 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005931 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005932 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005933 // Function is still referenced somewhere. Don't free it but
5934 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005935 if (func_remove(fp))
5936 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005937 }
5938 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005939 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005940 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005941 }
5942}
5943
5944/*
5945 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005946 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005947 */
5948 void
5949func_unref(char_u *name)
5950{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005951 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005952
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005953 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005954 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005955 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005956 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005957 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005958#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005959 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005960#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005961 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005962 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005963 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005964}
5965
5966/*
5967 * Unreference a Function: decrement the reference count and free it when it
5968 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005969 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005970 */
5971 void
5972func_ptr_unref(ufunc_T *fp)
5973{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005974 if (fp != NULL && (--fp->uf_refcount <= 0
5975 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5976 && fp->uf_partial->pt_refcount <= 1
5977 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005978 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005979 // Only delete it when it's not being used. Otherwise it's done
5980 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005981 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005982 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005983 }
5984}
5985
5986/*
5987 * Count a reference to a Function.
5988 */
5989 void
5990func_ref(char_u *name)
5991{
5992 ufunc_T *fp;
5993
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005994 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005995 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005996 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005997 if (fp != NULL)
5998 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005999 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01006000 // Only give an error for a numbered function.
6001 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01006002 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006003}
6004
6005/*
6006 * Count a reference to a Function.
6007 */
6008 void
6009func_ptr_ref(ufunc_T *fp)
6010{
6011 if (fp != NULL)
6012 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006013}
6014
6015/*
6016 * Return TRUE if items in "fc" do not have "copyID". That means they are not
6017 * referenced from anywhere that is in use.
6018 */
6019 static int
6020can_free_funccal(funccall_T *fc, int copyID)
6021{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006022 return (fc->fc_l_varlist.lv_copyID != copyID
6023 && fc->fc_l_vars.dv_copyID != copyID
6024 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006025 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006026}
6027
6028/*
6029 * ":return [expr]"
6030 */
6031 void
6032ex_return(exarg_T *eap)
6033{
6034 char_u *arg = eap->arg;
6035 typval_T rettv;
6036 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006037 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006038
6039 if (current_funccal == NULL)
6040 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006041 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006042 return;
6043 }
6044
Bram Moolenaar844fb642021-10-23 13:32:30 +01006045 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006046 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
6047
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006048 if (eap->skip)
6049 ++emsg_skip;
6050
6051 eap->nextcmd = NULL;
6052 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006053 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006054 {
6055 if (!eap->skip)
6056 returning = do_return(eap, FALSE, TRUE, &rettv);
6057 else
6058 clear_tv(&rettv);
6059 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01006060 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006061 else if (!eap->skip)
6062 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006063 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01006064 update_force_abort();
6065
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006066 /*
6067 * Return unless the expression evaluation has been cancelled due to an
6068 * aborting error, an interrupt, or an exception.
6069 */
6070 if (!aborting())
6071 returning = do_return(eap, FALSE, TRUE, NULL);
6072 }
6073
Bram Moolenaare38eab22019-12-05 21:50:01 +01006074 // When skipping or the return gets pending, advance to the next command
6075 // in this line (!returning). Otherwise, ignore the rest of the line.
6076 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006077 if (returning)
6078 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006079 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02006080 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006081
6082 if (eap->skip)
6083 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02006084 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006085}
6086
zeertzjq5299c092023-04-12 20:48:16 +01006087/*
6088 * Lower level implementation of "call". Only called when not skipping.
6089 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006090 static int
6091ex_call_inner(
6092 exarg_T *eap,
6093 char_u *name,
6094 char_u **arg,
6095 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006096 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006097 evalarg_T *evalarg)
6098{
6099 linenr_T lnum;
6100 int doesrange;
6101 typval_T rettv;
6102 int failed = FALSE;
6103
zeertzjq5299c092023-04-12 20:48:16 +01006104 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006105 for ( ; lnum <= eap->line2; ++lnum)
6106 {
6107 funcexe_T funcexe;
6108
zeertzjq5299c092023-04-12 20:48:16 +01006109 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006110 {
6111 if (lnum > curbuf->b_ml.ml_line_count)
6112 {
6113 // If the function deleted lines or switched to another buffer
6114 // the line number may become invalid.
6115 emsg(_(e_invalid_range));
6116 break;
6117 }
6118 curwin->w_cursor.lnum = lnum;
6119 curwin->w_cursor.col = 0;
6120 curwin->w_cursor.coladd = 0;
6121 }
6122 *arg = startarg;
6123
6124 funcexe = *funcexe_init;
6125 funcexe.fe_doesrange = &doesrange;
6126 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6127 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6128 {
6129 failed = TRUE;
6130 break;
6131 }
6132 if (has_watchexpr())
6133 dbg_check_breakpoint(eap);
6134
6135 // Handle a function returning a Funcref, Dictionary or List.
6136 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006137 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006138 {
6139 failed = TRUE;
6140 break;
6141 }
6142
6143 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006144 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006145 break;
6146
6147 // Stop when immediately aborting on error, or when an interrupt
6148 // occurred or an exception was thrown but not caught.
6149 // get_func_tv() returned OK, so that the check for trailing
6150 // characters below is executed.
6151 if (aborting())
6152 break;
6153 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006154 return failed;
6155}
6156
6157/*
6158 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6159 * Returns FAIL or OK.
6160 */
6161 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006162ex_defer_inner(
6163 char_u *name,
6164 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006165 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006166 partial_T *partial,
6167 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006168{
6169 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006170 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006171 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006172 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006173
6174 if (current_funccal == NULL)
6175 {
6176 semsg(_(e_str_not_inside_function), "defer");
6177 return FAIL;
6178 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006179 if (partial != NULL)
6180 {
6181 if (partial->pt_dict != NULL)
6182 {
6183 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6184 return FAIL;
6185 }
6186 if (partial->pt_argc > 0)
6187 {
6188 int i;
6189
6190 partial_argc = partial->pt_argc;
6191 for (i = 0; i < partial_argc; ++i)
6192 copy_tv(&partial->pt_argv[i], &argvars[i]);
6193 }
6194 }
Ernie Raelb077b582023-12-14 20:11:44 +01006195 int is_builtin = builtin_function(name, -1);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006196 r = get_func_arguments(arg, evalarg, FALSE,
Ernie Raelb077b582023-12-14 20:11:44 +01006197 argvars + partial_argc, &argcount, is_builtin);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006198 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006199
6200 if (r == OK)
6201 {
6202 if (type != NULL)
6203 {
6204 // Check that the arguments are OK for the types of the funcref.
6205 r = check_argument_types(type, argvars, argcount, NULL, name);
6206 }
Ernie Raelb077b582023-12-14 20:11:44 +01006207 else if (is_builtin)
Bram Moolenaar16900322022-09-08 19:51:45 +01006208 {
6209 int idx = find_internal_func(name);
6210
6211 if (idx < 0)
6212 {
6213 emsg_funcname(e_unknown_function_str, name);
6214 r = FAIL;
6215 }
6216 else if (check_internal_func(idx, argcount) == -1)
6217 r = FAIL;
6218 }
6219 else
6220 {
6221 ufunc_T *ufunc = find_func(name, FALSE);
6222
6223 // we tolerate an unknown function here, it might be defined later
6224 if (ufunc != NULL)
6225 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006226 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006227 if (error != FCERR_UNKNOWN)
6228 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006229 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006230 r = FAIL;
6231 }
6232 }
6233 }
6234 }
6235
Bram Moolenaar86d87252022-09-05 21:21:25 +01006236 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006237 {
6238 while (--argcount >= 0)
6239 clear_tv(&argvars[argcount]);
6240 return FAIL;
6241 }
6242 return add_defer(name, argcount, argvars);
6243}
6244
6245/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006246 * Return TRUE if currently inside a function call.
6247 * Give an error message and return FALSE when not.
6248 */
6249 int
6250can_add_defer(void)
6251{
6252 if (!in_def_function() && get_current_funccal() == NULL)
6253 {
6254 semsg(_(e_str_not_inside_function), "defer");
6255 return FALSE;
6256 }
6257 return TRUE;
6258}
6259
6260/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006261 * Add a deferred call for "name" with arguments "argvars[argcount]".
6262 * Consumes "argvars[]".
6263 * Caller must check that in_def_function() returns TRUE or current_funccal is
6264 * not NULL.
6265 * Returns OK or FAIL.
6266 */
6267 int
6268add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6269{
6270 char_u *saved_name = vim_strsave(name);
6271 int argcount = argcount_arg;
6272 defer_T *dr;
6273 int ret = FAIL;
6274
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006275 if (saved_name == NULL)
6276 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006277 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006278 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006279 if (add_defer_function(saved_name, argcount, argvars) == OK)
6280 argcount = 0;
6281 }
6282 else
6283 {
6284 if (current_funccal->fc_defer.ga_itemsize == 0)
6285 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6286 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6287 goto theend;
6288 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6289 + current_funccal->fc_defer.ga_len++;
6290 dr->dr_name = saved_name;
6291 dr->dr_argcount = argcount;
6292 while (argcount > 0)
6293 {
6294 --argcount;
6295 dr->dr_argvars[argcount] = argvars[argcount];
6296 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006297 }
6298 ret = OK;
6299
6300theend:
6301 while (--argcount >= 0)
6302 clear_tv(&argvars[argcount]);
6303 return ret;
6304}
6305
6306/*
6307 * Invoked after a functions has finished: invoke ":defer" functions.
6308 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006309 static void
6310handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006311{
6312 int idx;
6313
Bram Moolenaar58779852022-09-06 18:31:14 +01006314 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006315 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006316 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006317
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006318 if (dr->dr_name == NULL)
6319 // already being called, can happen if function does ":qa"
6320 continue;
6321
6322 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006323 CLEAR_FIELD(funcexe);
6324 funcexe.fe_evaluate = TRUE;
6325
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006326 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006327 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006328
6329 char_u *name = dr->dr_name;
6330 dr->dr_name = NULL;
6331
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006332 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006333 // first statement in the function will be executed (because of the
6334 // exception). So save and restore the try/catch/throw exception
6335 // state.
6336 exception_state_T estate;
6337 exception_state_save(&estate);
6338 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006339
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006340 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6341
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006342 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006343
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006344 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006345 vim_free(name);
6346 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006347 clear_tv(&dr->dr_argvars[i]);
6348 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006349 ga_clear(&funccal->fc_defer);
6350}
6351
zeertzjq960cf912023-04-18 21:52:54 +01006352 static void
6353invoke_funccall_defer(funccall_T *fc)
6354{
6355 if (fc->fc_ectx != NULL)
6356 {
6357 // :def function
6358 unwind_def_callstack(fc->fc_ectx);
6359 may_invoke_defer_funcs(fc->fc_ectx);
6360 }
6361 else
6362 {
6363 // legacy function
6364 handle_defer_one(fc);
6365 }
6366}
6367
Bram Moolenaar58779852022-09-06 18:31:14 +01006368/*
6369 * Called when exiting: call all defer functions.
6370 */
6371 void
6372invoke_all_defer(void)
6373{
zeertzjq1be4b812023-04-19 14:21:24 +01006374 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6375 invoke_funccall_defer(fc);
6376
zeertzjq960cf912023-04-18 21:52:54 +01006377 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6378 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6379 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006380}
6381
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006382/*
6383 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006384 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006385 */
6386 void
6387ex_call(exarg_T *eap)
6388{
6389 char_u *arg = eap->arg;
6390 char_u *startarg;
6391 char_u *name;
6392 char_u *tofree;
6393 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006394 int failed = FALSE;
6395 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006396 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006397 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006398 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006399 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006400 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006401 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006402
Bram Moolenaare6b53242020-07-01 17:28:33 +02006403 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006404 if (eap->skip)
6405 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006406 typval_T rettv;
6407
Bram Moolenaare38eab22019-12-05 21:50:01 +01006408 // trans_function_name() doesn't work well when skipping, use eval0()
6409 // instead to skip to any following command, e.g. for:
6410 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006411 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006412 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006413 clear_tv(&rettv);
6414 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006415 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006416 return;
6417 }
6418
zeertzjq5299c092023-04-12 20:48:16 +01006419 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006420 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006421 if (fudi.fd_newkey != NULL)
6422 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006423 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006424 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006425 vim_free(fudi.fd_newkey);
6426 }
6427 if (tofree == NULL)
6428 return;
6429
Bram Moolenaare38eab22019-12-05 21:50:01 +01006430 // Increase refcount on dictionary, it could get deleted when evaluating
6431 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006432 if (fudi.fd_dict != NULL)
6433 ++fudi.fd_dict->dv_refcount;
6434
Bram Moolenaare38eab22019-12-05 21:50:01 +01006435 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6436 // contents. For VAR_PARTIAL get its partial, unless we already have one
6437 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006438 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006439 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006440 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006441 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006442
Bram Moolenaare38eab22019-12-05 21:50:01 +01006443 // Skip white space to allow ":call func ()". Not good, but required for
6444 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006445 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006446 if (*startarg != '(')
6447 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006448 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006449 goto end;
6450 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006451 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006452 {
6453 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6454 goto end;
6455 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006456
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006457 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006458 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006459 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006460 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006461 }
6462 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006463 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006464 funcexe_T funcexe;
6465
Bram Moolenaara80faa82020-04-12 19:37:17 +02006466 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006467 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006468 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006469 funcexe.fe_partial = partial;
6470 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006471 funcexe.fe_firstline = eap->line1;
6472 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006473 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006474 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006475 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006476 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006477
Bram Moolenaar1d341892021-09-18 15:25:52 +02006478 // When inside :try we need to check for following "| catch" or "| endtry".
6479 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006480 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006481 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006482 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006483 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006484 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006485 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006486 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006487 {
6488 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006489 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006490 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006491 }
6492 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006493 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006494 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006495 // Must be after using "arg", it may point into memory cleared here.
6496 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006497
6498end:
6499 dict_unref(fudi.fd_dict);
6500 vim_free(tofree);
6501}
6502
6503/*
6504 * Return from a function. Possibly makes the return pending. Also called
6505 * for a pending return at the ":endtry" or after returning from an extra
6506 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6507 * when called due to a ":return" command. "rettv" may point to a typval_T
6508 * with the return rettv. Returns TRUE when the return can be carried out,
6509 * FALSE when the return gets pending.
6510 */
6511 int
6512do_return(
6513 exarg_T *eap,
6514 int reanimate,
6515 int is_cmd,
6516 void *rettv)
6517{
6518 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006519 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006520
6521 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006522 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006523 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006524
6525 /*
6526 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6527 * not in its finally clause (which then is to be executed next) is found.
6528 * In this case, make the ":return" pending for execution at the ":endtry".
6529 * Otherwise, return normally.
6530 */
6531 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6532 if (idx >= 0)
6533 {
6534 cstack->cs_pending[idx] = CSTP_RETURN;
6535
6536 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006537 // A pending return again gets pending. "rettv" points to an
6538 // allocated variable with the rettv of the original ":return"'s
6539 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006540 cstack->cs_rettv[idx] = rettv;
6541 else
6542 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006543 // When undoing a return in order to make it pending, get the stored
6544 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006545 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006546 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006547
6548 if (rettv != NULL)
6549 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006550 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006551 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6552 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6553 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006554 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006555 }
6556 else
6557 cstack->cs_rettv[idx] = NULL;
6558
6559 if (reanimate)
6560 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006561 // The pending return value could be overwritten by a ":return"
6562 // without argument in a finally clause; reset the default
6563 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006564 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6565 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006566 }
6567 }
6568 report_make_pending(CSTP_RETURN, rettv);
6569 }
6570 else
6571 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006572 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006573
Bram Moolenaare38eab22019-12-05 21:50:01 +01006574 // If the return is carried out now, store the return value. For
6575 // a return immediately after reanimation, the value is already
6576 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006577 if (!reanimate && rettv != NULL)
6578 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006579 clear_tv(current_funccal->fc_rettv);
6580 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006581 if (!is_cmd)
6582 vim_free(rettv);
6583 }
6584 }
6585
6586 return idx < 0;
6587}
6588
6589/*
6590 * Free the variable with a pending return value.
6591 */
6592 void
6593discard_pending_return(void *rettv)
6594{
6595 free_tv((typval_T *)rettv);
6596}
6597
6598/*
6599 * Generate a return command for producing the value of "rettv". The result
6600 * is an allocated string. Used by report_pending() for verbose messages.
6601 */
6602 char_u *
6603get_return_cmd(void *rettv)
6604{
6605 char_u *s = NULL;
6606 char_u *tofree = NULL;
6607 char_u numbuf[NUMBUFLEN];
6608
6609 if (rettv != NULL)
6610 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6611 if (s == NULL)
6612 s = (char_u *)"";
6613
6614 STRCPY(IObuff, ":return ");
6615 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6616 if (STRLEN(s) + 8 >= IOSIZE)
6617 STRCPY(IObuff + IOSIZE - 4, "...");
6618 vim_free(tofree);
6619 return vim_strsave(IObuff);
6620}
6621
6622/*
6623 * Get next function line.
6624 * Called by do_cmdline() to get the next line.
6625 * Returns allocated string, or NULL for end of function.
6626 */
6627 char_u *
6628get_func_line(
6629 int c UNUSED,
6630 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006631 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006632 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006633{
6634 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006635 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006636 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006637 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006638
Bram Moolenaare38eab22019-12-05 21:50:01 +01006639 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006640 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006641 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006642 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006643 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006644 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006645 }
6646#ifdef FEAT_PROFILE
6647 if (do_profiling == PROF_YES)
6648 func_line_end(cookie);
6649#endif
6650
6651 gap = &fp->uf_lines;
6652 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006653 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006654 retval = NULL;
6655 else
6656 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006657 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006658 while (fcp->fc_linenr < gap->ga_len
6659 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6660 ++fcp->fc_linenr;
6661 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006662 retval = NULL;
6663 else
6664 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006665 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6666 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006667#ifdef FEAT_PROFILE
6668 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006669 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006670#endif
6671 }
6672 }
6673
Bram Moolenaare38eab22019-12-05 21:50:01 +01006674 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006675 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006676 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006677 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006678 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006679 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006680 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006681 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006682 }
6683
6684 return retval;
6685}
6686
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006687/*
6688 * Return TRUE if the currently active function should be ended, because a
6689 * return was encountered or an error occurred. Used inside a ":while".
6690 */
6691 int
6692func_has_ended(void *cookie)
6693{
6694 funccall_T *fcp = (funccall_T *)cookie;
6695
Bram Moolenaare38eab22019-12-05 21:50:01 +01006696 // Ignore the "abort" flag if the abortion behavior has been changed due to
6697 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006698 return (((fcp->fc_func->uf_flags & FC_ABORT)
6699 && did_emsg && !aborted_in_try())
6700 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006701}
6702
6703/*
6704 * return TRUE if cookie indicates a function which "abort"s on errors.
6705 */
6706 int
6707func_has_abort(
6708 void *cookie)
6709{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006710 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006711}
6712
6713
6714/*
6715 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6716 * Don't do this when "Func" is already a partial that was bound
6717 * explicitly (pt_auto is FALSE).
6718 * Changes "rettv" in-place.
6719 * Returns the updated "selfdict_in".
6720 */
6721 dict_T *
6722make_partial(dict_T *selfdict_in, typval_T *rettv)
6723{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006724 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006725 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006726 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006727 dict_T *selfdict = selfdict_in;
6728
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006729 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6730 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006731 fp = rettv->vval.v_partial->pt_func;
6732 else
6733 {
6734 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006735 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006736 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006737 if (fname == NULL)
6738 {
6739 // There is no point binding a dict to a NULL function, just create
6740 // a function reference.
6741 rettv->v_type = VAR_FUNC;
6742 rettv->vval.v_string = NULL;
6743 }
6744 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006745 {
6746 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006747 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006748
6749 // Translate "s:func" to the stored function name.
6750 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6751 fp = find_func(fname, FALSE);
6752 vim_free(tofree);
6753 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006754 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006755
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006756 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006757 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006758 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006759
6760 if (pt != NULL)
6761 {
6762 pt->pt_refcount = 1;
6763 pt->pt_dict = selfdict;
6764 pt->pt_auto = TRUE;
6765 selfdict = NULL;
6766 if (rettv->v_type == VAR_FUNC)
6767 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006768 // Just a function: Take over the function name and use
6769 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006770 pt->pt_name = rettv->vval.v_string;
6771 }
6772 else
6773 {
6774 partial_T *ret_pt = rettv->vval.v_partial;
6775 int i;
6776
Bram Moolenaare38eab22019-12-05 21:50:01 +01006777 // Partial: copy the function name, use selfdict and copy
6778 // args. Can't take over name or args, the partial might
6779 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006780 if (ret_pt->pt_name != NULL)
6781 {
6782 pt->pt_name = vim_strsave(ret_pt->pt_name);
6783 func_ref(pt->pt_name);
6784 }
6785 else
6786 {
6787 pt->pt_func = ret_pt->pt_func;
6788 func_ptr_ref(pt->pt_func);
6789 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006790 if (ret_pt->pt_argc > 0)
6791 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006792 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006793 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006794 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006795 pt->pt_argc = 0;
6796 else
6797 {
6798 pt->pt_argc = ret_pt->pt_argc;
6799 for (i = 0; i < pt->pt_argc; i++)
6800 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6801 }
6802 }
6803 partial_unref(ret_pt);
6804 }
6805 rettv->v_type = VAR_PARTIAL;
6806 rettv->vval.v_partial = pt;
6807 }
6808 }
6809 return selfdict;
6810}
6811
6812/*
6813 * Return the name of the executed function.
6814 */
6815 char_u *
6816func_name(void *cookie)
6817{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006818 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006819}
6820
6821/*
6822 * Return the address holding the next breakpoint line for a funccall cookie.
6823 */
6824 linenr_T *
6825func_breakpoint(void *cookie)
6826{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006827 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006828}
6829
6830/*
6831 * Return the address holding the debug tick for a funccall cookie.
6832 */
6833 int *
6834func_dbg_tick(void *cookie)
6835{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006836 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006837}
6838
6839/*
6840 * Return the nesting level for a funccall cookie.
6841 */
6842 int
6843func_level(void *cookie)
6844{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006845 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006846}
6847
6848/*
6849 * Return TRUE when a function was ended by a ":return" command.
6850 */
6851 int
6852current_func_returned(void)
6853{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006854 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006855}
6856
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006857 int
6858free_unref_funccal(int copyID, int testing)
6859{
6860 int did_free = FALSE;
6861 int did_free_funccal = FALSE;
6862 funccall_T *fc, **pfc;
6863
6864 for (pfc = &previous_funccal; *pfc != NULL; )
6865 {
6866 if (can_free_funccal(*pfc, copyID))
6867 {
6868 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006869 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006870 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006871 did_free = TRUE;
6872 did_free_funccal = TRUE;
6873 }
6874 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006875 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006876 }
6877 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006878 // When a funccal was freed some more items might be garbage
6879 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006880 (void)garbage_collect(testing);
6881
6882 return did_free;
6883}
6884
6885/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006886 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006887 */
6888 static funccall_T *
6889get_funccal(void)
6890{
6891 int i;
6892 funccall_T *funccal;
6893 funccall_T *temp_funccal;
6894
6895 funccal = current_funccal;
6896 if (debug_backtrace_level > 0)
6897 {
6898 for (i = 0; i < debug_backtrace_level; i++)
6899 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006900 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006901 if (temp_funccal)
6902 funccal = temp_funccal;
6903 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006904 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006905 debug_backtrace_level = i;
6906 }
6907 }
6908 return funccal;
6909}
6910
6911/*
6912 * Return the hashtable used for local variables in the current funccal.
6913 * Return NULL if there is no current funccal.
6914 */
6915 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006916get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006917{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006918 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006919 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006920 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006921}
6922
6923/*
6924 * Return the l: scope variable.
6925 * Return NULL if there is no current funccal.
6926 */
6927 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006928get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006929{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006930 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006931 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006932 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006933}
6934
6935/*
6936 * Return the hashtable used for argument in the current funccal.
6937 * Return NULL if there is no current funccal.
6938 */
6939 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006940get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006941{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006942 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006943 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006944 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006945}
6946
6947/*
6948 * Return the a: scope variable.
6949 * Return NULL if there is no current funccal.
6950 */
6951 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006952get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006953{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006954 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006955 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006956 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006957}
6958
6959/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006960 * List function variables, if there is a function.
6961 */
6962 void
6963list_func_vars(int *first)
6964{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006965 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
6966 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006967 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006968}
6969
6970/*
6971 * If "ht" is the hashtable for local variables in the current funccal, return
6972 * the dict that contains it.
6973 * Otherwise return NULL.
6974 */
6975 dict_T *
6976get_current_funccal_dict(hashtab_T *ht)
6977{
6978 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01006979 && ht == &current_funccal->fc_l_vars.dv_hashtab)
6980 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006981 return NULL;
6982}
6983
6984/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006985 * Search hashitem in parent scope.
6986 */
6987 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006988find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006989{
6990 funccall_T *old_current_funccal = current_funccal;
6991 hashtab_T *ht;
6992 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006993 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006994
Bram Moolenaarca16c602022-09-06 18:57:08 +01006995 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006996 return NULL;
6997
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006998 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006999 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02007000 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007001 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007002 ht = find_var_ht(name, &varname);
7003 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02007004 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007005 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02007006 if (!HASHITEM_EMPTY(hi))
7007 {
7008 *pht = ht;
7009 break;
7010 }
7011 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007012 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02007013 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007014 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007015 }
7016 current_funccal = old_current_funccal;
7017
7018 return hi;
7019}
7020
7021/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007022 * Search variable in parent scope.
7023 */
7024 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007025find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007026{
7027 dictitem_T *v = NULL;
7028 funccall_T *old_current_funccal = current_funccal;
7029 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007030 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007031
Bram Moolenaarca16c602022-09-06 18:57:08 +01007032 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007033 return NULL;
7034
Bram Moolenaare38eab22019-12-05 21:50:01 +01007035 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01007036 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007037 while (current_funccal)
7038 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007039 ht = find_var_ht(name, &varname);
7040 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007041 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007042 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007043 if (v != NULL)
7044 break;
7045 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007046 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007047 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007048 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007049 }
7050 current_funccal = old_current_funccal;
7051
7052 return v;
7053}
7054
7055/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007056 * Set "copyID + 1" in previous_funccal and callers.
7057 */
7058 int
7059set_ref_in_previous_funccal(int copyID)
7060{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007061 funccall_T *fc;
7062
Bram Moolenaarca16c602022-09-06 18:57:08 +01007063 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007064 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007065 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007066 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
7067 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
7068 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007069 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007070 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007071 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007072}
7073
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007074 static int
7075set_ref_in_funccal(funccall_T *fc, int copyID)
7076{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007077 if (fc->fc_copyID != copyID)
7078 {
7079 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007080 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
7081 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
7082 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
7083 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007084 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007085 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007086 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007087}
7088
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007089/*
7090 * Set "copyID" in all local vars and arguments in the call stack.
7091 */
7092 int
7093set_ref_in_call_stack(int copyID)
7094{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007095 funccall_T *fc;
7096 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007097
Bram Moolenaarca16c602022-09-06 18:57:08 +01007098 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007099 if (set_ref_in_funccal(fc, copyID))
7100 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007101
7102 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007103 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007104 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007105 if (set_ref_in_funccal(fc, copyID))
7106 return TRUE;
7107 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007108}
7109
7110/*
7111 * Set "copyID" in all functions available by name.
7112 */
7113 int
7114set_ref_in_functions(int copyID)
7115{
7116 int todo;
7117 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007118 ufunc_T *fp;
7119
7120 todo = (int)func_hashtab.ht_used;
7121 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007122 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007123 if (!HASHITEM_EMPTY(hi))
7124 {
7125 --todo;
7126 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007127 if (!func_name_refcount(fp->uf_name)
7128 && set_ref_in_func(NULL, fp, copyID))
7129 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007130 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007131 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007132 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007133}
7134
7135/*
7136 * Set "copyID" in all function arguments.
7137 */
7138 int
7139set_ref_in_func_args(int copyID)
7140{
7141 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007142
7143 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007144 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7145 copyID, NULL, NULL))
7146 return TRUE;
7147 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007148}
7149
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007150/*
7151 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007152 * Returns TRUE if setting references failed somehow.
7153 */
7154 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007155set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007156{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007157 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007158 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007159 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007160 char_u fname_buf[FLEN_FIXED + 1];
7161 char_u *tofree = NULL;
7162 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007163 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007164
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007165 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007166 return FALSE;
7167
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007168 if (fp_in == NULL)
7169 {
7170 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007171 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007172 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007173 if (fp != NULL)
7174 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007175 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007176 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007177 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007178
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007179 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007180 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007181}
7182
Bram Moolenaare38eab22019-12-05 21:50:01 +01007183#endif // FEAT_EVAL