blob: 71b39837c74ce7e72a27c9278da63abe52d8329c [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +020011 * userfunc.c: User defined function support
Bram Moolenaara9b579f2016-07-17 18:29:19 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020017/*
18 * All user-defined functions are found in this hashtable.
19 */
20static hashtab_T func_hashtab;
21
Bram Moolenaare38eab22019-12-05 21:50:01 +010022// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020023static garray_T funcargs = GA_EMPTY;
24
Bram Moolenaar209b8e32019-03-14 13:43:24 +010025// pointer to funccal for currently active function
26static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020027
Bram Moolenaar209b8e32019-03-14 13:43:24 +010028// Pointer to list of previously used funccal, still around because some
29// item in it is still being used.
30static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020031
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020032static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +010033static void func_clear(ufunc_T *fp, int force);
34static int func_free(ufunc_T *fp, int force);
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +010035static char_u *untrans_function_name(char_u *name);
Bram Moolenaar58779852022-09-06 18:31:14 +010036static void handle_defer_one(funccall_T *funccal);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020037
38 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +000039func_init(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020040{
41 hash_init(&func_hashtab);
42}
43
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020044/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020045 * Return the function hash table
46 */
47 hashtab_T *
48func_tbl_get(void)
49{
50 return &func_hashtab;
51}
52
53/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020054 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020055 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010056 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010057 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaardce24412022-02-08 20:35:30 +000058 * If "eap" is not NULL use it to check for an already declared name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010059 * Return a pointer to after the type.
60 * When something is wrong return "arg".
61 */
62 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010063one_function_arg(
64 char_u *arg,
65 garray_T *newargs,
66 garray_T *argtypes,
67 int types_optional,
h-eastb895b0f2023-09-24 15:46:31 +020068 garray_T *arg_objm,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010069 evalarg_T *evalarg,
Bram Moolenaardce24412022-02-08 20:35:30 +000070 exarg_T *eap,
Bram Moolenaar2a389082021-04-09 20:24:31 +020071 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010072 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073{
Bram Moolenaar6e949782020-04-13 17:21:00 +020074 char_u *p = arg;
75 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020076 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077
78 while (ASCII_ISALNUM(*p) || *p == '_')
79 ++p;
Keith Thompson184f71c2024-01-04 21:19:04 +010080 if (arg == p || SAFE_isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020081 || (argtypes == NULL
82 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
83 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010084 {
85 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000086 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087 return arg;
88 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010089
Bram Moolenaarce723f32023-06-10 19:00:12 +010090 // Extra checks in Vim9 script.
91 if (!skip && argtypes != NULL)
92 {
93 int c = *p;
94 *p = NUL;
95 int r = check_reserved_name(arg, FALSE);
96 *p = c;
97 if (r == FAIL)
98 return arg;
99
100 // Cannot use script var name for argument. In function: also check
101 // local vars and arguments.
102 if (check_defined(arg, p - arg,
103 evalarg == NULL ? NULL : evalarg->eval_cctx,
Bram Moolenaardce24412022-02-08 20:35:30 +0000104 eap == NULL ? NULL : eap->cstack, TRUE) == FAIL)
Bram Moolenaarce723f32023-06-10 19:00:12 +0100105 return arg;
106 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100108 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
109 return arg;
110 if (newargs != NULL)
111 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100112 int c;
113 int i;
114
115 c = *p;
116 *p = NUL;
117 arg_copy = vim_strsave(arg);
118 if (arg_copy == NULL)
119 {
120 *p = c;
121 return arg;
122 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200123 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200124 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200125 // Check for duplicate argument name.
126 for (i = 0; i < newargs->ga_len; ++i)
127 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
128 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000129 semsg(_(e_duplicate_argument_name_str), arg_copy);
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200130 vim_free(arg_copy);
131 return arg;
132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100133 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
134 newargs->ga_len++;
135
136 *p = c;
137 }
138
139 // get any type from "arg: type"
h-eastb895b0f2023-09-24 15:46:31 +0200140 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK)
141 && arg_objm != NULL && (skip || ga_grow(arg_objm, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 {
143 char_u *type = NULL;
144
Bram Moolenaar6e949782020-04-13 17:21:00 +0200145 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
146 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200147 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200148 arg_copy == NULL ? arg : arg_copy);
149 p = skipwhite(p);
150 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 if (*p == ':')
152 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200153 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100154 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200155 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100156 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200157 return arg;
158 }
159 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200160 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100161 if (!skip)
162 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100163 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200164 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200165 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200166 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200167 arg_copy == NULL ? arg : arg_copy);
168 return arg;
169 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100170 if (!skip)
171 {
172 if (type == NULL && types_optional)
173 // lambda arguments default to "any" type
Bram Moolenaar2a389082021-04-09 20:24:31 +0200174 type = vim_strsave((char_u *)
175 (is_vararg ? "list<any>" : "any"));
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100176 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
h-eastb895b0f2023-09-24 15:46:31 +0200177 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = FALSE;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 }
180
181 return p;
182}
183
184/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000185 * Handle line continuation in function arguments or body.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000186 * Get a next line, store it in "eap" if appropriate and put the line in
187 * "lines_to_free" to free the line later.
Bram Moolenaar7473a842021-12-28 17:55:26 +0000188 */
189 static char_u *
190get_function_line(
191 exarg_T *eap,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000192 garray_T *lines_to_free,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000193 int indent,
194 getline_opt_T getline_options)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000195{
196 char_u *theline;
197
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100198 if (eap->ea_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +0000199 theline = getcmdline(':', 0L, indent, 0);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000200 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100201 theline = eap->ea_getline(':', eap->cookie, indent, getline_options);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000202 if (theline != NULL)
203 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000204 if (lines_to_free->ga_len > 0
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000205 && eap->cmdlinep != NULL
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000206 && *eap->cmdlinep == ((char_u **)lines_to_free->ga_data)
207 [lines_to_free->ga_len - 1])
Bram Moolenaar7473a842021-12-28 17:55:26 +0000208 *eap->cmdlinep = theline;
Bram Moolenaarb5820102023-01-25 12:27:13 +0000209 (void)ga_add_string(lines_to_free, theline);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000210 }
211
212 return theline;
213}
214
215/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200216 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100217 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100218 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200219 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100220 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200221get_function_args(
222 char_u **argp,
223 char_u endchar,
224 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100225 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100226 int types_optional, // types optional if "argtypes" is not NULL
h-eastb895b0f2023-09-24 15:46:31 +0200227 garray_T *arg_objm, // NULL unless using :def
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100228 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200229 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200230 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200231 int skip,
Bram Moolenaardce24412022-02-08 20:35:30 +0000232 exarg_T *eap, // can be NULL
Bram Moolenaar554d0312023-01-05 19:59:18 +0000233 int in_class, // non-zero when inside a class or interface
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000234 garray_T *newlines, // function body lines
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000235 garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200236{
237 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100238 char_u *arg;
239 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200240 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200241 int any_default = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100242 char_u *whitep = *argp;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100243 int need_expr = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200244
245 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000246 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100247 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000248 ga_init2(argtypes, sizeof(char_u *), 3);
h-eastb895b0f2023-09-24 15:46:31 +0200249 if (arg_objm != NULL)
250 ga_init2(arg_objm, sizeof(int8_T), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200251 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000252 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200253
254 if (varargs != NULL)
255 *varargs = FALSE;
256
257 /*
258 * Isolate the arguments: "arg1, arg2, ...)"
259 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100260 arg = skipwhite(*argp);
261 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200262 while (*p != endchar)
263 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100264 while (eap != NULL && eap->ea_getline != NULL
Bram Moolenaar2c330432020-04-13 14:41:35 +0200265 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200266 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200267 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000268 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000269 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000270
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200271 if (theline == NULL)
272 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200273 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200274 p = skipwhite(theline);
275 }
276
277 if (mustend && *p != endchar)
278 {
279 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000280 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100281 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200282 }
Christian Brabandte4a450a2023-12-08 20:57:38 +0100283 if (*p == endchar && !need_expr)
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200284 break;
285
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200286 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
287 {
288 if (varargs != NULL)
289 *varargs = TRUE;
290 p += 3;
291 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100292
293 if (argtypes != NULL)
294 {
295 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200296 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100298 if (!skip)
299 emsg(_(e_missing_name_after_dots));
300 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100301 }
302
303 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100304 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200305 arg_objm, evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100306 if (p == arg)
307 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100308 if (*skipwhite(p) == '=')
309 {
310 emsg(_(e_cannot_use_default_for_variable_arguments));
311 break;
312 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100313 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200314 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000315 else if (in_class && STRNCMP(p, "this.", 5) == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000316 {
317 // this.memberName
318 p += 5;
319 arg = p;
320 while (ASCII_ISALNUM(*p) || *p == '_')
321 ++p;
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000322 char_u *argend = p;
323
h-eastdb385522023-09-28 22:18:19 +0200324 // object variable this. can be used only in a constructor
325 if (STRNCMP(eap->arg, "new", 3) != 0)
326 {
327 c = *argend;
328 *argend = NUL;
329 semsg(_(e_cannot_use_an_object_variable_except_with_the_new_method_str), arg);
330 *argend = c;
331 break;
332 }
333
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000334 if (*skipwhite(p) == '=')
335 {
336 char_u *defval = skipwhite(skipwhite(p) + 1);
337 if (STRNCMP(defval, "v:none", 6) != 0)
338 {
339 semsg(_(e_constructor_default_value_must_be_vnone_str), p);
340 goto err_ret;
341 }
342 any_default = TRUE;
343 p = defval + 6;
344
345 if (ga_grow(default_args, 1) == FAIL)
346 goto err_ret;
347
348 char_u *expr = vim_strsave((char_u *)"v:none");
349 if (expr == NULL)
350 goto err_ret;
351 ((char_u **)(default_args->ga_data))
352 [default_args->ga_len] = expr;
353 default_args->ga_len++;
354 }
355 else if (any_default)
356 {
357 emsg(_(e_non_default_argument_follows_default_argument));
358 goto err_ret;
359 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000360
361 // TODO: check the argument is indeed a member
362 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
363 return FAIL;
364 if (newargs != NULL)
365 {
366 ((char_u **)(newargs->ga_data))[newargs->ga_len] =
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000367 vim_strnsave(arg, argend - arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000368 newargs->ga_len++;
369
h-eastb895b0f2023-09-24 15:46:31 +0200370 if (argtypes != NULL && ga_grow(argtypes, 1) == OK
371 && arg_objm != NULL && ga_grow(arg_objm, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000372 {
373 // TODO: use the actual type
374 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] =
375 vim_strsave((char_u *)"any");
h-eastb895b0f2023-09-24 15:46:31 +0200376 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000377
378 // Add a line to the function body for the assignment.
379 if (ga_grow(newlines, 1) == OK)
380 {
381 // "this.name = name"
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000382 int len = 5 + (argend - arg) + 3 + (argend - arg) + 1;
383 if (any_default)
384 len += 14 + 10;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000385 char_u *assignment = alloc(len);
386 if (assignment != NULL)
387 {
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000388 c = *argend;
389 *argend = NUL;
390 if (any_default)
391 vim_snprintf((char *)assignment, len,
392 "ifargisset %d this.%s = %s",
393 default_args->ga_len - 1, arg, arg);
394 else
395 vim_snprintf((char *)assignment, len,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000396 "this.%s = %s", arg, arg);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000397 *argend = c;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000398 ((char_u **)(newlines->ga_data))[
399 newlines->ga_len++] = assignment;
400 }
401 }
402 }
403 }
404 if (*p == ',')
405 ++p;
406 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200407 else
408 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200409 char_u *np;
410
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200411 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100412 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200413 arg_objm, evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200415 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200416
Bram Moolenaar015cf102021-06-26 21:52:02 +0200417 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200418 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200419 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200420 if (*np == '=' && np[1] != '=' && np[1] != '~'
421 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200422 {
423 typval_T rettv;
424
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100425 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200426 any_default = TRUE;
Bram Moolenaareb5adf12022-09-04 13:41:37 +0100427 p = skipwhite(np + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000428 char_u *expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200429 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200430 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200431 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200432 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200433 if (ga_grow(default_args, 1) == FAIL)
434 goto err_ret;
435
Christian Brabandte4a450a2023-12-08 20:57:38 +0100436 if (need_expr)
437 need_expr = FALSE;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200438 // trim trailing whitespace
439 while (p > expr && VIM_ISWHITE(p[-1]))
440 p--;
441 c = *p;
442 *p = NUL;
443 expr = vim_strsave(expr);
444 if (expr == NULL)
445 {
446 *p = c;
447 goto err_ret;
448 }
449 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200450 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200451 default_args->ga_len++;
452 *p = c;
453 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200454 }
455 else
Christian Brabandte4a450a2023-12-08 20:57:38 +0100456 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200457 mustend = TRUE;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100458 if (*skipwhite(p) == NUL)
459 need_expr = TRUE;
460 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200461 }
462 else if (any_default)
463 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000464 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200465 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200466 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200467
468 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
469 {
470 // Be tolerant when skipping
471 if (!skip)
472 {
473 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
474 goto err_ret;
475 }
476 p = skipwhite(p);
477 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200478 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200479 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200480 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200481 // Don't give this error when skipping, it makes the "->" not
482 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100483 // Allow missing space after comma in legacy functions.
484 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200485 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
486 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100487 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200488 goto err_ret;
489 }
490 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200491 else
492 mustend = TRUE;
493 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200494 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200495 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200496 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200497
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200498 if (*p != endchar)
499 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100500 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200501
502 *argp = p;
503 return OK;
504
505err_ret:
506 if (newargs != NULL)
507 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200508 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200509 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200510 return FAIL;
511}
512
513/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100514 * Parse the argument types, filling "fp->uf_arg_types".
515 * Return OK or FAIL.
516 */
517 static int
h-eastb895b0f2023-09-24 15:46:31 +0200518parse_argument_types(
519 ufunc_T *fp,
520 garray_T *argtypes,
521 int varargs,
522 garray_T *arg_objm,
523 ocmember_T *obj_members,
524 int obj_member_count)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100525{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200526 int len = 0;
527
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100528 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
529 if (argtypes->ga_len > 0)
530 {
531 // When "varargs" is set the last name/type goes into uf_va_name
532 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200533 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100534
535 if (len > 0)
536 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
537 if (fp->uf_arg_types != NULL)
538 {
539 int i;
540 type_T *type;
541
542 for (i = 0; i < len; ++ i)
543 {
544 char_u *p = ((char_u **)argtypes->ga_data)[i];
545
546 if (p == NULL)
547 // will get the type from the default value
548 type = &t_unknown;
549 else
h-eastb895b0f2023-09-24 15:46:31 +0200550 {
551 if (arg_objm != NULL && ((int8_T *)arg_objm->ga_data)[i])
552 {
553 char_u *aname = ((char_u **)fp->uf_args.ga_data)[i];
554
555 type = &t_any;
556 for (int om = 0; om < obj_member_count; ++om)
557 {
Christian Brabandt915f3bf2024-04-05 20:12:19 +0200558 if (obj_members != NULL
559 && STRCMP(aname,
560 obj_members[om].ocm_name) == 0)
h-eastb895b0f2023-09-24 15:46:31 +0200561 {
562 type = obj_members[om].ocm_type;
563 break;
564 }
565 }
566 }
567 else
568 type = parse_type(&p, &fp->uf_type_list, TRUE);
569 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100570 if (type == NULL)
571 return FAIL;
572 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000573 if (i < fp->uf_args.ga_len
574 && (type->tt_type == VAR_FUNC
Yegappan Lakshmanan3e336502024-04-04 19:35:59 +0200575 || type->tt_type == VAR_PARTIAL))
576 {
577 char_u *name = ((char_u **)fp->uf_args.ga_data)[i];
578 if (obj_members != NULL && *name == '_')
579 // protected object method
580 name++;
581
582 if (var_wrong_func_name(name, TRUE))
583 return FAIL;
584 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100585 }
586 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100587 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200588
589 if (varargs)
590 {
591 char_u *p;
592
593 // Move the last argument "...name: type" to uf_va_name and
594 // uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200595 --fp->uf_args.ga_len;
Bram Moolenaarf0571712023-01-04 13:16:20 +0000596 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len];
597 ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len] = NULL;
Bram Moolenaar2a389082021-04-09 20:24:31 +0200598 p = ((char_u **)argtypes->ga_data)[len];
599 if (p == NULL)
600 // TODO: get type from default value
601 fp->uf_va_type = &t_list_any;
602 else
603 {
604 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
605 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
606 {
607 semsg(_(e_variable_arguments_type_must_be_list_str),
608 ((char_u **)argtypes->ga_data)[len]);
609 return FAIL;
610 }
611 }
612 if (fp->uf_va_type == NULL)
613 return FAIL;
614 }
615
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100616 return OK;
617}
618
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100619 static int
620parse_return_type(ufunc_T *fp, char_u *ret_type)
621{
622 if (ret_type == NULL)
623 fp->uf_ret_type = &t_void;
624 else
625 {
626 char_u *p = ret_type;
627
628 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
629 if (fp->uf_ret_type == NULL)
630 {
631 fp->uf_ret_type = &t_void;
632 return FAIL;
633 }
634 }
635 return OK;
636}
637
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100638/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200639 * Register function "fp" as using "current_funccal" as its scope.
640 */
641 static int
642register_closure(ufunc_T *fp)
643{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200644 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100645 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200646 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200647 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200648 fp->uf_scoped = current_funccal;
649 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200650
Bram Moolenaarca16c602022-09-06 18:57:08 +0100651 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200652 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100653 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
654 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200655 return OK;
656}
657
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100658 static void
659set_ufunc_name(ufunc_T *fp, char_u *name)
660{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100661 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
662 // actually extends beyond the struct.
663 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100664
665 if (name[0] == K_SPECIAL)
666 {
667 fp->uf_name_exp = alloc(STRLEN(name) + 3);
668 if (fp->uf_name_exp != NULL)
669 {
670 STRCPY(fp->uf_name_exp, "<SNR>");
671 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
672 }
673 }
674}
675
Bram Moolenaar58016442016-07-31 18:30:22 +0200676/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100677 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
678 * return "buf" filled with a readable function name.
679 * Otherwise just return "name", thus the return value can always be used.
680 * "name" and "buf" may be equal.
681 */
682 char_u *
683make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
684{
685 size_t len;
686
687 if (name[0] != K_SPECIAL)
688 return name;
689 len = STRLEN(name);
690 if (len + 3 > bufsize)
691 return name;
692
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100693 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100694 mch_memmove(buf, "<SNR>", 5);
695 return buf;
696}
697
698/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200699 * Get a name for a lambda. Returned in static memory.
700 */
701 char_u *
702get_lambda_name(void)
703{
704 static char_u name[30];
705 static int lambda_no = 0;
706
707 sprintf((char*)name, "<lambda>%d", ++lambda_no);
708 return name;
709}
710
Bram Moolenaare01e5212023-01-08 20:31:18 +0000711/*
712 * Allocate a "ufunc_T" for a function called "name".
713 * Makes sure the size is right.
714 */
715 static ufunc_T *
716alloc_ufunc(char_u *name)
717{
718 // When the name is short we need to make sure we allocate enough bytes for
719 // the whole struct, including any padding.
720 size_t len = offsetof(ufunc_T, uf_name) + STRLEN(name) + 1;
721 return alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
722}
723
Bram Moolenaar801ab062020-06-25 19:27:56 +0200724#if defined(FEAT_LUA) || defined(PROTO)
725/*
726 * Registers a native C callback which can be called from Vim script.
727 * Returns the name of the Vim script function.
728 */
729 char_u *
730register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
731{
732 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200733 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200734
Bram Moolenaare01e5212023-01-08 20:31:18 +0000735 fp = alloc_ufunc(name);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200736 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200737 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200738
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200739 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200740 fp->uf_refcount = 1;
741 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000742 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200743 fp->uf_calls = 0;
744 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200745 fp->uf_cb = cb;
746 fp->uf_cb_free = cb_free;
747 fp->uf_cb_state = state;
748
749 set_ufunc_name(fp, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000750 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200751
752 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200753}
754#endif
755
Bram Moolenaar04b12692020-05-04 23:24:44 +0200756/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100757 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100758 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100759 * If "white_error" is not NULL check for correct use of white space and set
760 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100761 * Return NULL if no valid arrow found.
762 */
763 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100764skip_arrow(
765 char_u *start,
766 int equal_arrow,
767 char_u **ret_type,
768 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100769{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100770 char_u *s = start;
771 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100772
773 if (equal_arrow)
774 {
775 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100776 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100777 if (white_error != NULL && !VIM_ISWHITE(s[1]))
778 {
779 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100780 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100781 return NULL;
782 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100783 s = skipwhite(s + 1);
784 *ret_type = s;
785 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100786 if (s == *ret_type)
787 {
788 emsg(_(e_missing_return_type));
789 return NULL;
790 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100791 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100792 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100793 s = skipwhite(s);
794 if (*s != '=')
795 return NULL;
796 ++s;
797 }
798 if (*s != '>')
799 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100800 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
801 || !IS_WHITE_OR_NUL(s[1])))
802 {
803 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100804 semsg(_(e_white_space_required_before_and_after_str_at_str),
805 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100806 return NULL;
807 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100808 return skipwhite(s + 1);
809}
810
811/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100812 * Check if "*cmd" points to a function command and if so advance "*cmd" and
813 * return TRUE.
814 * Otherwise return FALSE;
815 * Do not consider "function(" to be a command.
816 */
817 static int
818is_function_cmd(char_u **cmd)
819{
820 char_u *p = *cmd;
821
822 if (checkforcmd(&p, "function", 2))
823 {
824 if (*p == '(')
825 return FALSE;
826 *cmd = p;
827 return TRUE;
828 }
829 return FALSE;
830}
831
832/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200833 * Called when defining a function: The context may be needed for script
834 * variables declared in a block that is visible now but not when the function
835 * is compiled or called later.
836 */
837 static void
838function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
839{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000840 if (cstack == NULL || cstack->cs_idx < 0)
841 return;
842
843 int count = cstack->cs_idx + 1;
844 int i;
845
846 fp->uf_block_ids = ALLOC_MULT(int, count);
847 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200848 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000849 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
850 sizeof(int) * count);
851 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200852 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000853
854 // Set flag in each block to indicate a function was defined. This
855 // is used to keep the variable when leaving the block, see
856 // hide_script_var().
857 for (i = 0; i <= cstack->cs_idx; ++i)
858 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200859}
860
861/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100862 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200863 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100864 * "newlines" must already have been initialized.
865 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
866 */
867 static int
868get_function_body(
869 exarg_T *eap,
870 garray_T *newlines,
871 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000872 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100873{
874 linenr_T sourcing_lnum_top = SOURCING_LNUM;
875 linenr_T sourcing_lnum_off;
876 int saved_wait_return = need_wait_return;
877 char_u *line_arg = line_arg_in;
878 int vim9_function = eap->cmdidx == CMD_def
879 || eap->cmdidx == CMD_block;
880#define MAX_FUNC_NESTING 50
881 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200882 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100883 int nesting = 0;
884 getline_opt_T getline_options;
885 int indent = 2;
886 char_u *skip_until = NULL;
887 int ret = FAIL;
888 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200889 int heredoc_concat_len = 0;
890 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100891 char_u *heredoc_trimmed = NULL;
892
Bram Moolenaar20677332021-06-06 17:02:53 +0200893 ga_init2(&heredoc_ga, 1, 500);
894
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100895 // Detect having skipped over comment lines to find the return
896 // type. Add NULL lines to keep the line count correct.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100897 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100898 if (SOURCING_LNUM < sourcing_lnum_off)
899 {
900 sourcing_lnum_off -= SOURCING_LNUM;
901 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
902 goto theend;
903 while (sourcing_lnum_off-- > 0)
904 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
905 }
906
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200907 nesting_def[0] = vim9_function;
908 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100909 getline_options = vim9_function
910 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
911 for (;;)
912 {
913 char_u *theline;
914 char_u *p;
915 char_u *arg;
916
917 if (KeyTyped)
918 {
919 msg_scroll = TRUE;
920 saved_wait_return = FALSE;
921 }
922 need_wait_return = FALSE;
923
924 if (line_arg != NULL)
925 {
926 // Use eap->arg, split up in parts by line breaks.
927 theline = line_arg;
928 p = vim_strchr(theline, '\n');
929 if (p == NULL)
930 line_arg += STRLEN(line_arg);
931 else
932 {
933 *p = NUL;
934 line_arg = p + 1;
935 }
936 }
937 else
938 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000939 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100940 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100941 }
942 if (KeyTyped)
943 lines_left = Rows - 1;
944 if (theline == NULL)
945 {
946 // Use the start of the function for the line number.
947 SOURCING_LNUM = sourcing_lnum_top;
948 if (skip_until != NULL)
949 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200950 else if (nesting_inline[nesting])
951 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100952 else if (eap->cmdidx == CMD_def)
953 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100954 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000955 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100956 goto theend;
957 }
958
959 // Detect line continuation: SOURCING_LNUM increased more than one.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100960 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100961 if (SOURCING_LNUM < sourcing_lnum_off)
962 sourcing_lnum_off -= SOURCING_LNUM;
963 else
964 sourcing_lnum_off = 0;
965
966 if (skip_until != NULL)
967 {
968 // Don't check for ":endfunc"/":enddef" between
969 // * ":append" and "."
970 // * ":python <<EOF" and "EOF"
971 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
972 if (heredoc_trimmed == NULL
973 || (is_heredoc && skipwhite(theline) == theline)
974 || STRNCMP(theline, heredoc_trimmed,
975 STRLEN(heredoc_trimmed)) == 0)
976 {
977 if (heredoc_trimmed == NULL)
978 p = theline;
979 else if (is_heredoc)
980 p = skipwhite(theline) == theline
981 ? theline : theline + STRLEN(heredoc_trimmed);
982 else
983 p = theline + STRLEN(heredoc_trimmed);
984 if (STRCMP(p, skip_until) == 0)
985 {
986 VIM_CLEAR(skip_until);
987 VIM_CLEAR(heredoc_trimmed);
988 getline_options = vim9_function
989 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
990 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200991
992 if (heredoc_concat_len > 0)
993 {
994 // Replace the starting line with all the concatenated
995 // lines.
996 ga_concat(&heredoc_ga, theline);
997 vim_free(((char_u **)(newlines->ga_data))[
998 heredoc_concat_len - 1]);
999 ((char_u **)(newlines->ga_data))[
1000 heredoc_concat_len - 1] = heredoc_ga.ga_data;
1001 ga_init(&heredoc_ga);
1002 heredoc_concat_len = 0;
1003 theline += STRLEN(theline); // skip the "EOF"
1004 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001005 }
1006 }
1007 }
1008 else
1009 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001010 int c;
1011 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +00001012 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001013
1014 // skip ':' and blanks
1015 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
1016 ;
1017
1018 // Check for "endfunction", "enddef" or "}".
1019 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +00001020 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001021 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001022 ? *p == '}'
1023 : (checkforcmd(&p, nesting_def[nesting]
1024 ? "enddef" : "endfunction", 4)
1025 && *p != ':'))
1026 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00001027 if (!nesting_inline[nesting] && nesting_def[nesting]
1028 && p < cmd + 6)
1029 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001030 if (nesting-- == 0)
1031 {
1032 char_u *nextcmd = NULL;
1033
1034 if (*p == '|' || *p == '}')
1035 nextcmd = p + 1;
1036 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
1037 nextcmd = line_arg;
1038 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001039 && (vim9_function || p_verbose > 0))
1040 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +02001041 SOURCING_LNUM = sourcing_lnum_top
1042 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001043 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +00001044 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001045 else
1046 give_warning2((char_u *)
1047 _("W22: Text found after :endfunction: %s"),
1048 p, TRUE);
1049 }
1050 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001051 {
1052 // Another command follows. If the line came from "eap"
1053 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001054 // change "eap->cmdlinep" to point to the last fetched
1055 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001056 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001057 if (lines_to_free->ga_len > 0
1058 && *eap->cmdlinep !=
1059 ((char_u **)lines_to_free->ga_data)
1060 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001061 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001062 // *cmdlinep will be freed later, thus remove the
1063 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001064 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001065 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
1066 [lines_to_free->ga_len - 1];
1067 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001068 }
1069 }
1070 break;
1071 }
1072 }
1073
1074 // Check for mismatched "endfunc" or "enddef".
1075 // We don't check for "def" inside "func" thus we also can't check
1076 // for "enddef".
1077 // We continue to find the end of the function, although we might
1078 // not find it.
1079 else if (nesting_def[nesting])
1080 {
1081 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1082 emsg(_(e_mismatched_endfunction));
1083 }
1084 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1085 emsg(_(e_mismatched_enddef));
1086
1087 // Increase indent inside "if", "while", "for" and "try", decrease
1088 // at "end".
1089 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1090 indent -= 2;
1091 else if (STRNCMP(p, "if", 2) == 0
1092 || STRNCMP(p, "wh", 2) == 0
1093 || STRNCMP(p, "for", 3) == 0
1094 || STRNCMP(p, "try", 3) == 0)
1095 indent += 2;
1096
1097 // Check for defining a function inside this function.
1098 // Only recognize "def" inside "def", not inside "function",
1099 // For backwards compatibility, see Test_function_python().
1100 c = *p;
1101 if (is_function_cmd(&p)
1102 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1103 {
1104 if (*p == '!')
1105 p = skipwhite(p + 1);
1106 p += eval_fname_script(p);
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001107 vim_free(trans_function_name(&p, NULL, TRUE, 0));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001108 if (*skipwhite(p) == '(')
1109 {
1110 if (nesting == MAX_FUNC_NESTING - 1)
1111 emsg(_(e_function_nesting_too_deep));
1112 else
1113 {
1114 ++nesting;
1115 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001116 nesting_inline[nesting] = FALSE;
1117 indent += 2;
1118 }
1119 }
1120 }
1121
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001122 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001123 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001124 // Not a comment line: check for nested inline function.
1125 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001126 while (end > p && VIM_ISWHITE(*end))
1127 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001128 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001129 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001130 int is_block;
1131
1132 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001133 --end;
1134 while (end > p && VIM_ISWHITE(*end))
1135 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001136 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1137 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001138 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001139 char_u *s = p;
1140
1141 // check for line starting with "au" for :autocmd or
1142 // "com" for :command, these can use a {} block
1143 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1144 || checkforcmd_noparen(&s, "command", 3);
1145 }
1146
1147 if (is_block)
1148 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001149 if (nesting == MAX_FUNC_NESTING - 1)
1150 emsg(_(e_function_nesting_too_deep));
1151 else
1152 {
1153 ++nesting;
1154 nesting_def[nesting] = TRUE;
1155 nesting_inline[nesting] = TRUE;
1156 indent += 2;
1157 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001158 }
1159 }
1160 }
1161
1162 // Check for ":append", ":change", ":insert". Not for :def.
1163 p = skip_range(p, FALSE, NULL);
1164 if (!vim9_function
1165 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1166 || (p[0] == 'c'
1167 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1168 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1169 && (STRNCMP(&p[3], "nge", 3) != 0
1170 || !ASCII_ISALPHA(p[6])))))))
1171 || (p[0] == 'i'
1172 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1173 && (!ASCII_ISALPHA(p[2])
1174 || (p[2] == 's'
1175 && (!ASCII_ISALPHA(p[3])
1176 || p[3] == 'e'))))))))
1177 skip_until = vim_strsave((char_u *)".");
1178
1179 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1180 arg = skipwhite(skiptowhite(p));
1181 if (arg[0] == '<' && arg[1] =='<'
1182 && ((p[0] == 'p' && p[1] == 'y'
1183 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1184 || ((p[2] == '3' || p[2] == 'x')
1185 && !ASCII_ISALPHA(p[3]))))
1186 || (p[0] == 'p' && p[1] == 'e'
1187 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1188 || (p[0] == 't' && p[1] == 'c'
1189 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1190 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1191 && !ASCII_ISALPHA(p[3]))
1192 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1193 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1194 || (p[0] == 'm' && p[1] == 'z'
1195 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1196 ))
1197 {
1198 // ":python <<" continues until a dot, like ":append"
1199 p = skipwhite(arg + 2);
1200 if (STRNCMP(p, "trim", 4) == 0)
1201 {
1202 // Ignore leading white space.
1203 p = skipwhite(p + 4);
1204 heredoc_trimmed = vim_strnsave(theline,
1205 skipwhite(theline) - theline);
1206 }
1207 if (*p == NUL)
1208 skip_until = vim_strsave((char_u *)".");
1209 else
1210 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1211 getline_options = GETLINE_NONE;
1212 is_heredoc = TRUE;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001213 if (vim9_function && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001214 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001215 }
1216
Bram Moolenaard881d152022-05-13 13:50:36 +01001217 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001218 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001219 // Check for ":cmd v =<< [trim] EOF"
1220 // and ":cmd [a, b] =<< [trim] EOF"
1221 // and "lines =<< [trim] EOF" for Vim9
1222 // Where "cmd" can be "let", "var", "final" or "const".
zeertzjqa93d9cd2023-05-02 16:25:47 +01001223 arg = p;
1224 if (checkforcmd(&arg, "let", 2)
1225 || checkforcmd(&arg, "var", 3)
1226 || checkforcmd(&arg, "final", 5)
1227 || checkforcmd(&arg, "const", 5)
1228 || vim9_function)
Bram Moolenaard881d152022-05-13 13:50:36 +01001229 {
zeertzjq9a91d2b2024-04-09 21:47:10 +02001230 int save_sc_version = current_sctx.sc_version;
1231 int var_count = 0;
1232 int semicolon = 0;
zeertzjq9a91d2b2024-04-09 21:47:10 +02001233
1234 current_sctx.sc_version
1235 = vim9_function ? SCRIPT_VERSION_VIM9 : 1;
zeertzjq1817ccd2024-04-10 17:37:47 +02001236 arg = skip_var_list(arg, TRUE, &var_count, &semicolon,
zeertzjq9a91d2b2024-04-09 21:47:10 +02001237 TRUE);
zeertzjq1817ccd2024-04-10 17:37:47 +02001238 if (arg != NULL)
1239 arg = skipwhite(arg);
zeertzjq9a91d2b2024-04-09 21:47:10 +02001240 current_sctx.sc_version = save_sc_version;
zeertzjq1817ccd2024-04-10 17:37:47 +02001241 if (arg != NULL && STRNCMP(arg, "=<<", 3) == 0)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001242 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001243 p = skipwhite(arg + 3);
1244 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001245 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001246 if (STRNCMP(p, "trim", 4) == 0)
1247 {
1248 // Ignore leading white space.
1249 p = skipwhite(p + 4);
1250 heredoc_trimmed = vim_strnsave(theline,
1251 skipwhite(theline) - theline);
1252 continue;
1253 }
1254 if (STRNCMP(p, "eval", 4) == 0)
1255 {
1256 // Ignore leading white space.
1257 p = skipwhite(p + 4);
1258 continue;
1259 }
1260 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001261 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001262 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1263 getline_options = GETLINE_NONE;
1264 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001265 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001266 }
1267 }
1268 }
1269
1270 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001271 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001272 goto theend;
1273
Bram Moolenaar20677332021-06-06 17:02:53 +02001274 if (heredoc_concat_len > 0)
1275 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001276 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001277 // to be used for the instruction later.
1278 ga_concat(&heredoc_ga, theline);
1279 ga_concat(&heredoc_ga, (char_u *)"\n");
1280 p = vim_strsave((char_u *)"");
1281 }
1282 else
1283 {
1284 // Copy the line to newly allocated memory. get_one_sourceline()
1285 // allocates 250 bytes per line, this saves 80% on average. The
1286 // cost is an extra alloc/free.
1287 p = vim_strsave(theline);
1288 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001289 if (p == NULL)
1290 goto theend;
1291 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1292
1293 // Add NULL lines for continuation lines, so that the line count is
1294 // equal to the index in the growarray.
1295 while (sourcing_lnum_off-- > 0)
1296 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1297
1298 // Check for end of eap->arg.
1299 if (line_arg != NULL && *line_arg == NUL)
1300 line_arg = NULL;
1301 }
1302
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001303 // Return OK when no error was detected.
1304 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001305 ret = OK;
1306
1307theend:
1308 vim_free(skip_until);
1309 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001310 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001311 need_wait_return |= saved_wait_return;
1312 return ret;
1313}
1314
1315/*
1316 * Handle the body of a lambda. *arg points to the "{", process statements
1317 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001318 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001319 * When successful "rettv" is set to a funcref.
1320 */
1321 static int
1322lambda_function_body(
1323 char_u **arg,
1324 typval_T *rettv,
1325 evalarg_T *evalarg,
1326 garray_T *newargs,
1327 garray_T *argtypes,
1328 int varargs,
1329 garray_T *default_args,
1330 char_u *ret_type)
1331{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001332 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001333 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001334 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001335 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001336 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001337 exarg_T eap;
1338 garray_T newlines;
1339 char_u *cmdline = NULL;
1340 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001341 partial_T *pt;
1342 char_u *name;
1343 int lnum_save = -1;
1344 linenr_T sourcing_lnum_top = SOURCING_LNUM;
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001345 char_u *line_arg = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001346
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001347 *arg = skipwhite(*arg + 1);
1348 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001349 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001350 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001351 return FAIL;
1352 }
1353
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001354 // When there is a line break use what follows for the lambda body.
1355 // Makes lambda body initializers work for object and enum member
1356 // variables.
1357 if (**arg == '\n')
1358 line_arg = *arg + 1;
1359
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001360 CLEAR_FIELD(eap);
1361 eap.cmdidx = CMD_block;
1362 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001363 eap.cmdlinep = &cmdline;
1364 eap.skip = !evaluate;
1365 if (evalarg->eval_cctx != NULL)
1366 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1367 else
1368 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001369 eap.ea_getline = evalarg->eval_getline;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001370 eap.cookie = evalarg->eval_cookie;
1371 }
1372
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001373 ga_init2(&newlines, sizeof(char_u *), 10);
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001374 if (get_function_body(&eap, &newlines, line_arg,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001375 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001376 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001377
1378 // When inside a lambda must add the function lines to evalarg.eval_ga.
1379 evalarg->eval_break_count += newlines.ga_len;
1380 if (gap->ga_itemsize > 0)
1381 {
1382 int idx;
1383 char_u *last;
1384 size_t plen;
1385 char_u *pnl;
1386
1387 for (idx = 0; idx < newlines.ga_len; ++idx)
1388 {
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001389 char_u *p = ((char_u **)newlines.ga_data)[idx];
1390 if (p == NULL)
1391 // comment line in the lambda body
1392 continue;
1393
1394 p = skipwhite(p);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001395
Bram Moolenaarecb66452021-05-18 15:09:18 +02001396 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001397 goto erret;
1398
1399 // Going to concatenate the lines after parsing. For an empty or
1400 // comment line use an empty string.
1401 // Insert NL characters at the start of each line, the string will
1402 // be split again later in .get_lambda_tv().
1403 if (*p == NUL || vim9_comment_start(p))
1404 p = (char_u *)"";
1405 plen = STRLEN(p);
1406 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1407 if (pnl != NULL)
1408 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001409 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1410 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001411 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001412 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001413 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001414 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001415 // more is following after the "}", which was skipped
1416 last = cmdline;
1417 else
1418 // nothing is following the "}"
1419 last = (char_u *)"}";
1420 plen = STRLEN(last);
1421 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1422 if (pnl != NULL)
1423 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001424 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1425 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001426 }
1427
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001428 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001429 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001430 garray_T *tfgap = &evalarg->eval_tofree_ga;
1431
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001432 // Something comes after the "}".
1433 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001434
1435 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001436 if (ga_grow(tfgap, 1) == OK)
1437 {
1438 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1439 evalarg->eval_using_cmdline = TRUE;
1440 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001441 }
1442 else
1443 *arg = (char_u *)"";
1444
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001445 if (!evaluate)
1446 {
1447 ret = OK;
1448 goto erret;
1449 }
1450
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001451 name = get_lambda_name();
Bram Moolenaare01e5212023-01-08 20:31:18 +00001452 ufunc = alloc_ufunc(name);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001453 if (ufunc == NULL)
1454 goto erret;
1455 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001456 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001457 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001458 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001459 ufunc->uf_refcount = 1;
1460 ufunc->uf_args = *newargs;
1461 newargs->ga_data = NULL;
1462 ufunc->uf_def_args = *default_args;
1463 default_args->ga_data = NULL;
1464 ufunc->uf_func_type = &t_func_any;
1465
1466 // error messages are for the first function line
1467 lnum_save = SOURCING_LNUM;
1468 SOURCING_LNUM = sourcing_lnum_top;
1469
1470 // parse argument types
h-eastb895b0f2023-09-24 15:46:31 +02001471 if (parse_argument_types(ufunc, argtypes, varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001472 {
1473 SOURCING_LNUM = lnum_save;
1474 goto erret;
1475 }
1476
1477 // parse the return type, if any
1478 if (parse_return_type(ufunc, ret_type) == FAIL)
1479 goto erret;
1480
1481 pt = ALLOC_CLEAR_ONE(partial_T);
1482 if (pt == NULL)
1483 goto erret;
1484 pt->pt_func = ufunc;
1485 pt->pt_refcount = 1;
1486
1487 ufunc->uf_lines = newlines;
1488 newlines.ga_data = NULL;
1489 if (sandbox)
1490 ufunc->uf_flags |= FC_SANDBOX;
1491 if (!ASCII_ISUPPER(*ufunc->uf_name))
1492 ufunc->uf_flags |= FC_VIM9;
1493 ufunc->uf_script_ctx = current_sctx;
1494 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1495 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1496 set_function_type(ufunc);
1497
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001498 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1499
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001500 rettv->vval.v_partial = pt;
1501 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001502 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001503 ret = OK;
1504
1505erret:
1506 if (lnum_save >= 0)
1507 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001508 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001509 if (newargs != NULL)
1510 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001511 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001512 if (ufunc != NULL)
1513 {
1514 func_clear(ufunc, TRUE);
1515 func_free(ufunc, TRUE);
1516 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001517 return ret;
1518}
1519
1520/*
1521 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001522 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001523 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001524 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1525 */
1526 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001527get_lambda_tv(
1528 char_u **arg,
1529 typval_T *rettv,
1530 int types_optional,
1531 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001532{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001533 int evaluate = evalarg != NULL
1534 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001535 garray_T newargs;
1536 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001537 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001538 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001539 garray_T default_args;
h-eastb895b0f2023-09-24 15:46:31 +02001540 garray_T arg_objm;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001541 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001542 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001543 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001544 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001545 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001546 char_u *s;
1547 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001548 int *old_eval_lavars = eval_lavars_used;
1549 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001550 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001551 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001552 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001553 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001554 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001555 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001556
Bram Moolenaar4525a572022-02-13 11:57:33 +00001557 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001558 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001559
1560 ga_init(&newargs);
1561 ga_init(&newlines);
1562
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001563 // First, check if this is really a lambda expression. "->" or "=>" must
1564 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001565 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001566 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001567 types_optional ? &argtypes : NULL, types_optional,
1568 types_optional ? &arg_objm : NULL, evalarg,
1569 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001570 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001571 {
1572 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001573 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001574 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001575 ga_clear(&arg_objm);
1576 }
Bram Moolenaar0346b792021-01-31 22:18:29 +01001577 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001578 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001579
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001580 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001581 if (evaluate)
1582 pnewargs = &newargs;
1583 else
1584 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001585 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001586 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
h-eastb895b0f2023-09-24 15:46:31 +02001587 types_optional ? &argtypes : NULL, types_optional,
1588 types_optional ? &arg_objm : NULL, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001589 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001590 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001591 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001592 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001593 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001594 {
1595 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001596 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001597 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001598 ga_clear(&arg_objm);
1599 }
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001600 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001601 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001602 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001603 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001604
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001605 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1606 if (ret_type != NULL)
1607 {
1608 ret_type = vim_strsave(ret_type);
1609 tofree2 = ret_type;
1610 }
1611
Bram Moolenaare38eab22019-12-05 21:50:01 +01001612 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001613 if (evaluate)
1614 eval_lavars_used = &eval_lavars;
1615
Bram Moolenaar65c44152020-12-24 15:14:01 +01001616 *arg = skipwhite_and_linebreak(*arg, evalarg);
1617
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001618 // Recognize "{" as the start of a function body.
1619 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001620 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001621 if (evalarg == NULL)
1622 // cannot happen?
1623 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001624 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001625 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1626 types_optional ? &argtypes : NULL, varargs,
1627 &default_args, ret_type) == FAIL)
1628 goto errret;
1629 goto theend;
1630 }
1631 if (default_args.ga_len > 0)
1632 {
1633 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001634 goto errret;
1635 }
1636
Bram Moolenaare38eab22019-12-05 21:50:01 +01001637 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001638 start = *arg;
1639 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001640 if (ret == FAIL)
1641 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001642
Bram Moolenaar65c44152020-12-24 15:14:01 +01001643 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001644 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001645 *arg = skipwhite_and_linebreak(*arg, evalarg);
1646 if (**arg != '}')
1647 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001648 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001649 goto errret;
1650 }
1651 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001652 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001653
1654 if (evaluate)
1655 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001656 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001657 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001658 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001659 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001660 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001661
Bram Moolenaare01e5212023-01-08 20:31:18 +00001662 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001663 if (fp == NULL)
1664 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001665 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001666 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001667 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001668 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001669
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001670 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001671 if (ga_grow(&newlines, 1) == FAIL)
1672 goto errret;
1673
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001674 // If there are line breaks, we need to split up the string.
1675 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001676 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001677 line_end = end;
1678
1679 // Add "return " before the expression (or the first line).
1680 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001681 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001682 if (p == NULL)
1683 goto errret;
1684 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1685 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001686 vim_strncpy(p + 7, start, line_end - start);
1687
1688 if (line_end != end)
1689 {
1690 // Add more lines, split by line breaks. Thus is used when a
1691 // lambda with { cmds } is encountered.
1692 while (*line_end == '\n')
1693 {
1694 if (ga_grow(&newlines, 1) == FAIL)
1695 goto errret;
1696 start = line_end + 1;
1697 line_end = vim_strchr(start, '\n');
1698 if (line_end == NULL)
1699 line_end = end;
1700 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1701 vim_strnsave(start, line_end - start);
1702 }
1703 }
1704
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001705 if (strstr((char *)p + 7, "a:") == NULL)
1706 // No a: variables are used for sure.
1707 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001708
1709 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001710 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001711 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001712 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001713 if (types_optional)
1714 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001715 if (parse_argument_types(fp, &argtypes,
h-eastb895b0f2023-09-24 15:46:31 +02001716 vim9script && varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001717 goto errret;
1718 if (ret_type != NULL)
1719 {
1720 fp->uf_ret_type = parse_type(&ret_type,
1721 &fp->uf_type_list, TRUE);
1722 if (fp->uf_ret_type == NULL)
1723 goto errret;
1724 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001725 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001726 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001727 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001728
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001729 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001730 if (current_funccal != NULL && eval_lavars)
1731 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001732 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001733 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001734 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001735 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001736
1737#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001738 if (prof_def_func())
1739 func_do_profile(fp);
1740#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001741 if (sandbox)
1742 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001743 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001744 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001745 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001746 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001747 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001748 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001749 // Use the line number of the arguments.
1750 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001751
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001752 function_using_block_scopes(fp, evalarg->eval_cstack);
1753
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001754 pt->pt_func = fp;
1755 pt->pt_refcount = 1;
1756 rettv->vval.v_partial = pt;
1757 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001758
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001759 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001760 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001761
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001762theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001763 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001764 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001765 if (types_optional)
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001766 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001767 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001768 ga_clear(&arg_objm);
1769 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001770
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001771 return OK;
1772
1773errret:
1774 ga_clear_strings(&newargs);
1775 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001776 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001777 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001778 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001779 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001780 ga_clear(&arg_objm);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001781 if (fp != NULL)
1782 vim_free(fp->uf_arg_types);
1783 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001784 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001785 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001786 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001787 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001788 return FAIL;
1789}
1790
1791/*
1792 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1793 * name it contains, otherwise return "name".
1794 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1795 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001796 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1797 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001798 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001799 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001800 */
1801 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001802deref_func_name(
1803 char_u *name,
1804 int *lenp,
1805 partial_T **partialp,
1806 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001807 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001808 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001809 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001810{
1811 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001812 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001813 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001814 char_u *s = NULL;
1815 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001816 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001817
1818 if (partialp != NULL)
1819 *partialp = NULL;
1820
1821 cc = name[*lenp];
1822 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001823
Bram Moolenaar71f21932022-01-07 18:20:55 +00001824 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001825 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001826 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001827 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001828 tv = &v->di_tv;
1829 }
1830 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1831 {
1832 imported_T *import;
1833 char_u *p = name;
1834 int len = *lenp;
1835
1836 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001837 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001838 p = name + 2;
1839 len -= 2;
1840 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001841 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001842
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001843 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001844 if (import != NULL)
1845 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001846 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001847 if (new_function)
1848 semsg(_(e_redefining_imported_item_str), name);
1849 else
1850 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001851 name[len] = cc;
1852 *lenp = 0;
1853 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001854 }
1855 }
1856
1857 if (tv != NULL)
1858 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001859 if (found_var != NULL)
1860 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001861 if (tv->v_type == VAR_FUNC)
1862 {
1863 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001864 {
1865 *lenp = 0;
1866 return (char_u *)""; // just in case
1867 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001868 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001869 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001870 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001871
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001872 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001873 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001874 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001875
1876 if (pt == NULL)
1877 {
1878 *lenp = 0;
1879 return (char_u *)""; // just in case
1880 }
1881 if (partialp != NULL)
1882 *partialp = pt;
1883 s = partial_name(pt);
1884 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001885 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001886
1887 if (s != NULL)
1888 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001889 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001890 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001891 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001892
1893 if (sv != NULL)
1894 *type = sv->sv_type;
1895 }
1896 return s;
1897 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001898 }
1899
1900 return name;
1901}
1902
1903/*
1904 * Give an error message with a function name. Handle <SNR> things.
1905 * "ermsg" is to be passed without translation, use N_() instead of _().
1906 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001907 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001908emsg_funcname(char *ermsg, char_u *name)
1909{
Bram Moolenaar54598062022-01-13 12:05:09 +00001910 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001911
Bram Moolenaar54598062022-01-13 12:05:09 +00001912 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001913 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001914 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001915 if (p != name)
1916 vim_free(p);
1917}
1918
1919/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001920 * Get function arguments at "*arg" and advance it.
1921 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001922 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001923 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001924 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001925get_func_arguments(
1926 char_u **arg,
1927 evalarg_T *evalarg,
1928 int partial_argc,
1929 typval_T *argvars,
Ernie Raelb077b582023-12-14 20:11:44 +01001930 int *argcount,
1931 int is_builtin)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001932{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001933 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001934 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001935 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001936 int evaluate = evalarg == NULL
1937 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001938
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001939 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001940 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001941 // skip the '(' or ',' and possibly line breaks
1942 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1943
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001944 if (*argp == ')' || *argp == ',' || *argp == NUL)
1945 break;
Ernie Raelb077b582023-12-14 20:11:44 +01001946
1947 int arg_idx = *argcount;
1948 if (eval1(&argp, &argvars[arg_idx], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001949 {
1950 ret = FAIL;
1951 break;
1952 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001953 ++*argcount;
Ernie Raelb077b582023-12-14 20:11:44 +01001954 if (!is_builtin && check_typval_is_value(&argvars[arg_idx]) == FAIL)
1955 {
1956 ret = FAIL;
1957 break;
1958 }
1959
Bram Moolenaar9d489562020-07-30 20:08:50 +02001960 // The comma should come right after the argument, but this wasn't
1961 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001962 if (vim9script)
1963 {
1964 if (*argp != ',' && *skipwhite(argp) == ',')
1965 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001966 if (evaluate)
1967 semsg(_(e_no_white_space_allowed_before_str_str),
1968 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001969 ret = FAIL;
1970 break;
1971 }
1972 }
1973 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001974 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001975 if (*argp != ',')
1976 break;
Christian Brabandt00cb2472023-09-05 20:46:25 +02001977 if (vim9script && !IS_WHITE_NL_OR_NUL(argp[1]))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001978 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001979 if (evaluate)
1980 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001981 ret = FAIL;
1982 break;
1983 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001984 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001985
Bram Moolenaare6b53242020-07-01 17:28:33 +02001986 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001987 if (*argp == ')')
1988 ++argp;
1989 else
1990 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001991 *arg = argp;
1992 return ret;
1993}
1994
1995/*
1996 * Call a function and put the result in "rettv".
1997 * Return OK or FAIL.
1998 */
1999 int
2000get_func_tv(
2001 char_u *name, // name of the function
2002 int len, // length of "name" or -1 to use strlen()
2003 typval_T *rettv,
2004 char_u **arg, // argument, pointing to the '('
2005 evalarg_T *evalarg, // for line continuation
2006 funcexe_T *funcexe) // various values
2007{
2008 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002009 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002010 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
2011 int argcount = 0; // number of arguments found
2012 int vim9script = in_vim9script();
2013 int evaluate = evalarg == NULL
2014 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
2015
2016 argp = *arg;
2017 ret = get_func_arguments(&argp, evalarg,
2018 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
Ernie Raelb077b582023-12-14 20:11:44 +01002019 argvars, &argcount, builtin_function(name, -1));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002020
2021 if (ret == OK)
2022 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002023 int i = 0;
2024 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002025
2026 if (get_vim_var_nr(VV_TESTING))
2027 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002028 // Prepare for calling test_garbagecollect_now(), need to know
2029 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002030 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002031 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002032 for (i = 0; i < argcount; ++i)
2033 if (ga_grow(&funcargs, 1) == OK)
2034 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
2035 &argvars[i];
2036 }
2037
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002038 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00002039 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002040 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002041 // An error in a builtin function does not return FAIL, but we do
2042 // want to abort further processing if an error was given.
2043 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002044 clear_tv(rettv);
2045 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002046
2047 funcargs.ga_len -= i;
2048 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002049 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002050 {
2051 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002052 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002053 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00002054 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002055 }
2056
2057 while (--argcount >= 0)
2058 clear_tv(&argvars[argcount]);
2059
Bram Moolenaar4525a572022-02-13 11:57:33 +00002060 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02002061 *arg = argp;
2062 else
2063 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002064 return ret;
2065}
2066
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002067/*
2068 * Return TRUE if "p" starts with "<SID>" or "s:".
2069 * Only works if eval_fname_script() returned non-zero for "p"!
2070 */
2071 static int
2072eval_fname_sid(char_u *p)
2073{
2074 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
2075}
2076
2077/*
2078 * In a script change <SID>name() and s:name() to K_SNR 123_name().
2079 * Change <SNR>123_name() to K_SNR 123_name().
2080 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002081 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002082 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002083 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00002084fname_trans_sid(
2085 char_u *name,
2086 char_u *fname_buf,
2087 char_u **tofree,
2088 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002089{
2090 int llen;
2091 char_u *fname;
2092 int i;
2093
2094 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002095 if (llen == 0)
2096 return name; // no prefix
2097
2098 fname_buf[0] = K_SPECIAL;
2099 fname_buf[1] = KS_EXTRA;
2100 fname_buf[2] = (int)KE_SNR;
2101 i = 3;
2102 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002103 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002104 if (current_sctx.sc_sid <= 0)
2105 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002106 else
2107 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002108 sprintf((char *)fname_buf + 3, "%ld_",
2109 (long)current_sctx.sc_sid);
2110 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002111 }
2112 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002113 if (i + STRLEN(name + llen) < FLEN_FIXED)
2114 {
2115 STRCPY(fname_buf + i, name + llen);
2116 fname = fname_buf;
2117 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002118 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002119 {
2120 fname = alloc(i + STRLEN(name + llen) + 1);
2121 if (fname == NULL)
2122 *error = FCERR_OTHER;
2123 else
2124 {
2125 *tofree = fname;
2126 mch_memmove(fname, fname_buf, (size_t)i);
2127 STRCPY(fname + i, name + llen);
2128 }
2129 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002130 return fname;
2131}
2132
2133/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002134 * Concatenate the script ID and function name into "<SNR>99_name".
2135 * "buffer" must have size MAX_FUNC_NAME_LEN.
2136 */
2137 void
2138func_name_with_sid(char_u *name, int sid, char_u *buffer)
2139{
2140 // A script-local function is stored as "<SNR>99_name".
2141 buffer[0] = K_SPECIAL;
2142 buffer[1] = KS_EXTRA;
2143 buffer[2] = (int)KE_SNR;
2144 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2145 (long)sid, name);
2146}
2147
2148/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002149 * Find a function "name" in script "sid".
2150 */
2151 static ufunc_T *
2152find_func_with_sid(char_u *name, int sid)
2153{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002154 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002155 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156
Bram Moolenaarb8822442022-01-11 15:24:05 +00002157 if (!SCRIPT_ID_VALID(sid))
2158 return NULL; // not in a script
2159
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002160 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002161 hi = hash_find(&func_hashtab, buffer);
2162 if (!HASHITEM_EMPTY(hi))
2163 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002164 return NULL;
2165}
2166
2167/*
2168 * Find a function "name" in script "sid" prefixing the autoload prefix.
2169 */
2170 static ufunc_T *
2171find_func_with_prefix(char_u *name, int sid)
2172{
2173 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002174 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002175 scriptitem_T *si;
2176
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002177 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002178 return NULL; // already has the prefix
2179 if (!SCRIPT_ID_VALID(sid))
2180 return NULL; // not in a script
2181 si = SCRIPT_ITEM(sid);
2182 if (si->sn_autoload_prefix != NULL)
2183 {
2184 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2185 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002186 char_u *namep;
2187
2188 // skip a "<SNR>99_" prefix
2189 namep = untrans_function_name(name);
2190 if (namep == NULL)
2191 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002192
2193 // An exported function in an autoload script is stored as
2194 // "dir#path#name".
2195 if (len < sizeof(buffer))
2196 auto_name = buffer;
2197 else
2198 auto_name = alloc(len);
2199 if (auto_name != NULL)
2200 {
2201 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002202 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002203 hi = hash_find(&func_hashtab, auto_name);
2204 if (auto_name != buffer)
2205 vim_free(auto_name);
2206 if (!HASHITEM_EMPTY(hi))
2207 return HI2UF(hi);
2208 }
2209 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210
2211 return NULL;
2212}
2213
2214/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002215 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002216 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2217 * functions.
2218 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002219 * Return NULL for unknown function.
2220 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002221 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002222find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002223{
2224 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002225 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002226
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002227 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002229 // Find script-local function before global one.
2230 if (in_vim9script() && eval_isnamec1(*name)
2231 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002232 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002233 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2234 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002235 if (func != NULL)
2236 return func;
2237 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002238 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2239 {
2240 char_u *p = name + 5;
2241 long sid;
2242
2243 // printable "<SNR>123_Name" form
2244 sid = getdigits(&p);
2245 if (*p == '_')
2246 {
2247 func = find_func_with_sid(p + 1, (int)sid);
2248 if (func != NULL)
2249 return func;
2250 }
2251 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002253
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002254 if ((flags & FFED_NO_GLOBAL) == 0)
2255 {
2256 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002257 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002258 if (!HASHITEM_EMPTY(hi))
2259 return HI2UF(hi);
2260 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002261
Bram Moolenaarb8822442022-01-11 15:24:05 +00002262 // Find autoload function if this is an autoload script.
2263 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2264 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002265}
2266
2267/*
2268 * Find a function by name, return pointer to it in ufuncs.
2269 * "cctx" is passed in a :def function to find imported functions.
2270 * Return NULL for unknown or dead function.
2271 */
2272 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002273find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002275 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276
2277 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2278 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002279 return NULL;
2280}
2281
2282/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002283 * Return TRUE if "ufunc" is a global function.
2284 */
2285 int
2286func_is_global(ufunc_T *ufunc)
2287{
2288 return ufunc->uf_name[0] != K_SPECIAL;
2289}
2290
2291/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002292 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2293 */
2294 int
2295func_requires_g_prefix(ufunc_T *ufunc)
2296{
2297 return ufunc->uf_name[0] != K_SPECIAL
2298 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002299 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01002300 && !SAFE_isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002301}
2302
2303/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002304 * Copy the function name of "fp" to buffer "buf".
2305 * "buf" must be able to hold the function name plus three bytes.
2306 * Takes care of script-local function names.
2307 */
2308 static void
2309cat_func_name(char_u *buf, ufunc_T *fp)
2310{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002311 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002312 {
2313 STRCPY(buf, "<SNR>");
2314 STRCAT(buf, fp->uf_name + 3);
2315 }
2316 else
2317 STRCPY(buf, fp->uf_name);
2318}
2319
2320/*
2321 * Add a number variable "name" to dict "dp" with value "nr".
2322 */
2323 static void
2324add_nr_var(
2325 dict_T *dp,
2326 dictitem_T *v,
2327 char *name,
2328 varnumber_T nr)
2329{
2330 STRCPY(v->di_key, name);
2331 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002332 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002333 v->di_tv.v_type = VAR_NUMBER;
2334 v->di_tv.v_lock = VAR_FIXED;
2335 v->di_tv.vval.v_number = nr;
2336}
2337
2338/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002339 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002340 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002341 static void
2342free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002343{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002344 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002345
Bram Moolenaarca16c602022-09-06 18:57:08 +01002346 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002347 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002348 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002349
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002350 // When garbage collecting a funccall_T may be freed before the
2351 // function that references it, clear its uf_scoped field.
2352 // The function may have been redefined and point to another
2353 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002354 if (fp != NULL && fp->uf_scoped == fc)
2355 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002356 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002357 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002358
Bram Moolenaarca16c602022-09-06 18:57:08 +01002359 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002360 vim_free(fc);
2361}
2362
2363/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002364 * Free "fc" and what it contains.
2365 * Can be called only when "fc" is kept beyond the period of it called,
2366 * i.e. after cleanup_function_call(fc).
2367 */
2368 static void
2369free_funccal_contents(funccall_T *fc)
2370{
2371 listitem_T *li;
2372
2373 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002374 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002375
2376 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002377 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002378
2379 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002380 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002381 clear_tv(&li->li_tv);
2382
2383 free_funccal(fc);
2384}
2385
2386/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002387 * Handle the last part of returning from a function: free the local hashtable.
2388 * Unless it is still in use by a closure.
2389 */
2390 static void
2391cleanup_function_call(funccall_T *fc)
2392{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002393 int may_free_fc = fc->fc_refcount <= 0;
2394 int free_fc = TRUE;
2395
Bram Moolenaarca16c602022-09-06 18:57:08 +01002396 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002397
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002398 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002399 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2400 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002401 else
2402 free_fc = FALSE;
2403
2404 // If the a:000 list and the l: and a: dicts are not referenced and
2405 // there is no closure using it, we can free the funccall_T and what's
2406 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002407 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2408 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002409 else
2410 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002411 int todo;
2412 hashitem_T *hi;
2413 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002414
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002415 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002416
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002417 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002418 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002419 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002420 {
2421 if (!HASHITEM_EMPTY(hi))
2422 {
2423 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002424 di = HI2DI(hi);
2425 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002426 }
2427 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002428 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002429
Bram Moolenaarca16c602022-09-06 18:57:08 +01002430 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2431 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002432 else
2433 {
2434 listitem_T *li;
2435
2436 free_fc = FALSE;
2437
2438 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002439 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002440 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002441 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002442
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002443 if (free_fc)
2444 free_funccal(fc);
2445 else
2446 {
2447 static int made_copy = 0;
2448
2449 // "fc" is still in use. This can happen when returning "a:000",
2450 // assigning "l:" to a global variable or defining a closure.
2451 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002452 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002453 previous_funccal = fc;
2454
2455 if (want_garbage_collect)
2456 // If garbage collector is ready, clear count.
2457 made_copy = 0;
2458 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002459 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002460 // We have made a lot of copies, worth 4 Mbyte. This can happen
2461 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002462 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002463 // too much memory.
2464 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002465 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002466 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002467 }
2468}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002469
2470/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002471 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2472 */
2473 static int
2474numbered_function(char_u *name)
2475{
Keith Thompson184f71c2024-01-04 21:19:04 +01002476 return SAFE_isdigit(*name)
2477 || (name[0] == 'g' && name[1] == ':' && SAFE_isdigit(name[2]));
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002478}
2479
2480/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002481 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002482 * 1. ordinary names, function defined with :function or :def;
2483 * can start with "<SNR>123_" literally or with K_SPECIAL.
2484 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002485 * For the first we only count the name stored in func_hashtab as a reference,
2486 * using function() does not count as a reference, because the function is
2487 * looked up by name.
2488 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002489 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002490func_name_refcount(char_u *name)
2491{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002492 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002493}
2494
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495/*
2496 * Unreference "fc": decrement the reference count and free it when it
2497 * becomes zero. "fp" is detached from "fc".
2498 * When "force" is TRUE we are exiting.
2499 */
2500 static void
2501funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2502{
2503 funccall_T **pfc;
2504 int i;
2505
2506 if (fc == NULL)
2507 return;
2508
2509 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002510 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2511 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2512 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2513 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 {
2515 if (fc == *pfc)
2516 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002517 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 free_funccal_contents(fc);
2519 return;
2520 }
2521 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002522 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2523 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2524 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525}
2526
2527/*
2528 * Remove the function from the function hashtable. If the function was
2529 * deleted while it still has references this was already done.
2530 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2531 */
2532 static int
2533func_remove(ufunc_T *fp)
2534{
2535 hashitem_T *hi;
2536
2537 // Return if it was already virtually deleted.
2538 if (fp->uf_flags & FC_DEAD)
2539 return FALSE;
2540
2541 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002542 if (HASHITEM_EMPTY(hi))
2543 return FALSE;
2544
2545 // When there is a def-function index do not actually remove the
2546 // function, so we can find the index when defining the function again.
2547 // Do remove it when it's a copy.
2548 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002550 fp->uf_flags |= FC_DEAD;
2551 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002553 hash_remove(&func_hashtab, hi, "remove function");
2554 fp->uf_flags |= FC_DELETED;
2555 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002556}
2557
2558 static void
2559func_clear_items(ufunc_T *fp)
2560{
2561 ga_clear_strings(&(fp->uf_args));
2562 ga_clear_strings(&(fp->uf_def_args));
2563 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002565 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002566 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01002567 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002568
2569 // Increment the refcount of this function to avoid it being freed
2570 // recursively when the partial is freed.
2571 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002572 partial_unref(fp->uf_partial);
2573 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002574 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002575
2576#ifdef FEAT_LUA
2577 if (fp->uf_cb_free != NULL)
2578 {
2579 fp->uf_cb_free(fp->uf_cb_state);
2580 fp->uf_cb_free = NULL;
2581 }
2582
2583 fp->uf_cb_state = NULL;
2584 fp->uf_cb = NULL;
2585#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586#ifdef FEAT_PROFILE
2587 VIM_CLEAR(fp->uf_tml_count);
2588 VIM_CLEAR(fp->uf_tml_total);
2589 VIM_CLEAR(fp->uf_tml_self);
2590#endif
2591}
2592
2593/*
2594 * Free all things that a function contains. Does not free the function
2595 * itself, use func_free() for that.
2596 * When "force" is TRUE we are exiting.
2597 */
2598 static void
2599func_clear(ufunc_T *fp, int force)
2600{
2601 if (fp->uf_cleared)
2602 return;
2603 fp->uf_cleared = TRUE;
2604
2605 // clear this function
2606 func_clear_items(fp);
2607 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002608 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609}
2610
2611/*
2612 * Free a function and remove it from the list of functions. Does not free
2613 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002614 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002615 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002617 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002618func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002619{
2620 // Only remove it when not done already, otherwise we would remove a newer
2621 // version of the function with the same name.
2622 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2623 func_remove(fp);
2624
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002625 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002626 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002627 if (fp->uf_dfunc_idx > 0)
2628 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002629 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002631 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002632 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002633 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634}
2635
2636/*
2637 * Free all things that a function contains and free the function itself.
2638 * When "force" is TRUE we are exiting.
2639 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002640 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002641func_clear_free(ufunc_T *fp, int force)
2642{
2643 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002644 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2645 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002646 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002647 else
2648 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649}
2650
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002651/*
2652 * Copy already defined function "lambda" to a new function with name "global".
2653 * This is for when a compiled function defines a global function.
2654 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002655 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002656copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002657 char_u *lambda,
2658 char_u *global,
2659 loopvarinfo_T *loopvarinfo,
2660 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002661{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002662 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002663 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002664
2665 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002666 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002667 semsg(_(e_lambda_function_not_found_str), lambda);
2668 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002669 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002670
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002671 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002672 if (fp != NULL)
2673 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002674 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002675 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002676 return FAIL;
2677 }
2678
Bram Moolenaare01e5212023-01-08 20:31:18 +00002679 fp = alloc_ufunc(global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002680 if (fp == NULL)
2681 return FAIL;
2682
2683 fp->uf_varargs = ufunc->uf_varargs;
2684 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2685 fp->uf_def_status = ufunc->uf_def_status;
2686 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2687 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2688 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2689 == FAIL
2690 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2691 goto failed;
2692
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002693 if (ufunc->uf_arg_types != NULL)
2694 {
2695 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2696 if (fp->uf_arg_types == NULL)
2697 goto failed;
2698 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2699 sizeof(type_T *) * fp->uf_args.ga_len);
2700 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002701 if (ufunc->uf_va_name != NULL)
2702 {
2703 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2704 if (fp->uf_va_name == NULL)
2705 goto failed;
2706 }
2707 fp->uf_ret_type = ufunc->uf_ret_type;
2708
2709 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002710
2711 fp->uf_name_exp = NULL;
2712 set_ufunc_name(fp, global);
2713
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002714 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002715
2716 // the referenced dfunc_T is now used one more time
2717 link_def_function(fp);
2718
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002719 // Create a partial to store the context of the function where it was
2720 // instantiated. Only needs to be done once. Do this on the original
2721 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002722 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2723 {
2724 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2725
2726 if (pt == NULL)
2727 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002728 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002729 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002730 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002731 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002732 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002733 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002734 }
2735
2736 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002737
2738failed:
2739 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002740 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002741}
2742
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002743static int funcdepth = 0;
2744
2745/*
2746 * Increment the function call depth count.
2747 * Return FAIL when going over 'maxfuncdepth'.
2748 * Otherwise return OK, must call funcdepth_decrement() later!
2749 */
2750 int
2751funcdepth_increment(void)
2752{
2753 if (funcdepth >= p_mfd)
2754 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002755 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002756 return FAIL;
2757 }
2758 ++funcdepth;
2759 return OK;
2760}
2761
2762 void
2763funcdepth_decrement(void)
2764{
2765 --funcdepth;
2766}
2767
2768/*
2769 * Get the current function call depth.
2770 */
2771 int
2772funcdepth_get(void)
2773{
2774 return funcdepth;
2775}
2776
2777/*
2778 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002779 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002780 * times as funcdepth_increment().
2781 */
2782 void
2783funcdepth_restore(int depth)
2784{
2785 funcdepth = depth;
2786}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002787
2788/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002789 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2790 * "rettv".
2791 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2792 * Returns NULL when allocation fails.
2793 */
2794 funccall_T *
2795create_funccal(ufunc_T *fp, typval_T *rettv)
2796{
2797 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2798
2799 if (fc == NULL)
2800 return NULL;
2801 fc->fc_caller = current_funccal;
2802 current_funccal = fc;
2803 fc->fc_func = fp;
2804 func_ptr_ref(fp);
2805 fc->fc_rettv = rettv;
2806 return fc;
2807}
2808
2809/*
2810 * To be called when returning from a compiled function; restores
2811 * current_funccal.
2812 */
2813 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002814remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002815{
2816 funccall_T *fc = current_funccal;
2817
2818 current_funccal = fc->fc_caller;
2819 free_funccal(fc);
2820}
2821
2822/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002823 * Call a user function.
2824 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002825 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002826call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002827 ufunc_T *fp, // pointer to function
2828 int argcount, // nr of args
2829 typval_T *argvars, // arguments
2830 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002831 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002832 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002833{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002834 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002835 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002836 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002837 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002838 funccall_T *fc;
2839 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002840 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002841 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002842 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002843 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002844 int i;
2845 int ai;
2846 int islambda = FALSE;
2847 char_u numbuf[NUMBUFLEN];
2848 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002849 typval_T *tv_to_free[MAX_FUNC_ARGS];
2850 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002851#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002852 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002853#endif
ichizok7e5fe382023-04-15 13:17:50 +01002854 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002855
Bram Moolenaarb2049902021-01-24 12:53:53 +01002856#ifdef FEAT_PROFILE
2857 CLEAR_FIELD(profile_info);
2858#endif
2859
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002860 // If depth of calling is getting too high, don't execute the function.
2861 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002862 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002863 rettv->v_type = VAR_NUMBER;
2864 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002865 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002866 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002867
Bram Moolenaare38eab22019-12-05 21:50:01 +01002868 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002869
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002870 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002871 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00002872 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002873 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002874 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002875 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2876 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002877 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002878 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002879
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002880 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002882#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002883 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002884#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002885 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002886#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002887 if (do_profiling == PROF_YES)
2888 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002889#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002890 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002891 if (call_def_function(fp, argcount, argvars, 0,
2892 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
2893 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002894 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002895#ifdef FEAT_PROFILE
2896 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002897 || (caller != NULL && caller->uf_profiling)))
2898 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002899#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002900 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002901 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002902 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903 }
2904
Bram Moolenaar38453522021-11-28 22:00:12 +00002905 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002906
2907 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002908 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2909 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2910 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002911 */
2912 /*
2913 * Init l: variables.
2914 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002915 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002916 if (selfdict != NULL)
2917 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002918 // Set l:self to "selfdict". Use "name" to avoid a warning from
2919 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002920 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002921 name = v->di_key;
2922 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002923 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002924 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002925 v->di_tv.v_type = VAR_DICT;
2926 v->di_tv.v_lock = 0;
2927 v->di_tv.vval.v_dict = selfdict;
2928 ++selfdict->dv_refcount;
2929 }
2930
2931 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002932 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002933 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002934 * Set a:000 to a list with room for the "..." arguments.
2935 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002936 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002937 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002938 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002939 (varnumber_T)(argcount >= fp->uf_args.ga_len
2940 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002941 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002942 if ((fp->uf_flags & FC_NOARGS) == 0)
2943 {
2944 // Use "name" to avoid a warning from some compiler that checks the
2945 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002946 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002947 name = v->di_key;
2948 STRCPY(name, "000");
2949 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002950 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002951 v->di_tv.v_type = VAR_LIST;
2952 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002953 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002954 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002955 CLEAR_FIELD(fc->fc_l_varlist);
2956 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2957 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002958
2959 /*
2960 * Set a:firstline to "firstline" and a:lastline to "lastline".
2961 * Set a:name to named arguments.
2962 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002963 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002964 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002965 if ((fp->uf_flags & FC_NOARGS) == 0)
2966 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002967 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2968 "firstline", (varnumber_T)funcexe->fe_firstline);
2969 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2970 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002971 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002972 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002973 {
2974 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002975 typval_T def_rettv;
2976 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002977
2978 ai = i - fp->uf_args.ga_len;
2979 if (ai < 0)
2980 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002981 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002982 name = FUNCARG(fp, i);
2983 if (islambda)
2984 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002985
2986 // evaluate named argument default expression
2987 isdefault = ai + fp->uf_def_args.ga_len >= 0
2988 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2989 && argvars[i].vval.v_number == VVAL_NONE));
2990 if (isdefault)
2991 {
2992 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002993
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002994 def_rettv.v_type = VAR_NUMBER;
2995 def_rettv.vval.v_number = -1;
2996
2997 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2998 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002999 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003000 {
3001 default_arg_err = 1;
3002 break;
3003 }
3004 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003005 }
3006 else
3007 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003008 if ((fp->uf_flags & FC_NOARGS) != 0)
3009 // Bail out if no a: arguments used (in lambda).
3010 break;
3011
Bram Moolenaare38eab22019-12-05 21:50:01 +01003012 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003013 sprintf((char *)numbuf, "%d", ai + 1);
3014 name = numbuf;
3015 }
3016 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
3017 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003018 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003019 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003020 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003021 }
3022 else
3023 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003024 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003025 if (v == NULL)
3026 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003027 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003028 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003029
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003030 // Note: the values are copied directly to avoid alloc/free.
3031 // "argvars" must have VAR_FIXED for v_lock.
3032 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003033 v->di_tv.v_lock = VAR_FIXED;
3034
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003035 if (isdefault)
3036 // Need to free this later, no matter where it's stored.
3037 tv_to_free[tv_to_free_len++] = &v->di_tv;
3038
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003039 if (addlocal)
3040 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003041 // Named arguments should be accessed without the "a:" prefix in
3042 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003043 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003044 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003045 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003046 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003047 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003048
3049 if (ai >= 0 && ai < MAX_FUNC_ARGS)
3050 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003051 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003052
3053 li->li_tv = argvars[i];
3054 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003055 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003056 }
3057 }
3058
Bram Moolenaare38eab22019-12-05 21:50:01 +01003059 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003060 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003061
3062 if (fp->uf_flags & FC_SANDBOX)
3063 {
3064 using_sandbox = TRUE;
3065 ++sandbox;
3066 }
3067
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003068 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003069 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003070 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003071 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003072 ++no_wait_return;
3073 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003074
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003075 smsg(_("calling %s"), SOURCING_NAME);
3076 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003077 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003078 char_u buf[MSG_BUF_LEN];
3079 char_u numbuf2[NUMBUFLEN];
3080 char_u *tofree;
3081 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003082
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003083 msg_puts("(");
3084 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003085 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003086 if (i > 0)
3087 msg_puts(", ");
3088 if (argvars[i].v_type == VAR_NUMBER)
3089 msg_outnum((long)argvars[i].vval.v_number);
3090 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003091 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003092 // Do not want errors such as E724 here.
3093 ++emsg_off;
3094 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3095 --emsg_off;
3096 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003097 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003098 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003099 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003100 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3101 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003102 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003103 msg_puts((char *)s);
3104 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003105 }
3106 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003107 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003108 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003109 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003110 msg_puts("\n"); // don't overwrite this either
3111
3112 verbose_leave_scroll();
3113 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003114 }
3115#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003116 if (do_profiling == PROF_YES)
3117 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003118 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003119#endif
3120
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003121 // "legacy" does not apply to commands in the function
3122 sticky_cmdmod_flags = 0;
3123
Bram Moolenaar98aff652022-09-06 21:02:35 +01003124 // If called from a compiled :def function the execution context must be
3125 // hidden, any deferred functions need to be added to the function being
3126 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003127 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003128
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003129 save_current_sctx = current_sctx;
3130 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003131 save_did_emsg = did_emsg;
3132 did_emsg = FALSE;
3133
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003134 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003135 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003136 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003137 retval = FCERR_FAILED;
3138 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003139 else if (islambda)
3140 {
3141 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3142
3143 // A Lambda always has the command "return {expr}". It is much faster
3144 // to evaluate {expr} directly.
3145 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003146 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003147 --ex_nesting_level;
3148 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003149 else
3150 // call do_cmdline() to execute the lines
3151 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003152 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3153
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003154 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003155 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003156
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003157 if (RedrawingDisabled > 0)
3158 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003159
Bram Moolenaare38eab22019-12-05 21:50:01 +01003160 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003161 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3162 {
3163 clear_tv(rettv);
3164 rettv->v_type = VAR_NUMBER;
3165 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003166
3167 // In corner cases returning a "failed" value is not backwards
3168 // compatible. Only do this for Vim9 script.
3169 if (in_vim9script())
3170 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003171 }
3172
3173#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003174 if (do_profiling == PROF_YES)
3175 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003176 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003177
3178 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3179 profile_may_end_func(&profile_info, fp, caller);
3180 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003181#endif
3182
Bram Moolenaare38eab22019-12-05 21:50:01 +01003183 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003184 if (p_verbose >= 12)
3185 {
3186 ++no_wait_return;
3187 verbose_enter_scroll();
3188
3189 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003190 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003191 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003192 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003193 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003194 else
3195 {
3196 char_u buf[MSG_BUF_LEN];
3197 char_u numbuf2[NUMBUFLEN];
3198 char_u *tofree;
3199 char_u *s;
3200
Bram Moolenaare38eab22019-12-05 21:50:01 +01003201 // The value may be very long. Skip the middle part, so that we
3202 // have some idea how it starts and ends. smsg() would always
3203 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003204 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003205 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003206 --emsg_off;
3207 if (s != NULL)
3208 {
3209 if (vim_strsize(s) > MSG_BUF_CLEN)
3210 {
3211 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3212 s = buf;
3213 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003214 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003215 vim_free(tofree);
3216 }
3217 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003218 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003219
3220 verbose_leave_scroll();
3221 --no_wait_return;
3222 }
3223
ichizok7e5fe382023-04-15 13:17:50 +01003224 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003225 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003226 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003227 restore_current_ectx(save_current_ectx);
3228
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003229#ifdef FEAT_PROFILE
3230 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003231 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003232#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003233 if (using_sandbox)
3234 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003235 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003236
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003237 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003238 {
3239 ++no_wait_return;
3240 verbose_enter_scroll();
3241
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003242 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003243 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003244
3245 verbose_leave_scroll();
3246 --no_wait_return;
3247 }
3248
3249 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003250 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003251 for (i = 0; i < tv_to_free_len; ++i)
3252 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003253 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003254
3255 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003256}
3257
3258/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003259 * Check the argument count for user function "fp".
3260 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3261 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003262 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003263check_user_func_argcount(ufunc_T *fp, int argcount)
3264{
3265 int regular_args = fp->uf_args.ga_len;
3266
3267 if (argcount < regular_args - fp->uf_def_args.ga_len)
3268 return FCERR_TOOFEW;
3269 else if (!has_varargs(fp) && argcount > regular_args)
3270 return FCERR_TOOMANY;
3271 return FCERR_UNKNOWN;
3272}
3273
3274/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003276 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003277 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278call_user_func_check(
3279 ufunc_T *fp,
3280 int argcount,
3281 typval_T *argvars,
3282 typval_T *rettv,
3283 funcexe_T *funcexe,
3284 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003285{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003286 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003287
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003288#ifdef FEAT_LUA
3289 if (fp->uf_flags & FC_CFUNC)
3290 {
3291 cfunc_T cb = fp->uf_cb;
3292
3293 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3294 }
3295#endif
3296
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003297 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3298 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003299 error = check_user_func_argcount(fp, argcount);
3300 if (error != FCERR_UNKNOWN)
3301 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003302
Bram Moolenaar50824712020-12-20 21:10:17 +01003303 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003304 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003306 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003308 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 int did_save_redo = FALSE;
3310 save_redo_T save_redo;
3311
3312 /*
3313 * Call the user function.
3314 * Save and restore search patterns, script variables and
3315 * redo buffer.
3316 */
3317 save_search_patterns();
3318 if (!ins_compl_active())
3319 {
3320 saveRedobuff(&save_redo);
3321 did_save_redo = TRUE;
3322 }
3323 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003324 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003325 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3327 // Function was unreferenced while being used, free it now.
3328 func_clear_free(fp, FALSE);
3329 if (did_save_redo)
3330 restoreRedobuff(&save_redo);
3331 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003332 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003333
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003335}
3336
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003337static funccal_entry_T *funccal_stack = NULL;
3338
3339/*
3340 * Save the current function call pointer, and set it to NULL.
3341 * Used when executing autocommands and for ":source".
3342 */
3343 void
3344save_funccal(funccal_entry_T *entry)
3345{
3346 entry->top_funccal = current_funccal;
3347 entry->next = funccal_stack;
3348 funccal_stack = entry;
3349 current_funccal = NULL;
3350}
3351
3352 void
3353restore_funccal(void)
3354{
3355 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003356 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003357 else
3358 {
3359 current_funccal = funccal_stack->top_funccal;
3360 funccal_stack = funccal_stack->next;
3361 }
3362}
3363
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003364 funccall_T *
3365get_current_funccal(void)
3366{
3367 return current_funccal;
3368}
3369
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003370/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003371 * Return TRUE when currently at the script level:
3372 * - not in a function
3373 * - not executing an autocommand
3374 * Note that when an autocommand sources a script the result is FALSE;
3375 */
3376 int
3377at_script_level(void)
3378{
3379 return current_funccal == NULL && autocmd_match == NULL;
3380}
3381
3382/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003383 * Mark all functions of script "sid" as deleted.
3384 */
3385 void
3386delete_script_functions(int sid)
3387{
3388 hashitem_T *hi;
3389 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003390 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003391 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003392 size_t len;
3393
3394 buf[0] = K_SPECIAL;
3395 buf[1] = KS_EXTRA;
3396 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003397 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003398 len = STRLEN(buf);
3399
Bram Moolenaarce658352020-07-31 23:47:12 +02003400 while (todo > 0)
3401 {
3402 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003403 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003404 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003405 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003406 fp = HI2UF(hi);
3407 if (STRNCMP(fp->uf_name, buf, len) == 0)
3408 {
3409 int changed = func_hashtab.ht_changed;
3410
3411 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003412
3413 if (fp->uf_calls > 0)
3414 {
3415 // Function is executing, don't free it but do remove
3416 // it from the hashtable.
3417 if (func_remove(fp))
3418 fp->uf_refcount--;
3419 }
3420 else
3421 {
3422 func_clear(fp, TRUE);
3423 // When clearing a function another function can be
3424 // cleared as a side effect. When that happens start
3425 // over.
3426 if (changed != func_hashtab.ht_changed)
3427 break;
3428 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003429 }
3430 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003431 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003432 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003433}
3434
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003435#if defined(EXITFREE) || defined(PROTO)
3436 void
3437free_all_functions(void)
3438{
3439 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003440 ufunc_T *fp;
3441 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003442 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003443 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003444
Bram Moolenaare38eab22019-12-05 21:50:01 +01003445 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003446 while (current_funccal != NULL)
3447 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003448 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003449 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003450 if (current_funccal == NULL && funccal_stack != NULL)
3451 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003452 }
3453
Bram Moolenaare38eab22019-12-05 21:50:01 +01003454 // First clear what the functions contain. Since this may lower the
3455 // reference count of a function, it may also free a function and change
3456 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003457 while (todo > 0)
3458 {
3459 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003460 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003461 if (!HASHITEM_EMPTY(hi))
3462 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 // clear the def function index now
3464 fp = HI2UF(hi);
3465 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003466 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467
Bram Moolenaare38eab22019-12-05 21:50:01 +01003468 // Only free functions that are not refcounted, those are
3469 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003470 if (func_name_refcount(fp->uf_name))
3471 ++skipped;
3472 else
3473 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003474 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003475 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003476 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003477 {
3478 skipped = 0;
3479 break;
3480 }
3481 }
3482 --todo;
3483 }
3484 }
3485
Bram Moolenaare38eab22019-12-05 21:50:01 +01003486 // Now actually free the functions. Need to start all over every time,
3487 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003488 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003489 while (func_hashtab.ht_used > skipped)
3490 {
3491 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003492 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003493 if (!HASHITEM_EMPTY(hi))
3494 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003495 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003496 // Only free functions that are not refcounted, those are
3497 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003498 fp = HI2UF(hi);
3499 if (func_name_refcount(fp->uf_name))
3500 ++skipped;
3501 else
3502 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003503 if (func_free(fp, FALSE) == OK)
3504 {
3505 skipped = 0;
3506 break;
3507 }
3508 // did not actually free it
3509 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003510 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003511 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003512 }
3513 if (skipped == 0)
3514 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515
3516 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003517}
3518#endif
3519
3520/*
3521 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003522 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3523 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003524 * "len" is the length of "name", or -1 for NUL terminated.
3525 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003527builtin_function(char_u *name, int len)
3528{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003529 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003530
Bram Moolenaara26b9702020-04-18 19:53:28 +02003531 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003532 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003533 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3534 {
3535 if (name[i] == AUTOLOAD_CHAR)
3536 return FALSE;
3537 if (!eval_isnamec(name[i]))
3538 {
3539 // "name.something" is not a builtin function
3540 if (name[i] == '.')
3541 return FALSE;
3542 break;
3543 }
3544 }
3545 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003546}
3547
3548 int
3549func_call(
3550 char_u *name,
3551 typval_T *args,
3552 partial_T *partial,
3553 dict_T *selfdict,
3554 typval_T *rettv)
3555{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003556 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003557 listitem_T *item;
3558 typval_T argv[MAX_FUNC_ARGS + 1];
3559 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003560 int r = 0;
3561
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003562 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003563 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003564 {
3565 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3566 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003567 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003568 break;
3569 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003570 // Make a copy of each argument. This is needed to be able to set
3571 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003572 copy_tv(&item->li_tv, &argv[argc++]);
3573 }
3574
3575 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003576 {
3577 funcexe_T funcexe;
3578
Bram Moolenaara80faa82020-04-12 19:37:17 +02003579 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003580 funcexe.fe_firstline = curwin->w_cursor.lnum;
3581 funcexe.fe_lastline = curwin->w_cursor.lnum;
3582 funcexe.fe_evaluate = TRUE;
3583 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003584 if (partial != NULL)
3585 {
3586 funcexe.fe_object = partial->pt_obj;
3587 if (funcexe.fe_object != NULL)
3588 ++funcexe.fe_object->obj_refcount;
3589 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003590 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003591 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3592 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003593
Bram Moolenaare38eab22019-12-05 21:50:01 +01003594 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003595 while (argc > 0)
3596 clear_tv(&argv[--argc]);
3597
3598 return r;
3599}
3600
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003601static int callback_depth = 0;
3602
3603 int
3604get_callback_depth(void)
3605{
3606 return callback_depth;
3607}
3608
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003609/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003610 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003611 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003612 */
3613 int
3614call_callback(
3615 callback_T *callback,
3616 int len, // length of "name" or -1 to use strlen()
3617 typval_T *rettv, // return value goes here
3618 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003619 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003620 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003621{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003622 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003623 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003624
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003625 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3626 return FAIL;
Christian Brabandt47510f32023-10-15 09:56:16 +02003627
zeertzjqfe583b12023-12-21 16:59:26 +01003628 if (callback_depth > p_mfd)
Christian Brabandt47510f32023-10-15 09:56:16 +02003629 {
3630 emsg(_(e_command_too_recursive));
3631 return FAIL;
3632 }
3633
Bram Moolenaara80faa82020-04-12 19:37:17 +02003634 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003635 funcexe.fe_evaluate = TRUE;
3636 funcexe.fe_partial = callback->cb_partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003637 if (callback->cb_partial != NULL)
3638 {
3639 funcexe.fe_object = callback->cb_partial->pt_obj;
3640 if (funcexe.fe_object != NULL)
3641 ++funcexe.fe_object->obj_refcount;
3642 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003643 ++callback_depth;
3644 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3645 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003646
3647 // When a :def function was called that uses :try an error would be turned
3648 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003649 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003650 {
3651 need_rethrow = FALSE;
3652 handle_did_throw();
3653 }
3654
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003655 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003656}
3657
3658/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003659 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003660 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003661 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3662 */
3663 varnumber_T
3664call_callback_retnr(
3665 callback_T *callback,
3666 int argcount, // number of "argvars"
3667 typval_T *argvars) // vars for arguments, must have "argcount"
3668 // PLUS ONE elements!
3669{
3670 typval_T rettv;
3671 varnumber_T retval;
3672
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003673 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003674 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003675
3676 retval = tv_get_number_chk(&rettv, NULL);
3677 clear_tv(&rettv);
3678 return retval;
3679}
3680
3681/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 * Give an error message for the result of a function.
3683 * Nothing if "error" is FCERR_NONE.
3684 */
3685 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003686user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687{
3688 switch (error)
3689 {
3690 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003691 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003692 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003693 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003694 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 break;
3696 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003697 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 break;
3699 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003700 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 break;
3702 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003703 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 break;
3705 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003706 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 break;
3708 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003709 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 break;
3711 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003712 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3713 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003715 case FCERR_OTHER:
3716 case FCERR_FAILED:
3717 // assume the error message was already given
3718 break;
3719 case FCERR_NONE:
3720 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 }
3722}
3723
3724/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003725 * Check the argument types "argvars[argcount]" for "name" using the
3726 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3727 * is already included in "argvars[]".
3728 * Will do nothing if "funcexe->fe_check_type" is NULL or
3729 * "funcexe->fe_evaluate" is FALSE;
3730 * Returns an FCERR_ value.
3731 */
3732 static funcerror_T
3733may_check_argument_types(
3734 funcexe_T *funcexe,
3735 typval_T *argvars,
3736 int argcount,
3737 int base_included,
3738 char_u *name)
3739{
3740 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3741 {
3742 // Check that the argument types are OK for the types of the funcref.
3743 if (check_argument_types(funcexe->fe_check_type,
3744 argvars, argcount,
3745 base_included ? NULL : funcexe->fe_basetv,
3746 name) == FAIL)
3747 return FCERR_OTHER;
3748 }
3749 return FCERR_NONE;
3750}
3751
3752/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003753 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003754 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003755 * Return FAIL when the function can't be called, OK otherwise.
3756 * Also returns OK when an error was encountered while executing the function.
3757 */
3758 int
3759call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003760 char_u *funcname, // name of the function
3761 int len, // length of "name" or -1 to use strlen()
3762 typval_T *rettv, // return value goes here
3763 int argcount_in, // number of "argvars"
3764 typval_T *argvars_in, // vars for arguments, must have "argcount"
3765 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003766 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003767{
3768 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003769 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003770 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003771 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003772 char_u fname_buf[FLEN_FIXED + 1];
3773 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003774 char_u *fname = NULL;
3775 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003776 int argcount = argcount_in;
3777 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003778 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003779 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003780 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003781 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003782 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003783 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003784 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003785 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003786
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003787 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3788 // even when call_func() returns FAIL.
3789 rettv->v_type = VAR_UNKNOWN;
3790
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003791 if (partial != NULL)
3792 fp = partial->pt_func;
3793 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003794 fp = funcexe->fe_ufunc;
3795
3796 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003797 {
3798 // Make a copy of the name, if it comes from a funcref variable it
3799 // could be changed or deleted in the called function.
3800 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3801 if (name == NULL)
3802 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003803
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003804 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3805 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003806
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003807 if (funcexe->fe_doesrange != NULL)
3808 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003809
3810 if (partial != NULL)
3811 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003812 // When the function has a partial with a dict and there is a dict
3813 // argument, use the dict argument. That is backwards compatible.
3814 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003815 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003816 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003817 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003818 {
3819 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003820 {
3821 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3822 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003823 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003824 goto theend;
3825 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003826 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003827 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003828 for (i = 0; i < argcount_in; ++i)
3829 argv[i + argv_clear] = argvars_in[i];
3830 argvars = argv;
3831 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003832
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003833 if (funcexe->fe_check_type != NULL
3834 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003835 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003836 // Now funcexe->fe_check_type is missing the added arguments,
3837 // make a copy of the type with the correction.
3838 check_type = *funcexe->fe_check_type;
3839 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003840 check_type.tt_args = check_type_args;
3841 CLEAR_FIELD(check_type_args);
3842 for (i = 0; i < check_type.tt_argcount; ++i)
3843 check_type_args[i + partial->pt_argc] =
3844 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003845 check_type.tt_argcount += partial->pt_argc;
3846 check_type.tt_min_argcount += partial->pt_argc;
3847 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003848 }
3849 }
3850
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003851 if (error == FCERR_NONE)
3852 // check the argument types if possible
3853 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
3854 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003855
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003856 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003857 {
3858 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003859 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003860
Bram Moolenaar333894b2020-08-01 18:53:07 +02003861 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003862 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003863 {
3864 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003865 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003866 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003867
Bram Moolenaare38eab22019-12-05 21:50:01 +01003868 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003869 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003870 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003871
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003872 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003873 {
3874 /*
3875 * User defined function.
3876 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003877 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003878 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003879 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003880 if (fp != NULL && !is_global && in_vim9script()
3881 && func_requires_g_prefix(fp))
3882 // In Vim9 script g: is required to find a global
3883 // non-autoload function.
3884 fp = NULL;
3885 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003886
Bram Moolenaare38eab22019-12-05 21:50:01 +01003887 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003888 if (fp == NULL
3889 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003890 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003891 && !aborting())
3892 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003893 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003894 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003895 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003896 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003897 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3898 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003899 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003900 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003901 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003902 if (fp == NULL)
3903 {
3904 char_u *p = untrans_function_name(rfname);
3905
3906 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003907 // Don't do this if the name starts with "s:".
3908 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003909 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003910 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003911
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003912 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003913 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003914 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003915 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003916 int need_arg_check = FALSE;
3917 if (funcexe->fe_check_type == NULL)
3918 {
3919 funcexe->fe_check_type = fp->uf_func_type;
3920 need_arg_check = TRUE;
3921 }
3922
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003923 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003924 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003925 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003926 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003927 argv_clear, fp);
3928 need_arg_check = TRUE;
3929 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003930
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003931 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003932 {
3933 // Method call: base->Method()
3934 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003935 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003936 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003937 argvars = argv;
3938 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003939 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003940 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003941
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003942 // Check the argument types now that the function type and all
3943 // argument values are known, if not done above.
3944 if (need_arg_check)
3945 error = may_check_argument_types(funcexe, argvars, argcount,
3946 TRUE, (name != NULL) ? name : funcname);
3947 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
3948 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003950 }
3951 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003952 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003953 {
3954 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003955 * expr->method(): Find the method name in the table, call its
3956 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003957 */
3958 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003959 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003960 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003961 else
3962 {
3963 /*
3964 * Find the function name in the table, call its implementation.
3965 */
3966 error = call_internal_func(fname, argcount, argvars, rettv);
3967 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003968
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003969 /*
3970 * The function call (or "FuncUndefined" autocommand sequence) might
3971 * have been aborted by an error, an interrupt, or an explicitly thrown
3972 * exception that has not been caught so far. This situation can be
3973 * tested for by calling aborting(). For an error in an internal
3974 * function or for the "E132" error in call_user_func(), however, the
3975 * throw point at which the "force_abort" flag (temporarily reset by
3976 * emsg()) is normally updated has not been reached yet. We need to
3977 * update that flag first to make aborting() reliable.
3978 */
3979 update_force_abort();
3980 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003981 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003982 ret = OK;
3983
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003984theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003985 /*
3986 * Report an error unless the argument evaluation or function call has been
3987 * cancelled due to an aborting error, an interrupt, or an exception.
3988 */
3989 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003990 user_func_error(error, (name != NULL) ? name : funcname,
3991 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003992
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003993 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003994 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003995 clear_tv(&argv[--argv_clear + argv_base]);
3996
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003997 vim_free(tofree);
3998 vim_free(name);
3999
4000 return ret;
4001}
4002
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004003/*
4004 * Call a function without arguments, partial or dict.
4005 * This is like call_func() when the call is only "FuncName()".
4006 * To be used by "expr" options.
4007 * Returns NOTDONE when the function could not be found.
4008 */
4009 int
4010call_simple_func(
4011 char_u *funcname, // name of the function
4012 int len, // length of "name" or -1 to use strlen()
4013 typval_T *rettv) // return value goes here
4014{
4015 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00004016 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004017 char_u fname_buf[FLEN_FIXED + 1];
4018 char_u *tofree = NULL;
4019 char_u *name;
4020 char_u *fname;
4021 char_u *rfname;
4022 int is_global = FALSE;
4023 ufunc_T *fp;
4024
4025 rettv->v_type = VAR_NUMBER; // default rettv is number zero
4026 rettv->vval.v_number = 0;
4027
4028 // Make a copy of the name, an option can be changed in the function.
4029 name = vim_strnsave(funcname, len);
4030 if (name == NULL)
4031 return ret;
4032
4033 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
4034
4035 // Skip "g:" before a function name.
4036 if (fname[0] == 'g' && fname[1] == ':')
4037 {
4038 is_global = TRUE;
4039 rfname = fname + 2;
4040 }
4041 else
4042 rfname = fname;
4043 fp = find_func(rfname, is_global);
4044 if (fp != NULL && !is_global && in_vim9script()
4045 && func_requires_g_prefix(fp))
4046 // In Vim9 script g: is required to find a global non-autoload
4047 // function.
4048 fp = NULL;
4049 if (fp == NULL)
4050 ret = NOTDONE;
4051 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
4052 error = FCERR_DELETED;
4053 else if (fp != NULL)
4054 {
4055 typval_T argvars[1];
4056 funcexe_T funcexe;
4057
4058 argvars[0].v_type = VAR_UNKNOWN;
4059 CLEAR_FIELD(funcexe);
4060 funcexe.fe_evaluate = TRUE;
4061
4062 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
4063 if (error == FCERR_NONE)
4064 ret = OK;
4065 }
4066
4067 user_func_error(error, name, FALSE);
4068 vim_free(tofree);
4069 vim_free(name);
4070
4071 return ret;
4072}
4073
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004074 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075printable_func_name(ufunc_T *fp)
4076{
4077 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4078}
4079
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004080/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004081 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4082 * TRUE. Otherwise return FALSE.
4083 */
4084 static int
4085function_list_modified(int prev_ht_changed)
4086{
4087 if (prev_ht_changed != func_hashtab.ht_changed)
4088 {
4089 emsg(_(e_function_list_was_modified));
4090 return TRUE;
4091 }
4092 return FALSE;
4093}
4094
4095/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004096 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004097 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004098 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004099list_func_head(ufunc_T *fp, int indent)
4100{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004101 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004102 int j;
4103
4104 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004105
4106 // a timer at the more prompt may have deleted the function
4107 if (function_list_modified(prev_ht_changed))
4108 return FAIL;
4109
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004110 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004111 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004112 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004113 msg_puts("def ");
4114 else
4115 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004117 msg_putchar('(');
4118 for (j = 0; j < fp->uf_args.ga_len; ++j)
4119 {
4120 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004121 msg_puts(", ");
4122 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 if (fp->uf_arg_types != NULL)
4124 {
4125 char *tofree;
4126
4127 msg_puts(": ");
4128 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4129 vim_free(tofree);
4130 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004131 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4132 {
4133 msg_puts(" = ");
4134 msg_puts(((char **)(fp->uf_def_args.ga_data))
4135 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4136 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004137 }
4138 if (fp->uf_varargs)
4139 {
4140 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004141 msg_puts(", ");
4142 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004143 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 if (fp->uf_va_name != NULL)
4145 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004146 if (!fp->uf_varargs)
4147 {
4148 if (j)
4149 msg_puts(", ");
4150 msg_puts("...");
4151 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004153 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 {
4155 char *tofree;
4156
4157 msg_puts(": ");
4158 msg_puts(type_name(fp->uf_va_type, &tofree));
4159 vim_free(tofree);
4160 }
4161 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004162 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004163
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004164 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004165 {
4166 if (fp->uf_ret_type != &t_void)
4167 {
4168 char *tofree;
4169
4170 msg_puts(": ");
4171 msg_puts(type_name(fp->uf_ret_type, &tofree));
4172 vim_free(tofree);
4173 }
4174 }
4175 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004176 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004177 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004178 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004179 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004180 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004181 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004182 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004183 msg_clr_eos();
4184 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004185 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004186
4187 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004188}
4189
4190/*
4191 * Get a function name, translating "<SID>" and "<SNR>".
4192 * Also handles a Funcref in a List or Dictionary.
4193 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004194 * Set "*is_global" to TRUE when the function must be global, unless
4195 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004196 * flags:
4197 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004198 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004199 * TFN_QUIET: be quiet
4200 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004201 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004202 * Advances "pp" to just after the function name (if no error).
4203 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004204 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004205trans_function_name(
4206 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004207 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004208 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004209 int flags)
4210{
4211 return trans_function_name_ext(pp, is_global, skip, flags,
4212 NULL, NULL, NULL, NULL);
4213}
4214
4215/*
4216 * trans_function_name() with extra arguments.
4217 * "fdp", "partial", "type" and "ufunc" can be NULL.
4218 */
4219 char_u *
4220trans_function_name_ext(
4221 char_u **pp,
4222 int *is_global,
4223 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004224 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004225 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004226 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004227 type_T **type, // return: type of funcref
4228 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004229{
4230 char_u *name = NULL;
4231 char_u *start;
4232 char_u *end;
4233 int lead;
4234 char_u sid_buf[20];
4235 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004236 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004237 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004238 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004239 int vim9script = in_vim9script();
4240 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004241
4242 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004243 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004244 start = *pp;
4245
Bram Moolenaare38eab22019-12-05 21:50:01 +01004246 // Check for hard coded <SNR>: already translated function ID (from a user
4247 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004248 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4249 && (*pp)[2] == (int)KE_SNR)
4250 {
4251 *pp += 3;
4252 len = get_id_len(pp) + 3;
4253 return vim_strnsave(start, len);
4254 }
4255
Bram Moolenaare38eab22019-12-05 21:50:01 +01004256 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4257 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004258 lead = eval_fname_script(start);
4259 if (lead > 2)
4260 start += lead;
4261
Bram Moolenaare38eab22019-12-05 21:50:01 +01004262 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004263 end = get_lval(start, NULL, &lv, FALSE, skip,
4264 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004265 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004266 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004267 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004268 {
4269 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004270 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004271 goto theend;
4272 }
4273 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4274 {
4275 /*
4276 * Report an invalid expression in braces, unless the expression
4277 * evaluation has been cancelled due to an aborting error, an
4278 * interrupt, or an exception.
4279 */
4280 if (!aborting())
4281 {
4282 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004283 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004284 }
4285 else
4286 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4287 goto theend;
4288 }
4289
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004290 if (lv.ll_ufunc != NULL)
4291 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004292 if (ufunc != NULL)
4293 *ufunc = lv.ll_ufunc;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004294 name = vim_strsave(lv.ll_ufunc->uf_name);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004295 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004296 goto theend;
4297 }
4298
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004299 if (lv.ll_tv != NULL)
4300 {
4301 if (fdp != NULL)
4302 {
4303 fdp->fd_dict = lv.ll_dict;
4304 fdp->fd_newkey = lv.ll_newkey;
4305 lv.ll_newkey = NULL;
4306 fdp->fd_di = lv.ll_di;
4307 }
4308 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4309 {
4310 name = vim_strsave(lv.ll_tv->vval.v_string);
4311 *pp = end;
4312 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004313 else if (lv.ll_tv->v_type == VAR_CLASS
4314 && lv.ll_tv->vval.v_class != NULL)
4315 {
4316 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4317 *pp = end;
4318 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004319 else if (lv.ll_tv->v_type == VAR_PARTIAL
4320 && lv.ll_tv->vval.v_partial != NULL)
4321 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004322 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004323 *pp = end;
4324 if (partial != NULL)
4325 *partial = lv.ll_tv->vval.v_partial;
4326 }
4327 else
4328 {
4329 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4330 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004331 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004332 else
4333 *pp = end;
4334 name = NULL;
4335 }
4336 goto theend;
4337 }
4338
4339 if (lv.ll_name == NULL)
4340 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004341 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004342 *pp = end;
4343 goto theend;
4344 }
4345
Bram Moolenaare38eab22019-12-05 21:50:01 +01004346 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004347 if (lv.ll_exp_name != NULL)
4348 {
4349 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004350 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004351 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004352 if (name == lv.ll_exp_name)
4353 name = NULL;
4354 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004355 else if (lv.ll_sid > 0)
4356 {
4357 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4358 int cc = *lv.ll_name_end;
4359
4360 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4361 *lv.ll_name_end = NUL;
4362 if (si->sn_autoload_prefix != NULL)
4363 {
4364 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4365 }
4366 else
4367 {
4368 sid_buf[0] = K_SPECIAL;
4369 sid_buf[1] = KS_EXTRA;
4370 sid_buf[2] = (int)KE_SNR;
4371 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004372 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004373 name = concat_str(sid_buf, lv.ll_name);
4374 }
4375 *lv.ll_name_end = cc;
4376 *pp = end;
4377 goto theend;
4378 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004379 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004380 {
4381 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004382 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004383 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004384 if (name == *pp)
4385 name = NULL;
4386 }
4387 if (name != NULL)
4388 {
4389 name = vim_strsave(name);
4390 *pp = end;
4391 if (STRNCMP(name, "<SNR>", 5) == 0)
4392 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004393 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004394 name[0] = K_SPECIAL;
4395 name[1] = KS_EXTRA;
4396 name[2] = (int)KE_SNR;
4397 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4398 }
4399 goto theend;
4400 }
4401
4402 if (lv.ll_exp_name != NULL)
4403 {
4404 len = (int)STRLEN(lv.ll_exp_name);
4405 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4406 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4407 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004408 // When there was "s:" already or the name expanded to get a
4409 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004410 lv.ll_name += 2;
4411 len -= 2;
4412 lead = 2;
4413 }
4414 }
4415 else
4416 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004417 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004418 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004419 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004420 if (lv.ll_name[0] == 'g')
4421 {
4422 if (is_global != NULL)
4423 {
4424 *is_global = TRUE;
4425 }
4426 else
4427 {
4428 // dropping "g:" without setting "is_global" won't work in
4429 // Vim9script, put it back later
4430 prefix_g = TRUE;
4431 extra = 2;
4432 }
4433 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004434 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004435 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004436 len = (int)(end - lv.ll_name);
4437 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004438 if (len <= 0)
4439 {
4440 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004441 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004442 goto theend;
4443 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004444
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004445 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004446 // starts with a lower case character: dict.func(). Or when in a class.
4447 vim9_local = ASCII_ISUPPER(*start) && vim9script
4448 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004450 /*
4451 * Copy the function name to allocated memory.
4452 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4453 * Accept <SNR>123_name() outside a script.
4454 */
4455 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004456 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004457 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004458 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004459 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004460 {
Kota Kato948a3892022-08-16 16:09:59 +01004461 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4462 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004463 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004464 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004465 goto theend;
4466 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004467 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004468 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004469 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004471 || eval_fname_sid(*pp))
4472 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004474 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004475 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004476 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004477 goto theend;
4478 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004479 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004480 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 extra = 3 + (int)STRLEN(sid_buf);
4482 else
4483 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004484 }
4485 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004486 // The function name must start with an upper case letter (unless it is a
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004487 // Vim9 class new() function or a Vim9 class private method or one of the
4488 // supported Vim9 object builtin functions)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004489 else if (!(flags & TFN_INT)
4490 && (builtin_function(lv.ll_name, len)
4491 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004492 && !((flags & TFN_IN_CLASS)
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004493 && (is_valid_builtin_obj_methodname(lv.ll_name)
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004494 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004495 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004496 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004497 : e_function_name_must_start_with_capital_or_s_str),
4498 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004499 goto theend;
4500 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004501 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004502 {
4503 char_u *cp = vim_strchr(lv.ll_name, ':');
4504
4505 if (cp != NULL && cp < end)
4506 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004507 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004508 goto theend;
4509 }
4510 }
4511
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004513 if (name != NULL)
4514 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004515 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004516 {
4517 name[0] = K_SPECIAL;
4518 name[1] = KS_EXTRA;
4519 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004520 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004521 STRCPY(name + 3, sid_buf);
4522 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004523 else if (prefix_g)
4524 {
4525 name[0] = 'g';
4526 name[1] = ':';
4527 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004528 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4529 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004530 }
4531 *pp = end;
4532
4533theend:
4534 clear_lval(&lv);
4535 return name;
4536}
4537
4538/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004539 * Assuming "name" is the result of trans_function_name() and it was prefixed
4540 * to use the script-local name, return the unmodified name (points into
4541 * "name"). Otherwise return NULL.
4542 * This can be used to first search for a script-local function and fall back
4543 * to the global function if not found.
4544 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004545 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004546untrans_function_name(char_u *name)
4547{
4548 char_u *p;
4549
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004550 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004551 {
4552 p = vim_strchr(name, '_');
4553 if (p != NULL)
4554 return p + 1;
4555 }
4556 return NULL;
4557}
4558
4559/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004560 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4561 * current script ID and returns the expanded function name. The caller should
4562 * free the returned name. If not called from a script context or the function
4563 * name doesn't start with these prefixes, then returns NULL.
4564 * This doesn't check whether the script-local function exists or not.
4565 */
4566 char_u *
4567get_scriptlocal_funcname(char_u *funcname)
4568{
4569 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004570 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004571 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004572 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004573
4574 if (funcname == NULL)
4575 return NULL;
4576
4577 if (STRNCMP(funcname, "s:", 2) != 0
4578 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004579 {
4580 ufunc_T *ufunc;
4581
4582 // The function name does not have a script-local prefix. Try finding
4583 // it when in a Vim9 script and there is no "g:" prefix.
4584 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4585 return NULL;
4586 ufunc = find_func(funcname, FALSE);
4587 if (ufunc == NULL || func_is_global(ufunc)
4588 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4589 return NULL;
4590 ++p;
4591 off = 0;
4592 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004593 else
4594 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004595
4596 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4597 {
4598 emsg(_(e_using_sid_not_in_script_context));
4599 return NULL;
4600 }
4601 // Expand s: prefix into <SNR>nr_<name>
4602 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4603 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004604 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004605 if (newname == NULL)
4606 return NULL;
4607 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004608 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004609
4610 return newname;
4611}
4612
4613/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004614 * Return script-local "fname" with the 3-byte sequence replaced by
4615 * printable <SNR> in allocated memory.
4616 */
4617 char_u *
4618alloc_printable_func_name(char_u *fname)
4619{
4620 char_u *n = alloc(STRLEN(fname + 3) + 6);
4621
4622 if (n != NULL)
4623 {
4624 STRCPY(n, "<SNR>");
4625 STRCPY(n + 5, fname + 3);
4626 }
4627 return n;
4628}
4629
4630/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004631 * Call trans_function_name(), except that a lambda is returned as-is.
4632 * Returns the name in allocated memory.
4633 */
4634 char_u *
4635save_function_name(
4636 char_u **name,
4637 int *is_global,
4638 int skip,
4639 int flags,
4640 funcdict_T *fudi)
4641{
4642 char_u *p = *name;
4643 char_u *saved;
4644
4645 if (STRNCMP(p, "<lambda>", 8) == 0)
4646 {
4647 p += 8;
4648 (void)getdigits(&p);
4649 saved = vim_strnsave(*name, p - *name);
4650 if (fudi != NULL)
4651 CLEAR_POINTER(fudi);
4652 }
4653 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004654 saved = trans_function_name_ext(&p, is_global, skip,
4655 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004656 *name = p;
4657 return saved;
4658}
4659
4660/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004661 * List functions. When "regmatch" is NULL all of then.
4662 * Otherwise functions matching "regmatch".
4663 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004664 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004665list_functions(regmatch_T *regmatch)
4666{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004667 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004668 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004669 hashitem_T *hi;
4670
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004671 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004672 {
4673 if (!HASHITEM_EMPTY(hi))
4674 {
4675 ufunc_T *fp = HI2UF(hi);
4676
4677 --todo;
4678 if ((fp->uf_flags & FC_DEAD) == 0
4679 && (regmatch == NULL
4680 ? !message_filtered(fp->uf_name)
4681 && !func_name_refcount(fp->uf_name)
Keith Thompson184f71c2024-01-04 21:19:04 +01004682 : !SAFE_isdigit(*fp->uf_name)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004683 && vim_regexec(regmatch, fp->uf_name, 0)))
4684 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004685 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004686 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004687 if (function_list_modified(prev_ht_changed))
4688 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004689 }
4690 }
4691 }
4692}
4693
4694/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004695 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004696 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4697 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004698 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004699 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4700 * With CF_INTERFACE the function is define inside an interface, only the
4701 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004702 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004703 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004704 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004705define_function(
4706 exarg_T *eap,
4707 char_u *name_arg,
4708 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004709 int class_flags,
4710 ocmember_T *obj_members,
4711 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004712{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004713 int j;
4714 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004715 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004716 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004717 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004718 char_u *p;
4719 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004720 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004721 char_u *line_arg = NULL;
4722 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004723 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004724 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004725 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004726 garray_T newlines;
4727 int varargs = FALSE;
4728 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004729 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004730 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004731 int fp_allocated = FALSE;
4732 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004733 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004734 dictitem_T *v;
4735 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004736 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004737 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004738 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004739 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004740 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004741 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004742
4743 /*
4744 * ":function" without argument: list functions.
4745 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004746 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004747 {
4748 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004749 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004750 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004751 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004752 }
4753
4754 /*
4755 * ":function /pat": list functions matching pattern.
4756 */
4757 if (*eap->arg == '/')
4758 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004759 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004760 if (!eap->skip)
4761 {
4762 regmatch_T regmatch;
4763
4764 c = *p;
4765 *p = NUL;
4766 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4767 *p = c;
4768 if (regmatch.regprog != NULL)
4769 {
4770 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004771 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004772 vim_regfree(regmatch.regprog);
4773 }
4774 }
4775 if (*p == '/')
4776 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004777 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004778 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004779 }
4780
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004781 ga_init(&newargs);
4782 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004783 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004784 ga_init(&default_args);
4785
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004786 /*
4787 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004788 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004789 * "name" == func, "fudi.fd_dict" == NULL
4790 * dict.func new dictionary entry
4791 * "name" == NULL, "fudi.fd_dict" set,
4792 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4793 * dict.func existing dict entry with a Funcref
4794 * "name" == func, "fudi.fd_dict" set,
4795 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4796 * dict.func existing dict entry that's not a Funcref
4797 * "name" == NULL, "fudi.fd_dict" set,
4798 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4799 * s:func script-local function name
4800 * g:func global function name, same as "func"
4801 */
4802 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004803 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004804 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004805 // nested function, argument is (args).
4806 paren = TRUE;
4807 CLEAR_FIELD(fudi);
4808 }
4809 else
4810 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004811 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004812 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004813 if (p[0] == 's' && p[1] == ':')
4814 {
4815 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4816 return NULL;
4817 }
4818 p = to_name_end(p, TRUE);
4819 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4820 {
4821 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4822 eap->arg);
4823 return NULL;
4824 }
4825 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004826 }
4827
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004828 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004829 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004830 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004831 paren = (vim_strchr(p, '(') != NULL);
4832 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004833 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004834 /*
4835 * Return on an invalid expression in braces, unless the expression
4836 * evaluation has been cancelled due to an aborting error, an
4837 * interrupt, or an exception.
4838 */
4839 if (!aborting())
4840 {
4841 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004842 semsg(_(e_key_not_present_in_dictionary_str),
4843 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004844 vim_free(fudi.fd_newkey);
4845 return NULL;
4846 }
4847 else
4848 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004849 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004850
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004851 // For "export def FuncName()" in an autoload script the function name
4852 // is stored with the legacy autoload name "dir#script#FuncName" so
4853 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004854 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004855 {
4856 char_u *prefixed = may_prefix_autoload(name);
4857
4858 if (prefixed != NULL && prefixed != name)
4859 {
4860 vim_free(name);
4861 name = prefixed;
4862 }
4863 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004864 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004865 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004866 {
4867 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4868 goto ret_free;
4869 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004870 }
4871
Bram Moolenaare38eab22019-12-05 21:50:01 +01004872 // An error in a function call during evaluation of an expression in magic
4873 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004874 saved_did_emsg = did_emsg;
4875 did_emsg = FALSE;
4876
4877 /*
4878 * ":function func" with only function name: list function.
4879 */
4880 if (!paren)
4881 {
4882 if (!ends_excmd(*skipwhite(p)))
4883 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004884 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004885 goto ret_free;
4886 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004887 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004888 if (eap->nextcmd != NULL)
4889 *p = NUL;
4890 if (!eap->skip && !got_int)
4891 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004892 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004893 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4894 {
4895 char_u *up = untrans_function_name(name);
4896
4897 // With Vim9 script the name was made script-local, if not
4898 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004899 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004900 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004901 }
4902
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004903 if (fp != NULL)
4904 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004905 // Check no function was added or removed from a timer, e.g. at
4906 // the more prompt. "fp" may then be invalid.
4907 int prev_ht_changed = func_hashtab.ht_changed;
4908
4909 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004910 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004911 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4912 {
4913 if (FUNCLINE(fp, j) == NULL)
4914 continue;
4915 msg_putchar('\n');
4916 msg_outnum((long)(j + 1));
4917 if (j < 9)
4918 msg_putchar(' ');
4919 if (j < 99)
4920 msg_putchar(' ');
4921 if (function_list_modified(prev_ht_changed))
4922 break;
4923 msg_prt_line(FUNCLINE(fp, j), FALSE);
4924 out_flush(); // show a line at a time
4925 ui_breakcheck();
4926 }
4927 if (!got_int)
4928 {
4929 msg_putchar('\n');
4930 if (!function_list_modified(prev_ht_changed))
4931 {
4932 if (fp->uf_def_status != UF_NOT_COMPILED)
4933 msg_puts(" enddef");
4934 else
4935 msg_puts(" endfunction");
4936 }
4937 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004938 }
4939 }
4940 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004941 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004942 }
4943 goto ret_free;
4944 }
4945
4946 /*
4947 * ":function name(arg1, arg2)" Define function.
4948 */
4949 p = skipwhite(p);
4950 if (*p != '(')
4951 {
4952 if (!eap->skip)
4953 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004954 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004955 goto ret_free;
4956 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004957 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004958 if (vim_strchr(p, '(') != NULL)
4959 p = vim_strchr(p, '(');
4960 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004961
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004962 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4963 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004964 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004965 goto ret_free;
4966 }
4967
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004968 // In Vim9 script only global functions can be redefined.
4969 if (vim9script && eap->forceit && !is_global)
4970 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004971 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004972 goto ret_free;
4973 }
4974
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004975 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004976
Bram Moolenaar04b12692020-05-04 23:24:44 +02004977 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004978 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004979 // Check the name of the function. Unless it's a dictionary function
4980 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004981 if (name != NULL)
4982 arg = name;
4983 else
4984 arg = fudi.fd_newkey;
4985 if (arg != NULL && (fudi.fd_di == NULL
4986 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4987 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4988 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004989 char_u *name_base = arg;
4990 int i;
4991
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004992 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004993 {
4994 name_base = vim_strchr(arg, '_');
4995 if (name_base == NULL)
4996 name_base = arg + 3;
4997 else
4998 ++name_base;
4999 }
5000 for (i = 0; name_base[i] != NUL && (i == 0
5001 ? eval_isnamec1(name_base[i])
5002 : eval_isnamec(name_base[i])); ++i)
5003 ;
5004 if (name_base[i] != NUL)
zeertzjq6a04bf52024-03-16 09:39:06 +01005005 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005006 emsg_funcname(e_invalid_argument_str, arg);
zeertzjq6a04bf52024-03-16 09:39:06 +01005007 goto ret_free;
5008 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00005009
5010 // In Vim9 script a function cannot have the same name as a
5011 // variable.
5012 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00005013 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
5014 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00005015 + EVAL_VAR_NO_FUNC) == OK)
5016 {
5017 semsg(_(e_redefining_script_item_str), name_base);
5018 goto ret_free;
5019 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005020 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005021 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005022 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005023 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005024 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00005025 goto ret_free;
5026 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005027 }
5028
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005029 // This may get more lines and make the pointers into the first line
5030 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01005031 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005032 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00005033 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02005034 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005035 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00005036 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005037 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01005038 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005039
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005040 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005041 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005042 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005043 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005044 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005045 if (*p != ':')
5046 {
5047 semsg(_(e_no_white_space_allowed_before_colon_str), p);
5048 p = skipwhite(p);
5049 }
5050 else if (!IS_WHITE_OR_NUL(p[1]))
5051 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005052 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02005053 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005054 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005055 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005056 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01005057 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005058 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005059 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005060 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005061 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005062 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02005063 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005064 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005065 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02005066 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005067 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068 else
5069 // find extra arguments "range", "dict", "abort" and "closure"
5070 for (;;)
5071 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01005072 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005073 p = skipwhite(p);
5074 if (STRNCMP(p, "range", 5) == 0)
5075 {
5076 flags |= FC_RANGE;
5077 p += 5;
5078 }
5079 else if (STRNCMP(p, "dict", 4) == 0)
5080 {
5081 flags |= FC_DICT;
5082 p += 4;
5083 }
5084 else if (STRNCMP(p, "abort", 5) == 0)
5085 {
5086 flags |= FC_ABORT;
5087 p += 5;
5088 }
5089 else if (STRNCMP(p, "closure", 7) == 0)
5090 {
5091 flags |= FC_CLOSURE;
5092 p += 7;
5093 if (current_funccal == NULL)
5094 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005095 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096 name == NULL ? (char_u *)"" : name);
5097 goto erret;
5098 }
5099 }
5100 else
5101 break;
5102 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005103
Bram Moolenaare38eab22019-12-05 21:50:01 +01005104 // When there is a line break use what follows for the function body.
5105 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005106 if (*p == '\n')
5107 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005108 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005109 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5110 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005111 && !(VIM_ISWHITE(*whitep) && *p == '#'
5112 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005113 && !eap->skip
5114 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005115 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005116
5117 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005118 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5119 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005120 */
5121 if (KeyTyped)
5122 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005123 // Check if the function already exists, don't let the user type the
5124 // whole function before telling him it doesn't work! For a script we
5125 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005126 if (!eap->skip && !eap->forceit)
5127 {
5128 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005129 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005130 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005131 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005132 }
5133
5134 if (!eap->skip && did_emsg)
5135 goto erret;
5136
Bram Moolenaare38eab22019-12-05 21:50:01 +01005137 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005138 cmdline_row = msg_row;
5139 }
5140
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005141 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005142 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005143
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005144 // Do not define the function when getting the body fails and when
5145 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005146 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005147 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005148 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5149 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005150 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005151 goto erret;
5152
5153 /*
5154 * If there are no errors, add the function
5155 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005156 if (fudi.fd_dict != NULL)
5157 {
5158 char numbuf[20];
5159
5160 fp = NULL;
5161 if (fudi.fd_newkey == NULL && !eap->forceit)
5162 {
5163 emsg(_(e_dictionary_entry_already_exists));
5164 goto erret;
5165 }
5166 if (fudi.fd_di == NULL)
5167 {
5168 // Can't add a function to a locked dictionary
5169 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5170 goto erret;
5171 }
5172 // Can't change an existing function if it is locked
5173 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5174 goto erret;
5175
5176 // Give the function a sequential number. Can only be used with a
5177 // Funcref!
5178 vim_free(name);
5179 sprintf(numbuf, "%d", ++func_nr);
5180 name = vim_strsave((char_u *)numbuf);
5181 if (name == NULL)
5182 goto erret;
5183 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005184 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005185 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005186 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005187 char_u *find_name = name;
5188 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005189 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005190
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005191 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005192 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005193 var_conflict = TRUE;
5194
5195 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5196 {
5197 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5198
5199 if (si->sn_autoload_prefix != NULL)
5200 {
5201 if (is_export)
5202 {
5203 find_name = name + STRLEN(si->sn_autoload_prefix);
5204 v = find_var(find_name, &ht, TRUE);
5205 if (v != NULL)
5206 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005207 // Only check if the function already exists in the script,
5208 // global functions can be shadowed.
5209 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005210 }
5211 else
5212 {
5213 char_u *prefixed = may_prefix_autoload(name);
5214
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005215 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005216 {
5217 v = find_var(prefixed, &ht, TRUE);
5218 if (v != NULL)
5219 var_conflict = TRUE;
5220 vim_free(prefixed);
5221 }
5222 }
5223 }
5224 }
5225 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005226 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005227 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005228 goto erret;
5229 }
5230
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005231 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005232 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005233 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005234 char_u *uname = untrans_function_name(name);
5235
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005236 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005237 }
5238
5239 if (fp != NULL || import != NULL)
5240 {
5241 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005242
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005243 // Function can be replaced with "function!" and when sourcing the
5244 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005245 // A name that is used by an import can not be overruled.
5246 if (import != NULL
5247 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005248 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005249 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005250 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005251 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005252 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005253 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005254 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005255 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005256 goto erret;
5257 }
5258 if (fp->uf_calls > 0)
5259 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005260 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005261 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005262 goto erret;
5263 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005264 if (fp->uf_refcount > 1)
5265 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005266 // This function is referenced somewhere, don't redefine it but
5267 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005268 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005269 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005270 fp = NULL;
5271 overwrite = TRUE;
5272 }
5273 else
5274 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005275 char_u *exp_name = fp->uf_name_exp;
5276
5277 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005278 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005279 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005280 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005281 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005282 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005283#ifdef FEAT_PROFILE
5284 fp->uf_profiling = FALSE;
5285 fp->uf_prof_initialized = FALSE;
5286#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005287 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005288 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005289 }
5290 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005291
5292 if (fp == NULL)
5293 {
5294 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5295 {
5296 int slen, plen;
5297 char_u *scriptname;
5298
Bram Moolenaare38eab22019-12-05 21:50:01 +01005299 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005300 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005301 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005302 {
5303 scriptname = autoload_name(name);
5304 if (scriptname != NULL)
5305 {
5306 p = vim_strchr(scriptname, '/');
5307 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005308 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005309 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005310 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005311 j = OK;
5312 vim_free(scriptname);
5313 }
5314 }
5315 if (j == FAIL)
5316 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005317 linenr_T save_lnum = SOURCING_LNUM;
5318
5319 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005320 semsg(_(e_function_name_does_not_match_script_file_name_str),
5321 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005322 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005323 goto erret;
5324 }
5325 }
5326
Bram Moolenaare01e5212023-01-08 20:31:18 +00005327 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005328 if (fp == NULL)
5329 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005330 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005331
5332 if (fudi.fd_dict != NULL)
5333 {
5334 if (fudi.fd_di == NULL)
5335 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005336 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005337 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5338 if (fudi.fd_di == NULL)
5339 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005340 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005341 goto erret;
5342 }
5343 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5344 {
5345 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005346 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005347 goto erret;
5348 }
5349 }
5350 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005351 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005352 clear_tv(&fudi.fd_di->di_tv);
5353 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005354 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005355
Bram Moolenaare38eab22019-12-05 21:50:01 +01005356 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005357 flags |= FC_DICT;
5358 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005359 }
5360 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005361 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005362 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005363 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005364
5365 if (eap->cmdidx == CMD_def)
5366 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005367 int lnum_save = SOURCING_LNUM;
5368 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005369
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005370 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005371
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005372 // error messages are for the first function line
5373 SOURCING_LNUM = sourcing_lnum_top;
5374
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005375 // The function may use script variables from the context.
5376 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005377
h-eastb895b0f2023-09-24 15:46:31 +02005378 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5379 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005380 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005381 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005382 free_fp = fp_allocated;
5383 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005384 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005385 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005386
5387 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005388 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005389 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005390 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005391 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005392 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005393 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005394 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005395 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005396 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005397 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005398
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005399 if (fp_allocated)
5400 {
5401 // insert the new function in the function list
5402 set_ufunc_name(fp, name);
5403 if (overwrite)
5404 {
5405 hi = hash_find(&func_hashtab, name);
5406 hi->hi_key = UF2HIKEY(fp);
5407 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005408 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005409 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005410 {
5411 free_fp = TRUE;
5412 goto erret;
5413 }
5414 fp->uf_refcount = 1;
5415 }
5416
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005417 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005418 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005419 if ((flags & FC_CLOSURE) != 0)
5420 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005421 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005422 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005423 }
5424 else
5425 fp->uf_scoped = NULL;
5426
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005427#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005428 if (prof_def_func())
5429 func_do_profile(fp);
5430#endif
5431 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005432 if (sandbox)
5433 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005434 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005435 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005436 fp->uf_flags = flags;
5437 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005438 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005439 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005440 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005441 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005442 if (is_export)
5443 {
5444 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005445 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005446 is_export = FALSE;
5447 }
5448
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005449 if (eap->cmdidx == CMD_def)
5450 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005451 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5452 // :func does not use Vim9 script syntax, even in a Vim9 script file
5453 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005454
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005455 goto ret_free;
5456
5457erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005458 if (fp != NULL)
5459 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005460 // these were set to "newargs" and "default_args", which are cleared
5461 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005462 ga_init(&fp->uf_args);
5463 ga_init(&fp->uf_def_args);
5464 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005465errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005466 ga_clear_strings(&newargs);
5467 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005468 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005469 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005470 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005471 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005472 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01005473 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005474 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005475 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005476 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005477ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005478 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005479 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005480 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005481 if (name != name_arg)
5482 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005483 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005484 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005485
5486 return fp;
5487}
5488
5489/*
5490 * ":function"
5491 */
5492 void
5493ex_function(exarg_T *eap)
5494{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005495 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005496
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005497 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005498 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005499 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005500}
5501
5502/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005503 * Find a function by name, including "<lambda>123".
5504 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005505 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005506 * Return NULL if not found.
5507 */
5508 ufunc_T *
5509find_func_by_name(char_u *name, compiletype_T *compile_type)
5510{
5511 char_u *arg = name;
5512 char_u *fname;
5513 ufunc_T *ufunc;
5514 int is_global = FALSE;
5515
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005516 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5517 {
5518 *compile_type = CT_PROFILE;
5519 arg = skipwhite(arg + 7);
5520 }
5521 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5522 {
5523 *compile_type = CT_DEBUG;
5524 arg = skipwhite(arg + 5);
5525 }
5526
5527 if (STRNCMP(arg, "<lambda>", 8) == 0)
5528 {
5529 arg += 8;
5530 (void)getdigits(&arg);
5531 fname = vim_strnsave(name, arg - name);
5532 }
5533 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005534 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005535 // First try finding a method in a class, trans_function_name() will
5536 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005537 ufunc = find_class_func(&arg);
5538 if (ufunc != NULL)
5539 return ufunc;
5540
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005541 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005542 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005543 NULL, NULL, NULL, &ufunc);
5544 if (ufunc != NULL)
5545 {
5546 vim_free(fname);
5547 return ufunc;
5548 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005549 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005550 if (fname == NULL)
5551 {
5552 semsg(_(e_invalid_argument_str), name);
5553 return NULL;
5554 }
5555 if (!ends_excmd2(name, arg))
5556 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005557 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005558 emsg(ex_errmsg(e_trailing_characters_str, arg));
5559 return NULL;
5560 }
5561
5562 ufunc = find_func(fname, is_global);
5563 if (ufunc == NULL)
5564 {
5565 char_u *p = untrans_function_name(fname);
5566
5567 if (p != NULL)
5568 // Try again without making it script-local.
5569 ufunc = find_func(p, FALSE);
5570 }
5571 vim_free(fname);
5572 if (ufunc == NULL)
5573 semsg(_(e_cannot_find_function_str), name);
5574 return ufunc;
5575}
5576
5577/*
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005578 * Compile the :def function "ufunc". If "cl" is not NULL, then compile the
5579 * class or object method "ufunc" in "cl".
5580 */
5581 void
5582defcompile_function(ufunc_T *ufunc, class_T *cl)
5583{
5584 compiletype_T compile_type = CT_NONE;
5585
5586 if (func_needs_compiling(ufunc, compile_type))
5587 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5588 else
5589 smsg(_("Function %s%s%s does not need compiling"),
5590 cl != NULL ? cl->class_name : (char_u *)"",
5591 cl != NULL ? (char_u *)"." : (char_u *)"",
5592 ufunc->uf_name);
5593}
5594
5595/*
5596 * Compile all the :def functions defined in the current script
5597 */
5598 static void
5599defcompile_funcs_in_script(void)
5600{
5601 long todo = (long)func_hashtab.ht_used;
5602 int changed = func_hashtab.ht_changed;
5603 hashitem_T *hi;
5604
5605 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5606 {
5607 if (!HASHITEM_EMPTY(hi))
5608 {
5609 --todo;
5610 ufunc_T *ufunc = HI2UF(hi);
5611 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5612 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5613 && (ufunc->uf_flags & FC_DEAD) == 0)
5614 {
5615 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5616
5617 if (func_hashtab.ht_changed != changed)
5618 {
5619 // a function has been added or removed, need to start
5620 // over
5621 todo = (long)func_hashtab.ht_used;
5622 changed = func_hashtab.ht_changed;
5623 hi = func_hashtab.ht_array;
5624 --hi;
5625 }
5626 }
5627 }
5628 }
5629}
5630
5631/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005632 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005633 * be compiled or the one specified by the argument.
5634 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005635 */
5636 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005637ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005638{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005639 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005640 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005641 typval_T tv;
5642
5643 if (is_class_name(eap->arg, &tv))
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005644 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005645 class_T *cl = tv.vval.v_class;
5646
5647 if (cl != NULL)
5648 defcompile_class(cl);
5649 }
5650 else
5651 {
5652 compiletype_T compile_type = CT_NONE;
5653 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
5654 if (ufunc != NULL)
5655 defcompile_function(ufunc, NULL);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005656 }
5657 }
5658 else
5659 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005660 defcompile_funcs_in_script();
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005661
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005662 // compile all the class defined in the current script
5663 defcompile_classes_in_script();
Bram Moolenaar822ba242020-05-24 23:00:18 +02005664 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005665}
5666
5667/*
5668 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5669 * Return 2 if "p" starts with "s:".
5670 * Return 0 otherwise.
5671 */
5672 int
5673eval_fname_script(char_u *p)
5674{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005675 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5676 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005677 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5678 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5679 return 5;
5680 if (p[0] == 's' && p[1] == ':')
5681 return 2;
5682 return 0;
5683}
5684
5685 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005686translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005687{
5688 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005689 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005690 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005691}
5692
5693/*
5694 * Return TRUE when "ufunc" has old-style "..." varargs
5695 * or named varargs "...name: type".
5696 */
5697 int
5698has_varargs(ufunc_T *ufunc)
5699{
5700 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005701}
5702
5703/*
5704 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005705 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005706 */
5707 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005708function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005709{
5710 char_u *nm = name;
5711 char_u *p;
5712 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005713 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005714 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005715
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005716 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5717 if (no_deref)
5718 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005719 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005720 nm = skipwhite(nm);
5721
Bram Moolenaare38eab22019-12-05 21:50:01 +01005722 // Only accept "funcname", "funcname ", "funcname (..." and
5723 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005724 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005725 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005726 vim_free(p);
5727 return n;
5728}
5729
Bram Moolenaar113e1072019-01-20 15:30:40 +01005730#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005731 char_u *
5732get_expanded_name(char_u *name, int check)
5733{
5734 char_u *nm = name;
5735 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005736 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005737
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005738 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005739
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005740 if (p != NULL && *nm == NUL
5741 && (!check || translated_function_exists(p, is_global)))
5742 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005743
5744 vim_free(p);
5745 return NULL;
5746}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005747#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005748
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005749/*
5750 * Function given to ExpandGeneric() to obtain the list of user defined
5751 * function names.
5752 */
5753 char_u *
5754get_user_func_name(expand_T *xp, int idx)
5755{
5756 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005757 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005758 static hashitem_T *hi;
5759 ufunc_T *fp;
5760
5761 if (idx == 0)
5762 {
5763 done = 0;
5764 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005765 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005766 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005767 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005768 {
5769 if (done++ > 0)
5770 ++hi;
5771 while (HASHITEM_EMPTY(hi))
5772 ++hi;
5773 fp = HI2UF(hi);
5774
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005775 // don't show dead, dict and lambda functions
5776 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005777 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005778 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005779
5780 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005781 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005782
5783 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005784 if (xp->xp_context != EXPAND_USER_FUNC
5785 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005786 {
5787 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005788 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005789 STRCAT(IObuff, ")");
5790 }
5791 return IObuff;
5792 }
5793 return NULL;
5794}
5795
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005796/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005797 * Make a copy of a function.
5798 * Intended to be used for a function defined on a base class that has a copy
5799 * on the child class.
5800 * The copy has uf_refcount set to one.
5801 * Returns NULL when out of memory.
5802 */
5803 ufunc_T *
5804copy_function(ufunc_T *fp)
5805{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005806 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005807 if (ufunc == NULL)
5808 return NULL;
5809
5810 // Most things can just be copied.
5811 *ufunc = *fp;
5812
5813 ufunc->uf_def_status = UF_TO_BE_COMPILED;
5814 ufunc->uf_dfunc_idx = 0;
5815 ufunc->uf_class = NULL;
5816
5817 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
5818 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
5819
5820 if (ufunc->uf_arg_types != NULL)
5821 {
5822 // "uf_arg_types" is an allocated array, make a copy.
5823 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
5824 if (at != NULL)
5825 {
5826 mch_memmove(at, ufunc->uf_arg_types,
5827 sizeof(type_T *) * ufunc->uf_args.ga_len);
5828 ufunc->uf_arg_types = at;
5829 }
5830 }
5831
5832 // TODO: how about the types themselves? they can be freed when the
5833 // original function is freed:
5834 // type_T **uf_arg_types;
5835 // type_T *uf_ret_type;
5836
Ernie Rael114ec812023-06-04 18:11:35 +01005837 // make uf_type_list empty
5838 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00005839
5840 // TODO: partial_T *uf_partial;
5841
5842 if (ufunc->uf_va_name != NULL)
5843 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
5844
5845 // TODO:
5846 // type_T *uf_va_type;
5847 // type_T *uf_func_type;
5848
5849 ufunc->uf_block_depth = 0;
5850 ufunc->uf_block_ids = NULL;
5851
5852 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
5853
5854 ufunc->uf_refcount = 1;
5855 ufunc->uf_name_exp = NULL;
5856 STRCPY(ufunc->uf_name, fp->uf_name);
5857
5858 return ufunc;
5859}
5860
5861/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005862 * ":delfunction {name}"
5863 */
5864 void
5865ex_delfunction(exarg_T *eap)
5866{
5867 ufunc_T *fp = NULL;
5868 char_u *p;
5869 char_u *name;
5870 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005871 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005872
5873 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005874 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
5875 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005876 vim_free(fudi.fd_newkey);
5877 if (name == NULL)
5878 {
5879 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005880 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005881 return;
5882 }
5883 if (!ends_excmd(*skipwhite(p)))
5884 {
5885 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005886 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005887 return;
5888 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005889 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005890 if (eap->nextcmd != NULL)
5891 *p = NUL;
5892
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005893 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005894 {
5895 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005896 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005897 vim_free(name);
5898 return;
5899 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005900 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005901 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005902 vim_free(name);
5903
5904 if (!eap->skip)
5905 {
5906 if (fp == NULL)
5907 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005908 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005909 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005910 return;
5911 }
5912 if (fp->uf_calls > 0)
5913 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005914 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005915 return;
5916 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005917 if (fp->uf_flags & FC_VIM9)
5918 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005919 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005920 return;
5921 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005922
5923 if (fudi.fd_dict != NULL)
5924 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005925 // Delete the dict item that refers to the function, it will
5926 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00005927 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005928 }
5929 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005930 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005931 // A normal function (not a numbered function or lambda) has a
5932 // refcount of 1 for the entry in the hashtable. When deleting
5933 // it and the refcount is more than one, it should be kept.
5934 // A numbered function and lambda should be kept if the refcount is
5935 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005936 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005937 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005938 // Function is still referenced somewhere. Don't free it but
5939 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005940 if (func_remove(fp))
5941 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005942 }
5943 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005944 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005945 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005946 }
5947}
5948
5949/*
5950 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005951 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005952 */
5953 void
5954func_unref(char_u *name)
5955{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005956 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005957
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005958 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005959 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005960 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005961 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005962 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005963#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005964 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005965#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005966 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005967 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005968 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005969}
5970
5971/*
5972 * Unreference a Function: decrement the reference count and free it when it
5973 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005974 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005975 */
5976 void
5977func_ptr_unref(ufunc_T *fp)
5978{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005979 if (fp != NULL && (--fp->uf_refcount <= 0
5980 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5981 && fp->uf_partial->pt_refcount <= 1
5982 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005983 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005984 // Only delete it when it's not being used. Otherwise it's done
5985 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005986 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005987 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005988 }
5989}
5990
5991/*
5992 * Count a reference to a Function.
5993 */
5994 void
5995func_ref(char_u *name)
5996{
5997 ufunc_T *fp;
5998
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005999 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006000 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006001 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006002 if (fp != NULL)
6003 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006004 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01006005 // Only give an error for a numbered function.
6006 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01006007 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006008}
6009
6010/*
6011 * Count a reference to a Function.
6012 */
6013 void
6014func_ptr_ref(ufunc_T *fp)
6015{
6016 if (fp != NULL)
6017 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006018}
6019
6020/*
6021 * Return TRUE if items in "fc" do not have "copyID". That means they are not
6022 * referenced from anywhere that is in use.
6023 */
6024 static int
6025can_free_funccal(funccall_T *fc, int copyID)
6026{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006027 return (fc->fc_l_varlist.lv_copyID != copyID
6028 && fc->fc_l_vars.dv_copyID != copyID
6029 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006030 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006031}
6032
6033/*
6034 * ":return [expr]"
6035 */
6036 void
6037ex_return(exarg_T *eap)
6038{
6039 char_u *arg = eap->arg;
6040 typval_T rettv;
6041 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006042 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006043
6044 if (current_funccal == NULL)
6045 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006046 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006047 return;
6048 }
6049
Bram Moolenaar844fb642021-10-23 13:32:30 +01006050 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006051 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
6052
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006053 if (eap->skip)
6054 ++emsg_skip;
6055
6056 eap->nextcmd = NULL;
6057 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006058 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006059 {
6060 if (!eap->skip)
6061 returning = do_return(eap, FALSE, TRUE, &rettv);
6062 else
6063 clear_tv(&rettv);
6064 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01006065 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006066 else if (!eap->skip)
6067 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006068 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01006069 update_force_abort();
6070
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006071 /*
6072 * Return unless the expression evaluation has been cancelled due to an
6073 * aborting error, an interrupt, or an exception.
6074 */
6075 if (!aborting())
6076 returning = do_return(eap, FALSE, TRUE, NULL);
6077 }
6078
Bram Moolenaare38eab22019-12-05 21:50:01 +01006079 // When skipping or the return gets pending, advance to the next command
6080 // in this line (!returning). Otherwise, ignore the rest of the line.
6081 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006082 if (returning)
6083 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006084 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02006085 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006086
6087 if (eap->skip)
6088 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02006089 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006090}
6091
zeertzjq5299c092023-04-12 20:48:16 +01006092/*
6093 * Lower level implementation of "call". Only called when not skipping.
6094 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006095 static int
6096ex_call_inner(
6097 exarg_T *eap,
6098 char_u *name,
6099 char_u **arg,
6100 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006101 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006102 evalarg_T *evalarg)
6103{
6104 linenr_T lnum;
6105 int doesrange;
6106 typval_T rettv;
6107 int failed = FALSE;
6108
zeertzjq5299c092023-04-12 20:48:16 +01006109 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006110 for ( ; lnum <= eap->line2; ++lnum)
6111 {
6112 funcexe_T funcexe;
6113
zeertzjq5299c092023-04-12 20:48:16 +01006114 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006115 {
6116 if (lnum > curbuf->b_ml.ml_line_count)
6117 {
6118 // If the function deleted lines or switched to another buffer
6119 // the line number may become invalid.
6120 emsg(_(e_invalid_range));
6121 break;
6122 }
6123 curwin->w_cursor.lnum = lnum;
6124 curwin->w_cursor.col = 0;
6125 curwin->w_cursor.coladd = 0;
6126 }
6127 *arg = startarg;
6128
6129 funcexe = *funcexe_init;
6130 funcexe.fe_doesrange = &doesrange;
6131 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6132 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6133 {
6134 failed = TRUE;
6135 break;
6136 }
6137 if (has_watchexpr())
6138 dbg_check_breakpoint(eap);
6139
6140 // Handle a function returning a Funcref, Dictionary or List.
6141 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006142 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006143 {
6144 failed = TRUE;
6145 break;
6146 }
6147
6148 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006149 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006150 break;
6151
6152 // Stop when immediately aborting on error, or when an interrupt
6153 // occurred or an exception was thrown but not caught.
6154 // get_func_tv() returned OK, so that the check for trailing
6155 // characters below is executed.
6156 if (aborting())
6157 break;
6158 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006159 return failed;
6160}
6161
6162/*
6163 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6164 * Returns FAIL or OK.
6165 */
6166 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006167ex_defer_inner(
6168 char_u *name,
6169 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006170 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006171 partial_T *partial,
6172 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006173{
6174 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006175 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006176 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006177 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006178
6179 if (current_funccal == NULL)
6180 {
6181 semsg(_(e_str_not_inside_function), "defer");
6182 return FAIL;
6183 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006184 if (partial != NULL)
6185 {
6186 if (partial->pt_dict != NULL)
6187 {
6188 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6189 return FAIL;
6190 }
6191 if (partial->pt_argc > 0)
6192 {
6193 int i;
6194
6195 partial_argc = partial->pt_argc;
6196 for (i = 0; i < partial_argc; ++i)
6197 copy_tv(&partial->pt_argv[i], &argvars[i]);
6198 }
6199 }
Ernie Raelb077b582023-12-14 20:11:44 +01006200 int is_builtin = builtin_function(name, -1);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006201 r = get_func_arguments(arg, evalarg, FALSE,
Ernie Raelb077b582023-12-14 20:11:44 +01006202 argvars + partial_argc, &argcount, is_builtin);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006203 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006204
6205 if (r == OK)
6206 {
6207 if (type != NULL)
6208 {
6209 // Check that the arguments are OK for the types of the funcref.
6210 r = check_argument_types(type, argvars, argcount, NULL, name);
6211 }
Ernie Raelb077b582023-12-14 20:11:44 +01006212 else if (is_builtin)
Bram Moolenaar16900322022-09-08 19:51:45 +01006213 {
6214 int idx = find_internal_func(name);
6215
6216 if (idx < 0)
6217 {
6218 emsg_funcname(e_unknown_function_str, name);
6219 r = FAIL;
6220 }
6221 else if (check_internal_func(idx, argcount) == -1)
6222 r = FAIL;
6223 }
6224 else
6225 {
6226 ufunc_T *ufunc = find_func(name, FALSE);
6227
6228 // we tolerate an unknown function here, it might be defined later
6229 if (ufunc != NULL)
6230 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006231 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006232 if (error != FCERR_UNKNOWN)
6233 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006234 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006235 r = FAIL;
6236 }
6237 }
6238 }
6239 }
6240
Bram Moolenaar86d87252022-09-05 21:21:25 +01006241 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006242 {
6243 while (--argcount >= 0)
6244 clear_tv(&argvars[argcount]);
6245 return FAIL;
6246 }
6247 return add_defer(name, argcount, argvars);
6248}
6249
6250/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006251 * Return TRUE if currently inside a function call.
6252 * Give an error message and return FALSE when not.
6253 */
6254 int
6255can_add_defer(void)
6256{
6257 if (!in_def_function() && get_current_funccal() == NULL)
6258 {
6259 semsg(_(e_str_not_inside_function), "defer");
6260 return FALSE;
6261 }
6262 return TRUE;
6263}
6264
6265/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006266 * Add a deferred call for "name" with arguments "argvars[argcount]".
6267 * Consumes "argvars[]".
6268 * Caller must check that in_def_function() returns TRUE or current_funccal is
6269 * not NULL.
6270 * Returns OK or FAIL.
6271 */
6272 int
6273add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6274{
6275 char_u *saved_name = vim_strsave(name);
6276 int argcount = argcount_arg;
6277 defer_T *dr;
6278 int ret = FAIL;
6279
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006280 if (saved_name == NULL)
6281 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006282 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006283 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006284 if (add_defer_function(saved_name, argcount, argvars) == OK)
6285 argcount = 0;
6286 }
6287 else
6288 {
6289 if (current_funccal->fc_defer.ga_itemsize == 0)
6290 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6291 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6292 goto theend;
6293 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6294 + current_funccal->fc_defer.ga_len++;
6295 dr->dr_name = saved_name;
6296 dr->dr_argcount = argcount;
6297 while (argcount > 0)
6298 {
6299 --argcount;
6300 dr->dr_argvars[argcount] = argvars[argcount];
6301 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006302 }
6303 ret = OK;
6304
6305theend:
6306 while (--argcount >= 0)
6307 clear_tv(&argvars[argcount]);
6308 return ret;
6309}
6310
6311/*
6312 * Invoked after a functions has finished: invoke ":defer" functions.
6313 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006314 static void
6315handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006316{
6317 int idx;
6318
Bram Moolenaar58779852022-09-06 18:31:14 +01006319 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006320 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006321 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006322
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006323 if (dr->dr_name == NULL)
6324 // already being called, can happen if function does ":qa"
6325 continue;
6326
6327 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006328 CLEAR_FIELD(funcexe);
6329 funcexe.fe_evaluate = TRUE;
6330
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006331 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006332 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006333
6334 char_u *name = dr->dr_name;
6335 dr->dr_name = NULL;
6336
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006337 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006338 // first statement in the function will be executed (because of the
6339 // exception). So save and restore the try/catch/throw exception
6340 // state.
6341 exception_state_T estate;
6342 exception_state_save(&estate);
6343 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006344
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006345 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6346
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006347 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006348
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006349 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006350 vim_free(name);
6351 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006352 clear_tv(&dr->dr_argvars[i]);
6353 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006354 ga_clear(&funccal->fc_defer);
6355}
6356
zeertzjq960cf912023-04-18 21:52:54 +01006357 static void
6358invoke_funccall_defer(funccall_T *fc)
6359{
6360 if (fc->fc_ectx != NULL)
6361 {
6362 // :def function
6363 unwind_def_callstack(fc->fc_ectx);
6364 may_invoke_defer_funcs(fc->fc_ectx);
6365 }
6366 else
6367 {
6368 // legacy function
6369 handle_defer_one(fc);
6370 }
6371}
6372
Bram Moolenaar58779852022-09-06 18:31:14 +01006373/*
6374 * Called when exiting: call all defer functions.
6375 */
6376 void
6377invoke_all_defer(void)
6378{
zeertzjq1be4b812023-04-19 14:21:24 +01006379 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6380 invoke_funccall_defer(fc);
6381
zeertzjq960cf912023-04-18 21:52:54 +01006382 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6383 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6384 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006385}
6386
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006387/*
6388 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006389 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006390 */
6391 void
6392ex_call(exarg_T *eap)
6393{
6394 char_u *arg = eap->arg;
6395 char_u *startarg;
6396 char_u *name;
6397 char_u *tofree;
6398 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006399 int failed = FALSE;
6400 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006401 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006402 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006403 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006404 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006405 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006406 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006407
Bram Moolenaare6b53242020-07-01 17:28:33 +02006408 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006409 if (eap->skip)
6410 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006411 typval_T rettv;
6412
Bram Moolenaare38eab22019-12-05 21:50:01 +01006413 // trans_function_name() doesn't work well when skipping, use eval0()
6414 // instead to skip to any following command, e.g. for:
6415 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006416 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006417 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006418 clear_tv(&rettv);
6419 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006420 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006421 return;
6422 }
6423
zeertzjq5299c092023-04-12 20:48:16 +01006424 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006425 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006426 if (fudi.fd_newkey != NULL)
6427 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006428 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006429 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006430 vim_free(fudi.fd_newkey);
6431 }
6432 if (tofree == NULL)
6433 return;
6434
Bram Moolenaare38eab22019-12-05 21:50:01 +01006435 // Increase refcount on dictionary, it could get deleted when evaluating
6436 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006437 if (fudi.fd_dict != NULL)
6438 ++fudi.fd_dict->dv_refcount;
6439
Bram Moolenaare38eab22019-12-05 21:50:01 +01006440 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6441 // contents. For VAR_PARTIAL get its partial, unless we already have one
6442 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006443 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006444 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006445 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006446 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006447
Bram Moolenaare38eab22019-12-05 21:50:01 +01006448 // Skip white space to allow ":call func ()". Not good, but required for
6449 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006450 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006451 if (*startarg != '(')
6452 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006453 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006454 goto end;
6455 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006456 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006457 {
6458 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6459 goto end;
6460 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006461
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006462 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006463 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006464 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006465 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006466 }
6467 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006468 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006469 funcexe_T funcexe;
6470
Bram Moolenaara80faa82020-04-12 19:37:17 +02006471 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006472 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006473 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006474 funcexe.fe_partial = partial;
6475 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006476 funcexe.fe_firstline = eap->line1;
6477 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006478 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006479 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006480 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006481 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006482
Bram Moolenaar1d341892021-09-18 15:25:52 +02006483 // When inside :try we need to check for following "| catch" or "| endtry".
6484 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006485 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006486 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006487 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006488 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006489 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006490 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006491 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006492 {
6493 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006494 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006495 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006496 }
6497 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006498 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006499 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006500 // Must be after using "arg", it may point into memory cleared here.
6501 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006502
6503end:
6504 dict_unref(fudi.fd_dict);
6505 vim_free(tofree);
6506}
6507
6508/*
6509 * Return from a function. Possibly makes the return pending. Also called
6510 * for a pending return at the ":endtry" or after returning from an extra
6511 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6512 * when called due to a ":return" command. "rettv" may point to a typval_T
6513 * with the return rettv. Returns TRUE when the return can be carried out,
6514 * FALSE when the return gets pending.
6515 */
6516 int
6517do_return(
6518 exarg_T *eap,
6519 int reanimate,
6520 int is_cmd,
6521 void *rettv)
6522{
6523 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006524 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006525
6526 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006527 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006528 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006529
6530 /*
6531 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6532 * not in its finally clause (which then is to be executed next) is found.
6533 * In this case, make the ":return" pending for execution at the ":endtry".
6534 * Otherwise, return normally.
6535 */
6536 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6537 if (idx >= 0)
6538 {
6539 cstack->cs_pending[idx] = CSTP_RETURN;
6540
6541 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006542 // A pending return again gets pending. "rettv" points to an
6543 // allocated variable with the rettv of the original ":return"'s
6544 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006545 cstack->cs_rettv[idx] = rettv;
6546 else
6547 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006548 // When undoing a return in order to make it pending, get the stored
6549 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006550 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006551 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006552
6553 if (rettv != NULL)
6554 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006555 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006556 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6557 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6558 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006559 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006560 }
6561 else
6562 cstack->cs_rettv[idx] = NULL;
6563
6564 if (reanimate)
6565 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006566 // The pending return value could be overwritten by a ":return"
6567 // without argument in a finally clause; reset the default
6568 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006569 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6570 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006571 }
6572 }
6573 report_make_pending(CSTP_RETURN, rettv);
6574 }
6575 else
6576 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006577 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006578
Bram Moolenaare38eab22019-12-05 21:50:01 +01006579 // If the return is carried out now, store the return value. For
6580 // a return immediately after reanimation, the value is already
6581 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006582 if (!reanimate && rettv != NULL)
6583 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006584 clear_tv(current_funccal->fc_rettv);
6585 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006586 if (!is_cmd)
6587 vim_free(rettv);
6588 }
6589 }
6590
6591 return idx < 0;
6592}
6593
6594/*
6595 * Free the variable with a pending return value.
6596 */
6597 void
6598discard_pending_return(void *rettv)
6599{
6600 free_tv((typval_T *)rettv);
6601}
6602
6603/*
6604 * Generate a return command for producing the value of "rettv". The result
6605 * is an allocated string. Used by report_pending() for verbose messages.
6606 */
6607 char_u *
6608get_return_cmd(void *rettv)
6609{
6610 char_u *s = NULL;
6611 char_u *tofree = NULL;
6612 char_u numbuf[NUMBUFLEN];
6613
6614 if (rettv != NULL)
6615 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6616 if (s == NULL)
6617 s = (char_u *)"";
6618
6619 STRCPY(IObuff, ":return ");
6620 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6621 if (STRLEN(s) + 8 >= IOSIZE)
6622 STRCPY(IObuff + IOSIZE - 4, "...");
6623 vim_free(tofree);
6624 return vim_strsave(IObuff);
6625}
6626
6627/*
6628 * Get next function line.
6629 * Called by do_cmdline() to get the next line.
6630 * Returns allocated string, or NULL for end of function.
6631 */
6632 char_u *
6633get_func_line(
6634 int c UNUSED,
6635 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006636 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006637 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006638{
6639 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006640 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006641 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006642 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006643
Bram Moolenaare38eab22019-12-05 21:50:01 +01006644 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006645 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006646 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006647 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006648 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006649 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006650 }
6651#ifdef FEAT_PROFILE
6652 if (do_profiling == PROF_YES)
6653 func_line_end(cookie);
6654#endif
6655
6656 gap = &fp->uf_lines;
6657 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006658 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006659 retval = NULL;
6660 else
6661 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006662 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006663 while (fcp->fc_linenr < gap->ga_len
6664 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6665 ++fcp->fc_linenr;
6666 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006667 retval = NULL;
6668 else
6669 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006670 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6671 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006672#ifdef FEAT_PROFILE
6673 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006674 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006675#endif
6676 }
6677 }
6678
Bram Moolenaare38eab22019-12-05 21:50:01 +01006679 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006680 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006681 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006682 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006683 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006684 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006685 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006686 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006687 }
6688
6689 return retval;
6690}
6691
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006692/*
6693 * Return TRUE if the currently active function should be ended, because a
6694 * return was encountered or an error occurred. Used inside a ":while".
6695 */
6696 int
6697func_has_ended(void *cookie)
6698{
6699 funccall_T *fcp = (funccall_T *)cookie;
6700
Bram Moolenaare38eab22019-12-05 21:50:01 +01006701 // Ignore the "abort" flag if the abortion behavior has been changed due to
6702 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006703 return (((fcp->fc_func->uf_flags & FC_ABORT)
6704 && did_emsg && !aborted_in_try())
6705 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006706}
6707
6708/*
6709 * return TRUE if cookie indicates a function which "abort"s on errors.
6710 */
6711 int
6712func_has_abort(
6713 void *cookie)
6714{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006715 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006716}
6717
6718
6719/*
6720 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6721 * Don't do this when "Func" is already a partial that was bound
6722 * explicitly (pt_auto is FALSE).
6723 * Changes "rettv" in-place.
6724 * Returns the updated "selfdict_in".
6725 */
6726 dict_T *
6727make_partial(dict_T *selfdict_in, typval_T *rettv)
6728{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006729 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006730 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006731 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006732 dict_T *selfdict = selfdict_in;
6733
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006734 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6735 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006736 fp = rettv->vval.v_partial->pt_func;
6737 else
6738 {
6739 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006740 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006741 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006742 if (fname == NULL)
6743 {
6744 // There is no point binding a dict to a NULL function, just create
6745 // a function reference.
6746 rettv->v_type = VAR_FUNC;
6747 rettv->vval.v_string = NULL;
6748 }
6749 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006750 {
6751 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006752 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006753
6754 // Translate "s:func" to the stored function name.
6755 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6756 fp = find_func(fname, FALSE);
6757 vim_free(tofree);
6758 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006759 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006760
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006761 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006762 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006763 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006764
6765 if (pt != NULL)
6766 {
6767 pt->pt_refcount = 1;
6768 pt->pt_dict = selfdict;
6769 pt->pt_auto = TRUE;
6770 selfdict = NULL;
6771 if (rettv->v_type == VAR_FUNC)
6772 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006773 // Just a function: Take over the function name and use
6774 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006775 pt->pt_name = rettv->vval.v_string;
6776 }
6777 else
6778 {
6779 partial_T *ret_pt = rettv->vval.v_partial;
6780 int i;
6781
Bram Moolenaare38eab22019-12-05 21:50:01 +01006782 // Partial: copy the function name, use selfdict and copy
6783 // args. Can't take over name or args, the partial might
6784 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006785 if (ret_pt->pt_name != NULL)
6786 {
6787 pt->pt_name = vim_strsave(ret_pt->pt_name);
6788 func_ref(pt->pt_name);
6789 }
6790 else
6791 {
6792 pt->pt_func = ret_pt->pt_func;
6793 func_ptr_ref(pt->pt_func);
6794 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006795 if (ret_pt->pt_argc > 0)
6796 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006797 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006798 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006799 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006800 pt->pt_argc = 0;
6801 else
6802 {
6803 pt->pt_argc = ret_pt->pt_argc;
6804 for (i = 0; i < pt->pt_argc; i++)
6805 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6806 }
6807 }
6808 partial_unref(ret_pt);
6809 }
6810 rettv->v_type = VAR_PARTIAL;
6811 rettv->vval.v_partial = pt;
6812 }
6813 }
6814 return selfdict;
6815}
6816
6817/*
6818 * Return the name of the executed function.
6819 */
6820 char_u *
6821func_name(void *cookie)
6822{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006823 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006824}
6825
6826/*
6827 * Return the address holding the next breakpoint line for a funccall cookie.
6828 */
6829 linenr_T *
6830func_breakpoint(void *cookie)
6831{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006832 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006833}
6834
6835/*
6836 * Return the address holding the debug tick for a funccall cookie.
6837 */
6838 int *
6839func_dbg_tick(void *cookie)
6840{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006841 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006842}
6843
6844/*
6845 * Return the nesting level for a funccall cookie.
6846 */
6847 int
6848func_level(void *cookie)
6849{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006850 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006851}
6852
6853/*
6854 * Return TRUE when a function was ended by a ":return" command.
6855 */
6856 int
6857current_func_returned(void)
6858{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006859 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006860}
6861
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006862 int
6863free_unref_funccal(int copyID, int testing)
6864{
6865 int did_free = FALSE;
6866 int did_free_funccal = FALSE;
6867 funccall_T *fc, **pfc;
6868
6869 for (pfc = &previous_funccal; *pfc != NULL; )
6870 {
6871 if (can_free_funccal(*pfc, copyID))
6872 {
6873 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006874 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006875 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006876 did_free = TRUE;
6877 did_free_funccal = TRUE;
6878 }
6879 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006880 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006881 }
6882 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006883 // When a funccal was freed some more items might be garbage
6884 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006885 (void)garbage_collect(testing);
6886
6887 return did_free;
6888}
6889
6890/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006891 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006892 */
6893 static funccall_T *
6894get_funccal(void)
6895{
6896 int i;
6897 funccall_T *funccal;
6898 funccall_T *temp_funccal;
6899
6900 funccal = current_funccal;
6901 if (debug_backtrace_level > 0)
6902 {
6903 for (i = 0; i < debug_backtrace_level; i++)
6904 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006905 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006906 if (temp_funccal)
6907 funccal = temp_funccal;
6908 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006909 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006910 debug_backtrace_level = i;
6911 }
6912 }
6913 return funccal;
6914}
6915
6916/*
6917 * Return the hashtable used for local variables in the current funccal.
6918 * Return NULL if there is no current funccal.
6919 */
6920 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006921get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006922{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006923 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006924 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006925 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006926}
6927
6928/*
6929 * Return the l: scope variable.
6930 * Return NULL if there is no current funccal.
6931 */
6932 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006933get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006934{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006935 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006936 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006937 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006938}
6939
6940/*
6941 * Return the hashtable used for argument in the current funccal.
6942 * Return NULL if there is no current funccal.
6943 */
6944 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006945get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006946{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006947 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006948 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006949 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006950}
6951
6952/*
6953 * Return the a: scope variable.
6954 * Return NULL if there is no current funccal.
6955 */
6956 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006957get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006958{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006959 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006960 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006961 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006962}
6963
6964/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006965 * List function variables, if there is a function.
6966 */
6967 void
6968list_func_vars(int *first)
6969{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006970 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
6971 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006972 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006973}
6974
6975/*
6976 * If "ht" is the hashtable for local variables in the current funccal, return
6977 * the dict that contains it.
6978 * Otherwise return NULL.
6979 */
6980 dict_T *
6981get_current_funccal_dict(hashtab_T *ht)
6982{
6983 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01006984 && ht == &current_funccal->fc_l_vars.dv_hashtab)
6985 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006986 return NULL;
6987}
6988
6989/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006990 * Search hashitem in parent scope.
6991 */
6992 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006993find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006994{
6995 funccall_T *old_current_funccal = current_funccal;
6996 hashtab_T *ht;
6997 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006998 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006999
Bram Moolenaarca16c602022-09-06 18:57:08 +01007000 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007001 return NULL;
7002
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02007003 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01007004 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02007005 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007006 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007007 ht = find_var_ht(name, &varname);
7008 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02007009 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007010 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02007011 if (!HASHITEM_EMPTY(hi))
7012 {
7013 *pht = ht;
7014 break;
7015 }
7016 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007017 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02007018 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007019 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007020 }
7021 current_funccal = old_current_funccal;
7022
7023 return hi;
7024}
7025
7026/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007027 * Search variable in parent scope.
7028 */
7029 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007030find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007031{
7032 dictitem_T *v = NULL;
7033 funccall_T *old_current_funccal = current_funccal;
7034 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007035 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007036
Bram Moolenaarca16c602022-09-06 18:57:08 +01007037 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007038 return NULL;
7039
Bram Moolenaare38eab22019-12-05 21:50:01 +01007040 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01007041 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007042 while (current_funccal)
7043 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007044 ht = find_var_ht(name, &varname);
7045 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007046 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007047 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007048 if (v != NULL)
7049 break;
7050 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007051 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007052 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007053 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007054 }
7055 current_funccal = old_current_funccal;
7056
7057 return v;
7058}
7059
7060/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007061 * Set "copyID + 1" in previous_funccal and callers.
7062 */
7063 int
7064set_ref_in_previous_funccal(int copyID)
7065{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007066 funccall_T *fc;
7067
Bram Moolenaarca16c602022-09-06 18:57:08 +01007068 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007069 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007070 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007071 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
7072 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
7073 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007074 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007075 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007076 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007077}
7078
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007079 static int
7080set_ref_in_funccal(funccall_T *fc, int copyID)
7081{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007082 if (fc->fc_copyID != copyID)
7083 {
7084 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007085 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
7086 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
7087 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
7088 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007089 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007090 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007091 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007092}
7093
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007094/*
7095 * Set "copyID" in all local vars and arguments in the call stack.
7096 */
7097 int
7098set_ref_in_call_stack(int copyID)
7099{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007100 funccall_T *fc;
7101 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007102
Bram Moolenaarca16c602022-09-06 18:57:08 +01007103 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007104 if (set_ref_in_funccal(fc, copyID))
7105 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007106
7107 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007108 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007109 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007110 if (set_ref_in_funccal(fc, copyID))
7111 return TRUE;
7112 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007113}
7114
7115/*
7116 * Set "copyID" in all functions available by name.
7117 */
7118 int
7119set_ref_in_functions(int copyID)
7120{
7121 int todo;
7122 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007123 ufunc_T *fp;
7124
7125 todo = (int)func_hashtab.ht_used;
7126 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007127 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007128 if (!HASHITEM_EMPTY(hi))
7129 {
7130 --todo;
7131 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007132 if (!func_name_refcount(fp->uf_name)
7133 && set_ref_in_func(NULL, fp, copyID))
7134 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007135 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007136 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007137 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007138}
7139
7140/*
7141 * Set "copyID" in all function arguments.
7142 */
7143 int
7144set_ref_in_func_args(int copyID)
7145{
7146 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007147
7148 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007149 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7150 copyID, NULL, NULL))
7151 return TRUE;
7152 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007153}
7154
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007155/*
7156 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007157 * Returns TRUE if setting references failed somehow.
7158 */
7159 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007160set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007161{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007162 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007163 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007164 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007165 char_u fname_buf[FLEN_FIXED + 1];
7166 char_u *tofree = NULL;
7167 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007168 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007169
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007170 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007171 return FALSE;
7172
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007173 if (fp_in == NULL)
7174 {
7175 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007176 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007177 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007178 if (fp != NULL)
7179 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007180 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007181 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007182 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007183
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007184 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007185 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007186}
7187
Bram Moolenaare38eab22019-12-05 21:50:01 +01007188#endif // FEAT_EVAL