blob: b023c3a966af6dde708db5297825fb5c38d494a2 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +020011 * userfunc.c: User defined function support
Bram Moolenaara9b579f2016-07-17 18:29:19 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020017/*
18 * All user-defined functions are found in this hashtable.
19 */
20static hashtab_T func_hashtab;
21
Bram Moolenaare38eab22019-12-05 21:50:01 +010022// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020023static garray_T funcargs = GA_EMPTY;
24
Bram Moolenaar209b8e32019-03-14 13:43:24 +010025// pointer to funccal for currently active function
26static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020027
Bram Moolenaar209b8e32019-03-14 13:43:24 +010028// Pointer to list of previously used funccal, still around because some
29// item in it is still being used.
30static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020031
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020032static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +010033static void func_clear(ufunc_T *fp, int force);
34static int func_free(ufunc_T *fp, int force);
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +010035static char_u *untrans_function_name(char_u *name);
Bram Moolenaar58779852022-09-06 18:31:14 +010036static void handle_defer_one(funccall_T *funccal);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020037
38 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +000039func_init(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020040{
41 hash_init(&func_hashtab);
42}
43
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020044/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020045 * Return the function hash table
46 */
47 hashtab_T *
48func_tbl_get(void)
49{
50 return &func_hashtab;
51}
52
53/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020054 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020055 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010056 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010057 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaardce24412022-02-08 20:35:30 +000058 * If "eap" is not NULL use it to check for an already declared name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010059 * Return a pointer to after the type.
60 * When something is wrong return "arg".
61 */
62 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010063one_function_arg(
64 char_u *arg,
65 garray_T *newargs,
66 garray_T *argtypes,
67 int types_optional,
h-eastb895b0f2023-09-24 15:46:31 +020068 garray_T *arg_objm,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010069 evalarg_T *evalarg,
Bram Moolenaardce24412022-02-08 20:35:30 +000070 exarg_T *eap,
Bram Moolenaar2a389082021-04-09 20:24:31 +020071 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010072 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073{
Bram Moolenaar6e949782020-04-13 17:21:00 +020074 char_u *p = arg;
75 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020076 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077
78 while (ASCII_ISALNUM(*p) || *p == '_')
79 ++p;
Keith Thompson184f71c2024-01-04 21:19:04 +010080 if (arg == p || SAFE_isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020081 || (argtypes == NULL
82 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
83 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010084 {
85 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000086 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087 return arg;
88 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010089
Bram Moolenaarce723f32023-06-10 19:00:12 +010090 // Extra checks in Vim9 script.
91 if (!skip && argtypes != NULL)
92 {
93 int c = *p;
94 *p = NUL;
95 int r = check_reserved_name(arg, FALSE);
96 *p = c;
97 if (r == FAIL)
98 return arg;
99
100 // Cannot use script var name for argument. In function: also check
101 // local vars and arguments.
102 if (check_defined(arg, p - arg,
103 evalarg == NULL ? NULL : evalarg->eval_cctx,
Bram Moolenaardce24412022-02-08 20:35:30 +0000104 eap == NULL ? NULL : eap->cstack, TRUE) == FAIL)
Bram Moolenaarce723f32023-06-10 19:00:12 +0100105 return arg;
106 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100108 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
109 return arg;
110 if (newargs != NULL)
111 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100112 int c;
113 int i;
114
115 c = *p;
116 *p = NUL;
117 arg_copy = vim_strsave(arg);
118 if (arg_copy == NULL)
119 {
120 *p = c;
121 return arg;
122 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200123 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200124 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200125 // Check for duplicate argument name.
126 for (i = 0; i < newargs->ga_len; ++i)
127 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
128 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000129 semsg(_(e_duplicate_argument_name_str), arg_copy);
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200130 vim_free(arg_copy);
131 return arg;
132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100133 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
134 newargs->ga_len++;
135
136 *p = c;
137 }
138
139 // get any type from "arg: type"
h-eastb895b0f2023-09-24 15:46:31 +0200140 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK)
141 && arg_objm != NULL && (skip || ga_grow(arg_objm, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 {
143 char_u *type = NULL;
144
Bram Moolenaar6e949782020-04-13 17:21:00 +0200145 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
146 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200147 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200148 arg_copy == NULL ? arg : arg_copy);
149 p = skipwhite(p);
150 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 if (*p == ':')
152 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200153 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100154 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200155 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100156 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200157 return arg;
158 }
159 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200160 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100161 if (!skip)
162 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100163 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200164 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200165 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200166 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200167 arg_copy == NULL ? arg : arg_copy);
168 return arg;
169 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100170 if (!skip)
171 {
172 if (type == NULL && types_optional)
173 // lambda arguments default to "any" type
Bram Moolenaar2a389082021-04-09 20:24:31 +0200174 type = vim_strsave((char_u *)
175 (is_vararg ? "list<any>" : "any"));
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100176 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
h-eastb895b0f2023-09-24 15:46:31 +0200177 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = FALSE;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 }
180
181 return p;
182}
183
184/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000185 * Handle line continuation in function arguments or body.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000186 * Get a next line, store it in "eap" if appropriate and put the line in
187 * "lines_to_free" to free the line later.
Bram Moolenaar7473a842021-12-28 17:55:26 +0000188 */
189 static char_u *
190get_function_line(
191 exarg_T *eap,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000192 garray_T *lines_to_free,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000193 int indent,
194 getline_opt_T getline_options)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000195{
196 char_u *theline;
197
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100198 if (eap->ea_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +0000199 theline = getcmdline(':', 0L, indent, 0);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000200 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100201 theline = eap->ea_getline(':', eap->cookie, indent, getline_options);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000202 if (theline != NULL)
203 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000204 if (lines_to_free->ga_len > 0
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000205 && eap->cmdlinep != NULL
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000206 && *eap->cmdlinep == ((char_u **)lines_to_free->ga_data)
207 [lines_to_free->ga_len - 1])
Bram Moolenaar7473a842021-12-28 17:55:26 +0000208 *eap->cmdlinep = theline;
Bram Moolenaarb5820102023-01-25 12:27:13 +0000209 (void)ga_add_string(lines_to_free, theline);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000210 }
211
212 return theline;
213}
214
215/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200216 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100217 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100218 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200219 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100220 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200221get_function_args(
222 char_u **argp,
223 char_u endchar,
224 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100225 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100226 int types_optional, // types optional if "argtypes" is not NULL
h-eastb895b0f2023-09-24 15:46:31 +0200227 garray_T *arg_objm, // NULL unless using :def
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100228 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200229 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200230 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200231 int skip,
Bram Moolenaardce24412022-02-08 20:35:30 +0000232 exarg_T *eap, // can be NULL
Bram Moolenaar554d0312023-01-05 19:59:18 +0000233 int in_class, // non-zero when inside a class or interface
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000234 garray_T *newlines, // function body lines
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000235 garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200236{
237 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100238 char_u *arg;
239 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200240 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200241 int any_default = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100242 char_u *whitep = *argp;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100243 int need_expr = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200244
245 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000246 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100247 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000248 ga_init2(argtypes, sizeof(char_u *), 3);
h-eastb895b0f2023-09-24 15:46:31 +0200249 if (arg_objm != NULL)
250 ga_init2(arg_objm, sizeof(int8_T), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200251 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000252 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200253
254 if (varargs != NULL)
255 *varargs = FALSE;
256
257 /*
258 * Isolate the arguments: "arg1, arg2, ...)"
259 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100260 arg = skipwhite(*argp);
261 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200262 while (*p != endchar)
263 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100264 while (eap != NULL && eap->ea_getline != NULL
Bram Moolenaar2c330432020-04-13 14:41:35 +0200265 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200266 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200267 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000268 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000269 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000270
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200271 if (theline == NULL)
272 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200273 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200274 p = skipwhite(theline);
275 }
276
277 if (mustend && *p != endchar)
278 {
279 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000280 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100281 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200282 }
Christian Brabandte4a450a2023-12-08 20:57:38 +0100283 if (*p == endchar && !need_expr)
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200284 break;
285
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200286 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
287 {
288 if (varargs != NULL)
289 *varargs = TRUE;
290 p += 3;
291 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100292
293 if (argtypes != NULL)
294 {
295 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200296 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100298 if (!skip)
299 emsg(_(e_missing_name_after_dots));
300 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100301 }
302
303 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100304 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200305 arg_objm, evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100306 if (p == arg)
307 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100308 if (*skipwhite(p) == '=')
309 {
310 emsg(_(e_cannot_use_default_for_variable_arguments));
311 break;
312 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100313 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200314 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000315 else if (in_class && STRNCMP(p, "this.", 5) == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000316 {
317 // this.memberName
318 p += 5;
319 arg = p;
320 while (ASCII_ISALNUM(*p) || *p == '_')
321 ++p;
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000322 char_u *argend = p;
323
h-eastdb385522023-09-28 22:18:19 +0200324 // object variable this. can be used only in a constructor
325 if (STRNCMP(eap->arg, "new", 3) != 0)
326 {
327 c = *argend;
328 *argend = NUL;
329 semsg(_(e_cannot_use_an_object_variable_except_with_the_new_method_str), arg);
330 *argend = c;
331 break;
332 }
333
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000334 if (*skipwhite(p) == '=')
335 {
336 char_u *defval = skipwhite(skipwhite(p) + 1);
337 if (STRNCMP(defval, "v:none", 6) != 0)
338 {
339 semsg(_(e_constructor_default_value_must_be_vnone_str), p);
340 goto err_ret;
341 }
342 any_default = TRUE;
343 p = defval + 6;
344
345 if (ga_grow(default_args, 1) == FAIL)
346 goto err_ret;
347
348 char_u *expr = vim_strsave((char_u *)"v:none");
349 if (expr == NULL)
350 goto err_ret;
351 ((char_u **)(default_args->ga_data))
352 [default_args->ga_len] = expr;
353 default_args->ga_len++;
354 }
355 else if (any_default)
356 {
357 emsg(_(e_non_default_argument_follows_default_argument));
358 goto err_ret;
359 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000360
361 // TODO: check the argument is indeed a member
362 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
363 return FAIL;
364 if (newargs != NULL)
365 {
366 ((char_u **)(newargs->ga_data))[newargs->ga_len] =
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000367 vim_strnsave(arg, argend - arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000368 newargs->ga_len++;
369
h-eastb895b0f2023-09-24 15:46:31 +0200370 if (argtypes != NULL && ga_grow(argtypes, 1) == OK
371 && arg_objm != NULL && ga_grow(arg_objm, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000372 {
373 // TODO: use the actual type
374 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] =
375 vim_strsave((char_u *)"any");
h-eastb895b0f2023-09-24 15:46:31 +0200376 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000377
378 // Add a line to the function body for the assignment.
379 if (ga_grow(newlines, 1) == OK)
380 {
381 // "this.name = name"
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000382 int len = 5 + (argend - arg) + 3 + (argend - arg) + 1;
383 if (any_default)
384 len += 14 + 10;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000385 char_u *assignment = alloc(len);
386 if (assignment != NULL)
387 {
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000388 c = *argend;
389 *argend = NUL;
390 if (any_default)
391 vim_snprintf((char *)assignment, len,
392 "ifargisset %d this.%s = %s",
393 default_args->ga_len - 1, arg, arg);
394 else
395 vim_snprintf((char *)assignment, len,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000396 "this.%s = %s", arg, arg);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000397 *argend = c;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000398 ((char_u **)(newlines->ga_data))[
399 newlines->ga_len++] = assignment;
400 }
401 }
402 }
403 }
404 if (*p == ',')
405 ++p;
406 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200407 else
408 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200409 char_u *np;
410
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200411 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100412 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200413 arg_objm, evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200415 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200416
Bram Moolenaar015cf102021-06-26 21:52:02 +0200417 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200418 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200419 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200420 if (*np == '=' && np[1] != '=' && np[1] != '~'
421 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200422 {
423 typval_T rettv;
424
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100425 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200426 any_default = TRUE;
Bram Moolenaareb5adf12022-09-04 13:41:37 +0100427 p = skipwhite(np + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000428 char_u *expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200429 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200430 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200431 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200432 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200433 if (ga_grow(default_args, 1) == FAIL)
434 goto err_ret;
435
Christian Brabandte4a450a2023-12-08 20:57:38 +0100436 if (need_expr)
437 need_expr = FALSE;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200438 // trim trailing whitespace
439 while (p > expr && VIM_ISWHITE(p[-1]))
440 p--;
441 c = *p;
442 *p = NUL;
443 expr = vim_strsave(expr);
444 if (expr == NULL)
445 {
446 *p = c;
447 goto err_ret;
448 }
449 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200450 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200451 default_args->ga_len++;
452 *p = c;
453 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200454 }
455 else
Christian Brabandte4a450a2023-12-08 20:57:38 +0100456 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200457 mustend = TRUE;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100458 if (*skipwhite(p) == NUL)
459 need_expr = TRUE;
460 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200461 }
462 else if (any_default)
463 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000464 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200465 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200466 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200467
468 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
469 {
470 // Be tolerant when skipping
471 if (!skip)
472 {
473 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
474 goto err_ret;
475 }
476 p = skipwhite(p);
477 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200478 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200479 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200480 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200481 // Don't give this error when skipping, it makes the "->" not
482 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100483 // Allow missing space after comma in legacy functions.
484 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200485 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
486 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100487 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200488 goto err_ret;
489 }
490 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200491 else
492 mustend = TRUE;
493 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200494 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200495 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200496 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200497
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200498 if (*p != endchar)
499 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100500 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200501
502 *argp = p;
503 return OK;
504
505err_ret:
506 if (newargs != NULL)
507 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200508 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200509 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200510 return FAIL;
511}
512
513/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100514 * Parse the argument types, filling "fp->uf_arg_types".
515 * Return OK or FAIL.
516 */
517 static int
h-eastb895b0f2023-09-24 15:46:31 +0200518parse_argument_types(
519 ufunc_T *fp,
520 garray_T *argtypes,
521 int varargs,
522 garray_T *arg_objm,
523 ocmember_T *obj_members,
524 int obj_member_count)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100525{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200526 int len = 0;
527
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100528 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
529 if (argtypes->ga_len > 0)
530 {
531 // When "varargs" is set the last name/type goes into uf_va_name
532 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200533 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100534
535 if (len > 0)
536 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
537 if (fp->uf_arg_types != NULL)
538 {
539 int i;
540 type_T *type;
541
542 for (i = 0; i < len; ++ i)
543 {
544 char_u *p = ((char_u **)argtypes->ga_data)[i];
545
546 if (p == NULL)
547 // will get the type from the default value
548 type = &t_unknown;
549 else
h-eastb895b0f2023-09-24 15:46:31 +0200550 {
551 if (arg_objm != NULL && ((int8_T *)arg_objm->ga_data)[i])
552 {
553 char_u *aname = ((char_u **)fp->uf_args.ga_data)[i];
554
555 type = &t_any;
556 for (int om = 0; om < obj_member_count; ++om)
557 {
558 if (STRCMP(aname, obj_members[om].ocm_name) == 0)
559 {
560 type = obj_members[om].ocm_type;
561 break;
562 }
563 }
564 }
565 else
566 type = parse_type(&p, &fp->uf_type_list, TRUE);
567 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100568 if (type == NULL)
569 return FAIL;
570 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000571 if (i < fp->uf_args.ga_len
572 && (type->tt_type == VAR_FUNC
Yegappan Lakshmanan3e336502024-04-04 19:35:59 +0200573 || type->tt_type == VAR_PARTIAL))
574 {
575 char_u *name = ((char_u **)fp->uf_args.ga_data)[i];
576 if (obj_members != NULL && *name == '_')
577 // protected object method
578 name++;
579
580 if (var_wrong_func_name(name, TRUE))
581 return FAIL;
582 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100583 }
584 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100585 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200586
587 if (varargs)
588 {
589 char_u *p;
590
591 // Move the last argument "...name: type" to uf_va_name and
592 // uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200593 --fp->uf_args.ga_len;
Bram Moolenaarf0571712023-01-04 13:16:20 +0000594 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len];
595 ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len] = NULL;
Bram Moolenaar2a389082021-04-09 20:24:31 +0200596 p = ((char_u **)argtypes->ga_data)[len];
597 if (p == NULL)
598 // TODO: get type from default value
599 fp->uf_va_type = &t_list_any;
600 else
601 {
602 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
603 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
604 {
605 semsg(_(e_variable_arguments_type_must_be_list_str),
606 ((char_u **)argtypes->ga_data)[len]);
607 return FAIL;
608 }
609 }
610 if (fp->uf_va_type == NULL)
611 return FAIL;
612 }
613
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100614 return OK;
615}
616
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100617 static int
618parse_return_type(ufunc_T *fp, char_u *ret_type)
619{
620 if (ret_type == NULL)
621 fp->uf_ret_type = &t_void;
622 else
623 {
624 char_u *p = ret_type;
625
626 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
627 if (fp->uf_ret_type == NULL)
628 {
629 fp->uf_ret_type = &t_void;
630 return FAIL;
631 }
632 }
633 return OK;
634}
635
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100636/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200637 * Register function "fp" as using "current_funccal" as its scope.
638 */
639 static int
640register_closure(ufunc_T *fp)
641{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200642 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100643 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200644 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200645 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200646 fp->uf_scoped = current_funccal;
647 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200648
Bram Moolenaarca16c602022-09-06 18:57:08 +0100649 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200650 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100651 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
652 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200653 return OK;
654}
655
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100656 static void
657set_ufunc_name(ufunc_T *fp, char_u *name)
658{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100659 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
660 // actually extends beyond the struct.
661 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100662
663 if (name[0] == K_SPECIAL)
664 {
665 fp->uf_name_exp = alloc(STRLEN(name) + 3);
666 if (fp->uf_name_exp != NULL)
667 {
668 STRCPY(fp->uf_name_exp, "<SNR>");
669 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
670 }
671 }
672}
673
Bram Moolenaar58016442016-07-31 18:30:22 +0200674/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100675 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
676 * return "buf" filled with a readable function name.
677 * Otherwise just return "name", thus the return value can always be used.
678 * "name" and "buf" may be equal.
679 */
680 char_u *
681make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
682{
683 size_t len;
684
685 if (name[0] != K_SPECIAL)
686 return name;
687 len = STRLEN(name);
688 if (len + 3 > bufsize)
689 return name;
690
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100691 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100692 mch_memmove(buf, "<SNR>", 5);
693 return buf;
694}
695
696/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200697 * Get a name for a lambda. Returned in static memory.
698 */
699 char_u *
700get_lambda_name(void)
701{
702 static char_u name[30];
703 static int lambda_no = 0;
704
705 sprintf((char*)name, "<lambda>%d", ++lambda_no);
706 return name;
707}
708
Bram Moolenaare01e5212023-01-08 20:31:18 +0000709/*
710 * Allocate a "ufunc_T" for a function called "name".
711 * Makes sure the size is right.
712 */
713 static ufunc_T *
714alloc_ufunc(char_u *name)
715{
716 // When the name is short we need to make sure we allocate enough bytes for
717 // the whole struct, including any padding.
718 size_t len = offsetof(ufunc_T, uf_name) + STRLEN(name) + 1;
719 return alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
720}
721
Bram Moolenaar801ab062020-06-25 19:27:56 +0200722#if defined(FEAT_LUA) || defined(PROTO)
723/*
724 * Registers a native C callback which can be called from Vim script.
725 * Returns the name of the Vim script function.
726 */
727 char_u *
728register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
729{
730 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200731 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200732
Bram Moolenaare01e5212023-01-08 20:31:18 +0000733 fp = alloc_ufunc(name);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200734 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200735 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200736
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200737 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200738 fp->uf_refcount = 1;
739 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000740 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200741 fp->uf_calls = 0;
742 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200743 fp->uf_cb = cb;
744 fp->uf_cb_free = cb_free;
745 fp->uf_cb_state = state;
746
747 set_ufunc_name(fp, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000748 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200749
750 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200751}
752#endif
753
Bram Moolenaar04b12692020-05-04 23:24:44 +0200754/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100755 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100756 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100757 * If "white_error" is not NULL check for correct use of white space and set
758 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100759 * Return NULL if no valid arrow found.
760 */
761 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100762skip_arrow(
763 char_u *start,
764 int equal_arrow,
765 char_u **ret_type,
766 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100767{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100768 char_u *s = start;
769 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100770
771 if (equal_arrow)
772 {
773 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100774 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100775 if (white_error != NULL && !VIM_ISWHITE(s[1]))
776 {
777 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100778 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100779 return NULL;
780 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100781 s = skipwhite(s + 1);
782 *ret_type = s;
783 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100784 if (s == *ret_type)
785 {
786 emsg(_(e_missing_return_type));
787 return NULL;
788 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100789 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100790 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100791 s = skipwhite(s);
792 if (*s != '=')
793 return NULL;
794 ++s;
795 }
796 if (*s != '>')
797 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100798 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
799 || !IS_WHITE_OR_NUL(s[1])))
800 {
801 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100802 semsg(_(e_white_space_required_before_and_after_str_at_str),
803 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100804 return NULL;
805 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100806 return skipwhite(s + 1);
807}
808
809/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100810 * Check if "*cmd" points to a function command and if so advance "*cmd" and
811 * return TRUE.
812 * Otherwise return FALSE;
813 * Do not consider "function(" to be a command.
814 */
815 static int
816is_function_cmd(char_u **cmd)
817{
818 char_u *p = *cmd;
819
820 if (checkforcmd(&p, "function", 2))
821 {
822 if (*p == '(')
823 return FALSE;
824 *cmd = p;
825 return TRUE;
826 }
827 return FALSE;
828}
829
830/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200831 * Called when defining a function: The context may be needed for script
832 * variables declared in a block that is visible now but not when the function
833 * is compiled or called later.
834 */
835 static void
836function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
837{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000838 if (cstack == NULL || cstack->cs_idx < 0)
839 return;
840
841 int count = cstack->cs_idx + 1;
842 int i;
843
844 fp->uf_block_ids = ALLOC_MULT(int, count);
845 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200846 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000847 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
848 sizeof(int) * count);
849 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200850 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000851
852 // Set flag in each block to indicate a function was defined. This
853 // is used to keep the variable when leaving the block, see
854 // hide_script_var().
855 for (i = 0; i <= cstack->cs_idx; ++i)
856 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200857}
858
859/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100860 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200861 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100862 * "newlines" must already have been initialized.
863 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
864 */
865 static int
866get_function_body(
867 exarg_T *eap,
868 garray_T *newlines,
869 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000870 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100871{
872 linenr_T sourcing_lnum_top = SOURCING_LNUM;
873 linenr_T sourcing_lnum_off;
874 int saved_wait_return = need_wait_return;
875 char_u *line_arg = line_arg_in;
876 int vim9_function = eap->cmdidx == CMD_def
877 || eap->cmdidx == CMD_block;
878#define MAX_FUNC_NESTING 50
879 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200880 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100881 int nesting = 0;
882 getline_opt_T getline_options;
883 int indent = 2;
884 char_u *skip_until = NULL;
885 int ret = FAIL;
886 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200887 int heredoc_concat_len = 0;
888 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100889 char_u *heredoc_trimmed = NULL;
890
Bram Moolenaar20677332021-06-06 17:02:53 +0200891 ga_init2(&heredoc_ga, 1, 500);
892
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100893 // Detect having skipped over comment lines to find the return
894 // type. Add NULL lines to keep the line count correct.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100895 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100896 if (SOURCING_LNUM < sourcing_lnum_off)
897 {
898 sourcing_lnum_off -= SOURCING_LNUM;
899 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
900 goto theend;
901 while (sourcing_lnum_off-- > 0)
902 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
903 }
904
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200905 nesting_def[0] = vim9_function;
906 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100907 getline_options = vim9_function
908 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
909 for (;;)
910 {
911 char_u *theline;
912 char_u *p;
913 char_u *arg;
914
915 if (KeyTyped)
916 {
917 msg_scroll = TRUE;
918 saved_wait_return = FALSE;
919 }
920 need_wait_return = FALSE;
921
922 if (line_arg != NULL)
923 {
924 // Use eap->arg, split up in parts by line breaks.
925 theline = line_arg;
926 p = vim_strchr(theline, '\n');
927 if (p == NULL)
928 line_arg += STRLEN(line_arg);
929 else
930 {
931 *p = NUL;
932 line_arg = p + 1;
933 }
934 }
935 else
936 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000937 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100938 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100939 }
940 if (KeyTyped)
941 lines_left = Rows - 1;
942 if (theline == NULL)
943 {
944 // Use the start of the function for the line number.
945 SOURCING_LNUM = sourcing_lnum_top;
946 if (skip_until != NULL)
947 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200948 else if (nesting_inline[nesting])
949 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100950 else if (eap->cmdidx == CMD_def)
951 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100952 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000953 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100954 goto theend;
955 }
956
957 // Detect line continuation: SOURCING_LNUM increased more than one.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100958 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100959 if (SOURCING_LNUM < sourcing_lnum_off)
960 sourcing_lnum_off -= SOURCING_LNUM;
961 else
962 sourcing_lnum_off = 0;
963
964 if (skip_until != NULL)
965 {
966 // Don't check for ":endfunc"/":enddef" between
967 // * ":append" and "."
968 // * ":python <<EOF" and "EOF"
969 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
970 if (heredoc_trimmed == NULL
971 || (is_heredoc && skipwhite(theline) == theline)
972 || STRNCMP(theline, heredoc_trimmed,
973 STRLEN(heredoc_trimmed)) == 0)
974 {
975 if (heredoc_trimmed == NULL)
976 p = theline;
977 else if (is_heredoc)
978 p = skipwhite(theline) == theline
979 ? theline : theline + STRLEN(heredoc_trimmed);
980 else
981 p = theline + STRLEN(heredoc_trimmed);
982 if (STRCMP(p, skip_until) == 0)
983 {
984 VIM_CLEAR(skip_until);
985 VIM_CLEAR(heredoc_trimmed);
986 getline_options = vim9_function
987 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
988 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200989
990 if (heredoc_concat_len > 0)
991 {
992 // Replace the starting line with all the concatenated
993 // lines.
994 ga_concat(&heredoc_ga, theline);
995 vim_free(((char_u **)(newlines->ga_data))[
996 heredoc_concat_len - 1]);
997 ((char_u **)(newlines->ga_data))[
998 heredoc_concat_len - 1] = heredoc_ga.ga_data;
999 ga_init(&heredoc_ga);
1000 heredoc_concat_len = 0;
1001 theline += STRLEN(theline); // skip the "EOF"
1002 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001003 }
1004 }
1005 }
1006 else
1007 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001008 int c;
1009 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +00001010 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001011
1012 // skip ':' and blanks
1013 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
1014 ;
1015
1016 // Check for "endfunction", "enddef" or "}".
1017 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +00001018 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001019 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001020 ? *p == '}'
1021 : (checkforcmd(&p, nesting_def[nesting]
1022 ? "enddef" : "endfunction", 4)
1023 && *p != ':'))
1024 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00001025 if (!nesting_inline[nesting] && nesting_def[nesting]
1026 && p < cmd + 6)
1027 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001028 if (nesting-- == 0)
1029 {
1030 char_u *nextcmd = NULL;
1031
1032 if (*p == '|' || *p == '}')
1033 nextcmd = p + 1;
1034 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
1035 nextcmd = line_arg;
1036 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001037 && (vim9_function || p_verbose > 0))
1038 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +02001039 SOURCING_LNUM = sourcing_lnum_top
1040 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001041 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +00001042 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001043 else
1044 give_warning2((char_u *)
1045 _("W22: Text found after :endfunction: %s"),
1046 p, TRUE);
1047 }
1048 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001049 {
1050 // Another command follows. If the line came from "eap"
1051 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001052 // change "eap->cmdlinep" to point to the last fetched
1053 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001054 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001055 if (lines_to_free->ga_len > 0
1056 && *eap->cmdlinep !=
1057 ((char_u **)lines_to_free->ga_data)
1058 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001059 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001060 // *cmdlinep will be freed later, thus remove the
1061 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001062 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001063 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
1064 [lines_to_free->ga_len - 1];
1065 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001066 }
1067 }
1068 break;
1069 }
1070 }
1071
1072 // Check for mismatched "endfunc" or "enddef".
1073 // We don't check for "def" inside "func" thus we also can't check
1074 // for "enddef".
1075 // We continue to find the end of the function, although we might
1076 // not find it.
1077 else if (nesting_def[nesting])
1078 {
1079 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1080 emsg(_(e_mismatched_endfunction));
1081 }
1082 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1083 emsg(_(e_mismatched_enddef));
1084
1085 // Increase indent inside "if", "while", "for" and "try", decrease
1086 // at "end".
1087 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1088 indent -= 2;
1089 else if (STRNCMP(p, "if", 2) == 0
1090 || STRNCMP(p, "wh", 2) == 0
1091 || STRNCMP(p, "for", 3) == 0
1092 || STRNCMP(p, "try", 3) == 0)
1093 indent += 2;
1094
1095 // Check for defining a function inside this function.
1096 // Only recognize "def" inside "def", not inside "function",
1097 // For backwards compatibility, see Test_function_python().
1098 c = *p;
1099 if (is_function_cmd(&p)
1100 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1101 {
1102 if (*p == '!')
1103 p = skipwhite(p + 1);
1104 p += eval_fname_script(p);
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001105 vim_free(trans_function_name(&p, NULL, TRUE, 0));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001106 if (*skipwhite(p) == '(')
1107 {
1108 if (nesting == MAX_FUNC_NESTING - 1)
1109 emsg(_(e_function_nesting_too_deep));
1110 else
1111 {
1112 ++nesting;
1113 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001114 nesting_inline[nesting] = FALSE;
1115 indent += 2;
1116 }
1117 }
1118 }
1119
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001120 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001121 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001122 // Not a comment line: check for nested inline function.
1123 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001124 while (end > p && VIM_ISWHITE(*end))
1125 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001126 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001127 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001128 int is_block;
1129
1130 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001131 --end;
1132 while (end > p && VIM_ISWHITE(*end))
1133 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001134 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1135 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001136 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001137 char_u *s = p;
1138
1139 // check for line starting with "au" for :autocmd or
1140 // "com" for :command, these can use a {} block
1141 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1142 || checkforcmd_noparen(&s, "command", 3);
1143 }
1144
1145 if (is_block)
1146 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001147 if (nesting == MAX_FUNC_NESTING - 1)
1148 emsg(_(e_function_nesting_too_deep));
1149 else
1150 {
1151 ++nesting;
1152 nesting_def[nesting] = TRUE;
1153 nesting_inline[nesting] = TRUE;
1154 indent += 2;
1155 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001156 }
1157 }
1158 }
1159
1160 // Check for ":append", ":change", ":insert". Not for :def.
1161 p = skip_range(p, FALSE, NULL);
1162 if (!vim9_function
1163 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1164 || (p[0] == 'c'
1165 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1166 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1167 && (STRNCMP(&p[3], "nge", 3) != 0
1168 || !ASCII_ISALPHA(p[6])))))))
1169 || (p[0] == 'i'
1170 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1171 && (!ASCII_ISALPHA(p[2])
1172 || (p[2] == 's'
1173 && (!ASCII_ISALPHA(p[3])
1174 || p[3] == 'e'))))))))
1175 skip_until = vim_strsave((char_u *)".");
1176
1177 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1178 arg = skipwhite(skiptowhite(p));
1179 if (arg[0] == '<' && arg[1] =='<'
1180 && ((p[0] == 'p' && p[1] == 'y'
1181 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1182 || ((p[2] == '3' || p[2] == 'x')
1183 && !ASCII_ISALPHA(p[3]))))
1184 || (p[0] == 'p' && p[1] == 'e'
1185 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1186 || (p[0] == 't' && p[1] == 'c'
1187 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1188 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1189 && !ASCII_ISALPHA(p[3]))
1190 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1191 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1192 || (p[0] == 'm' && p[1] == 'z'
1193 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1194 ))
1195 {
1196 // ":python <<" continues until a dot, like ":append"
1197 p = skipwhite(arg + 2);
1198 if (STRNCMP(p, "trim", 4) == 0)
1199 {
1200 // Ignore leading white space.
1201 p = skipwhite(p + 4);
1202 heredoc_trimmed = vim_strnsave(theline,
1203 skipwhite(theline) - theline);
1204 }
1205 if (*p == NUL)
1206 skip_until = vim_strsave((char_u *)".");
1207 else
1208 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1209 getline_options = GETLINE_NONE;
1210 is_heredoc = TRUE;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001211 if (vim9_function && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001212 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001213 }
1214
Bram Moolenaard881d152022-05-13 13:50:36 +01001215 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001216 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001217 // Check for ":cmd v =<< [trim] EOF"
1218 // and ":cmd [a, b] =<< [trim] EOF"
1219 // and "lines =<< [trim] EOF" for Vim9
1220 // Where "cmd" can be "let", "var", "final" or "const".
zeertzjqa93d9cd2023-05-02 16:25:47 +01001221 arg = p;
1222 if (checkforcmd(&arg, "let", 2)
1223 || checkforcmd(&arg, "var", 3)
1224 || checkforcmd(&arg, "final", 5)
1225 || checkforcmd(&arg, "const", 5)
1226 || vim9_function)
Bram Moolenaard881d152022-05-13 13:50:36 +01001227 {
zeertzjqa93d9cd2023-05-02 16:25:47 +01001228 while (vim_strchr((char_u *)"$@&", *arg) != NULL)
1229 ++arg;
1230 arg = skipwhite(find_name_end(arg, NULL, NULL,
1231 FNE_INCL_BR | FNE_ALLOW_CURLY));
1232 if (vim9_function && *arg == ':')
1233 arg = skipwhite(skip_type(skipwhite(arg + 1), FALSE));
1234 if (arg[0] == '=' && arg[1] == '<' && arg[2] =='<')
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001235 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001236 p = skipwhite(arg + 3);
1237 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001238 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001239 if (STRNCMP(p, "trim", 4) == 0)
1240 {
1241 // Ignore leading white space.
1242 p = skipwhite(p + 4);
1243 heredoc_trimmed = vim_strnsave(theline,
1244 skipwhite(theline) - theline);
1245 continue;
1246 }
1247 if (STRNCMP(p, "eval", 4) == 0)
1248 {
1249 // Ignore leading white space.
1250 p = skipwhite(p + 4);
1251 continue;
1252 }
1253 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001254 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001255 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1256 getline_options = GETLINE_NONE;
1257 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001258 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001259 }
1260 }
1261 }
1262
1263 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001264 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001265 goto theend;
1266
Bram Moolenaar20677332021-06-06 17:02:53 +02001267 if (heredoc_concat_len > 0)
1268 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001269 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001270 // to be used for the instruction later.
1271 ga_concat(&heredoc_ga, theline);
1272 ga_concat(&heredoc_ga, (char_u *)"\n");
1273 p = vim_strsave((char_u *)"");
1274 }
1275 else
1276 {
1277 // Copy the line to newly allocated memory. get_one_sourceline()
1278 // allocates 250 bytes per line, this saves 80% on average. The
1279 // cost is an extra alloc/free.
1280 p = vim_strsave(theline);
1281 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001282 if (p == NULL)
1283 goto theend;
1284 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1285
1286 // Add NULL lines for continuation lines, so that the line count is
1287 // equal to the index in the growarray.
1288 while (sourcing_lnum_off-- > 0)
1289 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1290
1291 // Check for end of eap->arg.
1292 if (line_arg != NULL && *line_arg == NUL)
1293 line_arg = NULL;
1294 }
1295
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001296 // Return OK when no error was detected.
1297 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001298 ret = OK;
1299
1300theend:
1301 vim_free(skip_until);
1302 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001303 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001304 need_wait_return |= saved_wait_return;
1305 return ret;
1306}
1307
1308/*
1309 * Handle the body of a lambda. *arg points to the "{", process statements
1310 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001311 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001312 * When successful "rettv" is set to a funcref.
1313 */
1314 static int
1315lambda_function_body(
1316 char_u **arg,
1317 typval_T *rettv,
1318 evalarg_T *evalarg,
1319 garray_T *newargs,
1320 garray_T *argtypes,
1321 int varargs,
1322 garray_T *default_args,
1323 char_u *ret_type)
1324{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001325 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001326 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001327 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001328 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001329 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001330 exarg_T eap;
1331 garray_T newlines;
1332 char_u *cmdline = NULL;
1333 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001334 partial_T *pt;
1335 char_u *name;
1336 int lnum_save = -1;
1337 linenr_T sourcing_lnum_top = SOURCING_LNUM;
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001338 char_u *line_arg = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001339
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001340 *arg = skipwhite(*arg + 1);
1341 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001342 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001343 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001344 return FAIL;
1345 }
1346
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001347 // When there is a line break use what follows for the lambda body.
1348 // Makes lambda body initializers work for object and enum member
1349 // variables.
1350 if (**arg == '\n')
1351 line_arg = *arg + 1;
1352
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001353 CLEAR_FIELD(eap);
1354 eap.cmdidx = CMD_block;
1355 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001356 eap.cmdlinep = &cmdline;
1357 eap.skip = !evaluate;
1358 if (evalarg->eval_cctx != NULL)
1359 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1360 else
1361 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001362 eap.ea_getline = evalarg->eval_getline;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001363 eap.cookie = evalarg->eval_cookie;
1364 }
1365
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001366 ga_init2(&newlines, sizeof(char_u *), 10);
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001367 if (get_function_body(&eap, &newlines, line_arg,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001368 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001369 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001370
1371 // When inside a lambda must add the function lines to evalarg.eval_ga.
1372 evalarg->eval_break_count += newlines.ga_len;
1373 if (gap->ga_itemsize > 0)
1374 {
1375 int idx;
1376 char_u *last;
1377 size_t plen;
1378 char_u *pnl;
1379
1380 for (idx = 0; idx < newlines.ga_len; ++idx)
1381 {
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001382 char_u *p = ((char_u **)newlines.ga_data)[idx];
1383 if (p == NULL)
1384 // comment line in the lambda body
1385 continue;
1386
1387 p = skipwhite(p);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001388
Bram Moolenaarecb66452021-05-18 15:09:18 +02001389 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001390 goto erret;
1391
1392 // Going to concatenate the lines after parsing. For an empty or
1393 // comment line use an empty string.
1394 // Insert NL characters at the start of each line, the string will
1395 // be split again later in .get_lambda_tv().
1396 if (*p == NUL || vim9_comment_start(p))
1397 p = (char_u *)"";
1398 plen = STRLEN(p);
1399 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1400 if (pnl != NULL)
1401 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001402 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1403 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001404 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001405 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001406 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001407 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001408 // more is following after the "}", which was skipped
1409 last = cmdline;
1410 else
1411 // nothing is following the "}"
1412 last = (char_u *)"}";
1413 plen = STRLEN(last);
1414 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1415 if (pnl != NULL)
1416 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001417 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1418 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001419 }
1420
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001421 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001422 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001423 garray_T *tfgap = &evalarg->eval_tofree_ga;
1424
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001425 // Something comes after the "}".
1426 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001427
1428 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001429 if (ga_grow(tfgap, 1) == OK)
1430 {
1431 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1432 evalarg->eval_using_cmdline = TRUE;
1433 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001434 }
1435 else
1436 *arg = (char_u *)"";
1437
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001438 if (!evaluate)
1439 {
1440 ret = OK;
1441 goto erret;
1442 }
1443
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001444 name = get_lambda_name();
Bram Moolenaare01e5212023-01-08 20:31:18 +00001445 ufunc = alloc_ufunc(name);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001446 if (ufunc == NULL)
1447 goto erret;
1448 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001449 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001450 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001451 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001452 ufunc->uf_refcount = 1;
1453 ufunc->uf_args = *newargs;
1454 newargs->ga_data = NULL;
1455 ufunc->uf_def_args = *default_args;
1456 default_args->ga_data = NULL;
1457 ufunc->uf_func_type = &t_func_any;
1458
1459 // error messages are for the first function line
1460 lnum_save = SOURCING_LNUM;
1461 SOURCING_LNUM = sourcing_lnum_top;
1462
1463 // parse argument types
h-eastb895b0f2023-09-24 15:46:31 +02001464 if (parse_argument_types(ufunc, argtypes, varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001465 {
1466 SOURCING_LNUM = lnum_save;
1467 goto erret;
1468 }
1469
1470 // parse the return type, if any
1471 if (parse_return_type(ufunc, ret_type) == FAIL)
1472 goto erret;
1473
1474 pt = ALLOC_CLEAR_ONE(partial_T);
1475 if (pt == NULL)
1476 goto erret;
1477 pt->pt_func = ufunc;
1478 pt->pt_refcount = 1;
1479
1480 ufunc->uf_lines = newlines;
1481 newlines.ga_data = NULL;
1482 if (sandbox)
1483 ufunc->uf_flags |= FC_SANDBOX;
1484 if (!ASCII_ISUPPER(*ufunc->uf_name))
1485 ufunc->uf_flags |= FC_VIM9;
1486 ufunc->uf_script_ctx = current_sctx;
1487 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1488 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1489 set_function_type(ufunc);
1490
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001491 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1492
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001493 rettv->vval.v_partial = pt;
1494 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001495 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001496 ret = OK;
1497
1498erret:
1499 if (lnum_save >= 0)
1500 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001501 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001502 if (newargs != NULL)
1503 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001504 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001505 if (ufunc != NULL)
1506 {
1507 func_clear(ufunc, TRUE);
1508 func_free(ufunc, TRUE);
1509 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001510 return ret;
1511}
1512
1513/*
1514 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001515 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001516 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001517 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1518 */
1519 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001520get_lambda_tv(
1521 char_u **arg,
1522 typval_T *rettv,
1523 int types_optional,
1524 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001525{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001526 int evaluate = evalarg != NULL
1527 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001528 garray_T newargs;
1529 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001530 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001531 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001532 garray_T default_args;
h-eastb895b0f2023-09-24 15:46:31 +02001533 garray_T arg_objm;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001534 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001535 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001536 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001537 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001538 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001539 char_u *s;
1540 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001541 int *old_eval_lavars = eval_lavars_used;
1542 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001543 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001544 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001545 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001546 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001547 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001548 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001549
Bram Moolenaar4525a572022-02-13 11:57:33 +00001550 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001551 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001552
1553 ga_init(&newargs);
1554 ga_init(&newlines);
1555
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001556 // First, check if this is really a lambda expression. "->" or "=>" must
1557 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001558 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001559 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001560 types_optional ? &argtypes : NULL, types_optional,
1561 types_optional ? &arg_objm : NULL, evalarg,
1562 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001563 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001564 {
1565 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001566 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001567 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001568 ga_clear(&arg_objm);
1569 }
Bram Moolenaar0346b792021-01-31 22:18:29 +01001570 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001571 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001572
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001573 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001574 if (evaluate)
1575 pnewargs = &newargs;
1576 else
1577 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001578 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001579 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
h-eastb895b0f2023-09-24 15:46:31 +02001580 types_optional ? &argtypes : NULL, types_optional,
1581 types_optional ? &arg_objm : NULL, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001582 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001583 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001584 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001585 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001586 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001587 {
1588 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001589 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001590 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001591 ga_clear(&arg_objm);
1592 }
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001593 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001594 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001595 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001596 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001597
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001598 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1599 if (ret_type != NULL)
1600 {
1601 ret_type = vim_strsave(ret_type);
1602 tofree2 = ret_type;
1603 }
1604
Bram Moolenaare38eab22019-12-05 21:50:01 +01001605 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001606 if (evaluate)
1607 eval_lavars_used = &eval_lavars;
1608
Bram Moolenaar65c44152020-12-24 15:14:01 +01001609 *arg = skipwhite_and_linebreak(*arg, evalarg);
1610
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001611 // Recognize "{" as the start of a function body.
1612 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001613 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001614 if (evalarg == NULL)
1615 // cannot happen?
1616 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001617 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001618 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1619 types_optional ? &argtypes : NULL, varargs,
1620 &default_args, ret_type) == FAIL)
1621 goto errret;
1622 goto theend;
1623 }
1624 if (default_args.ga_len > 0)
1625 {
1626 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001627 goto errret;
1628 }
1629
Bram Moolenaare38eab22019-12-05 21:50:01 +01001630 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001631 start = *arg;
1632 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001633 if (ret == FAIL)
1634 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001635
Bram Moolenaar65c44152020-12-24 15:14:01 +01001636 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001637 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001638 *arg = skipwhite_and_linebreak(*arg, evalarg);
1639 if (**arg != '}')
1640 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001641 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001642 goto errret;
1643 }
1644 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001645 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001646
1647 if (evaluate)
1648 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001649 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001650 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001651 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001652 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001653 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001654
Bram Moolenaare01e5212023-01-08 20:31:18 +00001655 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001656 if (fp == NULL)
1657 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001658 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001659 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001660 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001661 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001662
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001663 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001664 if (ga_grow(&newlines, 1) == FAIL)
1665 goto errret;
1666
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001667 // If there are line breaks, we need to split up the string.
1668 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001669 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001670 line_end = end;
1671
1672 // Add "return " before the expression (or the first line).
1673 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001674 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001675 if (p == NULL)
1676 goto errret;
1677 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1678 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001679 vim_strncpy(p + 7, start, line_end - start);
1680
1681 if (line_end != end)
1682 {
1683 // Add more lines, split by line breaks. Thus is used when a
1684 // lambda with { cmds } is encountered.
1685 while (*line_end == '\n')
1686 {
1687 if (ga_grow(&newlines, 1) == FAIL)
1688 goto errret;
1689 start = line_end + 1;
1690 line_end = vim_strchr(start, '\n');
1691 if (line_end == NULL)
1692 line_end = end;
1693 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1694 vim_strnsave(start, line_end - start);
1695 }
1696 }
1697
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001698 if (strstr((char *)p + 7, "a:") == NULL)
1699 // No a: variables are used for sure.
1700 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001701
1702 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001703 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001704 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001705 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001706 if (types_optional)
1707 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001708 if (parse_argument_types(fp, &argtypes,
h-eastb895b0f2023-09-24 15:46:31 +02001709 vim9script && varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001710 goto errret;
1711 if (ret_type != NULL)
1712 {
1713 fp->uf_ret_type = parse_type(&ret_type,
1714 &fp->uf_type_list, TRUE);
1715 if (fp->uf_ret_type == NULL)
1716 goto errret;
1717 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001718 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001719 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001720 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001721
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001722 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001723 if (current_funccal != NULL && eval_lavars)
1724 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001725 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001726 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001727 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001728 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001729
1730#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001731 if (prof_def_func())
1732 func_do_profile(fp);
1733#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001734 if (sandbox)
1735 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001736 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001737 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001738 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001739 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001740 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001741 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001742 // Use the line number of the arguments.
1743 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001744
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001745 function_using_block_scopes(fp, evalarg->eval_cstack);
1746
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001747 pt->pt_func = fp;
1748 pt->pt_refcount = 1;
1749 rettv->vval.v_partial = pt;
1750 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001751
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001752 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001753 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001754
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001755theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001756 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001757 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001758 if (types_optional)
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001759 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001760 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001761 ga_clear(&arg_objm);
1762 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001763
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001764 return OK;
1765
1766errret:
1767 ga_clear_strings(&newargs);
1768 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001769 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001770 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001771 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001772 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001773 ga_clear(&arg_objm);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001774 if (fp != NULL)
1775 vim_free(fp->uf_arg_types);
1776 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001777 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001778 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001779 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001780 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001781 return FAIL;
1782}
1783
1784/*
1785 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1786 * name it contains, otherwise return "name".
1787 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1788 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001789 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1790 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001791 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001792 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001793 */
1794 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001795deref_func_name(
1796 char_u *name,
1797 int *lenp,
1798 partial_T **partialp,
1799 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001800 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001801 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001802 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001803{
1804 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001805 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001806 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001807 char_u *s = NULL;
1808 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001809 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001810
1811 if (partialp != NULL)
1812 *partialp = NULL;
1813
1814 cc = name[*lenp];
1815 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001816
Bram Moolenaar71f21932022-01-07 18:20:55 +00001817 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001818 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001819 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001820 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001821 tv = &v->di_tv;
1822 }
1823 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1824 {
1825 imported_T *import;
1826 char_u *p = name;
1827 int len = *lenp;
1828
1829 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001830 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001831 p = name + 2;
1832 len -= 2;
1833 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001834 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001835
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001836 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001837 if (import != NULL)
1838 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001839 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001840 if (new_function)
1841 semsg(_(e_redefining_imported_item_str), name);
1842 else
1843 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001844 name[len] = cc;
1845 *lenp = 0;
1846 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001847 }
1848 }
1849
1850 if (tv != NULL)
1851 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001852 if (found_var != NULL)
1853 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001854 if (tv->v_type == VAR_FUNC)
1855 {
1856 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001857 {
1858 *lenp = 0;
1859 return (char_u *)""; // just in case
1860 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001861 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001862 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001863 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001864
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001865 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001866 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001867 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001868
1869 if (pt == NULL)
1870 {
1871 *lenp = 0;
1872 return (char_u *)""; // just in case
1873 }
1874 if (partialp != NULL)
1875 *partialp = pt;
1876 s = partial_name(pt);
1877 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001878 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001879
1880 if (s != NULL)
1881 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001882 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001883 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001884 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001885
1886 if (sv != NULL)
1887 *type = sv->sv_type;
1888 }
1889 return s;
1890 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001891 }
1892
1893 return name;
1894}
1895
1896/*
1897 * Give an error message with a function name. Handle <SNR> things.
1898 * "ermsg" is to be passed without translation, use N_() instead of _().
1899 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001900 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001901emsg_funcname(char *ermsg, char_u *name)
1902{
Bram Moolenaar54598062022-01-13 12:05:09 +00001903 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001904
Bram Moolenaar54598062022-01-13 12:05:09 +00001905 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001906 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001907 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001908 if (p != name)
1909 vim_free(p);
1910}
1911
1912/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001913 * Get function arguments at "*arg" and advance it.
1914 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001915 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001916 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001917 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001918get_func_arguments(
1919 char_u **arg,
1920 evalarg_T *evalarg,
1921 int partial_argc,
1922 typval_T *argvars,
Ernie Raelb077b582023-12-14 20:11:44 +01001923 int *argcount,
1924 int is_builtin)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001925{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001926 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001927 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001928 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001929 int evaluate = evalarg == NULL
1930 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001931
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001932 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001933 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001934 // skip the '(' or ',' and possibly line breaks
1935 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1936
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001937 if (*argp == ')' || *argp == ',' || *argp == NUL)
1938 break;
Ernie Raelb077b582023-12-14 20:11:44 +01001939
1940 int arg_idx = *argcount;
1941 if (eval1(&argp, &argvars[arg_idx], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001942 {
1943 ret = FAIL;
1944 break;
1945 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001946 ++*argcount;
Ernie Raelb077b582023-12-14 20:11:44 +01001947 if (!is_builtin && check_typval_is_value(&argvars[arg_idx]) == FAIL)
1948 {
1949 ret = FAIL;
1950 break;
1951 }
1952
Bram Moolenaar9d489562020-07-30 20:08:50 +02001953 // The comma should come right after the argument, but this wasn't
1954 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001955 if (vim9script)
1956 {
1957 if (*argp != ',' && *skipwhite(argp) == ',')
1958 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001959 if (evaluate)
1960 semsg(_(e_no_white_space_allowed_before_str_str),
1961 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001962 ret = FAIL;
1963 break;
1964 }
1965 }
1966 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001967 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001968 if (*argp != ',')
1969 break;
Christian Brabandt00cb2472023-09-05 20:46:25 +02001970 if (vim9script && !IS_WHITE_NL_OR_NUL(argp[1]))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001971 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001972 if (evaluate)
1973 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001974 ret = FAIL;
1975 break;
1976 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001977 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001978
Bram Moolenaare6b53242020-07-01 17:28:33 +02001979 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001980 if (*argp == ')')
1981 ++argp;
1982 else
1983 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001984 *arg = argp;
1985 return ret;
1986}
1987
1988/*
1989 * Call a function and put the result in "rettv".
1990 * Return OK or FAIL.
1991 */
1992 int
1993get_func_tv(
1994 char_u *name, // name of the function
1995 int len, // length of "name" or -1 to use strlen()
1996 typval_T *rettv,
1997 char_u **arg, // argument, pointing to the '('
1998 evalarg_T *evalarg, // for line continuation
1999 funcexe_T *funcexe) // various values
2000{
2001 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002002 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002003 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
2004 int argcount = 0; // number of arguments found
2005 int vim9script = in_vim9script();
2006 int evaluate = evalarg == NULL
2007 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
2008
2009 argp = *arg;
2010 ret = get_func_arguments(&argp, evalarg,
2011 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
Ernie Raelb077b582023-12-14 20:11:44 +01002012 argvars, &argcount, builtin_function(name, -1));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002013
2014 if (ret == OK)
2015 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002016 int i = 0;
2017 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002018
2019 if (get_vim_var_nr(VV_TESTING))
2020 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002021 // Prepare for calling test_garbagecollect_now(), need to know
2022 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002023 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002024 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002025 for (i = 0; i < argcount; ++i)
2026 if (ga_grow(&funcargs, 1) == OK)
2027 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
2028 &argvars[i];
2029 }
2030
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002031 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00002032 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002033 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002034 // An error in a builtin function does not return FAIL, but we do
2035 // want to abort further processing if an error was given.
2036 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002037 clear_tv(rettv);
2038 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002039
2040 funcargs.ga_len -= i;
2041 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002042 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002043 {
2044 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002045 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002046 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00002047 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002048 }
2049
2050 while (--argcount >= 0)
2051 clear_tv(&argvars[argcount]);
2052
Bram Moolenaar4525a572022-02-13 11:57:33 +00002053 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02002054 *arg = argp;
2055 else
2056 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002057 return ret;
2058}
2059
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002060/*
2061 * Return TRUE if "p" starts with "<SID>" or "s:".
2062 * Only works if eval_fname_script() returned non-zero for "p"!
2063 */
2064 static int
2065eval_fname_sid(char_u *p)
2066{
2067 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
2068}
2069
2070/*
2071 * In a script change <SID>name() and s:name() to K_SNR 123_name().
2072 * Change <SNR>123_name() to K_SNR 123_name().
2073 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002074 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002075 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002076 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00002077fname_trans_sid(
2078 char_u *name,
2079 char_u *fname_buf,
2080 char_u **tofree,
2081 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002082{
2083 int llen;
2084 char_u *fname;
2085 int i;
2086
2087 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002088 if (llen == 0)
2089 return name; // no prefix
2090
2091 fname_buf[0] = K_SPECIAL;
2092 fname_buf[1] = KS_EXTRA;
2093 fname_buf[2] = (int)KE_SNR;
2094 i = 3;
2095 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002096 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002097 if (current_sctx.sc_sid <= 0)
2098 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002099 else
2100 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002101 sprintf((char *)fname_buf + 3, "%ld_",
2102 (long)current_sctx.sc_sid);
2103 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002104 }
2105 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002106 if (i + STRLEN(name + llen) < FLEN_FIXED)
2107 {
2108 STRCPY(fname_buf + i, name + llen);
2109 fname = fname_buf;
2110 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002111 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002112 {
2113 fname = alloc(i + STRLEN(name + llen) + 1);
2114 if (fname == NULL)
2115 *error = FCERR_OTHER;
2116 else
2117 {
2118 *tofree = fname;
2119 mch_memmove(fname, fname_buf, (size_t)i);
2120 STRCPY(fname + i, name + llen);
2121 }
2122 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002123 return fname;
2124}
2125
2126/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002127 * Concatenate the script ID and function name into "<SNR>99_name".
2128 * "buffer" must have size MAX_FUNC_NAME_LEN.
2129 */
2130 void
2131func_name_with_sid(char_u *name, int sid, char_u *buffer)
2132{
2133 // A script-local function is stored as "<SNR>99_name".
2134 buffer[0] = K_SPECIAL;
2135 buffer[1] = KS_EXTRA;
2136 buffer[2] = (int)KE_SNR;
2137 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2138 (long)sid, name);
2139}
2140
2141/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142 * Find a function "name" in script "sid".
2143 */
2144 static ufunc_T *
2145find_func_with_sid(char_u *name, int sid)
2146{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002147 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002148 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002149
Bram Moolenaarb8822442022-01-11 15:24:05 +00002150 if (!SCRIPT_ID_VALID(sid))
2151 return NULL; // not in a script
2152
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002153 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002154 hi = hash_find(&func_hashtab, buffer);
2155 if (!HASHITEM_EMPTY(hi))
2156 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002157 return NULL;
2158}
2159
2160/*
2161 * Find a function "name" in script "sid" prefixing the autoload prefix.
2162 */
2163 static ufunc_T *
2164find_func_with_prefix(char_u *name, int sid)
2165{
2166 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002167 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002168 scriptitem_T *si;
2169
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002170 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002171 return NULL; // already has the prefix
2172 if (!SCRIPT_ID_VALID(sid))
2173 return NULL; // not in a script
2174 si = SCRIPT_ITEM(sid);
2175 if (si->sn_autoload_prefix != NULL)
2176 {
2177 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2178 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002179 char_u *namep;
2180
2181 // skip a "<SNR>99_" prefix
2182 namep = untrans_function_name(name);
2183 if (namep == NULL)
2184 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002185
2186 // An exported function in an autoload script is stored as
2187 // "dir#path#name".
2188 if (len < sizeof(buffer))
2189 auto_name = buffer;
2190 else
2191 auto_name = alloc(len);
2192 if (auto_name != NULL)
2193 {
2194 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002195 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002196 hi = hash_find(&func_hashtab, auto_name);
2197 if (auto_name != buffer)
2198 vim_free(auto_name);
2199 if (!HASHITEM_EMPTY(hi))
2200 return HI2UF(hi);
2201 }
2202 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002203
2204 return NULL;
2205}
2206
2207/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002208 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002209 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2210 * functions.
2211 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002212 * Return NULL for unknown function.
2213 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002214 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002215find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002216{
2217 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002218 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002220 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002221 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002222 // Find script-local function before global one.
2223 if (in_vim9script() && eval_isnamec1(*name)
2224 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002225 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002226 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2227 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002228 if (func != NULL)
2229 return func;
2230 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002231 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2232 {
2233 char_u *p = name + 5;
2234 long sid;
2235
2236 // printable "<SNR>123_Name" form
2237 sid = getdigits(&p);
2238 if (*p == '_')
2239 {
2240 func = find_func_with_sid(p + 1, (int)sid);
2241 if (func != NULL)
2242 return func;
2243 }
2244 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002246
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002247 if ((flags & FFED_NO_GLOBAL) == 0)
2248 {
2249 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002250 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002251 if (!HASHITEM_EMPTY(hi))
2252 return HI2UF(hi);
2253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002254
Bram Moolenaarb8822442022-01-11 15:24:05 +00002255 // Find autoload function if this is an autoload script.
2256 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2257 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258}
2259
2260/*
2261 * Find a function by name, return pointer to it in ufuncs.
2262 * "cctx" is passed in a :def function to find imported functions.
2263 * Return NULL for unknown or dead function.
2264 */
2265 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002266find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002267{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002268 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269
2270 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2271 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002272 return NULL;
2273}
2274
2275/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002276 * Return TRUE if "ufunc" is a global function.
2277 */
2278 int
2279func_is_global(ufunc_T *ufunc)
2280{
2281 return ufunc->uf_name[0] != K_SPECIAL;
2282}
2283
2284/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002285 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2286 */
2287 int
2288func_requires_g_prefix(ufunc_T *ufunc)
2289{
2290 return ufunc->uf_name[0] != K_SPECIAL
2291 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002292 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01002293 && !SAFE_isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002294}
2295
2296/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002297 * Copy the function name of "fp" to buffer "buf".
2298 * "buf" must be able to hold the function name plus three bytes.
2299 * Takes care of script-local function names.
2300 */
2301 static void
2302cat_func_name(char_u *buf, ufunc_T *fp)
2303{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002304 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002305 {
2306 STRCPY(buf, "<SNR>");
2307 STRCAT(buf, fp->uf_name + 3);
2308 }
2309 else
2310 STRCPY(buf, fp->uf_name);
2311}
2312
2313/*
2314 * Add a number variable "name" to dict "dp" with value "nr".
2315 */
2316 static void
2317add_nr_var(
2318 dict_T *dp,
2319 dictitem_T *v,
2320 char *name,
2321 varnumber_T nr)
2322{
2323 STRCPY(v->di_key, name);
2324 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002325 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002326 v->di_tv.v_type = VAR_NUMBER;
2327 v->di_tv.v_lock = VAR_FIXED;
2328 v->di_tv.vval.v_number = nr;
2329}
2330
2331/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002332 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002333 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002334 static void
2335free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002336{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002337 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002338
Bram Moolenaarca16c602022-09-06 18:57:08 +01002339 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002340 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002341 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002342
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002343 // When garbage collecting a funccall_T may be freed before the
2344 // function that references it, clear its uf_scoped field.
2345 // The function may have been redefined and point to another
2346 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002347 if (fp != NULL && fp->uf_scoped == fc)
2348 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002349 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002350 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002351
Bram Moolenaarca16c602022-09-06 18:57:08 +01002352 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002353 vim_free(fc);
2354}
2355
2356/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002357 * Free "fc" and what it contains.
2358 * Can be called only when "fc" is kept beyond the period of it called,
2359 * i.e. after cleanup_function_call(fc).
2360 */
2361 static void
2362free_funccal_contents(funccall_T *fc)
2363{
2364 listitem_T *li;
2365
2366 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002367 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002368
2369 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002370 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002371
2372 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002373 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002374 clear_tv(&li->li_tv);
2375
2376 free_funccal(fc);
2377}
2378
2379/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002380 * Handle the last part of returning from a function: free the local hashtable.
2381 * Unless it is still in use by a closure.
2382 */
2383 static void
2384cleanup_function_call(funccall_T *fc)
2385{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002386 int may_free_fc = fc->fc_refcount <= 0;
2387 int free_fc = TRUE;
2388
Bram Moolenaarca16c602022-09-06 18:57:08 +01002389 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002390
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002391 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002392 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2393 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002394 else
2395 free_fc = FALSE;
2396
2397 // If the a:000 list and the l: and a: dicts are not referenced and
2398 // there is no closure using it, we can free the funccall_T and what's
2399 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002400 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2401 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002402 else
2403 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002404 int todo;
2405 hashitem_T *hi;
2406 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002407
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002408 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002409
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002410 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002411 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002412 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002413 {
2414 if (!HASHITEM_EMPTY(hi))
2415 {
2416 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002417 di = HI2DI(hi);
2418 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002419 }
2420 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002421 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002422
Bram Moolenaarca16c602022-09-06 18:57:08 +01002423 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2424 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002425 else
2426 {
2427 listitem_T *li;
2428
2429 free_fc = FALSE;
2430
2431 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002432 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002433 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002434 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002435
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002436 if (free_fc)
2437 free_funccal(fc);
2438 else
2439 {
2440 static int made_copy = 0;
2441
2442 // "fc" is still in use. This can happen when returning "a:000",
2443 // assigning "l:" to a global variable or defining a closure.
2444 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002445 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002446 previous_funccal = fc;
2447
2448 if (want_garbage_collect)
2449 // If garbage collector is ready, clear count.
2450 made_copy = 0;
2451 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002452 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002453 // We have made a lot of copies, worth 4 Mbyte. This can happen
2454 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002455 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002456 // too much memory.
2457 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002458 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002459 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002460 }
2461}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002462
2463/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002464 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2465 */
2466 static int
2467numbered_function(char_u *name)
2468{
Keith Thompson184f71c2024-01-04 21:19:04 +01002469 return SAFE_isdigit(*name)
2470 || (name[0] == 'g' && name[1] == ':' && SAFE_isdigit(name[2]));
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002471}
2472
2473/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002474 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002475 * 1. ordinary names, function defined with :function or :def;
2476 * can start with "<SNR>123_" literally or with K_SPECIAL.
2477 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002478 * For the first we only count the name stored in func_hashtab as a reference,
2479 * using function() does not count as a reference, because the function is
2480 * looked up by name.
2481 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002482 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002483func_name_refcount(char_u *name)
2484{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002485 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002486}
2487
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488/*
2489 * Unreference "fc": decrement the reference count and free it when it
2490 * becomes zero. "fp" is detached from "fc".
2491 * When "force" is TRUE we are exiting.
2492 */
2493 static void
2494funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2495{
2496 funccall_T **pfc;
2497 int i;
2498
2499 if (fc == NULL)
2500 return;
2501
2502 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002503 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2504 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2505 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2506 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 {
2508 if (fc == *pfc)
2509 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002510 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511 free_funccal_contents(fc);
2512 return;
2513 }
2514 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002515 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2516 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2517 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518}
2519
2520/*
2521 * Remove the function from the function hashtable. If the function was
2522 * deleted while it still has references this was already done.
2523 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2524 */
2525 static int
2526func_remove(ufunc_T *fp)
2527{
2528 hashitem_T *hi;
2529
2530 // Return if it was already virtually deleted.
2531 if (fp->uf_flags & FC_DEAD)
2532 return FALSE;
2533
2534 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002535 if (HASHITEM_EMPTY(hi))
2536 return FALSE;
2537
2538 // When there is a def-function index do not actually remove the
2539 // function, so we can find the index when defining the function again.
2540 // Do remove it when it's a copy.
2541 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002543 fp->uf_flags |= FC_DEAD;
2544 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002546 hash_remove(&func_hashtab, hi, "remove function");
2547 fp->uf_flags |= FC_DELETED;
2548 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549}
2550
2551 static void
2552func_clear_items(ufunc_T *fp)
2553{
2554 ga_clear_strings(&(fp->uf_args));
2555 ga_clear_strings(&(fp->uf_def_args));
2556 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002558 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002559 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01002560 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002561
2562 // Increment the refcount of this function to avoid it being freed
2563 // recursively when the partial is freed.
2564 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002565 partial_unref(fp->uf_partial);
2566 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002567 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002568
2569#ifdef FEAT_LUA
2570 if (fp->uf_cb_free != NULL)
2571 {
2572 fp->uf_cb_free(fp->uf_cb_state);
2573 fp->uf_cb_free = NULL;
2574 }
2575
2576 fp->uf_cb_state = NULL;
2577 fp->uf_cb = NULL;
2578#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579#ifdef FEAT_PROFILE
2580 VIM_CLEAR(fp->uf_tml_count);
2581 VIM_CLEAR(fp->uf_tml_total);
2582 VIM_CLEAR(fp->uf_tml_self);
2583#endif
2584}
2585
2586/*
2587 * Free all things that a function contains. Does not free the function
2588 * itself, use func_free() for that.
2589 * When "force" is TRUE we are exiting.
2590 */
2591 static void
2592func_clear(ufunc_T *fp, int force)
2593{
2594 if (fp->uf_cleared)
2595 return;
2596 fp->uf_cleared = TRUE;
2597
2598 // clear this function
2599 func_clear_items(fp);
2600 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002601 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602}
2603
2604/*
2605 * Free a function and remove it from the list of functions. Does not free
2606 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002607 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002608 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002610 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002611func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612{
2613 // Only remove it when not done already, otherwise we would remove a newer
2614 // version of the function with the same name.
2615 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2616 func_remove(fp);
2617
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002618 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002619 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002620 if (fp->uf_dfunc_idx > 0)
2621 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002622 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002623 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002624 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002625 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002626 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002627}
2628
2629/*
2630 * Free all things that a function contains and free the function itself.
2631 * When "force" is TRUE we are exiting.
2632 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002633 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634func_clear_free(ufunc_T *fp, int force)
2635{
2636 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002637 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2638 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002639 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002640 else
2641 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642}
2643
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002644/*
2645 * Copy already defined function "lambda" to a new function with name "global".
2646 * This is for when a compiled function defines a global function.
2647 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002648 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002649copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002650 char_u *lambda,
2651 char_u *global,
2652 loopvarinfo_T *loopvarinfo,
2653 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002654{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002655 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002656 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002657
2658 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002659 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002660 semsg(_(e_lambda_function_not_found_str), lambda);
2661 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002662 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002663
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002664 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002665 if (fp != NULL)
2666 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002667 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002668 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002669 return FAIL;
2670 }
2671
Bram Moolenaare01e5212023-01-08 20:31:18 +00002672 fp = alloc_ufunc(global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002673 if (fp == NULL)
2674 return FAIL;
2675
2676 fp->uf_varargs = ufunc->uf_varargs;
2677 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2678 fp->uf_def_status = ufunc->uf_def_status;
2679 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2680 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2681 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2682 == FAIL
2683 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2684 goto failed;
2685
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002686 if (ufunc->uf_arg_types != NULL)
2687 {
2688 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2689 if (fp->uf_arg_types == NULL)
2690 goto failed;
2691 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2692 sizeof(type_T *) * fp->uf_args.ga_len);
2693 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002694 if (ufunc->uf_va_name != NULL)
2695 {
2696 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2697 if (fp->uf_va_name == NULL)
2698 goto failed;
2699 }
2700 fp->uf_ret_type = ufunc->uf_ret_type;
2701
2702 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002703
2704 fp->uf_name_exp = NULL;
2705 set_ufunc_name(fp, global);
2706
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002707 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002708
2709 // the referenced dfunc_T is now used one more time
2710 link_def_function(fp);
2711
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002712 // Create a partial to store the context of the function where it was
2713 // instantiated. Only needs to be done once. Do this on the original
2714 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002715 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2716 {
2717 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2718
2719 if (pt == NULL)
2720 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002721 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002722 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002723 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002724 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002725 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002726 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002727 }
2728
2729 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002730
2731failed:
2732 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002733 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002734}
2735
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002736static int funcdepth = 0;
2737
2738/*
2739 * Increment the function call depth count.
2740 * Return FAIL when going over 'maxfuncdepth'.
2741 * Otherwise return OK, must call funcdepth_decrement() later!
2742 */
2743 int
2744funcdepth_increment(void)
2745{
2746 if (funcdepth >= p_mfd)
2747 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002748 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002749 return FAIL;
2750 }
2751 ++funcdepth;
2752 return OK;
2753}
2754
2755 void
2756funcdepth_decrement(void)
2757{
2758 --funcdepth;
2759}
2760
2761/*
2762 * Get the current function call depth.
2763 */
2764 int
2765funcdepth_get(void)
2766{
2767 return funcdepth;
2768}
2769
2770/*
2771 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002772 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002773 * times as funcdepth_increment().
2774 */
2775 void
2776funcdepth_restore(int depth)
2777{
2778 funcdepth = depth;
2779}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002780
2781/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002782 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2783 * "rettv".
2784 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2785 * Returns NULL when allocation fails.
2786 */
2787 funccall_T *
2788create_funccal(ufunc_T *fp, typval_T *rettv)
2789{
2790 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2791
2792 if (fc == NULL)
2793 return NULL;
2794 fc->fc_caller = current_funccal;
2795 current_funccal = fc;
2796 fc->fc_func = fp;
2797 func_ptr_ref(fp);
2798 fc->fc_rettv = rettv;
2799 return fc;
2800}
2801
2802/*
2803 * To be called when returning from a compiled function; restores
2804 * current_funccal.
2805 */
2806 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002807remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002808{
2809 funccall_T *fc = current_funccal;
2810
2811 current_funccal = fc->fc_caller;
2812 free_funccal(fc);
2813}
2814
2815/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002816 * Call a user function.
2817 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002818 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002819call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002820 ufunc_T *fp, // pointer to function
2821 int argcount, // nr of args
2822 typval_T *argvars, // arguments
2823 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002824 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002825 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002826{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002827 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002828 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002829 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002830 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002831 funccall_T *fc;
2832 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002833 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002834 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002835 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002836 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002837 int i;
2838 int ai;
2839 int islambda = FALSE;
2840 char_u numbuf[NUMBUFLEN];
2841 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002842 typval_T *tv_to_free[MAX_FUNC_ARGS];
2843 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002844#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002845 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002846#endif
ichizok7e5fe382023-04-15 13:17:50 +01002847 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002848
Bram Moolenaarb2049902021-01-24 12:53:53 +01002849#ifdef FEAT_PROFILE
2850 CLEAR_FIELD(profile_info);
2851#endif
2852
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002853 // If depth of calling is getting too high, don't execute the function.
2854 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002855 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002856 rettv->v_type = VAR_NUMBER;
2857 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002858 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002859 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002860
Bram Moolenaare38eab22019-12-05 21:50:01 +01002861 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002862
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002863 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002864 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00002865 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002866 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002867 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002868 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2869 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002870 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002871 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002872
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002873 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002875#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002876 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002877#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002878 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002879#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002880 if (do_profiling == PROF_YES)
2881 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002882#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002883 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002884 if (call_def_function(fp, argcount, argvars, 0,
2885 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
2886 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002887 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002888#ifdef FEAT_PROFILE
2889 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002890 || (caller != NULL && caller->uf_profiling)))
2891 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002892#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002893 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002894 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002895 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 }
2897
Bram Moolenaar38453522021-11-28 22:00:12 +00002898 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002899
2900 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002901 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2902 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2903 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002904 */
2905 /*
2906 * Init l: variables.
2907 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002908 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002909 if (selfdict != NULL)
2910 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002911 // Set l:self to "selfdict". Use "name" to avoid a warning from
2912 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002913 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002914 name = v->di_key;
2915 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002916 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002917 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002918 v->di_tv.v_type = VAR_DICT;
2919 v->di_tv.v_lock = 0;
2920 v->di_tv.vval.v_dict = selfdict;
2921 ++selfdict->dv_refcount;
2922 }
2923
2924 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002925 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002926 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002927 * Set a:000 to a list with room for the "..." arguments.
2928 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002929 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002930 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002931 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002932 (varnumber_T)(argcount >= fp->uf_args.ga_len
2933 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002934 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002935 if ((fp->uf_flags & FC_NOARGS) == 0)
2936 {
2937 // Use "name" to avoid a warning from some compiler that checks the
2938 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002939 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002940 name = v->di_key;
2941 STRCPY(name, "000");
2942 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002943 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002944 v->di_tv.v_type = VAR_LIST;
2945 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002946 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002947 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002948 CLEAR_FIELD(fc->fc_l_varlist);
2949 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2950 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002951
2952 /*
2953 * Set a:firstline to "firstline" and a:lastline to "lastline".
2954 * Set a:name to named arguments.
2955 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002956 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002957 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002958 if ((fp->uf_flags & FC_NOARGS) == 0)
2959 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002960 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2961 "firstline", (varnumber_T)funcexe->fe_firstline);
2962 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2963 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002964 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002965 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002966 {
2967 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002968 typval_T def_rettv;
2969 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002970
2971 ai = i - fp->uf_args.ga_len;
2972 if (ai < 0)
2973 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002974 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002975 name = FUNCARG(fp, i);
2976 if (islambda)
2977 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002978
2979 // evaluate named argument default expression
2980 isdefault = ai + fp->uf_def_args.ga_len >= 0
2981 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2982 && argvars[i].vval.v_number == VVAL_NONE));
2983 if (isdefault)
2984 {
2985 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002986
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002987 def_rettv.v_type = VAR_NUMBER;
2988 def_rettv.vval.v_number = -1;
2989
2990 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2991 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002992 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002993 {
2994 default_arg_err = 1;
2995 break;
2996 }
2997 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002998 }
2999 else
3000 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003001 if ((fp->uf_flags & FC_NOARGS) != 0)
3002 // Bail out if no a: arguments used (in lambda).
3003 break;
3004
Bram Moolenaare38eab22019-12-05 21:50:01 +01003005 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003006 sprintf((char *)numbuf, "%d", ai + 1);
3007 name = numbuf;
3008 }
3009 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
3010 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003011 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003012 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003013 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003014 }
3015 else
3016 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003017 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003018 if (v == NULL)
3019 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003020 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003021 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003022
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003023 // Note: the values are copied directly to avoid alloc/free.
3024 // "argvars" must have VAR_FIXED for v_lock.
3025 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003026 v->di_tv.v_lock = VAR_FIXED;
3027
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003028 if (isdefault)
3029 // Need to free this later, no matter where it's stored.
3030 tv_to_free[tv_to_free_len++] = &v->di_tv;
3031
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003032 if (addlocal)
3033 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003034 // Named arguments should be accessed without the "a:" prefix in
3035 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003036 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003037 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003038 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003039 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003040 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003041
3042 if (ai >= 0 && ai < MAX_FUNC_ARGS)
3043 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003044 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003045
3046 li->li_tv = argvars[i];
3047 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003048 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003049 }
3050 }
3051
Bram Moolenaare38eab22019-12-05 21:50:01 +01003052 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003053 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003054
3055 if (fp->uf_flags & FC_SANDBOX)
3056 {
3057 using_sandbox = TRUE;
3058 ++sandbox;
3059 }
3060
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003061 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003062 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003063 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003064 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003065 ++no_wait_return;
3066 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003067
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003068 smsg(_("calling %s"), SOURCING_NAME);
3069 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003070 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003071 char_u buf[MSG_BUF_LEN];
3072 char_u numbuf2[NUMBUFLEN];
3073 char_u *tofree;
3074 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003075
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003076 msg_puts("(");
3077 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003078 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003079 if (i > 0)
3080 msg_puts(", ");
3081 if (argvars[i].v_type == VAR_NUMBER)
3082 msg_outnum((long)argvars[i].vval.v_number);
3083 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003084 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003085 // Do not want errors such as E724 here.
3086 ++emsg_off;
3087 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3088 --emsg_off;
3089 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003090 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003091 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003092 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003093 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3094 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003095 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003096 msg_puts((char *)s);
3097 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003098 }
3099 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003100 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003101 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003102 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003103 msg_puts("\n"); // don't overwrite this either
3104
3105 verbose_leave_scroll();
3106 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003107 }
3108#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003109 if (do_profiling == PROF_YES)
3110 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003111 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003112#endif
3113
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003114 // "legacy" does not apply to commands in the function
3115 sticky_cmdmod_flags = 0;
3116
Bram Moolenaar98aff652022-09-06 21:02:35 +01003117 // If called from a compiled :def function the execution context must be
3118 // hidden, any deferred functions need to be added to the function being
3119 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003120 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003121
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003122 save_current_sctx = current_sctx;
3123 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003124 save_did_emsg = did_emsg;
3125 did_emsg = FALSE;
3126
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003127 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003128 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003129 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003130 retval = FCERR_FAILED;
3131 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003132 else if (islambda)
3133 {
3134 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3135
3136 // A Lambda always has the command "return {expr}". It is much faster
3137 // to evaluate {expr} directly.
3138 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003139 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003140 --ex_nesting_level;
3141 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003142 else
3143 // call do_cmdline() to execute the lines
3144 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003145 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3146
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003147 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003148 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003149
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003150 if (RedrawingDisabled > 0)
3151 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003152
Bram Moolenaare38eab22019-12-05 21:50:01 +01003153 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003154 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3155 {
3156 clear_tv(rettv);
3157 rettv->v_type = VAR_NUMBER;
3158 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003159
3160 // In corner cases returning a "failed" value is not backwards
3161 // compatible. Only do this for Vim9 script.
3162 if (in_vim9script())
3163 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003164 }
3165
3166#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003167 if (do_profiling == PROF_YES)
3168 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003169 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003170
3171 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3172 profile_may_end_func(&profile_info, fp, caller);
3173 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003174#endif
3175
Bram Moolenaare38eab22019-12-05 21:50:01 +01003176 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003177 if (p_verbose >= 12)
3178 {
3179 ++no_wait_return;
3180 verbose_enter_scroll();
3181
3182 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003183 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003184 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003185 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003186 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003187 else
3188 {
3189 char_u buf[MSG_BUF_LEN];
3190 char_u numbuf2[NUMBUFLEN];
3191 char_u *tofree;
3192 char_u *s;
3193
Bram Moolenaare38eab22019-12-05 21:50:01 +01003194 // The value may be very long. Skip the middle part, so that we
3195 // have some idea how it starts and ends. smsg() would always
3196 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003197 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003198 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003199 --emsg_off;
3200 if (s != NULL)
3201 {
3202 if (vim_strsize(s) > MSG_BUF_CLEN)
3203 {
3204 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3205 s = buf;
3206 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003207 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003208 vim_free(tofree);
3209 }
3210 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003211 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003212
3213 verbose_leave_scroll();
3214 --no_wait_return;
3215 }
3216
ichizok7e5fe382023-04-15 13:17:50 +01003217 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003218 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003219 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003220 restore_current_ectx(save_current_ectx);
3221
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003222#ifdef FEAT_PROFILE
3223 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003224 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003225#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003226 if (using_sandbox)
3227 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003228 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003229
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003230 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003231 {
3232 ++no_wait_return;
3233 verbose_enter_scroll();
3234
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003235 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003236 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003237
3238 verbose_leave_scroll();
3239 --no_wait_return;
3240 }
3241
3242 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003243 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003244 for (i = 0; i < tv_to_free_len; ++i)
3245 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003246 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003247
3248 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003249}
3250
3251/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003252 * Check the argument count for user function "fp".
3253 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3254 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003255 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003256check_user_func_argcount(ufunc_T *fp, int argcount)
3257{
3258 int regular_args = fp->uf_args.ga_len;
3259
3260 if (argcount < regular_args - fp->uf_def_args.ga_len)
3261 return FCERR_TOOFEW;
3262 else if (!has_varargs(fp) && argcount > regular_args)
3263 return FCERR_TOOMANY;
3264 return FCERR_UNKNOWN;
3265}
3266
3267/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003269 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003270 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271call_user_func_check(
3272 ufunc_T *fp,
3273 int argcount,
3274 typval_T *argvars,
3275 typval_T *rettv,
3276 funcexe_T *funcexe,
3277 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003278{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003279 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003280
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003281#ifdef FEAT_LUA
3282 if (fp->uf_flags & FC_CFUNC)
3283 {
3284 cfunc_T cb = fp->uf_cb;
3285
3286 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3287 }
3288#endif
3289
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003290 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3291 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003292 error = check_user_func_argcount(fp, argcount);
3293 if (error != FCERR_UNKNOWN)
3294 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003295
Bram Moolenaar50824712020-12-20 21:10:17 +01003296 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003297 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003299 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003301 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 int did_save_redo = FALSE;
3303 save_redo_T save_redo;
3304
3305 /*
3306 * Call the user function.
3307 * Save and restore search patterns, script variables and
3308 * redo buffer.
3309 */
3310 save_search_patterns();
3311 if (!ins_compl_active())
3312 {
3313 saveRedobuff(&save_redo);
3314 did_save_redo = TRUE;
3315 }
3316 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003317 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003318 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3320 // Function was unreferenced while being used, free it now.
3321 func_clear_free(fp, FALSE);
3322 if (did_save_redo)
3323 restoreRedobuff(&save_redo);
3324 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003325 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003326
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003328}
3329
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003330static funccal_entry_T *funccal_stack = NULL;
3331
3332/*
3333 * Save the current function call pointer, and set it to NULL.
3334 * Used when executing autocommands and for ":source".
3335 */
3336 void
3337save_funccal(funccal_entry_T *entry)
3338{
3339 entry->top_funccal = current_funccal;
3340 entry->next = funccal_stack;
3341 funccal_stack = entry;
3342 current_funccal = NULL;
3343}
3344
3345 void
3346restore_funccal(void)
3347{
3348 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003349 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003350 else
3351 {
3352 current_funccal = funccal_stack->top_funccal;
3353 funccal_stack = funccal_stack->next;
3354 }
3355}
3356
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003357 funccall_T *
3358get_current_funccal(void)
3359{
3360 return current_funccal;
3361}
3362
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003363/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003364 * Return TRUE when currently at the script level:
3365 * - not in a function
3366 * - not executing an autocommand
3367 * Note that when an autocommand sources a script the result is FALSE;
3368 */
3369 int
3370at_script_level(void)
3371{
3372 return current_funccal == NULL && autocmd_match == NULL;
3373}
3374
3375/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003376 * Mark all functions of script "sid" as deleted.
3377 */
3378 void
3379delete_script_functions(int sid)
3380{
3381 hashitem_T *hi;
3382 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003383 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003384 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003385 size_t len;
3386
3387 buf[0] = K_SPECIAL;
3388 buf[1] = KS_EXTRA;
3389 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003390 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003391 len = STRLEN(buf);
3392
Bram Moolenaarce658352020-07-31 23:47:12 +02003393 while (todo > 0)
3394 {
3395 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003396 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003397 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003398 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003399 fp = HI2UF(hi);
3400 if (STRNCMP(fp->uf_name, buf, len) == 0)
3401 {
3402 int changed = func_hashtab.ht_changed;
3403
3404 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003405
3406 if (fp->uf_calls > 0)
3407 {
3408 // Function is executing, don't free it but do remove
3409 // it from the hashtable.
3410 if (func_remove(fp))
3411 fp->uf_refcount--;
3412 }
3413 else
3414 {
3415 func_clear(fp, TRUE);
3416 // When clearing a function another function can be
3417 // cleared as a side effect. When that happens start
3418 // over.
3419 if (changed != func_hashtab.ht_changed)
3420 break;
3421 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003422 }
3423 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003424 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003425 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003426}
3427
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003428#if defined(EXITFREE) || defined(PROTO)
3429 void
3430free_all_functions(void)
3431{
3432 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003433 ufunc_T *fp;
3434 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003435 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003436 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003437
Bram Moolenaare38eab22019-12-05 21:50:01 +01003438 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003439 while (current_funccal != NULL)
3440 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003441 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003442 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003443 if (current_funccal == NULL && funccal_stack != NULL)
3444 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003445 }
3446
Bram Moolenaare38eab22019-12-05 21:50:01 +01003447 // First clear what the functions contain. Since this may lower the
3448 // reference count of a function, it may also free a function and change
3449 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003450 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 Moolenaar03ff9bc2017-02-02 22:59:27 +01003454 if (!HASHITEM_EMPTY(hi))
3455 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 // clear the def function index now
3457 fp = HI2UF(hi);
3458 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003459 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460
Bram Moolenaare38eab22019-12-05 21:50:01 +01003461 // Only free functions that are not refcounted, those are
3462 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003463 if (func_name_refcount(fp->uf_name))
3464 ++skipped;
3465 else
3466 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003467 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003468 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003469 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003470 {
3471 skipped = 0;
3472 break;
3473 }
3474 }
3475 --todo;
3476 }
3477 }
3478
Bram Moolenaare38eab22019-12-05 21:50:01 +01003479 // Now actually free the functions. Need to start all over every time,
3480 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003481 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003482 while (func_hashtab.ht_used > skipped)
3483 {
3484 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003485 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003486 if (!HASHITEM_EMPTY(hi))
3487 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003488 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003489 // Only free functions that are not refcounted, those are
3490 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003491 fp = HI2UF(hi);
3492 if (func_name_refcount(fp->uf_name))
3493 ++skipped;
3494 else
3495 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003496 if (func_free(fp, FALSE) == OK)
3497 {
3498 skipped = 0;
3499 break;
3500 }
3501 // did not actually free it
3502 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003503 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003504 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003505 }
3506 if (skipped == 0)
3507 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508
3509 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003510}
3511#endif
3512
3513/*
3514 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003515 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3516 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003517 * "len" is the length of "name", or -1 for NUL terminated.
3518 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003520builtin_function(char_u *name, int len)
3521{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003522 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003523
Bram Moolenaara26b9702020-04-18 19:53:28 +02003524 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003525 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003526 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3527 {
3528 if (name[i] == AUTOLOAD_CHAR)
3529 return FALSE;
3530 if (!eval_isnamec(name[i]))
3531 {
3532 // "name.something" is not a builtin function
3533 if (name[i] == '.')
3534 return FALSE;
3535 break;
3536 }
3537 }
3538 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003539}
3540
3541 int
3542func_call(
3543 char_u *name,
3544 typval_T *args,
3545 partial_T *partial,
3546 dict_T *selfdict,
3547 typval_T *rettv)
3548{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003549 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003550 listitem_T *item;
3551 typval_T argv[MAX_FUNC_ARGS + 1];
3552 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003553 int r = 0;
3554
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003555 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003556 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003557 {
3558 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3559 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003560 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003561 break;
3562 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003563 // Make a copy of each argument. This is needed to be able to set
3564 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003565 copy_tv(&item->li_tv, &argv[argc++]);
3566 }
3567
3568 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003569 {
3570 funcexe_T funcexe;
3571
Bram Moolenaara80faa82020-04-12 19:37:17 +02003572 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003573 funcexe.fe_firstline = curwin->w_cursor.lnum;
3574 funcexe.fe_lastline = curwin->w_cursor.lnum;
3575 funcexe.fe_evaluate = TRUE;
3576 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003577 if (partial != NULL)
3578 {
3579 funcexe.fe_object = partial->pt_obj;
3580 if (funcexe.fe_object != NULL)
3581 ++funcexe.fe_object->obj_refcount;
3582 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003583 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003584 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3585 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003586
Bram Moolenaare38eab22019-12-05 21:50:01 +01003587 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003588 while (argc > 0)
3589 clear_tv(&argv[--argc]);
3590
3591 return r;
3592}
3593
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003594static int callback_depth = 0;
3595
3596 int
3597get_callback_depth(void)
3598{
3599 return callback_depth;
3600}
3601
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003602/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003603 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003604 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003605 */
3606 int
3607call_callback(
3608 callback_T *callback,
3609 int len, // length of "name" or -1 to use strlen()
3610 typval_T *rettv, // return value goes here
3611 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003612 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003613 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003614{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003615 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003616 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003617
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003618 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3619 return FAIL;
Christian Brabandt47510f32023-10-15 09:56:16 +02003620
zeertzjqfe583b12023-12-21 16:59:26 +01003621 if (callback_depth > p_mfd)
Christian Brabandt47510f32023-10-15 09:56:16 +02003622 {
3623 emsg(_(e_command_too_recursive));
3624 return FAIL;
3625 }
3626
Bram Moolenaara80faa82020-04-12 19:37:17 +02003627 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003628 funcexe.fe_evaluate = TRUE;
3629 funcexe.fe_partial = callback->cb_partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003630 if (callback->cb_partial != NULL)
3631 {
3632 funcexe.fe_object = callback->cb_partial->pt_obj;
3633 if (funcexe.fe_object != NULL)
3634 ++funcexe.fe_object->obj_refcount;
3635 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003636 ++callback_depth;
3637 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3638 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003639
3640 // When a :def function was called that uses :try an error would be turned
3641 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003642 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003643 {
3644 need_rethrow = FALSE;
3645 handle_did_throw();
3646 }
3647
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003648 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003649}
3650
3651/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003652 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003653 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003654 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3655 */
3656 varnumber_T
3657call_callback_retnr(
3658 callback_T *callback,
3659 int argcount, // number of "argvars"
3660 typval_T *argvars) // vars for arguments, must have "argcount"
3661 // PLUS ONE elements!
3662{
3663 typval_T rettv;
3664 varnumber_T retval;
3665
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003666 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003667 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003668
3669 retval = tv_get_number_chk(&rettv, NULL);
3670 clear_tv(&rettv);
3671 return retval;
3672}
3673
3674/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 * Give an error message for the result of a function.
3676 * Nothing if "error" is FCERR_NONE.
3677 */
3678 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003679user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680{
3681 switch (error)
3682 {
3683 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003684 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003685 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003686 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003687 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 break;
3689 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003690 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 break;
3692 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003693 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 break;
3695 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003696 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 break;
3698 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003699 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 break;
3701 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003702 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 break;
3704 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003705 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3706 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003708 case FCERR_OTHER:
3709 case FCERR_FAILED:
3710 // assume the error message was already given
3711 break;
3712 case FCERR_NONE:
3713 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 }
3715}
3716
3717/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003718 * Check the argument types "argvars[argcount]" for "name" using the
3719 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3720 * is already included in "argvars[]".
3721 * Will do nothing if "funcexe->fe_check_type" is NULL or
3722 * "funcexe->fe_evaluate" is FALSE;
3723 * Returns an FCERR_ value.
3724 */
3725 static funcerror_T
3726may_check_argument_types(
3727 funcexe_T *funcexe,
3728 typval_T *argvars,
3729 int argcount,
3730 int base_included,
3731 char_u *name)
3732{
3733 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3734 {
3735 // Check that the argument types are OK for the types of the funcref.
3736 if (check_argument_types(funcexe->fe_check_type,
3737 argvars, argcount,
3738 base_included ? NULL : funcexe->fe_basetv,
3739 name) == FAIL)
3740 return FCERR_OTHER;
3741 }
3742 return FCERR_NONE;
3743}
3744
3745/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003746 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003747 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003748 * Return FAIL when the function can't be called, OK otherwise.
3749 * Also returns OK when an error was encountered while executing the function.
3750 */
3751 int
3752call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003753 char_u *funcname, // name of the function
3754 int len, // length of "name" or -1 to use strlen()
3755 typval_T *rettv, // return value goes here
3756 int argcount_in, // number of "argvars"
3757 typval_T *argvars_in, // vars for arguments, must have "argcount"
3758 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003759 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003760{
3761 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003762 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003763 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003764 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003765 char_u fname_buf[FLEN_FIXED + 1];
3766 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003767 char_u *fname = NULL;
3768 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003769 int argcount = argcount_in;
3770 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003771 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003772 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003773 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003774 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003775 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003776 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003777 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003778 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003779
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003780 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3781 // even when call_func() returns FAIL.
3782 rettv->v_type = VAR_UNKNOWN;
3783
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003784 if (partial != NULL)
3785 fp = partial->pt_func;
3786 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003787 fp = funcexe->fe_ufunc;
3788
3789 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003790 {
3791 // Make a copy of the name, if it comes from a funcref variable it
3792 // could be changed or deleted in the called function.
3793 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3794 if (name == NULL)
3795 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003796
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003797 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3798 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003799
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003800 if (funcexe->fe_doesrange != NULL)
3801 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003802
3803 if (partial != NULL)
3804 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003805 // When the function has a partial with a dict and there is a dict
3806 // argument, use the dict argument. That is backwards compatible.
3807 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003808 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003809 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003810 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003811 {
3812 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003813 {
3814 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3815 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003816 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003817 goto theend;
3818 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003819 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003820 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003821 for (i = 0; i < argcount_in; ++i)
3822 argv[i + argv_clear] = argvars_in[i];
3823 argvars = argv;
3824 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003825
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003826 if (funcexe->fe_check_type != NULL
3827 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003828 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003829 // Now funcexe->fe_check_type is missing the added arguments,
3830 // make a copy of the type with the correction.
3831 check_type = *funcexe->fe_check_type;
3832 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003833 check_type.tt_args = check_type_args;
3834 CLEAR_FIELD(check_type_args);
3835 for (i = 0; i < check_type.tt_argcount; ++i)
3836 check_type_args[i + partial->pt_argc] =
3837 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003838 check_type.tt_argcount += partial->pt_argc;
3839 check_type.tt_min_argcount += partial->pt_argc;
3840 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003841 }
3842 }
3843
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003844 if (error == FCERR_NONE)
3845 // check the argument types if possible
3846 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
3847 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003848
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003849 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003850 {
3851 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003852 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003853
Bram Moolenaar333894b2020-08-01 18:53:07 +02003854 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003855 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003856 {
3857 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003858 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003859 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003860
Bram Moolenaare38eab22019-12-05 21:50:01 +01003861 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003862 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003863 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003864
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003865 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003866 {
3867 /*
3868 * User defined function.
3869 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003870 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003871 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003872 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003873 if (fp != NULL && !is_global && in_vim9script()
3874 && func_requires_g_prefix(fp))
3875 // In Vim9 script g: is required to find a global
3876 // non-autoload function.
3877 fp = NULL;
3878 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003879
Bram Moolenaare38eab22019-12-05 21:50:01 +01003880 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003881 if (fp == NULL
3882 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003883 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003884 && !aborting())
3885 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003886 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003887 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003888 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003889 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003890 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3891 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003892 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003893 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003894 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003895 if (fp == NULL)
3896 {
3897 char_u *p = untrans_function_name(rfname);
3898
3899 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003900 // Don't do this if the name starts with "s:".
3901 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003902 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003903 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003904
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003905 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003906 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003907 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003908 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003909 int need_arg_check = FALSE;
3910 if (funcexe->fe_check_type == NULL)
3911 {
3912 funcexe->fe_check_type = fp->uf_func_type;
3913 need_arg_check = TRUE;
3914 }
3915
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003916 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003917 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003918 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003919 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003920 argv_clear, fp);
3921 need_arg_check = TRUE;
3922 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003923
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003924 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003925 {
3926 // Method call: base->Method()
3927 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003928 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003929 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003930 argvars = argv;
3931 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003932 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003933 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003934
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003935 // Check the argument types now that the function type and all
3936 // argument values are known, if not done above.
3937 if (need_arg_check)
3938 error = may_check_argument_types(funcexe, argvars, argcount,
3939 TRUE, (name != NULL) ? name : funcname);
3940 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
3941 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003943 }
3944 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003945 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003946 {
3947 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003948 * expr->method(): Find the method name in the table, call its
3949 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003950 */
3951 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003952 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003953 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003954 else
3955 {
3956 /*
3957 * Find the function name in the table, call its implementation.
3958 */
3959 error = call_internal_func(fname, argcount, argvars, rettv);
3960 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003961
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003962 /*
3963 * The function call (or "FuncUndefined" autocommand sequence) might
3964 * have been aborted by an error, an interrupt, or an explicitly thrown
3965 * exception that has not been caught so far. This situation can be
3966 * tested for by calling aborting(). For an error in an internal
3967 * function or for the "E132" error in call_user_func(), however, the
3968 * throw point at which the "force_abort" flag (temporarily reset by
3969 * emsg()) is normally updated has not been reached yet. We need to
3970 * update that flag first to make aborting() reliable.
3971 */
3972 update_force_abort();
3973 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003974 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003975 ret = OK;
3976
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003977theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003978 /*
3979 * Report an error unless the argument evaluation or function call has been
3980 * cancelled due to an aborting error, an interrupt, or an exception.
3981 */
3982 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003983 user_func_error(error, (name != NULL) ? name : funcname,
3984 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003985
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003986 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003987 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003988 clear_tv(&argv[--argv_clear + argv_base]);
3989
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003990 vim_free(tofree);
3991 vim_free(name);
3992
3993 return ret;
3994}
3995
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003996/*
3997 * Call a function without arguments, partial or dict.
3998 * This is like call_func() when the call is only "FuncName()".
3999 * To be used by "expr" options.
4000 * Returns NOTDONE when the function could not be found.
4001 */
4002 int
4003call_simple_func(
4004 char_u *funcname, // name of the function
4005 int len, // length of "name" or -1 to use strlen()
4006 typval_T *rettv) // return value goes here
4007{
4008 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00004009 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004010 char_u fname_buf[FLEN_FIXED + 1];
4011 char_u *tofree = NULL;
4012 char_u *name;
4013 char_u *fname;
4014 char_u *rfname;
4015 int is_global = FALSE;
4016 ufunc_T *fp;
4017
4018 rettv->v_type = VAR_NUMBER; // default rettv is number zero
4019 rettv->vval.v_number = 0;
4020
4021 // Make a copy of the name, an option can be changed in the function.
4022 name = vim_strnsave(funcname, len);
4023 if (name == NULL)
4024 return ret;
4025
4026 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
4027
4028 // Skip "g:" before a function name.
4029 if (fname[0] == 'g' && fname[1] == ':')
4030 {
4031 is_global = TRUE;
4032 rfname = fname + 2;
4033 }
4034 else
4035 rfname = fname;
4036 fp = find_func(rfname, is_global);
4037 if (fp != NULL && !is_global && in_vim9script()
4038 && func_requires_g_prefix(fp))
4039 // In Vim9 script g: is required to find a global non-autoload
4040 // function.
4041 fp = NULL;
4042 if (fp == NULL)
4043 ret = NOTDONE;
4044 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
4045 error = FCERR_DELETED;
4046 else if (fp != NULL)
4047 {
4048 typval_T argvars[1];
4049 funcexe_T funcexe;
4050
4051 argvars[0].v_type = VAR_UNKNOWN;
4052 CLEAR_FIELD(funcexe);
4053 funcexe.fe_evaluate = TRUE;
4054
4055 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
4056 if (error == FCERR_NONE)
4057 ret = OK;
4058 }
4059
4060 user_func_error(error, name, FALSE);
4061 vim_free(tofree);
4062 vim_free(name);
4063
4064 return ret;
4065}
4066
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004067 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068printable_func_name(ufunc_T *fp)
4069{
4070 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4071}
4072
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004073/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004074 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4075 * TRUE. Otherwise return FALSE.
4076 */
4077 static int
4078function_list_modified(int prev_ht_changed)
4079{
4080 if (prev_ht_changed != func_hashtab.ht_changed)
4081 {
4082 emsg(_(e_function_list_was_modified));
4083 return TRUE;
4084 }
4085 return FALSE;
4086}
4087
4088/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004089 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004090 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004091 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004092list_func_head(ufunc_T *fp, int indent)
4093{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004094 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004095 int j;
4096
4097 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004098
4099 // a timer at the more prompt may have deleted the function
4100 if (function_list_modified(prev_ht_changed))
4101 return FAIL;
4102
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004103 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004104 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004105 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004106 msg_puts("def ");
4107 else
4108 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004109 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004110 msg_putchar('(');
4111 for (j = 0; j < fp->uf_args.ga_len; ++j)
4112 {
4113 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004114 msg_puts(", ");
4115 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116 if (fp->uf_arg_types != NULL)
4117 {
4118 char *tofree;
4119
4120 msg_puts(": ");
4121 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4122 vim_free(tofree);
4123 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004124 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4125 {
4126 msg_puts(" = ");
4127 msg_puts(((char **)(fp->uf_def_args.ga_data))
4128 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4129 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004130 }
4131 if (fp->uf_varargs)
4132 {
4133 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004134 msg_puts(", ");
4135 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004136 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 if (fp->uf_va_name != NULL)
4138 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004139 if (!fp->uf_varargs)
4140 {
4141 if (j)
4142 msg_puts(", ");
4143 msg_puts("...");
4144 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004146 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147 {
4148 char *tofree;
4149
4150 msg_puts(": ");
4151 msg_puts(type_name(fp->uf_va_type, &tofree));
4152 vim_free(tofree);
4153 }
4154 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004155 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004156
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004157 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004158 {
4159 if (fp->uf_ret_type != &t_void)
4160 {
4161 char *tofree;
4162
4163 msg_puts(": ");
4164 msg_puts(type_name(fp->uf_ret_type, &tofree));
4165 vim_free(tofree);
4166 }
4167 }
4168 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004169 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004170 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004171 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004172 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004173 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004174 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004175 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004176 msg_clr_eos();
4177 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004178 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004179
4180 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004181}
4182
4183/*
4184 * Get a function name, translating "<SID>" and "<SNR>".
4185 * Also handles a Funcref in a List or Dictionary.
4186 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004187 * Set "*is_global" to TRUE when the function must be global, unless
4188 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004189 * flags:
4190 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004191 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004192 * TFN_QUIET: be quiet
4193 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004194 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004195 * Advances "pp" to just after the function name (if no error).
4196 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004197 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004198trans_function_name(
4199 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004200 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004201 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004202 int flags)
4203{
4204 return trans_function_name_ext(pp, is_global, skip, flags,
4205 NULL, NULL, NULL, NULL);
4206}
4207
4208/*
4209 * trans_function_name() with extra arguments.
4210 * "fdp", "partial", "type" and "ufunc" can be NULL.
4211 */
4212 char_u *
4213trans_function_name_ext(
4214 char_u **pp,
4215 int *is_global,
4216 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004217 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004218 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004219 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004220 type_T **type, // return: type of funcref
4221 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004222{
4223 char_u *name = NULL;
4224 char_u *start;
4225 char_u *end;
4226 int lead;
4227 char_u sid_buf[20];
4228 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004230 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004231 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004232 int vim9script = in_vim9script();
4233 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004234
4235 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004236 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004237 start = *pp;
4238
Bram Moolenaare38eab22019-12-05 21:50:01 +01004239 // Check for hard coded <SNR>: already translated function ID (from a user
4240 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004241 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4242 && (*pp)[2] == (int)KE_SNR)
4243 {
4244 *pp += 3;
4245 len = get_id_len(pp) + 3;
4246 return vim_strnsave(start, len);
4247 }
4248
Bram Moolenaare38eab22019-12-05 21:50:01 +01004249 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4250 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004251 lead = eval_fname_script(start);
4252 if (lead > 2)
4253 start += lead;
4254
Bram Moolenaare38eab22019-12-05 21:50:01 +01004255 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004256 end = get_lval(start, NULL, &lv, FALSE, skip,
4257 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004258 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004259 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004260 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004261 {
4262 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004263 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004264 goto theend;
4265 }
4266 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4267 {
4268 /*
4269 * Report an invalid expression in braces, unless the expression
4270 * evaluation has been cancelled due to an aborting error, an
4271 * interrupt, or an exception.
4272 */
4273 if (!aborting())
4274 {
4275 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004276 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004277 }
4278 else
4279 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4280 goto theend;
4281 }
4282
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004283 if (lv.ll_ufunc != NULL)
4284 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004285 if (ufunc != NULL)
4286 *ufunc = lv.ll_ufunc;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004287 name = vim_strsave(lv.ll_ufunc->uf_name);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004288 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004289 goto theend;
4290 }
4291
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004292 if (lv.ll_tv != NULL)
4293 {
4294 if (fdp != NULL)
4295 {
4296 fdp->fd_dict = lv.ll_dict;
4297 fdp->fd_newkey = lv.ll_newkey;
4298 lv.ll_newkey = NULL;
4299 fdp->fd_di = lv.ll_di;
4300 }
4301 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4302 {
4303 name = vim_strsave(lv.ll_tv->vval.v_string);
4304 *pp = end;
4305 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004306 else if (lv.ll_tv->v_type == VAR_CLASS
4307 && lv.ll_tv->vval.v_class != NULL)
4308 {
4309 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4310 *pp = end;
4311 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004312 else if (lv.ll_tv->v_type == VAR_PARTIAL
4313 && lv.ll_tv->vval.v_partial != NULL)
4314 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004315 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004316 *pp = end;
4317 if (partial != NULL)
4318 *partial = lv.ll_tv->vval.v_partial;
4319 }
4320 else
4321 {
4322 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4323 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004324 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004325 else
4326 *pp = end;
4327 name = NULL;
4328 }
4329 goto theend;
4330 }
4331
4332 if (lv.ll_name == NULL)
4333 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004334 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004335 *pp = end;
4336 goto theend;
4337 }
4338
Bram Moolenaare38eab22019-12-05 21:50:01 +01004339 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004340 if (lv.ll_exp_name != NULL)
4341 {
4342 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004343 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004344 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004345 if (name == lv.ll_exp_name)
4346 name = NULL;
4347 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004348 else if (lv.ll_sid > 0)
4349 {
4350 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4351 int cc = *lv.ll_name_end;
4352
4353 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4354 *lv.ll_name_end = NUL;
4355 if (si->sn_autoload_prefix != NULL)
4356 {
4357 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4358 }
4359 else
4360 {
4361 sid_buf[0] = K_SPECIAL;
4362 sid_buf[1] = KS_EXTRA;
4363 sid_buf[2] = (int)KE_SNR;
4364 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004365 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004366 name = concat_str(sid_buf, lv.ll_name);
4367 }
4368 *lv.ll_name_end = cc;
4369 *pp = end;
4370 goto theend;
4371 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004372 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004373 {
4374 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004375 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004376 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004377 if (name == *pp)
4378 name = NULL;
4379 }
4380 if (name != NULL)
4381 {
4382 name = vim_strsave(name);
4383 *pp = end;
4384 if (STRNCMP(name, "<SNR>", 5) == 0)
4385 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004386 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004387 name[0] = K_SPECIAL;
4388 name[1] = KS_EXTRA;
4389 name[2] = (int)KE_SNR;
4390 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4391 }
4392 goto theend;
4393 }
4394
4395 if (lv.ll_exp_name != NULL)
4396 {
4397 len = (int)STRLEN(lv.ll_exp_name);
4398 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4399 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4400 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004401 // When there was "s:" already or the name expanded to get a
4402 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004403 lv.ll_name += 2;
4404 len -= 2;
4405 lead = 2;
4406 }
4407 }
4408 else
4409 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004410 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004411 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004412 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004413 if (lv.ll_name[0] == 'g')
4414 {
4415 if (is_global != NULL)
4416 {
4417 *is_global = TRUE;
4418 }
4419 else
4420 {
4421 // dropping "g:" without setting "is_global" won't work in
4422 // Vim9script, put it back later
4423 prefix_g = TRUE;
4424 extra = 2;
4425 }
4426 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004427 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004428 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004429 len = (int)(end - lv.ll_name);
4430 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004431 if (len <= 0)
4432 {
4433 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004434 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004435 goto theend;
4436 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004437
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004438 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004439 // starts with a lower case character: dict.func(). Or when in a class.
4440 vim9_local = ASCII_ISUPPER(*start) && vim9script
4441 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004443 /*
4444 * Copy the function name to allocated memory.
4445 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4446 * Accept <SNR>123_name() outside a script.
4447 */
4448 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004449 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004450 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004451 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004452 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004453 {
Kota Kato948a3892022-08-16 16:09:59 +01004454 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4455 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004456 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004457 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004458 goto theend;
4459 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004461 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004462 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004464 || eval_fname_sid(*pp))
4465 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004467 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004468 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004469 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004470 goto theend;
4471 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004472 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004473 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004474 extra = 3 + (int)STRLEN(sid_buf);
4475 else
4476 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004477 }
4478 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004479 // The function name must start with an upper case letter (unless it is a
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004480 // Vim9 class new() function or a Vim9 class private method or one of the
4481 // supported Vim9 object builtin functions)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004482 else if (!(flags & TFN_INT)
4483 && (builtin_function(lv.ll_name, len)
4484 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004485 && !((flags & TFN_IN_CLASS)
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004486 && (is_valid_builtin_obj_methodname(lv.ll_name)
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004487 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004488 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004489 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004490 : e_function_name_must_start_with_capital_or_s_str),
4491 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004492 goto theend;
4493 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004494 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004495 {
4496 char_u *cp = vim_strchr(lv.ll_name, ':');
4497
4498 if (cp != NULL && cp < end)
4499 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004500 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004501 goto theend;
4502 }
4503 }
4504
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004506 if (name != NULL)
4507 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004508 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004509 {
4510 name[0] = K_SPECIAL;
4511 name[1] = KS_EXTRA;
4512 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004513 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004514 STRCPY(name + 3, sid_buf);
4515 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004516 else if (prefix_g)
4517 {
4518 name[0] = 'g';
4519 name[1] = ':';
4520 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004521 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4522 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004523 }
4524 *pp = end;
4525
4526theend:
4527 clear_lval(&lv);
4528 return name;
4529}
4530
4531/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004532 * Assuming "name" is the result of trans_function_name() and it was prefixed
4533 * to use the script-local name, return the unmodified name (points into
4534 * "name"). Otherwise return NULL.
4535 * This can be used to first search for a script-local function and fall back
4536 * to the global function if not found.
4537 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004538 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004539untrans_function_name(char_u *name)
4540{
4541 char_u *p;
4542
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004543 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004544 {
4545 p = vim_strchr(name, '_');
4546 if (p != NULL)
4547 return p + 1;
4548 }
4549 return NULL;
4550}
4551
4552/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004553 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4554 * current script ID and returns the expanded function name. The caller should
4555 * free the returned name. If not called from a script context or the function
4556 * name doesn't start with these prefixes, then returns NULL.
4557 * This doesn't check whether the script-local function exists or not.
4558 */
4559 char_u *
4560get_scriptlocal_funcname(char_u *funcname)
4561{
4562 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004563 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004564 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004565 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004566
4567 if (funcname == NULL)
4568 return NULL;
4569
4570 if (STRNCMP(funcname, "s:", 2) != 0
4571 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004572 {
4573 ufunc_T *ufunc;
4574
4575 // The function name does not have a script-local prefix. Try finding
4576 // it when in a Vim9 script and there is no "g:" prefix.
4577 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4578 return NULL;
4579 ufunc = find_func(funcname, FALSE);
4580 if (ufunc == NULL || func_is_global(ufunc)
4581 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4582 return NULL;
4583 ++p;
4584 off = 0;
4585 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004586 else
4587 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004588
4589 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4590 {
4591 emsg(_(e_using_sid_not_in_script_context));
4592 return NULL;
4593 }
4594 // Expand s: prefix into <SNR>nr_<name>
4595 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4596 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004597 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004598 if (newname == NULL)
4599 return NULL;
4600 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004601 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004602
4603 return newname;
4604}
4605
4606/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004607 * Return script-local "fname" with the 3-byte sequence replaced by
4608 * printable <SNR> in allocated memory.
4609 */
4610 char_u *
4611alloc_printable_func_name(char_u *fname)
4612{
4613 char_u *n = alloc(STRLEN(fname + 3) + 6);
4614
4615 if (n != NULL)
4616 {
4617 STRCPY(n, "<SNR>");
4618 STRCPY(n + 5, fname + 3);
4619 }
4620 return n;
4621}
4622
4623/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004624 * Call trans_function_name(), except that a lambda is returned as-is.
4625 * Returns the name in allocated memory.
4626 */
4627 char_u *
4628save_function_name(
4629 char_u **name,
4630 int *is_global,
4631 int skip,
4632 int flags,
4633 funcdict_T *fudi)
4634{
4635 char_u *p = *name;
4636 char_u *saved;
4637
4638 if (STRNCMP(p, "<lambda>", 8) == 0)
4639 {
4640 p += 8;
4641 (void)getdigits(&p);
4642 saved = vim_strnsave(*name, p - *name);
4643 if (fudi != NULL)
4644 CLEAR_POINTER(fudi);
4645 }
4646 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004647 saved = trans_function_name_ext(&p, is_global, skip,
4648 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004649 *name = p;
4650 return saved;
4651}
4652
4653/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004654 * List functions. When "regmatch" is NULL all of then.
4655 * Otherwise functions matching "regmatch".
4656 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004657 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004658list_functions(regmatch_T *regmatch)
4659{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004660 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004661 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004662 hashitem_T *hi;
4663
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004664 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004665 {
4666 if (!HASHITEM_EMPTY(hi))
4667 {
4668 ufunc_T *fp = HI2UF(hi);
4669
4670 --todo;
4671 if ((fp->uf_flags & FC_DEAD) == 0
4672 && (regmatch == NULL
4673 ? !message_filtered(fp->uf_name)
4674 && !func_name_refcount(fp->uf_name)
Keith Thompson184f71c2024-01-04 21:19:04 +01004675 : !SAFE_isdigit(*fp->uf_name)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004676 && vim_regexec(regmatch, fp->uf_name, 0)))
4677 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004678 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004679 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004680 if (function_list_modified(prev_ht_changed))
4681 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004682 }
4683 }
4684 }
4685}
4686
4687/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004688 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004689 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4690 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004691 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004692 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4693 * With CF_INTERFACE the function is define inside an interface, only the
4694 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004695 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004696 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004697 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004698define_function(
4699 exarg_T *eap,
4700 char_u *name_arg,
4701 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004702 int class_flags,
4703 ocmember_T *obj_members,
4704 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004705{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004706 int j;
4707 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004708 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004709 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004710 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004711 char_u *p;
4712 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004713 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004714 char_u *line_arg = NULL;
4715 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004717 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004718 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004719 garray_T newlines;
4720 int varargs = FALSE;
4721 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004722 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004723 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004724 int fp_allocated = FALSE;
4725 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004726 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004727 dictitem_T *v;
4728 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004729 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004730 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004731 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004732 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004733 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004734 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004735
4736 /*
4737 * ":function" without argument: list functions.
4738 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004739 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004740 {
4741 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004742 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004743 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004744 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004745 }
4746
4747 /*
4748 * ":function /pat": list functions matching pattern.
4749 */
4750 if (*eap->arg == '/')
4751 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004752 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004753 if (!eap->skip)
4754 {
4755 regmatch_T regmatch;
4756
4757 c = *p;
4758 *p = NUL;
4759 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4760 *p = c;
4761 if (regmatch.regprog != NULL)
4762 {
4763 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004764 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004765 vim_regfree(regmatch.regprog);
4766 }
4767 }
4768 if (*p == '/')
4769 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004770 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004771 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004772 }
4773
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004774 ga_init(&newargs);
4775 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004776 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004777 ga_init(&default_args);
4778
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004779 /*
4780 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004781 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004782 * "name" == func, "fudi.fd_dict" == NULL
4783 * dict.func new dictionary entry
4784 * "name" == NULL, "fudi.fd_dict" set,
4785 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4786 * dict.func existing dict entry with a Funcref
4787 * "name" == func, "fudi.fd_dict" set,
4788 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4789 * dict.func existing dict entry that's not a Funcref
4790 * "name" == NULL, "fudi.fd_dict" set,
4791 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4792 * s:func script-local function name
4793 * g:func global function name, same as "func"
4794 */
4795 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004796 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004797 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004798 // nested function, argument is (args).
4799 paren = TRUE;
4800 CLEAR_FIELD(fudi);
4801 }
4802 else
4803 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004804 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004805 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004806 if (p[0] == 's' && p[1] == ':')
4807 {
4808 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4809 return NULL;
4810 }
4811 p = to_name_end(p, TRUE);
4812 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4813 {
4814 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4815 eap->arg);
4816 return NULL;
4817 }
4818 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004819 }
4820
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004821 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004822 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004823 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004824 paren = (vim_strchr(p, '(') != NULL);
4825 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004826 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004827 /*
4828 * Return on an invalid expression in braces, unless the expression
4829 * evaluation has been cancelled due to an aborting error, an
4830 * interrupt, or an exception.
4831 */
4832 if (!aborting())
4833 {
4834 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004835 semsg(_(e_key_not_present_in_dictionary_str),
4836 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004837 vim_free(fudi.fd_newkey);
4838 return NULL;
4839 }
4840 else
4841 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004842 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004843
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004844 // For "export def FuncName()" in an autoload script the function name
4845 // is stored with the legacy autoload name "dir#script#FuncName" so
4846 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004847 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004848 {
4849 char_u *prefixed = may_prefix_autoload(name);
4850
4851 if (prefixed != NULL && prefixed != name)
4852 {
4853 vim_free(name);
4854 name = prefixed;
4855 }
4856 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004857 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004858 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004859 {
4860 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4861 goto ret_free;
4862 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004863 }
4864
Bram Moolenaare38eab22019-12-05 21:50:01 +01004865 // An error in a function call during evaluation of an expression in magic
4866 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004867 saved_did_emsg = did_emsg;
4868 did_emsg = FALSE;
4869
4870 /*
4871 * ":function func" with only function name: list function.
4872 */
4873 if (!paren)
4874 {
4875 if (!ends_excmd(*skipwhite(p)))
4876 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004877 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004878 goto ret_free;
4879 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004880 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004881 if (eap->nextcmd != NULL)
4882 *p = NUL;
4883 if (!eap->skip && !got_int)
4884 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004885 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004886 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4887 {
4888 char_u *up = untrans_function_name(name);
4889
4890 // With Vim9 script the name was made script-local, if not
4891 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004892 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004893 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004894 }
4895
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004896 if (fp != NULL)
4897 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004898 // Check no function was added or removed from a timer, e.g. at
4899 // the more prompt. "fp" may then be invalid.
4900 int prev_ht_changed = func_hashtab.ht_changed;
4901
4902 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004903 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004904 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4905 {
4906 if (FUNCLINE(fp, j) == NULL)
4907 continue;
4908 msg_putchar('\n');
4909 msg_outnum((long)(j + 1));
4910 if (j < 9)
4911 msg_putchar(' ');
4912 if (j < 99)
4913 msg_putchar(' ');
4914 if (function_list_modified(prev_ht_changed))
4915 break;
4916 msg_prt_line(FUNCLINE(fp, j), FALSE);
4917 out_flush(); // show a line at a time
4918 ui_breakcheck();
4919 }
4920 if (!got_int)
4921 {
4922 msg_putchar('\n');
4923 if (!function_list_modified(prev_ht_changed))
4924 {
4925 if (fp->uf_def_status != UF_NOT_COMPILED)
4926 msg_puts(" enddef");
4927 else
4928 msg_puts(" endfunction");
4929 }
4930 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004931 }
4932 }
4933 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004934 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004935 }
4936 goto ret_free;
4937 }
4938
4939 /*
4940 * ":function name(arg1, arg2)" Define function.
4941 */
4942 p = skipwhite(p);
4943 if (*p != '(')
4944 {
4945 if (!eap->skip)
4946 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004947 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004948 goto ret_free;
4949 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004950 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004951 if (vim_strchr(p, '(') != NULL)
4952 p = vim_strchr(p, '(');
4953 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004954
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004955 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4956 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004957 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004958 goto ret_free;
4959 }
4960
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004961 // In Vim9 script only global functions can be redefined.
4962 if (vim9script && eap->forceit && !is_global)
4963 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004964 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004965 goto ret_free;
4966 }
4967
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004968 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004969
Bram Moolenaar04b12692020-05-04 23:24:44 +02004970 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004971 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004972 // Check the name of the function. Unless it's a dictionary function
4973 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004974 if (name != NULL)
4975 arg = name;
4976 else
4977 arg = fudi.fd_newkey;
4978 if (arg != NULL && (fudi.fd_di == NULL
4979 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4980 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4981 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004982 char_u *name_base = arg;
4983 int i;
4984
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004985 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004986 {
4987 name_base = vim_strchr(arg, '_');
4988 if (name_base == NULL)
4989 name_base = arg + 3;
4990 else
4991 ++name_base;
4992 }
4993 for (i = 0; name_base[i] != NUL && (i == 0
4994 ? eval_isnamec1(name_base[i])
4995 : eval_isnamec(name_base[i])); ++i)
4996 ;
4997 if (name_base[i] != NUL)
zeertzjq6a04bf52024-03-16 09:39:06 +01004998 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004999 emsg_funcname(e_invalid_argument_str, arg);
zeertzjq6a04bf52024-03-16 09:39:06 +01005000 goto ret_free;
5001 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00005002
5003 // In Vim9 script a function cannot have the same name as a
5004 // variable.
5005 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00005006 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
5007 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00005008 + EVAL_VAR_NO_FUNC) == OK)
5009 {
5010 semsg(_(e_redefining_script_item_str), name_base);
5011 goto ret_free;
5012 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005013 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005014 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005015 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005016 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005017 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00005018 goto ret_free;
5019 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005020 }
5021
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005022 // This may get more lines and make the pointers into the first line
5023 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01005024 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005025 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00005026 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02005027 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005028 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00005029 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005030 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01005031 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005032
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005034 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005035 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005036 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005037 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005038 if (*p != ':')
5039 {
5040 semsg(_(e_no_white_space_allowed_before_colon_str), p);
5041 p = skipwhite(p);
5042 }
5043 else if (!IS_WHITE_OR_NUL(p[1]))
5044 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02005046 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005047 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005048 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005049 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01005050 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005051 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005052 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005053 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005054 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005055 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02005056 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005057 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005058 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02005059 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005060 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005061 else
5062 // find extra arguments "range", "dict", "abort" and "closure"
5063 for (;;)
5064 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01005065 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066 p = skipwhite(p);
5067 if (STRNCMP(p, "range", 5) == 0)
5068 {
5069 flags |= FC_RANGE;
5070 p += 5;
5071 }
5072 else if (STRNCMP(p, "dict", 4) == 0)
5073 {
5074 flags |= FC_DICT;
5075 p += 4;
5076 }
5077 else if (STRNCMP(p, "abort", 5) == 0)
5078 {
5079 flags |= FC_ABORT;
5080 p += 5;
5081 }
5082 else if (STRNCMP(p, "closure", 7) == 0)
5083 {
5084 flags |= FC_CLOSURE;
5085 p += 7;
5086 if (current_funccal == NULL)
5087 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005088 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005089 name == NULL ? (char_u *)"" : name);
5090 goto erret;
5091 }
5092 }
5093 else
5094 break;
5095 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005096
Bram Moolenaare38eab22019-12-05 21:50:01 +01005097 // When there is a line break use what follows for the function body.
5098 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005099 if (*p == '\n')
5100 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005101 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005102 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5103 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005104 && !(VIM_ISWHITE(*whitep) && *p == '#'
5105 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005106 && !eap->skip
5107 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005108 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005109
5110 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005111 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5112 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005113 */
5114 if (KeyTyped)
5115 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005116 // Check if the function already exists, don't let the user type the
5117 // whole function before telling him it doesn't work! For a script we
5118 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005119 if (!eap->skip && !eap->forceit)
5120 {
5121 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005122 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005123 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005124 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005125 }
5126
5127 if (!eap->skip && did_emsg)
5128 goto erret;
5129
Bram Moolenaare38eab22019-12-05 21:50:01 +01005130 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005131 cmdline_row = msg_row;
5132 }
5133
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005134 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005135 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005136
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005137 // Do not define the function when getting the body fails and when
5138 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005139 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005140 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005141 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5142 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005143 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005144 goto erret;
5145
5146 /*
5147 * If there are no errors, add the function
5148 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005149 if (fudi.fd_dict != NULL)
5150 {
5151 char numbuf[20];
5152
5153 fp = NULL;
5154 if (fudi.fd_newkey == NULL && !eap->forceit)
5155 {
5156 emsg(_(e_dictionary_entry_already_exists));
5157 goto erret;
5158 }
5159 if (fudi.fd_di == NULL)
5160 {
5161 // Can't add a function to a locked dictionary
5162 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5163 goto erret;
5164 }
5165 // Can't change an existing function if it is locked
5166 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5167 goto erret;
5168
5169 // Give the function a sequential number. Can only be used with a
5170 // Funcref!
5171 vim_free(name);
5172 sprintf(numbuf, "%d", ++func_nr);
5173 name = vim_strsave((char_u *)numbuf);
5174 if (name == NULL)
5175 goto erret;
5176 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005177 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005178 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005179 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005180 char_u *find_name = name;
5181 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005182 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005183
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005184 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005185 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005186 var_conflict = TRUE;
5187
5188 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5189 {
5190 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5191
5192 if (si->sn_autoload_prefix != NULL)
5193 {
5194 if (is_export)
5195 {
5196 find_name = name + STRLEN(si->sn_autoload_prefix);
5197 v = find_var(find_name, &ht, TRUE);
5198 if (v != NULL)
5199 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005200 // Only check if the function already exists in the script,
5201 // global functions can be shadowed.
5202 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005203 }
5204 else
5205 {
5206 char_u *prefixed = may_prefix_autoload(name);
5207
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005208 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005209 {
5210 v = find_var(prefixed, &ht, TRUE);
5211 if (v != NULL)
5212 var_conflict = TRUE;
5213 vim_free(prefixed);
5214 }
5215 }
5216 }
5217 }
5218 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005219 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005220 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005221 goto erret;
5222 }
5223
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005224 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005225 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005226 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005227 char_u *uname = untrans_function_name(name);
5228
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005229 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005230 }
5231
5232 if (fp != NULL || import != NULL)
5233 {
5234 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005235
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005236 // Function can be replaced with "function!" and when sourcing the
5237 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005238 // A name that is used by an import can not be overruled.
5239 if (import != NULL
5240 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005241 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005242 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005243 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005244 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005245 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005246 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005247 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005248 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005249 goto erret;
5250 }
5251 if (fp->uf_calls > 0)
5252 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005253 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005254 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005255 goto erret;
5256 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005257 if (fp->uf_refcount > 1)
5258 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005259 // This function is referenced somewhere, don't redefine it but
5260 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005261 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005262 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005263 fp = NULL;
5264 overwrite = TRUE;
5265 }
5266 else
5267 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005268 char_u *exp_name = fp->uf_name_exp;
5269
5270 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005271 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005272 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005273 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005274 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005275 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005276#ifdef FEAT_PROFILE
5277 fp->uf_profiling = FALSE;
5278 fp->uf_prof_initialized = FALSE;
5279#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005280 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005281 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005282 }
5283 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005284
5285 if (fp == NULL)
5286 {
5287 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5288 {
5289 int slen, plen;
5290 char_u *scriptname;
5291
Bram Moolenaare38eab22019-12-05 21:50:01 +01005292 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005293 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005294 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005295 {
5296 scriptname = autoload_name(name);
5297 if (scriptname != NULL)
5298 {
5299 p = vim_strchr(scriptname, '/');
5300 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005301 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005302 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005303 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005304 j = OK;
5305 vim_free(scriptname);
5306 }
5307 }
5308 if (j == FAIL)
5309 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005310 linenr_T save_lnum = SOURCING_LNUM;
5311
5312 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005313 semsg(_(e_function_name_does_not_match_script_file_name_str),
5314 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005315 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005316 goto erret;
5317 }
5318 }
5319
Bram Moolenaare01e5212023-01-08 20:31:18 +00005320 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005321 if (fp == NULL)
5322 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005323 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005324
5325 if (fudi.fd_dict != NULL)
5326 {
5327 if (fudi.fd_di == NULL)
5328 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005329 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005330 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5331 if (fudi.fd_di == NULL)
5332 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005333 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005334 goto erret;
5335 }
5336 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5337 {
5338 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005339 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005340 goto erret;
5341 }
5342 }
5343 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005344 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005345 clear_tv(&fudi.fd_di->di_tv);
5346 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005347 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005348
Bram Moolenaare38eab22019-12-05 21:50:01 +01005349 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005350 flags |= FC_DICT;
5351 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005352 }
5353 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005354 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005355 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005356 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005357
5358 if (eap->cmdidx == CMD_def)
5359 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005360 int lnum_save = SOURCING_LNUM;
5361 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005362
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005363 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005364
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005365 // error messages are for the first function line
5366 SOURCING_LNUM = sourcing_lnum_top;
5367
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005368 // The function may use script variables from the context.
5369 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005370
h-eastb895b0f2023-09-24 15:46:31 +02005371 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5372 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005373 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005374 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005375 free_fp = fp_allocated;
5376 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005377 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005378 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005379
5380 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005381 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005382 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005383 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005384 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005385 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005386 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005387 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005388 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005389 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005390 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005391
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005392 if (fp_allocated)
5393 {
5394 // insert the new function in the function list
5395 set_ufunc_name(fp, name);
5396 if (overwrite)
5397 {
5398 hi = hash_find(&func_hashtab, name);
5399 hi->hi_key = UF2HIKEY(fp);
5400 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005401 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005402 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005403 {
5404 free_fp = TRUE;
5405 goto erret;
5406 }
5407 fp->uf_refcount = 1;
5408 }
5409
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005410 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005411 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005412 if ((flags & FC_CLOSURE) != 0)
5413 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005414 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005415 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005416 }
5417 else
5418 fp->uf_scoped = NULL;
5419
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005420#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005421 if (prof_def_func())
5422 func_do_profile(fp);
5423#endif
5424 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005425 if (sandbox)
5426 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005427 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005428 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005429 fp->uf_flags = flags;
5430 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005431 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005432 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005433 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005434 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005435 if (is_export)
5436 {
5437 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005438 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005439 is_export = FALSE;
5440 }
5441
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005442 if (eap->cmdidx == CMD_def)
5443 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005444 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5445 // :func does not use Vim9 script syntax, even in a Vim9 script file
5446 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005447
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005448 goto ret_free;
5449
5450erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005451 if (fp != NULL)
5452 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005453 // these were set to "newargs" and "default_args", which are cleared
5454 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005455 ga_init(&fp->uf_args);
5456 ga_init(&fp->uf_def_args);
5457 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005458errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005459 ga_clear_strings(&newargs);
5460 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005461 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005462 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005463 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005464 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005465 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01005466 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005467 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005468 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005469 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005470ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005471 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005472 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005473 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005474 if (name != name_arg)
5475 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005476 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005477 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005478
5479 return fp;
5480}
5481
5482/*
5483 * ":function"
5484 */
5485 void
5486ex_function(exarg_T *eap)
5487{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005488 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005489
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005490 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005491 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005492 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005493}
5494
5495/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005496 * Find a function by name, including "<lambda>123".
5497 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005498 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005499 * Return NULL if not found.
5500 */
5501 ufunc_T *
5502find_func_by_name(char_u *name, compiletype_T *compile_type)
5503{
5504 char_u *arg = name;
5505 char_u *fname;
5506 ufunc_T *ufunc;
5507 int is_global = FALSE;
5508
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005509 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5510 {
5511 *compile_type = CT_PROFILE;
5512 arg = skipwhite(arg + 7);
5513 }
5514 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5515 {
5516 *compile_type = CT_DEBUG;
5517 arg = skipwhite(arg + 5);
5518 }
5519
5520 if (STRNCMP(arg, "<lambda>", 8) == 0)
5521 {
5522 arg += 8;
5523 (void)getdigits(&arg);
5524 fname = vim_strnsave(name, arg - name);
5525 }
5526 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005527 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005528 // First try finding a method in a class, trans_function_name() will
5529 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005530 ufunc = find_class_func(&arg);
5531 if (ufunc != NULL)
5532 return ufunc;
5533
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005534 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005535 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005536 NULL, NULL, NULL, &ufunc);
5537 if (ufunc != NULL)
5538 {
5539 vim_free(fname);
5540 return ufunc;
5541 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005542 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005543 if (fname == NULL)
5544 {
5545 semsg(_(e_invalid_argument_str), name);
5546 return NULL;
5547 }
5548 if (!ends_excmd2(name, arg))
5549 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005550 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005551 emsg(ex_errmsg(e_trailing_characters_str, arg));
5552 return NULL;
5553 }
5554
5555 ufunc = find_func(fname, is_global);
5556 if (ufunc == NULL)
5557 {
5558 char_u *p = untrans_function_name(fname);
5559
5560 if (p != NULL)
5561 // Try again without making it script-local.
5562 ufunc = find_func(p, FALSE);
5563 }
5564 vim_free(fname);
5565 if (ufunc == NULL)
5566 semsg(_(e_cannot_find_function_str), name);
5567 return ufunc;
5568}
5569
5570/*
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005571 * Compile the :def function "ufunc". If "cl" is not NULL, then compile the
5572 * class or object method "ufunc" in "cl".
5573 */
5574 void
5575defcompile_function(ufunc_T *ufunc, class_T *cl)
5576{
5577 compiletype_T compile_type = CT_NONE;
5578
5579 if (func_needs_compiling(ufunc, compile_type))
5580 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5581 else
5582 smsg(_("Function %s%s%s does not need compiling"),
5583 cl != NULL ? cl->class_name : (char_u *)"",
5584 cl != NULL ? (char_u *)"." : (char_u *)"",
5585 ufunc->uf_name);
5586}
5587
5588/*
5589 * Compile all the :def functions defined in the current script
5590 */
5591 static void
5592defcompile_funcs_in_script(void)
5593{
5594 long todo = (long)func_hashtab.ht_used;
5595 int changed = func_hashtab.ht_changed;
5596 hashitem_T *hi;
5597
5598 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5599 {
5600 if (!HASHITEM_EMPTY(hi))
5601 {
5602 --todo;
5603 ufunc_T *ufunc = HI2UF(hi);
5604 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5605 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5606 && (ufunc->uf_flags & FC_DEAD) == 0)
5607 {
5608 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5609
5610 if (func_hashtab.ht_changed != changed)
5611 {
5612 // a function has been added or removed, need to start
5613 // over
5614 todo = (long)func_hashtab.ht_used;
5615 changed = func_hashtab.ht_changed;
5616 hi = func_hashtab.ht_array;
5617 --hi;
5618 }
5619 }
5620 }
5621 }
5622}
5623
5624/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005625 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005626 * be compiled or the one specified by the argument.
5627 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005628 */
5629 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005630ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005631{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005632 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005633 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005634 typval_T tv;
5635
5636 if (is_class_name(eap->arg, &tv))
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005637 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005638 class_T *cl = tv.vval.v_class;
5639
5640 if (cl != NULL)
5641 defcompile_class(cl);
5642 }
5643 else
5644 {
5645 compiletype_T compile_type = CT_NONE;
5646 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
5647 if (ufunc != NULL)
5648 defcompile_function(ufunc, NULL);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005649 }
5650 }
5651 else
5652 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005653 defcompile_funcs_in_script();
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005654
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005655 // compile all the class defined in the current script
5656 defcompile_classes_in_script();
Bram Moolenaar822ba242020-05-24 23:00:18 +02005657 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005658}
5659
5660/*
5661 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5662 * Return 2 if "p" starts with "s:".
5663 * Return 0 otherwise.
5664 */
5665 int
5666eval_fname_script(char_u *p)
5667{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005668 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5669 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005670 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5671 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5672 return 5;
5673 if (p[0] == 's' && p[1] == ':')
5674 return 2;
5675 return 0;
5676}
5677
5678 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005679translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005680{
5681 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005682 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005683 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005684}
5685
5686/*
5687 * Return TRUE when "ufunc" has old-style "..." varargs
5688 * or named varargs "...name: type".
5689 */
5690 int
5691has_varargs(ufunc_T *ufunc)
5692{
5693 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005694}
5695
5696/*
5697 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005698 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005699 */
5700 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005701function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005702{
5703 char_u *nm = name;
5704 char_u *p;
5705 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005706 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005707 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005708
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005709 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5710 if (no_deref)
5711 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005712 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005713 nm = skipwhite(nm);
5714
Bram Moolenaare38eab22019-12-05 21:50:01 +01005715 // Only accept "funcname", "funcname ", "funcname (..." and
5716 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005717 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005718 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005719 vim_free(p);
5720 return n;
5721}
5722
Bram Moolenaar113e1072019-01-20 15:30:40 +01005723#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005724 char_u *
5725get_expanded_name(char_u *name, int check)
5726{
5727 char_u *nm = name;
5728 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005729 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005730
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005731 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005732
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005733 if (p != NULL && *nm == NUL
5734 && (!check || translated_function_exists(p, is_global)))
5735 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005736
5737 vim_free(p);
5738 return NULL;
5739}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005740#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005741
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005742/*
5743 * Function given to ExpandGeneric() to obtain the list of user defined
5744 * function names.
5745 */
5746 char_u *
5747get_user_func_name(expand_T *xp, int idx)
5748{
5749 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005750 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005751 static hashitem_T *hi;
5752 ufunc_T *fp;
5753
5754 if (idx == 0)
5755 {
5756 done = 0;
5757 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005758 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005759 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005760 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005761 {
5762 if (done++ > 0)
5763 ++hi;
5764 while (HASHITEM_EMPTY(hi))
5765 ++hi;
5766 fp = HI2UF(hi);
5767
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005768 // don't show dead, dict and lambda functions
5769 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005770 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005771 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005772
5773 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005774 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005775
5776 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005777 if (xp->xp_context != EXPAND_USER_FUNC
5778 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005779 {
5780 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005781 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005782 STRCAT(IObuff, ")");
5783 }
5784 return IObuff;
5785 }
5786 return NULL;
5787}
5788
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005789/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005790 * Make a copy of a function.
5791 * Intended to be used for a function defined on a base class that has a copy
5792 * on the child class.
5793 * The copy has uf_refcount set to one.
5794 * Returns NULL when out of memory.
5795 */
5796 ufunc_T *
5797copy_function(ufunc_T *fp)
5798{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005799 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005800 if (ufunc == NULL)
5801 return NULL;
5802
5803 // Most things can just be copied.
5804 *ufunc = *fp;
5805
5806 ufunc->uf_def_status = UF_TO_BE_COMPILED;
5807 ufunc->uf_dfunc_idx = 0;
5808 ufunc->uf_class = NULL;
5809
5810 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
5811 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
5812
5813 if (ufunc->uf_arg_types != NULL)
5814 {
5815 // "uf_arg_types" is an allocated array, make a copy.
5816 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
5817 if (at != NULL)
5818 {
5819 mch_memmove(at, ufunc->uf_arg_types,
5820 sizeof(type_T *) * ufunc->uf_args.ga_len);
5821 ufunc->uf_arg_types = at;
5822 }
5823 }
5824
5825 // TODO: how about the types themselves? they can be freed when the
5826 // original function is freed:
5827 // type_T **uf_arg_types;
5828 // type_T *uf_ret_type;
5829
Ernie Rael114ec812023-06-04 18:11:35 +01005830 // make uf_type_list empty
5831 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00005832
5833 // TODO: partial_T *uf_partial;
5834
5835 if (ufunc->uf_va_name != NULL)
5836 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
5837
5838 // TODO:
5839 // type_T *uf_va_type;
5840 // type_T *uf_func_type;
5841
5842 ufunc->uf_block_depth = 0;
5843 ufunc->uf_block_ids = NULL;
5844
5845 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
5846
5847 ufunc->uf_refcount = 1;
5848 ufunc->uf_name_exp = NULL;
5849 STRCPY(ufunc->uf_name, fp->uf_name);
5850
5851 return ufunc;
5852}
5853
5854/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005855 * ":delfunction {name}"
5856 */
5857 void
5858ex_delfunction(exarg_T *eap)
5859{
5860 ufunc_T *fp = NULL;
5861 char_u *p;
5862 char_u *name;
5863 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005864 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005865
5866 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005867 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
5868 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005869 vim_free(fudi.fd_newkey);
5870 if (name == NULL)
5871 {
5872 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005873 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005874 return;
5875 }
5876 if (!ends_excmd(*skipwhite(p)))
5877 {
5878 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005879 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005880 return;
5881 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005882 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005883 if (eap->nextcmd != NULL)
5884 *p = NUL;
5885
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005886 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005887 {
5888 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005889 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005890 vim_free(name);
5891 return;
5892 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005893 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005894 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005895 vim_free(name);
5896
5897 if (!eap->skip)
5898 {
5899 if (fp == NULL)
5900 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005901 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005902 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005903 return;
5904 }
5905 if (fp->uf_calls > 0)
5906 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005907 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005908 return;
5909 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005910 if (fp->uf_flags & FC_VIM9)
5911 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005912 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005913 return;
5914 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005915
5916 if (fudi.fd_dict != NULL)
5917 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005918 // Delete the dict item that refers to the function, it will
5919 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00005920 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005921 }
5922 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005923 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005924 // A normal function (not a numbered function or lambda) has a
5925 // refcount of 1 for the entry in the hashtable. When deleting
5926 // it and the refcount is more than one, it should be kept.
5927 // A numbered function and lambda should be kept if the refcount is
5928 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005929 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005930 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005931 // Function is still referenced somewhere. Don't free it but
5932 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005933 if (func_remove(fp))
5934 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005935 }
5936 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005937 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005938 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005939 }
5940}
5941
5942/*
5943 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005944 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005945 */
5946 void
5947func_unref(char_u *name)
5948{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005949 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005950
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005951 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005952 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005953 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005954 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005955 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005956#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005957 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005958#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005959 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005960 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005961 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005962}
5963
5964/*
5965 * Unreference a Function: decrement the reference count and free it when it
5966 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005967 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005968 */
5969 void
5970func_ptr_unref(ufunc_T *fp)
5971{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005972 if (fp != NULL && (--fp->uf_refcount <= 0
5973 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5974 && fp->uf_partial->pt_refcount <= 1
5975 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005976 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005977 // Only delete it when it's not being used. Otherwise it's done
5978 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005979 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005980 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005981 }
5982}
5983
5984/*
5985 * Count a reference to a Function.
5986 */
5987 void
5988func_ref(char_u *name)
5989{
5990 ufunc_T *fp;
5991
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005992 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005993 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005994 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005995 if (fp != NULL)
5996 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005997 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005998 // Only give an error for a numbered function.
5999 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01006000 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006001}
6002
6003/*
6004 * Count a reference to a Function.
6005 */
6006 void
6007func_ptr_ref(ufunc_T *fp)
6008{
6009 if (fp != NULL)
6010 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006011}
6012
6013/*
6014 * Return TRUE if items in "fc" do not have "copyID". That means they are not
6015 * referenced from anywhere that is in use.
6016 */
6017 static int
6018can_free_funccal(funccall_T *fc, int copyID)
6019{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006020 return (fc->fc_l_varlist.lv_copyID != copyID
6021 && fc->fc_l_vars.dv_copyID != copyID
6022 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006023 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006024}
6025
6026/*
6027 * ":return [expr]"
6028 */
6029 void
6030ex_return(exarg_T *eap)
6031{
6032 char_u *arg = eap->arg;
6033 typval_T rettv;
6034 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006035 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006036
6037 if (current_funccal == NULL)
6038 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006039 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006040 return;
6041 }
6042
Bram Moolenaar844fb642021-10-23 13:32:30 +01006043 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006044 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
6045
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006046 if (eap->skip)
6047 ++emsg_skip;
6048
6049 eap->nextcmd = NULL;
6050 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006051 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006052 {
6053 if (!eap->skip)
6054 returning = do_return(eap, FALSE, TRUE, &rettv);
6055 else
6056 clear_tv(&rettv);
6057 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01006058 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006059 else if (!eap->skip)
6060 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006061 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01006062 update_force_abort();
6063
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006064 /*
6065 * Return unless the expression evaluation has been cancelled due to an
6066 * aborting error, an interrupt, or an exception.
6067 */
6068 if (!aborting())
6069 returning = do_return(eap, FALSE, TRUE, NULL);
6070 }
6071
Bram Moolenaare38eab22019-12-05 21:50:01 +01006072 // When skipping or the return gets pending, advance to the next command
6073 // in this line (!returning). Otherwise, ignore the rest of the line.
6074 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006075 if (returning)
6076 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006077 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02006078 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006079
6080 if (eap->skip)
6081 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02006082 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006083}
6084
zeertzjq5299c092023-04-12 20:48:16 +01006085/*
6086 * Lower level implementation of "call". Only called when not skipping.
6087 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006088 static int
6089ex_call_inner(
6090 exarg_T *eap,
6091 char_u *name,
6092 char_u **arg,
6093 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006094 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006095 evalarg_T *evalarg)
6096{
6097 linenr_T lnum;
6098 int doesrange;
6099 typval_T rettv;
6100 int failed = FALSE;
6101
zeertzjq5299c092023-04-12 20:48:16 +01006102 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006103 for ( ; lnum <= eap->line2; ++lnum)
6104 {
6105 funcexe_T funcexe;
6106
zeertzjq5299c092023-04-12 20:48:16 +01006107 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006108 {
6109 if (lnum > curbuf->b_ml.ml_line_count)
6110 {
6111 // If the function deleted lines or switched to another buffer
6112 // the line number may become invalid.
6113 emsg(_(e_invalid_range));
6114 break;
6115 }
6116 curwin->w_cursor.lnum = lnum;
6117 curwin->w_cursor.col = 0;
6118 curwin->w_cursor.coladd = 0;
6119 }
6120 *arg = startarg;
6121
6122 funcexe = *funcexe_init;
6123 funcexe.fe_doesrange = &doesrange;
6124 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6125 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6126 {
6127 failed = TRUE;
6128 break;
6129 }
6130 if (has_watchexpr())
6131 dbg_check_breakpoint(eap);
6132
6133 // Handle a function returning a Funcref, Dictionary or List.
6134 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006135 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006136 {
6137 failed = TRUE;
6138 break;
6139 }
6140
6141 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006142 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006143 break;
6144
6145 // Stop when immediately aborting on error, or when an interrupt
6146 // occurred or an exception was thrown but not caught.
6147 // get_func_tv() returned OK, so that the check for trailing
6148 // characters below is executed.
6149 if (aborting())
6150 break;
6151 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006152 return failed;
6153}
6154
6155/*
6156 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6157 * Returns FAIL or OK.
6158 */
6159 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006160ex_defer_inner(
6161 char_u *name,
6162 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006163 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006164 partial_T *partial,
6165 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006166{
6167 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006168 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006169 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006170 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006171
6172 if (current_funccal == NULL)
6173 {
6174 semsg(_(e_str_not_inside_function), "defer");
6175 return FAIL;
6176 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006177 if (partial != NULL)
6178 {
6179 if (partial->pt_dict != NULL)
6180 {
6181 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6182 return FAIL;
6183 }
6184 if (partial->pt_argc > 0)
6185 {
6186 int i;
6187
6188 partial_argc = partial->pt_argc;
6189 for (i = 0; i < partial_argc; ++i)
6190 copy_tv(&partial->pt_argv[i], &argvars[i]);
6191 }
6192 }
Ernie Raelb077b582023-12-14 20:11:44 +01006193 int is_builtin = builtin_function(name, -1);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006194 r = get_func_arguments(arg, evalarg, FALSE,
Ernie Raelb077b582023-12-14 20:11:44 +01006195 argvars + partial_argc, &argcount, is_builtin);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006196 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006197
6198 if (r == OK)
6199 {
6200 if (type != NULL)
6201 {
6202 // Check that the arguments are OK for the types of the funcref.
6203 r = check_argument_types(type, argvars, argcount, NULL, name);
6204 }
Ernie Raelb077b582023-12-14 20:11:44 +01006205 else if (is_builtin)
Bram Moolenaar16900322022-09-08 19:51:45 +01006206 {
6207 int idx = find_internal_func(name);
6208
6209 if (idx < 0)
6210 {
6211 emsg_funcname(e_unknown_function_str, name);
6212 r = FAIL;
6213 }
6214 else if (check_internal_func(idx, argcount) == -1)
6215 r = FAIL;
6216 }
6217 else
6218 {
6219 ufunc_T *ufunc = find_func(name, FALSE);
6220
6221 // we tolerate an unknown function here, it might be defined later
6222 if (ufunc != NULL)
6223 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006224 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006225 if (error != FCERR_UNKNOWN)
6226 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006227 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006228 r = FAIL;
6229 }
6230 }
6231 }
6232 }
6233
Bram Moolenaar86d87252022-09-05 21:21:25 +01006234 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006235 {
6236 while (--argcount >= 0)
6237 clear_tv(&argvars[argcount]);
6238 return FAIL;
6239 }
6240 return add_defer(name, argcount, argvars);
6241}
6242
6243/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006244 * Return TRUE if currently inside a function call.
6245 * Give an error message and return FALSE when not.
6246 */
6247 int
6248can_add_defer(void)
6249{
6250 if (!in_def_function() && get_current_funccal() == NULL)
6251 {
6252 semsg(_(e_str_not_inside_function), "defer");
6253 return FALSE;
6254 }
6255 return TRUE;
6256}
6257
6258/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006259 * Add a deferred call for "name" with arguments "argvars[argcount]".
6260 * Consumes "argvars[]".
6261 * Caller must check that in_def_function() returns TRUE or current_funccal is
6262 * not NULL.
6263 * Returns OK or FAIL.
6264 */
6265 int
6266add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6267{
6268 char_u *saved_name = vim_strsave(name);
6269 int argcount = argcount_arg;
6270 defer_T *dr;
6271 int ret = FAIL;
6272
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006273 if (saved_name == NULL)
6274 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006275 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006276 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006277 if (add_defer_function(saved_name, argcount, argvars) == OK)
6278 argcount = 0;
6279 }
6280 else
6281 {
6282 if (current_funccal->fc_defer.ga_itemsize == 0)
6283 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6284 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6285 goto theend;
6286 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6287 + current_funccal->fc_defer.ga_len++;
6288 dr->dr_name = saved_name;
6289 dr->dr_argcount = argcount;
6290 while (argcount > 0)
6291 {
6292 --argcount;
6293 dr->dr_argvars[argcount] = argvars[argcount];
6294 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006295 }
6296 ret = OK;
6297
6298theend:
6299 while (--argcount >= 0)
6300 clear_tv(&argvars[argcount]);
6301 return ret;
6302}
6303
6304/*
6305 * Invoked after a functions has finished: invoke ":defer" functions.
6306 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006307 static void
6308handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006309{
6310 int idx;
6311
Bram Moolenaar58779852022-09-06 18:31:14 +01006312 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006313 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006314 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006315
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006316 if (dr->dr_name == NULL)
6317 // already being called, can happen if function does ":qa"
6318 continue;
6319
6320 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006321 CLEAR_FIELD(funcexe);
6322 funcexe.fe_evaluate = TRUE;
6323
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006324 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006325 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006326
6327 char_u *name = dr->dr_name;
6328 dr->dr_name = NULL;
6329
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006330 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006331 // first statement in the function will be executed (because of the
6332 // exception). So save and restore the try/catch/throw exception
6333 // state.
6334 exception_state_T estate;
6335 exception_state_save(&estate);
6336 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006337
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006338 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6339
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006340 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006341
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006342 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006343 vim_free(name);
6344 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006345 clear_tv(&dr->dr_argvars[i]);
6346 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006347 ga_clear(&funccal->fc_defer);
6348}
6349
zeertzjq960cf912023-04-18 21:52:54 +01006350 static void
6351invoke_funccall_defer(funccall_T *fc)
6352{
6353 if (fc->fc_ectx != NULL)
6354 {
6355 // :def function
6356 unwind_def_callstack(fc->fc_ectx);
6357 may_invoke_defer_funcs(fc->fc_ectx);
6358 }
6359 else
6360 {
6361 // legacy function
6362 handle_defer_one(fc);
6363 }
6364}
6365
Bram Moolenaar58779852022-09-06 18:31:14 +01006366/*
6367 * Called when exiting: call all defer functions.
6368 */
6369 void
6370invoke_all_defer(void)
6371{
zeertzjq1be4b812023-04-19 14:21:24 +01006372 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6373 invoke_funccall_defer(fc);
6374
zeertzjq960cf912023-04-18 21:52:54 +01006375 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6376 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6377 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006378}
6379
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006380/*
6381 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006382 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006383 */
6384 void
6385ex_call(exarg_T *eap)
6386{
6387 char_u *arg = eap->arg;
6388 char_u *startarg;
6389 char_u *name;
6390 char_u *tofree;
6391 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006392 int failed = FALSE;
6393 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006394 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006395 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006396 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006397 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006398 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006399 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006400
Bram Moolenaare6b53242020-07-01 17:28:33 +02006401 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006402 if (eap->skip)
6403 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006404 typval_T rettv;
6405
Bram Moolenaare38eab22019-12-05 21:50:01 +01006406 // trans_function_name() doesn't work well when skipping, use eval0()
6407 // instead to skip to any following command, e.g. for:
6408 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006409 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006410 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006411 clear_tv(&rettv);
6412 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006413 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006414 return;
6415 }
6416
zeertzjq5299c092023-04-12 20:48:16 +01006417 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006418 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006419 if (fudi.fd_newkey != NULL)
6420 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006421 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006422 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006423 vim_free(fudi.fd_newkey);
6424 }
6425 if (tofree == NULL)
6426 return;
6427
Bram Moolenaare38eab22019-12-05 21:50:01 +01006428 // Increase refcount on dictionary, it could get deleted when evaluating
6429 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006430 if (fudi.fd_dict != NULL)
6431 ++fudi.fd_dict->dv_refcount;
6432
Bram Moolenaare38eab22019-12-05 21:50:01 +01006433 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6434 // contents. For VAR_PARTIAL get its partial, unless we already have one
6435 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006436 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006437 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006438 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006439 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006440
Bram Moolenaare38eab22019-12-05 21:50:01 +01006441 // Skip white space to allow ":call func ()". Not good, but required for
6442 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006443 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006444 if (*startarg != '(')
6445 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006446 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006447 goto end;
6448 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006449 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006450 {
6451 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6452 goto end;
6453 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006454
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006455 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006456 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006457 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006458 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006459 }
6460 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006461 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006462 funcexe_T funcexe;
6463
Bram Moolenaara80faa82020-04-12 19:37:17 +02006464 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006465 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006466 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006467 funcexe.fe_partial = partial;
6468 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006469 funcexe.fe_firstline = eap->line1;
6470 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006471 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006472 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006473 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006474 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006475
Bram Moolenaar1d341892021-09-18 15:25:52 +02006476 // When inside :try we need to check for following "| catch" or "| endtry".
6477 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006478 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006479 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006480 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006481 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006482 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006483 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006484 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006485 {
6486 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006487 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006488 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006489 }
6490 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006491 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006492 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006493 // Must be after using "arg", it may point into memory cleared here.
6494 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006495
6496end:
6497 dict_unref(fudi.fd_dict);
6498 vim_free(tofree);
6499}
6500
6501/*
6502 * Return from a function. Possibly makes the return pending. Also called
6503 * for a pending return at the ":endtry" or after returning from an extra
6504 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6505 * when called due to a ":return" command. "rettv" may point to a typval_T
6506 * with the return rettv. Returns TRUE when the return can be carried out,
6507 * FALSE when the return gets pending.
6508 */
6509 int
6510do_return(
6511 exarg_T *eap,
6512 int reanimate,
6513 int is_cmd,
6514 void *rettv)
6515{
6516 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006517 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006518
6519 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006520 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006521 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006522
6523 /*
6524 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6525 * not in its finally clause (which then is to be executed next) is found.
6526 * In this case, make the ":return" pending for execution at the ":endtry".
6527 * Otherwise, return normally.
6528 */
6529 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6530 if (idx >= 0)
6531 {
6532 cstack->cs_pending[idx] = CSTP_RETURN;
6533
6534 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006535 // A pending return again gets pending. "rettv" points to an
6536 // allocated variable with the rettv of the original ":return"'s
6537 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006538 cstack->cs_rettv[idx] = rettv;
6539 else
6540 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006541 // When undoing a return in order to make it pending, get the stored
6542 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006543 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006544 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006545
6546 if (rettv != NULL)
6547 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006548 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006549 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6550 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6551 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006552 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006553 }
6554 else
6555 cstack->cs_rettv[idx] = NULL;
6556
6557 if (reanimate)
6558 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006559 // The pending return value could be overwritten by a ":return"
6560 // without argument in a finally clause; reset the default
6561 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006562 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6563 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006564 }
6565 }
6566 report_make_pending(CSTP_RETURN, rettv);
6567 }
6568 else
6569 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006570 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006571
Bram Moolenaare38eab22019-12-05 21:50:01 +01006572 // If the return is carried out now, store the return value. For
6573 // a return immediately after reanimation, the value is already
6574 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006575 if (!reanimate && rettv != NULL)
6576 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006577 clear_tv(current_funccal->fc_rettv);
6578 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006579 if (!is_cmd)
6580 vim_free(rettv);
6581 }
6582 }
6583
6584 return idx < 0;
6585}
6586
6587/*
6588 * Free the variable with a pending return value.
6589 */
6590 void
6591discard_pending_return(void *rettv)
6592{
6593 free_tv((typval_T *)rettv);
6594}
6595
6596/*
6597 * Generate a return command for producing the value of "rettv". The result
6598 * is an allocated string. Used by report_pending() for verbose messages.
6599 */
6600 char_u *
6601get_return_cmd(void *rettv)
6602{
6603 char_u *s = NULL;
6604 char_u *tofree = NULL;
6605 char_u numbuf[NUMBUFLEN];
6606
6607 if (rettv != NULL)
6608 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6609 if (s == NULL)
6610 s = (char_u *)"";
6611
6612 STRCPY(IObuff, ":return ");
6613 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6614 if (STRLEN(s) + 8 >= IOSIZE)
6615 STRCPY(IObuff + IOSIZE - 4, "...");
6616 vim_free(tofree);
6617 return vim_strsave(IObuff);
6618}
6619
6620/*
6621 * Get next function line.
6622 * Called by do_cmdline() to get the next line.
6623 * Returns allocated string, or NULL for end of function.
6624 */
6625 char_u *
6626get_func_line(
6627 int c UNUSED,
6628 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006629 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006630 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006631{
6632 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006633 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006634 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006635 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006636
Bram Moolenaare38eab22019-12-05 21:50:01 +01006637 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006638 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006639 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006640 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006641 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006642 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006643 }
6644#ifdef FEAT_PROFILE
6645 if (do_profiling == PROF_YES)
6646 func_line_end(cookie);
6647#endif
6648
6649 gap = &fp->uf_lines;
6650 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006651 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006652 retval = NULL;
6653 else
6654 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006655 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006656 while (fcp->fc_linenr < gap->ga_len
6657 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6658 ++fcp->fc_linenr;
6659 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006660 retval = NULL;
6661 else
6662 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006663 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6664 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006665#ifdef FEAT_PROFILE
6666 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006667 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006668#endif
6669 }
6670 }
6671
Bram Moolenaare38eab22019-12-05 21:50:01 +01006672 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006673 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006674 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006675 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006676 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006677 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006678 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006679 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006680 }
6681
6682 return retval;
6683}
6684
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006685/*
6686 * Return TRUE if the currently active function should be ended, because a
6687 * return was encountered or an error occurred. Used inside a ":while".
6688 */
6689 int
6690func_has_ended(void *cookie)
6691{
6692 funccall_T *fcp = (funccall_T *)cookie;
6693
Bram Moolenaare38eab22019-12-05 21:50:01 +01006694 // Ignore the "abort" flag if the abortion behavior has been changed due to
6695 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006696 return (((fcp->fc_func->uf_flags & FC_ABORT)
6697 && did_emsg && !aborted_in_try())
6698 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006699}
6700
6701/*
6702 * return TRUE if cookie indicates a function which "abort"s on errors.
6703 */
6704 int
6705func_has_abort(
6706 void *cookie)
6707{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006708 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006709}
6710
6711
6712/*
6713 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6714 * Don't do this when "Func" is already a partial that was bound
6715 * explicitly (pt_auto is FALSE).
6716 * Changes "rettv" in-place.
6717 * Returns the updated "selfdict_in".
6718 */
6719 dict_T *
6720make_partial(dict_T *selfdict_in, typval_T *rettv)
6721{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006722 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006723 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006724 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006725 dict_T *selfdict = selfdict_in;
6726
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006727 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6728 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006729 fp = rettv->vval.v_partial->pt_func;
6730 else
6731 {
6732 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006733 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006734 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006735 if (fname == NULL)
6736 {
6737 // There is no point binding a dict to a NULL function, just create
6738 // a function reference.
6739 rettv->v_type = VAR_FUNC;
6740 rettv->vval.v_string = NULL;
6741 }
6742 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006743 {
6744 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006745 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006746
6747 // Translate "s:func" to the stored function name.
6748 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6749 fp = find_func(fname, FALSE);
6750 vim_free(tofree);
6751 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006752 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006753
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006754 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006755 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006756 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006757
6758 if (pt != NULL)
6759 {
6760 pt->pt_refcount = 1;
6761 pt->pt_dict = selfdict;
6762 pt->pt_auto = TRUE;
6763 selfdict = NULL;
6764 if (rettv->v_type == VAR_FUNC)
6765 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006766 // Just a function: Take over the function name and use
6767 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006768 pt->pt_name = rettv->vval.v_string;
6769 }
6770 else
6771 {
6772 partial_T *ret_pt = rettv->vval.v_partial;
6773 int i;
6774
Bram Moolenaare38eab22019-12-05 21:50:01 +01006775 // Partial: copy the function name, use selfdict and copy
6776 // args. Can't take over name or args, the partial might
6777 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006778 if (ret_pt->pt_name != NULL)
6779 {
6780 pt->pt_name = vim_strsave(ret_pt->pt_name);
6781 func_ref(pt->pt_name);
6782 }
6783 else
6784 {
6785 pt->pt_func = ret_pt->pt_func;
6786 func_ptr_ref(pt->pt_func);
6787 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006788 if (ret_pt->pt_argc > 0)
6789 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006790 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006791 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006792 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006793 pt->pt_argc = 0;
6794 else
6795 {
6796 pt->pt_argc = ret_pt->pt_argc;
6797 for (i = 0; i < pt->pt_argc; i++)
6798 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6799 }
6800 }
6801 partial_unref(ret_pt);
6802 }
6803 rettv->v_type = VAR_PARTIAL;
6804 rettv->vval.v_partial = pt;
6805 }
6806 }
6807 return selfdict;
6808}
6809
6810/*
6811 * Return the name of the executed function.
6812 */
6813 char_u *
6814func_name(void *cookie)
6815{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006816 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006817}
6818
6819/*
6820 * Return the address holding the next breakpoint line for a funccall cookie.
6821 */
6822 linenr_T *
6823func_breakpoint(void *cookie)
6824{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006825 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006826}
6827
6828/*
6829 * Return the address holding the debug tick for a funccall cookie.
6830 */
6831 int *
6832func_dbg_tick(void *cookie)
6833{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006834 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006835}
6836
6837/*
6838 * Return the nesting level for a funccall cookie.
6839 */
6840 int
6841func_level(void *cookie)
6842{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006843 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006844}
6845
6846/*
6847 * Return TRUE when a function was ended by a ":return" command.
6848 */
6849 int
6850current_func_returned(void)
6851{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006852 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006853}
6854
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006855 int
6856free_unref_funccal(int copyID, int testing)
6857{
6858 int did_free = FALSE;
6859 int did_free_funccal = FALSE;
6860 funccall_T *fc, **pfc;
6861
6862 for (pfc = &previous_funccal; *pfc != NULL; )
6863 {
6864 if (can_free_funccal(*pfc, copyID))
6865 {
6866 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006867 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006868 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006869 did_free = TRUE;
6870 did_free_funccal = TRUE;
6871 }
6872 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006873 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006874 }
6875 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006876 // When a funccal was freed some more items might be garbage
6877 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006878 (void)garbage_collect(testing);
6879
6880 return did_free;
6881}
6882
6883/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006884 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006885 */
6886 static funccall_T *
6887get_funccal(void)
6888{
6889 int i;
6890 funccall_T *funccal;
6891 funccall_T *temp_funccal;
6892
6893 funccal = current_funccal;
6894 if (debug_backtrace_level > 0)
6895 {
6896 for (i = 0; i < debug_backtrace_level; i++)
6897 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006898 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006899 if (temp_funccal)
6900 funccal = temp_funccal;
6901 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006902 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006903 debug_backtrace_level = i;
6904 }
6905 }
6906 return funccal;
6907}
6908
6909/*
6910 * Return the hashtable used for local variables in the current funccal.
6911 * Return NULL if there is no current funccal.
6912 */
6913 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006914get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006915{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006916 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006917 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006918 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006919}
6920
6921/*
6922 * Return the l: scope variable.
6923 * Return NULL if there is no current funccal.
6924 */
6925 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006926get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006927{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006928 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006929 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006930 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006931}
6932
6933/*
6934 * Return the hashtable used for argument in the current funccal.
6935 * Return NULL if there is no current funccal.
6936 */
6937 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006938get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006939{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006940 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006941 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006942 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006943}
6944
6945/*
6946 * Return the a: scope variable.
6947 * Return NULL if there is no current funccal.
6948 */
6949 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006950get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006951{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006952 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006953 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006954 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006955}
6956
6957/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006958 * List function variables, if there is a function.
6959 */
6960 void
6961list_func_vars(int *first)
6962{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006963 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
6964 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006965 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006966}
6967
6968/*
6969 * If "ht" is the hashtable for local variables in the current funccal, return
6970 * the dict that contains it.
6971 * Otherwise return NULL.
6972 */
6973 dict_T *
6974get_current_funccal_dict(hashtab_T *ht)
6975{
6976 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01006977 && ht == &current_funccal->fc_l_vars.dv_hashtab)
6978 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006979 return NULL;
6980}
6981
6982/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006983 * Search hashitem in parent scope.
6984 */
6985 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006986find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006987{
6988 funccall_T *old_current_funccal = current_funccal;
6989 hashtab_T *ht;
6990 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006991 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006992
Bram Moolenaarca16c602022-09-06 18:57:08 +01006993 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006994 return NULL;
6995
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006996 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006997 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006998 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006999 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007000 ht = find_var_ht(name, &varname);
7001 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02007002 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007003 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02007004 if (!HASHITEM_EMPTY(hi))
7005 {
7006 *pht = ht;
7007 break;
7008 }
7009 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007010 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02007011 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007012 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007013 }
7014 current_funccal = old_current_funccal;
7015
7016 return hi;
7017}
7018
7019/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007020 * Search variable in parent scope.
7021 */
7022 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007023find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007024{
7025 dictitem_T *v = NULL;
7026 funccall_T *old_current_funccal = current_funccal;
7027 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007028 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007029
Bram Moolenaarca16c602022-09-06 18:57:08 +01007030 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007031 return NULL;
7032
Bram Moolenaare38eab22019-12-05 21:50:01 +01007033 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01007034 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007035 while (current_funccal)
7036 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007037 ht = find_var_ht(name, &varname);
7038 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007039 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007040 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007041 if (v != NULL)
7042 break;
7043 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007044 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007045 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007046 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007047 }
7048 current_funccal = old_current_funccal;
7049
7050 return v;
7051}
7052
7053/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007054 * Set "copyID + 1" in previous_funccal and callers.
7055 */
7056 int
7057set_ref_in_previous_funccal(int copyID)
7058{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007059 funccall_T *fc;
7060
Bram Moolenaarca16c602022-09-06 18:57:08 +01007061 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007062 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007063 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007064 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
7065 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
7066 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007067 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007068 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007069 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007070}
7071
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007072 static int
7073set_ref_in_funccal(funccall_T *fc, int copyID)
7074{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007075 if (fc->fc_copyID != copyID)
7076 {
7077 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007078 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
7079 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
7080 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
7081 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007082 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007083 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007084 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007085}
7086
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007087/*
7088 * Set "copyID" in all local vars and arguments in the call stack.
7089 */
7090 int
7091set_ref_in_call_stack(int copyID)
7092{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007093 funccall_T *fc;
7094 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007095
Bram Moolenaarca16c602022-09-06 18:57:08 +01007096 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007097 if (set_ref_in_funccal(fc, copyID))
7098 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007099
7100 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007101 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007102 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007103 if (set_ref_in_funccal(fc, copyID))
7104 return TRUE;
7105 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007106}
7107
7108/*
7109 * Set "copyID" in all functions available by name.
7110 */
7111 int
7112set_ref_in_functions(int copyID)
7113{
7114 int todo;
7115 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007116 ufunc_T *fp;
7117
7118 todo = (int)func_hashtab.ht_used;
7119 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007120 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007121 if (!HASHITEM_EMPTY(hi))
7122 {
7123 --todo;
7124 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007125 if (!func_name_refcount(fp->uf_name)
7126 && set_ref_in_func(NULL, fp, copyID))
7127 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007128 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007129 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007130 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007131}
7132
7133/*
7134 * Set "copyID" in all function arguments.
7135 */
7136 int
7137set_ref_in_func_args(int copyID)
7138{
7139 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007140
7141 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007142 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7143 copyID, NULL, NULL))
7144 return TRUE;
7145 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007146}
7147
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007148/*
7149 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007150 * Returns TRUE if setting references failed somehow.
7151 */
7152 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007153set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007154{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007155 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007156 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007157 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007158 char_u fname_buf[FLEN_FIXED + 1];
7159 char_u *tofree = NULL;
7160 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007161 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007162
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007163 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007164 return FALSE;
7165
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007166 if (fp_in == NULL)
7167 {
7168 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007169 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007170 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007171 if (fp != NULL)
7172 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007173 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007174 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007175 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007176
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007177 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007178 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007179}
7180
Bram Moolenaare38eab22019-12-05 21:50:01 +01007181#endif // FEAT_EVAL