blob: 9a307966b722e6cacbf2f8ebab7937cdf4b1c9f7 [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 {
558 if (STRCMP(aname, obj_members[om].ocm_name) == 0)
559 {
560 type = obj_members[om].ocm_type;
561 break;
562 }
563 }
564 }
565 else
566 type = parse_type(&p, &fp->uf_type_list, TRUE);
567 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100568 if (type == NULL)
569 return FAIL;
570 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000571 if (i < fp->uf_args.ga_len
572 && (type->tt_type == VAR_FUNC
Yegappan Lakshmanan3e336502024-04-04 19:35:59 +0200573 || type->tt_type == VAR_PARTIAL))
574 {
575 char_u *name = ((char_u **)fp->uf_args.ga_data)[i];
576 if (obj_members != NULL && *name == '_')
577 // protected object method
578 name++;
579
580 if (var_wrong_func_name(name, TRUE))
581 return FAIL;
582 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100583 }
584 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100585 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200586
587 if (varargs)
588 {
589 char_u *p;
590
591 // Move the last argument "...name: type" to uf_va_name and
592 // uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200593 --fp->uf_args.ga_len;
Bram Moolenaarf0571712023-01-04 13:16:20 +0000594 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len];
595 ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len] = NULL;
Bram Moolenaar2a389082021-04-09 20:24:31 +0200596 p = ((char_u **)argtypes->ga_data)[len];
597 if (p == NULL)
598 // TODO: get type from default value
599 fp->uf_va_type = &t_list_any;
600 else
601 {
602 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
603 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
604 {
605 semsg(_(e_variable_arguments_type_must_be_list_str),
606 ((char_u **)argtypes->ga_data)[len]);
607 return FAIL;
608 }
609 }
610 if (fp->uf_va_type == NULL)
611 return FAIL;
612 }
613
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100614 return OK;
615}
616
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100617 static int
618parse_return_type(ufunc_T *fp, char_u *ret_type)
619{
620 if (ret_type == NULL)
621 fp->uf_ret_type = &t_void;
622 else
623 {
624 char_u *p = ret_type;
625
626 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
627 if (fp->uf_ret_type == NULL)
628 {
629 fp->uf_ret_type = &t_void;
630 return FAIL;
631 }
632 }
633 return OK;
634}
635
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100636/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200637 * Register function "fp" as using "current_funccal" as its scope.
638 */
639 static int
640register_closure(ufunc_T *fp)
641{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200642 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100643 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200644 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200645 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200646 fp->uf_scoped = current_funccal;
647 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200648
Bram Moolenaarca16c602022-09-06 18:57:08 +0100649 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200650 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100651 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
652 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200653 return OK;
654}
655
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100656 static void
657set_ufunc_name(ufunc_T *fp, char_u *name)
658{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100659 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
660 // actually extends beyond the struct.
661 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100662
663 if (name[0] == K_SPECIAL)
664 {
665 fp->uf_name_exp = alloc(STRLEN(name) + 3);
666 if (fp->uf_name_exp != NULL)
667 {
668 STRCPY(fp->uf_name_exp, "<SNR>");
669 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
670 }
671 }
672}
673
Bram Moolenaar58016442016-07-31 18:30:22 +0200674/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100675 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
676 * return "buf" filled with a readable function name.
677 * Otherwise just return "name", thus the return value can always be used.
678 * "name" and "buf" may be equal.
679 */
680 char_u *
681make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
682{
683 size_t len;
684
685 if (name[0] != K_SPECIAL)
686 return name;
687 len = STRLEN(name);
688 if (len + 3 > bufsize)
689 return name;
690
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100691 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100692 mch_memmove(buf, "<SNR>", 5);
693 return buf;
694}
695
696/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200697 * Get a name for a lambda. Returned in static memory.
698 */
699 char_u *
700get_lambda_name(void)
701{
702 static char_u name[30];
703 static int lambda_no = 0;
704
705 sprintf((char*)name, "<lambda>%d", ++lambda_no);
706 return name;
707}
708
Bram Moolenaare01e5212023-01-08 20:31:18 +0000709/*
710 * Allocate a "ufunc_T" for a function called "name".
711 * Makes sure the size is right.
712 */
713 static ufunc_T *
714alloc_ufunc(char_u *name)
715{
716 // When the name is short we need to make sure we allocate enough bytes for
717 // the whole struct, including any padding.
718 size_t len = offsetof(ufunc_T, uf_name) + STRLEN(name) + 1;
719 return alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
720}
721
Bram Moolenaar801ab062020-06-25 19:27:56 +0200722#if defined(FEAT_LUA) || defined(PROTO)
723/*
724 * Registers a native C callback which can be called from Vim script.
725 * Returns the name of the Vim script function.
726 */
727 char_u *
728register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
729{
730 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200731 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200732
Bram Moolenaare01e5212023-01-08 20:31:18 +0000733 fp = alloc_ufunc(name);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200734 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200735 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200736
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200737 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200738 fp->uf_refcount = 1;
739 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000740 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200741 fp->uf_calls = 0;
742 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200743 fp->uf_cb = cb;
744 fp->uf_cb_free = cb_free;
745 fp->uf_cb_state = state;
746
747 set_ufunc_name(fp, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000748 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200749
750 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200751}
752#endif
753
Bram Moolenaar04b12692020-05-04 23:24:44 +0200754/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100755 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100756 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100757 * If "white_error" is not NULL check for correct use of white space and set
758 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100759 * Return NULL if no valid arrow found.
760 */
761 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100762skip_arrow(
763 char_u *start,
764 int equal_arrow,
765 char_u **ret_type,
766 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100767{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100768 char_u *s = start;
769 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100770
771 if (equal_arrow)
772 {
773 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100774 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100775 if (white_error != NULL && !VIM_ISWHITE(s[1]))
776 {
777 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100778 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100779 return NULL;
780 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100781 s = skipwhite(s + 1);
782 *ret_type = s;
783 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100784 if (s == *ret_type)
785 {
786 emsg(_(e_missing_return_type));
787 return NULL;
788 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100789 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100790 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100791 s = skipwhite(s);
792 if (*s != '=')
793 return NULL;
794 ++s;
795 }
796 if (*s != '>')
797 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100798 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
799 || !IS_WHITE_OR_NUL(s[1])))
800 {
801 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100802 semsg(_(e_white_space_required_before_and_after_str_at_str),
803 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100804 return NULL;
805 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100806 return skipwhite(s + 1);
807}
808
809/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100810 * Check if "*cmd" points to a function command and if so advance "*cmd" and
811 * return TRUE.
812 * Otherwise return FALSE;
813 * Do not consider "function(" to be a command.
814 */
815 static int
816is_function_cmd(char_u **cmd)
817{
818 char_u *p = *cmd;
819
820 if (checkforcmd(&p, "function", 2))
821 {
822 if (*p == '(')
823 return FALSE;
824 *cmd = p;
825 return TRUE;
826 }
827 return FALSE;
828}
829
830/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200831 * Called when defining a function: The context may be needed for script
832 * variables declared in a block that is visible now but not when the function
833 * is compiled or called later.
834 */
835 static void
836function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
837{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000838 if (cstack == NULL || cstack->cs_idx < 0)
839 return;
840
841 int count = cstack->cs_idx + 1;
842 int i;
843
844 fp->uf_block_ids = ALLOC_MULT(int, count);
845 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200846 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000847 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
848 sizeof(int) * count);
849 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200850 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000851
852 // Set flag in each block to indicate a function was defined. This
853 // is used to keep the variable when leaving the block, see
854 // hide_script_var().
855 for (i = 0; i <= cstack->cs_idx; ++i)
856 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200857}
858
859/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100860 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200861 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100862 * "newlines" must already have been initialized.
863 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
864 */
865 static int
866get_function_body(
867 exarg_T *eap,
868 garray_T *newlines,
869 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000870 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100871{
872 linenr_T sourcing_lnum_top = SOURCING_LNUM;
873 linenr_T sourcing_lnum_off;
874 int saved_wait_return = need_wait_return;
875 char_u *line_arg = line_arg_in;
876 int vim9_function = eap->cmdidx == CMD_def
877 || eap->cmdidx == CMD_block;
878#define MAX_FUNC_NESTING 50
879 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200880 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100881 int nesting = 0;
882 getline_opt_T getline_options;
883 int indent = 2;
884 char_u *skip_until = NULL;
885 int ret = FAIL;
886 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200887 int heredoc_concat_len = 0;
888 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100889 char_u *heredoc_trimmed = NULL;
890
Bram Moolenaar20677332021-06-06 17:02:53 +0200891 ga_init2(&heredoc_ga, 1, 500);
892
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100893 // Detect having skipped over comment lines to find the return
894 // type. Add NULL lines to keep the line count correct.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100895 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100896 if (SOURCING_LNUM < sourcing_lnum_off)
897 {
898 sourcing_lnum_off -= SOURCING_LNUM;
899 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
900 goto theend;
901 while (sourcing_lnum_off-- > 0)
902 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
903 }
904
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200905 nesting_def[0] = vim9_function;
906 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100907 getline_options = vim9_function
908 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
909 for (;;)
910 {
911 char_u *theline;
912 char_u *p;
913 char_u *arg;
914
915 if (KeyTyped)
916 {
917 msg_scroll = TRUE;
918 saved_wait_return = FALSE;
919 }
920 need_wait_return = FALSE;
921
922 if (line_arg != NULL)
923 {
924 // Use eap->arg, split up in parts by line breaks.
925 theline = line_arg;
926 p = vim_strchr(theline, '\n');
927 if (p == NULL)
928 line_arg += STRLEN(line_arg);
929 else
930 {
931 *p = NUL;
932 line_arg = p + 1;
933 }
934 }
935 else
936 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000937 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100938 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100939 }
940 if (KeyTyped)
941 lines_left = Rows - 1;
942 if (theline == NULL)
943 {
944 // Use the start of the function for the line number.
945 SOURCING_LNUM = sourcing_lnum_top;
946 if (skip_until != NULL)
947 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200948 else if (nesting_inline[nesting])
949 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100950 else if (eap->cmdidx == CMD_def)
951 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100952 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000953 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100954 goto theend;
955 }
956
957 // Detect line continuation: SOURCING_LNUM increased more than one.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100958 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100959 if (SOURCING_LNUM < sourcing_lnum_off)
960 sourcing_lnum_off -= SOURCING_LNUM;
961 else
962 sourcing_lnum_off = 0;
963
964 if (skip_until != NULL)
965 {
966 // Don't check for ":endfunc"/":enddef" between
967 // * ":append" and "."
968 // * ":python <<EOF" and "EOF"
969 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
970 if (heredoc_trimmed == NULL
971 || (is_heredoc && skipwhite(theline) == theline)
972 || STRNCMP(theline, heredoc_trimmed,
973 STRLEN(heredoc_trimmed)) == 0)
974 {
975 if (heredoc_trimmed == NULL)
976 p = theline;
977 else if (is_heredoc)
978 p = skipwhite(theline) == theline
979 ? theline : theline + STRLEN(heredoc_trimmed);
980 else
981 p = theline + STRLEN(heredoc_trimmed);
982 if (STRCMP(p, skip_until) == 0)
983 {
984 VIM_CLEAR(skip_until);
985 VIM_CLEAR(heredoc_trimmed);
986 getline_options = vim9_function
987 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
988 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200989
990 if (heredoc_concat_len > 0)
991 {
992 // Replace the starting line with all the concatenated
993 // lines.
994 ga_concat(&heredoc_ga, theline);
995 vim_free(((char_u **)(newlines->ga_data))[
996 heredoc_concat_len - 1]);
997 ((char_u **)(newlines->ga_data))[
998 heredoc_concat_len - 1] = heredoc_ga.ga_data;
999 ga_init(&heredoc_ga);
1000 heredoc_concat_len = 0;
1001 theline += STRLEN(theline); // skip the "EOF"
1002 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001003 }
1004 }
1005 }
1006 else
1007 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001008 int c;
1009 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +00001010 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001011
1012 // skip ':' and blanks
1013 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
1014 ;
1015
1016 // Check for "endfunction", "enddef" or "}".
1017 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +00001018 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001019 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001020 ? *p == '}'
1021 : (checkforcmd(&p, nesting_def[nesting]
1022 ? "enddef" : "endfunction", 4)
1023 && *p != ':'))
1024 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00001025 if (!nesting_inline[nesting] && nesting_def[nesting]
1026 && p < cmd + 6)
1027 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001028 if (nesting-- == 0)
1029 {
1030 char_u *nextcmd = NULL;
1031
1032 if (*p == '|' || *p == '}')
1033 nextcmd = p + 1;
1034 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
1035 nextcmd = line_arg;
1036 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001037 && (vim9_function || p_verbose > 0))
1038 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +02001039 SOURCING_LNUM = sourcing_lnum_top
1040 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001041 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +00001042 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001043 else
1044 give_warning2((char_u *)
1045 _("W22: Text found after :endfunction: %s"),
1046 p, TRUE);
1047 }
1048 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001049 {
1050 // Another command follows. If the line came from "eap"
1051 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001052 // change "eap->cmdlinep" to point to the last fetched
1053 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001054 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001055 if (lines_to_free->ga_len > 0
1056 && *eap->cmdlinep !=
1057 ((char_u **)lines_to_free->ga_data)
1058 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001059 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001060 // *cmdlinep will be freed later, thus remove the
1061 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001062 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001063 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
1064 [lines_to_free->ga_len - 1];
1065 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001066 }
1067 }
1068 break;
1069 }
1070 }
1071
1072 // Check for mismatched "endfunc" or "enddef".
1073 // We don't check for "def" inside "func" thus we also can't check
1074 // for "enddef".
1075 // We continue to find the end of the function, although we might
1076 // not find it.
1077 else if (nesting_def[nesting])
1078 {
1079 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1080 emsg(_(e_mismatched_endfunction));
1081 }
1082 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1083 emsg(_(e_mismatched_enddef));
1084
1085 // Increase indent inside "if", "while", "for" and "try", decrease
1086 // at "end".
1087 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1088 indent -= 2;
1089 else if (STRNCMP(p, "if", 2) == 0
1090 || STRNCMP(p, "wh", 2) == 0
1091 || STRNCMP(p, "for", 3) == 0
1092 || STRNCMP(p, "try", 3) == 0)
1093 indent += 2;
1094
1095 // Check for defining a function inside this function.
1096 // Only recognize "def" inside "def", not inside "function",
1097 // For backwards compatibility, see Test_function_python().
1098 c = *p;
1099 if (is_function_cmd(&p)
1100 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1101 {
1102 if (*p == '!')
1103 p = skipwhite(p + 1);
1104 p += eval_fname_script(p);
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001105 vim_free(trans_function_name(&p, NULL, TRUE, 0));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001106 if (*skipwhite(p) == '(')
1107 {
1108 if (nesting == MAX_FUNC_NESTING - 1)
1109 emsg(_(e_function_nesting_too_deep));
1110 else
1111 {
1112 ++nesting;
1113 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001114 nesting_inline[nesting] = FALSE;
1115 indent += 2;
1116 }
1117 }
1118 }
1119
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001120 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001121 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001122 // Not a comment line: check for nested inline function.
1123 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001124 while (end > p && VIM_ISWHITE(*end))
1125 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001126 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001127 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001128 int is_block;
1129
1130 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001131 --end;
1132 while (end > p && VIM_ISWHITE(*end))
1133 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001134 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1135 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001136 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001137 char_u *s = p;
1138
1139 // check for line starting with "au" for :autocmd or
1140 // "com" for :command, these can use a {} block
1141 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1142 || checkforcmd_noparen(&s, "command", 3);
1143 }
1144
1145 if (is_block)
1146 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001147 if (nesting == MAX_FUNC_NESTING - 1)
1148 emsg(_(e_function_nesting_too_deep));
1149 else
1150 {
1151 ++nesting;
1152 nesting_def[nesting] = TRUE;
1153 nesting_inline[nesting] = TRUE;
1154 indent += 2;
1155 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001156 }
1157 }
1158 }
1159
1160 // Check for ":append", ":change", ":insert". Not for :def.
1161 p = skip_range(p, FALSE, NULL);
1162 if (!vim9_function
1163 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1164 || (p[0] == 'c'
1165 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1166 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1167 && (STRNCMP(&p[3], "nge", 3) != 0
1168 || !ASCII_ISALPHA(p[6])))))))
1169 || (p[0] == 'i'
1170 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1171 && (!ASCII_ISALPHA(p[2])
1172 || (p[2] == 's'
1173 && (!ASCII_ISALPHA(p[3])
1174 || p[3] == 'e'))))))))
1175 skip_until = vim_strsave((char_u *)".");
1176
1177 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1178 arg = skipwhite(skiptowhite(p));
1179 if (arg[0] == '<' && arg[1] =='<'
1180 && ((p[0] == 'p' && p[1] == 'y'
1181 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1182 || ((p[2] == '3' || p[2] == 'x')
1183 && !ASCII_ISALPHA(p[3]))))
1184 || (p[0] == 'p' && p[1] == 'e'
1185 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1186 || (p[0] == 't' && p[1] == 'c'
1187 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1188 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1189 && !ASCII_ISALPHA(p[3]))
1190 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1191 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1192 || (p[0] == 'm' && p[1] == 'z'
1193 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1194 ))
1195 {
1196 // ":python <<" continues until a dot, like ":append"
1197 p = skipwhite(arg + 2);
1198 if (STRNCMP(p, "trim", 4) == 0)
1199 {
1200 // Ignore leading white space.
1201 p = skipwhite(p + 4);
1202 heredoc_trimmed = vim_strnsave(theline,
1203 skipwhite(theline) - theline);
1204 }
1205 if (*p == NUL)
1206 skip_until = vim_strsave((char_u *)".");
1207 else
1208 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1209 getline_options = GETLINE_NONE;
1210 is_heredoc = TRUE;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001211 if (vim9_function && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001212 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001213 }
1214
Bram Moolenaard881d152022-05-13 13:50:36 +01001215 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001216 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001217 // Check for ":cmd v =<< [trim] EOF"
1218 // and ":cmd [a, b] =<< [trim] EOF"
1219 // and "lines =<< [trim] EOF" for Vim9
1220 // Where "cmd" can be "let", "var", "final" or "const".
zeertzjqa93d9cd2023-05-02 16:25:47 +01001221 arg = p;
1222 if (checkforcmd(&arg, "let", 2)
1223 || checkforcmd(&arg, "var", 3)
1224 || checkforcmd(&arg, "final", 5)
1225 || checkforcmd(&arg, "const", 5)
1226 || vim9_function)
Bram Moolenaard881d152022-05-13 13:50:36 +01001227 {
zeertzjqa93d9cd2023-05-02 16:25:47 +01001228 while (vim_strchr((char_u *)"$@&", *arg) != NULL)
1229 ++arg;
1230 arg = skipwhite(find_name_end(arg, NULL, NULL,
1231 FNE_INCL_BR | FNE_ALLOW_CURLY));
1232 if (vim9_function && *arg == ':')
1233 arg = skipwhite(skip_type(skipwhite(arg + 1), FALSE));
1234 if (arg[0] == '=' && arg[1] == '<' && arg[2] =='<')
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001235 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001236 p = skipwhite(arg + 3);
1237 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001238 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001239 if (STRNCMP(p, "trim", 4) == 0)
1240 {
1241 // Ignore leading white space.
1242 p = skipwhite(p + 4);
1243 heredoc_trimmed = vim_strnsave(theline,
1244 skipwhite(theline) - theline);
1245 continue;
1246 }
1247 if (STRNCMP(p, "eval", 4) == 0)
1248 {
1249 // Ignore leading white space.
1250 p = skipwhite(p + 4);
1251 continue;
1252 }
1253 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001254 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001255 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1256 getline_options = GETLINE_NONE;
1257 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001258 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001259 }
1260 }
1261 }
1262
1263 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001264 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001265 goto theend;
1266
Bram Moolenaar20677332021-06-06 17:02:53 +02001267 if (heredoc_concat_len > 0)
1268 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001269 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001270 // to be used for the instruction later.
1271 ga_concat(&heredoc_ga, theline);
1272 ga_concat(&heredoc_ga, (char_u *)"\n");
1273 p = vim_strsave((char_u *)"");
1274 }
1275 else
1276 {
1277 // Copy the line to newly allocated memory. get_one_sourceline()
1278 // allocates 250 bytes per line, this saves 80% on average. The
1279 // cost is an extra alloc/free.
1280 p = vim_strsave(theline);
1281 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001282 if (p == NULL)
1283 goto theend;
1284 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1285
1286 // Add NULL lines for continuation lines, so that the line count is
1287 // equal to the index in the growarray.
1288 while (sourcing_lnum_off-- > 0)
1289 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1290
1291 // Check for end of eap->arg.
1292 if (line_arg != NULL && *line_arg == NUL)
1293 line_arg = NULL;
1294 }
1295
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001296 // Return OK when no error was detected.
1297 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001298 ret = OK;
1299
1300theend:
1301 vim_free(skip_until);
1302 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001303 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001304 need_wait_return |= saved_wait_return;
1305 return ret;
1306}
1307
1308/*
1309 * Handle the body of a lambda. *arg points to the "{", process statements
1310 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001311 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001312 * When successful "rettv" is set to a funcref.
1313 */
1314 static int
1315lambda_function_body(
1316 char_u **arg,
1317 typval_T *rettv,
1318 evalarg_T *evalarg,
1319 garray_T *newargs,
1320 garray_T *argtypes,
1321 int varargs,
1322 garray_T *default_args,
1323 char_u *ret_type)
1324{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001325 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001326 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001327 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001328 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001329 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001330 exarg_T eap;
1331 garray_T newlines;
1332 char_u *cmdline = NULL;
1333 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001334 partial_T *pt;
1335 char_u *name;
1336 int lnum_save = -1;
1337 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1338
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001339 *arg = skipwhite(*arg + 1);
1340 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001341 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001342 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001343 return FAIL;
1344 }
1345
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001346 CLEAR_FIELD(eap);
1347 eap.cmdidx = CMD_block;
1348 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001349 eap.cmdlinep = &cmdline;
1350 eap.skip = !evaluate;
1351 if (evalarg->eval_cctx != NULL)
1352 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1353 else
1354 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001355 eap.ea_getline = evalarg->eval_getline;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001356 eap.cookie = evalarg->eval_cookie;
1357 }
1358
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001359 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001360 if (get_function_body(&eap, &newlines, NULL,
1361 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001362 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001363
1364 // When inside a lambda must add the function lines to evalarg.eval_ga.
1365 evalarg->eval_break_count += newlines.ga_len;
1366 if (gap->ga_itemsize > 0)
1367 {
1368 int idx;
1369 char_u *last;
1370 size_t plen;
1371 char_u *pnl;
1372
1373 for (idx = 0; idx < newlines.ga_len; ++idx)
1374 {
1375 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1376
Bram Moolenaarecb66452021-05-18 15:09:18 +02001377 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001378 goto erret;
1379
1380 // Going to concatenate the lines after parsing. For an empty or
1381 // comment line use an empty string.
1382 // Insert NL characters at the start of each line, the string will
1383 // be split again later in .get_lambda_tv().
1384 if (*p == NUL || vim9_comment_start(p))
1385 p = (char_u *)"";
1386 plen = STRLEN(p);
1387 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1388 if (pnl != NULL)
1389 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001390 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1391 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001392 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001393 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001394 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001395 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001396 // more is following after the "}", which was skipped
1397 last = cmdline;
1398 else
1399 // nothing is following the "}"
1400 last = (char_u *)"}";
1401 plen = STRLEN(last);
1402 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1403 if (pnl != NULL)
1404 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001405 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1406 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001407 }
1408
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001409 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001410 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001411 garray_T *tfgap = &evalarg->eval_tofree_ga;
1412
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001413 // Something comes after the "}".
1414 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001415
1416 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001417 if (ga_grow(tfgap, 1) == OK)
1418 {
1419 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1420 evalarg->eval_using_cmdline = TRUE;
1421 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001422 }
1423 else
1424 *arg = (char_u *)"";
1425
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001426 if (!evaluate)
1427 {
1428 ret = OK;
1429 goto erret;
1430 }
1431
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001432 name = get_lambda_name();
Bram Moolenaare01e5212023-01-08 20:31:18 +00001433 ufunc = alloc_ufunc(name);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001434 if (ufunc == NULL)
1435 goto erret;
1436 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001437 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001438 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001439 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001440 ufunc->uf_refcount = 1;
1441 ufunc->uf_args = *newargs;
1442 newargs->ga_data = NULL;
1443 ufunc->uf_def_args = *default_args;
1444 default_args->ga_data = NULL;
1445 ufunc->uf_func_type = &t_func_any;
1446
1447 // error messages are for the first function line
1448 lnum_save = SOURCING_LNUM;
1449 SOURCING_LNUM = sourcing_lnum_top;
1450
1451 // parse argument types
h-eastb895b0f2023-09-24 15:46:31 +02001452 if (parse_argument_types(ufunc, argtypes, varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001453 {
1454 SOURCING_LNUM = lnum_save;
1455 goto erret;
1456 }
1457
1458 // parse the return type, if any
1459 if (parse_return_type(ufunc, ret_type) == FAIL)
1460 goto erret;
1461
1462 pt = ALLOC_CLEAR_ONE(partial_T);
1463 if (pt == NULL)
1464 goto erret;
1465 pt->pt_func = ufunc;
1466 pt->pt_refcount = 1;
1467
1468 ufunc->uf_lines = newlines;
1469 newlines.ga_data = NULL;
1470 if (sandbox)
1471 ufunc->uf_flags |= FC_SANDBOX;
1472 if (!ASCII_ISUPPER(*ufunc->uf_name))
1473 ufunc->uf_flags |= FC_VIM9;
1474 ufunc->uf_script_ctx = current_sctx;
1475 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1476 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1477 set_function_type(ufunc);
1478
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001479 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1480
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001481 rettv->vval.v_partial = pt;
1482 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001483 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001484 ret = OK;
1485
1486erret:
1487 if (lnum_save >= 0)
1488 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001489 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001490 if (newargs != NULL)
1491 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001492 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001493 if (ufunc != NULL)
1494 {
1495 func_clear(ufunc, TRUE);
1496 func_free(ufunc, TRUE);
1497 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001498 return ret;
1499}
1500
1501/*
1502 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001503 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001504 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001505 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1506 */
1507 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001508get_lambda_tv(
1509 char_u **arg,
1510 typval_T *rettv,
1511 int types_optional,
1512 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001513{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001514 int evaluate = evalarg != NULL
1515 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001516 garray_T newargs;
1517 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001518 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001519 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001520 garray_T default_args;
h-eastb895b0f2023-09-24 15:46:31 +02001521 garray_T arg_objm;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001522 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001523 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001524 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001525 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001526 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001527 char_u *s;
1528 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001529 int *old_eval_lavars = eval_lavars_used;
1530 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001531 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001532 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001533 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001534 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001535 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001536 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001537
Bram Moolenaar4525a572022-02-13 11:57:33 +00001538 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001539 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001540
1541 ga_init(&newargs);
1542 ga_init(&newlines);
1543
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001544 // First, check if this is really a lambda expression. "->" or "=>" must
1545 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001546 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001547 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001548 types_optional ? &argtypes : NULL, types_optional,
1549 types_optional ? &arg_objm : NULL, evalarg,
1550 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001551 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001552 {
1553 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001554 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001555 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001556 ga_clear(&arg_objm);
1557 }
Bram Moolenaar0346b792021-01-31 22:18:29 +01001558 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001559 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001560
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001561 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001562 if (evaluate)
1563 pnewargs = &newargs;
1564 else
1565 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001566 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001567 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
h-eastb895b0f2023-09-24 15:46:31 +02001568 types_optional ? &argtypes : NULL, types_optional,
1569 types_optional ? &arg_objm : NULL, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001570 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001571 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001572 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001573 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001574 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001575 {
1576 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001577 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001578 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001579 ga_clear(&arg_objm);
1580 }
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001581 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001582 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001583 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001584 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001585
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001586 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1587 if (ret_type != NULL)
1588 {
1589 ret_type = vim_strsave(ret_type);
1590 tofree2 = ret_type;
1591 }
1592
Bram Moolenaare38eab22019-12-05 21:50:01 +01001593 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001594 if (evaluate)
1595 eval_lavars_used = &eval_lavars;
1596
Bram Moolenaar65c44152020-12-24 15:14:01 +01001597 *arg = skipwhite_and_linebreak(*arg, evalarg);
1598
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001599 // Recognize "{" as the start of a function body.
1600 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001601 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001602 if (evalarg == NULL)
1603 // cannot happen?
1604 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001605 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001606 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1607 types_optional ? &argtypes : NULL, varargs,
1608 &default_args, ret_type) == FAIL)
1609 goto errret;
1610 goto theend;
1611 }
1612 if (default_args.ga_len > 0)
1613 {
1614 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001615 goto errret;
1616 }
1617
Bram Moolenaare38eab22019-12-05 21:50:01 +01001618 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001619 start = *arg;
1620 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001621 if (ret == FAIL)
1622 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001623
Bram Moolenaar65c44152020-12-24 15:14:01 +01001624 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001625 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001626 *arg = skipwhite_and_linebreak(*arg, evalarg);
1627 if (**arg != '}')
1628 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001629 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001630 goto errret;
1631 }
1632 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001633 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001634
1635 if (evaluate)
1636 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001637 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001638 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001639 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001640 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001641 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001642
Bram Moolenaare01e5212023-01-08 20:31:18 +00001643 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001644 if (fp == NULL)
1645 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001646 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001647 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001648 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001649 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001650
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001651 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001652 if (ga_grow(&newlines, 1) == FAIL)
1653 goto errret;
1654
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001655 // If there are line breaks, we need to split up the string.
1656 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001657 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001658 line_end = end;
1659
1660 // Add "return " before the expression (or the first line).
1661 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001662 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001663 if (p == NULL)
1664 goto errret;
1665 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1666 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001667 vim_strncpy(p + 7, start, line_end - start);
1668
1669 if (line_end != end)
1670 {
1671 // Add more lines, split by line breaks. Thus is used when a
1672 // lambda with { cmds } is encountered.
1673 while (*line_end == '\n')
1674 {
1675 if (ga_grow(&newlines, 1) == FAIL)
1676 goto errret;
1677 start = line_end + 1;
1678 line_end = vim_strchr(start, '\n');
1679 if (line_end == NULL)
1680 line_end = end;
1681 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1682 vim_strnsave(start, line_end - start);
1683 }
1684 }
1685
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001686 if (strstr((char *)p + 7, "a:") == NULL)
1687 // No a: variables are used for sure.
1688 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001689
1690 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001691 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001692 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001693 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001694 if (types_optional)
1695 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001696 if (parse_argument_types(fp, &argtypes,
h-eastb895b0f2023-09-24 15:46:31 +02001697 vim9script && varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001698 goto errret;
1699 if (ret_type != NULL)
1700 {
1701 fp->uf_ret_type = parse_type(&ret_type,
1702 &fp->uf_type_list, TRUE);
1703 if (fp->uf_ret_type == NULL)
1704 goto errret;
1705 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001706 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001707 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001708 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001709
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001710 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001711 if (current_funccal != NULL && eval_lavars)
1712 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001713 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001714 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001715 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001716 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001717
1718#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001719 if (prof_def_func())
1720 func_do_profile(fp);
1721#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001722 if (sandbox)
1723 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001724 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001725 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001726 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001727 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001728 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001729 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001730 // Use the line number of the arguments.
1731 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001732
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001733 function_using_block_scopes(fp, evalarg->eval_cstack);
1734
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001735 pt->pt_func = fp;
1736 pt->pt_refcount = 1;
1737 rettv->vval.v_partial = pt;
1738 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001739
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001740 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001741 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001742
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001743theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001744 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001745 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001746 if (types_optional)
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001747 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001748 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001749 ga_clear(&arg_objm);
1750 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001751
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001752 return OK;
1753
1754errret:
1755 ga_clear_strings(&newargs);
1756 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001757 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001758 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001759 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001760 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001761 ga_clear(&arg_objm);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001762 if (fp != NULL)
1763 vim_free(fp->uf_arg_types);
1764 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001765 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001766 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001767 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001768 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001769 return FAIL;
1770}
1771
1772/*
1773 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1774 * name it contains, otherwise return "name".
1775 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1776 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001777 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1778 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001779 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001780 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001781 */
1782 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001783deref_func_name(
1784 char_u *name,
1785 int *lenp,
1786 partial_T **partialp,
1787 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001788 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001789 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001790 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001791{
1792 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001793 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001794 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001795 char_u *s = NULL;
1796 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001797 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001798
1799 if (partialp != NULL)
1800 *partialp = NULL;
1801
1802 cc = name[*lenp];
1803 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001804
Bram Moolenaar71f21932022-01-07 18:20:55 +00001805 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001806 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001807 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001808 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001809 tv = &v->di_tv;
1810 }
1811 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1812 {
1813 imported_T *import;
1814 char_u *p = name;
1815 int len = *lenp;
1816
1817 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001818 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001819 p = name + 2;
1820 len -= 2;
1821 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001822 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001823
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001824 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001825 if (import != NULL)
1826 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001827 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001828 if (new_function)
1829 semsg(_(e_redefining_imported_item_str), name);
1830 else
1831 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001832 name[len] = cc;
1833 *lenp = 0;
1834 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001835 }
1836 }
1837
1838 if (tv != NULL)
1839 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001840 if (found_var != NULL)
1841 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001842 if (tv->v_type == VAR_FUNC)
1843 {
1844 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001845 {
1846 *lenp = 0;
1847 return (char_u *)""; // just in case
1848 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001849 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001850 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001851 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001852
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001853 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001854 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001855 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001856
1857 if (pt == NULL)
1858 {
1859 *lenp = 0;
1860 return (char_u *)""; // just in case
1861 }
1862 if (partialp != NULL)
1863 *partialp = pt;
1864 s = partial_name(pt);
1865 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001866 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001867
1868 if (s != NULL)
1869 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001870 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001871 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001872 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001873
1874 if (sv != NULL)
1875 *type = sv->sv_type;
1876 }
1877 return s;
1878 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001879 }
1880
1881 return name;
1882}
1883
1884/*
1885 * Give an error message with a function name. Handle <SNR> things.
1886 * "ermsg" is to be passed without translation, use N_() instead of _().
1887 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001888 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001889emsg_funcname(char *ermsg, char_u *name)
1890{
Bram Moolenaar54598062022-01-13 12:05:09 +00001891 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001892
Bram Moolenaar54598062022-01-13 12:05:09 +00001893 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001894 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001895 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001896 if (p != name)
1897 vim_free(p);
1898}
1899
1900/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001901 * Get function arguments at "*arg" and advance it.
1902 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001903 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001904 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001905 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001906get_func_arguments(
1907 char_u **arg,
1908 evalarg_T *evalarg,
1909 int partial_argc,
1910 typval_T *argvars,
Ernie Raelb077b582023-12-14 20:11:44 +01001911 int *argcount,
1912 int is_builtin)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001913{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001914 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001915 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001916 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001917 int evaluate = evalarg == NULL
1918 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001919
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001920 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001921 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001922 // skip the '(' or ',' and possibly line breaks
1923 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1924
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001925 if (*argp == ')' || *argp == ',' || *argp == NUL)
1926 break;
Ernie Raelb077b582023-12-14 20:11:44 +01001927
1928 int arg_idx = *argcount;
1929 if (eval1(&argp, &argvars[arg_idx], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001930 {
1931 ret = FAIL;
1932 break;
1933 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001934 ++*argcount;
Ernie Raelb077b582023-12-14 20:11:44 +01001935 if (!is_builtin && check_typval_is_value(&argvars[arg_idx]) == FAIL)
1936 {
1937 ret = FAIL;
1938 break;
1939 }
1940
Bram Moolenaar9d489562020-07-30 20:08:50 +02001941 // The comma should come right after the argument, but this wasn't
1942 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001943 if (vim9script)
1944 {
1945 if (*argp != ',' && *skipwhite(argp) == ',')
1946 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001947 if (evaluate)
1948 semsg(_(e_no_white_space_allowed_before_str_str),
1949 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001950 ret = FAIL;
1951 break;
1952 }
1953 }
1954 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001955 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001956 if (*argp != ',')
1957 break;
Christian Brabandt00cb2472023-09-05 20:46:25 +02001958 if (vim9script && !IS_WHITE_NL_OR_NUL(argp[1]))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001959 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001960 if (evaluate)
1961 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001962 ret = FAIL;
1963 break;
1964 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001965 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001966
Bram Moolenaare6b53242020-07-01 17:28:33 +02001967 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001968 if (*argp == ')')
1969 ++argp;
1970 else
1971 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001972 *arg = argp;
1973 return ret;
1974}
1975
1976/*
1977 * Call a function and put the result in "rettv".
1978 * Return OK or FAIL.
1979 */
1980 int
1981get_func_tv(
1982 char_u *name, // name of the function
1983 int len, // length of "name" or -1 to use strlen()
1984 typval_T *rettv,
1985 char_u **arg, // argument, pointing to the '('
1986 evalarg_T *evalarg, // for line continuation
1987 funcexe_T *funcexe) // various values
1988{
1989 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001990 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001991 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1992 int argcount = 0; // number of arguments found
1993 int vim9script = in_vim9script();
1994 int evaluate = evalarg == NULL
1995 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
1996
1997 argp = *arg;
1998 ret = get_func_arguments(&argp, evalarg,
1999 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
Ernie Raelb077b582023-12-14 20:11:44 +01002000 argvars, &argcount, builtin_function(name, -1));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002001
2002 if (ret == OK)
2003 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002004 int i = 0;
2005 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002006
2007 if (get_vim_var_nr(VV_TESTING))
2008 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002009 // Prepare for calling test_garbagecollect_now(), need to know
2010 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002011 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002012 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002013 for (i = 0; i < argcount; ++i)
2014 if (ga_grow(&funcargs, 1) == OK)
2015 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
2016 &argvars[i];
2017 }
2018
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002019 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00002020 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002021 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002022 // An error in a builtin function does not return FAIL, but we do
2023 // want to abort further processing if an error was given.
2024 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002025 clear_tv(rettv);
2026 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002027
2028 funcargs.ga_len -= i;
2029 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002030 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002031 {
2032 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002033 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002034 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00002035 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002036 }
2037
2038 while (--argcount >= 0)
2039 clear_tv(&argvars[argcount]);
2040
Bram Moolenaar4525a572022-02-13 11:57:33 +00002041 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02002042 *arg = argp;
2043 else
2044 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002045 return ret;
2046}
2047
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002048/*
2049 * Return TRUE if "p" starts with "<SID>" or "s:".
2050 * Only works if eval_fname_script() returned non-zero for "p"!
2051 */
2052 static int
2053eval_fname_sid(char_u *p)
2054{
2055 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
2056}
2057
2058/*
2059 * In a script change <SID>name() and s:name() to K_SNR 123_name().
2060 * Change <SNR>123_name() to K_SNR 123_name().
2061 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002062 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002063 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002064 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00002065fname_trans_sid(
2066 char_u *name,
2067 char_u *fname_buf,
2068 char_u **tofree,
2069 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002070{
2071 int llen;
2072 char_u *fname;
2073 int i;
2074
2075 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002076 if (llen == 0)
2077 return name; // no prefix
2078
2079 fname_buf[0] = K_SPECIAL;
2080 fname_buf[1] = KS_EXTRA;
2081 fname_buf[2] = (int)KE_SNR;
2082 i = 3;
2083 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002084 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002085 if (current_sctx.sc_sid <= 0)
2086 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002087 else
2088 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002089 sprintf((char *)fname_buf + 3, "%ld_",
2090 (long)current_sctx.sc_sid);
2091 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002092 }
2093 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002094 if (i + STRLEN(name + llen) < FLEN_FIXED)
2095 {
2096 STRCPY(fname_buf + i, name + llen);
2097 fname = fname_buf;
2098 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002099 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002100 {
2101 fname = alloc(i + STRLEN(name + llen) + 1);
2102 if (fname == NULL)
2103 *error = FCERR_OTHER;
2104 else
2105 {
2106 *tofree = fname;
2107 mch_memmove(fname, fname_buf, (size_t)i);
2108 STRCPY(fname + i, name + llen);
2109 }
2110 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002111 return fname;
2112}
2113
2114/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002115 * Concatenate the script ID and function name into "<SNR>99_name".
2116 * "buffer" must have size MAX_FUNC_NAME_LEN.
2117 */
2118 void
2119func_name_with_sid(char_u *name, int sid, char_u *buffer)
2120{
2121 // A script-local function is stored as "<SNR>99_name".
2122 buffer[0] = K_SPECIAL;
2123 buffer[1] = KS_EXTRA;
2124 buffer[2] = (int)KE_SNR;
2125 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2126 (long)sid, name);
2127}
2128
2129/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002130 * Find a function "name" in script "sid".
2131 */
2132 static ufunc_T *
2133find_func_with_sid(char_u *name, int sid)
2134{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002135 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002136 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137
Bram Moolenaarb8822442022-01-11 15:24:05 +00002138 if (!SCRIPT_ID_VALID(sid))
2139 return NULL; // not in a script
2140
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002141 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142 hi = hash_find(&func_hashtab, buffer);
2143 if (!HASHITEM_EMPTY(hi))
2144 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002145 return NULL;
2146}
2147
2148/*
2149 * Find a function "name" in script "sid" prefixing the autoload prefix.
2150 */
2151 static ufunc_T *
2152find_func_with_prefix(char_u *name, int sid)
2153{
2154 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002155 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002156 scriptitem_T *si;
2157
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002158 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002159 return NULL; // already has the prefix
2160 if (!SCRIPT_ID_VALID(sid))
2161 return NULL; // not in a script
2162 si = SCRIPT_ITEM(sid);
2163 if (si->sn_autoload_prefix != NULL)
2164 {
2165 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2166 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002167 char_u *namep;
2168
2169 // skip a "<SNR>99_" prefix
2170 namep = untrans_function_name(name);
2171 if (namep == NULL)
2172 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002173
2174 // An exported function in an autoload script is stored as
2175 // "dir#path#name".
2176 if (len < sizeof(buffer))
2177 auto_name = buffer;
2178 else
2179 auto_name = alloc(len);
2180 if (auto_name != NULL)
2181 {
2182 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002183 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002184 hi = hash_find(&func_hashtab, auto_name);
2185 if (auto_name != buffer)
2186 vim_free(auto_name);
2187 if (!HASHITEM_EMPTY(hi))
2188 return HI2UF(hi);
2189 }
2190 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191
2192 return NULL;
2193}
2194
2195/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002196 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002197 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2198 * functions.
2199 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002200 * Return NULL for unknown function.
2201 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002202 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002203find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002204{
2205 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002206 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002207
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002208 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002209 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002210 // Find script-local function before global one.
2211 if (in_vim9script() && eval_isnamec1(*name)
2212 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002213 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002214 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2215 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002216 if (func != NULL)
2217 return func;
2218 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002219 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2220 {
2221 char_u *p = name + 5;
2222 long sid;
2223
2224 // printable "<SNR>123_Name" form
2225 sid = getdigits(&p);
2226 if (*p == '_')
2227 {
2228 func = find_func_with_sid(p + 1, (int)sid);
2229 if (func != NULL)
2230 return func;
2231 }
2232 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002233 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002234
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002235 if ((flags & FFED_NO_GLOBAL) == 0)
2236 {
2237 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002238 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002239 if (!HASHITEM_EMPTY(hi))
2240 return HI2UF(hi);
2241 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242
Bram Moolenaarb8822442022-01-11 15:24:05 +00002243 // Find autoload function if this is an autoload script.
2244 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2245 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246}
2247
2248/*
2249 * Find a function by name, return pointer to it in ufuncs.
2250 * "cctx" is passed in a :def function to find imported functions.
2251 * Return NULL for unknown or dead function.
2252 */
2253 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002254find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002255{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002256 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257
2258 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2259 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002260 return NULL;
2261}
2262
2263/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002264 * Return TRUE if "ufunc" is a global function.
2265 */
2266 int
2267func_is_global(ufunc_T *ufunc)
2268{
2269 return ufunc->uf_name[0] != K_SPECIAL;
2270}
2271
2272/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002273 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2274 */
2275 int
2276func_requires_g_prefix(ufunc_T *ufunc)
2277{
2278 return ufunc->uf_name[0] != K_SPECIAL
2279 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002280 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01002281 && !SAFE_isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002282}
2283
2284/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002285 * Copy the function name of "fp" to buffer "buf".
2286 * "buf" must be able to hold the function name plus three bytes.
2287 * Takes care of script-local function names.
2288 */
2289 static void
2290cat_func_name(char_u *buf, ufunc_T *fp)
2291{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002292 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002293 {
2294 STRCPY(buf, "<SNR>");
2295 STRCAT(buf, fp->uf_name + 3);
2296 }
2297 else
2298 STRCPY(buf, fp->uf_name);
2299}
2300
2301/*
2302 * Add a number variable "name" to dict "dp" with value "nr".
2303 */
2304 static void
2305add_nr_var(
2306 dict_T *dp,
2307 dictitem_T *v,
2308 char *name,
2309 varnumber_T nr)
2310{
2311 STRCPY(v->di_key, name);
2312 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002313 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002314 v->di_tv.v_type = VAR_NUMBER;
2315 v->di_tv.v_lock = VAR_FIXED;
2316 v->di_tv.vval.v_number = nr;
2317}
2318
2319/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002320 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002321 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002322 static void
2323free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002324{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002325 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002326
Bram Moolenaarca16c602022-09-06 18:57:08 +01002327 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002328 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002329 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002330
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002331 // When garbage collecting a funccall_T may be freed before the
2332 // function that references it, clear its uf_scoped field.
2333 // The function may have been redefined and point to another
2334 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002335 if (fp != NULL && fp->uf_scoped == fc)
2336 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002337 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002338 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002339
Bram Moolenaarca16c602022-09-06 18:57:08 +01002340 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002341 vim_free(fc);
2342}
2343
2344/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002345 * Free "fc" and what it contains.
2346 * Can be called only when "fc" is kept beyond the period of it called,
2347 * i.e. after cleanup_function_call(fc).
2348 */
2349 static void
2350free_funccal_contents(funccall_T *fc)
2351{
2352 listitem_T *li;
2353
2354 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002355 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002356
2357 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002358 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002359
2360 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002361 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002362 clear_tv(&li->li_tv);
2363
2364 free_funccal(fc);
2365}
2366
2367/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002368 * Handle the last part of returning from a function: free the local hashtable.
2369 * Unless it is still in use by a closure.
2370 */
2371 static void
2372cleanup_function_call(funccall_T *fc)
2373{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002374 int may_free_fc = fc->fc_refcount <= 0;
2375 int free_fc = TRUE;
2376
Bram Moolenaarca16c602022-09-06 18:57:08 +01002377 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002378
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002379 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002380 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2381 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002382 else
2383 free_fc = FALSE;
2384
2385 // If the a:000 list and the l: and a: dicts are not referenced and
2386 // there is no closure using it, we can free the funccall_T and what's
2387 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002388 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2389 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002390 else
2391 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002392 int todo;
2393 hashitem_T *hi;
2394 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002395
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002396 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002397
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002398 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002399 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002400 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002401 {
2402 if (!HASHITEM_EMPTY(hi))
2403 {
2404 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002405 di = HI2DI(hi);
2406 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002407 }
2408 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002409 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002410
Bram Moolenaarca16c602022-09-06 18:57:08 +01002411 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2412 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002413 else
2414 {
2415 listitem_T *li;
2416
2417 free_fc = FALSE;
2418
2419 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002420 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002421 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002422 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002423
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002424 if (free_fc)
2425 free_funccal(fc);
2426 else
2427 {
2428 static int made_copy = 0;
2429
2430 // "fc" is still in use. This can happen when returning "a:000",
2431 // assigning "l:" to a global variable or defining a closure.
2432 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002433 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002434 previous_funccal = fc;
2435
2436 if (want_garbage_collect)
2437 // If garbage collector is ready, clear count.
2438 made_copy = 0;
2439 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002440 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002441 // We have made a lot of copies, worth 4 Mbyte. This can happen
2442 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002443 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002444 // too much memory.
2445 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002446 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002447 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002448 }
2449}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002450
2451/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002452 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2453 */
2454 static int
2455numbered_function(char_u *name)
2456{
Keith Thompson184f71c2024-01-04 21:19:04 +01002457 return SAFE_isdigit(*name)
2458 || (name[0] == 'g' && name[1] == ':' && SAFE_isdigit(name[2]));
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002459}
2460
2461/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002462 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002463 * 1. ordinary names, function defined with :function or :def;
2464 * can start with "<SNR>123_" literally or with K_SPECIAL.
2465 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002466 * For the first we only count the name stored in func_hashtab as a reference,
2467 * using function() does not count as a reference, because the function is
2468 * looked up by name.
2469 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002470 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002471func_name_refcount(char_u *name)
2472{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002473 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002474}
2475
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476/*
2477 * Unreference "fc": decrement the reference count and free it when it
2478 * becomes zero. "fp" is detached from "fc".
2479 * When "force" is TRUE we are exiting.
2480 */
2481 static void
2482funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2483{
2484 funccall_T **pfc;
2485 int i;
2486
2487 if (fc == NULL)
2488 return;
2489
2490 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002491 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2492 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2493 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2494 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 {
2496 if (fc == *pfc)
2497 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002498 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499 free_funccal_contents(fc);
2500 return;
2501 }
2502 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002503 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2504 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2505 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506}
2507
2508/*
2509 * Remove the function from the function hashtable. If the function was
2510 * deleted while it still has references this was already done.
2511 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2512 */
2513 static int
2514func_remove(ufunc_T *fp)
2515{
2516 hashitem_T *hi;
2517
2518 // Return if it was already virtually deleted.
2519 if (fp->uf_flags & FC_DEAD)
2520 return FALSE;
2521
2522 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002523 if (HASHITEM_EMPTY(hi))
2524 return FALSE;
2525
2526 // When there is a def-function index do not actually remove the
2527 // function, so we can find the index when defining the function again.
2528 // Do remove it when it's a copy.
2529 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002531 fp->uf_flags |= FC_DEAD;
2532 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002534 hash_remove(&func_hashtab, hi, "remove function");
2535 fp->uf_flags |= FC_DELETED;
2536 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537}
2538
2539 static void
2540func_clear_items(ufunc_T *fp)
2541{
2542 ga_clear_strings(&(fp->uf_args));
2543 ga_clear_strings(&(fp->uf_def_args));
2544 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002546 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002547 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01002548 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002549
2550 // Increment the refcount of this function to avoid it being freed
2551 // recursively when the partial is freed.
2552 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002553 partial_unref(fp->uf_partial);
2554 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002555 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002556
2557#ifdef FEAT_LUA
2558 if (fp->uf_cb_free != NULL)
2559 {
2560 fp->uf_cb_free(fp->uf_cb_state);
2561 fp->uf_cb_free = NULL;
2562 }
2563
2564 fp->uf_cb_state = NULL;
2565 fp->uf_cb = NULL;
2566#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567#ifdef FEAT_PROFILE
2568 VIM_CLEAR(fp->uf_tml_count);
2569 VIM_CLEAR(fp->uf_tml_total);
2570 VIM_CLEAR(fp->uf_tml_self);
2571#endif
2572}
2573
2574/*
2575 * Free all things that a function contains. Does not free the function
2576 * itself, use func_free() for that.
2577 * When "force" is TRUE we are exiting.
2578 */
2579 static void
2580func_clear(ufunc_T *fp, int force)
2581{
2582 if (fp->uf_cleared)
2583 return;
2584 fp->uf_cleared = TRUE;
2585
2586 // clear this function
2587 func_clear_items(fp);
2588 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002589 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590}
2591
2592/*
2593 * Free a function and remove it from the list of functions. Does not free
2594 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002595 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002596 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002598 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002599func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600{
2601 // Only remove it when not done already, otherwise we would remove a newer
2602 // version of the function with the same name.
2603 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2604 func_remove(fp);
2605
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002606 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002607 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002608 if (fp->uf_dfunc_idx > 0)
2609 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002610 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002612 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002613 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002614 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615}
2616
2617/*
2618 * Free all things that a function contains and free the function itself.
2619 * When "force" is TRUE we are exiting.
2620 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002621 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002622func_clear_free(ufunc_T *fp, int force)
2623{
2624 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002625 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2626 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002627 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002628 else
2629 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630}
2631
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002632/*
2633 * Copy already defined function "lambda" to a new function with name "global".
2634 * This is for when a compiled function defines a global function.
2635 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002636 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002637copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002638 char_u *lambda,
2639 char_u *global,
2640 loopvarinfo_T *loopvarinfo,
2641 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002642{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002643 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002644 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002645
2646 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002647 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002648 semsg(_(e_lambda_function_not_found_str), lambda);
2649 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002650 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002651
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002652 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002653 if (fp != NULL)
2654 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002655 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002656 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002657 return FAIL;
2658 }
2659
Bram Moolenaare01e5212023-01-08 20:31:18 +00002660 fp = alloc_ufunc(global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002661 if (fp == NULL)
2662 return FAIL;
2663
2664 fp->uf_varargs = ufunc->uf_varargs;
2665 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2666 fp->uf_def_status = ufunc->uf_def_status;
2667 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2668 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2669 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2670 == FAIL
2671 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2672 goto failed;
2673
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002674 if (ufunc->uf_arg_types != NULL)
2675 {
2676 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2677 if (fp->uf_arg_types == NULL)
2678 goto failed;
2679 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2680 sizeof(type_T *) * fp->uf_args.ga_len);
2681 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002682 if (ufunc->uf_va_name != NULL)
2683 {
2684 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2685 if (fp->uf_va_name == NULL)
2686 goto failed;
2687 }
2688 fp->uf_ret_type = ufunc->uf_ret_type;
2689
2690 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002691
2692 fp->uf_name_exp = NULL;
2693 set_ufunc_name(fp, global);
2694
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002695 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002696
2697 // the referenced dfunc_T is now used one more time
2698 link_def_function(fp);
2699
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002700 // Create a partial to store the context of the function where it was
2701 // instantiated. Only needs to be done once. Do this on the original
2702 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002703 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2704 {
2705 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2706
2707 if (pt == NULL)
2708 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002709 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002710 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002711 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002712 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002713 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002714 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002715 }
2716
2717 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002718
2719failed:
2720 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002721 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002722}
2723
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002724static int funcdepth = 0;
2725
2726/*
2727 * Increment the function call depth count.
2728 * Return FAIL when going over 'maxfuncdepth'.
2729 * Otherwise return OK, must call funcdepth_decrement() later!
2730 */
2731 int
2732funcdepth_increment(void)
2733{
2734 if (funcdepth >= p_mfd)
2735 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002736 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002737 return FAIL;
2738 }
2739 ++funcdepth;
2740 return OK;
2741}
2742
2743 void
2744funcdepth_decrement(void)
2745{
2746 --funcdepth;
2747}
2748
2749/*
2750 * Get the current function call depth.
2751 */
2752 int
2753funcdepth_get(void)
2754{
2755 return funcdepth;
2756}
2757
2758/*
2759 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002760 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002761 * times as funcdepth_increment().
2762 */
2763 void
2764funcdepth_restore(int depth)
2765{
2766 funcdepth = depth;
2767}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002768
2769/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002770 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2771 * "rettv".
2772 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2773 * Returns NULL when allocation fails.
2774 */
2775 funccall_T *
2776create_funccal(ufunc_T *fp, typval_T *rettv)
2777{
2778 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2779
2780 if (fc == NULL)
2781 return NULL;
2782 fc->fc_caller = current_funccal;
2783 current_funccal = fc;
2784 fc->fc_func = fp;
2785 func_ptr_ref(fp);
2786 fc->fc_rettv = rettv;
2787 return fc;
2788}
2789
2790/*
2791 * To be called when returning from a compiled function; restores
2792 * current_funccal.
2793 */
2794 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002795remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002796{
2797 funccall_T *fc = current_funccal;
2798
2799 current_funccal = fc->fc_caller;
2800 free_funccal(fc);
2801}
2802
2803/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002804 * Call a user function.
2805 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002806 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002807call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002808 ufunc_T *fp, // pointer to function
2809 int argcount, // nr of args
2810 typval_T *argvars, // arguments
2811 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002812 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002813 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002814{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002815 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002816 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002817 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002818 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002819 funccall_T *fc;
2820 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002821 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002822 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002823 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002824 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002825 int i;
2826 int ai;
2827 int islambda = FALSE;
2828 char_u numbuf[NUMBUFLEN];
2829 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002830 typval_T *tv_to_free[MAX_FUNC_ARGS];
2831 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002832#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002833 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002834#endif
ichizok7e5fe382023-04-15 13:17:50 +01002835 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002836
Bram Moolenaarb2049902021-01-24 12:53:53 +01002837#ifdef FEAT_PROFILE
2838 CLEAR_FIELD(profile_info);
2839#endif
2840
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002841 // If depth of calling is getting too high, don't execute the function.
2842 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002843 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002844 rettv->v_type = VAR_NUMBER;
2845 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002846 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002847 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002848
Bram Moolenaare38eab22019-12-05 21:50:01 +01002849 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002850
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002851 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002852 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00002853 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002854 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002855 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002856 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2857 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002858 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002859 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002860
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002861 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002863#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002864 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002865#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002866 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002867#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002868 if (do_profiling == PROF_YES)
2869 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002870#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002871 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002872 if (call_def_function(fp, argcount, argvars, 0,
2873 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
2874 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002875 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002876#ifdef FEAT_PROFILE
2877 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002878 || (caller != NULL && caller->uf_profiling)))
2879 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002880#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002881 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002882 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002883 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 }
2885
Bram Moolenaar38453522021-11-28 22:00:12 +00002886 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002887
2888 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002889 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2890 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2891 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002892 */
2893 /*
2894 * Init l: variables.
2895 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002896 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002897 if (selfdict != NULL)
2898 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002899 // Set l:self to "selfdict". Use "name" to avoid a warning from
2900 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002901 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002902 name = v->di_key;
2903 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002904 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002905 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002906 v->di_tv.v_type = VAR_DICT;
2907 v->di_tv.v_lock = 0;
2908 v->di_tv.vval.v_dict = selfdict;
2909 ++selfdict->dv_refcount;
2910 }
2911
2912 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002913 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002914 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002915 * Set a:000 to a list with room for the "..." arguments.
2916 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002917 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002918 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002919 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002920 (varnumber_T)(argcount >= fp->uf_args.ga_len
2921 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002922 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002923 if ((fp->uf_flags & FC_NOARGS) == 0)
2924 {
2925 // Use "name" to avoid a warning from some compiler that checks the
2926 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002927 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002928 name = v->di_key;
2929 STRCPY(name, "000");
2930 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002931 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002932 v->di_tv.v_type = VAR_LIST;
2933 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002934 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002935 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002936 CLEAR_FIELD(fc->fc_l_varlist);
2937 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2938 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002939
2940 /*
2941 * Set a:firstline to "firstline" and a:lastline to "lastline".
2942 * Set a:name to named arguments.
2943 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002944 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002945 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002946 if ((fp->uf_flags & FC_NOARGS) == 0)
2947 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002948 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2949 "firstline", (varnumber_T)funcexe->fe_firstline);
2950 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2951 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002952 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002953 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002954 {
2955 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002956 typval_T def_rettv;
2957 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002958
2959 ai = i - fp->uf_args.ga_len;
2960 if (ai < 0)
2961 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002962 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002963 name = FUNCARG(fp, i);
2964 if (islambda)
2965 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002966
2967 // evaluate named argument default expression
2968 isdefault = ai + fp->uf_def_args.ga_len >= 0
2969 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2970 && argvars[i].vval.v_number == VVAL_NONE));
2971 if (isdefault)
2972 {
2973 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002974
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002975 def_rettv.v_type = VAR_NUMBER;
2976 def_rettv.vval.v_number = -1;
2977
2978 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2979 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002980 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002981 {
2982 default_arg_err = 1;
2983 break;
2984 }
2985 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002986 }
2987 else
2988 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002989 if ((fp->uf_flags & FC_NOARGS) != 0)
2990 // Bail out if no a: arguments used (in lambda).
2991 break;
2992
Bram Moolenaare38eab22019-12-05 21:50:01 +01002993 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002994 sprintf((char *)numbuf, "%d", ai + 1);
2995 name = numbuf;
2996 }
2997 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2998 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002999 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003000 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003001 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003002 }
3003 else
3004 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003005 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003006 if (v == NULL)
3007 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003008 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003009 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003010
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003011 // Note: the values are copied directly to avoid alloc/free.
3012 // "argvars" must have VAR_FIXED for v_lock.
3013 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003014 v->di_tv.v_lock = VAR_FIXED;
3015
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003016 if (isdefault)
3017 // Need to free this later, no matter where it's stored.
3018 tv_to_free[tv_to_free_len++] = &v->di_tv;
3019
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003020 if (addlocal)
3021 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003022 // Named arguments should be accessed without the "a:" prefix in
3023 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003024 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003025 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003026 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003027 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003028 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003029
3030 if (ai >= 0 && ai < MAX_FUNC_ARGS)
3031 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003032 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003033
3034 li->li_tv = argvars[i];
3035 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003036 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003037 }
3038 }
3039
Bram Moolenaare38eab22019-12-05 21:50:01 +01003040 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003041 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003042
3043 if (fp->uf_flags & FC_SANDBOX)
3044 {
3045 using_sandbox = TRUE;
3046 ++sandbox;
3047 }
3048
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003049 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003050 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003051 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003052 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003053 ++no_wait_return;
3054 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003055
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003056 smsg(_("calling %s"), SOURCING_NAME);
3057 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003058 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003059 char_u buf[MSG_BUF_LEN];
3060 char_u numbuf2[NUMBUFLEN];
3061 char_u *tofree;
3062 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003063
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003064 msg_puts("(");
3065 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003066 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003067 if (i > 0)
3068 msg_puts(", ");
3069 if (argvars[i].v_type == VAR_NUMBER)
3070 msg_outnum((long)argvars[i].vval.v_number);
3071 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003072 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003073 // Do not want errors such as E724 here.
3074 ++emsg_off;
3075 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3076 --emsg_off;
3077 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003078 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003079 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003080 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003081 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3082 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003083 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003084 msg_puts((char *)s);
3085 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003086 }
3087 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003088 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003089 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003090 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003091 msg_puts("\n"); // don't overwrite this either
3092
3093 verbose_leave_scroll();
3094 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003095 }
3096#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003097 if (do_profiling == PROF_YES)
3098 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003099 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003100#endif
3101
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003102 // "legacy" does not apply to commands in the function
3103 sticky_cmdmod_flags = 0;
3104
Bram Moolenaar98aff652022-09-06 21:02:35 +01003105 // If called from a compiled :def function the execution context must be
3106 // hidden, any deferred functions need to be added to the function being
3107 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003108 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003109
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003110 save_current_sctx = current_sctx;
3111 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003112 save_did_emsg = did_emsg;
3113 did_emsg = FALSE;
3114
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003115 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003116 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003117 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003118 retval = FCERR_FAILED;
3119 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003120 else if (islambda)
3121 {
3122 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3123
3124 // A Lambda always has the command "return {expr}". It is much faster
3125 // to evaluate {expr} directly.
3126 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003127 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003128 --ex_nesting_level;
3129 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003130 else
3131 // call do_cmdline() to execute the lines
3132 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003133 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3134
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003135 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003136 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003137
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003138 if (RedrawingDisabled > 0)
3139 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003140
Bram Moolenaare38eab22019-12-05 21:50:01 +01003141 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003142 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3143 {
3144 clear_tv(rettv);
3145 rettv->v_type = VAR_NUMBER;
3146 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003147
3148 // In corner cases returning a "failed" value is not backwards
3149 // compatible. Only do this for Vim9 script.
3150 if (in_vim9script())
3151 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003152 }
3153
3154#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003155 if (do_profiling == PROF_YES)
3156 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003157 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003158
3159 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3160 profile_may_end_func(&profile_info, fp, caller);
3161 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003162#endif
3163
Bram Moolenaare38eab22019-12-05 21:50:01 +01003164 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003165 if (p_verbose >= 12)
3166 {
3167 ++no_wait_return;
3168 verbose_enter_scroll();
3169
3170 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003171 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003172 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003173 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003174 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003175 else
3176 {
3177 char_u buf[MSG_BUF_LEN];
3178 char_u numbuf2[NUMBUFLEN];
3179 char_u *tofree;
3180 char_u *s;
3181
Bram Moolenaare38eab22019-12-05 21:50:01 +01003182 // The value may be very long. Skip the middle part, so that we
3183 // have some idea how it starts and ends. smsg() would always
3184 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003185 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003186 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003187 --emsg_off;
3188 if (s != NULL)
3189 {
3190 if (vim_strsize(s) > MSG_BUF_CLEN)
3191 {
3192 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3193 s = buf;
3194 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003195 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003196 vim_free(tofree);
3197 }
3198 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003199 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003200
3201 verbose_leave_scroll();
3202 --no_wait_return;
3203 }
3204
ichizok7e5fe382023-04-15 13:17:50 +01003205 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003206 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003207 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003208 restore_current_ectx(save_current_ectx);
3209
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003210#ifdef FEAT_PROFILE
3211 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003212 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003213#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003214 if (using_sandbox)
3215 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003216 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003217
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003218 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003219 {
3220 ++no_wait_return;
3221 verbose_enter_scroll();
3222
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003223 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003224 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003225
3226 verbose_leave_scroll();
3227 --no_wait_return;
3228 }
3229
3230 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003231 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003232 for (i = 0; i < tv_to_free_len; ++i)
3233 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003234 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003235
3236 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003237}
3238
3239/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003240 * Check the argument count for user function "fp".
3241 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3242 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003243 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003244check_user_func_argcount(ufunc_T *fp, int argcount)
3245{
3246 int regular_args = fp->uf_args.ga_len;
3247
3248 if (argcount < regular_args - fp->uf_def_args.ga_len)
3249 return FCERR_TOOFEW;
3250 else if (!has_varargs(fp) && argcount > regular_args)
3251 return FCERR_TOOMANY;
3252 return FCERR_UNKNOWN;
3253}
3254
3255/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003257 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003258 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259call_user_func_check(
3260 ufunc_T *fp,
3261 int argcount,
3262 typval_T *argvars,
3263 typval_T *rettv,
3264 funcexe_T *funcexe,
3265 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003266{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003267 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003268
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003269#ifdef FEAT_LUA
3270 if (fp->uf_flags & FC_CFUNC)
3271 {
3272 cfunc_T cb = fp->uf_cb;
3273
3274 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3275 }
3276#endif
3277
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003278 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3279 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003280 error = check_user_func_argcount(fp, argcount);
3281 if (error != FCERR_UNKNOWN)
3282 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003283
Bram Moolenaar50824712020-12-20 21:10:17 +01003284 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003285 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003287 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003289 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 int did_save_redo = FALSE;
3291 save_redo_T save_redo;
3292
3293 /*
3294 * Call the user function.
3295 * Save and restore search patterns, script variables and
3296 * redo buffer.
3297 */
3298 save_search_patterns();
3299 if (!ins_compl_active())
3300 {
3301 saveRedobuff(&save_redo);
3302 did_save_redo = TRUE;
3303 }
3304 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003305 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003306 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3308 // Function was unreferenced while being used, free it now.
3309 func_clear_free(fp, FALSE);
3310 if (did_save_redo)
3311 restoreRedobuff(&save_redo);
3312 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003313 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003314
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003315 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003316}
3317
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003318static funccal_entry_T *funccal_stack = NULL;
3319
3320/*
3321 * Save the current function call pointer, and set it to NULL.
3322 * Used when executing autocommands and for ":source".
3323 */
3324 void
3325save_funccal(funccal_entry_T *entry)
3326{
3327 entry->top_funccal = current_funccal;
3328 entry->next = funccal_stack;
3329 funccal_stack = entry;
3330 current_funccal = NULL;
3331}
3332
3333 void
3334restore_funccal(void)
3335{
3336 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003337 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003338 else
3339 {
3340 current_funccal = funccal_stack->top_funccal;
3341 funccal_stack = funccal_stack->next;
3342 }
3343}
3344
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003345 funccall_T *
3346get_current_funccal(void)
3347{
3348 return current_funccal;
3349}
3350
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003351/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003352 * Return TRUE when currently at the script level:
3353 * - not in a function
3354 * - not executing an autocommand
3355 * Note that when an autocommand sources a script the result is FALSE;
3356 */
3357 int
3358at_script_level(void)
3359{
3360 return current_funccal == NULL && autocmd_match == NULL;
3361}
3362
3363/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003364 * Mark all functions of script "sid" as deleted.
3365 */
3366 void
3367delete_script_functions(int sid)
3368{
3369 hashitem_T *hi;
3370 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003371 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003372 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003373 size_t len;
3374
3375 buf[0] = K_SPECIAL;
3376 buf[1] = KS_EXTRA;
3377 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003378 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003379 len = STRLEN(buf);
3380
Bram Moolenaarce658352020-07-31 23:47:12 +02003381 while (todo > 0)
3382 {
3383 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003384 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003385 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003386 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003387 fp = HI2UF(hi);
3388 if (STRNCMP(fp->uf_name, buf, len) == 0)
3389 {
3390 int changed = func_hashtab.ht_changed;
3391
3392 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003393
3394 if (fp->uf_calls > 0)
3395 {
3396 // Function is executing, don't free it but do remove
3397 // it from the hashtable.
3398 if (func_remove(fp))
3399 fp->uf_refcount--;
3400 }
3401 else
3402 {
3403 func_clear(fp, TRUE);
3404 // When clearing a function another function can be
3405 // cleared as a side effect. When that happens start
3406 // over.
3407 if (changed != func_hashtab.ht_changed)
3408 break;
3409 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003410 }
3411 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003412 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003413 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003414}
3415
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003416#if defined(EXITFREE) || defined(PROTO)
3417 void
3418free_all_functions(void)
3419{
3420 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003421 ufunc_T *fp;
3422 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003423 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003424 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003425
Bram Moolenaare38eab22019-12-05 21:50:01 +01003426 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003427 while (current_funccal != NULL)
3428 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003429 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003430 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003431 if (current_funccal == NULL && funccal_stack != NULL)
3432 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003433 }
3434
Bram Moolenaare38eab22019-12-05 21:50:01 +01003435 // First clear what the functions contain. Since this may lower the
3436 // reference count of a function, it may also free a function and change
3437 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003438 while (todo > 0)
3439 {
3440 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003441 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003442 if (!HASHITEM_EMPTY(hi))
3443 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444 // clear the def function index now
3445 fp = HI2UF(hi);
3446 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003447 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448
Bram Moolenaare38eab22019-12-05 21:50:01 +01003449 // Only free functions that are not refcounted, those are
3450 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003451 if (func_name_refcount(fp->uf_name))
3452 ++skipped;
3453 else
3454 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003455 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003456 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003457 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003458 {
3459 skipped = 0;
3460 break;
3461 }
3462 }
3463 --todo;
3464 }
3465 }
3466
Bram Moolenaare38eab22019-12-05 21:50:01 +01003467 // Now actually free the functions. Need to start all over every time,
3468 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003469 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003470 while (func_hashtab.ht_used > skipped)
3471 {
3472 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003473 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003474 if (!HASHITEM_EMPTY(hi))
3475 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003476 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003477 // Only free functions that are not refcounted, those are
3478 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003479 fp = HI2UF(hi);
3480 if (func_name_refcount(fp->uf_name))
3481 ++skipped;
3482 else
3483 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003484 if (func_free(fp, FALSE) == OK)
3485 {
3486 skipped = 0;
3487 break;
3488 }
3489 // did not actually free it
3490 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003491 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003492 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003493 }
3494 if (skipped == 0)
3495 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496
3497 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003498}
3499#endif
3500
3501/*
3502 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003503 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3504 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003505 * "len" is the length of "name", or -1 for NUL terminated.
3506 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003508builtin_function(char_u *name, int len)
3509{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003510 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003511
Bram Moolenaara26b9702020-04-18 19:53:28 +02003512 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003513 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003514 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3515 {
3516 if (name[i] == AUTOLOAD_CHAR)
3517 return FALSE;
3518 if (!eval_isnamec(name[i]))
3519 {
3520 // "name.something" is not a builtin function
3521 if (name[i] == '.')
3522 return FALSE;
3523 break;
3524 }
3525 }
3526 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003527}
3528
3529 int
3530func_call(
3531 char_u *name,
3532 typval_T *args,
3533 partial_T *partial,
3534 dict_T *selfdict,
3535 typval_T *rettv)
3536{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003537 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003538 listitem_T *item;
3539 typval_T argv[MAX_FUNC_ARGS + 1];
3540 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003541 int r = 0;
3542
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003543 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003544 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003545 {
3546 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3547 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003548 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003549 break;
3550 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003551 // Make a copy of each argument. This is needed to be able to set
3552 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003553 copy_tv(&item->li_tv, &argv[argc++]);
3554 }
3555
3556 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003557 {
3558 funcexe_T funcexe;
3559
Bram Moolenaara80faa82020-04-12 19:37:17 +02003560 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003561 funcexe.fe_firstline = curwin->w_cursor.lnum;
3562 funcexe.fe_lastline = curwin->w_cursor.lnum;
3563 funcexe.fe_evaluate = TRUE;
3564 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003565 if (partial != NULL)
3566 {
3567 funcexe.fe_object = partial->pt_obj;
3568 if (funcexe.fe_object != NULL)
3569 ++funcexe.fe_object->obj_refcount;
3570 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003571 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003572 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3573 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003574
Bram Moolenaare38eab22019-12-05 21:50:01 +01003575 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003576 while (argc > 0)
3577 clear_tv(&argv[--argc]);
3578
3579 return r;
3580}
3581
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003582static int callback_depth = 0;
3583
3584 int
3585get_callback_depth(void)
3586{
3587 return callback_depth;
3588}
3589
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003590/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003591 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003592 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003593 */
3594 int
3595call_callback(
3596 callback_T *callback,
3597 int len, // length of "name" or -1 to use strlen()
3598 typval_T *rettv, // return value goes here
3599 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003600 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003601 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003602{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003603 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003604 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003605
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003606 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3607 return FAIL;
Christian Brabandt47510f32023-10-15 09:56:16 +02003608
zeertzjqfe583b12023-12-21 16:59:26 +01003609 if (callback_depth > p_mfd)
Christian Brabandt47510f32023-10-15 09:56:16 +02003610 {
3611 emsg(_(e_command_too_recursive));
3612 return FAIL;
3613 }
3614
Bram Moolenaara80faa82020-04-12 19:37:17 +02003615 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003616 funcexe.fe_evaluate = TRUE;
3617 funcexe.fe_partial = callback->cb_partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003618 if (callback->cb_partial != NULL)
3619 {
3620 funcexe.fe_object = callback->cb_partial->pt_obj;
3621 if (funcexe.fe_object != NULL)
3622 ++funcexe.fe_object->obj_refcount;
3623 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003624 ++callback_depth;
3625 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3626 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003627
3628 // When a :def function was called that uses :try an error would be turned
3629 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003630 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003631 {
3632 need_rethrow = FALSE;
3633 handle_did_throw();
3634 }
3635
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003636 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003637}
3638
3639/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003640 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003641 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003642 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3643 */
3644 varnumber_T
3645call_callback_retnr(
3646 callback_T *callback,
3647 int argcount, // number of "argvars"
3648 typval_T *argvars) // vars for arguments, must have "argcount"
3649 // PLUS ONE elements!
3650{
3651 typval_T rettv;
3652 varnumber_T retval;
3653
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003654 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003655 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003656
3657 retval = tv_get_number_chk(&rettv, NULL);
3658 clear_tv(&rettv);
3659 return retval;
3660}
3661
3662/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 * Give an error message for the result of a function.
3664 * Nothing if "error" is FCERR_NONE.
3665 */
3666 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003667user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668{
3669 switch (error)
3670 {
3671 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003672 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003673 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003674 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003675 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 break;
3677 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003678 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 break;
3680 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003681 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 break;
3683 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003684 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 break;
3686 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003687 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 break;
3689 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003690 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 break;
3692 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003693 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3694 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003696 case FCERR_OTHER:
3697 case FCERR_FAILED:
3698 // assume the error message was already given
3699 break;
3700 case FCERR_NONE:
3701 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 }
3703}
3704
3705/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003706 * Check the argument types "argvars[argcount]" for "name" using the
3707 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3708 * is already included in "argvars[]".
3709 * Will do nothing if "funcexe->fe_check_type" is NULL or
3710 * "funcexe->fe_evaluate" is FALSE;
3711 * Returns an FCERR_ value.
3712 */
3713 static funcerror_T
3714may_check_argument_types(
3715 funcexe_T *funcexe,
3716 typval_T *argvars,
3717 int argcount,
3718 int base_included,
3719 char_u *name)
3720{
3721 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3722 {
3723 // Check that the argument types are OK for the types of the funcref.
3724 if (check_argument_types(funcexe->fe_check_type,
3725 argvars, argcount,
3726 base_included ? NULL : funcexe->fe_basetv,
3727 name) == FAIL)
3728 return FCERR_OTHER;
3729 }
3730 return FCERR_NONE;
3731}
3732
3733/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003734 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003735 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003736 * Return FAIL when the function can't be called, OK otherwise.
3737 * Also returns OK when an error was encountered while executing the function.
3738 */
3739 int
3740call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003741 char_u *funcname, // name of the function
3742 int len, // length of "name" or -1 to use strlen()
3743 typval_T *rettv, // return value goes here
3744 int argcount_in, // number of "argvars"
3745 typval_T *argvars_in, // vars for arguments, must have "argcount"
3746 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003747 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003748{
3749 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003750 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003751 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003752 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003753 char_u fname_buf[FLEN_FIXED + 1];
3754 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003755 char_u *fname = NULL;
3756 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003757 int argcount = argcount_in;
3758 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003759 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003760 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003761 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003762 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003763 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003764 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003765 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003766 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003767
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003768 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3769 // even when call_func() returns FAIL.
3770 rettv->v_type = VAR_UNKNOWN;
3771
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003772 if (partial != NULL)
3773 fp = partial->pt_func;
3774 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003775 fp = funcexe->fe_ufunc;
3776
3777 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003778 {
3779 // Make a copy of the name, if it comes from a funcref variable it
3780 // could be changed or deleted in the called function.
3781 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3782 if (name == NULL)
3783 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003784
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003785 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3786 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003787
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003788 if (funcexe->fe_doesrange != NULL)
3789 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003790
3791 if (partial != NULL)
3792 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003793 // When the function has a partial with a dict and there is a dict
3794 // argument, use the dict argument. That is backwards compatible.
3795 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003796 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003797 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003798 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003799 {
3800 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003801 {
3802 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3803 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003804 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003805 goto theend;
3806 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003807 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003808 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003809 for (i = 0; i < argcount_in; ++i)
3810 argv[i + argv_clear] = argvars_in[i];
3811 argvars = argv;
3812 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003813
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003814 if (funcexe->fe_check_type != NULL
3815 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003816 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003817 // Now funcexe->fe_check_type is missing the added arguments,
3818 // make a copy of the type with the correction.
3819 check_type = *funcexe->fe_check_type;
3820 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003821 check_type.tt_args = check_type_args;
3822 CLEAR_FIELD(check_type_args);
3823 for (i = 0; i < check_type.tt_argcount; ++i)
3824 check_type_args[i + partial->pt_argc] =
3825 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003826 check_type.tt_argcount += partial->pt_argc;
3827 check_type.tt_min_argcount += partial->pt_argc;
3828 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003829 }
3830 }
3831
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003832 if (error == FCERR_NONE)
3833 // check the argument types if possible
3834 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
3835 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003836
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003837 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003838 {
3839 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003840 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003841
Bram Moolenaar333894b2020-08-01 18:53:07 +02003842 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003843 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003844 {
3845 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003846 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003847 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003848
Bram Moolenaare38eab22019-12-05 21:50:01 +01003849 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003850 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003851 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003852
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003853 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003854 {
3855 /*
3856 * User defined function.
3857 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003858 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003859 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003860 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003861 if (fp != NULL && !is_global && in_vim9script()
3862 && func_requires_g_prefix(fp))
3863 // In Vim9 script g: is required to find a global
3864 // non-autoload function.
3865 fp = NULL;
3866 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003867
Bram Moolenaare38eab22019-12-05 21:50:01 +01003868 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003869 if (fp == NULL
3870 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003871 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003872 && !aborting())
3873 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003874 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003875 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003876 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003877 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003878 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3879 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003880 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003881 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003882 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003883 if (fp == NULL)
3884 {
3885 char_u *p = untrans_function_name(rfname);
3886
3887 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003888 // Don't do this if the name starts with "s:".
3889 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003890 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003891 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003892
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003893 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003894 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003895 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003896 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003897 int need_arg_check = FALSE;
3898 if (funcexe->fe_check_type == NULL)
3899 {
3900 funcexe->fe_check_type = fp->uf_func_type;
3901 need_arg_check = TRUE;
3902 }
3903
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003904 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003905 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003906 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003907 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003908 argv_clear, fp);
3909 need_arg_check = TRUE;
3910 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003911
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003912 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003913 {
3914 // Method call: base->Method()
3915 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003916 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003917 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003918 argvars = argv;
3919 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003920 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003921 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003922
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003923 // Check the argument types now that the function type and all
3924 // argument values are known, if not done above.
3925 if (need_arg_check)
3926 error = may_check_argument_types(funcexe, argvars, argcount,
3927 TRUE, (name != NULL) ? name : funcname);
3928 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
3929 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003931 }
3932 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003933 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003934 {
3935 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003936 * expr->method(): Find the method name in the table, call its
3937 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003938 */
3939 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003940 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003941 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003942 else
3943 {
3944 /*
3945 * Find the function name in the table, call its implementation.
3946 */
3947 error = call_internal_func(fname, argcount, argvars, rettv);
3948 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003949
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003950 /*
3951 * The function call (or "FuncUndefined" autocommand sequence) might
3952 * have been aborted by an error, an interrupt, or an explicitly thrown
3953 * exception that has not been caught so far. This situation can be
3954 * tested for by calling aborting(). For an error in an internal
3955 * function or for the "E132" error in call_user_func(), however, the
3956 * throw point at which the "force_abort" flag (temporarily reset by
3957 * emsg()) is normally updated has not been reached yet. We need to
3958 * update that flag first to make aborting() reliable.
3959 */
3960 update_force_abort();
3961 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003962 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003963 ret = OK;
3964
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003965theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003966 /*
3967 * Report an error unless the argument evaluation or function call has been
3968 * cancelled due to an aborting error, an interrupt, or an exception.
3969 */
3970 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003971 user_func_error(error, (name != NULL) ? name : funcname,
3972 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003973
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003974 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003975 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003976 clear_tv(&argv[--argv_clear + argv_base]);
3977
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003978 vim_free(tofree);
3979 vim_free(name);
3980
3981 return ret;
3982}
3983
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003984/*
3985 * Call a function without arguments, partial or dict.
3986 * This is like call_func() when the call is only "FuncName()".
3987 * To be used by "expr" options.
3988 * Returns NOTDONE when the function could not be found.
3989 */
3990 int
3991call_simple_func(
3992 char_u *funcname, // name of the function
3993 int len, // length of "name" or -1 to use strlen()
3994 typval_T *rettv) // return value goes here
3995{
3996 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003997 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003998 char_u fname_buf[FLEN_FIXED + 1];
3999 char_u *tofree = NULL;
4000 char_u *name;
4001 char_u *fname;
4002 char_u *rfname;
4003 int is_global = FALSE;
4004 ufunc_T *fp;
4005
4006 rettv->v_type = VAR_NUMBER; // default rettv is number zero
4007 rettv->vval.v_number = 0;
4008
4009 // Make a copy of the name, an option can be changed in the function.
4010 name = vim_strnsave(funcname, len);
4011 if (name == NULL)
4012 return ret;
4013
4014 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
4015
4016 // Skip "g:" before a function name.
4017 if (fname[0] == 'g' && fname[1] == ':')
4018 {
4019 is_global = TRUE;
4020 rfname = fname + 2;
4021 }
4022 else
4023 rfname = fname;
4024 fp = find_func(rfname, is_global);
4025 if (fp != NULL && !is_global && in_vim9script()
4026 && func_requires_g_prefix(fp))
4027 // In Vim9 script g: is required to find a global non-autoload
4028 // function.
4029 fp = NULL;
4030 if (fp == NULL)
4031 ret = NOTDONE;
4032 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
4033 error = FCERR_DELETED;
4034 else if (fp != NULL)
4035 {
4036 typval_T argvars[1];
4037 funcexe_T funcexe;
4038
4039 argvars[0].v_type = VAR_UNKNOWN;
4040 CLEAR_FIELD(funcexe);
4041 funcexe.fe_evaluate = TRUE;
4042
4043 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
4044 if (error == FCERR_NONE)
4045 ret = OK;
4046 }
4047
4048 user_func_error(error, name, FALSE);
4049 vim_free(tofree);
4050 vim_free(name);
4051
4052 return ret;
4053}
4054
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004055 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056printable_func_name(ufunc_T *fp)
4057{
4058 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4059}
4060
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004061/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004062 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4063 * TRUE. Otherwise return FALSE.
4064 */
4065 static int
4066function_list_modified(int prev_ht_changed)
4067{
4068 if (prev_ht_changed != func_hashtab.ht_changed)
4069 {
4070 emsg(_(e_function_list_was_modified));
4071 return TRUE;
4072 }
4073 return FALSE;
4074}
4075
4076/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004077 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004078 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004079 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004080list_func_head(ufunc_T *fp, int indent)
4081{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004082 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004083 int j;
4084
4085 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004086
4087 // a timer at the more prompt may have deleted the function
4088 if (function_list_modified(prev_ht_changed))
4089 return FAIL;
4090
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004091 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004092 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004093 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004094 msg_puts("def ");
4095 else
4096 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004098 msg_putchar('(');
4099 for (j = 0; j < fp->uf_args.ga_len; ++j)
4100 {
4101 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004102 msg_puts(", ");
4103 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 if (fp->uf_arg_types != NULL)
4105 {
4106 char *tofree;
4107
4108 msg_puts(": ");
4109 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4110 vim_free(tofree);
4111 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004112 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4113 {
4114 msg_puts(" = ");
4115 msg_puts(((char **)(fp->uf_def_args.ga_data))
4116 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4117 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004118 }
4119 if (fp->uf_varargs)
4120 {
4121 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004122 msg_puts(", ");
4123 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125 if (fp->uf_va_name != NULL)
4126 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004127 if (!fp->uf_varargs)
4128 {
4129 if (j)
4130 msg_puts(", ");
4131 msg_puts("...");
4132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004134 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135 {
4136 char *tofree;
4137
4138 msg_puts(": ");
4139 msg_puts(type_name(fp->uf_va_type, &tofree));
4140 vim_free(tofree);
4141 }
4142 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004143 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004144
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004145 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004146 {
4147 if (fp->uf_ret_type != &t_void)
4148 {
4149 char *tofree;
4150
4151 msg_puts(": ");
4152 msg_puts(type_name(fp->uf_ret_type, &tofree));
4153 vim_free(tofree);
4154 }
4155 }
4156 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004157 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004158 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004159 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004160 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004161 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004162 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004163 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004164 msg_clr_eos();
4165 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004166 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004167
4168 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004169}
4170
4171/*
4172 * Get a function name, translating "<SID>" and "<SNR>".
4173 * Also handles a Funcref in a List or Dictionary.
4174 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004175 * Set "*is_global" to TRUE when the function must be global, unless
4176 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004177 * flags:
4178 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004179 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004180 * TFN_QUIET: be quiet
4181 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004182 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004183 * Advances "pp" to just after the function name (if no error).
4184 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004185 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004186trans_function_name(
4187 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004188 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004189 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004190 int flags)
4191{
4192 return trans_function_name_ext(pp, is_global, skip, flags,
4193 NULL, NULL, NULL, NULL);
4194}
4195
4196/*
4197 * trans_function_name() with extra arguments.
4198 * "fdp", "partial", "type" and "ufunc" can be NULL.
4199 */
4200 char_u *
4201trans_function_name_ext(
4202 char_u **pp,
4203 int *is_global,
4204 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004205 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004206 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004207 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004208 type_T **type, // return: type of funcref
4209 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004210{
4211 char_u *name = NULL;
4212 char_u *start;
4213 char_u *end;
4214 int lead;
4215 char_u sid_buf[20];
4216 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004218 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004219 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004220 int vim9script = in_vim9script();
4221 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004222
4223 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004224 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004225 start = *pp;
4226
Bram Moolenaare38eab22019-12-05 21:50:01 +01004227 // Check for hard coded <SNR>: already translated function ID (from a user
4228 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004229 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4230 && (*pp)[2] == (int)KE_SNR)
4231 {
4232 *pp += 3;
4233 len = get_id_len(pp) + 3;
4234 return vim_strnsave(start, len);
4235 }
4236
Bram Moolenaare38eab22019-12-05 21:50:01 +01004237 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4238 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004239 lead = eval_fname_script(start);
4240 if (lead > 2)
4241 start += lead;
4242
Bram Moolenaare38eab22019-12-05 21:50:01 +01004243 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004244 end = get_lval(start, NULL, &lv, FALSE, skip,
4245 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004246 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004247 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004248 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004249 {
4250 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004251 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004252 goto theend;
4253 }
4254 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4255 {
4256 /*
4257 * Report an invalid expression in braces, unless the expression
4258 * evaluation has been cancelled due to an aborting error, an
4259 * interrupt, or an exception.
4260 */
4261 if (!aborting())
4262 {
4263 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004264 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004265 }
4266 else
4267 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4268 goto theend;
4269 }
4270
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004271 if (lv.ll_ufunc != NULL)
4272 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004273 if (ufunc != NULL)
4274 *ufunc = lv.ll_ufunc;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004275 name = vim_strsave(lv.ll_ufunc->uf_name);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004276 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004277 goto theend;
4278 }
4279
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004280 if (lv.ll_tv != NULL)
4281 {
4282 if (fdp != NULL)
4283 {
4284 fdp->fd_dict = lv.ll_dict;
4285 fdp->fd_newkey = lv.ll_newkey;
4286 lv.ll_newkey = NULL;
4287 fdp->fd_di = lv.ll_di;
4288 }
4289 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4290 {
4291 name = vim_strsave(lv.ll_tv->vval.v_string);
4292 *pp = end;
4293 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004294 else if (lv.ll_tv->v_type == VAR_CLASS
4295 && lv.ll_tv->vval.v_class != NULL)
4296 {
4297 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4298 *pp = end;
4299 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004300 else if (lv.ll_tv->v_type == VAR_PARTIAL
4301 && lv.ll_tv->vval.v_partial != NULL)
4302 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004303 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004304 *pp = end;
4305 if (partial != NULL)
4306 *partial = lv.ll_tv->vval.v_partial;
4307 }
4308 else
4309 {
4310 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4311 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004312 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004313 else
4314 *pp = end;
4315 name = NULL;
4316 }
4317 goto theend;
4318 }
4319
4320 if (lv.ll_name == NULL)
4321 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004322 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004323 *pp = end;
4324 goto theend;
4325 }
4326
Bram Moolenaare38eab22019-12-05 21:50:01 +01004327 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004328 if (lv.ll_exp_name != NULL)
4329 {
4330 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004331 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004332 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004333 if (name == lv.ll_exp_name)
4334 name = NULL;
4335 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004336 else if (lv.ll_sid > 0)
4337 {
4338 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4339 int cc = *lv.ll_name_end;
4340
4341 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4342 *lv.ll_name_end = NUL;
4343 if (si->sn_autoload_prefix != NULL)
4344 {
4345 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4346 }
4347 else
4348 {
4349 sid_buf[0] = K_SPECIAL;
4350 sid_buf[1] = KS_EXTRA;
4351 sid_buf[2] = (int)KE_SNR;
4352 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004353 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004354 name = concat_str(sid_buf, lv.ll_name);
4355 }
4356 *lv.ll_name_end = cc;
4357 *pp = end;
4358 goto theend;
4359 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004360 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004361 {
4362 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004363 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004364 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004365 if (name == *pp)
4366 name = NULL;
4367 }
4368 if (name != NULL)
4369 {
4370 name = vim_strsave(name);
4371 *pp = end;
4372 if (STRNCMP(name, "<SNR>", 5) == 0)
4373 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004374 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004375 name[0] = K_SPECIAL;
4376 name[1] = KS_EXTRA;
4377 name[2] = (int)KE_SNR;
4378 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4379 }
4380 goto theend;
4381 }
4382
4383 if (lv.ll_exp_name != NULL)
4384 {
4385 len = (int)STRLEN(lv.ll_exp_name);
4386 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4387 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4388 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004389 // When there was "s:" already or the name expanded to get a
4390 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004391 lv.ll_name += 2;
4392 len -= 2;
4393 lead = 2;
4394 }
4395 }
4396 else
4397 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004398 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004399 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004400 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004401 if (lv.ll_name[0] == 'g')
4402 {
4403 if (is_global != NULL)
4404 {
4405 *is_global = TRUE;
4406 }
4407 else
4408 {
4409 // dropping "g:" without setting "is_global" won't work in
4410 // Vim9script, put it back later
4411 prefix_g = TRUE;
4412 extra = 2;
4413 }
4414 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004415 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004416 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004417 len = (int)(end - lv.ll_name);
4418 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004419 if (len <= 0)
4420 {
4421 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004422 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004423 goto theend;
4424 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004425
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004426 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004427 // starts with a lower case character: dict.func(). Or when in a class.
4428 vim9_local = ASCII_ISUPPER(*start) && vim9script
4429 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004430
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004431 /*
4432 * Copy the function name to allocated memory.
4433 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4434 * Accept <SNR>123_name() outside a script.
4435 */
4436 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004437 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004438 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004439 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004440 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004441 {
Kota Kato948a3892022-08-16 16:09:59 +01004442 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4443 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004444 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004445 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004446 goto theend;
4447 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004449 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004450 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004452 || eval_fname_sid(*pp))
4453 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004454 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004455 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004456 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004457 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004458 goto theend;
4459 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004460 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004461 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462 extra = 3 + (int)STRLEN(sid_buf);
4463 else
4464 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004465 }
4466 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004467 // The function name must start with an upper case letter (unless it is a
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004468 // Vim9 class new() function or a Vim9 class private method or one of the
4469 // supported Vim9 object builtin functions)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004470 else if (!(flags & TFN_INT)
4471 && (builtin_function(lv.ll_name, len)
4472 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004473 && !((flags & TFN_IN_CLASS)
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004474 && (is_valid_builtin_obj_methodname(lv.ll_name)
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004475 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004476 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004477 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004478 : e_function_name_must_start_with_capital_or_s_str),
4479 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004480 goto theend;
4481 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004482 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004483 {
4484 char_u *cp = vim_strchr(lv.ll_name, ':');
4485
4486 if (cp != NULL && cp < end)
4487 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004488 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004489 goto theend;
4490 }
4491 }
4492
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004494 if (name != NULL)
4495 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004496 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004497 {
4498 name[0] = K_SPECIAL;
4499 name[1] = KS_EXTRA;
4500 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004501 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004502 STRCPY(name + 3, sid_buf);
4503 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004504 else if (prefix_g)
4505 {
4506 name[0] = 'g';
4507 name[1] = ':';
4508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004509 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4510 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004511 }
4512 *pp = end;
4513
4514theend:
4515 clear_lval(&lv);
4516 return name;
4517}
4518
4519/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004520 * Assuming "name" is the result of trans_function_name() and it was prefixed
4521 * to use the script-local name, return the unmodified name (points into
4522 * "name"). Otherwise return NULL.
4523 * This can be used to first search for a script-local function and fall back
4524 * to the global function if not found.
4525 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004526 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004527untrans_function_name(char_u *name)
4528{
4529 char_u *p;
4530
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004531 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004532 {
4533 p = vim_strchr(name, '_');
4534 if (p != NULL)
4535 return p + 1;
4536 }
4537 return NULL;
4538}
4539
4540/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004541 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4542 * current script ID and returns the expanded function name. The caller should
4543 * free the returned name. If not called from a script context or the function
4544 * name doesn't start with these prefixes, then returns NULL.
4545 * This doesn't check whether the script-local function exists or not.
4546 */
4547 char_u *
4548get_scriptlocal_funcname(char_u *funcname)
4549{
4550 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004551 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004552 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004553 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004554
4555 if (funcname == NULL)
4556 return NULL;
4557
4558 if (STRNCMP(funcname, "s:", 2) != 0
4559 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004560 {
4561 ufunc_T *ufunc;
4562
4563 // The function name does not have a script-local prefix. Try finding
4564 // it when in a Vim9 script and there is no "g:" prefix.
4565 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4566 return NULL;
4567 ufunc = find_func(funcname, FALSE);
4568 if (ufunc == NULL || func_is_global(ufunc)
4569 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4570 return NULL;
4571 ++p;
4572 off = 0;
4573 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004574 else
4575 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004576
4577 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4578 {
4579 emsg(_(e_using_sid_not_in_script_context));
4580 return NULL;
4581 }
4582 // Expand s: prefix into <SNR>nr_<name>
4583 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4584 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004585 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004586 if (newname == NULL)
4587 return NULL;
4588 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004589 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004590
4591 return newname;
4592}
4593
4594/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004595 * Return script-local "fname" with the 3-byte sequence replaced by
4596 * printable <SNR> in allocated memory.
4597 */
4598 char_u *
4599alloc_printable_func_name(char_u *fname)
4600{
4601 char_u *n = alloc(STRLEN(fname + 3) + 6);
4602
4603 if (n != NULL)
4604 {
4605 STRCPY(n, "<SNR>");
4606 STRCPY(n + 5, fname + 3);
4607 }
4608 return n;
4609}
4610
4611/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004612 * Call trans_function_name(), except that a lambda is returned as-is.
4613 * Returns the name in allocated memory.
4614 */
4615 char_u *
4616save_function_name(
4617 char_u **name,
4618 int *is_global,
4619 int skip,
4620 int flags,
4621 funcdict_T *fudi)
4622{
4623 char_u *p = *name;
4624 char_u *saved;
4625
4626 if (STRNCMP(p, "<lambda>", 8) == 0)
4627 {
4628 p += 8;
4629 (void)getdigits(&p);
4630 saved = vim_strnsave(*name, p - *name);
4631 if (fudi != NULL)
4632 CLEAR_POINTER(fudi);
4633 }
4634 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004635 saved = trans_function_name_ext(&p, is_global, skip,
4636 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004637 *name = p;
4638 return saved;
4639}
4640
4641/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004642 * List functions. When "regmatch" is NULL all of then.
4643 * Otherwise functions matching "regmatch".
4644 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004645 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004646list_functions(regmatch_T *regmatch)
4647{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004648 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004649 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004650 hashitem_T *hi;
4651
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004652 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004653 {
4654 if (!HASHITEM_EMPTY(hi))
4655 {
4656 ufunc_T *fp = HI2UF(hi);
4657
4658 --todo;
4659 if ((fp->uf_flags & FC_DEAD) == 0
4660 && (regmatch == NULL
4661 ? !message_filtered(fp->uf_name)
4662 && !func_name_refcount(fp->uf_name)
Keith Thompson184f71c2024-01-04 21:19:04 +01004663 : !SAFE_isdigit(*fp->uf_name)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004664 && vim_regexec(regmatch, fp->uf_name, 0)))
4665 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004666 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004667 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004668 if (function_list_modified(prev_ht_changed))
4669 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004670 }
4671 }
4672 }
4673}
4674
4675/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004676 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004677 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4678 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004679 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004680 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4681 * With CF_INTERFACE the function is define inside an interface, only the
4682 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004683 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004684 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004685 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004686define_function(
4687 exarg_T *eap,
4688 char_u *name_arg,
4689 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004690 int class_flags,
4691 ocmember_T *obj_members,
4692 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004693{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004694 int j;
4695 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004696 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004697 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004698 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004699 char_u *p;
4700 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004701 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004702 char_u *line_arg = NULL;
4703 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004704 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004705 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004706 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004707 garray_T newlines;
4708 int varargs = FALSE;
4709 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004711 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004712 int fp_allocated = FALSE;
4713 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004714 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004715 dictitem_T *v;
4716 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004717 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004718 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004719 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004720 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004721 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004722 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004723
4724 /*
4725 * ":function" without argument: list functions.
4726 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004727 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004728 {
4729 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004730 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004731 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004732 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004733 }
4734
4735 /*
4736 * ":function /pat": list functions matching pattern.
4737 */
4738 if (*eap->arg == '/')
4739 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004740 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004741 if (!eap->skip)
4742 {
4743 regmatch_T regmatch;
4744
4745 c = *p;
4746 *p = NUL;
4747 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4748 *p = c;
4749 if (regmatch.regprog != NULL)
4750 {
4751 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004752 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004753 vim_regfree(regmatch.regprog);
4754 }
4755 }
4756 if (*p == '/')
4757 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004758 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004759 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004760 }
4761
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 ga_init(&newargs);
4763 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004764 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765 ga_init(&default_args);
4766
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004767 /*
4768 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004769 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004770 * "name" == func, "fudi.fd_dict" == NULL
4771 * dict.func new dictionary entry
4772 * "name" == NULL, "fudi.fd_dict" set,
4773 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4774 * dict.func existing dict entry with a Funcref
4775 * "name" == func, "fudi.fd_dict" set,
4776 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4777 * dict.func existing dict entry that's not a Funcref
4778 * "name" == NULL, "fudi.fd_dict" set,
4779 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4780 * s:func script-local function name
4781 * g:func global function name, same as "func"
4782 */
4783 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004784 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004785 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004786 // nested function, argument is (args).
4787 paren = TRUE;
4788 CLEAR_FIELD(fudi);
4789 }
4790 else
4791 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004792 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004793 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004794 if (p[0] == 's' && p[1] == ':')
4795 {
4796 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4797 return NULL;
4798 }
4799 p = to_name_end(p, TRUE);
4800 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4801 {
4802 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4803 eap->arg);
4804 return NULL;
4805 }
4806 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004807 }
4808
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004809 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004810 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004811 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004812 paren = (vim_strchr(p, '(') != NULL);
4813 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004814 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004815 /*
4816 * Return on an invalid expression in braces, unless the expression
4817 * evaluation has been cancelled due to an aborting error, an
4818 * interrupt, or an exception.
4819 */
4820 if (!aborting())
4821 {
4822 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004823 semsg(_(e_key_not_present_in_dictionary_str),
4824 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004825 vim_free(fudi.fd_newkey);
4826 return NULL;
4827 }
4828 else
4829 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004830 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004831
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004832 // For "export def FuncName()" in an autoload script the function name
4833 // is stored with the legacy autoload name "dir#script#FuncName" so
4834 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004835 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004836 {
4837 char_u *prefixed = may_prefix_autoload(name);
4838
4839 if (prefixed != NULL && prefixed != name)
4840 {
4841 vim_free(name);
4842 name = prefixed;
4843 }
4844 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004845 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004846 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004847 {
4848 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4849 goto ret_free;
4850 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004851 }
4852
Bram Moolenaare38eab22019-12-05 21:50:01 +01004853 // An error in a function call during evaluation of an expression in magic
4854 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004855 saved_did_emsg = did_emsg;
4856 did_emsg = FALSE;
4857
4858 /*
4859 * ":function func" with only function name: list function.
4860 */
4861 if (!paren)
4862 {
4863 if (!ends_excmd(*skipwhite(p)))
4864 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004865 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004866 goto ret_free;
4867 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004868 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004869 if (eap->nextcmd != NULL)
4870 *p = NUL;
4871 if (!eap->skip && !got_int)
4872 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004873 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004874 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4875 {
4876 char_u *up = untrans_function_name(name);
4877
4878 // With Vim9 script the name was made script-local, if not
4879 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004880 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004881 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004882 }
4883
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004884 if (fp != NULL)
4885 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004886 // Check no function was added or removed from a timer, e.g. at
4887 // the more prompt. "fp" may then be invalid.
4888 int prev_ht_changed = func_hashtab.ht_changed;
4889
4890 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004891 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004892 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4893 {
4894 if (FUNCLINE(fp, j) == NULL)
4895 continue;
4896 msg_putchar('\n');
4897 msg_outnum((long)(j + 1));
4898 if (j < 9)
4899 msg_putchar(' ');
4900 if (j < 99)
4901 msg_putchar(' ');
4902 if (function_list_modified(prev_ht_changed))
4903 break;
4904 msg_prt_line(FUNCLINE(fp, j), FALSE);
4905 out_flush(); // show a line at a time
4906 ui_breakcheck();
4907 }
4908 if (!got_int)
4909 {
4910 msg_putchar('\n');
4911 if (!function_list_modified(prev_ht_changed))
4912 {
4913 if (fp->uf_def_status != UF_NOT_COMPILED)
4914 msg_puts(" enddef");
4915 else
4916 msg_puts(" endfunction");
4917 }
4918 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004919 }
4920 }
4921 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004922 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004923 }
4924 goto ret_free;
4925 }
4926
4927 /*
4928 * ":function name(arg1, arg2)" Define function.
4929 */
4930 p = skipwhite(p);
4931 if (*p != '(')
4932 {
4933 if (!eap->skip)
4934 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004935 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004936 goto ret_free;
4937 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004938 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004939 if (vim_strchr(p, '(') != NULL)
4940 p = vim_strchr(p, '(');
4941 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004942
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004943 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4944 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004945 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004946 goto ret_free;
4947 }
4948
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004949 // In Vim9 script only global functions can be redefined.
4950 if (vim9script && eap->forceit && !is_global)
4951 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004952 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004953 goto ret_free;
4954 }
4955
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004956 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004957
Bram Moolenaar04b12692020-05-04 23:24:44 +02004958 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004959 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004960 // Check the name of the function. Unless it's a dictionary function
4961 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004962 if (name != NULL)
4963 arg = name;
4964 else
4965 arg = fudi.fd_newkey;
4966 if (arg != NULL && (fudi.fd_di == NULL
4967 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4968 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4969 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004970 char_u *name_base = arg;
4971 int i;
4972
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004973 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004974 {
4975 name_base = vim_strchr(arg, '_');
4976 if (name_base == NULL)
4977 name_base = arg + 3;
4978 else
4979 ++name_base;
4980 }
4981 for (i = 0; name_base[i] != NUL && (i == 0
4982 ? eval_isnamec1(name_base[i])
4983 : eval_isnamec(name_base[i])); ++i)
4984 ;
4985 if (name_base[i] != NUL)
zeertzjq6a04bf52024-03-16 09:39:06 +01004986 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004987 emsg_funcname(e_invalid_argument_str, arg);
zeertzjq6a04bf52024-03-16 09:39:06 +01004988 goto ret_free;
4989 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00004990
4991 // In Vim9 script a function cannot have the same name as a
4992 // variable.
4993 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004994 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4995 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004996 + EVAL_VAR_NO_FUNC) == OK)
4997 {
4998 semsg(_(e_redefining_script_item_str), name_base);
4999 goto ret_free;
5000 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005001 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005002 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005003 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005004 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005005 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00005006 goto ret_free;
5007 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005008 }
5009
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005010 // This may get more lines and make the pointers into the first line
5011 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01005012 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00005014 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02005015 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005016 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00005017 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005018 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01005019 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005020
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005021 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005022 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005023 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005024 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005025 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005026 if (*p != ':')
5027 {
5028 semsg(_(e_no_white_space_allowed_before_colon_str), p);
5029 p = skipwhite(p);
5030 }
5031 else if (!IS_WHITE_OR_NUL(p[1]))
5032 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02005034 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005035 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005036 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005037 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01005038 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005039 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005040 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005041 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005042 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005043 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02005044 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005045 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005046 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02005047 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005048 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005049 else
5050 // find extra arguments "range", "dict", "abort" and "closure"
5051 for (;;)
5052 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01005053 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005054 p = skipwhite(p);
5055 if (STRNCMP(p, "range", 5) == 0)
5056 {
5057 flags |= FC_RANGE;
5058 p += 5;
5059 }
5060 else if (STRNCMP(p, "dict", 4) == 0)
5061 {
5062 flags |= FC_DICT;
5063 p += 4;
5064 }
5065 else if (STRNCMP(p, "abort", 5) == 0)
5066 {
5067 flags |= FC_ABORT;
5068 p += 5;
5069 }
5070 else if (STRNCMP(p, "closure", 7) == 0)
5071 {
5072 flags |= FC_CLOSURE;
5073 p += 7;
5074 if (current_funccal == NULL)
5075 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005076 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005077 name == NULL ? (char_u *)"" : name);
5078 goto erret;
5079 }
5080 }
5081 else
5082 break;
5083 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005084
Bram Moolenaare38eab22019-12-05 21:50:01 +01005085 // When there is a line break use what follows for the function body.
5086 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005087 if (*p == '\n')
5088 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005089 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005090 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5091 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005092 && !(VIM_ISWHITE(*whitep) && *p == '#'
5093 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005094 && !eap->skip
5095 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005096 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005097
5098 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005099 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5100 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005101 */
5102 if (KeyTyped)
5103 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005104 // Check if the function already exists, don't let the user type the
5105 // whole function before telling him it doesn't work! For a script we
5106 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005107 if (!eap->skip && !eap->forceit)
5108 {
5109 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005110 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005111 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005112 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005113 }
5114
5115 if (!eap->skip && did_emsg)
5116 goto erret;
5117
Bram Moolenaare38eab22019-12-05 21:50:01 +01005118 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005119 cmdline_row = msg_row;
5120 }
5121
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005122 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005123 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005124
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005125 // Do not define the function when getting the body fails and when
5126 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005127 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005128 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005129 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5130 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005131 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005132 goto erret;
5133
5134 /*
5135 * If there are no errors, add the function
5136 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005137 if (fudi.fd_dict != NULL)
5138 {
5139 char numbuf[20];
5140
5141 fp = NULL;
5142 if (fudi.fd_newkey == NULL && !eap->forceit)
5143 {
5144 emsg(_(e_dictionary_entry_already_exists));
5145 goto erret;
5146 }
5147 if (fudi.fd_di == NULL)
5148 {
5149 // Can't add a function to a locked dictionary
5150 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5151 goto erret;
5152 }
5153 // Can't change an existing function if it is locked
5154 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5155 goto erret;
5156
5157 // Give the function a sequential number. Can only be used with a
5158 // Funcref!
5159 vim_free(name);
5160 sprintf(numbuf, "%d", ++func_nr);
5161 name = vim_strsave((char_u *)numbuf);
5162 if (name == NULL)
5163 goto erret;
5164 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005165 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005166 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005167 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005168 char_u *find_name = name;
5169 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005170 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005171
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005172 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005173 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005174 var_conflict = TRUE;
5175
5176 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5177 {
5178 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5179
5180 if (si->sn_autoload_prefix != NULL)
5181 {
5182 if (is_export)
5183 {
5184 find_name = name + STRLEN(si->sn_autoload_prefix);
5185 v = find_var(find_name, &ht, TRUE);
5186 if (v != NULL)
5187 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005188 // Only check if the function already exists in the script,
5189 // global functions can be shadowed.
5190 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005191 }
5192 else
5193 {
5194 char_u *prefixed = may_prefix_autoload(name);
5195
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005196 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005197 {
5198 v = find_var(prefixed, &ht, TRUE);
5199 if (v != NULL)
5200 var_conflict = TRUE;
5201 vim_free(prefixed);
5202 }
5203 }
5204 }
5205 }
5206 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005207 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005208 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005209 goto erret;
5210 }
5211
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005212 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005213 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005214 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005215 char_u *uname = untrans_function_name(name);
5216
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005217 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005218 }
5219
5220 if (fp != NULL || import != NULL)
5221 {
5222 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005223
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005224 // Function can be replaced with "function!" and when sourcing the
5225 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005226 // A name that is used by an import can not be overruled.
5227 if (import != NULL
5228 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005229 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005230 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005231 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005232 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005233 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005234 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005235 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005236 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005237 goto erret;
5238 }
5239 if (fp->uf_calls > 0)
5240 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005241 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005242 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005243 goto erret;
5244 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005245 if (fp->uf_refcount > 1)
5246 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005247 // This function is referenced somewhere, don't redefine it but
5248 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005249 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005250 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005251 fp = NULL;
5252 overwrite = TRUE;
5253 }
5254 else
5255 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005256 char_u *exp_name = fp->uf_name_exp;
5257
5258 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005259 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005260 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005261 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005262 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005263 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005264#ifdef FEAT_PROFILE
5265 fp->uf_profiling = FALSE;
5266 fp->uf_prof_initialized = FALSE;
5267#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005268 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005269 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005270 }
5271 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005272
5273 if (fp == NULL)
5274 {
5275 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5276 {
5277 int slen, plen;
5278 char_u *scriptname;
5279
Bram Moolenaare38eab22019-12-05 21:50:01 +01005280 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005281 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005282 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005283 {
5284 scriptname = autoload_name(name);
5285 if (scriptname != NULL)
5286 {
5287 p = vim_strchr(scriptname, '/');
5288 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005289 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005290 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005291 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005292 j = OK;
5293 vim_free(scriptname);
5294 }
5295 }
5296 if (j == FAIL)
5297 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005298 linenr_T save_lnum = SOURCING_LNUM;
5299
5300 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005301 semsg(_(e_function_name_does_not_match_script_file_name_str),
5302 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005303 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005304 goto erret;
5305 }
5306 }
5307
Bram Moolenaare01e5212023-01-08 20:31:18 +00005308 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005309 if (fp == NULL)
5310 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005311 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005312
5313 if (fudi.fd_dict != NULL)
5314 {
5315 if (fudi.fd_di == NULL)
5316 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005317 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005318 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5319 if (fudi.fd_di == NULL)
5320 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005321 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005322 goto erret;
5323 }
5324 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5325 {
5326 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005327 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005328 goto erret;
5329 }
5330 }
5331 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005332 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005333 clear_tv(&fudi.fd_di->di_tv);
5334 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005335 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005336
Bram Moolenaare38eab22019-12-05 21:50:01 +01005337 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005338 flags |= FC_DICT;
5339 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005340 }
5341 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005342 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005343 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005344 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005345
5346 if (eap->cmdidx == CMD_def)
5347 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005348 int lnum_save = SOURCING_LNUM;
5349 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005350
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005351 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005352
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005353 // error messages are for the first function line
5354 SOURCING_LNUM = sourcing_lnum_top;
5355
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005356 // The function may use script variables from the context.
5357 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005358
h-eastb895b0f2023-09-24 15:46:31 +02005359 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5360 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005361 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005362 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005363 free_fp = fp_allocated;
5364 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005365 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005366 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005367
5368 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005369 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005370 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005371 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005372 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005373 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005374 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005375 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005376 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005377 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005378 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005379
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005380 if (fp_allocated)
5381 {
5382 // insert the new function in the function list
5383 set_ufunc_name(fp, name);
5384 if (overwrite)
5385 {
5386 hi = hash_find(&func_hashtab, name);
5387 hi->hi_key = UF2HIKEY(fp);
5388 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005389 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005390 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005391 {
5392 free_fp = TRUE;
5393 goto erret;
5394 }
5395 fp->uf_refcount = 1;
5396 }
5397
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005398 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005399 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005400 if ((flags & FC_CLOSURE) != 0)
5401 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005402 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005403 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005404 }
5405 else
5406 fp->uf_scoped = NULL;
5407
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005408#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005409 if (prof_def_func())
5410 func_do_profile(fp);
5411#endif
5412 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005413 if (sandbox)
5414 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005415 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005416 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005417 fp->uf_flags = flags;
5418 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005419 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005420 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005421 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005422 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005423 if (is_export)
5424 {
5425 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005426 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005427 is_export = FALSE;
5428 }
5429
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005430 if (eap->cmdidx == CMD_def)
5431 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005432 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5433 // :func does not use Vim9 script syntax, even in a Vim9 script file
5434 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005435
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005436 goto ret_free;
5437
5438erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005439 if (fp != NULL)
5440 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005441 // these were set to "newargs" and "default_args", which are cleared
5442 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005443 ga_init(&fp->uf_args);
5444 ga_init(&fp->uf_def_args);
5445 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005446errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005447 ga_clear_strings(&newargs);
5448 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005449 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005450 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005451 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005452 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005453 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01005454 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005455 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005456 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005457 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005458ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005459 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005460 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005461 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005462 if (name != name_arg)
5463 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005464 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005465 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005466
5467 return fp;
5468}
5469
5470/*
5471 * ":function"
5472 */
5473 void
5474ex_function(exarg_T *eap)
5475{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005476 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005477
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005478 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005479 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005480 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005481}
5482
5483/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005484 * Find a function by name, including "<lambda>123".
5485 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005486 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005487 * Return NULL if not found.
5488 */
5489 ufunc_T *
5490find_func_by_name(char_u *name, compiletype_T *compile_type)
5491{
5492 char_u *arg = name;
5493 char_u *fname;
5494 ufunc_T *ufunc;
5495 int is_global = FALSE;
5496
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005497 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5498 {
5499 *compile_type = CT_PROFILE;
5500 arg = skipwhite(arg + 7);
5501 }
5502 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5503 {
5504 *compile_type = CT_DEBUG;
5505 arg = skipwhite(arg + 5);
5506 }
5507
5508 if (STRNCMP(arg, "<lambda>", 8) == 0)
5509 {
5510 arg += 8;
5511 (void)getdigits(&arg);
5512 fname = vim_strnsave(name, arg - name);
5513 }
5514 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005515 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005516 // First try finding a method in a class, trans_function_name() will
5517 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005518 ufunc = find_class_func(&arg);
5519 if (ufunc != NULL)
5520 return ufunc;
5521
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005522 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005523 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005524 NULL, NULL, NULL, &ufunc);
5525 if (ufunc != NULL)
5526 {
5527 vim_free(fname);
5528 return ufunc;
5529 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005530 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005531 if (fname == NULL)
5532 {
5533 semsg(_(e_invalid_argument_str), name);
5534 return NULL;
5535 }
5536 if (!ends_excmd2(name, arg))
5537 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005538 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005539 emsg(ex_errmsg(e_trailing_characters_str, arg));
5540 return NULL;
5541 }
5542
5543 ufunc = find_func(fname, is_global);
5544 if (ufunc == NULL)
5545 {
5546 char_u *p = untrans_function_name(fname);
5547
5548 if (p != NULL)
5549 // Try again without making it script-local.
5550 ufunc = find_func(p, FALSE);
5551 }
5552 vim_free(fname);
5553 if (ufunc == NULL)
5554 semsg(_(e_cannot_find_function_str), name);
5555 return ufunc;
5556}
5557
5558/*
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005559 * Compile the :def function "ufunc". If "cl" is not NULL, then compile the
5560 * class or object method "ufunc" in "cl".
5561 */
5562 void
5563defcompile_function(ufunc_T *ufunc, class_T *cl)
5564{
5565 compiletype_T compile_type = CT_NONE;
5566
5567 if (func_needs_compiling(ufunc, compile_type))
5568 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5569 else
5570 smsg(_("Function %s%s%s does not need compiling"),
5571 cl != NULL ? cl->class_name : (char_u *)"",
5572 cl != NULL ? (char_u *)"." : (char_u *)"",
5573 ufunc->uf_name);
5574}
5575
5576/*
5577 * Compile all the :def functions defined in the current script
5578 */
5579 static void
5580defcompile_funcs_in_script(void)
5581{
5582 long todo = (long)func_hashtab.ht_used;
5583 int changed = func_hashtab.ht_changed;
5584 hashitem_T *hi;
5585
5586 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5587 {
5588 if (!HASHITEM_EMPTY(hi))
5589 {
5590 --todo;
5591 ufunc_T *ufunc = HI2UF(hi);
5592 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5593 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5594 && (ufunc->uf_flags & FC_DEAD) == 0)
5595 {
5596 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5597
5598 if (func_hashtab.ht_changed != changed)
5599 {
5600 // a function has been added or removed, need to start
5601 // over
5602 todo = (long)func_hashtab.ht_used;
5603 changed = func_hashtab.ht_changed;
5604 hi = func_hashtab.ht_array;
5605 --hi;
5606 }
5607 }
5608 }
5609 }
5610}
5611
5612/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005613 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005614 * be compiled or the one specified by the argument.
5615 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005616 */
5617 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005618ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005619{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005620 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005621 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005622 typval_T tv;
5623
5624 if (is_class_name(eap->arg, &tv))
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005625 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005626 class_T *cl = tv.vval.v_class;
5627
5628 if (cl != NULL)
5629 defcompile_class(cl);
5630 }
5631 else
5632 {
5633 compiletype_T compile_type = CT_NONE;
5634 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
5635 if (ufunc != NULL)
5636 defcompile_function(ufunc, NULL);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005637 }
5638 }
5639 else
5640 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005641 defcompile_funcs_in_script();
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005642
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005643 // compile all the class defined in the current script
5644 defcompile_classes_in_script();
Bram Moolenaar822ba242020-05-24 23:00:18 +02005645 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005646}
5647
5648/*
5649 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5650 * Return 2 if "p" starts with "s:".
5651 * Return 0 otherwise.
5652 */
5653 int
5654eval_fname_script(char_u *p)
5655{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005656 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5657 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005658 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5659 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5660 return 5;
5661 if (p[0] == 's' && p[1] == ':')
5662 return 2;
5663 return 0;
5664}
5665
5666 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005667translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005668{
5669 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005670 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005671 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005672}
5673
5674/*
5675 * Return TRUE when "ufunc" has old-style "..." varargs
5676 * or named varargs "...name: type".
5677 */
5678 int
5679has_varargs(ufunc_T *ufunc)
5680{
5681 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005682}
5683
5684/*
5685 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005686 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005687 */
5688 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005689function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005690{
5691 char_u *nm = name;
5692 char_u *p;
5693 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005694 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005695 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005696
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005697 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5698 if (no_deref)
5699 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005700 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005701 nm = skipwhite(nm);
5702
Bram Moolenaare38eab22019-12-05 21:50:01 +01005703 // Only accept "funcname", "funcname ", "funcname (..." and
5704 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005705 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005706 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005707 vim_free(p);
5708 return n;
5709}
5710
Bram Moolenaar113e1072019-01-20 15:30:40 +01005711#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005712 char_u *
5713get_expanded_name(char_u *name, int check)
5714{
5715 char_u *nm = name;
5716 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005717 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005718
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005719 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005720
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005721 if (p != NULL && *nm == NUL
5722 && (!check || translated_function_exists(p, is_global)))
5723 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005724
5725 vim_free(p);
5726 return NULL;
5727}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005728#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005729
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005730/*
5731 * Function given to ExpandGeneric() to obtain the list of user defined
5732 * function names.
5733 */
5734 char_u *
5735get_user_func_name(expand_T *xp, int idx)
5736{
5737 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005738 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005739 static hashitem_T *hi;
5740 ufunc_T *fp;
5741
5742 if (idx == 0)
5743 {
5744 done = 0;
5745 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005746 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005747 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005748 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005749 {
5750 if (done++ > 0)
5751 ++hi;
5752 while (HASHITEM_EMPTY(hi))
5753 ++hi;
5754 fp = HI2UF(hi);
5755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005756 // don't show dead, dict and lambda functions
5757 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005758 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005759 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005760
5761 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005762 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005763
5764 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005765 if (xp->xp_context != EXPAND_USER_FUNC
5766 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005767 {
5768 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005769 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005770 STRCAT(IObuff, ")");
5771 }
5772 return IObuff;
5773 }
5774 return NULL;
5775}
5776
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005777/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005778 * Make a copy of a function.
5779 * Intended to be used for a function defined on a base class that has a copy
5780 * on the child class.
5781 * The copy has uf_refcount set to one.
5782 * Returns NULL when out of memory.
5783 */
5784 ufunc_T *
5785copy_function(ufunc_T *fp)
5786{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005787 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005788 if (ufunc == NULL)
5789 return NULL;
5790
5791 // Most things can just be copied.
5792 *ufunc = *fp;
5793
5794 ufunc->uf_def_status = UF_TO_BE_COMPILED;
5795 ufunc->uf_dfunc_idx = 0;
5796 ufunc->uf_class = NULL;
5797
5798 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
5799 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
5800
5801 if (ufunc->uf_arg_types != NULL)
5802 {
5803 // "uf_arg_types" is an allocated array, make a copy.
5804 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
5805 if (at != NULL)
5806 {
5807 mch_memmove(at, ufunc->uf_arg_types,
5808 sizeof(type_T *) * ufunc->uf_args.ga_len);
5809 ufunc->uf_arg_types = at;
5810 }
5811 }
5812
5813 // TODO: how about the types themselves? they can be freed when the
5814 // original function is freed:
5815 // type_T **uf_arg_types;
5816 // type_T *uf_ret_type;
5817
Ernie Rael114ec812023-06-04 18:11:35 +01005818 // make uf_type_list empty
5819 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00005820
5821 // TODO: partial_T *uf_partial;
5822
5823 if (ufunc->uf_va_name != NULL)
5824 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
5825
5826 // TODO:
5827 // type_T *uf_va_type;
5828 // type_T *uf_func_type;
5829
5830 ufunc->uf_block_depth = 0;
5831 ufunc->uf_block_ids = NULL;
5832
5833 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
5834
5835 ufunc->uf_refcount = 1;
5836 ufunc->uf_name_exp = NULL;
5837 STRCPY(ufunc->uf_name, fp->uf_name);
5838
5839 return ufunc;
5840}
5841
5842/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005843 * ":delfunction {name}"
5844 */
5845 void
5846ex_delfunction(exarg_T *eap)
5847{
5848 ufunc_T *fp = NULL;
5849 char_u *p;
5850 char_u *name;
5851 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005852 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005853
5854 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005855 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
5856 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005857 vim_free(fudi.fd_newkey);
5858 if (name == NULL)
5859 {
5860 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005861 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005862 return;
5863 }
5864 if (!ends_excmd(*skipwhite(p)))
5865 {
5866 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005867 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005868 return;
5869 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005870 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005871 if (eap->nextcmd != NULL)
5872 *p = NUL;
5873
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005874 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005875 {
5876 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005877 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005878 vim_free(name);
5879 return;
5880 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005881 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005882 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005883 vim_free(name);
5884
5885 if (!eap->skip)
5886 {
5887 if (fp == NULL)
5888 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005889 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005890 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005891 return;
5892 }
5893 if (fp->uf_calls > 0)
5894 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005895 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005896 return;
5897 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005898 if (fp->uf_flags & FC_VIM9)
5899 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005900 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005901 return;
5902 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005903
5904 if (fudi.fd_dict != NULL)
5905 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005906 // Delete the dict item that refers to the function, it will
5907 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00005908 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005909 }
5910 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005911 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005912 // A normal function (not a numbered function or lambda) has a
5913 // refcount of 1 for the entry in the hashtable. When deleting
5914 // it and the refcount is more than one, it should be kept.
5915 // A numbered function and lambda should be kept if the refcount is
5916 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005917 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005918 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005919 // Function is still referenced somewhere. Don't free it but
5920 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005921 if (func_remove(fp))
5922 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005923 }
5924 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005925 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005926 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005927 }
5928}
5929
5930/*
5931 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005932 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005933 */
5934 void
5935func_unref(char_u *name)
5936{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005937 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005938
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005939 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005940 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005941 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005942 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005943 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005944#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005945 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005946#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005947 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005948 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005949 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005950}
5951
5952/*
5953 * Unreference a Function: decrement the reference count and free it when it
5954 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005955 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005956 */
5957 void
5958func_ptr_unref(ufunc_T *fp)
5959{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005960 if (fp != NULL && (--fp->uf_refcount <= 0
5961 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5962 && fp->uf_partial->pt_refcount <= 1
5963 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005964 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005965 // Only delete it when it's not being used. Otherwise it's done
5966 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005967 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005968 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005969 }
5970}
5971
5972/*
5973 * Count a reference to a Function.
5974 */
5975 void
5976func_ref(char_u *name)
5977{
5978 ufunc_T *fp;
5979
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005980 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005981 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005982 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005983 if (fp != NULL)
5984 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005985 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005986 // Only give an error for a numbered function.
5987 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005988 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005989}
5990
5991/*
5992 * Count a reference to a Function.
5993 */
5994 void
5995func_ptr_ref(ufunc_T *fp)
5996{
5997 if (fp != NULL)
5998 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005999}
6000
6001/*
6002 * Return TRUE if items in "fc" do not have "copyID". That means they are not
6003 * referenced from anywhere that is in use.
6004 */
6005 static int
6006can_free_funccal(funccall_T *fc, int copyID)
6007{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006008 return (fc->fc_l_varlist.lv_copyID != copyID
6009 && fc->fc_l_vars.dv_copyID != copyID
6010 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006011 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006012}
6013
6014/*
6015 * ":return [expr]"
6016 */
6017 void
6018ex_return(exarg_T *eap)
6019{
6020 char_u *arg = eap->arg;
6021 typval_T rettv;
6022 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006023 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006024
6025 if (current_funccal == NULL)
6026 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006027 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006028 return;
6029 }
6030
Bram Moolenaar844fb642021-10-23 13:32:30 +01006031 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006032 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
6033
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006034 if (eap->skip)
6035 ++emsg_skip;
6036
6037 eap->nextcmd = NULL;
6038 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006039 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006040 {
6041 if (!eap->skip)
6042 returning = do_return(eap, FALSE, TRUE, &rettv);
6043 else
6044 clear_tv(&rettv);
6045 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01006046 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006047 else if (!eap->skip)
6048 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006049 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01006050 update_force_abort();
6051
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006052 /*
6053 * Return unless the expression evaluation has been cancelled due to an
6054 * aborting error, an interrupt, or an exception.
6055 */
6056 if (!aborting())
6057 returning = do_return(eap, FALSE, TRUE, NULL);
6058 }
6059
Bram Moolenaare38eab22019-12-05 21:50:01 +01006060 // When skipping or the return gets pending, advance to the next command
6061 // in this line (!returning). Otherwise, ignore the rest of the line.
6062 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006063 if (returning)
6064 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006065 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02006066 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006067
6068 if (eap->skip)
6069 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02006070 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006071}
6072
zeertzjq5299c092023-04-12 20:48:16 +01006073/*
6074 * Lower level implementation of "call". Only called when not skipping.
6075 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006076 static int
6077ex_call_inner(
6078 exarg_T *eap,
6079 char_u *name,
6080 char_u **arg,
6081 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006082 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006083 evalarg_T *evalarg)
6084{
6085 linenr_T lnum;
6086 int doesrange;
6087 typval_T rettv;
6088 int failed = FALSE;
6089
zeertzjq5299c092023-04-12 20:48:16 +01006090 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006091 for ( ; lnum <= eap->line2; ++lnum)
6092 {
6093 funcexe_T funcexe;
6094
zeertzjq5299c092023-04-12 20:48:16 +01006095 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006096 {
6097 if (lnum > curbuf->b_ml.ml_line_count)
6098 {
6099 // If the function deleted lines or switched to another buffer
6100 // the line number may become invalid.
6101 emsg(_(e_invalid_range));
6102 break;
6103 }
6104 curwin->w_cursor.lnum = lnum;
6105 curwin->w_cursor.col = 0;
6106 curwin->w_cursor.coladd = 0;
6107 }
6108 *arg = startarg;
6109
6110 funcexe = *funcexe_init;
6111 funcexe.fe_doesrange = &doesrange;
6112 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6113 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6114 {
6115 failed = TRUE;
6116 break;
6117 }
6118 if (has_watchexpr())
6119 dbg_check_breakpoint(eap);
6120
6121 // Handle a function returning a Funcref, Dictionary or List.
6122 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006123 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006124 {
6125 failed = TRUE;
6126 break;
6127 }
6128
6129 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006130 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006131 break;
6132
6133 // Stop when immediately aborting on error, or when an interrupt
6134 // occurred or an exception was thrown but not caught.
6135 // get_func_tv() returned OK, so that the check for trailing
6136 // characters below is executed.
6137 if (aborting())
6138 break;
6139 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006140 return failed;
6141}
6142
6143/*
6144 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6145 * Returns FAIL or OK.
6146 */
6147 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006148ex_defer_inner(
6149 char_u *name,
6150 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006151 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006152 partial_T *partial,
6153 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006154{
6155 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006156 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006157 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006158 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006159
6160 if (current_funccal == NULL)
6161 {
6162 semsg(_(e_str_not_inside_function), "defer");
6163 return FAIL;
6164 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006165 if (partial != NULL)
6166 {
6167 if (partial->pt_dict != NULL)
6168 {
6169 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6170 return FAIL;
6171 }
6172 if (partial->pt_argc > 0)
6173 {
6174 int i;
6175
6176 partial_argc = partial->pt_argc;
6177 for (i = 0; i < partial_argc; ++i)
6178 copy_tv(&partial->pt_argv[i], &argvars[i]);
6179 }
6180 }
Ernie Raelb077b582023-12-14 20:11:44 +01006181 int is_builtin = builtin_function(name, -1);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006182 r = get_func_arguments(arg, evalarg, FALSE,
Ernie Raelb077b582023-12-14 20:11:44 +01006183 argvars + partial_argc, &argcount, is_builtin);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006184 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006185
6186 if (r == OK)
6187 {
6188 if (type != NULL)
6189 {
6190 // Check that the arguments are OK for the types of the funcref.
6191 r = check_argument_types(type, argvars, argcount, NULL, name);
6192 }
Ernie Raelb077b582023-12-14 20:11:44 +01006193 else if (is_builtin)
Bram Moolenaar16900322022-09-08 19:51:45 +01006194 {
6195 int idx = find_internal_func(name);
6196
6197 if (idx < 0)
6198 {
6199 emsg_funcname(e_unknown_function_str, name);
6200 r = FAIL;
6201 }
6202 else if (check_internal_func(idx, argcount) == -1)
6203 r = FAIL;
6204 }
6205 else
6206 {
6207 ufunc_T *ufunc = find_func(name, FALSE);
6208
6209 // we tolerate an unknown function here, it might be defined later
6210 if (ufunc != NULL)
6211 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006212 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006213 if (error != FCERR_UNKNOWN)
6214 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006215 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006216 r = FAIL;
6217 }
6218 }
6219 }
6220 }
6221
Bram Moolenaar86d87252022-09-05 21:21:25 +01006222 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006223 {
6224 while (--argcount >= 0)
6225 clear_tv(&argvars[argcount]);
6226 return FAIL;
6227 }
6228 return add_defer(name, argcount, argvars);
6229}
6230
6231/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006232 * Return TRUE if currently inside a function call.
6233 * Give an error message and return FALSE when not.
6234 */
6235 int
6236can_add_defer(void)
6237{
6238 if (!in_def_function() && get_current_funccal() == NULL)
6239 {
6240 semsg(_(e_str_not_inside_function), "defer");
6241 return FALSE;
6242 }
6243 return TRUE;
6244}
6245
6246/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006247 * Add a deferred call for "name" with arguments "argvars[argcount]".
6248 * Consumes "argvars[]".
6249 * Caller must check that in_def_function() returns TRUE or current_funccal is
6250 * not NULL.
6251 * Returns OK or FAIL.
6252 */
6253 int
6254add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6255{
6256 char_u *saved_name = vim_strsave(name);
6257 int argcount = argcount_arg;
6258 defer_T *dr;
6259 int ret = FAIL;
6260
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006261 if (saved_name == NULL)
6262 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006263 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006264 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006265 if (add_defer_function(saved_name, argcount, argvars) == OK)
6266 argcount = 0;
6267 }
6268 else
6269 {
6270 if (current_funccal->fc_defer.ga_itemsize == 0)
6271 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6272 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6273 goto theend;
6274 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6275 + current_funccal->fc_defer.ga_len++;
6276 dr->dr_name = saved_name;
6277 dr->dr_argcount = argcount;
6278 while (argcount > 0)
6279 {
6280 --argcount;
6281 dr->dr_argvars[argcount] = argvars[argcount];
6282 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006283 }
6284 ret = OK;
6285
6286theend:
6287 while (--argcount >= 0)
6288 clear_tv(&argvars[argcount]);
6289 return ret;
6290}
6291
6292/*
6293 * Invoked after a functions has finished: invoke ":defer" functions.
6294 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006295 static void
6296handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006297{
6298 int idx;
6299
Bram Moolenaar58779852022-09-06 18:31:14 +01006300 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006301 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006302 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006303
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006304 if (dr->dr_name == NULL)
6305 // already being called, can happen if function does ":qa"
6306 continue;
6307
6308 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006309 CLEAR_FIELD(funcexe);
6310 funcexe.fe_evaluate = TRUE;
6311
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006312 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006313 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006314
6315 char_u *name = dr->dr_name;
6316 dr->dr_name = NULL;
6317
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006318 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006319 // first statement in the function will be executed (because of the
6320 // exception). So save and restore the try/catch/throw exception
6321 // state.
6322 exception_state_T estate;
6323 exception_state_save(&estate);
6324 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006325
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006326 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6327
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006328 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006329
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006330 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006331 vim_free(name);
6332 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006333 clear_tv(&dr->dr_argvars[i]);
6334 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006335 ga_clear(&funccal->fc_defer);
6336}
6337
zeertzjq960cf912023-04-18 21:52:54 +01006338 static void
6339invoke_funccall_defer(funccall_T *fc)
6340{
6341 if (fc->fc_ectx != NULL)
6342 {
6343 // :def function
6344 unwind_def_callstack(fc->fc_ectx);
6345 may_invoke_defer_funcs(fc->fc_ectx);
6346 }
6347 else
6348 {
6349 // legacy function
6350 handle_defer_one(fc);
6351 }
6352}
6353
Bram Moolenaar58779852022-09-06 18:31:14 +01006354/*
6355 * Called when exiting: call all defer functions.
6356 */
6357 void
6358invoke_all_defer(void)
6359{
zeertzjq1be4b812023-04-19 14:21:24 +01006360 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6361 invoke_funccall_defer(fc);
6362
zeertzjq960cf912023-04-18 21:52:54 +01006363 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6364 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6365 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006366}
6367
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006368/*
6369 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006370 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006371 */
6372 void
6373ex_call(exarg_T *eap)
6374{
6375 char_u *arg = eap->arg;
6376 char_u *startarg;
6377 char_u *name;
6378 char_u *tofree;
6379 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006380 int failed = FALSE;
6381 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006382 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006383 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006384 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006385 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006386 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006387 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006388
Bram Moolenaare6b53242020-07-01 17:28:33 +02006389 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006390 if (eap->skip)
6391 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006392 typval_T rettv;
6393
Bram Moolenaare38eab22019-12-05 21:50:01 +01006394 // trans_function_name() doesn't work well when skipping, use eval0()
6395 // instead to skip to any following command, e.g. for:
6396 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006397 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006398 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006399 clear_tv(&rettv);
6400 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006401 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006402 return;
6403 }
6404
zeertzjq5299c092023-04-12 20:48:16 +01006405 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006406 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006407 if (fudi.fd_newkey != NULL)
6408 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006409 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006410 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006411 vim_free(fudi.fd_newkey);
6412 }
6413 if (tofree == NULL)
6414 return;
6415
Bram Moolenaare38eab22019-12-05 21:50:01 +01006416 // Increase refcount on dictionary, it could get deleted when evaluating
6417 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006418 if (fudi.fd_dict != NULL)
6419 ++fudi.fd_dict->dv_refcount;
6420
Bram Moolenaare38eab22019-12-05 21:50:01 +01006421 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6422 // contents. For VAR_PARTIAL get its partial, unless we already have one
6423 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006424 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006425 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006426 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006427 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006428
Bram Moolenaare38eab22019-12-05 21:50:01 +01006429 // Skip white space to allow ":call func ()". Not good, but required for
6430 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006431 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006432 if (*startarg != '(')
6433 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006434 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006435 goto end;
6436 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006437 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006438 {
6439 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6440 goto end;
6441 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006442
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006443 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006444 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006445 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006446 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006447 }
6448 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006449 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006450 funcexe_T funcexe;
6451
Bram Moolenaara80faa82020-04-12 19:37:17 +02006452 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006453 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006454 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006455 funcexe.fe_partial = partial;
6456 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006457 funcexe.fe_firstline = eap->line1;
6458 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006459 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006460 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006461 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006462 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006463
Bram Moolenaar1d341892021-09-18 15:25:52 +02006464 // When inside :try we need to check for following "| catch" or "| endtry".
6465 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006466 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006467 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006468 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006469 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006470 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006471 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006472 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006473 {
6474 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006475 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006476 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006477 }
6478 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006479 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006480 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006481 // Must be after using "arg", it may point into memory cleared here.
6482 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006483
6484end:
6485 dict_unref(fudi.fd_dict);
6486 vim_free(tofree);
6487}
6488
6489/*
6490 * Return from a function. Possibly makes the return pending. Also called
6491 * for a pending return at the ":endtry" or after returning from an extra
6492 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6493 * when called due to a ":return" command. "rettv" may point to a typval_T
6494 * with the return rettv. Returns TRUE when the return can be carried out,
6495 * FALSE when the return gets pending.
6496 */
6497 int
6498do_return(
6499 exarg_T *eap,
6500 int reanimate,
6501 int is_cmd,
6502 void *rettv)
6503{
6504 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006505 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006506
6507 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006508 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006509 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006510
6511 /*
6512 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6513 * not in its finally clause (which then is to be executed next) is found.
6514 * In this case, make the ":return" pending for execution at the ":endtry".
6515 * Otherwise, return normally.
6516 */
6517 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6518 if (idx >= 0)
6519 {
6520 cstack->cs_pending[idx] = CSTP_RETURN;
6521
6522 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006523 // A pending return again gets pending. "rettv" points to an
6524 // allocated variable with the rettv of the original ":return"'s
6525 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006526 cstack->cs_rettv[idx] = rettv;
6527 else
6528 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006529 // When undoing a return in order to make it pending, get the stored
6530 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006531 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006532 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006533
6534 if (rettv != NULL)
6535 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006536 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006537 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6538 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6539 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006540 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006541 }
6542 else
6543 cstack->cs_rettv[idx] = NULL;
6544
6545 if (reanimate)
6546 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006547 // The pending return value could be overwritten by a ":return"
6548 // without argument in a finally clause; reset the default
6549 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006550 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6551 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006552 }
6553 }
6554 report_make_pending(CSTP_RETURN, rettv);
6555 }
6556 else
6557 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006558 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006559
Bram Moolenaare38eab22019-12-05 21:50:01 +01006560 // If the return is carried out now, store the return value. For
6561 // a return immediately after reanimation, the value is already
6562 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006563 if (!reanimate && rettv != NULL)
6564 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006565 clear_tv(current_funccal->fc_rettv);
6566 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006567 if (!is_cmd)
6568 vim_free(rettv);
6569 }
6570 }
6571
6572 return idx < 0;
6573}
6574
6575/*
6576 * Free the variable with a pending return value.
6577 */
6578 void
6579discard_pending_return(void *rettv)
6580{
6581 free_tv((typval_T *)rettv);
6582}
6583
6584/*
6585 * Generate a return command for producing the value of "rettv". The result
6586 * is an allocated string. Used by report_pending() for verbose messages.
6587 */
6588 char_u *
6589get_return_cmd(void *rettv)
6590{
6591 char_u *s = NULL;
6592 char_u *tofree = NULL;
6593 char_u numbuf[NUMBUFLEN];
6594
6595 if (rettv != NULL)
6596 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6597 if (s == NULL)
6598 s = (char_u *)"";
6599
6600 STRCPY(IObuff, ":return ");
6601 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6602 if (STRLEN(s) + 8 >= IOSIZE)
6603 STRCPY(IObuff + IOSIZE - 4, "...");
6604 vim_free(tofree);
6605 return vim_strsave(IObuff);
6606}
6607
6608/*
6609 * Get next function line.
6610 * Called by do_cmdline() to get the next line.
6611 * Returns allocated string, or NULL for end of function.
6612 */
6613 char_u *
6614get_func_line(
6615 int c UNUSED,
6616 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006617 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006618 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006619{
6620 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006621 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006622 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006623 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006624
Bram Moolenaare38eab22019-12-05 21:50:01 +01006625 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006626 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006627 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006628 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006629 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006630 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006631 }
6632#ifdef FEAT_PROFILE
6633 if (do_profiling == PROF_YES)
6634 func_line_end(cookie);
6635#endif
6636
6637 gap = &fp->uf_lines;
6638 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006639 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006640 retval = NULL;
6641 else
6642 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006643 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006644 while (fcp->fc_linenr < gap->ga_len
6645 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6646 ++fcp->fc_linenr;
6647 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006648 retval = NULL;
6649 else
6650 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006651 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6652 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006653#ifdef FEAT_PROFILE
6654 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006655 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006656#endif
6657 }
6658 }
6659
Bram Moolenaare38eab22019-12-05 21:50:01 +01006660 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006661 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006662 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006663 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006664 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006665 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006666 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006667 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006668 }
6669
6670 return retval;
6671}
6672
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006673/*
6674 * Return TRUE if the currently active function should be ended, because a
6675 * return was encountered or an error occurred. Used inside a ":while".
6676 */
6677 int
6678func_has_ended(void *cookie)
6679{
6680 funccall_T *fcp = (funccall_T *)cookie;
6681
Bram Moolenaare38eab22019-12-05 21:50:01 +01006682 // Ignore the "abort" flag if the abortion behavior has been changed due to
6683 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006684 return (((fcp->fc_func->uf_flags & FC_ABORT)
6685 && did_emsg && !aborted_in_try())
6686 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006687}
6688
6689/*
6690 * return TRUE if cookie indicates a function which "abort"s on errors.
6691 */
6692 int
6693func_has_abort(
6694 void *cookie)
6695{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006696 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006697}
6698
6699
6700/*
6701 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6702 * Don't do this when "Func" is already a partial that was bound
6703 * explicitly (pt_auto is FALSE).
6704 * Changes "rettv" in-place.
6705 * Returns the updated "selfdict_in".
6706 */
6707 dict_T *
6708make_partial(dict_T *selfdict_in, typval_T *rettv)
6709{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006710 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006711 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006712 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006713 dict_T *selfdict = selfdict_in;
6714
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006715 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6716 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006717 fp = rettv->vval.v_partial->pt_func;
6718 else
6719 {
6720 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006721 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006722 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006723 if (fname == NULL)
6724 {
6725 // There is no point binding a dict to a NULL function, just create
6726 // a function reference.
6727 rettv->v_type = VAR_FUNC;
6728 rettv->vval.v_string = NULL;
6729 }
6730 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006731 {
6732 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006733 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006734
6735 // Translate "s:func" to the stored function name.
6736 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6737 fp = find_func(fname, FALSE);
6738 vim_free(tofree);
6739 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006740 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006741
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006742 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006743 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006744 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006745
6746 if (pt != NULL)
6747 {
6748 pt->pt_refcount = 1;
6749 pt->pt_dict = selfdict;
6750 pt->pt_auto = TRUE;
6751 selfdict = NULL;
6752 if (rettv->v_type == VAR_FUNC)
6753 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006754 // Just a function: Take over the function name and use
6755 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006756 pt->pt_name = rettv->vval.v_string;
6757 }
6758 else
6759 {
6760 partial_T *ret_pt = rettv->vval.v_partial;
6761 int i;
6762
Bram Moolenaare38eab22019-12-05 21:50:01 +01006763 // Partial: copy the function name, use selfdict and copy
6764 // args. Can't take over name or args, the partial might
6765 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006766 if (ret_pt->pt_name != NULL)
6767 {
6768 pt->pt_name = vim_strsave(ret_pt->pt_name);
6769 func_ref(pt->pt_name);
6770 }
6771 else
6772 {
6773 pt->pt_func = ret_pt->pt_func;
6774 func_ptr_ref(pt->pt_func);
6775 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006776 if (ret_pt->pt_argc > 0)
6777 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006778 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006779 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006780 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006781 pt->pt_argc = 0;
6782 else
6783 {
6784 pt->pt_argc = ret_pt->pt_argc;
6785 for (i = 0; i < pt->pt_argc; i++)
6786 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6787 }
6788 }
6789 partial_unref(ret_pt);
6790 }
6791 rettv->v_type = VAR_PARTIAL;
6792 rettv->vval.v_partial = pt;
6793 }
6794 }
6795 return selfdict;
6796}
6797
6798/*
6799 * Return the name of the executed function.
6800 */
6801 char_u *
6802func_name(void *cookie)
6803{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006804 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006805}
6806
6807/*
6808 * Return the address holding the next breakpoint line for a funccall cookie.
6809 */
6810 linenr_T *
6811func_breakpoint(void *cookie)
6812{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006813 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006814}
6815
6816/*
6817 * Return the address holding the debug tick for a funccall cookie.
6818 */
6819 int *
6820func_dbg_tick(void *cookie)
6821{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006822 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006823}
6824
6825/*
6826 * Return the nesting level for a funccall cookie.
6827 */
6828 int
6829func_level(void *cookie)
6830{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006831 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006832}
6833
6834/*
6835 * Return TRUE when a function was ended by a ":return" command.
6836 */
6837 int
6838current_func_returned(void)
6839{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006840 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006841}
6842
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006843 int
6844free_unref_funccal(int copyID, int testing)
6845{
6846 int did_free = FALSE;
6847 int did_free_funccal = FALSE;
6848 funccall_T *fc, **pfc;
6849
6850 for (pfc = &previous_funccal; *pfc != NULL; )
6851 {
6852 if (can_free_funccal(*pfc, copyID))
6853 {
6854 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006855 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006856 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006857 did_free = TRUE;
6858 did_free_funccal = TRUE;
6859 }
6860 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006861 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006862 }
6863 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006864 // When a funccal was freed some more items might be garbage
6865 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006866 (void)garbage_collect(testing);
6867
6868 return did_free;
6869}
6870
6871/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006872 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006873 */
6874 static funccall_T *
6875get_funccal(void)
6876{
6877 int i;
6878 funccall_T *funccal;
6879 funccall_T *temp_funccal;
6880
6881 funccal = current_funccal;
6882 if (debug_backtrace_level > 0)
6883 {
6884 for (i = 0; i < debug_backtrace_level; i++)
6885 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006886 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006887 if (temp_funccal)
6888 funccal = temp_funccal;
6889 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006890 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006891 debug_backtrace_level = i;
6892 }
6893 }
6894 return funccal;
6895}
6896
6897/*
6898 * Return the hashtable used for local variables in the current funccal.
6899 * Return NULL if there is no current funccal.
6900 */
6901 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006902get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006903{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006904 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006905 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006906 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006907}
6908
6909/*
6910 * Return the l: scope variable.
6911 * Return NULL if there is no current funccal.
6912 */
6913 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006914get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006915{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006916 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006917 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006918 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006919}
6920
6921/*
6922 * Return the hashtable used for argument in the current funccal.
6923 * Return NULL if there is no current funccal.
6924 */
6925 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006926get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006927{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006928 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006929 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006930 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006931}
6932
6933/*
6934 * Return the a: scope variable.
6935 * Return NULL if there is no current funccal.
6936 */
6937 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006938get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006939{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006940 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006941 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006942 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006943}
6944
6945/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006946 * List function variables, if there is a function.
6947 */
6948 void
6949list_func_vars(int *first)
6950{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006951 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
6952 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006953 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006954}
6955
6956/*
6957 * If "ht" is the hashtable for local variables in the current funccal, return
6958 * the dict that contains it.
6959 * Otherwise return NULL.
6960 */
6961 dict_T *
6962get_current_funccal_dict(hashtab_T *ht)
6963{
6964 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01006965 && ht == &current_funccal->fc_l_vars.dv_hashtab)
6966 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006967 return NULL;
6968}
6969
6970/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006971 * Search hashitem in parent scope.
6972 */
6973 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006974find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006975{
6976 funccall_T *old_current_funccal = current_funccal;
6977 hashtab_T *ht;
6978 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006979 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006980
Bram Moolenaarca16c602022-09-06 18:57:08 +01006981 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006982 return NULL;
6983
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006984 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006985 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006986 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006987 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006988 ht = find_var_ht(name, &varname);
6989 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006990 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006991 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006992 if (!HASHITEM_EMPTY(hi))
6993 {
6994 *pht = ht;
6995 break;
6996 }
6997 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006998 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02006999 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007000 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007001 }
7002 current_funccal = old_current_funccal;
7003
7004 return hi;
7005}
7006
7007/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007008 * Search variable in parent scope.
7009 */
7010 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007011find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007012{
7013 dictitem_T *v = NULL;
7014 funccall_T *old_current_funccal = current_funccal;
7015 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007016 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007017
Bram Moolenaarca16c602022-09-06 18:57:08 +01007018 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007019 return NULL;
7020
Bram Moolenaare38eab22019-12-05 21:50:01 +01007021 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01007022 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007023 while (current_funccal)
7024 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007025 ht = find_var_ht(name, &varname);
7026 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007027 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007028 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007029 if (v != NULL)
7030 break;
7031 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007032 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007033 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007034 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007035 }
7036 current_funccal = old_current_funccal;
7037
7038 return v;
7039}
7040
7041/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007042 * Set "copyID + 1" in previous_funccal and callers.
7043 */
7044 int
7045set_ref_in_previous_funccal(int copyID)
7046{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007047 funccall_T *fc;
7048
Bram Moolenaarca16c602022-09-06 18:57:08 +01007049 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007050 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007051 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007052 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
7053 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
7054 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007055 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007056 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007057 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007058}
7059
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007060 static int
7061set_ref_in_funccal(funccall_T *fc, int copyID)
7062{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007063 if (fc->fc_copyID != copyID)
7064 {
7065 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007066 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
7067 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
7068 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
7069 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007070 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007071 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007072 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007073}
7074
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007075/*
7076 * Set "copyID" in all local vars and arguments in the call stack.
7077 */
7078 int
7079set_ref_in_call_stack(int copyID)
7080{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007081 funccall_T *fc;
7082 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007083
Bram Moolenaarca16c602022-09-06 18:57:08 +01007084 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007085 if (set_ref_in_funccal(fc, copyID))
7086 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007087
7088 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007089 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007090 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007091 if (set_ref_in_funccal(fc, copyID))
7092 return TRUE;
7093 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007094}
7095
7096/*
7097 * Set "copyID" in all functions available by name.
7098 */
7099 int
7100set_ref_in_functions(int copyID)
7101{
7102 int todo;
7103 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007104 ufunc_T *fp;
7105
7106 todo = (int)func_hashtab.ht_used;
7107 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007108 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007109 if (!HASHITEM_EMPTY(hi))
7110 {
7111 --todo;
7112 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007113 if (!func_name_refcount(fp->uf_name)
7114 && set_ref_in_func(NULL, fp, copyID))
7115 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007116 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007117 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007118 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007119}
7120
7121/*
7122 * Set "copyID" in all function arguments.
7123 */
7124 int
7125set_ref_in_func_args(int copyID)
7126{
7127 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007128
7129 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007130 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7131 copyID, NULL, NULL))
7132 return TRUE;
7133 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007134}
7135
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007136/*
7137 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007138 * Returns TRUE if setting references failed somehow.
7139 */
7140 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007141set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007142{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007143 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007144 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007145 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007146 char_u fname_buf[FLEN_FIXED + 1];
7147 char_u *tofree = NULL;
7148 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007149 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007150
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007151 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007152 return FALSE;
7153
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007154 if (fp_in == NULL)
7155 {
7156 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007157 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007158 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007159 if (fp != NULL)
7160 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007161 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007162 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007163 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007164
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007165 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007166 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007167}
7168
Bram Moolenaare38eab22019-12-05 21:50:01 +01007169#endif // FEAT_EVAL