blob: 5d167101d850b94372fba773df28bc0d1b4ea246 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +020011 * userfunc.c: User defined function support
Bram Moolenaara9b579f2016-07-17 18:29:19 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020017/*
18 * All user-defined functions are found in this hashtable.
19 */
20static hashtab_T func_hashtab;
21
Bram Moolenaare38eab22019-12-05 21:50:01 +010022// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020023static garray_T funcargs = GA_EMPTY;
24
Bram Moolenaar209b8e32019-03-14 13:43:24 +010025// pointer to funccal for currently active function
26static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020027
Bram Moolenaar209b8e32019-03-14 13:43:24 +010028// Pointer to list of previously used funccal, still around because some
29// item in it is still being used.
30static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020031
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020032static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +010033static void func_clear(ufunc_T *fp, int force);
34static int func_free(ufunc_T *fp, int force);
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +010035static char_u *untrans_function_name(char_u *name);
Bram Moolenaar58779852022-09-06 18:31:14 +010036static void handle_defer_one(funccall_T *funccal);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020037
38 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +000039func_init(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020040{
41 hash_init(&func_hashtab);
42}
43
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020044/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020045 * Return the function hash table
46 */
47 hashtab_T *
48func_tbl_get(void)
49{
50 return &func_hashtab;
51}
52
53/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020054 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020055 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010056 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010057 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaardce24412022-02-08 20:35:30 +000058 * If "eap" is not NULL use it to check for an already declared name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010059 * Return a pointer to after the type.
60 * When something is wrong return "arg".
61 */
62 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010063one_function_arg(
64 char_u *arg,
65 garray_T *newargs,
66 garray_T *argtypes,
67 int types_optional,
h-eastb895b0f2023-09-24 15:46:31 +020068 garray_T *arg_objm,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010069 evalarg_T *evalarg,
Bram Moolenaardce24412022-02-08 20:35:30 +000070 exarg_T *eap,
Bram Moolenaar2a389082021-04-09 20:24:31 +020071 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010072 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073{
Bram Moolenaar6e949782020-04-13 17:21:00 +020074 char_u *p = arg;
75 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020076 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077
78 while (ASCII_ISALNUM(*p) || *p == '_')
79 ++p;
Keith Thompson184f71c2024-01-04 21:19:04 +010080 if (arg == p || SAFE_isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020081 || (argtypes == NULL
82 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
83 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010084 {
85 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000086 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087 return arg;
88 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010089
Bram Moolenaarce723f32023-06-10 19:00:12 +010090 // Extra checks in Vim9 script.
91 if (!skip && argtypes != NULL)
92 {
93 int c = *p;
94 *p = NUL;
95 int r = check_reserved_name(arg, FALSE);
96 *p = c;
97 if (r == FAIL)
98 return arg;
99
100 // Cannot use script var name for argument. In function: also check
101 // local vars and arguments.
102 if (check_defined(arg, p - arg,
103 evalarg == NULL ? NULL : evalarg->eval_cctx,
Bram Moolenaardce24412022-02-08 20:35:30 +0000104 eap == NULL ? NULL : eap->cstack, TRUE) == FAIL)
Bram Moolenaarce723f32023-06-10 19:00:12 +0100105 return arg;
106 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100108 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
109 return arg;
110 if (newargs != NULL)
111 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100112 int c;
113 int i;
114
115 c = *p;
116 *p = NUL;
117 arg_copy = vim_strsave(arg);
118 if (arg_copy == NULL)
119 {
120 *p = c;
121 return arg;
122 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200123 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200124 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200125 // Check for duplicate argument name.
126 for (i = 0; i < newargs->ga_len; ++i)
127 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
128 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000129 semsg(_(e_duplicate_argument_name_str), arg_copy);
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200130 vim_free(arg_copy);
131 return arg;
132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100133 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
134 newargs->ga_len++;
135
136 *p = c;
137 }
138
139 // get any type from "arg: type"
h-eastb895b0f2023-09-24 15:46:31 +0200140 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK)
141 && arg_objm != NULL && (skip || ga_grow(arg_objm, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 {
143 char_u *type = NULL;
144
Bram Moolenaar6e949782020-04-13 17:21:00 +0200145 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
146 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200147 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200148 arg_copy == NULL ? arg : arg_copy);
149 p = skipwhite(p);
150 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 if (*p == ':')
152 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200153 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100154 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200155 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100156 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200157 return arg;
158 }
159 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200160 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100161 if (!skip)
162 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100163 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200164 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200165 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200166 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200167 arg_copy == NULL ? arg : arg_copy);
168 return arg;
169 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100170 if (!skip)
171 {
172 if (type == NULL && types_optional)
173 // lambda arguments default to "any" type
Bram Moolenaar2a389082021-04-09 20:24:31 +0200174 type = vim_strsave((char_u *)
175 (is_vararg ? "list<any>" : "any"));
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100176 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
h-eastb895b0f2023-09-24 15:46:31 +0200177 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = FALSE;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 }
180
181 return p;
182}
183
184/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000185 * Handle line continuation in function arguments or body.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000186 * Get a next line, store it in "eap" if appropriate and put the line in
187 * "lines_to_free" to free the line later.
Bram Moolenaar7473a842021-12-28 17:55:26 +0000188 */
189 static char_u *
190get_function_line(
191 exarg_T *eap,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000192 garray_T *lines_to_free,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000193 int indent,
194 getline_opt_T getline_options)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000195{
196 char_u *theline;
197
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100198 if (eap->ea_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +0000199 theline = getcmdline(':', 0L, indent, 0);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000200 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100201 theline = eap->ea_getline(':', eap->cookie, indent, getline_options);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000202 if (theline != NULL)
203 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000204 if (lines_to_free->ga_len > 0
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000205 && eap->cmdlinep != NULL
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000206 && *eap->cmdlinep == ((char_u **)lines_to_free->ga_data)
207 [lines_to_free->ga_len - 1])
Bram Moolenaar7473a842021-12-28 17:55:26 +0000208 *eap->cmdlinep = theline;
Bram Moolenaarb5820102023-01-25 12:27:13 +0000209 (void)ga_add_string(lines_to_free, theline);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000210 }
211
212 return theline;
213}
214
215/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200216 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100217 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100218 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200219 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100220 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200221get_function_args(
222 char_u **argp,
223 char_u endchar,
224 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100225 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100226 int types_optional, // types optional if "argtypes" is not NULL
h-eastb895b0f2023-09-24 15:46:31 +0200227 garray_T *arg_objm, // NULL unless using :def
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100228 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200229 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200230 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200231 int skip,
Bram Moolenaardce24412022-02-08 20:35:30 +0000232 exarg_T *eap, // can be NULL
Bram Moolenaar554d0312023-01-05 19:59:18 +0000233 int in_class, // non-zero when inside a class or interface
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000234 garray_T *newlines, // function body lines
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000235 garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200236{
237 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100238 char_u *arg;
239 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200240 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200241 int any_default = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100242 char_u *whitep = *argp;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100243 int need_expr = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200244
245 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000246 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100247 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000248 ga_init2(argtypes, sizeof(char_u *), 3);
h-eastb895b0f2023-09-24 15:46:31 +0200249 if (arg_objm != NULL)
250 ga_init2(arg_objm, sizeof(int8_T), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200251 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000252 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200253
254 if (varargs != NULL)
255 *varargs = FALSE;
256
257 /*
258 * Isolate the arguments: "arg1, arg2, ...)"
259 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100260 arg = skipwhite(*argp);
261 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200262 while (*p != endchar)
263 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100264 while (eap != NULL && eap->ea_getline != NULL
Bram Moolenaar2c330432020-04-13 14:41:35 +0200265 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200266 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200267 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000268 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000269 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000270
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200271 if (theline == NULL)
272 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200273 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200274 p = skipwhite(theline);
275 }
276
277 if (mustend && *p != endchar)
278 {
279 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000280 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100281 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200282 }
Christian Brabandte4a450a2023-12-08 20:57:38 +0100283 if (*p == endchar && !need_expr)
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200284 break;
285
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200286 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
287 {
288 if (varargs != NULL)
289 *varargs = TRUE;
290 p += 3;
291 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100292
293 if (argtypes != NULL)
294 {
295 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200296 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100298 if (!skip)
299 emsg(_(e_missing_name_after_dots));
300 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100301 }
302
303 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100304 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200305 arg_objm, evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100306 if (p == arg)
307 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100308 if (*skipwhite(p) == '=')
309 {
310 emsg(_(e_cannot_use_default_for_variable_arguments));
311 break;
312 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100313 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200314 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000315 else if (in_class && STRNCMP(p, "this.", 5) == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000316 {
317 // this.memberName
318 p += 5;
319 arg = p;
320 while (ASCII_ISALNUM(*p) || *p == '_')
321 ++p;
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000322 char_u *argend = p;
323
h-eastdb385522023-09-28 22:18:19 +0200324 // object variable this. can be used only in a constructor
325 if (STRNCMP(eap->arg, "new", 3) != 0)
326 {
327 c = *argend;
328 *argend = NUL;
329 semsg(_(e_cannot_use_an_object_variable_except_with_the_new_method_str), arg);
330 *argend = c;
331 break;
332 }
333
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000334 if (*skipwhite(p) == '=')
335 {
336 char_u *defval = skipwhite(skipwhite(p) + 1);
337 if (STRNCMP(defval, "v:none", 6) != 0)
338 {
339 semsg(_(e_constructor_default_value_must_be_vnone_str), p);
340 goto err_ret;
341 }
342 any_default = TRUE;
343 p = defval + 6;
344
345 if (ga_grow(default_args, 1) == FAIL)
346 goto err_ret;
347
348 char_u *expr = vim_strsave((char_u *)"v:none");
349 if (expr == NULL)
350 goto err_ret;
351 ((char_u **)(default_args->ga_data))
352 [default_args->ga_len] = expr;
353 default_args->ga_len++;
354 }
355 else if (any_default)
356 {
357 emsg(_(e_non_default_argument_follows_default_argument));
358 goto err_ret;
359 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000360
361 // TODO: check the argument is indeed a member
362 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
363 return FAIL;
364 if (newargs != NULL)
365 {
366 ((char_u **)(newargs->ga_data))[newargs->ga_len] =
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000367 vim_strnsave(arg, argend - arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000368 newargs->ga_len++;
369
h-eastb895b0f2023-09-24 15:46:31 +0200370 if (argtypes != NULL && ga_grow(argtypes, 1) == OK
371 && arg_objm != NULL && ga_grow(arg_objm, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000372 {
373 // TODO: use the actual type
374 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] =
375 vim_strsave((char_u *)"any");
h-eastb895b0f2023-09-24 15:46:31 +0200376 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000377
378 // Add a line to the function body for the assignment.
379 if (ga_grow(newlines, 1) == OK)
380 {
381 // "this.name = name"
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000382 int len = 5 + (argend - arg) + 3 + (argend - arg) + 1;
383 if (any_default)
384 len += 14 + 10;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000385 char_u *assignment = alloc(len);
386 if (assignment != NULL)
387 {
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000388 c = *argend;
389 *argend = NUL;
390 if (any_default)
391 vim_snprintf((char *)assignment, len,
392 "ifargisset %d this.%s = %s",
393 default_args->ga_len - 1, arg, arg);
394 else
395 vim_snprintf((char *)assignment, len,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000396 "this.%s = %s", arg, arg);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000397 *argend = c;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000398 ((char_u **)(newlines->ga_data))[
399 newlines->ga_len++] = assignment;
400 }
401 }
402 }
403 }
404 if (*p == ',')
405 ++p;
406 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200407 else
408 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200409 char_u *np;
410
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200411 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100412 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200413 arg_objm, evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200415 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200416
Bram Moolenaar015cf102021-06-26 21:52:02 +0200417 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200418 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200419 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200420 if (*np == '=' && np[1] != '=' && np[1] != '~'
421 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200422 {
423 typval_T rettv;
424
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100425 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200426 any_default = TRUE;
Bram Moolenaareb5adf12022-09-04 13:41:37 +0100427 p = skipwhite(np + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000428 char_u *expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200429 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200430 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200431 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200432 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200433 if (ga_grow(default_args, 1) == FAIL)
434 goto err_ret;
435
Christian Brabandte4a450a2023-12-08 20:57:38 +0100436 if (need_expr)
437 need_expr = FALSE;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200438 // trim trailing whitespace
439 while (p > expr && VIM_ISWHITE(p[-1]))
440 p--;
441 c = *p;
442 *p = NUL;
443 expr = vim_strsave(expr);
444 if (expr == NULL)
445 {
446 *p = c;
447 goto err_ret;
448 }
449 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200450 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200451 default_args->ga_len++;
452 *p = c;
453 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200454 }
455 else
Christian Brabandte4a450a2023-12-08 20:57:38 +0100456 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200457 mustend = TRUE;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100458 if (*skipwhite(p) == NUL)
459 need_expr = TRUE;
460 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200461 }
462 else if (any_default)
463 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000464 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200465 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200466 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200467
468 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
469 {
470 // Be tolerant when skipping
471 if (!skip)
472 {
473 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
474 goto err_ret;
475 }
476 p = skipwhite(p);
477 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200478 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200479 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200480 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200481 // Don't give this error when skipping, it makes the "->" not
482 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100483 // Allow missing space after comma in legacy functions.
484 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200485 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
486 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100487 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200488 goto err_ret;
489 }
490 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200491 else
492 mustend = TRUE;
493 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200494 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200495 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200496 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200497
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200498 if (*p != endchar)
499 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100500 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200501
502 *argp = p;
503 return OK;
504
505err_ret:
506 if (newargs != NULL)
507 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200508 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200509 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200510 return FAIL;
511}
512
513/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100514 * Parse the argument types, filling "fp->uf_arg_types".
515 * Return OK or FAIL.
516 */
517 static int
h-eastb895b0f2023-09-24 15:46:31 +0200518parse_argument_types(
519 ufunc_T *fp,
520 garray_T *argtypes,
521 int varargs,
522 garray_T *arg_objm,
523 ocmember_T *obj_members,
524 int obj_member_count)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100525{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200526 int len = 0;
527
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100528 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
529 if (argtypes->ga_len > 0)
530 {
531 // When "varargs" is set the last name/type goes into uf_va_name
532 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200533 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100534
535 if (len > 0)
536 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
537 if (fp->uf_arg_types != NULL)
538 {
539 int i;
540 type_T *type;
541
542 for (i = 0; i < len; ++ i)
543 {
544 char_u *p = ((char_u **)argtypes->ga_data)[i];
545
546 if (p == NULL)
547 // will get the type from the default value
548 type = &t_unknown;
549 else
h-eastb895b0f2023-09-24 15:46:31 +0200550 {
551 if (arg_objm != NULL && ((int8_T *)arg_objm->ga_data)[i])
552 {
553 char_u *aname = ((char_u **)fp->uf_args.ga_data)[i];
554
555 type = &t_any;
556 for (int om = 0; om < obj_member_count; ++om)
557 {
Christian Brabandt915f3bf2024-04-05 20:12:19 +0200558 if (obj_members != NULL
559 && STRCMP(aname,
560 obj_members[om].ocm_name) == 0)
h-eastb895b0f2023-09-24 15:46:31 +0200561 {
562 type = obj_members[om].ocm_type;
563 break;
564 }
565 }
566 }
567 else
568 type = parse_type(&p, &fp->uf_type_list, TRUE);
569 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100570 if (type == NULL)
571 return FAIL;
572 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000573 if (i < fp->uf_args.ga_len
574 && (type->tt_type == VAR_FUNC
Yegappan Lakshmanan3e336502024-04-04 19:35:59 +0200575 || type->tt_type == VAR_PARTIAL))
576 {
577 char_u *name = ((char_u **)fp->uf_args.ga_data)[i];
578 if (obj_members != NULL && *name == '_')
579 // protected object method
580 name++;
581
582 if (var_wrong_func_name(name, TRUE))
583 return FAIL;
584 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100585 }
586 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100587 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200588
589 if (varargs)
590 {
591 char_u *p;
592
593 // Move the last argument "...name: type" to uf_va_name and
594 // uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200595 --fp->uf_args.ga_len;
Bram Moolenaarf0571712023-01-04 13:16:20 +0000596 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len];
597 ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len] = NULL;
Bram Moolenaar2a389082021-04-09 20:24:31 +0200598 p = ((char_u **)argtypes->ga_data)[len];
599 if (p == NULL)
600 // TODO: get type from default value
601 fp->uf_va_type = &t_list_any;
602 else
603 {
604 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
605 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
606 {
607 semsg(_(e_variable_arguments_type_must_be_list_str),
608 ((char_u **)argtypes->ga_data)[len]);
609 return FAIL;
610 }
611 }
612 if (fp->uf_va_type == NULL)
613 return FAIL;
614 }
615
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100616 return OK;
617}
618
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100619 static int
620parse_return_type(ufunc_T *fp, char_u *ret_type)
621{
622 if (ret_type == NULL)
623 fp->uf_ret_type = &t_void;
624 else
625 {
626 char_u *p = ret_type;
627
628 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
629 if (fp->uf_ret_type == NULL)
630 {
631 fp->uf_ret_type = &t_void;
632 return FAIL;
633 }
634 }
635 return OK;
636}
637
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100638/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200639 * Register function "fp" as using "current_funccal" as its scope.
640 */
641 static int
642register_closure(ufunc_T *fp)
643{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200644 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100645 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200646 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200647 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200648 fp->uf_scoped = current_funccal;
649 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200650
Bram Moolenaarca16c602022-09-06 18:57:08 +0100651 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200652 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100653 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
654 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200655 return OK;
656}
657
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100658 static void
659set_ufunc_name(ufunc_T *fp, char_u *name)
660{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100661 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
662 // actually extends beyond the struct.
663 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100664
665 if (name[0] == K_SPECIAL)
666 {
667 fp->uf_name_exp = alloc(STRLEN(name) + 3);
668 if (fp->uf_name_exp != NULL)
669 {
670 STRCPY(fp->uf_name_exp, "<SNR>");
671 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
672 }
673 }
674}
675
Bram Moolenaar58016442016-07-31 18:30:22 +0200676/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100677 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
678 * return "buf" filled with a readable function name.
679 * Otherwise just return "name", thus the return value can always be used.
680 * "name" and "buf" may be equal.
681 */
682 char_u *
683make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
684{
685 size_t len;
686
687 if (name[0] != K_SPECIAL)
688 return name;
689 len = STRLEN(name);
690 if (len + 3 > bufsize)
691 return name;
692
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100693 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100694 mch_memmove(buf, "<SNR>", 5);
695 return buf;
696}
697
698/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200699 * Get a name for a lambda. Returned in static memory.
700 */
701 char_u *
702get_lambda_name(void)
703{
704 static char_u name[30];
705 static int lambda_no = 0;
706
707 sprintf((char*)name, "<lambda>%d", ++lambda_no);
708 return name;
709}
710
Bram Moolenaare01e5212023-01-08 20:31:18 +0000711/*
712 * Allocate a "ufunc_T" for a function called "name".
713 * Makes sure the size is right.
714 */
715 static ufunc_T *
716alloc_ufunc(char_u *name)
717{
718 // When the name is short we need to make sure we allocate enough bytes for
719 // the whole struct, including any padding.
720 size_t len = offsetof(ufunc_T, uf_name) + STRLEN(name) + 1;
721 return alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
722}
723
Bram Moolenaar801ab062020-06-25 19:27:56 +0200724#if defined(FEAT_LUA) || defined(PROTO)
725/*
726 * Registers a native C callback which can be called from Vim script.
727 * Returns the name of the Vim script function.
728 */
729 char_u *
730register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
731{
732 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200733 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200734
Bram Moolenaare01e5212023-01-08 20:31:18 +0000735 fp = alloc_ufunc(name);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200736 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200737 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200738
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200739 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200740 fp->uf_refcount = 1;
741 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000742 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200743 fp->uf_calls = 0;
744 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200745 fp->uf_cb = cb;
746 fp->uf_cb_free = cb_free;
747 fp->uf_cb_state = state;
748
749 set_ufunc_name(fp, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000750 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200751
752 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200753}
754#endif
755
Bram Moolenaar04b12692020-05-04 23:24:44 +0200756/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100757 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100758 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100759 * If "white_error" is not NULL check for correct use of white space and set
760 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100761 * Return NULL if no valid arrow found.
762 */
763 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100764skip_arrow(
765 char_u *start,
766 int equal_arrow,
767 char_u **ret_type,
768 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100769{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100770 char_u *s = start;
771 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100772
773 if (equal_arrow)
774 {
775 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100776 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100777 if (white_error != NULL && !VIM_ISWHITE(s[1]))
778 {
779 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100780 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100781 return NULL;
782 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100783 s = skipwhite(s + 1);
784 *ret_type = s;
785 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100786 if (s == *ret_type)
787 {
788 emsg(_(e_missing_return_type));
789 return NULL;
790 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100791 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100792 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100793 s = skipwhite(s);
794 if (*s != '=')
795 return NULL;
796 ++s;
797 }
798 if (*s != '>')
799 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100800 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
801 || !IS_WHITE_OR_NUL(s[1])))
802 {
803 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100804 semsg(_(e_white_space_required_before_and_after_str_at_str),
805 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100806 return NULL;
807 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100808 return skipwhite(s + 1);
809}
810
811/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100812 * Check if "*cmd" points to a function command and if so advance "*cmd" and
813 * return TRUE.
814 * Otherwise return FALSE;
815 * Do not consider "function(" to be a command.
816 */
817 static int
818is_function_cmd(char_u **cmd)
819{
820 char_u *p = *cmd;
821
822 if (checkforcmd(&p, "function", 2))
823 {
824 if (*p == '(')
825 return FALSE;
826 *cmd = p;
827 return TRUE;
828 }
829 return FALSE;
830}
831
832/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200833 * Called when defining a function: The context may be needed for script
834 * variables declared in a block that is visible now but not when the function
835 * is compiled or called later.
836 */
837 static void
838function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
839{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000840 if (cstack == NULL || cstack->cs_idx < 0)
841 return;
842
843 int count = cstack->cs_idx + 1;
844 int i;
845
846 fp->uf_block_ids = ALLOC_MULT(int, count);
847 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200848 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000849 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
850 sizeof(int) * count);
851 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200852 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000853
854 // Set flag in each block to indicate a function was defined. This
855 // is used to keep the variable when leaving the block, see
856 // hide_script_var().
857 for (i = 0; i <= cstack->cs_idx; ++i)
858 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200859}
860
861/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100862 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200863 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100864 * "newlines" must already have been initialized.
865 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
866 */
867 static int
868get_function_body(
869 exarg_T *eap,
870 garray_T *newlines,
871 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000872 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100873{
874 linenr_T sourcing_lnum_top = SOURCING_LNUM;
875 linenr_T sourcing_lnum_off;
876 int saved_wait_return = need_wait_return;
877 char_u *line_arg = line_arg_in;
878 int vim9_function = eap->cmdidx == CMD_def
879 || eap->cmdidx == CMD_block;
880#define MAX_FUNC_NESTING 50
881 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200882 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100883 int nesting = 0;
884 getline_opt_T getline_options;
885 int indent = 2;
886 char_u *skip_until = NULL;
887 int ret = FAIL;
888 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200889 int heredoc_concat_len = 0;
890 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100891 char_u *heredoc_trimmed = NULL;
892
Bram Moolenaar20677332021-06-06 17:02:53 +0200893 ga_init2(&heredoc_ga, 1, 500);
894
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100895 // Detect having skipped over comment lines to find the return
896 // type. Add NULL lines to keep the line count correct.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100897 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100898 if (SOURCING_LNUM < sourcing_lnum_off)
899 {
900 sourcing_lnum_off -= SOURCING_LNUM;
901 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
902 goto theend;
903 while (sourcing_lnum_off-- > 0)
904 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
905 }
906
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200907 nesting_def[0] = vim9_function;
908 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100909 getline_options = vim9_function
910 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
911 for (;;)
912 {
913 char_u *theline;
914 char_u *p;
915 char_u *arg;
916
917 if (KeyTyped)
918 {
919 msg_scroll = TRUE;
920 saved_wait_return = FALSE;
921 }
922 need_wait_return = FALSE;
923
924 if (line_arg != NULL)
925 {
926 // Use eap->arg, split up in parts by line breaks.
927 theline = line_arg;
928 p = vim_strchr(theline, '\n');
929 if (p == NULL)
930 line_arg += STRLEN(line_arg);
931 else
932 {
933 *p = NUL;
934 line_arg = p + 1;
935 }
936 }
937 else
938 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000939 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100940 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100941 }
942 if (KeyTyped)
943 lines_left = Rows - 1;
944 if (theline == NULL)
945 {
946 // Use the start of the function for the line number.
947 SOURCING_LNUM = sourcing_lnum_top;
948 if (skip_until != NULL)
949 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200950 else if (nesting_inline[nesting])
951 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100952 else if (eap->cmdidx == CMD_def)
953 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100954 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000955 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100956 goto theend;
957 }
958
959 // Detect line continuation: SOURCING_LNUM increased more than one.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100960 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100961 if (SOURCING_LNUM < sourcing_lnum_off)
962 sourcing_lnum_off -= SOURCING_LNUM;
963 else
964 sourcing_lnum_off = 0;
965
966 if (skip_until != NULL)
967 {
968 // Don't check for ":endfunc"/":enddef" between
969 // * ":append" and "."
970 // * ":python <<EOF" and "EOF"
971 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
972 if (heredoc_trimmed == NULL
973 || (is_heredoc && skipwhite(theline) == theline)
974 || STRNCMP(theline, heredoc_trimmed,
975 STRLEN(heredoc_trimmed)) == 0)
976 {
977 if (heredoc_trimmed == NULL)
978 p = theline;
979 else if (is_heredoc)
980 p = skipwhite(theline) == theline
981 ? theline : theline + STRLEN(heredoc_trimmed);
982 else
983 p = theline + STRLEN(heredoc_trimmed);
984 if (STRCMP(p, skip_until) == 0)
985 {
986 VIM_CLEAR(skip_until);
987 VIM_CLEAR(heredoc_trimmed);
988 getline_options = vim9_function
989 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
990 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200991
992 if (heredoc_concat_len > 0)
993 {
994 // Replace the starting line with all the concatenated
995 // lines.
996 ga_concat(&heredoc_ga, theline);
997 vim_free(((char_u **)(newlines->ga_data))[
998 heredoc_concat_len - 1]);
999 ((char_u **)(newlines->ga_data))[
1000 heredoc_concat_len - 1] = heredoc_ga.ga_data;
1001 ga_init(&heredoc_ga);
1002 heredoc_concat_len = 0;
1003 theline += STRLEN(theline); // skip the "EOF"
1004 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001005 }
1006 }
1007 }
1008 else
1009 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001010 int c;
1011 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +00001012 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001013
1014 // skip ':' and blanks
1015 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
1016 ;
1017
1018 // Check for "endfunction", "enddef" or "}".
1019 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +00001020 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001021 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001022 ? *p == '}'
1023 : (checkforcmd(&p, nesting_def[nesting]
1024 ? "enddef" : "endfunction", 4)
1025 && *p != ':'))
1026 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00001027 if (!nesting_inline[nesting] && nesting_def[nesting]
1028 && p < cmd + 6)
1029 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001030 if (nesting-- == 0)
1031 {
1032 char_u *nextcmd = NULL;
1033
1034 if (*p == '|' || *p == '}')
1035 nextcmd = p + 1;
1036 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
1037 nextcmd = line_arg;
1038 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001039 && (vim9_function || p_verbose > 0))
1040 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +02001041 SOURCING_LNUM = sourcing_lnum_top
1042 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001043 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +00001044 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001045 else
1046 give_warning2((char_u *)
1047 _("W22: Text found after :endfunction: %s"),
1048 p, TRUE);
1049 }
1050 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001051 {
1052 // Another command follows. If the line came from "eap"
1053 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001054 // change "eap->cmdlinep" to point to the last fetched
1055 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001056 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001057 if (lines_to_free->ga_len > 0
1058 && *eap->cmdlinep !=
1059 ((char_u **)lines_to_free->ga_data)
1060 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001061 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001062 // *cmdlinep will be freed later, thus remove the
1063 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001064 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001065 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
1066 [lines_to_free->ga_len - 1];
1067 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001068 }
1069 }
1070 break;
1071 }
1072 }
1073
1074 // Check for mismatched "endfunc" or "enddef".
1075 // We don't check for "def" inside "func" thus we also can't check
1076 // for "enddef".
1077 // We continue to find the end of the function, although we might
1078 // not find it.
1079 else if (nesting_def[nesting])
1080 {
1081 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1082 emsg(_(e_mismatched_endfunction));
1083 }
1084 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1085 emsg(_(e_mismatched_enddef));
1086
1087 // Increase indent inside "if", "while", "for" and "try", decrease
1088 // at "end".
1089 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1090 indent -= 2;
1091 else if (STRNCMP(p, "if", 2) == 0
1092 || STRNCMP(p, "wh", 2) == 0
1093 || STRNCMP(p, "for", 3) == 0
1094 || STRNCMP(p, "try", 3) == 0)
1095 indent += 2;
1096
1097 // Check for defining a function inside this function.
1098 // Only recognize "def" inside "def", not inside "function",
1099 // For backwards compatibility, see Test_function_python().
1100 c = *p;
1101 if (is_function_cmd(&p)
1102 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1103 {
1104 if (*p == '!')
1105 p = skipwhite(p + 1);
1106 p += eval_fname_script(p);
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001107 vim_free(trans_function_name(&p, NULL, TRUE, 0));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001108 if (*skipwhite(p) == '(')
1109 {
1110 if (nesting == MAX_FUNC_NESTING - 1)
1111 emsg(_(e_function_nesting_too_deep));
1112 else
1113 {
1114 ++nesting;
1115 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001116 nesting_inline[nesting] = FALSE;
1117 indent += 2;
1118 }
1119 }
1120 }
1121
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001122 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001123 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001124 // Not a comment line: check for nested inline function.
1125 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001126 while (end > p && VIM_ISWHITE(*end))
1127 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001128 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001129 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001130 int is_block;
1131
1132 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001133 --end;
1134 while (end > p && VIM_ISWHITE(*end))
1135 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001136 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1137 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001138 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001139 char_u *s = p;
1140
1141 // check for line starting with "au" for :autocmd or
1142 // "com" for :command, these can use a {} block
1143 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1144 || checkforcmd_noparen(&s, "command", 3);
1145 }
1146
1147 if (is_block)
1148 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001149 if (nesting == MAX_FUNC_NESTING - 1)
1150 emsg(_(e_function_nesting_too_deep));
1151 else
1152 {
1153 ++nesting;
1154 nesting_def[nesting] = TRUE;
1155 nesting_inline[nesting] = TRUE;
1156 indent += 2;
1157 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001158 }
1159 }
1160 }
1161
1162 // Check for ":append", ":change", ":insert". Not for :def.
1163 p = skip_range(p, FALSE, NULL);
1164 if (!vim9_function
1165 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1166 || (p[0] == 'c'
1167 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1168 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1169 && (STRNCMP(&p[3], "nge", 3) != 0
1170 || !ASCII_ISALPHA(p[6])))))))
1171 || (p[0] == 'i'
1172 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1173 && (!ASCII_ISALPHA(p[2])
1174 || (p[2] == 's'
1175 && (!ASCII_ISALPHA(p[3])
1176 || p[3] == 'e'))))))))
1177 skip_until = vim_strsave((char_u *)".");
1178
1179 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1180 arg = skipwhite(skiptowhite(p));
1181 if (arg[0] == '<' && arg[1] =='<'
1182 && ((p[0] == 'p' && p[1] == 'y'
1183 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1184 || ((p[2] == '3' || p[2] == 'x')
1185 && !ASCII_ISALPHA(p[3]))))
1186 || (p[0] == 'p' && p[1] == 'e'
1187 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1188 || (p[0] == 't' && p[1] == 'c'
1189 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1190 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1191 && !ASCII_ISALPHA(p[3]))
1192 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1193 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1194 || (p[0] == 'm' && p[1] == 'z'
1195 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1196 ))
1197 {
1198 // ":python <<" continues until a dot, like ":append"
1199 p = skipwhite(arg + 2);
1200 if (STRNCMP(p, "trim", 4) == 0)
1201 {
1202 // Ignore leading white space.
1203 p = skipwhite(p + 4);
1204 heredoc_trimmed = vim_strnsave(theline,
1205 skipwhite(theline) - theline);
1206 }
1207 if (*p == NUL)
1208 skip_until = vim_strsave((char_u *)".");
1209 else
1210 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1211 getline_options = GETLINE_NONE;
1212 is_heredoc = TRUE;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001213 if (vim9_function && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001214 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001215 }
1216
Bram Moolenaard881d152022-05-13 13:50:36 +01001217 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001218 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001219 // Check for ":cmd v =<< [trim] EOF"
1220 // and ":cmd [a, b] =<< [trim] EOF"
1221 // and "lines =<< [trim] EOF" for Vim9
1222 // Where "cmd" can be "let", "var", "final" or "const".
zeertzjqa93d9cd2023-05-02 16:25:47 +01001223 arg = p;
1224 if (checkforcmd(&arg, "let", 2)
1225 || checkforcmd(&arg, "var", 3)
1226 || checkforcmd(&arg, "final", 5)
1227 || checkforcmd(&arg, "const", 5)
1228 || vim9_function)
Bram Moolenaard881d152022-05-13 13:50:36 +01001229 {
zeertzjq9a91d2b2024-04-09 21:47:10 +02001230 int save_sc_version = current_sctx.sc_version;
1231 int var_count = 0;
1232 int semicolon = 0;
zeertzjq9a91d2b2024-04-09 21:47:10 +02001233
1234 current_sctx.sc_version
1235 = vim9_function ? SCRIPT_VERSION_VIM9 : 1;
zeertzjq1817ccd2024-04-10 17:37:47 +02001236 arg = skip_var_list(arg, TRUE, &var_count, &semicolon,
zeertzjq9a91d2b2024-04-09 21:47:10 +02001237 TRUE);
zeertzjq1817ccd2024-04-10 17:37:47 +02001238 if (arg != NULL)
1239 arg = skipwhite(arg);
zeertzjq9a91d2b2024-04-09 21:47:10 +02001240 current_sctx.sc_version = save_sc_version;
zeertzjq1817ccd2024-04-10 17:37:47 +02001241 if (arg != NULL && STRNCMP(arg, "=<<", 3) == 0)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001242 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001243 p = skipwhite(arg + 3);
1244 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001245 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001246 if (STRNCMP(p, "trim", 4) == 0)
1247 {
1248 // Ignore leading white space.
1249 p = skipwhite(p + 4);
1250 heredoc_trimmed = vim_strnsave(theline,
1251 skipwhite(theline) - theline);
1252 continue;
1253 }
1254 if (STRNCMP(p, "eval", 4) == 0)
1255 {
1256 // Ignore leading white space.
1257 p = skipwhite(p + 4);
1258 continue;
1259 }
1260 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001261 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001262 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1263 getline_options = GETLINE_NONE;
1264 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001265 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001266 }
1267 }
1268 }
1269
1270 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001271 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001272 goto theend;
1273
Bram Moolenaar20677332021-06-06 17:02:53 +02001274 if (heredoc_concat_len > 0)
1275 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001276 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001277 // to be used for the instruction later.
1278 ga_concat(&heredoc_ga, theline);
1279 ga_concat(&heredoc_ga, (char_u *)"\n");
1280 p = vim_strsave((char_u *)"");
1281 }
1282 else
1283 {
1284 // Copy the line to newly allocated memory. get_one_sourceline()
1285 // allocates 250 bytes per line, this saves 80% on average. The
1286 // cost is an extra alloc/free.
1287 p = vim_strsave(theline);
1288 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001289 if (p == NULL)
1290 goto theend;
1291 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1292
1293 // Add NULL lines for continuation lines, so that the line count is
1294 // equal to the index in the growarray.
1295 while (sourcing_lnum_off-- > 0)
1296 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1297
1298 // Check for end of eap->arg.
1299 if (line_arg != NULL && *line_arg == NUL)
1300 line_arg = NULL;
1301 }
1302
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001303 // Return OK when no error was detected.
1304 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001305 ret = OK;
1306
1307theend:
1308 vim_free(skip_until);
1309 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001310 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001311 need_wait_return |= saved_wait_return;
1312 return ret;
1313}
1314
1315/*
1316 * Handle the body of a lambda. *arg points to the "{", process statements
1317 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001318 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001319 * When successful "rettv" is set to a funcref.
1320 */
1321 static int
1322lambda_function_body(
1323 char_u **arg,
1324 typval_T *rettv,
1325 evalarg_T *evalarg,
1326 garray_T *newargs,
1327 garray_T *argtypes,
1328 int varargs,
1329 garray_T *default_args,
1330 char_u *ret_type)
1331{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001332 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001333 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001334 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001335 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001336 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001337 exarg_T eap;
1338 garray_T newlines;
1339 char_u *cmdline = NULL;
1340 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001341 partial_T *pt;
1342 char_u *name;
1343 int lnum_save = -1;
1344 linenr_T sourcing_lnum_top = SOURCING_LNUM;
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001345 char_u *line_arg = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001346
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001347 *arg = skipwhite(*arg + 1);
1348 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001349 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001350 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001351 return FAIL;
1352 }
1353
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001354 // When there is a line break use what follows for the lambda body.
1355 // Makes lambda body initializers work for object and enum member
1356 // variables.
1357 if (**arg == '\n')
1358 line_arg = *arg + 1;
1359
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001360 CLEAR_FIELD(eap);
1361 eap.cmdidx = CMD_block;
1362 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001363 eap.cmdlinep = &cmdline;
1364 eap.skip = !evaluate;
1365 if (evalarg->eval_cctx != NULL)
1366 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1367 else
1368 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001369 eap.ea_getline = evalarg->eval_getline;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001370 eap.cookie = evalarg->eval_cookie;
1371 }
1372
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001373 ga_init2(&newlines, sizeof(char_u *), 10);
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001374 if (get_function_body(&eap, &newlines, line_arg,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001375 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001376 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001377
1378 // When inside a lambda must add the function lines to evalarg.eval_ga.
1379 evalarg->eval_break_count += newlines.ga_len;
1380 if (gap->ga_itemsize > 0)
1381 {
1382 int idx;
1383 char_u *last;
1384 size_t plen;
1385 char_u *pnl;
1386
1387 for (idx = 0; idx < newlines.ga_len; ++idx)
1388 {
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001389 char_u *p = ((char_u **)newlines.ga_data)[idx];
1390 if (p == NULL)
1391 // comment line in the lambda body
1392 continue;
1393
1394 p = skipwhite(p);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001395
Bram Moolenaarecb66452021-05-18 15:09:18 +02001396 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001397 goto erret;
1398
1399 // Going to concatenate the lines after parsing. For an empty or
1400 // comment line use an empty string.
1401 // Insert NL characters at the start of each line, the string will
1402 // be split again later in .get_lambda_tv().
1403 if (*p == NUL || vim9_comment_start(p))
1404 p = (char_u *)"";
1405 plen = STRLEN(p);
1406 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1407 if (pnl != NULL)
1408 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001409 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1410 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001411 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001412 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001413 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001414 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001415 // more is following after the "}", which was skipped
1416 last = cmdline;
1417 else
1418 // nothing is following the "}"
1419 last = (char_u *)"}";
1420 plen = STRLEN(last);
1421 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1422 if (pnl != NULL)
1423 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001424 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1425 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001426 }
1427
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001428 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001429 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001430 garray_T *tfgap = &evalarg->eval_tofree_ga;
1431
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001432 // Something comes after the "}".
1433 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001434
1435 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001436 if (ga_grow(tfgap, 1) == OK)
1437 {
1438 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1439 evalarg->eval_using_cmdline = TRUE;
1440 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001441 }
1442 else
1443 *arg = (char_u *)"";
1444
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001445 if (!evaluate)
1446 {
1447 ret = OK;
1448 goto erret;
1449 }
1450
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001451 name = get_lambda_name();
Bram Moolenaare01e5212023-01-08 20:31:18 +00001452 ufunc = alloc_ufunc(name);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001453 if (ufunc == NULL)
1454 goto erret;
1455 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001456 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001457 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001458 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001459 ufunc->uf_refcount = 1;
1460 ufunc->uf_args = *newargs;
1461 newargs->ga_data = NULL;
1462 ufunc->uf_def_args = *default_args;
1463 default_args->ga_data = NULL;
1464 ufunc->uf_func_type = &t_func_any;
1465
1466 // error messages are for the first function line
1467 lnum_save = SOURCING_LNUM;
1468 SOURCING_LNUM = sourcing_lnum_top;
1469
1470 // parse argument types
h-eastb895b0f2023-09-24 15:46:31 +02001471 if (parse_argument_types(ufunc, argtypes, varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001472 {
1473 SOURCING_LNUM = lnum_save;
1474 goto erret;
1475 }
1476
1477 // parse the return type, if any
1478 if (parse_return_type(ufunc, ret_type) == FAIL)
1479 goto erret;
1480
1481 pt = ALLOC_CLEAR_ONE(partial_T);
1482 if (pt == NULL)
1483 goto erret;
1484 pt->pt_func = ufunc;
1485 pt->pt_refcount = 1;
1486
1487 ufunc->uf_lines = newlines;
1488 newlines.ga_data = NULL;
1489 if (sandbox)
1490 ufunc->uf_flags |= FC_SANDBOX;
1491 if (!ASCII_ISUPPER(*ufunc->uf_name))
1492 ufunc->uf_flags |= FC_VIM9;
1493 ufunc->uf_script_ctx = current_sctx;
1494 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1495 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1496 set_function_type(ufunc);
1497
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001498 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1499
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001500 rettv->vval.v_partial = pt;
1501 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001502 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001503 ret = OK;
1504
1505erret:
1506 if (lnum_save >= 0)
1507 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001508 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001509 if (newargs != NULL)
1510 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001511 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001512 if (ufunc != NULL)
1513 {
1514 func_clear(ufunc, TRUE);
1515 func_free(ufunc, TRUE);
1516 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001517 return ret;
1518}
1519
1520/*
1521 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001522 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001523 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001524 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1525 */
1526 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001527get_lambda_tv(
1528 char_u **arg,
1529 typval_T *rettv,
1530 int types_optional,
1531 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001532{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001533 int evaluate = evalarg != NULL
1534 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001535 garray_T newargs;
1536 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001537 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001538 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001539 garray_T default_args;
h-eastb895b0f2023-09-24 15:46:31 +02001540 garray_T arg_objm;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001541 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001542 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001543 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001544 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001545 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001546 char_u *s;
1547 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001548 int *old_eval_lavars = eval_lavars_used;
1549 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001550 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001551 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001552 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001553 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001554 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001555 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001556
Bram Moolenaar4525a572022-02-13 11:57:33 +00001557 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001558 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001559
1560 ga_init(&newargs);
1561 ga_init(&newlines);
1562
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001563 // First, check if this is really a lambda expression. "->" or "=>" must
1564 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001565 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001566 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001567 types_optional ? &argtypes : NULL, types_optional,
1568 types_optional ? &arg_objm : NULL, evalarg,
1569 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001570 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001571 {
1572 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001573 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001574 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001575 ga_clear(&arg_objm);
1576 }
Bram Moolenaar0346b792021-01-31 22:18:29 +01001577 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001578 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001579
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001580 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001581 if (evaluate)
1582 pnewargs = &newargs;
1583 else
1584 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001585 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001586 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
h-eastb895b0f2023-09-24 15:46:31 +02001587 types_optional ? &argtypes : NULL, types_optional,
1588 types_optional ? &arg_objm : NULL, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001589 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001590 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001591 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001592 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001593 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001594 {
1595 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001596 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001597 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001598 ga_clear(&arg_objm);
1599 }
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001600 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001601 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001602 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001603 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001604
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001605 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1606 if (ret_type != NULL)
1607 {
1608 ret_type = vim_strsave(ret_type);
1609 tofree2 = ret_type;
1610 }
1611
Bram Moolenaare38eab22019-12-05 21:50:01 +01001612 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001613 if (evaluate)
1614 eval_lavars_used = &eval_lavars;
1615
Bram Moolenaar65c44152020-12-24 15:14:01 +01001616 *arg = skipwhite_and_linebreak(*arg, evalarg);
1617
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001618 // Recognize "{" as the start of a function body.
1619 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001620 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001621 if (evalarg == NULL)
1622 // cannot happen?
1623 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001624 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001625 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1626 types_optional ? &argtypes : NULL, varargs,
1627 &default_args, ret_type) == FAIL)
1628 goto errret;
1629 goto theend;
1630 }
1631 if (default_args.ga_len > 0)
1632 {
1633 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001634 goto errret;
1635 }
1636
Bram Moolenaare38eab22019-12-05 21:50:01 +01001637 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001638 start = *arg;
1639 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001640 if (ret == FAIL)
1641 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001642
Bram Moolenaar65c44152020-12-24 15:14:01 +01001643 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001644 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001645 *arg = skipwhite_and_linebreak(*arg, evalarg);
1646 if (**arg != '}')
1647 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001648 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001649 goto errret;
1650 }
1651 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001652 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001653
1654 if (evaluate)
1655 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001656 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001657 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001658 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001659 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001660 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001661
Bram Moolenaare01e5212023-01-08 20:31:18 +00001662 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001663 if (fp == NULL)
1664 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001665 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001666 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001667 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001668 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001669
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001670 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001671 if (ga_grow(&newlines, 1) == FAIL)
1672 goto errret;
1673
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001674 // If there are line breaks, we need to split up the string.
1675 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001676 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001677 line_end = end;
1678
1679 // Add "return " before the expression (or the first line).
1680 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001681 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001682 if (p == NULL)
1683 goto errret;
1684 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1685 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001686 vim_strncpy(p + 7, start, line_end - start);
1687
1688 if (line_end != end)
1689 {
1690 // Add more lines, split by line breaks. Thus is used when a
1691 // lambda with { cmds } is encountered.
1692 while (*line_end == '\n')
1693 {
1694 if (ga_grow(&newlines, 1) == FAIL)
1695 goto errret;
1696 start = line_end + 1;
1697 line_end = vim_strchr(start, '\n');
1698 if (line_end == NULL)
1699 line_end = end;
1700 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1701 vim_strnsave(start, line_end - start);
1702 }
1703 }
1704
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001705 if (strstr((char *)p + 7, "a:") == NULL)
1706 // No a: variables are used for sure.
1707 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001708
1709 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001710 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001711 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001712 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001713 if (types_optional)
1714 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001715 if (parse_argument_types(fp, &argtypes,
h-eastb895b0f2023-09-24 15:46:31 +02001716 vim9script && varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001717 goto errret;
1718 if (ret_type != NULL)
1719 {
1720 fp->uf_ret_type = parse_type(&ret_type,
1721 &fp->uf_type_list, TRUE);
1722 if (fp->uf_ret_type == NULL)
1723 goto errret;
1724 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001725 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001726 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001727 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001728
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001729 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001730 if (current_funccal != NULL && eval_lavars)
1731 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001732 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001733 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001734 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001735 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001736
1737#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001738 if (prof_def_func())
1739 func_do_profile(fp);
1740#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001741 if (sandbox)
1742 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001743 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001744 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001745 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001746 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001747 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001748 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001749 // Use the line number of the arguments.
1750 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001751
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001752 function_using_block_scopes(fp, evalarg->eval_cstack);
1753
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001754 pt->pt_func = fp;
1755 pt->pt_refcount = 1;
1756 rettv->vval.v_partial = pt;
1757 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001758
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001759 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001760 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001761
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001762theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001763 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001764 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001765 if (types_optional)
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001766 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001767 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001768 ga_clear(&arg_objm);
1769 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001770
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001771 return OK;
1772
1773errret:
1774 ga_clear_strings(&newargs);
1775 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001776 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001777 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001778 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001779 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001780 ga_clear(&arg_objm);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001781 if (fp != NULL)
1782 vim_free(fp->uf_arg_types);
1783 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001784 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001785 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001786 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001787 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001788 return FAIL;
1789}
1790
1791/*
1792 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1793 * name it contains, otherwise return "name".
1794 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1795 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001796 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1797 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001798 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001799 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001800 */
1801 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001802deref_func_name(
1803 char_u *name,
1804 int *lenp,
1805 partial_T **partialp,
1806 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001807 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001808 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001809 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001810{
1811 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001812 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001813 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001814 char_u *s = NULL;
1815 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001816 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001817
1818 if (partialp != NULL)
1819 *partialp = NULL;
1820
1821 cc = name[*lenp];
1822 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001823
Bram Moolenaar71f21932022-01-07 18:20:55 +00001824 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001825 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001826 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001827 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001828 tv = &v->di_tv;
1829 }
1830 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1831 {
1832 imported_T *import;
1833 char_u *p = name;
1834 int len = *lenp;
1835
1836 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001837 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001838 p = name + 2;
1839 len -= 2;
1840 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001841 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001842
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001843 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001844 if (import != NULL)
1845 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001846 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001847 if (new_function)
1848 semsg(_(e_redefining_imported_item_str), name);
1849 else
1850 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001851 name[len] = cc;
1852 *lenp = 0;
1853 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001854 }
1855 }
1856
1857 if (tv != NULL)
1858 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001859 if (found_var != NULL)
1860 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001861 if (tv->v_type == VAR_FUNC)
1862 {
1863 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001864 {
1865 *lenp = 0;
1866 return (char_u *)""; // just in case
1867 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001868 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001869 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001870 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001871
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001872 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001873 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001874 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001875
1876 if (pt == NULL)
1877 {
1878 *lenp = 0;
1879 return (char_u *)""; // just in case
1880 }
1881 if (partialp != NULL)
1882 *partialp = pt;
1883 s = partial_name(pt);
1884 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001885 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001886
1887 if (s != NULL)
1888 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001889 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001890 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001891 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001892
1893 if (sv != NULL)
1894 *type = sv->sv_type;
1895 }
1896 return s;
1897 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001898 }
1899
1900 return name;
1901}
1902
1903/*
1904 * Give an error message with a function name. Handle <SNR> things.
1905 * "ermsg" is to be passed without translation, use N_() instead of _().
1906 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001907 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001908emsg_funcname(char *ermsg, char_u *name)
1909{
Bram Moolenaar54598062022-01-13 12:05:09 +00001910 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001911
Bram Moolenaar54598062022-01-13 12:05:09 +00001912 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001913 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001914 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001915 if (p != name)
1916 vim_free(p);
1917}
1918
1919/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001920 * Get function arguments at "*arg" and advance it.
1921 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001922 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001923 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001924 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001925get_func_arguments(
1926 char_u **arg,
1927 evalarg_T *evalarg,
1928 int partial_argc,
1929 typval_T *argvars,
Ernie Raelb077b582023-12-14 20:11:44 +01001930 int *argcount,
1931 int is_builtin)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001932{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001933 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001934 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001935 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001936 int evaluate = evalarg == NULL
1937 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001938
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001939 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001940 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001941 // skip the '(' or ',' and possibly line breaks
1942 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1943
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001944 if (*argp == ')' || *argp == ',' || *argp == NUL)
1945 break;
Ernie Raelb077b582023-12-14 20:11:44 +01001946
1947 int arg_idx = *argcount;
1948 if (eval1(&argp, &argvars[arg_idx], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001949 {
1950 ret = FAIL;
1951 break;
1952 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001953 ++*argcount;
Ernie Raelb077b582023-12-14 20:11:44 +01001954 if (!is_builtin && check_typval_is_value(&argvars[arg_idx]) == FAIL)
1955 {
1956 ret = FAIL;
1957 break;
1958 }
1959
Bram Moolenaar9d489562020-07-30 20:08:50 +02001960 // The comma should come right after the argument, but this wasn't
1961 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001962 if (vim9script)
1963 {
1964 if (*argp != ',' && *skipwhite(argp) == ',')
1965 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001966 if (evaluate)
1967 semsg(_(e_no_white_space_allowed_before_str_str),
1968 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001969 ret = FAIL;
1970 break;
1971 }
1972 }
1973 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001974 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001975 if (*argp != ',')
1976 break;
Christian Brabandt00cb2472023-09-05 20:46:25 +02001977 if (vim9script && !IS_WHITE_NL_OR_NUL(argp[1]))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001978 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001979 if (evaluate)
1980 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001981 ret = FAIL;
1982 break;
1983 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001984 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001985
Bram Moolenaare6b53242020-07-01 17:28:33 +02001986 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001987 if (*argp == ')')
1988 ++argp;
1989 else
1990 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001991 *arg = argp;
1992 return ret;
1993}
1994
1995/*
1996 * Call a function and put the result in "rettv".
1997 * Return OK or FAIL.
1998 */
1999 int
2000get_func_tv(
2001 char_u *name, // name of the function
2002 int len, // length of "name" or -1 to use strlen()
2003 typval_T *rettv,
2004 char_u **arg, // argument, pointing to the '('
2005 evalarg_T *evalarg, // for line continuation
2006 funcexe_T *funcexe) // various values
2007{
2008 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002009 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002010 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
2011 int argcount = 0; // number of arguments found
2012 int vim9script = in_vim9script();
2013 int evaluate = evalarg == NULL
2014 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
2015
2016 argp = *arg;
2017 ret = get_func_arguments(&argp, evalarg,
2018 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
Ernie Raelb077b582023-12-14 20:11:44 +01002019 argvars, &argcount, builtin_function(name, -1));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002020
2021 if (ret == OK)
2022 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002023 int i = 0;
2024 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002025
2026 if (get_vim_var_nr(VV_TESTING))
2027 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002028 // Prepare for calling test_garbagecollect_now(), need to know
2029 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002030 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002031 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002032 for (i = 0; i < argcount; ++i)
2033 if (ga_grow(&funcargs, 1) == OK)
2034 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
2035 &argvars[i];
2036 }
2037
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002038 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00002039 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002040 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002041 // An error in a builtin function does not return FAIL, but we do
2042 // want to abort further processing if an error was given.
2043 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002044 clear_tv(rettv);
2045 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002046
2047 funcargs.ga_len -= i;
2048 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002049 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002050 {
2051 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002052 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002053 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00002054 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002055 }
2056
2057 while (--argcount >= 0)
2058 clear_tv(&argvars[argcount]);
2059
Bram Moolenaar4525a572022-02-13 11:57:33 +00002060 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02002061 *arg = argp;
2062 else
2063 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002064 return ret;
2065}
2066
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002067/*
2068 * Return TRUE if "p" starts with "<SID>" or "s:".
2069 * Only works if eval_fname_script() returned non-zero for "p"!
2070 */
2071 static int
2072eval_fname_sid(char_u *p)
2073{
2074 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
2075}
2076
2077/*
2078 * In a script change <SID>name() and s:name() to K_SNR 123_name().
2079 * Change <SNR>123_name() to K_SNR 123_name().
2080 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002081 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002082 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002083 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00002084fname_trans_sid(
2085 char_u *name,
2086 char_u *fname_buf,
2087 char_u **tofree,
2088 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002089{
2090 int llen;
2091 char_u *fname;
2092 int i;
2093
2094 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002095 if (llen == 0)
2096 return name; // no prefix
2097
2098 fname_buf[0] = K_SPECIAL;
2099 fname_buf[1] = KS_EXTRA;
2100 fname_buf[2] = (int)KE_SNR;
2101 i = 3;
2102 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002103 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002104 if (current_sctx.sc_sid <= 0)
2105 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002106 else
2107 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002108 sprintf((char *)fname_buf + 3, "%ld_",
2109 (long)current_sctx.sc_sid);
2110 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002111 }
2112 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002113 if (i + STRLEN(name + llen) < FLEN_FIXED)
2114 {
2115 STRCPY(fname_buf + i, name + llen);
2116 fname = fname_buf;
2117 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002118 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002119 {
2120 fname = alloc(i + STRLEN(name + llen) + 1);
2121 if (fname == NULL)
2122 *error = FCERR_OTHER;
2123 else
2124 {
2125 *tofree = fname;
2126 mch_memmove(fname, fname_buf, (size_t)i);
2127 STRCPY(fname + i, name + llen);
2128 }
2129 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002130 return fname;
2131}
2132
2133/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002134 * Concatenate the script ID and function name into "<SNR>99_name".
2135 * "buffer" must have size MAX_FUNC_NAME_LEN.
2136 */
2137 void
2138func_name_with_sid(char_u *name, int sid, char_u *buffer)
2139{
2140 // A script-local function is stored as "<SNR>99_name".
2141 buffer[0] = K_SPECIAL;
2142 buffer[1] = KS_EXTRA;
2143 buffer[2] = (int)KE_SNR;
2144 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2145 (long)sid, name);
2146}
2147
2148/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002149 * Find a function "name" in script "sid".
2150 */
2151 static ufunc_T *
2152find_func_with_sid(char_u *name, int sid)
2153{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002154 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002155 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156
Bram Moolenaarb8822442022-01-11 15:24:05 +00002157 if (!SCRIPT_ID_VALID(sid))
2158 return NULL; // not in a script
2159
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002160 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002161 hi = hash_find(&func_hashtab, buffer);
2162 if (!HASHITEM_EMPTY(hi))
2163 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002164 return NULL;
2165}
2166
2167/*
2168 * Find a function "name" in script "sid" prefixing the autoload prefix.
2169 */
2170 static ufunc_T *
2171find_func_with_prefix(char_u *name, int sid)
2172{
2173 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002174 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002175 scriptitem_T *si;
2176
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002177 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002178 return NULL; // already has the prefix
2179 if (!SCRIPT_ID_VALID(sid))
2180 return NULL; // not in a script
2181 si = SCRIPT_ITEM(sid);
2182 if (si->sn_autoload_prefix != NULL)
2183 {
2184 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2185 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002186 char_u *namep;
2187
2188 // skip a "<SNR>99_" prefix
2189 namep = untrans_function_name(name);
2190 if (namep == NULL)
2191 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002192
2193 // An exported function in an autoload script is stored as
2194 // "dir#path#name".
2195 if (len < sizeof(buffer))
2196 auto_name = buffer;
2197 else
2198 auto_name = alloc(len);
2199 if (auto_name != NULL)
2200 {
2201 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002202 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002203 hi = hash_find(&func_hashtab, auto_name);
2204 if (auto_name != buffer)
2205 vim_free(auto_name);
2206 if (!HASHITEM_EMPTY(hi))
2207 return HI2UF(hi);
2208 }
2209 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210
2211 return NULL;
2212}
2213
2214/*
Ernie Rael16409692024-07-31 22:18:11 +02002215 * Find a function by name, return pointer to it.
2216 * The name may be a local script variable, VAR_FUNC. or it may be a fully
2217 * qualified import name such as 'i_imp.FuncName'.
2218 *
2219 * When VAR_FUNC, the import might either direct or autoload.
2220 * When 'i_imp.FuncName' it is direct, autoload is rewritten as i_imp#FuncName
2221 * in f_call and subsequently found.
2222 */
2223 static ufunc_T *
2224find_func_imported(char_u *name, int flags)
2225{
2226 ufunc_T *func = NULL;
2227 char_u *dot = name; // Find a dot, '.', in the name
2228
2229 // Either run into '.' or the end of the string
2230 while (eval_isnamec(*dot))
2231 ++dot;
2232
2233 if (*dot == '.')
2234 {
2235 imported_T *import = find_imported(name, dot - name, FALSE);
2236 if (import != NULL)
2237 func = find_func_with_sid(dot + 1, import->imp_sid);
2238 }
2239 else if (*dot == NUL) // looking at the entire string
2240 {
2241 hashtab_T *ht = get_script_local_ht();
2242 if (ht != NULL)
2243 {
2244 hashitem_T *hi = hash_find(ht, name);
2245 if (!HASHITEM_EMPTY(hi))
2246 {
2247 dictitem_T *di = HI2DI(hi);
Ernie Raelcb90ea92024-08-19 21:45:23 +02002248 if (di->di_tv.v_type == VAR_FUNC
2249 && di->di_tv.vval.v_string != NULL)
Ernie Rael16409692024-07-31 22:18:11 +02002250 func = find_func_even_dead(di->di_tv.vval.v_string, flags);
2251 }
2252 }
2253 }
2254 return func;
2255}
2256
2257/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002258 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002259 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2260 * functions.
2261 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002262 * Return NULL for unknown function.
2263 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002264 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002265find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002266{
2267 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002270 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002272 // Find script-local function before global one.
2273 if (in_vim9script() && eval_isnamec1(*name)
2274 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002275 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002276 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2277 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002278 if (func != NULL)
2279 return func;
2280 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002281 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2282 {
2283 char_u *p = name + 5;
2284 long sid;
2285
2286 // printable "<SNR>123_Name" form
2287 sid = getdigits(&p);
2288 if (*p == '_')
2289 {
2290 func = find_func_with_sid(p + 1, (int)sid);
2291 if (func != NULL)
2292 return func;
2293 }
2294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002296
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002297 if ((flags & FFED_NO_GLOBAL) == 0)
2298 {
2299 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002300 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002301 if (!HASHITEM_EMPTY(hi))
2302 return HI2UF(hi);
2303 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002304
Bram Moolenaarb8822442022-01-11 15:24:05 +00002305 // Find autoload function if this is an autoload script.
Ernie Rael16409692024-07-31 22:18:11 +02002306 func = find_func_with_prefix(name[0] == 's' && name[1] == ':'
Bram Moolenaarb8822442022-01-11 15:24:05 +00002307 ? name + 2 : name, current_sctx.sc_sid);
Ernie Rael16409692024-07-31 22:18:11 +02002308 if (func != NULL)
2309 return func;
2310
2311 // Find a script-local "VAR_FUNC" or i_"imp.Func", so vim9script).
2312 if (in_vim9script())
2313 func = find_func_imported(name, flags);
2314 return func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315}
2316
2317/*
2318 * Find a function by name, return pointer to it in ufuncs.
2319 * "cctx" is passed in a :def function to find imported functions.
2320 * Return NULL for unknown or dead function.
2321 */
2322 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002323find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002324{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002325 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002326
2327 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2328 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002329 return NULL;
2330}
2331
2332/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002333 * Return TRUE if "ufunc" is a global function.
2334 */
2335 int
2336func_is_global(ufunc_T *ufunc)
2337{
2338 return ufunc->uf_name[0] != K_SPECIAL;
2339}
2340
2341/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002342 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2343 */
2344 int
2345func_requires_g_prefix(ufunc_T *ufunc)
2346{
2347 return ufunc->uf_name[0] != K_SPECIAL
2348 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002349 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01002350 && !SAFE_isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002351}
2352
2353/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002354 * Copy the function name of "fp" to buffer "buf".
2355 * "buf" must be able to hold the function name plus three bytes.
2356 * Takes care of script-local function names.
2357 */
2358 static void
2359cat_func_name(char_u *buf, ufunc_T *fp)
2360{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002361 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002362 {
2363 STRCPY(buf, "<SNR>");
2364 STRCAT(buf, fp->uf_name + 3);
2365 }
2366 else
2367 STRCPY(buf, fp->uf_name);
2368}
2369
2370/*
2371 * Add a number variable "name" to dict "dp" with value "nr".
2372 */
2373 static void
2374add_nr_var(
2375 dict_T *dp,
2376 dictitem_T *v,
2377 char *name,
2378 varnumber_T nr)
2379{
2380 STRCPY(v->di_key, name);
2381 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002382 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002383 v->di_tv.v_type = VAR_NUMBER;
2384 v->di_tv.v_lock = VAR_FIXED;
2385 v->di_tv.vval.v_number = nr;
2386}
2387
2388/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002389 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002390 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002391 static void
2392free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002393{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002394 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002395
Bram Moolenaarca16c602022-09-06 18:57:08 +01002396 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002397 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002398 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002399
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002400 // When garbage collecting a funccall_T may be freed before the
2401 // function that references it, clear its uf_scoped field.
2402 // The function may have been redefined and point to another
2403 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002404 if (fp != NULL && fp->uf_scoped == fc)
2405 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002406 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002407 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002408
Bram Moolenaarca16c602022-09-06 18:57:08 +01002409 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002410 vim_free(fc);
2411}
2412
2413/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002414 * Free "fc" and what it contains.
2415 * Can be called only when "fc" is kept beyond the period of it called,
2416 * i.e. after cleanup_function_call(fc).
2417 */
2418 static void
2419free_funccal_contents(funccall_T *fc)
2420{
2421 listitem_T *li;
2422
2423 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002424 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002425
2426 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002427 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002428
2429 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002430 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002431 clear_tv(&li->li_tv);
2432
2433 free_funccal(fc);
2434}
2435
2436/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002437 * Handle the last part of returning from a function: free the local hashtable.
2438 * Unless it is still in use by a closure.
2439 */
2440 static void
2441cleanup_function_call(funccall_T *fc)
2442{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002443 int may_free_fc = fc->fc_refcount <= 0;
2444 int free_fc = TRUE;
2445
Bram Moolenaarca16c602022-09-06 18:57:08 +01002446 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002447
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002448 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002449 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2450 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002451 else
2452 free_fc = FALSE;
2453
2454 // If the a:000 list and the l: and a: dicts are not referenced and
2455 // there is no closure using it, we can free the funccall_T and what's
2456 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002457 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2458 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002459 else
2460 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002461 int todo;
2462 hashitem_T *hi;
2463 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002464
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002465 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002466
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002467 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002468 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002469 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002470 {
2471 if (!HASHITEM_EMPTY(hi))
2472 {
2473 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002474 di = HI2DI(hi);
2475 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002476 }
2477 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002478 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002479
Bram Moolenaarca16c602022-09-06 18:57:08 +01002480 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2481 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002482 else
2483 {
2484 listitem_T *li;
2485
2486 free_fc = FALSE;
2487
2488 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002489 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002490 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002491 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002492
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002493 if (free_fc)
2494 free_funccal(fc);
2495 else
2496 {
2497 static int made_copy = 0;
2498
2499 // "fc" is still in use. This can happen when returning "a:000",
2500 // assigning "l:" to a global variable or defining a closure.
2501 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002502 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002503 previous_funccal = fc;
2504
2505 if (want_garbage_collect)
2506 // If garbage collector is ready, clear count.
2507 made_copy = 0;
2508 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002509 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002510 // We have made a lot of copies, worth 4 Mbyte. This can happen
2511 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002512 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002513 // too much memory.
2514 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002515 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002516 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002517 }
2518}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002519
2520/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002521 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2522 */
2523 static int
2524numbered_function(char_u *name)
2525{
Keith Thompson184f71c2024-01-04 21:19:04 +01002526 return SAFE_isdigit(*name)
2527 || (name[0] == 'g' && name[1] == ':' && SAFE_isdigit(name[2]));
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002528}
2529
2530/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002531 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002532 * 1. ordinary names, function defined with :function or :def;
2533 * can start with "<SNR>123_" literally or with K_SPECIAL.
2534 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002535 * For the first we only count the name stored in func_hashtab as a reference,
2536 * using function() does not count as a reference, because the function is
2537 * looked up by name.
2538 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002539 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002540func_name_refcount(char_u *name)
2541{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002542 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002543}
2544
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545/*
2546 * Unreference "fc": decrement the reference count and free it when it
2547 * becomes zero. "fp" is detached from "fc".
2548 * When "force" is TRUE we are exiting.
2549 */
2550 static void
2551funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2552{
2553 funccall_T **pfc;
2554 int i;
2555
2556 if (fc == NULL)
2557 return;
2558
2559 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002560 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2561 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2562 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2563 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 {
2565 if (fc == *pfc)
2566 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002567 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 free_funccal_contents(fc);
2569 return;
2570 }
2571 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002572 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2573 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2574 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575}
2576
2577/*
2578 * Remove the function from the function hashtable. If the function was
2579 * deleted while it still has references this was already done.
2580 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2581 */
2582 static int
2583func_remove(ufunc_T *fp)
2584{
2585 hashitem_T *hi;
2586
2587 // Return if it was already virtually deleted.
2588 if (fp->uf_flags & FC_DEAD)
2589 return FALSE;
2590
2591 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002592 if (HASHITEM_EMPTY(hi))
2593 return FALSE;
2594
2595 // When there is a def-function index do not actually remove the
2596 // function, so we can find the index when defining the function again.
2597 // Do remove it when it's a copy.
2598 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002600 fp->uf_flags |= FC_DEAD;
2601 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002603 hash_remove(&func_hashtab, hi, "remove function");
2604 fp->uf_flags |= FC_DELETED;
2605 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606}
2607
2608 static void
2609func_clear_items(ufunc_T *fp)
2610{
2611 ga_clear_strings(&(fp->uf_args));
2612 ga_clear_strings(&(fp->uf_def_args));
2613 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002615 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002616 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01002617 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002618
2619 // Increment the refcount of this function to avoid it being freed
2620 // recursively when the partial is freed.
2621 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002622 partial_unref(fp->uf_partial);
2623 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002624 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002625
2626#ifdef FEAT_LUA
2627 if (fp->uf_cb_free != NULL)
2628 {
2629 fp->uf_cb_free(fp->uf_cb_state);
2630 fp->uf_cb_free = NULL;
2631 }
2632
2633 fp->uf_cb_state = NULL;
2634 fp->uf_cb = NULL;
2635#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636#ifdef FEAT_PROFILE
2637 VIM_CLEAR(fp->uf_tml_count);
2638 VIM_CLEAR(fp->uf_tml_total);
2639 VIM_CLEAR(fp->uf_tml_self);
2640#endif
2641}
2642
2643/*
2644 * Free all things that a function contains. Does not free the function
2645 * itself, use func_free() for that.
2646 * When "force" is TRUE we are exiting.
2647 */
2648 static void
2649func_clear(ufunc_T *fp, int force)
2650{
2651 if (fp->uf_cleared)
2652 return;
2653 fp->uf_cleared = TRUE;
2654
2655 // clear this function
2656 func_clear_items(fp);
2657 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002658 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659}
2660
2661/*
2662 * Free a function and remove it from the list of functions. Does not free
2663 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002664 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002665 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002667 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002668func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669{
2670 // Only remove it when not done already, otherwise we would remove a newer
2671 // version of the function with the same name.
2672 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2673 func_remove(fp);
2674
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002675 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002676 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002677 if (fp->uf_dfunc_idx > 0)
2678 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002679 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002681 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002682 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002683 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684}
2685
2686/*
2687 * Free all things that a function contains and free the function itself.
2688 * When "force" is TRUE we are exiting.
2689 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002690 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691func_clear_free(ufunc_T *fp, int force)
2692{
2693 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002694 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2695 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002696 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002697 else
2698 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699}
2700
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002701/*
2702 * Copy already defined function "lambda" to a new function with name "global".
2703 * This is for when a compiled function defines a global function.
2704 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002705 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002706copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002707 char_u *lambda,
2708 char_u *global,
2709 loopvarinfo_T *loopvarinfo,
2710 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002711{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002712 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002713 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002714
2715 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002716 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002717 semsg(_(e_lambda_function_not_found_str), lambda);
2718 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002719 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002720
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002721 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002722 if (fp != NULL)
2723 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002724 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002725 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002726 return FAIL;
2727 }
2728
Bram Moolenaare01e5212023-01-08 20:31:18 +00002729 fp = alloc_ufunc(global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002730 if (fp == NULL)
2731 return FAIL;
2732
2733 fp->uf_varargs = ufunc->uf_varargs;
2734 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2735 fp->uf_def_status = ufunc->uf_def_status;
2736 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2737 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2738 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2739 == FAIL
2740 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2741 goto failed;
2742
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002743 if (ufunc->uf_arg_types != NULL)
2744 {
2745 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2746 if (fp->uf_arg_types == NULL)
2747 goto failed;
2748 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2749 sizeof(type_T *) * fp->uf_args.ga_len);
2750 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002751 if (ufunc->uf_va_name != NULL)
2752 {
2753 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2754 if (fp->uf_va_name == NULL)
2755 goto failed;
2756 }
2757 fp->uf_ret_type = ufunc->uf_ret_type;
2758
2759 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002760
2761 fp->uf_name_exp = NULL;
2762 set_ufunc_name(fp, global);
2763
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002764 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002765
2766 // the referenced dfunc_T is now used one more time
2767 link_def_function(fp);
2768
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002769 // Create a partial to store the context of the function where it was
2770 // instantiated. Only needs to be done once. Do this on the original
2771 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002772 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2773 {
2774 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2775
2776 if (pt == NULL)
2777 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002778 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002779 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002780 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002781 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002782 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002783 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002784 }
2785
2786 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002787
2788failed:
2789 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002790 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002791}
2792
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002793static int funcdepth = 0;
2794
2795/*
2796 * Increment the function call depth count.
2797 * Return FAIL when going over 'maxfuncdepth'.
2798 * Otherwise return OK, must call funcdepth_decrement() later!
2799 */
2800 int
2801funcdepth_increment(void)
2802{
2803 if (funcdepth >= p_mfd)
2804 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002805 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002806 return FAIL;
2807 }
2808 ++funcdepth;
2809 return OK;
2810}
2811
2812 void
2813funcdepth_decrement(void)
2814{
2815 --funcdepth;
2816}
2817
2818/*
2819 * Get the current function call depth.
2820 */
2821 int
2822funcdepth_get(void)
2823{
2824 return funcdepth;
2825}
2826
2827/*
2828 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002829 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002830 * times as funcdepth_increment().
2831 */
2832 void
2833funcdepth_restore(int depth)
2834{
2835 funcdepth = depth;
2836}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002837
2838/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002839 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2840 * "rettv".
2841 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2842 * Returns NULL when allocation fails.
2843 */
2844 funccall_T *
2845create_funccal(ufunc_T *fp, typval_T *rettv)
2846{
2847 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2848
2849 if (fc == NULL)
2850 return NULL;
2851 fc->fc_caller = current_funccal;
2852 current_funccal = fc;
2853 fc->fc_func = fp;
2854 func_ptr_ref(fp);
2855 fc->fc_rettv = rettv;
2856 return fc;
2857}
2858
2859/*
2860 * To be called when returning from a compiled function; restores
2861 * current_funccal.
2862 */
2863 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002864remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002865{
2866 funccall_T *fc = current_funccal;
2867
2868 current_funccal = fc->fc_caller;
2869 free_funccal(fc);
2870}
2871
2872/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002873 * Call a user function.
2874 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002875 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002876call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002877 ufunc_T *fp, // pointer to function
2878 int argcount, // nr of args
2879 typval_T *argvars, // arguments
2880 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002881 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002882 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002883{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002884 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002885 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002886 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002887 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002888 funccall_T *fc;
2889 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002890 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002891 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002892 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002893 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002894 int i;
2895 int ai;
2896 int islambda = FALSE;
2897 char_u numbuf[NUMBUFLEN];
2898 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002899 typval_T *tv_to_free[MAX_FUNC_ARGS];
2900 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002901#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002902 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002903#endif
ichizok7e5fe382023-04-15 13:17:50 +01002904 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002905
Bram Moolenaarb2049902021-01-24 12:53:53 +01002906#ifdef FEAT_PROFILE
2907 CLEAR_FIELD(profile_info);
2908#endif
2909
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002910 // If depth of calling is getting too high, don't execute the function.
2911 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002912 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002913 rettv->v_type = VAR_NUMBER;
2914 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002915 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002916 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002917
Bram Moolenaare38eab22019-12-05 21:50:01 +01002918 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002919
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002920 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002921 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00002922 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002923 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002924 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002925 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2926 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002927 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002928 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002929
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002930 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002932#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002933 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002934#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002935 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002936#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002937 if (do_profiling == PROF_YES)
2938 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002939#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002940 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002941 if (call_def_function(fp, argcount, argvars, 0,
2942 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
2943 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002944 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002945#ifdef FEAT_PROFILE
2946 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002947 || (caller != NULL && caller->uf_profiling)))
2948 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002949#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002950 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002951 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002952 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 }
2954
Bram Moolenaar38453522021-11-28 22:00:12 +00002955 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002956
2957 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002958 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2959 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2960 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002961 */
2962 /*
2963 * Init l: variables.
2964 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002965 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002966 if (selfdict != NULL)
2967 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002968 // Set l:self to "selfdict". Use "name" to avoid a warning from
2969 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002970 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002971 name = v->di_key;
2972 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002973 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002974 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002975 v->di_tv.v_type = VAR_DICT;
2976 v->di_tv.v_lock = 0;
2977 v->di_tv.vval.v_dict = selfdict;
2978 ++selfdict->dv_refcount;
2979 }
2980
2981 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002982 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002983 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002984 * Set a:000 to a list with room for the "..." arguments.
2985 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002986 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002987 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002988 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002989 (varnumber_T)(argcount >= fp->uf_args.ga_len
2990 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002991 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002992 if ((fp->uf_flags & FC_NOARGS) == 0)
2993 {
2994 // Use "name" to avoid a warning from some compiler that checks the
2995 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002996 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002997 name = v->di_key;
2998 STRCPY(name, "000");
2999 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003000 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003001 v->di_tv.v_type = VAR_LIST;
3002 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003003 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003004 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01003005 CLEAR_FIELD(fc->fc_l_varlist);
3006 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
3007 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003008
3009 /*
3010 * Set a:firstline to "firstline" and a:lastline to "lastline".
3011 * Set a:name to named arguments.
3012 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003013 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003014 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003015 if ((fp->uf_flags & FC_NOARGS) == 0)
3016 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003017 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
3018 "firstline", (varnumber_T)funcexe->fe_firstline);
3019 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
3020 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003021 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003022 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003023 {
3024 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003025 typval_T def_rettv;
3026 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003027
3028 ai = i - fp->uf_args.ga_len;
3029 if (ai < 0)
3030 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003031 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003032 name = FUNCARG(fp, i);
3033 if (islambda)
3034 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003035
3036 // evaluate named argument default expression
3037 isdefault = ai + fp->uf_def_args.ga_len >= 0
3038 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
3039 && argvars[i].vval.v_number == VVAL_NONE));
3040 if (isdefault)
3041 {
3042 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003043
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003044 def_rettv.v_type = VAR_NUMBER;
3045 def_rettv.vval.v_number = -1;
3046
3047 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
3048 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003049 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003050 {
3051 default_arg_err = 1;
3052 break;
3053 }
3054 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003055 }
3056 else
3057 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003058 if ((fp->uf_flags & FC_NOARGS) != 0)
3059 // Bail out if no a: arguments used (in lambda).
3060 break;
3061
Bram Moolenaare38eab22019-12-05 21:50:01 +01003062 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003063 sprintf((char *)numbuf, "%d", ai + 1);
3064 name = numbuf;
3065 }
3066 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
3067 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003068 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003069 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003070 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003071 }
3072 else
3073 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003074 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003075 if (v == NULL)
3076 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003077 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003078 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003079
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003080 // Note: the values are copied directly to avoid alloc/free.
3081 // "argvars" must have VAR_FIXED for v_lock.
3082 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003083 v->di_tv.v_lock = VAR_FIXED;
3084
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003085 if (isdefault)
3086 // Need to free this later, no matter where it's stored.
3087 tv_to_free[tv_to_free_len++] = &v->di_tv;
3088
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003089 if (addlocal)
3090 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003091 // Named arguments should be accessed without the "a:" prefix in
3092 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003093 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003094 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003095 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003096 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003097 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003098
3099 if (ai >= 0 && ai < MAX_FUNC_ARGS)
3100 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003101 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003102
3103 li->li_tv = argvars[i];
3104 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003105 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003106 }
3107 }
3108
Bram Moolenaare38eab22019-12-05 21:50:01 +01003109 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003110 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003111
3112 if (fp->uf_flags & FC_SANDBOX)
3113 {
3114 using_sandbox = TRUE;
3115 ++sandbox;
3116 }
3117
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003118 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003119 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003120 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003121 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003122 ++no_wait_return;
3123 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003124
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003125 smsg(_("calling %s"), SOURCING_NAME);
3126 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003127 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003128 char_u buf[MSG_BUF_LEN];
3129 char_u numbuf2[NUMBUFLEN];
3130 char_u *tofree;
3131 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003132
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003133 msg_puts("(");
3134 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003135 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003136 if (i > 0)
3137 msg_puts(", ");
3138 if (argvars[i].v_type == VAR_NUMBER)
3139 msg_outnum((long)argvars[i].vval.v_number);
3140 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003141 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003142 // Do not want errors such as E724 here.
3143 ++emsg_off;
3144 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3145 --emsg_off;
3146 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003147 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003148 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003149 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003150 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3151 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003152 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003153 msg_puts((char *)s);
3154 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003155 }
3156 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003157 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003158 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003159 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003160 msg_puts("\n"); // don't overwrite this either
3161
3162 verbose_leave_scroll();
3163 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003164 }
3165#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003166 if (do_profiling == PROF_YES)
3167 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003168 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003169#endif
3170
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003171 // "legacy" does not apply to commands in the function
3172 sticky_cmdmod_flags = 0;
3173
Bram Moolenaar98aff652022-09-06 21:02:35 +01003174 // If called from a compiled :def function the execution context must be
3175 // hidden, any deferred functions need to be added to the function being
3176 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003177 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003178
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003179 save_current_sctx = current_sctx;
3180 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003181 save_did_emsg = did_emsg;
3182 did_emsg = FALSE;
3183
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003184 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003185 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003186 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003187 retval = FCERR_FAILED;
3188 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003189 else if (islambda)
3190 {
3191 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3192
3193 // A Lambda always has the command "return {expr}". It is much faster
3194 // to evaluate {expr} directly.
3195 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003196 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003197 --ex_nesting_level;
3198 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003199 else
3200 // call do_cmdline() to execute the lines
3201 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003202 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3203
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003204 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003205 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003206
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003207 if (RedrawingDisabled > 0)
3208 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003209
Bram Moolenaare38eab22019-12-05 21:50:01 +01003210 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003211 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3212 {
3213 clear_tv(rettv);
3214 rettv->v_type = VAR_NUMBER;
3215 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003216
3217 // In corner cases returning a "failed" value is not backwards
3218 // compatible. Only do this for Vim9 script.
3219 if (in_vim9script())
3220 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003221 }
3222
3223#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003224 if (do_profiling == PROF_YES)
3225 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003226 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003227
3228 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3229 profile_may_end_func(&profile_info, fp, caller);
3230 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003231#endif
3232
Bram Moolenaare38eab22019-12-05 21:50:01 +01003233 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003234 if (p_verbose >= 12)
3235 {
3236 ++no_wait_return;
3237 verbose_enter_scroll();
3238
3239 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003240 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003241 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003242 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003243 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003244 else
3245 {
3246 char_u buf[MSG_BUF_LEN];
3247 char_u numbuf2[NUMBUFLEN];
3248 char_u *tofree;
3249 char_u *s;
3250
Bram Moolenaare38eab22019-12-05 21:50:01 +01003251 // The value may be very long. Skip the middle part, so that we
3252 // have some idea how it starts and ends. smsg() would always
3253 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003254 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003255 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003256 --emsg_off;
3257 if (s != NULL)
3258 {
3259 if (vim_strsize(s) > MSG_BUF_CLEN)
3260 {
3261 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3262 s = buf;
3263 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003264 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003265 vim_free(tofree);
3266 }
3267 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003268 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003269
3270 verbose_leave_scroll();
3271 --no_wait_return;
3272 }
3273
ichizok7e5fe382023-04-15 13:17:50 +01003274 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003275 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003276 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003277 restore_current_ectx(save_current_ectx);
3278
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003279#ifdef FEAT_PROFILE
3280 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003281 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003282#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003283 if (using_sandbox)
3284 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003285 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003286
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003287 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003288 {
3289 ++no_wait_return;
3290 verbose_enter_scroll();
3291
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003292 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003293 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003294
3295 verbose_leave_scroll();
3296 --no_wait_return;
3297 }
3298
3299 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003300 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003301 for (i = 0; i < tv_to_free_len; ++i)
3302 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003303 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003304
3305 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003306}
3307
3308/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003309 * Check the argument count for user function "fp".
3310 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3311 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003312 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003313check_user_func_argcount(ufunc_T *fp, int argcount)
3314{
3315 int regular_args = fp->uf_args.ga_len;
3316
3317 if (argcount < regular_args - fp->uf_def_args.ga_len)
3318 return FCERR_TOOFEW;
3319 else if (!has_varargs(fp) && argcount > regular_args)
3320 return FCERR_TOOMANY;
3321 return FCERR_UNKNOWN;
3322}
3323
3324/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003326 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003327 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328call_user_func_check(
3329 ufunc_T *fp,
3330 int argcount,
3331 typval_T *argvars,
3332 typval_T *rettv,
3333 funcexe_T *funcexe,
3334 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003335{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003336 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003337
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003338#ifdef FEAT_LUA
3339 if (fp->uf_flags & FC_CFUNC)
3340 {
3341 cfunc_T cb = fp->uf_cb;
3342
3343 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3344 }
3345#endif
3346
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003347 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3348 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003349 error = check_user_func_argcount(fp, argcount);
3350 if (error != FCERR_UNKNOWN)
3351 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003352
Bram Moolenaar50824712020-12-20 21:10:17 +01003353 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003354 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003356 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003358 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 int did_save_redo = FALSE;
3360 save_redo_T save_redo;
3361
3362 /*
3363 * Call the user function.
3364 * Save and restore search patterns, script variables and
3365 * redo buffer.
3366 */
3367 save_search_patterns();
3368 if (!ins_compl_active())
3369 {
3370 saveRedobuff(&save_redo);
3371 did_save_redo = TRUE;
3372 }
3373 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003374 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003375 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3377 // Function was unreferenced while being used, free it now.
3378 func_clear_free(fp, FALSE);
3379 if (did_save_redo)
3380 restoreRedobuff(&save_redo);
3381 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003382 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003383
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003385}
3386
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003387static funccal_entry_T *funccal_stack = NULL;
3388
3389/*
3390 * Save the current function call pointer, and set it to NULL.
3391 * Used when executing autocommands and for ":source".
3392 */
3393 void
3394save_funccal(funccal_entry_T *entry)
3395{
3396 entry->top_funccal = current_funccal;
3397 entry->next = funccal_stack;
3398 funccal_stack = entry;
3399 current_funccal = NULL;
3400}
3401
3402 void
3403restore_funccal(void)
3404{
3405 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003406 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003407 else
3408 {
3409 current_funccal = funccal_stack->top_funccal;
3410 funccal_stack = funccal_stack->next;
3411 }
3412}
3413
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003414 funccall_T *
3415get_current_funccal(void)
3416{
3417 return current_funccal;
3418}
3419
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003420/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003421 * Return TRUE when currently at the script level:
3422 * - not in a function
3423 * - not executing an autocommand
3424 * Note that when an autocommand sources a script the result is FALSE;
3425 */
3426 int
3427at_script_level(void)
3428{
3429 return current_funccal == NULL && autocmd_match == NULL;
3430}
3431
3432/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003433 * Mark all functions of script "sid" as deleted.
3434 */
3435 void
3436delete_script_functions(int sid)
3437{
3438 hashitem_T *hi;
3439 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003440 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003441 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003442 size_t len;
3443
3444 buf[0] = K_SPECIAL;
3445 buf[1] = KS_EXTRA;
3446 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003447 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003448 len = STRLEN(buf);
3449
Bram Moolenaarce658352020-07-31 23:47:12 +02003450 while (todo > 0)
3451 {
3452 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003453 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003454 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003455 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003456 fp = HI2UF(hi);
3457 if (STRNCMP(fp->uf_name, buf, len) == 0)
3458 {
3459 int changed = func_hashtab.ht_changed;
3460
3461 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003462
3463 if (fp->uf_calls > 0)
3464 {
3465 // Function is executing, don't free it but do remove
3466 // it from the hashtable.
3467 if (func_remove(fp))
3468 fp->uf_refcount--;
3469 }
3470 else
3471 {
3472 func_clear(fp, TRUE);
3473 // When clearing a function another function can be
3474 // cleared as a side effect. When that happens start
3475 // over.
3476 if (changed != func_hashtab.ht_changed)
3477 break;
3478 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003479 }
3480 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003481 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003482 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003483}
3484
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003485#if defined(EXITFREE) || defined(PROTO)
3486 void
3487free_all_functions(void)
3488{
3489 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003490 ufunc_T *fp;
3491 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003492 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003493 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003494
Bram Moolenaare38eab22019-12-05 21:50:01 +01003495 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003496 while (current_funccal != NULL)
3497 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003498 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003499 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003500 if (current_funccal == NULL && funccal_stack != NULL)
3501 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003502 }
3503
Bram Moolenaare38eab22019-12-05 21:50:01 +01003504 // First clear what the functions contain. Since this may lower the
3505 // reference count of a function, it may also free a function and change
3506 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003507 while (todo > 0)
3508 {
3509 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003510 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003511 if (!HASHITEM_EMPTY(hi))
3512 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 // clear the def function index now
3514 fp = HI2UF(hi);
3515 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003516 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517
Bram Moolenaare38eab22019-12-05 21:50:01 +01003518 // Only free functions that are not refcounted, those are
3519 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003520 if (func_name_refcount(fp->uf_name))
3521 ++skipped;
3522 else
3523 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003524 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003525 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003526 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003527 {
3528 skipped = 0;
3529 break;
3530 }
3531 }
3532 --todo;
3533 }
3534 }
3535
Bram Moolenaare38eab22019-12-05 21:50:01 +01003536 // Now actually free the functions. Need to start all over every time,
3537 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003538 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003539 while (func_hashtab.ht_used > skipped)
3540 {
3541 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003542 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003543 if (!HASHITEM_EMPTY(hi))
3544 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003545 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003546 // Only free functions that are not refcounted, those are
3547 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003548 fp = HI2UF(hi);
3549 if (func_name_refcount(fp->uf_name))
3550 ++skipped;
3551 else
3552 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003553 if (func_free(fp, FALSE) == OK)
3554 {
3555 skipped = 0;
3556 break;
3557 }
3558 // did not actually free it
3559 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003560 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003561 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003562 }
3563 if (skipped == 0)
3564 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565
3566 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003567}
3568#endif
3569
3570/*
3571 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003572 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3573 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003574 * "len" is the length of "name", or -1 for NUL terminated.
3575 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003577builtin_function(char_u *name, int len)
3578{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003579 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003580
Bram Moolenaara26b9702020-04-18 19:53:28 +02003581 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003582 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003583 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3584 {
3585 if (name[i] == AUTOLOAD_CHAR)
3586 return FALSE;
3587 if (!eval_isnamec(name[i]))
3588 {
3589 // "name.something" is not a builtin function
3590 if (name[i] == '.')
3591 return FALSE;
3592 break;
3593 }
3594 }
3595 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003596}
3597
3598 int
3599func_call(
3600 char_u *name,
3601 typval_T *args,
3602 partial_T *partial,
3603 dict_T *selfdict,
3604 typval_T *rettv)
3605{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003606 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003607 listitem_T *item;
3608 typval_T argv[MAX_FUNC_ARGS + 1];
3609 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003610 int r = 0;
3611
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003612 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003613 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003614 {
3615 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3616 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003617 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003618 break;
3619 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003620 // Make a copy of each argument. This is needed to be able to set
3621 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003622 copy_tv(&item->li_tv, &argv[argc++]);
3623 }
3624
3625 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003626 {
3627 funcexe_T funcexe;
3628
Bram Moolenaara80faa82020-04-12 19:37:17 +02003629 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003630 funcexe.fe_firstline = curwin->w_cursor.lnum;
3631 funcexe.fe_lastline = curwin->w_cursor.lnum;
3632 funcexe.fe_evaluate = TRUE;
3633 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003634 if (partial != NULL)
3635 {
3636 funcexe.fe_object = partial->pt_obj;
3637 if (funcexe.fe_object != NULL)
3638 ++funcexe.fe_object->obj_refcount;
3639 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003640 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003641 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3642 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003643
Bram Moolenaare38eab22019-12-05 21:50:01 +01003644 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003645 while (argc > 0)
3646 clear_tv(&argv[--argc]);
3647
3648 return r;
3649}
3650
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003651static int callback_depth = 0;
3652
3653 int
3654get_callback_depth(void)
3655{
3656 return callback_depth;
3657}
3658
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003659/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003660 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003661 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003662 */
3663 int
3664call_callback(
3665 callback_T *callback,
3666 int len, // length of "name" or -1 to use strlen()
3667 typval_T *rettv, // return value goes here
3668 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003669 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003670 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003671{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003672 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003673 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003674
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003675 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3676 return FAIL;
Christian Brabandt47510f32023-10-15 09:56:16 +02003677
zeertzjqfe583b12023-12-21 16:59:26 +01003678 if (callback_depth > p_mfd)
Christian Brabandt47510f32023-10-15 09:56:16 +02003679 {
3680 emsg(_(e_command_too_recursive));
3681 return FAIL;
3682 }
3683
Bram Moolenaara80faa82020-04-12 19:37:17 +02003684 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003685 funcexe.fe_evaluate = TRUE;
3686 funcexe.fe_partial = callback->cb_partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003687 if (callback->cb_partial != NULL)
3688 {
3689 funcexe.fe_object = callback->cb_partial->pt_obj;
3690 if (funcexe.fe_object != NULL)
3691 ++funcexe.fe_object->obj_refcount;
3692 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003693 ++callback_depth;
3694 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3695 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003696
3697 // When a :def function was called that uses :try an error would be turned
3698 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003699 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003700 {
3701 need_rethrow = FALSE;
3702 handle_did_throw();
3703 }
3704
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003705 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003706}
3707
3708/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003709 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003710 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003711 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3712 */
3713 varnumber_T
3714call_callback_retnr(
3715 callback_T *callback,
3716 int argcount, // number of "argvars"
3717 typval_T *argvars) // vars for arguments, must have "argcount"
3718 // PLUS ONE elements!
3719{
3720 typval_T rettv;
3721 varnumber_T retval;
3722
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003723 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003724 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003725
3726 retval = tv_get_number_chk(&rettv, NULL);
3727 clear_tv(&rettv);
3728 return retval;
3729}
3730
3731/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732 * Give an error message for the result of a function.
3733 * Nothing if "error" is FCERR_NONE.
3734 */
3735 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003736user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737{
3738 switch (error)
3739 {
3740 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003741 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003742 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003743 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003744 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745 break;
3746 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003747 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 break;
3749 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003750 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 break;
3752 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003753 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003754 break;
3755 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003756 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757 break;
3758 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003759 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 break;
3761 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003762 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3763 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003765 case FCERR_OTHER:
3766 case FCERR_FAILED:
3767 // assume the error message was already given
3768 break;
3769 case FCERR_NONE:
3770 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 }
3772}
3773
3774/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003775 * Check the argument types "argvars[argcount]" for "name" using the
3776 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3777 * is already included in "argvars[]".
3778 * Will do nothing if "funcexe->fe_check_type" is NULL or
3779 * "funcexe->fe_evaluate" is FALSE;
3780 * Returns an FCERR_ value.
3781 */
3782 static funcerror_T
3783may_check_argument_types(
3784 funcexe_T *funcexe,
3785 typval_T *argvars,
3786 int argcount,
3787 int base_included,
3788 char_u *name)
3789{
3790 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3791 {
3792 // Check that the argument types are OK for the types of the funcref.
3793 if (check_argument_types(funcexe->fe_check_type,
3794 argvars, argcount,
3795 base_included ? NULL : funcexe->fe_basetv,
3796 name) == FAIL)
3797 return FCERR_OTHER;
3798 }
3799 return FCERR_NONE;
3800}
3801
3802/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003803 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003804 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003805 * Return FAIL when the function can't be called, OK otherwise.
3806 * Also returns OK when an error was encountered while executing the function.
3807 */
3808 int
3809call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003810 char_u *funcname, // name of the function
3811 int len, // length of "name" or -1 to use strlen()
3812 typval_T *rettv, // return value goes here
3813 int argcount_in, // number of "argvars"
3814 typval_T *argvars_in, // vars for arguments, must have "argcount"
3815 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003816 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003817{
3818 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003819 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003820 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003821 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003822 char_u fname_buf[FLEN_FIXED + 1];
3823 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003824 char_u *fname = NULL;
3825 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003826 int argcount = argcount_in;
3827 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003828 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003829 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003830 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003831 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003832 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003833 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003834 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003835 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003836
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003837 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3838 // even when call_func() returns FAIL.
3839 rettv->v_type = VAR_UNKNOWN;
3840
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003841 if (partial != NULL)
3842 fp = partial->pt_func;
3843 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003844 fp = funcexe->fe_ufunc;
3845
3846 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003847 {
3848 // Make a copy of the name, if it comes from a funcref variable it
3849 // could be changed or deleted in the called function.
3850 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3851 if (name == NULL)
3852 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003853
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003854 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3855 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003856
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003857 if (funcexe->fe_doesrange != NULL)
3858 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003859
3860 if (partial != NULL)
3861 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003862 // When the function has a partial with a dict and there is a dict
3863 // argument, use the dict argument. That is backwards compatible.
3864 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003865 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003866 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003867 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003868 {
3869 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003870 {
3871 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3872 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003873 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003874 goto theend;
3875 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003876 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003877 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003878 for (i = 0; i < argcount_in; ++i)
3879 argv[i + argv_clear] = argvars_in[i];
3880 argvars = argv;
3881 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003882
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003883 if (funcexe->fe_check_type != NULL
3884 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003885 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003886 // Now funcexe->fe_check_type is missing the added arguments,
3887 // make a copy of the type with the correction.
3888 check_type = *funcexe->fe_check_type;
3889 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003890 check_type.tt_args = check_type_args;
3891 CLEAR_FIELD(check_type_args);
3892 for (i = 0; i < check_type.tt_argcount; ++i)
3893 check_type_args[i + partial->pt_argc] =
3894 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003895 check_type.tt_argcount += partial->pt_argc;
3896 check_type.tt_min_argcount += partial->pt_argc;
3897 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003898 }
3899 }
3900
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003901 if (error == FCERR_NONE)
3902 // check the argument types if possible
3903 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
3904 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003905
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003906 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003907 {
3908 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003909 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003910
Bram Moolenaar333894b2020-08-01 18:53:07 +02003911 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003912 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003913 {
3914 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003915 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003916 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003917
Bram Moolenaare38eab22019-12-05 21:50:01 +01003918 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003919 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003920 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003921
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003922 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003923 {
3924 /*
3925 * User defined function.
3926 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003927 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003928 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003929 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003930 if (fp != NULL && !is_global && in_vim9script()
3931 && func_requires_g_prefix(fp))
3932 // In Vim9 script g: is required to find a global
3933 // non-autoload function.
3934 fp = NULL;
3935 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003936
Bram Moolenaare38eab22019-12-05 21:50:01 +01003937 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003938 if (fp == NULL
3939 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003940 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003941 && !aborting())
3942 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003943 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003944 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003945 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003946 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003947 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3948 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003949 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003950 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003951 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003952 if (fp == NULL)
3953 {
3954 char_u *p = untrans_function_name(rfname);
3955
3956 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003957 // Don't do this if the name starts with "s:".
3958 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003959 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003960 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003961
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003962 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003963 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003964 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003965 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003966 int need_arg_check = FALSE;
3967 if (funcexe->fe_check_type == NULL)
3968 {
3969 funcexe->fe_check_type = fp->uf_func_type;
3970 need_arg_check = TRUE;
3971 }
3972
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003973 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003974 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003975 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003976 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003977 argv_clear, fp);
3978 need_arg_check = TRUE;
3979 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003980
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003981 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003982 {
3983 // Method call: base->Method()
3984 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003985 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003986 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003987 argvars = argv;
3988 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003989 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003990 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003991
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003992 // Check the argument types now that the function type and all
3993 // argument values are known, if not done above.
3994 if (need_arg_check)
3995 error = may_check_argument_types(funcexe, argvars, argcount,
3996 TRUE, (name != NULL) ? name : funcname);
3997 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
3998 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004000 }
4001 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004002 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004003 {
4004 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004005 * expr->method(): Find the method name in the table, call its
4006 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02004007 */
4008 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004009 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004010 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004011 else
4012 {
4013 /*
4014 * Find the function name in the table, call its implementation.
4015 */
4016 error = call_internal_func(fname, argcount, argvars, rettv);
4017 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02004018
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004019 /*
4020 * The function call (or "FuncUndefined" autocommand sequence) might
4021 * have been aborted by an error, an interrupt, or an explicitly thrown
4022 * exception that has not been caught so far. This situation can be
4023 * tested for by calling aborting(). For an error in an internal
4024 * function or for the "E132" error in call_user_func(), however, the
4025 * throw point at which the "force_abort" flag (temporarily reset by
4026 * emsg()) is normally updated has not been reached yet. We need to
4027 * update that flag first to make aborting() reliable.
4028 */
4029 update_force_abort();
4030 }
Bram Moolenaaref140542019-12-31 21:27:13 +01004031 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004032 ret = OK;
4033
Bram Moolenaar4c054e92019-11-10 00:13:50 +01004034theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004035 /*
4036 * Report an error unless the argument evaluation or function call has been
4037 * cancelled due to an aborting error, an interrupt, or an exception.
4038 */
4039 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004040 user_func_error(error, (name != NULL) ? name : funcname,
4041 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004042
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004043 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004044 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004045 clear_tv(&argv[--argv_clear + argv_base]);
4046
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004047 vim_free(tofree);
4048 vim_free(name);
4049
4050 return ret;
4051}
4052
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004053/*
4054 * Call a function without arguments, partial or dict.
4055 * This is like call_func() when the call is only "FuncName()".
4056 * To be used by "expr" options.
4057 * Returns NOTDONE when the function could not be found.
4058 */
4059 int
4060call_simple_func(
4061 char_u *funcname, // name of the function
zeertzjqc1ed7882024-08-01 22:46:54 +02004062 size_t len, // length of "name"
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004063 typval_T *rettv) // return value goes here
4064{
4065 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00004066 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004067 char_u fname_buf[FLEN_FIXED + 1];
4068 char_u *tofree = NULL;
4069 char_u *name;
4070 char_u *fname;
4071 char_u *rfname;
4072 int is_global = FALSE;
4073 ufunc_T *fp;
4074
4075 rettv->v_type = VAR_NUMBER; // default rettv is number zero
4076 rettv->vval.v_number = 0;
4077
4078 // Make a copy of the name, an option can be changed in the function.
4079 name = vim_strnsave(funcname, len);
4080 if (name == NULL)
4081 return ret;
4082
4083 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
4084
4085 // Skip "g:" before a function name.
4086 if (fname[0] == 'g' && fname[1] == ':')
4087 {
4088 is_global = TRUE;
4089 rfname = fname + 2;
4090 }
4091 else
4092 rfname = fname;
4093 fp = find_func(rfname, is_global);
4094 if (fp != NULL && !is_global && in_vim9script()
4095 && func_requires_g_prefix(fp))
4096 // In Vim9 script g: is required to find a global non-autoload
4097 // function.
4098 fp = NULL;
4099 if (fp == NULL)
4100 ret = NOTDONE;
4101 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
4102 error = FCERR_DELETED;
4103 else if (fp != NULL)
4104 {
4105 typval_T argvars[1];
4106 funcexe_T funcexe;
4107
4108 argvars[0].v_type = VAR_UNKNOWN;
4109 CLEAR_FIELD(funcexe);
4110 funcexe.fe_evaluate = TRUE;
4111
4112 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
4113 if (error == FCERR_NONE)
4114 ret = OK;
4115 }
4116
4117 user_func_error(error, name, FALSE);
4118 vim_free(tofree);
4119 vim_free(name);
4120
4121 return ret;
4122}
4123
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004124 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125printable_func_name(ufunc_T *fp)
4126{
4127 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4128}
4129
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004130/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004131 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4132 * TRUE. Otherwise return FALSE.
4133 */
4134 static int
4135function_list_modified(int prev_ht_changed)
4136{
4137 if (prev_ht_changed != func_hashtab.ht_changed)
4138 {
4139 emsg(_(e_function_list_was_modified));
4140 return TRUE;
4141 }
4142 return FALSE;
4143}
4144
4145/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004146 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004147 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004148 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004149list_func_head(ufunc_T *fp, int indent)
4150{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004151 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004152 int j;
4153
4154 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004155
4156 // a timer at the more prompt may have deleted the function
4157 if (function_list_modified(prev_ht_changed))
4158 return FAIL;
4159
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004160 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004161 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004162 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004163 msg_puts("def ");
4164 else
4165 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004167 msg_putchar('(');
4168 for (j = 0; j < fp->uf_args.ga_len; ++j)
4169 {
4170 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004171 msg_puts(", ");
4172 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173 if (fp->uf_arg_types != NULL)
4174 {
4175 char *tofree;
4176
4177 msg_puts(": ");
4178 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4179 vim_free(tofree);
4180 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004181 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4182 {
4183 msg_puts(" = ");
4184 msg_puts(((char **)(fp->uf_def_args.ga_data))
4185 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4186 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004187 }
4188 if (fp->uf_varargs)
4189 {
4190 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004191 msg_puts(", ");
4192 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004193 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 if (fp->uf_va_name != NULL)
4195 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004196 if (!fp->uf_varargs)
4197 {
4198 if (j)
4199 msg_puts(", ");
4200 msg_puts("...");
4201 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004203 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204 {
4205 char *tofree;
4206
4207 msg_puts(": ");
4208 msg_puts(type_name(fp->uf_va_type, &tofree));
4209 vim_free(tofree);
4210 }
4211 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004212 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004213
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004214 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004215 {
4216 if (fp->uf_ret_type != &t_void)
4217 {
4218 char *tofree;
4219
4220 msg_puts(": ");
4221 msg_puts(type_name(fp->uf_ret_type, &tofree));
4222 vim_free(tofree);
4223 }
4224 }
4225 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004226 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004227 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004228 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004229 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004230 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004231 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004232 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004233 msg_clr_eos();
4234 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004235 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004236
4237 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004238}
4239
4240/*
4241 * Get a function name, translating "<SID>" and "<SNR>".
4242 * Also handles a Funcref in a List or Dictionary.
4243 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004244 * Set "*is_global" to TRUE when the function must be global, unless
4245 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004246 * flags:
4247 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004248 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004249 * TFN_QUIET: be quiet
4250 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004251 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004252 * Advances "pp" to just after the function name (if no error).
4253 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004254 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004255trans_function_name(
4256 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004257 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004258 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004259 int flags)
4260{
4261 return trans_function_name_ext(pp, is_global, skip, flags,
4262 NULL, NULL, NULL, NULL);
4263}
4264
4265/*
4266 * trans_function_name() with extra arguments.
4267 * "fdp", "partial", "type" and "ufunc" can be NULL.
4268 */
4269 char_u *
4270trans_function_name_ext(
4271 char_u **pp,
4272 int *is_global,
4273 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004274 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004275 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004276 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004277 type_T **type, // return: type of funcref
4278 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004279{
4280 char_u *name = NULL;
4281 char_u *start;
4282 char_u *end;
4283 int lead;
4284 char_u sid_buf[20];
4285 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004287 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004288 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004289 int vim9script = in_vim9script();
4290 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004291
4292 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004293 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004294 start = *pp;
4295
Bram Moolenaare38eab22019-12-05 21:50:01 +01004296 // Check for hard coded <SNR>: already translated function ID (from a user
4297 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004298 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4299 && (*pp)[2] == (int)KE_SNR)
4300 {
4301 *pp += 3;
4302 len = get_id_len(pp) + 3;
4303 return vim_strnsave(start, len);
4304 }
4305
Bram Moolenaare38eab22019-12-05 21:50:01 +01004306 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4307 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004308 lead = eval_fname_script(start);
4309 if (lead > 2)
4310 start += lead;
4311
Bram Moolenaare38eab22019-12-05 21:50:01 +01004312 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004313 end = get_lval(start, NULL, &lv, FALSE, skip,
4314 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004315 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004316 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004317 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004318 {
4319 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004320 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004321 goto theend;
4322 }
4323 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4324 {
4325 /*
4326 * Report an invalid expression in braces, unless the expression
4327 * evaluation has been cancelled due to an aborting error, an
4328 * interrupt, or an exception.
4329 */
4330 if (!aborting())
4331 {
4332 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004333 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004334 }
4335 else
4336 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4337 goto theend;
4338 }
4339
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004340 if (lv.ll_ufunc != NULL)
4341 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004342 if (ufunc != NULL)
4343 *ufunc = lv.ll_ufunc;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004344 name = vim_strsave(lv.ll_ufunc->uf_name);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004345 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004346 goto theend;
4347 }
4348
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004349 if (lv.ll_tv != NULL)
4350 {
4351 if (fdp != NULL)
4352 {
4353 fdp->fd_dict = lv.ll_dict;
4354 fdp->fd_newkey = lv.ll_newkey;
4355 lv.ll_newkey = NULL;
4356 fdp->fd_di = lv.ll_di;
4357 }
4358 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4359 {
4360 name = vim_strsave(lv.ll_tv->vval.v_string);
4361 *pp = end;
4362 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004363 else if (lv.ll_tv->v_type == VAR_CLASS
4364 && lv.ll_tv->vval.v_class != NULL)
4365 {
4366 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4367 *pp = end;
4368 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004369 else if (lv.ll_tv->v_type == VAR_PARTIAL
4370 && lv.ll_tv->vval.v_partial != NULL)
4371 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004372 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004373 *pp = end;
4374 if (partial != NULL)
4375 *partial = lv.ll_tv->vval.v_partial;
4376 }
4377 else
4378 {
4379 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4380 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004381 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004382 else
4383 *pp = end;
4384 name = NULL;
4385 }
4386 goto theend;
4387 }
4388
4389 if (lv.ll_name == NULL)
4390 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004391 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004392 *pp = end;
4393 goto theend;
4394 }
4395
Bram Moolenaare38eab22019-12-05 21:50:01 +01004396 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004397 if (lv.ll_exp_name != NULL)
4398 {
4399 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004400 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004401 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004402 if (name == lv.ll_exp_name)
4403 name = NULL;
4404 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004405 else if (lv.ll_sid > 0)
4406 {
4407 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4408 int cc = *lv.ll_name_end;
4409
4410 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4411 *lv.ll_name_end = NUL;
4412 if (si->sn_autoload_prefix != NULL)
4413 {
4414 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4415 }
4416 else
4417 {
4418 sid_buf[0] = K_SPECIAL;
4419 sid_buf[1] = KS_EXTRA;
4420 sid_buf[2] = (int)KE_SNR;
4421 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004422 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004423 name = concat_str(sid_buf, lv.ll_name);
4424 }
4425 *lv.ll_name_end = cc;
4426 *pp = end;
4427 goto theend;
4428 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004429 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004430 {
4431 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004432 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004433 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004434 if (name == *pp)
4435 name = NULL;
4436 }
4437 if (name != NULL)
4438 {
4439 name = vim_strsave(name);
4440 *pp = end;
4441 if (STRNCMP(name, "<SNR>", 5) == 0)
4442 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004443 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004444 name[0] = K_SPECIAL;
4445 name[1] = KS_EXTRA;
4446 name[2] = (int)KE_SNR;
4447 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4448 }
4449 goto theend;
4450 }
4451
4452 if (lv.ll_exp_name != NULL)
4453 {
4454 len = (int)STRLEN(lv.ll_exp_name);
4455 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4456 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4457 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004458 // When there was "s:" already or the name expanded to get a
4459 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004460 lv.ll_name += 2;
4461 len -= 2;
4462 lead = 2;
4463 }
4464 }
4465 else
4466 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004467 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004468 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004469 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004470 if (lv.ll_name[0] == 'g')
4471 {
4472 if (is_global != NULL)
4473 {
4474 *is_global = TRUE;
4475 }
4476 else
4477 {
4478 // dropping "g:" without setting "is_global" won't work in
4479 // Vim9script, put it back later
4480 prefix_g = TRUE;
4481 extra = 2;
4482 }
4483 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004484 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004485 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004486 len = (int)(end - lv.ll_name);
4487 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004488 if (len <= 0)
4489 {
4490 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004491 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004492 goto theend;
4493 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004494
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004495 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004496 // starts with a lower case character: dict.func(). Or when in a class.
4497 vim9_local = ASCII_ISUPPER(*start) && vim9script
4498 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004500 /*
4501 * Copy the function name to allocated memory.
4502 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4503 * Accept <SNR>123_name() outside a script.
4504 */
4505 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004506 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004507 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004508 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004509 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004510 {
Kota Kato948a3892022-08-16 16:09:59 +01004511 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4512 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004513 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004514 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004515 goto theend;
4516 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004517 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004518 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004519 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004520 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004521 || eval_fname_sid(*pp))
4522 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004524 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004525 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004526 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004527 goto theend;
4528 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004529 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004530 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004531 extra = 3 + (int)STRLEN(sid_buf);
4532 else
4533 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004534 }
4535 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004536 // The function name must start with an upper case letter (unless it is a
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004537 // Vim9 class new() function or a Vim9 class private method or one of the
4538 // supported Vim9 object builtin functions)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004539 else if (!(flags & TFN_INT)
4540 && (builtin_function(lv.ll_name, len)
4541 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004542 && !((flags & TFN_IN_CLASS)
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004543 && (is_valid_builtin_obj_methodname(lv.ll_name)
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004544 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004545 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004546 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004547 : e_function_name_must_start_with_capital_or_s_str),
4548 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004549 goto theend;
4550 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004551 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004552 {
4553 char_u *cp = vim_strchr(lv.ll_name, ':');
4554
4555 if (cp != NULL && cp < end)
4556 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004557 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004558 goto theend;
4559 }
4560 }
4561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004563 if (name != NULL)
4564 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004565 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004566 {
4567 name[0] = K_SPECIAL;
4568 name[1] = KS_EXTRA;
4569 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004570 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004571 STRCPY(name + 3, sid_buf);
4572 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004573 else if (prefix_g)
4574 {
4575 name[0] = 'g';
4576 name[1] = ':';
4577 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4579 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004580 }
4581 *pp = end;
4582
4583theend:
4584 clear_lval(&lv);
4585 return name;
4586}
4587
4588/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004589 * Assuming "name" is the result of trans_function_name() and it was prefixed
4590 * to use the script-local name, return the unmodified name (points into
4591 * "name"). Otherwise return NULL.
4592 * This can be used to first search for a script-local function and fall back
4593 * to the global function if not found.
4594 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004595 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004596untrans_function_name(char_u *name)
4597{
4598 char_u *p;
4599
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004600 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004601 {
4602 p = vim_strchr(name, '_');
4603 if (p != NULL)
4604 return p + 1;
4605 }
4606 return NULL;
4607}
4608
4609/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004610 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4611 * current script ID and returns the expanded function name. The caller should
4612 * free the returned name. If not called from a script context or the function
4613 * name doesn't start with these prefixes, then returns NULL.
4614 * This doesn't check whether the script-local function exists or not.
4615 */
4616 char_u *
4617get_scriptlocal_funcname(char_u *funcname)
4618{
4619 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004620 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004621 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004622 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004623
4624 if (funcname == NULL)
4625 return NULL;
4626
4627 if (STRNCMP(funcname, "s:", 2) != 0
4628 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004629 {
4630 ufunc_T *ufunc;
4631
4632 // The function name does not have a script-local prefix. Try finding
4633 // it when in a Vim9 script and there is no "g:" prefix.
4634 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4635 return NULL;
4636 ufunc = find_func(funcname, FALSE);
4637 if (ufunc == NULL || func_is_global(ufunc)
4638 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4639 return NULL;
4640 ++p;
4641 off = 0;
4642 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004643 else
4644 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004645
4646 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4647 {
4648 emsg(_(e_using_sid_not_in_script_context));
4649 return NULL;
4650 }
4651 // Expand s: prefix into <SNR>nr_<name>
4652 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4653 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004654 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004655 if (newname == NULL)
4656 return NULL;
4657 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004658 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004659
4660 return newname;
4661}
4662
4663/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004664 * Return script-local "fname" with the 3-byte sequence replaced by
4665 * printable <SNR> in allocated memory.
4666 */
4667 char_u *
4668alloc_printable_func_name(char_u *fname)
4669{
4670 char_u *n = alloc(STRLEN(fname + 3) + 6);
4671
4672 if (n != NULL)
4673 {
4674 STRCPY(n, "<SNR>");
4675 STRCPY(n + 5, fname + 3);
4676 }
4677 return n;
4678}
4679
4680/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004681 * Call trans_function_name(), except that a lambda is returned as-is.
4682 * Returns the name in allocated memory.
4683 */
4684 char_u *
4685save_function_name(
4686 char_u **name,
4687 int *is_global,
4688 int skip,
4689 int flags,
4690 funcdict_T *fudi)
4691{
4692 char_u *p = *name;
4693 char_u *saved;
4694
4695 if (STRNCMP(p, "<lambda>", 8) == 0)
4696 {
4697 p += 8;
4698 (void)getdigits(&p);
4699 saved = vim_strnsave(*name, p - *name);
4700 if (fudi != NULL)
4701 CLEAR_POINTER(fudi);
4702 }
4703 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004704 saved = trans_function_name_ext(&p, is_global, skip,
4705 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004706 *name = p;
4707 return saved;
4708}
4709
4710/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004711 * List functions. When "regmatch" is NULL all of then.
4712 * Otherwise functions matching "regmatch".
4713 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004714 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004715list_functions(regmatch_T *regmatch)
4716{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004717 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004718 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004719 hashitem_T *hi;
4720
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004721 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004722 {
4723 if (!HASHITEM_EMPTY(hi))
4724 {
4725 ufunc_T *fp = HI2UF(hi);
4726
4727 --todo;
4728 if ((fp->uf_flags & FC_DEAD) == 0
4729 && (regmatch == NULL
4730 ? !message_filtered(fp->uf_name)
4731 && !func_name_refcount(fp->uf_name)
Keith Thompson184f71c2024-01-04 21:19:04 +01004732 : !SAFE_isdigit(*fp->uf_name)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004733 && vim_regexec(regmatch, fp->uf_name, 0)))
4734 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004735 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004736 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004737 if (function_list_modified(prev_ht_changed))
4738 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004739 }
4740 }
4741 }
4742}
4743
4744/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004745 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004746 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4747 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004748 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004749 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4750 * With CF_INTERFACE the function is define inside an interface, only the
4751 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004752 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004753 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004754 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004755define_function(
4756 exarg_T *eap,
4757 char_u *name_arg,
4758 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004759 int class_flags,
4760 ocmember_T *obj_members,
4761 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004762{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004763 int j;
4764 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004765 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004766 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004767 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004768 char_u *p;
4769 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004770 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004771 char_u *line_arg = NULL;
4772 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004773 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004774 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004775 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004776 garray_T newlines;
4777 int varargs = FALSE;
4778 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004780 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004781 int fp_allocated = FALSE;
4782 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004783 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004784 dictitem_T *v;
4785 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004786 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004787 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004788 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004789 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004790 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004791 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004792
4793 /*
4794 * ":function" without argument: list functions.
4795 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004796 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004797 {
4798 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004799 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004800 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004801 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004802 }
4803
4804 /*
4805 * ":function /pat": list functions matching pattern.
4806 */
4807 if (*eap->arg == '/')
4808 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004809 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004810 if (!eap->skip)
4811 {
4812 regmatch_T regmatch;
4813
4814 c = *p;
4815 *p = NUL;
4816 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4817 *p = c;
4818 if (regmatch.regprog != NULL)
4819 {
4820 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004821 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004822 vim_regfree(regmatch.regprog);
4823 }
4824 }
4825 if (*p == '/')
4826 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004827 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004828 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004829 }
4830
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004831 ga_init(&newargs);
4832 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004833 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004834 ga_init(&default_args);
4835
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004836 /*
4837 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004838 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004839 * "name" == func, "fudi.fd_dict" == NULL
4840 * dict.func new dictionary entry
4841 * "name" == NULL, "fudi.fd_dict" set,
4842 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4843 * dict.func existing dict entry with a Funcref
4844 * "name" == func, "fudi.fd_dict" set,
4845 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4846 * dict.func existing dict entry that's not a Funcref
4847 * "name" == NULL, "fudi.fd_dict" set,
4848 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4849 * s:func script-local function name
4850 * g:func global function name, same as "func"
4851 */
4852 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004853 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004854 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004855 // nested function, argument is (args).
4856 paren = TRUE;
4857 CLEAR_FIELD(fudi);
4858 }
4859 else
4860 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004861 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004862 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004863 if (p[0] == 's' && p[1] == ':')
4864 {
4865 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4866 return NULL;
4867 }
4868 p = to_name_end(p, TRUE);
4869 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4870 {
4871 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4872 eap->arg);
4873 return NULL;
4874 }
4875 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004876 }
4877
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004878 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004879 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004880 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004881 paren = (vim_strchr(p, '(') != NULL);
4882 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004883 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004884 /*
4885 * Return on an invalid expression in braces, unless the expression
4886 * evaluation has been cancelled due to an aborting error, an
4887 * interrupt, or an exception.
4888 */
4889 if (!aborting())
4890 {
4891 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004892 semsg(_(e_key_not_present_in_dictionary_str),
4893 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004894 vim_free(fudi.fd_newkey);
4895 return NULL;
4896 }
4897 else
4898 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004899 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004900
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004901 // For "export def FuncName()" in an autoload script the function name
4902 // is stored with the legacy autoload name "dir#script#FuncName" so
4903 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004904 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004905 {
4906 char_u *prefixed = may_prefix_autoload(name);
4907
4908 if (prefixed != NULL && prefixed != name)
4909 {
4910 vim_free(name);
4911 name = prefixed;
4912 }
4913 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004914 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004915 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004916 {
4917 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4918 goto ret_free;
4919 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004920 }
4921
Bram Moolenaare38eab22019-12-05 21:50:01 +01004922 // An error in a function call during evaluation of an expression in magic
4923 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004924 saved_did_emsg = did_emsg;
4925 did_emsg = FALSE;
4926
4927 /*
4928 * ":function func" with only function name: list function.
4929 */
4930 if (!paren)
4931 {
4932 if (!ends_excmd(*skipwhite(p)))
4933 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004934 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004935 goto ret_free;
4936 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004937 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004938 if (eap->nextcmd != NULL)
4939 *p = NUL;
4940 if (!eap->skip && !got_int)
4941 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004942 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004943 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4944 {
4945 char_u *up = untrans_function_name(name);
4946
4947 // With Vim9 script the name was made script-local, if not
4948 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004949 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004950 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004951 }
4952
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004953 if (fp != NULL)
4954 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004955 // Check no function was added or removed from a timer, e.g. at
4956 // the more prompt. "fp" may then be invalid.
4957 int prev_ht_changed = func_hashtab.ht_changed;
4958
4959 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004960 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004961 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4962 {
4963 if (FUNCLINE(fp, j) == NULL)
4964 continue;
4965 msg_putchar('\n');
4966 msg_outnum((long)(j + 1));
4967 if (j < 9)
4968 msg_putchar(' ');
4969 if (j < 99)
4970 msg_putchar(' ');
4971 if (function_list_modified(prev_ht_changed))
4972 break;
4973 msg_prt_line(FUNCLINE(fp, j), FALSE);
4974 out_flush(); // show a line at a time
4975 ui_breakcheck();
4976 }
4977 if (!got_int)
4978 {
4979 msg_putchar('\n');
4980 if (!function_list_modified(prev_ht_changed))
4981 {
4982 if (fp->uf_def_status != UF_NOT_COMPILED)
4983 msg_puts(" enddef");
4984 else
4985 msg_puts(" endfunction");
4986 }
4987 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004988 }
4989 }
4990 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004991 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004992 }
4993 goto ret_free;
4994 }
4995
4996 /*
4997 * ":function name(arg1, arg2)" Define function.
4998 */
4999 p = skipwhite(p);
5000 if (*p != '(')
5001 {
5002 if (!eap->skip)
5003 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005004 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005005 goto ret_free;
5006 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005007 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005008 if (vim_strchr(p, '(') != NULL)
5009 p = vim_strchr(p, '(');
5010 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005011
Bram Moolenaar4efd9942021-01-24 21:14:20 +01005012 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
5013 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01005014 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01005015 goto ret_free;
5016 }
5017
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02005018 // In Vim9 script only global functions can be redefined.
5019 if (vim9script && eap->forceit && !is_global)
5020 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005021 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02005022 goto ret_free;
5023 }
5024
Bram Moolenaar04935fb2022-01-08 16:19:22 +00005025 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005026
Bram Moolenaar04b12692020-05-04 23:24:44 +02005027 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005028 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005029 // Check the name of the function. Unless it's a dictionary function
5030 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005031 if (name != NULL)
5032 arg = name;
5033 else
5034 arg = fudi.fd_newkey;
5035 if (arg != NULL && (fudi.fd_di == NULL
5036 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
5037 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
5038 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00005039 char_u *name_base = arg;
5040 int i;
5041
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005042 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005043 {
5044 name_base = vim_strchr(arg, '_');
5045 if (name_base == NULL)
5046 name_base = arg + 3;
5047 else
5048 ++name_base;
5049 }
5050 for (i = 0; name_base[i] != NUL && (i == 0
5051 ? eval_isnamec1(name_base[i])
5052 : eval_isnamec(name_base[i])); ++i)
5053 ;
5054 if (name_base[i] != NUL)
zeertzjq6a04bf52024-03-16 09:39:06 +01005055 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005056 emsg_funcname(e_invalid_argument_str, arg);
zeertzjq6a04bf52024-03-16 09:39:06 +01005057 goto ret_free;
5058 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00005059
5060 // In Vim9 script a function cannot have the same name as a
5061 // variable.
5062 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00005063 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
5064 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00005065 + EVAL_VAR_NO_FUNC) == OK)
5066 {
5067 semsg(_(e_redefining_script_item_str), name_base);
5068 goto ret_free;
5069 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005070 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005071 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005072 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005073 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005074 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00005075 goto ret_free;
5076 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005077 }
5078
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005079 // This may get more lines and make the pointers into the first line
5080 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01005081 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005082 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00005083 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02005084 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005085 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00005086 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005087 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01005088 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005089
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005090 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005091 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005092 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005093 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005094 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005095 if (*p != ':')
5096 {
5097 semsg(_(e_no_white_space_allowed_before_colon_str), p);
5098 p = skipwhite(p);
5099 }
5100 else if (!IS_WHITE_OR_NUL(p[1]))
5101 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005102 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02005103 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005104 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005105 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005106 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01005107 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005108 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005109 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005111 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005112 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02005113 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005114 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005115 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02005116 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005117 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005118 else
5119 // find extra arguments "range", "dict", "abort" and "closure"
5120 for (;;)
5121 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01005122 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005123 p = skipwhite(p);
5124 if (STRNCMP(p, "range", 5) == 0)
5125 {
5126 flags |= FC_RANGE;
5127 p += 5;
5128 }
5129 else if (STRNCMP(p, "dict", 4) == 0)
5130 {
5131 flags |= FC_DICT;
5132 p += 4;
5133 }
5134 else if (STRNCMP(p, "abort", 5) == 0)
5135 {
5136 flags |= FC_ABORT;
5137 p += 5;
5138 }
5139 else if (STRNCMP(p, "closure", 7) == 0)
5140 {
5141 flags |= FC_CLOSURE;
5142 p += 7;
5143 if (current_funccal == NULL)
5144 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005145 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005146 name == NULL ? (char_u *)"" : name);
5147 goto erret;
5148 }
5149 }
5150 else
5151 break;
5152 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005153
Bram Moolenaare38eab22019-12-05 21:50:01 +01005154 // When there is a line break use what follows for the function body.
5155 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005156 if (*p == '\n')
5157 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005158 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005159 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5160 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005161 && !(VIM_ISWHITE(*whitep) && *p == '#'
5162 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005163 && !eap->skip
5164 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005165 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005166
5167 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005168 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5169 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005170 */
5171 if (KeyTyped)
5172 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005173 // Check if the function already exists, don't let the user type the
5174 // whole function before telling him it doesn't work! For a script we
5175 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005176 if (!eap->skip && !eap->forceit)
5177 {
5178 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005179 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005180 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005181 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005182 }
5183
5184 if (!eap->skip && did_emsg)
5185 goto erret;
5186
Bram Moolenaare38eab22019-12-05 21:50:01 +01005187 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005188 cmdline_row = msg_row;
5189 }
5190
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005191 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005192 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005193
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005194 // Do not define the function when getting the body fails and when
5195 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005196 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005197 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005198 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5199 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005200 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005201 goto erret;
5202
5203 /*
5204 * If there are no errors, add the function
5205 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005206 if (fudi.fd_dict != NULL)
5207 {
5208 char numbuf[20];
5209
5210 fp = NULL;
5211 if (fudi.fd_newkey == NULL && !eap->forceit)
5212 {
5213 emsg(_(e_dictionary_entry_already_exists));
5214 goto erret;
5215 }
5216 if (fudi.fd_di == NULL)
5217 {
5218 // Can't add a function to a locked dictionary
5219 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5220 goto erret;
5221 }
5222 // Can't change an existing function if it is locked
5223 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5224 goto erret;
5225
5226 // Give the function a sequential number. Can only be used with a
5227 // Funcref!
5228 vim_free(name);
5229 sprintf(numbuf, "%d", ++func_nr);
5230 name = vim_strsave((char_u *)numbuf);
5231 if (name == NULL)
5232 goto erret;
5233 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005234 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005235 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005236 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005237 char_u *find_name = name;
5238 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005239 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005240
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005241 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005242 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005243 var_conflict = TRUE;
5244
5245 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5246 {
5247 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5248
5249 if (si->sn_autoload_prefix != NULL)
5250 {
5251 if (is_export)
5252 {
5253 find_name = name + STRLEN(si->sn_autoload_prefix);
5254 v = find_var(find_name, &ht, TRUE);
5255 if (v != NULL)
5256 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005257 // Only check if the function already exists in the script,
5258 // global functions can be shadowed.
5259 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005260 }
5261 else
5262 {
5263 char_u *prefixed = may_prefix_autoload(name);
5264
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005265 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005266 {
5267 v = find_var(prefixed, &ht, TRUE);
5268 if (v != NULL)
5269 var_conflict = TRUE;
5270 vim_free(prefixed);
5271 }
5272 }
5273 }
5274 }
5275 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005276 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005277 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005278 goto erret;
5279 }
5280
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005281 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005282 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005283 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005284 char_u *uname = untrans_function_name(name);
5285
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005286 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005287 }
5288
5289 if (fp != NULL || import != NULL)
5290 {
5291 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005292
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005293 // Function can be replaced with "function!" and when sourcing the
5294 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005295 // A name that is used by an import can not be overruled.
5296 if (import != NULL
5297 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005298 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005299 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005300 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005301 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005302 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005303 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005304 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005305 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005306 goto erret;
5307 }
5308 if (fp->uf_calls > 0)
5309 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005310 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005311 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005312 goto erret;
5313 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005314 if (fp->uf_refcount > 1)
5315 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005316 // This function is referenced somewhere, don't redefine it but
5317 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005318 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005319 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005320 fp = NULL;
5321 overwrite = TRUE;
5322 }
5323 else
5324 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005325 char_u *exp_name = fp->uf_name_exp;
5326
5327 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005328 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005329 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005330 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005331 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005332 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005333#ifdef FEAT_PROFILE
5334 fp->uf_profiling = FALSE;
5335 fp->uf_prof_initialized = FALSE;
5336#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005337 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005338 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005339 }
5340 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005341
5342 if (fp == NULL)
5343 {
5344 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5345 {
5346 int slen, plen;
5347 char_u *scriptname;
5348
Bram Moolenaare38eab22019-12-05 21:50:01 +01005349 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005350 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005351 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005352 {
5353 scriptname = autoload_name(name);
5354 if (scriptname != NULL)
5355 {
5356 p = vim_strchr(scriptname, '/');
5357 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005358 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005359 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005360 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005361 j = OK;
5362 vim_free(scriptname);
5363 }
5364 }
5365 if (j == FAIL)
5366 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005367 linenr_T save_lnum = SOURCING_LNUM;
5368
5369 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005370 semsg(_(e_function_name_does_not_match_script_file_name_str),
5371 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005372 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005373 goto erret;
5374 }
5375 }
5376
Bram Moolenaare01e5212023-01-08 20:31:18 +00005377 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005378 if (fp == NULL)
5379 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005380 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005381
5382 if (fudi.fd_dict != NULL)
5383 {
5384 if (fudi.fd_di == NULL)
5385 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005386 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005387 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5388 if (fudi.fd_di == NULL)
5389 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005390 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005391 goto erret;
5392 }
5393 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5394 {
5395 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005396 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005397 goto erret;
5398 }
5399 }
5400 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005401 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005402 clear_tv(&fudi.fd_di->di_tv);
5403 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005404 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005405
Bram Moolenaare38eab22019-12-05 21:50:01 +01005406 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005407 flags |= FC_DICT;
5408 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005409 }
5410 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005411 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005412 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005413 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005414
5415 if (eap->cmdidx == CMD_def)
5416 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005417 int lnum_save = SOURCING_LNUM;
5418 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005419
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005420 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005421
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005422 // error messages are for the first function line
5423 SOURCING_LNUM = sourcing_lnum_top;
5424
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005425 // The function may use script variables from the context.
5426 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005427
h-eastb895b0f2023-09-24 15:46:31 +02005428 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5429 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005430 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005431 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005432 free_fp = fp_allocated;
5433 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005434 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005435 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005436
5437 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005438 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005439 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005440 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005441 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005442 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005443 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005444 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005445 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005446 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005447 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005448
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005449 if (fp_allocated)
5450 {
5451 // insert the new function in the function list
5452 set_ufunc_name(fp, name);
5453 if (overwrite)
5454 {
5455 hi = hash_find(&func_hashtab, name);
5456 hi->hi_key = UF2HIKEY(fp);
5457 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005458 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005459 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005460 {
5461 free_fp = TRUE;
5462 goto erret;
5463 }
5464 fp->uf_refcount = 1;
5465 }
5466
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005467 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005468 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005469 if ((flags & FC_CLOSURE) != 0)
5470 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005471 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005472 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005473 }
5474 else
5475 fp->uf_scoped = NULL;
5476
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005477#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005478 if (prof_def_func())
5479 func_do_profile(fp);
5480#endif
5481 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005482 if (sandbox)
5483 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005484 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005485 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005486 fp->uf_flags = flags;
5487 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005488 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005489 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005490 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005491 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005492 if (is_export)
5493 {
5494 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005495 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005496 is_export = FALSE;
5497 }
5498
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005499 if (eap->cmdidx == CMD_def)
5500 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005501 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5502 // :func does not use Vim9 script syntax, even in a Vim9 script file
5503 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005504
Yegappan Lakshmanan5715a722024-05-03 18:24:07 +02005505 // If test_override('defcompile' 1) is used, then compile the function now
5506 if (eap->cmdidx == CMD_def && override_defcompile)
5507 defcompile_function(fp, NULL);
5508
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005509 goto ret_free;
5510
5511erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005512 if (fp != NULL)
5513 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005514 // these were set to "newargs" and "default_args", which are cleared
5515 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005516 ga_init(&fp->uf_args);
5517 ga_init(&fp->uf_def_args);
5518 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005519errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005520 ga_clear_strings(&newargs);
5521 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005522 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005523 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005524 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005525 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005526 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01005527 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005528 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005529 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005530 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005531ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005532 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005533 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005534 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005535 if (name != name_arg)
5536 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005537 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005538 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005539
5540 return fp;
5541}
5542
5543/*
5544 * ":function"
5545 */
5546 void
5547ex_function(exarg_T *eap)
5548{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005549 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005550
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005551 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005552 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005553 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005554}
5555
LemonBoy48b7d052024-07-09 18:24:59 +02005556 int
5557get_func_arity(char_u *name, int *required, int *optional, int *varargs)
5558{
5559 ufunc_T *ufunc = NULL;
5560 int argcount = 0;
5561 int min_argcount = 0;
5562 int idx;
5563
5564 idx = find_internal_func(name);
5565 if (idx >= 0)
5566 {
5567 internal_func_get_argcount(idx, &argcount, &min_argcount);
5568 *varargs = FALSE;
5569 }
5570 else
5571 {
5572 char_u fname_buf[FLEN_FIXED + 1];
5573 char_u *tofree = NULL;
5574 funcerror_T error = FCERR_NONE;
5575 char_u *fname;
5576
5577 // May need to translate <SNR>123_ to K_SNR.
5578 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
5579 if (error == FCERR_NONE)
5580 ufunc = find_func(fname, FALSE);
5581 vim_free(tofree);
5582
5583 if (ufunc == NULL)
5584 return FAIL;
5585
5586 argcount = ufunc->uf_args.ga_len;
5587 min_argcount = ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len;
5588 *varargs = has_varargs(ufunc);
5589 }
5590
5591 *required = min_argcount;
5592 *optional = argcount - min_argcount;
5593
5594 return OK;
5595}
5596
Bram Moolenaar822ba242020-05-24 23:00:18 +02005597/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005598 * Find a function by name, including "<lambda>123".
5599 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005600 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005601 * Return NULL if not found.
5602 */
5603 ufunc_T *
5604find_func_by_name(char_u *name, compiletype_T *compile_type)
5605{
5606 char_u *arg = name;
5607 char_u *fname;
5608 ufunc_T *ufunc;
5609 int is_global = FALSE;
5610
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005611 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5612 {
5613 *compile_type = CT_PROFILE;
5614 arg = skipwhite(arg + 7);
5615 }
5616 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5617 {
5618 *compile_type = CT_DEBUG;
5619 arg = skipwhite(arg + 5);
5620 }
5621
5622 if (STRNCMP(arg, "<lambda>", 8) == 0)
5623 {
5624 arg += 8;
5625 (void)getdigits(&arg);
5626 fname = vim_strnsave(name, arg - name);
5627 }
5628 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005629 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005630 // First try finding a method in a class, trans_function_name() will
5631 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005632 ufunc = find_class_func(&arg);
5633 if (ufunc != NULL)
5634 return ufunc;
5635
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005636 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005637 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005638 NULL, NULL, NULL, &ufunc);
5639 if (ufunc != NULL)
5640 {
5641 vim_free(fname);
5642 return ufunc;
5643 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005644 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005645 if (fname == NULL)
5646 {
5647 semsg(_(e_invalid_argument_str), name);
5648 return NULL;
5649 }
5650 if (!ends_excmd2(name, arg))
5651 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005652 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005653 emsg(ex_errmsg(e_trailing_characters_str, arg));
5654 return NULL;
5655 }
5656
5657 ufunc = find_func(fname, is_global);
5658 if (ufunc == NULL)
5659 {
5660 char_u *p = untrans_function_name(fname);
5661
5662 if (p != NULL)
5663 // Try again without making it script-local.
5664 ufunc = find_func(p, FALSE);
5665 }
5666 vim_free(fname);
5667 if (ufunc == NULL)
5668 semsg(_(e_cannot_find_function_str), name);
5669 return ufunc;
5670}
5671
5672/*
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005673 * Compile the :def function "ufunc". If "cl" is not NULL, then compile the
5674 * class or object method "ufunc" in "cl".
5675 */
5676 void
5677defcompile_function(ufunc_T *ufunc, class_T *cl)
5678{
5679 compiletype_T compile_type = CT_NONE;
5680
5681 if (func_needs_compiling(ufunc, compile_type))
5682 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5683 else
5684 smsg(_("Function %s%s%s does not need compiling"),
5685 cl != NULL ? cl->class_name : (char_u *)"",
5686 cl != NULL ? (char_u *)"." : (char_u *)"",
5687 ufunc->uf_name);
5688}
5689
5690/*
5691 * Compile all the :def functions defined in the current script
5692 */
5693 static void
5694defcompile_funcs_in_script(void)
5695{
5696 long todo = (long)func_hashtab.ht_used;
5697 int changed = func_hashtab.ht_changed;
5698 hashitem_T *hi;
5699
5700 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5701 {
5702 if (!HASHITEM_EMPTY(hi))
5703 {
5704 --todo;
5705 ufunc_T *ufunc = HI2UF(hi);
5706 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5707 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5708 && (ufunc->uf_flags & FC_DEAD) == 0)
5709 {
5710 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5711
5712 if (func_hashtab.ht_changed != changed)
5713 {
5714 // a function has been added or removed, need to start
5715 // over
5716 todo = (long)func_hashtab.ht_used;
5717 changed = func_hashtab.ht_changed;
5718 hi = func_hashtab.ht_array;
5719 --hi;
5720 }
5721 }
5722 }
5723 }
5724}
5725
5726/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005727 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005728 * be compiled or the one specified by the argument.
5729 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005730 */
5731 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005732ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005733{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005734 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005735 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005736 typval_T tv;
5737
5738 if (is_class_name(eap->arg, &tv))
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005739 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005740 class_T *cl = tv.vval.v_class;
5741
5742 if (cl != NULL)
5743 defcompile_class(cl);
5744 }
5745 else
5746 {
5747 compiletype_T compile_type = CT_NONE;
5748 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
5749 if (ufunc != NULL)
5750 defcompile_function(ufunc, NULL);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005751 }
5752 }
5753 else
5754 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005755 defcompile_funcs_in_script();
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005756
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005757 // compile all the class defined in the current script
5758 defcompile_classes_in_script();
Bram Moolenaar822ba242020-05-24 23:00:18 +02005759 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005760}
5761
5762/*
5763 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5764 * Return 2 if "p" starts with "s:".
5765 * Return 0 otherwise.
5766 */
5767 int
5768eval_fname_script(char_u *p)
5769{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005770 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5771 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005772 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5773 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5774 return 5;
5775 if (p[0] == 's' && p[1] == ':')
5776 return 2;
5777 return 0;
5778}
5779
5780 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005781translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005782{
5783 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005784 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005785 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005786}
5787
5788/*
5789 * Return TRUE when "ufunc" has old-style "..." varargs
5790 * or named varargs "...name: type".
5791 */
5792 int
5793has_varargs(ufunc_T *ufunc)
5794{
5795 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005796}
5797
5798/*
5799 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005800 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005801 */
5802 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005803function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005804{
5805 char_u *nm = name;
5806 char_u *p;
5807 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005808 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005809 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005810
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005811 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5812 if (no_deref)
5813 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005814 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005815 nm = skipwhite(nm);
5816
Bram Moolenaare38eab22019-12-05 21:50:01 +01005817 // Only accept "funcname", "funcname ", "funcname (..." and
5818 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005819 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005820 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005821 vim_free(p);
5822 return n;
5823}
5824
Bram Moolenaar113e1072019-01-20 15:30:40 +01005825#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005826 char_u *
5827get_expanded_name(char_u *name, int check)
5828{
5829 char_u *nm = name;
5830 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005831 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005832
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005833 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005834
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005835 if (p != NULL && *nm == NUL
5836 && (!check || translated_function_exists(p, is_global)))
5837 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005838
5839 vim_free(p);
5840 return NULL;
5841}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005842#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005843
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005844/*
5845 * Function given to ExpandGeneric() to obtain the list of user defined
5846 * function names.
5847 */
5848 char_u *
5849get_user_func_name(expand_T *xp, int idx)
5850{
5851 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005852 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005853 static hashitem_T *hi;
5854 ufunc_T *fp;
5855
5856 if (idx == 0)
5857 {
5858 done = 0;
5859 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005860 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005861 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005862 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005863 {
5864 if (done++ > 0)
5865 ++hi;
5866 while (HASHITEM_EMPTY(hi))
5867 ++hi;
5868 fp = HI2UF(hi);
5869
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005870 // don't show dead, dict and lambda functions
5871 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005872 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005873 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005874
5875 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005876 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005877
5878 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005879 if (xp->xp_context != EXPAND_USER_FUNC
5880 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005881 {
5882 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005883 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005884 STRCAT(IObuff, ")");
5885 }
5886 return IObuff;
5887 }
5888 return NULL;
5889}
5890
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005891/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005892 * Make a copy of a function.
5893 * Intended to be used for a function defined on a base class that has a copy
5894 * on the child class.
5895 * The copy has uf_refcount set to one.
5896 * Returns NULL when out of memory.
5897 */
5898 ufunc_T *
5899copy_function(ufunc_T *fp)
5900{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005901 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005902 if (ufunc == NULL)
5903 return NULL;
5904
5905 // Most things can just be copied.
5906 *ufunc = *fp;
5907
5908 ufunc->uf_def_status = UF_TO_BE_COMPILED;
5909 ufunc->uf_dfunc_idx = 0;
5910 ufunc->uf_class = NULL;
5911
5912 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
5913 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
5914
5915 if (ufunc->uf_arg_types != NULL)
5916 {
5917 // "uf_arg_types" is an allocated array, make a copy.
5918 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
5919 if (at != NULL)
5920 {
5921 mch_memmove(at, ufunc->uf_arg_types,
5922 sizeof(type_T *) * ufunc->uf_args.ga_len);
5923 ufunc->uf_arg_types = at;
5924 }
5925 }
5926
5927 // TODO: how about the types themselves? they can be freed when the
5928 // original function is freed:
5929 // type_T **uf_arg_types;
5930 // type_T *uf_ret_type;
5931
Ernie Rael114ec812023-06-04 18:11:35 +01005932 // make uf_type_list empty
5933 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00005934
5935 // TODO: partial_T *uf_partial;
5936
5937 if (ufunc->uf_va_name != NULL)
5938 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
5939
5940 // TODO:
5941 // type_T *uf_va_type;
5942 // type_T *uf_func_type;
5943
5944 ufunc->uf_block_depth = 0;
5945 ufunc->uf_block_ids = NULL;
5946
5947 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
5948
5949 ufunc->uf_refcount = 1;
5950 ufunc->uf_name_exp = NULL;
5951 STRCPY(ufunc->uf_name, fp->uf_name);
5952
5953 return ufunc;
5954}
5955
5956/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005957 * ":delfunction {name}"
5958 */
5959 void
5960ex_delfunction(exarg_T *eap)
5961{
5962 ufunc_T *fp = NULL;
5963 char_u *p;
5964 char_u *name;
5965 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005966 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005967
5968 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005969 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
5970 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005971 vim_free(fudi.fd_newkey);
5972 if (name == NULL)
5973 {
5974 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005975 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005976 return;
5977 }
5978 if (!ends_excmd(*skipwhite(p)))
5979 {
5980 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005981 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005982 return;
5983 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005984 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005985 if (eap->nextcmd != NULL)
5986 *p = NUL;
5987
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005988 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005989 {
5990 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005991 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005992 vim_free(name);
5993 return;
5994 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005995 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005996 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005997 vim_free(name);
5998
5999 if (!eap->skip)
6000 {
6001 if (fp == NULL)
6002 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02006003 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00006004 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006005 return;
6006 }
6007 if (fp->uf_calls > 0)
6008 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006009 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006010 return;
6011 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006012 if (fp->uf_flags & FC_VIM9)
6013 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006014 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006015 return;
6016 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006017
6018 if (fudi.fd_dict != NULL)
6019 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006020 // Delete the dict item that refers to the function, it will
6021 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00006022 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006023 }
6024 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006025 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006026 // A normal function (not a numbered function or lambda) has a
6027 // refcount of 1 for the entry in the hashtable. When deleting
6028 // it and the refcount is more than one, it should be kept.
6029 // A numbered function and lambda should be kept if the refcount is
6030 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006031 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006032 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006033 // Function is still referenced somewhere. Don't free it but
6034 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006035 if (func_remove(fp))
6036 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006037 }
6038 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01006039 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006040 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006041 }
6042}
6043
6044/*
6045 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006046 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006047 */
6048 void
6049func_unref(char_u *name)
6050{
Bram Moolenaar97baee82016-07-26 20:46:08 +02006051 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006052
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006053 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006054 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006055 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006056 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006057 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006058#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006059 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006060#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01006061 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006062 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006063 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006064}
6065
6066/*
6067 * Unreference a Function: decrement the reference count and free it when it
6068 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006069 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006070 */
6071 void
6072func_ptr_unref(ufunc_T *fp)
6073{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006074 if (fp != NULL && (--fp->uf_refcount <= 0
6075 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
6076 && fp->uf_partial->pt_refcount <= 1
6077 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02006078 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006079 // Only delete it when it's not being used. Otherwise it's done
6080 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02006081 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01006082 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006083 }
6084}
6085
6086/*
6087 * Count a reference to a Function.
6088 */
6089 void
6090func_ref(char_u *name)
6091{
6092 ufunc_T *fp;
6093
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006094 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006095 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006096 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006097 if (fp != NULL)
6098 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006099 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01006100 // Only give an error for a numbered function.
6101 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01006102 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006103}
6104
6105/*
6106 * Count a reference to a Function.
6107 */
6108 void
6109func_ptr_ref(ufunc_T *fp)
6110{
6111 if (fp != NULL)
6112 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006113}
6114
6115/*
6116 * Return TRUE if items in "fc" do not have "copyID". That means they are not
6117 * referenced from anywhere that is in use.
6118 */
6119 static int
6120can_free_funccal(funccall_T *fc, int copyID)
6121{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006122 return (fc->fc_l_varlist.lv_copyID != copyID
6123 && fc->fc_l_vars.dv_copyID != copyID
6124 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006125 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006126}
6127
6128/*
6129 * ":return [expr]"
6130 */
6131 void
6132ex_return(exarg_T *eap)
6133{
6134 char_u *arg = eap->arg;
6135 typval_T rettv;
6136 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006137 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006138
6139 if (current_funccal == NULL)
6140 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006141 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006142 return;
6143 }
6144
Bram Moolenaar844fb642021-10-23 13:32:30 +01006145 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006146 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
6147
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006148 if (eap->skip)
6149 ++emsg_skip;
6150
6151 eap->nextcmd = NULL;
6152 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006153 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006154 {
6155 if (!eap->skip)
6156 returning = do_return(eap, FALSE, TRUE, &rettv);
6157 else
6158 clear_tv(&rettv);
6159 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01006160 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006161 else if (!eap->skip)
6162 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006163 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01006164 update_force_abort();
6165
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006166 /*
6167 * Return unless the expression evaluation has been cancelled due to an
6168 * aborting error, an interrupt, or an exception.
6169 */
6170 if (!aborting())
6171 returning = do_return(eap, FALSE, TRUE, NULL);
6172 }
6173
Bram Moolenaare38eab22019-12-05 21:50:01 +01006174 // When skipping or the return gets pending, advance to the next command
6175 // in this line (!returning). Otherwise, ignore the rest of the line.
6176 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006177 if (returning)
6178 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006179 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02006180 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006181
6182 if (eap->skip)
6183 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02006184 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006185}
6186
zeertzjq5299c092023-04-12 20:48:16 +01006187/*
6188 * Lower level implementation of "call". Only called when not skipping.
6189 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006190 static int
6191ex_call_inner(
6192 exarg_T *eap,
6193 char_u *name,
6194 char_u **arg,
6195 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006196 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006197 evalarg_T *evalarg)
6198{
6199 linenr_T lnum;
6200 int doesrange;
6201 typval_T rettv;
6202 int failed = FALSE;
6203
zeertzjq5299c092023-04-12 20:48:16 +01006204 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006205 for ( ; lnum <= eap->line2; ++lnum)
6206 {
6207 funcexe_T funcexe;
6208
zeertzjq5299c092023-04-12 20:48:16 +01006209 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006210 {
6211 if (lnum > curbuf->b_ml.ml_line_count)
6212 {
6213 // If the function deleted lines or switched to another buffer
6214 // the line number may become invalid.
6215 emsg(_(e_invalid_range));
6216 break;
6217 }
6218 curwin->w_cursor.lnum = lnum;
6219 curwin->w_cursor.col = 0;
6220 curwin->w_cursor.coladd = 0;
6221 }
6222 *arg = startarg;
6223
6224 funcexe = *funcexe_init;
6225 funcexe.fe_doesrange = &doesrange;
6226 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6227 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6228 {
6229 failed = TRUE;
6230 break;
6231 }
6232 if (has_watchexpr())
6233 dbg_check_breakpoint(eap);
6234
6235 // Handle a function returning a Funcref, Dictionary or List.
6236 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006237 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006238 {
6239 failed = TRUE;
6240 break;
6241 }
6242
6243 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006244 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006245 break;
6246
6247 // Stop when immediately aborting on error, or when an interrupt
6248 // occurred or an exception was thrown but not caught.
6249 // get_func_tv() returned OK, so that the check for trailing
6250 // characters below is executed.
6251 if (aborting())
6252 break;
6253 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006254 return failed;
6255}
6256
6257/*
6258 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6259 * Returns FAIL or OK.
6260 */
6261 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006262ex_defer_inner(
6263 char_u *name,
6264 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006265 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006266 partial_T *partial,
6267 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006268{
6269 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006270 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006271 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006272 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006273
6274 if (current_funccal == NULL)
6275 {
6276 semsg(_(e_str_not_inside_function), "defer");
6277 return FAIL;
6278 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006279 if (partial != NULL)
6280 {
6281 if (partial->pt_dict != NULL)
6282 {
6283 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6284 return FAIL;
6285 }
6286 if (partial->pt_argc > 0)
6287 {
6288 int i;
6289
6290 partial_argc = partial->pt_argc;
6291 for (i = 0; i < partial_argc; ++i)
6292 copy_tv(&partial->pt_argv[i], &argvars[i]);
6293 }
6294 }
Ernie Raelb077b582023-12-14 20:11:44 +01006295 int is_builtin = builtin_function(name, -1);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006296 r = get_func_arguments(arg, evalarg, FALSE,
Ernie Raelb077b582023-12-14 20:11:44 +01006297 argvars + partial_argc, &argcount, is_builtin);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006298 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006299
6300 if (r == OK)
6301 {
6302 if (type != NULL)
6303 {
6304 // Check that the arguments are OK for the types of the funcref.
6305 r = check_argument_types(type, argvars, argcount, NULL, name);
6306 }
Ernie Raelb077b582023-12-14 20:11:44 +01006307 else if (is_builtin)
Bram Moolenaar16900322022-09-08 19:51:45 +01006308 {
6309 int idx = find_internal_func(name);
6310
6311 if (idx < 0)
6312 {
6313 emsg_funcname(e_unknown_function_str, name);
6314 r = FAIL;
6315 }
6316 else if (check_internal_func(idx, argcount) == -1)
6317 r = FAIL;
6318 }
6319 else
6320 {
6321 ufunc_T *ufunc = find_func(name, FALSE);
6322
6323 // we tolerate an unknown function here, it might be defined later
6324 if (ufunc != NULL)
6325 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006326 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006327 if (error != FCERR_UNKNOWN)
6328 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006329 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006330 r = FAIL;
6331 }
6332 }
6333 }
6334 }
6335
Bram Moolenaar86d87252022-09-05 21:21:25 +01006336 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006337 {
6338 while (--argcount >= 0)
6339 clear_tv(&argvars[argcount]);
6340 return FAIL;
6341 }
6342 return add_defer(name, argcount, argvars);
6343}
6344
6345/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006346 * Return TRUE if currently inside a function call.
6347 * Give an error message and return FALSE when not.
6348 */
6349 int
6350can_add_defer(void)
6351{
6352 if (!in_def_function() && get_current_funccal() == NULL)
6353 {
6354 semsg(_(e_str_not_inside_function), "defer");
6355 return FALSE;
6356 }
6357 return TRUE;
6358}
6359
6360/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006361 * Add a deferred call for "name" with arguments "argvars[argcount]".
6362 * Consumes "argvars[]".
6363 * Caller must check that in_def_function() returns TRUE or current_funccal is
6364 * not NULL.
6365 * Returns OK or FAIL.
6366 */
6367 int
6368add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6369{
6370 char_u *saved_name = vim_strsave(name);
6371 int argcount = argcount_arg;
6372 defer_T *dr;
6373 int ret = FAIL;
6374
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006375 if (saved_name == NULL)
6376 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006377 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006378 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006379 if (add_defer_function(saved_name, argcount, argvars) == OK)
6380 argcount = 0;
6381 }
6382 else
6383 {
6384 if (current_funccal->fc_defer.ga_itemsize == 0)
6385 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6386 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6387 goto theend;
6388 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6389 + current_funccal->fc_defer.ga_len++;
6390 dr->dr_name = saved_name;
6391 dr->dr_argcount = argcount;
6392 while (argcount > 0)
6393 {
6394 --argcount;
6395 dr->dr_argvars[argcount] = argvars[argcount];
6396 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006397 }
6398 ret = OK;
6399
6400theend:
6401 while (--argcount >= 0)
6402 clear_tv(&argvars[argcount]);
6403 return ret;
6404}
6405
6406/*
6407 * Invoked after a functions has finished: invoke ":defer" functions.
6408 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006409 static void
6410handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006411{
6412 int idx;
6413
Bram Moolenaar58779852022-09-06 18:31:14 +01006414 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006415 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006416 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006417
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006418 if (dr->dr_name == NULL)
6419 // already being called, can happen if function does ":qa"
6420 continue;
6421
6422 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006423 CLEAR_FIELD(funcexe);
6424 funcexe.fe_evaluate = TRUE;
6425
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006426 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006427 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006428
6429 char_u *name = dr->dr_name;
6430 dr->dr_name = NULL;
6431
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006432 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006433 // first statement in the function will be executed (because of the
6434 // exception). So save and restore the try/catch/throw exception
6435 // state.
6436 exception_state_T estate;
6437 exception_state_save(&estate);
6438 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006439
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006440 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6441
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006442 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006443
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006444 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006445 vim_free(name);
6446 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006447 clear_tv(&dr->dr_argvars[i]);
6448 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006449 ga_clear(&funccal->fc_defer);
6450}
6451
zeertzjq960cf912023-04-18 21:52:54 +01006452 static void
6453invoke_funccall_defer(funccall_T *fc)
6454{
6455 if (fc->fc_ectx != NULL)
6456 {
6457 // :def function
6458 unwind_def_callstack(fc->fc_ectx);
6459 may_invoke_defer_funcs(fc->fc_ectx);
6460 }
6461 else
6462 {
6463 // legacy function
6464 handle_defer_one(fc);
6465 }
6466}
6467
Bram Moolenaar58779852022-09-06 18:31:14 +01006468/*
6469 * Called when exiting: call all defer functions.
6470 */
6471 void
6472invoke_all_defer(void)
6473{
zeertzjq1be4b812023-04-19 14:21:24 +01006474 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6475 invoke_funccall_defer(fc);
6476
zeertzjq960cf912023-04-18 21:52:54 +01006477 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6478 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6479 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006480}
6481
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006482/*
6483 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006484 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006485 */
6486 void
6487ex_call(exarg_T *eap)
6488{
6489 char_u *arg = eap->arg;
6490 char_u *startarg;
6491 char_u *name;
6492 char_u *tofree;
6493 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006494 int failed = FALSE;
6495 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006496 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006497 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006498 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006499 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006500 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006501 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006502
Bram Moolenaare6b53242020-07-01 17:28:33 +02006503 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006504 if (eap->skip)
6505 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006506 typval_T rettv;
6507
Bram Moolenaare38eab22019-12-05 21:50:01 +01006508 // trans_function_name() doesn't work well when skipping, use eval0()
6509 // instead to skip to any following command, e.g. for:
6510 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006511 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006512 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006513 clear_tv(&rettv);
6514 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006515 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006516 return;
6517 }
6518
zeertzjq5299c092023-04-12 20:48:16 +01006519 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006520 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006521 if (fudi.fd_newkey != NULL)
6522 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006523 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006524 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006525 vim_free(fudi.fd_newkey);
6526 }
6527 if (tofree == NULL)
6528 return;
6529
Bram Moolenaare38eab22019-12-05 21:50:01 +01006530 // Increase refcount on dictionary, it could get deleted when evaluating
6531 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006532 if (fudi.fd_dict != NULL)
6533 ++fudi.fd_dict->dv_refcount;
6534
Bram Moolenaare38eab22019-12-05 21:50:01 +01006535 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6536 // contents. For VAR_PARTIAL get its partial, unless we already have one
6537 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006538 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006539 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006540 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006541 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006542
Bram Moolenaare38eab22019-12-05 21:50:01 +01006543 // Skip white space to allow ":call func ()". Not good, but required for
6544 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006545 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006546 if (*startarg != '(')
6547 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006548 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006549 goto end;
6550 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006551 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006552 {
6553 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6554 goto end;
6555 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006556
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006557 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006558 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006559 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006560 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006561 }
6562 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006563 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006564 funcexe_T funcexe;
6565
Bram Moolenaara80faa82020-04-12 19:37:17 +02006566 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006567 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006568 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006569 funcexe.fe_partial = partial;
6570 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006571 funcexe.fe_firstline = eap->line1;
6572 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006573 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006574 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006575 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006576 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006577
Bram Moolenaar1d341892021-09-18 15:25:52 +02006578 // When inside :try we need to check for following "| catch" or "| endtry".
6579 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006580 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006581 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006582 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006583 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006584 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006585 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006586 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006587 {
6588 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006589 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006590 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006591 }
6592 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006593 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006594 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006595 // Must be after using "arg", it may point into memory cleared here.
6596 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006597
6598end:
6599 dict_unref(fudi.fd_dict);
6600 vim_free(tofree);
6601}
6602
6603/*
6604 * Return from a function. Possibly makes the return pending. Also called
6605 * for a pending return at the ":endtry" or after returning from an extra
6606 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6607 * when called due to a ":return" command. "rettv" may point to a typval_T
6608 * with the return rettv. Returns TRUE when the return can be carried out,
6609 * FALSE when the return gets pending.
6610 */
6611 int
6612do_return(
6613 exarg_T *eap,
6614 int reanimate,
6615 int is_cmd,
6616 void *rettv)
6617{
6618 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006619 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006620
6621 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006622 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006623 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006624
6625 /*
6626 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6627 * not in its finally clause (which then is to be executed next) is found.
6628 * In this case, make the ":return" pending for execution at the ":endtry".
6629 * Otherwise, return normally.
6630 */
6631 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6632 if (idx >= 0)
6633 {
6634 cstack->cs_pending[idx] = CSTP_RETURN;
6635
6636 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006637 // A pending return again gets pending. "rettv" points to an
6638 // allocated variable with the rettv of the original ":return"'s
6639 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006640 cstack->cs_rettv[idx] = rettv;
6641 else
6642 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006643 // When undoing a return in order to make it pending, get the stored
6644 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006645 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006646 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006647
6648 if (rettv != NULL)
6649 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006650 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006651 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6652 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6653 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006654 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006655 }
6656 else
6657 cstack->cs_rettv[idx] = NULL;
6658
6659 if (reanimate)
6660 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006661 // The pending return value could be overwritten by a ":return"
6662 // without argument in a finally clause; reset the default
6663 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006664 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6665 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006666 }
6667 }
6668 report_make_pending(CSTP_RETURN, rettv);
6669 }
6670 else
6671 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006672 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006673
Bram Moolenaare38eab22019-12-05 21:50:01 +01006674 // If the return is carried out now, store the return value. For
6675 // a return immediately after reanimation, the value is already
6676 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006677 if (!reanimate && rettv != NULL)
6678 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006679 clear_tv(current_funccal->fc_rettv);
6680 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006681 if (!is_cmd)
6682 vim_free(rettv);
6683 }
6684 }
6685
6686 return idx < 0;
6687}
6688
6689/*
6690 * Free the variable with a pending return value.
6691 */
6692 void
6693discard_pending_return(void *rettv)
6694{
6695 free_tv((typval_T *)rettv);
6696}
6697
6698/*
6699 * Generate a return command for producing the value of "rettv". The result
6700 * is an allocated string. Used by report_pending() for verbose messages.
6701 */
6702 char_u *
6703get_return_cmd(void *rettv)
6704{
6705 char_u *s = NULL;
6706 char_u *tofree = NULL;
6707 char_u numbuf[NUMBUFLEN];
6708
6709 if (rettv != NULL)
6710 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6711 if (s == NULL)
6712 s = (char_u *)"";
6713
6714 STRCPY(IObuff, ":return ");
6715 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6716 if (STRLEN(s) + 8 >= IOSIZE)
6717 STRCPY(IObuff + IOSIZE - 4, "...");
6718 vim_free(tofree);
6719 return vim_strsave(IObuff);
6720}
6721
6722/*
6723 * Get next function line.
6724 * Called by do_cmdline() to get the next line.
6725 * Returns allocated string, or NULL for end of function.
6726 */
6727 char_u *
6728get_func_line(
6729 int c UNUSED,
6730 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006731 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006732 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006733{
6734 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006735 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006736 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006737 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006738
Bram Moolenaare38eab22019-12-05 21:50:01 +01006739 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006740 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006741 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006742 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006743 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006744 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006745 }
6746#ifdef FEAT_PROFILE
6747 if (do_profiling == PROF_YES)
6748 func_line_end(cookie);
6749#endif
6750
6751 gap = &fp->uf_lines;
6752 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006753 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006754 retval = NULL;
6755 else
6756 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006757 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006758 while (fcp->fc_linenr < gap->ga_len
6759 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6760 ++fcp->fc_linenr;
6761 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006762 retval = NULL;
6763 else
6764 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006765 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6766 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006767#ifdef FEAT_PROFILE
6768 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006769 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006770#endif
6771 }
6772 }
6773
Bram Moolenaare38eab22019-12-05 21:50:01 +01006774 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006775 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006776 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006777 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006778 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006779 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006780 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006781 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006782 }
6783
6784 return retval;
6785}
6786
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006787/*
6788 * Return TRUE if the currently active function should be ended, because a
6789 * return was encountered or an error occurred. Used inside a ":while".
6790 */
6791 int
6792func_has_ended(void *cookie)
6793{
6794 funccall_T *fcp = (funccall_T *)cookie;
6795
Bram Moolenaare38eab22019-12-05 21:50:01 +01006796 // Ignore the "abort" flag if the abortion behavior has been changed due to
6797 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006798 return (((fcp->fc_func->uf_flags & FC_ABORT)
6799 && did_emsg && !aborted_in_try())
6800 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006801}
6802
6803/*
6804 * return TRUE if cookie indicates a function which "abort"s on errors.
6805 */
6806 int
6807func_has_abort(
6808 void *cookie)
6809{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006810 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006811}
6812
6813
6814/*
6815 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6816 * Don't do this when "Func" is already a partial that was bound
6817 * explicitly (pt_auto is FALSE).
6818 * Changes "rettv" in-place.
6819 * Returns the updated "selfdict_in".
6820 */
6821 dict_T *
6822make_partial(dict_T *selfdict_in, typval_T *rettv)
6823{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006824 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006825 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006826 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006827 dict_T *selfdict = selfdict_in;
6828
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006829 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6830 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006831 fp = rettv->vval.v_partial->pt_func;
6832 else
6833 {
6834 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006835 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006836 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006837 if (fname == NULL)
6838 {
6839 // There is no point binding a dict to a NULL function, just create
6840 // a function reference.
6841 rettv->v_type = VAR_FUNC;
6842 rettv->vval.v_string = NULL;
6843 }
6844 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006845 {
6846 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006847 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006848
6849 // Translate "s:func" to the stored function name.
6850 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6851 fp = find_func(fname, FALSE);
6852 vim_free(tofree);
6853 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006854 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006855
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006856 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006857 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006858 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006859
6860 if (pt != NULL)
6861 {
6862 pt->pt_refcount = 1;
6863 pt->pt_dict = selfdict;
6864 pt->pt_auto = TRUE;
6865 selfdict = NULL;
6866 if (rettv->v_type == VAR_FUNC)
6867 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006868 // Just a function: Take over the function name and use
6869 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006870 pt->pt_name = rettv->vval.v_string;
6871 }
6872 else
6873 {
6874 partial_T *ret_pt = rettv->vval.v_partial;
6875 int i;
6876
Bram Moolenaare38eab22019-12-05 21:50:01 +01006877 // Partial: copy the function name, use selfdict and copy
6878 // args. Can't take over name or args, the partial might
6879 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006880 if (ret_pt->pt_name != NULL)
6881 {
6882 pt->pt_name = vim_strsave(ret_pt->pt_name);
6883 func_ref(pt->pt_name);
6884 }
6885 else
6886 {
6887 pt->pt_func = ret_pt->pt_func;
6888 func_ptr_ref(pt->pt_func);
6889 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006890 if (ret_pt->pt_argc > 0)
6891 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006892 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006893 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006894 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006895 pt->pt_argc = 0;
6896 else
6897 {
6898 pt->pt_argc = ret_pt->pt_argc;
6899 for (i = 0; i < pt->pt_argc; i++)
6900 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6901 }
6902 }
6903 partial_unref(ret_pt);
6904 }
6905 rettv->v_type = VAR_PARTIAL;
6906 rettv->vval.v_partial = pt;
6907 }
6908 }
6909 return selfdict;
6910}
6911
6912/*
6913 * Return the name of the executed function.
6914 */
6915 char_u *
6916func_name(void *cookie)
6917{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006918 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006919}
6920
6921/*
6922 * Return the address holding the next breakpoint line for a funccall cookie.
6923 */
6924 linenr_T *
6925func_breakpoint(void *cookie)
6926{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006927 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006928}
6929
6930/*
6931 * Return the address holding the debug tick for a funccall cookie.
6932 */
6933 int *
6934func_dbg_tick(void *cookie)
6935{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006936 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006937}
6938
6939/*
6940 * Return the nesting level for a funccall cookie.
6941 */
6942 int
6943func_level(void *cookie)
6944{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006945 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006946}
6947
6948/*
6949 * Return TRUE when a function was ended by a ":return" command.
6950 */
6951 int
6952current_func_returned(void)
6953{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006954 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006955}
6956
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006957 int
6958free_unref_funccal(int copyID, int testing)
6959{
6960 int did_free = FALSE;
6961 int did_free_funccal = FALSE;
6962 funccall_T *fc, **pfc;
6963
6964 for (pfc = &previous_funccal; *pfc != NULL; )
6965 {
6966 if (can_free_funccal(*pfc, copyID))
6967 {
6968 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006969 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006970 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006971 did_free = TRUE;
6972 did_free_funccal = TRUE;
6973 }
6974 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006975 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006976 }
6977 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006978 // When a funccal was freed some more items might be garbage
6979 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006980 (void)garbage_collect(testing);
6981
6982 return did_free;
6983}
6984
6985/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006986 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006987 */
6988 static funccall_T *
6989get_funccal(void)
6990{
6991 int i;
6992 funccall_T *funccal;
6993 funccall_T *temp_funccal;
6994
6995 funccal = current_funccal;
6996 if (debug_backtrace_level > 0)
6997 {
6998 for (i = 0; i < debug_backtrace_level; i++)
6999 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007000 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007001 if (temp_funccal)
7002 funccal = temp_funccal;
7003 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01007004 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007005 debug_backtrace_level = i;
7006 }
7007 }
7008 return funccal;
7009}
7010
7011/*
7012 * Return the hashtable used for local variables in the current funccal.
7013 * Return NULL if there is no current funccal.
7014 */
7015 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007016get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007017{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007018 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007019 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007020 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007021}
7022
7023/*
7024 * Return the l: scope variable.
7025 * Return NULL if there is no current funccal.
7026 */
7027 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007028get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007029{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007030 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007031 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007032 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007033}
7034
7035/*
7036 * Return the hashtable used for argument in the current funccal.
7037 * Return NULL if there is no current funccal.
7038 */
7039 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007040get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007041{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007042 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007043 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007044 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007045}
7046
7047/*
7048 * Return the a: scope variable.
7049 * Return NULL if there is no current funccal.
7050 */
7051 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007052get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007053{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007054 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007055 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007056 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007057}
7058
7059/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007060 * List function variables, if there is a function.
7061 */
7062 void
7063list_func_vars(int *first)
7064{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007065 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
7066 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01007067 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007068}
7069
7070/*
7071 * If "ht" is the hashtable for local variables in the current funccal, return
7072 * the dict that contains it.
7073 * Otherwise return NULL.
7074 */
7075 dict_T *
7076get_current_funccal_dict(hashtab_T *ht)
7077{
7078 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01007079 && ht == &current_funccal->fc_l_vars.dv_hashtab)
7080 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007081 return NULL;
7082}
7083
7084/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007085 * Search hashitem in parent scope.
7086 */
7087 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007088find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007089{
7090 funccall_T *old_current_funccal = current_funccal;
7091 hashtab_T *ht;
7092 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007093 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007094
Bram Moolenaarca16c602022-09-06 18:57:08 +01007095 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007096 return NULL;
7097
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02007098 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01007099 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02007100 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007101 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007102 ht = find_var_ht(name, &varname);
7103 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02007104 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007105 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02007106 if (!HASHITEM_EMPTY(hi))
7107 {
7108 *pht = ht;
7109 break;
7110 }
7111 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007112 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02007113 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007114 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007115 }
7116 current_funccal = old_current_funccal;
7117
7118 return hi;
7119}
7120
7121/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007122 * Search variable in parent scope.
7123 */
7124 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007125find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007126{
7127 dictitem_T *v = NULL;
7128 funccall_T *old_current_funccal = current_funccal;
7129 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007130 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007131
Bram Moolenaarca16c602022-09-06 18:57:08 +01007132 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007133 return NULL;
7134
Bram Moolenaare38eab22019-12-05 21:50:01 +01007135 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01007136 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007137 while (current_funccal)
7138 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007139 ht = find_var_ht(name, &varname);
7140 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007141 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007142 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007143 if (v != NULL)
7144 break;
7145 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007146 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007147 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007148 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007149 }
7150 current_funccal = old_current_funccal;
7151
7152 return v;
7153}
7154
7155/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007156 * Set "copyID + 1" in previous_funccal and callers.
7157 */
7158 int
7159set_ref_in_previous_funccal(int copyID)
7160{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007161 funccall_T *fc;
7162
Bram Moolenaarca16c602022-09-06 18:57:08 +01007163 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007164 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007165 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007166 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
7167 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
7168 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007169 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007170 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007171 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007172}
7173
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007174 static int
7175set_ref_in_funccal(funccall_T *fc, int copyID)
7176{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007177 if (fc->fc_copyID != copyID)
7178 {
7179 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007180 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
7181 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
7182 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
7183 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007184 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007185 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007186 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007187}
7188
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007189/*
7190 * Set "copyID" in all local vars and arguments in the call stack.
7191 */
7192 int
7193set_ref_in_call_stack(int copyID)
7194{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007195 funccall_T *fc;
7196 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007197
Bram Moolenaarca16c602022-09-06 18:57:08 +01007198 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007199 if (set_ref_in_funccal(fc, copyID))
7200 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007201
7202 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007203 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007204 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007205 if (set_ref_in_funccal(fc, copyID))
7206 return TRUE;
7207 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007208}
7209
7210/*
7211 * Set "copyID" in all functions available by name.
7212 */
7213 int
7214set_ref_in_functions(int copyID)
7215{
7216 int todo;
7217 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007218 ufunc_T *fp;
7219
7220 todo = (int)func_hashtab.ht_used;
7221 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007222 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007223 if (!HASHITEM_EMPTY(hi))
7224 {
7225 --todo;
7226 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007227 if (!func_name_refcount(fp->uf_name)
7228 && set_ref_in_func(NULL, fp, copyID))
7229 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007230 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007231 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007232 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007233}
7234
7235/*
7236 * Set "copyID" in all function arguments.
7237 */
7238 int
7239set_ref_in_func_args(int copyID)
7240{
7241 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007242
7243 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007244 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7245 copyID, NULL, NULL))
7246 return TRUE;
7247 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007248}
7249
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007250/*
7251 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007252 * Returns TRUE if setting references failed somehow.
7253 */
7254 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007255set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007256{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007257 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007258 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007259 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007260 char_u fname_buf[FLEN_FIXED + 1];
7261 char_u *tofree = NULL;
7262 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007263 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007264
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007265 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007266 return FALSE;
7267
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007268 if (fp_in == NULL)
7269 {
7270 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007271 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007272 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007273 if (fp != NULL)
7274 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007275 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007276 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007277 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007278
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007279 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007280 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007281}
7282
Bram Moolenaare38eab22019-12-05 21:50:01 +01007283#endif // FEAT_EVAL