blob: b7d2752dca27d9f2cb5dc1bcfc5f41cdbc918b86 [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];
686static size_t lambda_namelen = 0;
687
zeertzjq21012302025-02-02 08:55:57 +0100688/*
689 * Get a name for a lambda. Returned in static memory.
690 */
Bram Moolenaar04b12692020-05-04 23:24:44 +0200691 char_u *
692get_lambda_name(void)
693{
John Marriottb32800f2025-02-01 15:25:34 +0100694 static int lambda_no = 0;
695 int n;
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)
699 lambda_namelen = 0;
700 else
701 if (n >= (int)sizeof(lambda_name))
702 lambda_namelen = sizeof(lambda_name) - 1;
703 else
704 lambda_namelen = (size_t)n;
705
706 return lambda_name;
707}
708
709/*
710 * Get the length of the last lambda name.
711 */
712 size_t
713get_lambda_name_len(void)
714{
715 return lambda_namelen;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200716}
717
Bram Moolenaare01e5212023-01-08 20:31:18 +0000718/*
719 * Allocate a "ufunc_T" for a function called "name".
720 * Makes sure the size is right.
721 */
722 static ufunc_T *
John Marriottb32800f2025-02-01 15:25:34 +0100723alloc_ufunc(char_u *name, size_t namelen)
Bram Moolenaare01e5212023-01-08 20:31:18 +0000724{
John Marriottb32800f2025-02-01 15:25:34 +0100725 size_t len;
726 ufunc_T *fp;
727
Bram Moolenaare01e5212023-01-08 20:31:18 +0000728 // When the name is short we need to make sure we allocate enough bytes for
729 // the whole struct, including any padding.
John Marriottb32800f2025-02-01 15:25:34 +0100730 len = offsetof(ufunc_T, uf_name) + namelen + 1;
731 fp = alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
732 if (fp != NULL)
733 {
734 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
735 // can actually extend beyond the struct.
736 STRCPY((void *)fp->uf_name, name);
737 fp->uf_namelen = namelen;
738
739 if (name[0] == K_SPECIAL)
740 {
741 len = namelen + 3; // including +1 for NUL
742 fp->uf_name_exp = alloc(len);
743 if (fp->uf_name_exp != NULL)
744 vim_snprintf((char *)fp->uf_name_exp, len, "<SNR>%s", fp->uf_name + 3);
745 }
746 }
747
748 return fp;
Bram Moolenaare01e5212023-01-08 20:31:18 +0000749}
750
Bram Moolenaar801ab062020-06-25 19:27:56 +0200751#if defined(FEAT_LUA) || defined(PROTO)
752/*
753 * Registers a native C callback which can be called from Vim script.
754 * Returns the name of the Vim script function.
755 */
756 char_u *
757register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
758{
759 char_u *name = get_lambda_name();
John Marriottb32800f2025-02-01 15:25:34 +0100760 size_t namelen = get_lambda_name_len();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200761 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200762
John Marriottb32800f2025-02-01 15:25:34 +0100763 fp = alloc_ufunc(name, namelen);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200764 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200765 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200766
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200767 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200768 fp->uf_refcount = 1;
769 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000770 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200771 fp->uf_calls = 0;
772 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200773 fp->uf_cb = cb;
774 fp->uf_cb_free = cb_free;
775 fp->uf_cb_state = state;
776
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000777 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200778
779 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200780}
781#endif
782
Bram Moolenaar04b12692020-05-04 23:24:44 +0200783/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100784 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100785 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100786 * If "white_error" is not NULL check for correct use of white space and set
787 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100788 * Return NULL if no valid arrow found.
789 */
790 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100791skip_arrow(
792 char_u *start,
793 int equal_arrow,
794 char_u **ret_type,
795 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100796{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100797 char_u *s = start;
798 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100799
800 if (equal_arrow)
801 {
802 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100803 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100804 if (white_error != NULL && !VIM_ISWHITE(s[1]))
805 {
806 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100807 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100808 return NULL;
809 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100810 s = skipwhite(s + 1);
811 *ret_type = s;
812 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100813 if (s == *ret_type)
814 {
815 emsg(_(e_missing_return_type));
816 return NULL;
817 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100818 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100819 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100820 s = skipwhite(s);
821 if (*s != '=')
822 return NULL;
823 ++s;
824 }
825 if (*s != '>')
826 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100827 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
828 || !IS_WHITE_OR_NUL(s[1])))
829 {
830 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100831 semsg(_(e_white_space_required_before_and_after_str_at_str),
832 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100833 return NULL;
834 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100835 return skipwhite(s + 1);
836}
837
838/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100839 * Check if "*cmd" points to a function command and if so advance "*cmd" and
840 * return TRUE.
841 * Otherwise return FALSE;
842 * Do not consider "function(" to be a command.
843 */
844 static int
845is_function_cmd(char_u **cmd)
846{
847 char_u *p = *cmd;
848
849 if (checkforcmd(&p, "function", 2))
850 {
851 if (*p == '(')
852 return FALSE;
853 *cmd = p;
854 return TRUE;
855 }
856 return FALSE;
857}
858
859/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200860 * Called when defining a function: The context may be needed for script
861 * variables declared in a block that is visible now but not when the function
862 * is compiled or called later.
863 */
864 static void
865function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
866{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000867 if (cstack == NULL || cstack->cs_idx < 0)
868 return;
869
870 int count = cstack->cs_idx + 1;
871 int i;
872
873 fp->uf_block_ids = ALLOC_MULT(int, count);
874 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200875 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000876 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
877 sizeof(int) * count);
878 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200879 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000880
881 // Set flag in each block to indicate a function was defined. This
882 // is used to keep the variable when leaving the block, see
883 // hide_script_var().
884 for (i = 0; i <= cstack->cs_idx; ++i)
885 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200886}
887
888/*
Yegappan Lakshmanan0072cee2025-01-07 20:22:32 +0100889 * Skip over all the characters in a single quoted string starting at "p" and
890 * return a pointer to the character following the ending single quote.
891 * If the ending single quote is missing, then return a pointer to the NUL
892 * character.
893 */
894 static char_u *
895skip_single_quote_string(char_u *p)
896{
897 p++; // skip the beginning single quote
898 while (*p != NUL)
899 {
900 // Within the string, a single quote can be escaped by using
901 // two single quotes.
902 if (*p == '\'' && *(p + 1) == '\'')
903 p += 2;
904 else if (*p == '\'')
905 {
906 p++; // skip the ending single quote
907 break;
908 }
909 else
910 MB_PTR_ADV(p);
911 }
912
913 return p;
914}
915
916/*
917 * Skip over all the characters in a double quoted string starting at "p" and
918 * return a pointer to the character following the ending double quote.
919 * If the ending double quote is missing, then return a pointer to the NUL
920 * character.
921 */
922 static char_u *
923skip_double_quote_string(char_u *p)
924{
925 p++; // skip the beginning double quote
926 while (*p != NUL)
927 {
928 // Within the string, a double quote can be escaped by
929 // preceding it with a backslash.
930 if (*p == '\\' && *(p + 1) == '"')
931 p += 2;
932 else if (*p == '"')
933 {
934 p++; // skip the ending double quote
935 break;
936 }
937 else
938 MB_PTR_ADV(p);
939 }
940
941 return p;
942}
943
944/*
945 * Return the start of a Vim9 comment (#) in the line starting at "line".
946 * If a comment is not found, then returns a pointer to the end of the
947 * string (NUL).
948 */
949 static char_u *
950find_start_of_vim9_comment(char_u *line)
951{
952 char_u *p = line;
953
954 while (*p != NUL)
955 {
956 if (*p == '\'')
957 // Skip a single quoted string.
958 p = skip_single_quote_string(p);
959 else if (*p == '"')
960 // Skip a double quoted string.
961 p = skip_double_quote_string(p);
962 else
963 {
964 if (*p == '#')
965 // Found the start of a Vim9 comment
966 break;
967 MB_PTR_ADV(p);
968 }
969 }
970
971 return p;
972}
973
974/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100975 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200976 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100977 * "newlines" must already have been initialized.
978 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
979 */
980 static int
981get_function_body(
982 exarg_T *eap,
983 garray_T *newlines,
984 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000985 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100986{
987 linenr_T sourcing_lnum_top = SOURCING_LNUM;
988 linenr_T sourcing_lnum_off;
989 int saved_wait_return = need_wait_return;
990 char_u *line_arg = line_arg_in;
991 int vim9_function = eap->cmdidx == CMD_def
992 || eap->cmdidx == CMD_block;
993#define MAX_FUNC_NESTING 50
994 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200995 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100996 int nesting = 0;
997 getline_opt_T getline_options;
998 int indent = 2;
999 char_u *skip_until = NULL;
1000 int ret = FAIL;
1001 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +02001002 int heredoc_concat_len = 0;
1003 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001004 char_u *heredoc_trimmed = NULL;
John Marriottb32800f2025-02-01 15:25:34 +01001005 size_t heredoc_trimmedlen = 0;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001006
Bram Moolenaar20677332021-06-06 17:02:53 +02001007 ga_init2(&heredoc_ga, 1, 500);
1008
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001009 // Detect having skipped over comment lines to find the return
1010 // type. Add NULL lines to keep the line count correct.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001011 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001012 if (SOURCING_LNUM < sourcing_lnum_off)
1013 {
1014 sourcing_lnum_off -= SOURCING_LNUM;
1015 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
1016 goto theend;
1017 while (sourcing_lnum_off-- > 0)
1018 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1019 }
1020
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001021 nesting_def[0] = vim9_function;
1022 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001023 getline_options = vim9_function
1024 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
1025 for (;;)
1026 {
1027 char_u *theline;
1028 char_u *p;
1029 char_u *arg;
1030
1031 if (KeyTyped)
1032 {
1033 msg_scroll = TRUE;
1034 saved_wait_return = FALSE;
1035 }
1036 need_wait_return = FALSE;
1037
1038 if (line_arg != NULL)
1039 {
1040 // Use eap->arg, split up in parts by line breaks.
1041 theline = line_arg;
1042 p = vim_strchr(theline, '\n');
1043 if (p == NULL)
1044 line_arg += STRLEN(line_arg);
1045 else
1046 {
1047 *p = NUL;
1048 line_arg = p + 1;
1049 }
1050 }
1051 else
1052 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001053 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001054 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001055 }
1056 if (KeyTyped)
1057 lines_left = Rows - 1;
1058 if (theline == NULL)
1059 {
1060 // Use the start of the function for the line number.
1061 SOURCING_LNUM = sourcing_lnum_top;
1062 if (skip_until != NULL)
1063 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001064 else if (nesting_inline[nesting])
1065 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001066 else if (eap->cmdidx == CMD_def)
1067 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001068 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001069 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001070 goto theend;
1071 }
1072
1073 // Detect line continuation: SOURCING_LNUM increased more than one.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001074 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001075 if (SOURCING_LNUM < sourcing_lnum_off)
1076 sourcing_lnum_off -= SOURCING_LNUM;
1077 else
1078 sourcing_lnum_off = 0;
1079
1080 if (skip_until != NULL)
1081 {
1082 // Don't check for ":endfunc"/":enddef" between
1083 // * ":append" and "."
1084 // * ":python <<EOF" and "EOF"
1085 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
1086 if (heredoc_trimmed == NULL
1087 || (is_heredoc && skipwhite(theline) == theline)
1088 || STRNCMP(theline, heredoc_trimmed,
John Marriottb32800f2025-02-01 15:25:34 +01001089 heredoc_trimmedlen) == 0)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001090 {
1091 if (heredoc_trimmed == NULL)
1092 p = theline;
1093 else if (is_heredoc)
1094 p = skipwhite(theline) == theline
John Marriottb32800f2025-02-01 15:25:34 +01001095 ? theline : theline + heredoc_trimmedlen;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001096 else
John Marriottb32800f2025-02-01 15:25:34 +01001097 p = theline + heredoc_trimmedlen;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001098 if (STRCMP(p, skip_until) == 0)
1099 {
1100 VIM_CLEAR(skip_until);
1101 VIM_CLEAR(heredoc_trimmed);
John Marriottb32800f2025-02-01 15:25:34 +01001102 heredoc_trimmedlen = 0;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001103 getline_options = vim9_function
1104 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
1105 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +02001106
1107 if (heredoc_concat_len > 0)
1108 {
1109 // Replace the starting line with all the concatenated
1110 // lines.
1111 ga_concat(&heredoc_ga, theline);
1112 vim_free(((char_u **)(newlines->ga_data))[
1113 heredoc_concat_len - 1]);
1114 ((char_u **)(newlines->ga_data))[
1115 heredoc_concat_len - 1] = heredoc_ga.ga_data;
1116 ga_init(&heredoc_ga);
1117 heredoc_concat_len = 0;
1118 theline += STRLEN(theline); // skip the "EOF"
1119 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001120 }
1121 }
1122 }
1123 else
1124 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001125 int c;
1126 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +00001127 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001128
1129 // skip ':' and blanks
1130 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
1131 ;
1132
1133 // Check for "endfunction", "enddef" or "}".
1134 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +00001135 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001136 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001137 ? *p == '}'
1138 : (checkforcmd(&p, nesting_def[nesting]
1139 ? "enddef" : "endfunction", 4)
1140 && *p != ':'))
1141 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00001142 if (!nesting_inline[nesting] && nesting_def[nesting]
1143 && p < cmd + 6)
1144 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001145 if (nesting-- == 0)
1146 {
1147 char_u *nextcmd = NULL;
1148
1149 if (*p == '|' || *p == '}')
1150 nextcmd = p + 1;
1151 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
1152 nextcmd = line_arg;
1153 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001154 && (vim9_function || p_verbose > 0))
1155 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +02001156 SOURCING_LNUM = sourcing_lnum_top
1157 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001158 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +00001159 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001160 else
1161 give_warning2((char_u *)
1162 _("W22: Text found after :endfunction: %s"),
1163 p, TRUE);
1164 }
1165 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001166 {
1167 // Another command follows. If the line came from "eap"
1168 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001169 // change "eap->cmdlinep" to point to the last fetched
1170 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001171 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001172 if (lines_to_free->ga_len > 0
1173 && *eap->cmdlinep !=
1174 ((char_u **)lines_to_free->ga_data)
1175 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001176 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001177 // *cmdlinep will be freed later, thus remove the
1178 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001179 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001180 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
1181 [lines_to_free->ga_len - 1];
1182 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001183 }
1184 }
1185 break;
1186 }
1187 }
1188
1189 // Check for mismatched "endfunc" or "enddef".
1190 // We don't check for "def" inside "func" thus we also can't check
1191 // for "enddef".
1192 // We continue to find the end of the function, although we might
1193 // not find it.
1194 else if (nesting_def[nesting])
1195 {
1196 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1197 emsg(_(e_mismatched_endfunction));
1198 }
1199 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1200 emsg(_(e_mismatched_enddef));
1201
1202 // Increase indent inside "if", "while", "for" and "try", decrease
1203 // at "end".
1204 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1205 indent -= 2;
1206 else if (STRNCMP(p, "if", 2) == 0
1207 || STRNCMP(p, "wh", 2) == 0
1208 || STRNCMP(p, "for", 3) == 0
1209 || STRNCMP(p, "try", 3) == 0)
1210 indent += 2;
1211
1212 // Check for defining a function inside this function.
1213 // Only recognize "def" inside "def", not inside "function",
1214 // For backwards compatibility, see Test_function_python().
1215 c = *p;
1216 if (is_function_cmd(&p)
1217 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1218 {
1219 if (*p == '!')
1220 p = skipwhite(p + 1);
1221 p += eval_fname_script(p);
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001222 vim_free(trans_function_name(&p, NULL, TRUE, 0));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001223 if (*skipwhite(p) == '(')
1224 {
1225 if (nesting == MAX_FUNC_NESTING - 1)
1226 emsg(_(e_function_nesting_too_deep));
1227 else
1228 {
1229 ++nesting;
1230 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001231 nesting_inline[nesting] = FALSE;
1232 indent += 2;
1233 }
1234 }
1235 }
1236
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001237 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001238 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001239 // Not a comment line: check for nested inline function.
Yegappan Lakshmanan0072cee2025-01-07 20:22:32 +01001240
1241 if (nesting_inline[nesting])
1242 {
1243 // A comment (#) can follow the opening curly brace of a
1244 // block statement. Need to ignore the comment and look
1245 // for the opening curly brace before the comment.
1246 end = find_start_of_vim9_comment(p) - 1;
1247 }
1248 else
1249 end = p + STRLEN(p) - 1;
1250
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001251 while (end > p && VIM_ISWHITE(*end))
1252 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001253 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001254 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001255 int is_block;
1256
1257 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001258 --end;
1259 while (end > p && VIM_ISWHITE(*end))
1260 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001261 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1262 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001263 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001264 char_u *s = p;
1265
1266 // check for line starting with "au" for :autocmd or
1267 // "com" for :command, these can use a {} block
1268 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1269 || checkforcmd_noparen(&s, "command", 3);
1270 }
1271
1272 if (is_block)
1273 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001274 if (nesting == MAX_FUNC_NESTING - 1)
1275 emsg(_(e_function_nesting_too_deep));
1276 else
1277 {
1278 ++nesting;
1279 nesting_def[nesting] = TRUE;
1280 nesting_inline[nesting] = TRUE;
1281 indent += 2;
1282 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001283 }
1284 }
1285 }
1286
1287 // Check for ":append", ":change", ":insert". Not for :def.
1288 p = skip_range(p, FALSE, NULL);
1289 if (!vim9_function
1290 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1291 || (p[0] == 'c'
1292 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1293 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1294 && (STRNCMP(&p[3], "nge", 3) != 0
1295 || !ASCII_ISALPHA(p[6])))))))
1296 || (p[0] == 'i'
1297 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1298 && (!ASCII_ISALPHA(p[2])
1299 || (p[2] == 's'
1300 && (!ASCII_ISALPHA(p[3])
1301 || p[3] == 'e'))))))))
John Marriottb32800f2025-02-01 15:25:34 +01001302 skip_until = vim_strnsave((char_u *)".", 1);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001303
1304 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1305 arg = skipwhite(skiptowhite(p));
1306 if (arg[0] == '<' && arg[1] =='<'
1307 && ((p[0] == 'p' && p[1] == 'y'
1308 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1309 || ((p[2] == '3' || p[2] == 'x')
1310 && !ASCII_ISALPHA(p[3]))))
1311 || (p[0] == 'p' && p[1] == 'e'
1312 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1313 || (p[0] == 't' && p[1] == 'c'
1314 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1315 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1316 && !ASCII_ISALPHA(p[3]))
1317 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1318 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1319 || (p[0] == 'm' && p[1] == 'z'
1320 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1321 ))
1322 {
1323 // ":python <<" continues until a dot, like ":append"
1324 p = skipwhite(arg + 2);
zeertzjq449c2e52025-02-03 18:56:16 +01001325 if (STRNCMP(p, "trim", 4) == 0
1326 && (p[4] == NUL || VIM_ISWHITE(p[4])))
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001327 {
1328 // Ignore leading white space.
1329 p = skipwhite(p + 4);
John Marriottb32800f2025-02-01 15:25:34 +01001330 heredoc_trimmedlen = skipwhite(theline) - theline;
1331 heredoc_trimmed = vim_strnsave(theline, heredoc_trimmedlen);
1332 if (heredoc_trimmed == NULL)
1333 heredoc_trimmedlen = 0;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001334 }
1335 if (*p == NUL)
John Marriottb32800f2025-02-01 15:25:34 +01001336 skip_until = vim_strnsave((char_u *)".", 1);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001337 else
1338 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1339 getline_options = GETLINE_NONE;
1340 is_heredoc = TRUE;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001341 if (vim9_function && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001342 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001343 }
1344
Bram Moolenaard881d152022-05-13 13:50:36 +01001345 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001346 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001347 // Check for ":cmd v =<< [trim] EOF"
1348 // and ":cmd [a, b] =<< [trim] EOF"
1349 // and "lines =<< [trim] EOF" for Vim9
1350 // Where "cmd" can be "let", "var", "final" or "const".
zeertzjqa93d9cd2023-05-02 16:25:47 +01001351 arg = p;
1352 if (checkforcmd(&arg, "let", 2)
1353 || checkforcmd(&arg, "var", 3)
1354 || checkforcmd(&arg, "final", 5)
1355 || checkforcmd(&arg, "const", 5)
1356 || vim9_function)
Bram Moolenaard881d152022-05-13 13:50:36 +01001357 {
zeertzjq9a91d2b2024-04-09 21:47:10 +02001358 int save_sc_version = current_sctx.sc_version;
1359 int var_count = 0;
1360 int semicolon = 0;
zeertzjq9a91d2b2024-04-09 21:47:10 +02001361
1362 current_sctx.sc_version
1363 = vim9_function ? SCRIPT_VERSION_VIM9 : 1;
zeertzjq1817ccd2024-04-10 17:37:47 +02001364 arg = skip_var_list(arg, TRUE, &var_count, &semicolon,
zeertzjq9a91d2b2024-04-09 21:47:10 +02001365 TRUE);
zeertzjq1817ccd2024-04-10 17:37:47 +02001366 if (arg != NULL)
1367 arg = skipwhite(arg);
zeertzjq9a91d2b2024-04-09 21:47:10 +02001368 current_sctx.sc_version = save_sc_version;
zeertzjq1817ccd2024-04-10 17:37:47 +02001369 if (arg != NULL && STRNCMP(arg, "=<<", 3) == 0)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001370 {
zeertzjq449c2e52025-02-03 18:56:16 +01001371 int has_trim = FALSE;
1372
Bram Moolenaard881d152022-05-13 13:50:36 +01001373 p = skipwhite(arg + 3);
1374 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001375 {
zeertzjq449c2e52025-02-03 18:56:16 +01001376 if (STRNCMP(p, "trim", 4) == 0
1377 && (p[4] == NUL || VIM_ISWHITE(p[4])))
Bram Moolenaard881d152022-05-13 13:50:36 +01001378 {
1379 // Ignore leading white space.
1380 p = skipwhite(p + 4);
zeertzjq449c2e52025-02-03 18:56:16 +01001381 has_trim = TRUE;
Bram Moolenaard881d152022-05-13 13:50:36 +01001382 continue;
1383 }
zeertzjq449c2e52025-02-03 18:56:16 +01001384 if (STRNCMP(p, "eval", 4) == 0
1385 && (p[4] == NUL || VIM_ISWHITE(p[4])))
Bram Moolenaard881d152022-05-13 13:50:36 +01001386 {
1387 // Ignore leading white space.
1388 p = skipwhite(p + 4);
1389 continue;
1390 }
1391 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001392 }
zeertzjq449c2e52025-02-03 18:56:16 +01001393 if (has_trim)
1394 {
1395 heredoc_trimmedlen = skipwhite(theline) - theline;
1396 heredoc_trimmed = vim_strnsave(theline, heredoc_trimmedlen);
1397 if (heredoc_trimmed == NULL)
1398 heredoc_trimmedlen = 0;
1399 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001400 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1401 getline_options = GETLINE_NONE;
1402 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001403 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001404 }
1405 }
1406 }
1407
1408 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001409 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001410 goto theend;
1411
Bram Moolenaar20677332021-06-06 17:02:53 +02001412 if (heredoc_concat_len > 0)
1413 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001414 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001415 // to be used for the instruction later.
1416 ga_concat(&heredoc_ga, theline);
1417 ga_concat(&heredoc_ga, (char_u *)"\n");
John Marriottb32800f2025-02-01 15:25:34 +01001418 p = vim_strnsave((char_u *)"", 0);
Bram Moolenaar20677332021-06-06 17:02:53 +02001419 }
1420 else
1421 {
1422 // Copy the line to newly allocated memory. get_one_sourceline()
1423 // allocates 250 bytes per line, this saves 80% on average. The
1424 // cost is an extra alloc/free.
1425 p = vim_strsave(theline);
1426 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001427 if (p == NULL)
1428 goto theend;
1429 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1430
1431 // Add NULL lines for continuation lines, so that the line count is
1432 // equal to the index in the growarray.
1433 while (sourcing_lnum_off-- > 0)
1434 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1435
1436 // Check for end of eap->arg.
1437 if (line_arg != NULL && *line_arg == NUL)
1438 line_arg = NULL;
1439 }
1440
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001441 // Return OK when no error was detected.
1442 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001443 ret = OK;
1444
1445theend:
1446 vim_free(skip_until);
1447 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001448 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001449 need_wait_return |= saved_wait_return;
1450 return ret;
1451}
1452
1453/*
1454 * Handle the body of a lambda. *arg points to the "{", process statements
1455 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001456 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001457 * When successful "rettv" is set to a funcref.
1458 */
1459 static int
1460lambda_function_body(
1461 char_u **arg,
1462 typval_T *rettv,
1463 evalarg_T *evalarg,
1464 garray_T *newargs,
1465 garray_T *argtypes,
1466 int varargs,
1467 garray_T *default_args,
1468 char_u *ret_type)
1469{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001470 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001471 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001472 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001473 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001474 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001475 exarg_T eap;
1476 garray_T newlines;
1477 char_u *cmdline = NULL;
1478 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001479 partial_T *pt;
1480 char_u *name;
John Marriottb32800f2025-02-01 15:25:34 +01001481 size_t namelen;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001482 int lnum_save = -1;
1483 linenr_T sourcing_lnum_top = SOURCING_LNUM;
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001484 char_u *line_arg = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001485
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001486 *arg = skipwhite(*arg + 1);
1487 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001488 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001489 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001490 return FAIL;
1491 }
1492
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001493 // When there is a line break use what follows for the lambda body.
1494 // Makes lambda body initializers work for object and enum member
1495 // variables.
1496 if (**arg == '\n')
1497 line_arg = *arg + 1;
1498
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001499 CLEAR_FIELD(eap);
1500 eap.cmdidx = CMD_block;
1501 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001502 eap.cmdlinep = &cmdline;
1503 eap.skip = !evaluate;
1504 if (evalarg->eval_cctx != NULL)
1505 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1506 else
1507 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001508 eap.ea_getline = evalarg->eval_getline;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001509 eap.cookie = evalarg->eval_cookie;
1510 }
1511
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001512 ga_init2(&newlines, sizeof(char_u *), 10);
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001513 if (get_function_body(&eap, &newlines, line_arg,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001514 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001515 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001516
1517 // When inside a lambda must add the function lines to evalarg.eval_ga.
1518 evalarg->eval_break_count += newlines.ga_len;
1519 if (gap->ga_itemsize > 0)
1520 {
1521 int idx;
1522 char_u *last;
1523 size_t plen;
1524 char_u *pnl;
1525
1526 for (idx = 0; idx < newlines.ga_len; ++idx)
1527 {
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001528 char_u *p = ((char_u **)newlines.ga_data)[idx];
1529 if (p == NULL)
1530 // comment line in the lambda body
1531 continue;
1532
1533 p = skipwhite(p);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001534
Bram Moolenaarecb66452021-05-18 15:09:18 +02001535 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001536 goto erret;
1537
1538 // Going to concatenate the lines after parsing. For an empty or
1539 // comment line use an empty string.
1540 // Insert NL characters at the start of each line, the string will
1541 // be split again later in .get_lambda_tv().
1542 if (*p == NUL || vim9_comment_start(p))
John Marriottb32800f2025-02-01 15:25:34 +01001543 {
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001544 p = (char_u *)"";
John Marriottb32800f2025-02-01 15:25:34 +01001545 plen = 0;
1546 }
1547 else
1548 plen = STRLEN(p);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001549 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1550 if (pnl != NULL)
1551 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001552 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1553 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001554 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001555 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001556 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001557 if (eap.nextcmd != NULL)
John Marriottb32800f2025-02-01 15:25:34 +01001558 {
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001559 // more is following after the "}", which was skipped
1560 last = cmdline;
John Marriottb32800f2025-02-01 15:25:34 +01001561 plen = STRLEN(last);
1562 }
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001563 else
John Marriottb32800f2025-02-01 15:25:34 +01001564 {
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001565 // nothing is following the "}"
1566 last = (char_u *)"}";
John Marriottb32800f2025-02-01 15:25:34 +01001567 plen = 1;
1568 }
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001569 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1570 if (pnl != NULL)
1571 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001572 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1573 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001574 }
1575
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001576 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001577 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001578 garray_T *tfgap = &evalarg->eval_tofree_ga;
1579
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001580 // Something comes after the "}".
1581 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001582
1583 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001584 if (ga_grow(tfgap, 1) == OK)
1585 {
1586 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1587 evalarg->eval_using_cmdline = TRUE;
1588 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001589 }
1590 else
1591 *arg = (char_u *)"";
1592
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001593 if (!evaluate)
1594 {
1595 ret = OK;
1596 goto erret;
1597 }
1598
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001599 name = get_lambda_name();
John Marriottb32800f2025-02-01 15:25:34 +01001600 namelen = get_lambda_name_len();
1601 ufunc = alloc_ufunc(name, namelen);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001602 if (ufunc == NULL)
1603 goto erret;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001604 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001605 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001606 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001607 ufunc->uf_refcount = 1;
1608 ufunc->uf_args = *newargs;
1609 newargs->ga_data = NULL;
1610 ufunc->uf_def_args = *default_args;
1611 default_args->ga_data = NULL;
1612 ufunc->uf_func_type = &t_func_any;
1613
1614 // error messages are for the first function line
1615 lnum_save = SOURCING_LNUM;
1616 SOURCING_LNUM = sourcing_lnum_top;
1617
1618 // parse argument types
h-eastb895b0f2023-09-24 15:46:31 +02001619 if (parse_argument_types(ufunc, argtypes, varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001620 {
1621 SOURCING_LNUM = lnum_save;
1622 goto erret;
1623 }
1624
1625 // parse the return type, if any
1626 if (parse_return_type(ufunc, ret_type) == FAIL)
1627 goto erret;
1628
1629 pt = ALLOC_CLEAR_ONE(partial_T);
1630 if (pt == NULL)
1631 goto erret;
1632 pt->pt_func = ufunc;
1633 pt->pt_refcount = 1;
1634
1635 ufunc->uf_lines = newlines;
1636 newlines.ga_data = NULL;
1637 if (sandbox)
1638 ufunc->uf_flags |= FC_SANDBOX;
1639 if (!ASCII_ISUPPER(*ufunc->uf_name))
1640 ufunc->uf_flags |= FC_VIM9;
1641 ufunc->uf_script_ctx = current_sctx;
1642 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1643 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1644 set_function_type(ufunc);
1645
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001646 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1647
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001648 rettv->vval.v_partial = pt;
1649 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001650 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001651 ret = OK;
1652
1653erret:
1654 if (lnum_save >= 0)
1655 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001656 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001657 if (newargs != NULL)
1658 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001659 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001660 if (ufunc != NULL)
1661 {
1662 func_clear(ufunc, TRUE);
1663 func_free(ufunc, TRUE);
1664 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001665 return ret;
1666}
1667
1668/*
1669 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001670 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001671 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001672 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1673 */
1674 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001675get_lambda_tv(
1676 char_u **arg,
1677 typval_T *rettv,
1678 int types_optional,
1679 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001680{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001681 int evaluate = evalarg != NULL
1682 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001683 garray_T newargs;
1684 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001685 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001686 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001687 garray_T default_args;
h-eastb895b0f2023-09-24 15:46:31 +02001688 garray_T arg_objm;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001689 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001690 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001691 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001692 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001693 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001694 char_u *s;
1695 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001696 int *old_eval_lavars = eval_lavars_used;
1697 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001698 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001699 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001700 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001701 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001702 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001703 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001704
Bram Moolenaar4525a572022-02-13 11:57:33 +00001705 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001706 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001707
1708 ga_init(&newargs);
1709 ga_init(&newlines);
1710
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001711 // First, check if this is really a lambda expression. "->" or "=>" must
1712 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001713 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001714 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001715 types_optional ? &argtypes : NULL, types_optional,
1716 types_optional ? &arg_objm : NULL, evalarg,
1717 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001718 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001719 {
1720 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001721 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001722 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001723 ga_clear(&arg_objm);
1724 }
Bram Moolenaar0346b792021-01-31 22:18:29 +01001725 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001726 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001727
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001728 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001729 if (evaluate)
1730 pnewargs = &newargs;
1731 else
1732 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001733 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001734 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
h-eastb895b0f2023-09-24 15:46:31 +02001735 types_optional ? &argtypes : NULL, types_optional,
1736 types_optional ? &arg_objm : NULL, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001737 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001738 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001739 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001740 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001741 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001742 {
1743 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001744 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001745 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001746 ga_clear(&arg_objm);
1747 }
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001748 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001749 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001750 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001751 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001752
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001753 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1754 if (ret_type != NULL)
1755 {
1756 ret_type = vim_strsave(ret_type);
1757 tofree2 = ret_type;
1758 }
1759
Bram Moolenaare38eab22019-12-05 21:50:01 +01001760 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001761 if (evaluate)
1762 eval_lavars_used = &eval_lavars;
1763
Bram Moolenaar65c44152020-12-24 15:14:01 +01001764 *arg = skipwhite_and_linebreak(*arg, evalarg);
1765
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001766 // Recognize "{" as the start of a function body.
1767 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001768 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001769 if (evalarg == NULL)
1770 // cannot happen?
1771 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001772 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001773 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1774 types_optional ? &argtypes : NULL, varargs,
1775 &default_args, ret_type) == FAIL)
1776 goto errret;
1777 goto theend;
1778 }
1779 if (default_args.ga_len > 0)
1780 {
1781 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001782 goto errret;
1783 }
1784
Bram Moolenaare38eab22019-12-05 21:50:01 +01001785 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001786 start = *arg;
1787 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001788 if (ret == FAIL)
1789 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001790
Bram Moolenaar65c44152020-12-24 15:14:01 +01001791 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001792 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001793 *arg = skipwhite_and_linebreak(*arg, evalarg);
1794 if (**arg != '}')
1795 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001796 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001797 goto errret;
1798 }
1799 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001800 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001801
1802 if (evaluate)
1803 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001804 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001805 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001806 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001807 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001808 char_u *name = get_lambda_name();
John Marriottb32800f2025-02-01 15:25:34 +01001809 size_t namelen = get_lambda_name_len();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001810
John Marriottb32800f2025-02-01 15:25:34 +01001811 fp = alloc_ufunc(name, namelen);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001812 if (fp == NULL)
1813 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001814 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001815 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001816 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001817 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001818
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001819 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001820 if (ga_grow(&newlines, 1) == FAIL)
1821 goto errret;
1822
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001823 // If there are line breaks, we need to split up the string.
1824 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001825 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001826 line_end = end;
1827
1828 // Add "return " before the expression (or the first line).
1829 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001830 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001831 if (p == NULL)
1832 goto errret;
1833 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1834 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001835 vim_strncpy(p + 7, start, line_end - start);
1836
1837 if (line_end != end)
1838 {
1839 // Add more lines, split by line breaks. Thus is used when a
1840 // lambda with { cmds } is encountered.
1841 while (*line_end == '\n')
1842 {
1843 if (ga_grow(&newlines, 1) == FAIL)
1844 goto errret;
1845 start = line_end + 1;
1846 line_end = vim_strchr(start, '\n');
1847 if (line_end == NULL)
1848 line_end = end;
1849 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1850 vim_strnsave(start, line_end - start);
1851 }
1852 }
1853
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001854 if (strstr((char *)p + 7, "a:") == NULL)
1855 // No a: variables are used for sure.
1856 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001857
1858 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001859 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001860 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001861 if (types_optional)
1862 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001863 if (parse_argument_types(fp, &argtypes,
h-eastb895b0f2023-09-24 15:46:31 +02001864 vim9script && varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001865 goto errret;
1866 if (ret_type != NULL)
1867 {
1868 fp->uf_ret_type = parse_type(&ret_type,
1869 &fp->uf_type_list, TRUE);
1870 if (fp->uf_ret_type == NULL)
1871 goto errret;
1872 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001873 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001874 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001875 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001876
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001877 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001878 if (current_funccal != NULL && eval_lavars)
1879 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001880 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001881 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001882 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001883 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001884
1885#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001886 if (prof_def_func())
1887 func_do_profile(fp);
1888#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001889 if (sandbox)
1890 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001891 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001892 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001893 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001894 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001895 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001896 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001897 // Use the line number of the arguments.
1898 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001899
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001900 function_using_block_scopes(fp, evalarg->eval_cstack);
1901
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001902 pt->pt_func = fp;
1903 pt->pt_refcount = 1;
1904 rettv->vval.v_partial = pt;
1905 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001906
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001907 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001908 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001909
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001910theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001911 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001912 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001913 if (types_optional)
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +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);
1917 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001918
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001919 return OK;
1920
1921errret:
1922 ga_clear_strings(&newargs);
1923 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001924 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001925 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001926 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001927 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001928 ga_clear(&arg_objm);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001929 }
John Marriottb32800f2025-02-01 15:25:34 +01001930 if (fp != NULL)
1931 {
1932 vim_free(fp->uf_arg_types);
1933 vim_free(fp->uf_name_exp);
1934 vim_free(fp);
1935 }
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001936 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001937 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001938 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001939 return FAIL;
1940}
1941
1942/*
1943 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1944 * name it contains, otherwise return "name".
1945 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1946 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001947 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1948 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001949 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001950 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001951 */
1952 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001953deref_func_name(
1954 char_u *name,
1955 int *lenp,
1956 partial_T **partialp,
1957 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001958 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001959 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001960 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001961{
1962 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001963 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001964 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001965 char_u *s = NULL;
1966 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001967 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001968
1969 if (partialp != NULL)
1970 *partialp = NULL;
1971
1972 cc = name[*lenp];
1973 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001974
Bram Moolenaar71f21932022-01-07 18:20:55 +00001975 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001976 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001977 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001978 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001979 tv = &v->di_tv;
1980 }
1981 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1982 {
1983 imported_T *import;
1984 char_u *p = name;
1985 int len = *lenp;
1986
1987 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001988 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001989 p = name + 2;
1990 len -= 2;
1991 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001992 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001993
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001994 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001995 if (import != NULL)
1996 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001997 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001998 if (new_function)
1999 semsg(_(e_redefining_imported_item_str), name);
2000 else
2001 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002002 name[len] = cc;
2003 *lenp = 0;
2004 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01002005 }
2006 }
2007
2008 if (tv != NULL)
2009 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002010 if (found_var != NULL)
2011 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01002012 if (tv->v_type == VAR_FUNC)
2013 {
2014 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002015 {
2016 *lenp = 0;
2017 return (char_u *)""; // just in case
2018 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01002019 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002020 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002021 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002022
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01002023 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002024 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01002025 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002026
2027 if (pt == NULL)
2028 {
2029 *lenp = 0;
2030 return (char_u *)""; // just in case
2031 }
2032 if (partialp != NULL)
2033 *partialp = pt;
2034 s = partial_name(pt);
2035 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002036 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002037
2038 if (s != NULL)
2039 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01002040 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002041 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002042 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002043
2044 if (sv != NULL)
2045 *type = sv->sv_type;
2046 }
2047 return s;
2048 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002049 }
2050
2051 return name;
2052}
2053
2054/*
2055 * Give an error message with a function name. Handle <SNR> things.
2056 * "ermsg" is to be passed without translation, use N_() instead of _().
2057 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002058 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002059emsg_funcname(char *ermsg, char_u *name)
2060{
Bram Moolenaar54598062022-01-13 12:05:09 +00002061 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002062
Bram Moolenaar54598062022-01-13 12:05:09 +00002063 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002064 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002065 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002066 if (p != name)
2067 vim_free(p);
2068}
2069
2070/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002071 * Get function arguments at "*arg" and advance it.
2072 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01002073 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002074 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002075 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002076get_func_arguments(
2077 char_u **arg,
2078 evalarg_T *evalarg,
2079 int partial_argc,
2080 typval_T *argvars,
Ernie Raelb077b582023-12-14 20:11:44 +01002081 int *argcount,
2082 int is_builtin)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002083{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002084 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002085 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002086 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002087 int evaluate = evalarg == NULL
2088 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002089
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002090 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002091 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02002092 // skip the '(' or ',' and possibly line breaks
2093 argp = skipwhite_and_linebreak(argp + 1, evalarg);
2094
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002095 if (*argp == ')' || *argp == ',' || *argp == NUL)
2096 break;
Ernie Raelb077b582023-12-14 20:11:44 +01002097
2098 int arg_idx = *argcount;
2099 if (eval1(&argp, &argvars[arg_idx], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002100 {
2101 ret = FAIL;
2102 break;
2103 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002104 ++*argcount;
Ernie Raelb077b582023-12-14 20:11:44 +01002105 if (!is_builtin && check_typval_is_value(&argvars[arg_idx]) == FAIL)
2106 {
2107 ret = FAIL;
2108 break;
2109 }
2110
Bram Moolenaar9d489562020-07-30 20:08:50 +02002111 // The comma should come right after the argument, but this wasn't
2112 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002113 if (vim9script)
2114 {
2115 if (*argp != ',' && *skipwhite(argp) == ',')
2116 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002117 if (evaluate)
2118 semsg(_(e_no_white_space_allowed_before_str_str),
2119 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002120 ret = FAIL;
2121 break;
2122 }
2123 }
2124 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002125 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002126 if (*argp != ',')
2127 break;
Christian Brabandt00cb2472023-09-05 20:46:25 +02002128 if (vim9script && !IS_WHITE_NL_OR_NUL(argp[1]))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002129 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002130 if (evaluate)
2131 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002132 ret = FAIL;
2133 break;
2134 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002135 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002136
Bram Moolenaare6b53242020-07-01 17:28:33 +02002137 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002138 if (*argp == ')')
2139 ++argp;
2140 else
2141 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002142 *arg = argp;
2143 return ret;
2144}
2145
2146/*
2147 * Call a function and put the result in "rettv".
2148 * Return OK or FAIL.
2149 */
2150 int
2151get_func_tv(
2152 char_u *name, // name of the function
2153 int len, // length of "name" or -1 to use strlen()
2154 typval_T *rettv,
2155 char_u **arg, // argument, pointing to the '('
2156 evalarg_T *evalarg, // for line continuation
2157 funcexe_T *funcexe) // various values
2158{
2159 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002160 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002161 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
2162 int argcount = 0; // number of arguments found
2163 int vim9script = in_vim9script();
2164 int evaluate = evalarg == NULL
2165 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
2166
2167 argp = *arg;
2168 ret = get_func_arguments(&argp, evalarg,
2169 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
Ernie Raelb077b582023-12-14 20:11:44 +01002170 argvars, &argcount, builtin_function(name, -1));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002171
2172 if (ret == OK)
2173 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002174 int i = 0;
2175 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002176
2177 if (get_vim_var_nr(VV_TESTING))
2178 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002179 // Prepare for calling test_garbagecollect_now(), need to know
2180 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002181 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002182 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002183 for (i = 0; i < argcount; ++i)
2184 if (ga_grow(&funcargs, 1) == OK)
2185 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
2186 &argvars[i];
2187 }
2188
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002189 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00002190 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002191 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002192 // An error in a builtin function does not return FAIL, but we do
2193 // want to abort further processing if an error was given.
2194 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002195 clear_tv(rettv);
2196 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002197
2198 funcargs.ga_len -= i;
2199 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002200 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002201 {
2202 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002203 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002204 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00002205 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002206 }
2207
2208 while (--argcount >= 0)
2209 clear_tv(&argvars[argcount]);
2210
Bram Moolenaar4525a572022-02-13 11:57:33 +00002211 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02002212 *arg = argp;
2213 else
2214 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002215 return ret;
2216}
2217
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002218/*
2219 * Return TRUE if "p" starts with "<SID>" or "s:".
2220 * Only works if eval_fname_script() returned non-zero for "p"!
2221 */
2222 static int
2223eval_fname_sid(char_u *p)
2224{
2225 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
2226}
2227
2228/*
2229 * In a script change <SID>name() and s:name() to K_SNR 123_name().
2230 * Change <SNR>123_name() to K_SNR 123_name().
2231 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002232 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002233 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002234 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00002235fname_trans_sid(
2236 char_u *name,
2237 char_u *fname_buf,
2238 char_u **tofree,
2239 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002240{
John Marriottb32800f2025-02-01 15:25:34 +01002241 char_u *script_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002242 char_u *fname;
John Marriottb32800f2025-02-01 15:25:34 +01002243 size_t fnamelen;
2244 size_t fname_buflen;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002245
John Marriottb32800f2025-02-01 15:25:34 +01002246 script_name = name + eval_fname_script(name);
2247 if (script_name == name)
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002248 return name; // no prefix
2249
2250 fname_buf[0] = K_SPECIAL;
2251 fname_buf[1] = KS_EXTRA;
2252 fname_buf[2] = (int)KE_SNR;
John Marriottb32800f2025-02-01 15:25:34 +01002253 fname_buflen = 3;
2254 if (!eval_fname_sid(name)) // "<SID>" or "s:"
2255 fname_buf[fname_buflen] = NUL;
2256 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002257 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002258 if (current_sctx.sc_sid <= 0)
2259 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002260 else
2261 {
John Marriottb32800f2025-02-01 15:25:34 +01002262 fname_buflen += vim_snprintf((char *)fname_buf + 3,
2263 FLEN_FIXED - 3,
2264 "%ld_",
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002265 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002266 }
2267 }
John Marriottb32800f2025-02-01 15:25:34 +01002268 fnamelen = fname_buflen + STRLEN(script_name);
2269 if (fnamelen < FLEN_FIXED)
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002270 {
John Marriottb32800f2025-02-01 15:25:34 +01002271 STRCPY(fname_buf + fname_buflen, script_name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002272 fname = fname_buf;
2273 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002274 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002275 {
John Marriottb32800f2025-02-01 15:25:34 +01002276 fname = alloc(fnamelen + 1);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002277 if (fname == NULL)
2278 *error = FCERR_OTHER;
2279 else
2280 {
2281 *tofree = fname;
John Marriottb32800f2025-02-01 15:25:34 +01002282 vim_snprintf((char *)fname, fnamelen + 1, "%s%s", fname_buf, script_name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002283 }
2284 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002285 return fname;
2286}
2287
2288/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002289 * Concatenate the script ID and function name into "<SNR>99_name".
2290 * "buffer" must have size MAX_FUNC_NAME_LEN.
2291 */
2292 void
2293func_name_with_sid(char_u *name, int sid, char_u *buffer)
2294{
2295 // A script-local function is stored as "<SNR>99_name".
2296 buffer[0] = K_SPECIAL;
2297 buffer[1] = KS_EXTRA;
2298 buffer[2] = (int)KE_SNR;
2299 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2300 (long)sid, name);
2301}
2302
2303/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002304 * Find a function "name" in script "sid".
2305 */
2306 static ufunc_T *
2307find_func_with_sid(char_u *name, int sid)
2308{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002309 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002310 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311
Bram Moolenaarb8822442022-01-11 15:24:05 +00002312 if (!SCRIPT_ID_VALID(sid))
2313 return NULL; // not in a script
2314
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002315 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316 hi = hash_find(&func_hashtab, buffer);
2317 if (!HASHITEM_EMPTY(hi))
2318 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002319 return NULL;
2320}
2321
2322/*
2323 * Find a function "name" in script "sid" prefixing the autoload prefix.
2324 */
2325 static ufunc_T *
2326find_func_with_prefix(char_u *name, int sid)
2327{
2328 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002329 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002330 scriptitem_T *si;
2331
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002332 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002333 return NULL; // already has the prefix
2334 if (!SCRIPT_ID_VALID(sid))
2335 return NULL; // not in a script
2336 si = SCRIPT_ITEM(sid);
2337 if (si->sn_autoload_prefix != NULL)
2338 {
2339 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2340 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002341 char_u *namep;
2342
2343 // skip a "<SNR>99_" prefix
2344 namep = untrans_function_name(name);
2345 if (namep == NULL)
2346 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002347
2348 // An exported function in an autoload script is stored as
2349 // "dir#path#name".
2350 if (len < sizeof(buffer))
2351 auto_name = buffer;
2352 else
2353 auto_name = alloc(len);
2354 if (auto_name != NULL)
2355 {
2356 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002357 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002358 hi = hash_find(&func_hashtab, auto_name);
2359 if (auto_name != buffer)
2360 vim_free(auto_name);
2361 if (!HASHITEM_EMPTY(hi))
2362 return HI2UF(hi);
2363 }
2364 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365
2366 return NULL;
2367}
2368
2369/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002370 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002371 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2372 * functions.
2373 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002374 * Return NULL for unknown function.
2375 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002376 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002377find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002378{
2379 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002380 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002382 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002384 // Find script-local function before global one.
2385 if (in_vim9script() && eval_isnamec1(*name)
2386 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002387 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002388 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2389 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002390 if (func != NULL)
2391 return func;
2392 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002393 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2394 {
2395 char_u *p = name + 5;
2396 long sid;
2397
2398 // printable "<SNR>123_Name" form
2399 sid = getdigits(&p);
2400 if (*p == '_')
2401 {
2402 func = find_func_with_sid(p + 1, (int)sid);
2403 if (func != NULL)
2404 return func;
2405 }
2406 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002408
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002409 if ((flags & FFED_NO_GLOBAL) == 0)
2410 {
2411 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002412 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002413 if (!HASHITEM_EMPTY(hi))
2414 return HI2UF(hi);
2415 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416
Bram Moolenaarb8822442022-01-11 15:24:05 +00002417 // Find autoload function if this is an autoload script.
Yegappan Lakshmanan6289f912025-01-14 17:13:36 +01002418 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
Bram Moolenaarb8822442022-01-11 15:24:05 +00002419 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420}
2421
2422/*
2423 * Find a function by name, return pointer to it in ufuncs.
2424 * "cctx" is passed in a :def function to find imported functions.
2425 * Return NULL for unknown or dead function.
2426 */
2427 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002428find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002429{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002430 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002431
2432 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2433 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002434 return NULL;
2435}
2436
2437/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002438 * Return TRUE if "ufunc" is a global function.
2439 */
2440 int
2441func_is_global(ufunc_T *ufunc)
2442{
2443 return ufunc->uf_name[0] != K_SPECIAL;
2444}
2445
2446/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002447 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2448 */
2449 int
2450func_requires_g_prefix(ufunc_T *ufunc)
2451{
John Marriottb32800f2025-02-01 15:25:34 +01002452 return func_is_global(ufunc)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002453 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002454 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01002455 && !SAFE_isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002456}
2457
2458/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002459 * Copy the function name of "fp" to buffer "buf".
2460 * "buf" must be able to hold the function name plus three bytes.
2461 * Takes care of script-local function names.
2462 */
John Marriottb32800f2025-02-01 15:25:34 +01002463 static int
2464cat_func_name(char_u *buf, size_t bufsize, ufunc_T *fp)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002465{
John Marriottb32800f2025-02-01 15:25:34 +01002466 int len;
2467
Bram Moolenaar0f769812020-09-12 18:32:34 +02002468 if (!func_is_global(fp))
John Marriottb32800f2025-02-01 15:25:34 +01002469 len = vim_snprintf((char *)buf, bufsize, "<SNR>%s", fp->uf_name + 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002470 else
John Marriottb32800f2025-02-01 15:25:34 +01002471 len = vim_snprintf((char *)buf, bufsize, "%s", fp->uf_name);
2472
2473 return (len >= (int)bufsize) ? (int)bufsize - 1 : len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002474}
2475
2476/*
2477 * Add a number variable "name" to dict "dp" with value "nr".
2478 */
2479 static void
2480add_nr_var(
2481 dict_T *dp,
2482 dictitem_T *v,
2483 char *name,
2484 varnumber_T nr)
2485{
2486 STRCPY(v->di_key, name);
2487 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002488 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002489 v->di_tv.v_type = VAR_NUMBER;
2490 v->di_tv.v_lock = VAR_FIXED;
2491 v->di_tv.vval.v_number = nr;
2492}
2493
2494/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002495 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002496 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002497 static void
2498free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002499{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002500 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002501
Bram Moolenaarca16c602022-09-06 18:57:08 +01002502 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002503 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002504 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002505
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002506 // When garbage collecting a funccall_T may be freed before the
2507 // function that references it, clear its uf_scoped field.
2508 // The function may have been redefined and point to another
2509 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002510 if (fp != NULL && fp->uf_scoped == fc)
2511 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002512 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002513 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002514
Bram Moolenaarca16c602022-09-06 18:57:08 +01002515 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002516 vim_free(fc);
2517}
2518
2519/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002520 * Free "fc" and what it contains.
2521 * Can be called only when "fc" is kept beyond the period of it called,
2522 * i.e. after cleanup_function_call(fc).
2523 */
2524 static void
2525free_funccal_contents(funccall_T *fc)
2526{
2527 listitem_T *li;
2528
2529 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002530 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002531
2532 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002533 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002534
2535 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002536 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002537 clear_tv(&li->li_tv);
2538
2539 free_funccal(fc);
2540}
2541
2542/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002543 * Handle the last part of returning from a function: free the local hashtable.
2544 * Unless it is still in use by a closure.
2545 */
2546 static void
2547cleanup_function_call(funccall_T *fc)
2548{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002549 int may_free_fc = fc->fc_refcount <= 0;
2550 int free_fc = TRUE;
2551
Bram Moolenaarca16c602022-09-06 18:57:08 +01002552 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002553
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002554 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002555 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2556 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002557 else
2558 free_fc = FALSE;
2559
2560 // If the a:000 list and the l: and a: dicts are not referenced and
2561 // there is no closure using it, we can free the funccall_T and what's
2562 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002563 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2564 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002565 else
2566 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002567 int todo;
2568 hashitem_T *hi;
2569 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002570
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002571 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002572
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002573 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002574 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002575 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002576 {
2577 if (!HASHITEM_EMPTY(hi))
2578 {
2579 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002580 di = HI2DI(hi);
2581 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002582 }
2583 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002584 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002585
Bram Moolenaarca16c602022-09-06 18:57:08 +01002586 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2587 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002588 else
2589 {
2590 listitem_T *li;
2591
2592 free_fc = FALSE;
2593
2594 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002595 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002596 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002597 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002598
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002599 if (free_fc)
2600 free_funccal(fc);
2601 else
2602 {
2603 static int made_copy = 0;
2604
2605 // "fc" is still in use. This can happen when returning "a:000",
2606 // assigning "l:" to a global variable or defining a closure.
2607 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002608 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002609 previous_funccal = fc;
2610
2611 if (want_garbage_collect)
2612 // If garbage collector is ready, clear count.
2613 made_copy = 0;
2614 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002615 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002616 // We have made a lot of copies, worth 4 Mbyte. This can happen
2617 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002618 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002619 // too much memory.
2620 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002621 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002622 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002623 }
2624}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002625
2626/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002627 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2628 */
2629 static int
2630numbered_function(char_u *name)
2631{
Keith Thompson184f71c2024-01-04 21:19:04 +01002632 return SAFE_isdigit(*name)
2633 || (name[0] == 'g' && name[1] == ':' && SAFE_isdigit(name[2]));
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002634}
2635
2636/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002637 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002638 * 1. ordinary names, function defined with :function or :def;
2639 * can start with "<SNR>123_" literally or with K_SPECIAL.
2640 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002641 * For the first we only count the name stored in func_hashtab as a reference,
2642 * using function() does not count as a reference, because the function is
2643 * looked up by name.
2644 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002645 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002646func_name_refcount(char_u *name)
2647{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002648 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002649}
2650
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651/*
2652 * Unreference "fc": decrement the reference count and free it when it
2653 * becomes zero. "fp" is detached from "fc".
2654 * When "force" is TRUE we are exiting.
2655 */
2656 static void
2657funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2658{
2659 funccall_T **pfc;
2660 int i;
2661
2662 if (fc == NULL)
2663 return;
2664
2665 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002666 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2667 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2668 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2669 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670 {
2671 if (fc == *pfc)
2672 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002673 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002674 free_funccal_contents(fc);
2675 return;
2676 }
2677 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002678 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2679 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2680 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681}
2682
2683/*
2684 * Remove the function from the function hashtable. If the function was
2685 * deleted while it still has references this was already done.
2686 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2687 */
2688 static int
2689func_remove(ufunc_T *fp)
2690{
2691 hashitem_T *hi;
2692
2693 // Return if it was already virtually deleted.
2694 if (fp->uf_flags & FC_DEAD)
2695 return FALSE;
2696
2697 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002698 if (HASHITEM_EMPTY(hi))
2699 return FALSE;
2700
2701 // When there is a def-function index do not actually remove the
2702 // function, so we can find the index when defining the function again.
2703 // Do remove it when it's a copy.
2704 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002706 fp->uf_flags |= FC_DEAD;
2707 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002709 hash_remove(&func_hashtab, hi, "remove function");
2710 fp->uf_flags |= FC_DELETED;
2711 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002712}
2713
2714 static void
2715func_clear_items(ufunc_T *fp)
2716{
2717 ga_clear_strings(&(fp->uf_args));
2718 ga_clear_strings(&(fp->uf_def_args));
2719 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002721 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002722 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01002723 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002724
2725 // Increment the refcount of this function to avoid it being freed
2726 // recursively when the partial is freed.
2727 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002728 partial_unref(fp->uf_partial);
2729 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002730 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002731
2732#ifdef FEAT_LUA
2733 if (fp->uf_cb_free != NULL)
2734 {
2735 fp->uf_cb_free(fp->uf_cb_state);
2736 fp->uf_cb_free = NULL;
2737 }
2738
2739 fp->uf_cb_state = NULL;
2740 fp->uf_cb = NULL;
2741#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742#ifdef FEAT_PROFILE
2743 VIM_CLEAR(fp->uf_tml_count);
2744 VIM_CLEAR(fp->uf_tml_total);
2745 VIM_CLEAR(fp->uf_tml_self);
2746#endif
2747}
2748
2749/*
2750 * Free all things that a function contains. Does not free the function
2751 * itself, use func_free() for that.
2752 * When "force" is TRUE we are exiting.
2753 */
2754 static void
2755func_clear(ufunc_T *fp, int force)
2756{
2757 if (fp->uf_cleared)
2758 return;
2759 fp->uf_cleared = TRUE;
2760
2761 // clear this function
2762 func_clear_items(fp);
2763 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002764 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765}
2766
2767/*
2768 * Free a function and remove it from the list of functions. Does not free
2769 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002770 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002771 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002772 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002773 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002774func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775{
2776 // Only remove it when not done already, otherwise we would remove a newer
2777 // version of the function with the same name.
2778 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2779 func_remove(fp);
2780
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002781 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002782 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002783 if (fp->uf_dfunc_idx > 0)
2784 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002785 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002786 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002787 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002788 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002789 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790}
2791
2792/*
2793 * Free all things that a function contains and free the function itself.
2794 * When "force" is TRUE we are exiting.
2795 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002796 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797func_clear_free(ufunc_T *fp, int force)
2798{
2799 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002800 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2801 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002802 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002803 else
2804 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805}
2806
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002807/*
2808 * Copy already defined function "lambda" to a new function with name "global".
2809 * This is for when a compiled function defines a global function.
2810 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002811 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002812copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002813 char_u *lambda,
2814 char_u *global,
2815 loopvarinfo_T *loopvarinfo,
2816 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002817{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002818 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002819 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002820
2821 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002822 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002823 semsg(_(e_lambda_function_not_found_str), lambda);
2824 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002825 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002826
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002827 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002828 if (fp != NULL)
2829 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002830 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002831 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002832 return FAIL;
2833 }
2834
John Marriottb32800f2025-02-01 15:25:34 +01002835 fp = alloc_ufunc(global, STRLEN(global));
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002836 if (fp == NULL)
2837 return FAIL;
2838
2839 fp->uf_varargs = ufunc->uf_varargs;
2840 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2841 fp->uf_def_status = ufunc->uf_def_status;
2842 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2843 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2844 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2845 == FAIL
2846 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2847 goto failed;
2848
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002849 if (ufunc->uf_arg_types != NULL)
2850 {
2851 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2852 if (fp->uf_arg_types == NULL)
2853 goto failed;
2854 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2855 sizeof(type_T *) * fp->uf_args.ga_len);
2856 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002857 if (ufunc->uf_va_name != NULL)
2858 {
2859 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2860 if (fp->uf_va_name == NULL)
2861 goto failed;
2862 }
2863 fp->uf_ret_type = ufunc->uf_ret_type;
2864
2865 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002866
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002867 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002868
2869 // the referenced dfunc_T is now used one more time
2870 link_def_function(fp);
2871
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002872 // Create a partial to store the context of the function where it was
2873 // instantiated. Only needs to be done once. Do this on the original
2874 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002875 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2876 {
2877 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2878
2879 if (pt == NULL)
2880 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002881 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002882 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002883 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002884 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002885 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002886 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002887 }
2888
2889 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002890
2891failed:
2892 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002893 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002894}
2895
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002896static int funcdepth = 0;
2897
2898/*
2899 * Increment the function call depth count.
2900 * Return FAIL when going over 'maxfuncdepth'.
2901 * Otherwise return OK, must call funcdepth_decrement() later!
2902 */
2903 int
2904funcdepth_increment(void)
2905{
2906 if (funcdepth >= p_mfd)
2907 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002908 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002909 return FAIL;
2910 }
2911 ++funcdepth;
2912 return OK;
2913}
2914
2915 void
2916funcdepth_decrement(void)
2917{
2918 --funcdepth;
2919}
2920
2921/*
2922 * Get the current function call depth.
2923 */
2924 int
2925funcdepth_get(void)
2926{
2927 return funcdepth;
2928}
2929
2930/*
2931 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002932 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002933 * times as funcdepth_increment().
2934 */
2935 void
2936funcdepth_restore(int depth)
2937{
2938 funcdepth = depth;
2939}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002940
2941/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002942 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2943 * "rettv".
2944 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2945 * Returns NULL when allocation fails.
2946 */
2947 funccall_T *
2948create_funccal(ufunc_T *fp, typval_T *rettv)
2949{
2950 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2951
2952 if (fc == NULL)
2953 return NULL;
2954 fc->fc_caller = current_funccal;
2955 current_funccal = fc;
2956 fc->fc_func = fp;
2957 func_ptr_ref(fp);
2958 fc->fc_rettv = rettv;
2959 return fc;
2960}
2961
2962/*
2963 * To be called when returning from a compiled function; restores
2964 * current_funccal.
2965 */
2966 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002967remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002968{
2969 funccall_T *fc = current_funccal;
2970
2971 current_funccal = fc->fc_caller;
2972 free_funccal(fc);
2973}
2974
2975/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002976 * Call a user function.
2977 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002978 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002979call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002980 ufunc_T *fp, // pointer to function
2981 int argcount, // nr of args
2982 typval_T *argvars, // arguments
2983 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002984 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002985 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002986{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002987 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002988 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002989 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002990 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002991 funccall_T *fc;
2992 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002993 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002994 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002995 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002996 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002997 int i;
2998 int ai;
2999 int islambda = FALSE;
3000 char_u numbuf[NUMBUFLEN];
3001 char_u *name;
John Marriottb32800f2025-02-01 15:25:34 +01003002 size_t namelen;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003003 typval_T *tv_to_free[MAX_FUNC_ARGS];
3004 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003005#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003006 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003007#endif
ichizok7e5fe382023-04-15 13:17:50 +01003008 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003009
Bram Moolenaarb2049902021-01-24 12:53:53 +01003010#ifdef FEAT_PROFILE
3011 CLEAR_FIELD(profile_info);
3012#endif
3013
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003014 // If depth of calling is getting too high, don't execute the function.
3015 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003016 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003017 rettv->v_type = VAR_NUMBER;
3018 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003019 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003020 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003021
Bram Moolenaare38eab22019-12-05 21:50:01 +01003022 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003023
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01003024 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01003025 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003026 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003027 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003028 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01003029 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
3030 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003031 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01003032 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003033
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003034 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01003036#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01003037 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003038#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003039 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01003040#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003041 if (do_profiling == PROF_YES)
3042 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01003043#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003044 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003045 if (call_def_function(fp, argcount, argvars, 0,
3046 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
3047 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003048 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01003049#ifdef FEAT_PROFILE
3050 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01003051 || (caller != NULL && caller->uf_profiling)))
3052 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01003053#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01003054 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003055 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003056 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057 }
3058
Bram Moolenaar38453522021-11-28 22:00:12 +00003059 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003060
3061 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01003062 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
3063 * variables with names up to VAR_SHORT_LEN long. This avoids having to
3064 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003065 */
3066 /*
3067 * Init l: variables.
3068 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01003069 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003070 if (selfdict != NULL)
3071 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003072 // Set l:self to "selfdict". Use "name" to avoid a warning from
3073 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01003074 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003075 name = v->di_key;
3076 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01003077 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003078 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003079 v->di_tv.v_type = VAR_DICT;
3080 v->di_tv.v_lock = 0;
3081 v->di_tv.vval.v_dict = selfdict;
3082 ++selfdict->dv_refcount;
3083 }
3084
3085 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003086 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003087 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003088 * Set a:000 to a list with room for the "..." arguments.
3089 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01003090 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003091 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01003092 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003093 (varnumber_T)(argcount >= fp->uf_args.ga_len
3094 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01003095 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003096 if ((fp->uf_flags & FC_NOARGS) == 0)
3097 {
3098 // Use "name" to avoid a warning from some compiler that checks the
3099 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01003100 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003101 name = v->di_key;
3102 STRCPY(name, "000");
3103 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003104 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003105 v->di_tv.v_type = VAR_LIST;
3106 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003107 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003108 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01003109 CLEAR_FIELD(fc->fc_l_varlist);
3110 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
3111 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003112
3113 /*
3114 * Set a:firstline to "firstline" and a:lastline to "lastline".
3115 * Set a:name to named arguments.
3116 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003117 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003118 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003119 if ((fp->uf_flags & FC_NOARGS) == 0)
3120 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003121 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
3122 "firstline", (varnumber_T)funcexe->fe_firstline);
3123 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
3124 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003125 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003126 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003127 {
3128 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003129 typval_T def_rettv;
3130 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003131
3132 ai = i - fp->uf_args.ga_len;
3133 if (ai < 0)
3134 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003135 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003136 name = FUNCARG(fp, i);
3137 if (islambda)
3138 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003139
3140 // evaluate named argument default expression
3141 isdefault = ai + fp->uf_def_args.ga_len >= 0
3142 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
3143 && argvars[i].vval.v_number == VVAL_NONE));
3144 if (isdefault)
3145 {
3146 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003147
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003148 def_rettv.v_type = VAR_NUMBER;
3149 def_rettv.vval.v_number = -1;
3150
3151 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
3152 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003153 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003154 {
3155 default_arg_err = 1;
3156 break;
3157 }
3158 }
John Marriottb32800f2025-02-01 15:25:34 +01003159
3160 namelen = STRLEN(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003161 }
3162 else
3163 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003164 if ((fp->uf_flags & FC_NOARGS) != 0)
3165 // Bail out if no a: arguments used (in lambda).
3166 break;
3167
Bram Moolenaare38eab22019-12-05 21:50:01 +01003168 // "..." argument a:1, a:2, etc.
John Marriottb32800f2025-02-01 15:25:34 +01003169 namelen = vim_snprintf((char *)numbuf, sizeof(numbuf), "%d", ai + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003170 name = numbuf;
3171 }
John Marriottb32800f2025-02-01 15:25:34 +01003172 if (fixvar_idx < FIXVAR_CNT && namelen <= VAR_SHORT_LEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003173 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003174 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003175 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003176 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003177 }
3178 else
3179 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003180 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003181 if (v == NULL)
3182 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003183 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003184 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003185
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003186 // Note: the values are copied directly to avoid alloc/free.
3187 // "argvars" must have VAR_FIXED for v_lock.
3188 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003189 v->di_tv.v_lock = VAR_FIXED;
3190
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003191 if (isdefault)
3192 // Need to free this later, no matter where it's stored.
3193 tv_to_free[tv_to_free_len++] = &v->di_tv;
3194
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003195 if (addlocal)
3196 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003197 // Named arguments should be accessed without the "a:" prefix in
3198 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003199 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003200 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003201 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003202 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003203 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003204
3205 if (ai >= 0 && ai < MAX_FUNC_ARGS)
3206 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003207 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003208
3209 li->li_tv = argvars[i];
3210 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003211 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003212 }
3213 }
3214
Bram Moolenaare38eab22019-12-05 21:50:01 +01003215 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003216 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003217
3218 if (fp->uf_flags & FC_SANDBOX)
3219 {
3220 using_sandbox = TRUE;
3221 ++sandbox;
3222 }
3223
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003224 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003225 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003226 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003227 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003228 ++no_wait_return;
3229 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003230
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003231 smsg(_("calling %s"), SOURCING_NAME);
3232 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003233 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003234 char_u buf[MSG_BUF_LEN];
3235 char_u numbuf2[NUMBUFLEN];
3236 char_u *tofree;
3237 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003238
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003239 msg_puts("(");
3240 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003241 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003242 if (i > 0)
3243 msg_puts(", ");
3244 if (argvars[i].v_type == VAR_NUMBER)
3245 msg_outnum((long)argvars[i].vval.v_number);
3246 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003247 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003248 // Do not want errors such as E724 here.
3249 ++emsg_off;
3250 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3251 --emsg_off;
3252 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003253 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003254 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003255 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003256 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3257 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003258 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003259 msg_puts((char *)s);
3260 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003261 }
3262 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003263 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003264 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003265 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003266 msg_puts("\n"); // don't overwrite this either
3267
3268 verbose_leave_scroll();
3269 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003270 }
3271#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003272 if (do_profiling == PROF_YES)
3273 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003274 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003275#endif
3276
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003277 // "legacy" does not apply to commands in the function
3278 sticky_cmdmod_flags = 0;
3279
Bram Moolenaar98aff652022-09-06 21:02:35 +01003280 // If called from a compiled :def function the execution context must be
3281 // hidden, any deferred functions need to be added to the function being
3282 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003283 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003284
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003285 save_current_sctx = current_sctx;
3286 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003287 save_did_emsg = did_emsg;
3288 did_emsg = FALSE;
3289
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003290 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003291 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003292 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003293 retval = FCERR_FAILED;
3294 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003295 else if (islambda)
3296 {
3297 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3298
3299 // A Lambda always has the command "return {expr}". It is much faster
3300 // to evaluate {expr} directly.
3301 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003302 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003303 --ex_nesting_level;
3304 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003305 else
3306 // call do_cmdline() to execute the lines
3307 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003308 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3309
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003310 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003311 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003312
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003313 if (RedrawingDisabled > 0)
3314 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003315
Bram Moolenaare38eab22019-12-05 21:50:01 +01003316 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003317 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3318 {
3319 clear_tv(rettv);
3320 rettv->v_type = VAR_NUMBER;
3321 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003322
3323 // In corner cases returning a "failed" value is not backwards
3324 // compatible. Only do this for Vim9 script.
3325 if (in_vim9script())
3326 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003327 }
3328
3329#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003330 if (do_profiling == PROF_YES)
3331 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003332 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003333
3334 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3335 profile_may_end_func(&profile_info, fp, caller);
3336 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003337#endif
3338
Bram Moolenaare38eab22019-12-05 21:50:01 +01003339 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003340 if (p_verbose >= 12)
3341 {
3342 ++no_wait_return;
3343 verbose_enter_scroll();
3344
3345 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003346 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003347 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003348 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003349 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003350 else
3351 {
3352 char_u buf[MSG_BUF_LEN];
3353 char_u numbuf2[NUMBUFLEN];
3354 char_u *tofree;
3355 char_u *s;
3356
Bram Moolenaare38eab22019-12-05 21:50:01 +01003357 // The value may be very long. Skip the middle part, so that we
3358 // have some idea how it starts and ends. smsg() would always
3359 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003360 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003361 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003362 --emsg_off;
3363 if (s != NULL)
3364 {
3365 if (vim_strsize(s) > MSG_BUF_CLEN)
3366 {
3367 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3368 s = buf;
3369 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003370 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003371 vim_free(tofree);
3372 }
3373 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003374 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003375
3376 verbose_leave_scroll();
3377 --no_wait_return;
3378 }
3379
ichizok7e5fe382023-04-15 13:17:50 +01003380 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003381 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003382 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003383 restore_current_ectx(save_current_ectx);
3384
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003385#ifdef FEAT_PROFILE
3386 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003387 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003388#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003389 if (using_sandbox)
3390 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003391 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003392
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003393 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003394 {
3395 ++no_wait_return;
3396 verbose_enter_scroll();
3397
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003398 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003399 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003400
3401 verbose_leave_scroll();
3402 --no_wait_return;
3403 }
3404
3405 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003406 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003407 for (i = 0; i < tv_to_free_len; ++i)
3408 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003409 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003410
3411 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003412}
3413
3414/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003415 * Check the argument count for user function "fp".
3416 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3417 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003418 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003419check_user_func_argcount(ufunc_T *fp, int argcount)
3420{
3421 int regular_args = fp->uf_args.ga_len;
3422
3423 if (argcount < regular_args - fp->uf_def_args.ga_len)
3424 return FCERR_TOOFEW;
3425 else if (!has_varargs(fp) && argcount > regular_args)
3426 return FCERR_TOOMANY;
3427 return FCERR_UNKNOWN;
3428}
3429
3430/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003432 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003433 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434call_user_func_check(
3435 ufunc_T *fp,
3436 int argcount,
3437 typval_T *argvars,
3438 typval_T *rettv,
3439 funcexe_T *funcexe,
3440 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003441{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003442 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003443
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003444#ifdef FEAT_LUA
3445 if (fp->uf_flags & FC_CFUNC)
3446 {
3447 cfunc_T cb = fp->uf_cb;
3448
3449 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3450 }
3451#endif
3452
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003453 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3454 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003455 error = check_user_func_argcount(fp, argcount);
3456 if (error != FCERR_UNKNOWN)
3457 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003458
Bram Moolenaar50824712020-12-20 21:10:17 +01003459 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003460 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003462 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003464 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465 int did_save_redo = FALSE;
3466 save_redo_T save_redo;
3467
3468 /*
3469 * Call the user function.
3470 * Save and restore search patterns, script variables and
3471 * redo buffer.
3472 */
3473 save_search_patterns();
3474 if (!ins_compl_active())
3475 {
3476 saveRedobuff(&save_redo);
3477 did_save_redo = TRUE;
3478 }
3479 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003480 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003481 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3483 // Function was unreferenced while being used, free it now.
3484 func_clear_free(fp, FALSE);
3485 if (did_save_redo)
3486 restoreRedobuff(&save_redo);
3487 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003488 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003489
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003491}
3492
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003493static funccal_entry_T *funccal_stack = NULL;
3494
3495/*
3496 * Save the current function call pointer, and set it to NULL.
3497 * Used when executing autocommands and for ":source".
3498 */
3499 void
3500save_funccal(funccal_entry_T *entry)
3501{
3502 entry->top_funccal = current_funccal;
3503 entry->next = funccal_stack;
3504 funccal_stack = entry;
3505 current_funccal = NULL;
3506}
3507
3508 void
3509restore_funccal(void)
3510{
3511 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003512 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003513 else
3514 {
3515 current_funccal = funccal_stack->top_funccal;
3516 funccal_stack = funccal_stack->next;
3517 }
3518}
3519
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003520 funccall_T *
3521get_current_funccal(void)
3522{
3523 return current_funccal;
3524}
3525
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003526/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003527 * Return TRUE when currently at the script level:
3528 * - not in a function
3529 * - not executing an autocommand
3530 * Note that when an autocommand sources a script the result is FALSE;
3531 */
3532 int
3533at_script_level(void)
3534{
3535 return current_funccal == NULL && autocmd_match == NULL;
3536}
3537
3538/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003539 * Mark all functions of script "sid" as deleted.
3540 */
3541 void
3542delete_script_functions(int sid)
3543{
3544 hashitem_T *hi;
3545 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003546 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003547 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003548 size_t len;
3549
3550 buf[0] = K_SPECIAL;
3551 buf[1] = KS_EXTRA;
3552 buf[2] = (int)KE_SNR;
John Marriottb32800f2025-02-01 15:25:34 +01003553 len = 3 + vim_snprintf((char *)buf + 3, sizeof(buf) - 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003554
Bram Moolenaarce658352020-07-31 23:47:12 +02003555 while (todo > 0)
3556 {
3557 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003558 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003559 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003560 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003561 fp = HI2UF(hi);
3562 if (STRNCMP(fp->uf_name, buf, len) == 0)
3563 {
3564 int changed = func_hashtab.ht_changed;
3565
3566 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003567
3568 if (fp->uf_calls > 0)
3569 {
3570 // Function is executing, don't free it but do remove
3571 // it from the hashtable.
3572 if (func_remove(fp))
3573 fp->uf_refcount--;
3574 }
3575 else
3576 {
3577 func_clear(fp, TRUE);
3578 // When clearing a function another function can be
3579 // cleared as a side effect. When that happens start
3580 // over.
3581 if (changed != func_hashtab.ht_changed)
3582 break;
3583 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003584 }
3585 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003586 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003587 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003588}
3589
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003590#if defined(EXITFREE) || defined(PROTO)
3591 void
3592free_all_functions(void)
3593{
3594 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003595 ufunc_T *fp;
3596 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003597 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003598 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003599
Bram Moolenaare38eab22019-12-05 21:50:01 +01003600 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003601 while (current_funccal != NULL)
3602 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003603 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003604 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003605 if (current_funccal == NULL && funccal_stack != NULL)
3606 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003607 }
3608
Bram Moolenaare38eab22019-12-05 21:50:01 +01003609 // First clear what the functions contain. Since this may lower the
3610 // reference count of a function, it may also free a function and change
3611 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003612 while (todo > 0)
3613 {
3614 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003615 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003616 if (!HASHITEM_EMPTY(hi))
3617 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618 // clear the def function index now
3619 fp = HI2UF(hi);
3620 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003621 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003622
Bram Moolenaare38eab22019-12-05 21:50:01 +01003623 // Only free functions that are not refcounted, those are
3624 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003625 if (func_name_refcount(fp->uf_name))
3626 ++skipped;
3627 else
3628 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003629 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003630 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003631 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003632 {
3633 skipped = 0;
3634 break;
3635 }
3636 }
3637 --todo;
3638 }
3639 }
3640
Bram Moolenaare38eab22019-12-05 21:50:01 +01003641 // Now actually free the functions. Need to start all over every time,
3642 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003643 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003644 while (func_hashtab.ht_used > skipped)
3645 {
3646 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003647 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003648 if (!HASHITEM_EMPTY(hi))
3649 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003650 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003651 // Only free functions that are not refcounted, those are
3652 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003653 fp = HI2UF(hi);
3654 if (func_name_refcount(fp->uf_name))
3655 ++skipped;
3656 else
3657 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003658 if (func_free(fp, FALSE) == OK)
3659 {
3660 skipped = 0;
3661 break;
3662 }
3663 // did not actually free it
3664 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003665 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003666 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003667 }
3668 if (skipped == 0)
3669 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670
3671 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003672}
3673#endif
3674
3675/*
3676 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003677 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3678 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003679 * "len" is the length of "name", or -1 for NUL terminated.
3680 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003682builtin_function(char_u *name, int len)
3683{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003684 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003685
Bram Moolenaara26b9702020-04-18 19:53:28 +02003686 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003687 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003688 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3689 {
3690 if (name[i] == AUTOLOAD_CHAR)
3691 return FALSE;
3692 if (!eval_isnamec(name[i]))
3693 {
3694 // "name.something" is not a builtin function
3695 if (name[i] == '.')
3696 return FALSE;
3697 break;
3698 }
3699 }
3700 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003701}
3702
3703 int
3704func_call(
3705 char_u *name,
3706 typval_T *args,
3707 partial_T *partial,
3708 dict_T *selfdict,
3709 typval_T *rettv)
3710{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003711 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003712 listitem_T *item;
3713 typval_T argv[MAX_FUNC_ARGS + 1];
3714 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003715 int r = 0;
3716
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003717 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003718 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003719 {
3720 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3721 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003722 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003723 break;
3724 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003725 // Make a copy of each argument. This is needed to be able to set
3726 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003727 copy_tv(&item->li_tv, &argv[argc++]);
3728 }
3729
3730 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003731 {
3732 funcexe_T funcexe;
3733
Bram Moolenaara80faa82020-04-12 19:37:17 +02003734 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003735 funcexe.fe_firstline = curwin->w_cursor.lnum;
3736 funcexe.fe_lastline = curwin->w_cursor.lnum;
3737 funcexe.fe_evaluate = TRUE;
3738 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003739 if (partial != NULL)
3740 {
3741 funcexe.fe_object = partial->pt_obj;
3742 if (funcexe.fe_object != NULL)
3743 ++funcexe.fe_object->obj_refcount;
3744 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003745 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003746 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3747 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003748
Bram Moolenaare38eab22019-12-05 21:50:01 +01003749 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003750 while (argc > 0)
3751 clear_tv(&argv[--argc]);
3752
3753 return r;
3754}
3755
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003756static int callback_depth = 0;
3757
3758 int
3759get_callback_depth(void)
3760{
3761 return callback_depth;
3762}
3763
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003764/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003765 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003766 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003767 */
3768 int
3769call_callback(
3770 callback_T *callback,
3771 int len, // length of "name" or -1 to use strlen()
3772 typval_T *rettv, // return value goes here
3773 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003774 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003775 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003776{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003777 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003778 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003779
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003780 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3781 return FAIL;
Christian Brabandt47510f32023-10-15 09:56:16 +02003782
zeertzjqfe583b12023-12-21 16:59:26 +01003783 if (callback_depth > p_mfd)
Christian Brabandt47510f32023-10-15 09:56:16 +02003784 {
3785 emsg(_(e_command_too_recursive));
3786 return FAIL;
3787 }
3788
Bram Moolenaara80faa82020-04-12 19:37:17 +02003789 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003790 funcexe.fe_evaluate = TRUE;
3791 funcexe.fe_partial = callback->cb_partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003792 if (callback->cb_partial != NULL)
3793 {
3794 funcexe.fe_object = callback->cb_partial->pt_obj;
3795 if (funcexe.fe_object != NULL)
3796 ++funcexe.fe_object->obj_refcount;
3797 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003798 ++callback_depth;
3799 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3800 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003801
3802 // When a :def function was called that uses :try an error would be turned
3803 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003804 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003805 {
3806 need_rethrow = FALSE;
3807 handle_did_throw();
3808 }
3809
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003810 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003811}
3812
3813/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003814 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003815 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003816 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3817 */
3818 varnumber_T
3819call_callback_retnr(
3820 callback_T *callback,
3821 int argcount, // number of "argvars"
3822 typval_T *argvars) // vars for arguments, must have "argcount"
3823 // PLUS ONE elements!
3824{
3825 typval_T rettv;
3826 varnumber_T retval;
3827
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003828 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003829 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003830
3831 retval = tv_get_number_chk(&rettv, NULL);
3832 clear_tv(&rettv);
3833 return retval;
3834}
3835
3836/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 * Give an error message for the result of a function.
3838 * Nothing if "error" is FCERR_NONE.
3839 */
3840 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003841user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842{
3843 switch (error)
3844 {
3845 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003846 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003847 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003848 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003849 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 break;
3851 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003852 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 break;
3854 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003855 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 break;
3857 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003858 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 break;
3860 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003861 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 break;
3863 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003864 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 break;
3866 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003867 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3868 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003870 case FCERR_OTHER:
3871 case FCERR_FAILED:
3872 // assume the error message was already given
3873 break;
3874 case FCERR_NONE:
3875 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876 }
3877}
3878
3879/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003880 * Check the argument types "argvars[argcount]" for "name" using the
3881 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3882 * is already included in "argvars[]".
3883 * Will do nothing if "funcexe->fe_check_type" is NULL or
3884 * "funcexe->fe_evaluate" is FALSE;
3885 * Returns an FCERR_ value.
3886 */
3887 static funcerror_T
3888may_check_argument_types(
3889 funcexe_T *funcexe,
3890 typval_T *argvars,
3891 int argcount,
3892 int base_included,
3893 char_u *name)
3894{
3895 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3896 {
3897 // Check that the argument types are OK for the types of the funcref.
3898 if (check_argument_types(funcexe->fe_check_type,
3899 argvars, argcount,
3900 base_included ? NULL : funcexe->fe_basetv,
3901 name) == FAIL)
3902 return FCERR_OTHER;
3903 }
3904 return FCERR_NONE;
3905}
3906
3907/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003908 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003909 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003910 * Return FAIL when the function can't be called, OK otherwise.
3911 * Also returns OK when an error was encountered while executing the function.
3912 */
3913 int
3914call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003915 char_u *funcname, // name of the function
3916 int len, // length of "name" or -1 to use strlen()
3917 typval_T *rettv, // return value goes here
3918 int argcount_in, // number of "argvars"
3919 typval_T *argvars_in, // vars for arguments, must have "argcount"
3920 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003921 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003922{
3923 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003924 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003925 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003926 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003927 char_u fname_buf[FLEN_FIXED + 1];
3928 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003929 char_u *fname = NULL;
3930 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003931 int argcount = argcount_in;
3932 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003933 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003934 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003935 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003936 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003937 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003938 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003939 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003940 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003941
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003942 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3943 // even when call_func() returns FAIL.
3944 rettv->v_type = VAR_UNKNOWN;
3945
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003946 if (partial != NULL)
3947 fp = partial->pt_func;
3948 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003949 fp = funcexe->fe_ufunc;
3950
3951 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003952 {
3953 // Make a copy of the name, if it comes from a funcref variable it
3954 // could be changed or deleted in the called function.
3955 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3956 if (name == NULL)
3957 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003958
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003959 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3960 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003961
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003962 if (funcexe->fe_doesrange != NULL)
3963 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003964
3965 if (partial != NULL)
3966 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003967 // When the function has a partial with a dict and there is a dict
3968 // argument, use the dict argument. That is backwards compatible.
3969 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003970 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003971 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003972 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003973 {
3974 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003975 {
3976 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3977 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003978 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003979 goto theend;
3980 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003981 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003982 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003983 for (i = 0; i < argcount_in; ++i)
3984 argv[i + argv_clear] = argvars_in[i];
3985 argvars = argv;
3986 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003987
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003988 if (funcexe->fe_check_type != NULL
3989 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003990 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003991 // Now funcexe->fe_check_type is missing the added arguments,
3992 // make a copy of the type with the correction.
3993 check_type = *funcexe->fe_check_type;
3994 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003995 check_type.tt_args = check_type_args;
3996 CLEAR_FIELD(check_type_args);
3997 for (i = 0; i < check_type.tt_argcount; ++i)
3998 check_type_args[i + partial->pt_argc] =
3999 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02004000 check_type.tt_argcount += partial->pt_argc;
4001 check_type.tt_min_argcount += partial->pt_argc;
4002 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004003 }
4004 }
4005
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004006 if (error == FCERR_NONE)
4007 // check the argument types if possible
4008 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
4009 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004010
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004011 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004012 {
4013 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02004014 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004015
Bram Moolenaar333894b2020-08-01 18:53:07 +02004016 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02004017 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02004018 {
4019 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004020 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02004021 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004022
Bram Moolenaare38eab22019-12-05 21:50:01 +01004023 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004024 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01004025 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004026
Bram Moolenaarf10806b2020-04-02 18:34:35 +02004027 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004028 {
4029 /*
4030 * User defined function.
4031 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02004032 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004033 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004034 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004035 if (fp != NULL && !is_global && in_vim9script()
4036 && func_requires_g_prefix(fp))
4037 // In Vim9 script g: is required to find a global
4038 // non-autoload function.
4039 fp = NULL;
4040 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004041
Bram Moolenaare38eab22019-12-05 21:50:01 +01004042 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004043 if (fp == NULL
4044 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004045 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004046 && !aborting())
4047 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004048 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004049 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004050 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004051 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004052 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
4053 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004054 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004055 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004056 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02004057 if (fp == NULL)
4058 {
4059 char_u *p = untrans_function_name(rfname);
4060
4061 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02004062 // Don't do this if the name starts with "s:".
4063 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004064 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004065 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004066
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004067 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01004068 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004069 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004070 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004071 int need_arg_check = FALSE;
4072 if (funcexe->fe_check_type == NULL)
4073 {
4074 funcexe->fe_check_type = fp->uf_func_type;
4075 need_arg_check = TRUE;
4076 }
4077
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004078 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004079 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01004080 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004081 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004082 argv_clear, fp);
4083 need_arg_check = TRUE;
4084 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02004085
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004086 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004087 {
4088 // Method call: base->Method()
4089 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004090 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004091 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004092 argvars = argv;
4093 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004094 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004095 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004096
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004097 // Check the argument types now that the function type and all
4098 // argument values are known, if not done above.
4099 if (need_arg_check)
4100 error = may_check_argument_types(funcexe, argvars, argcount,
4101 TRUE, (name != NULL) ? name : funcname);
4102 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
4103 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004105 }
4106 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004107 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004108 {
4109 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004110 * expr->method(): Find the method name in the table, call its
4111 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02004112 */
4113 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004114 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004115 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004116 else
4117 {
4118 /*
4119 * Find the function name in the table, call its implementation.
4120 */
4121 error = call_internal_func(fname, argcount, argvars, rettv);
4122 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02004123
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004124 /*
4125 * The function call (or "FuncUndefined" autocommand sequence) might
4126 * have been aborted by an error, an interrupt, or an explicitly thrown
4127 * exception that has not been caught so far. This situation can be
4128 * tested for by calling aborting(). For an error in an internal
4129 * function or for the "E132" error in call_user_func(), however, the
4130 * throw point at which the "force_abort" flag (temporarily reset by
4131 * emsg()) is normally updated has not been reached yet. We need to
4132 * update that flag first to make aborting() reliable.
4133 */
4134 update_force_abort();
4135 }
Bram Moolenaaref140542019-12-31 21:27:13 +01004136 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004137 ret = OK;
4138
Bram Moolenaar4c054e92019-11-10 00:13:50 +01004139theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004140 /*
4141 * Report an error unless the argument evaluation or function call has been
4142 * cancelled due to an aborting error, an interrupt, or an exception.
4143 */
4144 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004145 user_func_error(error, (name != NULL) ? name : funcname,
4146 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004147
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004148 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004149 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004150 clear_tv(&argv[--argv_clear + argv_base]);
4151
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004152 vim_free(tofree);
4153 vim_free(name);
4154
4155 return ret;
4156}
4157
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004158/*
4159 * Call a function without arguments, partial or dict.
4160 * This is like call_func() when the call is only "FuncName()".
4161 * To be used by "expr" options.
4162 * Returns NOTDONE when the function could not be found.
4163 */
4164 int
4165call_simple_func(
4166 char_u *funcname, // name of the function
zeertzjqc1ed7882024-08-01 22:46:54 +02004167 size_t len, // length of "name"
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004168 typval_T *rettv) // return value goes here
4169{
4170 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00004171 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004172 char_u fname_buf[FLEN_FIXED + 1];
4173 char_u *tofree = NULL;
4174 char_u *name;
4175 char_u *fname;
4176 char_u *rfname;
4177 int is_global = FALSE;
4178 ufunc_T *fp;
4179
4180 rettv->v_type = VAR_NUMBER; // default rettv is number zero
4181 rettv->vval.v_number = 0;
4182
4183 // Make a copy of the name, an option can be changed in the function.
4184 name = vim_strnsave(funcname, len);
4185 if (name == NULL)
4186 return ret;
4187
4188 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
4189
4190 // Skip "g:" before a function name.
4191 if (fname[0] == 'g' && fname[1] == ':')
4192 {
4193 is_global = TRUE;
4194 rfname = fname + 2;
4195 }
4196 else
4197 rfname = fname;
4198 fp = find_func(rfname, is_global);
4199 if (fp != NULL && !is_global && in_vim9script()
4200 && func_requires_g_prefix(fp))
4201 // In Vim9 script g: is required to find a global non-autoload
4202 // function.
4203 fp = NULL;
4204 if (fp == NULL)
4205 ret = NOTDONE;
4206 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
4207 error = FCERR_DELETED;
4208 else if (fp != NULL)
4209 {
4210 typval_T argvars[1];
4211 funcexe_T funcexe;
4212
4213 argvars[0].v_type = VAR_UNKNOWN;
4214 CLEAR_FIELD(funcexe);
4215 funcexe.fe_evaluate = TRUE;
4216
4217 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
4218 if (error == FCERR_NONE)
4219 ret = OK;
4220 }
4221
4222 user_func_error(error, name, FALSE);
4223 vim_free(tofree);
4224 vim_free(name);
4225
4226 return ret;
4227}
4228
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004229 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004230printable_func_name(ufunc_T *fp)
4231{
4232 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4233}
4234
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004235/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004236 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4237 * TRUE. Otherwise return FALSE.
4238 */
4239 static int
4240function_list_modified(int prev_ht_changed)
4241{
4242 if (prev_ht_changed != func_hashtab.ht_changed)
4243 {
4244 emsg(_(e_function_list_was_modified));
4245 return TRUE;
4246 }
4247 return FALSE;
4248}
4249
4250/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004251 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004252 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004253 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004254list_func_head(ufunc_T *fp, int indent)
4255{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004256 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004257 int j;
4258
4259 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004260
4261 // a timer at the more prompt may have deleted the function
4262 if (function_list_modified(prev_ht_changed))
4263 return FAIL;
4264
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004265 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004266 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004267 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004268 msg_puts("def ");
4269 else
4270 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004271 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004272 msg_putchar('(');
4273 for (j = 0; j < fp->uf_args.ga_len; ++j)
4274 {
4275 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004276 msg_puts(", ");
4277 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 if (fp->uf_arg_types != NULL)
4279 {
4280 char *tofree;
4281
4282 msg_puts(": ");
4283 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4284 vim_free(tofree);
4285 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004286 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4287 {
4288 msg_puts(" = ");
4289 msg_puts(((char **)(fp->uf_def_args.ga_data))
4290 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4291 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004292 }
4293 if (fp->uf_varargs)
4294 {
4295 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004296 msg_puts(", ");
4297 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 if (fp->uf_va_name != NULL)
4300 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004301 if (!fp->uf_varargs)
4302 {
4303 if (j)
4304 msg_puts(", ");
4305 msg_puts("...");
4306 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004308 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309 {
4310 char *tofree;
4311
4312 msg_puts(": ");
4313 msg_puts(type_name(fp->uf_va_type, &tofree));
4314 vim_free(tofree);
4315 }
4316 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004317 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004318
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004319 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004320 {
4321 if (fp->uf_ret_type != &t_void)
4322 {
4323 char *tofree;
4324
4325 msg_puts(": ");
4326 msg_puts(type_name(fp->uf_ret_type, &tofree));
4327 vim_free(tofree);
4328 }
4329 }
4330 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004331 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004332 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004333 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004334 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004335 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004336 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004337 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004338 msg_clr_eos();
4339 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004340 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004341
4342 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004343}
4344
4345/*
4346 * Get a function name, translating "<SID>" and "<SNR>".
4347 * Also handles a Funcref in a List or Dictionary.
4348 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004349 * Set "*is_global" to TRUE when the function must be global, unless
4350 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004351 * flags:
4352 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004353 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004354 * TFN_QUIET: be quiet
4355 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004356 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004357 * Advances "pp" to just after the function name (if no error).
4358 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004359 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004360trans_function_name(
4361 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004362 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004363 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004364 int flags)
4365{
4366 return trans_function_name_ext(pp, is_global, skip, flags,
4367 NULL, NULL, NULL, NULL);
4368}
4369
4370/*
4371 * trans_function_name() with extra arguments.
4372 * "fdp", "partial", "type" and "ufunc" can be NULL.
4373 */
Yegappan Lakshmanana04003a2024-10-27 21:54:11 +01004374 static char_u *
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004375trans_function_name_ext(
4376 char_u **pp,
4377 int *is_global,
4378 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004379 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004380 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004381 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004382 type_T **type, // return: type of funcref
4383 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004384{
4385 char_u *name = NULL;
4386 char_u *start;
4387 char_u *end;
4388 int lead;
4389 char_u sid_buf[20];
4390 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004392 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004393 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004394 int vim9script = in_vim9script();
4395 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004396
4397 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004398 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004399 start = *pp;
4400
Bram Moolenaare38eab22019-12-05 21:50:01 +01004401 // Check for hard coded <SNR>: already translated function ID (from a user
4402 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004403 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4404 && (*pp)[2] == (int)KE_SNR)
4405 {
4406 *pp += 3;
4407 len = get_id_len(pp) + 3;
4408 return vim_strnsave(start, len);
4409 }
4410
Bram Moolenaare38eab22019-12-05 21:50:01 +01004411 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4412 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004413 lead = eval_fname_script(start);
4414 if (lead > 2)
4415 start += lead;
4416
Bram Moolenaare38eab22019-12-05 21:50:01 +01004417 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004418 end = get_lval(start, NULL, &lv, FALSE, skip,
4419 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004420 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004421 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004422 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004423 {
4424 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004425 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004426 goto theend;
4427 }
4428 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4429 {
4430 /*
4431 * Report an invalid expression in braces, unless the expression
4432 * evaluation has been cancelled due to an aborting error, an
4433 * interrupt, or an exception.
4434 */
4435 if (!aborting())
4436 {
4437 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004438 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004439 }
4440 else
4441 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4442 goto theend;
4443 }
4444
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004445 if (lv.ll_ufunc != NULL)
4446 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004447 if (ufunc != NULL)
4448 *ufunc = lv.ll_ufunc;
John Marriottb32800f2025-02-01 15:25:34 +01004449 name = vim_strnsave(lv.ll_ufunc->uf_name, lv.ll_ufunc->uf_namelen);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004450 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004451 goto theend;
4452 }
4453
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004454 if (lv.ll_tv != NULL)
4455 {
4456 if (fdp != NULL)
4457 {
4458 fdp->fd_dict = lv.ll_dict;
4459 fdp->fd_newkey = lv.ll_newkey;
4460 lv.ll_newkey = NULL;
4461 fdp->fd_di = lv.ll_di;
4462 }
4463 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4464 {
4465 name = vim_strsave(lv.ll_tv->vval.v_string);
4466 *pp = end;
4467 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004468 else if (lv.ll_tv->v_type == VAR_CLASS
4469 && lv.ll_tv->vval.v_class != NULL)
4470 {
4471 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4472 *pp = end;
4473 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004474 else if (lv.ll_tv->v_type == VAR_PARTIAL
4475 && lv.ll_tv->vval.v_partial != NULL)
4476 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004477 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004478 *pp = end;
4479 if (partial != NULL)
4480 *partial = lv.ll_tv->vval.v_partial;
4481 }
4482 else
4483 {
4484 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4485 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004486 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004487 else
4488 *pp = end;
4489 name = NULL;
4490 }
4491 goto theend;
4492 }
4493
4494 if (lv.ll_name == NULL)
4495 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004496 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004497 *pp = end;
4498 goto theend;
4499 }
4500
Bram Moolenaare38eab22019-12-05 21:50:01 +01004501 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004502 if (lv.ll_exp_name != NULL)
4503 {
4504 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004505 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004506 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004507 if (name == lv.ll_exp_name)
4508 name = NULL;
4509 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004510 else if (lv.ll_sid > 0)
4511 {
4512 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4513 int cc = *lv.ll_name_end;
4514
4515 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4516 *lv.ll_name_end = NUL;
4517 if (si->sn_autoload_prefix != NULL)
4518 {
4519 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4520 }
4521 else
4522 {
4523 sid_buf[0] = K_SPECIAL;
4524 sid_buf[1] = KS_EXTRA;
4525 sid_buf[2] = (int)KE_SNR;
4526 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004527 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004528 name = concat_str(sid_buf, lv.ll_name);
4529 }
4530 *lv.ll_name_end = cc;
4531 *pp = end;
4532 goto theend;
4533 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004534 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004535 {
4536 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004537 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004538 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004539 if (name == *pp)
4540 name = NULL;
4541 }
4542 if (name != NULL)
4543 {
4544 name = vim_strsave(name);
4545 *pp = end;
John Marriottb32800f2025-02-01 15:25:34 +01004546 if (name != NULL && STRNCMP(name, "<SNR>", 5) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004547 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004548 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004549 name[0] = K_SPECIAL;
4550 name[1] = KS_EXTRA;
4551 name[2] = (int)KE_SNR;
4552 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4553 }
4554 goto theend;
4555 }
4556
4557 if (lv.ll_exp_name != NULL)
4558 {
4559 len = (int)STRLEN(lv.ll_exp_name);
4560 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4561 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4562 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004563 // When there was "s:" already or the name expanded to get a
4564 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004565 lv.ll_name += 2;
4566 len -= 2;
4567 lead = 2;
4568 }
4569 }
4570 else
4571 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004572 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004573 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004574 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004575 if (lv.ll_name[0] == 'g')
4576 {
4577 if (is_global != NULL)
4578 {
4579 *is_global = TRUE;
4580 }
4581 else
4582 {
4583 // dropping "g:" without setting "is_global" won't work in
4584 // Vim9script, put it back later
4585 prefix_g = TRUE;
4586 extra = 2;
4587 }
4588 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004589 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004590 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004591 len = (int)(end - lv.ll_name);
4592 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004593 if (len <= 0)
4594 {
4595 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004596 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004597 goto theend;
4598 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004599
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004600 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004601 // starts with a lower case character: dict.func(). Or when in a class.
4602 vim9_local = ASCII_ISUPPER(*start) && vim9script
4603 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004605 /*
4606 * Copy the function name to allocated memory.
4607 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4608 * Accept <SNR>123_name() outside a script.
4609 */
4610 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004611 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004612 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004613 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004614 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004615 {
Kota Kato948a3892022-08-16 16:09:59 +01004616 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4617 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004618 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004619 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004620 goto theend;
4621 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004623 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004624 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004626 || eval_fname_sid(*pp))
4627 {
John Marriottb32800f2025-02-01 15:25:34 +01004628 size_t sid_buflen;
4629
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004631 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004632 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004633 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004634 goto theend;
4635 }
John Marriottb32800f2025-02-01 15:25:34 +01004636 sid_buflen = vim_snprintf((char *)sid_buf, sizeof(sid_buf), "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004637 if (vim9_local)
John Marriottb32800f2025-02-01 15:25:34 +01004638 extra = 3 + (int)sid_buflen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 else
John Marriottb32800f2025-02-01 15:25:34 +01004640 lead += (int)sid_buflen;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004641 }
4642 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004643 // The function name must start with an upper case letter (unless it is a
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004644 // Vim9 class new() function or a Vim9 class private method or one of the
4645 // supported Vim9 object builtin functions)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004646 else if (!(flags & TFN_INT)
4647 && (builtin_function(lv.ll_name, len)
4648 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004649 && !((flags & TFN_IN_CLASS)
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004650 && (is_valid_builtin_obj_methodname(lv.ll_name)
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004651 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004652 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004653 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004654 : e_function_name_must_start_with_capital_or_s_str),
4655 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004656 goto theend;
4657 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004658 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004659 {
4660 char_u *cp = vim_strchr(lv.ll_name, ':');
4661
4662 if (cp != NULL && cp < end)
4663 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004664 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004665 goto theend;
4666 }
4667 }
4668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004669 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004670 if (name != NULL)
4671 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004672 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004673 {
4674 name[0] = K_SPECIAL;
4675 name[1] = KS_EXTRA;
4676 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004677 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004678 STRCPY(name + 3, sid_buf);
4679 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004680 else if (prefix_g)
4681 {
4682 name[0] = 'g';
4683 name[1] = ':';
4684 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004685 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4686 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004687 }
4688 *pp = end;
4689
4690theend:
4691 clear_lval(&lv);
4692 return name;
4693}
4694
4695/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004696 * Assuming "name" is the result of trans_function_name() and it was prefixed
4697 * to use the script-local name, return the unmodified name (points into
4698 * "name"). Otherwise return NULL.
4699 * This can be used to first search for a script-local function and fall back
4700 * to the global function if not found.
4701 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004702 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004703untrans_function_name(char_u *name)
4704{
4705 char_u *p;
4706
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004707 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004708 {
4709 p = vim_strchr(name, '_');
4710 if (p != NULL)
4711 return p + 1;
4712 }
4713 return NULL;
4714}
4715
4716/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004717 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4718 * current script ID and returns the expanded function name. The caller should
4719 * free the returned name. If not called from a script context or the function
4720 * name doesn't start with these prefixes, then returns NULL.
4721 * This doesn't check whether the script-local function exists or not.
4722 */
4723 char_u *
4724get_scriptlocal_funcname(char_u *funcname)
4725{
4726 char sid_buf[25];
John Marriottb32800f2025-02-01 15:25:34 +01004727 size_t sid_buflen;
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004728 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004729 char_u *newname;
John Marriottb32800f2025-02-01 15:25:34 +01004730 size_t newnamesize;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004731 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004732
4733 if (funcname == NULL)
4734 return NULL;
4735
4736 if (STRNCMP(funcname, "s:", 2) != 0
4737 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004738 {
4739 ufunc_T *ufunc;
4740
4741 // The function name does not have a script-local prefix. Try finding
4742 // it when in a Vim9 script and there is no "g:" prefix.
4743 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4744 return NULL;
4745 ufunc = find_func(funcname, FALSE);
4746 if (ufunc == NULL || func_is_global(ufunc)
4747 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4748 return NULL;
4749 ++p;
4750 off = 0;
4751 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004752 else
4753 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004754
4755 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4756 {
4757 emsg(_(e_using_sid_not_in_script_context));
4758 return NULL;
4759 }
4760 // Expand s: prefix into <SNR>nr_<name>
John Marriottb32800f2025-02-01 15:25:34 +01004761 sid_buflen = vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004762 (long)current_sctx.sc_sid);
John Marriottb32800f2025-02-01 15:25:34 +01004763 newnamesize = sid_buflen + STRLEN(p + off) + 1;
4764 newname = alloc(newnamesize);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004765 if (newname == NULL)
4766 return NULL;
John Marriottb32800f2025-02-01 15:25:34 +01004767 vim_snprintf((char *)newname, newnamesize, "%s%s", sid_buf, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004768
4769 return newname;
4770}
4771
4772/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004773 * Return script-local "fname" with the 3-byte sequence replaced by
4774 * printable <SNR> in allocated memory.
4775 */
4776 char_u *
4777alloc_printable_func_name(char_u *fname)
4778{
4779 char_u *n = alloc(STRLEN(fname + 3) + 6);
4780
4781 if (n != NULL)
4782 {
4783 STRCPY(n, "<SNR>");
4784 STRCPY(n + 5, fname + 3);
4785 }
4786 return n;
4787}
4788
4789/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004790 * Call trans_function_name(), except that a lambda is returned as-is.
4791 * Returns the name in allocated memory.
4792 */
4793 char_u *
4794save_function_name(
4795 char_u **name,
4796 int *is_global,
4797 int skip,
4798 int flags,
4799 funcdict_T *fudi)
4800{
4801 char_u *p = *name;
4802 char_u *saved;
4803
4804 if (STRNCMP(p, "<lambda>", 8) == 0)
4805 {
4806 p += 8;
4807 (void)getdigits(&p);
4808 saved = vim_strnsave(*name, p - *name);
4809 if (fudi != NULL)
4810 CLEAR_POINTER(fudi);
4811 }
4812 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004813 saved = trans_function_name_ext(&p, is_global, skip,
4814 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004815 *name = p;
4816 return saved;
4817}
4818
4819/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004820 * List functions. When "regmatch" is NULL all of then.
4821 * Otherwise functions matching "regmatch".
4822 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004823 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004824list_functions(regmatch_T *regmatch)
4825{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004826 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004827 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004828 hashitem_T *hi;
4829
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004830 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004831 {
4832 if (!HASHITEM_EMPTY(hi))
4833 {
4834 ufunc_T *fp = HI2UF(hi);
4835
4836 --todo;
4837 if ((fp->uf_flags & FC_DEAD) == 0
4838 && (regmatch == NULL
4839 ? !message_filtered(fp->uf_name)
4840 && !func_name_refcount(fp->uf_name)
Keith Thompson184f71c2024-01-04 21:19:04 +01004841 : !SAFE_isdigit(*fp->uf_name)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004842 && vim_regexec(regmatch, fp->uf_name, 0)))
4843 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004844 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004845 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004846 if (function_list_modified(prev_ht_changed))
4847 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004848 }
4849 }
4850 }
4851}
4852
4853/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004854 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004855 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4856 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004857 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004858 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4859 * With CF_INTERFACE the function is define inside an interface, only the
4860 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004861 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004862 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004863 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004864define_function(
4865 exarg_T *eap,
4866 char_u *name_arg,
4867 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004868 int class_flags,
4869 ocmember_T *obj_members,
4870 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004871{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004872 int j;
4873 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004874 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004875 char_u *name = name_arg;
John Marriottb32800f2025-02-01 15:25:34 +01004876 size_t namelen = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004877 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004878 char_u *p;
4879 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004880 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004881 char_u *line_arg = NULL;
4882 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004884 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004885 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004886 garray_T newlines;
4887 int varargs = FALSE;
4888 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004890 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004891 int fp_allocated = FALSE;
4892 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004893 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004894 dictitem_T *v;
4895 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004896 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004897 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004898 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004899 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004900 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004901 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004902
4903 /*
4904 * ":function" without argument: list functions.
4905 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004906 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004907 {
4908 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004909 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004910 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004911 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004912 }
4913
4914 /*
4915 * ":function /pat": list functions matching pattern.
4916 */
4917 if (*eap->arg == '/')
4918 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004919 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004920 if (!eap->skip)
4921 {
4922 regmatch_T regmatch;
4923
4924 c = *p;
4925 *p = NUL;
4926 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4927 *p = c;
4928 if (regmatch.regprog != NULL)
4929 {
4930 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004931 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004932 vim_regfree(regmatch.regprog);
4933 }
4934 }
4935 if (*p == '/')
4936 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004937 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004938 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004939 }
4940
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941 ga_init(&newargs);
4942 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004943 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004944 ga_init(&default_args);
4945
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004946 /*
4947 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004948 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004949 * "name" == func, "fudi.fd_dict" == NULL
4950 * dict.func new dictionary entry
4951 * "name" == NULL, "fudi.fd_dict" set,
4952 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4953 * dict.func existing dict entry with a Funcref
4954 * "name" == func, "fudi.fd_dict" set,
4955 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4956 * dict.func existing dict entry that's not a Funcref
4957 * "name" == NULL, "fudi.fd_dict" set,
4958 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4959 * s:func script-local function name
4960 * g:func global function name, same as "func"
4961 */
4962 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004963 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004964 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004965 // nested function, argument is (args).
4966 paren = TRUE;
4967 CLEAR_FIELD(fudi);
4968 }
4969 else
4970 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004971 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004972 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004973 if (p[0] == 's' && p[1] == ':')
4974 {
4975 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4976 return NULL;
4977 }
4978 p = to_name_end(p, TRUE);
4979 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4980 {
4981 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4982 eap->arg);
4983 return NULL;
4984 }
4985 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004986 }
4987
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004988 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004989 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004990 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004991 paren = (vim_strchr(p, '(') != NULL);
4992 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004993 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004994 /*
4995 * Return on an invalid expression in braces, unless the expression
4996 * evaluation has been cancelled due to an aborting error, an
4997 * interrupt, or an exception.
4998 */
4999 if (!aborting())
5000 {
5001 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005002 semsg(_(e_key_not_present_in_dictionary_str),
5003 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005004 vim_free(fudi.fd_newkey);
5005 return NULL;
5006 }
5007 else
5008 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005009 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00005010
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00005011 // For "export def FuncName()" in an autoload script the function name
5012 // is stored with the legacy autoload name "dir#script#FuncName" so
5013 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00005014 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005015 {
5016 char_u *prefixed = may_prefix_autoload(name);
5017
5018 if (prefixed != NULL && prefixed != name)
5019 {
5020 vim_free(name);
5021 name = prefixed;
5022 }
5023 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00005024 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00005025 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00005026 {
5027 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
5028 goto ret_free;
5029 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005030 }
5031
Bram Moolenaare38eab22019-12-05 21:50:01 +01005032 // An error in a function call during evaluation of an expression in magic
5033 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005034 saved_did_emsg = did_emsg;
5035 did_emsg = FALSE;
5036
5037 /*
5038 * ":function func" with only function name: list function.
5039 */
5040 if (!paren)
5041 {
5042 if (!ends_excmd(*skipwhite(p)))
5043 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00005044 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005045 goto ret_free;
5046 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005047 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005048 if (eap->nextcmd != NULL)
5049 *p = NUL;
5050 if (!eap->skip && !got_int)
5051 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005052 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005053 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
5054 {
5055 char_u *up = untrans_function_name(name);
5056
5057 // With Vim9 script the name was made script-local, if not
5058 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02005059 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005060 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005061 }
5062
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005063 if (fp != NULL)
5064 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00005065 // Check no function was added or removed from a timer, e.g. at
5066 // the more prompt. "fp" may then be invalid.
5067 int prev_ht_changed = func_hashtab.ht_changed;
5068
5069 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005070 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00005071 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
5072 {
5073 if (FUNCLINE(fp, j) == NULL)
5074 continue;
5075 msg_putchar('\n');
5076 msg_outnum((long)(j + 1));
5077 if (j < 9)
5078 msg_putchar(' ');
5079 if (j < 99)
5080 msg_putchar(' ');
5081 if (function_list_modified(prev_ht_changed))
5082 break;
5083 msg_prt_line(FUNCLINE(fp, j), FALSE);
5084 out_flush(); // show a line at a time
5085 ui_breakcheck();
5086 }
5087 if (!got_int)
5088 {
5089 msg_putchar('\n');
5090 if (!function_list_modified(prev_ht_changed))
5091 {
5092 if (fp->uf_def_status != UF_NOT_COMPILED)
5093 msg_puts(" enddef");
5094 else
5095 msg_puts(" endfunction");
5096 }
5097 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005098 }
5099 }
5100 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00005101 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005102 }
5103 goto ret_free;
5104 }
5105
5106 /*
5107 * ":function name(arg1, arg2)" Define function.
5108 */
5109 p = skipwhite(p);
5110 if (*p != '(')
5111 {
5112 if (!eap->skip)
5113 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005114 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005115 goto ret_free;
5116 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005117 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005118 if (vim_strchr(p, '(') != NULL)
5119 p = vim_strchr(p, '(');
5120 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005121
Bram Moolenaar4efd9942021-01-24 21:14:20 +01005122 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
5123 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01005124 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01005125 goto ret_free;
5126 }
5127
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02005128 // In Vim9 script only global functions can be redefined.
5129 if (vim9script && eap->forceit && !is_global)
5130 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005131 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02005132 goto ret_free;
5133 }
5134
Bram Moolenaar04935fb2022-01-08 16:19:22 +00005135 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005136
Bram Moolenaar04b12692020-05-04 23:24:44 +02005137 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005138 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005139 // Check the name of the function. Unless it's a dictionary function
5140 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005141 if (name != NULL)
5142 arg = name;
5143 else
5144 arg = fudi.fd_newkey;
5145 if (arg != NULL && (fudi.fd_di == NULL
5146 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
5147 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
5148 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00005149 char_u *name_base = arg;
5150 int i;
5151
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005152 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005153 {
5154 name_base = vim_strchr(arg, '_');
5155 if (name_base == NULL)
5156 name_base = arg + 3;
5157 else
5158 ++name_base;
5159 }
5160 for (i = 0; name_base[i] != NUL && (i == 0
5161 ? eval_isnamec1(name_base[i])
5162 : eval_isnamec(name_base[i])); ++i)
5163 ;
5164 if (name_base[i] != NUL)
zeertzjq6a04bf52024-03-16 09:39:06 +01005165 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005166 emsg_funcname(e_invalid_argument_str, arg);
zeertzjq6a04bf52024-03-16 09:39:06 +01005167 goto ret_free;
5168 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00005169
5170 // In Vim9 script a function cannot have the same name as a
5171 // variable.
5172 if (vim9script && *arg == K_SPECIAL
John Marriottb32800f2025-02-01 15:25:34 +01005173 && eval_variable(name_base, i, 0, NULL,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00005174 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00005175 + EVAL_VAR_NO_FUNC) == OK)
5176 {
5177 semsg(_(e_redefining_script_item_str), name_base);
5178 goto ret_free;
5179 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005180 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005181 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005182 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005183 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005184 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00005185 goto ret_free;
5186 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005187 }
5188
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005189 // This may get more lines and make the pointers into the first line
5190 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01005191 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005192 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00005193 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02005194 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005195 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00005196 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005197 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01005198 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005199
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005201 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005203 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005204 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005205 if (*p != ':')
5206 {
5207 semsg(_(e_no_white_space_allowed_before_colon_str), p);
5208 p = skipwhite(p);
5209 }
5210 else if (!IS_WHITE_OR_NUL(p[1]))
5211 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005212 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02005213 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005214 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005215 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005216 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01005217 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005219 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005220 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005221 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005222 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02005223 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005224 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005225 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02005226 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005227 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005228 else
5229 // find extra arguments "range", "dict", "abort" and "closure"
5230 for (;;)
5231 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01005232 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005233 p = skipwhite(p);
5234 if (STRNCMP(p, "range", 5) == 0)
5235 {
5236 flags |= FC_RANGE;
5237 p += 5;
5238 }
5239 else if (STRNCMP(p, "dict", 4) == 0)
5240 {
5241 flags |= FC_DICT;
5242 p += 4;
5243 }
5244 else if (STRNCMP(p, "abort", 5) == 0)
5245 {
5246 flags |= FC_ABORT;
5247 p += 5;
5248 }
5249 else if (STRNCMP(p, "closure", 7) == 0)
5250 {
5251 flags |= FC_CLOSURE;
5252 p += 7;
5253 if (current_funccal == NULL)
5254 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005255 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005256 name == NULL ? (char_u *)"" : name);
5257 goto erret;
5258 }
5259 }
5260 else
5261 break;
5262 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005263
Bram Moolenaare38eab22019-12-05 21:50:01 +01005264 // When there is a line break use what follows for the function body.
5265 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005266 if (*p == '\n')
5267 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005268 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005269 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5270 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005271 && !(VIM_ISWHITE(*whitep) && *p == '#'
5272 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005273 && !eap->skip
5274 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005275 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005276
5277 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005278 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5279 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005280 */
5281 if (KeyTyped)
5282 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005283 // Check if the function already exists, don't let the user type the
5284 // whole function before telling him it doesn't work! For a script we
5285 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005286 if (!eap->skip && !eap->forceit)
5287 {
5288 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005289 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005290 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005291 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005292 }
5293
5294 if (!eap->skip && did_emsg)
5295 goto erret;
5296
Bram Moolenaare38eab22019-12-05 21:50:01 +01005297 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005298 cmdline_row = msg_row;
5299 }
5300
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005301 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005302 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005303
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005304 // Do not define the function when getting the body fails and when
5305 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005306 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005307 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005308 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5309 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005310 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005311 goto erret;
5312
5313 /*
5314 * If there are no errors, add the function
5315 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005316 if (fudi.fd_dict != NULL)
5317 {
John Marriottb32800f2025-02-01 15:25:34 +01005318 char numbuf[NUMBUFLEN];
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005319
5320 fp = NULL;
5321 if (fudi.fd_newkey == NULL && !eap->forceit)
5322 {
5323 emsg(_(e_dictionary_entry_already_exists));
5324 goto erret;
5325 }
5326 if (fudi.fd_di == NULL)
5327 {
5328 // Can't add a function to a locked dictionary
5329 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5330 goto erret;
5331 }
5332 // Can't change an existing function if it is locked
5333 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5334 goto erret;
5335
5336 // Give the function a sequential number. Can only be used with a
5337 // Funcref!
5338 vim_free(name);
John Marriottb32800f2025-02-01 15:25:34 +01005339 namelen = vim_snprintf(numbuf, sizeof(numbuf), "%d", ++func_nr);
5340 name = vim_strnsave((char_u *)numbuf, namelen);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005341 if (name == NULL)
5342 goto erret;
5343 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005344 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005345 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005346 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005347 char_u *find_name = name;
5348 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005349 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005350
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005351 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005352 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005353 var_conflict = TRUE;
5354
5355 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5356 {
5357 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5358
5359 if (si->sn_autoload_prefix != NULL)
5360 {
5361 if (is_export)
5362 {
5363 find_name = name + STRLEN(si->sn_autoload_prefix);
5364 v = find_var(find_name, &ht, TRUE);
5365 if (v != NULL)
5366 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005367 // Only check if the function already exists in the script,
5368 // global functions can be shadowed.
5369 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005370 }
5371 else
5372 {
5373 char_u *prefixed = may_prefix_autoload(name);
5374
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005375 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005376 {
5377 v = find_var(prefixed, &ht, TRUE);
5378 if (v != NULL)
5379 var_conflict = TRUE;
5380 vim_free(prefixed);
5381 }
5382 }
5383 }
5384 }
5385 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005386 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005387 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005388 goto erret;
5389 }
5390
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005391 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005392 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005393 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005394 char_u *uname = untrans_function_name(name);
5395
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005396 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005397 }
5398
5399 if (fp != NULL || import != NULL)
5400 {
5401 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005402
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005403 // Function can be replaced with "function!" and when sourcing the
5404 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005405 // A name that is used by an import can not be overruled.
5406 if (import != NULL
5407 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005408 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005409 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005410 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005411 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005412 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005413 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005414 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005415 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
zeertzjq04d2a3f2025-02-02 19:03:17 +01005416 goto errret_keep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005417 }
5418 if (fp->uf_calls > 0)
5419 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005420 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005421 e_cannot_redefine_function_str_it_is_in_use, name);
zeertzjq04d2a3f2025-02-02 19:03:17 +01005422 goto errret_keep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005423 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005424 if (fp->uf_refcount > 1)
5425 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005426 // This function is referenced somewhere, don't redefine it but
5427 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005428 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005429 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005430 fp = NULL;
5431 overwrite = TRUE;
5432 }
5433 else
5434 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005435 char_u *exp_name = fp->uf_name_exp;
5436
5437 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005438 VIM_CLEAR(name);
John Marriottb32800f2025-02-01 15:25:34 +01005439 namelen = 0;
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005440 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005441 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005442 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005443 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005444#ifdef FEAT_PROFILE
5445 fp->uf_profiling = FALSE;
5446 fp->uf_prof_initialized = FALSE;
5447#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005448 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005449 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005450 }
5451 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005452
5453 if (fp == NULL)
5454 {
5455 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5456 {
5457 int slen, plen;
5458 char_u *scriptname;
5459
Bram Moolenaare38eab22019-12-05 21:50:01 +01005460 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005461 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005462 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005463 {
5464 scriptname = autoload_name(name);
5465 if (scriptname != NULL)
5466 {
5467 p = vim_strchr(scriptname, '/');
5468 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005469 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005470 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005471 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005472 j = OK;
5473 vim_free(scriptname);
5474 }
5475 }
5476 if (j == FAIL)
5477 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005478 linenr_T save_lnum = SOURCING_LNUM;
5479
5480 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005481 semsg(_(e_function_name_does_not_match_script_file_name_str),
5482 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005483 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005484 goto erret;
5485 }
5486 }
5487
John Marriottb32800f2025-02-01 15:25:34 +01005488 if (namelen == 0)
5489 namelen = STRLEN(name);
5490 fp = alloc_ufunc(name, namelen);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005491 if (fp == NULL)
5492 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005493 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005494
5495 if (fudi.fd_dict != NULL)
5496 {
5497 if (fudi.fd_di == NULL)
5498 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005499 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005500 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5501 if (fudi.fd_di == NULL)
5502 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005503 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005504 goto erret;
5505 }
5506 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5507 {
5508 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005509 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005510 goto erret;
5511 }
5512 }
5513 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005514 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005515 clear_tv(&fudi.fd_di->di_tv);
5516 fudi.fd_di->di_tv.v_type = VAR_FUNC;
John Marriottb32800f2025-02-01 15:25:34 +01005517 fudi.fd_di->di_tv.vval.v_string = vim_strnsave(name, namelen);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005518
Bram Moolenaare38eab22019-12-05 21:50:01 +01005519 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005520 flags |= FC_DICT;
5521 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005522 }
5523 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005524 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005525 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005526 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005527
5528 if (eap->cmdidx == CMD_def)
5529 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005530 int lnum_save = SOURCING_LNUM;
5531 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005532
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005533 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005534
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005535 // error messages are for the first function line
5536 SOURCING_LNUM = sourcing_lnum_top;
5537
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005538 // The function may use script variables from the context.
5539 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005540
Yegappan Lakshmanand79ea472025-01-20 21:09:13 +01005541 // The argument types and the return type may use an imported type.
5542 // In that case, the imported file will be sourced. To avoid treating
5543 // everything in the imported file as exported, temporarily reset
5544 // is_export.
5545 int save_is_export = is_export;
5546 is_export = FALSE;
5547
h-eastb895b0f2023-09-24 15:46:31 +02005548 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5549 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005550 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005551 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005552 free_fp = fp_allocated;
Yegappan Lakshmanand79ea472025-01-20 21:09:13 +01005553 is_export = save_is_export;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005554 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005555 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005556 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005557
5558 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005559 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005560 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005561 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005562 free_fp = fp_allocated;
Yegappan Lakshmanand79ea472025-01-20 21:09:13 +01005563 is_export = save_is_export;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005564 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005565 }
Yegappan Lakshmanand79ea472025-01-20 21:09:13 +01005566 is_export = save_is_export;
Bram Moolenaar09689a02020-05-09 22:50:08 +02005567 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005568 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005569 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005570 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005571
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005572 if (fp_allocated)
5573 {
5574 // insert the new function in the function list
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005575 if (overwrite)
5576 {
5577 hi = hash_find(&func_hashtab, name);
5578 hi->hi_key = UF2HIKEY(fp);
5579 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005580 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005581 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005582 {
5583 free_fp = TRUE;
5584 goto erret;
5585 }
5586 fp->uf_refcount = 1;
5587 }
5588
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005589 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005590 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005591 if ((flags & FC_CLOSURE) != 0)
5592 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005593 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005594 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005595 }
5596 else
5597 fp->uf_scoped = NULL;
5598
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005599#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005600 if (prof_def_func())
5601 func_do_profile(fp);
5602#endif
5603 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005604 if (sandbox)
5605 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005606 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005607 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005608 fp->uf_flags = flags;
5609 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005610 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005611 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005612 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005613 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005614 if (is_export)
5615 {
5616 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005617 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005618 is_export = FALSE;
5619 }
5620
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005621 if (eap->cmdidx == CMD_def)
5622 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005623 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5624 // :func does not use Vim9 script syntax, even in a Vim9 script file
5625 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005626
Yegappan Lakshmanan5715a722024-05-03 18:24:07 +02005627 // If test_override('defcompile' 1) is used, then compile the function now
5628 if (eap->cmdidx == CMD_def && override_defcompile)
5629 defcompile_function(fp, NULL);
5630
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005631 goto ret_free;
5632
5633erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005634 if (fp != NULL)
5635 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005636 // these were set to "newargs" and "default_args", which are cleared
5637 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005638 ga_init(&fp->uf_args);
5639 ga_init(&fp->uf_def_args);
5640 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005641errret_2:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005642 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005643 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005644 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005645 VIM_CLEAR(fp->uf_va_name);
John Marriottb32800f2025-02-01 15:25:34 +01005646 VIM_CLEAR(fp->uf_name_exp);
Christian Brabandt0f287912023-12-11 17:53:25 +01005647 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005648 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005649 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005650 VIM_CLEAR(fp);
zeertzjq04d2a3f2025-02-02 19:03:17 +01005651errret_keep:
5652 ga_clear_strings(&newargs);
5653 ga_clear_strings(&default_args);
5654 ga_clear_strings(&newlines);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005655ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005656 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005657 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005658 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005659 if (name != name_arg)
5660 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005661 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005662 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005663
5664 return fp;
5665}
5666
5667/*
5668 * ":function"
5669 */
5670 void
5671ex_function(exarg_T *eap)
5672{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005673 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005674
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005675 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005676 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005677 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005678}
5679
LemonBoy48b7d052024-07-09 18:24:59 +02005680 int
5681get_func_arity(char_u *name, int *required, int *optional, int *varargs)
5682{
5683 ufunc_T *ufunc = NULL;
5684 int argcount = 0;
5685 int min_argcount = 0;
5686 int idx;
5687
5688 idx = find_internal_func(name);
5689 if (idx >= 0)
5690 {
5691 internal_func_get_argcount(idx, &argcount, &min_argcount);
5692 *varargs = FALSE;
5693 }
5694 else
5695 {
5696 char_u fname_buf[FLEN_FIXED + 1];
5697 char_u *tofree = NULL;
5698 funcerror_T error = FCERR_NONE;
5699 char_u *fname;
5700
5701 // May need to translate <SNR>123_ to K_SNR.
5702 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
5703 if (error == FCERR_NONE)
5704 ufunc = find_func(fname, FALSE);
5705 vim_free(tofree);
5706
5707 if (ufunc == NULL)
5708 return FAIL;
5709
5710 argcount = ufunc->uf_args.ga_len;
5711 min_argcount = ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len;
5712 *varargs = has_varargs(ufunc);
5713 }
5714
5715 *required = min_argcount;
5716 *optional = argcount - min_argcount;
5717
5718 return OK;
5719}
5720
Bram Moolenaar822ba242020-05-24 23:00:18 +02005721/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005722 * Find a function by name, including "<lambda>123".
5723 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005724 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005725 * Return NULL if not found.
5726 */
5727 ufunc_T *
5728find_func_by_name(char_u *name, compiletype_T *compile_type)
5729{
5730 char_u *arg = name;
5731 char_u *fname;
5732 ufunc_T *ufunc;
5733 int is_global = FALSE;
5734
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005735 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5736 {
5737 *compile_type = CT_PROFILE;
5738 arg = skipwhite(arg + 7);
5739 }
5740 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5741 {
5742 *compile_type = CT_DEBUG;
5743 arg = skipwhite(arg + 5);
5744 }
5745
5746 if (STRNCMP(arg, "<lambda>", 8) == 0)
5747 {
5748 arg += 8;
5749 (void)getdigits(&arg);
5750 fname = vim_strnsave(name, arg - name);
5751 }
5752 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005753 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005754 // First try finding a method in a class, trans_function_name() will
5755 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005756 ufunc = find_class_func(&arg);
5757 if (ufunc != NULL)
5758 return ufunc;
5759
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005760 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005761 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005762 NULL, NULL, NULL, &ufunc);
5763 if (ufunc != NULL)
5764 {
5765 vim_free(fname);
5766 return ufunc;
5767 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005768 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005769 if (fname == NULL)
5770 {
5771 semsg(_(e_invalid_argument_str), name);
5772 return NULL;
5773 }
5774 if (!ends_excmd2(name, arg))
5775 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005776 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005777 emsg(ex_errmsg(e_trailing_characters_str, arg));
5778 return NULL;
5779 }
5780
5781 ufunc = find_func(fname, is_global);
5782 if (ufunc == NULL)
5783 {
5784 char_u *p = untrans_function_name(fname);
5785
5786 if (p != NULL)
5787 // Try again without making it script-local.
5788 ufunc = find_func(p, FALSE);
5789 }
5790 vim_free(fname);
5791 if (ufunc == NULL)
5792 semsg(_(e_cannot_find_function_str), name);
5793 return ufunc;
5794}
5795
5796/*
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005797 * Compile the :def function "ufunc". If "cl" is not NULL, then compile the
5798 * class or object method "ufunc" in "cl".
5799 */
5800 void
5801defcompile_function(ufunc_T *ufunc, class_T *cl)
5802{
5803 compiletype_T compile_type = CT_NONE;
5804
5805 if (func_needs_compiling(ufunc, compile_type))
5806 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5807 else
5808 smsg(_("Function %s%s%s does not need compiling"),
5809 cl != NULL ? cl->class_name : (char_u *)"",
5810 cl != NULL ? (char_u *)"." : (char_u *)"",
5811 ufunc->uf_name);
5812}
5813
5814/*
5815 * Compile all the :def functions defined in the current script
5816 */
5817 static void
5818defcompile_funcs_in_script(void)
5819{
5820 long todo = (long)func_hashtab.ht_used;
5821 int changed = func_hashtab.ht_changed;
5822 hashitem_T *hi;
5823
5824 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5825 {
5826 if (!HASHITEM_EMPTY(hi))
5827 {
5828 --todo;
5829 ufunc_T *ufunc = HI2UF(hi);
5830 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5831 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5832 && (ufunc->uf_flags & FC_DEAD) == 0)
5833 {
5834 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5835
5836 if (func_hashtab.ht_changed != changed)
5837 {
5838 // a function has been added or removed, need to start
5839 // over
5840 todo = (long)func_hashtab.ht_used;
5841 changed = func_hashtab.ht_changed;
5842 hi = func_hashtab.ht_array;
5843 --hi;
5844 }
5845 }
5846 }
5847 }
5848}
5849
5850/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005851 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005852 * be compiled or the one specified by the argument.
5853 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005854 */
5855 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005856ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005857{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005858 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005859 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005860 typval_T tv;
5861
5862 if (is_class_name(eap->arg, &tv))
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005863 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005864 class_T *cl = tv.vval.v_class;
5865
5866 if (cl != NULL)
5867 defcompile_class(cl);
5868 }
5869 else
5870 {
5871 compiletype_T compile_type = CT_NONE;
5872 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
5873 if (ufunc != NULL)
5874 defcompile_function(ufunc, NULL);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005875 }
5876 }
5877 else
5878 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005879 defcompile_funcs_in_script();
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005880
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005881 // compile all the class defined in the current script
5882 defcompile_classes_in_script();
Bram Moolenaar822ba242020-05-24 23:00:18 +02005883 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005884}
5885
5886/*
5887 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5888 * Return 2 if "p" starts with "s:".
5889 * Return 0 otherwise.
5890 */
5891 int
5892eval_fname_script(char_u *p)
5893{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005894 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5895 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005896 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5897 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5898 return 5;
5899 if (p[0] == 's' && p[1] == ':')
5900 return 2;
5901 return 0;
5902}
5903
5904 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005905translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005906{
5907 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005908 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005909 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005910}
5911
5912/*
5913 * Return TRUE when "ufunc" has old-style "..." varargs
5914 * or named varargs "...name: type".
5915 */
5916 int
5917has_varargs(ufunc_T *ufunc)
5918{
5919 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005920}
5921
5922/*
5923 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005924 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005925 */
5926 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005927function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005928{
5929 char_u *nm = name;
5930 char_u *p;
5931 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005932 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005933 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005934
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005935 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5936 if (no_deref)
5937 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005938 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005939 nm = skipwhite(nm);
5940
Bram Moolenaare38eab22019-12-05 21:50:01 +01005941 // Only accept "funcname", "funcname ", "funcname (..." and
5942 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005943 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005944 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005945 vim_free(p);
5946 return n;
5947}
5948
Bram Moolenaar113e1072019-01-20 15:30:40 +01005949#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005950 char_u *
5951get_expanded_name(char_u *name, int check)
5952{
5953 char_u *nm = name;
5954 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005955 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005956
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005957 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005958
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005959 if (p != NULL && *nm == NUL
5960 && (!check || translated_function_exists(p, is_global)))
5961 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005962
5963 vim_free(p);
5964 return NULL;
5965}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005966#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005967
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005968/*
5969 * Function given to ExpandGeneric() to obtain the list of user defined
5970 * function names.
5971 */
5972 char_u *
5973get_user_func_name(expand_T *xp, int idx)
5974{
5975 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005976 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005977 static hashitem_T *hi;
5978 ufunc_T *fp;
5979
5980 if (idx == 0)
5981 {
5982 done = 0;
5983 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005984 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005985 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005986 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005987 {
John Marriottb32800f2025-02-01 15:25:34 +01005988 int len;
5989
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005990 if (done++ > 0)
5991 ++hi;
5992 while (HASHITEM_EMPTY(hi))
5993 ++hi;
5994 fp = HI2UF(hi);
5995
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005996 // don't show dead, dict and lambda functions
5997 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005998 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005999 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006000
John Marriottb32800f2025-02-01 15:25:34 +01006001 if (fp->uf_namelen + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006002 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006003
John Marriottb32800f2025-02-01 15:25:34 +01006004 len = cat_func_name(IObuff, IOSIZE, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02006005 if (xp->xp_context != EXPAND_USER_FUNC
6006 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006007 {
John Marriottb32800f2025-02-01 15:25:34 +01006008 STRCPY(IObuff + len, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006009 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
John Marriottb32800f2025-02-01 15:25:34 +01006010 {
6011 ++len;
6012 STRCPY(IObuff + len, ")");
6013 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006014 }
6015 return IObuff;
6016 }
6017 return NULL;
6018}
6019
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006020/*
Bram Moolenaar83677162023-01-08 19:54:10 +00006021 * Make a copy of a function.
6022 * Intended to be used for a function defined on a base class that has a copy
6023 * on the child class.
6024 * The copy has uf_refcount set to one.
6025 * Returns NULL when out of memory.
6026 */
6027 ufunc_T *
6028copy_function(ufunc_T *fp)
6029{
John Marriottb32800f2025-02-01 15:25:34 +01006030 ufunc_T *ufunc = alloc_ufunc(fp->uf_name, fp->uf_namelen);
Bram Moolenaar83677162023-01-08 19:54:10 +00006031 if (ufunc == NULL)
6032 return NULL;
6033
6034 // Most things can just be copied.
6035 *ufunc = *fp;
6036
6037 ufunc->uf_def_status = UF_TO_BE_COMPILED;
6038 ufunc->uf_dfunc_idx = 0;
6039 ufunc->uf_class = NULL;
6040
6041 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
6042 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
6043
6044 if (ufunc->uf_arg_types != NULL)
6045 {
6046 // "uf_arg_types" is an allocated array, make a copy.
6047 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
6048 if (at != NULL)
6049 {
6050 mch_memmove(at, ufunc->uf_arg_types,
6051 sizeof(type_T *) * ufunc->uf_args.ga_len);
6052 ufunc->uf_arg_types = at;
6053 }
6054 }
6055
6056 // TODO: how about the types themselves? they can be freed when the
6057 // original function is freed:
6058 // type_T **uf_arg_types;
6059 // type_T *uf_ret_type;
6060
Ernie Rael114ec812023-06-04 18:11:35 +01006061 // make uf_type_list empty
6062 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00006063
6064 // TODO: partial_T *uf_partial;
6065
6066 if (ufunc->uf_va_name != NULL)
6067 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
6068
6069 // TODO:
6070 // type_T *uf_va_type;
6071 // type_T *uf_func_type;
6072
6073 ufunc->uf_block_depth = 0;
6074 ufunc->uf_block_ids = NULL;
6075
6076 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
6077
6078 ufunc->uf_refcount = 1;
Bram Moolenaar83677162023-01-08 19:54:10 +00006079
6080 return ufunc;
6081}
6082
6083/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006084 * ":delfunction {name}"
6085 */
6086 void
6087ex_delfunction(exarg_T *eap)
6088{
6089 ufunc_T *fp = NULL;
6090 char_u *p;
6091 char_u *name;
6092 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006093 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006094
6095 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00006096 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
6097 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006098 vim_free(fudi.fd_newkey);
6099 if (name == NULL)
6100 {
6101 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00006102 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006103 return;
6104 }
6105 if (!ends_excmd(*skipwhite(p)))
6106 {
6107 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00006108 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006109 return;
6110 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006111 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006112 if (eap->nextcmd != NULL)
6113 *p = NUL;
6114
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006115 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02006116 {
6117 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006118 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02006119 vim_free(name);
6120 return;
6121 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006122 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006123 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006124 vim_free(name);
6125
6126 if (!eap->skip)
6127 {
6128 if (fp == NULL)
6129 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02006130 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00006131 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006132 return;
6133 }
6134 if (fp->uf_calls > 0)
6135 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006136 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006137 return;
6138 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006139 if (fp->uf_flags & FC_VIM9)
6140 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006141 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006142 return;
6143 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006144
6145 if (fudi.fd_dict != NULL)
6146 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006147 // Delete the dict item that refers to the function, it will
6148 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00006149 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006150 }
6151 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006152 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006153 // A normal function (not a numbered function or lambda) has a
6154 // refcount of 1 for the entry in the hashtable. When deleting
6155 // it and the refcount is more than one, it should be kept.
6156 // A numbered function and lambda should be kept if the refcount is
6157 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006158 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006159 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006160 // Function is still referenced somewhere. Don't free it but
6161 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006162 if (func_remove(fp))
6163 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006164 }
6165 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01006166 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006167 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006168 }
6169}
6170
6171/*
6172 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006173 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006174 */
6175 void
6176func_unref(char_u *name)
6177{
Bram Moolenaar97baee82016-07-26 20:46:08 +02006178 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006179
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006180 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006181 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006182 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006183 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006184 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006185#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006186 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006187#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01006188 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006189 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006190 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006191}
6192
6193/*
6194 * Unreference a Function: decrement the reference count and free it when it
6195 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006196 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006197 */
6198 void
6199func_ptr_unref(ufunc_T *fp)
6200{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006201 if (fp != NULL && (--fp->uf_refcount <= 0
6202 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
6203 && fp->uf_partial->pt_refcount <= 1
6204 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02006205 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006206 // Only delete it when it's not being used. Otherwise it's done
6207 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02006208 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01006209 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006210 }
6211}
6212
6213/*
6214 * Count a reference to a Function.
6215 */
6216 void
6217func_ref(char_u *name)
6218{
6219 ufunc_T *fp;
6220
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006221 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006222 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006223 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006224 if (fp != NULL)
6225 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006226 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01006227 // Only give an error for a numbered function.
6228 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01006229 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006230}
6231
6232/*
6233 * Count a reference to a Function.
6234 */
6235 void
6236func_ptr_ref(ufunc_T *fp)
6237{
6238 if (fp != NULL)
6239 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006240}
6241
6242/*
6243 * Return TRUE if items in "fc" do not have "copyID". That means they are not
6244 * referenced from anywhere that is in use.
6245 */
6246 static int
6247can_free_funccal(funccall_T *fc, int copyID)
6248{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006249 return (fc->fc_l_varlist.lv_copyID != copyID
6250 && fc->fc_l_vars.dv_copyID != copyID
6251 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006252 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006253}
6254
6255/*
6256 * ":return [expr]"
6257 */
6258 void
6259ex_return(exarg_T *eap)
6260{
6261 char_u *arg = eap->arg;
6262 typval_T rettv;
6263 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006264 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006265
6266 if (current_funccal == NULL)
6267 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006268 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006269 return;
6270 }
6271
Bram Moolenaar844fb642021-10-23 13:32:30 +01006272 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006273 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
6274
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006275 if (eap->skip)
6276 ++emsg_skip;
6277
6278 eap->nextcmd = NULL;
6279 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006280 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006281 {
6282 if (!eap->skip)
6283 returning = do_return(eap, FALSE, TRUE, &rettv);
6284 else
6285 clear_tv(&rettv);
6286 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01006287 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006288 else if (!eap->skip)
6289 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006290 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01006291 update_force_abort();
6292
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006293 /*
6294 * Return unless the expression evaluation has been cancelled due to an
6295 * aborting error, an interrupt, or an exception.
6296 */
6297 if (!aborting())
6298 returning = do_return(eap, FALSE, TRUE, NULL);
6299 }
6300
Bram Moolenaare38eab22019-12-05 21:50:01 +01006301 // When skipping or the return gets pending, advance to the next command
6302 // in this line (!returning). Otherwise, ignore the rest of the line.
6303 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006304 if (returning)
6305 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006306 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02006307 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006308
6309 if (eap->skip)
6310 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02006311 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006312}
6313
zeertzjq5299c092023-04-12 20:48:16 +01006314/*
6315 * Lower level implementation of "call". Only called when not skipping.
6316 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006317 static int
6318ex_call_inner(
6319 exarg_T *eap,
6320 char_u *name,
6321 char_u **arg,
6322 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006323 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006324 evalarg_T *evalarg)
6325{
6326 linenr_T lnum;
6327 int doesrange;
6328 typval_T rettv;
6329 int failed = FALSE;
6330
zeertzjq5299c092023-04-12 20:48:16 +01006331 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006332 for ( ; lnum <= eap->line2; ++lnum)
6333 {
6334 funcexe_T funcexe;
6335
zeertzjq5299c092023-04-12 20:48:16 +01006336 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006337 {
6338 if (lnum > curbuf->b_ml.ml_line_count)
6339 {
6340 // If the function deleted lines or switched to another buffer
6341 // the line number may become invalid.
6342 emsg(_(e_invalid_range));
6343 break;
6344 }
6345 curwin->w_cursor.lnum = lnum;
6346 curwin->w_cursor.col = 0;
6347 curwin->w_cursor.coladd = 0;
6348 }
6349 *arg = startarg;
6350
6351 funcexe = *funcexe_init;
6352 funcexe.fe_doesrange = &doesrange;
6353 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6354 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6355 {
6356 failed = TRUE;
6357 break;
6358 }
6359 if (has_watchexpr())
6360 dbg_check_breakpoint(eap);
6361
6362 // Handle a function returning a Funcref, Dictionary or List.
6363 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006364 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006365 {
6366 failed = TRUE;
6367 break;
6368 }
6369
6370 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006371 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006372 break;
6373
6374 // Stop when immediately aborting on error, or when an interrupt
6375 // occurred or an exception was thrown but not caught.
6376 // get_func_tv() returned OK, so that the check for trailing
6377 // characters below is executed.
6378 if (aborting())
6379 break;
6380 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006381 return failed;
6382}
6383
6384/*
6385 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6386 * Returns FAIL or OK.
6387 */
6388 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006389ex_defer_inner(
6390 char_u *name,
6391 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006392 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006393 partial_T *partial,
6394 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006395{
6396 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006397 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006398 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006399 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006400
6401 if (current_funccal == NULL)
6402 {
6403 semsg(_(e_str_not_inside_function), "defer");
6404 return FAIL;
6405 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006406 if (partial != NULL)
6407 {
6408 if (partial->pt_dict != NULL)
6409 {
6410 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6411 return FAIL;
6412 }
6413 if (partial->pt_argc > 0)
6414 {
6415 int i;
6416
6417 partial_argc = partial->pt_argc;
6418 for (i = 0; i < partial_argc; ++i)
6419 copy_tv(&partial->pt_argv[i], &argvars[i]);
6420 }
6421 }
Ernie Raelb077b582023-12-14 20:11:44 +01006422 int is_builtin = builtin_function(name, -1);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006423 r = get_func_arguments(arg, evalarg, FALSE,
Ernie Raelb077b582023-12-14 20:11:44 +01006424 argvars + partial_argc, &argcount, is_builtin);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006425 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006426
6427 if (r == OK)
6428 {
6429 if (type != NULL)
6430 {
6431 // Check that the arguments are OK for the types of the funcref.
6432 r = check_argument_types(type, argvars, argcount, NULL, name);
6433 }
Ernie Raelb077b582023-12-14 20:11:44 +01006434 else if (is_builtin)
Bram Moolenaar16900322022-09-08 19:51:45 +01006435 {
6436 int idx = find_internal_func(name);
6437
6438 if (idx < 0)
6439 {
6440 emsg_funcname(e_unknown_function_str, name);
6441 r = FAIL;
6442 }
6443 else if (check_internal_func(idx, argcount) == -1)
6444 r = FAIL;
6445 }
6446 else
6447 {
6448 ufunc_T *ufunc = find_func(name, FALSE);
6449
6450 // we tolerate an unknown function here, it might be defined later
6451 if (ufunc != NULL)
6452 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006453 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006454 if (error != FCERR_UNKNOWN)
6455 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006456 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006457 r = FAIL;
6458 }
6459 }
6460 }
6461 }
6462
Bram Moolenaar86d87252022-09-05 21:21:25 +01006463 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006464 {
6465 while (--argcount >= 0)
6466 clear_tv(&argvars[argcount]);
6467 return FAIL;
6468 }
6469 return add_defer(name, argcount, argvars);
6470}
6471
6472/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006473 * Return TRUE if currently inside a function call.
6474 * Give an error message and return FALSE when not.
6475 */
6476 int
6477can_add_defer(void)
6478{
6479 if (!in_def_function() && get_current_funccal() == NULL)
6480 {
6481 semsg(_(e_str_not_inside_function), "defer");
6482 return FALSE;
6483 }
6484 return TRUE;
6485}
6486
6487/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006488 * Add a deferred call for "name" with arguments "argvars[argcount]".
6489 * Consumes "argvars[]".
6490 * Caller must check that in_def_function() returns TRUE or current_funccal is
6491 * not NULL.
6492 * Returns OK or FAIL.
6493 */
6494 int
6495add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6496{
6497 char_u *saved_name = vim_strsave(name);
6498 int argcount = argcount_arg;
6499 defer_T *dr;
6500 int ret = FAIL;
6501
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006502 if (saved_name == NULL)
6503 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006504 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006505 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006506 if (add_defer_function(saved_name, argcount, argvars) == OK)
6507 argcount = 0;
6508 }
6509 else
6510 {
6511 if (current_funccal->fc_defer.ga_itemsize == 0)
6512 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6513 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6514 goto theend;
6515 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6516 + current_funccal->fc_defer.ga_len++;
6517 dr->dr_name = saved_name;
6518 dr->dr_argcount = argcount;
6519 while (argcount > 0)
6520 {
6521 --argcount;
6522 dr->dr_argvars[argcount] = argvars[argcount];
6523 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006524 }
6525 ret = OK;
6526
6527theend:
6528 while (--argcount >= 0)
6529 clear_tv(&argvars[argcount]);
6530 return ret;
6531}
6532
6533/*
6534 * Invoked after a functions has finished: invoke ":defer" functions.
6535 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006536 static void
6537handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006538{
6539 int idx;
6540
Bram Moolenaar58779852022-09-06 18:31:14 +01006541 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006542 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006543 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006544
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006545 if (dr->dr_name == NULL)
6546 // already being called, can happen if function does ":qa"
6547 continue;
6548
6549 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006550 CLEAR_FIELD(funcexe);
6551 funcexe.fe_evaluate = TRUE;
6552
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006553 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006554 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006555
6556 char_u *name = dr->dr_name;
6557 dr->dr_name = NULL;
6558
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006559 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006560 // first statement in the function will be executed (because of the
6561 // exception). So save and restore the try/catch/throw exception
6562 // state.
6563 exception_state_T estate;
6564 exception_state_save(&estate);
6565 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006566
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006567 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6568
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006569 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006570
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006571 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006572 vim_free(name);
6573 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006574 clear_tv(&dr->dr_argvars[i]);
6575 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006576 ga_clear(&funccal->fc_defer);
6577}
6578
zeertzjq960cf912023-04-18 21:52:54 +01006579 static void
6580invoke_funccall_defer(funccall_T *fc)
6581{
6582 if (fc->fc_ectx != NULL)
6583 {
6584 // :def function
6585 unwind_def_callstack(fc->fc_ectx);
6586 may_invoke_defer_funcs(fc->fc_ectx);
6587 }
6588 else
6589 {
6590 // legacy function
6591 handle_defer_one(fc);
6592 }
6593}
6594
Bram Moolenaar58779852022-09-06 18:31:14 +01006595/*
6596 * Called when exiting: call all defer functions.
6597 */
6598 void
6599invoke_all_defer(void)
6600{
zeertzjq1be4b812023-04-19 14:21:24 +01006601 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6602 invoke_funccall_defer(fc);
6603
zeertzjq960cf912023-04-18 21:52:54 +01006604 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6605 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6606 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006607}
6608
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006609/*
6610 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006611 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006612 */
6613 void
6614ex_call(exarg_T *eap)
6615{
6616 char_u *arg = eap->arg;
6617 char_u *startarg;
6618 char_u *name;
6619 char_u *tofree;
6620 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006621 int failed = FALSE;
6622 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006623 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006624 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006625 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006626 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006627 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006628 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006629
Bram Moolenaare6b53242020-07-01 17:28:33 +02006630 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006631 if (eap->skip)
6632 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006633 typval_T rettv;
6634
Bram Moolenaare38eab22019-12-05 21:50:01 +01006635 // trans_function_name() doesn't work well when skipping, use eval0()
6636 // instead to skip to any following command, e.g. for:
6637 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006638 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006639 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006640 clear_tv(&rettv);
6641 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006642 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006643 return;
6644 }
6645
zeertzjq5299c092023-04-12 20:48:16 +01006646 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006647 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006648 if (fudi.fd_newkey != NULL)
6649 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006650 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006651 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006652 vim_free(fudi.fd_newkey);
6653 }
6654 if (tofree == NULL)
6655 return;
6656
Bram Moolenaare38eab22019-12-05 21:50:01 +01006657 // Increase refcount on dictionary, it could get deleted when evaluating
6658 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006659 if (fudi.fd_dict != NULL)
6660 ++fudi.fd_dict->dv_refcount;
6661
Bram Moolenaare38eab22019-12-05 21:50:01 +01006662 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6663 // contents. For VAR_PARTIAL get its partial, unless we already have one
6664 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006665 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006666 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006667 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006668 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006669
Bram Moolenaare38eab22019-12-05 21:50:01 +01006670 // Skip white space to allow ":call func ()". Not good, but required for
6671 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006672 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006673 if (*startarg != '(')
6674 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006675 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006676 goto end;
6677 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006678 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006679 {
6680 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6681 goto end;
6682 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006683
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006684 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006685 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006686 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006687 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006688 }
6689 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006690 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006691 funcexe_T funcexe;
6692
Bram Moolenaara80faa82020-04-12 19:37:17 +02006693 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006694 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006695 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006696 funcexe.fe_partial = partial;
6697 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006698 funcexe.fe_firstline = eap->line1;
6699 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006700 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006701 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006702 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006703 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006704
Bram Moolenaar1d341892021-09-18 15:25:52 +02006705 // When inside :try we need to check for following "| catch" or "| endtry".
6706 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006707 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006708 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006709 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006710 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006711 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006712 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006713 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006714 {
6715 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006716 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006717 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006718 }
6719 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006720 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006721 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006722 // Must be after using "arg", it may point into memory cleared here.
6723 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006724
6725end:
6726 dict_unref(fudi.fd_dict);
6727 vim_free(tofree);
6728}
6729
6730/*
6731 * Return from a function. Possibly makes the return pending. Also called
6732 * for a pending return at the ":endtry" or after returning from an extra
6733 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6734 * when called due to a ":return" command. "rettv" may point to a typval_T
6735 * with the return rettv. Returns TRUE when the return can be carried out,
6736 * FALSE when the return gets pending.
6737 */
6738 int
6739do_return(
6740 exarg_T *eap,
6741 int reanimate,
6742 int is_cmd,
6743 void *rettv)
6744{
6745 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006746 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006747
6748 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006749 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006750 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006751
6752 /*
6753 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6754 * not in its finally clause (which then is to be executed next) is found.
6755 * In this case, make the ":return" pending for execution at the ":endtry".
6756 * Otherwise, return normally.
6757 */
6758 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6759 if (idx >= 0)
6760 {
6761 cstack->cs_pending[idx] = CSTP_RETURN;
6762
6763 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006764 // A pending return again gets pending. "rettv" points to an
6765 // allocated variable with the rettv of the original ":return"'s
6766 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006767 cstack->cs_rettv[idx] = rettv;
6768 else
6769 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006770 // When undoing a return in order to make it pending, get the stored
6771 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006772 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006773 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006774
6775 if (rettv != NULL)
6776 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006777 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006778 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6779 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6780 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006781 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006782 }
6783 else
6784 cstack->cs_rettv[idx] = NULL;
6785
6786 if (reanimate)
6787 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006788 // The pending return value could be overwritten by a ":return"
6789 // without argument in a finally clause; reset the default
6790 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006791 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6792 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006793 }
6794 }
6795 report_make_pending(CSTP_RETURN, rettv);
6796 }
6797 else
6798 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006799 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006800
Bram Moolenaare38eab22019-12-05 21:50:01 +01006801 // If the return is carried out now, store the return value. For
6802 // a return immediately after reanimation, the value is already
6803 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006804 if (!reanimate && rettv != NULL)
6805 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006806 clear_tv(current_funccal->fc_rettv);
6807 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006808 if (!is_cmd)
6809 vim_free(rettv);
6810 }
6811 }
6812
6813 return idx < 0;
6814}
6815
6816/*
6817 * Free the variable with a pending return value.
6818 */
6819 void
6820discard_pending_return(void *rettv)
6821{
6822 free_tv((typval_T *)rettv);
6823}
6824
6825/*
6826 * Generate a return command for producing the value of "rettv". The result
6827 * is an allocated string. Used by report_pending() for verbose messages.
6828 */
6829 char_u *
6830get_return_cmd(void *rettv)
6831{
6832 char_u *s = NULL;
zeertzjq21012302025-02-02 08:55:57 +01006833 char_u *tofree = NULL;
6834 char_u numbuf[NUMBUFLEN];
John Marriottb32800f2025-02-01 15:25:34 +01006835 size_t slen = 0;
6836 size_t IObufflen;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006837
6838 if (rettv != NULL)
6839 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6840 if (s == NULL)
6841 s = (char_u *)"";
John Marriottb32800f2025-02-01 15:25:34 +01006842 else
6843 slen = STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006844
6845 STRCPY(IObuff, ":return ");
6846 STRNCPY(IObuff + 8, s, IOSIZE - 8);
John Marriottb32800f2025-02-01 15:25:34 +01006847 IObufflen = 8 + slen;
zeertzjq21012302025-02-02 08:55:57 +01006848 if (IObufflen >= IOSIZE)
John Marriottb32800f2025-02-01 15:25:34 +01006849 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006850 STRCPY(IObuff + IOSIZE - 4, "...");
zeertzjq21012302025-02-02 08:55:57 +01006851 IObufflen = IOSIZE - 1;
John Marriottb32800f2025-02-01 15:25:34 +01006852 }
zeertzjq21012302025-02-02 08:55:57 +01006853 vim_free(tofree);
John Marriottb32800f2025-02-01 15:25:34 +01006854 return vim_strnsave(IObuff, IObufflen);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006855}
6856
6857/*
6858 * Get next function line.
6859 * Called by do_cmdline() to get the next line.
6860 * Returns allocated string, or NULL for end of function.
6861 */
6862 char_u *
6863get_func_line(
6864 int c UNUSED,
6865 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006866 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006867 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006868{
6869 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006870 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006871 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006872 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006873
Bram Moolenaare38eab22019-12-05 21:50:01 +01006874 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006875 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006876 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006877 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006878 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006879 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006880 }
6881#ifdef FEAT_PROFILE
6882 if (do_profiling == PROF_YES)
6883 func_line_end(cookie);
6884#endif
6885
6886 gap = &fp->uf_lines;
6887 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006888 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006889 retval = NULL;
6890 else
6891 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006892 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006893 while (fcp->fc_linenr < gap->ga_len
6894 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6895 ++fcp->fc_linenr;
6896 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006897 retval = NULL;
6898 else
6899 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006900 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6901 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006902#ifdef FEAT_PROFILE
6903 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006904 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006905#endif
6906 }
6907 }
6908
Bram Moolenaare38eab22019-12-05 21:50:01 +01006909 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006910 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006911 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006912 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006913 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006914 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006915 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006916 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006917 }
6918
6919 return retval;
6920}
6921
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006922/*
6923 * Return TRUE if the currently active function should be ended, because a
6924 * return was encountered or an error occurred. Used inside a ":while".
6925 */
6926 int
6927func_has_ended(void *cookie)
6928{
6929 funccall_T *fcp = (funccall_T *)cookie;
6930
Bram Moolenaare38eab22019-12-05 21:50:01 +01006931 // Ignore the "abort" flag if the abortion behavior has been changed due to
6932 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006933 return (((fcp->fc_func->uf_flags & FC_ABORT)
6934 && did_emsg && !aborted_in_try())
6935 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006936}
6937
6938/*
6939 * return TRUE if cookie indicates a function which "abort"s on errors.
6940 */
6941 int
6942func_has_abort(
6943 void *cookie)
6944{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006945 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006946}
6947
6948
6949/*
6950 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6951 * Don't do this when "Func" is already a partial that was bound
6952 * explicitly (pt_auto is FALSE).
6953 * Changes "rettv" in-place.
6954 * Returns the updated "selfdict_in".
6955 */
6956 dict_T *
6957make_partial(dict_T *selfdict_in, typval_T *rettv)
6958{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006959 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006960 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006961 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006962 dict_T *selfdict = selfdict_in;
6963
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006964 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6965 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006966 fp = rettv->vval.v_partial->pt_func;
6967 else
6968 {
6969 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006970 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006971 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006972 if (fname == NULL)
6973 {
6974 // There is no point binding a dict to a NULL function, just create
6975 // a function reference.
6976 rettv->v_type = VAR_FUNC;
6977 rettv->vval.v_string = NULL;
6978 }
6979 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006980 {
6981 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006982 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006983
6984 // Translate "s:func" to the stored function name.
6985 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6986 fp = find_func(fname, FALSE);
6987 vim_free(tofree);
6988 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006989 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006990
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006991 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006992 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006993 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006994
6995 if (pt != NULL)
6996 {
6997 pt->pt_refcount = 1;
6998 pt->pt_dict = selfdict;
6999 pt->pt_auto = TRUE;
7000 selfdict = NULL;
7001 if (rettv->v_type == VAR_FUNC)
7002 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01007003 // Just a function: Take over the function name and use
7004 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007005 pt->pt_name = rettv->vval.v_string;
7006 }
7007 else
7008 {
7009 partial_T *ret_pt = rettv->vval.v_partial;
7010 int i;
7011
Bram Moolenaare38eab22019-12-05 21:50:01 +01007012 // Partial: copy the function name, use selfdict and copy
7013 // args. Can't take over name or args, the partial might
7014 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007015 if (ret_pt->pt_name != NULL)
7016 {
7017 pt->pt_name = vim_strsave(ret_pt->pt_name);
7018 func_ref(pt->pt_name);
7019 }
7020 else
7021 {
7022 pt->pt_func = ret_pt->pt_func;
7023 func_ptr_ref(pt->pt_func);
7024 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007025 if (ret_pt->pt_argc > 0)
7026 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007027 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007028 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01007029 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007030 pt->pt_argc = 0;
7031 else
7032 {
7033 pt->pt_argc = ret_pt->pt_argc;
7034 for (i = 0; i < pt->pt_argc; i++)
7035 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
7036 }
7037 }
7038 partial_unref(ret_pt);
7039 }
7040 rettv->v_type = VAR_PARTIAL;
7041 rettv->vval.v_partial = pt;
7042 }
7043 }
7044 return selfdict;
7045}
7046
7047/*
7048 * Return the name of the executed function.
7049 */
7050 char_u *
7051func_name(void *cookie)
7052{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007053 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007054}
7055
7056/*
7057 * Return the address holding the next breakpoint line for a funccall cookie.
7058 */
7059 linenr_T *
7060func_breakpoint(void *cookie)
7061{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007062 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007063}
7064
7065/*
7066 * Return the address holding the debug tick for a funccall cookie.
7067 */
7068 int *
7069func_dbg_tick(void *cookie)
7070{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007071 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007072}
7073
7074/*
7075 * Return the nesting level for a funccall cookie.
7076 */
7077 int
7078func_level(void *cookie)
7079{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007080 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007081}
7082
7083/*
7084 * Return TRUE when a function was ended by a ":return" command.
7085 */
7086 int
7087current_func_returned(void)
7088{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007089 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007090}
7091
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007092 int
7093free_unref_funccal(int copyID, int testing)
7094{
7095 int did_free = FALSE;
7096 int did_free_funccal = FALSE;
7097 funccall_T *fc, **pfc;
7098
7099 for (pfc = &previous_funccal; *pfc != NULL; )
7100 {
7101 if (can_free_funccal(*pfc, copyID))
7102 {
7103 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007104 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01007105 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007106 did_free = TRUE;
7107 did_free_funccal = TRUE;
7108 }
7109 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01007110 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007111 }
7112 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01007113 // When a funccal was freed some more items might be garbage
7114 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007115 (void)garbage_collect(testing);
7116
7117 return did_free;
7118}
7119
7120/*
Bram Moolenaarba209902016-08-24 22:06:38 +02007121 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007122 */
7123 static funccall_T *
7124get_funccal(void)
7125{
7126 int i;
7127 funccall_T *funccal;
7128 funccall_T *temp_funccal;
7129
7130 funccal = current_funccal;
7131 if (debug_backtrace_level > 0)
7132 {
7133 for (i = 0; i < debug_backtrace_level; i++)
7134 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007135 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007136 if (temp_funccal)
7137 funccal = temp_funccal;
7138 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01007139 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007140 debug_backtrace_level = i;
7141 }
7142 }
7143 return funccal;
7144}
7145
7146/*
7147 * Return the hashtable used for local variables in the current funccal.
7148 * Return NULL if there is no current funccal.
7149 */
7150 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007151get_funccal_local_ht(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.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007156}
7157
7158/*
7159 * Return the l: scope variable.
7160 * Return NULL if there is no current funccal.
7161 */
7162 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007163get_funccal_local_var(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_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007168}
7169
7170/*
7171 * Return the hashtable used for argument in the current funccal.
7172 * Return NULL if there is no current funccal.
7173 */
7174 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007175get_funccal_args_ht(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.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007180}
7181
7182/*
7183 * Return the a: scope variable.
7184 * Return NULL if there is no current funccal.
7185 */
7186 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007187get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007188{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007189 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007190 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007191 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007192}
7193
7194/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007195 * List function variables, if there is a function.
7196 */
7197 void
7198list_func_vars(int *first)
7199{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007200 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
7201 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01007202 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007203}
7204
7205/*
7206 * If "ht" is the hashtable for local variables in the current funccal, return
7207 * the dict that contains it.
7208 * Otherwise return NULL.
7209 */
7210 dict_T *
7211get_current_funccal_dict(hashtab_T *ht)
7212{
7213 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01007214 && ht == &current_funccal->fc_l_vars.dv_hashtab)
7215 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007216 return NULL;
7217}
7218
7219/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007220 * Search hashitem in parent scope.
7221 */
7222 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007223find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007224{
7225 funccall_T *old_current_funccal = current_funccal;
7226 hashtab_T *ht;
7227 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007228 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007229
Bram Moolenaarca16c602022-09-06 18:57:08 +01007230 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007231 return NULL;
7232
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02007233 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01007234 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02007235 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007236 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007237 ht = find_var_ht(name, &varname);
7238 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02007239 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007240 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02007241 if (!HASHITEM_EMPTY(hi))
7242 {
7243 *pht = ht;
7244 break;
7245 }
7246 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007247 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02007248 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007249 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007250 }
7251 current_funccal = old_current_funccal;
7252
7253 return hi;
7254}
7255
7256/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007257 * Search variable in parent scope.
7258 */
7259 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007260find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007261{
7262 dictitem_T *v = NULL;
7263 funccall_T *old_current_funccal = current_funccal;
7264 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007265 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007266
Bram Moolenaarca16c602022-09-06 18:57:08 +01007267 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007268 return NULL;
7269
Bram Moolenaare38eab22019-12-05 21:50:01 +01007270 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01007271 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007272 while (current_funccal)
7273 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007274 ht = find_var_ht(name, &varname);
7275 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007276 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007277 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007278 if (v != NULL)
7279 break;
7280 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007281 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007282 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007283 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007284 }
7285 current_funccal = old_current_funccal;
7286
7287 return v;
7288}
7289
7290/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007291 * Set "copyID + 1" in previous_funccal and callers.
7292 */
7293 int
7294set_ref_in_previous_funccal(int copyID)
7295{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007296 funccall_T *fc;
7297
Bram Moolenaarca16c602022-09-06 18:57:08 +01007298 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007299 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007300 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007301 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
7302 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
7303 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007304 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007305 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007306 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007307}
7308
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007309 static int
7310set_ref_in_funccal(funccall_T *fc, int copyID)
7311{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007312 if (fc->fc_copyID != copyID)
7313 {
7314 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007315 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
7316 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
7317 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
7318 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007319 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007320 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007321 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007322}
7323
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007324/*
7325 * Set "copyID" in all local vars and arguments in the call stack.
7326 */
7327 int
7328set_ref_in_call_stack(int copyID)
7329{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007330 funccall_T *fc;
7331 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007332
Bram Moolenaarca16c602022-09-06 18:57:08 +01007333 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007334 if (set_ref_in_funccal(fc, copyID))
7335 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007336
7337 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007338 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007339 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007340 if (set_ref_in_funccal(fc, copyID))
7341 return TRUE;
7342 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007343}
7344
7345/*
7346 * Set "copyID" in all functions available by name.
7347 */
7348 int
7349set_ref_in_functions(int copyID)
7350{
7351 int todo;
7352 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007353 ufunc_T *fp;
7354
7355 todo = (int)func_hashtab.ht_used;
7356 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007357 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007358 if (!HASHITEM_EMPTY(hi))
7359 {
7360 --todo;
7361 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007362 if (!func_name_refcount(fp->uf_name)
7363 && set_ref_in_func(NULL, fp, copyID))
7364 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007365 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007366 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007367 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007368}
7369
7370/*
7371 * Set "copyID" in all function arguments.
7372 */
7373 int
7374set_ref_in_func_args(int copyID)
7375{
7376 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007377
7378 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007379 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7380 copyID, NULL, NULL))
7381 return TRUE;
7382 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007383}
7384
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007385/*
7386 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007387 * Returns TRUE if setting references failed somehow.
7388 */
7389 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007390set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007391{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007392 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007393 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007394 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007395 char_u fname_buf[FLEN_FIXED + 1];
7396 char_u *tofree = NULL;
7397 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007398 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007399
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007400 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007401 return FALSE;
7402
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007403 if (fp_in == NULL)
7404 {
7405 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007406 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007407 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007408 if (fp != NULL)
7409 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007410 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007411 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007412 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007413
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007414 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007415 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007416}
7417
Bram Moolenaare38eab22019-12-05 21:50:01 +01007418#endif // FEAT_EVAL