blob: b9093a0d295b7bfcb62088cb1a66bdbaaac7d5d5 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +020011 * userfunc.c: User defined function support
Bram Moolenaara9b579f2016-07-17 18:29:19 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020017/*
18 * All user-defined functions are found in this hashtable.
19 */
20static hashtab_T func_hashtab;
21
Bram Moolenaare38eab22019-12-05 21:50:01 +010022// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020023static garray_T funcargs = GA_EMPTY;
24
Bram Moolenaar209b8e32019-03-14 13:43:24 +010025// pointer to funccal for currently active function
26static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020027
Bram Moolenaar209b8e32019-03-14 13:43:24 +010028// Pointer to list of previously used funccal, still around because some
29// item in it is still being used.
30static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020031
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020032static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +010033static void func_clear(ufunc_T *fp, int force);
34static int func_free(ufunc_T *fp, int force);
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +010035static char_u *untrans_function_name(char_u *name);
Bram Moolenaar58779852022-09-06 18:31:14 +010036static void handle_defer_one(funccall_T *funccal);
Yegappan Lakshmanana04003a2024-10-27 21:54:11 +010037static char_u *trans_function_name_ext(char_u **pp, int *is_global, int skip, int flags, funcdict_T *fdp, partial_T **partial, type_T **type, ufunc_T **ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020038
39 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +000040func_init(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020041{
42 hash_init(&func_hashtab);
43}
44
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020045/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020046 * Return the function hash table
47 */
48 hashtab_T *
49func_tbl_get(void)
50{
51 return &func_hashtab;
52}
53
54/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020055 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020056 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010057 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010058 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaardce24412022-02-08 20:35:30 +000059 * If "eap" is not NULL use it to check for an already declared name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010060 * Return a pointer to after the type.
61 * When something is wrong return "arg".
62 */
63 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010064one_function_arg(
65 char_u *arg,
66 garray_T *newargs,
67 garray_T *argtypes,
68 int types_optional,
h-eastb895b0f2023-09-24 15:46:31 +020069 garray_T *arg_objm,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010070 evalarg_T *evalarg,
Bram Moolenaardce24412022-02-08 20:35:30 +000071 exarg_T *eap,
Bram Moolenaar2a389082021-04-09 20:24:31 +020072 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010073 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074{
Bram Moolenaar6e949782020-04-13 17:21:00 +020075 char_u *p = arg;
76 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020077 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010078
79 while (ASCII_ISALNUM(*p) || *p == '_')
80 ++p;
Keith Thompson184f71c2024-01-04 21:19:04 +010081 if (arg == p || SAFE_isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020082 || (argtypes == NULL
83 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
84 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085 {
86 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000087 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 return arg;
89 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010090
Bram Moolenaarce723f32023-06-10 19:00:12 +010091 // Extra checks in Vim9 script.
92 if (!skip && argtypes != NULL)
93 {
94 int c = *p;
95 *p = NUL;
96 int r = check_reserved_name(arg, FALSE);
97 *p = c;
98 if (r == FAIL)
99 return arg;
100
101 // Cannot use script var name for argument. In function: also check
102 // local vars and arguments.
103 if (check_defined(arg, p - arg,
104 evalarg == NULL ? NULL : evalarg->eval_cctx,
Bram Moolenaardce24412022-02-08 20:35:30 +0000105 eap == NULL ? NULL : eap->cstack, TRUE) == FAIL)
Bram Moolenaarce723f32023-06-10 19:00:12 +0100106 return arg;
107 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100108
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100109 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
110 return arg;
111 if (newargs != NULL)
112 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113 int c;
114 int i;
115
116 c = *p;
117 *p = NUL;
118 arg_copy = vim_strsave(arg);
119 if (arg_copy == NULL)
120 {
121 *p = c;
122 return arg;
123 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200124 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200125 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200126 // Check for duplicate argument name.
127 for (i = 0; i < newargs->ga_len; ++i)
128 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
129 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000130 semsg(_(e_duplicate_argument_name_str), arg_copy);
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200131 vim_free(arg_copy);
132 return arg;
133 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100134 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
135 newargs->ga_len++;
136
137 *p = c;
138 }
139
140 // get any type from "arg: type"
h-eastb895b0f2023-09-24 15:46:31 +0200141 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK)
142 && arg_objm != NULL && (skip || ga_grow(arg_objm, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143 {
144 char_u *type = NULL;
145
Bram Moolenaar6e949782020-04-13 17:21:00 +0200146 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
147 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200148 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200149 arg_copy == NULL ? arg : arg_copy);
150 p = skipwhite(p);
151 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100152 if (*p == ':')
153 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200154 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100155 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200156 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100157 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200158 return arg;
159 }
160 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200161 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100162 if (!skip)
163 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200165 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200166 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200167 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200168 arg_copy == NULL ? arg : arg_copy);
169 return arg;
170 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100171 if (!skip)
172 {
173 if (type == NULL && types_optional)
John Marriottb32800f2025-02-01 15:25:34 +0100174 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100175 // lambda arguments default to "any" type
John Marriottb32800f2025-02-01 15:25:34 +0100176 if (is_vararg)
177 type = vim_strnsave((char_u *)"list<any>", 9);
178 else
179 type = vim_strnsave((char_u *)"any", 3);
180 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100181 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
h-eastb895b0f2023-09-24 15:46:31 +0200182 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = FALSE;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100183 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100184 }
185
186 return p;
187}
188
189/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000190 * Handle line continuation in function arguments or body.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000191 * Get a next line, store it in "eap" if appropriate and put the line in
192 * "lines_to_free" to free the line later.
Bram Moolenaar7473a842021-12-28 17:55:26 +0000193 */
194 static char_u *
195get_function_line(
196 exarg_T *eap,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000197 garray_T *lines_to_free,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000198 int indent,
199 getline_opt_T getline_options)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000200{
201 char_u *theline;
202
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100203 if (eap->ea_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +0000204 theline = getcmdline(':', 0L, indent, 0);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000205 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100206 theline = eap->ea_getline(':', eap->cookie, indent, getline_options);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000207 if (theline != NULL)
208 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000209 if (lines_to_free->ga_len > 0
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000210 && eap->cmdlinep != NULL
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000211 && *eap->cmdlinep == ((char_u **)lines_to_free->ga_data)
212 [lines_to_free->ga_len - 1])
Bram Moolenaar7473a842021-12-28 17:55:26 +0000213 *eap->cmdlinep = theline;
Bram Moolenaarb5820102023-01-25 12:27:13 +0000214 (void)ga_add_string(lines_to_free, theline);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000215 }
216
217 return theline;
218}
219
220/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200221 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100222 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100223 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200224 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100225 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200226get_function_args(
227 char_u **argp,
228 char_u endchar,
229 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100230 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100231 int types_optional, // types optional if "argtypes" is not NULL
h-eastb895b0f2023-09-24 15:46:31 +0200232 garray_T *arg_objm, // NULL unless using :def
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100233 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200234 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200235 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200236 int skip,
Bram Moolenaardce24412022-02-08 20:35:30 +0000237 exarg_T *eap, // can be NULL
Bram Moolenaar554d0312023-01-05 19:59:18 +0000238 int in_class, // non-zero when inside a class or interface
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000239 garray_T *newlines, // function body lines
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000240 garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200241{
242 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100243 char_u *arg;
244 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200245 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200246 int any_default = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100247 char_u *whitep = *argp;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100248 int need_expr = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200249
250 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000251 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000253 ga_init2(argtypes, sizeof(char_u *), 3);
h-eastb895b0f2023-09-24 15:46:31 +0200254 if (arg_objm != NULL)
255 ga_init2(arg_objm, sizeof(int8_T), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200256 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000257 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200258
259 if (varargs != NULL)
260 *varargs = FALSE;
261
262 /*
263 * Isolate the arguments: "arg1, arg2, ...)"
264 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100265 arg = skipwhite(*argp);
266 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200267 while (*p != endchar)
268 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100269 while (eap != NULL && eap->ea_getline != NULL
Bram Moolenaar2c330432020-04-13 14:41:35 +0200270 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200271 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200272 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000273 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000274 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000275
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200276 if (theline == NULL)
277 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200278 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200279 p = skipwhite(theline);
280 }
281
282 if (mustend && *p != endchar)
283 {
284 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000285 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100286 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200287 }
Christian Brabandte4a450a2023-12-08 20:57:38 +0100288 if (*p == endchar && !need_expr)
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200289 break;
290
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200291 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
292 {
293 if (varargs != NULL)
294 *varargs = TRUE;
295 p += 3;
296 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297
298 if (argtypes != NULL)
299 {
300 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200301 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100302 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100303 if (!skip)
304 emsg(_(e_missing_name_after_dots));
305 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100306 }
307
308 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100309 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200310 arg_objm, evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100311 if (p == arg)
312 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100313 if (*skipwhite(p) == '=')
314 {
315 emsg(_(e_cannot_use_default_for_variable_arguments));
316 break;
317 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100318 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200319 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000320 else if (in_class && STRNCMP(p, "this.", 5) == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000321 {
322 // this.memberName
323 p += 5;
324 arg = p;
325 while (ASCII_ISALNUM(*p) || *p == '_')
326 ++p;
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000327 char_u *argend = p;
328
h-eastdb385522023-09-28 22:18:19 +0200329 // object variable this. can be used only in a constructor
330 if (STRNCMP(eap->arg, "new", 3) != 0)
331 {
332 c = *argend;
333 *argend = NUL;
334 semsg(_(e_cannot_use_an_object_variable_except_with_the_new_method_str), arg);
335 *argend = c;
336 break;
337 }
338
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000339 if (*skipwhite(p) == '=')
340 {
341 char_u *defval = skipwhite(skipwhite(p) + 1);
342 if (STRNCMP(defval, "v:none", 6) != 0)
343 {
344 semsg(_(e_constructor_default_value_must_be_vnone_str), p);
345 goto err_ret;
346 }
347 any_default = TRUE;
348 p = defval + 6;
349
350 if (ga_grow(default_args, 1) == FAIL)
351 goto err_ret;
352
John Marriottb32800f2025-02-01 15:25:34 +0100353 char_u *expr = vim_strnsave((char_u *)"v:none", 6);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000354 if (expr == NULL)
355 goto err_ret;
356 ((char_u **)(default_args->ga_data))
357 [default_args->ga_len] = expr;
358 default_args->ga_len++;
359 }
360 else if (any_default)
361 {
362 emsg(_(e_non_default_argument_follows_default_argument));
363 goto err_ret;
364 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000365
366 // TODO: check the argument is indeed a member
367 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
368 return FAIL;
369 if (newargs != NULL)
370 {
371 ((char_u **)(newargs->ga_data))[newargs->ga_len] =
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000372 vim_strnsave(arg, argend - arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000373 newargs->ga_len++;
374
h-eastb895b0f2023-09-24 15:46:31 +0200375 if (argtypes != NULL && ga_grow(argtypes, 1) == OK
376 && arg_objm != NULL && ga_grow(arg_objm, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000377 {
378 // TODO: use the actual type
379 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] =
John Marriottb32800f2025-02-01 15:25:34 +0100380 vim_strnsave((char_u *)"any", 3);
h-eastb895b0f2023-09-24 15:46:31 +0200381 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000382
383 // Add a line to the function body for the assignment.
384 if (ga_grow(newlines, 1) == OK)
385 {
386 // "this.name = name"
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000387 int len = 5 + (argend - arg) + 3 + (argend - arg) + 1;
388 if (any_default)
389 len += 14 + 10;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000390 char_u *assignment = alloc(len);
391 if (assignment != NULL)
392 {
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000393 c = *argend;
394 *argend = NUL;
395 if (any_default)
396 vim_snprintf((char *)assignment, len,
397 "ifargisset %d this.%s = %s",
398 default_args->ga_len - 1, arg, arg);
399 else
400 vim_snprintf((char *)assignment, len,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000401 "this.%s = %s", arg, arg);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000402 *argend = c;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000403 ((char_u **)(newlines->ga_data))[
404 newlines->ga_len++] = assignment;
405 }
406 }
407 }
408 }
409 if (*p == ',')
410 ++p;
411 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200412 else
413 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200414 char_u *np;
415
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200416 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100417 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200418 arg_objm, evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100419 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200420 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200421
Bram Moolenaar015cf102021-06-26 21:52:02 +0200422 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200423 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200424 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200425 if (*np == '=' && np[1] != '=' && np[1] != '~'
426 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200427 {
428 typval_T rettv;
429
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100430 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200431 any_default = TRUE;
Bram Moolenaareb5adf12022-09-04 13:41:37 +0100432 p = skipwhite(np + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000433 char_u *expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200434 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200435 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200436 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200437 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200438 if (ga_grow(default_args, 1) == FAIL)
439 goto err_ret;
440
Christian Brabandte4a450a2023-12-08 20:57:38 +0100441 if (need_expr)
442 need_expr = FALSE;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200443 // trim trailing whitespace
444 while (p > expr && VIM_ISWHITE(p[-1]))
445 p--;
446 c = *p;
447 *p = NUL;
448 expr = vim_strsave(expr);
449 if (expr == NULL)
450 {
451 *p = c;
452 goto err_ret;
453 }
454 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200455 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200456 default_args->ga_len++;
457 *p = c;
458 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200459 }
460 else
Christian Brabandte4a450a2023-12-08 20:57:38 +0100461 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200462 mustend = TRUE;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100463 if (*skipwhite(p) == NUL)
464 need_expr = TRUE;
465 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200466 }
467 else if (any_default)
468 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000469 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200470 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200471 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200472
473 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
474 {
475 // Be tolerant when skipping
476 if (!skip)
477 {
478 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
479 goto err_ret;
480 }
481 p = skipwhite(p);
482 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200483 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200484 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200485 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200486 // Don't give this error when skipping, it makes the "->" not
487 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100488 // Allow missing space after comma in legacy functions.
489 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200490 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
491 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100492 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200493 goto err_ret;
494 }
495 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200496 else
497 mustend = TRUE;
498 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200499 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200500 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200501 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200502
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200503 if (*p != endchar)
504 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100505 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200506
507 *argp = p;
508 return OK;
509
510err_ret:
511 if (newargs != NULL)
512 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200513 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200514 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200515 return FAIL;
516}
517
518/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100519 * Parse the argument types, filling "fp->uf_arg_types".
520 * Return OK or FAIL.
521 */
522 static int
h-eastb895b0f2023-09-24 15:46:31 +0200523parse_argument_types(
524 ufunc_T *fp,
525 garray_T *argtypes,
526 int varargs,
527 garray_T *arg_objm,
528 ocmember_T *obj_members,
529 int obj_member_count)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100530{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200531 int len = 0;
532
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100533 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
534 if (argtypes->ga_len > 0)
535 {
536 // When "varargs" is set the last name/type goes into uf_va_name
537 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200538 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100539
540 if (len > 0)
541 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
542 if (fp->uf_arg_types != NULL)
543 {
544 int i;
545 type_T *type;
546
547 for (i = 0; i < len; ++ i)
548 {
549 char_u *p = ((char_u **)argtypes->ga_data)[i];
550
551 if (p == NULL)
552 // will get the type from the default value
553 type = &t_unknown;
554 else
h-eastb895b0f2023-09-24 15:46:31 +0200555 {
556 if (arg_objm != NULL && ((int8_T *)arg_objm->ga_data)[i])
557 {
558 char_u *aname = ((char_u **)fp->uf_args.ga_data)[i];
559
560 type = &t_any;
561 for (int om = 0; om < obj_member_count; ++om)
562 {
Christian Brabandt915f3bf2024-04-05 20:12:19 +0200563 if (obj_members != NULL
564 && STRCMP(aname,
565 obj_members[om].ocm_name) == 0)
h-eastb895b0f2023-09-24 15:46:31 +0200566 {
567 type = obj_members[om].ocm_type;
568 break;
569 }
570 }
571 }
572 else
573 type = parse_type(&p, &fp->uf_type_list, TRUE);
574 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100575 if (type == NULL)
576 return FAIL;
577 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000578 if (i < fp->uf_args.ga_len
579 && (type->tt_type == VAR_FUNC
Yegappan Lakshmanan3e336502024-04-04 19:35:59 +0200580 || type->tt_type == VAR_PARTIAL))
581 {
582 char_u *name = ((char_u **)fp->uf_args.ga_data)[i];
583 if (obj_members != NULL && *name == '_')
584 // protected object method
585 name++;
586
587 if (var_wrong_func_name(name, TRUE))
588 return FAIL;
589 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100590 }
591 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100592 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200593
594 if (varargs)
595 {
596 char_u *p;
597
598 // Move the last argument "...name: type" to uf_va_name and
599 // uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200600 --fp->uf_args.ga_len;
Bram Moolenaarf0571712023-01-04 13:16:20 +0000601 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len];
602 ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len] = NULL;
Bram Moolenaar2a389082021-04-09 20:24:31 +0200603 p = ((char_u **)argtypes->ga_data)[len];
604 if (p == NULL)
605 // TODO: get type from default value
606 fp->uf_va_type = &t_list_any;
607 else
608 {
609 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
610 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
611 {
612 semsg(_(e_variable_arguments_type_must_be_list_str),
613 ((char_u **)argtypes->ga_data)[len]);
614 return FAIL;
615 }
616 }
617 if (fp->uf_va_type == NULL)
618 return FAIL;
619 }
620
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100621 return OK;
622}
623
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100624 static int
625parse_return_type(ufunc_T *fp, char_u *ret_type)
626{
627 if (ret_type == NULL)
628 fp->uf_ret_type = &t_void;
629 else
630 {
631 char_u *p = ret_type;
632
633 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
634 if (fp->uf_ret_type == NULL)
635 {
636 fp->uf_ret_type = &t_void;
637 return FAIL;
638 }
639 }
640 return OK;
641}
642
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100643/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200644 * Register function "fp" as using "current_funccal" as its scope.
645 */
646 static int
647register_closure(ufunc_T *fp)
648{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200649 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100650 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200651 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200652 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200653 fp->uf_scoped = current_funccal;
654 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200655
Bram Moolenaarca16c602022-09-06 18:57:08 +0100656 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200657 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100658 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
659 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200660 return OK;
661}
662
663/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100664 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
665 * return "buf" filled with a readable function name.
666 * Otherwise just return "name", thus the return value can always be used.
667 * "name" and "buf" may be equal.
668 */
669 char_u *
670make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
671{
672 size_t len;
673
674 if (name[0] != K_SPECIAL)
675 return name;
676 len = STRLEN(name);
677 if (len + 3 > bufsize)
678 return name;
679
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100680 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100681 mch_memmove(buf, "<SNR>", 5);
682 return buf;
683}
684
John Marriottb32800f2025-02-01 15:25:34 +0100685static char_u lambda_name[8 + NUMBUFLEN];
John Marriottb32800f2025-02-01 15:25:34 +0100686
zeertzjq21012302025-02-02 08:55:57 +0100687/*
688 * Get a name for a lambda. Returned in static memory.
689 */
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +0100690 string_T
Bram Moolenaar04b12692020-05-04 23:24:44 +0200691get_lambda_name(void)
692{
John Marriottb32800f2025-02-01 15:25:34 +0100693 static int lambda_no = 0;
694 int n;
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +0100695 string_T ret;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200696
John Marriottb32800f2025-02-01 15:25:34 +0100697 n = vim_snprintf((char *)lambda_name, sizeof(lambda_name), "<lambda>%d", ++lambda_no);
698 if (n < 1)
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +0100699 ret.length = 0;
John Marriottb32800f2025-02-01 15:25:34 +0100700 else
701 if (n >= (int)sizeof(lambda_name))
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +0100702 ret.length = sizeof(lambda_name) - 1;
John Marriottb32800f2025-02-01 15:25:34 +0100703 else
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +0100704 ret.length = (size_t)n;
John Marriottb32800f2025-02-01 15:25:34 +0100705
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +0100706 ret.string = lambda_name;
707 return ret;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200708}
709
Bram Moolenaare01e5212023-01-08 20:31:18 +0000710/*
711 * Allocate a "ufunc_T" for a function called "name".
712 * Makes sure the size is right.
713 */
714 static ufunc_T *
John Marriottb32800f2025-02-01 15:25:34 +0100715alloc_ufunc(char_u *name, size_t namelen)
Bram Moolenaare01e5212023-01-08 20:31:18 +0000716{
John Marriottb32800f2025-02-01 15:25:34 +0100717 size_t len;
718 ufunc_T *fp;
719
Bram Moolenaare01e5212023-01-08 20:31:18 +0000720 // When the name is short we need to make sure we allocate enough bytes for
721 // the whole struct, including any padding.
John Marriottb32800f2025-02-01 15:25:34 +0100722 len = offsetof(ufunc_T, uf_name) + namelen + 1;
723 fp = alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
724 if (fp != NULL)
725 {
726 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
727 // can actually extend beyond the struct.
728 STRCPY((void *)fp->uf_name, name);
729 fp->uf_namelen = namelen;
730
731 if (name[0] == K_SPECIAL)
732 {
733 len = namelen + 3; // including +1 for NUL
734 fp->uf_name_exp = alloc(len);
735 if (fp->uf_name_exp != NULL)
736 vim_snprintf((char *)fp->uf_name_exp, len, "<SNR>%s", fp->uf_name + 3);
737 }
738 }
739
740 return fp;
Bram Moolenaare01e5212023-01-08 20:31:18 +0000741}
742
Bram Moolenaar801ab062020-06-25 19:27:56 +0200743#if defined(FEAT_LUA) || defined(PROTO)
744/*
745 * Registers a native C callback which can be called from Vim script.
746 * Returns the name of the Vim script function.
747 */
748 char_u *
749register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
750{
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +0100751 string_T name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200752 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200753
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +0100754 fp = alloc_ufunc(name.string, name.length);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200755 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200756 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200757
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200758 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200759 fp->uf_refcount = 1;
760 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000761 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200762 fp->uf_calls = 0;
763 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200764 fp->uf_cb = cb;
765 fp->uf_cb_free = cb_free;
766 fp->uf_cb_state = state;
767
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000768 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200769
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +0100770 return name.string;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200771}
772#endif
773
Bram Moolenaar04b12692020-05-04 23:24:44 +0200774/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100775 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100776 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100777 * If "white_error" is not NULL check for correct use of white space and set
778 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100779 * Return NULL if no valid arrow found.
780 */
781 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100782skip_arrow(
783 char_u *start,
784 int equal_arrow,
785 char_u **ret_type,
786 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100787{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100788 char_u *s = start;
789 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100790
791 if (equal_arrow)
792 {
793 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100794 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100795 if (white_error != NULL && !VIM_ISWHITE(s[1]))
796 {
797 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100798 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100799 return NULL;
800 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100801 s = skipwhite(s + 1);
802 *ret_type = s;
803 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100804 if (s == *ret_type)
805 {
806 emsg(_(e_missing_return_type));
807 return NULL;
808 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100809 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100810 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100811 s = skipwhite(s);
812 if (*s != '=')
813 return NULL;
814 ++s;
815 }
816 if (*s != '>')
817 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100818 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
819 || !IS_WHITE_OR_NUL(s[1])))
820 {
821 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100822 semsg(_(e_white_space_required_before_and_after_str_at_str),
823 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100824 return NULL;
825 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100826 return skipwhite(s + 1);
827}
828
829/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100830 * Check if "*cmd" points to a function command and if so advance "*cmd" and
831 * return TRUE.
832 * Otherwise return FALSE;
833 * Do not consider "function(" to be a command.
834 */
835 static int
836is_function_cmd(char_u **cmd)
837{
838 char_u *p = *cmd;
839
840 if (checkforcmd(&p, "function", 2))
841 {
842 if (*p == '(')
843 return FALSE;
844 *cmd = p;
845 return TRUE;
846 }
847 return FALSE;
848}
849
850/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200851 * Called when defining a function: The context may be needed for script
852 * variables declared in a block that is visible now but not when the function
853 * is compiled or called later.
854 */
855 static void
856function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
857{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000858 if (cstack == NULL || cstack->cs_idx < 0)
859 return;
860
861 int count = cstack->cs_idx + 1;
862 int i;
863
864 fp->uf_block_ids = ALLOC_MULT(int, count);
865 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200866 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000867 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
868 sizeof(int) * count);
869 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200870 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000871
872 // Set flag in each block to indicate a function was defined. This
873 // is used to keep the variable when leaving the block, see
874 // hide_script_var().
875 for (i = 0; i <= cstack->cs_idx; ++i)
876 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200877}
878
879/*
Yegappan Lakshmanan0072cee2025-01-07 20:22:32 +0100880 * Skip over all the characters in a single quoted string starting at "p" and
881 * return a pointer to the character following the ending single quote.
882 * If the ending single quote is missing, then return a pointer to the NUL
883 * character.
884 */
885 static char_u *
886skip_single_quote_string(char_u *p)
887{
888 p++; // skip the beginning single quote
889 while (*p != NUL)
890 {
891 // Within the string, a single quote can be escaped by using
892 // two single quotes.
893 if (*p == '\'' && *(p + 1) == '\'')
894 p += 2;
895 else if (*p == '\'')
896 {
897 p++; // skip the ending single quote
898 break;
899 }
900 else
901 MB_PTR_ADV(p);
902 }
903
904 return p;
905}
906
907/*
908 * Skip over all the characters in a double quoted string starting at "p" and
909 * return a pointer to the character following the ending double quote.
910 * If the ending double quote is missing, then return a pointer to the NUL
911 * character.
912 */
913 static char_u *
914skip_double_quote_string(char_u *p)
915{
916 p++; // skip the beginning double quote
917 while (*p != NUL)
918 {
919 // Within the string, a double quote can be escaped by
920 // preceding it with a backslash.
921 if (*p == '\\' && *(p + 1) == '"')
922 p += 2;
923 else if (*p == '"')
924 {
925 p++; // skip the ending double quote
926 break;
927 }
928 else
929 MB_PTR_ADV(p);
930 }
931
932 return p;
933}
934
935/*
936 * Return the start of a Vim9 comment (#) in the line starting at "line".
937 * If a comment is not found, then returns a pointer to the end of the
938 * string (NUL).
939 */
940 static char_u *
941find_start_of_vim9_comment(char_u *line)
942{
943 char_u *p = line;
944
945 while (*p != NUL)
946 {
947 if (*p == '\'')
948 // Skip a single quoted string.
949 p = skip_single_quote_string(p);
950 else if (*p == '"')
951 // Skip a double quoted string.
952 p = skip_double_quote_string(p);
953 else
954 {
955 if (*p == '#')
956 // Found the start of a Vim9 comment
957 break;
958 MB_PTR_ADV(p);
959 }
960 }
961
962 return p;
963}
964
965/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100966 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200967 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100968 * "newlines" must already have been initialized.
969 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
970 */
971 static int
972get_function_body(
973 exarg_T *eap,
974 garray_T *newlines,
975 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000976 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100977{
978 linenr_T sourcing_lnum_top = SOURCING_LNUM;
979 linenr_T sourcing_lnum_off;
980 int saved_wait_return = need_wait_return;
981 char_u *line_arg = line_arg_in;
982 int vim9_function = eap->cmdidx == CMD_def
983 || eap->cmdidx == CMD_block;
984#define MAX_FUNC_NESTING 50
985 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200986 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100987 int nesting = 0;
988 getline_opt_T getline_options;
989 int indent = 2;
990 char_u *skip_until = NULL;
991 int ret = FAIL;
992 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200993 int heredoc_concat_len = 0;
994 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100995 char_u *heredoc_trimmed = NULL;
John Marriottb32800f2025-02-01 15:25:34 +0100996 size_t heredoc_trimmedlen = 0;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100997
Bram Moolenaar20677332021-06-06 17:02:53 +0200998 ga_init2(&heredoc_ga, 1, 500);
999
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001000 // Detect having skipped over comment lines to find the return
1001 // type. Add NULL lines to keep the line count correct.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001002 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001003 if (SOURCING_LNUM < sourcing_lnum_off)
1004 {
1005 sourcing_lnum_off -= SOURCING_LNUM;
1006 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
1007 goto theend;
1008 while (sourcing_lnum_off-- > 0)
1009 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1010 }
1011
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001012 nesting_def[0] = vim9_function;
1013 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001014 getline_options = vim9_function
1015 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
1016 for (;;)
1017 {
1018 char_u *theline;
1019 char_u *p;
1020 char_u *arg;
1021
1022 if (KeyTyped)
1023 {
1024 msg_scroll = TRUE;
1025 saved_wait_return = FALSE;
1026 }
1027 need_wait_return = FALSE;
1028
1029 if (line_arg != NULL)
1030 {
1031 // Use eap->arg, split up in parts by line breaks.
1032 theline = line_arg;
1033 p = vim_strchr(theline, '\n');
1034 if (p == NULL)
1035 line_arg += STRLEN(line_arg);
1036 else
1037 {
1038 *p = NUL;
1039 line_arg = p + 1;
1040 }
1041 }
1042 else
1043 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001044 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001045 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001046 }
1047 if (KeyTyped)
1048 lines_left = Rows - 1;
1049 if (theline == NULL)
1050 {
1051 // Use the start of the function for the line number.
1052 SOURCING_LNUM = sourcing_lnum_top;
1053 if (skip_until != NULL)
1054 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001055 else if (nesting_inline[nesting])
1056 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001057 else if (eap->cmdidx == CMD_def)
1058 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001059 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001060 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001061 goto theend;
1062 }
1063
1064 // Detect line continuation: SOURCING_LNUM increased more than one.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001065 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001066 if (SOURCING_LNUM < sourcing_lnum_off)
1067 sourcing_lnum_off -= SOURCING_LNUM;
1068 else
1069 sourcing_lnum_off = 0;
1070
1071 if (skip_until != NULL)
1072 {
1073 // Don't check for ":endfunc"/":enddef" between
1074 // * ":append" and "."
1075 // * ":python <<EOF" and "EOF"
1076 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
1077 if (heredoc_trimmed == NULL
1078 || (is_heredoc && skipwhite(theline) == theline)
1079 || STRNCMP(theline, heredoc_trimmed,
John Marriottb32800f2025-02-01 15:25:34 +01001080 heredoc_trimmedlen) == 0)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001081 {
1082 if (heredoc_trimmed == NULL)
1083 p = theline;
1084 else if (is_heredoc)
1085 p = skipwhite(theline) == theline
John Marriottb32800f2025-02-01 15:25:34 +01001086 ? theline : theline + heredoc_trimmedlen;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001087 else
John Marriottb32800f2025-02-01 15:25:34 +01001088 p = theline + heredoc_trimmedlen;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001089 if (STRCMP(p, skip_until) == 0)
1090 {
1091 VIM_CLEAR(skip_until);
1092 VIM_CLEAR(heredoc_trimmed);
John Marriottb32800f2025-02-01 15:25:34 +01001093 heredoc_trimmedlen = 0;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001094 getline_options = vim9_function
1095 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
1096 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +02001097
1098 if (heredoc_concat_len > 0)
1099 {
1100 // Replace the starting line with all the concatenated
1101 // lines.
1102 ga_concat(&heredoc_ga, theline);
1103 vim_free(((char_u **)(newlines->ga_data))[
1104 heredoc_concat_len - 1]);
1105 ((char_u **)(newlines->ga_data))[
1106 heredoc_concat_len - 1] = heredoc_ga.ga_data;
1107 ga_init(&heredoc_ga);
1108 heredoc_concat_len = 0;
1109 theline += STRLEN(theline); // skip the "EOF"
1110 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001111 }
1112 }
1113 }
1114 else
1115 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001116 int c;
1117 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +00001118 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001119
1120 // skip ':' and blanks
1121 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
1122 ;
1123
1124 // Check for "endfunction", "enddef" or "}".
1125 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +00001126 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001127 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001128 ? *p == '}'
1129 : (checkforcmd(&p, nesting_def[nesting]
1130 ? "enddef" : "endfunction", 4)
1131 && *p != ':'))
1132 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00001133 if (!nesting_inline[nesting] && nesting_def[nesting]
1134 && p < cmd + 6)
1135 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001136 if (nesting-- == 0)
1137 {
1138 char_u *nextcmd = NULL;
1139
1140 if (*p == '|' || *p == '}')
1141 nextcmd = p + 1;
1142 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
1143 nextcmd = line_arg;
1144 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001145 && (vim9_function || p_verbose > 0))
1146 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +02001147 SOURCING_LNUM = sourcing_lnum_top
1148 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001149 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +00001150 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001151 else
1152 give_warning2((char_u *)
1153 _("W22: Text found after :endfunction: %s"),
1154 p, TRUE);
1155 }
1156 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001157 {
1158 // Another command follows. If the line came from "eap"
1159 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001160 // change "eap->cmdlinep" to point to the last fetched
1161 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001162 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001163 if (lines_to_free->ga_len > 0
1164 && *eap->cmdlinep !=
1165 ((char_u **)lines_to_free->ga_data)
1166 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001167 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001168 // *cmdlinep will be freed later, thus remove the
1169 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001170 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001171 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
1172 [lines_to_free->ga_len - 1];
1173 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001174 }
1175 }
1176 break;
1177 }
1178 }
1179
1180 // Check for mismatched "endfunc" or "enddef".
1181 // We don't check for "def" inside "func" thus we also can't check
1182 // for "enddef".
1183 // We continue to find the end of the function, although we might
1184 // not find it.
1185 else if (nesting_def[nesting])
1186 {
1187 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1188 emsg(_(e_mismatched_endfunction));
1189 }
1190 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1191 emsg(_(e_mismatched_enddef));
1192
1193 // Increase indent inside "if", "while", "for" and "try", decrease
1194 // at "end".
1195 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1196 indent -= 2;
1197 else if (STRNCMP(p, "if", 2) == 0
1198 || STRNCMP(p, "wh", 2) == 0
1199 || STRNCMP(p, "for", 3) == 0
1200 || STRNCMP(p, "try", 3) == 0)
1201 indent += 2;
1202
1203 // Check for defining a function inside this function.
1204 // Only recognize "def" inside "def", not inside "function",
1205 // For backwards compatibility, see Test_function_python().
1206 c = *p;
1207 if (is_function_cmd(&p)
1208 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1209 {
1210 if (*p == '!')
1211 p = skipwhite(p + 1);
1212 p += eval_fname_script(p);
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001213 vim_free(trans_function_name(&p, NULL, TRUE, 0));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001214 if (*skipwhite(p) == '(')
1215 {
1216 if (nesting == MAX_FUNC_NESTING - 1)
1217 emsg(_(e_function_nesting_too_deep));
1218 else
1219 {
1220 ++nesting;
1221 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001222 nesting_inline[nesting] = FALSE;
1223 indent += 2;
1224 }
1225 }
1226 }
1227
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001228 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001229 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001230 // Not a comment line: check for nested inline function.
Yegappan Lakshmanan0072cee2025-01-07 20:22:32 +01001231
1232 if (nesting_inline[nesting])
1233 {
1234 // A comment (#) can follow the opening curly brace of a
1235 // block statement. Need to ignore the comment and look
1236 // for the opening curly brace before the comment.
1237 end = find_start_of_vim9_comment(p) - 1;
1238 }
1239 else
1240 end = p + STRLEN(p) - 1;
1241
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001242 while (end > p && VIM_ISWHITE(*end))
1243 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001244 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001245 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001246 int is_block;
1247
1248 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001249 --end;
1250 while (end > p && VIM_ISWHITE(*end))
1251 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001252 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1253 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001254 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001255 char_u *s = p;
1256
1257 // check for line starting with "au" for :autocmd or
1258 // "com" for :command, these can use a {} block
1259 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1260 || checkforcmd_noparen(&s, "command", 3);
1261 }
1262
1263 if (is_block)
1264 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001265 if (nesting == MAX_FUNC_NESTING - 1)
1266 emsg(_(e_function_nesting_too_deep));
1267 else
1268 {
1269 ++nesting;
1270 nesting_def[nesting] = TRUE;
1271 nesting_inline[nesting] = TRUE;
1272 indent += 2;
1273 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001274 }
1275 }
1276 }
1277
1278 // Check for ":append", ":change", ":insert". Not for :def.
1279 p = skip_range(p, FALSE, NULL);
1280 if (!vim9_function
1281 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1282 || (p[0] == 'c'
1283 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1284 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1285 && (STRNCMP(&p[3], "nge", 3) != 0
1286 || !ASCII_ISALPHA(p[6])))))))
1287 || (p[0] == 'i'
1288 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1289 && (!ASCII_ISALPHA(p[2])
1290 || (p[2] == 's'
1291 && (!ASCII_ISALPHA(p[3])
1292 || p[3] == 'e'))))))))
John Marriottb32800f2025-02-01 15:25:34 +01001293 skip_until = vim_strnsave((char_u *)".", 1);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001294
1295 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1296 arg = skipwhite(skiptowhite(p));
1297 if (arg[0] == '<' && arg[1] =='<'
1298 && ((p[0] == 'p' && p[1] == 'y'
1299 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1300 || ((p[2] == '3' || p[2] == 'x')
1301 && !ASCII_ISALPHA(p[3]))))
1302 || (p[0] == 'p' && p[1] == 'e'
1303 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1304 || (p[0] == 't' && p[1] == 'c'
1305 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1306 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1307 && !ASCII_ISALPHA(p[3]))
1308 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1309 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1310 || (p[0] == 'm' && p[1] == 'z'
1311 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1312 ))
1313 {
1314 // ":python <<" continues until a dot, like ":append"
1315 p = skipwhite(arg + 2);
zeertzjq449c2e52025-02-03 18:56:16 +01001316 if (STRNCMP(p, "trim", 4) == 0
1317 && (p[4] == NUL || VIM_ISWHITE(p[4])))
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001318 {
1319 // Ignore leading white space.
1320 p = skipwhite(p + 4);
John Marriottb32800f2025-02-01 15:25:34 +01001321 heredoc_trimmedlen = skipwhite(theline) - theline;
1322 heredoc_trimmed = vim_strnsave(theline, heredoc_trimmedlen);
1323 if (heredoc_trimmed == NULL)
1324 heredoc_trimmedlen = 0;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001325 }
1326 if (*p == NUL)
John Marriottb32800f2025-02-01 15:25:34 +01001327 skip_until = vim_strnsave((char_u *)".", 1);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001328 else
1329 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1330 getline_options = GETLINE_NONE;
1331 is_heredoc = TRUE;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001332 if (vim9_function && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001333 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001334 }
1335
Bram Moolenaard881d152022-05-13 13:50:36 +01001336 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001337 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001338 // Check for ":cmd v =<< [trim] EOF"
1339 // and ":cmd [a, b] =<< [trim] EOF"
1340 // and "lines =<< [trim] EOF" for Vim9
1341 // Where "cmd" can be "let", "var", "final" or "const".
zeertzjqa93d9cd2023-05-02 16:25:47 +01001342 arg = p;
1343 if (checkforcmd(&arg, "let", 2)
1344 || checkforcmd(&arg, "var", 3)
1345 || checkforcmd(&arg, "final", 5)
1346 || checkforcmd(&arg, "const", 5)
1347 || vim9_function)
Bram Moolenaard881d152022-05-13 13:50:36 +01001348 {
zeertzjq9a91d2b2024-04-09 21:47:10 +02001349 int save_sc_version = current_sctx.sc_version;
1350 int var_count = 0;
1351 int semicolon = 0;
zeertzjq9a91d2b2024-04-09 21:47:10 +02001352
1353 current_sctx.sc_version
1354 = vim9_function ? SCRIPT_VERSION_VIM9 : 1;
zeertzjq1817ccd2024-04-10 17:37:47 +02001355 arg = skip_var_list(arg, TRUE, &var_count, &semicolon,
zeertzjq9a91d2b2024-04-09 21:47:10 +02001356 TRUE);
zeertzjq1817ccd2024-04-10 17:37:47 +02001357 if (arg != NULL)
1358 arg = skipwhite(arg);
zeertzjq9a91d2b2024-04-09 21:47:10 +02001359 current_sctx.sc_version = save_sc_version;
zeertzjq1817ccd2024-04-10 17:37:47 +02001360 if (arg != NULL && STRNCMP(arg, "=<<", 3) == 0)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001361 {
zeertzjq449c2e52025-02-03 18:56:16 +01001362 int has_trim = FALSE;
1363
Bram Moolenaard881d152022-05-13 13:50:36 +01001364 p = skipwhite(arg + 3);
1365 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001366 {
zeertzjq449c2e52025-02-03 18:56:16 +01001367 if (STRNCMP(p, "trim", 4) == 0
1368 && (p[4] == NUL || VIM_ISWHITE(p[4])))
Bram Moolenaard881d152022-05-13 13:50:36 +01001369 {
1370 // Ignore leading white space.
1371 p = skipwhite(p + 4);
zeertzjq449c2e52025-02-03 18:56:16 +01001372 has_trim = TRUE;
Bram Moolenaard881d152022-05-13 13:50:36 +01001373 continue;
1374 }
zeertzjq449c2e52025-02-03 18:56:16 +01001375 if (STRNCMP(p, "eval", 4) == 0
1376 && (p[4] == NUL || VIM_ISWHITE(p[4])))
Bram Moolenaard881d152022-05-13 13:50:36 +01001377 {
1378 // Ignore leading white space.
1379 p = skipwhite(p + 4);
1380 continue;
1381 }
1382 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001383 }
zeertzjq449c2e52025-02-03 18:56:16 +01001384 if (has_trim)
1385 {
1386 heredoc_trimmedlen = skipwhite(theline) - theline;
1387 heredoc_trimmed = vim_strnsave(theline, heredoc_trimmedlen);
1388 if (heredoc_trimmed == NULL)
1389 heredoc_trimmedlen = 0;
1390 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001391 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1392 getline_options = GETLINE_NONE;
1393 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001394 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001395 }
1396 }
1397 }
1398
1399 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001400 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001401 goto theend;
1402
Bram Moolenaar20677332021-06-06 17:02:53 +02001403 if (heredoc_concat_len > 0)
1404 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001405 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001406 // to be used for the instruction later.
1407 ga_concat(&heredoc_ga, theline);
1408 ga_concat(&heredoc_ga, (char_u *)"\n");
John Marriottb32800f2025-02-01 15:25:34 +01001409 p = vim_strnsave((char_u *)"", 0);
Bram Moolenaar20677332021-06-06 17:02:53 +02001410 }
1411 else
1412 {
1413 // Copy the line to newly allocated memory. get_one_sourceline()
1414 // allocates 250 bytes per line, this saves 80% on average. The
1415 // cost is an extra alloc/free.
1416 p = vim_strsave(theline);
1417 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001418 if (p == NULL)
1419 goto theend;
1420 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1421
1422 // Add NULL lines for continuation lines, so that the line count is
1423 // equal to the index in the growarray.
1424 while (sourcing_lnum_off-- > 0)
1425 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1426
1427 // Check for end of eap->arg.
1428 if (line_arg != NULL && *line_arg == NUL)
1429 line_arg = NULL;
1430 }
1431
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001432 // Return OK when no error was detected.
1433 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001434 ret = OK;
1435
1436theend:
1437 vim_free(skip_until);
1438 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001439 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001440 need_wait_return |= saved_wait_return;
1441 return ret;
1442}
1443
1444/*
1445 * Handle the body of a lambda. *arg points to the "{", process statements
1446 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001447 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001448 * When successful "rettv" is set to a funcref.
1449 */
1450 static int
1451lambda_function_body(
1452 char_u **arg,
1453 typval_T *rettv,
1454 evalarg_T *evalarg,
1455 garray_T *newargs,
1456 garray_T *argtypes,
1457 int varargs,
1458 garray_T *default_args,
1459 char_u *ret_type)
1460{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001461 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001462 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001463 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001464 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001465 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001466 exarg_T eap;
1467 garray_T newlines;
1468 char_u *cmdline = NULL;
1469 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001470 partial_T *pt;
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001471 string_T name;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001472 int lnum_save = -1;
1473 linenr_T sourcing_lnum_top = SOURCING_LNUM;
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001474 char_u *line_arg = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001475
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001476 *arg = skipwhite(*arg + 1);
1477 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001478 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001479 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001480 return FAIL;
1481 }
1482
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001483 // When there is a line break use what follows for the lambda body.
1484 // Makes lambda body initializers work for object and enum member
1485 // variables.
1486 if (**arg == '\n')
1487 line_arg = *arg + 1;
1488
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001489 CLEAR_FIELD(eap);
1490 eap.cmdidx = CMD_block;
1491 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001492 eap.cmdlinep = &cmdline;
1493 eap.skip = !evaluate;
1494 if (evalarg->eval_cctx != NULL)
1495 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1496 else
1497 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001498 eap.ea_getline = evalarg->eval_getline;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001499 eap.cookie = evalarg->eval_cookie;
1500 }
1501
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001502 ga_init2(&newlines, sizeof(char_u *), 10);
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001503 if (get_function_body(&eap, &newlines, line_arg,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001504 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001505 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001506
1507 // When inside a lambda must add the function lines to evalarg.eval_ga.
1508 evalarg->eval_break_count += newlines.ga_len;
1509 if (gap->ga_itemsize > 0)
1510 {
1511 int idx;
1512 char_u *last;
1513 size_t plen;
1514 char_u *pnl;
1515
1516 for (idx = 0; idx < newlines.ga_len; ++idx)
1517 {
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001518 char_u *p = ((char_u **)newlines.ga_data)[idx];
1519 if (p == NULL)
1520 // comment line in the lambda body
1521 continue;
1522
1523 p = skipwhite(p);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001524
Bram Moolenaarecb66452021-05-18 15:09:18 +02001525 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001526 goto erret;
1527
1528 // Going to concatenate the lines after parsing. For an empty or
1529 // comment line use an empty string.
1530 // Insert NL characters at the start of each line, the string will
1531 // be split again later in .get_lambda_tv().
1532 if (*p == NUL || vim9_comment_start(p))
John Marriottb32800f2025-02-01 15:25:34 +01001533 {
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001534 p = (char_u *)"";
John Marriottb32800f2025-02-01 15:25:34 +01001535 plen = 0;
1536 }
1537 else
1538 plen = STRLEN(p);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001539 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1540 if (pnl != NULL)
1541 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001542 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1543 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001544 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001545 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001546 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001547 if (eap.nextcmd != NULL)
John Marriottb32800f2025-02-01 15:25:34 +01001548 {
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001549 // more is following after the "}", which was skipped
1550 last = cmdline;
John Marriottb32800f2025-02-01 15:25:34 +01001551 plen = STRLEN(last);
1552 }
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001553 else
John Marriottb32800f2025-02-01 15:25:34 +01001554 {
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001555 // nothing is following the "}"
1556 last = (char_u *)"}";
John Marriottb32800f2025-02-01 15:25:34 +01001557 plen = 1;
1558 }
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001559 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1560 if (pnl != NULL)
1561 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001562 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1563 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001564 }
1565
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001566 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001567 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001568 garray_T *tfgap = &evalarg->eval_tofree_ga;
1569
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001570 // Something comes after the "}".
1571 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001572
1573 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001574 if (ga_grow(tfgap, 1) == OK)
1575 {
1576 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1577 evalarg->eval_using_cmdline = TRUE;
1578 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001579 }
1580 else
1581 *arg = (char_u *)"";
1582
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001583 if (!evaluate)
1584 {
1585 ret = OK;
1586 goto erret;
1587 }
1588
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001589 name = get_lambda_name();
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001590 ufunc = alloc_ufunc(name.string, name.length);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001591 if (ufunc == NULL)
1592 goto erret;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001593 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001594 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001595 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001596 ufunc->uf_refcount = 1;
1597 ufunc->uf_args = *newargs;
1598 newargs->ga_data = NULL;
1599 ufunc->uf_def_args = *default_args;
1600 default_args->ga_data = NULL;
1601 ufunc->uf_func_type = &t_func_any;
1602
1603 // error messages are for the first function line
1604 lnum_save = SOURCING_LNUM;
1605 SOURCING_LNUM = sourcing_lnum_top;
1606
1607 // parse argument types
h-eastb895b0f2023-09-24 15:46:31 +02001608 if (parse_argument_types(ufunc, argtypes, varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001609 {
1610 SOURCING_LNUM = lnum_save;
1611 goto erret;
1612 }
1613
1614 // parse the return type, if any
1615 if (parse_return_type(ufunc, ret_type) == FAIL)
1616 goto erret;
1617
1618 pt = ALLOC_CLEAR_ONE(partial_T);
1619 if (pt == NULL)
1620 goto erret;
1621 pt->pt_func = ufunc;
1622 pt->pt_refcount = 1;
1623
1624 ufunc->uf_lines = newlines;
1625 newlines.ga_data = NULL;
1626 if (sandbox)
1627 ufunc->uf_flags |= FC_SANDBOX;
1628 if (!ASCII_ISUPPER(*ufunc->uf_name))
1629 ufunc->uf_flags |= FC_VIM9;
1630 ufunc->uf_script_ctx = current_sctx;
1631 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1632 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1633 set_function_type(ufunc);
1634
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001635 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1636
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001637 rettv->vval.v_partial = pt;
1638 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001639 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001640 ret = OK;
1641
1642erret:
1643 if (lnum_save >= 0)
1644 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001645 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001646 if (newargs != NULL)
1647 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001648 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001649 if (ufunc != NULL)
1650 {
1651 func_clear(ufunc, TRUE);
1652 func_free(ufunc, TRUE);
1653 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001654 return ret;
1655}
1656
1657/*
1658 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001659 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001660 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001661 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1662 */
1663 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001664get_lambda_tv(
1665 char_u **arg,
1666 typval_T *rettv,
1667 int types_optional,
1668 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001669{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001670 int evaluate = evalarg != NULL
1671 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001672 garray_T newargs;
1673 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001674 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001675 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001676 garray_T default_args;
h-eastb895b0f2023-09-24 15:46:31 +02001677 garray_T arg_objm;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001678 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001679 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001680 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001681 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001682 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001683 char_u *s;
1684 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001685 int *old_eval_lavars = eval_lavars_used;
1686 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001687 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001688 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001689 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001690 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001691 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001692 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001693
Bram Moolenaar4525a572022-02-13 11:57:33 +00001694 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001695 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001696
1697 ga_init(&newargs);
1698 ga_init(&newlines);
1699
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001700 // First, check if this is really a lambda expression. "->" or "=>" must
1701 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001702 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001703 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001704 types_optional ? &argtypes : NULL, types_optional,
1705 types_optional ? &arg_objm : NULL, evalarg,
1706 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001707 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001708 {
1709 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001710 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001711 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001712 ga_clear(&arg_objm);
1713 }
Bram Moolenaar0346b792021-01-31 22:18:29 +01001714 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001715 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001716
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001717 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001718 if (evaluate)
1719 pnewargs = &newargs;
1720 else
1721 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001722 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001723 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
h-eastb895b0f2023-09-24 15:46:31 +02001724 types_optional ? &argtypes : NULL, types_optional,
1725 types_optional ? &arg_objm : NULL, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001726 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001727 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001728 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001729 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001730 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001731 {
1732 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001733 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001734 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001735 ga_clear(&arg_objm);
1736 }
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001737 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001738 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001739 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001740 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001741
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001742 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1743 if (ret_type != NULL)
1744 {
1745 ret_type = vim_strsave(ret_type);
1746 tofree2 = ret_type;
1747 }
1748
Bram Moolenaare38eab22019-12-05 21:50:01 +01001749 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001750 if (evaluate)
1751 eval_lavars_used = &eval_lavars;
1752
Bram Moolenaar65c44152020-12-24 15:14:01 +01001753 *arg = skipwhite_and_linebreak(*arg, evalarg);
1754
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001755 // Recognize "{" as the start of a function body.
1756 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001757 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001758 if (evalarg == NULL)
1759 // cannot happen?
1760 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001761 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001762 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1763 types_optional ? &argtypes : NULL, varargs,
1764 &default_args, ret_type) == FAIL)
1765 goto errret;
1766 goto theend;
1767 }
1768 if (default_args.ga_len > 0)
1769 {
1770 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001771 goto errret;
1772 }
1773
Bram Moolenaare38eab22019-12-05 21:50:01 +01001774 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001775 start = *arg;
1776 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001777 if (ret == FAIL)
1778 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001779
Bram Moolenaar65c44152020-12-24 15:14:01 +01001780 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001781 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001782 *arg = skipwhite_and_linebreak(*arg, evalarg);
1783 if (**arg != '}')
1784 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001785 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001786 goto errret;
1787 }
1788 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001789 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001790
1791 if (evaluate)
1792 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001793 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001794 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001795 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001796 char_u *line_end;
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001797 string_T name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001798
Yee Cheng Chin0b5fe422025-03-03 20:12:05 +01001799 fp = alloc_ufunc(name.string, name.length);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001800 if (fp == NULL)
1801 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001802 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001803 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001804 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001805 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001806
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001807 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001808 if (ga_grow(&newlines, 1) == FAIL)
1809 goto errret;
1810
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001811 // If there are line breaks, we need to split up the string.
1812 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001813 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001814 line_end = end;
1815
1816 // Add "return " before the expression (or the first line).
1817 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001818 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001819 if (p == NULL)
1820 goto errret;
1821 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1822 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001823 vim_strncpy(p + 7, start, line_end - start);
1824
1825 if (line_end != end)
1826 {
1827 // Add more lines, split by line breaks. Thus is used when a
1828 // lambda with { cmds } is encountered.
1829 while (*line_end == '\n')
1830 {
1831 if (ga_grow(&newlines, 1) == FAIL)
1832 goto errret;
1833 start = line_end + 1;
1834 line_end = vim_strchr(start, '\n');
1835 if (line_end == NULL)
1836 line_end = end;
1837 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1838 vim_strnsave(start, line_end - start);
1839 }
1840 }
1841
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001842 if (strstr((char *)p + 7, "a:") == NULL)
1843 // No a: variables are used for sure.
1844 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001845
1846 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001847 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001848 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001849 if (types_optional)
1850 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001851 if (parse_argument_types(fp, &argtypes,
h-eastb895b0f2023-09-24 15:46:31 +02001852 vim9script && varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001853 goto errret;
1854 if (ret_type != NULL)
1855 {
1856 fp->uf_ret_type = parse_type(&ret_type,
1857 &fp->uf_type_list, TRUE);
1858 if (fp->uf_ret_type == NULL)
1859 goto errret;
1860 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001861 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001862 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001863 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001864
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001865 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001866 if (current_funccal != NULL && eval_lavars)
1867 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001868 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001869 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001870 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001871 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001872
1873#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001874 if (prof_def_func())
1875 func_do_profile(fp);
1876#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001877 if (sandbox)
1878 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001879 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001880 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001881 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001882 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001883 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001884 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001885 // Use the line number of the arguments.
1886 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001887
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001888 function_using_block_scopes(fp, evalarg->eval_cstack);
1889
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001890 pt->pt_func = fp;
1891 pt->pt_refcount = 1;
1892 rettv->vval.v_partial = pt;
1893 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001894
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001895 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001896 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001897
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001898theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001899 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001900 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001901 if (types_optional)
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001902 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001903 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001904 ga_clear(&arg_objm);
1905 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001906
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001907 return OK;
1908
1909errret:
1910 ga_clear_strings(&newargs);
1911 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001912 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001913 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001914 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001915 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001916 ga_clear(&arg_objm);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001917 }
John Marriottb32800f2025-02-01 15:25:34 +01001918 if (fp != NULL)
1919 {
1920 vim_free(fp->uf_arg_types);
1921 vim_free(fp->uf_name_exp);
1922 vim_free(fp);
1923 }
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001924 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001925 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001926 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001927 return FAIL;
1928}
1929
1930/*
1931 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1932 * name it contains, otherwise return "name".
1933 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1934 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001935 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1936 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001937 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001938 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001939 */
1940 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001941deref_func_name(
1942 char_u *name,
1943 int *lenp,
1944 partial_T **partialp,
1945 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001946 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001947 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001948 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001949{
1950 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001951 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001952 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001953 char_u *s = NULL;
1954 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001955 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001956
1957 if (partialp != NULL)
1958 *partialp = NULL;
1959
1960 cc = name[*lenp];
1961 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001962
Bram Moolenaar71f21932022-01-07 18:20:55 +00001963 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001964 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001965 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001966 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001967 tv = &v->di_tv;
1968 }
1969 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1970 {
1971 imported_T *import;
1972 char_u *p = name;
1973 int len = *lenp;
1974
1975 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001976 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001977 p = name + 2;
1978 len -= 2;
1979 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001980 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001981
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001982 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001983 if (import != NULL)
1984 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001985 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001986 if (new_function)
1987 semsg(_(e_redefining_imported_item_str), name);
1988 else
1989 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001990 name[len] = cc;
1991 *lenp = 0;
1992 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001993 }
1994 }
1995
1996 if (tv != NULL)
1997 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001998 if (found_var != NULL)
1999 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01002000 if (tv->v_type == VAR_FUNC)
2001 {
2002 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002003 {
2004 *lenp = 0;
2005 return (char_u *)""; // just in case
2006 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01002007 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002008 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002009 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002010
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01002011 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002012 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01002013 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002014
2015 if (pt == NULL)
2016 {
2017 *lenp = 0;
2018 return (char_u *)""; // just in case
2019 }
2020 if (partialp != NULL)
2021 *partialp = pt;
2022 s = partial_name(pt);
2023 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002024 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002025
2026 if (s != NULL)
2027 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01002028 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002029 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002030 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002031
2032 if (sv != NULL)
2033 *type = sv->sv_type;
2034 }
2035 return s;
2036 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002037 }
2038
2039 return name;
2040}
2041
2042/*
2043 * Give an error message with a function name. Handle <SNR> things.
2044 * "ermsg" is to be passed without translation, use N_() instead of _().
2045 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002046 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002047emsg_funcname(char *ermsg, char_u *name)
2048{
Bram Moolenaar54598062022-01-13 12:05:09 +00002049 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002050
Bram Moolenaar54598062022-01-13 12:05:09 +00002051 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002052 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002053 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002054 if (p != name)
2055 vim_free(p);
2056}
2057
2058/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002059 * Get function arguments at "*arg" and advance it.
2060 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01002061 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002062 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002063 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002064get_func_arguments(
2065 char_u **arg,
2066 evalarg_T *evalarg,
2067 int partial_argc,
2068 typval_T *argvars,
Ernie Raelb077b582023-12-14 20:11:44 +01002069 int *argcount,
2070 int is_builtin)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002071{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002072 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002073 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002074 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002075 int evaluate = evalarg == NULL
2076 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002077
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002078 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002079 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02002080 // skip the '(' or ',' and possibly line breaks
2081 argp = skipwhite_and_linebreak(argp + 1, evalarg);
2082
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002083 if (*argp == ')' || *argp == ',' || *argp == NUL)
2084 break;
Ernie Raelb077b582023-12-14 20:11:44 +01002085
2086 int arg_idx = *argcount;
2087 if (eval1(&argp, &argvars[arg_idx], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002088 {
2089 ret = FAIL;
2090 break;
2091 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002092 ++*argcount;
Ernie Raelb077b582023-12-14 20:11:44 +01002093 if (!is_builtin && check_typval_is_value(&argvars[arg_idx]) == FAIL)
2094 {
2095 ret = FAIL;
2096 break;
2097 }
2098
Bram Moolenaar9d489562020-07-30 20:08:50 +02002099 // The comma should come right after the argument, but this wasn't
2100 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002101 if (vim9script)
2102 {
2103 if (*argp != ',' && *skipwhite(argp) == ',')
2104 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002105 if (evaluate)
2106 semsg(_(e_no_white_space_allowed_before_str_str),
2107 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002108 ret = FAIL;
2109 break;
2110 }
2111 }
2112 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002113 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002114 if (*argp != ',')
2115 break;
Christian Brabandt00cb2472023-09-05 20:46:25 +02002116 if (vim9script && !IS_WHITE_NL_OR_NUL(argp[1]))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002117 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002118 if (evaluate)
2119 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002120 ret = FAIL;
2121 break;
2122 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002123 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002124
Bram Moolenaare6b53242020-07-01 17:28:33 +02002125 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002126 if (*argp == ')')
2127 ++argp;
2128 else
2129 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002130 *arg = argp;
2131 return ret;
2132}
2133
2134/*
2135 * Call a function and put the result in "rettv".
2136 * Return OK or FAIL.
2137 */
2138 int
2139get_func_tv(
2140 char_u *name, // name of the function
2141 int len, // length of "name" or -1 to use strlen()
2142 typval_T *rettv,
2143 char_u **arg, // argument, pointing to the '('
2144 evalarg_T *evalarg, // for line continuation
2145 funcexe_T *funcexe) // various values
2146{
2147 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002148 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002149 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
2150 int argcount = 0; // number of arguments found
2151 int vim9script = in_vim9script();
2152 int evaluate = evalarg == NULL
2153 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
2154
2155 argp = *arg;
2156 ret = get_func_arguments(&argp, evalarg,
2157 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
Ernie Raelb077b582023-12-14 20:11:44 +01002158 argvars, &argcount, builtin_function(name, -1));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002159
2160 if (ret == OK)
2161 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002162 int i = 0;
2163 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002164
2165 if (get_vim_var_nr(VV_TESTING))
2166 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002167 // Prepare for calling test_garbagecollect_now(), need to know
2168 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002169 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002170 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002171 for (i = 0; i < argcount; ++i)
2172 if (ga_grow(&funcargs, 1) == OK)
2173 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
2174 &argvars[i];
2175 }
2176
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002177 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00002178 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002179 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002180 // An error in a builtin function does not return FAIL, but we do
2181 // want to abort further processing if an error was given.
2182 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002183 clear_tv(rettv);
2184 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002185
2186 funcargs.ga_len -= i;
2187 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002188 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002189 {
2190 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002191 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002192 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00002193 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002194 }
2195
2196 while (--argcount >= 0)
2197 clear_tv(&argvars[argcount]);
2198
Bram Moolenaar4525a572022-02-13 11:57:33 +00002199 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02002200 *arg = argp;
2201 else
2202 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002203 return ret;
2204}
2205
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002206/*
2207 * Return TRUE if "p" starts with "<SID>" or "s:".
2208 * Only works if eval_fname_script() returned non-zero for "p"!
2209 */
2210 static int
2211eval_fname_sid(char_u *p)
2212{
2213 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
2214}
2215
2216/*
2217 * In a script change <SID>name() and s:name() to K_SNR 123_name().
2218 * Change <SNR>123_name() to K_SNR 123_name().
2219 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002220 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002221 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002222 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00002223fname_trans_sid(
2224 char_u *name,
2225 char_u *fname_buf,
2226 char_u **tofree,
2227 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002228{
John Marriottb32800f2025-02-01 15:25:34 +01002229 char_u *script_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002230 char_u *fname;
John Marriottb32800f2025-02-01 15:25:34 +01002231 size_t fnamelen;
2232 size_t fname_buflen;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002233
John Marriottb32800f2025-02-01 15:25:34 +01002234 script_name = name + eval_fname_script(name);
2235 if (script_name == name)
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002236 return name; // no prefix
2237
2238 fname_buf[0] = K_SPECIAL;
2239 fname_buf[1] = KS_EXTRA;
2240 fname_buf[2] = (int)KE_SNR;
John Marriottb32800f2025-02-01 15:25:34 +01002241 fname_buflen = 3;
2242 if (!eval_fname_sid(name)) // "<SID>" or "s:"
2243 fname_buf[fname_buflen] = NUL;
2244 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002245 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002246 if (current_sctx.sc_sid <= 0)
2247 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002248 else
2249 {
John Marriottb32800f2025-02-01 15:25:34 +01002250 fname_buflen += vim_snprintf((char *)fname_buf + 3,
2251 FLEN_FIXED - 3,
2252 "%ld_",
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002253 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002254 }
2255 }
John Marriottb32800f2025-02-01 15:25:34 +01002256 fnamelen = fname_buflen + STRLEN(script_name);
2257 if (fnamelen < FLEN_FIXED)
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002258 {
John Marriottb32800f2025-02-01 15:25:34 +01002259 STRCPY(fname_buf + fname_buflen, script_name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002260 fname = fname_buf;
2261 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002262 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002263 {
John Marriottb32800f2025-02-01 15:25:34 +01002264 fname = alloc(fnamelen + 1);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002265 if (fname == NULL)
2266 *error = FCERR_OTHER;
2267 else
2268 {
2269 *tofree = fname;
John Marriottb32800f2025-02-01 15:25:34 +01002270 vim_snprintf((char *)fname, fnamelen + 1, "%s%s", fname_buf, script_name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002271 }
2272 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002273 return fname;
2274}
2275
2276/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002277 * Concatenate the script ID and function name into "<SNR>99_name".
2278 * "buffer" must have size MAX_FUNC_NAME_LEN.
2279 */
2280 void
2281func_name_with_sid(char_u *name, int sid, char_u *buffer)
2282{
2283 // A script-local function is stored as "<SNR>99_name".
2284 buffer[0] = K_SPECIAL;
2285 buffer[1] = KS_EXTRA;
2286 buffer[2] = (int)KE_SNR;
2287 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2288 (long)sid, name);
2289}
2290
2291/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292 * Find a function "name" in script "sid".
2293 */
2294 static ufunc_T *
2295find_func_with_sid(char_u *name, int sid)
2296{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002297 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002298 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299
Bram Moolenaarb8822442022-01-11 15:24:05 +00002300 if (!SCRIPT_ID_VALID(sid))
2301 return NULL; // not in a script
2302
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002303 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002304 hi = hash_find(&func_hashtab, buffer);
2305 if (!HASHITEM_EMPTY(hi))
2306 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002307 return NULL;
2308}
2309
2310/*
2311 * Find a function "name" in script "sid" prefixing the autoload prefix.
2312 */
2313 static ufunc_T *
2314find_func_with_prefix(char_u *name, int sid)
2315{
2316 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002317 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002318 scriptitem_T *si;
2319
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002320 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002321 return NULL; // already has the prefix
2322 if (!SCRIPT_ID_VALID(sid))
2323 return NULL; // not in a script
2324 si = SCRIPT_ITEM(sid);
2325 if (si->sn_autoload_prefix != NULL)
2326 {
2327 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2328 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002329 char_u *namep;
2330
2331 // skip a "<SNR>99_" prefix
2332 namep = untrans_function_name(name);
2333 if (namep == NULL)
2334 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002335
2336 // An exported function in an autoload script is stored as
2337 // "dir#path#name".
2338 if (len < sizeof(buffer))
2339 auto_name = buffer;
2340 else
2341 auto_name = alloc(len);
2342 if (auto_name != NULL)
2343 {
2344 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002345 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002346 hi = hash_find(&func_hashtab, auto_name);
2347 if (auto_name != buffer)
2348 vim_free(auto_name);
2349 if (!HASHITEM_EMPTY(hi))
2350 return HI2UF(hi);
2351 }
2352 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353
2354 return NULL;
2355}
2356
2357/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002358 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002359 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2360 * functions.
2361 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002362 * Return NULL for unknown function.
2363 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002364 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002365find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002366{
2367 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002369
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002370 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002372 // Find script-local function before global one.
2373 if (in_vim9script() && eval_isnamec1(*name)
2374 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002375 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002376 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2377 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002378 if (func != NULL)
2379 return func;
2380 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002381 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2382 {
2383 char_u *p = name + 5;
2384 long sid;
2385
2386 // printable "<SNR>123_Name" form
2387 sid = getdigits(&p);
2388 if (*p == '_')
2389 {
2390 func = find_func_with_sid(p + 1, (int)sid);
2391 if (func != NULL)
2392 return func;
2393 }
2394 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002396
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002397 if ((flags & FFED_NO_GLOBAL) == 0)
2398 {
2399 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002400 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002401 if (!HASHITEM_EMPTY(hi))
2402 return HI2UF(hi);
2403 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404
Bram Moolenaarb8822442022-01-11 15:24:05 +00002405 // Find autoload function if this is an autoload script.
Yegappan Lakshmanan6289f912025-01-14 17:13:36 +01002406 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
Bram Moolenaarb8822442022-01-11 15:24:05 +00002407 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408}
2409
2410/*
2411 * Find a function by name, return pointer to it in ufuncs.
2412 * "cctx" is passed in a :def function to find imported functions.
2413 * Return NULL for unknown or dead function.
2414 */
2415 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002416find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002418 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419
2420 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2421 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002422 return NULL;
2423}
2424
2425/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002426 * Return TRUE if "ufunc" is a global function.
2427 */
2428 int
2429func_is_global(ufunc_T *ufunc)
2430{
2431 return ufunc->uf_name[0] != K_SPECIAL;
2432}
2433
2434/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002435 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2436 */
2437 int
2438func_requires_g_prefix(ufunc_T *ufunc)
2439{
John Marriottb32800f2025-02-01 15:25:34 +01002440 return func_is_global(ufunc)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002441 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002442 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01002443 && !SAFE_isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002444}
2445
2446/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002447 * Copy the function name of "fp" to buffer "buf".
2448 * "buf" must be able to hold the function name plus three bytes.
2449 * Takes care of script-local function names.
2450 */
John Marriottb32800f2025-02-01 15:25:34 +01002451 static int
2452cat_func_name(char_u *buf, size_t bufsize, ufunc_T *fp)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002453{
John Marriottb32800f2025-02-01 15:25:34 +01002454 int len;
2455
Bram Moolenaar0f769812020-09-12 18:32:34 +02002456 if (!func_is_global(fp))
John Marriottb32800f2025-02-01 15:25:34 +01002457 len = vim_snprintf((char *)buf, bufsize, "<SNR>%s", fp->uf_name + 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002458 else
John Marriottb32800f2025-02-01 15:25:34 +01002459 len = vim_snprintf((char *)buf, bufsize, "%s", fp->uf_name);
2460
2461 return (len >= (int)bufsize) ? (int)bufsize - 1 : len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002462}
2463
2464/*
2465 * Add a number variable "name" to dict "dp" with value "nr".
2466 */
2467 static void
2468add_nr_var(
2469 dict_T *dp,
2470 dictitem_T *v,
2471 char *name,
2472 varnumber_T nr)
2473{
2474 STRCPY(v->di_key, name);
2475 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002476 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002477 v->di_tv.v_type = VAR_NUMBER;
2478 v->di_tv.v_lock = VAR_FIXED;
2479 v->di_tv.vval.v_number = nr;
2480}
2481
2482/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002483 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002484 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002485 static void
2486free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002487{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002488 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002489
Bram Moolenaarca16c602022-09-06 18:57:08 +01002490 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002491 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002492 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002493
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002494 // When garbage collecting a funccall_T may be freed before the
2495 // function that references it, clear its uf_scoped field.
2496 // The function may have been redefined and point to another
2497 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002498 if (fp != NULL && fp->uf_scoped == fc)
2499 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002500 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002501 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002502
Bram Moolenaarca16c602022-09-06 18:57:08 +01002503 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002504 vim_free(fc);
2505}
2506
2507/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002508 * Free "fc" and what it contains.
2509 * Can be called only when "fc" is kept beyond the period of it called,
2510 * i.e. after cleanup_function_call(fc).
2511 */
2512 static void
2513free_funccal_contents(funccall_T *fc)
2514{
2515 listitem_T *li;
2516
2517 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002518 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002519
2520 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002521 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002522
2523 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002524 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002525 clear_tv(&li->li_tv);
2526
2527 free_funccal(fc);
2528}
2529
2530/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002531 * Handle the last part of returning from a function: free the local hashtable.
2532 * Unless it is still in use by a closure.
2533 */
2534 static void
2535cleanup_function_call(funccall_T *fc)
2536{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002537 int may_free_fc = fc->fc_refcount <= 0;
2538 int free_fc = TRUE;
2539
Bram Moolenaarca16c602022-09-06 18:57:08 +01002540 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002541
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002542 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002543 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2544 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002545 else
2546 free_fc = FALSE;
2547
2548 // If the a:000 list and the l: and a: dicts are not referenced and
2549 // there is no closure using it, we can free the funccall_T and what's
2550 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002551 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2552 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002553 else
2554 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002555 int todo;
2556 hashitem_T *hi;
2557 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002558
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002559 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002560
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002561 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002562 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002563 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002564 {
2565 if (!HASHITEM_EMPTY(hi))
2566 {
2567 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002568 di = HI2DI(hi);
2569 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002570 }
2571 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002572 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002573
Bram Moolenaarca16c602022-09-06 18:57:08 +01002574 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2575 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002576 else
2577 {
2578 listitem_T *li;
2579
2580 free_fc = FALSE;
2581
2582 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002583 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002584 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002585 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002586
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002587 if (free_fc)
2588 free_funccal(fc);
2589 else
2590 {
2591 static int made_copy = 0;
2592
2593 // "fc" is still in use. This can happen when returning "a:000",
2594 // assigning "l:" to a global variable or defining a closure.
2595 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002596 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002597 previous_funccal = fc;
2598
2599 if (want_garbage_collect)
2600 // If garbage collector is ready, clear count.
2601 made_copy = 0;
2602 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002603 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002604 // We have made a lot of copies, worth 4 Mbyte. This can happen
2605 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002606 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002607 // too much memory.
2608 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002609 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002610 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002611 }
2612}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002613
2614/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002615 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2616 */
2617 static int
2618numbered_function(char_u *name)
2619{
Keith Thompson184f71c2024-01-04 21:19:04 +01002620 return SAFE_isdigit(*name)
2621 || (name[0] == 'g' && name[1] == ':' && SAFE_isdigit(name[2]));
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002622}
2623
2624/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002625 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002626 * 1. ordinary names, function defined with :function or :def;
2627 * can start with "<SNR>123_" literally or with K_SPECIAL.
2628 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002629 * For the first we only count the name stored in func_hashtab as a reference,
2630 * using function() does not count as a reference, because the function is
2631 * looked up by name.
2632 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002633 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002634func_name_refcount(char_u *name)
2635{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002636 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002637}
2638
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639/*
2640 * Unreference "fc": decrement the reference count and free it when it
2641 * becomes zero. "fp" is detached from "fc".
2642 * When "force" is TRUE we are exiting.
2643 */
2644 static void
2645funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2646{
2647 funccall_T **pfc;
2648 int i;
2649
2650 if (fc == NULL)
2651 return;
2652
2653 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002654 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2655 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2656 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2657 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 {
2659 if (fc == *pfc)
2660 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002661 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 free_funccal_contents(fc);
2663 return;
2664 }
2665 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002666 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2667 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2668 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669}
2670
2671/*
2672 * Remove the function from the function hashtable. If the function was
2673 * deleted while it still has references this was already done.
2674 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2675 */
2676 static int
2677func_remove(ufunc_T *fp)
2678{
2679 hashitem_T *hi;
2680
2681 // Return if it was already virtually deleted.
2682 if (fp->uf_flags & FC_DEAD)
2683 return FALSE;
2684
2685 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002686 if (HASHITEM_EMPTY(hi))
2687 return FALSE;
2688
2689 // When there is a def-function index do not actually remove the
2690 // function, so we can find the index when defining the function again.
2691 // Do remove it when it's a copy.
2692 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002694 fp->uf_flags |= FC_DEAD;
2695 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002696 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002697 hash_remove(&func_hashtab, hi, "remove function");
2698 fp->uf_flags |= FC_DELETED;
2699 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002700}
2701
2702 static void
2703func_clear_items(ufunc_T *fp)
2704{
2705 ga_clear_strings(&(fp->uf_args));
2706 ga_clear_strings(&(fp->uf_def_args));
2707 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002709 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002710 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01002711 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002712
2713 // Increment the refcount of this function to avoid it being freed
2714 // recursively when the partial is freed.
2715 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002716 partial_unref(fp->uf_partial);
2717 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002718 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002719
2720#ifdef FEAT_LUA
2721 if (fp->uf_cb_free != NULL)
2722 {
2723 fp->uf_cb_free(fp->uf_cb_state);
2724 fp->uf_cb_free = NULL;
2725 }
2726
2727 fp->uf_cb_state = NULL;
2728 fp->uf_cb = NULL;
2729#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730#ifdef FEAT_PROFILE
2731 VIM_CLEAR(fp->uf_tml_count);
2732 VIM_CLEAR(fp->uf_tml_total);
2733 VIM_CLEAR(fp->uf_tml_self);
2734#endif
2735}
2736
2737/*
2738 * Free all things that a function contains. Does not free the function
2739 * itself, use func_free() for that.
2740 * When "force" is TRUE we are exiting.
2741 */
2742 static void
2743func_clear(ufunc_T *fp, int force)
2744{
2745 if (fp->uf_cleared)
2746 return;
2747 fp->uf_cleared = TRUE;
2748
2749 // clear this function
2750 func_clear_items(fp);
2751 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002752 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753}
2754
2755/*
2756 * Free a function and remove it from the list of functions. Does not free
2757 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002758 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002759 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002761 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002762func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763{
2764 // Only remove it when not done already, otherwise we would remove a newer
2765 // version of the function with the same name.
2766 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2767 func_remove(fp);
2768
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002769 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002770 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002771 if (fp->uf_dfunc_idx > 0)
2772 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002773 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002775 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002776 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002777 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778}
2779
2780/*
2781 * Free all things that a function contains and free the function itself.
2782 * When "force" is TRUE we are exiting.
2783 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002784 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785func_clear_free(ufunc_T *fp, int force)
2786{
2787 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002788 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2789 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002790 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002791 else
2792 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793}
2794
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002795/*
2796 * Copy already defined function "lambda" to a new function with name "global".
2797 * This is for when a compiled function defines a global function.
2798 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002799 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002800copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002801 char_u *lambda,
2802 char_u *global,
2803 loopvarinfo_T *loopvarinfo,
2804 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002805{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002806 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002807 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002808
2809 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002810 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002811 semsg(_(e_lambda_function_not_found_str), lambda);
2812 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002813 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002814
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002815 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002816 if (fp != NULL)
2817 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002818 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002819 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002820 return FAIL;
2821 }
2822
John Marriottb32800f2025-02-01 15:25:34 +01002823 fp = alloc_ufunc(global, STRLEN(global));
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002824 if (fp == NULL)
2825 return FAIL;
2826
2827 fp->uf_varargs = ufunc->uf_varargs;
2828 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2829 fp->uf_def_status = ufunc->uf_def_status;
2830 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2831 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2832 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2833 == FAIL
2834 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2835 goto failed;
2836
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002837 if (ufunc->uf_arg_types != NULL)
2838 {
2839 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2840 if (fp->uf_arg_types == NULL)
2841 goto failed;
2842 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2843 sizeof(type_T *) * fp->uf_args.ga_len);
2844 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002845 if (ufunc->uf_va_name != NULL)
2846 {
2847 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2848 if (fp->uf_va_name == NULL)
2849 goto failed;
2850 }
2851 fp->uf_ret_type = ufunc->uf_ret_type;
2852
2853 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002854
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002855 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002856
2857 // the referenced dfunc_T is now used one more time
2858 link_def_function(fp);
2859
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002860 // Create a partial to store the context of the function where it was
2861 // instantiated. Only needs to be done once. Do this on the original
2862 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002863 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2864 {
2865 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2866
2867 if (pt == NULL)
2868 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002869 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002870 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002871 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002872 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002873 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002874 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002875 }
2876
2877 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002878
2879failed:
2880 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002881 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002882}
2883
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002884static int funcdepth = 0;
2885
2886/*
2887 * Increment the function call depth count.
2888 * Return FAIL when going over 'maxfuncdepth'.
2889 * Otherwise return OK, must call funcdepth_decrement() later!
2890 */
2891 int
2892funcdepth_increment(void)
2893{
2894 if (funcdepth >= p_mfd)
2895 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002896 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002897 return FAIL;
2898 }
2899 ++funcdepth;
2900 return OK;
2901}
2902
2903 void
2904funcdepth_decrement(void)
2905{
2906 --funcdepth;
2907}
2908
2909/*
2910 * Get the current function call depth.
2911 */
2912 int
2913funcdepth_get(void)
2914{
2915 return funcdepth;
2916}
2917
2918/*
2919 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002920 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002921 * times as funcdepth_increment().
2922 */
2923 void
2924funcdepth_restore(int depth)
2925{
2926 funcdepth = depth;
2927}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002928
2929/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002930 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2931 * "rettv".
2932 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2933 * Returns NULL when allocation fails.
2934 */
2935 funccall_T *
2936create_funccal(ufunc_T *fp, typval_T *rettv)
2937{
2938 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2939
2940 if (fc == NULL)
2941 return NULL;
2942 fc->fc_caller = current_funccal;
2943 current_funccal = fc;
2944 fc->fc_func = fp;
2945 func_ptr_ref(fp);
2946 fc->fc_rettv = rettv;
2947 return fc;
2948}
2949
2950/*
2951 * To be called when returning from a compiled function; restores
2952 * current_funccal.
2953 */
2954 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002955remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002956{
2957 funccall_T *fc = current_funccal;
2958
2959 current_funccal = fc->fc_caller;
2960 free_funccal(fc);
2961}
2962
2963/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002964 * Call a user function.
2965 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002966 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002967call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002968 ufunc_T *fp, // pointer to function
2969 int argcount, // nr of args
2970 typval_T *argvars, // arguments
2971 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002972 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002973 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002974{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002975 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002976 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002977 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002978 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002979 funccall_T *fc;
2980 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002981 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002982 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002983 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002984 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002985 int i;
2986 int ai;
2987 int islambda = FALSE;
2988 char_u numbuf[NUMBUFLEN];
2989 char_u *name;
John Marriottb32800f2025-02-01 15:25:34 +01002990 size_t namelen;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002991 typval_T *tv_to_free[MAX_FUNC_ARGS];
2992 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002993#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002994 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002995#endif
ichizok7e5fe382023-04-15 13:17:50 +01002996 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002997
Bram Moolenaarb2049902021-01-24 12:53:53 +01002998#ifdef FEAT_PROFILE
2999 CLEAR_FIELD(profile_info);
3000#endif
3001
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003002 // If depth of calling is getting too high, don't execute the function.
3003 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003004 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003005 rettv->v_type = VAR_NUMBER;
3006 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003007 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003008 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003009
Bram Moolenaare38eab22019-12-05 21:50:01 +01003010 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003011
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01003012 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01003013 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003014 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003015 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003016 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01003017 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
3018 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003019 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01003020 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003021
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003022 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01003024#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01003025 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003026#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003027 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01003028#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003029 if (do_profiling == PROF_YES)
3030 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01003031#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003032 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003033 if (call_def_function(fp, argcount, argvars, 0,
3034 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
3035 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003036 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01003037#ifdef FEAT_PROFILE
3038 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01003039 || (caller != NULL && caller->uf_profiling)))
3040 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01003041#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01003042 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003043 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003044 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 }
3046
Bram Moolenaar38453522021-11-28 22:00:12 +00003047 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003048
3049 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01003050 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
3051 * variables with names up to VAR_SHORT_LEN long. This avoids having to
3052 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003053 */
3054 /*
3055 * Init l: variables.
3056 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01003057 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003058 if (selfdict != NULL)
3059 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003060 // Set l:self to "selfdict". Use "name" to avoid a warning from
3061 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01003062 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003063 name = v->di_key;
3064 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01003065 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003066 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003067 v->di_tv.v_type = VAR_DICT;
3068 v->di_tv.v_lock = 0;
3069 v->di_tv.vval.v_dict = selfdict;
3070 ++selfdict->dv_refcount;
3071 }
3072
3073 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003074 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003075 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003076 * Set a:000 to a list with room for the "..." arguments.
3077 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01003078 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003079 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01003080 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003081 (varnumber_T)(argcount >= fp->uf_args.ga_len
3082 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01003083 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003084 if ((fp->uf_flags & FC_NOARGS) == 0)
3085 {
3086 // Use "name" to avoid a warning from some compiler that checks the
3087 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01003088 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003089 name = v->di_key;
3090 STRCPY(name, "000");
3091 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003092 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003093 v->di_tv.v_type = VAR_LIST;
3094 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003095 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003096 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01003097 CLEAR_FIELD(fc->fc_l_varlist);
3098 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
3099 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003100
3101 /*
3102 * Set a:firstline to "firstline" and a:lastline to "lastline".
3103 * Set a:name to named arguments.
3104 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003105 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003106 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003107 if ((fp->uf_flags & FC_NOARGS) == 0)
3108 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003109 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
3110 "firstline", (varnumber_T)funcexe->fe_firstline);
3111 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
3112 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003113 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003114 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003115 {
3116 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003117 typval_T def_rettv;
3118 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003119
3120 ai = i - fp->uf_args.ga_len;
3121 if (ai < 0)
3122 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003123 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003124 name = FUNCARG(fp, i);
3125 if (islambda)
3126 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003127
3128 // evaluate named argument default expression
3129 isdefault = ai + fp->uf_def_args.ga_len >= 0
3130 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
3131 && argvars[i].vval.v_number == VVAL_NONE));
3132 if (isdefault)
3133 {
3134 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003135
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003136 def_rettv.v_type = VAR_NUMBER;
3137 def_rettv.vval.v_number = -1;
3138
3139 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
3140 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003141 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003142 {
3143 default_arg_err = 1;
3144 break;
3145 }
3146 }
John Marriottb32800f2025-02-01 15:25:34 +01003147
3148 namelen = STRLEN(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003149 }
3150 else
3151 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003152 if ((fp->uf_flags & FC_NOARGS) != 0)
3153 // Bail out if no a: arguments used (in lambda).
3154 break;
3155
Bram Moolenaare38eab22019-12-05 21:50:01 +01003156 // "..." argument a:1, a:2, etc.
John Marriottb32800f2025-02-01 15:25:34 +01003157 namelen = vim_snprintf((char *)numbuf, sizeof(numbuf), "%d", ai + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003158 name = numbuf;
3159 }
John Marriottb32800f2025-02-01 15:25:34 +01003160 if (fixvar_idx < FIXVAR_CNT && namelen <= VAR_SHORT_LEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003161 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003162 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003163 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003164 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003165 }
3166 else
3167 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003168 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003169 if (v == NULL)
3170 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003171 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003172 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003173
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003174 // Note: the values are copied directly to avoid alloc/free.
3175 // "argvars" must have VAR_FIXED for v_lock.
3176 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003177 v->di_tv.v_lock = VAR_FIXED;
3178
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003179 if (isdefault)
3180 // Need to free this later, no matter where it's stored.
3181 tv_to_free[tv_to_free_len++] = &v->di_tv;
3182
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003183 if (addlocal)
3184 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003185 // Named arguments should be accessed without the "a:" prefix in
3186 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003187 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003188 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003189 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003190 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003191 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003192
3193 if (ai >= 0 && ai < MAX_FUNC_ARGS)
3194 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003195 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003196
3197 li->li_tv = argvars[i];
3198 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003199 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003200 }
3201 }
3202
Bram Moolenaare38eab22019-12-05 21:50:01 +01003203 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003204 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003205
3206 if (fp->uf_flags & FC_SANDBOX)
3207 {
3208 using_sandbox = TRUE;
3209 ++sandbox;
3210 }
3211
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003212 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003213 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003214 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003215 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003216 ++no_wait_return;
3217 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003218
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003219 smsg(_("calling %s"), SOURCING_NAME);
3220 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003221 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003222 char_u buf[MSG_BUF_LEN];
3223 char_u numbuf2[NUMBUFLEN];
3224 char_u *tofree;
3225 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003226
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003227 msg_puts("(");
3228 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003229 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003230 if (i > 0)
3231 msg_puts(", ");
3232 if (argvars[i].v_type == VAR_NUMBER)
3233 msg_outnum((long)argvars[i].vval.v_number);
3234 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003235 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003236 // Do not want errors such as E724 here.
3237 ++emsg_off;
3238 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3239 --emsg_off;
3240 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003241 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003242 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003243 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003244 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3245 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003246 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003247 msg_puts((char *)s);
3248 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003249 }
3250 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003251 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003252 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003253 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003254 msg_puts("\n"); // don't overwrite this either
3255
3256 verbose_leave_scroll();
3257 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003258 }
3259#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003260 if (do_profiling == PROF_YES)
3261 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003262 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003263#endif
3264
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003265 // "legacy" does not apply to commands in the function
3266 sticky_cmdmod_flags = 0;
3267
Bram Moolenaar98aff652022-09-06 21:02:35 +01003268 // If called from a compiled :def function the execution context must be
3269 // hidden, any deferred functions need to be added to the function being
3270 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003271 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003272
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003273 save_current_sctx = current_sctx;
3274 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003275 save_did_emsg = did_emsg;
3276 did_emsg = FALSE;
3277
Shane Harper2d187892025-03-12 21:12:12 +01003278 if (default_arg_err && (fp->uf_flags & FC_ABORT || trylevel > 0 ))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003279 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003280 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003281 retval = FCERR_FAILED;
3282 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003283 else if (islambda)
3284 {
3285 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3286
3287 // A Lambda always has the command "return {expr}". It is much faster
3288 // to evaluate {expr} directly.
3289 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003290 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003291 --ex_nesting_level;
3292 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003293 else
3294 // call do_cmdline() to execute the lines
3295 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003296 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3297
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003298 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003299 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003300
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003301 if (RedrawingDisabled > 0)
3302 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003303
Bram Moolenaare38eab22019-12-05 21:50:01 +01003304 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003305 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3306 {
3307 clear_tv(rettv);
3308 rettv->v_type = VAR_NUMBER;
3309 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003310
3311 // In corner cases returning a "failed" value is not backwards
3312 // compatible. Only do this for Vim9 script.
3313 if (in_vim9script())
3314 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003315 }
3316
3317#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003318 if (do_profiling == PROF_YES)
3319 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003320 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003321
3322 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3323 profile_may_end_func(&profile_info, fp, caller);
3324 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003325#endif
3326
Bram Moolenaare38eab22019-12-05 21:50:01 +01003327 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003328 if (p_verbose >= 12)
3329 {
3330 ++no_wait_return;
3331 verbose_enter_scroll();
3332
3333 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003334 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003335 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003336 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003337 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003338 else
3339 {
3340 char_u buf[MSG_BUF_LEN];
3341 char_u numbuf2[NUMBUFLEN];
3342 char_u *tofree;
3343 char_u *s;
3344
Bram Moolenaare38eab22019-12-05 21:50:01 +01003345 // The value may be very long. Skip the middle part, so that we
3346 // have some idea how it starts and ends. smsg() would always
3347 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003348 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003349 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003350 --emsg_off;
3351 if (s != NULL)
3352 {
3353 if (vim_strsize(s) > MSG_BUF_CLEN)
3354 {
3355 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3356 s = buf;
3357 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003358 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003359 vim_free(tofree);
3360 }
3361 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003362 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003363
3364 verbose_leave_scroll();
3365 --no_wait_return;
3366 }
3367
ichizok7e5fe382023-04-15 13:17:50 +01003368 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003369 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003370 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003371 restore_current_ectx(save_current_ectx);
3372
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003373#ifdef FEAT_PROFILE
3374 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003375 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003376#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003377 if (using_sandbox)
3378 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003379 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003380
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003381 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003382 {
3383 ++no_wait_return;
3384 verbose_enter_scroll();
3385
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003386 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003387 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003388
3389 verbose_leave_scroll();
3390 --no_wait_return;
3391 }
3392
3393 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003394 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003395 for (i = 0; i < tv_to_free_len; ++i)
3396 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003397 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003398
3399 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003400}
3401
3402/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003403 * Check the argument count for user function "fp".
3404 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3405 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003406 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003407check_user_func_argcount(ufunc_T *fp, int argcount)
3408{
3409 int regular_args = fp->uf_args.ga_len;
3410
3411 if (argcount < regular_args - fp->uf_def_args.ga_len)
3412 return FCERR_TOOFEW;
3413 else if (!has_varargs(fp) && argcount > regular_args)
3414 return FCERR_TOOMANY;
3415 return FCERR_UNKNOWN;
3416}
3417
3418/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003420 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003421 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422call_user_func_check(
3423 ufunc_T *fp,
3424 int argcount,
3425 typval_T *argvars,
3426 typval_T *rettv,
3427 funcexe_T *funcexe,
3428 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003429{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003430 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003431
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003432#ifdef FEAT_LUA
3433 if (fp->uf_flags & FC_CFUNC)
3434 {
3435 cfunc_T cb = fp->uf_cb;
3436
3437 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3438 }
3439#endif
3440
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003441 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3442 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003443 error = check_user_func_argcount(fp, argcount);
3444 if (error != FCERR_UNKNOWN)
3445 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003446
Bram Moolenaar50824712020-12-20 21:10:17 +01003447 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003448 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003450 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003452 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 int did_save_redo = FALSE;
3454 save_redo_T save_redo;
3455
3456 /*
3457 * Call the user function.
3458 * Save and restore search patterns, script variables and
3459 * redo buffer.
3460 */
3461 save_search_patterns();
3462 if (!ins_compl_active())
3463 {
3464 saveRedobuff(&save_redo);
3465 did_save_redo = TRUE;
3466 }
3467 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003468 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003469 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003470 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3471 // Function was unreferenced while being used, free it now.
3472 func_clear_free(fp, FALSE);
3473 if (did_save_redo)
3474 restoreRedobuff(&save_redo);
3475 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003476 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003477
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003479}
3480
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003481static funccal_entry_T *funccal_stack = NULL;
3482
3483/*
3484 * Save the current function call pointer, and set it to NULL.
3485 * Used when executing autocommands and for ":source".
3486 */
3487 void
3488save_funccal(funccal_entry_T *entry)
3489{
3490 entry->top_funccal = current_funccal;
3491 entry->next = funccal_stack;
3492 funccal_stack = entry;
3493 current_funccal = NULL;
3494}
3495
3496 void
3497restore_funccal(void)
3498{
3499 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003500 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003501 else
3502 {
3503 current_funccal = funccal_stack->top_funccal;
3504 funccal_stack = funccal_stack->next;
3505 }
3506}
3507
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003508 funccall_T *
3509get_current_funccal(void)
3510{
3511 return current_funccal;
3512}
3513
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003514/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003515 * Return TRUE when currently at the script level:
3516 * - not in a function
3517 * - not executing an autocommand
3518 * Note that when an autocommand sources a script the result is FALSE;
3519 */
3520 int
3521at_script_level(void)
3522{
3523 return current_funccal == NULL && autocmd_match == NULL;
3524}
3525
3526/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003527 * Mark all functions of script "sid" as deleted.
3528 */
3529 void
3530delete_script_functions(int sid)
3531{
3532 hashitem_T *hi;
3533 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003534 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003535 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003536 size_t len;
3537
3538 buf[0] = K_SPECIAL;
3539 buf[1] = KS_EXTRA;
3540 buf[2] = (int)KE_SNR;
John Marriottb32800f2025-02-01 15:25:34 +01003541 len = 3 + vim_snprintf((char *)buf + 3, sizeof(buf) - 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003542
Bram Moolenaarce658352020-07-31 23:47:12 +02003543 while (todo > 0)
3544 {
3545 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003546 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003547 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003548 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003549 fp = HI2UF(hi);
3550 if (STRNCMP(fp->uf_name, buf, len) == 0)
3551 {
3552 int changed = func_hashtab.ht_changed;
3553
3554 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003555
3556 if (fp->uf_calls > 0)
3557 {
3558 // Function is executing, don't free it but do remove
3559 // it from the hashtable.
3560 if (func_remove(fp))
3561 fp->uf_refcount--;
3562 }
3563 else
3564 {
3565 func_clear(fp, TRUE);
3566 // When clearing a function another function can be
3567 // cleared as a side effect. When that happens start
3568 // over.
3569 if (changed != func_hashtab.ht_changed)
3570 break;
3571 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003572 }
3573 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003574 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003575 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003576}
3577
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003578#if defined(EXITFREE) || defined(PROTO)
3579 void
3580free_all_functions(void)
3581{
3582 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003583 ufunc_T *fp;
3584 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003585 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003586 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003587
Bram Moolenaare38eab22019-12-05 21:50:01 +01003588 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003589 while (current_funccal != NULL)
3590 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003591 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003592 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003593 if (current_funccal == NULL && funccal_stack != NULL)
3594 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003595 }
3596
Bram Moolenaare38eab22019-12-05 21:50:01 +01003597 // First clear what the functions contain. Since this may lower the
3598 // reference count of a function, it may also free a function and change
3599 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003600 while (todo > 0)
3601 {
3602 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003603 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003604 if (!HASHITEM_EMPTY(hi))
3605 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 // clear the def function index now
3607 fp = HI2UF(hi);
3608 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003609 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610
Bram Moolenaare38eab22019-12-05 21:50:01 +01003611 // Only free functions that are not refcounted, those are
3612 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003613 if (func_name_refcount(fp->uf_name))
3614 ++skipped;
3615 else
3616 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003617 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003618 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003619 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003620 {
3621 skipped = 0;
3622 break;
3623 }
3624 }
3625 --todo;
3626 }
3627 }
3628
Bram Moolenaare38eab22019-12-05 21:50:01 +01003629 // Now actually free the functions. Need to start all over every time,
3630 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003631 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003632 while (func_hashtab.ht_used > skipped)
3633 {
3634 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003635 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003636 if (!HASHITEM_EMPTY(hi))
3637 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003638 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003639 // Only free functions that are not refcounted, those are
3640 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003641 fp = HI2UF(hi);
3642 if (func_name_refcount(fp->uf_name))
3643 ++skipped;
3644 else
3645 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003646 if (func_free(fp, FALSE) == OK)
3647 {
3648 skipped = 0;
3649 break;
3650 }
3651 // did not actually free it
3652 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003653 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003654 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003655 }
3656 if (skipped == 0)
3657 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658
3659 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003660}
3661#endif
3662
3663/*
3664 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003665 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3666 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003667 * "len" is the length of "name", or -1 for NUL terminated.
3668 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003670builtin_function(char_u *name, int len)
3671{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003672 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003673
Bram Moolenaara26b9702020-04-18 19:53:28 +02003674 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003675 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003676 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3677 {
3678 if (name[i] == AUTOLOAD_CHAR)
3679 return FALSE;
3680 if (!eval_isnamec(name[i]))
3681 {
3682 // "name.something" is not a builtin function
3683 if (name[i] == '.')
3684 return FALSE;
3685 break;
3686 }
3687 }
3688 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003689}
3690
3691 int
3692func_call(
3693 char_u *name,
3694 typval_T *args,
3695 partial_T *partial,
3696 dict_T *selfdict,
3697 typval_T *rettv)
3698{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003699 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003700 listitem_T *item;
3701 typval_T argv[MAX_FUNC_ARGS + 1];
3702 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003703 int r = 0;
3704
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003705 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003706 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003707 {
3708 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3709 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003710 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003711 break;
3712 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003713 // Make a copy of each argument. This is needed to be able to set
3714 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003715 copy_tv(&item->li_tv, &argv[argc++]);
3716 }
3717
3718 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003719 {
3720 funcexe_T funcexe;
3721
Bram Moolenaara80faa82020-04-12 19:37:17 +02003722 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003723 funcexe.fe_firstline = curwin->w_cursor.lnum;
3724 funcexe.fe_lastline = curwin->w_cursor.lnum;
3725 funcexe.fe_evaluate = TRUE;
3726 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003727 if (partial != NULL)
3728 {
3729 funcexe.fe_object = partial->pt_obj;
3730 if (funcexe.fe_object != NULL)
3731 ++funcexe.fe_object->obj_refcount;
3732 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003733 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003734 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3735 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003736
Bram Moolenaare38eab22019-12-05 21:50:01 +01003737 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003738 while (argc > 0)
3739 clear_tv(&argv[--argc]);
3740
3741 return r;
3742}
3743
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003744static int callback_depth = 0;
3745
3746 int
3747get_callback_depth(void)
3748{
3749 return callback_depth;
3750}
3751
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003752/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003753 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003754 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003755 */
3756 int
3757call_callback(
3758 callback_T *callback,
3759 int len, // length of "name" or -1 to use strlen()
3760 typval_T *rettv, // return value goes here
3761 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003762 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003763 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003764{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003765 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003766 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003767
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003768 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3769 return FAIL;
Christian Brabandt47510f32023-10-15 09:56:16 +02003770
zeertzjqfe583b12023-12-21 16:59:26 +01003771 if (callback_depth > p_mfd)
Christian Brabandt47510f32023-10-15 09:56:16 +02003772 {
3773 emsg(_(e_command_too_recursive));
3774 return FAIL;
3775 }
3776
Bram Moolenaara80faa82020-04-12 19:37:17 +02003777 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003778 funcexe.fe_evaluate = TRUE;
3779 funcexe.fe_partial = callback->cb_partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003780 if (callback->cb_partial != NULL)
3781 {
3782 funcexe.fe_object = callback->cb_partial->pt_obj;
3783 if (funcexe.fe_object != NULL)
3784 ++funcexe.fe_object->obj_refcount;
3785 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003786 ++callback_depth;
3787 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3788 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003789
3790 // When a :def function was called that uses :try an error would be turned
3791 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003792 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003793 {
3794 need_rethrow = FALSE;
3795 handle_did_throw();
3796 }
3797
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003798 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003799}
3800
3801/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003802 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003803 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003804 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3805 */
3806 varnumber_T
3807call_callback_retnr(
3808 callback_T *callback,
3809 int argcount, // number of "argvars"
3810 typval_T *argvars) // vars for arguments, must have "argcount"
3811 // PLUS ONE elements!
3812{
3813 typval_T rettv;
3814 varnumber_T retval;
3815
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003816 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003817 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003818
3819 retval = tv_get_number_chk(&rettv, NULL);
3820 clear_tv(&rettv);
3821 return retval;
3822}
3823
3824/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 * Give an error message for the result of a function.
3826 * Nothing if "error" is FCERR_NONE.
3827 */
3828 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003829user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830{
3831 switch (error)
3832 {
3833 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003834 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003835 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003836 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003837 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838 break;
3839 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003840 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 break;
3842 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003843 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 break;
3845 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003846 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 break;
3848 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003849 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 break;
3851 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003852 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 break;
3854 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003855 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3856 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003858 case FCERR_OTHER:
3859 case FCERR_FAILED:
3860 // assume the error message was already given
3861 break;
3862 case FCERR_NONE:
3863 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 }
3865}
3866
3867/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003868 * Check the argument types "argvars[argcount]" for "name" using the
3869 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3870 * is already included in "argvars[]".
3871 * Will do nothing if "funcexe->fe_check_type" is NULL or
3872 * "funcexe->fe_evaluate" is FALSE;
3873 * Returns an FCERR_ value.
3874 */
3875 static funcerror_T
3876may_check_argument_types(
3877 funcexe_T *funcexe,
3878 typval_T *argvars,
3879 int argcount,
3880 int base_included,
3881 char_u *name)
3882{
3883 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3884 {
3885 // Check that the argument types are OK for the types of the funcref.
3886 if (check_argument_types(funcexe->fe_check_type,
3887 argvars, argcount,
3888 base_included ? NULL : funcexe->fe_basetv,
3889 name) == FAIL)
3890 return FCERR_OTHER;
3891 }
3892 return FCERR_NONE;
3893}
3894
3895/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003896 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003897 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003898 * Return FAIL when the function can't be called, OK otherwise.
3899 * Also returns OK when an error was encountered while executing the function.
3900 */
3901 int
3902call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003903 char_u *funcname, // name of the function
3904 int len, // length of "name" or -1 to use strlen()
3905 typval_T *rettv, // return value goes here
3906 int argcount_in, // number of "argvars"
3907 typval_T *argvars_in, // vars for arguments, must have "argcount"
3908 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003909 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003910{
3911 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003912 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003913 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003914 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003915 char_u fname_buf[FLEN_FIXED + 1];
3916 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003917 char_u *fname = NULL;
3918 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003919 int argcount = argcount_in;
3920 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003921 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003922 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003923 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003924 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003925 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003926 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003927 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003928 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003929
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003930 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3931 // even when call_func() returns FAIL.
3932 rettv->v_type = VAR_UNKNOWN;
3933
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003934 if (partial != NULL)
3935 fp = partial->pt_func;
3936 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003937 fp = funcexe->fe_ufunc;
3938
3939 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003940 {
3941 // Make a copy of the name, if it comes from a funcref variable it
3942 // could be changed or deleted in the called function.
3943 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3944 if (name == NULL)
3945 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003946
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003947 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3948 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003949
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003950 if (funcexe->fe_doesrange != NULL)
3951 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003952
3953 if (partial != NULL)
3954 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003955 // When the function has a partial with a dict and there is a dict
3956 // argument, use the dict argument. That is backwards compatible.
3957 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003958 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003959 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003960 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003961 {
3962 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003963 {
3964 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3965 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003966 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003967 goto theend;
3968 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003969 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003970 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003971 for (i = 0; i < argcount_in; ++i)
3972 argv[i + argv_clear] = argvars_in[i];
3973 argvars = argv;
3974 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003975
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003976 if (funcexe->fe_check_type != NULL
3977 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003978 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003979 // Now funcexe->fe_check_type is missing the added arguments,
3980 // make a copy of the type with the correction.
3981 check_type = *funcexe->fe_check_type;
3982 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003983 check_type.tt_args = check_type_args;
3984 CLEAR_FIELD(check_type_args);
3985 for (i = 0; i < check_type.tt_argcount; ++i)
3986 check_type_args[i + partial->pt_argc] =
3987 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003988 check_type.tt_argcount += partial->pt_argc;
3989 check_type.tt_min_argcount += partial->pt_argc;
3990 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003991 }
3992 }
3993
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003994 if (error == FCERR_NONE)
3995 // check the argument types if possible
3996 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
3997 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003998
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003999 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004000 {
4001 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02004002 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004003
Bram Moolenaar333894b2020-08-01 18:53:07 +02004004 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02004005 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02004006 {
4007 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004008 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02004009 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004010
Bram Moolenaare38eab22019-12-05 21:50:01 +01004011 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004012 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01004013 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004014
Bram Moolenaarf10806b2020-04-02 18:34:35 +02004015 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004016 {
4017 /*
4018 * User defined function.
4019 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02004020 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004021 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004022 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004023 if (fp != NULL && !is_global && in_vim9script()
4024 && func_requires_g_prefix(fp))
4025 // In Vim9 script g: is required to find a global
4026 // non-autoload function.
4027 fp = NULL;
4028 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004029
Bram Moolenaare38eab22019-12-05 21:50:01 +01004030 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004031 if (fp == NULL
4032 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004033 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004034 && !aborting())
4035 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004036 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004037 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004038 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004039 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004040 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
4041 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004042 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004043 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004044 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02004045 if (fp == NULL)
4046 {
4047 char_u *p = untrans_function_name(rfname);
4048
4049 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02004050 // Don't do this if the name starts with "s:".
4051 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004052 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004053 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004054
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004055 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01004056 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004057 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004058 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004059 int need_arg_check = FALSE;
4060 if (funcexe->fe_check_type == NULL)
4061 {
4062 funcexe->fe_check_type = fp->uf_func_type;
4063 need_arg_check = TRUE;
4064 }
4065
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004066 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004067 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01004068 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004069 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004070 argv_clear, fp);
4071 need_arg_check = TRUE;
4072 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02004073
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004074 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004075 {
4076 // Method call: base->Method()
4077 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004078 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004079 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004080 argvars = argv;
4081 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004082 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004083 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004084
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004085 // Check the argument types now that the function type and all
4086 // argument values are known, if not done above.
4087 if (need_arg_check)
4088 error = may_check_argument_types(funcexe, argvars, argcount,
4089 TRUE, (name != NULL) ? name : funcname);
4090 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
4091 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004093 }
4094 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004095 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004096 {
4097 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004098 * expr->method(): Find the method name in the table, call its
4099 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02004100 */
4101 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004102 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004103 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004104 else
4105 {
4106 /*
4107 * Find the function name in the table, call its implementation.
4108 */
4109 error = call_internal_func(fname, argcount, argvars, rettv);
4110 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02004111
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004112 /*
4113 * The function call (or "FuncUndefined" autocommand sequence) might
4114 * have been aborted by an error, an interrupt, or an explicitly thrown
4115 * exception that has not been caught so far. This situation can be
4116 * tested for by calling aborting(). For an error in an internal
4117 * function or for the "E132" error in call_user_func(), however, the
4118 * throw point at which the "force_abort" flag (temporarily reset by
4119 * emsg()) is normally updated has not been reached yet. We need to
4120 * update that flag first to make aborting() reliable.
4121 */
4122 update_force_abort();
4123 }
Bram Moolenaaref140542019-12-31 21:27:13 +01004124 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004125 ret = OK;
4126
Bram Moolenaar4c054e92019-11-10 00:13:50 +01004127theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004128 /*
4129 * Report an error unless the argument evaluation or function call has been
4130 * cancelled due to an aborting error, an interrupt, or an exception.
4131 */
4132 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004133 user_func_error(error, (name != NULL) ? name : funcname,
4134 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004135
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004136 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004137 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004138 clear_tv(&argv[--argv_clear + argv_base]);
4139
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004140 vim_free(tofree);
4141 vim_free(name);
4142
4143 return ret;
4144}
4145
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004146/*
4147 * Call a function without arguments, partial or dict.
4148 * This is like call_func() when the call is only "FuncName()".
4149 * To be used by "expr" options.
4150 * Returns NOTDONE when the function could not be found.
4151 */
4152 int
4153call_simple_func(
4154 char_u *funcname, // name of the function
zeertzjqc1ed7882024-08-01 22:46:54 +02004155 size_t len, // length of "name"
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004156 typval_T *rettv) // return value goes here
4157{
4158 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00004159 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004160 char_u fname_buf[FLEN_FIXED + 1];
4161 char_u *tofree = NULL;
4162 char_u *name;
4163 char_u *fname;
4164 char_u *rfname;
4165 int is_global = FALSE;
4166 ufunc_T *fp;
4167
4168 rettv->v_type = VAR_NUMBER; // default rettv is number zero
4169 rettv->vval.v_number = 0;
4170
4171 // Make a copy of the name, an option can be changed in the function.
4172 name = vim_strnsave(funcname, len);
4173 if (name == NULL)
4174 return ret;
4175
4176 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
4177
4178 // Skip "g:" before a function name.
4179 if (fname[0] == 'g' && fname[1] == ':')
4180 {
4181 is_global = TRUE;
4182 rfname = fname + 2;
4183 }
4184 else
4185 rfname = fname;
4186 fp = find_func(rfname, is_global);
4187 if (fp != NULL && !is_global && in_vim9script()
4188 && func_requires_g_prefix(fp))
4189 // In Vim9 script g: is required to find a global non-autoload
4190 // function.
4191 fp = NULL;
4192 if (fp == NULL)
4193 ret = NOTDONE;
4194 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
4195 error = FCERR_DELETED;
4196 else if (fp != NULL)
4197 {
4198 typval_T argvars[1];
4199 funcexe_T funcexe;
4200
4201 argvars[0].v_type = VAR_UNKNOWN;
4202 CLEAR_FIELD(funcexe);
4203 funcexe.fe_evaluate = TRUE;
4204
4205 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
4206 if (error == FCERR_NONE)
4207 ret = OK;
4208 }
4209
4210 user_func_error(error, name, FALSE);
4211 vim_free(tofree);
4212 vim_free(name);
4213
4214 return ret;
4215}
4216
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004217 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218printable_func_name(ufunc_T *fp)
4219{
4220 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4221}
4222
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004223/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004224 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4225 * TRUE. Otherwise return FALSE.
4226 */
4227 static int
4228function_list_modified(int prev_ht_changed)
4229{
4230 if (prev_ht_changed != func_hashtab.ht_changed)
4231 {
4232 emsg(_(e_function_list_was_modified));
4233 return TRUE;
4234 }
4235 return FALSE;
4236}
4237
4238/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004239 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004240 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004241 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004242list_func_head(ufunc_T *fp, int indent)
4243{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004244 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004245 int j;
4246
4247 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004248
4249 // a timer at the more prompt may have deleted the function
4250 if (function_list_modified(prev_ht_changed))
4251 return FAIL;
4252
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004253 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004254 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004255 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004256 msg_puts("def ");
4257 else
4258 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004260 msg_putchar('(');
4261 for (j = 0; j < fp->uf_args.ga_len; ++j)
4262 {
4263 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004264 msg_puts(", ");
4265 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 if (fp->uf_arg_types != NULL)
4267 {
4268 char *tofree;
4269
4270 msg_puts(": ");
4271 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4272 vim_free(tofree);
4273 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004274 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4275 {
4276 msg_puts(" = ");
4277 msg_puts(((char **)(fp->uf_def_args.ga_data))
4278 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4279 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004280 }
4281 if (fp->uf_varargs)
4282 {
4283 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004284 msg_puts(", ");
4285 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004286 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 if (fp->uf_va_name != NULL)
4288 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004289 if (!fp->uf_varargs)
4290 {
4291 if (j)
4292 msg_puts(", ");
4293 msg_puts("...");
4294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004296 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297 {
4298 char *tofree;
4299
4300 msg_puts(": ");
4301 msg_puts(type_name(fp->uf_va_type, &tofree));
4302 vim_free(tofree);
4303 }
4304 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004305 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004306
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004307 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004308 {
4309 if (fp->uf_ret_type != &t_void)
4310 {
4311 char *tofree;
4312
4313 msg_puts(": ");
4314 msg_puts(type_name(fp->uf_ret_type, &tofree));
4315 vim_free(tofree);
4316 }
4317 }
4318 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004319 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004320 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004321 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004322 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004323 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004324 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004325 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004326 msg_clr_eos();
4327 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004328 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004329
4330 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004331}
4332
4333/*
4334 * Get a function name, translating "<SID>" and "<SNR>".
4335 * Also handles a Funcref in a List or Dictionary.
4336 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004337 * Set "*is_global" to TRUE when the function must be global, unless
4338 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004339 * flags:
4340 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004341 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004342 * TFN_QUIET: be quiet
4343 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004344 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004345 * Advances "pp" to just after the function name (if no error).
4346 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004347 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004348trans_function_name(
4349 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004350 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004351 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004352 int flags)
4353{
4354 return trans_function_name_ext(pp, is_global, skip, flags,
4355 NULL, NULL, NULL, NULL);
4356}
4357
4358/*
4359 * trans_function_name() with extra arguments.
4360 * "fdp", "partial", "type" and "ufunc" can be NULL.
4361 */
Yegappan Lakshmanana04003a2024-10-27 21:54:11 +01004362 static char_u *
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004363trans_function_name_ext(
4364 char_u **pp,
4365 int *is_global,
4366 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004367 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004368 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004369 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004370 type_T **type, // return: type of funcref
4371 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004372{
4373 char_u *name = NULL;
4374 char_u *start;
4375 char_u *end;
4376 int lead;
4377 char_u sid_buf[20];
4378 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004380 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004381 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004382 int vim9script = in_vim9script();
4383 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004384
4385 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004386 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004387 start = *pp;
4388
Bram Moolenaare38eab22019-12-05 21:50:01 +01004389 // Check for hard coded <SNR>: already translated function ID (from a user
4390 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004391 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4392 && (*pp)[2] == (int)KE_SNR)
4393 {
4394 *pp += 3;
4395 len = get_id_len(pp) + 3;
4396 return vim_strnsave(start, len);
4397 }
4398
Bram Moolenaare38eab22019-12-05 21:50:01 +01004399 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4400 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004401 lead = eval_fname_script(start);
4402 if (lead > 2)
4403 start += lead;
4404
Bram Moolenaare38eab22019-12-05 21:50:01 +01004405 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004406 end = get_lval(start, NULL, &lv, FALSE, skip,
4407 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004408 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004409 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004410 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004411 {
4412 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004413 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004414 goto theend;
4415 }
4416 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4417 {
4418 /*
4419 * Report an invalid expression in braces, unless the expression
4420 * evaluation has been cancelled due to an aborting error, an
4421 * interrupt, or an exception.
4422 */
4423 if (!aborting())
4424 {
4425 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004426 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004427 }
4428 else
4429 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4430 goto theend;
4431 }
4432
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004433 if (lv.ll_ufunc != NULL)
4434 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004435 if (ufunc != NULL)
4436 *ufunc = lv.ll_ufunc;
John Marriottb32800f2025-02-01 15:25:34 +01004437 name = vim_strnsave(lv.ll_ufunc->uf_name, lv.ll_ufunc->uf_namelen);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004438 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004439 goto theend;
4440 }
4441
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004442 if (lv.ll_tv != NULL)
4443 {
4444 if (fdp != NULL)
4445 {
4446 fdp->fd_dict = lv.ll_dict;
4447 fdp->fd_newkey = lv.ll_newkey;
4448 lv.ll_newkey = NULL;
4449 fdp->fd_di = lv.ll_di;
4450 }
4451 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4452 {
4453 name = vim_strsave(lv.ll_tv->vval.v_string);
4454 *pp = end;
4455 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004456 else if (lv.ll_tv->v_type == VAR_CLASS
4457 && lv.ll_tv->vval.v_class != NULL)
4458 {
4459 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4460 *pp = end;
4461 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004462 else if (lv.ll_tv->v_type == VAR_PARTIAL
4463 && lv.ll_tv->vval.v_partial != NULL)
4464 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004465 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004466 *pp = end;
4467 if (partial != NULL)
4468 *partial = lv.ll_tv->vval.v_partial;
4469 }
4470 else
4471 {
4472 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4473 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004474 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004475 else
4476 *pp = end;
4477 name = NULL;
4478 }
4479 goto theend;
4480 }
4481
4482 if (lv.ll_name == NULL)
4483 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004484 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004485 *pp = end;
4486 goto theend;
4487 }
4488
Bram Moolenaare38eab22019-12-05 21:50:01 +01004489 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004490 if (lv.ll_exp_name != NULL)
4491 {
4492 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004493 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004494 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004495 if (name == lv.ll_exp_name)
4496 name = NULL;
4497 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004498 else if (lv.ll_sid > 0)
4499 {
4500 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4501 int cc = *lv.ll_name_end;
4502
4503 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4504 *lv.ll_name_end = NUL;
4505 if (si->sn_autoload_prefix != NULL)
4506 {
4507 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4508 }
4509 else
4510 {
4511 sid_buf[0] = K_SPECIAL;
4512 sid_buf[1] = KS_EXTRA;
4513 sid_buf[2] = (int)KE_SNR;
4514 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004515 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004516 name = concat_str(sid_buf, lv.ll_name);
4517 }
4518 *lv.ll_name_end = cc;
4519 *pp = end;
4520 goto theend;
4521 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004522 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004523 {
4524 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004525 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004526 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004527 if (name == *pp)
4528 name = NULL;
4529 }
4530 if (name != NULL)
4531 {
4532 name = vim_strsave(name);
4533 *pp = end;
John Marriottb32800f2025-02-01 15:25:34 +01004534 if (name != NULL && STRNCMP(name, "<SNR>", 5) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004535 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004536 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004537 name[0] = K_SPECIAL;
4538 name[1] = KS_EXTRA;
4539 name[2] = (int)KE_SNR;
4540 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4541 }
4542 goto theend;
4543 }
4544
4545 if (lv.ll_exp_name != NULL)
4546 {
4547 len = (int)STRLEN(lv.ll_exp_name);
4548 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4549 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4550 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004551 // When there was "s:" already or the name expanded to get a
4552 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004553 lv.ll_name += 2;
4554 len -= 2;
4555 lead = 2;
4556 }
4557 }
4558 else
4559 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004560 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004561 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004562 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004563 if (lv.ll_name[0] == 'g')
4564 {
4565 if (is_global != NULL)
4566 {
4567 *is_global = TRUE;
4568 }
4569 else
4570 {
4571 // dropping "g:" without setting "is_global" won't work in
4572 // Vim9script, put it back later
4573 prefix_g = TRUE;
4574 extra = 2;
4575 }
4576 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004577 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004578 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004579 len = (int)(end - lv.ll_name);
4580 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004581 if (len <= 0)
4582 {
4583 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004584 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004585 goto theend;
4586 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004587
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004588 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004589 // starts with a lower case character: dict.func(). Or when in a class.
4590 vim9_local = ASCII_ISUPPER(*start) && vim9script
4591 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004593 /*
4594 * Copy the function name to allocated memory.
4595 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4596 * Accept <SNR>123_name() outside a script.
4597 */
4598 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004599 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004600 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004601 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004602 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004603 {
Kota Kato948a3892022-08-16 16:09:59 +01004604 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4605 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004606 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004607 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004608 goto theend;
4609 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004611 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004612 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004614 || eval_fname_sid(*pp))
4615 {
John Marriottb32800f2025-02-01 15:25:34 +01004616 size_t sid_buflen;
4617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004619 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004620 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004621 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004622 goto theend;
4623 }
John Marriottb32800f2025-02-01 15:25:34 +01004624 sid_buflen = vim_snprintf((char *)sid_buf, sizeof(sid_buf), "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004625 if (vim9_local)
John Marriottb32800f2025-02-01 15:25:34 +01004626 extra = 3 + (int)sid_buflen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627 else
John Marriottb32800f2025-02-01 15:25:34 +01004628 lead += (int)sid_buflen;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004629 }
4630 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004631 // The function name must start with an upper case letter (unless it is a
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004632 // Vim9 class new() function or a Vim9 class private method or one of the
4633 // supported Vim9 object builtin functions)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004634 else if (!(flags & TFN_INT)
4635 && (builtin_function(lv.ll_name, len)
4636 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004637 && !((flags & TFN_IN_CLASS)
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004638 && (is_valid_builtin_obj_methodname(lv.ll_name)
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004639 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004640 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004641 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004642 : e_function_name_must_start_with_capital_or_s_str),
4643 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004644 goto theend;
4645 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004646 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004647 {
4648 char_u *cp = vim_strchr(lv.ll_name, ':');
4649
4650 if (cp != NULL && cp < end)
4651 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004652 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004653 goto theend;
4654 }
4655 }
4656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004658 if (name != NULL)
4659 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004660 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004661 {
4662 name[0] = K_SPECIAL;
4663 name[1] = KS_EXTRA;
4664 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004665 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004666 STRCPY(name + 3, sid_buf);
4667 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004668 else if (prefix_g)
4669 {
4670 name[0] = 'g';
4671 name[1] = ':';
4672 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004673 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4674 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004675 }
4676 *pp = end;
4677
4678theend:
4679 clear_lval(&lv);
4680 return name;
4681}
4682
4683/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004684 * Assuming "name" is the result of trans_function_name() and it was prefixed
4685 * to use the script-local name, return the unmodified name (points into
4686 * "name"). Otherwise return NULL.
4687 * This can be used to first search for a script-local function and fall back
4688 * to the global function if not found.
4689 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004690 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004691untrans_function_name(char_u *name)
4692{
4693 char_u *p;
4694
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004695 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004696 {
4697 p = vim_strchr(name, '_');
4698 if (p != NULL)
4699 return p + 1;
4700 }
4701 return NULL;
4702}
4703
4704/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004705 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4706 * current script ID and returns the expanded function name. The caller should
4707 * free the returned name. If not called from a script context or the function
4708 * name doesn't start with these prefixes, then returns NULL.
4709 * This doesn't check whether the script-local function exists or not.
4710 */
4711 char_u *
4712get_scriptlocal_funcname(char_u *funcname)
4713{
4714 char sid_buf[25];
John Marriottb32800f2025-02-01 15:25:34 +01004715 size_t sid_buflen;
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004716 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004717 char_u *newname;
John Marriottb32800f2025-02-01 15:25:34 +01004718 size_t newnamesize;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004719 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004720
4721 if (funcname == NULL)
4722 return NULL;
4723
4724 if (STRNCMP(funcname, "s:", 2) != 0
4725 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004726 {
4727 ufunc_T *ufunc;
4728
4729 // The function name does not have a script-local prefix. Try finding
4730 // it when in a Vim9 script and there is no "g:" prefix.
4731 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4732 return NULL;
4733 ufunc = find_func(funcname, FALSE);
4734 if (ufunc == NULL || func_is_global(ufunc)
4735 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4736 return NULL;
4737 ++p;
4738 off = 0;
4739 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004740 else
4741 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004742
4743 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4744 {
4745 emsg(_(e_using_sid_not_in_script_context));
4746 return NULL;
4747 }
4748 // Expand s: prefix into <SNR>nr_<name>
John Marriottb32800f2025-02-01 15:25:34 +01004749 sid_buflen = vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004750 (long)current_sctx.sc_sid);
John Marriottb32800f2025-02-01 15:25:34 +01004751 newnamesize = sid_buflen + STRLEN(p + off) + 1;
4752 newname = alloc(newnamesize);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004753 if (newname == NULL)
4754 return NULL;
John Marriottb32800f2025-02-01 15:25:34 +01004755 vim_snprintf((char *)newname, newnamesize, "%s%s", sid_buf, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004756
4757 return newname;
4758}
4759
4760/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004761 * Return script-local "fname" with the 3-byte sequence replaced by
4762 * printable <SNR> in allocated memory.
4763 */
4764 char_u *
4765alloc_printable_func_name(char_u *fname)
4766{
4767 char_u *n = alloc(STRLEN(fname + 3) + 6);
4768
4769 if (n != NULL)
4770 {
4771 STRCPY(n, "<SNR>");
4772 STRCPY(n + 5, fname + 3);
4773 }
4774 return n;
4775}
4776
4777/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004778 * Call trans_function_name(), except that a lambda is returned as-is.
4779 * Returns the name in allocated memory.
4780 */
4781 char_u *
4782save_function_name(
4783 char_u **name,
4784 int *is_global,
4785 int skip,
4786 int flags,
4787 funcdict_T *fudi)
4788{
4789 char_u *p = *name;
4790 char_u *saved;
4791
4792 if (STRNCMP(p, "<lambda>", 8) == 0)
4793 {
4794 p += 8;
4795 (void)getdigits(&p);
4796 saved = vim_strnsave(*name, p - *name);
4797 if (fudi != NULL)
4798 CLEAR_POINTER(fudi);
4799 }
4800 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004801 saved = trans_function_name_ext(&p, is_global, skip,
4802 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004803 *name = p;
4804 return saved;
4805}
4806
4807/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004808 * List functions. When "regmatch" is NULL all of then.
4809 * Otherwise functions matching "regmatch".
4810 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004811 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004812list_functions(regmatch_T *regmatch)
4813{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004814 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004815 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004816 hashitem_T *hi;
4817
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004818 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004819 {
4820 if (!HASHITEM_EMPTY(hi))
4821 {
4822 ufunc_T *fp = HI2UF(hi);
4823
4824 --todo;
4825 if ((fp->uf_flags & FC_DEAD) == 0
4826 && (regmatch == NULL
4827 ? !message_filtered(fp->uf_name)
4828 && !func_name_refcount(fp->uf_name)
Keith Thompson184f71c2024-01-04 21:19:04 +01004829 : !SAFE_isdigit(*fp->uf_name)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004830 && vim_regexec(regmatch, fp->uf_name, 0)))
4831 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004832 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004833 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004834 if (function_list_modified(prev_ht_changed))
4835 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004836 }
4837 }
4838 }
4839}
4840
4841/*
Yegappan Lakshmanan3956c5b2025-04-13 17:49:50 +02004842 * ":function /pat": list functions matching pattern.
4843 */
4844 static char_u *
4845list_functions_matching_pat(exarg_T *eap)
4846{
4847 char_u *p;
4848 char_u c;
4849
4850 p = skip_regexp(eap->arg + 1, '/', TRUE);
4851 if (!eap->skip)
4852 {
4853 regmatch_T regmatch;
4854
4855 c = *p;
4856 *p = NUL;
4857 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4858 *p = c;
4859 if (regmatch.regprog != NULL)
4860 {
4861 regmatch.rm_ic = p_ic;
4862 list_functions(&regmatch);
4863 vim_regfree(regmatch.regprog);
4864 }
4865 }
4866 if (*p == '/')
4867 ++p;
4868
4869 return p;
4870}
4871
4872/*
4873 * List function "name".
4874 * Returns the function pointer or NULL on failure.
4875 */
4876 static ufunc_T *
4877list_one_function(exarg_T *eap, char_u *name, char_u *p, int is_global)
4878{
4879 ufunc_T *fp = NULL;
4880 int j;
4881
4882 if (!ends_excmd(*skipwhite(p)))
4883 {
4884 semsg(_(e_trailing_characters_str), p);
4885 return NULL;
4886 }
4887
4888 set_nextcmd(eap, p);
4889
4890 if (eap->nextcmd != NULL)
4891 *p = NUL;
4892
4893 if (eap->skip || got_int)
4894 return NULL;
4895
4896 fp = find_func(name, is_global);
4897 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4898 {
4899 char_u *up = untrans_function_name(name);
4900
4901 // With Vim9 script the name was made script-local, if not
4902 // found try again with the original name.
4903 if (up != NULL)
4904 fp = find_func(up, FALSE);
4905 }
4906
4907 if (fp == NULL)
4908 {
4909 emsg_funcname(e_undefined_function_str, eap->arg);
4910 return NULL;
4911 }
4912
4913 // Check no function was added or removed from a timer, e.g. at
4914 // the more prompt. "fp" may then be invalid.
4915 int prev_ht_changed = func_hashtab.ht_changed;
4916
4917 if (list_func_head(fp, TRUE) != OK)
4918 return fp;
4919
4920 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4921 {
4922 if (FUNCLINE(fp, j) == NULL)
4923 continue;
4924 msg_putchar('\n');
4925 msg_outnum((long)(j + 1));
4926 if (j < 9)
4927 msg_putchar(' ');
4928 if (j < 99)
4929 msg_putchar(' ');
4930 if (function_list_modified(prev_ht_changed))
4931 break;
4932 msg_prt_line(FUNCLINE(fp, j), FALSE);
4933 out_flush(); // show a line at a time
4934 ui_breakcheck();
4935 }
4936
4937 if (!got_int)
4938 {
4939 msg_putchar('\n');
4940 if (!function_list_modified(prev_ht_changed))
4941 {
4942 if (fp->uf_def_status != UF_NOT_COMPILED)
4943 msg_puts(" enddef");
4944 else
4945 msg_puts(" endfunction");
4946 }
4947 }
4948
4949 return fp;
4950}
4951
4952/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004953 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004954 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4955 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004956 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004957 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4958 * With CF_INTERFACE the function is define inside an interface, only the
4959 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004960 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004961 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004962 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004963define_function(
4964 exarg_T *eap,
4965 char_u *name_arg,
4966 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004967 int class_flags,
4968 ocmember_T *obj_members,
4969 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004970{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004971 int j;
Bram Moolenaar57033102022-01-30 19:37:52 +00004972 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004973 char_u *name = name_arg;
John Marriottb32800f2025-02-01 15:25:34 +01004974 size_t namelen = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004975 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004976 char_u *p;
4977 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004978 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004979 char_u *line_arg = NULL;
4980 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004982 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004983 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004984 garray_T newlines;
4985 int varargs = FALSE;
4986 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004987 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004988 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004989 int fp_allocated = FALSE;
4990 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004991 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004992 dictitem_T *v;
4993 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004994 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004995 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004996 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004997 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004998 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004999 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005000
Yegappan Lakshmanan3956c5b2025-04-13 17:49:50 +02005001 // ":function" without argument: list functions.
Bram Moolenaara72cfb82020-04-23 17:07:30 +02005002 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005003 {
5004 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02005005 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02005006 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005007 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005008 }
5009
5010 /*
5011 * ":function /pat": list functions matching pattern.
5012 */
5013 if (*eap->arg == '/')
5014 {
Yegappan Lakshmanan3956c5b2025-04-13 17:49:50 +02005015 p = list_functions_matching_pat(eap);
Bram Moolenaar63b91732021-08-05 20:40:03 +02005016 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005017 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005018 }
5019
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005020 ga_init(&newargs);
5021 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005022 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005023 ga_init(&default_args);
5024
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005025 /*
5026 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00005027 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005028 * "name" == func, "fudi.fd_dict" == NULL
5029 * dict.func new dictionary entry
5030 * "name" == NULL, "fudi.fd_dict" set,
5031 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
5032 * dict.func existing dict entry with a Funcref
5033 * "name" == func, "fudi.fd_dict" set,
5034 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
5035 * dict.func existing dict entry that's not a Funcref
5036 * "name" == NULL, "fudi.fd_dict" set,
5037 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
5038 * s:func script-local function name
5039 * g:func global function name, same as "func"
5040 */
5041 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005042 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005043 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02005044 // nested function, argument is (args).
5045 paren = TRUE;
5046 CLEAR_FIELD(fudi);
5047 }
5048 else
5049 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00005050 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00005051 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00005052 if (p[0] == 's' && p[1] == ':')
5053 {
5054 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
5055 return NULL;
5056 }
5057 p = to_name_end(p, TRUE);
5058 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
5059 {
5060 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
5061 eap->arg);
5062 return NULL;
5063 }
5064 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00005065 }
5066
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005067 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00005068 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005069 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005070 paren = (vim_strchr(p, '(') != NULL);
5071 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005072 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02005073 /*
5074 * Return on an invalid expression in braces, unless the expression
5075 * evaluation has been cancelled due to an aborting error, an
5076 * interrupt, or an exception.
5077 */
5078 if (!aborting())
5079 {
5080 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005081 semsg(_(e_key_not_present_in_dictionary_str),
5082 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005083 vim_free(fudi.fd_newkey);
5084 return NULL;
5085 }
5086 else
5087 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005088 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00005089
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00005090 // For "export def FuncName()" in an autoload script the function name
5091 // is stored with the legacy autoload name "dir#script#FuncName" so
5092 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00005093 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005094 {
5095 char_u *prefixed = may_prefix_autoload(name);
5096
5097 if (prefixed != NULL && prefixed != name)
5098 {
5099 vim_free(name);
5100 name = prefixed;
5101 }
5102 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00005103 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00005104 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00005105 {
5106 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
5107 goto ret_free;
5108 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005109 }
5110
Bram Moolenaare38eab22019-12-05 21:50:01 +01005111 // An error in a function call during evaluation of an expression in magic
5112 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005113 saved_did_emsg = did_emsg;
5114 did_emsg = FALSE;
5115
5116 /*
5117 * ":function func" with only function name: list function.
5118 */
5119 if (!paren)
5120 {
Yegappan Lakshmanan3956c5b2025-04-13 17:49:50 +02005121 fp = list_one_function(eap, name, p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005122 goto ret_free;
5123 }
5124
5125 /*
5126 * ":function name(arg1, arg2)" Define function.
5127 */
5128 p = skipwhite(p);
5129 if (*p != '(')
5130 {
5131 if (!eap->skip)
5132 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005133 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005134 goto ret_free;
5135 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005136 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005137 if (vim_strchr(p, '(') != NULL)
5138 p = vim_strchr(p, '(');
5139 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005140
Bram Moolenaar4efd9942021-01-24 21:14:20 +01005141 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
5142 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01005143 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01005144 goto ret_free;
5145 }
5146
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02005147 // In Vim9 script only global functions can be redefined.
5148 if (vim9script && eap->forceit && !is_global)
5149 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005150 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02005151 goto ret_free;
5152 }
5153
Bram Moolenaar04935fb2022-01-08 16:19:22 +00005154 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005155
Bram Moolenaar04b12692020-05-04 23:24:44 +02005156 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005157 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005158 // Check the name of the function. Unless it's a dictionary function
5159 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005160 if (name != NULL)
5161 arg = name;
5162 else
5163 arg = fudi.fd_newkey;
5164 if (arg != NULL && (fudi.fd_di == NULL
5165 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
5166 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
5167 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00005168 char_u *name_base = arg;
5169 int i;
5170
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005171 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005172 {
5173 name_base = vim_strchr(arg, '_');
5174 if (name_base == NULL)
5175 name_base = arg + 3;
5176 else
5177 ++name_base;
5178 }
5179 for (i = 0; name_base[i] != NUL && (i == 0
5180 ? eval_isnamec1(name_base[i])
5181 : eval_isnamec(name_base[i])); ++i)
5182 ;
5183 if (name_base[i] != NUL)
zeertzjq6a04bf52024-03-16 09:39:06 +01005184 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005185 emsg_funcname(e_invalid_argument_str, arg);
zeertzjq6a04bf52024-03-16 09:39:06 +01005186 goto ret_free;
5187 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00005188
5189 // In Vim9 script a function cannot have the same name as a
5190 // variable.
5191 if (vim9script && *arg == K_SPECIAL
John Marriottb32800f2025-02-01 15:25:34 +01005192 && eval_variable(name_base, i, 0, NULL,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00005193 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00005194 + EVAL_VAR_NO_FUNC) == OK)
5195 {
5196 semsg(_(e_redefining_script_item_str), name_base);
5197 goto ret_free;
5198 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005199 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005200 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005201 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005202 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005203 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00005204 goto ret_free;
5205 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005206 }
5207
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005208 // This may get more lines and make the pointers into the first line
5209 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01005210 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005211 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00005212 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02005213 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005214 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00005215 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005216 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01005217 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005219 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005220 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005221 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005222 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005223 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005224 if (*p != ':')
5225 {
5226 semsg(_(e_no_white_space_allowed_before_colon_str), p);
5227 p = skipwhite(p);
5228 }
5229 else if (!IS_WHITE_OR_NUL(p[1]))
5230 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005231 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02005232 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005233 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005234 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005235 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01005236 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005237 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005238 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005239 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005240 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005241 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02005242 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005243 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005244 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02005245 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005246 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005247 else
5248 // find extra arguments "range", "dict", "abort" and "closure"
5249 for (;;)
5250 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01005251 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005252 p = skipwhite(p);
5253 if (STRNCMP(p, "range", 5) == 0)
5254 {
5255 flags |= FC_RANGE;
5256 p += 5;
5257 }
5258 else if (STRNCMP(p, "dict", 4) == 0)
5259 {
5260 flags |= FC_DICT;
5261 p += 4;
5262 }
5263 else if (STRNCMP(p, "abort", 5) == 0)
5264 {
5265 flags |= FC_ABORT;
5266 p += 5;
5267 }
5268 else if (STRNCMP(p, "closure", 7) == 0)
5269 {
5270 flags |= FC_CLOSURE;
5271 p += 7;
5272 if (current_funccal == NULL)
5273 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005274 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005275 name == NULL ? (char_u *)"" : name);
5276 goto erret;
5277 }
5278 }
5279 else
5280 break;
5281 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005282
Bram Moolenaare38eab22019-12-05 21:50:01 +01005283 // When there is a line break use what follows for the function body.
5284 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005285 if (*p == '\n')
5286 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005287 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005288 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5289 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005290 && !(VIM_ISWHITE(*whitep) && *p == '#'
5291 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005292 && !eap->skip
5293 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005294 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005295
5296 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005297 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5298 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005299 */
5300 if (KeyTyped)
5301 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005302 // Check if the function already exists, don't let the user type the
5303 // whole function before telling him it doesn't work! For a script we
5304 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005305 if (!eap->skip && !eap->forceit)
5306 {
5307 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005308 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005309 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005310 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005311 }
5312
5313 if (!eap->skip && did_emsg)
5314 goto erret;
5315
Bram Moolenaare38eab22019-12-05 21:50:01 +01005316 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005317 cmdline_row = msg_row;
5318 }
5319
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005320 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005321 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005322
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005323 // Do not define the function when getting the body fails and when
5324 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005325 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005326 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005327 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5328 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005329 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005330 goto erret;
5331
5332 /*
5333 * If there are no errors, add the function
5334 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005335 if (fudi.fd_dict != NULL)
5336 {
John Marriottb32800f2025-02-01 15:25:34 +01005337 char numbuf[NUMBUFLEN];
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005338
5339 fp = NULL;
5340 if (fudi.fd_newkey == NULL && !eap->forceit)
5341 {
5342 emsg(_(e_dictionary_entry_already_exists));
5343 goto erret;
5344 }
5345 if (fudi.fd_di == NULL)
5346 {
5347 // Can't add a function to a locked dictionary
5348 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5349 goto erret;
5350 }
5351 // Can't change an existing function if it is locked
5352 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5353 goto erret;
5354
5355 // Give the function a sequential number. Can only be used with a
5356 // Funcref!
5357 vim_free(name);
John Marriottb32800f2025-02-01 15:25:34 +01005358 namelen = vim_snprintf(numbuf, sizeof(numbuf), "%d", ++func_nr);
5359 name = vim_strnsave((char_u *)numbuf, namelen);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005360 if (name == NULL)
5361 goto erret;
5362 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005363 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005364 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005365 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005366 char_u *find_name = name;
5367 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005368 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005369
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005370 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005371 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005372 var_conflict = TRUE;
5373
5374 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5375 {
5376 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5377
5378 if (si->sn_autoload_prefix != NULL)
5379 {
5380 if (is_export)
5381 {
5382 find_name = name + STRLEN(si->sn_autoload_prefix);
5383 v = find_var(find_name, &ht, TRUE);
5384 if (v != NULL)
5385 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005386 // Only check if the function already exists in the script,
5387 // global functions can be shadowed.
5388 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005389 }
5390 else
5391 {
5392 char_u *prefixed = may_prefix_autoload(name);
5393
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005394 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005395 {
5396 v = find_var(prefixed, &ht, TRUE);
5397 if (v != NULL)
5398 var_conflict = TRUE;
5399 vim_free(prefixed);
5400 }
5401 }
5402 }
5403 }
5404 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005405 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005406 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005407 goto erret;
5408 }
5409
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005410 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005411 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005412 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005413 char_u *uname = untrans_function_name(name);
5414
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005415 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005416 }
5417
5418 if (fp != NULL || import != NULL)
5419 {
5420 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005421
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005422 // Function can be replaced with "function!" and when sourcing the
5423 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005424 // A name that is used by an import can not be overruled.
5425 if (import != NULL
5426 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005427 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005428 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005429 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005430 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005431 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005432 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005433 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005434 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
zeertzjq04d2a3f2025-02-02 19:03:17 +01005435 goto errret_keep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005436 }
5437 if (fp->uf_calls > 0)
5438 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005439 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005440 e_cannot_redefine_function_str_it_is_in_use, name);
zeertzjq04d2a3f2025-02-02 19:03:17 +01005441 goto errret_keep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005442 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005443 if (fp->uf_refcount > 1)
5444 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005445 // This function is referenced somewhere, don't redefine it but
5446 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005447 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005448 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005449 fp = NULL;
5450 overwrite = TRUE;
5451 }
5452 else
5453 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005454 char_u *exp_name = fp->uf_name_exp;
5455
5456 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005457 VIM_CLEAR(name);
John Marriottb32800f2025-02-01 15:25:34 +01005458 namelen = 0;
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005459 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005460 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005461 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005462 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005463#ifdef FEAT_PROFILE
5464 fp->uf_profiling = FALSE;
5465 fp->uf_prof_initialized = FALSE;
5466#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005467 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005468 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005469 }
5470 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005471
5472 if (fp == NULL)
5473 {
5474 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5475 {
5476 int slen, plen;
5477 char_u *scriptname;
5478
Bram Moolenaare38eab22019-12-05 21:50:01 +01005479 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005480 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005481 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005482 {
5483 scriptname = autoload_name(name);
5484 if (scriptname != NULL)
5485 {
5486 p = vim_strchr(scriptname, '/');
5487 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005488 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005489 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005490 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005491 j = OK;
5492 vim_free(scriptname);
5493 }
5494 }
5495 if (j == FAIL)
5496 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005497 linenr_T save_lnum = SOURCING_LNUM;
5498
5499 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005500 semsg(_(e_function_name_does_not_match_script_file_name_str),
5501 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005502 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005503 goto erret;
5504 }
5505 }
5506
John Marriottb32800f2025-02-01 15:25:34 +01005507 if (namelen == 0)
5508 namelen = STRLEN(name);
5509 fp = alloc_ufunc(name, namelen);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005510 if (fp == NULL)
5511 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005512 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005513
5514 if (fudi.fd_dict != NULL)
5515 {
5516 if (fudi.fd_di == NULL)
5517 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005518 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005519 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5520 if (fudi.fd_di == NULL)
5521 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005522 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005523 goto erret;
5524 }
5525 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5526 {
5527 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005528 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005529 goto erret;
5530 }
5531 }
5532 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005533 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005534 clear_tv(&fudi.fd_di->di_tv);
5535 fudi.fd_di->di_tv.v_type = VAR_FUNC;
John Marriottb32800f2025-02-01 15:25:34 +01005536 fudi.fd_di->di_tv.vval.v_string = vim_strnsave(name, namelen);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005537
Bram Moolenaare38eab22019-12-05 21:50:01 +01005538 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005539 flags |= FC_DICT;
5540 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005541 }
5542 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005543 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005544 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005545 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005546
5547 if (eap->cmdidx == CMD_def)
5548 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005549 int lnum_save = SOURCING_LNUM;
5550 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005551
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005552 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005553
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005554 // error messages are for the first function line
5555 SOURCING_LNUM = sourcing_lnum_top;
5556
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005557 // The function may use script variables from the context.
5558 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005559
Yegappan Lakshmanand79ea472025-01-20 21:09:13 +01005560 // The argument types and the return type may use an imported type.
5561 // In that case, the imported file will be sourced. To avoid treating
5562 // everything in the imported file as exported, temporarily reset
5563 // is_export.
5564 int save_is_export = is_export;
5565 is_export = FALSE;
5566
h-eastb895b0f2023-09-24 15:46:31 +02005567 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5568 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005569 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005570 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005571 free_fp = fp_allocated;
Yegappan Lakshmanand79ea472025-01-20 21:09:13 +01005572 is_export = save_is_export;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005573 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005574 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005575 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005576
5577 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005578 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005579 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005580 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005581 free_fp = fp_allocated;
Yegappan Lakshmanand79ea472025-01-20 21:09:13 +01005582 is_export = save_is_export;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005583 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005584 }
Yegappan Lakshmanand79ea472025-01-20 21:09:13 +01005585 is_export = save_is_export;
Bram Moolenaar09689a02020-05-09 22:50:08 +02005586 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005587 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005588 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005589 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005590
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005591 if (fp_allocated)
5592 {
5593 // insert the new function in the function list
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005594 if (overwrite)
5595 {
5596 hi = hash_find(&func_hashtab, name);
5597 hi->hi_key = UF2HIKEY(fp);
5598 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005599 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005600 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005601 {
5602 free_fp = TRUE;
5603 goto erret;
5604 }
5605 fp->uf_refcount = 1;
5606 }
5607
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005608 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005609 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005610 if ((flags & FC_CLOSURE) != 0)
5611 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005612 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005613 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005614 }
5615 else
5616 fp->uf_scoped = NULL;
5617
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005618#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005619 if (prof_def_func())
5620 func_do_profile(fp);
5621#endif
5622 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005623 if (sandbox)
5624 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005625 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005626 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005627 fp->uf_flags = flags;
5628 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005629 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005630 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005631 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005632 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005633 if (is_export)
5634 {
5635 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005636 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005637 is_export = FALSE;
5638 }
5639
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005640 if (eap->cmdidx == CMD_def)
5641 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005642 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5643 // :func does not use Vim9 script syntax, even in a Vim9 script file
5644 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005645
Yegappan Lakshmanan5715a722024-05-03 18:24:07 +02005646 // If test_override('defcompile' 1) is used, then compile the function now
5647 if (eap->cmdidx == CMD_def && override_defcompile)
5648 defcompile_function(fp, NULL);
5649
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005650 goto ret_free;
5651
5652erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005653 if (fp != NULL)
5654 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005655 // these were set to "newargs" and "default_args", which are cleared
5656 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005657 ga_init(&fp->uf_args);
5658 ga_init(&fp->uf_def_args);
5659 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005660errret_2:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005661 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005662 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005663 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005664 VIM_CLEAR(fp->uf_va_name);
John Marriottb32800f2025-02-01 15:25:34 +01005665 VIM_CLEAR(fp->uf_name_exp);
Christian Brabandt0f287912023-12-11 17:53:25 +01005666 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005667 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005668 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005669 VIM_CLEAR(fp);
zeertzjq04d2a3f2025-02-02 19:03:17 +01005670errret_keep:
5671 ga_clear_strings(&newargs);
5672 ga_clear_strings(&default_args);
5673 ga_clear_strings(&newlines);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005674ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005675 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005676 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005677 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005678 if (name != name_arg)
5679 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005680 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005681 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005682
5683 return fp;
5684}
5685
5686/*
5687 * ":function"
5688 */
5689 void
5690ex_function(exarg_T *eap)
5691{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005692 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005693
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005694 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005695 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005696 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005697}
5698
LemonBoy48b7d052024-07-09 18:24:59 +02005699 int
5700get_func_arity(char_u *name, int *required, int *optional, int *varargs)
5701{
5702 ufunc_T *ufunc = NULL;
5703 int argcount = 0;
5704 int min_argcount = 0;
5705 int idx;
5706
5707 idx = find_internal_func(name);
5708 if (idx >= 0)
5709 {
5710 internal_func_get_argcount(idx, &argcount, &min_argcount);
5711 *varargs = FALSE;
5712 }
5713 else
5714 {
5715 char_u fname_buf[FLEN_FIXED + 1];
5716 char_u *tofree = NULL;
5717 funcerror_T error = FCERR_NONE;
5718 char_u *fname;
5719
5720 // May need to translate <SNR>123_ to K_SNR.
5721 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
5722 if (error == FCERR_NONE)
5723 ufunc = find_func(fname, FALSE);
5724 vim_free(tofree);
5725
5726 if (ufunc == NULL)
5727 return FAIL;
5728
5729 argcount = ufunc->uf_args.ga_len;
5730 min_argcount = ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len;
5731 *varargs = has_varargs(ufunc);
5732 }
5733
5734 *required = min_argcount;
5735 *optional = argcount - min_argcount;
5736
5737 return OK;
5738}
5739
Bram Moolenaar822ba242020-05-24 23:00:18 +02005740/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005741 * Find a function by name, including "<lambda>123".
5742 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005743 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005744 * Return NULL if not found.
5745 */
5746 ufunc_T *
5747find_func_by_name(char_u *name, compiletype_T *compile_type)
5748{
5749 char_u *arg = name;
5750 char_u *fname;
5751 ufunc_T *ufunc;
5752 int is_global = FALSE;
5753
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005754 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5755 {
5756 *compile_type = CT_PROFILE;
5757 arg = skipwhite(arg + 7);
5758 }
5759 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5760 {
5761 *compile_type = CT_DEBUG;
5762 arg = skipwhite(arg + 5);
5763 }
5764
5765 if (STRNCMP(arg, "<lambda>", 8) == 0)
5766 {
5767 arg += 8;
5768 (void)getdigits(&arg);
5769 fname = vim_strnsave(name, arg - name);
5770 }
5771 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005772 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005773 // First try finding a method in a class, trans_function_name() will
5774 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005775 ufunc = find_class_func(&arg);
5776 if (ufunc != NULL)
5777 return ufunc;
5778
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005779 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005780 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005781 NULL, NULL, NULL, &ufunc);
5782 if (ufunc != NULL)
5783 {
5784 vim_free(fname);
5785 return ufunc;
5786 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005787 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005788 if (fname == NULL)
5789 {
5790 semsg(_(e_invalid_argument_str), name);
5791 return NULL;
5792 }
5793 if (!ends_excmd2(name, arg))
5794 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005795 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005796 emsg(ex_errmsg(e_trailing_characters_str, arg));
5797 return NULL;
5798 }
5799
5800 ufunc = find_func(fname, is_global);
5801 if (ufunc == NULL)
5802 {
5803 char_u *p = untrans_function_name(fname);
5804
5805 if (p != NULL)
5806 // Try again without making it script-local.
5807 ufunc = find_func(p, FALSE);
5808 }
5809 vim_free(fname);
5810 if (ufunc == NULL)
5811 semsg(_(e_cannot_find_function_str), name);
5812 return ufunc;
5813}
5814
5815/*
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005816 * Compile the :def function "ufunc". If "cl" is not NULL, then compile the
5817 * class or object method "ufunc" in "cl".
5818 */
5819 void
5820defcompile_function(ufunc_T *ufunc, class_T *cl)
5821{
5822 compiletype_T compile_type = CT_NONE;
5823
5824 if (func_needs_compiling(ufunc, compile_type))
5825 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5826 else
5827 smsg(_("Function %s%s%s does not need compiling"),
5828 cl != NULL ? cl->class_name : (char_u *)"",
5829 cl != NULL ? (char_u *)"." : (char_u *)"",
5830 ufunc->uf_name);
5831}
5832
5833/*
5834 * Compile all the :def functions defined in the current script
5835 */
5836 static void
5837defcompile_funcs_in_script(void)
5838{
5839 long todo = (long)func_hashtab.ht_used;
5840 int changed = func_hashtab.ht_changed;
5841 hashitem_T *hi;
5842
5843 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5844 {
5845 if (!HASHITEM_EMPTY(hi))
5846 {
5847 --todo;
5848 ufunc_T *ufunc = HI2UF(hi);
5849 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5850 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5851 && (ufunc->uf_flags & FC_DEAD) == 0)
5852 {
5853 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5854
5855 if (func_hashtab.ht_changed != changed)
5856 {
5857 // a function has been added or removed, need to start
5858 // over
5859 todo = (long)func_hashtab.ht_used;
5860 changed = func_hashtab.ht_changed;
5861 hi = func_hashtab.ht_array;
5862 --hi;
5863 }
5864 }
5865 }
5866 }
5867}
5868
5869/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005870 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005871 * be compiled or the one specified by the argument.
5872 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005873 */
5874 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005875ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005876{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005877 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005878 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005879 typval_T tv;
5880
5881 if (is_class_name(eap->arg, &tv))
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005882 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005883 class_T *cl = tv.vval.v_class;
5884
5885 if (cl != NULL)
5886 defcompile_class(cl);
5887 }
5888 else
5889 {
5890 compiletype_T compile_type = CT_NONE;
5891 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
5892 if (ufunc != NULL)
5893 defcompile_function(ufunc, NULL);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005894 }
5895 }
5896 else
5897 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005898 defcompile_funcs_in_script();
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005899
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005900 // compile all the class defined in the current script
5901 defcompile_classes_in_script();
Bram Moolenaar822ba242020-05-24 23:00:18 +02005902 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005903}
5904
5905/*
5906 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5907 * Return 2 if "p" starts with "s:".
5908 * Return 0 otherwise.
5909 */
5910 int
5911eval_fname_script(char_u *p)
5912{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005913 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5914 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005915 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5916 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5917 return 5;
5918 if (p[0] == 's' && p[1] == ':')
5919 return 2;
5920 return 0;
5921}
5922
5923 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005924translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005925{
5926 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005927 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005928 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005929}
5930
5931/*
5932 * Return TRUE when "ufunc" has old-style "..." varargs
5933 * or named varargs "...name: type".
5934 */
5935 int
5936has_varargs(ufunc_T *ufunc)
5937{
5938 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005939}
5940
5941/*
5942 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005943 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005944 */
5945 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005946function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005947{
5948 char_u *nm = name;
5949 char_u *p;
5950 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005951 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005952 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005953
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005954 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5955 if (no_deref)
5956 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005957 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005958 nm = skipwhite(nm);
5959
Bram Moolenaare38eab22019-12-05 21:50:01 +01005960 // Only accept "funcname", "funcname ", "funcname (..." and
5961 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005962 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005963 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005964 vim_free(p);
5965 return n;
5966}
5967
Bram Moolenaar113e1072019-01-20 15:30:40 +01005968#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005969 char_u *
5970get_expanded_name(char_u *name, int check)
5971{
5972 char_u *nm = name;
5973 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005974 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005975
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005976 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005977
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005978 if (p != NULL && *nm == NUL
5979 && (!check || translated_function_exists(p, is_global)))
5980 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005981
5982 vim_free(p);
5983 return NULL;
5984}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005985#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005986
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005987/*
5988 * Function given to ExpandGeneric() to obtain the list of user defined
5989 * function names.
5990 */
5991 char_u *
5992get_user_func_name(expand_T *xp, int idx)
5993{
5994 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005995 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005996 static hashitem_T *hi;
5997 ufunc_T *fp;
5998
5999 if (idx == 0)
6000 {
6001 done = 0;
6002 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02006003 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006004 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02006005 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006006 {
John Marriottb32800f2025-02-01 15:25:34 +01006007 int len;
6008
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006009 if (done++ > 0)
6010 ++hi;
6011 while (HASHITEM_EMPTY(hi))
6012 ++hi;
6013 fp = HI2UF(hi);
6014
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006015 // don't show dead, dict and lambda functions
6016 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02006017 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006018 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006019
John Marriottb32800f2025-02-01 15:25:34 +01006020 if (fp->uf_namelen + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006021 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006022
John Marriottb32800f2025-02-01 15:25:34 +01006023 len = cat_func_name(IObuff, IOSIZE, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02006024 if (xp->xp_context != EXPAND_USER_FUNC
6025 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006026 {
John Marriottb32800f2025-02-01 15:25:34 +01006027 STRCPY(IObuff + len, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006028 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
John Marriottb32800f2025-02-01 15:25:34 +01006029 {
6030 ++len;
6031 STRCPY(IObuff + len, ")");
6032 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006033 }
6034 return IObuff;
6035 }
6036 return NULL;
6037}
6038
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006039/*
Bram Moolenaar83677162023-01-08 19:54:10 +00006040 * Make a copy of a function.
6041 * Intended to be used for a function defined on a base class that has a copy
6042 * on the child class.
6043 * The copy has uf_refcount set to one.
6044 * Returns NULL when out of memory.
6045 */
6046 ufunc_T *
6047copy_function(ufunc_T *fp)
6048{
John Marriottb32800f2025-02-01 15:25:34 +01006049 ufunc_T *ufunc = alloc_ufunc(fp->uf_name, fp->uf_namelen);
Bram Moolenaar83677162023-01-08 19:54:10 +00006050 if (ufunc == NULL)
6051 return NULL;
6052
6053 // Most things can just be copied.
6054 *ufunc = *fp;
6055
6056 ufunc->uf_def_status = UF_TO_BE_COMPILED;
6057 ufunc->uf_dfunc_idx = 0;
6058 ufunc->uf_class = NULL;
6059
6060 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
6061 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
6062
6063 if (ufunc->uf_arg_types != NULL)
6064 {
6065 // "uf_arg_types" is an allocated array, make a copy.
6066 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
6067 if (at != NULL)
6068 {
6069 mch_memmove(at, ufunc->uf_arg_types,
6070 sizeof(type_T *) * ufunc->uf_args.ga_len);
6071 ufunc->uf_arg_types = at;
6072 }
6073 }
6074
6075 // TODO: how about the types themselves? they can be freed when the
6076 // original function is freed:
6077 // type_T **uf_arg_types;
6078 // type_T *uf_ret_type;
6079
Ernie Rael114ec812023-06-04 18:11:35 +01006080 // make uf_type_list empty
6081 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00006082
6083 // TODO: partial_T *uf_partial;
6084
6085 if (ufunc->uf_va_name != NULL)
6086 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
6087
6088 // TODO:
6089 // type_T *uf_va_type;
6090 // type_T *uf_func_type;
6091
6092 ufunc->uf_block_depth = 0;
6093 ufunc->uf_block_ids = NULL;
6094
6095 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
6096
6097 ufunc->uf_refcount = 1;
Bram Moolenaar83677162023-01-08 19:54:10 +00006098
6099 return ufunc;
6100}
6101
6102/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006103 * ":delfunction {name}"
6104 */
6105 void
6106ex_delfunction(exarg_T *eap)
6107{
6108 ufunc_T *fp = NULL;
6109 char_u *p;
6110 char_u *name;
6111 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006112 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006113
6114 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00006115 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
6116 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006117 vim_free(fudi.fd_newkey);
6118 if (name == NULL)
6119 {
6120 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00006121 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006122 return;
6123 }
6124 if (!ends_excmd(*skipwhite(p)))
6125 {
6126 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00006127 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006128 return;
6129 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006130 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006131 if (eap->nextcmd != NULL)
6132 *p = NUL;
6133
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006134 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02006135 {
6136 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006137 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02006138 vim_free(name);
6139 return;
6140 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006141 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006142 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006143 vim_free(name);
6144
6145 if (!eap->skip)
6146 {
6147 if (fp == NULL)
6148 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02006149 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00006150 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006151 return;
6152 }
6153 if (fp->uf_calls > 0)
6154 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006155 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006156 return;
6157 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006158 if (fp->uf_flags & FC_VIM9)
6159 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006160 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006161 return;
6162 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006163
6164 if (fudi.fd_dict != NULL)
6165 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006166 // Delete the dict item that refers to the function, it will
6167 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00006168 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006169 }
6170 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006171 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006172 // A normal function (not a numbered function or lambda) has a
6173 // refcount of 1 for the entry in the hashtable. When deleting
6174 // it and the refcount is more than one, it should be kept.
6175 // A numbered function and lambda should be kept if the refcount is
6176 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006177 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006178 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006179 // Function is still referenced somewhere. Don't free it but
6180 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006181 if (func_remove(fp))
6182 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006183 }
6184 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01006185 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006186 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006187 }
6188}
6189
6190/*
6191 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006192 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006193 */
6194 void
6195func_unref(char_u *name)
6196{
Bram Moolenaar97baee82016-07-26 20:46:08 +02006197 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006198
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006199 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006200 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006201 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006202 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006203 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006204#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006205 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006206#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01006207 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006208 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006209 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006210}
6211
6212/*
6213 * Unreference a Function: decrement the reference count and free it when it
6214 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006215 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006216 */
6217 void
6218func_ptr_unref(ufunc_T *fp)
6219{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006220 if (fp != NULL && (--fp->uf_refcount <= 0
6221 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
6222 && fp->uf_partial->pt_refcount <= 1
6223 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02006224 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006225 // Only delete it when it's not being used. Otherwise it's done
6226 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02006227 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01006228 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006229 }
6230}
6231
6232/*
6233 * Count a reference to a Function.
6234 */
6235 void
6236func_ref(char_u *name)
6237{
6238 ufunc_T *fp;
6239
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006240 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006241 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006242 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006243 if (fp != NULL)
6244 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006245 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01006246 // Only give an error for a numbered function.
6247 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01006248 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006249}
6250
6251/*
6252 * Count a reference to a Function.
6253 */
6254 void
6255func_ptr_ref(ufunc_T *fp)
6256{
6257 if (fp != NULL)
6258 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006259}
6260
6261/*
6262 * Return TRUE if items in "fc" do not have "copyID". That means they are not
6263 * referenced from anywhere that is in use.
6264 */
6265 static int
6266can_free_funccal(funccall_T *fc, int copyID)
6267{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006268 return (fc->fc_l_varlist.lv_copyID != copyID
6269 && fc->fc_l_vars.dv_copyID != copyID
6270 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006271 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006272}
6273
6274/*
6275 * ":return [expr]"
6276 */
6277 void
6278ex_return(exarg_T *eap)
6279{
6280 char_u *arg = eap->arg;
6281 typval_T rettv;
6282 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006283 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006284
6285 if (current_funccal == NULL)
6286 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006287 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006288 return;
6289 }
6290
Bram Moolenaar844fb642021-10-23 13:32:30 +01006291 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006292 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
6293
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006294 if (eap->skip)
6295 ++emsg_skip;
6296
6297 eap->nextcmd = NULL;
6298 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006299 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006300 {
6301 if (!eap->skip)
6302 returning = do_return(eap, FALSE, TRUE, &rettv);
6303 else
6304 clear_tv(&rettv);
6305 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01006306 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006307 else if (!eap->skip)
6308 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006309 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01006310 update_force_abort();
6311
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006312 /*
6313 * Return unless the expression evaluation has been cancelled due to an
6314 * aborting error, an interrupt, or an exception.
6315 */
6316 if (!aborting())
6317 returning = do_return(eap, FALSE, TRUE, NULL);
6318 }
6319
Bram Moolenaare38eab22019-12-05 21:50:01 +01006320 // When skipping or the return gets pending, advance to the next command
6321 // in this line (!returning). Otherwise, ignore the rest of the line.
6322 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006323 if (returning)
6324 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006325 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02006326 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006327
6328 if (eap->skip)
6329 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02006330 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006331}
6332
zeertzjq5299c092023-04-12 20:48:16 +01006333/*
6334 * Lower level implementation of "call". Only called when not skipping.
6335 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006336 static int
6337ex_call_inner(
6338 exarg_T *eap,
6339 char_u *name,
6340 char_u **arg,
6341 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006342 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006343 evalarg_T *evalarg)
6344{
6345 linenr_T lnum;
6346 int doesrange;
6347 typval_T rettv;
6348 int failed = FALSE;
6349
zeertzjq5299c092023-04-12 20:48:16 +01006350 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006351 for ( ; lnum <= eap->line2; ++lnum)
6352 {
6353 funcexe_T funcexe;
6354
zeertzjq5299c092023-04-12 20:48:16 +01006355 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006356 {
6357 if (lnum > curbuf->b_ml.ml_line_count)
6358 {
6359 // If the function deleted lines or switched to another buffer
6360 // the line number may become invalid.
6361 emsg(_(e_invalid_range));
6362 break;
6363 }
6364 curwin->w_cursor.lnum = lnum;
6365 curwin->w_cursor.col = 0;
6366 curwin->w_cursor.coladd = 0;
6367 }
6368 *arg = startarg;
6369
6370 funcexe = *funcexe_init;
6371 funcexe.fe_doesrange = &doesrange;
6372 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6373 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6374 {
6375 failed = TRUE;
6376 break;
6377 }
6378 if (has_watchexpr())
6379 dbg_check_breakpoint(eap);
6380
6381 // Handle a function returning a Funcref, Dictionary or List.
6382 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006383 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006384 {
6385 failed = TRUE;
6386 break;
6387 }
6388
6389 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006390 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006391 break;
6392
6393 // Stop when immediately aborting on error, or when an interrupt
6394 // occurred or an exception was thrown but not caught.
6395 // get_func_tv() returned OK, so that the check for trailing
6396 // characters below is executed.
6397 if (aborting())
6398 break;
6399 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006400 return failed;
6401}
6402
6403/*
6404 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6405 * Returns FAIL or OK.
6406 */
6407 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006408ex_defer_inner(
6409 char_u *name,
6410 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006411 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006412 partial_T *partial,
6413 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006414{
6415 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006416 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006417 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006418 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006419
6420 if (current_funccal == NULL)
6421 {
6422 semsg(_(e_str_not_inside_function), "defer");
6423 return FAIL;
6424 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006425 if (partial != NULL)
6426 {
6427 if (partial->pt_dict != NULL)
6428 {
6429 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6430 return FAIL;
6431 }
6432 if (partial->pt_argc > 0)
6433 {
6434 int i;
6435
6436 partial_argc = partial->pt_argc;
6437 for (i = 0; i < partial_argc; ++i)
6438 copy_tv(&partial->pt_argv[i], &argvars[i]);
6439 }
6440 }
Ernie Raelb077b582023-12-14 20:11:44 +01006441 int is_builtin = builtin_function(name, -1);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006442 r = get_func_arguments(arg, evalarg, FALSE,
Ernie Raelb077b582023-12-14 20:11:44 +01006443 argvars + partial_argc, &argcount, is_builtin);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006444 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006445
6446 if (r == OK)
6447 {
6448 if (type != NULL)
6449 {
6450 // Check that the arguments are OK for the types of the funcref.
6451 r = check_argument_types(type, argvars, argcount, NULL, name);
6452 }
Ernie Raelb077b582023-12-14 20:11:44 +01006453 else if (is_builtin)
Bram Moolenaar16900322022-09-08 19:51:45 +01006454 {
6455 int idx = find_internal_func(name);
6456
6457 if (idx < 0)
6458 {
6459 emsg_funcname(e_unknown_function_str, name);
6460 r = FAIL;
6461 }
6462 else if (check_internal_func(idx, argcount) == -1)
6463 r = FAIL;
6464 }
6465 else
6466 {
6467 ufunc_T *ufunc = find_func(name, FALSE);
6468
6469 // we tolerate an unknown function here, it might be defined later
6470 if (ufunc != NULL)
6471 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006472 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006473 if (error != FCERR_UNKNOWN)
6474 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006475 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006476 r = FAIL;
6477 }
6478 }
6479 }
6480 }
6481
Bram Moolenaar86d87252022-09-05 21:21:25 +01006482 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006483 {
6484 while (--argcount >= 0)
6485 clear_tv(&argvars[argcount]);
6486 return FAIL;
6487 }
6488 return add_defer(name, argcount, argvars);
6489}
6490
6491/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006492 * Return TRUE if currently inside a function call.
6493 * Give an error message and return FALSE when not.
6494 */
6495 int
6496can_add_defer(void)
6497{
6498 if (!in_def_function() && get_current_funccal() == NULL)
6499 {
6500 semsg(_(e_str_not_inside_function), "defer");
6501 return FALSE;
6502 }
6503 return TRUE;
6504}
6505
6506/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006507 * Add a deferred call for "name" with arguments "argvars[argcount]".
6508 * Consumes "argvars[]".
6509 * Caller must check that in_def_function() returns TRUE or current_funccal is
6510 * not NULL.
6511 * Returns OK or FAIL.
6512 */
6513 int
6514add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6515{
6516 char_u *saved_name = vim_strsave(name);
6517 int argcount = argcount_arg;
6518 defer_T *dr;
6519 int ret = FAIL;
6520
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006521 if (saved_name == NULL)
6522 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006523 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006524 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006525 if (add_defer_function(saved_name, argcount, argvars) == OK)
6526 argcount = 0;
6527 }
6528 else
6529 {
6530 if (current_funccal->fc_defer.ga_itemsize == 0)
6531 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6532 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6533 goto theend;
6534 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6535 + current_funccal->fc_defer.ga_len++;
6536 dr->dr_name = saved_name;
6537 dr->dr_argcount = argcount;
6538 while (argcount > 0)
6539 {
6540 --argcount;
6541 dr->dr_argvars[argcount] = argvars[argcount];
6542 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006543 }
6544 ret = OK;
6545
6546theend:
6547 while (--argcount >= 0)
6548 clear_tv(&argvars[argcount]);
6549 return ret;
6550}
6551
6552/*
6553 * Invoked after a functions has finished: invoke ":defer" functions.
6554 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006555 static void
6556handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006557{
6558 int idx;
6559
Bram Moolenaar58779852022-09-06 18:31:14 +01006560 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006561 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006562 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006563
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006564 if (dr->dr_name == NULL)
6565 // already being called, can happen if function does ":qa"
6566 continue;
6567
6568 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006569 CLEAR_FIELD(funcexe);
6570 funcexe.fe_evaluate = TRUE;
6571
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006572 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006573 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006574
6575 char_u *name = dr->dr_name;
6576 dr->dr_name = NULL;
6577
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006578 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006579 // first statement in the function will be executed (because of the
6580 // exception). So save and restore the try/catch/throw exception
6581 // state.
6582 exception_state_T estate;
6583 exception_state_save(&estate);
6584 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006585
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006586 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6587
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006588 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006589
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006590 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006591 vim_free(name);
6592 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006593 clear_tv(&dr->dr_argvars[i]);
6594 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006595 ga_clear(&funccal->fc_defer);
6596}
6597
zeertzjq960cf912023-04-18 21:52:54 +01006598 static void
6599invoke_funccall_defer(funccall_T *fc)
6600{
6601 if (fc->fc_ectx != NULL)
6602 {
6603 // :def function
6604 unwind_def_callstack(fc->fc_ectx);
6605 may_invoke_defer_funcs(fc->fc_ectx);
6606 }
6607 else
6608 {
6609 // legacy function
6610 handle_defer_one(fc);
6611 }
6612}
6613
Bram Moolenaar58779852022-09-06 18:31:14 +01006614/*
6615 * Called when exiting: call all defer functions.
6616 */
6617 void
6618invoke_all_defer(void)
6619{
zeertzjq1be4b812023-04-19 14:21:24 +01006620 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6621 invoke_funccall_defer(fc);
6622
zeertzjq960cf912023-04-18 21:52:54 +01006623 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6624 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6625 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006626}
6627
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006628/*
6629 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006630 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006631 */
6632 void
6633ex_call(exarg_T *eap)
6634{
6635 char_u *arg = eap->arg;
6636 char_u *startarg;
6637 char_u *name;
6638 char_u *tofree;
6639 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006640 int failed = FALSE;
6641 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006642 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006643 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006644 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006645 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006646 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006647 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006648
Bram Moolenaare6b53242020-07-01 17:28:33 +02006649 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006650 if (eap->skip)
6651 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006652 typval_T rettv;
6653
Bram Moolenaare38eab22019-12-05 21:50:01 +01006654 // trans_function_name() doesn't work well when skipping, use eval0()
6655 // instead to skip to any following command, e.g. for:
6656 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006657 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006658 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006659 clear_tv(&rettv);
6660 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006661 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006662 return;
6663 }
6664
zeertzjq5299c092023-04-12 20:48:16 +01006665 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006666 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006667 if (fudi.fd_newkey != NULL)
6668 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006669 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006670 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006671 vim_free(fudi.fd_newkey);
6672 }
6673 if (tofree == NULL)
6674 return;
6675
Bram Moolenaare38eab22019-12-05 21:50:01 +01006676 // Increase refcount on dictionary, it could get deleted when evaluating
6677 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006678 if (fudi.fd_dict != NULL)
6679 ++fudi.fd_dict->dv_refcount;
6680
Bram Moolenaare38eab22019-12-05 21:50:01 +01006681 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6682 // contents. For VAR_PARTIAL get its partial, unless we already have one
6683 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006684 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006685 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006686 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006687 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006688
Bram Moolenaare38eab22019-12-05 21:50:01 +01006689 // Skip white space to allow ":call func ()". Not good, but required for
6690 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006691 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006692 if (*startarg != '(')
6693 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006694 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006695 goto end;
6696 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006697 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006698 {
6699 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6700 goto end;
6701 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006702
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006703 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006704 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006705 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006706 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006707 }
6708 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006709 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006710 funcexe_T funcexe;
6711
Bram Moolenaara80faa82020-04-12 19:37:17 +02006712 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006713 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006714 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006715 funcexe.fe_partial = partial;
6716 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006717 funcexe.fe_firstline = eap->line1;
6718 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006719 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006720 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006721 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006722 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006723
Bram Moolenaar1d341892021-09-18 15:25:52 +02006724 // When inside :try we need to check for following "| catch" or "| endtry".
6725 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006726 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006727 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006728 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006729 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006730 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006731 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006732 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006733 {
6734 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006735 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006736 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006737 }
6738 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006739 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006740 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006741 // Must be after using "arg", it may point into memory cleared here.
6742 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006743
6744end:
6745 dict_unref(fudi.fd_dict);
6746 vim_free(tofree);
6747}
6748
6749/*
6750 * Return from a function. Possibly makes the return pending. Also called
6751 * for a pending return at the ":endtry" or after returning from an extra
6752 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6753 * when called due to a ":return" command. "rettv" may point to a typval_T
6754 * with the return rettv. Returns TRUE when the return can be carried out,
6755 * FALSE when the return gets pending.
6756 */
6757 int
6758do_return(
6759 exarg_T *eap,
6760 int reanimate,
6761 int is_cmd,
6762 void *rettv)
6763{
6764 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006765 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006766
6767 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006768 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006769 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006770
6771 /*
6772 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6773 * not in its finally clause (which then is to be executed next) is found.
6774 * In this case, make the ":return" pending for execution at the ":endtry".
6775 * Otherwise, return normally.
6776 */
6777 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6778 if (idx >= 0)
6779 {
6780 cstack->cs_pending[idx] = CSTP_RETURN;
6781
6782 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006783 // A pending return again gets pending. "rettv" points to an
6784 // allocated variable with the rettv of the original ":return"'s
6785 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006786 cstack->cs_rettv[idx] = rettv;
6787 else
6788 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006789 // When undoing a return in order to make it pending, get the stored
6790 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006791 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006792 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006793
6794 if (rettv != NULL)
6795 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006796 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006797 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6798 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6799 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006800 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006801 }
6802 else
6803 cstack->cs_rettv[idx] = NULL;
6804
6805 if (reanimate)
6806 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006807 // The pending return value could be overwritten by a ":return"
6808 // without argument in a finally clause; reset the default
6809 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006810 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6811 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006812 }
6813 }
6814 report_make_pending(CSTP_RETURN, rettv);
6815 }
6816 else
6817 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006818 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006819
Bram Moolenaare38eab22019-12-05 21:50:01 +01006820 // If the return is carried out now, store the return value. For
6821 // a return immediately after reanimation, the value is already
6822 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006823 if (!reanimate && rettv != NULL)
6824 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006825 clear_tv(current_funccal->fc_rettv);
6826 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006827 if (!is_cmd)
6828 vim_free(rettv);
6829 }
6830 }
6831
6832 return idx < 0;
6833}
6834
6835/*
6836 * Free the variable with a pending return value.
6837 */
6838 void
6839discard_pending_return(void *rettv)
6840{
6841 free_tv((typval_T *)rettv);
6842}
6843
6844/*
6845 * Generate a return command for producing the value of "rettv". The result
6846 * is an allocated string. Used by report_pending() for verbose messages.
6847 */
6848 char_u *
6849get_return_cmd(void *rettv)
6850{
6851 char_u *s = NULL;
zeertzjq21012302025-02-02 08:55:57 +01006852 char_u *tofree = NULL;
6853 char_u numbuf[NUMBUFLEN];
John Marriottb32800f2025-02-01 15:25:34 +01006854 size_t slen = 0;
6855 size_t IObufflen;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006856
6857 if (rettv != NULL)
6858 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6859 if (s == NULL)
6860 s = (char_u *)"";
John Marriottb32800f2025-02-01 15:25:34 +01006861 else
6862 slen = STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006863
6864 STRCPY(IObuff, ":return ");
6865 STRNCPY(IObuff + 8, s, IOSIZE - 8);
John Marriottb32800f2025-02-01 15:25:34 +01006866 IObufflen = 8 + slen;
zeertzjq21012302025-02-02 08:55:57 +01006867 if (IObufflen >= IOSIZE)
John Marriottb32800f2025-02-01 15:25:34 +01006868 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006869 STRCPY(IObuff + IOSIZE - 4, "...");
zeertzjq21012302025-02-02 08:55:57 +01006870 IObufflen = IOSIZE - 1;
John Marriottb32800f2025-02-01 15:25:34 +01006871 }
zeertzjq21012302025-02-02 08:55:57 +01006872 vim_free(tofree);
John Marriottb32800f2025-02-01 15:25:34 +01006873 return vim_strnsave(IObuff, IObufflen);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006874}
6875
6876/*
6877 * Get next function line.
6878 * Called by do_cmdline() to get the next line.
6879 * Returns allocated string, or NULL for end of function.
6880 */
6881 char_u *
6882get_func_line(
6883 int c UNUSED,
6884 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006885 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006886 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006887{
6888 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006889 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006890 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006891 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006892
Bram Moolenaare38eab22019-12-05 21:50:01 +01006893 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006894 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006895 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006896 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006897 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006898 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006899 }
6900#ifdef FEAT_PROFILE
6901 if (do_profiling == PROF_YES)
6902 func_line_end(cookie);
6903#endif
6904
6905 gap = &fp->uf_lines;
6906 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006907 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006908 retval = NULL;
6909 else
6910 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006911 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006912 while (fcp->fc_linenr < gap->ga_len
6913 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6914 ++fcp->fc_linenr;
6915 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006916 retval = NULL;
6917 else
6918 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006919 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6920 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006921#ifdef FEAT_PROFILE
6922 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006923 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006924#endif
6925 }
6926 }
6927
Bram Moolenaare38eab22019-12-05 21:50:01 +01006928 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006929 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006930 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006931 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006932 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006933 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006934 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006935 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006936 }
6937
6938 return retval;
6939}
6940
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006941/*
6942 * Return TRUE if the currently active function should be ended, because a
6943 * return was encountered or an error occurred. Used inside a ":while".
6944 */
6945 int
6946func_has_ended(void *cookie)
6947{
6948 funccall_T *fcp = (funccall_T *)cookie;
6949
Bram Moolenaare38eab22019-12-05 21:50:01 +01006950 // Ignore the "abort" flag if the abortion behavior has been changed due to
6951 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006952 return (((fcp->fc_func->uf_flags & FC_ABORT)
6953 && did_emsg && !aborted_in_try())
6954 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006955}
6956
6957/*
6958 * return TRUE if cookie indicates a function which "abort"s on errors.
6959 */
6960 int
6961func_has_abort(
6962 void *cookie)
6963{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006964 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006965}
6966
6967
6968/*
6969 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6970 * Don't do this when "Func" is already a partial that was bound
6971 * explicitly (pt_auto is FALSE).
6972 * Changes "rettv" in-place.
6973 * Returns the updated "selfdict_in".
6974 */
6975 dict_T *
6976make_partial(dict_T *selfdict_in, typval_T *rettv)
6977{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006978 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006979 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006980 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006981 dict_T *selfdict = selfdict_in;
6982
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006983 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6984 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006985 fp = rettv->vval.v_partial->pt_func;
6986 else
6987 {
6988 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006989 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006990 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006991 if (fname == NULL)
6992 {
6993 // There is no point binding a dict to a NULL function, just create
6994 // a function reference.
6995 rettv->v_type = VAR_FUNC;
6996 rettv->vval.v_string = NULL;
6997 }
6998 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006999 {
7000 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007001 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00007002
7003 // Translate "s:func" to the stored function name.
7004 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
7005 fp = find_func(fname, FALSE);
7006 vim_free(tofree);
7007 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007008 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007009
Bram Moolenaared0c62e2022-03-08 19:43:55 +00007010 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007011 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007012 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007013
7014 if (pt != NULL)
7015 {
7016 pt->pt_refcount = 1;
7017 pt->pt_dict = selfdict;
7018 pt->pt_auto = TRUE;
7019 selfdict = NULL;
7020 if (rettv->v_type == VAR_FUNC)
7021 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01007022 // Just a function: Take over the function name and use
7023 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007024 pt->pt_name = rettv->vval.v_string;
7025 }
7026 else
7027 {
7028 partial_T *ret_pt = rettv->vval.v_partial;
7029 int i;
7030
Bram Moolenaare38eab22019-12-05 21:50:01 +01007031 // Partial: copy the function name, use selfdict and copy
7032 // args. Can't take over name or args, the partial might
7033 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007034 if (ret_pt->pt_name != NULL)
7035 {
7036 pt->pt_name = vim_strsave(ret_pt->pt_name);
7037 func_ref(pt->pt_name);
7038 }
7039 else
7040 {
7041 pt->pt_func = ret_pt->pt_func;
7042 func_ptr_ref(pt->pt_func);
7043 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007044 if (ret_pt->pt_argc > 0)
7045 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007046 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007047 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01007048 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007049 pt->pt_argc = 0;
7050 else
7051 {
7052 pt->pt_argc = ret_pt->pt_argc;
7053 for (i = 0; i < pt->pt_argc; i++)
7054 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
7055 }
7056 }
7057 partial_unref(ret_pt);
7058 }
7059 rettv->v_type = VAR_PARTIAL;
7060 rettv->vval.v_partial = pt;
7061 }
7062 }
7063 return selfdict;
7064}
7065
7066/*
7067 * Return the name of the executed function.
7068 */
7069 char_u *
7070func_name(void *cookie)
7071{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007072 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007073}
7074
7075/*
7076 * Return the address holding the next breakpoint line for a funccall cookie.
7077 */
7078 linenr_T *
7079func_breakpoint(void *cookie)
7080{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007081 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007082}
7083
7084/*
7085 * Return the address holding the debug tick for a funccall cookie.
7086 */
7087 int *
7088func_dbg_tick(void *cookie)
7089{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007090 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007091}
7092
7093/*
7094 * Return the nesting level for a funccall cookie.
7095 */
7096 int
7097func_level(void *cookie)
7098{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007099 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007100}
7101
7102/*
7103 * Return TRUE when a function was ended by a ":return" command.
7104 */
7105 int
7106current_func_returned(void)
7107{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007108 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007109}
7110
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007111 int
7112free_unref_funccal(int copyID, int testing)
7113{
7114 int did_free = FALSE;
7115 int did_free_funccal = FALSE;
7116 funccall_T *fc, **pfc;
7117
7118 for (pfc = &previous_funccal; *pfc != NULL; )
7119 {
7120 if (can_free_funccal(*pfc, copyID))
7121 {
7122 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007123 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01007124 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007125 did_free = TRUE;
7126 did_free_funccal = TRUE;
7127 }
7128 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01007129 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007130 }
7131 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01007132 // When a funccal was freed some more items might be garbage
7133 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007134 (void)garbage_collect(testing);
7135
7136 return did_free;
7137}
7138
7139/*
Bram Moolenaarba209902016-08-24 22:06:38 +02007140 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007141 */
7142 static funccall_T *
7143get_funccal(void)
7144{
7145 int i;
7146 funccall_T *funccal;
7147 funccall_T *temp_funccal;
7148
7149 funccal = current_funccal;
7150 if (debug_backtrace_level > 0)
7151 {
7152 for (i = 0; i < debug_backtrace_level; i++)
7153 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007154 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007155 if (temp_funccal)
7156 funccal = temp_funccal;
7157 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01007158 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007159 debug_backtrace_level = i;
7160 }
7161 }
7162 return funccal;
7163}
7164
7165/*
7166 * Return the hashtable used for local variables in the current funccal.
7167 * Return NULL if there is no current funccal.
7168 */
7169 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007170get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007171{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007172 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007173 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007174 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007175}
7176
7177/*
7178 * Return the l: scope variable.
7179 * Return NULL if there is no current funccal.
7180 */
7181 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007182get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007183{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007184 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007185 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007186 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007187}
7188
7189/*
7190 * Return the hashtable used for argument in the current funccal.
7191 * Return NULL if there is no current funccal.
7192 */
7193 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007194get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007195{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007196 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007197 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007198 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007199}
7200
7201/*
7202 * Return the a: scope variable.
7203 * Return NULL if there is no current funccal.
7204 */
7205 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007206get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007207{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007208 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007209 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007210 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007211}
7212
7213/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007214 * List function variables, if there is a function.
7215 */
7216 void
7217list_func_vars(int *first)
7218{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007219 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
7220 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01007221 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007222}
7223
7224/*
7225 * If "ht" is the hashtable for local variables in the current funccal, return
7226 * the dict that contains it.
7227 * Otherwise return NULL.
7228 */
7229 dict_T *
7230get_current_funccal_dict(hashtab_T *ht)
7231{
7232 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01007233 && ht == &current_funccal->fc_l_vars.dv_hashtab)
7234 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007235 return NULL;
7236}
7237
7238/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007239 * Search hashitem in parent scope.
7240 */
7241 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007242find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007243{
7244 funccall_T *old_current_funccal = current_funccal;
7245 hashtab_T *ht;
7246 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007247 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007248
Bram Moolenaarca16c602022-09-06 18:57:08 +01007249 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Yegappan Lakshmanane89aef32025-05-14 20:31:55 +02007250 return NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007251
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02007252 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01007253 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02007254 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007255 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007256 ht = find_var_ht(name, &varname);
7257 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02007258 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007259 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02007260 if (!HASHITEM_EMPTY(hi))
7261 {
7262 *pht = ht;
7263 break;
7264 }
7265 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007266 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02007267 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007268 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007269 }
7270 current_funccal = old_current_funccal;
7271
7272 return hi;
7273}
7274
7275/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007276 * Search variable in parent scope.
7277 */
7278 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007279find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007280{
7281 dictitem_T *v = NULL;
7282 funccall_T *old_current_funccal = current_funccal;
7283 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007284 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007285
Bram Moolenaarca16c602022-09-06 18:57:08 +01007286 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007287 return NULL;
7288
Bram Moolenaare38eab22019-12-05 21:50:01 +01007289 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01007290 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007291 while (current_funccal)
7292 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007293 ht = find_var_ht(name, &varname);
7294 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007295 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007296 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007297 if (v != NULL)
7298 break;
7299 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007300 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007301 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007302 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007303 }
7304 current_funccal = old_current_funccal;
7305
7306 return v;
7307}
7308
7309/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007310 * Set "copyID + 1" in previous_funccal and callers.
7311 */
7312 int
7313set_ref_in_previous_funccal(int copyID)
7314{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007315 funccall_T *fc;
7316
Bram Moolenaarca16c602022-09-06 18:57:08 +01007317 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007318 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007319 fc->fc_copyID = copyID + 1;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01007320 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL, NULL)
7321 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL, NULL)
7322 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007323 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007324 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007325 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007326}
7327
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007328 static int
7329set_ref_in_funccal(funccall_T *fc, int copyID)
7330{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007331 if (fc->fc_copyID != copyID)
7332 {
7333 fc->fc_copyID = copyID;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01007334 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL, NULL)
7335 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL, NULL)
7336 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL, NULL)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007337 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007338 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007339 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007340 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007341}
7342
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007343/*
7344 * Set "copyID" in all local vars and arguments in the call stack.
7345 */
7346 int
7347set_ref_in_call_stack(int copyID)
7348{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007349 funccall_T *fc;
7350 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007351
Bram Moolenaarca16c602022-09-06 18:57:08 +01007352 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007353 if (set_ref_in_funccal(fc, copyID))
7354 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007355
7356 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007357 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007358 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007359 if (set_ref_in_funccal(fc, copyID))
7360 return TRUE;
7361 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007362}
7363
7364/*
7365 * Set "copyID" in all functions available by name.
7366 */
7367 int
7368set_ref_in_functions(int copyID)
7369{
7370 int todo;
7371 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007372 ufunc_T *fp;
7373
7374 todo = (int)func_hashtab.ht_used;
7375 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007376 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007377 if (!HASHITEM_EMPTY(hi))
7378 {
7379 --todo;
7380 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007381 if (!func_name_refcount(fp->uf_name)
7382 && set_ref_in_func(NULL, fp, copyID))
7383 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007384 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007385 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007386 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007387}
7388
7389/*
7390 * Set "copyID" in all function arguments.
7391 */
7392 int
7393set_ref_in_func_args(int copyID)
7394{
7395 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007396
7397 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007398 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01007399 copyID, NULL, NULL, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007400 return TRUE;
7401 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007402}
7403
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007404/*
7405 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007406 * Returns TRUE if setting references failed somehow.
7407 */
7408 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007409set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007410{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007411 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007412 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007413 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007414 char_u fname_buf[FLEN_FIXED + 1];
7415 char_u *tofree = NULL;
7416 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007417 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007418
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007419 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007420 return FALSE;
7421
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007422 if (fp_in == NULL)
7423 {
7424 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007425 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007426 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007427 if (fp != NULL)
7428 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007429 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007430 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007431 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007432
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007433 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007434 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007435}
7436
Bram Moolenaare38eab22019-12-05 21:50:01 +01007437#endif // FEAT_EVAL