blob: b328cf58ad7f43438b1e2caac80c75be75e979c1 [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);
Yegappan Lakshmanana04003a2024-10-27 21:54:11 +010037static char_u *trans_function_name_ext(char_u **pp, int *is_global, int skip, int flags, funcdict_T *fdp, partial_T **partial, type_T **type, ufunc_T **ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020038
39 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +000040func_init(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020041{
42 hash_init(&func_hashtab);
43}
44
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020045/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020046 * Return the function hash table
47 */
48 hashtab_T *
49func_tbl_get(void)
50{
51 return &func_hashtab;
52}
53
54/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020055 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020056 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010057 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010058 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaardce24412022-02-08 20:35:30 +000059 * If "eap" is not NULL use it to check for an already declared name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010060 * Return a pointer to after the type.
61 * When something is wrong return "arg".
62 */
63 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010064one_function_arg(
65 char_u *arg,
66 garray_T *newargs,
67 garray_T *argtypes,
68 int types_optional,
h-eastb895b0f2023-09-24 15:46:31 +020069 garray_T *arg_objm,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010070 evalarg_T *evalarg,
Bram Moolenaardce24412022-02-08 20:35:30 +000071 exarg_T *eap,
Bram Moolenaar2a389082021-04-09 20:24:31 +020072 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010073 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074{
Bram Moolenaar6e949782020-04-13 17:21:00 +020075 char_u *p = arg;
76 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020077 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010078
79 while (ASCII_ISALNUM(*p) || *p == '_')
80 ++p;
Keith Thompson184f71c2024-01-04 21:19:04 +010081 if (arg == p || SAFE_isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020082 || (argtypes == NULL
83 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
84 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085 {
86 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000087 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 return arg;
89 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010090
Bram Moolenaarce723f32023-06-10 19:00:12 +010091 // Extra checks in Vim9 script.
92 if (!skip && argtypes != NULL)
93 {
94 int c = *p;
95 *p = NUL;
96 int r = check_reserved_name(arg, FALSE);
97 *p = c;
98 if (r == FAIL)
99 return arg;
100
101 // Cannot use script var name for argument. In function: also check
102 // local vars and arguments.
103 if (check_defined(arg, p - arg,
104 evalarg == NULL ? NULL : evalarg->eval_cctx,
Bram Moolenaardce24412022-02-08 20:35:30 +0000105 eap == NULL ? NULL : eap->cstack, TRUE) == FAIL)
Bram Moolenaarce723f32023-06-10 19:00:12 +0100106 return arg;
107 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100108
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100109 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
110 return arg;
111 if (newargs != NULL)
112 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113 int c;
114 int i;
115
116 c = *p;
117 *p = NUL;
118 arg_copy = vim_strsave(arg);
119 if (arg_copy == NULL)
120 {
121 *p = c;
122 return arg;
123 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200124 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200125 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200126 // Check for duplicate argument name.
127 for (i = 0; i < newargs->ga_len; ++i)
128 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
129 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000130 semsg(_(e_duplicate_argument_name_str), arg_copy);
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200131 vim_free(arg_copy);
132 return arg;
133 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100134 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
135 newargs->ga_len++;
136
137 *p = c;
138 }
139
140 // get any type from "arg: type"
h-eastb895b0f2023-09-24 15:46:31 +0200141 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK)
142 && arg_objm != NULL && (skip || ga_grow(arg_objm, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143 {
144 char_u *type = NULL;
145
Bram Moolenaar6e949782020-04-13 17:21:00 +0200146 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
147 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200148 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200149 arg_copy == NULL ? arg : arg_copy);
150 p = skipwhite(p);
151 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100152 if (*p == ':')
153 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200154 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100155 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200156 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100157 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200158 return arg;
159 }
160 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200161 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100162 if (!skip)
163 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200165 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200166 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200167 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200168 arg_copy == NULL ? arg : arg_copy);
169 return arg;
170 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100171 if (!skip)
172 {
173 if (type == NULL && types_optional)
John Marriottb32800f2025-02-01 15:25:34 +0100174 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100175 // lambda arguments default to "any" type
John Marriottb32800f2025-02-01 15:25:34 +0100176 if (is_vararg)
177 type = vim_strnsave((char_u *)"list<any>", 9);
178 else
179 type = vim_strnsave((char_u *)"any", 3);
180 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100181 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
h-eastb895b0f2023-09-24 15:46:31 +0200182 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = FALSE;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100183 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100184 }
185
186 return p;
187}
188
189/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000190 * Handle line continuation in function arguments or body.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000191 * Get a next line, store it in "eap" if appropriate and put the line in
192 * "lines_to_free" to free the line later.
Bram Moolenaar7473a842021-12-28 17:55:26 +0000193 */
194 static char_u *
195get_function_line(
196 exarg_T *eap,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000197 garray_T *lines_to_free,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000198 int indent,
199 getline_opt_T getline_options)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000200{
201 char_u *theline;
202
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100203 if (eap->ea_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +0000204 theline = getcmdline(':', 0L, indent, 0);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000205 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100206 theline = eap->ea_getline(':', eap->cookie, indent, getline_options);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000207 if (theline != NULL)
208 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000209 if (lines_to_free->ga_len > 0
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000210 && eap->cmdlinep != NULL
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000211 && *eap->cmdlinep == ((char_u **)lines_to_free->ga_data)
212 [lines_to_free->ga_len - 1])
Bram Moolenaar7473a842021-12-28 17:55:26 +0000213 *eap->cmdlinep = theline;
Bram Moolenaarb5820102023-01-25 12:27:13 +0000214 (void)ga_add_string(lines_to_free, theline);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000215 }
216
217 return theline;
218}
219
220/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200221 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100222 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100223 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200224 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100225 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200226get_function_args(
227 char_u **argp,
228 char_u endchar,
229 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100230 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100231 int types_optional, // types optional if "argtypes" is not NULL
h-eastb895b0f2023-09-24 15:46:31 +0200232 garray_T *arg_objm, // NULL unless using :def
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100233 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200234 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200235 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200236 int skip,
Bram Moolenaardce24412022-02-08 20:35:30 +0000237 exarg_T *eap, // can be NULL
Bram Moolenaar554d0312023-01-05 19:59:18 +0000238 int in_class, // non-zero when inside a class or interface
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000239 garray_T *newlines, // function body lines
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000240 garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200241{
242 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100243 char_u *arg;
244 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200245 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200246 int any_default = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100247 char_u *whitep = *argp;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100248 int need_expr = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200249
250 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000251 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000253 ga_init2(argtypes, sizeof(char_u *), 3);
h-eastb895b0f2023-09-24 15:46:31 +0200254 if (arg_objm != NULL)
255 ga_init2(arg_objm, sizeof(int8_T), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200256 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000257 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200258
259 if (varargs != NULL)
260 *varargs = FALSE;
261
262 /*
263 * Isolate the arguments: "arg1, arg2, ...)"
264 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100265 arg = skipwhite(*argp);
266 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200267 while (*p != endchar)
268 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100269 while (eap != NULL && eap->ea_getline != NULL
Bram Moolenaar2c330432020-04-13 14:41:35 +0200270 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200271 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200272 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000273 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000274 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000275
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200276 if (theline == NULL)
277 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200278 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200279 p = skipwhite(theline);
280 }
281
282 if (mustend && *p != endchar)
283 {
284 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000285 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100286 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200287 }
Christian Brabandte4a450a2023-12-08 20:57:38 +0100288 if (*p == endchar && !need_expr)
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200289 break;
290
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200291 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
292 {
293 if (varargs != NULL)
294 *varargs = TRUE;
295 p += 3;
296 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297
298 if (argtypes != NULL)
299 {
300 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200301 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100302 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100303 if (!skip)
304 emsg(_(e_missing_name_after_dots));
305 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100306 }
307
308 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100309 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200310 arg_objm, evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100311 if (p == arg)
312 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100313 if (*skipwhite(p) == '=')
314 {
315 emsg(_(e_cannot_use_default_for_variable_arguments));
316 break;
317 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100318 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200319 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000320 else if (in_class && STRNCMP(p, "this.", 5) == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000321 {
322 // this.memberName
323 p += 5;
324 arg = p;
325 while (ASCII_ISALNUM(*p) || *p == '_')
326 ++p;
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000327 char_u *argend = p;
328
h-eastdb385522023-09-28 22:18:19 +0200329 // object variable this. can be used only in a constructor
330 if (STRNCMP(eap->arg, "new", 3) != 0)
331 {
332 c = *argend;
333 *argend = NUL;
334 semsg(_(e_cannot_use_an_object_variable_except_with_the_new_method_str), arg);
335 *argend = c;
336 break;
337 }
338
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000339 if (*skipwhite(p) == '=')
340 {
341 char_u *defval = skipwhite(skipwhite(p) + 1);
342 if (STRNCMP(defval, "v:none", 6) != 0)
343 {
344 semsg(_(e_constructor_default_value_must_be_vnone_str), p);
345 goto err_ret;
346 }
347 any_default = TRUE;
348 p = defval + 6;
349
350 if (ga_grow(default_args, 1) == FAIL)
351 goto err_ret;
352
John Marriottb32800f2025-02-01 15:25:34 +0100353 char_u *expr = vim_strnsave((char_u *)"v:none", 6);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000354 if (expr == NULL)
355 goto err_ret;
356 ((char_u **)(default_args->ga_data))
357 [default_args->ga_len] = expr;
358 default_args->ga_len++;
359 }
360 else if (any_default)
361 {
362 emsg(_(e_non_default_argument_follows_default_argument));
363 goto err_ret;
364 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000365
366 // TODO: check the argument is indeed a member
367 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
368 return FAIL;
369 if (newargs != NULL)
370 {
371 ((char_u **)(newargs->ga_data))[newargs->ga_len] =
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000372 vim_strnsave(arg, argend - arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000373 newargs->ga_len++;
374
h-eastb895b0f2023-09-24 15:46:31 +0200375 if (argtypes != NULL && ga_grow(argtypes, 1) == OK
376 && arg_objm != NULL && ga_grow(arg_objm, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000377 {
378 // TODO: use the actual type
379 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] =
John Marriottb32800f2025-02-01 15:25:34 +0100380 vim_strnsave((char_u *)"any", 3);
h-eastb895b0f2023-09-24 15:46:31 +0200381 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000382
383 // Add a line to the function body for the assignment.
384 if (ga_grow(newlines, 1) == OK)
385 {
386 // "this.name = name"
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000387 int len = 5 + (argend - arg) + 3 + (argend - arg) + 1;
388 if (any_default)
389 len += 14 + 10;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000390 char_u *assignment = alloc(len);
391 if (assignment != NULL)
392 {
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000393 c = *argend;
394 *argend = NUL;
395 if (any_default)
396 vim_snprintf((char *)assignment, len,
397 "ifargisset %d this.%s = %s",
398 default_args->ga_len - 1, arg, arg);
399 else
400 vim_snprintf((char *)assignment, len,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000401 "this.%s = %s", arg, arg);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000402 *argend = c;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000403 ((char_u **)(newlines->ga_data))[
404 newlines->ga_len++] = assignment;
405 }
406 }
407 }
408 }
409 if (*p == ',')
410 ++p;
411 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200412 else
413 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200414 char_u *np;
415
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200416 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100417 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200418 arg_objm, evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100419 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200420 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200421
Bram Moolenaar015cf102021-06-26 21:52:02 +0200422 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200423 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200424 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200425 if (*np == '=' && np[1] != '=' && np[1] != '~'
426 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200427 {
428 typval_T rettv;
429
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100430 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200431 any_default = TRUE;
Bram Moolenaareb5adf12022-09-04 13:41:37 +0100432 p = skipwhite(np + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000433 char_u *expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200434 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200435 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200436 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200437 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200438 if (ga_grow(default_args, 1) == FAIL)
439 goto err_ret;
440
Christian Brabandte4a450a2023-12-08 20:57:38 +0100441 if (need_expr)
442 need_expr = FALSE;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200443 // trim trailing whitespace
444 while (p > expr && VIM_ISWHITE(p[-1]))
445 p--;
446 c = *p;
447 *p = NUL;
448 expr = vim_strsave(expr);
449 if (expr == NULL)
450 {
451 *p = c;
452 goto err_ret;
453 }
454 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200455 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200456 default_args->ga_len++;
457 *p = c;
458 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200459 }
460 else
Christian Brabandte4a450a2023-12-08 20:57:38 +0100461 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200462 mustend = TRUE;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100463 if (*skipwhite(p) == NUL)
464 need_expr = TRUE;
465 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200466 }
467 else if (any_default)
468 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000469 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200470 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200471 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200472
473 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
474 {
475 // Be tolerant when skipping
476 if (!skip)
477 {
478 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
479 goto err_ret;
480 }
481 p = skipwhite(p);
482 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200483 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200484 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200485 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200486 // Don't give this error when skipping, it makes the "->" not
487 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100488 // Allow missing space after comma in legacy functions.
489 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200490 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
491 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100492 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200493 goto err_ret;
494 }
495 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200496 else
497 mustend = TRUE;
498 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200499 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200500 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200501 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200502
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200503 if (*p != endchar)
504 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100505 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200506
507 *argp = p;
508 return OK;
509
510err_ret:
511 if (newargs != NULL)
512 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200513 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200514 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200515 return FAIL;
516}
517
518/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100519 * Parse the argument types, filling "fp->uf_arg_types".
520 * Return OK or FAIL.
521 */
522 static int
h-eastb895b0f2023-09-24 15:46:31 +0200523parse_argument_types(
524 ufunc_T *fp,
525 garray_T *argtypes,
526 int varargs,
527 garray_T *arg_objm,
528 ocmember_T *obj_members,
529 int obj_member_count)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100530{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200531 int len = 0;
532
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100533 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
534 if (argtypes->ga_len > 0)
535 {
536 // When "varargs" is set the last name/type goes into uf_va_name
537 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200538 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100539
540 if (len > 0)
541 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
542 if (fp->uf_arg_types != NULL)
543 {
544 int i;
545 type_T *type;
546
547 for (i = 0; i < len; ++ i)
548 {
549 char_u *p = ((char_u **)argtypes->ga_data)[i];
550
551 if (p == NULL)
552 // will get the type from the default value
553 type = &t_unknown;
554 else
h-eastb895b0f2023-09-24 15:46:31 +0200555 {
556 if (arg_objm != NULL && ((int8_T *)arg_objm->ga_data)[i])
557 {
558 char_u *aname = ((char_u **)fp->uf_args.ga_data)[i];
559
560 type = &t_any;
561 for (int om = 0; om < obj_member_count; ++om)
562 {
Christian Brabandt915f3bf2024-04-05 20:12:19 +0200563 if (obj_members != NULL
564 && STRCMP(aname,
565 obj_members[om].ocm_name) == 0)
h-eastb895b0f2023-09-24 15:46:31 +0200566 {
567 type = obj_members[om].ocm_type;
568 break;
569 }
570 }
571 }
572 else
573 type = parse_type(&p, &fp->uf_type_list, TRUE);
574 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100575 if (type == NULL)
576 return FAIL;
577 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000578 if (i < fp->uf_args.ga_len
579 && (type->tt_type == VAR_FUNC
Yegappan Lakshmanan3e336502024-04-04 19:35:59 +0200580 || type->tt_type == VAR_PARTIAL))
581 {
582 char_u *name = ((char_u **)fp->uf_args.ga_data)[i];
583 if (obj_members != NULL && *name == '_')
584 // protected object method
585 name++;
586
587 if (var_wrong_func_name(name, TRUE))
588 return FAIL;
589 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100590 }
591 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100592 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200593
594 if (varargs)
595 {
596 char_u *p;
597
598 // Move the last argument "...name: type" to uf_va_name and
599 // uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200600 --fp->uf_args.ga_len;
Bram Moolenaarf0571712023-01-04 13:16:20 +0000601 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len];
602 ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len] = NULL;
Bram Moolenaar2a389082021-04-09 20:24:31 +0200603 p = ((char_u **)argtypes->ga_data)[len];
604 if (p == NULL)
605 // TODO: get type from default value
606 fp->uf_va_type = &t_list_any;
607 else
608 {
609 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
610 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
611 {
612 semsg(_(e_variable_arguments_type_must_be_list_str),
613 ((char_u **)argtypes->ga_data)[len]);
614 return FAIL;
615 }
616 }
617 if (fp->uf_va_type == NULL)
618 return FAIL;
619 }
620
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100621 return OK;
622}
623
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100624 static int
625parse_return_type(ufunc_T *fp, char_u *ret_type)
626{
627 if (ret_type == NULL)
628 fp->uf_ret_type = &t_void;
629 else
630 {
631 char_u *p = ret_type;
632
633 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
634 if (fp->uf_ret_type == NULL)
635 {
636 fp->uf_ret_type = &t_void;
637 return FAIL;
638 }
639 }
640 return OK;
641}
642
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100643/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200644 * Register function "fp" as using "current_funccal" as its scope.
645 */
646 static int
647register_closure(ufunc_T *fp)
648{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200649 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100650 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200651 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200652 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200653 fp->uf_scoped = current_funccal;
654 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200655
Bram Moolenaarca16c602022-09-06 18:57:08 +0100656 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200657 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100658 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
659 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200660 return OK;
661}
662
663/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100664 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
665 * return "buf" filled with a readable function name.
666 * Otherwise just return "name", thus the return value can always be used.
667 * "name" and "buf" may be equal.
668 */
669 char_u *
670make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
671{
672 size_t len;
673
674 if (name[0] != K_SPECIAL)
675 return name;
676 len = STRLEN(name);
677 if (len + 3 > bufsize)
678 return name;
679
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100680 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100681 mch_memmove(buf, "<SNR>", 5);
682 return buf;
683}
684
John Marriottb32800f2025-02-01 15:25:34 +0100685static char_u lambda_name[8 + NUMBUFLEN];
John Marriottb32800f2025-02-01 15:25:34 +0100686
zeertzjq21012302025-02-02 08:55:57 +0100687/*
688 * Get a name for a lambda. Returned in static memory.
689 */
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +0100690 string_T
Bram Moolenaar04b12692020-05-04 23:24:44 +0200691get_lambda_name(void)
692{
John Marriottb32800f2025-02-01 15:25:34 +0100693 static int lambda_no = 0;
694 int n;
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +0100695 string_T ret;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200696
John Marriottb32800f2025-02-01 15:25:34 +0100697 n = vim_snprintf((char *)lambda_name, sizeof(lambda_name), "<lambda>%d", ++lambda_no);
698 if (n < 1)
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +0100699 ret.length = 0;
John Marriottb32800f2025-02-01 15:25:34 +0100700 else
701 if (n >= (int)sizeof(lambda_name))
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +0100702 ret.length = sizeof(lambda_name) - 1;
John Marriottb32800f2025-02-01 15:25:34 +0100703 else
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +0100704 ret.length = (size_t)n;
John Marriottb32800f2025-02-01 15:25:34 +0100705
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +0100706 ret.string = lambda_name;
707 return ret;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200708}
709
Bram Moolenaare01e5212023-01-08 20:31:18 +0000710/*
711 * Allocate a "ufunc_T" for a function called "name".
712 * Makes sure the size is right.
713 */
714 static ufunc_T *
John Marriottb32800f2025-02-01 15:25:34 +0100715alloc_ufunc(char_u *name, size_t namelen)
Bram Moolenaare01e5212023-01-08 20:31:18 +0000716{
John Marriottb32800f2025-02-01 15:25:34 +0100717 size_t len;
718 ufunc_T *fp;
719
Bram Moolenaare01e5212023-01-08 20:31:18 +0000720 // When the name is short we need to make sure we allocate enough bytes for
721 // the whole struct, including any padding.
John Marriottb32800f2025-02-01 15:25:34 +0100722 len = offsetof(ufunc_T, uf_name) + namelen + 1;
723 fp = alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
724 if (fp != NULL)
725 {
726 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
727 // can actually extend beyond the struct.
728 STRCPY((void *)fp->uf_name, name);
729 fp->uf_namelen = namelen;
730
731 if (name[0] == K_SPECIAL)
732 {
733 len = namelen + 3; // including +1 for NUL
734 fp->uf_name_exp = alloc(len);
735 if (fp->uf_name_exp != NULL)
736 vim_snprintf((char *)fp->uf_name_exp, len, "<SNR>%s", fp->uf_name + 3);
737 }
738 }
739
740 return fp;
Bram Moolenaare01e5212023-01-08 20:31:18 +0000741}
742
Bram Moolenaar801ab062020-06-25 19:27:56 +0200743#if defined(FEAT_LUA) || defined(PROTO)
744/*
745 * Registers a native C callback which can be called from Vim script.
746 * Returns the name of the Vim script function.
747 */
748 char_u *
749register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
750{
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +0100751 string_T name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200752 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200753
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +0100754 fp = alloc_ufunc(name.string, name.length);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200755 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200756 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200757
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200758 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200759 fp->uf_refcount = 1;
760 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000761 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200762 fp->uf_calls = 0;
763 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200764 fp->uf_cb = cb;
765 fp->uf_cb_free = cb_free;
766 fp->uf_cb_state = state;
767
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000768 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200769
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +0100770 return name.string;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200771}
772#endif
773
Bram Moolenaar04b12692020-05-04 23:24:44 +0200774/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100775 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100776 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100777 * If "white_error" is not NULL check for correct use of white space and set
778 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100779 * Return NULL if no valid arrow found.
780 */
781 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100782skip_arrow(
783 char_u *start,
784 int equal_arrow,
785 char_u **ret_type,
786 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100787{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100788 char_u *s = start;
789 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100790
791 if (equal_arrow)
792 {
793 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100794 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100795 if (white_error != NULL && !VIM_ISWHITE(s[1]))
796 {
797 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100798 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100799 return NULL;
800 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100801 s = skipwhite(s + 1);
802 *ret_type = s;
803 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100804 if (s == *ret_type)
805 {
806 emsg(_(e_missing_return_type));
807 return NULL;
808 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100809 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100810 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100811 s = skipwhite(s);
812 if (*s != '=')
813 return NULL;
814 ++s;
815 }
816 if (*s != '>')
817 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100818 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
819 || !IS_WHITE_OR_NUL(s[1])))
820 {
821 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100822 semsg(_(e_white_space_required_before_and_after_str_at_str),
823 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100824 return NULL;
825 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100826 return skipwhite(s + 1);
827}
828
829/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100830 * Check if "*cmd" points to a function command and if so advance "*cmd" and
831 * return TRUE.
832 * Otherwise return FALSE;
833 * Do not consider "function(" to be a command.
834 */
835 static int
836is_function_cmd(char_u **cmd)
837{
838 char_u *p = *cmd;
839
840 if (checkforcmd(&p, "function", 2))
841 {
842 if (*p == '(')
843 return FALSE;
844 *cmd = p;
845 return TRUE;
846 }
847 return FALSE;
848}
849
850/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200851 * Called when defining a function: The context may be needed for script
852 * variables declared in a block that is visible now but not when the function
853 * is compiled or called later.
854 */
855 static void
856function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
857{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000858 if (cstack == NULL || cstack->cs_idx < 0)
859 return;
860
861 int count = cstack->cs_idx + 1;
862 int i;
863
864 fp->uf_block_ids = ALLOC_MULT(int, count);
865 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200866 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000867 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
868 sizeof(int) * count);
869 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200870 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000871
872 // Set flag in each block to indicate a function was defined. This
873 // is used to keep the variable when leaving the block, see
874 // hide_script_var().
875 for (i = 0; i <= cstack->cs_idx; ++i)
876 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200877}
878
879/*
Yegappan Lakshmanan0072cee2025-01-07 20:22:32 +0100880 * Skip over all the characters in a single quoted string starting at "p" and
881 * return a pointer to the character following the ending single quote.
882 * If the ending single quote is missing, then return a pointer to the NUL
883 * character.
884 */
885 static char_u *
886skip_single_quote_string(char_u *p)
887{
888 p++; // skip the beginning single quote
889 while (*p != NUL)
890 {
891 // Within the string, a single quote can be escaped by using
892 // two single quotes.
893 if (*p == '\'' && *(p + 1) == '\'')
894 p += 2;
895 else if (*p == '\'')
896 {
897 p++; // skip the ending single quote
898 break;
899 }
900 else
901 MB_PTR_ADV(p);
902 }
903
904 return p;
905}
906
907/*
908 * Skip over all the characters in a double quoted string starting at "p" and
909 * return a pointer to the character following the ending double quote.
910 * If the ending double quote is missing, then return a pointer to the NUL
911 * character.
912 */
913 static char_u *
914skip_double_quote_string(char_u *p)
915{
916 p++; // skip the beginning double quote
917 while (*p != NUL)
918 {
919 // Within the string, a double quote can be escaped by
920 // preceding it with a backslash.
921 if (*p == '\\' && *(p + 1) == '"')
922 p += 2;
923 else if (*p == '"')
924 {
925 p++; // skip the ending double quote
926 break;
927 }
928 else
929 MB_PTR_ADV(p);
930 }
931
932 return p;
933}
934
935/*
936 * Return the start of a Vim9 comment (#) in the line starting at "line".
937 * If a comment is not found, then returns a pointer to the end of the
938 * string (NUL).
939 */
940 static char_u *
941find_start_of_vim9_comment(char_u *line)
942{
943 char_u *p = line;
944
945 while (*p != NUL)
946 {
947 if (*p == '\'')
948 // Skip a single quoted string.
949 p = skip_single_quote_string(p);
950 else if (*p == '"')
951 // Skip a double quoted string.
952 p = skip_double_quote_string(p);
953 else
954 {
955 if (*p == '#')
956 // Found the start of a Vim9 comment
957 break;
958 MB_PTR_ADV(p);
959 }
960 }
961
962 return p;
963}
964
965/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100966 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200967 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100968 * "newlines" must already have been initialized.
969 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
970 */
971 static int
972get_function_body(
973 exarg_T *eap,
974 garray_T *newlines,
975 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000976 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100977{
978 linenr_T sourcing_lnum_top = SOURCING_LNUM;
979 linenr_T sourcing_lnum_off;
980 int saved_wait_return = need_wait_return;
981 char_u *line_arg = line_arg_in;
982 int vim9_function = eap->cmdidx == CMD_def
983 || eap->cmdidx == CMD_block;
984#define MAX_FUNC_NESTING 50
985 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200986 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100987 int nesting = 0;
988 getline_opt_T getline_options;
989 int indent = 2;
990 char_u *skip_until = NULL;
991 int ret = FAIL;
992 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200993 int heredoc_concat_len = 0;
994 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100995 char_u *heredoc_trimmed = NULL;
John Marriottb32800f2025-02-01 15:25:34 +0100996 size_t heredoc_trimmedlen = 0;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100997
Bram Moolenaar20677332021-06-06 17:02:53 +0200998 ga_init2(&heredoc_ga, 1, 500);
999
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001000 // Detect having skipped over comment lines to find the return
1001 // type. Add NULL lines to keep the line count correct.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001002 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001003 if (SOURCING_LNUM < sourcing_lnum_off)
1004 {
1005 sourcing_lnum_off -= SOURCING_LNUM;
1006 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
1007 goto theend;
1008 while (sourcing_lnum_off-- > 0)
1009 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1010 }
1011
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001012 nesting_def[0] = vim9_function;
1013 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001014 getline_options = vim9_function
1015 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
1016 for (;;)
1017 {
1018 char_u *theline;
1019 char_u *p;
1020 char_u *arg;
1021
1022 if (KeyTyped)
1023 {
1024 msg_scroll = TRUE;
1025 saved_wait_return = FALSE;
1026 }
1027 need_wait_return = FALSE;
1028
1029 if (line_arg != NULL)
1030 {
1031 // Use eap->arg, split up in parts by line breaks.
1032 theline = line_arg;
1033 p = vim_strchr(theline, '\n');
1034 if (p == NULL)
1035 line_arg += STRLEN(line_arg);
1036 else
1037 {
1038 *p = NUL;
1039 line_arg = p + 1;
1040 }
1041 }
1042 else
1043 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001044 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001045 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001046 }
1047 if (KeyTyped)
1048 lines_left = Rows - 1;
1049 if (theline == NULL)
1050 {
1051 // Use the start of the function for the line number.
1052 SOURCING_LNUM = sourcing_lnum_top;
1053 if (skip_until != NULL)
1054 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001055 else if (nesting_inline[nesting])
1056 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001057 else if (eap->cmdidx == CMD_def)
1058 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001059 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001060 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001061 goto theend;
1062 }
1063
1064 // Detect line continuation: SOURCING_LNUM increased more than one.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001065 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001066 if (SOURCING_LNUM < sourcing_lnum_off)
1067 sourcing_lnum_off -= SOURCING_LNUM;
1068 else
1069 sourcing_lnum_off = 0;
1070
1071 if (skip_until != NULL)
1072 {
1073 // Don't check for ":endfunc"/":enddef" between
1074 // * ":append" and "."
1075 // * ":python <<EOF" and "EOF"
1076 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
1077 if (heredoc_trimmed == NULL
1078 || (is_heredoc && skipwhite(theline) == theline)
1079 || STRNCMP(theline, heredoc_trimmed,
John Marriottb32800f2025-02-01 15:25:34 +01001080 heredoc_trimmedlen) == 0)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001081 {
1082 if (heredoc_trimmed == NULL)
1083 p = theline;
1084 else if (is_heredoc)
1085 p = skipwhite(theline) == theline
John Marriottb32800f2025-02-01 15:25:34 +01001086 ? theline : theline + heredoc_trimmedlen;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001087 else
John Marriottb32800f2025-02-01 15:25:34 +01001088 p = theline + heredoc_trimmedlen;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001089 if (STRCMP(p, skip_until) == 0)
1090 {
1091 VIM_CLEAR(skip_until);
1092 VIM_CLEAR(heredoc_trimmed);
John Marriottb32800f2025-02-01 15:25:34 +01001093 heredoc_trimmedlen = 0;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001094 getline_options = vim9_function
1095 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
1096 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +02001097
1098 if (heredoc_concat_len > 0)
1099 {
1100 // Replace the starting line with all the concatenated
1101 // lines.
1102 ga_concat(&heredoc_ga, theline);
1103 vim_free(((char_u **)(newlines->ga_data))[
1104 heredoc_concat_len - 1]);
1105 ((char_u **)(newlines->ga_data))[
1106 heredoc_concat_len - 1] = heredoc_ga.ga_data;
1107 ga_init(&heredoc_ga);
1108 heredoc_concat_len = 0;
1109 theline += STRLEN(theline); // skip the "EOF"
1110 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001111 }
1112 }
1113 }
1114 else
1115 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001116 int c;
1117 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +00001118 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001119
1120 // skip ':' and blanks
1121 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
1122 ;
1123
1124 // Check for "endfunction", "enddef" or "}".
1125 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +00001126 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001127 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001128 ? *p == '}'
1129 : (checkforcmd(&p, nesting_def[nesting]
1130 ? "enddef" : "endfunction", 4)
1131 && *p != ':'))
1132 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00001133 if (!nesting_inline[nesting] && nesting_def[nesting]
1134 && p < cmd + 6)
1135 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001136 if (nesting-- == 0)
1137 {
1138 char_u *nextcmd = NULL;
1139
1140 if (*p == '|' || *p == '}')
1141 nextcmd = p + 1;
1142 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
1143 nextcmd = line_arg;
1144 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001145 && (vim9_function || p_verbose > 0))
1146 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +02001147 SOURCING_LNUM = sourcing_lnum_top
1148 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001149 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +00001150 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001151 else
1152 give_warning2((char_u *)
1153 _("W22: Text found after :endfunction: %s"),
1154 p, TRUE);
1155 }
1156 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001157 {
1158 // Another command follows. If the line came from "eap"
1159 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001160 // change "eap->cmdlinep" to point to the last fetched
1161 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001162 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001163 if (lines_to_free->ga_len > 0
1164 && *eap->cmdlinep !=
1165 ((char_u **)lines_to_free->ga_data)
1166 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001167 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001168 // *cmdlinep will be freed later, thus remove the
1169 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001170 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001171 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
1172 [lines_to_free->ga_len - 1];
1173 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001174 }
1175 }
1176 break;
1177 }
1178 }
1179
1180 // Check for mismatched "endfunc" or "enddef".
1181 // We don't check for "def" inside "func" thus we also can't check
1182 // for "enddef".
1183 // We continue to find the end of the function, although we might
1184 // not find it.
1185 else if (nesting_def[nesting])
1186 {
1187 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1188 emsg(_(e_mismatched_endfunction));
1189 }
1190 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1191 emsg(_(e_mismatched_enddef));
1192
1193 // Increase indent inside "if", "while", "for" and "try", decrease
1194 // at "end".
1195 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1196 indent -= 2;
1197 else if (STRNCMP(p, "if", 2) == 0
1198 || STRNCMP(p, "wh", 2) == 0
1199 || STRNCMP(p, "for", 3) == 0
1200 || STRNCMP(p, "try", 3) == 0)
1201 indent += 2;
1202
1203 // Check for defining a function inside this function.
1204 // Only recognize "def" inside "def", not inside "function",
1205 // For backwards compatibility, see Test_function_python().
1206 c = *p;
1207 if (is_function_cmd(&p)
1208 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1209 {
1210 if (*p == '!')
1211 p = skipwhite(p + 1);
1212 p += eval_fname_script(p);
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001213 vim_free(trans_function_name(&p, NULL, TRUE, 0));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001214 if (*skipwhite(p) == '(')
1215 {
1216 if (nesting == MAX_FUNC_NESTING - 1)
1217 emsg(_(e_function_nesting_too_deep));
1218 else
1219 {
1220 ++nesting;
1221 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001222 nesting_inline[nesting] = FALSE;
1223 indent += 2;
1224 }
1225 }
1226 }
1227
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001228 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001229 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001230 // Not a comment line: check for nested inline function.
Yegappan Lakshmanan0072cee2025-01-07 20:22:32 +01001231
1232 if (nesting_inline[nesting])
1233 {
1234 // A comment (#) can follow the opening curly brace of a
1235 // block statement. Need to ignore the comment and look
1236 // for the opening curly brace before the comment.
1237 end = find_start_of_vim9_comment(p) - 1;
1238 }
1239 else
1240 end = p + STRLEN(p) - 1;
1241
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001242 while (end > p && VIM_ISWHITE(*end))
1243 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001244 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001245 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001246 int is_block;
1247
1248 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001249 --end;
1250 while (end > p && VIM_ISWHITE(*end))
1251 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001252 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1253 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001254 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001255 char_u *s = p;
1256
1257 // check for line starting with "au" for :autocmd or
1258 // "com" for :command, these can use a {} block
1259 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1260 || checkforcmd_noparen(&s, "command", 3);
1261 }
1262
1263 if (is_block)
1264 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001265 if (nesting == MAX_FUNC_NESTING - 1)
1266 emsg(_(e_function_nesting_too_deep));
1267 else
1268 {
1269 ++nesting;
1270 nesting_def[nesting] = TRUE;
1271 nesting_inline[nesting] = TRUE;
1272 indent += 2;
1273 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001274 }
1275 }
1276 }
1277
1278 // Check for ":append", ":change", ":insert". Not for :def.
1279 p = skip_range(p, FALSE, NULL);
1280 if (!vim9_function
1281 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1282 || (p[0] == 'c'
1283 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1284 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1285 && (STRNCMP(&p[3], "nge", 3) != 0
1286 || !ASCII_ISALPHA(p[6])))))))
1287 || (p[0] == 'i'
1288 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1289 && (!ASCII_ISALPHA(p[2])
1290 || (p[2] == 's'
1291 && (!ASCII_ISALPHA(p[3])
1292 || p[3] == 'e'))))))))
John Marriottb32800f2025-02-01 15:25:34 +01001293 skip_until = vim_strnsave((char_u *)".", 1);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001294
1295 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1296 arg = skipwhite(skiptowhite(p));
1297 if (arg[0] == '<' && arg[1] =='<'
1298 && ((p[0] == 'p' && p[1] == 'y'
1299 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1300 || ((p[2] == '3' || p[2] == 'x')
1301 && !ASCII_ISALPHA(p[3]))))
1302 || (p[0] == 'p' && p[1] == 'e'
1303 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1304 || (p[0] == 't' && p[1] == 'c'
1305 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1306 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1307 && !ASCII_ISALPHA(p[3]))
1308 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1309 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1310 || (p[0] == 'm' && p[1] == 'z'
1311 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1312 ))
1313 {
1314 // ":python <<" continues until a dot, like ":append"
1315 p = skipwhite(arg + 2);
zeertzjq449c2e52025-02-03 18:56:16 +01001316 if (STRNCMP(p, "trim", 4) == 0
1317 && (p[4] == NUL || VIM_ISWHITE(p[4])))
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001318 {
1319 // Ignore leading white space.
1320 p = skipwhite(p + 4);
John Marriottb32800f2025-02-01 15:25:34 +01001321 heredoc_trimmedlen = skipwhite(theline) - theline;
1322 heredoc_trimmed = vim_strnsave(theline, heredoc_trimmedlen);
1323 if (heredoc_trimmed == NULL)
1324 heredoc_trimmedlen = 0;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001325 }
1326 if (*p == NUL)
John Marriottb32800f2025-02-01 15:25:34 +01001327 skip_until = vim_strnsave((char_u *)".", 1);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001328 else
1329 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1330 getline_options = GETLINE_NONE;
1331 is_heredoc = TRUE;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001332 if (vim9_function && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001333 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001334 }
1335
Bram Moolenaard881d152022-05-13 13:50:36 +01001336 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001337 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001338 // Check for ":cmd v =<< [trim] EOF"
1339 // and ":cmd [a, b] =<< [trim] EOF"
1340 // and "lines =<< [trim] EOF" for Vim9
1341 // Where "cmd" can be "let", "var", "final" or "const".
zeertzjqa93d9cd2023-05-02 16:25:47 +01001342 arg = p;
1343 if (checkforcmd(&arg, "let", 2)
1344 || checkforcmd(&arg, "var", 3)
1345 || checkforcmd(&arg, "final", 5)
1346 || checkforcmd(&arg, "const", 5)
1347 || vim9_function)
Bram Moolenaard881d152022-05-13 13:50:36 +01001348 {
zeertzjq9a91d2b2024-04-09 21:47:10 +02001349 int save_sc_version = current_sctx.sc_version;
1350 int var_count = 0;
1351 int semicolon = 0;
zeertzjq9a91d2b2024-04-09 21:47:10 +02001352
1353 current_sctx.sc_version
1354 = vim9_function ? SCRIPT_VERSION_VIM9 : 1;
zeertzjq1817ccd2024-04-10 17:37:47 +02001355 arg = skip_var_list(arg, TRUE, &var_count, &semicolon,
zeertzjq9a91d2b2024-04-09 21:47:10 +02001356 TRUE);
zeertzjq1817ccd2024-04-10 17:37:47 +02001357 if (arg != NULL)
1358 arg = skipwhite(arg);
zeertzjq9a91d2b2024-04-09 21:47:10 +02001359 current_sctx.sc_version = save_sc_version;
zeertzjq1817ccd2024-04-10 17:37:47 +02001360 if (arg != NULL && STRNCMP(arg, "=<<", 3) == 0)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001361 {
zeertzjq449c2e52025-02-03 18:56:16 +01001362 int has_trim = FALSE;
1363
Bram Moolenaard881d152022-05-13 13:50:36 +01001364 p = skipwhite(arg + 3);
1365 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001366 {
zeertzjq449c2e52025-02-03 18:56:16 +01001367 if (STRNCMP(p, "trim", 4) == 0
1368 && (p[4] == NUL || VIM_ISWHITE(p[4])))
Bram Moolenaard881d152022-05-13 13:50:36 +01001369 {
1370 // Ignore leading white space.
1371 p = skipwhite(p + 4);
zeertzjq449c2e52025-02-03 18:56:16 +01001372 has_trim = TRUE;
Bram Moolenaard881d152022-05-13 13:50:36 +01001373 continue;
1374 }
zeertzjq449c2e52025-02-03 18:56:16 +01001375 if (STRNCMP(p, "eval", 4) == 0
1376 && (p[4] == NUL || VIM_ISWHITE(p[4])))
Bram Moolenaard881d152022-05-13 13:50:36 +01001377 {
1378 // Ignore leading white space.
1379 p = skipwhite(p + 4);
1380 continue;
1381 }
1382 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001383 }
zeertzjq449c2e52025-02-03 18:56:16 +01001384 if (has_trim)
1385 {
1386 heredoc_trimmedlen = skipwhite(theline) - theline;
1387 heredoc_trimmed = vim_strnsave(theline, heredoc_trimmedlen);
1388 if (heredoc_trimmed == NULL)
1389 heredoc_trimmedlen = 0;
1390 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001391 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1392 getline_options = GETLINE_NONE;
1393 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001394 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001395 }
1396 }
1397 }
1398
1399 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001400 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001401 goto theend;
1402
Bram Moolenaar20677332021-06-06 17:02:53 +02001403 if (heredoc_concat_len > 0)
1404 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001405 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001406 // to be used for the instruction later.
1407 ga_concat(&heredoc_ga, theline);
1408 ga_concat(&heredoc_ga, (char_u *)"\n");
John Marriottb32800f2025-02-01 15:25:34 +01001409 p = vim_strnsave((char_u *)"", 0);
Bram Moolenaar20677332021-06-06 17:02:53 +02001410 }
1411 else
1412 {
1413 // Copy the line to newly allocated memory. get_one_sourceline()
1414 // allocates 250 bytes per line, this saves 80% on average. The
1415 // cost is an extra alloc/free.
1416 p = vim_strsave(theline);
1417 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001418 if (p == NULL)
1419 goto theend;
1420 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1421
1422 // Add NULL lines for continuation lines, so that the line count is
1423 // equal to the index in the growarray.
1424 while (sourcing_lnum_off-- > 0)
1425 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1426
1427 // Check for end of eap->arg.
1428 if (line_arg != NULL && *line_arg == NUL)
1429 line_arg = NULL;
1430 }
1431
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001432 // Return OK when no error was detected.
1433 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001434 ret = OK;
1435
1436theend:
1437 vim_free(skip_until);
1438 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001439 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001440 need_wait_return |= saved_wait_return;
1441 return ret;
1442}
1443
1444/*
1445 * Handle the body of a lambda. *arg points to the "{", process statements
1446 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001447 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001448 * When successful "rettv" is set to a funcref.
1449 */
1450 static int
1451lambda_function_body(
1452 char_u **arg,
1453 typval_T *rettv,
1454 evalarg_T *evalarg,
1455 garray_T *newargs,
1456 garray_T *argtypes,
1457 int varargs,
1458 garray_T *default_args,
1459 char_u *ret_type)
1460{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001461 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001462 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001463 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001464 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001465 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001466 exarg_T eap;
1467 garray_T newlines;
1468 char_u *cmdline = NULL;
1469 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001470 partial_T *pt;
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001471 string_T name;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001472 int lnum_save = -1;
1473 linenr_T sourcing_lnum_top = SOURCING_LNUM;
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001474 char_u *line_arg = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001475
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001476 *arg = skipwhite(*arg + 1);
1477 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001478 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001479 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001480 return FAIL;
1481 }
1482
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001483 // When there is a line break use what follows for the lambda body.
1484 // Makes lambda body initializers work for object and enum member
1485 // variables.
1486 if (**arg == '\n')
1487 line_arg = *arg + 1;
1488
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001489 CLEAR_FIELD(eap);
1490 eap.cmdidx = CMD_block;
1491 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001492 eap.cmdlinep = &cmdline;
1493 eap.skip = !evaluate;
1494 if (evalarg->eval_cctx != NULL)
1495 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1496 else
1497 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001498 eap.ea_getline = evalarg->eval_getline;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001499 eap.cookie = evalarg->eval_cookie;
1500 }
1501
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001502 ga_init2(&newlines, sizeof(char_u *), 10);
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001503 if (get_function_body(&eap, &newlines, line_arg,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001504 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001505 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001506
1507 // When inside a lambda must add the function lines to evalarg.eval_ga.
1508 evalarg->eval_break_count += newlines.ga_len;
1509 if (gap->ga_itemsize > 0)
1510 {
1511 int idx;
1512 char_u *last;
1513 size_t plen;
1514 char_u *pnl;
1515
1516 for (idx = 0; idx < newlines.ga_len; ++idx)
1517 {
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001518 char_u *p = ((char_u **)newlines.ga_data)[idx];
1519 if (p == NULL)
1520 // comment line in the lambda body
1521 continue;
1522
1523 p = skipwhite(p);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001524
Bram Moolenaarecb66452021-05-18 15:09:18 +02001525 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001526 goto erret;
1527
1528 // Going to concatenate the lines after parsing. For an empty or
1529 // comment line use an empty string.
1530 // Insert NL characters at the start of each line, the string will
1531 // be split again later in .get_lambda_tv().
1532 if (*p == NUL || vim9_comment_start(p))
John Marriottb32800f2025-02-01 15:25:34 +01001533 {
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001534 p = (char_u *)"";
John Marriottb32800f2025-02-01 15:25:34 +01001535 plen = 0;
1536 }
1537 else
1538 plen = STRLEN(p);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001539 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1540 if (pnl != NULL)
1541 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001542 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1543 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001544 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001545 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001546 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001547 if (eap.nextcmd != NULL)
John Marriottb32800f2025-02-01 15:25:34 +01001548 {
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001549 // more is following after the "}", which was skipped
1550 last = cmdline;
John Marriottb32800f2025-02-01 15:25:34 +01001551 plen = STRLEN(last);
1552 }
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001553 else
John Marriottb32800f2025-02-01 15:25:34 +01001554 {
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001555 // nothing is following the "}"
1556 last = (char_u *)"}";
John Marriottb32800f2025-02-01 15:25:34 +01001557 plen = 1;
1558 }
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001559 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1560 if (pnl != NULL)
1561 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001562 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1563 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001564 }
1565
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001566 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001567 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001568 garray_T *tfgap = &evalarg->eval_tofree_ga;
1569
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001570 // Something comes after the "}".
1571 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001572
1573 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001574 if (ga_grow(tfgap, 1) == OK)
1575 {
1576 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1577 evalarg->eval_using_cmdline = TRUE;
1578 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001579 }
1580 else
1581 *arg = (char_u *)"";
1582
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001583 if (!evaluate)
1584 {
1585 ret = OK;
1586 goto erret;
1587 }
1588
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001589 name = get_lambda_name();
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001590 ufunc = alloc_ufunc(name.string, name.length);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001591 if (ufunc == NULL)
1592 goto erret;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001593 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001594 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001595 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001596 ufunc->uf_refcount = 1;
1597 ufunc->uf_args = *newargs;
1598 newargs->ga_data = NULL;
1599 ufunc->uf_def_args = *default_args;
1600 default_args->ga_data = NULL;
1601 ufunc->uf_func_type = &t_func_any;
1602
1603 // error messages are for the first function line
1604 lnum_save = SOURCING_LNUM;
1605 SOURCING_LNUM = sourcing_lnum_top;
1606
1607 // parse argument types
h-eastb895b0f2023-09-24 15:46:31 +02001608 if (parse_argument_types(ufunc, argtypes, varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001609 {
1610 SOURCING_LNUM = lnum_save;
1611 goto erret;
1612 }
1613
1614 // parse the return type, if any
1615 if (parse_return_type(ufunc, ret_type) == FAIL)
1616 goto erret;
1617
1618 pt = ALLOC_CLEAR_ONE(partial_T);
1619 if (pt == NULL)
1620 goto erret;
1621 pt->pt_func = ufunc;
1622 pt->pt_refcount = 1;
1623
1624 ufunc->uf_lines = newlines;
1625 newlines.ga_data = NULL;
1626 if (sandbox)
1627 ufunc->uf_flags |= FC_SANDBOX;
1628 if (!ASCII_ISUPPER(*ufunc->uf_name))
1629 ufunc->uf_flags |= FC_VIM9;
1630 ufunc->uf_script_ctx = current_sctx;
1631 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1632 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1633 set_function_type(ufunc);
1634
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001635 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1636
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001637 rettv->vval.v_partial = pt;
1638 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001639 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001640 ret = OK;
1641
1642erret:
1643 if (lnum_save >= 0)
1644 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001645 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001646 if (newargs != NULL)
1647 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001648 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001649 if (ufunc != NULL)
1650 {
1651 func_clear(ufunc, TRUE);
1652 func_free(ufunc, TRUE);
1653 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001654 return ret;
1655}
1656
1657/*
1658 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001659 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001660 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001661 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1662 */
1663 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001664get_lambda_tv(
1665 char_u **arg,
1666 typval_T *rettv,
1667 int types_optional,
1668 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001669{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001670 int evaluate = evalarg != NULL
1671 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001672 garray_T newargs;
1673 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001674 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001675 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001676 garray_T default_args;
h-eastb895b0f2023-09-24 15:46:31 +02001677 garray_T arg_objm;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001678 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001679 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001680 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001681 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001682 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001683 char_u *s;
1684 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001685 int *old_eval_lavars = eval_lavars_used;
1686 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001687 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001688 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001689 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001690 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001691 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001692 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001693
Bram Moolenaar4525a572022-02-13 11:57:33 +00001694 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001695 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001696
1697 ga_init(&newargs);
1698 ga_init(&newlines);
1699
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001700 // First, check if this is really a lambda expression. "->" or "=>" must
1701 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001702 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001703 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001704 types_optional ? &argtypes : NULL, types_optional,
1705 types_optional ? &arg_objm : NULL, evalarg,
1706 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001707 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001708 {
1709 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001710 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001711 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001712 ga_clear(&arg_objm);
1713 }
Bram Moolenaar0346b792021-01-31 22:18:29 +01001714 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001715 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001716
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001717 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001718 if (evaluate)
1719 pnewargs = &newargs;
1720 else
1721 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001722 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001723 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
h-eastb895b0f2023-09-24 15:46:31 +02001724 types_optional ? &argtypes : NULL, types_optional,
1725 types_optional ? &arg_objm : NULL, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001726 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001727 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001728 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001729 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001730 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001731 {
1732 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001733 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001734 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001735 ga_clear(&arg_objm);
1736 }
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001737 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001738 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001739 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001740 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001741
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001742 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1743 if (ret_type != NULL)
1744 {
1745 ret_type = vim_strsave(ret_type);
1746 tofree2 = ret_type;
1747 }
1748
Bram Moolenaare38eab22019-12-05 21:50:01 +01001749 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001750 if (evaluate)
1751 eval_lavars_used = &eval_lavars;
1752
Bram Moolenaar65c44152020-12-24 15:14:01 +01001753 *arg = skipwhite_and_linebreak(*arg, evalarg);
1754
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001755 // Recognize "{" as the start of a function body.
1756 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001757 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001758 if (evalarg == NULL)
1759 // cannot happen?
1760 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001761 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001762 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1763 types_optional ? &argtypes : NULL, varargs,
1764 &default_args, ret_type) == FAIL)
1765 goto errret;
1766 goto theend;
1767 }
1768 if (default_args.ga_len > 0)
1769 {
1770 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001771 goto errret;
1772 }
1773
Bram Moolenaare38eab22019-12-05 21:50:01 +01001774 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001775 start = *arg;
1776 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001777 if (ret == FAIL)
1778 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001779
Bram Moolenaar65c44152020-12-24 15:14:01 +01001780 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001781 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001782 *arg = skipwhite_and_linebreak(*arg, evalarg);
1783 if (**arg != '}')
1784 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001785 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001786 goto errret;
1787 }
1788 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001789 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001790
1791 if (evaluate)
1792 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001793 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001794 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001795 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001796 char_u *line_end;
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001797 string_T name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001798
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001799 fp = alloc_ufunc(name.string, name.length);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001800 if (fp == NULL)
1801 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001802 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001803 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001804 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001805 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001806
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001807 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001808 if (ga_grow(&newlines, 1) == FAIL)
1809 goto errret;
1810
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001811 // If there are line breaks, we need to split up the string.
1812 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001813 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001814 line_end = end;
1815
1816 // Add "return " before the expression (or the first line).
1817 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001818 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001819 if (p == NULL)
1820 goto errret;
1821 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1822 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001823 vim_strncpy(p + 7, start, line_end - start);
1824
1825 if (line_end != end)
1826 {
1827 // Add more lines, split by line breaks. Thus is used when a
1828 // lambda with { cmds } is encountered.
1829 while (*line_end == '\n')
1830 {
1831 if (ga_grow(&newlines, 1) == FAIL)
1832 goto errret;
1833 start = line_end + 1;
1834 line_end = vim_strchr(start, '\n');
1835 if (line_end == NULL)
1836 line_end = end;
1837 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1838 vim_strnsave(start, line_end - start);
1839 }
1840 }
1841
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001842 if (strstr((char *)p + 7, "a:") == NULL)
1843 // No a: variables are used for sure.
1844 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001845
1846 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001847 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001848 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001849 if (types_optional)
1850 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001851 if (parse_argument_types(fp, &argtypes,
h-eastb895b0f2023-09-24 15:46:31 +02001852 vim9script && varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001853 goto errret;
1854 if (ret_type != NULL)
1855 {
1856 fp->uf_ret_type = parse_type(&ret_type,
1857 &fp->uf_type_list, TRUE);
1858 if (fp->uf_ret_type == NULL)
1859 goto errret;
1860 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001861 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001862 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001863 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001864
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001865 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001866 if (current_funccal != NULL && eval_lavars)
1867 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001868 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001869 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001870 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001871 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001872
1873#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001874 if (prof_def_func())
1875 func_do_profile(fp);
1876#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001877 if (sandbox)
1878 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001879 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001880 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001881 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001882 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001883 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001884 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001885 // Use the line number of the arguments.
1886 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001887
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001888 function_using_block_scopes(fp, evalarg->eval_cstack);
1889
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001890 pt->pt_func = fp;
1891 pt->pt_refcount = 1;
1892 rettv->vval.v_partial = pt;
1893 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001894
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001895 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001896 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001897
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001898theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001899 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001900 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001901 if (types_optional)
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001902 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001903 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001904 ga_clear(&arg_objm);
1905 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001906
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001907 return OK;
1908
1909errret:
1910 ga_clear_strings(&newargs);
1911 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001912 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001913 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001914 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001915 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001916 ga_clear(&arg_objm);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001917 }
John Marriottb32800f2025-02-01 15:25:34 +01001918 if (fp != NULL)
1919 {
1920 vim_free(fp->uf_arg_types);
1921 vim_free(fp->uf_name_exp);
1922 vim_free(fp);
1923 }
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001924 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001925 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001926 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001927 return FAIL;
1928}
1929
1930/*
1931 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1932 * name it contains, otherwise return "name".
1933 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1934 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001935 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1936 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001937 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001938 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001939 */
1940 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001941deref_func_name(
1942 char_u *name,
1943 int *lenp,
1944 partial_T **partialp,
1945 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001946 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001947 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001948 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001949{
1950 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001951 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001952 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001953 char_u *s = NULL;
1954 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001955 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001956
1957 if (partialp != NULL)
1958 *partialp = NULL;
1959
1960 cc = name[*lenp];
1961 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001962
Bram Moolenaar71f21932022-01-07 18:20:55 +00001963 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001964 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001965 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001966 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001967 tv = &v->di_tv;
1968 }
1969 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1970 {
1971 imported_T *import;
1972 char_u *p = name;
1973 int len = *lenp;
1974
1975 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001976 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001977 p = name + 2;
1978 len -= 2;
1979 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001980 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001981
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001982 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001983 if (import != NULL)
1984 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001985 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001986 if (new_function)
1987 semsg(_(e_redefining_imported_item_str), name);
1988 else
1989 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001990 name[len] = cc;
1991 *lenp = 0;
1992 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001993 }
1994 }
1995
1996 if (tv != NULL)
1997 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001998 if (found_var != NULL)
1999 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01002000 if (tv->v_type == VAR_FUNC)
2001 {
2002 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002003 {
2004 *lenp = 0;
2005 return (char_u *)""; // just in case
2006 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01002007 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002008 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002009 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002010
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01002011 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002012 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01002013 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002014
2015 if (pt == NULL)
2016 {
2017 *lenp = 0;
2018 return (char_u *)""; // just in case
2019 }
2020 if (partialp != NULL)
2021 *partialp = pt;
2022 s = partial_name(pt);
2023 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002024 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002025
2026 if (s != NULL)
2027 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01002028 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002029 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002030 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002031
2032 if (sv != NULL)
2033 *type = sv->sv_type;
2034 }
2035 return s;
2036 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002037 }
2038
2039 return name;
2040}
2041
2042/*
2043 * Give an error message with a function name. Handle <SNR> things.
2044 * "ermsg" is to be passed without translation, use N_() instead of _().
2045 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002046 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002047emsg_funcname(char *ermsg, char_u *name)
2048{
Bram Moolenaar54598062022-01-13 12:05:09 +00002049 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002050
Bram Moolenaar54598062022-01-13 12:05:09 +00002051 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002052 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002053 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002054 if (p != name)
2055 vim_free(p);
2056}
2057
2058/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002059 * Get function arguments at "*arg" and advance it.
2060 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01002061 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002062 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002063 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002064get_func_arguments(
2065 char_u **arg,
2066 evalarg_T *evalarg,
2067 int partial_argc,
2068 typval_T *argvars,
Ernie Raelb077b582023-12-14 20:11:44 +01002069 int *argcount,
2070 int is_builtin)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002071{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002072 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002073 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002074 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002075 int evaluate = evalarg == NULL
2076 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002077
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002078 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002079 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02002080 // skip the '(' or ',' and possibly line breaks
2081 argp = skipwhite_and_linebreak(argp + 1, evalarg);
2082
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002083 if (*argp == ')' || *argp == ',' || *argp == NUL)
2084 break;
Ernie Raelb077b582023-12-14 20:11:44 +01002085
2086 int arg_idx = *argcount;
2087 if (eval1(&argp, &argvars[arg_idx], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002088 {
2089 ret = FAIL;
2090 break;
2091 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002092 ++*argcount;
Ernie Raelb077b582023-12-14 20:11:44 +01002093 if (!is_builtin && check_typval_is_value(&argvars[arg_idx]) == FAIL)
2094 {
2095 ret = FAIL;
2096 break;
2097 }
2098
Bram Moolenaar9d489562020-07-30 20:08:50 +02002099 // The comma should come right after the argument, but this wasn't
2100 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002101 if (vim9script)
2102 {
2103 if (*argp != ',' && *skipwhite(argp) == ',')
2104 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002105 if (evaluate)
2106 semsg(_(e_no_white_space_allowed_before_str_str),
2107 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002108 ret = FAIL;
2109 break;
2110 }
2111 }
2112 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002113 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002114 if (*argp != ',')
2115 break;
Christian Brabandt00cb2472023-09-05 20:46:25 +02002116 if (vim9script && !IS_WHITE_NL_OR_NUL(argp[1]))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002117 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002118 if (evaluate)
2119 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002120 ret = FAIL;
2121 break;
2122 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002123 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002124
Bram Moolenaare6b53242020-07-01 17:28:33 +02002125 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002126 if (*argp == ')')
2127 ++argp;
2128 else
2129 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002130 *arg = argp;
2131 return ret;
2132}
2133
2134/*
2135 * Call a function and put the result in "rettv".
2136 * Return OK or FAIL.
2137 */
2138 int
2139get_func_tv(
2140 char_u *name, // name of the function
2141 int len, // length of "name" or -1 to use strlen()
2142 typval_T *rettv,
2143 char_u **arg, // argument, pointing to the '('
2144 evalarg_T *evalarg, // for line continuation
2145 funcexe_T *funcexe) // various values
2146{
2147 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002148 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002149 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
2150 int argcount = 0; // number of arguments found
2151 int vim9script = in_vim9script();
2152 int evaluate = evalarg == NULL
2153 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
2154
2155 argp = *arg;
2156 ret = get_func_arguments(&argp, evalarg,
2157 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
Ernie Raelb077b582023-12-14 20:11:44 +01002158 argvars, &argcount, builtin_function(name, -1));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002159
2160 if (ret == OK)
2161 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002162 int i = 0;
2163 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002164
2165 if (get_vim_var_nr(VV_TESTING))
2166 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002167 // Prepare for calling test_garbagecollect_now(), need to know
2168 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002169 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002170 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002171 for (i = 0; i < argcount; ++i)
2172 if (ga_grow(&funcargs, 1) == OK)
2173 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
2174 &argvars[i];
2175 }
2176
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002177 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00002178 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002179 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002180 // An error in a builtin function does not return FAIL, but we do
2181 // want to abort further processing if an error was given.
2182 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002183 clear_tv(rettv);
2184 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002185
2186 funcargs.ga_len -= i;
2187 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002188 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002189 {
2190 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002191 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002192 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00002193 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002194 }
2195
2196 while (--argcount >= 0)
2197 clear_tv(&argvars[argcount]);
2198
Bram Moolenaar4525a572022-02-13 11:57:33 +00002199 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02002200 *arg = argp;
2201 else
2202 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002203 return ret;
2204}
2205
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002206/*
2207 * Return TRUE if "p" starts with "<SID>" or "s:".
2208 * Only works if eval_fname_script() returned non-zero for "p"!
2209 */
2210 static int
2211eval_fname_sid(char_u *p)
2212{
2213 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
2214}
2215
2216/*
2217 * In a script change <SID>name() and s:name() to K_SNR 123_name().
2218 * Change <SNR>123_name() to K_SNR 123_name().
2219 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002220 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002221 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002222 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00002223fname_trans_sid(
2224 char_u *name,
2225 char_u *fname_buf,
2226 char_u **tofree,
2227 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002228{
John Marriottb32800f2025-02-01 15:25:34 +01002229 char_u *script_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002230 char_u *fname;
John Marriottb32800f2025-02-01 15:25:34 +01002231 size_t fnamelen;
2232 size_t fname_buflen;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002233
John Marriottb32800f2025-02-01 15:25:34 +01002234 script_name = name + eval_fname_script(name);
2235 if (script_name == name)
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002236 return name; // no prefix
2237
2238 fname_buf[0] = K_SPECIAL;
2239 fname_buf[1] = KS_EXTRA;
2240 fname_buf[2] = (int)KE_SNR;
John Marriottb32800f2025-02-01 15:25:34 +01002241 fname_buflen = 3;
2242 if (!eval_fname_sid(name)) // "<SID>" or "s:"
2243 fname_buf[fname_buflen] = NUL;
2244 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002245 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002246 if (current_sctx.sc_sid <= 0)
2247 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002248 else
2249 {
John Marriottb32800f2025-02-01 15:25:34 +01002250 fname_buflen += vim_snprintf((char *)fname_buf + 3,
2251 FLEN_FIXED - 3,
2252 "%ld_",
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002253 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002254 }
2255 }
John Marriottb32800f2025-02-01 15:25:34 +01002256 fnamelen = fname_buflen + STRLEN(script_name);
2257 if (fnamelen < FLEN_FIXED)
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002258 {
John Marriottb32800f2025-02-01 15:25:34 +01002259 STRCPY(fname_buf + fname_buflen, script_name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002260 fname = fname_buf;
2261 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002262 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002263 {
John Marriottb32800f2025-02-01 15:25:34 +01002264 fname = alloc(fnamelen + 1);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002265 if (fname == NULL)
2266 *error = FCERR_OTHER;
2267 else
2268 {
2269 *tofree = fname;
John Marriottb32800f2025-02-01 15:25:34 +01002270 vim_snprintf((char *)fname, fnamelen + 1, "%s%s", fname_buf, script_name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002271 }
2272 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002273 return fname;
2274}
2275
2276/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002277 * Concatenate the script ID and function name into "<SNR>99_name".
2278 * "buffer" must have size MAX_FUNC_NAME_LEN.
2279 */
2280 void
2281func_name_with_sid(char_u *name, int sid, char_u *buffer)
2282{
2283 // A script-local function is stored as "<SNR>99_name".
2284 buffer[0] = K_SPECIAL;
2285 buffer[1] = KS_EXTRA;
2286 buffer[2] = (int)KE_SNR;
2287 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2288 (long)sid, name);
2289}
2290
2291/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292 * Find a function "name" in script "sid".
2293 */
2294 static ufunc_T *
2295find_func_with_sid(char_u *name, int sid)
2296{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002297 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002298 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299
Bram Moolenaarb8822442022-01-11 15:24:05 +00002300 if (!SCRIPT_ID_VALID(sid))
2301 return NULL; // not in a script
2302
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002303 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002304 hi = hash_find(&func_hashtab, buffer);
2305 if (!HASHITEM_EMPTY(hi))
2306 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002307 return NULL;
2308}
2309
2310/*
2311 * Find a function "name" in script "sid" prefixing the autoload prefix.
2312 */
2313 static ufunc_T *
2314find_func_with_prefix(char_u *name, int sid)
2315{
2316 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002317 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002318 scriptitem_T *si;
2319
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002320 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002321 return NULL; // already has the prefix
2322 if (!SCRIPT_ID_VALID(sid))
2323 return NULL; // not in a script
2324 si = SCRIPT_ITEM(sid);
2325 if (si->sn_autoload_prefix != NULL)
2326 {
2327 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2328 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002329 char_u *namep;
2330
2331 // skip a "<SNR>99_" prefix
2332 namep = untrans_function_name(name);
2333 if (namep == NULL)
2334 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002335
2336 // An exported function in an autoload script is stored as
2337 // "dir#path#name".
2338 if (len < sizeof(buffer))
2339 auto_name = buffer;
2340 else
2341 auto_name = alloc(len);
2342 if (auto_name != NULL)
2343 {
2344 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002345 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002346 hi = hash_find(&func_hashtab, auto_name);
2347 if (auto_name != buffer)
2348 vim_free(auto_name);
2349 if (!HASHITEM_EMPTY(hi))
2350 return HI2UF(hi);
2351 }
2352 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353
2354 return NULL;
2355}
2356
2357/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002358 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002359 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2360 * functions.
2361 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002362 * Return NULL for unknown function.
2363 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002364 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002365find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002366{
2367 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002369
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002370 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002372 // Find script-local function before global one.
2373 if (in_vim9script() && eval_isnamec1(*name)
2374 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002375 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002376 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2377 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002378 if (func != NULL)
2379 return func;
2380 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002381 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2382 {
2383 char_u *p = name + 5;
2384 long sid;
2385
2386 // printable "<SNR>123_Name" form
2387 sid = getdigits(&p);
2388 if (*p == '_')
2389 {
2390 func = find_func_with_sid(p + 1, (int)sid);
2391 if (func != NULL)
2392 return func;
2393 }
2394 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002396
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002397 if ((flags & FFED_NO_GLOBAL) == 0)
2398 {
2399 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002400 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002401 if (!HASHITEM_EMPTY(hi))
2402 return HI2UF(hi);
2403 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404
Bram Moolenaarb8822442022-01-11 15:24:05 +00002405 // Find autoload function if this is an autoload script.
Yegappan Lakshmanan6289f912025-01-14 17:13:36 +01002406 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
Bram Moolenaarb8822442022-01-11 15:24:05 +00002407 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408}
2409
2410/*
2411 * Find a function by name, return pointer to it in ufuncs.
2412 * "cctx" is passed in a :def function to find imported functions.
2413 * Return NULL for unknown or dead function.
2414 */
2415 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002416find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002418 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419
2420 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2421 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002422 return NULL;
2423}
2424
2425/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002426 * Return TRUE if "ufunc" is a global function.
2427 */
2428 int
2429func_is_global(ufunc_T *ufunc)
2430{
2431 return ufunc->uf_name[0] != K_SPECIAL;
2432}
2433
2434/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002435 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2436 */
2437 int
2438func_requires_g_prefix(ufunc_T *ufunc)
2439{
John Marriottb32800f2025-02-01 15:25:34 +01002440 return func_is_global(ufunc)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002441 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002442 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01002443 && !SAFE_isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002444}
2445
2446/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002447 * Copy the function name of "fp" to buffer "buf".
2448 * "buf" must be able to hold the function name plus three bytes.
2449 * Takes care of script-local function names.
2450 */
John Marriottb32800f2025-02-01 15:25:34 +01002451 static int
2452cat_func_name(char_u *buf, size_t bufsize, ufunc_T *fp)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002453{
John Marriottb32800f2025-02-01 15:25:34 +01002454 int len;
2455
Bram Moolenaar0f769812020-09-12 18:32:34 +02002456 if (!func_is_global(fp))
John Marriottb32800f2025-02-01 15:25:34 +01002457 len = vim_snprintf((char *)buf, bufsize, "<SNR>%s", fp->uf_name + 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002458 else
John Marriottb32800f2025-02-01 15:25:34 +01002459 len = vim_snprintf((char *)buf, bufsize, "%s", fp->uf_name);
2460
2461 return (len >= (int)bufsize) ? (int)bufsize - 1 : len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002462}
2463
2464/*
2465 * Add a number variable "name" to dict "dp" with value "nr".
2466 */
2467 static void
2468add_nr_var(
2469 dict_T *dp,
2470 dictitem_T *v,
2471 char *name,
2472 varnumber_T nr)
2473{
2474 STRCPY(v->di_key, name);
2475 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002476 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002477 v->di_tv.v_type = VAR_NUMBER;
2478 v->di_tv.v_lock = VAR_FIXED;
2479 v->di_tv.vval.v_number = nr;
2480}
2481
2482/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002483 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002484 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002485 static void
2486free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002487{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002488 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002489
Bram Moolenaarca16c602022-09-06 18:57:08 +01002490 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002491 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002492 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002493
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002494 // When garbage collecting a funccall_T may be freed before the
2495 // function that references it, clear its uf_scoped field.
2496 // The function may have been redefined and point to another
2497 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002498 if (fp != NULL && fp->uf_scoped == fc)
2499 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002500 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002501 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002502
Bram Moolenaarca16c602022-09-06 18:57:08 +01002503 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002504 vim_free(fc);
2505}
2506
2507/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002508 * Free "fc" and what it contains.
2509 * Can be called only when "fc" is kept beyond the period of it called,
2510 * i.e. after cleanup_function_call(fc).
2511 */
2512 static void
2513free_funccal_contents(funccall_T *fc)
2514{
2515 listitem_T *li;
2516
2517 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002518 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002519
2520 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002521 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002522
2523 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002524 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002525 clear_tv(&li->li_tv);
2526
2527 free_funccal(fc);
2528}
2529
2530/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002531 * Handle the last part of returning from a function: free the local hashtable.
2532 * Unless it is still in use by a closure.
2533 */
2534 static void
2535cleanup_function_call(funccall_T *fc)
2536{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002537 int may_free_fc = fc->fc_refcount <= 0;
2538 int free_fc = TRUE;
2539
Bram Moolenaarca16c602022-09-06 18:57:08 +01002540 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002541
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002542 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002543 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2544 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002545 else
2546 free_fc = FALSE;
2547
2548 // If the a:000 list and the l: and a: dicts are not referenced and
2549 // there is no closure using it, we can free the funccall_T and what's
2550 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002551 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2552 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002553 else
2554 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002555 int todo;
2556 hashitem_T *hi;
2557 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002558
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002559 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002560
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002561 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002562 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002563 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002564 {
2565 if (!HASHITEM_EMPTY(hi))
2566 {
2567 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002568 di = HI2DI(hi);
2569 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002570 }
2571 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002572 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002573
Bram Moolenaarca16c602022-09-06 18:57:08 +01002574 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2575 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002576 else
2577 {
2578 listitem_T *li;
2579
2580 free_fc = FALSE;
2581
2582 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002583 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002584 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002585 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002586
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002587 if (free_fc)
2588 free_funccal(fc);
2589 else
2590 {
2591 static int made_copy = 0;
2592
2593 // "fc" is still in use. This can happen when returning "a:000",
2594 // assigning "l:" to a global variable or defining a closure.
2595 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002596 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002597 previous_funccal = fc;
2598
2599 if (want_garbage_collect)
2600 // If garbage collector is ready, clear count.
2601 made_copy = 0;
2602 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002603 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002604 // We have made a lot of copies, worth 4 Mbyte. This can happen
2605 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002606 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002607 // too much memory.
2608 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002609 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002610 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002611 }
2612}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002613
2614/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002615 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2616 */
2617 static int
2618numbered_function(char_u *name)
2619{
Keith Thompson184f71c2024-01-04 21:19:04 +01002620 return SAFE_isdigit(*name)
2621 || (name[0] == 'g' && name[1] == ':' && SAFE_isdigit(name[2]));
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002622}
2623
2624/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002625 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002626 * 1. ordinary names, function defined with :function or :def;
2627 * can start with "<SNR>123_" literally or with K_SPECIAL.
2628 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002629 * For the first we only count the name stored in func_hashtab as a reference,
2630 * using function() does not count as a reference, because the function is
2631 * looked up by name.
2632 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002633 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002634func_name_refcount(char_u *name)
2635{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002636 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002637}
2638
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639/*
2640 * Unreference "fc": decrement the reference count and free it when it
2641 * becomes zero. "fp" is detached from "fc".
2642 * When "force" is TRUE we are exiting.
2643 */
2644 static void
2645funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2646{
2647 funccall_T **pfc;
2648 int i;
2649
2650 if (fc == NULL)
2651 return;
2652
2653 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002654 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2655 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2656 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2657 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 {
2659 if (fc == *pfc)
2660 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002661 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 free_funccal_contents(fc);
2663 return;
2664 }
2665 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002666 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2667 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2668 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669}
2670
2671/*
2672 * Remove the function from the function hashtable. If the function was
2673 * deleted while it still has references this was already done.
2674 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2675 */
2676 static int
2677func_remove(ufunc_T *fp)
2678{
2679 hashitem_T *hi;
2680
2681 // Return if it was already virtually deleted.
2682 if (fp->uf_flags & FC_DEAD)
2683 return FALSE;
2684
2685 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002686 if (HASHITEM_EMPTY(hi))
2687 return FALSE;
2688
2689 // When there is a def-function index do not actually remove the
2690 // function, so we can find the index when defining the function again.
2691 // Do remove it when it's a copy.
2692 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002694 fp->uf_flags |= FC_DEAD;
2695 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002696 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002697 hash_remove(&func_hashtab, hi, "remove function");
2698 fp->uf_flags |= FC_DELETED;
2699 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002700}
2701
2702 static void
2703func_clear_items(ufunc_T *fp)
2704{
2705 ga_clear_strings(&(fp->uf_args));
2706 ga_clear_strings(&(fp->uf_def_args));
2707 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002709 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002710 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01002711 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002712
2713 // Increment the refcount of this function to avoid it being freed
2714 // recursively when the partial is freed.
2715 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002716 partial_unref(fp->uf_partial);
2717 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002718 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002719
2720#ifdef FEAT_LUA
2721 if (fp->uf_cb_free != NULL)
2722 {
2723 fp->uf_cb_free(fp->uf_cb_state);
2724 fp->uf_cb_free = NULL;
2725 }
2726
2727 fp->uf_cb_state = NULL;
2728 fp->uf_cb = NULL;
2729#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730#ifdef FEAT_PROFILE
2731 VIM_CLEAR(fp->uf_tml_count);
2732 VIM_CLEAR(fp->uf_tml_total);
2733 VIM_CLEAR(fp->uf_tml_self);
2734#endif
2735}
2736
2737/*
2738 * Free all things that a function contains. Does not free the function
2739 * itself, use func_free() for that.
2740 * When "force" is TRUE we are exiting.
2741 */
2742 static void
2743func_clear(ufunc_T *fp, int force)
2744{
2745 if (fp->uf_cleared)
2746 return;
2747 fp->uf_cleared = TRUE;
2748
2749 // clear this function
2750 func_clear_items(fp);
2751 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002752 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753}
2754
2755/*
2756 * Free a function and remove it from the list of functions. Does not free
2757 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002758 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002759 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002761 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002762func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763{
2764 // Only remove it when not done already, otherwise we would remove a newer
2765 // version of the function with the same name.
2766 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2767 func_remove(fp);
2768
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002769 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002770 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002771 if (fp->uf_dfunc_idx > 0)
2772 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002773 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002775 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002776 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002777 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778}
2779
2780/*
2781 * Free all things that a function contains and free the function itself.
2782 * When "force" is TRUE we are exiting.
2783 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002784 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785func_clear_free(ufunc_T *fp, int force)
2786{
2787 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002788 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2789 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002790 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002791 else
2792 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793}
2794
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002795/*
2796 * Copy already defined function "lambda" to a new function with name "global".
2797 * This is for when a compiled function defines a global function.
2798 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002799 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002800copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002801 char_u *lambda,
2802 char_u *global,
2803 loopvarinfo_T *loopvarinfo,
2804 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002805{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002806 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002807 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002808
2809 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002810 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002811 semsg(_(e_lambda_function_not_found_str), lambda);
2812 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002813 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002814
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002815 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002816 if (fp != NULL)
2817 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002818 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002819 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002820 return FAIL;
2821 }
2822
John Marriottb32800f2025-02-01 15:25:34 +01002823 fp = alloc_ufunc(global, STRLEN(global));
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002824 if (fp == NULL)
2825 return FAIL;
2826
2827 fp->uf_varargs = ufunc->uf_varargs;
2828 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2829 fp->uf_def_status = ufunc->uf_def_status;
2830 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2831 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2832 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2833 == FAIL
2834 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2835 goto failed;
2836
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002837 if (ufunc->uf_arg_types != NULL)
2838 {
2839 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2840 if (fp->uf_arg_types == NULL)
2841 goto failed;
2842 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2843 sizeof(type_T *) * fp->uf_args.ga_len);
2844 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002845 if (ufunc->uf_va_name != NULL)
2846 {
2847 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2848 if (fp->uf_va_name == NULL)
2849 goto failed;
2850 }
2851 fp->uf_ret_type = ufunc->uf_ret_type;
2852
2853 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002854
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002855 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002856
2857 // the referenced dfunc_T is now used one more time
2858 link_def_function(fp);
2859
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002860 // Create a partial to store the context of the function where it was
2861 // instantiated. Only needs to be done once. Do this on the original
2862 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002863 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2864 {
2865 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2866
2867 if (pt == NULL)
2868 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002869 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002870 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002871 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002872 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002873 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002874 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002875 }
2876
2877 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002878
2879failed:
2880 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002881 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002882}
2883
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002884static int funcdepth = 0;
2885
2886/*
2887 * Increment the function call depth count.
2888 * Return FAIL when going over 'maxfuncdepth'.
2889 * Otherwise return OK, must call funcdepth_decrement() later!
2890 */
2891 int
2892funcdepth_increment(void)
2893{
2894 if (funcdepth >= p_mfd)
2895 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002896 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002897 return FAIL;
2898 }
2899 ++funcdepth;
2900 return OK;
2901}
2902
2903 void
2904funcdepth_decrement(void)
2905{
2906 --funcdepth;
2907}
2908
2909/*
2910 * Get the current function call depth.
2911 */
2912 int
2913funcdepth_get(void)
2914{
2915 return funcdepth;
2916}
2917
2918/*
2919 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002920 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002921 * times as funcdepth_increment().
2922 */
2923 void
2924funcdepth_restore(int depth)
2925{
2926 funcdepth = depth;
2927}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002928
2929/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002930 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2931 * "rettv".
2932 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2933 * Returns NULL when allocation fails.
2934 */
2935 funccall_T *
2936create_funccal(ufunc_T *fp, typval_T *rettv)
2937{
2938 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2939
2940 if (fc == NULL)
2941 return NULL;
2942 fc->fc_caller = current_funccal;
2943 current_funccal = fc;
2944 fc->fc_func = fp;
2945 func_ptr_ref(fp);
2946 fc->fc_rettv = rettv;
2947 return fc;
2948}
2949
2950/*
2951 * To be called when returning from a compiled function; restores
2952 * current_funccal.
2953 */
2954 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002955remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002956{
2957 funccall_T *fc = current_funccal;
2958
2959 current_funccal = fc->fc_caller;
2960 free_funccal(fc);
2961}
2962
2963/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002964 * Call a user function.
2965 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002966 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002967call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002968 ufunc_T *fp, // pointer to function
2969 int argcount, // nr of args
2970 typval_T *argvars, // arguments
2971 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002972 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002973 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002974{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002975 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002976 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002977 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002978 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002979 funccall_T *fc;
2980 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002981 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002982 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002983 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002984 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002985 int i;
2986 int ai;
2987 int islambda = FALSE;
2988 char_u numbuf[NUMBUFLEN];
2989 char_u *name;
John Marriottb32800f2025-02-01 15:25:34 +01002990 size_t namelen;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002991 typval_T *tv_to_free[MAX_FUNC_ARGS];
2992 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002993#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002994 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002995#endif
ichizok7e5fe382023-04-15 13:17:50 +01002996 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002997
Bram Moolenaarb2049902021-01-24 12:53:53 +01002998#ifdef FEAT_PROFILE
2999 CLEAR_FIELD(profile_info);
3000#endif
3001
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003002 // If depth of calling is getting too high, don't execute the function.
3003 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003004 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003005 rettv->v_type = VAR_NUMBER;
3006 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003007 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003008 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003009
Bram Moolenaare38eab22019-12-05 21:50:01 +01003010 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003011
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01003012 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01003013 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003014 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003015 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003016 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01003017 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
3018 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003019 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01003020 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003021
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003022 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01003024#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01003025 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003026#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003027 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01003028#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003029 if (do_profiling == PROF_YES)
3030 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01003031#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003032 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003033 if (call_def_function(fp, argcount, argvars, 0,
3034 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
3035 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003036 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01003037#ifdef FEAT_PROFILE
3038 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01003039 || (caller != NULL && caller->uf_profiling)))
3040 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01003041#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01003042 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003043 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003044 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 }
3046
Bram Moolenaar38453522021-11-28 22:00:12 +00003047 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003048
3049 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01003050 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
3051 * variables with names up to VAR_SHORT_LEN long. This avoids having to
3052 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003053 */
3054 /*
3055 * Init l: variables.
3056 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01003057 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003058 if (selfdict != NULL)
3059 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003060 // Set l:self to "selfdict". Use "name" to avoid a warning from
3061 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01003062 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003063 name = v->di_key;
3064 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01003065 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003066 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003067 v->di_tv.v_type = VAR_DICT;
3068 v->di_tv.v_lock = 0;
3069 v->di_tv.vval.v_dict = selfdict;
3070 ++selfdict->dv_refcount;
3071 }
3072
3073 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003074 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003075 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003076 * Set a:000 to a list with room for the "..." arguments.
3077 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01003078 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003079 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01003080 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003081 (varnumber_T)(argcount >= fp->uf_args.ga_len
3082 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01003083 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003084 if ((fp->uf_flags & FC_NOARGS) == 0)
3085 {
3086 // Use "name" to avoid a warning from some compiler that checks the
3087 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01003088 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003089 name = v->di_key;
3090 STRCPY(name, "000");
3091 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003092 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003093 v->di_tv.v_type = VAR_LIST;
3094 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003095 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003096 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01003097 CLEAR_FIELD(fc->fc_l_varlist);
3098 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
3099 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003100
3101 /*
3102 * Set a:firstline to "firstline" and a:lastline to "lastline".
3103 * Set a:name to named arguments.
3104 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003105 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003106 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003107 if ((fp->uf_flags & FC_NOARGS) == 0)
3108 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003109 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
3110 "firstline", (varnumber_T)funcexe->fe_firstline);
3111 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
3112 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003113 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003114 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003115 {
3116 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003117 typval_T def_rettv;
3118 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003119
3120 ai = i - fp->uf_args.ga_len;
3121 if (ai < 0)
3122 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003123 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003124 name = FUNCARG(fp, i);
3125 if (islambda)
3126 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003127
3128 // evaluate named argument default expression
3129 isdefault = ai + fp->uf_def_args.ga_len >= 0
3130 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
3131 && argvars[i].vval.v_number == VVAL_NONE));
3132 if (isdefault)
3133 {
3134 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003135
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003136 def_rettv.v_type = VAR_NUMBER;
3137 def_rettv.vval.v_number = -1;
3138
3139 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
3140 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003141 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003142 {
3143 default_arg_err = 1;
3144 break;
3145 }
3146 }
John Marriottb32800f2025-02-01 15:25:34 +01003147
3148 namelen = STRLEN(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003149 }
3150 else
3151 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003152 if ((fp->uf_flags & FC_NOARGS) != 0)
3153 // Bail out if no a: arguments used (in lambda).
3154 break;
3155
Bram Moolenaare38eab22019-12-05 21:50:01 +01003156 // "..." argument a:1, a:2, etc.
John Marriottb32800f2025-02-01 15:25:34 +01003157 namelen = vim_snprintf((char *)numbuf, sizeof(numbuf), "%d", ai + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003158 name = numbuf;
3159 }
John Marriottb32800f2025-02-01 15:25:34 +01003160 if (fixvar_idx < FIXVAR_CNT && namelen <= VAR_SHORT_LEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003161 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003162 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003163 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003164 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003165 }
3166 else
3167 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003168 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003169 if (v == NULL)
3170 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003171 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003172 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003173
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003174 // Note: the values are copied directly to avoid alloc/free.
3175 // "argvars" must have VAR_FIXED for v_lock.
3176 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003177 v->di_tv.v_lock = VAR_FIXED;
3178
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003179 if (isdefault)
3180 // Need to free this later, no matter where it's stored.
3181 tv_to_free[tv_to_free_len++] = &v->di_tv;
3182
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003183 if (addlocal)
3184 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003185 // Named arguments should be accessed without the "a:" prefix in
3186 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003187 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003188 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003189 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003190 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003191 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003192
3193 if (ai >= 0 && ai < MAX_FUNC_ARGS)
3194 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003195 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003196
3197 li->li_tv = argvars[i];
3198 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003199 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003200 }
3201 }
3202
Bram Moolenaare38eab22019-12-05 21:50:01 +01003203 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003204 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003205
3206 if (fp->uf_flags & FC_SANDBOX)
3207 {
3208 using_sandbox = TRUE;
3209 ++sandbox;
3210 }
3211
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003212 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003213 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003214 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003215 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003216 ++no_wait_return;
3217 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003218
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003219 smsg(_("calling %s"), SOURCING_NAME);
3220 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003221 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003222 char_u buf[MSG_BUF_LEN];
3223 char_u numbuf2[NUMBUFLEN];
3224 char_u *tofree;
3225 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003226
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003227 msg_puts("(");
3228 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003229 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003230 if (i > 0)
3231 msg_puts(", ");
3232 if (argvars[i].v_type == VAR_NUMBER)
3233 msg_outnum((long)argvars[i].vval.v_number);
3234 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003235 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003236 // Do not want errors such as E724 here.
3237 ++emsg_off;
3238 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3239 --emsg_off;
3240 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003241 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003242 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003243 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003244 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3245 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003246 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003247 msg_puts((char *)s);
3248 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003249 }
3250 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003251 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003252 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003253 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003254 msg_puts("\n"); // don't overwrite this either
3255
3256 verbose_leave_scroll();
3257 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003258 }
3259#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003260 if (do_profiling == PROF_YES)
3261 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003262 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003263#endif
3264
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003265 // "legacy" does not apply to commands in the function
3266 sticky_cmdmod_flags = 0;
3267
Bram Moolenaar98aff652022-09-06 21:02:35 +01003268 // If called from a compiled :def function the execution context must be
3269 // hidden, any deferred functions need to be added to the function being
3270 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003271 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003272
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003273 save_current_sctx = current_sctx;
3274 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003275 save_did_emsg = did_emsg;
3276 did_emsg = FALSE;
3277
Shane Harper2d187892025-03-12 21:12:12 +01003278 if (default_arg_err && (fp->uf_flags & FC_ABORT || trylevel > 0 ))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003279 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003280 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003281 retval = FCERR_FAILED;
3282 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003283 else if (islambda)
3284 {
3285 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3286
3287 // A Lambda always has the command "return {expr}". It is much faster
3288 // to evaluate {expr} directly.
3289 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003290 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003291 --ex_nesting_level;
3292 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003293 else
3294 // call do_cmdline() to execute the lines
3295 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003296 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3297
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003298 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003299 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003300
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003301 if (RedrawingDisabled > 0)
3302 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003303
Bram Moolenaare38eab22019-12-05 21:50:01 +01003304 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003305 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3306 {
3307 clear_tv(rettv);
3308 rettv->v_type = VAR_NUMBER;
3309 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003310
3311 // In corner cases returning a "failed" value is not backwards
3312 // compatible. Only do this for Vim9 script.
3313 if (in_vim9script())
3314 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003315 }
3316
3317#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003318 if (do_profiling == PROF_YES)
3319 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003320 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003321
3322 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3323 profile_may_end_func(&profile_info, fp, caller);
3324 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003325#endif
3326
Bram Moolenaare38eab22019-12-05 21:50:01 +01003327 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003328 if (p_verbose >= 12)
3329 {
3330 ++no_wait_return;
3331 verbose_enter_scroll();
3332
3333 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003334 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003335 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003336 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003337 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003338 else
3339 {
3340 char_u buf[MSG_BUF_LEN];
3341 char_u numbuf2[NUMBUFLEN];
3342 char_u *tofree;
3343 char_u *s;
3344
Bram Moolenaare38eab22019-12-05 21:50:01 +01003345 // The value may be very long. Skip the middle part, so that we
3346 // have some idea how it starts and ends. smsg() would always
3347 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003348 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003349 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003350 --emsg_off;
3351 if (s != NULL)
3352 {
3353 if (vim_strsize(s) > MSG_BUF_CLEN)
3354 {
3355 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3356 s = buf;
3357 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003358 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003359 vim_free(tofree);
3360 }
3361 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003362 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003363
3364 verbose_leave_scroll();
3365 --no_wait_return;
3366 }
3367
ichizok7e5fe382023-04-15 13:17:50 +01003368 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003369 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003370 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003371 restore_current_ectx(save_current_ectx);
3372
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003373#ifdef FEAT_PROFILE
3374 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003375 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003376#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003377 if (using_sandbox)
3378 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003379 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003380
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003381 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003382 {
3383 ++no_wait_return;
3384 verbose_enter_scroll();
3385
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003386 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003387 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003388
3389 verbose_leave_scroll();
3390 --no_wait_return;
3391 }
3392
3393 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003394 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003395 for (i = 0; i < tv_to_free_len; ++i)
3396 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003397 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003398
3399 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003400}
3401
3402/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003403 * Check the argument count for user function "fp".
3404 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3405 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003406 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003407check_user_func_argcount(ufunc_T *fp, int argcount)
3408{
3409 int regular_args = fp->uf_args.ga_len;
3410
3411 if (argcount < regular_args - fp->uf_def_args.ga_len)
3412 return FCERR_TOOFEW;
3413 else if (!has_varargs(fp) && argcount > regular_args)
3414 return FCERR_TOOMANY;
3415 return FCERR_UNKNOWN;
3416}
3417
3418/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003420 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003421 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422call_user_func_check(
3423 ufunc_T *fp,
3424 int argcount,
3425 typval_T *argvars,
3426 typval_T *rettv,
3427 funcexe_T *funcexe,
3428 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003429{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003430 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003431
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003432#ifdef FEAT_LUA
3433 if (fp->uf_flags & FC_CFUNC)
3434 {
3435 cfunc_T cb = fp->uf_cb;
3436
3437 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3438 }
3439#endif
3440
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003441 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3442 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003443 error = check_user_func_argcount(fp, argcount);
3444 if (error != FCERR_UNKNOWN)
3445 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003446
Bram Moolenaar50824712020-12-20 21:10:17 +01003447 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003448 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003450 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003452 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 int did_save_redo = FALSE;
3454 save_redo_T save_redo;
3455
3456 /*
3457 * Call the user function.
3458 * Save and restore search patterns, script variables and
3459 * redo buffer.
3460 */
3461 save_search_patterns();
3462 if (!ins_compl_active())
3463 {
3464 saveRedobuff(&save_redo);
3465 did_save_redo = TRUE;
3466 }
3467 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003468 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003469 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003470 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3471 // Function was unreferenced while being used, free it now.
3472 func_clear_free(fp, FALSE);
3473 if (did_save_redo)
3474 restoreRedobuff(&save_redo);
3475 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003476 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003477
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003479}
3480
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003481static funccal_entry_T *funccal_stack = NULL;
3482
3483/*
3484 * Save the current function call pointer, and set it to NULL.
3485 * Used when executing autocommands and for ":source".
3486 */
3487 void
3488save_funccal(funccal_entry_T *entry)
3489{
3490 entry->top_funccal = current_funccal;
3491 entry->next = funccal_stack;
3492 funccal_stack = entry;
3493 current_funccal = NULL;
3494}
3495
3496 void
3497restore_funccal(void)
3498{
3499 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003500 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003501 else
3502 {
3503 current_funccal = funccal_stack->top_funccal;
3504 funccal_stack = funccal_stack->next;
3505 }
3506}
3507
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003508 funccall_T *
3509get_current_funccal(void)
3510{
3511 return current_funccal;
3512}
3513
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003514/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003515 * Return TRUE when currently at the script level:
3516 * - not in a function
3517 * - not executing an autocommand
3518 * Note that when an autocommand sources a script the result is FALSE;
3519 */
3520 int
3521at_script_level(void)
3522{
3523 return current_funccal == NULL && autocmd_match == NULL;
3524}
3525
3526/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003527 * Mark all functions of script "sid" as deleted.
3528 */
3529 void
3530delete_script_functions(int sid)
3531{
3532 hashitem_T *hi;
3533 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003534 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003535 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003536 size_t len;
3537
3538 buf[0] = K_SPECIAL;
3539 buf[1] = KS_EXTRA;
3540 buf[2] = (int)KE_SNR;
John Marriottb32800f2025-02-01 15:25:34 +01003541 len = 3 + vim_snprintf((char *)buf + 3, sizeof(buf) - 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003542
Bram Moolenaarce658352020-07-31 23:47:12 +02003543 while (todo > 0)
3544 {
3545 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003546 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003547 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003548 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003549 fp = HI2UF(hi);
3550 if (STRNCMP(fp->uf_name, buf, len) == 0)
3551 {
3552 int changed = func_hashtab.ht_changed;
3553
3554 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003555
3556 if (fp->uf_calls > 0)
3557 {
3558 // Function is executing, don't free it but do remove
3559 // it from the hashtable.
3560 if (func_remove(fp))
3561 fp->uf_refcount--;
3562 }
3563 else
3564 {
3565 func_clear(fp, TRUE);
3566 // When clearing a function another function can be
3567 // cleared as a side effect. When that happens start
3568 // over.
3569 if (changed != func_hashtab.ht_changed)
3570 break;
3571 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003572 }
3573 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003574 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003575 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003576}
3577
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003578#if defined(EXITFREE) || defined(PROTO)
3579 void
3580free_all_functions(void)
3581{
3582 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003583 ufunc_T *fp;
3584 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003585 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003586 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003587
Bram Moolenaare38eab22019-12-05 21:50:01 +01003588 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003589 while (current_funccal != NULL)
3590 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003591 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003592 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003593 if (current_funccal == NULL && funccal_stack != NULL)
3594 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003595 }
3596
Bram Moolenaare38eab22019-12-05 21:50:01 +01003597 // First clear what the functions contain. Since this may lower the
3598 // reference count of a function, it may also free a function and change
3599 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003600 while (todo > 0)
3601 {
3602 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003603 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003604 if (!HASHITEM_EMPTY(hi))
3605 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 // clear the def function index now
3607 fp = HI2UF(hi);
3608 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003609 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610
Bram Moolenaare38eab22019-12-05 21:50:01 +01003611 // Only free functions that are not refcounted, those are
3612 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003613 if (func_name_refcount(fp->uf_name))
3614 ++skipped;
3615 else
3616 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003617 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003618 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003619 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003620 {
3621 skipped = 0;
3622 break;
3623 }
3624 }
3625 --todo;
3626 }
3627 }
3628
Bram Moolenaare38eab22019-12-05 21:50:01 +01003629 // Now actually free the functions. Need to start all over every time,
3630 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003631 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003632 while (func_hashtab.ht_used > skipped)
3633 {
3634 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003635 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003636 if (!HASHITEM_EMPTY(hi))
3637 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003638 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003639 // Only free functions that are not refcounted, those are
3640 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003641 fp = HI2UF(hi);
3642 if (func_name_refcount(fp->uf_name))
3643 ++skipped;
3644 else
3645 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003646 if (func_free(fp, FALSE) == OK)
3647 {
3648 skipped = 0;
3649 break;
3650 }
3651 // did not actually free it
3652 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003653 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003654 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003655 }
3656 if (skipped == 0)
3657 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658
3659 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003660}
3661#endif
3662
3663/*
3664 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003665 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3666 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003667 * "len" is the length of "name", or -1 for NUL terminated.
3668 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003670builtin_function(char_u *name, int len)
3671{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003672 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003673
Bram Moolenaara26b9702020-04-18 19:53:28 +02003674 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003675 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003676 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3677 {
3678 if (name[i] == AUTOLOAD_CHAR)
3679 return FALSE;
3680 if (!eval_isnamec(name[i]))
3681 {
3682 // "name.something" is not a builtin function
3683 if (name[i] == '.')
3684 return FALSE;
3685 break;
3686 }
3687 }
3688 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003689}
3690
3691 int
3692func_call(
3693 char_u *name,
3694 typval_T *args,
3695 partial_T *partial,
3696 dict_T *selfdict,
3697 typval_T *rettv)
3698{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003699 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003700 listitem_T *item;
3701 typval_T argv[MAX_FUNC_ARGS + 1];
3702 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003703 int r = 0;
3704
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003705 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003706 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003707 {
3708 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3709 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003710 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003711 break;
3712 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003713 // Make a copy of each argument. This is needed to be able to set
3714 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003715 copy_tv(&item->li_tv, &argv[argc++]);
3716 }
3717
3718 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003719 {
3720 funcexe_T funcexe;
3721
Bram Moolenaara80faa82020-04-12 19:37:17 +02003722 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003723 funcexe.fe_firstline = curwin->w_cursor.lnum;
3724 funcexe.fe_lastline = curwin->w_cursor.lnum;
3725 funcexe.fe_evaluate = TRUE;
3726 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003727 if (partial != NULL)
3728 {
3729 funcexe.fe_object = partial->pt_obj;
3730 if (funcexe.fe_object != NULL)
3731 ++funcexe.fe_object->obj_refcount;
3732 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003733 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003734 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3735 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003736
Bram Moolenaare38eab22019-12-05 21:50:01 +01003737 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003738 while (argc > 0)
3739 clear_tv(&argv[--argc]);
3740
3741 return r;
3742}
3743
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003744static int callback_depth = 0;
3745
3746 int
3747get_callback_depth(void)
3748{
3749 return callback_depth;
3750}
3751
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003752/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003753 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003754 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003755 */
3756 int
3757call_callback(
3758 callback_T *callback,
3759 int len, // length of "name" or -1 to use strlen()
3760 typval_T *rettv, // return value goes here
3761 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003762 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003763 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003764{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003765 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003766 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003767
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003768 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3769 return FAIL;
Christian Brabandt47510f32023-10-15 09:56:16 +02003770
zeertzjqfe583b12023-12-21 16:59:26 +01003771 if (callback_depth > p_mfd)
Christian Brabandt47510f32023-10-15 09:56:16 +02003772 {
3773 emsg(_(e_command_too_recursive));
3774 return FAIL;
3775 }
3776
Bram Moolenaara80faa82020-04-12 19:37:17 +02003777 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003778 funcexe.fe_evaluate = TRUE;
3779 funcexe.fe_partial = callback->cb_partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003780 if (callback->cb_partial != NULL)
3781 {
3782 funcexe.fe_object = callback->cb_partial->pt_obj;
3783 if (funcexe.fe_object != NULL)
3784 ++funcexe.fe_object->obj_refcount;
3785 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003786 ++callback_depth;
3787 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3788 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003789
3790 // When a :def function was called that uses :try an error would be turned
3791 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003792 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003793 {
3794 need_rethrow = FALSE;
3795 handle_did_throw();
3796 }
3797
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003798 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003799}
3800
3801/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003802 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003803 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003804 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3805 */
3806 varnumber_T
3807call_callback_retnr(
3808 callback_T *callback,
3809 int argcount, // number of "argvars"
3810 typval_T *argvars) // vars for arguments, must have "argcount"
3811 // PLUS ONE elements!
3812{
3813 typval_T rettv;
3814 varnumber_T retval;
3815
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003816 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003817 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003818
3819 retval = tv_get_number_chk(&rettv, NULL);
3820 clear_tv(&rettv);
3821 return retval;
3822}
3823
3824/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 * Give an error message for the result of a function.
3826 * Nothing if "error" is FCERR_NONE.
3827 */
3828 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003829user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830{
3831 switch (error)
3832 {
3833 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003834 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003835 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003836 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003837 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838 break;
3839 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003840 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 break;
3842 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003843 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 break;
3845 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003846 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 break;
3848 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003849 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 break;
3851 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003852 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 break;
3854 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003855 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3856 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003858 case FCERR_OTHER:
3859 case FCERR_FAILED:
3860 // assume the error message was already given
3861 break;
3862 case FCERR_NONE:
3863 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 }
3865}
3866
3867/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003868 * Check the argument types "argvars[argcount]" for "name" using the
3869 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3870 * is already included in "argvars[]".
3871 * Will do nothing if "funcexe->fe_check_type" is NULL or
3872 * "funcexe->fe_evaluate" is FALSE;
3873 * Returns an FCERR_ value.
3874 */
3875 static funcerror_T
3876may_check_argument_types(
3877 funcexe_T *funcexe,
3878 typval_T *argvars,
3879 int argcount,
3880 int base_included,
3881 char_u *name)
3882{
3883 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3884 {
3885 // Check that the argument types are OK for the types of the funcref.
3886 if (check_argument_types(funcexe->fe_check_type,
3887 argvars, argcount,
3888 base_included ? NULL : funcexe->fe_basetv,
3889 name) == FAIL)
3890 return FCERR_OTHER;
3891 }
3892 return FCERR_NONE;
3893}
3894
3895/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003896 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003897 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003898 * Return FAIL when the function can't be called, OK otherwise.
3899 * Also returns OK when an error was encountered while executing the function.
3900 */
3901 int
3902call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003903 char_u *funcname, // name of the function
3904 int len, // length of "name" or -1 to use strlen()
3905 typval_T *rettv, // return value goes here
3906 int argcount_in, // number of "argvars"
3907 typval_T *argvars_in, // vars for arguments, must have "argcount"
3908 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003909 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003910{
3911 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003912 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003913 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003914 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003915 char_u fname_buf[FLEN_FIXED + 1];
3916 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003917 char_u *fname = NULL;
3918 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003919 int argcount = argcount_in;
3920 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003921 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003922 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003923 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003924 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003925 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003926 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003927 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003928 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003929
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003930 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3931 // even when call_func() returns FAIL.
3932 rettv->v_type = VAR_UNKNOWN;
3933
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003934 if (partial != NULL)
3935 fp = partial->pt_func;
3936 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003937 fp = funcexe->fe_ufunc;
3938
3939 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003940 {
3941 // Make a copy of the name, if it comes from a funcref variable it
3942 // could be changed or deleted in the called function.
3943 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3944 if (name == NULL)
3945 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003946
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003947 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3948 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003949
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003950 if (funcexe->fe_doesrange != NULL)
3951 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003952
3953 if (partial != NULL)
3954 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003955 // When the function has a partial with a dict and there is a dict
3956 // argument, use the dict argument. That is backwards compatible.
3957 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003958 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003959 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003960 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003961 {
3962 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003963 {
3964 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3965 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003966 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003967 goto theend;
3968 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003969 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003970 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003971 for (i = 0; i < argcount_in; ++i)
3972 argv[i + argv_clear] = argvars_in[i];
3973 argvars = argv;
3974 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003975
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003976 if (funcexe->fe_check_type != NULL
3977 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003978 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003979 // Now funcexe->fe_check_type is missing the added arguments,
3980 // make a copy of the type with the correction.
3981 check_type = *funcexe->fe_check_type;
3982 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003983 check_type.tt_args = check_type_args;
3984 CLEAR_FIELD(check_type_args);
3985 for (i = 0; i < check_type.tt_argcount; ++i)
3986 check_type_args[i + partial->pt_argc] =
3987 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003988 check_type.tt_argcount += partial->pt_argc;
3989 check_type.tt_min_argcount += partial->pt_argc;
3990 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003991 }
3992 }
3993
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003994 if (error == FCERR_NONE)
3995 // check the argument types if possible
3996 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
3997 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003998
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003999 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004000 {
4001 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02004002 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004003
Bram Moolenaar333894b2020-08-01 18:53:07 +02004004 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02004005 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02004006 {
4007 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004008 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02004009 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004010
Bram Moolenaare38eab22019-12-05 21:50:01 +01004011 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004012 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01004013 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004014
Bram Moolenaarf10806b2020-04-02 18:34:35 +02004015 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004016 {
4017 /*
4018 * User defined function.
4019 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02004020 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004021 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004022 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004023 if (fp != NULL && !is_global && in_vim9script()
4024 && func_requires_g_prefix(fp))
4025 // In Vim9 script g: is required to find a global
4026 // non-autoload function.
4027 fp = NULL;
4028 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004029
Bram Moolenaare38eab22019-12-05 21:50:01 +01004030 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004031 if (fp == NULL
4032 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004033 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004034 && !aborting())
4035 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004036 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004037 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004038 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004039 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004040 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
4041 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004042 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004043 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004044 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02004045 if (fp == NULL)
4046 {
4047 char_u *p = untrans_function_name(rfname);
4048
4049 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02004050 // Don't do this if the name starts with "s:".
4051 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004052 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004053 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004054
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004055 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01004056 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004057 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004058 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004059 int need_arg_check = FALSE;
4060 if (funcexe->fe_check_type == NULL)
4061 {
4062 funcexe->fe_check_type = fp->uf_func_type;
4063 need_arg_check = TRUE;
4064 }
4065
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004066 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004067 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01004068 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004069 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004070 argv_clear, fp);
4071 need_arg_check = TRUE;
4072 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02004073
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004074 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004075 {
4076 // Method call: base->Method()
4077 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004078 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004079 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004080 argvars = argv;
4081 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004082 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004083 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004084
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004085 // Check the argument types now that the function type and all
4086 // argument values are known, if not done above.
4087 if (need_arg_check)
4088 error = may_check_argument_types(funcexe, argvars, argcount,
4089 TRUE, (name != NULL) ? name : funcname);
4090 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
4091 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004093 }
4094 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004095 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004096 {
4097 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004098 * expr->method(): Find the method name in the table, call its
4099 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02004100 */
4101 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004102 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004103 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004104 else
4105 {
4106 /*
4107 * Find the function name in the table, call its implementation.
4108 */
4109 error = call_internal_func(fname, argcount, argvars, rettv);
4110 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02004111
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004112 /*
4113 * The function call (or "FuncUndefined" autocommand sequence) might
4114 * have been aborted by an error, an interrupt, or an explicitly thrown
4115 * exception that has not been caught so far. This situation can be
4116 * tested for by calling aborting(). For an error in an internal
4117 * function or for the "E132" error in call_user_func(), however, the
4118 * throw point at which the "force_abort" flag (temporarily reset by
4119 * emsg()) is normally updated has not been reached yet. We need to
4120 * update that flag first to make aborting() reliable.
4121 */
4122 update_force_abort();
4123 }
Bram Moolenaaref140542019-12-31 21:27:13 +01004124 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004125 ret = OK;
4126
Bram Moolenaar4c054e92019-11-10 00:13:50 +01004127theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004128 /*
4129 * Report an error unless the argument evaluation or function call has been
4130 * cancelled due to an aborting error, an interrupt, or an exception.
4131 */
4132 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004133 user_func_error(error, (name != NULL) ? name : funcname,
4134 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004135
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004136 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004137 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004138 clear_tv(&argv[--argv_clear + argv_base]);
4139
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004140 vim_free(tofree);
4141 vim_free(name);
4142
4143 return ret;
4144}
4145
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004146/*
4147 * Call a function without arguments, partial or dict.
4148 * This is like call_func() when the call is only "FuncName()".
4149 * To be used by "expr" options.
4150 * Returns NOTDONE when the function could not be found.
4151 */
4152 int
4153call_simple_func(
4154 char_u *funcname, // name of the function
zeertzjqc1ed7882024-08-01 22:46:54 +02004155 size_t len, // length of "name"
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004156 typval_T *rettv) // return value goes here
4157{
4158 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00004159 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004160 char_u fname_buf[FLEN_FIXED + 1];
4161 char_u *tofree = NULL;
4162 char_u *name;
4163 char_u *fname;
4164 char_u *rfname;
4165 int is_global = FALSE;
4166 ufunc_T *fp;
4167
4168 rettv->v_type = VAR_NUMBER; // default rettv is number zero
4169 rettv->vval.v_number = 0;
4170
4171 // Make a copy of the name, an option can be changed in the function.
4172 name = vim_strnsave(funcname, len);
4173 if (name == NULL)
4174 return ret;
4175
4176 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
4177
4178 // Skip "g:" before a function name.
4179 if (fname[0] == 'g' && fname[1] == ':')
4180 {
4181 is_global = TRUE;
4182 rfname = fname + 2;
4183 }
4184 else
4185 rfname = fname;
4186 fp = find_func(rfname, is_global);
4187 if (fp != NULL && !is_global && in_vim9script()
4188 && func_requires_g_prefix(fp))
4189 // In Vim9 script g: is required to find a global non-autoload
4190 // function.
4191 fp = NULL;
4192 if (fp == NULL)
4193 ret = NOTDONE;
4194 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
4195 error = FCERR_DELETED;
4196 else if (fp != NULL)
4197 {
4198 typval_T argvars[1];
4199 funcexe_T funcexe;
4200
4201 argvars[0].v_type = VAR_UNKNOWN;
4202 CLEAR_FIELD(funcexe);
4203 funcexe.fe_evaluate = TRUE;
4204
4205 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
4206 if (error == FCERR_NONE)
4207 ret = OK;
4208 }
4209
4210 user_func_error(error, name, FALSE);
4211 vim_free(tofree);
4212 vim_free(name);
4213
4214 return ret;
4215}
4216
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004217 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218printable_func_name(ufunc_T *fp)
4219{
4220 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4221}
4222
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004223/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004224 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4225 * TRUE. Otherwise return FALSE.
4226 */
4227 static int
4228function_list_modified(int prev_ht_changed)
4229{
4230 if (prev_ht_changed != func_hashtab.ht_changed)
4231 {
4232 emsg(_(e_function_list_was_modified));
4233 return TRUE;
4234 }
4235 return FALSE;
4236}
4237
4238/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004239 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004240 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004241 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004242list_func_head(ufunc_T *fp, int indent)
4243{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004244 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004245 int j;
4246
4247 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004248
4249 // a timer at the more prompt may have deleted the function
4250 if (function_list_modified(prev_ht_changed))
4251 return FAIL;
4252
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004253 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004254 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004255 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004256 msg_puts("def ");
4257 else
4258 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004260 msg_putchar('(');
4261 for (j = 0; j < fp->uf_args.ga_len; ++j)
4262 {
4263 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004264 msg_puts(", ");
4265 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 if (fp->uf_arg_types != NULL)
4267 {
4268 char *tofree;
4269
4270 msg_puts(": ");
4271 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4272 vim_free(tofree);
4273 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004274 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4275 {
4276 msg_puts(" = ");
4277 msg_puts(((char **)(fp->uf_def_args.ga_data))
4278 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4279 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004280 }
4281 if (fp->uf_varargs)
4282 {
4283 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004284 msg_puts(", ");
4285 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004286 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 if (fp->uf_va_name != NULL)
4288 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004289 if (!fp->uf_varargs)
4290 {
4291 if (j)
4292 msg_puts(", ");
4293 msg_puts("...");
4294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004296 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297 {
4298 char *tofree;
4299
4300 msg_puts(": ");
4301 msg_puts(type_name(fp->uf_va_type, &tofree));
4302 vim_free(tofree);
4303 }
4304 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004305 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004306
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004307 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004308 {
4309 if (fp->uf_ret_type != &t_void)
4310 {
4311 char *tofree;
4312
4313 msg_puts(": ");
4314 msg_puts(type_name(fp->uf_ret_type, &tofree));
4315 vim_free(tofree);
4316 }
4317 }
4318 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004319 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004320 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004321 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004322 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004323 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004324 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004325 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004326 msg_clr_eos();
4327 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004328 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004329
4330 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004331}
4332
4333/*
4334 * Get a function name, translating "<SID>" and "<SNR>".
4335 * Also handles a Funcref in a List or Dictionary.
4336 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004337 * Set "*is_global" to TRUE when the function must be global, unless
4338 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004339 * flags:
4340 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004341 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004342 * TFN_QUIET: be quiet
4343 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004344 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004345 * Advances "pp" to just after the function name (if no error).
4346 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004347 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004348trans_function_name(
4349 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004350 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004351 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004352 int flags)
4353{
4354 return trans_function_name_ext(pp, is_global, skip, flags,
4355 NULL, NULL, NULL, NULL);
4356}
4357
4358/*
4359 * trans_function_name() with extra arguments.
4360 * "fdp", "partial", "type" and "ufunc" can be NULL.
4361 */
Yegappan Lakshmanana04003a2024-10-27 21:54:11 +01004362 static char_u *
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004363trans_function_name_ext(
4364 char_u **pp,
4365 int *is_global,
4366 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004367 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004368 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004369 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004370 type_T **type, // return: type of funcref
4371 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004372{
4373 char_u *name = NULL;
4374 char_u *start;
4375 char_u *end;
4376 int lead;
4377 char_u sid_buf[20];
4378 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004380 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004381 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004382 int vim9script = in_vim9script();
4383 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004384
4385 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004386 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004387 start = *pp;
4388
Bram Moolenaare38eab22019-12-05 21:50:01 +01004389 // Check for hard coded <SNR>: already translated function ID (from a user
4390 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004391 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4392 && (*pp)[2] == (int)KE_SNR)
4393 {
4394 *pp += 3;
4395 len = get_id_len(pp) + 3;
4396 return vim_strnsave(start, len);
4397 }
4398
Bram Moolenaare38eab22019-12-05 21:50:01 +01004399 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4400 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004401 lead = eval_fname_script(start);
4402 if (lead > 2)
4403 start += lead;
4404
Bram Moolenaare38eab22019-12-05 21:50:01 +01004405 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004406 end = get_lval(start, NULL, &lv, FALSE, skip,
4407 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004408 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004409 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004410 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004411 {
4412 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004413 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004414 goto theend;
4415 }
4416 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4417 {
4418 /*
4419 * Report an invalid expression in braces, unless the expression
4420 * evaluation has been cancelled due to an aborting error, an
4421 * interrupt, or an exception.
4422 */
4423 if (!aborting())
4424 {
4425 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004426 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004427 }
4428 else
4429 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4430 goto theend;
4431 }
4432
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004433 if (lv.ll_ufunc != NULL)
4434 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004435 if (ufunc != NULL)
4436 *ufunc = lv.ll_ufunc;
John Marriottb32800f2025-02-01 15:25:34 +01004437 name = vim_strnsave(lv.ll_ufunc->uf_name, lv.ll_ufunc->uf_namelen);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004438 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004439 goto theend;
4440 }
4441
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004442 if (lv.ll_tv != NULL)
4443 {
4444 if (fdp != NULL)
4445 {
4446 fdp->fd_dict = lv.ll_dict;
4447 fdp->fd_newkey = lv.ll_newkey;
4448 lv.ll_newkey = NULL;
4449 fdp->fd_di = lv.ll_di;
4450 }
4451 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4452 {
4453 name = vim_strsave(lv.ll_tv->vval.v_string);
4454 *pp = end;
4455 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004456 else if (lv.ll_tv->v_type == VAR_CLASS
4457 && lv.ll_tv->vval.v_class != NULL)
4458 {
4459 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4460 *pp = end;
4461 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004462 else if (lv.ll_tv->v_type == VAR_PARTIAL
4463 && lv.ll_tv->vval.v_partial != NULL)
4464 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004465 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004466 *pp = end;
4467 if (partial != NULL)
4468 *partial = lv.ll_tv->vval.v_partial;
4469 }
4470 else
4471 {
4472 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4473 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004474 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004475 else
4476 *pp = end;
4477 name = NULL;
4478 }
4479 goto theend;
4480 }
4481
4482 if (lv.ll_name == NULL)
4483 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004484 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004485 *pp = end;
4486 goto theend;
4487 }
4488
Bram Moolenaare38eab22019-12-05 21:50:01 +01004489 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004490 if (lv.ll_exp_name != NULL)
4491 {
4492 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004493 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004494 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004495 if (name == lv.ll_exp_name)
4496 name = NULL;
4497 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004498 else if (lv.ll_sid > 0)
4499 {
4500 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4501 int cc = *lv.ll_name_end;
4502
4503 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4504 *lv.ll_name_end = NUL;
4505 if (si->sn_autoload_prefix != NULL)
4506 {
4507 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4508 }
4509 else
4510 {
4511 sid_buf[0] = K_SPECIAL;
4512 sid_buf[1] = KS_EXTRA;
4513 sid_buf[2] = (int)KE_SNR;
4514 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004515 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004516 name = concat_str(sid_buf, lv.ll_name);
4517 }
4518 *lv.ll_name_end = cc;
4519 *pp = end;
4520 goto theend;
4521 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004522 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004523 {
4524 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004525 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004526 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004527 if (name == *pp)
4528 name = NULL;
4529 }
4530 if (name != NULL)
4531 {
4532 name = vim_strsave(name);
4533 *pp = end;
John Marriottb32800f2025-02-01 15:25:34 +01004534 if (name != NULL && STRNCMP(name, "<SNR>", 5) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004535 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004536 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004537 name[0] = K_SPECIAL;
4538 name[1] = KS_EXTRA;
4539 name[2] = (int)KE_SNR;
4540 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4541 }
4542 goto theend;
4543 }
4544
4545 if (lv.ll_exp_name != NULL)
4546 {
4547 len = (int)STRLEN(lv.ll_exp_name);
4548 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4549 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4550 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004551 // When there was "s:" already or the name expanded to get a
4552 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004553 lv.ll_name += 2;
4554 len -= 2;
4555 lead = 2;
4556 }
4557 }
4558 else
4559 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004560 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004561 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004562 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004563 if (lv.ll_name[0] == 'g')
4564 {
4565 if (is_global != NULL)
4566 {
4567 *is_global = TRUE;
4568 }
4569 else
4570 {
4571 // dropping "g:" without setting "is_global" won't work in
4572 // Vim9script, put it back later
4573 prefix_g = TRUE;
4574 extra = 2;
4575 }
4576 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004577 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004578 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004579 len = (int)(end - lv.ll_name);
4580 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004581 if (len <= 0)
4582 {
4583 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004584 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004585 goto theend;
4586 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004587
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004588 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004589 // starts with a lower case character: dict.func(). Or when in a class.
4590 vim9_local = ASCII_ISUPPER(*start) && vim9script
4591 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004593 /*
4594 * Copy the function name to allocated memory.
4595 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4596 * Accept <SNR>123_name() outside a script.
4597 */
4598 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004599 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004600 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004601 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004602 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004603 {
Kota Kato948a3892022-08-16 16:09:59 +01004604 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4605 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004606 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004607 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004608 goto theend;
4609 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004611 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004612 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004614 || eval_fname_sid(*pp))
4615 {
John Marriottb32800f2025-02-01 15:25:34 +01004616 size_t sid_buflen;
4617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004619 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004620 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004621 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004622 goto theend;
4623 }
John Marriottb32800f2025-02-01 15:25:34 +01004624 sid_buflen = vim_snprintf((char *)sid_buf, sizeof(sid_buf), "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004625 if (vim9_local)
John Marriottb32800f2025-02-01 15:25:34 +01004626 extra = 3 + (int)sid_buflen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627 else
John Marriottb32800f2025-02-01 15:25:34 +01004628 lead += (int)sid_buflen;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004629 }
4630 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004631 // The function name must start with an upper case letter (unless it is a
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004632 // Vim9 class new() function or a Vim9 class private method or one of the
4633 // supported Vim9 object builtin functions)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004634 else if (!(flags & TFN_INT)
4635 && (builtin_function(lv.ll_name, len)
4636 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004637 && !((flags & TFN_IN_CLASS)
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004638 && (is_valid_builtin_obj_methodname(lv.ll_name)
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004639 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004640 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004641 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004642 : e_function_name_must_start_with_capital_or_s_str),
4643 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004644 goto theend;
4645 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004646 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004647 {
4648 char_u *cp = vim_strchr(lv.ll_name, ':');
4649
4650 if (cp != NULL && cp < end)
4651 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004652 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004653 goto theend;
4654 }
4655 }
4656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004658 if (name != NULL)
4659 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004660 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004661 {
4662 name[0] = K_SPECIAL;
4663 name[1] = KS_EXTRA;
4664 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004665 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004666 STRCPY(name + 3, sid_buf);
4667 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004668 else if (prefix_g)
4669 {
4670 name[0] = 'g';
4671 name[1] = ':';
4672 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004673 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4674 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004675 }
4676 *pp = end;
4677
4678theend:
4679 clear_lval(&lv);
4680 return name;
4681}
4682
4683/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004684 * Assuming "name" is the result of trans_function_name() and it was prefixed
4685 * to use the script-local name, return the unmodified name (points into
4686 * "name"). Otherwise return NULL.
4687 * This can be used to first search for a script-local function and fall back
4688 * to the global function if not found.
4689 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004690 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004691untrans_function_name(char_u *name)
4692{
4693 char_u *p;
4694
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004695 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004696 {
4697 p = vim_strchr(name, '_');
4698 if (p != NULL)
4699 return p + 1;
4700 }
4701 return NULL;
4702}
4703
4704/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004705 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4706 * current script ID and returns the expanded function name. The caller should
4707 * free the returned name. If not called from a script context or the function
4708 * name doesn't start with these prefixes, then returns NULL.
4709 * This doesn't check whether the script-local function exists or not.
4710 */
4711 char_u *
4712get_scriptlocal_funcname(char_u *funcname)
4713{
4714 char sid_buf[25];
John Marriottb32800f2025-02-01 15:25:34 +01004715 size_t sid_buflen;
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004716 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004717 char_u *newname;
John Marriottb32800f2025-02-01 15:25:34 +01004718 size_t newnamesize;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004719 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004720
4721 if (funcname == NULL)
4722 return NULL;
4723
4724 if (STRNCMP(funcname, "s:", 2) != 0
4725 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004726 {
4727 ufunc_T *ufunc;
4728
4729 // The function name does not have a script-local prefix. Try finding
4730 // it when in a Vim9 script and there is no "g:" prefix.
4731 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4732 return NULL;
4733 ufunc = find_func(funcname, FALSE);
4734 if (ufunc == NULL || func_is_global(ufunc)
4735 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4736 return NULL;
4737 ++p;
4738 off = 0;
4739 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004740 else
4741 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004742
4743 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4744 {
4745 emsg(_(e_using_sid_not_in_script_context));
4746 return NULL;
4747 }
4748 // Expand s: prefix into <SNR>nr_<name>
John Marriottb32800f2025-02-01 15:25:34 +01004749 sid_buflen = vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004750 (long)current_sctx.sc_sid);
John Marriottb32800f2025-02-01 15:25:34 +01004751 newnamesize = sid_buflen + STRLEN(p + off) + 1;
4752 newname = alloc(newnamesize);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004753 if (newname == NULL)
4754 return NULL;
John Marriottb32800f2025-02-01 15:25:34 +01004755 vim_snprintf((char *)newname, newnamesize, "%s%s", sid_buf, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004756
4757 return newname;
4758}
4759
4760/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004761 * Return script-local "fname" with the 3-byte sequence replaced by
4762 * printable <SNR> in allocated memory.
4763 */
4764 char_u *
4765alloc_printable_func_name(char_u *fname)
4766{
4767 char_u *n = alloc(STRLEN(fname + 3) + 6);
4768
4769 if (n != NULL)
4770 {
4771 STRCPY(n, "<SNR>");
4772 STRCPY(n + 5, fname + 3);
4773 }
4774 return n;
4775}
4776
4777/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004778 * Call trans_function_name(), except that a lambda is returned as-is.
4779 * Returns the name in allocated memory.
4780 */
4781 char_u *
4782save_function_name(
4783 char_u **name,
4784 int *is_global,
4785 int skip,
4786 int flags,
4787 funcdict_T *fudi)
4788{
4789 char_u *p = *name;
4790 char_u *saved;
4791
4792 if (STRNCMP(p, "<lambda>", 8) == 0)
4793 {
4794 p += 8;
4795 (void)getdigits(&p);
4796 saved = vim_strnsave(*name, p - *name);
4797 if (fudi != NULL)
4798 CLEAR_POINTER(fudi);
4799 }
4800 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004801 saved = trans_function_name_ext(&p, is_global, skip,
4802 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004803 *name = p;
4804 return saved;
4805}
4806
4807/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004808 * List functions. When "regmatch" is NULL all of then.
4809 * Otherwise functions matching "regmatch".
4810 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004811 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004812list_functions(regmatch_T *regmatch)
4813{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004814 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004815 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004816 hashitem_T *hi;
4817
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004818 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004819 {
4820 if (!HASHITEM_EMPTY(hi))
4821 {
4822 ufunc_T *fp = HI2UF(hi);
4823
4824 --todo;
4825 if ((fp->uf_flags & FC_DEAD) == 0
4826 && (regmatch == NULL
4827 ? !message_filtered(fp->uf_name)
4828 && !func_name_refcount(fp->uf_name)
Keith Thompson184f71c2024-01-04 21:19:04 +01004829 : !SAFE_isdigit(*fp->uf_name)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004830 && vim_regexec(regmatch, fp->uf_name, 0)))
4831 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004832 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004833 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004834 if (function_list_modified(prev_ht_changed))
4835 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004836 }
4837 }
4838 }
4839}
4840
4841/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004842 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004843 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4844 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004845 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004846 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4847 * With CF_INTERFACE the function is define inside an interface, only the
4848 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004849 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004850 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004851 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004852define_function(
4853 exarg_T *eap,
4854 char_u *name_arg,
4855 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004856 int class_flags,
4857 ocmember_T *obj_members,
4858 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004859{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004860 int j;
4861 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004862 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004863 char_u *name = name_arg;
John Marriottb32800f2025-02-01 15:25:34 +01004864 size_t namelen = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004865 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004866 char_u *p;
4867 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004868 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004869 char_u *line_arg = NULL;
4870 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004872 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004873 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004874 garray_T newlines;
4875 int varargs = FALSE;
4876 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004878 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004879 int fp_allocated = FALSE;
4880 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004881 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004882 dictitem_T *v;
4883 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004884 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004885 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004886 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004887 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004888 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004889 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004890
4891 /*
4892 * ":function" without argument: list functions.
4893 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004894 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004895 {
4896 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004897 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004898 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004899 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004900 }
4901
4902 /*
4903 * ":function /pat": list functions matching pattern.
4904 */
4905 if (*eap->arg == '/')
4906 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004907 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004908 if (!eap->skip)
4909 {
4910 regmatch_T regmatch;
4911
4912 c = *p;
4913 *p = NUL;
4914 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4915 *p = c;
4916 if (regmatch.regprog != NULL)
4917 {
4918 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004919 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004920 vim_regfree(regmatch.regprog);
4921 }
4922 }
4923 if (*p == '/')
4924 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004925 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004926 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004927 }
4928
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004929 ga_init(&newargs);
4930 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004931 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004932 ga_init(&default_args);
4933
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004934 /*
4935 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004936 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004937 * "name" == func, "fudi.fd_dict" == NULL
4938 * dict.func new dictionary entry
4939 * "name" == NULL, "fudi.fd_dict" set,
4940 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4941 * dict.func existing dict entry with a Funcref
4942 * "name" == func, "fudi.fd_dict" set,
4943 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4944 * dict.func existing dict entry that's not a Funcref
4945 * "name" == NULL, "fudi.fd_dict" set,
4946 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4947 * s:func script-local function name
4948 * g:func global function name, same as "func"
4949 */
4950 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004951 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004952 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004953 // nested function, argument is (args).
4954 paren = TRUE;
4955 CLEAR_FIELD(fudi);
4956 }
4957 else
4958 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004959 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004960 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004961 if (p[0] == 's' && p[1] == ':')
4962 {
4963 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4964 return NULL;
4965 }
4966 p = to_name_end(p, TRUE);
4967 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4968 {
4969 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4970 eap->arg);
4971 return NULL;
4972 }
4973 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004974 }
4975
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004976 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004977 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004978 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004979 paren = (vim_strchr(p, '(') != NULL);
4980 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004981 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004982 /*
4983 * Return on an invalid expression in braces, unless the expression
4984 * evaluation has been cancelled due to an aborting error, an
4985 * interrupt, or an exception.
4986 */
4987 if (!aborting())
4988 {
4989 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004990 semsg(_(e_key_not_present_in_dictionary_str),
4991 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004992 vim_free(fudi.fd_newkey);
4993 return NULL;
4994 }
4995 else
4996 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004997 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004998
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004999 // For "export def FuncName()" in an autoload script the function name
5000 // is stored with the legacy autoload name "dir#script#FuncName" so
5001 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00005002 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005003 {
5004 char_u *prefixed = may_prefix_autoload(name);
5005
5006 if (prefixed != NULL && prefixed != name)
5007 {
5008 vim_free(name);
5009 name = prefixed;
5010 }
5011 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00005012 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00005013 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00005014 {
5015 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
5016 goto ret_free;
5017 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005018 }
5019
Bram Moolenaare38eab22019-12-05 21:50:01 +01005020 // An error in a function call during evaluation of an expression in magic
5021 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005022 saved_did_emsg = did_emsg;
5023 did_emsg = FALSE;
5024
5025 /*
5026 * ":function func" with only function name: list function.
5027 */
5028 if (!paren)
5029 {
5030 if (!ends_excmd(*skipwhite(p)))
5031 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00005032 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005033 goto ret_free;
5034 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005035 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005036 if (eap->nextcmd != NULL)
5037 *p = NUL;
5038 if (!eap->skip && !got_int)
5039 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005040 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005041 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
5042 {
5043 char_u *up = untrans_function_name(name);
5044
5045 // With Vim9 script the name was made script-local, if not
5046 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02005047 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005048 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005049 }
5050
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005051 if (fp != NULL)
5052 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00005053 // Check no function was added or removed from a timer, e.g. at
5054 // the more prompt. "fp" may then be invalid.
5055 int prev_ht_changed = func_hashtab.ht_changed;
5056
5057 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005058 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00005059 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
5060 {
5061 if (FUNCLINE(fp, j) == NULL)
5062 continue;
5063 msg_putchar('\n');
5064 msg_outnum((long)(j + 1));
5065 if (j < 9)
5066 msg_putchar(' ');
5067 if (j < 99)
5068 msg_putchar(' ');
5069 if (function_list_modified(prev_ht_changed))
5070 break;
5071 msg_prt_line(FUNCLINE(fp, j), FALSE);
5072 out_flush(); // show a line at a time
5073 ui_breakcheck();
5074 }
5075 if (!got_int)
5076 {
5077 msg_putchar('\n');
5078 if (!function_list_modified(prev_ht_changed))
5079 {
5080 if (fp->uf_def_status != UF_NOT_COMPILED)
5081 msg_puts(" enddef");
5082 else
5083 msg_puts(" endfunction");
5084 }
5085 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005086 }
5087 }
5088 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00005089 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005090 }
5091 goto ret_free;
5092 }
5093
5094 /*
5095 * ":function name(arg1, arg2)" Define function.
5096 */
5097 p = skipwhite(p);
5098 if (*p != '(')
5099 {
5100 if (!eap->skip)
5101 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005102 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005103 goto ret_free;
5104 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005105 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005106 if (vim_strchr(p, '(') != NULL)
5107 p = vim_strchr(p, '(');
5108 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005109
Bram Moolenaar4efd9942021-01-24 21:14:20 +01005110 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
5111 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01005112 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01005113 goto ret_free;
5114 }
5115
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02005116 // In Vim9 script only global functions can be redefined.
5117 if (vim9script && eap->forceit && !is_global)
5118 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005119 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02005120 goto ret_free;
5121 }
5122
Bram Moolenaar04935fb2022-01-08 16:19:22 +00005123 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005124
Bram Moolenaar04b12692020-05-04 23:24:44 +02005125 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005126 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005127 // Check the name of the function. Unless it's a dictionary function
5128 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005129 if (name != NULL)
5130 arg = name;
5131 else
5132 arg = fudi.fd_newkey;
5133 if (arg != NULL && (fudi.fd_di == NULL
5134 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
5135 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
5136 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00005137 char_u *name_base = arg;
5138 int i;
5139
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005140 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005141 {
5142 name_base = vim_strchr(arg, '_');
5143 if (name_base == NULL)
5144 name_base = arg + 3;
5145 else
5146 ++name_base;
5147 }
5148 for (i = 0; name_base[i] != NUL && (i == 0
5149 ? eval_isnamec1(name_base[i])
5150 : eval_isnamec(name_base[i])); ++i)
5151 ;
5152 if (name_base[i] != NUL)
zeertzjq6a04bf52024-03-16 09:39:06 +01005153 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005154 emsg_funcname(e_invalid_argument_str, arg);
zeertzjq6a04bf52024-03-16 09:39:06 +01005155 goto ret_free;
5156 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00005157
5158 // In Vim9 script a function cannot have the same name as a
5159 // variable.
5160 if (vim9script && *arg == K_SPECIAL
John Marriottb32800f2025-02-01 15:25:34 +01005161 && eval_variable(name_base, i, 0, NULL,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00005162 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00005163 + EVAL_VAR_NO_FUNC) == OK)
5164 {
5165 semsg(_(e_redefining_script_item_str), name_base);
5166 goto ret_free;
5167 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005168 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005169 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005170 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005171 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005172 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00005173 goto ret_free;
5174 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005175 }
5176
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005177 // This may get more lines and make the pointers into the first line
5178 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01005179 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00005181 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02005182 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005183 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00005184 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005185 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01005186 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005187
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005188 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005189 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005190 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005191 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005192 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005193 if (*p != ':')
5194 {
5195 semsg(_(e_no_white_space_allowed_before_colon_str), p);
5196 p = skipwhite(p);
5197 }
5198 else if (!IS_WHITE_OR_NUL(p[1]))
5199 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02005201 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005203 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005204 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01005205 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005206 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005207 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005208 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005209 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005210 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02005211 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005212 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005213 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02005214 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005215 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005216 else
5217 // find extra arguments "range", "dict", "abort" and "closure"
5218 for (;;)
5219 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01005220 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005221 p = skipwhite(p);
5222 if (STRNCMP(p, "range", 5) == 0)
5223 {
5224 flags |= FC_RANGE;
5225 p += 5;
5226 }
5227 else if (STRNCMP(p, "dict", 4) == 0)
5228 {
5229 flags |= FC_DICT;
5230 p += 4;
5231 }
5232 else if (STRNCMP(p, "abort", 5) == 0)
5233 {
5234 flags |= FC_ABORT;
5235 p += 5;
5236 }
5237 else if (STRNCMP(p, "closure", 7) == 0)
5238 {
5239 flags |= FC_CLOSURE;
5240 p += 7;
5241 if (current_funccal == NULL)
5242 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005243 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005244 name == NULL ? (char_u *)"" : name);
5245 goto erret;
5246 }
5247 }
5248 else
5249 break;
5250 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005251
Bram Moolenaare38eab22019-12-05 21:50:01 +01005252 // When there is a line break use what follows for the function body.
5253 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005254 if (*p == '\n')
5255 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005256 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005257 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5258 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005259 && !(VIM_ISWHITE(*whitep) && *p == '#'
5260 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005261 && !eap->skip
5262 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005263 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005264
5265 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005266 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5267 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005268 */
5269 if (KeyTyped)
5270 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005271 // Check if the function already exists, don't let the user type the
5272 // whole function before telling him it doesn't work! For a script we
5273 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005274 if (!eap->skip && !eap->forceit)
5275 {
5276 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005277 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005278 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005279 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005280 }
5281
5282 if (!eap->skip && did_emsg)
5283 goto erret;
5284
Bram Moolenaare38eab22019-12-05 21:50:01 +01005285 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005286 cmdline_row = msg_row;
5287 }
5288
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005289 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005290 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005291
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005292 // Do not define the function when getting the body fails and when
5293 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005294 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005295 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005296 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5297 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005298 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005299 goto erret;
5300
5301 /*
5302 * If there are no errors, add the function
5303 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005304 if (fudi.fd_dict != NULL)
5305 {
John Marriottb32800f2025-02-01 15:25:34 +01005306 char numbuf[NUMBUFLEN];
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005307
5308 fp = NULL;
5309 if (fudi.fd_newkey == NULL && !eap->forceit)
5310 {
5311 emsg(_(e_dictionary_entry_already_exists));
5312 goto erret;
5313 }
5314 if (fudi.fd_di == NULL)
5315 {
5316 // Can't add a function to a locked dictionary
5317 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5318 goto erret;
5319 }
5320 // Can't change an existing function if it is locked
5321 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5322 goto erret;
5323
5324 // Give the function a sequential number. Can only be used with a
5325 // Funcref!
5326 vim_free(name);
John Marriottb32800f2025-02-01 15:25:34 +01005327 namelen = vim_snprintf(numbuf, sizeof(numbuf), "%d", ++func_nr);
5328 name = vim_strnsave((char_u *)numbuf, namelen);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005329 if (name == NULL)
5330 goto erret;
5331 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005332 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005333 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005334 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005335 char_u *find_name = name;
5336 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005337 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005338
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005339 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005340 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005341 var_conflict = TRUE;
5342
5343 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5344 {
5345 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5346
5347 if (si->sn_autoload_prefix != NULL)
5348 {
5349 if (is_export)
5350 {
5351 find_name = name + STRLEN(si->sn_autoload_prefix);
5352 v = find_var(find_name, &ht, TRUE);
5353 if (v != NULL)
5354 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005355 // Only check if the function already exists in the script,
5356 // global functions can be shadowed.
5357 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005358 }
5359 else
5360 {
5361 char_u *prefixed = may_prefix_autoload(name);
5362
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005363 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005364 {
5365 v = find_var(prefixed, &ht, TRUE);
5366 if (v != NULL)
5367 var_conflict = TRUE;
5368 vim_free(prefixed);
5369 }
5370 }
5371 }
5372 }
5373 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005374 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005375 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005376 goto erret;
5377 }
5378
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005379 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005380 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005381 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005382 char_u *uname = untrans_function_name(name);
5383
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005384 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005385 }
5386
5387 if (fp != NULL || import != NULL)
5388 {
5389 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005390
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005391 // Function can be replaced with "function!" and when sourcing the
5392 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005393 // A name that is used by an import can not be overruled.
5394 if (import != NULL
5395 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005396 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005397 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005398 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005399 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005400 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005401 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005402 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005403 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
zeertzjq04d2a3f2025-02-02 19:03:17 +01005404 goto errret_keep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005405 }
5406 if (fp->uf_calls > 0)
5407 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005408 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005409 e_cannot_redefine_function_str_it_is_in_use, name);
zeertzjq04d2a3f2025-02-02 19:03:17 +01005410 goto errret_keep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005411 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005412 if (fp->uf_refcount > 1)
5413 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005414 // This function is referenced somewhere, don't redefine it but
5415 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005416 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005417 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005418 fp = NULL;
5419 overwrite = TRUE;
5420 }
5421 else
5422 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005423 char_u *exp_name = fp->uf_name_exp;
5424
5425 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005426 VIM_CLEAR(name);
John Marriottb32800f2025-02-01 15:25:34 +01005427 namelen = 0;
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005428 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005429 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005430 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005431 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005432#ifdef FEAT_PROFILE
5433 fp->uf_profiling = FALSE;
5434 fp->uf_prof_initialized = FALSE;
5435#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005436 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005437 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005438 }
5439 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005440
5441 if (fp == NULL)
5442 {
5443 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5444 {
5445 int slen, plen;
5446 char_u *scriptname;
5447
Bram Moolenaare38eab22019-12-05 21:50:01 +01005448 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005449 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005450 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005451 {
5452 scriptname = autoload_name(name);
5453 if (scriptname != NULL)
5454 {
5455 p = vim_strchr(scriptname, '/');
5456 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005457 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005458 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005459 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005460 j = OK;
5461 vim_free(scriptname);
5462 }
5463 }
5464 if (j == FAIL)
5465 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005466 linenr_T save_lnum = SOURCING_LNUM;
5467
5468 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005469 semsg(_(e_function_name_does_not_match_script_file_name_str),
5470 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005471 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005472 goto erret;
5473 }
5474 }
5475
John Marriottb32800f2025-02-01 15:25:34 +01005476 if (namelen == 0)
5477 namelen = STRLEN(name);
5478 fp = alloc_ufunc(name, namelen);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005479 if (fp == NULL)
5480 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005481 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005482
5483 if (fudi.fd_dict != NULL)
5484 {
5485 if (fudi.fd_di == NULL)
5486 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005487 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005488 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5489 if (fudi.fd_di == NULL)
5490 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005491 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005492 goto erret;
5493 }
5494 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5495 {
5496 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005497 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005498 goto erret;
5499 }
5500 }
5501 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005502 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005503 clear_tv(&fudi.fd_di->di_tv);
5504 fudi.fd_di->di_tv.v_type = VAR_FUNC;
John Marriottb32800f2025-02-01 15:25:34 +01005505 fudi.fd_di->di_tv.vval.v_string = vim_strnsave(name, namelen);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005506
Bram Moolenaare38eab22019-12-05 21:50:01 +01005507 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005508 flags |= FC_DICT;
5509 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005510 }
5511 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005512 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005513 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005514 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005515
5516 if (eap->cmdidx == CMD_def)
5517 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005518 int lnum_save = SOURCING_LNUM;
5519 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005520
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005521 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005522
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005523 // error messages are for the first function line
5524 SOURCING_LNUM = sourcing_lnum_top;
5525
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005526 // The function may use script variables from the context.
5527 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005528
Yegappan Lakshmanand79ea472025-01-20 21:09:13 +01005529 // The argument types and the return type may use an imported type.
5530 // In that case, the imported file will be sourced. To avoid treating
5531 // everything in the imported file as exported, temporarily reset
5532 // is_export.
5533 int save_is_export = is_export;
5534 is_export = FALSE;
5535
h-eastb895b0f2023-09-24 15:46:31 +02005536 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5537 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005538 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005539 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005540 free_fp = fp_allocated;
Yegappan Lakshmanand79ea472025-01-20 21:09:13 +01005541 is_export = save_is_export;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005542 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005543 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005544 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005545
5546 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005547 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005548 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005549 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005550 free_fp = fp_allocated;
Yegappan Lakshmanand79ea472025-01-20 21:09:13 +01005551 is_export = save_is_export;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005552 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005553 }
Yegappan Lakshmanand79ea472025-01-20 21:09:13 +01005554 is_export = save_is_export;
Bram Moolenaar09689a02020-05-09 22:50:08 +02005555 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005556 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005557 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005558 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005559
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005560 if (fp_allocated)
5561 {
5562 // insert the new function in the function list
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005563 if (overwrite)
5564 {
5565 hi = hash_find(&func_hashtab, name);
5566 hi->hi_key = UF2HIKEY(fp);
5567 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005568 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005569 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005570 {
5571 free_fp = TRUE;
5572 goto erret;
5573 }
5574 fp->uf_refcount = 1;
5575 }
5576
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005577 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005578 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005579 if ((flags & FC_CLOSURE) != 0)
5580 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005581 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005582 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005583 }
5584 else
5585 fp->uf_scoped = NULL;
5586
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005587#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005588 if (prof_def_func())
5589 func_do_profile(fp);
5590#endif
5591 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005592 if (sandbox)
5593 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005594 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005595 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005596 fp->uf_flags = flags;
5597 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005598 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005599 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005600 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005601 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005602 if (is_export)
5603 {
5604 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005605 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005606 is_export = FALSE;
5607 }
5608
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005609 if (eap->cmdidx == CMD_def)
5610 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005611 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5612 // :func does not use Vim9 script syntax, even in a Vim9 script file
5613 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005614
Yegappan Lakshmanan5715a722024-05-03 18:24:07 +02005615 // If test_override('defcompile' 1) is used, then compile the function now
5616 if (eap->cmdidx == CMD_def && override_defcompile)
5617 defcompile_function(fp, NULL);
5618
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005619 goto ret_free;
5620
5621erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005622 if (fp != NULL)
5623 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005624 // these were set to "newargs" and "default_args", which are cleared
5625 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005626 ga_init(&fp->uf_args);
5627 ga_init(&fp->uf_def_args);
5628 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005629errret_2:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005630 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005631 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005632 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005633 VIM_CLEAR(fp->uf_va_name);
John Marriottb32800f2025-02-01 15:25:34 +01005634 VIM_CLEAR(fp->uf_name_exp);
Christian Brabandt0f287912023-12-11 17:53:25 +01005635 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005636 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005637 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005638 VIM_CLEAR(fp);
zeertzjq04d2a3f2025-02-02 19:03:17 +01005639errret_keep:
5640 ga_clear_strings(&newargs);
5641 ga_clear_strings(&default_args);
5642 ga_clear_strings(&newlines);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005643ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005644 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005645 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005646 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005647 if (name != name_arg)
5648 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005649 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005650 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005651
5652 return fp;
5653}
5654
5655/*
5656 * ":function"
5657 */
5658 void
5659ex_function(exarg_T *eap)
5660{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005661 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005662
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005663 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005664 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005665 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005666}
5667
LemonBoy48b7d052024-07-09 18:24:59 +02005668 int
5669get_func_arity(char_u *name, int *required, int *optional, int *varargs)
5670{
5671 ufunc_T *ufunc = NULL;
5672 int argcount = 0;
5673 int min_argcount = 0;
5674 int idx;
5675
5676 idx = find_internal_func(name);
5677 if (idx >= 0)
5678 {
5679 internal_func_get_argcount(idx, &argcount, &min_argcount);
5680 *varargs = FALSE;
5681 }
5682 else
5683 {
5684 char_u fname_buf[FLEN_FIXED + 1];
5685 char_u *tofree = NULL;
5686 funcerror_T error = FCERR_NONE;
5687 char_u *fname;
5688
5689 // May need to translate <SNR>123_ to K_SNR.
5690 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
5691 if (error == FCERR_NONE)
5692 ufunc = find_func(fname, FALSE);
5693 vim_free(tofree);
5694
5695 if (ufunc == NULL)
5696 return FAIL;
5697
5698 argcount = ufunc->uf_args.ga_len;
5699 min_argcount = ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len;
5700 *varargs = has_varargs(ufunc);
5701 }
5702
5703 *required = min_argcount;
5704 *optional = argcount - min_argcount;
5705
5706 return OK;
5707}
5708
Bram Moolenaar822ba242020-05-24 23:00:18 +02005709/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005710 * Find a function by name, including "<lambda>123".
5711 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005712 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005713 * Return NULL if not found.
5714 */
5715 ufunc_T *
5716find_func_by_name(char_u *name, compiletype_T *compile_type)
5717{
5718 char_u *arg = name;
5719 char_u *fname;
5720 ufunc_T *ufunc;
5721 int is_global = FALSE;
5722
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005723 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5724 {
5725 *compile_type = CT_PROFILE;
5726 arg = skipwhite(arg + 7);
5727 }
5728 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5729 {
5730 *compile_type = CT_DEBUG;
5731 arg = skipwhite(arg + 5);
5732 }
5733
5734 if (STRNCMP(arg, "<lambda>", 8) == 0)
5735 {
5736 arg += 8;
5737 (void)getdigits(&arg);
5738 fname = vim_strnsave(name, arg - name);
5739 }
5740 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005741 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005742 // First try finding a method in a class, trans_function_name() will
5743 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005744 ufunc = find_class_func(&arg);
5745 if (ufunc != NULL)
5746 return ufunc;
5747
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005748 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005749 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005750 NULL, NULL, NULL, &ufunc);
5751 if (ufunc != NULL)
5752 {
5753 vim_free(fname);
5754 return ufunc;
5755 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005756 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005757 if (fname == NULL)
5758 {
5759 semsg(_(e_invalid_argument_str), name);
5760 return NULL;
5761 }
5762 if (!ends_excmd2(name, arg))
5763 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005764 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005765 emsg(ex_errmsg(e_trailing_characters_str, arg));
5766 return NULL;
5767 }
5768
5769 ufunc = find_func(fname, is_global);
5770 if (ufunc == NULL)
5771 {
5772 char_u *p = untrans_function_name(fname);
5773
5774 if (p != NULL)
5775 // Try again without making it script-local.
5776 ufunc = find_func(p, FALSE);
5777 }
5778 vim_free(fname);
5779 if (ufunc == NULL)
5780 semsg(_(e_cannot_find_function_str), name);
5781 return ufunc;
5782}
5783
5784/*
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005785 * Compile the :def function "ufunc". If "cl" is not NULL, then compile the
5786 * class or object method "ufunc" in "cl".
5787 */
5788 void
5789defcompile_function(ufunc_T *ufunc, class_T *cl)
5790{
5791 compiletype_T compile_type = CT_NONE;
5792
5793 if (func_needs_compiling(ufunc, compile_type))
5794 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5795 else
5796 smsg(_("Function %s%s%s does not need compiling"),
5797 cl != NULL ? cl->class_name : (char_u *)"",
5798 cl != NULL ? (char_u *)"." : (char_u *)"",
5799 ufunc->uf_name);
5800}
5801
5802/*
5803 * Compile all the :def functions defined in the current script
5804 */
5805 static void
5806defcompile_funcs_in_script(void)
5807{
5808 long todo = (long)func_hashtab.ht_used;
5809 int changed = func_hashtab.ht_changed;
5810 hashitem_T *hi;
5811
5812 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5813 {
5814 if (!HASHITEM_EMPTY(hi))
5815 {
5816 --todo;
5817 ufunc_T *ufunc = HI2UF(hi);
5818 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5819 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5820 && (ufunc->uf_flags & FC_DEAD) == 0)
5821 {
5822 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5823
5824 if (func_hashtab.ht_changed != changed)
5825 {
5826 // a function has been added or removed, need to start
5827 // over
5828 todo = (long)func_hashtab.ht_used;
5829 changed = func_hashtab.ht_changed;
5830 hi = func_hashtab.ht_array;
5831 --hi;
5832 }
5833 }
5834 }
5835 }
5836}
5837
5838/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005839 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005840 * be compiled or the one specified by the argument.
5841 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005842 */
5843 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005844ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005845{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005846 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005847 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005848 typval_T tv;
5849
5850 if (is_class_name(eap->arg, &tv))
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005851 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005852 class_T *cl = tv.vval.v_class;
5853
5854 if (cl != NULL)
5855 defcompile_class(cl);
5856 }
5857 else
5858 {
5859 compiletype_T compile_type = CT_NONE;
5860 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
5861 if (ufunc != NULL)
5862 defcompile_function(ufunc, NULL);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005863 }
5864 }
5865 else
5866 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005867 defcompile_funcs_in_script();
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005868
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005869 // compile all the class defined in the current script
5870 defcompile_classes_in_script();
Bram Moolenaar822ba242020-05-24 23:00:18 +02005871 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005872}
5873
5874/*
5875 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5876 * Return 2 if "p" starts with "s:".
5877 * Return 0 otherwise.
5878 */
5879 int
5880eval_fname_script(char_u *p)
5881{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005882 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5883 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005884 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5885 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5886 return 5;
5887 if (p[0] == 's' && p[1] == ':')
5888 return 2;
5889 return 0;
5890}
5891
5892 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005893translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005894{
5895 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005896 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005897 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005898}
5899
5900/*
5901 * Return TRUE when "ufunc" has old-style "..." varargs
5902 * or named varargs "...name: type".
5903 */
5904 int
5905has_varargs(ufunc_T *ufunc)
5906{
5907 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005908}
5909
5910/*
5911 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005912 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005913 */
5914 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005915function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005916{
5917 char_u *nm = name;
5918 char_u *p;
5919 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005920 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005921 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005922
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005923 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5924 if (no_deref)
5925 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005926 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005927 nm = skipwhite(nm);
5928
Bram Moolenaare38eab22019-12-05 21:50:01 +01005929 // Only accept "funcname", "funcname ", "funcname (..." and
5930 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005931 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005932 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005933 vim_free(p);
5934 return n;
5935}
5936
Bram Moolenaar113e1072019-01-20 15:30:40 +01005937#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005938 char_u *
5939get_expanded_name(char_u *name, int check)
5940{
5941 char_u *nm = name;
5942 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005943 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005944
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005945 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005946
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005947 if (p != NULL && *nm == NUL
5948 && (!check || translated_function_exists(p, is_global)))
5949 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005950
5951 vim_free(p);
5952 return NULL;
5953}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005954#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005955
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005956/*
5957 * Function given to ExpandGeneric() to obtain the list of user defined
5958 * function names.
5959 */
5960 char_u *
5961get_user_func_name(expand_T *xp, int idx)
5962{
5963 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005964 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005965 static hashitem_T *hi;
5966 ufunc_T *fp;
5967
5968 if (idx == 0)
5969 {
5970 done = 0;
5971 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005972 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005973 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005974 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005975 {
John Marriottb32800f2025-02-01 15:25:34 +01005976 int len;
5977
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005978 if (done++ > 0)
5979 ++hi;
5980 while (HASHITEM_EMPTY(hi))
5981 ++hi;
5982 fp = HI2UF(hi);
5983
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005984 // don't show dead, dict and lambda functions
5985 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005986 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005987 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005988
John Marriottb32800f2025-02-01 15:25:34 +01005989 if (fp->uf_namelen + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005990 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005991
John Marriottb32800f2025-02-01 15:25:34 +01005992 len = cat_func_name(IObuff, IOSIZE, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005993 if (xp->xp_context != EXPAND_USER_FUNC
5994 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005995 {
John Marriottb32800f2025-02-01 15:25:34 +01005996 STRCPY(IObuff + len, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005997 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
John Marriottb32800f2025-02-01 15:25:34 +01005998 {
5999 ++len;
6000 STRCPY(IObuff + len, ")");
6001 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006002 }
6003 return IObuff;
6004 }
6005 return NULL;
6006}
6007
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006008/*
Bram Moolenaar83677162023-01-08 19:54:10 +00006009 * Make a copy of a function.
6010 * Intended to be used for a function defined on a base class that has a copy
6011 * on the child class.
6012 * The copy has uf_refcount set to one.
6013 * Returns NULL when out of memory.
6014 */
6015 ufunc_T *
6016copy_function(ufunc_T *fp)
6017{
John Marriottb32800f2025-02-01 15:25:34 +01006018 ufunc_T *ufunc = alloc_ufunc(fp->uf_name, fp->uf_namelen);
Bram Moolenaar83677162023-01-08 19:54:10 +00006019 if (ufunc == NULL)
6020 return NULL;
6021
6022 // Most things can just be copied.
6023 *ufunc = *fp;
6024
6025 ufunc->uf_def_status = UF_TO_BE_COMPILED;
6026 ufunc->uf_dfunc_idx = 0;
6027 ufunc->uf_class = NULL;
6028
6029 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
6030 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
6031
6032 if (ufunc->uf_arg_types != NULL)
6033 {
6034 // "uf_arg_types" is an allocated array, make a copy.
6035 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
6036 if (at != NULL)
6037 {
6038 mch_memmove(at, ufunc->uf_arg_types,
6039 sizeof(type_T *) * ufunc->uf_args.ga_len);
6040 ufunc->uf_arg_types = at;
6041 }
6042 }
6043
6044 // TODO: how about the types themselves? they can be freed when the
6045 // original function is freed:
6046 // type_T **uf_arg_types;
6047 // type_T *uf_ret_type;
6048
Ernie Rael114ec812023-06-04 18:11:35 +01006049 // make uf_type_list empty
6050 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00006051
6052 // TODO: partial_T *uf_partial;
6053
6054 if (ufunc->uf_va_name != NULL)
6055 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
6056
6057 // TODO:
6058 // type_T *uf_va_type;
6059 // type_T *uf_func_type;
6060
6061 ufunc->uf_block_depth = 0;
6062 ufunc->uf_block_ids = NULL;
6063
6064 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
6065
6066 ufunc->uf_refcount = 1;
Bram Moolenaar83677162023-01-08 19:54:10 +00006067
6068 return ufunc;
6069}
6070
6071/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006072 * ":delfunction {name}"
6073 */
6074 void
6075ex_delfunction(exarg_T *eap)
6076{
6077 ufunc_T *fp = NULL;
6078 char_u *p;
6079 char_u *name;
6080 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006081 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006082
6083 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00006084 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
6085 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006086 vim_free(fudi.fd_newkey);
6087 if (name == NULL)
6088 {
6089 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00006090 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006091 return;
6092 }
6093 if (!ends_excmd(*skipwhite(p)))
6094 {
6095 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00006096 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006097 return;
6098 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006099 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006100 if (eap->nextcmd != NULL)
6101 *p = NUL;
6102
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006103 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02006104 {
6105 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006106 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02006107 vim_free(name);
6108 return;
6109 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006110 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006111 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006112 vim_free(name);
6113
6114 if (!eap->skip)
6115 {
6116 if (fp == NULL)
6117 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02006118 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00006119 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006120 return;
6121 }
6122 if (fp->uf_calls > 0)
6123 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006124 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006125 return;
6126 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006127 if (fp->uf_flags & FC_VIM9)
6128 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006129 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006130 return;
6131 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006132
6133 if (fudi.fd_dict != NULL)
6134 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006135 // Delete the dict item that refers to the function, it will
6136 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00006137 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006138 }
6139 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006140 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006141 // A normal function (not a numbered function or lambda) has a
6142 // refcount of 1 for the entry in the hashtable. When deleting
6143 // it and the refcount is more than one, it should be kept.
6144 // A numbered function and lambda should be kept if the refcount is
6145 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006146 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006147 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006148 // Function is still referenced somewhere. Don't free it but
6149 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006150 if (func_remove(fp))
6151 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006152 }
6153 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01006154 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006155 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006156 }
6157}
6158
6159/*
6160 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006161 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006162 */
6163 void
6164func_unref(char_u *name)
6165{
Bram Moolenaar97baee82016-07-26 20:46:08 +02006166 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006167
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006168 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006169 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006170 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006171 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006172 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006173#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006174 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006175#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01006176 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006177 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006178 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006179}
6180
6181/*
6182 * Unreference a Function: decrement the reference count and free it when it
6183 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006184 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006185 */
6186 void
6187func_ptr_unref(ufunc_T *fp)
6188{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006189 if (fp != NULL && (--fp->uf_refcount <= 0
6190 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
6191 && fp->uf_partial->pt_refcount <= 1
6192 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02006193 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006194 // Only delete it when it's not being used. Otherwise it's done
6195 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02006196 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01006197 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006198 }
6199}
6200
6201/*
6202 * Count a reference to a Function.
6203 */
6204 void
6205func_ref(char_u *name)
6206{
6207 ufunc_T *fp;
6208
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006209 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006210 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006211 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006212 if (fp != NULL)
6213 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006214 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01006215 // Only give an error for a numbered function.
6216 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01006217 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006218}
6219
6220/*
6221 * Count a reference to a Function.
6222 */
6223 void
6224func_ptr_ref(ufunc_T *fp)
6225{
6226 if (fp != NULL)
6227 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006228}
6229
6230/*
6231 * Return TRUE if items in "fc" do not have "copyID". That means they are not
6232 * referenced from anywhere that is in use.
6233 */
6234 static int
6235can_free_funccal(funccall_T *fc, int copyID)
6236{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006237 return (fc->fc_l_varlist.lv_copyID != copyID
6238 && fc->fc_l_vars.dv_copyID != copyID
6239 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006240 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006241}
6242
6243/*
6244 * ":return [expr]"
6245 */
6246 void
6247ex_return(exarg_T *eap)
6248{
6249 char_u *arg = eap->arg;
6250 typval_T rettv;
6251 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006252 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006253
6254 if (current_funccal == NULL)
6255 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006256 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006257 return;
6258 }
6259
Bram Moolenaar844fb642021-10-23 13:32:30 +01006260 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006261 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
6262
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006263 if (eap->skip)
6264 ++emsg_skip;
6265
6266 eap->nextcmd = NULL;
6267 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006268 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006269 {
6270 if (!eap->skip)
6271 returning = do_return(eap, FALSE, TRUE, &rettv);
6272 else
6273 clear_tv(&rettv);
6274 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01006275 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006276 else if (!eap->skip)
6277 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006278 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01006279 update_force_abort();
6280
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006281 /*
6282 * Return unless the expression evaluation has been cancelled due to an
6283 * aborting error, an interrupt, or an exception.
6284 */
6285 if (!aborting())
6286 returning = do_return(eap, FALSE, TRUE, NULL);
6287 }
6288
Bram Moolenaare38eab22019-12-05 21:50:01 +01006289 // When skipping or the return gets pending, advance to the next command
6290 // in this line (!returning). Otherwise, ignore the rest of the line.
6291 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006292 if (returning)
6293 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006294 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02006295 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006296
6297 if (eap->skip)
6298 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02006299 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006300}
6301
zeertzjq5299c092023-04-12 20:48:16 +01006302/*
6303 * Lower level implementation of "call". Only called when not skipping.
6304 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006305 static int
6306ex_call_inner(
6307 exarg_T *eap,
6308 char_u *name,
6309 char_u **arg,
6310 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006311 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006312 evalarg_T *evalarg)
6313{
6314 linenr_T lnum;
6315 int doesrange;
6316 typval_T rettv;
6317 int failed = FALSE;
6318
zeertzjq5299c092023-04-12 20:48:16 +01006319 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006320 for ( ; lnum <= eap->line2; ++lnum)
6321 {
6322 funcexe_T funcexe;
6323
zeertzjq5299c092023-04-12 20:48:16 +01006324 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006325 {
6326 if (lnum > curbuf->b_ml.ml_line_count)
6327 {
6328 // If the function deleted lines or switched to another buffer
6329 // the line number may become invalid.
6330 emsg(_(e_invalid_range));
6331 break;
6332 }
6333 curwin->w_cursor.lnum = lnum;
6334 curwin->w_cursor.col = 0;
6335 curwin->w_cursor.coladd = 0;
6336 }
6337 *arg = startarg;
6338
6339 funcexe = *funcexe_init;
6340 funcexe.fe_doesrange = &doesrange;
6341 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6342 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6343 {
6344 failed = TRUE;
6345 break;
6346 }
6347 if (has_watchexpr())
6348 dbg_check_breakpoint(eap);
6349
6350 // Handle a function returning a Funcref, Dictionary or List.
6351 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006352 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006353 {
6354 failed = TRUE;
6355 break;
6356 }
6357
6358 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006359 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006360 break;
6361
6362 // Stop when immediately aborting on error, or when an interrupt
6363 // occurred or an exception was thrown but not caught.
6364 // get_func_tv() returned OK, so that the check for trailing
6365 // characters below is executed.
6366 if (aborting())
6367 break;
6368 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006369 return failed;
6370}
6371
6372/*
6373 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6374 * Returns FAIL or OK.
6375 */
6376 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006377ex_defer_inner(
6378 char_u *name,
6379 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006380 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006381 partial_T *partial,
6382 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006383{
6384 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006385 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006386 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006387 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006388
6389 if (current_funccal == NULL)
6390 {
6391 semsg(_(e_str_not_inside_function), "defer");
6392 return FAIL;
6393 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006394 if (partial != NULL)
6395 {
6396 if (partial->pt_dict != NULL)
6397 {
6398 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6399 return FAIL;
6400 }
6401 if (partial->pt_argc > 0)
6402 {
6403 int i;
6404
6405 partial_argc = partial->pt_argc;
6406 for (i = 0; i < partial_argc; ++i)
6407 copy_tv(&partial->pt_argv[i], &argvars[i]);
6408 }
6409 }
Ernie Raelb077b582023-12-14 20:11:44 +01006410 int is_builtin = builtin_function(name, -1);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006411 r = get_func_arguments(arg, evalarg, FALSE,
Ernie Raelb077b582023-12-14 20:11:44 +01006412 argvars + partial_argc, &argcount, is_builtin);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006413 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006414
6415 if (r == OK)
6416 {
6417 if (type != NULL)
6418 {
6419 // Check that the arguments are OK for the types of the funcref.
6420 r = check_argument_types(type, argvars, argcount, NULL, name);
6421 }
Ernie Raelb077b582023-12-14 20:11:44 +01006422 else if (is_builtin)
Bram Moolenaar16900322022-09-08 19:51:45 +01006423 {
6424 int idx = find_internal_func(name);
6425
6426 if (idx < 0)
6427 {
6428 emsg_funcname(e_unknown_function_str, name);
6429 r = FAIL;
6430 }
6431 else if (check_internal_func(idx, argcount) == -1)
6432 r = FAIL;
6433 }
6434 else
6435 {
6436 ufunc_T *ufunc = find_func(name, FALSE);
6437
6438 // we tolerate an unknown function here, it might be defined later
6439 if (ufunc != NULL)
6440 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006441 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006442 if (error != FCERR_UNKNOWN)
6443 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006444 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006445 r = FAIL;
6446 }
6447 }
6448 }
6449 }
6450
Bram Moolenaar86d87252022-09-05 21:21:25 +01006451 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006452 {
6453 while (--argcount >= 0)
6454 clear_tv(&argvars[argcount]);
6455 return FAIL;
6456 }
6457 return add_defer(name, argcount, argvars);
6458}
6459
6460/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006461 * Return TRUE if currently inside a function call.
6462 * Give an error message and return FALSE when not.
6463 */
6464 int
6465can_add_defer(void)
6466{
6467 if (!in_def_function() && get_current_funccal() == NULL)
6468 {
6469 semsg(_(e_str_not_inside_function), "defer");
6470 return FALSE;
6471 }
6472 return TRUE;
6473}
6474
6475/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006476 * Add a deferred call for "name" with arguments "argvars[argcount]".
6477 * Consumes "argvars[]".
6478 * Caller must check that in_def_function() returns TRUE or current_funccal is
6479 * not NULL.
6480 * Returns OK or FAIL.
6481 */
6482 int
6483add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6484{
6485 char_u *saved_name = vim_strsave(name);
6486 int argcount = argcount_arg;
6487 defer_T *dr;
6488 int ret = FAIL;
6489
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006490 if (saved_name == NULL)
6491 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006492 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006493 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006494 if (add_defer_function(saved_name, argcount, argvars) == OK)
6495 argcount = 0;
6496 }
6497 else
6498 {
6499 if (current_funccal->fc_defer.ga_itemsize == 0)
6500 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6501 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6502 goto theend;
6503 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6504 + current_funccal->fc_defer.ga_len++;
6505 dr->dr_name = saved_name;
6506 dr->dr_argcount = argcount;
6507 while (argcount > 0)
6508 {
6509 --argcount;
6510 dr->dr_argvars[argcount] = argvars[argcount];
6511 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006512 }
6513 ret = OK;
6514
6515theend:
6516 while (--argcount >= 0)
6517 clear_tv(&argvars[argcount]);
6518 return ret;
6519}
6520
6521/*
6522 * Invoked after a functions has finished: invoke ":defer" functions.
6523 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006524 static void
6525handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006526{
6527 int idx;
6528
Bram Moolenaar58779852022-09-06 18:31:14 +01006529 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006530 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006531 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006532
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006533 if (dr->dr_name == NULL)
6534 // already being called, can happen if function does ":qa"
6535 continue;
6536
6537 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006538 CLEAR_FIELD(funcexe);
6539 funcexe.fe_evaluate = TRUE;
6540
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006541 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006542 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006543
6544 char_u *name = dr->dr_name;
6545 dr->dr_name = NULL;
6546
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006547 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006548 // first statement in the function will be executed (because of the
6549 // exception). So save and restore the try/catch/throw exception
6550 // state.
6551 exception_state_T estate;
6552 exception_state_save(&estate);
6553 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006554
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006555 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6556
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006557 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006558
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006559 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006560 vim_free(name);
6561 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006562 clear_tv(&dr->dr_argvars[i]);
6563 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006564 ga_clear(&funccal->fc_defer);
6565}
6566
zeertzjq960cf912023-04-18 21:52:54 +01006567 static void
6568invoke_funccall_defer(funccall_T *fc)
6569{
6570 if (fc->fc_ectx != NULL)
6571 {
6572 // :def function
6573 unwind_def_callstack(fc->fc_ectx);
6574 may_invoke_defer_funcs(fc->fc_ectx);
6575 }
6576 else
6577 {
6578 // legacy function
6579 handle_defer_one(fc);
6580 }
6581}
6582
Bram Moolenaar58779852022-09-06 18:31:14 +01006583/*
6584 * Called when exiting: call all defer functions.
6585 */
6586 void
6587invoke_all_defer(void)
6588{
zeertzjq1be4b812023-04-19 14:21:24 +01006589 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6590 invoke_funccall_defer(fc);
6591
zeertzjq960cf912023-04-18 21:52:54 +01006592 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6593 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6594 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006595}
6596
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006597/*
6598 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006599 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006600 */
6601 void
6602ex_call(exarg_T *eap)
6603{
6604 char_u *arg = eap->arg;
6605 char_u *startarg;
6606 char_u *name;
6607 char_u *tofree;
6608 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006609 int failed = FALSE;
6610 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006611 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006612 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006613 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006614 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006615 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006616 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006617
Bram Moolenaare6b53242020-07-01 17:28:33 +02006618 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006619 if (eap->skip)
6620 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006621 typval_T rettv;
6622
Bram Moolenaare38eab22019-12-05 21:50:01 +01006623 // trans_function_name() doesn't work well when skipping, use eval0()
6624 // instead to skip to any following command, e.g. for:
6625 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006626 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006627 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006628 clear_tv(&rettv);
6629 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006630 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006631 return;
6632 }
6633
zeertzjq5299c092023-04-12 20:48:16 +01006634 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006635 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006636 if (fudi.fd_newkey != NULL)
6637 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006638 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006639 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006640 vim_free(fudi.fd_newkey);
6641 }
6642 if (tofree == NULL)
6643 return;
6644
Bram Moolenaare38eab22019-12-05 21:50:01 +01006645 // Increase refcount on dictionary, it could get deleted when evaluating
6646 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006647 if (fudi.fd_dict != NULL)
6648 ++fudi.fd_dict->dv_refcount;
6649
Bram Moolenaare38eab22019-12-05 21:50:01 +01006650 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6651 // contents. For VAR_PARTIAL get its partial, unless we already have one
6652 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006653 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006654 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006655 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006656 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006657
Bram Moolenaare38eab22019-12-05 21:50:01 +01006658 // Skip white space to allow ":call func ()". Not good, but required for
6659 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006660 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006661 if (*startarg != '(')
6662 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006663 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006664 goto end;
6665 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006666 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006667 {
6668 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6669 goto end;
6670 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006671
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006672 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006673 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006674 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006675 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006676 }
6677 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006678 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006679 funcexe_T funcexe;
6680
Bram Moolenaara80faa82020-04-12 19:37:17 +02006681 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006682 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006683 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006684 funcexe.fe_partial = partial;
6685 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006686 funcexe.fe_firstline = eap->line1;
6687 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006688 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006689 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006690 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006691 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006692
Bram Moolenaar1d341892021-09-18 15:25:52 +02006693 // When inside :try we need to check for following "| catch" or "| endtry".
6694 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006695 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006696 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006697 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006698 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006699 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006700 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006701 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006702 {
6703 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006704 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006705 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006706 }
6707 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006708 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006709 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006710 // Must be after using "arg", it may point into memory cleared here.
6711 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006712
6713end:
6714 dict_unref(fudi.fd_dict);
6715 vim_free(tofree);
6716}
6717
6718/*
6719 * Return from a function. Possibly makes the return pending. Also called
6720 * for a pending return at the ":endtry" or after returning from an extra
6721 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6722 * when called due to a ":return" command. "rettv" may point to a typval_T
6723 * with the return rettv. Returns TRUE when the return can be carried out,
6724 * FALSE when the return gets pending.
6725 */
6726 int
6727do_return(
6728 exarg_T *eap,
6729 int reanimate,
6730 int is_cmd,
6731 void *rettv)
6732{
6733 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006734 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006735
6736 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006737 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006738 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006739
6740 /*
6741 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6742 * not in its finally clause (which then is to be executed next) is found.
6743 * In this case, make the ":return" pending for execution at the ":endtry".
6744 * Otherwise, return normally.
6745 */
6746 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6747 if (idx >= 0)
6748 {
6749 cstack->cs_pending[idx] = CSTP_RETURN;
6750
6751 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006752 // A pending return again gets pending. "rettv" points to an
6753 // allocated variable with the rettv of the original ":return"'s
6754 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006755 cstack->cs_rettv[idx] = rettv;
6756 else
6757 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006758 // When undoing a return in order to make it pending, get the stored
6759 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006760 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006761 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006762
6763 if (rettv != NULL)
6764 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006765 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006766 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6767 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6768 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006769 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006770 }
6771 else
6772 cstack->cs_rettv[idx] = NULL;
6773
6774 if (reanimate)
6775 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006776 // The pending return value could be overwritten by a ":return"
6777 // without argument in a finally clause; reset the default
6778 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006779 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6780 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006781 }
6782 }
6783 report_make_pending(CSTP_RETURN, rettv);
6784 }
6785 else
6786 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006787 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006788
Bram Moolenaare38eab22019-12-05 21:50:01 +01006789 // If the return is carried out now, store the return value. For
6790 // a return immediately after reanimation, the value is already
6791 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006792 if (!reanimate && rettv != NULL)
6793 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006794 clear_tv(current_funccal->fc_rettv);
6795 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006796 if (!is_cmd)
6797 vim_free(rettv);
6798 }
6799 }
6800
6801 return idx < 0;
6802}
6803
6804/*
6805 * Free the variable with a pending return value.
6806 */
6807 void
6808discard_pending_return(void *rettv)
6809{
6810 free_tv((typval_T *)rettv);
6811}
6812
6813/*
6814 * Generate a return command for producing the value of "rettv". The result
6815 * is an allocated string. Used by report_pending() for verbose messages.
6816 */
6817 char_u *
6818get_return_cmd(void *rettv)
6819{
6820 char_u *s = NULL;
zeertzjq21012302025-02-02 08:55:57 +01006821 char_u *tofree = NULL;
6822 char_u numbuf[NUMBUFLEN];
John Marriottb32800f2025-02-01 15:25:34 +01006823 size_t slen = 0;
6824 size_t IObufflen;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006825
6826 if (rettv != NULL)
6827 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6828 if (s == NULL)
6829 s = (char_u *)"";
John Marriottb32800f2025-02-01 15:25:34 +01006830 else
6831 slen = STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006832
6833 STRCPY(IObuff, ":return ");
6834 STRNCPY(IObuff + 8, s, IOSIZE - 8);
John Marriottb32800f2025-02-01 15:25:34 +01006835 IObufflen = 8 + slen;
zeertzjq21012302025-02-02 08:55:57 +01006836 if (IObufflen >= IOSIZE)
John Marriottb32800f2025-02-01 15:25:34 +01006837 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006838 STRCPY(IObuff + IOSIZE - 4, "...");
zeertzjq21012302025-02-02 08:55:57 +01006839 IObufflen = IOSIZE - 1;
John Marriottb32800f2025-02-01 15:25:34 +01006840 }
zeertzjq21012302025-02-02 08:55:57 +01006841 vim_free(tofree);
John Marriottb32800f2025-02-01 15:25:34 +01006842 return vim_strnsave(IObuff, IObufflen);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006843}
6844
6845/*
6846 * Get next function line.
6847 * Called by do_cmdline() to get the next line.
6848 * Returns allocated string, or NULL for end of function.
6849 */
6850 char_u *
6851get_func_line(
6852 int c UNUSED,
6853 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006854 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006855 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006856{
6857 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006858 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006859 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006860 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006861
Bram Moolenaare38eab22019-12-05 21:50:01 +01006862 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006863 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006864 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006865 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006866 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006867 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006868 }
6869#ifdef FEAT_PROFILE
6870 if (do_profiling == PROF_YES)
6871 func_line_end(cookie);
6872#endif
6873
6874 gap = &fp->uf_lines;
6875 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006876 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006877 retval = NULL;
6878 else
6879 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006880 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006881 while (fcp->fc_linenr < gap->ga_len
6882 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6883 ++fcp->fc_linenr;
6884 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006885 retval = NULL;
6886 else
6887 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006888 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6889 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006890#ifdef FEAT_PROFILE
6891 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006892 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006893#endif
6894 }
6895 }
6896
Bram Moolenaare38eab22019-12-05 21:50:01 +01006897 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006898 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006899 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006900 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006901 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006902 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006903 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006904 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006905 }
6906
6907 return retval;
6908}
6909
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006910/*
6911 * Return TRUE if the currently active function should be ended, because a
6912 * return was encountered or an error occurred. Used inside a ":while".
6913 */
6914 int
6915func_has_ended(void *cookie)
6916{
6917 funccall_T *fcp = (funccall_T *)cookie;
6918
Bram Moolenaare38eab22019-12-05 21:50:01 +01006919 // Ignore the "abort" flag if the abortion behavior has been changed due to
6920 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006921 return (((fcp->fc_func->uf_flags & FC_ABORT)
6922 && did_emsg && !aborted_in_try())
6923 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006924}
6925
6926/*
6927 * return TRUE if cookie indicates a function which "abort"s on errors.
6928 */
6929 int
6930func_has_abort(
6931 void *cookie)
6932{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006933 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006934}
6935
6936
6937/*
6938 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6939 * Don't do this when "Func" is already a partial that was bound
6940 * explicitly (pt_auto is FALSE).
6941 * Changes "rettv" in-place.
6942 * Returns the updated "selfdict_in".
6943 */
6944 dict_T *
6945make_partial(dict_T *selfdict_in, typval_T *rettv)
6946{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006947 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006948 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006949 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006950 dict_T *selfdict = selfdict_in;
6951
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006952 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6953 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006954 fp = rettv->vval.v_partial->pt_func;
6955 else
6956 {
6957 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006958 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006959 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006960 if (fname == NULL)
6961 {
6962 // There is no point binding a dict to a NULL function, just create
6963 // a function reference.
6964 rettv->v_type = VAR_FUNC;
6965 rettv->vval.v_string = NULL;
6966 }
6967 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006968 {
6969 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006970 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006971
6972 // Translate "s:func" to the stored function name.
6973 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6974 fp = find_func(fname, FALSE);
6975 vim_free(tofree);
6976 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006977 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006978
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006979 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006980 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006981 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006982
6983 if (pt != NULL)
6984 {
6985 pt->pt_refcount = 1;
6986 pt->pt_dict = selfdict;
6987 pt->pt_auto = TRUE;
6988 selfdict = NULL;
6989 if (rettv->v_type == VAR_FUNC)
6990 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006991 // Just a function: Take over the function name and use
6992 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006993 pt->pt_name = rettv->vval.v_string;
6994 }
6995 else
6996 {
6997 partial_T *ret_pt = rettv->vval.v_partial;
6998 int i;
6999
Bram Moolenaare38eab22019-12-05 21:50:01 +01007000 // Partial: copy the function name, use selfdict and copy
7001 // args. Can't take over name or args, the partial might
7002 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007003 if (ret_pt->pt_name != NULL)
7004 {
7005 pt->pt_name = vim_strsave(ret_pt->pt_name);
7006 func_ref(pt->pt_name);
7007 }
7008 else
7009 {
7010 pt->pt_func = ret_pt->pt_func;
7011 func_ptr_ref(pt->pt_func);
7012 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007013 if (ret_pt->pt_argc > 0)
7014 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007015 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007016 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01007017 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007018 pt->pt_argc = 0;
7019 else
7020 {
7021 pt->pt_argc = ret_pt->pt_argc;
7022 for (i = 0; i < pt->pt_argc; i++)
7023 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
7024 }
7025 }
7026 partial_unref(ret_pt);
7027 }
7028 rettv->v_type = VAR_PARTIAL;
7029 rettv->vval.v_partial = pt;
7030 }
7031 }
7032 return selfdict;
7033}
7034
7035/*
7036 * Return the name of the executed function.
7037 */
7038 char_u *
7039func_name(void *cookie)
7040{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007041 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007042}
7043
7044/*
7045 * Return the address holding the next breakpoint line for a funccall cookie.
7046 */
7047 linenr_T *
7048func_breakpoint(void *cookie)
7049{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007050 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007051}
7052
7053/*
7054 * Return the address holding the debug tick for a funccall cookie.
7055 */
7056 int *
7057func_dbg_tick(void *cookie)
7058{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007059 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007060}
7061
7062/*
7063 * Return the nesting level for a funccall cookie.
7064 */
7065 int
7066func_level(void *cookie)
7067{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007068 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007069}
7070
7071/*
7072 * Return TRUE when a function was ended by a ":return" command.
7073 */
7074 int
7075current_func_returned(void)
7076{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007077 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007078}
7079
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007080 int
7081free_unref_funccal(int copyID, int testing)
7082{
7083 int did_free = FALSE;
7084 int did_free_funccal = FALSE;
7085 funccall_T *fc, **pfc;
7086
7087 for (pfc = &previous_funccal; *pfc != NULL; )
7088 {
7089 if (can_free_funccal(*pfc, copyID))
7090 {
7091 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007092 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01007093 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007094 did_free = TRUE;
7095 did_free_funccal = TRUE;
7096 }
7097 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01007098 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007099 }
7100 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01007101 // When a funccal was freed some more items might be garbage
7102 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007103 (void)garbage_collect(testing);
7104
7105 return did_free;
7106}
7107
7108/*
Bram Moolenaarba209902016-08-24 22:06:38 +02007109 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007110 */
7111 static funccall_T *
7112get_funccal(void)
7113{
7114 int i;
7115 funccall_T *funccal;
7116 funccall_T *temp_funccal;
7117
7118 funccal = current_funccal;
7119 if (debug_backtrace_level > 0)
7120 {
7121 for (i = 0; i < debug_backtrace_level; i++)
7122 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007123 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007124 if (temp_funccal)
7125 funccal = temp_funccal;
7126 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01007127 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007128 debug_backtrace_level = i;
7129 }
7130 }
7131 return funccal;
7132}
7133
7134/*
7135 * Return the hashtable used for local variables in the current funccal.
7136 * Return NULL if there is no current funccal.
7137 */
7138 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007139get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007140{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007141 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007142 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007143 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007144}
7145
7146/*
7147 * Return the l: scope variable.
7148 * Return NULL if there is no current funccal.
7149 */
7150 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007151get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007152{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007153 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007154 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007155 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007156}
7157
7158/*
7159 * Return the hashtable used for argument in the current funccal.
7160 * Return NULL if there is no current funccal.
7161 */
7162 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007163get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007164{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007165 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007166 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007167 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007168}
7169
7170/*
7171 * Return the a: scope variable.
7172 * Return NULL if there is no current funccal.
7173 */
7174 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007175get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007176{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007177 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007178 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007179 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007180}
7181
7182/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007183 * List function variables, if there is a function.
7184 */
7185 void
7186list_func_vars(int *first)
7187{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007188 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
7189 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01007190 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007191}
7192
7193/*
7194 * If "ht" is the hashtable for local variables in the current funccal, return
7195 * the dict that contains it.
7196 * Otherwise return NULL.
7197 */
7198 dict_T *
7199get_current_funccal_dict(hashtab_T *ht)
7200{
7201 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01007202 && ht == &current_funccal->fc_l_vars.dv_hashtab)
7203 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007204 return NULL;
7205}
7206
7207/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007208 * Search hashitem in parent scope.
7209 */
7210 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007211find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007212{
7213 funccall_T *old_current_funccal = current_funccal;
7214 hashtab_T *ht;
7215 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007216 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007217
Bram Moolenaarca16c602022-09-06 18:57:08 +01007218 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007219 return NULL;
7220
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02007221 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01007222 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02007223 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007224 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007225 ht = find_var_ht(name, &varname);
7226 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02007227 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007228 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02007229 if (!HASHITEM_EMPTY(hi))
7230 {
7231 *pht = ht;
7232 break;
7233 }
7234 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007235 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02007236 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007237 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007238 }
7239 current_funccal = old_current_funccal;
7240
7241 return hi;
7242}
7243
7244/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007245 * Search variable in parent scope.
7246 */
7247 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007248find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007249{
7250 dictitem_T *v = NULL;
7251 funccall_T *old_current_funccal = current_funccal;
7252 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007253 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007254
Bram Moolenaarca16c602022-09-06 18:57:08 +01007255 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007256 return NULL;
7257
Bram Moolenaare38eab22019-12-05 21:50:01 +01007258 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01007259 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007260 while (current_funccal)
7261 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007262 ht = find_var_ht(name, &varname);
7263 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007264 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007265 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007266 if (v != NULL)
7267 break;
7268 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007269 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007270 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007271 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007272 }
7273 current_funccal = old_current_funccal;
7274
7275 return v;
7276}
7277
7278/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007279 * Set "copyID + 1" in previous_funccal and callers.
7280 */
7281 int
7282set_ref_in_previous_funccal(int copyID)
7283{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007284 funccall_T *fc;
7285
Bram Moolenaarca16c602022-09-06 18:57:08 +01007286 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007287 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007288 fc->fc_copyID = copyID + 1;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01007289 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL, NULL)
7290 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL, NULL)
7291 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007292 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007293 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007294 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007295}
7296
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007297 static int
7298set_ref_in_funccal(funccall_T *fc, int copyID)
7299{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007300 if (fc->fc_copyID != copyID)
7301 {
7302 fc->fc_copyID = copyID;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01007303 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL, NULL)
7304 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL, NULL)
7305 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL, NULL)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007306 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007307 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007308 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007309 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007310}
7311
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007312/*
7313 * Set "copyID" in all local vars and arguments in the call stack.
7314 */
7315 int
7316set_ref_in_call_stack(int copyID)
7317{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007318 funccall_T *fc;
7319 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007320
Bram Moolenaarca16c602022-09-06 18:57:08 +01007321 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007322 if (set_ref_in_funccal(fc, copyID))
7323 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007324
7325 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007326 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007327 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007328 if (set_ref_in_funccal(fc, copyID))
7329 return TRUE;
7330 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007331}
7332
7333/*
7334 * Set "copyID" in all functions available by name.
7335 */
7336 int
7337set_ref_in_functions(int copyID)
7338{
7339 int todo;
7340 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007341 ufunc_T *fp;
7342
7343 todo = (int)func_hashtab.ht_used;
7344 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007345 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007346 if (!HASHITEM_EMPTY(hi))
7347 {
7348 --todo;
7349 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007350 if (!func_name_refcount(fp->uf_name)
7351 && set_ref_in_func(NULL, fp, copyID))
7352 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007353 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007354 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007355 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007356}
7357
7358/*
7359 * Set "copyID" in all function arguments.
7360 */
7361 int
7362set_ref_in_func_args(int copyID)
7363{
7364 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007365
7366 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007367 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01007368 copyID, NULL, NULL, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007369 return TRUE;
7370 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007371}
7372
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007373/*
7374 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007375 * Returns TRUE if setting references failed somehow.
7376 */
7377 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007378set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007379{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007380 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007381 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007382 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007383 char_u fname_buf[FLEN_FIXED + 1];
7384 char_u *tofree = NULL;
7385 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007386 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007387
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007388 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007389 return FALSE;
7390
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007391 if (fp_in == NULL)
7392 {
7393 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007394 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007395 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007396 if (fp != NULL)
7397 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007398 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007399 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007400 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007401
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007402 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007403 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007404}
7405
Bram Moolenaare38eab22019-12-05 21:50:01 +01007406#endif // FEAT_EVAL