blob: c0a2487b46611763cd63daab9346589315dec5d6 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +020011 * userfunc.c: User defined function support
Bram Moolenaara9b579f2016-07-17 18:29:19 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020017/*
18 * All user-defined functions are found in this hashtable.
19 */
20static hashtab_T func_hashtab;
21
Bram Moolenaare38eab22019-12-05 21:50:01 +010022// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020023static garray_T funcargs = GA_EMPTY;
24
Bram Moolenaar209b8e32019-03-14 13:43:24 +010025// pointer to funccal for currently active function
26static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020027
Bram Moolenaar209b8e32019-03-14 13:43:24 +010028// Pointer to list of previously used funccal, still around because some
29// item in it is still being used.
30static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020031
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020032static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +010033static void func_clear(ufunc_T *fp, int force);
34static int func_free(ufunc_T *fp, int force);
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +010035static char_u *untrans_function_name(char_u *name);
Bram Moolenaar58779852022-09-06 18:31:14 +010036static void handle_defer_one(funccall_T *funccal);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020037
38 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +000039func_init(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020040{
41 hash_init(&func_hashtab);
42}
43
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020044/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020045 * Return the function hash table
46 */
47 hashtab_T *
48func_tbl_get(void)
49{
50 return &func_hashtab;
51}
52
53/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020054 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020055 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010056 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010057 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaardce24412022-02-08 20:35:30 +000058 * If "eap" is not NULL use it to check for an already declared name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010059 * Return a pointer to after the type.
60 * When something is wrong return "arg".
61 */
62 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010063one_function_arg(
64 char_u *arg,
65 garray_T *newargs,
66 garray_T *argtypes,
67 int types_optional,
h-eastb895b0f2023-09-24 15:46:31 +020068 garray_T *arg_objm,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010069 evalarg_T *evalarg,
Bram Moolenaardce24412022-02-08 20:35:30 +000070 exarg_T *eap,
Bram Moolenaar2a389082021-04-09 20:24:31 +020071 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010072 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073{
Bram Moolenaar6e949782020-04-13 17:21:00 +020074 char_u *p = arg;
75 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020076 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077
78 while (ASCII_ISALNUM(*p) || *p == '_')
79 ++p;
80 if (arg == p || isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020081 || (argtypes == NULL
82 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
83 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010084 {
85 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000086 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087 return arg;
88 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010089
Bram Moolenaarce723f32023-06-10 19:00:12 +010090 // Extra checks in Vim9 script.
91 if (!skip && argtypes != NULL)
92 {
93 int c = *p;
94 *p = NUL;
95 int r = check_reserved_name(arg, FALSE);
96 *p = c;
97 if (r == FAIL)
98 return arg;
99
100 // Cannot use script var name for argument. In function: also check
101 // local vars and arguments.
102 if (check_defined(arg, p - arg,
103 evalarg == NULL ? NULL : evalarg->eval_cctx,
Bram Moolenaardce24412022-02-08 20:35:30 +0000104 eap == NULL ? NULL : eap->cstack, TRUE) == FAIL)
Bram Moolenaarce723f32023-06-10 19:00:12 +0100105 return arg;
106 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100108 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
109 return arg;
110 if (newargs != NULL)
111 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100112 int c;
113 int i;
114
115 c = *p;
116 *p = NUL;
117 arg_copy = vim_strsave(arg);
118 if (arg_copy == NULL)
119 {
120 *p = c;
121 return arg;
122 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200123 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200124 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200125 // Check for duplicate argument name.
126 for (i = 0; i < newargs->ga_len; ++i)
127 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
128 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000129 semsg(_(e_duplicate_argument_name_str), arg_copy);
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200130 vim_free(arg_copy);
131 return arg;
132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100133 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
134 newargs->ga_len++;
135
136 *p = c;
137 }
138
139 // get any type from "arg: type"
h-eastb895b0f2023-09-24 15:46:31 +0200140 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK)
141 && arg_objm != NULL && (skip || ga_grow(arg_objm, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 {
143 char_u *type = NULL;
144
Bram Moolenaar6e949782020-04-13 17:21:00 +0200145 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
146 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200147 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200148 arg_copy == NULL ? arg : arg_copy);
149 p = skipwhite(p);
150 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 if (*p == ':')
152 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200153 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100154 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200155 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100156 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200157 return arg;
158 }
159 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200160 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100161 if (!skip)
162 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100163 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200164 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200165 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200166 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200167 arg_copy == NULL ? arg : arg_copy);
168 return arg;
169 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100170 if (!skip)
171 {
172 if (type == NULL && types_optional)
173 // lambda arguments default to "any" type
Bram Moolenaar2a389082021-04-09 20:24:31 +0200174 type = vim_strsave((char_u *)
175 (is_vararg ? "list<any>" : "any"));
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100176 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
h-eastb895b0f2023-09-24 15:46:31 +0200177 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = FALSE;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 }
180
181 return p;
182}
183
184/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000185 * Handle line continuation in function arguments or body.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000186 * Get a next line, store it in "eap" if appropriate and put the line in
187 * "lines_to_free" to free the line later.
Bram Moolenaar7473a842021-12-28 17:55:26 +0000188 */
189 static char_u *
190get_function_line(
191 exarg_T *eap,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000192 garray_T *lines_to_free,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000193 int indent,
194 getline_opt_T getline_options)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000195{
196 char_u *theline;
197
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
573 || type->tt_type == VAR_PARTIAL)
574 && var_wrong_func_name(
575 ((char_u **)fp->uf_args.ga_data)[i], TRUE))
576 return FAIL;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100577 }
578 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100579 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200580
581 if (varargs)
582 {
583 char_u *p;
584
585 // Move the last argument "...name: type" to uf_va_name and
586 // uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200587 --fp->uf_args.ga_len;
Bram Moolenaarf0571712023-01-04 13:16:20 +0000588 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len];
589 ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len] = NULL;
Bram Moolenaar2a389082021-04-09 20:24:31 +0200590 p = ((char_u **)argtypes->ga_data)[len];
591 if (p == NULL)
592 // TODO: get type from default value
593 fp->uf_va_type = &t_list_any;
594 else
595 {
596 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
597 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
598 {
599 semsg(_(e_variable_arguments_type_must_be_list_str),
600 ((char_u **)argtypes->ga_data)[len]);
601 return FAIL;
602 }
603 }
604 if (fp->uf_va_type == NULL)
605 return FAIL;
606 }
607
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100608 return OK;
609}
610
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100611 static int
612parse_return_type(ufunc_T *fp, char_u *ret_type)
613{
614 if (ret_type == NULL)
615 fp->uf_ret_type = &t_void;
616 else
617 {
618 char_u *p = ret_type;
619
620 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
621 if (fp->uf_ret_type == NULL)
622 {
623 fp->uf_ret_type = &t_void;
624 return FAIL;
625 }
626 }
627 return OK;
628}
629
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100630/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200631 * Register function "fp" as using "current_funccal" as its scope.
632 */
633 static int
634register_closure(ufunc_T *fp)
635{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200636 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100637 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200638 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200639 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200640 fp->uf_scoped = current_funccal;
641 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200642
Bram Moolenaarca16c602022-09-06 18:57:08 +0100643 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200644 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100645 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
646 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200647 return OK;
648}
649
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100650 static void
651set_ufunc_name(ufunc_T *fp, char_u *name)
652{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100653 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
654 // actually extends beyond the struct.
655 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100656
657 if (name[0] == K_SPECIAL)
658 {
659 fp->uf_name_exp = alloc(STRLEN(name) + 3);
660 if (fp->uf_name_exp != NULL)
661 {
662 STRCPY(fp->uf_name_exp, "<SNR>");
663 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
664 }
665 }
666}
667
Bram Moolenaar58016442016-07-31 18:30:22 +0200668/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100669 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
670 * return "buf" filled with a readable function name.
671 * Otherwise just return "name", thus the return value can always be used.
672 * "name" and "buf" may be equal.
673 */
674 char_u *
675make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
676{
677 size_t len;
678
679 if (name[0] != K_SPECIAL)
680 return name;
681 len = STRLEN(name);
682 if (len + 3 > bufsize)
683 return name;
684
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100685 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100686 mch_memmove(buf, "<SNR>", 5);
687 return buf;
688}
689
690/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200691 * Get a name for a lambda. Returned in static memory.
692 */
693 char_u *
694get_lambda_name(void)
695{
696 static char_u name[30];
697 static int lambda_no = 0;
698
699 sprintf((char*)name, "<lambda>%d", ++lambda_no);
700 return name;
701}
702
Bram Moolenaare01e5212023-01-08 20:31:18 +0000703/*
704 * Allocate a "ufunc_T" for a function called "name".
705 * Makes sure the size is right.
706 */
707 static ufunc_T *
708alloc_ufunc(char_u *name)
709{
710 // When the name is short we need to make sure we allocate enough bytes for
711 // the whole struct, including any padding.
712 size_t len = offsetof(ufunc_T, uf_name) + STRLEN(name) + 1;
713 return alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
714}
715
Bram Moolenaar801ab062020-06-25 19:27:56 +0200716#if defined(FEAT_LUA) || defined(PROTO)
717/*
718 * Registers a native C callback which can be called from Vim script.
719 * Returns the name of the Vim script function.
720 */
721 char_u *
722register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
723{
724 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200725 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200726
Bram Moolenaare01e5212023-01-08 20:31:18 +0000727 fp = alloc_ufunc(name);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200728 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200729 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200730
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200731 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200732 fp->uf_refcount = 1;
733 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000734 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200735 fp->uf_calls = 0;
736 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200737 fp->uf_cb = cb;
738 fp->uf_cb_free = cb_free;
739 fp->uf_cb_state = state;
740
741 set_ufunc_name(fp, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000742 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200743
744 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200745}
746#endif
747
Bram Moolenaar04b12692020-05-04 23:24:44 +0200748/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100749 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100750 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100751 * If "white_error" is not NULL check for correct use of white space and set
752 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100753 * Return NULL if no valid arrow found.
754 */
755 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100756skip_arrow(
757 char_u *start,
758 int equal_arrow,
759 char_u **ret_type,
760 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100761{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100762 char_u *s = start;
763 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100764
765 if (equal_arrow)
766 {
767 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100768 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100769 if (white_error != NULL && !VIM_ISWHITE(s[1]))
770 {
771 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100772 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100773 return NULL;
774 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100775 s = skipwhite(s + 1);
776 *ret_type = s;
777 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100778 if (s == *ret_type)
779 {
780 emsg(_(e_missing_return_type));
781 return NULL;
782 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100783 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100784 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100785 s = skipwhite(s);
786 if (*s != '=')
787 return NULL;
788 ++s;
789 }
790 if (*s != '>')
791 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100792 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
793 || !IS_WHITE_OR_NUL(s[1])))
794 {
795 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100796 semsg(_(e_white_space_required_before_and_after_str_at_str),
797 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100798 return NULL;
799 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100800 return skipwhite(s + 1);
801}
802
803/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100804 * Check if "*cmd" points to a function command and if so advance "*cmd" and
805 * return TRUE.
806 * Otherwise return FALSE;
807 * Do not consider "function(" to be a command.
808 */
809 static int
810is_function_cmd(char_u **cmd)
811{
812 char_u *p = *cmd;
813
814 if (checkforcmd(&p, "function", 2))
815 {
816 if (*p == '(')
817 return FALSE;
818 *cmd = p;
819 return TRUE;
820 }
821 return FALSE;
822}
823
824/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200825 * Called when defining a function: The context may be needed for script
826 * variables declared in a block that is visible now but not when the function
827 * is compiled or called later.
828 */
829 static void
830function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
831{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000832 if (cstack == NULL || cstack->cs_idx < 0)
833 return;
834
835 int count = cstack->cs_idx + 1;
836 int i;
837
838 fp->uf_block_ids = ALLOC_MULT(int, count);
839 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200840 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000841 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
842 sizeof(int) * count);
843 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200844 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000845
846 // Set flag in each block to indicate a function was defined. This
847 // is used to keep the variable when leaving the block, see
848 // hide_script_var().
849 for (i = 0; i <= cstack->cs_idx; ++i)
850 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200851}
852
853/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100854 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200855 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100856 * "newlines" must already have been initialized.
857 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
858 */
859 static int
860get_function_body(
861 exarg_T *eap,
862 garray_T *newlines,
863 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000864 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100865{
866 linenr_T sourcing_lnum_top = SOURCING_LNUM;
867 linenr_T sourcing_lnum_off;
868 int saved_wait_return = need_wait_return;
869 char_u *line_arg = line_arg_in;
870 int vim9_function = eap->cmdidx == CMD_def
871 || eap->cmdidx == CMD_block;
872#define MAX_FUNC_NESTING 50
873 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200874 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100875 int nesting = 0;
876 getline_opt_T getline_options;
877 int indent = 2;
878 char_u *skip_until = NULL;
879 int ret = FAIL;
880 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200881 int heredoc_concat_len = 0;
882 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100883 char_u *heredoc_trimmed = NULL;
884
Bram Moolenaar20677332021-06-06 17:02:53 +0200885 ga_init2(&heredoc_ga, 1, 500);
886
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100887 // Detect having skipped over comment lines to find the return
888 // type. Add NULL lines to keep the line count correct.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100889 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100890 if (SOURCING_LNUM < sourcing_lnum_off)
891 {
892 sourcing_lnum_off -= SOURCING_LNUM;
893 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
894 goto theend;
895 while (sourcing_lnum_off-- > 0)
896 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
897 }
898
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200899 nesting_def[0] = vim9_function;
900 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100901 getline_options = vim9_function
902 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
903 for (;;)
904 {
905 char_u *theline;
906 char_u *p;
907 char_u *arg;
908
909 if (KeyTyped)
910 {
911 msg_scroll = TRUE;
912 saved_wait_return = FALSE;
913 }
914 need_wait_return = FALSE;
915
916 if (line_arg != NULL)
917 {
918 // Use eap->arg, split up in parts by line breaks.
919 theline = line_arg;
920 p = vim_strchr(theline, '\n');
921 if (p == NULL)
922 line_arg += STRLEN(line_arg);
923 else
924 {
925 *p = NUL;
926 line_arg = p + 1;
927 }
928 }
929 else
930 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000931 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100932 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100933 }
934 if (KeyTyped)
935 lines_left = Rows - 1;
936 if (theline == NULL)
937 {
938 // Use the start of the function for the line number.
939 SOURCING_LNUM = sourcing_lnum_top;
940 if (skip_until != NULL)
941 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200942 else if (nesting_inline[nesting])
943 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100944 else if (eap->cmdidx == CMD_def)
945 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100946 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000947 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100948 goto theend;
949 }
950
951 // Detect line continuation: SOURCING_LNUM increased more than one.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100952 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100953 if (SOURCING_LNUM < sourcing_lnum_off)
954 sourcing_lnum_off -= SOURCING_LNUM;
955 else
956 sourcing_lnum_off = 0;
957
958 if (skip_until != NULL)
959 {
960 // Don't check for ":endfunc"/":enddef" between
961 // * ":append" and "."
962 // * ":python <<EOF" and "EOF"
963 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
964 if (heredoc_trimmed == NULL
965 || (is_heredoc && skipwhite(theline) == theline)
966 || STRNCMP(theline, heredoc_trimmed,
967 STRLEN(heredoc_trimmed)) == 0)
968 {
969 if (heredoc_trimmed == NULL)
970 p = theline;
971 else if (is_heredoc)
972 p = skipwhite(theline) == theline
973 ? theline : theline + STRLEN(heredoc_trimmed);
974 else
975 p = theline + STRLEN(heredoc_trimmed);
976 if (STRCMP(p, skip_until) == 0)
977 {
978 VIM_CLEAR(skip_until);
979 VIM_CLEAR(heredoc_trimmed);
980 getline_options = vim9_function
981 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
982 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200983
984 if (heredoc_concat_len > 0)
985 {
986 // Replace the starting line with all the concatenated
987 // lines.
988 ga_concat(&heredoc_ga, theline);
989 vim_free(((char_u **)(newlines->ga_data))[
990 heredoc_concat_len - 1]);
991 ((char_u **)(newlines->ga_data))[
992 heredoc_concat_len - 1] = heredoc_ga.ga_data;
993 ga_init(&heredoc_ga);
994 heredoc_concat_len = 0;
995 theline += STRLEN(theline); // skip the "EOF"
996 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100997 }
998 }
999 }
1000 else
1001 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001002 int c;
1003 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +00001004 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001005
1006 // skip ':' and blanks
1007 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
1008 ;
1009
1010 // Check for "endfunction", "enddef" or "}".
1011 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +00001012 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001013 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001014 ? *p == '}'
1015 : (checkforcmd(&p, nesting_def[nesting]
1016 ? "enddef" : "endfunction", 4)
1017 && *p != ':'))
1018 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00001019 if (!nesting_inline[nesting] && nesting_def[nesting]
1020 && p < cmd + 6)
1021 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001022 if (nesting-- == 0)
1023 {
1024 char_u *nextcmd = NULL;
1025
1026 if (*p == '|' || *p == '}')
1027 nextcmd = p + 1;
1028 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
1029 nextcmd = line_arg;
1030 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001031 && (vim9_function || p_verbose > 0))
1032 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +02001033 SOURCING_LNUM = sourcing_lnum_top
1034 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001035 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +00001036 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001037 else
1038 give_warning2((char_u *)
1039 _("W22: Text found after :endfunction: %s"),
1040 p, TRUE);
1041 }
1042 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001043 {
1044 // Another command follows. If the line came from "eap"
1045 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001046 // change "eap->cmdlinep" to point to the last fetched
1047 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001048 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001049 if (lines_to_free->ga_len > 0
1050 && *eap->cmdlinep !=
1051 ((char_u **)lines_to_free->ga_data)
1052 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001053 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001054 // *cmdlinep will be freed later, thus remove the
1055 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001056 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001057 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
1058 [lines_to_free->ga_len - 1];
1059 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001060 }
1061 }
1062 break;
1063 }
1064 }
1065
1066 // Check for mismatched "endfunc" or "enddef".
1067 // We don't check for "def" inside "func" thus we also can't check
1068 // for "enddef".
1069 // We continue to find the end of the function, although we might
1070 // not find it.
1071 else if (nesting_def[nesting])
1072 {
1073 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1074 emsg(_(e_mismatched_endfunction));
1075 }
1076 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1077 emsg(_(e_mismatched_enddef));
1078
1079 // Increase indent inside "if", "while", "for" and "try", decrease
1080 // at "end".
1081 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1082 indent -= 2;
1083 else if (STRNCMP(p, "if", 2) == 0
1084 || STRNCMP(p, "wh", 2) == 0
1085 || STRNCMP(p, "for", 3) == 0
1086 || STRNCMP(p, "try", 3) == 0)
1087 indent += 2;
1088
1089 // Check for defining a function inside this function.
1090 // Only recognize "def" inside "def", not inside "function",
1091 // For backwards compatibility, see Test_function_python().
1092 c = *p;
1093 if (is_function_cmd(&p)
1094 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1095 {
1096 if (*p == '!')
1097 p = skipwhite(p + 1);
1098 p += eval_fname_script(p);
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001099 vim_free(trans_function_name(&p, NULL, TRUE, 0));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001100 if (*skipwhite(p) == '(')
1101 {
1102 if (nesting == MAX_FUNC_NESTING - 1)
1103 emsg(_(e_function_nesting_too_deep));
1104 else
1105 {
1106 ++nesting;
1107 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001108 nesting_inline[nesting] = FALSE;
1109 indent += 2;
1110 }
1111 }
1112 }
1113
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001114 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001115 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001116 // Not a comment line: check for nested inline function.
1117 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001118 while (end > p && VIM_ISWHITE(*end))
1119 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001120 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001121 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001122 int is_block;
1123
1124 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001125 --end;
1126 while (end > p && VIM_ISWHITE(*end))
1127 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001128 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1129 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001130 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001131 char_u *s = p;
1132
1133 // check for line starting with "au" for :autocmd or
1134 // "com" for :command, these can use a {} block
1135 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1136 || checkforcmd_noparen(&s, "command", 3);
1137 }
1138
1139 if (is_block)
1140 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001141 if (nesting == MAX_FUNC_NESTING - 1)
1142 emsg(_(e_function_nesting_too_deep));
1143 else
1144 {
1145 ++nesting;
1146 nesting_def[nesting] = TRUE;
1147 nesting_inline[nesting] = TRUE;
1148 indent += 2;
1149 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001150 }
1151 }
1152 }
1153
1154 // Check for ":append", ":change", ":insert". Not for :def.
1155 p = skip_range(p, FALSE, NULL);
1156 if (!vim9_function
1157 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1158 || (p[0] == 'c'
1159 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1160 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1161 && (STRNCMP(&p[3], "nge", 3) != 0
1162 || !ASCII_ISALPHA(p[6])))))))
1163 || (p[0] == 'i'
1164 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1165 && (!ASCII_ISALPHA(p[2])
1166 || (p[2] == 's'
1167 && (!ASCII_ISALPHA(p[3])
1168 || p[3] == 'e'))))))))
1169 skip_until = vim_strsave((char_u *)".");
1170
1171 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1172 arg = skipwhite(skiptowhite(p));
1173 if (arg[0] == '<' && arg[1] =='<'
1174 && ((p[0] == 'p' && p[1] == 'y'
1175 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1176 || ((p[2] == '3' || p[2] == 'x')
1177 && !ASCII_ISALPHA(p[3]))))
1178 || (p[0] == 'p' && p[1] == 'e'
1179 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1180 || (p[0] == 't' && p[1] == 'c'
1181 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1182 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1183 && !ASCII_ISALPHA(p[3]))
1184 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1185 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1186 || (p[0] == 'm' && p[1] == 'z'
1187 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1188 ))
1189 {
1190 // ":python <<" continues until a dot, like ":append"
1191 p = skipwhite(arg + 2);
1192 if (STRNCMP(p, "trim", 4) == 0)
1193 {
1194 // Ignore leading white space.
1195 p = skipwhite(p + 4);
1196 heredoc_trimmed = vim_strnsave(theline,
1197 skipwhite(theline) - theline);
1198 }
1199 if (*p == NUL)
1200 skip_until = vim_strsave((char_u *)".");
1201 else
1202 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1203 getline_options = GETLINE_NONE;
1204 is_heredoc = TRUE;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001205 if (vim9_function && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001206 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001207 }
1208
Bram Moolenaard881d152022-05-13 13:50:36 +01001209 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001210 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001211 // Check for ":cmd v =<< [trim] EOF"
1212 // and ":cmd [a, b] =<< [trim] EOF"
1213 // and "lines =<< [trim] EOF" for Vim9
1214 // Where "cmd" can be "let", "var", "final" or "const".
zeertzjqa93d9cd2023-05-02 16:25:47 +01001215 arg = p;
1216 if (checkforcmd(&arg, "let", 2)
1217 || checkforcmd(&arg, "var", 3)
1218 || checkforcmd(&arg, "final", 5)
1219 || checkforcmd(&arg, "const", 5)
1220 || vim9_function)
Bram Moolenaard881d152022-05-13 13:50:36 +01001221 {
zeertzjqa93d9cd2023-05-02 16:25:47 +01001222 while (vim_strchr((char_u *)"$@&", *arg) != NULL)
1223 ++arg;
1224 arg = skipwhite(find_name_end(arg, NULL, NULL,
1225 FNE_INCL_BR | FNE_ALLOW_CURLY));
1226 if (vim9_function && *arg == ':')
1227 arg = skipwhite(skip_type(skipwhite(arg + 1), FALSE));
1228 if (arg[0] == '=' && arg[1] == '<' && arg[2] =='<')
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001229 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001230 p = skipwhite(arg + 3);
1231 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001232 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001233 if (STRNCMP(p, "trim", 4) == 0)
1234 {
1235 // Ignore leading white space.
1236 p = skipwhite(p + 4);
1237 heredoc_trimmed = vim_strnsave(theline,
1238 skipwhite(theline) - theline);
1239 continue;
1240 }
1241 if (STRNCMP(p, "eval", 4) == 0)
1242 {
1243 // Ignore leading white space.
1244 p = skipwhite(p + 4);
1245 continue;
1246 }
1247 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001248 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001249 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1250 getline_options = GETLINE_NONE;
1251 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001252 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001253 }
1254 }
1255 }
1256
1257 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001258 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001259 goto theend;
1260
Bram Moolenaar20677332021-06-06 17:02:53 +02001261 if (heredoc_concat_len > 0)
1262 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001263 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001264 // to be used for the instruction later.
1265 ga_concat(&heredoc_ga, theline);
1266 ga_concat(&heredoc_ga, (char_u *)"\n");
1267 p = vim_strsave((char_u *)"");
1268 }
1269 else
1270 {
1271 // Copy the line to newly allocated memory. get_one_sourceline()
1272 // allocates 250 bytes per line, this saves 80% on average. The
1273 // cost is an extra alloc/free.
1274 p = vim_strsave(theline);
1275 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001276 if (p == NULL)
1277 goto theend;
1278 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1279
1280 // Add NULL lines for continuation lines, so that the line count is
1281 // equal to the index in the growarray.
1282 while (sourcing_lnum_off-- > 0)
1283 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1284
1285 // Check for end of eap->arg.
1286 if (line_arg != NULL && *line_arg == NUL)
1287 line_arg = NULL;
1288 }
1289
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001290 // Return OK when no error was detected.
1291 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001292 ret = OK;
1293
1294theend:
1295 vim_free(skip_until);
1296 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001297 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001298 need_wait_return |= saved_wait_return;
1299 return ret;
1300}
1301
1302/*
1303 * Handle the body of a lambda. *arg points to the "{", process statements
1304 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001305 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001306 * When successful "rettv" is set to a funcref.
1307 */
1308 static int
1309lambda_function_body(
1310 char_u **arg,
1311 typval_T *rettv,
1312 evalarg_T *evalarg,
1313 garray_T *newargs,
1314 garray_T *argtypes,
1315 int varargs,
1316 garray_T *default_args,
1317 char_u *ret_type)
1318{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001319 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001320 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001321 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001322 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001323 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001324 exarg_T eap;
1325 garray_T newlines;
1326 char_u *cmdline = NULL;
1327 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001328 partial_T *pt;
1329 char_u *name;
1330 int lnum_save = -1;
1331 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1332
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001333 *arg = skipwhite(*arg + 1);
1334 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001335 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001336 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001337 return FAIL;
1338 }
1339
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001340 CLEAR_FIELD(eap);
1341 eap.cmdidx = CMD_block;
1342 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001343 eap.cmdlinep = &cmdline;
1344 eap.skip = !evaluate;
1345 if (evalarg->eval_cctx != NULL)
1346 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1347 else
1348 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001349 eap.ea_getline = evalarg->eval_getline;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001350 eap.cookie = evalarg->eval_cookie;
1351 }
1352
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001353 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001354 if (get_function_body(&eap, &newlines, NULL,
1355 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001356 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001357
1358 // When inside a lambda must add the function lines to evalarg.eval_ga.
1359 evalarg->eval_break_count += newlines.ga_len;
1360 if (gap->ga_itemsize > 0)
1361 {
1362 int idx;
1363 char_u *last;
1364 size_t plen;
1365 char_u *pnl;
1366
1367 for (idx = 0; idx < newlines.ga_len; ++idx)
1368 {
1369 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1370
Bram Moolenaarecb66452021-05-18 15:09:18 +02001371 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001372 goto erret;
1373
1374 // Going to concatenate the lines after parsing. For an empty or
1375 // comment line use an empty string.
1376 // Insert NL characters at the start of each line, the string will
1377 // be split again later in .get_lambda_tv().
1378 if (*p == NUL || vim9_comment_start(p))
1379 p = (char_u *)"";
1380 plen = STRLEN(p);
1381 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1382 if (pnl != NULL)
1383 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001384 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1385 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001386 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001387 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001388 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001389 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001390 // more is following after the "}", which was skipped
1391 last = cmdline;
1392 else
1393 // nothing is following the "}"
1394 last = (char_u *)"}";
1395 plen = STRLEN(last);
1396 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1397 if (pnl != NULL)
1398 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001399 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1400 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001401 }
1402
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001403 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001404 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001405 garray_T *tfgap = &evalarg->eval_tofree_ga;
1406
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001407 // Something comes after the "}".
1408 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001409
1410 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001411 if (ga_grow(tfgap, 1) == OK)
1412 {
1413 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1414 evalarg->eval_using_cmdline = TRUE;
1415 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001416 }
1417 else
1418 *arg = (char_u *)"";
1419
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001420 if (!evaluate)
1421 {
1422 ret = OK;
1423 goto erret;
1424 }
1425
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001426 name = get_lambda_name();
Bram Moolenaare01e5212023-01-08 20:31:18 +00001427 ufunc = alloc_ufunc(name);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001428 if (ufunc == NULL)
1429 goto erret;
1430 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001431 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001432 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001433 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001434 ufunc->uf_refcount = 1;
1435 ufunc->uf_args = *newargs;
1436 newargs->ga_data = NULL;
1437 ufunc->uf_def_args = *default_args;
1438 default_args->ga_data = NULL;
1439 ufunc->uf_func_type = &t_func_any;
1440
1441 // error messages are for the first function line
1442 lnum_save = SOURCING_LNUM;
1443 SOURCING_LNUM = sourcing_lnum_top;
1444
1445 // parse argument types
h-eastb895b0f2023-09-24 15:46:31 +02001446 if (parse_argument_types(ufunc, argtypes, varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001447 {
1448 SOURCING_LNUM = lnum_save;
1449 goto erret;
1450 }
1451
1452 // parse the return type, if any
1453 if (parse_return_type(ufunc, ret_type) == FAIL)
1454 goto erret;
1455
1456 pt = ALLOC_CLEAR_ONE(partial_T);
1457 if (pt == NULL)
1458 goto erret;
1459 pt->pt_func = ufunc;
1460 pt->pt_refcount = 1;
1461
1462 ufunc->uf_lines = newlines;
1463 newlines.ga_data = NULL;
1464 if (sandbox)
1465 ufunc->uf_flags |= FC_SANDBOX;
1466 if (!ASCII_ISUPPER(*ufunc->uf_name))
1467 ufunc->uf_flags |= FC_VIM9;
1468 ufunc->uf_script_ctx = current_sctx;
1469 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1470 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1471 set_function_type(ufunc);
1472
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001473 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1474
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001475 rettv->vval.v_partial = pt;
1476 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001477 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001478 ret = OK;
1479
1480erret:
1481 if (lnum_save >= 0)
1482 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001483 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001484 if (newargs != NULL)
1485 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001486 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001487 if (ufunc != NULL)
1488 {
1489 func_clear(ufunc, TRUE);
1490 func_free(ufunc, TRUE);
1491 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001492 return ret;
1493}
1494
1495/*
1496 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001497 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001498 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001499 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1500 */
1501 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001502get_lambda_tv(
1503 char_u **arg,
1504 typval_T *rettv,
1505 int types_optional,
1506 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001507{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001508 int evaluate = evalarg != NULL
1509 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001510 garray_T newargs;
1511 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001512 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001513 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001514 garray_T default_args;
h-eastb895b0f2023-09-24 15:46:31 +02001515 garray_T arg_objm;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001516 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001517 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001518 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001519 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001520 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001521 char_u *s;
1522 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001523 int *old_eval_lavars = eval_lavars_used;
1524 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001525 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001526 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001527 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001528 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001529 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001530 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001531
Bram Moolenaar4525a572022-02-13 11:57:33 +00001532 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001533 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001534
1535 ga_init(&newargs);
1536 ga_init(&newlines);
1537
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001538 // First, check if this is really a lambda expression. "->" or "=>" must
1539 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001540 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001541 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001542 types_optional ? &argtypes : NULL, types_optional,
1543 types_optional ? &arg_objm : NULL, evalarg,
1544 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001545 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001546 {
1547 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001548 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001549 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001550 ga_clear(&arg_objm);
1551 }
Bram Moolenaar0346b792021-01-31 22:18:29 +01001552 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001553 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001554
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001555 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001556 if (evaluate)
1557 pnewargs = &newargs;
1558 else
1559 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001560 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001561 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
h-eastb895b0f2023-09-24 15:46:31 +02001562 types_optional ? &argtypes : NULL, types_optional,
1563 types_optional ? &arg_objm : NULL, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001564 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001565 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001566 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001567 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001568 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001569 {
1570 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001571 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001572 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001573 ga_clear(&arg_objm);
1574 }
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001575 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001576 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001577 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001578 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001579
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001580 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1581 if (ret_type != NULL)
1582 {
1583 ret_type = vim_strsave(ret_type);
1584 tofree2 = ret_type;
1585 }
1586
Bram Moolenaare38eab22019-12-05 21:50:01 +01001587 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001588 if (evaluate)
1589 eval_lavars_used = &eval_lavars;
1590
Bram Moolenaar65c44152020-12-24 15:14:01 +01001591 *arg = skipwhite_and_linebreak(*arg, evalarg);
1592
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001593 // Recognize "{" as the start of a function body.
1594 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001595 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001596 if (evalarg == NULL)
1597 // cannot happen?
1598 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001599 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001600 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1601 types_optional ? &argtypes : NULL, varargs,
1602 &default_args, ret_type) == FAIL)
1603 goto errret;
1604 goto theend;
1605 }
1606 if (default_args.ga_len > 0)
1607 {
1608 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001609 goto errret;
1610 }
1611
Bram Moolenaare38eab22019-12-05 21:50:01 +01001612 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001613 start = *arg;
1614 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001615 if (ret == FAIL)
1616 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001617
Bram Moolenaar65c44152020-12-24 15:14:01 +01001618 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001619 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001620 *arg = skipwhite_and_linebreak(*arg, evalarg);
1621 if (**arg != '}')
1622 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001623 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001624 goto errret;
1625 }
1626 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001627 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001628
1629 if (evaluate)
1630 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001631 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001632 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001633 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001634 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001635 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001636
Bram Moolenaare01e5212023-01-08 20:31:18 +00001637 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001638 if (fp == NULL)
1639 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001640 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001641 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001642 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001643 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001644
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001645 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001646 if (ga_grow(&newlines, 1) == FAIL)
1647 goto errret;
1648
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001649 // If there are line breaks, we need to split up the string.
1650 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001651 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001652 line_end = end;
1653
1654 // Add "return " before the expression (or the first line).
1655 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001656 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001657 if (p == NULL)
1658 goto errret;
1659 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1660 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001661 vim_strncpy(p + 7, start, line_end - start);
1662
1663 if (line_end != end)
1664 {
1665 // Add more lines, split by line breaks. Thus is used when a
1666 // lambda with { cmds } is encountered.
1667 while (*line_end == '\n')
1668 {
1669 if (ga_grow(&newlines, 1) == FAIL)
1670 goto errret;
1671 start = line_end + 1;
1672 line_end = vim_strchr(start, '\n');
1673 if (line_end == NULL)
1674 line_end = end;
1675 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1676 vim_strnsave(start, line_end - start);
1677 }
1678 }
1679
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001680 if (strstr((char *)p + 7, "a:") == NULL)
1681 // No a: variables are used for sure.
1682 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001683
1684 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001685 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001686 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001687 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001688 if (types_optional)
1689 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001690 if (parse_argument_types(fp, &argtypes,
h-eastb895b0f2023-09-24 15:46:31 +02001691 vim9script && varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001692 goto errret;
1693 if (ret_type != NULL)
1694 {
1695 fp->uf_ret_type = parse_type(&ret_type,
1696 &fp->uf_type_list, TRUE);
1697 if (fp->uf_ret_type == NULL)
1698 goto errret;
1699 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001700 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001701 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001702 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001703
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001704 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001705 if (current_funccal != NULL && eval_lavars)
1706 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001707 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001708 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001709 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001710 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001711
1712#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001713 if (prof_def_func())
1714 func_do_profile(fp);
1715#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001716 if (sandbox)
1717 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001718 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001719 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001720 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001721 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001722 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001723 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001724 // Use the line number of the arguments.
1725 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001726
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001727 function_using_block_scopes(fp, evalarg->eval_cstack);
1728
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001729 pt->pt_func = fp;
1730 pt->pt_refcount = 1;
1731 rettv->vval.v_partial = pt;
1732 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001733
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001734 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001735 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001736
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001737theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001738 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001739 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001740 if (types_optional)
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001741 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001742 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001743 ga_clear(&arg_objm);
1744 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001745
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001746 return OK;
1747
1748errret:
1749 ga_clear_strings(&newargs);
1750 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001751 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001752 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001753 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001754 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001755 ga_clear(&arg_objm);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001756 if (fp != NULL)
1757 vim_free(fp->uf_arg_types);
1758 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001759 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001760 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001761 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001762 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001763 return FAIL;
1764}
1765
1766/*
1767 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1768 * name it contains, otherwise return "name".
1769 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1770 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001771 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1772 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001773 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001774 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001775 */
1776 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001777deref_func_name(
1778 char_u *name,
1779 int *lenp,
1780 partial_T **partialp,
1781 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001782 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001783 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001784 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001785{
1786 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001787 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001788 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001789 char_u *s = NULL;
1790 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001791 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001792
1793 if (partialp != NULL)
1794 *partialp = NULL;
1795
1796 cc = name[*lenp];
1797 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001798
Bram Moolenaar71f21932022-01-07 18:20:55 +00001799 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001800 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001801 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001802 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001803 tv = &v->di_tv;
1804 }
1805 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1806 {
1807 imported_T *import;
1808 char_u *p = name;
1809 int len = *lenp;
1810
1811 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001812 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001813 p = name + 2;
1814 len -= 2;
1815 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001816 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001817
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001818 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001819 if (import != NULL)
1820 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001821 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001822 if (new_function)
1823 semsg(_(e_redefining_imported_item_str), name);
1824 else
1825 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001826 name[len] = cc;
1827 *lenp = 0;
1828 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001829 }
1830 }
1831
1832 if (tv != NULL)
1833 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001834 if (found_var != NULL)
1835 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001836 if (tv->v_type == VAR_FUNC)
1837 {
1838 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001839 {
1840 *lenp = 0;
1841 return (char_u *)""; // just in case
1842 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001843 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001844 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001845 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001846
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001847 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001848 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001849 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001850
1851 if (pt == NULL)
1852 {
1853 *lenp = 0;
1854 return (char_u *)""; // just in case
1855 }
1856 if (partialp != NULL)
1857 *partialp = pt;
1858 s = partial_name(pt);
1859 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001860 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001861
1862 if (s != NULL)
1863 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001864 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001865 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001866 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001867
1868 if (sv != NULL)
1869 *type = sv->sv_type;
1870 }
1871 return s;
1872 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001873 }
1874
1875 return name;
1876}
1877
1878/*
1879 * Give an error message with a function name. Handle <SNR> things.
1880 * "ermsg" is to be passed without translation, use N_() instead of _().
1881 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001882 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001883emsg_funcname(char *ermsg, char_u *name)
1884{
Bram Moolenaar54598062022-01-13 12:05:09 +00001885 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001886
Bram Moolenaar54598062022-01-13 12:05:09 +00001887 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001888 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001889 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001890 if (p != name)
1891 vim_free(p);
1892}
1893
1894/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001895 * Get function arguments at "*arg" and advance it.
1896 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001897 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001898 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001899 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001900get_func_arguments(
1901 char_u **arg,
1902 evalarg_T *evalarg,
1903 int partial_argc,
1904 typval_T *argvars,
Ernie Raelb077b582023-12-14 20:11:44 +01001905 int *argcount,
1906 int is_builtin)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001907{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001908 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001909 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001910 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001911 int evaluate = evalarg == NULL
1912 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001913
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001914 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001915 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001916 // skip the '(' or ',' and possibly line breaks
1917 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1918
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001919 if (*argp == ')' || *argp == ',' || *argp == NUL)
1920 break;
Ernie Raelb077b582023-12-14 20:11:44 +01001921
1922 int arg_idx = *argcount;
1923 if (eval1(&argp, &argvars[arg_idx], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001924 {
1925 ret = FAIL;
1926 break;
1927 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001928 ++*argcount;
Ernie Raelb077b582023-12-14 20:11:44 +01001929 if (!is_builtin && check_typval_is_value(&argvars[arg_idx]) == FAIL)
1930 {
1931 ret = FAIL;
1932 break;
1933 }
1934
Bram Moolenaar9d489562020-07-30 20:08:50 +02001935 // The comma should come right after the argument, but this wasn't
1936 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001937 if (vim9script)
1938 {
1939 if (*argp != ',' && *skipwhite(argp) == ',')
1940 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001941 if (evaluate)
1942 semsg(_(e_no_white_space_allowed_before_str_str),
1943 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001944 ret = FAIL;
1945 break;
1946 }
1947 }
1948 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001949 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001950 if (*argp != ',')
1951 break;
Christian Brabandt00cb2472023-09-05 20:46:25 +02001952 if (vim9script && !IS_WHITE_NL_OR_NUL(argp[1]))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001953 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001954 if (evaluate)
1955 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001956 ret = FAIL;
1957 break;
1958 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001959 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001960
Bram Moolenaare6b53242020-07-01 17:28:33 +02001961 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001962 if (*argp == ')')
1963 ++argp;
1964 else
1965 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001966 *arg = argp;
1967 return ret;
1968}
1969
1970/*
1971 * Call a function and put the result in "rettv".
1972 * Return OK or FAIL.
1973 */
1974 int
1975get_func_tv(
1976 char_u *name, // name of the function
1977 int len, // length of "name" or -1 to use strlen()
1978 typval_T *rettv,
1979 char_u **arg, // argument, pointing to the '('
1980 evalarg_T *evalarg, // for line continuation
1981 funcexe_T *funcexe) // various values
1982{
1983 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001984 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001985 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1986 int argcount = 0; // number of arguments found
1987 int vim9script = in_vim9script();
1988 int evaluate = evalarg == NULL
1989 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
1990
1991 argp = *arg;
1992 ret = get_func_arguments(&argp, evalarg,
1993 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
Ernie Raelb077b582023-12-14 20:11:44 +01001994 argvars, &argcount, builtin_function(name, -1));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001995
1996 if (ret == OK)
1997 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001998 int i = 0;
1999 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002000
2001 if (get_vim_var_nr(VV_TESTING))
2002 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002003 // Prepare for calling test_garbagecollect_now(), need to know
2004 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002005 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002006 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002007 for (i = 0; i < argcount; ++i)
2008 if (ga_grow(&funcargs, 1) == OK)
2009 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
2010 &argvars[i];
2011 }
2012
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002013 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00002014 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002015 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002016 // An error in a builtin function does not return FAIL, but we do
2017 // want to abort further processing if an error was given.
2018 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002019 clear_tv(rettv);
2020 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002021
2022 funcargs.ga_len -= i;
2023 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002024 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002025 {
2026 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002027 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002028 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00002029 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002030 }
2031
2032 while (--argcount >= 0)
2033 clear_tv(&argvars[argcount]);
2034
Bram Moolenaar4525a572022-02-13 11:57:33 +00002035 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02002036 *arg = argp;
2037 else
2038 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002039 return ret;
2040}
2041
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002042/*
2043 * Return TRUE if "p" starts with "<SID>" or "s:".
2044 * Only works if eval_fname_script() returned non-zero for "p"!
2045 */
2046 static int
2047eval_fname_sid(char_u *p)
2048{
2049 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
2050}
2051
2052/*
2053 * In a script change <SID>name() and s:name() to K_SNR 123_name().
2054 * Change <SNR>123_name() to K_SNR 123_name().
2055 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002056 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002057 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002058 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00002059fname_trans_sid(
2060 char_u *name,
2061 char_u *fname_buf,
2062 char_u **tofree,
2063 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002064{
2065 int llen;
2066 char_u *fname;
2067 int i;
2068
2069 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002070 if (llen == 0)
2071 return name; // no prefix
2072
2073 fname_buf[0] = K_SPECIAL;
2074 fname_buf[1] = KS_EXTRA;
2075 fname_buf[2] = (int)KE_SNR;
2076 i = 3;
2077 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002078 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002079 if (current_sctx.sc_sid <= 0)
2080 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002081 else
2082 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002083 sprintf((char *)fname_buf + 3, "%ld_",
2084 (long)current_sctx.sc_sid);
2085 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002086 }
2087 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002088 if (i + STRLEN(name + llen) < FLEN_FIXED)
2089 {
2090 STRCPY(fname_buf + i, name + llen);
2091 fname = fname_buf;
2092 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002093 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002094 {
2095 fname = alloc(i + STRLEN(name + llen) + 1);
2096 if (fname == NULL)
2097 *error = FCERR_OTHER;
2098 else
2099 {
2100 *tofree = fname;
2101 mch_memmove(fname, fname_buf, (size_t)i);
2102 STRCPY(fname + i, name + llen);
2103 }
2104 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002105 return fname;
2106}
2107
2108/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002109 * Concatenate the script ID and function name into "<SNR>99_name".
2110 * "buffer" must have size MAX_FUNC_NAME_LEN.
2111 */
2112 void
2113func_name_with_sid(char_u *name, int sid, char_u *buffer)
2114{
2115 // A script-local function is stored as "<SNR>99_name".
2116 buffer[0] = K_SPECIAL;
2117 buffer[1] = KS_EXTRA;
2118 buffer[2] = (int)KE_SNR;
2119 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2120 (long)sid, name);
2121}
2122
2123/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124 * Find a function "name" in script "sid".
2125 */
2126 static ufunc_T *
2127find_func_with_sid(char_u *name, int sid)
2128{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002129 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002130 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131
Bram Moolenaarb8822442022-01-11 15:24:05 +00002132 if (!SCRIPT_ID_VALID(sid))
2133 return NULL; // not in a script
2134
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002135 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136 hi = hash_find(&func_hashtab, buffer);
2137 if (!HASHITEM_EMPTY(hi))
2138 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002139 return NULL;
2140}
2141
2142/*
2143 * Find a function "name" in script "sid" prefixing the autoload prefix.
2144 */
2145 static ufunc_T *
2146find_func_with_prefix(char_u *name, int sid)
2147{
2148 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002149 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002150 scriptitem_T *si;
2151
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002152 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002153 return NULL; // already has the prefix
2154 if (!SCRIPT_ID_VALID(sid))
2155 return NULL; // not in a script
2156 si = SCRIPT_ITEM(sid);
2157 if (si->sn_autoload_prefix != NULL)
2158 {
2159 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2160 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002161 char_u *namep;
2162
2163 // skip a "<SNR>99_" prefix
2164 namep = untrans_function_name(name);
2165 if (namep == NULL)
2166 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002167
2168 // An exported function in an autoload script is stored as
2169 // "dir#path#name".
2170 if (len < sizeof(buffer))
2171 auto_name = buffer;
2172 else
2173 auto_name = alloc(len);
2174 if (auto_name != NULL)
2175 {
2176 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002177 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002178 hi = hash_find(&func_hashtab, auto_name);
2179 if (auto_name != buffer)
2180 vim_free(auto_name);
2181 if (!HASHITEM_EMPTY(hi))
2182 return HI2UF(hi);
2183 }
2184 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185
2186 return NULL;
2187}
2188
2189/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002190 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002191 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2192 * functions.
2193 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002194 * Return NULL for unknown function.
2195 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002196 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002197find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002198{
2199 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002200 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002202 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002203 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002204 // Find script-local function before global one.
2205 if (in_vim9script() && eval_isnamec1(*name)
2206 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002207 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002208 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2209 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002210 if (func != NULL)
2211 return func;
2212 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002213 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2214 {
2215 char_u *p = name + 5;
2216 long sid;
2217
2218 // printable "<SNR>123_Name" form
2219 sid = getdigits(&p);
2220 if (*p == '_')
2221 {
2222 func = find_func_with_sid(p + 1, (int)sid);
2223 if (func != NULL)
2224 return func;
2225 }
2226 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002228
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002229 if ((flags & FFED_NO_GLOBAL) == 0)
2230 {
2231 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002232 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002233 if (!HASHITEM_EMPTY(hi))
2234 return HI2UF(hi);
2235 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236
Bram Moolenaarb8822442022-01-11 15:24:05 +00002237 // Find autoload function if this is an autoload script.
2238 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2239 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002240}
2241
2242/*
2243 * Find a function by name, return pointer to it in ufuncs.
2244 * "cctx" is passed in a :def function to find imported functions.
2245 * Return NULL for unknown or dead function.
2246 */
2247 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002248find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002250 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251
2252 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2253 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002254 return NULL;
2255}
2256
2257/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002258 * Return TRUE if "ufunc" is a global function.
2259 */
2260 int
2261func_is_global(ufunc_T *ufunc)
2262{
2263 return ufunc->uf_name[0] != K_SPECIAL;
2264}
2265
2266/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002267 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2268 */
2269 int
2270func_requires_g_prefix(ufunc_T *ufunc)
2271{
2272 return ufunc->uf_name[0] != K_SPECIAL
2273 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002274 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
2275 && !isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002276}
2277
2278/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002279 * Copy the function name of "fp" to buffer "buf".
2280 * "buf" must be able to hold the function name plus three bytes.
2281 * Takes care of script-local function names.
2282 */
2283 static void
2284cat_func_name(char_u *buf, ufunc_T *fp)
2285{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002286 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002287 {
2288 STRCPY(buf, "<SNR>");
2289 STRCAT(buf, fp->uf_name + 3);
2290 }
2291 else
2292 STRCPY(buf, fp->uf_name);
2293}
2294
2295/*
2296 * Add a number variable "name" to dict "dp" with value "nr".
2297 */
2298 static void
2299add_nr_var(
2300 dict_T *dp,
2301 dictitem_T *v,
2302 char *name,
2303 varnumber_T nr)
2304{
2305 STRCPY(v->di_key, name);
2306 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002307 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002308 v->di_tv.v_type = VAR_NUMBER;
2309 v->di_tv.v_lock = VAR_FIXED;
2310 v->di_tv.vval.v_number = nr;
2311}
2312
2313/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002314 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002315 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002316 static void
2317free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002318{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002319 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002320
Bram Moolenaarca16c602022-09-06 18:57:08 +01002321 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002322 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002323 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002324
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002325 // When garbage collecting a funccall_T may be freed before the
2326 // function that references it, clear its uf_scoped field.
2327 // The function may have been redefined and point to another
2328 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002329 if (fp != NULL && fp->uf_scoped == fc)
2330 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002331 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002332 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002333
Bram Moolenaarca16c602022-09-06 18:57:08 +01002334 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002335 vim_free(fc);
2336}
2337
2338/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002339 * Free "fc" and what it contains.
2340 * Can be called only when "fc" is kept beyond the period of it called,
2341 * i.e. after cleanup_function_call(fc).
2342 */
2343 static void
2344free_funccal_contents(funccall_T *fc)
2345{
2346 listitem_T *li;
2347
2348 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002349 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002350
2351 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002352 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002353
2354 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002355 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002356 clear_tv(&li->li_tv);
2357
2358 free_funccal(fc);
2359}
2360
2361/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002362 * Handle the last part of returning from a function: free the local hashtable.
2363 * Unless it is still in use by a closure.
2364 */
2365 static void
2366cleanup_function_call(funccall_T *fc)
2367{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002368 int may_free_fc = fc->fc_refcount <= 0;
2369 int free_fc = TRUE;
2370
Bram Moolenaarca16c602022-09-06 18:57:08 +01002371 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002372
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002373 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002374 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2375 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002376 else
2377 free_fc = FALSE;
2378
2379 // If the a:000 list and the l: and a: dicts are not referenced and
2380 // there is no closure using it, we can free the funccall_T and what's
2381 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002382 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2383 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002384 else
2385 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002386 int todo;
2387 hashitem_T *hi;
2388 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002389
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002390 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002391
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002392 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002393 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002394 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002395 {
2396 if (!HASHITEM_EMPTY(hi))
2397 {
2398 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002399 di = HI2DI(hi);
2400 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002401 }
2402 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002403 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002404
Bram Moolenaarca16c602022-09-06 18:57:08 +01002405 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2406 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002407 else
2408 {
2409 listitem_T *li;
2410
2411 free_fc = FALSE;
2412
2413 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002414 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002415 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002416 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002417
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002418 if (free_fc)
2419 free_funccal(fc);
2420 else
2421 {
2422 static int made_copy = 0;
2423
2424 // "fc" is still in use. This can happen when returning "a:000",
2425 // assigning "l:" to a global variable or defining a closure.
2426 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002427 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002428 previous_funccal = fc;
2429
2430 if (want_garbage_collect)
2431 // If garbage collector is ready, clear count.
2432 made_copy = 0;
2433 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002434 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002435 // We have made a lot of copies, worth 4 Mbyte. This can happen
2436 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002437 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002438 // too much memory.
2439 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002440 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002441 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002442 }
2443}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002444
2445/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002446 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2447 */
2448 static int
2449numbered_function(char_u *name)
2450{
2451 return isdigit(*name)
2452 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2453}
2454
2455/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002456 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002457 * 1. ordinary names, function defined with :function or :def;
2458 * can start with "<SNR>123_" literally or with K_SPECIAL.
2459 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002460 * For the first we only count the name stored in func_hashtab as a reference,
2461 * using function() does not count as a reference, because the function is
2462 * looked up by name.
2463 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002464 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002465func_name_refcount(char_u *name)
2466{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002467 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002468}
2469
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470/*
2471 * Unreference "fc": decrement the reference count and free it when it
2472 * becomes zero. "fp" is detached from "fc".
2473 * When "force" is TRUE we are exiting.
2474 */
2475 static void
2476funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2477{
2478 funccall_T **pfc;
2479 int i;
2480
2481 if (fc == NULL)
2482 return;
2483
2484 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002485 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2486 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2487 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2488 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489 {
2490 if (fc == *pfc)
2491 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002492 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 free_funccal_contents(fc);
2494 return;
2495 }
2496 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002497 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2498 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2499 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500}
2501
2502/*
2503 * Remove the function from the function hashtable. If the function was
2504 * deleted while it still has references this was already done.
2505 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2506 */
2507 static int
2508func_remove(ufunc_T *fp)
2509{
2510 hashitem_T *hi;
2511
2512 // Return if it was already virtually deleted.
2513 if (fp->uf_flags & FC_DEAD)
2514 return FALSE;
2515
2516 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002517 if (HASHITEM_EMPTY(hi))
2518 return FALSE;
2519
2520 // When there is a def-function index do not actually remove the
2521 // function, so we can find the index when defining the function again.
2522 // Do remove it when it's a copy.
2523 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002525 fp->uf_flags |= FC_DEAD;
2526 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002528 hash_remove(&func_hashtab, hi, "remove function");
2529 fp->uf_flags |= FC_DELETED;
2530 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531}
2532
2533 static void
2534func_clear_items(ufunc_T *fp)
2535{
2536 ga_clear_strings(&(fp->uf_args));
2537 ga_clear_strings(&(fp->uf_def_args));
2538 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002540 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002541 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01002542 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002543
2544 // Increment the refcount of this function to avoid it being freed
2545 // recursively when the partial is freed.
2546 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002547 partial_unref(fp->uf_partial);
2548 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002549 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002550
2551#ifdef FEAT_LUA
2552 if (fp->uf_cb_free != NULL)
2553 {
2554 fp->uf_cb_free(fp->uf_cb_state);
2555 fp->uf_cb_free = NULL;
2556 }
2557
2558 fp->uf_cb_state = NULL;
2559 fp->uf_cb = NULL;
2560#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561#ifdef FEAT_PROFILE
2562 VIM_CLEAR(fp->uf_tml_count);
2563 VIM_CLEAR(fp->uf_tml_total);
2564 VIM_CLEAR(fp->uf_tml_self);
2565#endif
2566}
2567
2568/*
2569 * Free all things that a function contains. Does not free the function
2570 * itself, use func_free() for that.
2571 * When "force" is TRUE we are exiting.
2572 */
2573 static void
2574func_clear(ufunc_T *fp, int force)
2575{
2576 if (fp->uf_cleared)
2577 return;
2578 fp->uf_cleared = TRUE;
2579
2580 // clear this function
2581 func_clear_items(fp);
2582 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002583 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584}
2585
2586/*
2587 * Free a function and remove it from the list of functions. Does not free
2588 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002589 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002590 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002592 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002593func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594{
2595 // Only remove it when not done already, otherwise we would remove a newer
2596 // version of the function with the same name.
2597 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2598 func_remove(fp);
2599
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002600 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002601 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002602 if (fp->uf_dfunc_idx > 0)
2603 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002604 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002606 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002607 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002608 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609}
2610
2611/*
2612 * Free all things that a function contains and free the function itself.
2613 * When "force" is TRUE we are exiting.
2614 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002615 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616func_clear_free(ufunc_T *fp, int force)
2617{
2618 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002619 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2620 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002621 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002622 else
2623 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624}
2625
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002626/*
2627 * Copy already defined function "lambda" to a new function with name "global".
2628 * This is for when a compiled function defines a global function.
2629 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002630 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002631copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002632 char_u *lambda,
2633 char_u *global,
2634 loopvarinfo_T *loopvarinfo,
2635 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002636{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002637 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002638 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002639
2640 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002641 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002642 semsg(_(e_lambda_function_not_found_str), lambda);
2643 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002644 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002645
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002646 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002647 if (fp != NULL)
2648 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002649 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002650 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002651 return FAIL;
2652 }
2653
Bram Moolenaare01e5212023-01-08 20:31:18 +00002654 fp = alloc_ufunc(global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002655 if (fp == NULL)
2656 return FAIL;
2657
2658 fp->uf_varargs = ufunc->uf_varargs;
2659 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2660 fp->uf_def_status = ufunc->uf_def_status;
2661 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2662 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2663 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2664 == FAIL
2665 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2666 goto failed;
2667
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002668 if (ufunc->uf_arg_types != NULL)
2669 {
2670 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2671 if (fp->uf_arg_types == NULL)
2672 goto failed;
2673 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2674 sizeof(type_T *) * fp->uf_args.ga_len);
2675 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002676 if (ufunc->uf_va_name != NULL)
2677 {
2678 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2679 if (fp->uf_va_name == NULL)
2680 goto failed;
2681 }
2682 fp->uf_ret_type = ufunc->uf_ret_type;
2683
2684 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002685
2686 fp->uf_name_exp = NULL;
2687 set_ufunc_name(fp, global);
2688
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002689 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002690
2691 // the referenced dfunc_T is now used one more time
2692 link_def_function(fp);
2693
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002694 // Create a partial to store the context of the function where it was
2695 // instantiated. Only needs to be done once. Do this on the original
2696 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002697 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2698 {
2699 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2700
2701 if (pt == NULL)
2702 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002703 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002704 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002705 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002706 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002707 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002708 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002709 }
2710
2711 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002712
2713failed:
2714 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002715 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002716}
2717
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002718static int funcdepth = 0;
2719
2720/*
2721 * Increment the function call depth count.
2722 * Return FAIL when going over 'maxfuncdepth'.
2723 * Otherwise return OK, must call funcdepth_decrement() later!
2724 */
2725 int
2726funcdepth_increment(void)
2727{
2728 if (funcdepth >= p_mfd)
2729 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002730 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002731 return FAIL;
2732 }
2733 ++funcdepth;
2734 return OK;
2735}
2736
2737 void
2738funcdepth_decrement(void)
2739{
2740 --funcdepth;
2741}
2742
2743/*
2744 * Get the current function call depth.
2745 */
2746 int
2747funcdepth_get(void)
2748{
2749 return funcdepth;
2750}
2751
2752/*
2753 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002754 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002755 * times as funcdepth_increment().
2756 */
2757 void
2758funcdepth_restore(int depth)
2759{
2760 funcdepth = depth;
2761}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002762
2763/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002764 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2765 * "rettv".
2766 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2767 * Returns NULL when allocation fails.
2768 */
2769 funccall_T *
2770create_funccal(ufunc_T *fp, typval_T *rettv)
2771{
2772 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2773
2774 if (fc == NULL)
2775 return NULL;
2776 fc->fc_caller = current_funccal;
2777 current_funccal = fc;
2778 fc->fc_func = fp;
2779 func_ptr_ref(fp);
2780 fc->fc_rettv = rettv;
2781 return fc;
2782}
2783
2784/*
2785 * To be called when returning from a compiled function; restores
2786 * current_funccal.
2787 */
2788 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002789remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002790{
2791 funccall_T *fc = current_funccal;
2792
2793 current_funccal = fc->fc_caller;
2794 free_funccal(fc);
2795}
2796
2797/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002798 * Call a user function.
2799 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002800 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002801call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002802 ufunc_T *fp, // pointer to function
2803 int argcount, // nr of args
2804 typval_T *argvars, // arguments
2805 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002806 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002807 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002808{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002809 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002810 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002811 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002812 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002813 funccall_T *fc;
2814 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002815 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002816 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002817 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002818 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002819 int i;
2820 int ai;
2821 int islambda = FALSE;
2822 char_u numbuf[NUMBUFLEN];
2823 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002824 typval_T *tv_to_free[MAX_FUNC_ARGS];
2825 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002826#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002827 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002828#endif
ichizok7e5fe382023-04-15 13:17:50 +01002829 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002830
Bram Moolenaarb2049902021-01-24 12:53:53 +01002831#ifdef FEAT_PROFILE
2832 CLEAR_FIELD(profile_info);
2833#endif
2834
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002835 // If depth of calling is getting too high, don't execute the function.
2836 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002837 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002838 rettv->v_type = VAR_NUMBER;
2839 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002840 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002841 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002842
Bram Moolenaare38eab22019-12-05 21:50:01 +01002843 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002844
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002845 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002846 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00002847 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002848 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002849 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002850 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2851 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002852 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002853 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002854
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002855 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002857#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002858 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002859#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002860 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002861#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002862 if (do_profiling == PROF_YES)
2863 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002864#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002865 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002866 if (call_def_function(fp, argcount, argvars, 0,
2867 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
2868 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002869 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002870#ifdef FEAT_PROFILE
2871 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002872 || (caller != NULL && caller->uf_profiling)))
2873 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002874#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002875 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002876 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002877 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878 }
2879
Bram Moolenaar38453522021-11-28 22:00:12 +00002880 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002881
2882 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002883 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2884 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2885 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002886 */
2887 /*
2888 * Init l: variables.
2889 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002890 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002891 if (selfdict != NULL)
2892 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002893 // Set l:self to "selfdict". Use "name" to avoid a warning from
2894 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002895 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002896 name = v->di_key;
2897 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002898 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002899 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002900 v->di_tv.v_type = VAR_DICT;
2901 v->di_tv.v_lock = 0;
2902 v->di_tv.vval.v_dict = selfdict;
2903 ++selfdict->dv_refcount;
2904 }
2905
2906 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002907 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002908 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002909 * Set a:000 to a list with room for the "..." arguments.
2910 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002911 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002912 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002913 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002914 (varnumber_T)(argcount >= fp->uf_args.ga_len
2915 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002916 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002917 if ((fp->uf_flags & FC_NOARGS) == 0)
2918 {
2919 // Use "name" to avoid a warning from some compiler that checks the
2920 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002921 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002922 name = v->di_key;
2923 STRCPY(name, "000");
2924 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002925 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002926 v->di_tv.v_type = VAR_LIST;
2927 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002928 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002929 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002930 CLEAR_FIELD(fc->fc_l_varlist);
2931 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2932 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002933
2934 /*
2935 * Set a:firstline to "firstline" and a:lastline to "lastline".
2936 * Set a:name to named arguments.
2937 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002938 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002939 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002940 if ((fp->uf_flags & FC_NOARGS) == 0)
2941 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002942 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2943 "firstline", (varnumber_T)funcexe->fe_firstline);
2944 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2945 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002946 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002947 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002948 {
2949 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002950 typval_T def_rettv;
2951 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002952
2953 ai = i - fp->uf_args.ga_len;
2954 if (ai < 0)
2955 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002956 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002957 name = FUNCARG(fp, i);
2958 if (islambda)
2959 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002960
2961 // evaluate named argument default expression
2962 isdefault = ai + fp->uf_def_args.ga_len >= 0
2963 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2964 && argvars[i].vval.v_number == VVAL_NONE));
2965 if (isdefault)
2966 {
2967 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002968
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002969 def_rettv.v_type = VAR_NUMBER;
2970 def_rettv.vval.v_number = -1;
2971
2972 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2973 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002974 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002975 {
2976 default_arg_err = 1;
2977 break;
2978 }
2979 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002980 }
2981 else
2982 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002983 if ((fp->uf_flags & FC_NOARGS) != 0)
2984 // Bail out if no a: arguments used (in lambda).
2985 break;
2986
Bram Moolenaare38eab22019-12-05 21:50:01 +01002987 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002988 sprintf((char *)numbuf, "%d", ai + 1);
2989 name = numbuf;
2990 }
2991 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2992 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002993 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002994 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002995 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002996 }
2997 else
2998 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002999 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003000 if (v == NULL)
3001 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003002 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003003 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003004
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003005 // Note: the values are copied directly to avoid alloc/free.
3006 // "argvars" must have VAR_FIXED for v_lock.
3007 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003008 v->di_tv.v_lock = VAR_FIXED;
3009
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003010 if (isdefault)
3011 // Need to free this later, no matter where it's stored.
3012 tv_to_free[tv_to_free_len++] = &v->di_tv;
3013
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003014 if (addlocal)
3015 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003016 // Named arguments should be accessed without the "a:" prefix in
3017 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003018 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003019 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003020 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003021 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003022 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003023
3024 if (ai >= 0 && ai < MAX_FUNC_ARGS)
3025 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003026 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003027
3028 li->li_tv = argvars[i];
3029 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003030 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003031 }
3032 }
3033
Bram Moolenaare38eab22019-12-05 21:50:01 +01003034 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003035 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003036
3037 if (fp->uf_flags & FC_SANDBOX)
3038 {
3039 using_sandbox = TRUE;
3040 ++sandbox;
3041 }
3042
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003043 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003044 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003045 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003046 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003047 ++no_wait_return;
3048 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003049
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003050 smsg(_("calling %s"), SOURCING_NAME);
3051 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003052 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003053 char_u buf[MSG_BUF_LEN];
3054 char_u numbuf2[NUMBUFLEN];
3055 char_u *tofree;
3056 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003057
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003058 msg_puts("(");
3059 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003060 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003061 if (i > 0)
3062 msg_puts(", ");
3063 if (argvars[i].v_type == VAR_NUMBER)
3064 msg_outnum((long)argvars[i].vval.v_number);
3065 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003066 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003067 // Do not want errors such as E724 here.
3068 ++emsg_off;
3069 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3070 --emsg_off;
3071 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003072 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003073 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003074 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003075 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3076 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003077 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003078 msg_puts((char *)s);
3079 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003080 }
3081 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003082 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003083 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003084 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003085 msg_puts("\n"); // don't overwrite this either
3086
3087 verbose_leave_scroll();
3088 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003089 }
3090#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003091 if (do_profiling == PROF_YES)
3092 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003093 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003094#endif
3095
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003096 // "legacy" does not apply to commands in the function
3097 sticky_cmdmod_flags = 0;
3098
Bram Moolenaar98aff652022-09-06 21:02:35 +01003099 // If called from a compiled :def function the execution context must be
3100 // hidden, any deferred functions need to be added to the function being
3101 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003102 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003103
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003104 save_current_sctx = current_sctx;
3105 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003106 save_did_emsg = did_emsg;
3107 did_emsg = FALSE;
3108
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003109 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003110 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003111 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003112 retval = FCERR_FAILED;
3113 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003114 else if (islambda)
3115 {
3116 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3117
3118 // A Lambda always has the command "return {expr}". It is much faster
3119 // to evaluate {expr} directly.
3120 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003121 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003122 --ex_nesting_level;
3123 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003124 else
3125 // call do_cmdline() to execute the lines
3126 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003127 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3128
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003129 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003130 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003131
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003132 if (RedrawingDisabled > 0)
3133 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003134
Bram Moolenaare38eab22019-12-05 21:50:01 +01003135 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003136 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3137 {
3138 clear_tv(rettv);
3139 rettv->v_type = VAR_NUMBER;
3140 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003141
3142 // In corner cases returning a "failed" value is not backwards
3143 // compatible. Only do this for Vim9 script.
3144 if (in_vim9script())
3145 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003146 }
3147
3148#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003149 if (do_profiling == PROF_YES)
3150 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003151 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003152
3153 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3154 profile_may_end_func(&profile_info, fp, caller);
3155 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003156#endif
3157
Bram Moolenaare38eab22019-12-05 21:50:01 +01003158 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003159 if (p_verbose >= 12)
3160 {
3161 ++no_wait_return;
3162 verbose_enter_scroll();
3163
3164 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003165 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003166 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003167 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003168 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003169 else
3170 {
3171 char_u buf[MSG_BUF_LEN];
3172 char_u numbuf2[NUMBUFLEN];
3173 char_u *tofree;
3174 char_u *s;
3175
Bram Moolenaare38eab22019-12-05 21:50:01 +01003176 // The value may be very long. Skip the middle part, so that we
3177 // have some idea how it starts and ends. smsg() would always
3178 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003179 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003180 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003181 --emsg_off;
3182 if (s != NULL)
3183 {
3184 if (vim_strsize(s) > MSG_BUF_CLEN)
3185 {
3186 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3187 s = buf;
3188 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003189 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003190 vim_free(tofree);
3191 }
3192 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003193 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003194
3195 verbose_leave_scroll();
3196 --no_wait_return;
3197 }
3198
ichizok7e5fe382023-04-15 13:17:50 +01003199 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003200 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003201 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003202 restore_current_ectx(save_current_ectx);
3203
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003204#ifdef FEAT_PROFILE
3205 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003206 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003207#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003208 if (using_sandbox)
3209 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003210 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003211
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003212 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003213 {
3214 ++no_wait_return;
3215 verbose_enter_scroll();
3216
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003217 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003218 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003219
3220 verbose_leave_scroll();
3221 --no_wait_return;
3222 }
3223
3224 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003225 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003226 for (i = 0; i < tv_to_free_len; ++i)
3227 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003228 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003229
3230 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003231}
3232
3233/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003234 * Check the argument count for user function "fp".
3235 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3236 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003237 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003238check_user_func_argcount(ufunc_T *fp, int argcount)
3239{
3240 int regular_args = fp->uf_args.ga_len;
3241
3242 if (argcount < regular_args - fp->uf_def_args.ga_len)
3243 return FCERR_TOOFEW;
3244 else if (!has_varargs(fp) && argcount > regular_args)
3245 return FCERR_TOOMANY;
3246 return FCERR_UNKNOWN;
3247}
3248
3249/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003251 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003252 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253call_user_func_check(
3254 ufunc_T *fp,
3255 int argcount,
3256 typval_T *argvars,
3257 typval_T *rettv,
3258 funcexe_T *funcexe,
3259 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003260{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003261 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003262
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003263#ifdef FEAT_LUA
3264 if (fp->uf_flags & FC_CFUNC)
3265 {
3266 cfunc_T cb = fp->uf_cb;
3267
3268 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3269 }
3270#endif
3271
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003272 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3273 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003274 error = check_user_func_argcount(fp, argcount);
3275 if (error != FCERR_UNKNOWN)
3276 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003277
Bram Moolenaar50824712020-12-20 21:10:17 +01003278 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003279 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003281 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003283 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 int did_save_redo = FALSE;
3285 save_redo_T save_redo;
3286
3287 /*
3288 * Call the user function.
3289 * Save and restore search patterns, script variables and
3290 * redo buffer.
3291 */
3292 save_search_patterns();
3293 if (!ins_compl_active())
3294 {
3295 saveRedobuff(&save_redo);
3296 did_save_redo = TRUE;
3297 }
3298 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003299 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003300 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3302 // Function was unreferenced while being used, free it now.
3303 func_clear_free(fp, FALSE);
3304 if (did_save_redo)
3305 restoreRedobuff(&save_redo);
3306 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003307 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003308
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003310}
3311
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003312static funccal_entry_T *funccal_stack = NULL;
3313
3314/*
3315 * Save the current function call pointer, and set it to NULL.
3316 * Used when executing autocommands and for ":source".
3317 */
3318 void
3319save_funccal(funccal_entry_T *entry)
3320{
3321 entry->top_funccal = current_funccal;
3322 entry->next = funccal_stack;
3323 funccal_stack = entry;
3324 current_funccal = NULL;
3325}
3326
3327 void
3328restore_funccal(void)
3329{
3330 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003331 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003332 else
3333 {
3334 current_funccal = funccal_stack->top_funccal;
3335 funccal_stack = funccal_stack->next;
3336 }
3337}
3338
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003339 funccall_T *
3340get_current_funccal(void)
3341{
3342 return current_funccal;
3343}
3344
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003345/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003346 * Return TRUE when currently at the script level:
3347 * - not in a function
3348 * - not executing an autocommand
3349 * Note that when an autocommand sources a script the result is FALSE;
3350 */
3351 int
3352at_script_level(void)
3353{
3354 return current_funccal == NULL && autocmd_match == NULL;
3355}
3356
3357/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003358 * Mark all functions of script "sid" as deleted.
3359 */
3360 void
3361delete_script_functions(int sid)
3362{
3363 hashitem_T *hi;
3364 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003365 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003366 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003367 size_t len;
3368
3369 buf[0] = K_SPECIAL;
3370 buf[1] = KS_EXTRA;
3371 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003372 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003373 len = STRLEN(buf);
3374
Bram Moolenaarce658352020-07-31 23:47:12 +02003375 while (todo > 0)
3376 {
3377 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003378 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003379 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003380 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003381 fp = HI2UF(hi);
3382 if (STRNCMP(fp->uf_name, buf, len) == 0)
3383 {
3384 int changed = func_hashtab.ht_changed;
3385
3386 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003387
3388 if (fp->uf_calls > 0)
3389 {
3390 // Function is executing, don't free it but do remove
3391 // it from the hashtable.
3392 if (func_remove(fp))
3393 fp->uf_refcount--;
3394 }
3395 else
3396 {
3397 func_clear(fp, TRUE);
3398 // When clearing a function another function can be
3399 // cleared as a side effect. When that happens start
3400 // over.
3401 if (changed != func_hashtab.ht_changed)
3402 break;
3403 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003404 }
3405 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003406 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003407 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003408}
3409
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003410#if defined(EXITFREE) || defined(PROTO)
3411 void
3412free_all_functions(void)
3413{
3414 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003415 ufunc_T *fp;
3416 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003417 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003418 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003419
Bram Moolenaare38eab22019-12-05 21:50:01 +01003420 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003421 while (current_funccal != NULL)
3422 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003423 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003424 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003425 if (current_funccal == NULL && funccal_stack != NULL)
3426 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003427 }
3428
Bram Moolenaare38eab22019-12-05 21:50:01 +01003429 // First clear what the functions contain. Since this may lower the
3430 // reference count of a function, it may also free a function and change
3431 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003432 while (todo > 0)
3433 {
3434 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003435 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003436 if (!HASHITEM_EMPTY(hi))
3437 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438 // clear the def function index now
3439 fp = HI2UF(hi);
3440 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003441 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442
Bram Moolenaare38eab22019-12-05 21:50:01 +01003443 // Only free functions that are not refcounted, those are
3444 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003445 if (func_name_refcount(fp->uf_name))
3446 ++skipped;
3447 else
3448 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003449 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003450 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003451 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003452 {
3453 skipped = 0;
3454 break;
3455 }
3456 }
3457 --todo;
3458 }
3459 }
3460
Bram Moolenaare38eab22019-12-05 21:50:01 +01003461 // Now actually free the functions. Need to start all over every time,
3462 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003463 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003464 while (func_hashtab.ht_used > skipped)
3465 {
3466 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003467 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003468 if (!HASHITEM_EMPTY(hi))
3469 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003470 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003471 // Only free functions that are not refcounted, those are
3472 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003473 fp = HI2UF(hi);
3474 if (func_name_refcount(fp->uf_name))
3475 ++skipped;
3476 else
3477 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003478 if (func_free(fp, FALSE) == OK)
3479 {
3480 skipped = 0;
3481 break;
3482 }
3483 // did not actually free it
3484 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003485 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003486 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003487 }
3488 if (skipped == 0)
3489 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490
3491 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003492}
3493#endif
3494
3495/*
3496 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003497 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3498 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003499 * "len" is the length of "name", or -1 for NUL terminated.
3500 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003502builtin_function(char_u *name, int len)
3503{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003504 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003505
Bram Moolenaara26b9702020-04-18 19:53:28 +02003506 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003507 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003508 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3509 {
3510 if (name[i] == AUTOLOAD_CHAR)
3511 return FALSE;
3512 if (!eval_isnamec(name[i]))
3513 {
3514 // "name.something" is not a builtin function
3515 if (name[i] == '.')
3516 return FALSE;
3517 break;
3518 }
3519 }
3520 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003521}
3522
3523 int
3524func_call(
3525 char_u *name,
3526 typval_T *args,
3527 partial_T *partial,
3528 dict_T *selfdict,
3529 typval_T *rettv)
3530{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003531 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003532 listitem_T *item;
3533 typval_T argv[MAX_FUNC_ARGS + 1];
3534 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003535 int r = 0;
3536
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003537 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003538 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003539 {
3540 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3541 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003542 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003543 break;
3544 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003545 // Make a copy of each argument. This is needed to be able to set
3546 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003547 copy_tv(&item->li_tv, &argv[argc++]);
3548 }
3549
3550 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003551 {
3552 funcexe_T funcexe;
3553
Bram Moolenaara80faa82020-04-12 19:37:17 +02003554 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003555 funcexe.fe_firstline = curwin->w_cursor.lnum;
3556 funcexe.fe_lastline = curwin->w_cursor.lnum;
3557 funcexe.fe_evaluate = TRUE;
3558 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003559 if (partial != NULL)
3560 {
3561 funcexe.fe_object = partial->pt_obj;
3562 if (funcexe.fe_object != NULL)
3563 ++funcexe.fe_object->obj_refcount;
3564 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003565 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003566 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3567 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003568
Bram Moolenaare38eab22019-12-05 21:50:01 +01003569 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003570 while (argc > 0)
3571 clear_tv(&argv[--argc]);
3572
3573 return r;
3574}
3575
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003576static int callback_depth = 0;
3577
3578 int
3579get_callback_depth(void)
3580{
3581 return callback_depth;
3582}
3583
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003584/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003585 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003586 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003587 */
3588 int
3589call_callback(
3590 callback_T *callback,
3591 int len, // length of "name" or -1 to use strlen()
3592 typval_T *rettv, // return value goes here
3593 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003594 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003595 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003596{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003597 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003598 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003599
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003600 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3601 return FAIL;
Christian Brabandt47510f32023-10-15 09:56:16 +02003602
zeertzjqfe583b12023-12-21 16:59:26 +01003603 if (callback_depth > p_mfd)
Christian Brabandt47510f32023-10-15 09:56:16 +02003604 {
3605 emsg(_(e_command_too_recursive));
3606 return FAIL;
3607 }
3608
Bram Moolenaara80faa82020-04-12 19:37:17 +02003609 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003610 funcexe.fe_evaluate = TRUE;
3611 funcexe.fe_partial = callback->cb_partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003612 if (callback->cb_partial != NULL)
3613 {
3614 funcexe.fe_object = callback->cb_partial->pt_obj;
3615 if (funcexe.fe_object != NULL)
3616 ++funcexe.fe_object->obj_refcount;
3617 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003618 ++callback_depth;
3619 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3620 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003621
3622 // When a :def function was called that uses :try an error would be turned
3623 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003624 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003625 {
3626 need_rethrow = FALSE;
3627 handle_did_throw();
3628 }
3629
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003630 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003631}
3632
3633/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003634 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003635 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003636 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3637 */
3638 varnumber_T
3639call_callback_retnr(
3640 callback_T *callback,
3641 int argcount, // number of "argvars"
3642 typval_T *argvars) // vars for arguments, must have "argcount"
3643 // PLUS ONE elements!
3644{
3645 typval_T rettv;
3646 varnumber_T retval;
3647
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003648 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003649 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003650
3651 retval = tv_get_number_chk(&rettv, NULL);
3652 clear_tv(&rettv);
3653 return retval;
3654}
3655
3656/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 * Give an error message for the result of a function.
3658 * Nothing if "error" is FCERR_NONE.
3659 */
3660 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003661user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662{
3663 switch (error)
3664 {
3665 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003666 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003667 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003668 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003669 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 break;
3671 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003672 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 break;
3674 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003675 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 break;
3677 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003678 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 break;
3680 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003681 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 break;
3683 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003684 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 break;
3686 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003687 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3688 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003689 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003690 case FCERR_OTHER:
3691 case FCERR_FAILED:
3692 // assume the error message was already given
3693 break;
3694 case FCERR_NONE:
3695 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 }
3697}
3698
3699/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003700 * Check the argument types "argvars[argcount]" for "name" using the
3701 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3702 * is already included in "argvars[]".
3703 * Will do nothing if "funcexe->fe_check_type" is NULL or
3704 * "funcexe->fe_evaluate" is FALSE;
3705 * Returns an FCERR_ value.
3706 */
3707 static funcerror_T
3708may_check_argument_types(
3709 funcexe_T *funcexe,
3710 typval_T *argvars,
3711 int argcount,
3712 int base_included,
3713 char_u *name)
3714{
3715 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3716 {
3717 // Check that the argument types are OK for the types of the funcref.
3718 if (check_argument_types(funcexe->fe_check_type,
3719 argvars, argcount,
3720 base_included ? NULL : funcexe->fe_basetv,
3721 name) == FAIL)
3722 return FCERR_OTHER;
3723 }
3724 return FCERR_NONE;
3725}
3726
3727/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003728 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003729 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003730 * Return FAIL when the function can't be called, OK otherwise.
3731 * Also returns OK when an error was encountered while executing the function.
3732 */
3733 int
3734call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003735 char_u *funcname, // name of the function
3736 int len, // length of "name" or -1 to use strlen()
3737 typval_T *rettv, // return value goes here
3738 int argcount_in, // number of "argvars"
3739 typval_T *argvars_in, // vars for arguments, must have "argcount"
3740 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003741 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003742{
3743 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003744 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003745 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003746 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003747 char_u fname_buf[FLEN_FIXED + 1];
3748 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003749 char_u *fname = NULL;
3750 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003751 int argcount = argcount_in;
3752 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003753 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003754 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003755 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003756 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003757 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003758 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003759 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003760 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003761
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003762 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3763 // even when call_func() returns FAIL.
3764 rettv->v_type = VAR_UNKNOWN;
3765
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003766 if (partial != NULL)
3767 fp = partial->pt_func;
3768 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003769 fp = funcexe->fe_ufunc;
3770
3771 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003772 {
3773 // Make a copy of the name, if it comes from a funcref variable it
3774 // could be changed or deleted in the called function.
3775 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3776 if (name == NULL)
3777 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003778
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003779 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3780 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003781
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003782 if (funcexe->fe_doesrange != NULL)
3783 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003784
3785 if (partial != NULL)
3786 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003787 // When the function has a partial with a dict and there is a dict
3788 // argument, use the dict argument. That is backwards compatible.
3789 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003790 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003791 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003792 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003793 {
3794 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003795 {
3796 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3797 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003798 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003799 goto theend;
3800 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003801 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003802 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003803 for (i = 0; i < argcount_in; ++i)
3804 argv[i + argv_clear] = argvars_in[i];
3805 argvars = argv;
3806 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003807
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003808 if (funcexe->fe_check_type != NULL
3809 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003810 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003811 // Now funcexe->fe_check_type is missing the added arguments,
3812 // make a copy of the type with the correction.
3813 check_type = *funcexe->fe_check_type;
3814 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003815 check_type.tt_args = check_type_args;
3816 CLEAR_FIELD(check_type_args);
3817 for (i = 0; i < check_type.tt_argcount; ++i)
3818 check_type_args[i + partial->pt_argc] =
3819 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003820 check_type.tt_argcount += partial->pt_argc;
3821 check_type.tt_min_argcount += partial->pt_argc;
3822 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003823 }
3824 }
3825
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003826 if (error == FCERR_NONE)
3827 // check the argument types if possible
3828 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
3829 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003830
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003831 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003832 {
3833 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003834 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003835
Bram Moolenaar333894b2020-08-01 18:53:07 +02003836 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003837 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003838 {
3839 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003840 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003841 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003842
Bram Moolenaare38eab22019-12-05 21:50:01 +01003843 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003844 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003845 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003846
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003847 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003848 {
3849 /*
3850 * User defined function.
3851 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003852 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003853 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003854 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003855 if (fp != NULL && !is_global && in_vim9script()
3856 && func_requires_g_prefix(fp))
3857 // In Vim9 script g: is required to find a global
3858 // non-autoload function.
3859 fp = NULL;
3860 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003861
Bram Moolenaare38eab22019-12-05 21:50:01 +01003862 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003863 if (fp == NULL
3864 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003865 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003866 && !aborting())
3867 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003868 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003869 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003870 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003871 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003872 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3873 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003874 // loaded a package, 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 Moolenaara26b9702020-04-18 19:53:28 +02003877 if (fp == NULL)
3878 {
3879 char_u *p = untrans_function_name(rfname);
3880
3881 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003882 // Don't do this if the name starts with "s:".
3883 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003884 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003885 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003886
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003887 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003888 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003889 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003890 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003891 int need_arg_check = FALSE;
3892 if (funcexe->fe_check_type == NULL)
3893 {
3894 funcexe->fe_check_type = fp->uf_func_type;
3895 need_arg_check = TRUE;
3896 }
3897
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003898 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003899 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003900 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003901 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003902 argv_clear, fp);
3903 need_arg_check = TRUE;
3904 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003905
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003906 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003907 {
3908 // Method call: base->Method()
3909 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003910 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003911 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003912 argvars = argv;
3913 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003914 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003915 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003916
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003917 // Check the argument types now that the function type and all
3918 // argument values are known, if not done above.
3919 if (need_arg_check)
3920 error = may_check_argument_types(funcexe, argvars, argcount,
3921 TRUE, (name != NULL) ? name : funcname);
3922 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
3923 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003925 }
3926 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003927 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003928 {
3929 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003930 * expr->method(): Find the method name in the table, call its
3931 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003932 */
3933 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003934 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003935 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003936 else
3937 {
3938 /*
3939 * Find the function name in the table, call its implementation.
3940 */
3941 error = call_internal_func(fname, argcount, argvars, rettv);
3942 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003943
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003944 /*
3945 * The function call (or "FuncUndefined" autocommand sequence) might
3946 * have been aborted by an error, an interrupt, or an explicitly thrown
3947 * exception that has not been caught so far. This situation can be
3948 * tested for by calling aborting(). For an error in an internal
3949 * function or for the "E132" error in call_user_func(), however, the
3950 * throw point at which the "force_abort" flag (temporarily reset by
3951 * emsg()) is normally updated has not been reached yet. We need to
3952 * update that flag first to make aborting() reliable.
3953 */
3954 update_force_abort();
3955 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003956 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003957 ret = OK;
3958
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003959theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003960 /*
3961 * Report an error unless the argument evaluation or function call has been
3962 * cancelled due to an aborting error, an interrupt, or an exception.
3963 */
3964 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003965 user_func_error(error, (name != NULL) ? name : funcname,
3966 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003967
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003968 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003969 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003970 clear_tv(&argv[--argv_clear + argv_base]);
3971
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003972 vim_free(tofree);
3973 vim_free(name);
3974
3975 return ret;
3976}
3977
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003978/*
3979 * Call a function without arguments, partial or dict.
3980 * This is like call_func() when the call is only "FuncName()".
3981 * To be used by "expr" options.
3982 * Returns NOTDONE when the function could not be found.
3983 */
3984 int
3985call_simple_func(
3986 char_u *funcname, // name of the function
3987 int len, // length of "name" or -1 to use strlen()
3988 typval_T *rettv) // return value goes here
3989{
3990 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003991 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003992 char_u fname_buf[FLEN_FIXED + 1];
3993 char_u *tofree = NULL;
3994 char_u *name;
3995 char_u *fname;
3996 char_u *rfname;
3997 int is_global = FALSE;
3998 ufunc_T *fp;
3999
4000 rettv->v_type = VAR_NUMBER; // default rettv is number zero
4001 rettv->vval.v_number = 0;
4002
4003 // Make a copy of the name, an option can be changed in the function.
4004 name = vim_strnsave(funcname, len);
4005 if (name == NULL)
4006 return ret;
4007
4008 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
4009
4010 // Skip "g:" before a function name.
4011 if (fname[0] == 'g' && fname[1] == ':')
4012 {
4013 is_global = TRUE;
4014 rfname = fname + 2;
4015 }
4016 else
4017 rfname = fname;
4018 fp = find_func(rfname, is_global);
4019 if (fp != NULL && !is_global && in_vim9script()
4020 && func_requires_g_prefix(fp))
4021 // In Vim9 script g: is required to find a global non-autoload
4022 // function.
4023 fp = NULL;
4024 if (fp == NULL)
4025 ret = NOTDONE;
4026 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
4027 error = FCERR_DELETED;
4028 else if (fp != NULL)
4029 {
4030 typval_T argvars[1];
4031 funcexe_T funcexe;
4032
4033 argvars[0].v_type = VAR_UNKNOWN;
4034 CLEAR_FIELD(funcexe);
4035 funcexe.fe_evaluate = TRUE;
4036
4037 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
4038 if (error == FCERR_NONE)
4039 ret = OK;
4040 }
4041
4042 user_func_error(error, name, FALSE);
4043 vim_free(tofree);
4044 vim_free(name);
4045
4046 return ret;
4047}
4048
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004049 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050printable_func_name(ufunc_T *fp)
4051{
4052 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4053}
4054
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004055/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004056 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4057 * TRUE. Otherwise return FALSE.
4058 */
4059 static int
4060function_list_modified(int prev_ht_changed)
4061{
4062 if (prev_ht_changed != func_hashtab.ht_changed)
4063 {
4064 emsg(_(e_function_list_was_modified));
4065 return TRUE;
4066 }
4067 return FALSE;
4068}
4069
4070/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004071 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004072 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004073 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004074list_func_head(ufunc_T *fp, int indent)
4075{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004076 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004077 int j;
4078
4079 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004080
4081 // a timer at the more prompt may have deleted the function
4082 if (function_list_modified(prev_ht_changed))
4083 return FAIL;
4084
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004085 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004086 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004087 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004088 msg_puts("def ");
4089 else
4090 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004092 msg_putchar('(');
4093 for (j = 0; j < fp->uf_args.ga_len; ++j)
4094 {
4095 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004096 msg_puts(", ");
4097 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004098 if (fp->uf_arg_types != NULL)
4099 {
4100 char *tofree;
4101
4102 msg_puts(": ");
4103 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4104 vim_free(tofree);
4105 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004106 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4107 {
4108 msg_puts(" = ");
4109 msg_puts(((char **)(fp->uf_def_args.ga_data))
4110 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4111 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004112 }
4113 if (fp->uf_varargs)
4114 {
4115 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004116 msg_puts(", ");
4117 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004118 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 if (fp->uf_va_name != NULL)
4120 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004121 if (!fp->uf_varargs)
4122 {
4123 if (j)
4124 msg_puts(", ");
4125 msg_puts("...");
4126 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004128 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 {
4130 char *tofree;
4131
4132 msg_puts(": ");
4133 msg_puts(type_name(fp->uf_va_type, &tofree));
4134 vim_free(tofree);
4135 }
4136 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004137 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004138
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004139 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004140 {
4141 if (fp->uf_ret_type != &t_void)
4142 {
4143 char *tofree;
4144
4145 msg_puts(": ");
4146 msg_puts(type_name(fp->uf_ret_type, &tofree));
4147 vim_free(tofree);
4148 }
4149 }
4150 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004151 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004152 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004153 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004154 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004155 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004156 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004157 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004158 msg_clr_eos();
4159 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004160 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004161
4162 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004163}
4164
4165/*
4166 * Get a function name, translating "<SID>" and "<SNR>".
4167 * Also handles a Funcref in a List or Dictionary.
4168 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004169 * Set "*is_global" to TRUE when the function must be global, unless
4170 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004171 * flags:
4172 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004173 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004174 * TFN_QUIET: be quiet
4175 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004176 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004177 * Advances "pp" to just after the function name (if no error).
4178 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004179 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004180trans_function_name(
4181 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004182 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004183 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004184 int flags)
4185{
4186 return trans_function_name_ext(pp, is_global, skip, flags,
4187 NULL, NULL, NULL, NULL);
4188}
4189
4190/*
4191 * trans_function_name() with extra arguments.
4192 * "fdp", "partial", "type" and "ufunc" can be NULL.
4193 */
4194 char_u *
4195trans_function_name_ext(
4196 char_u **pp,
4197 int *is_global,
4198 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004199 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004200 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004201 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004202 type_T **type, // return: type of funcref
4203 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004204{
4205 char_u *name = NULL;
4206 char_u *start;
4207 char_u *end;
4208 int lead;
4209 char_u sid_buf[20];
4210 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004212 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004213 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004214 int vim9script = in_vim9script();
4215 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004216
4217 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004218 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004219 start = *pp;
4220
Bram Moolenaare38eab22019-12-05 21:50:01 +01004221 // Check for hard coded <SNR>: already translated function ID (from a user
4222 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004223 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4224 && (*pp)[2] == (int)KE_SNR)
4225 {
4226 *pp += 3;
4227 len = get_id_len(pp) + 3;
4228 return vim_strnsave(start, len);
4229 }
4230
Bram Moolenaare38eab22019-12-05 21:50:01 +01004231 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4232 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004233 lead = eval_fname_script(start);
4234 if (lead > 2)
4235 start += lead;
4236
Bram Moolenaare38eab22019-12-05 21:50:01 +01004237 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004238 end = get_lval(start, NULL, &lv, FALSE, skip,
4239 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004240 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004241 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004242 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004243 {
4244 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004245 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004246 goto theend;
4247 }
4248 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4249 {
4250 /*
4251 * Report an invalid expression in braces, unless the expression
4252 * evaluation has been cancelled due to an aborting error, an
4253 * interrupt, or an exception.
4254 */
4255 if (!aborting())
4256 {
4257 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004258 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004259 }
4260 else
4261 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4262 goto theend;
4263 }
4264
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004265 if (lv.ll_ufunc != NULL)
4266 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004267 if (ufunc != NULL)
4268 *ufunc = lv.ll_ufunc;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004269 name = vim_strsave(lv.ll_ufunc->uf_name);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004270 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004271 goto theend;
4272 }
4273
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004274 if (lv.ll_tv != NULL)
4275 {
4276 if (fdp != NULL)
4277 {
4278 fdp->fd_dict = lv.ll_dict;
4279 fdp->fd_newkey = lv.ll_newkey;
4280 lv.ll_newkey = NULL;
4281 fdp->fd_di = lv.ll_di;
4282 }
4283 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4284 {
4285 name = vim_strsave(lv.ll_tv->vval.v_string);
4286 *pp = end;
4287 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004288 else if (lv.ll_tv->v_type == VAR_CLASS
4289 && lv.ll_tv->vval.v_class != NULL)
4290 {
4291 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4292 *pp = end;
4293 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004294 else if (lv.ll_tv->v_type == VAR_PARTIAL
4295 && lv.ll_tv->vval.v_partial != NULL)
4296 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004297 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004298 *pp = end;
4299 if (partial != NULL)
4300 *partial = lv.ll_tv->vval.v_partial;
4301 }
4302 else
4303 {
4304 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4305 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004306 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004307 else
4308 *pp = end;
4309 name = NULL;
4310 }
4311 goto theend;
4312 }
4313
4314 if (lv.ll_name == NULL)
4315 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004316 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004317 *pp = end;
4318 goto theend;
4319 }
4320
Bram Moolenaare38eab22019-12-05 21:50:01 +01004321 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004322 if (lv.ll_exp_name != NULL)
4323 {
4324 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004325 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004326 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004327 if (name == lv.ll_exp_name)
4328 name = NULL;
4329 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004330 else if (lv.ll_sid > 0)
4331 {
4332 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4333 int cc = *lv.ll_name_end;
4334
4335 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4336 *lv.ll_name_end = NUL;
4337 if (si->sn_autoload_prefix != NULL)
4338 {
4339 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4340 }
4341 else
4342 {
4343 sid_buf[0] = K_SPECIAL;
4344 sid_buf[1] = KS_EXTRA;
4345 sid_buf[2] = (int)KE_SNR;
4346 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004347 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004348 name = concat_str(sid_buf, lv.ll_name);
4349 }
4350 *lv.ll_name_end = cc;
4351 *pp = end;
4352 goto theend;
4353 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004354 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004355 {
4356 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004357 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004358 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004359 if (name == *pp)
4360 name = NULL;
4361 }
4362 if (name != NULL)
4363 {
4364 name = vim_strsave(name);
4365 *pp = end;
4366 if (STRNCMP(name, "<SNR>", 5) == 0)
4367 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004368 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004369 name[0] = K_SPECIAL;
4370 name[1] = KS_EXTRA;
4371 name[2] = (int)KE_SNR;
4372 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4373 }
4374 goto theend;
4375 }
4376
4377 if (lv.ll_exp_name != NULL)
4378 {
4379 len = (int)STRLEN(lv.ll_exp_name);
4380 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4381 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4382 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004383 // When there was "s:" already or the name expanded to get a
4384 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004385 lv.ll_name += 2;
4386 len -= 2;
4387 lead = 2;
4388 }
4389 }
4390 else
4391 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004392 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004393 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004394 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004395 if (lv.ll_name[0] == 'g')
4396 {
4397 if (is_global != NULL)
4398 {
4399 *is_global = TRUE;
4400 }
4401 else
4402 {
4403 // dropping "g:" without setting "is_global" won't work in
4404 // Vim9script, put it back later
4405 prefix_g = TRUE;
4406 extra = 2;
4407 }
4408 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004409 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004410 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004411 len = (int)(end - lv.ll_name);
4412 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004413 if (len <= 0)
4414 {
4415 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004416 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004417 goto theend;
4418 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004419
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004420 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004421 // starts with a lower case character: dict.func(). Or when in a class.
4422 vim9_local = ASCII_ISUPPER(*start) && vim9script
4423 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004425 /*
4426 * Copy the function name to allocated memory.
4427 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4428 * Accept <SNR>123_name() outside a script.
4429 */
4430 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004431 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004432 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004433 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004434 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004435 {
Kota Kato948a3892022-08-16 16:09:59 +01004436 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4437 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004438 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004439 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004440 goto theend;
4441 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004443 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004444 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004446 || eval_fname_sid(*pp))
4447 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004449 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004450 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004451 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004452 goto theend;
4453 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004454 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004455 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456 extra = 3 + (int)STRLEN(sid_buf);
4457 else
4458 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004459 }
4460 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004461 // The function name must start with an upper case letter (unless it is a
4462 // Vim9 class new() function or a Vim9 class private method)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004463 else if (!(flags & TFN_INT)
4464 && (builtin_function(lv.ll_name, len)
4465 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004466 && !((flags & TFN_IN_CLASS)
4467 && (STRNCMP(lv.ll_name, "new", 3) == 0
4468 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004469 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004470 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004471 : e_function_name_must_start_with_capital_or_s_str),
4472 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004473 goto theend;
4474 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004475 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004476 {
4477 char_u *cp = vim_strchr(lv.ll_name, ':');
4478
4479 if (cp != NULL && cp < end)
4480 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004481 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004482 goto theend;
4483 }
4484 }
4485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004487 if (name != NULL)
4488 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004489 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004490 {
4491 name[0] = K_SPECIAL;
4492 name[1] = KS_EXTRA;
4493 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004494 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004495 STRCPY(name + 3, sid_buf);
4496 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004497 else if (prefix_g)
4498 {
4499 name[0] = 'g';
4500 name[1] = ':';
4501 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004502 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4503 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004504 }
4505 *pp = end;
4506
4507theend:
4508 clear_lval(&lv);
4509 return name;
4510}
4511
4512/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004513 * Assuming "name" is the result of trans_function_name() and it was prefixed
4514 * to use the script-local name, return the unmodified name (points into
4515 * "name"). Otherwise return NULL.
4516 * This can be used to first search for a script-local function and fall back
4517 * to the global function if not found.
4518 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004519 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004520untrans_function_name(char_u *name)
4521{
4522 char_u *p;
4523
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004524 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004525 {
4526 p = vim_strchr(name, '_');
4527 if (p != NULL)
4528 return p + 1;
4529 }
4530 return NULL;
4531}
4532
4533/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004534 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4535 * current script ID and returns the expanded function name. The caller should
4536 * free the returned name. If not called from a script context or the function
4537 * name doesn't start with these prefixes, then returns NULL.
4538 * This doesn't check whether the script-local function exists or not.
4539 */
4540 char_u *
4541get_scriptlocal_funcname(char_u *funcname)
4542{
4543 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004544 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004545 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004546 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004547
4548 if (funcname == NULL)
4549 return NULL;
4550
4551 if (STRNCMP(funcname, "s:", 2) != 0
4552 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004553 {
4554 ufunc_T *ufunc;
4555
4556 // The function name does not have a script-local prefix. Try finding
4557 // it when in a Vim9 script and there is no "g:" prefix.
4558 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4559 return NULL;
4560 ufunc = find_func(funcname, FALSE);
4561 if (ufunc == NULL || func_is_global(ufunc)
4562 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4563 return NULL;
4564 ++p;
4565 off = 0;
4566 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004567 else
4568 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004569
4570 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4571 {
4572 emsg(_(e_using_sid_not_in_script_context));
4573 return NULL;
4574 }
4575 // Expand s: prefix into <SNR>nr_<name>
4576 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4577 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004578 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004579 if (newname == NULL)
4580 return NULL;
4581 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004582 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004583
4584 return newname;
4585}
4586
4587/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004588 * Return script-local "fname" with the 3-byte sequence replaced by
4589 * printable <SNR> in allocated memory.
4590 */
4591 char_u *
4592alloc_printable_func_name(char_u *fname)
4593{
4594 char_u *n = alloc(STRLEN(fname + 3) + 6);
4595
4596 if (n != NULL)
4597 {
4598 STRCPY(n, "<SNR>");
4599 STRCPY(n + 5, fname + 3);
4600 }
4601 return n;
4602}
4603
4604/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004605 * Call trans_function_name(), except that a lambda is returned as-is.
4606 * Returns the name in allocated memory.
4607 */
4608 char_u *
4609save_function_name(
4610 char_u **name,
4611 int *is_global,
4612 int skip,
4613 int flags,
4614 funcdict_T *fudi)
4615{
4616 char_u *p = *name;
4617 char_u *saved;
4618
4619 if (STRNCMP(p, "<lambda>", 8) == 0)
4620 {
4621 p += 8;
4622 (void)getdigits(&p);
4623 saved = vim_strnsave(*name, p - *name);
4624 if (fudi != NULL)
4625 CLEAR_POINTER(fudi);
4626 }
4627 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004628 saved = trans_function_name_ext(&p, is_global, skip,
4629 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004630 *name = p;
4631 return saved;
4632}
4633
4634/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004635 * List functions. When "regmatch" is NULL all of then.
4636 * Otherwise functions matching "regmatch".
4637 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004638 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004639list_functions(regmatch_T *regmatch)
4640{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004641 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004642 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004643 hashitem_T *hi;
4644
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004645 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004646 {
4647 if (!HASHITEM_EMPTY(hi))
4648 {
4649 ufunc_T *fp = HI2UF(hi);
4650
4651 --todo;
4652 if ((fp->uf_flags & FC_DEAD) == 0
4653 && (regmatch == NULL
4654 ? !message_filtered(fp->uf_name)
4655 && !func_name_refcount(fp->uf_name)
4656 : !isdigit(*fp->uf_name)
4657 && vim_regexec(regmatch, fp->uf_name, 0)))
4658 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004659 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004660 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004661 if (function_list_modified(prev_ht_changed))
4662 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004663 }
4664 }
4665 }
4666}
4667
4668/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004669 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004670 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4671 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004672 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004673 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4674 * With CF_INTERFACE the function is define inside an interface, only the
4675 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004676 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004677 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004678 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004679define_function(
4680 exarg_T *eap,
4681 char_u *name_arg,
4682 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004683 int class_flags,
4684 ocmember_T *obj_members,
4685 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004686{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004687 int j;
4688 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004689 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004690 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004691 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004692 char_u *p;
4693 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004694 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004695 char_u *line_arg = NULL;
4696 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004698 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004699 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004700 garray_T newlines;
4701 int varargs = FALSE;
4702 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004703 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004704 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004705 int fp_allocated = FALSE;
4706 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004707 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004708 dictitem_T *v;
4709 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004710 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004711 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004712 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004713 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004714 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004715 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004716
4717 /*
4718 * ":function" without argument: list functions.
4719 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004720 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004721 {
4722 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004723 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004724 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004725 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004726 }
4727
4728 /*
4729 * ":function /pat": list functions matching pattern.
4730 */
4731 if (*eap->arg == '/')
4732 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004733 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004734 if (!eap->skip)
4735 {
4736 regmatch_T regmatch;
4737
4738 c = *p;
4739 *p = NUL;
4740 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4741 *p = c;
4742 if (regmatch.regprog != NULL)
4743 {
4744 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004745 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004746 vim_regfree(regmatch.regprog);
4747 }
4748 }
4749 if (*p == '/')
4750 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004751 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004752 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004753 }
4754
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004755 ga_init(&newargs);
4756 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004757 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004758 ga_init(&default_args);
4759
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004760 /*
4761 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004762 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004763 * "name" == func, "fudi.fd_dict" == NULL
4764 * dict.func new dictionary entry
4765 * "name" == NULL, "fudi.fd_dict" set,
4766 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4767 * dict.func existing dict entry with a Funcref
4768 * "name" == func, "fudi.fd_dict" set,
4769 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4770 * dict.func existing dict entry that's not a Funcref
4771 * "name" == NULL, "fudi.fd_dict" set,
4772 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4773 * s:func script-local function name
4774 * g:func global function name, same as "func"
4775 */
4776 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004777 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004778 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004779 // nested function, argument is (args).
4780 paren = TRUE;
4781 CLEAR_FIELD(fudi);
4782 }
4783 else
4784 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004785 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004786 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004787 if (p[0] == 's' && p[1] == ':')
4788 {
4789 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4790 return NULL;
4791 }
4792 p = to_name_end(p, TRUE);
4793 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4794 {
4795 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4796 eap->arg);
4797 return NULL;
4798 }
4799 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004800 }
4801
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004802 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004803 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004804 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004805 paren = (vim_strchr(p, '(') != NULL);
4806 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004807 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004808 /*
4809 * Return on an invalid expression in braces, unless the expression
4810 * evaluation has been cancelled due to an aborting error, an
4811 * interrupt, or an exception.
4812 */
4813 if (!aborting())
4814 {
4815 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004816 semsg(_(e_key_not_present_in_dictionary_str),
4817 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004818 vim_free(fudi.fd_newkey);
4819 return NULL;
4820 }
4821 else
4822 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004823 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004824
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004825 // For "export def FuncName()" in an autoload script the function name
4826 // is stored with the legacy autoload name "dir#script#FuncName" so
4827 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004828 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004829 {
4830 char_u *prefixed = may_prefix_autoload(name);
4831
4832 if (prefixed != NULL && prefixed != name)
4833 {
4834 vim_free(name);
4835 name = prefixed;
4836 }
4837 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004838 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004839 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004840 {
4841 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4842 goto ret_free;
4843 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004844 }
4845
Bram Moolenaare38eab22019-12-05 21:50:01 +01004846 // An error in a function call during evaluation of an expression in magic
4847 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004848 saved_did_emsg = did_emsg;
4849 did_emsg = FALSE;
4850
4851 /*
4852 * ":function func" with only function name: list function.
4853 */
4854 if (!paren)
4855 {
4856 if (!ends_excmd(*skipwhite(p)))
4857 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004858 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004859 goto ret_free;
4860 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004861 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004862 if (eap->nextcmd != NULL)
4863 *p = NUL;
4864 if (!eap->skip && !got_int)
4865 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004866 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004867 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4868 {
4869 char_u *up = untrans_function_name(name);
4870
4871 // With Vim9 script the name was made script-local, if not
4872 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004873 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004874 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004875 }
4876
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004877 if (fp != NULL)
4878 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004879 // Check no function was added or removed from a timer, e.g. at
4880 // the more prompt. "fp" may then be invalid.
4881 int prev_ht_changed = func_hashtab.ht_changed;
4882
4883 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004884 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004885 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4886 {
4887 if (FUNCLINE(fp, j) == NULL)
4888 continue;
4889 msg_putchar('\n');
4890 msg_outnum((long)(j + 1));
4891 if (j < 9)
4892 msg_putchar(' ');
4893 if (j < 99)
4894 msg_putchar(' ');
4895 if (function_list_modified(prev_ht_changed))
4896 break;
4897 msg_prt_line(FUNCLINE(fp, j), FALSE);
4898 out_flush(); // show a line at a time
4899 ui_breakcheck();
4900 }
4901 if (!got_int)
4902 {
4903 msg_putchar('\n');
4904 if (!function_list_modified(prev_ht_changed))
4905 {
4906 if (fp->uf_def_status != UF_NOT_COMPILED)
4907 msg_puts(" enddef");
4908 else
4909 msg_puts(" endfunction");
4910 }
4911 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004912 }
4913 }
4914 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004915 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004916 }
4917 goto ret_free;
4918 }
4919
4920 /*
4921 * ":function name(arg1, arg2)" Define function.
4922 */
4923 p = skipwhite(p);
4924 if (*p != '(')
4925 {
4926 if (!eap->skip)
4927 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004928 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004929 goto ret_free;
4930 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004931 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004932 if (vim_strchr(p, '(') != NULL)
4933 p = vim_strchr(p, '(');
4934 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004935
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004936 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4937 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004938 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004939 goto ret_free;
4940 }
4941
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004942 // In Vim9 script only global functions can be redefined.
4943 if (vim9script && eap->forceit && !is_global)
4944 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004945 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004946 goto ret_free;
4947 }
4948
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004949 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004950
Bram Moolenaar04b12692020-05-04 23:24:44 +02004951 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004952 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004953 // Check the name of the function. Unless it's a dictionary function
4954 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004955 if (name != NULL)
4956 arg = name;
4957 else
4958 arg = fudi.fd_newkey;
4959 if (arg != NULL && (fudi.fd_di == NULL
4960 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4961 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4962 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004963 char_u *name_base = arg;
4964 int i;
4965
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004966 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004967 {
4968 name_base = vim_strchr(arg, '_');
4969 if (name_base == NULL)
4970 name_base = arg + 3;
4971 else
4972 ++name_base;
4973 }
4974 for (i = 0; name_base[i] != NUL && (i == 0
4975 ? eval_isnamec1(name_base[i])
4976 : eval_isnamec(name_base[i])); ++i)
4977 ;
4978 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004979 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004980
4981 // In Vim9 script a function cannot have the same name as a
4982 // variable.
4983 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004984 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4985 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004986 + EVAL_VAR_NO_FUNC) == OK)
4987 {
4988 semsg(_(e_redefining_script_item_str), name_base);
4989 goto ret_free;
4990 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004991 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004992 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004993 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004994 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004995 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004996 goto ret_free;
4997 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004998 }
4999
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005000 // This may get more lines and make the pointers into the first line
5001 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01005002 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005003 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00005004 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02005005 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005006 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00005007 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005008 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01005009 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005012 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005014 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005015 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005016 if (*p != ':')
5017 {
5018 semsg(_(e_no_white_space_allowed_before_colon_str), p);
5019 p = skipwhite(p);
5020 }
5021 else if (!IS_WHITE_OR_NUL(p[1]))
5022 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005023 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02005024 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005025 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005026 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005027 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01005028 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005029 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005030 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005031 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005032 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005033 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02005034 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005035 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005036 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02005037 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005038 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005039 else
5040 // find extra arguments "range", "dict", "abort" and "closure"
5041 for (;;)
5042 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01005043 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005044 p = skipwhite(p);
5045 if (STRNCMP(p, "range", 5) == 0)
5046 {
5047 flags |= FC_RANGE;
5048 p += 5;
5049 }
5050 else if (STRNCMP(p, "dict", 4) == 0)
5051 {
5052 flags |= FC_DICT;
5053 p += 4;
5054 }
5055 else if (STRNCMP(p, "abort", 5) == 0)
5056 {
5057 flags |= FC_ABORT;
5058 p += 5;
5059 }
5060 else if (STRNCMP(p, "closure", 7) == 0)
5061 {
5062 flags |= FC_CLOSURE;
5063 p += 7;
5064 if (current_funccal == NULL)
5065 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005066 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005067 name == NULL ? (char_u *)"" : name);
5068 goto erret;
5069 }
5070 }
5071 else
5072 break;
5073 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005074
Bram Moolenaare38eab22019-12-05 21:50:01 +01005075 // When there is a line break use what follows for the function body.
5076 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005077 if (*p == '\n')
5078 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005079 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005080 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5081 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005082 && !(VIM_ISWHITE(*whitep) && *p == '#'
5083 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005084 && !eap->skip
5085 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005086 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005087
5088 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005089 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5090 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005091 */
5092 if (KeyTyped)
5093 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005094 // Check if the function already exists, don't let the user type the
5095 // whole function before telling him it doesn't work! For a script we
5096 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005097 if (!eap->skip && !eap->forceit)
5098 {
5099 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005100 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005101 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005102 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005103 }
5104
5105 if (!eap->skip && did_emsg)
5106 goto erret;
5107
Bram Moolenaare38eab22019-12-05 21:50:01 +01005108 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005109 cmdline_row = msg_row;
5110 }
5111
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005112 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005113 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005114
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005115 // Do not define the function when getting the body fails and when
5116 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005117 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005118 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005119 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5120 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005121 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005122 goto erret;
5123
5124 /*
5125 * If there are no errors, add the function
5126 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005127 if (fudi.fd_dict != NULL)
5128 {
5129 char numbuf[20];
5130
5131 fp = NULL;
5132 if (fudi.fd_newkey == NULL && !eap->forceit)
5133 {
5134 emsg(_(e_dictionary_entry_already_exists));
5135 goto erret;
5136 }
5137 if (fudi.fd_di == NULL)
5138 {
5139 // Can't add a function to a locked dictionary
5140 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5141 goto erret;
5142 }
5143 // Can't change an existing function if it is locked
5144 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5145 goto erret;
5146
5147 // Give the function a sequential number. Can only be used with a
5148 // Funcref!
5149 vim_free(name);
5150 sprintf(numbuf, "%d", ++func_nr);
5151 name = vim_strsave((char_u *)numbuf);
5152 if (name == NULL)
5153 goto erret;
5154 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005155 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005156 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005157 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005158 char_u *find_name = name;
5159 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005160 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005161
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005162 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005163 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005164 var_conflict = TRUE;
5165
5166 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5167 {
5168 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5169
5170 if (si->sn_autoload_prefix != NULL)
5171 {
5172 if (is_export)
5173 {
5174 find_name = name + STRLEN(si->sn_autoload_prefix);
5175 v = find_var(find_name, &ht, TRUE);
5176 if (v != NULL)
5177 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005178 // Only check if the function already exists in the script,
5179 // global functions can be shadowed.
5180 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005181 }
5182 else
5183 {
5184 char_u *prefixed = may_prefix_autoload(name);
5185
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005186 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005187 {
5188 v = find_var(prefixed, &ht, TRUE);
5189 if (v != NULL)
5190 var_conflict = TRUE;
5191 vim_free(prefixed);
5192 }
5193 }
5194 }
5195 }
5196 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005197 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005198 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005199 goto erret;
5200 }
5201
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005202 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005203 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005204 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005205 char_u *uname = untrans_function_name(name);
5206
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005207 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005208 }
5209
5210 if (fp != NULL || import != NULL)
5211 {
5212 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005213
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005214 // Function can be replaced with "function!" and when sourcing the
5215 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005216 // A name that is used by an import can not be overruled.
5217 if (import != NULL
5218 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005219 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005220 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005221 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005222 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005223 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005224 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005225 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005226 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005227 goto erret;
5228 }
5229 if (fp->uf_calls > 0)
5230 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005231 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005232 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005233 goto erret;
5234 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005235 if (fp->uf_refcount > 1)
5236 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005237 // This function is referenced somewhere, don't redefine it but
5238 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005239 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005240 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005241 fp = NULL;
5242 overwrite = TRUE;
5243 }
5244 else
5245 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005246 char_u *exp_name = fp->uf_name_exp;
5247
5248 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005249 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005250 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005251 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005252 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005253 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005254#ifdef FEAT_PROFILE
5255 fp->uf_profiling = FALSE;
5256 fp->uf_prof_initialized = FALSE;
5257#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005258 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005259 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005260 }
5261 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005262
5263 if (fp == NULL)
5264 {
5265 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5266 {
5267 int slen, plen;
5268 char_u *scriptname;
5269
Bram Moolenaare38eab22019-12-05 21:50:01 +01005270 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005271 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005272 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005273 {
5274 scriptname = autoload_name(name);
5275 if (scriptname != NULL)
5276 {
5277 p = vim_strchr(scriptname, '/');
5278 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005279 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005280 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005281 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005282 j = OK;
5283 vim_free(scriptname);
5284 }
5285 }
5286 if (j == FAIL)
5287 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005288 linenr_T save_lnum = SOURCING_LNUM;
5289
5290 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005291 semsg(_(e_function_name_does_not_match_script_file_name_str),
5292 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005293 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005294 goto erret;
5295 }
5296 }
5297
Bram Moolenaare01e5212023-01-08 20:31:18 +00005298 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005299 if (fp == NULL)
5300 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005301 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005302
5303 if (fudi.fd_dict != NULL)
5304 {
5305 if (fudi.fd_di == NULL)
5306 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005307 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005308 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5309 if (fudi.fd_di == NULL)
5310 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005311 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005312 goto erret;
5313 }
5314 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5315 {
5316 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005317 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005318 goto erret;
5319 }
5320 }
5321 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005322 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005323 clear_tv(&fudi.fd_di->di_tv);
5324 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005325 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005326
Bram Moolenaare38eab22019-12-05 21:50:01 +01005327 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005328 flags |= FC_DICT;
5329 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005330 }
5331 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005332 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005333 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005334 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005335
5336 if (eap->cmdidx == CMD_def)
5337 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005338 int lnum_save = SOURCING_LNUM;
5339 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005340
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005341 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005342
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005343 // error messages are for the first function line
5344 SOURCING_LNUM = sourcing_lnum_top;
5345
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005346 // The function may use script variables from the context.
5347 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005348
h-eastb895b0f2023-09-24 15:46:31 +02005349 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5350 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005351 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005352 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005353 free_fp = fp_allocated;
5354 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005355 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005356 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005357
5358 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005359 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005360 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005361 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005362 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005363 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005364 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005365 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005366 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005367 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005368 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005369
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005370 if (fp_allocated)
5371 {
5372 // insert the new function in the function list
5373 set_ufunc_name(fp, name);
5374 if (overwrite)
5375 {
5376 hi = hash_find(&func_hashtab, name);
5377 hi->hi_key = UF2HIKEY(fp);
5378 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005379 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005380 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005381 {
5382 free_fp = TRUE;
5383 goto erret;
5384 }
5385 fp->uf_refcount = 1;
5386 }
5387
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005388 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005389 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005390 if ((flags & FC_CLOSURE) != 0)
5391 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005392 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005393 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005394 }
5395 else
5396 fp->uf_scoped = NULL;
5397
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005398#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005399 if (prof_def_func())
5400 func_do_profile(fp);
5401#endif
5402 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005403 if (sandbox)
5404 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005405 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005406 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005407 fp->uf_flags = flags;
5408 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005409 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005410 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005411 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005412 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005413 if (is_export)
5414 {
5415 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005416 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005417 is_export = FALSE;
5418 }
5419
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005420 if (eap->cmdidx == CMD_def)
5421 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005422 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5423 // :func does not use Vim9 script syntax, even in a Vim9 script file
5424 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005425
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005426 goto ret_free;
5427
5428erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005429 if (fp != NULL)
5430 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005431 // these were set to "newargs" and "default_args", which are cleared
5432 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005433 ga_init(&fp->uf_args);
5434 ga_init(&fp->uf_def_args);
5435 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005436errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005437 ga_clear_strings(&newargs);
5438 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005439 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005440 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005441 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005442 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005443 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01005444 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005445 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005446 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005447 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005448ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005449 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005450 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005451 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005452 if (name != name_arg)
5453 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005454 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005455 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005456
5457 return fp;
5458}
5459
5460/*
5461 * ":function"
5462 */
5463 void
5464ex_function(exarg_T *eap)
5465{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005466 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005467
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005468 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005469 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005470 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005471}
5472
5473/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005474 * Find a function by name, including "<lambda>123".
5475 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005476 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005477 * Return NULL if not found.
5478 */
5479 ufunc_T *
5480find_func_by_name(char_u *name, compiletype_T *compile_type)
5481{
5482 char_u *arg = name;
5483 char_u *fname;
5484 ufunc_T *ufunc;
5485 int is_global = FALSE;
5486
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005487 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5488 {
5489 *compile_type = CT_PROFILE;
5490 arg = skipwhite(arg + 7);
5491 }
5492 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5493 {
5494 *compile_type = CT_DEBUG;
5495 arg = skipwhite(arg + 5);
5496 }
5497
5498 if (STRNCMP(arg, "<lambda>", 8) == 0)
5499 {
5500 arg += 8;
5501 (void)getdigits(&arg);
5502 fname = vim_strnsave(name, arg - name);
5503 }
5504 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005505 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005506 // First try finding a method in a class, trans_function_name() will
5507 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005508 ufunc = find_class_func(&arg);
5509 if (ufunc != NULL)
5510 return ufunc;
5511
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005512 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005513 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005514 NULL, NULL, NULL, &ufunc);
5515 if (ufunc != NULL)
5516 {
5517 vim_free(fname);
5518 return ufunc;
5519 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005520 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005521 if (fname == NULL)
5522 {
5523 semsg(_(e_invalid_argument_str), name);
5524 return NULL;
5525 }
5526 if (!ends_excmd2(name, arg))
5527 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005528 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005529 emsg(ex_errmsg(e_trailing_characters_str, arg));
5530 return NULL;
5531 }
5532
5533 ufunc = find_func(fname, is_global);
5534 if (ufunc == NULL)
5535 {
5536 char_u *p = untrans_function_name(fname);
5537
5538 if (p != NULL)
5539 // Try again without making it script-local.
5540 ufunc = find_func(p, FALSE);
5541 }
5542 vim_free(fname);
5543 if (ufunc == NULL)
5544 semsg(_(e_cannot_find_function_str), name);
5545 return ufunc;
5546}
5547
5548/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005549 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005550 * be compiled or the one specified by the argument.
5551 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005552 */
5553 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005554ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005555{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005556 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005557 {
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005558 compiletype_T compile_type = CT_NONE;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005559 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005560 if (ufunc != NULL)
5561 {
5562 if (func_needs_compiling(ufunc, compile_type))
5563 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5564 else
5565 smsg(_("Function %s does not need compiling"), eap->arg);
5566 }
5567 }
5568 else
5569 {
5570 long todo = (long)func_hashtab.ht_used;
5571 int changed = func_hashtab.ht_changed;
5572 hashitem_T *hi;
5573
5574 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5575 {
5576 if (!HASHITEM_EMPTY(hi))
5577 {
5578 --todo;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005579 ufunc_T *ufunc = HI2UF(hi);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005580 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5581 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5582 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005583 {
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005584 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5585
5586 if (func_hashtab.ht_changed != changed)
5587 {
5588 // a function has been added or removed, need to start
5589 // over
5590 todo = (long)func_hashtab.ht_used;
5591 changed = func_hashtab.ht_changed;
5592 hi = func_hashtab.ht_array;
5593 --hi;
5594 }
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005595 }
5596 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005597 }
5598 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005599}
5600
5601/*
5602 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5603 * Return 2 if "p" starts with "s:".
5604 * Return 0 otherwise.
5605 */
5606 int
5607eval_fname_script(char_u *p)
5608{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005609 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5610 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005611 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5612 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5613 return 5;
5614 if (p[0] == 's' && p[1] == ':')
5615 return 2;
5616 return 0;
5617}
5618
5619 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005620translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005621{
5622 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005623 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005624 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005625}
5626
5627/*
5628 * Return TRUE when "ufunc" has old-style "..." varargs
5629 * or named varargs "...name: type".
5630 */
5631 int
5632has_varargs(ufunc_T *ufunc)
5633{
5634 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005635}
5636
5637/*
5638 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005639 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005640 */
5641 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005642function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005643{
5644 char_u *nm = name;
5645 char_u *p;
5646 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005647 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005648 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005649
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005650 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5651 if (no_deref)
5652 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005653 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005654 nm = skipwhite(nm);
5655
Bram Moolenaare38eab22019-12-05 21:50:01 +01005656 // Only accept "funcname", "funcname ", "funcname (..." and
5657 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005658 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005659 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005660 vim_free(p);
5661 return n;
5662}
5663
Bram Moolenaar113e1072019-01-20 15:30:40 +01005664#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005665 char_u *
5666get_expanded_name(char_u *name, int check)
5667{
5668 char_u *nm = name;
5669 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005670 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005671
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005672 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005673
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005674 if (p != NULL && *nm == NUL
5675 && (!check || translated_function_exists(p, is_global)))
5676 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005677
5678 vim_free(p);
5679 return NULL;
5680}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005681#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005682
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005683/*
5684 * Function given to ExpandGeneric() to obtain the list of user defined
5685 * function names.
5686 */
5687 char_u *
5688get_user_func_name(expand_T *xp, int idx)
5689{
5690 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005691 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005692 static hashitem_T *hi;
5693 ufunc_T *fp;
5694
5695 if (idx == 0)
5696 {
5697 done = 0;
5698 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005699 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005700 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005701 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005702 {
5703 if (done++ > 0)
5704 ++hi;
5705 while (HASHITEM_EMPTY(hi))
5706 ++hi;
5707 fp = HI2UF(hi);
5708
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005709 // don't show dead, dict and lambda functions
5710 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005711 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005712 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005713
5714 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005715 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005716
5717 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005718 if (xp->xp_context != EXPAND_USER_FUNC
5719 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005720 {
5721 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005722 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005723 STRCAT(IObuff, ")");
5724 }
5725 return IObuff;
5726 }
5727 return NULL;
5728}
5729
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005730/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005731 * Make a copy of a function.
5732 * Intended to be used for a function defined on a base class that has a copy
5733 * on the child class.
5734 * The copy has uf_refcount set to one.
5735 * Returns NULL when out of memory.
5736 */
5737 ufunc_T *
5738copy_function(ufunc_T *fp)
5739{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005740 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005741 if (ufunc == NULL)
5742 return NULL;
5743
5744 // Most things can just be copied.
5745 *ufunc = *fp;
5746
5747 ufunc->uf_def_status = UF_TO_BE_COMPILED;
5748 ufunc->uf_dfunc_idx = 0;
5749 ufunc->uf_class = NULL;
5750
5751 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
5752 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
5753
5754 if (ufunc->uf_arg_types != NULL)
5755 {
5756 // "uf_arg_types" is an allocated array, make a copy.
5757 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
5758 if (at != NULL)
5759 {
5760 mch_memmove(at, ufunc->uf_arg_types,
5761 sizeof(type_T *) * ufunc->uf_args.ga_len);
5762 ufunc->uf_arg_types = at;
5763 }
5764 }
5765
5766 // TODO: how about the types themselves? they can be freed when the
5767 // original function is freed:
5768 // type_T **uf_arg_types;
5769 // type_T *uf_ret_type;
5770
Ernie Rael114ec812023-06-04 18:11:35 +01005771 // make uf_type_list empty
5772 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00005773
5774 // TODO: partial_T *uf_partial;
5775
5776 if (ufunc->uf_va_name != NULL)
5777 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
5778
5779 // TODO:
5780 // type_T *uf_va_type;
5781 // type_T *uf_func_type;
5782
5783 ufunc->uf_block_depth = 0;
5784 ufunc->uf_block_ids = NULL;
5785
5786 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
5787
5788 ufunc->uf_refcount = 1;
5789 ufunc->uf_name_exp = NULL;
5790 STRCPY(ufunc->uf_name, fp->uf_name);
5791
5792 return ufunc;
5793}
5794
5795/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005796 * ":delfunction {name}"
5797 */
5798 void
5799ex_delfunction(exarg_T *eap)
5800{
5801 ufunc_T *fp = NULL;
5802 char_u *p;
5803 char_u *name;
5804 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005805 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005806
5807 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005808 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
5809 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005810 vim_free(fudi.fd_newkey);
5811 if (name == NULL)
5812 {
5813 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005814 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005815 return;
5816 }
5817 if (!ends_excmd(*skipwhite(p)))
5818 {
5819 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005820 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005821 return;
5822 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005823 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005824 if (eap->nextcmd != NULL)
5825 *p = NUL;
5826
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005827 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005828 {
5829 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005830 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005831 vim_free(name);
5832 return;
5833 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005834 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005835 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005836 vim_free(name);
5837
5838 if (!eap->skip)
5839 {
5840 if (fp == NULL)
5841 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005842 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005843 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005844 return;
5845 }
5846 if (fp->uf_calls > 0)
5847 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005848 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005849 return;
5850 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005851 if (fp->uf_flags & FC_VIM9)
5852 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005853 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005854 return;
5855 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005856
5857 if (fudi.fd_dict != NULL)
5858 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005859 // Delete the dict item that refers to the function, it will
5860 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00005861 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005862 }
5863 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005864 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005865 // A normal function (not a numbered function or lambda) has a
5866 // refcount of 1 for the entry in the hashtable. When deleting
5867 // it and the refcount is more than one, it should be kept.
5868 // A numbered function and lambda should be kept if the refcount is
5869 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005870 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005871 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005872 // Function is still referenced somewhere. Don't free it but
5873 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005874 if (func_remove(fp))
5875 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005876 }
5877 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005878 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005879 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005880 }
5881}
5882
5883/*
5884 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005885 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005886 */
5887 void
5888func_unref(char_u *name)
5889{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005890 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005891
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005892 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005893 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005894 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005895 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005896 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005897#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005898 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005899#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005900 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005901 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005902 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005903}
5904
5905/*
5906 * Unreference a Function: decrement the reference count and free it when it
5907 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005908 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005909 */
5910 void
5911func_ptr_unref(ufunc_T *fp)
5912{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005913 if (fp != NULL && (--fp->uf_refcount <= 0
5914 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5915 && fp->uf_partial->pt_refcount <= 1
5916 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005917 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005918 // Only delete it when it's not being used. Otherwise it's done
5919 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005920 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005921 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005922 }
5923}
5924
5925/*
5926 * Count a reference to a Function.
5927 */
5928 void
5929func_ref(char_u *name)
5930{
5931 ufunc_T *fp;
5932
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005933 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005934 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005935 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005936 if (fp != NULL)
5937 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005938 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005939 // Only give an error for a numbered function.
5940 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005941 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005942}
5943
5944/*
5945 * Count a reference to a Function.
5946 */
5947 void
5948func_ptr_ref(ufunc_T *fp)
5949{
5950 if (fp != NULL)
5951 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005952}
5953
5954/*
5955 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5956 * referenced from anywhere that is in use.
5957 */
5958 static int
5959can_free_funccal(funccall_T *fc, int copyID)
5960{
Bram Moolenaarca16c602022-09-06 18:57:08 +01005961 return (fc->fc_l_varlist.lv_copyID != copyID
5962 && fc->fc_l_vars.dv_copyID != copyID
5963 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005964 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005965}
5966
5967/*
5968 * ":return [expr]"
5969 */
5970 void
5971ex_return(exarg_T *eap)
5972{
5973 char_u *arg = eap->arg;
5974 typval_T rettv;
5975 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005976 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005977
5978 if (current_funccal == NULL)
5979 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005980 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005981 return;
5982 }
5983
Bram Moolenaar844fb642021-10-23 13:32:30 +01005984 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005985 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5986
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005987 if (eap->skip)
5988 ++emsg_skip;
5989
5990 eap->nextcmd = NULL;
5991 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005992 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005993 {
5994 if (!eap->skip)
5995 returning = do_return(eap, FALSE, TRUE, &rettv);
5996 else
5997 clear_tv(&rettv);
5998 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005999 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006000 else if (!eap->skip)
6001 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006002 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01006003 update_force_abort();
6004
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006005 /*
6006 * Return unless the expression evaluation has been cancelled due to an
6007 * aborting error, an interrupt, or an exception.
6008 */
6009 if (!aborting())
6010 returning = do_return(eap, FALSE, TRUE, NULL);
6011 }
6012
Bram Moolenaare38eab22019-12-05 21:50:01 +01006013 // When skipping or the return gets pending, advance to the next command
6014 // in this line (!returning). Otherwise, ignore the rest of the line.
6015 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006016 if (returning)
6017 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006018 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02006019 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006020
6021 if (eap->skip)
6022 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02006023 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006024}
6025
zeertzjq5299c092023-04-12 20:48:16 +01006026/*
6027 * Lower level implementation of "call". Only called when not skipping.
6028 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006029 static int
6030ex_call_inner(
6031 exarg_T *eap,
6032 char_u *name,
6033 char_u **arg,
6034 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006035 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006036 evalarg_T *evalarg)
6037{
6038 linenr_T lnum;
6039 int doesrange;
6040 typval_T rettv;
6041 int failed = FALSE;
6042
zeertzjq5299c092023-04-12 20:48:16 +01006043 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006044 for ( ; lnum <= eap->line2; ++lnum)
6045 {
6046 funcexe_T funcexe;
6047
zeertzjq5299c092023-04-12 20:48:16 +01006048 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006049 {
6050 if (lnum > curbuf->b_ml.ml_line_count)
6051 {
6052 // If the function deleted lines or switched to another buffer
6053 // the line number may become invalid.
6054 emsg(_(e_invalid_range));
6055 break;
6056 }
6057 curwin->w_cursor.lnum = lnum;
6058 curwin->w_cursor.col = 0;
6059 curwin->w_cursor.coladd = 0;
6060 }
6061 *arg = startarg;
6062
6063 funcexe = *funcexe_init;
6064 funcexe.fe_doesrange = &doesrange;
6065 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6066 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6067 {
6068 failed = TRUE;
6069 break;
6070 }
6071 if (has_watchexpr())
6072 dbg_check_breakpoint(eap);
6073
6074 // Handle a function returning a Funcref, Dictionary or List.
6075 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006076 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006077 {
6078 failed = TRUE;
6079 break;
6080 }
6081
6082 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006083 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006084 break;
6085
6086 // Stop when immediately aborting on error, or when an interrupt
6087 // occurred or an exception was thrown but not caught.
6088 // get_func_tv() returned OK, so that the check for trailing
6089 // characters below is executed.
6090 if (aborting())
6091 break;
6092 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006093 return failed;
6094}
6095
6096/*
6097 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6098 * Returns FAIL or OK.
6099 */
6100 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006101ex_defer_inner(
6102 char_u *name,
6103 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006104 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006105 partial_T *partial,
6106 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006107{
6108 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006109 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006110 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006111 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006112
6113 if (current_funccal == NULL)
6114 {
6115 semsg(_(e_str_not_inside_function), "defer");
6116 return FAIL;
6117 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006118 if (partial != NULL)
6119 {
6120 if (partial->pt_dict != NULL)
6121 {
6122 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6123 return FAIL;
6124 }
6125 if (partial->pt_argc > 0)
6126 {
6127 int i;
6128
6129 partial_argc = partial->pt_argc;
6130 for (i = 0; i < partial_argc; ++i)
6131 copy_tv(&partial->pt_argv[i], &argvars[i]);
6132 }
6133 }
Ernie Raelb077b582023-12-14 20:11:44 +01006134 int is_builtin = builtin_function(name, -1);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006135 r = get_func_arguments(arg, evalarg, FALSE,
Ernie Raelb077b582023-12-14 20:11:44 +01006136 argvars + partial_argc, &argcount, is_builtin);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006137 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006138
6139 if (r == OK)
6140 {
6141 if (type != NULL)
6142 {
6143 // Check that the arguments are OK for the types of the funcref.
6144 r = check_argument_types(type, argvars, argcount, NULL, name);
6145 }
Ernie Raelb077b582023-12-14 20:11:44 +01006146 else if (is_builtin)
Bram Moolenaar16900322022-09-08 19:51:45 +01006147 {
6148 int idx = find_internal_func(name);
6149
6150 if (idx < 0)
6151 {
6152 emsg_funcname(e_unknown_function_str, name);
6153 r = FAIL;
6154 }
6155 else if (check_internal_func(idx, argcount) == -1)
6156 r = FAIL;
6157 }
6158 else
6159 {
6160 ufunc_T *ufunc = find_func(name, FALSE);
6161
6162 // we tolerate an unknown function here, it might be defined later
6163 if (ufunc != NULL)
6164 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006165 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006166 if (error != FCERR_UNKNOWN)
6167 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006168 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006169 r = FAIL;
6170 }
6171 }
6172 }
6173 }
6174
Bram Moolenaar86d87252022-09-05 21:21:25 +01006175 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006176 {
6177 while (--argcount >= 0)
6178 clear_tv(&argvars[argcount]);
6179 return FAIL;
6180 }
6181 return add_defer(name, argcount, argvars);
6182}
6183
6184/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006185 * Return TRUE if currently inside a function call.
6186 * Give an error message and return FALSE when not.
6187 */
6188 int
6189can_add_defer(void)
6190{
6191 if (!in_def_function() && get_current_funccal() == NULL)
6192 {
6193 semsg(_(e_str_not_inside_function), "defer");
6194 return FALSE;
6195 }
6196 return TRUE;
6197}
6198
6199/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006200 * Add a deferred call for "name" with arguments "argvars[argcount]".
6201 * Consumes "argvars[]".
6202 * Caller must check that in_def_function() returns TRUE or current_funccal is
6203 * not NULL.
6204 * Returns OK or FAIL.
6205 */
6206 int
6207add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6208{
6209 char_u *saved_name = vim_strsave(name);
6210 int argcount = argcount_arg;
6211 defer_T *dr;
6212 int ret = FAIL;
6213
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006214 if (saved_name == NULL)
6215 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006216 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006217 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006218 if (add_defer_function(saved_name, argcount, argvars) == OK)
6219 argcount = 0;
6220 }
6221 else
6222 {
6223 if (current_funccal->fc_defer.ga_itemsize == 0)
6224 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6225 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6226 goto theend;
6227 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6228 + current_funccal->fc_defer.ga_len++;
6229 dr->dr_name = saved_name;
6230 dr->dr_argcount = argcount;
6231 while (argcount > 0)
6232 {
6233 --argcount;
6234 dr->dr_argvars[argcount] = argvars[argcount];
6235 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006236 }
6237 ret = OK;
6238
6239theend:
6240 while (--argcount >= 0)
6241 clear_tv(&argvars[argcount]);
6242 return ret;
6243}
6244
6245/*
6246 * Invoked after a functions has finished: invoke ":defer" functions.
6247 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006248 static void
6249handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006250{
6251 int idx;
6252
Bram Moolenaar58779852022-09-06 18:31:14 +01006253 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006254 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006255 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006256
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006257 if (dr->dr_name == NULL)
6258 // already being called, can happen if function does ":qa"
6259 continue;
6260
6261 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006262 CLEAR_FIELD(funcexe);
6263 funcexe.fe_evaluate = TRUE;
6264
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006265 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006266 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006267
6268 char_u *name = dr->dr_name;
6269 dr->dr_name = NULL;
6270
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006271 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006272 // first statement in the function will be executed (because of the
6273 // exception). So save and restore the try/catch/throw exception
6274 // state.
6275 exception_state_T estate;
6276 exception_state_save(&estate);
6277 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006278
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006279 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6280
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006281 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006282
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006283 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006284 vim_free(name);
6285 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006286 clear_tv(&dr->dr_argvars[i]);
6287 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006288 ga_clear(&funccal->fc_defer);
6289}
6290
zeertzjq960cf912023-04-18 21:52:54 +01006291 static void
6292invoke_funccall_defer(funccall_T *fc)
6293{
6294 if (fc->fc_ectx != NULL)
6295 {
6296 // :def function
6297 unwind_def_callstack(fc->fc_ectx);
6298 may_invoke_defer_funcs(fc->fc_ectx);
6299 }
6300 else
6301 {
6302 // legacy function
6303 handle_defer_one(fc);
6304 }
6305}
6306
Bram Moolenaar58779852022-09-06 18:31:14 +01006307/*
6308 * Called when exiting: call all defer functions.
6309 */
6310 void
6311invoke_all_defer(void)
6312{
zeertzjq1be4b812023-04-19 14:21:24 +01006313 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6314 invoke_funccall_defer(fc);
6315
zeertzjq960cf912023-04-18 21:52:54 +01006316 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6317 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6318 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006319}
6320
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006321/*
6322 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006323 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006324 */
6325 void
6326ex_call(exarg_T *eap)
6327{
6328 char_u *arg = eap->arg;
6329 char_u *startarg;
6330 char_u *name;
6331 char_u *tofree;
6332 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006333 int failed = FALSE;
6334 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006335 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006336 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006337 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006338 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006339 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006340 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006341
Bram Moolenaare6b53242020-07-01 17:28:33 +02006342 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006343 if (eap->skip)
6344 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006345 typval_T rettv;
6346
Bram Moolenaare38eab22019-12-05 21:50:01 +01006347 // trans_function_name() doesn't work well when skipping, use eval0()
6348 // instead to skip to any following command, e.g. for:
6349 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006350 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006351 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006352 clear_tv(&rettv);
6353 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006354 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006355 return;
6356 }
6357
zeertzjq5299c092023-04-12 20:48:16 +01006358 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006359 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006360 if (fudi.fd_newkey != NULL)
6361 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006362 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006363 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006364 vim_free(fudi.fd_newkey);
6365 }
6366 if (tofree == NULL)
6367 return;
6368
Bram Moolenaare38eab22019-12-05 21:50:01 +01006369 // Increase refcount on dictionary, it could get deleted when evaluating
6370 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006371 if (fudi.fd_dict != NULL)
6372 ++fudi.fd_dict->dv_refcount;
6373
Bram Moolenaare38eab22019-12-05 21:50:01 +01006374 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6375 // contents. For VAR_PARTIAL get its partial, unless we already have one
6376 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006377 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006378 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006379 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006380 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006381
Bram Moolenaare38eab22019-12-05 21:50:01 +01006382 // Skip white space to allow ":call func ()". Not good, but required for
6383 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006384 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006385 if (*startarg != '(')
6386 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006387 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006388 goto end;
6389 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006390 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006391 {
6392 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6393 goto end;
6394 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006395
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006396 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006397 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006398 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006399 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006400 }
6401 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006402 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006403 funcexe_T funcexe;
6404
Bram Moolenaara80faa82020-04-12 19:37:17 +02006405 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006406 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006407 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006408 funcexe.fe_partial = partial;
6409 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006410 funcexe.fe_firstline = eap->line1;
6411 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006412 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006413 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006414 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006415 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006416
Bram Moolenaar1d341892021-09-18 15:25:52 +02006417 // When inside :try we need to check for following "| catch" or "| endtry".
6418 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006419 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006420 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006421 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006422 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006423 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006424 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006425 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006426 {
6427 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006428 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006429 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006430 }
6431 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006432 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006433 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006434 // Must be after using "arg", it may point into memory cleared here.
6435 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006436
6437end:
6438 dict_unref(fudi.fd_dict);
6439 vim_free(tofree);
6440}
6441
6442/*
6443 * Return from a function. Possibly makes the return pending. Also called
6444 * for a pending return at the ":endtry" or after returning from an extra
6445 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6446 * when called due to a ":return" command. "rettv" may point to a typval_T
6447 * with the return rettv. Returns TRUE when the return can be carried out,
6448 * FALSE when the return gets pending.
6449 */
6450 int
6451do_return(
6452 exarg_T *eap,
6453 int reanimate,
6454 int is_cmd,
6455 void *rettv)
6456{
6457 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006458 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006459
6460 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006461 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006462 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006463
6464 /*
6465 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6466 * not in its finally clause (which then is to be executed next) is found.
6467 * In this case, make the ":return" pending for execution at the ":endtry".
6468 * Otherwise, return normally.
6469 */
6470 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6471 if (idx >= 0)
6472 {
6473 cstack->cs_pending[idx] = CSTP_RETURN;
6474
6475 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006476 // A pending return again gets pending. "rettv" points to an
6477 // allocated variable with the rettv of the original ":return"'s
6478 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006479 cstack->cs_rettv[idx] = rettv;
6480 else
6481 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006482 // When undoing a return in order to make it pending, get the stored
6483 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006484 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006485 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006486
6487 if (rettv != NULL)
6488 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006489 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006490 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6491 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6492 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006493 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006494 }
6495 else
6496 cstack->cs_rettv[idx] = NULL;
6497
6498 if (reanimate)
6499 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006500 // The pending return value could be overwritten by a ":return"
6501 // without argument in a finally clause; reset the default
6502 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006503 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6504 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006505 }
6506 }
6507 report_make_pending(CSTP_RETURN, rettv);
6508 }
6509 else
6510 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006511 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006512
Bram Moolenaare38eab22019-12-05 21:50:01 +01006513 // If the return is carried out now, store the return value. For
6514 // a return immediately after reanimation, the value is already
6515 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006516 if (!reanimate && rettv != NULL)
6517 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006518 clear_tv(current_funccal->fc_rettv);
6519 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006520 if (!is_cmd)
6521 vim_free(rettv);
6522 }
6523 }
6524
6525 return idx < 0;
6526}
6527
6528/*
6529 * Free the variable with a pending return value.
6530 */
6531 void
6532discard_pending_return(void *rettv)
6533{
6534 free_tv((typval_T *)rettv);
6535}
6536
6537/*
6538 * Generate a return command for producing the value of "rettv". The result
6539 * is an allocated string. Used by report_pending() for verbose messages.
6540 */
6541 char_u *
6542get_return_cmd(void *rettv)
6543{
6544 char_u *s = NULL;
6545 char_u *tofree = NULL;
6546 char_u numbuf[NUMBUFLEN];
6547
6548 if (rettv != NULL)
6549 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6550 if (s == NULL)
6551 s = (char_u *)"";
6552
6553 STRCPY(IObuff, ":return ");
6554 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6555 if (STRLEN(s) + 8 >= IOSIZE)
6556 STRCPY(IObuff + IOSIZE - 4, "...");
6557 vim_free(tofree);
6558 return vim_strsave(IObuff);
6559}
6560
6561/*
6562 * Get next function line.
6563 * Called by do_cmdline() to get the next line.
6564 * Returns allocated string, or NULL for end of function.
6565 */
6566 char_u *
6567get_func_line(
6568 int c UNUSED,
6569 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006570 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006571 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006572{
6573 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006574 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006575 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006576 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006577
Bram Moolenaare38eab22019-12-05 21:50:01 +01006578 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006579 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006580 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006581 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006582 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006583 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006584 }
6585#ifdef FEAT_PROFILE
6586 if (do_profiling == PROF_YES)
6587 func_line_end(cookie);
6588#endif
6589
6590 gap = &fp->uf_lines;
6591 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006592 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006593 retval = NULL;
6594 else
6595 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006596 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006597 while (fcp->fc_linenr < gap->ga_len
6598 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6599 ++fcp->fc_linenr;
6600 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006601 retval = NULL;
6602 else
6603 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006604 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6605 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006606#ifdef FEAT_PROFILE
6607 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006608 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006609#endif
6610 }
6611 }
6612
Bram Moolenaare38eab22019-12-05 21:50:01 +01006613 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006614 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006615 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006616 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006617 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006618 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006619 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006620 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006621 }
6622
6623 return retval;
6624}
6625
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006626/*
6627 * Return TRUE if the currently active function should be ended, because a
6628 * return was encountered or an error occurred. Used inside a ":while".
6629 */
6630 int
6631func_has_ended(void *cookie)
6632{
6633 funccall_T *fcp = (funccall_T *)cookie;
6634
Bram Moolenaare38eab22019-12-05 21:50:01 +01006635 // Ignore the "abort" flag if the abortion behavior has been changed due to
6636 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006637 return (((fcp->fc_func->uf_flags & FC_ABORT)
6638 && did_emsg && !aborted_in_try())
6639 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006640}
6641
6642/*
6643 * return TRUE if cookie indicates a function which "abort"s on errors.
6644 */
6645 int
6646func_has_abort(
6647 void *cookie)
6648{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006649 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006650}
6651
6652
6653/*
6654 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6655 * Don't do this when "Func" is already a partial that was bound
6656 * explicitly (pt_auto is FALSE).
6657 * Changes "rettv" in-place.
6658 * Returns the updated "selfdict_in".
6659 */
6660 dict_T *
6661make_partial(dict_T *selfdict_in, typval_T *rettv)
6662{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006663 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006664 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006665 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006666 dict_T *selfdict = selfdict_in;
6667
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006668 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6669 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006670 fp = rettv->vval.v_partial->pt_func;
6671 else
6672 {
6673 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006674 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006675 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006676 if (fname == NULL)
6677 {
6678 // There is no point binding a dict to a NULL function, just create
6679 // a function reference.
6680 rettv->v_type = VAR_FUNC;
6681 rettv->vval.v_string = NULL;
6682 }
6683 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006684 {
6685 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006686 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006687
6688 // Translate "s:func" to the stored function name.
6689 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6690 fp = find_func(fname, FALSE);
6691 vim_free(tofree);
6692 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006693 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006694
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006695 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006696 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006697 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006698
6699 if (pt != NULL)
6700 {
6701 pt->pt_refcount = 1;
6702 pt->pt_dict = selfdict;
6703 pt->pt_auto = TRUE;
6704 selfdict = NULL;
6705 if (rettv->v_type == VAR_FUNC)
6706 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006707 // Just a function: Take over the function name and use
6708 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006709 pt->pt_name = rettv->vval.v_string;
6710 }
6711 else
6712 {
6713 partial_T *ret_pt = rettv->vval.v_partial;
6714 int i;
6715
Bram Moolenaare38eab22019-12-05 21:50:01 +01006716 // Partial: copy the function name, use selfdict and copy
6717 // args. Can't take over name or args, the partial might
6718 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006719 if (ret_pt->pt_name != NULL)
6720 {
6721 pt->pt_name = vim_strsave(ret_pt->pt_name);
6722 func_ref(pt->pt_name);
6723 }
6724 else
6725 {
6726 pt->pt_func = ret_pt->pt_func;
6727 func_ptr_ref(pt->pt_func);
6728 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006729 if (ret_pt->pt_argc > 0)
6730 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006731 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006732 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006733 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006734 pt->pt_argc = 0;
6735 else
6736 {
6737 pt->pt_argc = ret_pt->pt_argc;
6738 for (i = 0; i < pt->pt_argc; i++)
6739 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6740 }
6741 }
6742 partial_unref(ret_pt);
6743 }
6744 rettv->v_type = VAR_PARTIAL;
6745 rettv->vval.v_partial = pt;
6746 }
6747 }
6748 return selfdict;
6749}
6750
6751/*
6752 * Return the name of the executed function.
6753 */
6754 char_u *
6755func_name(void *cookie)
6756{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006757 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006758}
6759
6760/*
6761 * Return the address holding the next breakpoint line for a funccall cookie.
6762 */
6763 linenr_T *
6764func_breakpoint(void *cookie)
6765{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006766 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006767}
6768
6769/*
6770 * Return the address holding the debug tick for a funccall cookie.
6771 */
6772 int *
6773func_dbg_tick(void *cookie)
6774{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006775 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006776}
6777
6778/*
6779 * Return the nesting level for a funccall cookie.
6780 */
6781 int
6782func_level(void *cookie)
6783{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006784 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006785}
6786
6787/*
6788 * Return TRUE when a function was ended by a ":return" command.
6789 */
6790 int
6791current_func_returned(void)
6792{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006793 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006794}
6795
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006796 int
6797free_unref_funccal(int copyID, int testing)
6798{
6799 int did_free = FALSE;
6800 int did_free_funccal = FALSE;
6801 funccall_T *fc, **pfc;
6802
6803 for (pfc = &previous_funccal; *pfc != NULL; )
6804 {
6805 if (can_free_funccal(*pfc, copyID))
6806 {
6807 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006808 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006809 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006810 did_free = TRUE;
6811 did_free_funccal = TRUE;
6812 }
6813 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006814 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006815 }
6816 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006817 // When a funccal was freed some more items might be garbage
6818 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006819 (void)garbage_collect(testing);
6820
6821 return did_free;
6822}
6823
6824/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006825 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006826 */
6827 static funccall_T *
6828get_funccal(void)
6829{
6830 int i;
6831 funccall_T *funccal;
6832 funccall_T *temp_funccal;
6833
6834 funccal = current_funccal;
6835 if (debug_backtrace_level > 0)
6836 {
6837 for (i = 0; i < debug_backtrace_level; i++)
6838 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006839 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006840 if (temp_funccal)
6841 funccal = temp_funccal;
6842 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006843 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006844 debug_backtrace_level = i;
6845 }
6846 }
6847 return funccal;
6848}
6849
6850/*
6851 * Return the hashtable used for local variables in the current funccal.
6852 * Return NULL if there is no current funccal.
6853 */
6854 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006855get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006856{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006857 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006858 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006859 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006860}
6861
6862/*
6863 * Return the l: scope variable.
6864 * Return NULL if there is no current funccal.
6865 */
6866 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006867get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006868{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006869 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006870 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006871 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006872}
6873
6874/*
6875 * Return the hashtable used for argument in the current funccal.
6876 * Return NULL if there is no current funccal.
6877 */
6878 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006879get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006880{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006881 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006882 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006883 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006884}
6885
6886/*
6887 * Return the a: scope variable.
6888 * Return NULL if there is no current funccal.
6889 */
6890 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006891get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006892{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006893 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006894 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006895 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006896}
6897
6898/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006899 * List function variables, if there is a function.
6900 */
6901 void
6902list_func_vars(int *first)
6903{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006904 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
6905 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006906 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006907}
6908
6909/*
6910 * If "ht" is the hashtable for local variables in the current funccal, return
6911 * the dict that contains it.
6912 * Otherwise return NULL.
6913 */
6914 dict_T *
6915get_current_funccal_dict(hashtab_T *ht)
6916{
6917 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01006918 && ht == &current_funccal->fc_l_vars.dv_hashtab)
6919 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006920 return NULL;
6921}
6922
6923/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006924 * Search hashitem in parent scope.
6925 */
6926 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006927find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006928{
6929 funccall_T *old_current_funccal = current_funccal;
6930 hashtab_T *ht;
6931 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006932 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006933
Bram Moolenaarca16c602022-09-06 18:57:08 +01006934 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006935 return NULL;
6936
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006937 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006938 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006939 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006940 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006941 ht = find_var_ht(name, &varname);
6942 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006943 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006944 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006945 if (!HASHITEM_EMPTY(hi))
6946 {
6947 *pht = ht;
6948 break;
6949 }
6950 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006951 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02006952 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006953 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006954 }
6955 current_funccal = old_current_funccal;
6956
6957 return hi;
6958}
6959
6960/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006961 * Search variable in parent scope.
6962 */
6963 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006964find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006965{
6966 dictitem_T *v = NULL;
6967 funccall_T *old_current_funccal = current_funccal;
6968 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006969 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006970
Bram Moolenaarca16c602022-09-06 18:57:08 +01006971 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006972 return NULL;
6973
Bram Moolenaare38eab22019-12-05 21:50:01 +01006974 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01006975 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006976 while (current_funccal)
6977 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006978 ht = find_var_ht(name, &varname);
6979 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006980 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006981 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006982 if (v != NULL)
6983 break;
6984 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006985 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006986 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006987 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006988 }
6989 current_funccal = old_current_funccal;
6990
6991 return v;
6992}
6993
6994/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006995 * Set "copyID + 1" in previous_funccal and callers.
6996 */
6997 int
6998set_ref_in_previous_funccal(int copyID)
6999{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007000 funccall_T *fc;
7001
Bram Moolenaarca16c602022-09-06 18:57:08 +01007002 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007003 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007004 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007005 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
7006 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
7007 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007008 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007009 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007010 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007011}
7012
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007013 static int
7014set_ref_in_funccal(funccall_T *fc, int copyID)
7015{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007016 if (fc->fc_copyID != copyID)
7017 {
7018 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007019 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
7020 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
7021 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
7022 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007023 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007024 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007025 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007026}
7027
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007028/*
7029 * Set "copyID" in all local vars and arguments in the call stack.
7030 */
7031 int
7032set_ref_in_call_stack(int copyID)
7033{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007034 funccall_T *fc;
7035 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007036
Bram Moolenaarca16c602022-09-06 18:57:08 +01007037 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007038 if (set_ref_in_funccal(fc, copyID))
7039 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007040
7041 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007042 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007043 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007044 if (set_ref_in_funccal(fc, copyID))
7045 return TRUE;
7046 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007047}
7048
7049/*
7050 * Set "copyID" in all functions available by name.
7051 */
7052 int
7053set_ref_in_functions(int copyID)
7054{
7055 int todo;
7056 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007057 ufunc_T *fp;
7058
7059 todo = (int)func_hashtab.ht_used;
7060 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007061 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007062 if (!HASHITEM_EMPTY(hi))
7063 {
7064 --todo;
7065 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007066 if (!func_name_refcount(fp->uf_name)
7067 && set_ref_in_func(NULL, fp, copyID))
7068 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007069 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007070 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007071 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007072}
7073
7074/*
7075 * Set "copyID" in all function arguments.
7076 */
7077 int
7078set_ref_in_func_args(int copyID)
7079{
7080 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007081
7082 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007083 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7084 copyID, NULL, NULL))
7085 return TRUE;
7086 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007087}
7088
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007089/*
7090 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007091 * Returns TRUE if setting references failed somehow.
7092 */
7093 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007094set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007095{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007096 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007097 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007098 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007099 char_u fname_buf[FLEN_FIXED + 1];
7100 char_u *tofree = NULL;
7101 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007102 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007103
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007104 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007105 return FALSE;
7106
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007107 if (fp_in == NULL)
7108 {
7109 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007110 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007111 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007112 if (fp != NULL)
7113 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007114 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007115 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007116 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007117
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007118 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007119 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007120}
7121
Bram Moolenaare38eab22019-12-05 21:50:01 +01007122#endif // FEAT_EVAL