blob: a60eeb2d76a0d2a19e4a735b7da96e5ecd97e3e8 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +020011 * userfunc.c: User defined function support
Bram Moolenaara9b579f2016-07-17 18:29:19 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020017/*
18 * All user-defined functions are found in this hashtable.
19 */
20static hashtab_T func_hashtab;
21
Bram Moolenaare38eab22019-12-05 21:50:01 +010022// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020023static garray_T funcargs = GA_EMPTY;
24
Bram Moolenaar209b8e32019-03-14 13:43:24 +010025// pointer to funccal for currently active function
26static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020027
Bram Moolenaar209b8e32019-03-14 13:43:24 +010028// Pointer to list of previously used funccal, still around because some
29// item in it is still being used.
30static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020031
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020032static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +010033static void func_clear(ufunc_T *fp, int force);
34static int func_free(ufunc_T *fp, int force);
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +010035static char_u *untrans_function_name(char_u *name);
Bram Moolenaar58779852022-09-06 18:31:14 +010036static void handle_defer_one(funccall_T *funccal);
Yegappan Lakshmanana04003a2024-10-27 21:54:11 +010037static char_u *trans_function_name_ext(char_u **pp, int *is_global, int skip, int flags, funcdict_T *fdp, partial_T **partial, type_T **type, ufunc_T **ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020038
39 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +000040func_init(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020041{
42 hash_init(&func_hashtab);
43}
44
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020045/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020046 * Return the function hash table
47 */
48 hashtab_T *
49func_tbl_get(void)
50{
51 return &func_hashtab;
52}
53
54/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020055 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020056 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010057 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010058 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaardce24412022-02-08 20:35:30 +000059 * If "eap" is not NULL use it to check for an already declared name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010060 * Return a pointer to after the type.
61 * When something is wrong return "arg".
62 */
63 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010064one_function_arg(
65 char_u *arg,
66 garray_T *newargs,
67 garray_T *argtypes,
68 int types_optional,
h-eastb895b0f2023-09-24 15:46:31 +020069 garray_T *arg_objm,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010070 evalarg_T *evalarg,
Bram Moolenaardce24412022-02-08 20:35:30 +000071 exarg_T *eap,
Bram Moolenaar2a389082021-04-09 20:24:31 +020072 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010073 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074{
Bram Moolenaar6e949782020-04-13 17:21:00 +020075 char_u *p = arg;
76 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020077 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010078
79 while (ASCII_ISALNUM(*p) || *p == '_')
80 ++p;
Keith Thompson184f71c2024-01-04 21:19:04 +010081 if (arg == p || SAFE_isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020082 || (argtypes == NULL
83 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
84 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085 {
86 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000087 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 return arg;
89 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010090
Bram Moolenaarce723f32023-06-10 19:00:12 +010091 // Extra checks in Vim9 script.
92 if (!skip && argtypes != NULL)
93 {
94 int c = *p;
95 *p = NUL;
96 int r = check_reserved_name(arg, FALSE);
97 *p = c;
98 if (r == FAIL)
99 return arg;
100
101 // Cannot use script var name for argument. In function: also check
102 // local vars and arguments.
103 if (check_defined(arg, p - arg,
104 evalarg == NULL ? NULL : evalarg->eval_cctx,
Bram Moolenaardce24412022-02-08 20:35:30 +0000105 eap == NULL ? NULL : eap->cstack, TRUE) == FAIL)
Bram Moolenaarce723f32023-06-10 19:00:12 +0100106 return arg;
107 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100108
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100109 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
110 return arg;
111 if (newargs != NULL)
112 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113 int c;
114 int i;
115
116 c = *p;
117 *p = NUL;
118 arg_copy = vim_strsave(arg);
119 if (arg_copy == NULL)
120 {
121 *p = c;
122 return arg;
123 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200124 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200125 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200126 // Check for duplicate argument name.
127 for (i = 0; i < newargs->ga_len; ++i)
128 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
129 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000130 semsg(_(e_duplicate_argument_name_str), arg_copy);
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200131 vim_free(arg_copy);
132 return arg;
133 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100134 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
135 newargs->ga_len++;
136
137 *p = c;
138 }
139
140 // get any type from "arg: type"
h-eastb895b0f2023-09-24 15:46:31 +0200141 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK)
142 && arg_objm != NULL && (skip || ga_grow(arg_objm, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143 {
144 char_u *type = NULL;
145
Bram Moolenaar6e949782020-04-13 17:21:00 +0200146 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
147 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200148 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200149 arg_copy == NULL ? arg : arg_copy);
150 p = skipwhite(p);
151 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100152 if (*p == ':')
153 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200154 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100155 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200156 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100157 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200158 return arg;
159 }
160 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200161 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100162 if (!skip)
163 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200165 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200166 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200167 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200168 arg_copy == NULL ? arg : arg_copy);
169 return arg;
170 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100171 if (!skip)
172 {
173 if (type == NULL && types_optional)
John Marriottb32800f2025-02-01 15:25:34 +0100174 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100175 // lambda arguments default to "any" type
John Marriottb32800f2025-02-01 15:25:34 +0100176 if (is_vararg)
177 type = vim_strnsave((char_u *)"list<any>", 9);
178 else
179 type = vim_strnsave((char_u *)"any", 3);
180 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100181 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
h-eastb895b0f2023-09-24 15:46:31 +0200182 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = FALSE;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100183 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100184 }
185
186 return p;
187}
188
189/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000190 * Handle line continuation in function arguments or body.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000191 * Get a next line, store it in "eap" if appropriate and put the line in
192 * "lines_to_free" to free the line later.
Bram Moolenaar7473a842021-12-28 17:55:26 +0000193 */
194 static char_u *
195get_function_line(
196 exarg_T *eap,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000197 garray_T *lines_to_free,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000198 int indent,
199 getline_opt_T getline_options)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000200{
201 char_u *theline;
202
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100203 if (eap->ea_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +0000204 theline = getcmdline(':', 0L, indent, 0);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000205 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100206 theline = eap->ea_getline(':', eap->cookie, indent, getline_options);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000207 if (theline != NULL)
208 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000209 if (lines_to_free->ga_len > 0
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000210 && eap->cmdlinep != NULL
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000211 && *eap->cmdlinep == ((char_u **)lines_to_free->ga_data)
212 [lines_to_free->ga_len - 1])
Bram Moolenaar7473a842021-12-28 17:55:26 +0000213 *eap->cmdlinep = theline;
Bram Moolenaarb5820102023-01-25 12:27:13 +0000214 (void)ga_add_string(lines_to_free, theline);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000215 }
216
217 return theline;
218}
219
220/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200221 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100222 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100223 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200224 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100225 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200226get_function_args(
227 char_u **argp,
228 char_u endchar,
229 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100230 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100231 int types_optional, // types optional if "argtypes" is not NULL
h-eastb895b0f2023-09-24 15:46:31 +0200232 garray_T *arg_objm, // NULL unless using :def
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100233 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200234 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200235 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200236 int skip,
Bram Moolenaardce24412022-02-08 20:35:30 +0000237 exarg_T *eap, // can be NULL
Bram Moolenaar554d0312023-01-05 19:59:18 +0000238 int in_class, // non-zero when inside a class or interface
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000239 garray_T *newlines, // function body lines
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000240 garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200241{
242 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100243 char_u *arg;
244 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200245 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200246 int any_default = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100247 char_u *whitep = *argp;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100248 int need_expr = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200249
250 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000251 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000253 ga_init2(argtypes, sizeof(char_u *), 3);
h-eastb895b0f2023-09-24 15:46:31 +0200254 if (arg_objm != NULL)
255 ga_init2(arg_objm, sizeof(int8_T), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200256 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000257 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200258
259 if (varargs != NULL)
260 *varargs = FALSE;
261
262 /*
263 * Isolate the arguments: "arg1, arg2, ...)"
264 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100265 arg = skipwhite(*argp);
266 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200267 while (*p != endchar)
268 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100269 while (eap != NULL && eap->ea_getline != NULL
Bram Moolenaar2c330432020-04-13 14:41:35 +0200270 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200271 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200272 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000273 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000274 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000275
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200276 if (theline == NULL)
277 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200278 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200279 p = skipwhite(theline);
280 }
281
282 if (mustend && *p != endchar)
283 {
284 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000285 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100286 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200287 }
Christian Brabandte4a450a2023-12-08 20:57:38 +0100288 if (*p == endchar && !need_expr)
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200289 break;
290
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200291 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
292 {
293 if (varargs != NULL)
294 *varargs = TRUE;
295 p += 3;
296 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297
298 if (argtypes != NULL)
299 {
300 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200301 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100302 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100303 if (!skip)
304 emsg(_(e_missing_name_after_dots));
305 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100306 }
307
308 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100309 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200310 arg_objm, evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100311 if (p == arg)
312 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100313 if (*skipwhite(p) == '=')
314 {
315 emsg(_(e_cannot_use_default_for_variable_arguments));
316 break;
317 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100318 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200319 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000320 else if (in_class && STRNCMP(p, "this.", 5) == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000321 {
322 // this.memberName
323 p += 5;
324 arg = p;
325 while (ASCII_ISALNUM(*p) || *p == '_')
326 ++p;
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000327 char_u *argend = p;
328
h-eastdb385522023-09-28 22:18:19 +0200329 // object variable this. can be used only in a constructor
330 if (STRNCMP(eap->arg, "new", 3) != 0)
331 {
332 c = *argend;
333 *argend = NUL;
334 semsg(_(e_cannot_use_an_object_variable_except_with_the_new_method_str), arg);
335 *argend = c;
336 break;
337 }
338
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000339 if (*skipwhite(p) == '=')
340 {
341 char_u *defval = skipwhite(skipwhite(p) + 1);
342 if (STRNCMP(defval, "v:none", 6) != 0)
343 {
344 semsg(_(e_constructor_default_value_must_be_vnone_str), p);
345 goto err_ret;
346 }
347 any_default = TRUE;
348 p = defval + 6;
349
350 if (ga_grow(default_args, 1) == FAIL)
351 goto err_ret;
352
John Marriottb32800f2025-02-01 15:25:34 +0100353 char_u *expr = vim_strnsave((char_u *)"v:none", 6);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000354 if (expr == NULL)
355 goto err_ret;
356 ((char_u **)(default_args->ga_data))
357 [default_args->ga_len] = expr;
358 default_args->ga_len++;
359 }
360 else if (any_default)
361 {
362 emsg(_(e_non_default_argument_follows_default_argument));
363 goto err_ret;
364 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000365
366 // TODO: check the argument is indeed a member
367 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
368 return FAIL;
369 if (newargs != NULL)
370 {
371 ((char_u **)(newargs->ga_data))[newargs->ga_len] =
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000372 vim_strnsave(arg, argend - arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000373 newargs->ga_len++;
374
h-eastb895b0f2023-09-24 15:46:31 +0200375 if (argtypes != NULL && ga_grow(argtypes, 1) == OK
376 && arg_objm != NULL && ga_grow(arg_objm, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000377 {
378 // TODO: use the actual type
379 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] =
John Marriottb32800f2025-02-01 15:25:34 +0100380 vim_strnsave((char_u *)"any", 3);
h-eastb895b0f2023-09-24 15:46:31 +0200381 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000382
383 // Add a line to the function body for the assignment.
384 if (ga_grow(newlines, 1) == OK)
385 {
386 // "this.name = name"
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000387 int len = 5 + (argend - arg) + 3 + (argend - arg) + 1;
388 if (any_default)
389 len += 14 + 10;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000390 char_u *assignment = alloc(len);
391 if (assignment != NULL)
392 {
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000393 c = *argend;
394 *argend = NUL;
395 if (any_default)
396 vim_snprintf((char *)assignment, len,
397 "ifargisset %d this.%s = %s",
398 default_args->ga_len - 1, arg, arg);
399 else
400 vim_snprintf((char *)assignment, len,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000401 "this.%s = %s", arg, arg);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000402 *argend = c;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000403 ((char_u **)(newlines->ga_data))[
404 newlines->ga_len++] = assignment;
405 }
406 }
407 }
408 }
409 if (*p == ',')
410 ++p;
411 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200412 else
413 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200414 char_u *np;
415
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200416 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100417 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200418 arg_objm, evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100419 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200420 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200421
Bram Moolenaar015cf102021-06-26 21:52:02 +0200422 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200423 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200424 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200425 if (*np == '=' && np[1] != '=' && np[1] != '~'
426 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200427 {
428 typval_T rettv;
429
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100430 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200431 any_default = TRUE;
Bram Moolenaareb5adf12022-09-04 13:41:37 +0100432 p = skipwhite(np + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000433 char_u *expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200434 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200435 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200436 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200437 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200438 if (ga_grow(default_args, 1) == FAIL)
439 goto err_ret;
440
Christian Brabandte4a450a2023-12-08 20:57:38 +0100441 if (need_expr)
442 need_expr = FALSE;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200443 // trim trailing whitespace
444 while (p > expr && VIM_ISWHITE(p[-1]))
445 p--;
446 c = *p;
447 *p = NUL;
448 expr = vim_strsave(expr);
449 if (expr == NULL)
450 {
451 *p = c;
452 goto err_ret;
453 }
454 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200455 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200456 default_args->ga_len++;
457 *p = c;
458 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200459 }
460 else
Christian Brabandte4a450a2023-12-08 20:57:38 +0100461 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200462 mustend = TRUE;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100463 if (*skipwhite(p) == NUL)
464 need_expr = TRUE;
465 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200466 }
467 else if (any_default)
468 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000469 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200470 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200471 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200472
473 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
474 {
475 // Be tolerant when skipping
476 if (!skip)
477 {
478 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
479 goto err_ret;
480 }
481 p = skipwhite(p);
482 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200483 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200484 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200485 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200486 // Don't give this error when skipping, it makes the "->" not
487 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100488 // Allow missing space after comma in legacy functions.
489 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200490 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
491 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100492 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200493 goto err_ret;
494 }
495 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200496 else
497 mustend = TRUE;
498 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200499 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200500 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200501 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200502
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200503 if (*p != endchar)
504 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100505 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200506
507 *argp = p;
508 return OK;
509
510err_ret:
511 if (newargs != NULL)
512 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200513 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200514 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200515 return FAIL;
516}
517
518/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100519 * Parse the argument types, filling "fp->uf_arg_types".
520 * Return OK or FAIL.
521 */
522 static int
h-eastb895b0f2023-09-24 15:46:31 +0200523parse_argument_types(
524 ufunc_T *fp,
525 garray_T *argtypes,
526 int varargs,
527 garray_T *arg_objm,
528 ocmember_T *obj_members,
529 int obj_member_count)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100530{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200531 int len = 0;
532
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100533 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
534 if (argtypes->ga_len > 0)
535 {
536 // When "varargs" is set the last name/type goes into uf_va_name
537 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200538 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100539
540 if (len > 0)
541 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
542 if (fp->uf_arg_types != NULL)
543 {
544 int i;
545 type_T *type;
546
547 for (i = 0; i < len; ++ i)
548 {
549 char_u *p = ((char_u **)argtypes->ga_data)[i];
550
551 if (p == NULL)
552 // will get the type from the default value
553 type = &t_unknown;
554 else
h-eastb895b0f2023-09-24 15:46:31 +0200555 {
556 if (arg_objm != NULL && ((int8_T *)arg_objm->ga_data)[i])
557 {
558 char_u *aname = ((char_u **)fp->uf_args.ga_data)[i];
559
560 type = &t_any;
561 for (int om = 0; om < obj_member_count; ++om)
562 {
Christian Brabandt915f3bf2024-04-05 20:12:19 +0200563 if (obj_members != NULL
564 && STRCMP(aname,
565 obj_members[om].ocm_name) == 0)
h-eastb895b0f2023-09-24 15:46:31 +0200566 {
567 type = obj_members[om].ocm_type;
568 break;
569 }
570 }
571 }
572 else
573 type = parse_type(&p, &fp->uf_type_list, TRUE);
574 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100575 if (type == NULL)
576 return FAIL;
577 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000578 if (i < fp->uf_args.ga_len
579 && (type->tt_type == VAR_FUNC
Yegappan Lakshmanan3e336502024-04-04 19:35:59 +0200580 || type->tt_type == VAR_PARTIAL))
581 {
582 char_u *name = ((char_u **)fp->uf_args.ga_data)[i];
583 if (obj_members != NULL && *name == '_')
584 // protected object method
585 name++;
586
587 if (var_wrong_func_name(name, TRUE))
588 return FAIL;
589 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100590 }
591 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100592 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200593
594 if (varargs)
595 {
596 char_u *p;
597
598 // Move the last argument "...name: type" to uf_va_name and
599 // uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200600 --fp->uf_args.ga_len;
Bram Moolenaarf0571712023-01-04 13:16:20 +0000601 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len];
602 ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len] = NULL;
Bram Moolenaar2a389082021-04-09 20:24:31 +0200603 p = ((char_u **)argtypes->ga_data)[len];
604 if (p == NULL)
605 // TODO: get type from default value
606 fp->uf_va_type = &t_list_any;
607 else
608 {
609 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
610 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
611 {
612 semsg(_(e_variable_arguments_type_must_be_list_str),
613 ((char_u **)argtypes->ga_data)[len]);
614 return FAIL;
615 }
616 }
617 if (fp->uf_va_type == NULL)
618 return FAIL;
619 }
620
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100621 return OK;
622}
623
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100624 static int
625parse_return_type(ufunc_T *fp, char_u *ret_type)
626{
627 if (ret_type == NULL)
628 fp->uf_ret_type = &t_void;
629 else
630 {
631 char_u *p = ret_type;
632
633 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
634 if (fp->uf_ret_type == NULL)
635 {
636 fp->uf_ret_type = &t_void;
637 return FAIL;
638 }
639 }
640 return OK;
641}
642
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100643/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200644 * Register function "fp" as using "current_funccal" as its scope.
645 */
646 static int
647register_closure(ufunc_T *fp)
648{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200649 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100650 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200651 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200652 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200653 fp->uf_scoped = current_funccal;
654 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200655
Bram Moolenaarca16c602022-09-06 18:57:08 +0100656 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200657 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100658 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
659 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200660 return OK;
661}
662
663/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100664 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
665 * return "buf" filled with a readable function name.
666 * Otherwise just return "name", thus the return value can always be used.
667 * "name" and "buf" may be equal.
668 */
669 char_u *
670make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
671{
672 size_t len;
673
674 if (name[0] != K_SPECIAL)
675 return name;
676 len = STRLEN(name);
677 if (len + 3 > bufsize)
678 return name;
679
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100680 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100681 mch_memmove(buf, "<SNR>", 5);
682 return buf;
683}
684
John Marriottb32800f2025-02-01 15:25:34 +0100685static char_u lambda_name[8 + NUMBUFLEN];
686static size_t lambda_namelen = 0;
687
zeertzjq21012302025-02-02 08:55:57 +0100688/*
689 * Get a name for a lambda. Returned in static memory.
690 */
Bram Moolenaar04b12692020-05-04 23:24:44 +0200691 char_u *
692get_lambda_name(void)
693{
John Marriottb32800f2025-02-01 15:25:34 +0100694 static int lambda_no = 0;
695 int n;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200696
John Marriottb32800f2025-02-01 15:25:34 +0100697 n = vim_snprintf((char *)lambda_name, sizeof(lambda_name), "<lambda>%d", ++lambda_no);
698 if (n < 1)
699 lambda_namelen = 0;
700 else
701 if (n >= (int)sizeof(lambda_name))
702 lambda_namelen = sizeof(lambda_name) - 1;
703 else
704 lambda_namelen = (size_t)n;
705
706 return lambda_name;
707}
708
709/*
710 * Get the length of the last lambda name.
711 */
712 size_t
713get_lambda_name_len(void)
714{
715 return lambda_namelen;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200716}
717
Bram Moolenaare01e5212023-01-08 20:31:18 +0000718/*
719 * Allocate a "ufunc_T" for a function called "name".
720 * Makes sure the size is right.
721 */
722 static ufunc_T *
John Marriottb32800f2025-02-01 15:25:34 +0100723alloc_ufunc(char_u *name, size_t namelen)
Bram Moolenaare01e5212023-01-08 20:31:18 +0000724{
John Marriottb32800f2025-02-01 15:25:34 +0100725 size_t len;
726 ufunc_T *fp;
727
Bram Moolenaare01e5212023-01-08 20:31:18 +0000728 // When the name is short we need to make sure we allocate enough bytes for
729 // the whole struct, including any padding.
John Marriottb32800f2025-02-01 15:25:34 +0100730 len = offsetof(ufunc_T, uf_name) + namelen + 1;
731 fp = alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
732 if (fp != NULL)
733 {
734 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
735 // can actually extend beyond the struct.
736 STRCPY((void *)fp->uf_name, name);
737 fp->uf_namelen = namelen;
738
739 if (name[0] == K_SPECIAL)
740 {
741 len = namelen + 3; // including +1 for NUL
742 fp->uf_name_exp = alloc(len);
743 if (fp->uf_name_exp != NULL)
744 vim_snprintf((char *)fp->uf_name_exp, len, "<SNR>%s", fp->uf_name + 3);
745 }
746 }
747
748 return fp;
Bram Moolenaare01e5212023-01-08 20:31:18 +0000749}
750
Bram Moolenaar801ab062020-06-25 19:27:56 +0200751#if defined(FEAT_LUA) || defined(PROTO)
752/*
753 * Registers a native C callback which can be called from Vim script.
754 * Returns the name of the Vim script function.
755 */
756 char_u *
757register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
758{
759 char_u *name = get_lambda_name();
John Marriottb32800f2025-02-01 15:25:34 +0100760 size_t namelen = get_lambda_name_len();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200761 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200762
John Marriottb32800f2025-02-01 15:25:34 +0100763 fp = alloc_ufunc(name, namelen);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200764 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200765 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200766
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200767 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200768 fp->uf_refcount = 1;
769 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000770 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200771 fp->uf_calls = 0;
772 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200773 fp->uf_cb = cb;
774 fp->uf_cb_free = cb_free;
775 fp->uf_cb_state = state;
776
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000777 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200778
779 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200780}
781#endif
782
Bram Moolenaar04b12692020-05-04 23:24:44 +0200783/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100784 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100785 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100786 * If "white_error" is not NULL check for correct use of white space and set
787 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100788 * Return NULL if no valid arrow found.
789 */
790 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100791skip_arrow(
792 char_u *start,
793 int equal_arrow,
794 char_u **ret_type,
795 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100796{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100797 char_u *s = start;
798 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100799
800 if (equal_arrow)
801 {
802 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100803 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100804 if (white_error != NULL && !VIM_ISWHITE(s[1]))
805 {
806 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100807 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100808 return NULL;
809 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100810 s = skipwhite(s + 1);
811 *ret_type = s;
812 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100813 if (s == *ret_type)
814 {
815 emsg(_(e_missing_return_type));
816 return NULL;
817 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100818 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100819 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100820 s = skipwhite(s);
821 if (*s != '=')
822 return NULL;
823 ++s;
824 }
825 if (*s != '>')
826 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100827 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
828 || !IS_WHITE_OR_NUL(s[1])))
829 {
830 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100831 semsg(_(e_white_space_required_before_and_after_str_at_str),
832 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100833 return NULL;
834 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100835 return skipwhite(s + 1);
836}
837
838/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100839 * Check if "*cmd" points to a function command and if so advance "*cmd" and
840 * return TRUE.
841 * Otherwise return FALSE;
842 * Do not consider "function(" to be a command.
843 */
844 static int
845is_function_cmd(char_u **cmd)
846{
847 char_u *p = *cmd;
848
849 if (checkforcmd(&p, "function", 2))
850 {
851 if (*p == '(')
852 return FALSE;
853 *cmd = p;
854 return TRUE;
855 }
856 return FALSE;
857}
858
859/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200860 * Called when defining a function: The context may be needed for script
861 * variables declared in a block that is visible now but not when the function
862 * is compiled or called later.
863 */
864 static void
865function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
866{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000867 if (cstack == NULL || cstack->cs_idx < 0)
868 return;
869
870 int count = cstack->cs_idx + 1;
871 int i;
872
873 fp->uf_block_ids = ALLOC_MULT(int, count);
874 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200875 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000876 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
877 sizeof(int) * count);
878 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200879 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000880
881 // Set flag in each block to indicate a function was defined. This
882 // is used to keep the variable when leaving the block, see
883 // hide_script_var().
884 for (i = 0; i <= cstack->cs_idx; ++i)
885 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200886}
887
888/*
Yegappan Lakshmanan0072cee2025-01-07 20:22:32 +0100889 * Skip over all the characters in a single quoted string starting at "p" and
890 * return a pointer to the character following the ending single quote.
891 * If the ending single quote is missing, then return a pointer to the NUL
892 * character.
893 */
894 static char_u *
895skip_single_quote_string(char_u *p)
896{
897 p++; // skip the beginning single quote
898 while (*p != NUL)
899 {
900 // Within the string, a single quote can be escaped by using
901 // two single quotes.
902 if (*p == '\'' && *(p + 1) == '\'')
903 p += 2;
904 else if (*p == '\'')
905 {
906 p++; // skip the ending single quote
907 break;
908 }
909 else
910 MB_PTR_ADV(p);
911 }
912
913 return p;
914}
915
916/*
917 * Skip over all the characters in a double quoted string starting at "p" and
918 * return a pointer to the character following the ending double quote.
919 * If the ending double quote is missing, then return a pointer to the NUL
920 * character.
921 */
922 static char_u *
923skip_double_quote_string(char_u *p)
924{
925 p++; // skip the beginning double quote
926 while (*p != NUL)
927 {
928 // Within the string, a double quote can be escaped by
929 // preceding it with a backslash.
930 if (*p == '\\' && *(p + 1) == '"')
931 p += 2;
932 else if (*p == '"')
933 {
934 p++; // skip the ending double quote
935 break;
936 }
937 else
938 MB_PTR_ADV(p);
939 }
940
941 return p;
942}
943
944/*
945 * Return the start of a Vim9 comment (#) in the line starting at "line".
946 * If a comment is not found, then returns a pointer to the end of the
947 * string (NUL).
948 */
949 static char_u *
950find_start_of_vim9_comment(char_u *line)
951{
952 char_u *p = line;
953
954 while (*p != NUL)
955 {
956 if (*p == '\'')
957 // Skip a single quoted string.
958 p = skip_single_quote_string(p);
959 else if (*p == '"')
960 // Skip a double quoted string.
961 p = skip_double_quote_string(p);
962 else
963 {
964 if (*p == '#')
965 // Found the start of a Vim9 comment
966 break;
967 MB_PTR_ADV(p);
968 }
969 }
970
971 return p;
972}
973
974/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100975 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200976 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100977 * "newlines" must already have been initialized.
978 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
979 */
980 static int
981get_function_body(
982 exarg_T *eap,
983 garray_T *newlines,
984 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000985 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100986{
987 linenr_T sourcing_lnum_top = SOURCING_LNUM;
988 linenr_T sourcing_lnum_off;
989 int saved_wait_return = need_wait_return;
990 char_u *line_arg = line_arg_in;
991 int vim9_function = eap->cmdidx == CMD_def
992 || eap->cmdidx == CMD_block;
993#define MAX_FUNC_NESTING 50
994 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200995 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100996 int nesting = 0;
997 getline_opt_T getline_options;
998 int indent = 2;
999 char_u *skip_until = NULL;
1000 int ret = FAIL;
1001 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +02001002 int heredoc_concat_len = 0;
1003 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001004 char_u *heredoc_trimmed = NULL;
John Marriottb32800f2025-02-01 15:25:34 +01001005 size_t heredoc_trimmedlen = 0;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001006
Bram Moolenaar20677332021-06-06 17:02:53 +02001007 ga_init2(&heredoc_ga, 1, 500);
1008
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001009 // Detect having skipped over comment lines to find the return
1010 // type. Add NULL lines to keep the line count correct.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001011 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001012 if (SOURCING_LNUM < sourcing_lnum_off)
1013 {
1014 sourcing_lnum_off -= SOURCING_LNUM;
1015 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
1016 goto theend;
1017 while (sourcing_lnum_off-- > 0)
1018 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1019 }
1020
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001021 nesting_def[0] = vim9_function;
1022 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001023 getline_options = vim9_function
1024 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
1025 for (;;)
1026 {
1027 char_u *theline;
1028 char_u *p;
1029 char_u *arg;
1030
1031 if (KeyTyped)
1032 {
1033 msg_scroll = TRUE;
1034 saved_wait_return = FALSE;
1035 }
1036 need_wait_return = FALSE;
1037
1038 if (line_arg != NULL)
1039 {
1040 // Use eap->arg, split up in parts by line breaks.
1041 theline = line_arg;
1042 p = vim_strchr(theline, '\n');
1043 if (p == NULL)
1044 line_arg += STRLEN(line_arg);
1045 else
1046 {
1047 *p = NUL;
1048 line_arg = p + 1;
1049 }
1050 }
1051 else
1052 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001053 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001054 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001055 }
1056 if (KeyTyped)
1057 lines_left = Rows - 1;
1058 if (theline == NULL)
1059 {
1060 // Use the start of the function for the line number.
1061 SOURCING_LNUM = sourcing_lnum_top;
1062 if (skip_until != NULL)
1063 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001064 else if (nesting_inline[nesting])
1065 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001066 else if (eap->cmdidx == CMD_def)
1067 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001068 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001069 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001070 goto theend;
1071 }
1072
1073 // Detect line continuation: SOURCING_LNUM increased more than one.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001074 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001075 if (SOURCING_LNUM < sourcing_lnum_off)
1076 sourcing_lnum_off -= SOURCING_LNUM;
1077 else
1078 sourcing_lnum_off = 0;
1079
1080 if (skip_until != NULL)
1081 {
1082 // Don't check for ":endfunc"/":enddef" between
1083 // * ":append" and "."
1084 // * ":python <<EOF" and "EOF"
1085 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
1086 if (heredoc_trimmed == NULL
1087 || (is_heredoc && skipwhite(theline) == theline)
1088 || STRNCMP(theline, heredoc_trimmed,
John Marriottb32800f2025-02-01 15:25:34 +01001089 heredoc_trimmedlen) == 0)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001090 {
1091 if (heredoc_trimmed == NULL)
1092 p = theline;
1093 else if (is_heredoc)
1094 p = skipwhite(theline) == theline
John Marriottb32800f2025-02-01 15:25:34 +01001095 ? theline : theline + heredoc_trimmedlen;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001096 else
John Marriottb32800f2025-02-01 15:25:34 +01001097 p = theline + heredoc_trimmedlen;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001098 if (STRCMP(p, skip_until) == 0)
1099 {
1100 VIM_CLEAR(skip_until);
1101 VIM_CLEAR(heredoc_trimmed);
John Marriottb32800f2025-02-01 15:25:34 +01001102 heredoc_trimmedlen = 0;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001103 getline_options = vim9_function
1104 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
1105 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +02001106
1107 if (heredoc_concat_len > 0)
1108 {
1109 // Replace the starting line with all the concatenated
1110 // lines.
1111 ga_concat(&heredoc_ga, theline);
1112 vim_free(((char_u **)(newlines->ga_data))[
1113 heredoc_concat_len - 1]);
1114 ((char_u **)(newlines->ga_data))[
1115 heredoc_concat_len - 1] = heredoc_ga.ga_data;
1116 ga_init(&heredoc_ga);
1117 heredoc_concat_len = 0;
1118 theline += STRLEN(theline); // skip the "EOF"
1119 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001120 }
1121 }
1122 }
1123 else
1124 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001125 int c;
1126 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +00001127 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001128
1129 // skip ':' and blanks
1130 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
1131 ;
1132
1133 // Check for "endfunction", "enddef" or "}".
1134 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +00001135 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001136 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001137 ? *p == '}'
1138 : (checkforcmd(&p, nesting_def[nesting]
1139 ? "enddef" : "endfunction", 4)
1140 && *p != ':'))
1141 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00001142 if (!nesting_inline[nesting] && nesting_def[nesting]
1143 && p < cmd + 6)
1144 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001145 if (nesting-- == 0)
1146 {
1147 char_u *nextcmd = NULL;
1148
1149 if (*p == '|' || *p == '}')
1150 nextcmd = p + 1;
1151 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
1152 nextcmd = line_arg;
1153 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001154 && (vim9_function || p_verbose > 0))
1155 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +02001156 SOURCING_LNUM = sourcing_lnum_top
1157 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001158 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +00001159 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001160 else
1161 give_warning2((char_u *)
1162 _("W22: Text found after :endfunction: %s"),
1163 p, TRUE);
1164 }
1165 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001166 {
1167 // Another command follows. If the line came from "eap"
1168 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001169 // change "eap->cmdlinep" to point to the last fetched
1170 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001171 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001172 if (lines_to_free->ga_len > 0
1173 && *eap->cmdlinep !=
1174 ((char_u **)lines_to_free->ga_data)
1175 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001176 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001177 // *cmdlinep will be freed later, thus remove the
1178 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001179 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001180 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
1181 [lines_to_free->ga_len - 1];
1182 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001183 }
1184 }
1185 break;
1186 }
1187 }
1188
1189 // Check for mismatched "endfunc" or "enddef".
1190 // We don't check for "def" inside "func" thus we also can't check
1191 // for "enddef".
1192 // We continue to find the end of the function, although we might
1193 // not find it.
1194 else if (nesting_def[nesting])
1195 {
1196 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1197 emsg(_(e_mismatched_endfunction));
1198 }
1199 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1200 emsg(_(e_mismatched_enddef));
1201
1202 // Increase indent inside "if", "while", "for" and "try", decrease
1203 // at "end".
1204 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1205 indent -= 2;
1206 else if (STRNCMP(p, "if", 2) == 0
1207 || STRNCMP(p, "wh", 2) == 0
1208 || STRNCMP(p, "for", 3) == 0
1209 || STRNCMP(p, "try", 3) == 0)
1210 indent += 2;
1211
1212 // Check for defining a function inside this function.
1213 // Only recognize "def" inside "def", not inside "function",
1214 // For backwards compatibility, see Test_function_python().
1215 c = *p;
1216 if (is_function_cmd(&p)
1217 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1218 {
1219 if (*p == '!')
1220 p = skipwhite(p + 1);
1221 p += eval_fname_script(p);
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001222 vim_free(trans_function_name(&p, NULL, TRUE, 0));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001223 if (*skipwhite(p) == '(')
1224 {
1225 if (nesting == MAX_FUNC_NESTING - 1)
1226 emsg(_(e_function_nesting_too_deep));
1227 else
1228 {
1229 ++nesting;
1230 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001231 nesting_inline[nesting] = FALSE;
1232 indent += 2;
1233 }
1234 }
1235 }
1236
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001237 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001238 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001239 // Not a comment line: check for nested inline function.
Yegappan Lakshmanan0072cee2025-01-07 20:22:32 +01001240
1241 if (nesting_inline[nesting])
1242 {
1243 // A comment (#) can follow the opening curly brace of a
1244 // block statement. Need to ignore the comment and look
1245 // for the opening curly brace before the comment.
1246 end = find_start_of_vim9_comment(p) - 1;
1247 }
1248 else
1249 end = p + STRLEN(p) - 1;
1250
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001251 while (end > p && VIM_ISWHITE(*end))
1252 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001253 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001254 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001255 int is_block;
1256
1257 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001258 --end;
1259 while (end > p && VIM_ISWHITE(*end))
1260 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001261 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1262 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001263 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001264 char_u *s = p;
1265
1266 // check for line starting with "au" for :autocmd or
1267 // "com" for :command, these can use a {} block
1268 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1269 || checkforcmd_noparen(&s, "command", 3);
1270 }
1271
1272 if (is_block)
1273 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001274 if (nesting == MAX_FUNC_NESTING - 1)
1275 emsg(_(e_function_nesting_too_deep));
1276 else
1277 {
1278 ++nesting;
1279 nesting_def[nesting] = TRUE;
1280 nesting_inline[nesting] = TRUE;
1281 indent += 2;
1282 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001283 }
1284 }
1285 }
1286
1287 // Check for ":append", ":change", ":insert". Not for :def.
1288 p = skip_range(p, FALSE, NULL);
1289 if (!vim9_function
1290 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1291 || (p[0] == 'c'
1292 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1293 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1294 && (STRNCMP(&p[3], "nge", 3) != 0
1295 || !ASCII_ISALPHA(p[6])))))))
1296 || (p[0] == 'i'
1297 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1298 && (!ASCII_ISALPHA(p[2])
1299 || (p[2] == 's'
1300 && (!ASCII_ISALPHA(p[3])
1301 || p[3] == 'e'))))))))
John Marriottb32800f2025-02-01 15:25:34 +01001302 skip_until = vim_strnsave((char_u *)".", 1);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001303
1304 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1305 arg = skipwhite(skiptowhite(p));
1306 if (arg[0] == '<' && arg[1] =='<'
1307 && ((p[0] == 'p' && p[1] == 'y'
1308 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1309 || ((p[2] == '3' || p[2] == 'x')
1310 && !ASCII_ISALPHA(p[3]))))
1311 || (p[0] == 'p' && p[1] == 'e'
1312 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1313 || (p[0] == 't' && p[1] == 'c'
1314 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1315 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1316 && !ASCII_ISALPHA(p[3]))
1317 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1318 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1319 || (p[0] == 'm' && p[1] == 'z'
1320 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1321 ))
1322 {
1323 // ":python <<" continues until a dot, like ":append"
1324 p = skipwhite(arg + 2);
1325 if (STRNCMP(p, "trim", 4) == 0)
1326 {
1327 // Ignore leading white space.
1328 p = skipwhite(p + 4);
John Marriottb32800f2025-02-01 15:25:34 +01001329 heredoc_trimmedlen = skipwhite(theline) - theline;
1330 heredoc_trimmed = vim_strnsave(theline, heredoc_trimmedlen);
1331 if (heredoc_trimmed == NULL)
1332 heredoc_trimmedlen = 0;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001333 }
1334 if (*p == NUL)
John Marriottb32800f2025-02-01 15:25:34 +01001335 skip_until = vim_strnsave((char_u *)".", 1);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001336 else
1337 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1338 getline_options = GETLINE_NONE;
1339 is_heredoc = TRUE;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001340 if (vim9_function && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001341 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001342 }
1343
Bram Moolenaard881d152022-05-13 13:50:36 +01001344 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001345 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001346 // Check for ":cmd v =<< [trim] EOF"
1347 // and ":cmd [a, b] =<< [trim] EOF"
1348 // and "lines =<< [trim] EOF" for Vim9
1349 // Where "cmd" can be "let", "var", "final" or "const".
zeertzjqa93d9cd2023-05-02 16:25:47 +01001350 arg = p;
1351 if (checkforcmd(&arg, "let", 2)
1352 || checkforcmd(&arg, "var", 3)
1353 || checkforcmd(&arg, "final", 5)
1354 || checkforcmd(&arg, "const", 5)
1355 || vim9_function)
Bram Moolenaard881d152022-05-13 13:50:36 +01001356 {
zeertzjq9a91d2b2024-04-09 21:47:10 +02001357 int save_sc_version = current_sctx.sc_version;
1358 int var_count = 0;
1359 int semicolon = 0;
zeertzjq9a91d2b2024-04-09 21:47:10 +02001360
1361 current_sctx.sc_version
1362 = vim9_function ? SCRIPT_VERSION_VIM9 : 1;
zeertzjq1817ccd2024-04-10 17:37:47 +02001363 arg = skip_var_list(arg, TRUE, &var_count, &semicolon,
zeertzjq9a91d2b2024-04-09 21:47:10 +02001364 TRUE);
zeertzjq1817ccd2024-04-10 17:37:47 +02001365 if (arg != NULL)
1366 arg = skipwhite(arg);
zeertzjq9a91d2b2024-04-09 21:47:10 +02001367 current_sctx.sc_version = save_sc_version;
zeertzjq1817ccd2024-04-10 17:37:47 +02001368 if (arg != NULL && STRNCMP(arg, "=<<", 3) == 0)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001369 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001370 p = skipwhite(arg + 3);
1371 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001372 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001373 if (STRNCMP(p, "trim", 4) == 0)
1374 {
1375 // Ignore leading white space.
1376 p = skipwhite(p + 4);
John Marriottb32800f2025-02-01 15:25:34 +01001377 heredoc_trimmedlen = skipwhite(theline) - theline;
1378 heredoc_trimmed = vim_strnsave(theline, heredoc_trimmedlen);
1379 if (heredoc_trimmed == NULL)
1380 heredoc_trimmedlen = 0;
Bram Moolenaard881d152022-05-13 13:50:36 +01001381 continue;
1382 }
1383 if (STRNCMP(p, "eval", 4) == 0)
1384 {
1385 // Ignore leading white space.
1386 p = skipwhite(p + 4);
1387 continue;
1388 }
1389 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001390 }
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;
1471 char_u *name;
John Marriottb32800f2025-02-01 15:25:34 +01001472 size_t namelen;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001473 int lnum_save = -1;
1474 linenr_T sourcing_lnum_top = SOURCING_LNUM;
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001475 char_u *line_arg = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001476
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001477 *arg = skipwhite(*arg + 1);
1478 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001479 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001480 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001481 return FAIL;
1482 }
1483
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001484 // When there is a line break use what follows for the lambda body.
1485 // Makes lambda body initializers work for object and enum member
1486 // variables.
1487 if (**arg == '\n')
1488 line_arg = *arg + 1;
1489
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001490 CLEAR_FIELD(eap);
1491 eap.cmdidx = CMD_block;
1492 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001493 eap.cmdlinep = &cmdline;
1494 eap.skip = !evaluate;
1495 if (evalarg->eval_cctx != NULL)
1496 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1497 else
1498 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001499 eap.ea_getline = evalarg->eval_getline;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001500 eap.cookie = evalarg->eval_cookie;
1501 }
1502
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001503 ga_init2(&newlines, sizeof(char_u *), 10);
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001504 if (get_function_body(&eap, &newlines, line_arg,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001505 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001506 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001507
1508 // When inside a lambda must add the function lines to evalarg.eval_ga.
1509 evalarg->eval_break_count += newlines.ga_len;
1510 if (gap->ga_itemsize > 0)
1511 {
1512 int idx;
1513 char_u *last;
1514 size_t plen;
1515 char_u *pnl;
1516
1517 for (idx = 0; idx < newlines.ga_len; ++idx)
1518 {
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001519 char_u *p = ((char_u **)newlines.ga_data)[idx];
1520 if (p == NULL)
1521 // comment line in the lambda body
1522 continue;
1523
1524 p = skipwhite(p);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001525
Bram Moolenaarecb66452021-05-18 15:09:18 +02001526 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001527 goto erret;
1528
1529 // Going to concatenate the lines after parsing. For an empty or
1530 // comment line use an empty string.
1531 // Insert NL characters at the start of each line, the string will
1532 // be split again later in .get_lambda_tv().
1533 if (*p == NUL || vim9_comment_start(p))
John Marriottb32800f2025-02-01 15:25:34 +01001534 {
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001535 p = (char_u *)"";
John Marriottb32800f2025-02-01 15:25:34 +01001536 plen = 0;
1537 }
1538 else
1539 plen = STRLEN(p);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001540 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1541 if (pnl != NULL)
1542 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001543 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1544 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001545 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001546 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001547 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001548 if (eap.nextcmd != NULL)
John Marriottb32800f2025-02-01 15:25:34 +01001549 {
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001550 // more is following after the "}", which was skipped
1551 last = cmdline;
John Marriottb32800f2025-02-01 15:25:34 +01001552 plen = STRLEN(last);
1553 }
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001554 else
John Marriottb32800f2025-02-01 15:25:34 +01001555 {
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001556 // nothing is following the "}"
1557 last = (char_u *)"}";
John Marriottb32800f2025-02-01 15:25:34 +01001558 plen = 1;
1559 }
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001560 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1561 if (pnl != NULL)
1562 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001563 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1564 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001565 }
1566
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001567 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001568 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001569 garray_T *tfgap = &evalarg->eval_tofree_ga;
1570
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001571 // Something comes after the "}".
1572 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001573
1574 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001575 if (ga_grow(tfgap, 1) == OK)
1576 {
1577 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1578 evalarg->eval_using_cmdline = TRUE;
1579 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001580 }
1581 else
1582 *arg = (char_u *)"";
1583
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001584 if (!evaluate)
1585 {
1586 ret = OK;
1587 goto erret;
1588 }
1589
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001590 name = get_lambda_name();
John Marriottb32800f2025-02-01 15:25:34 +01001591 namelen = get_lambda_name_len();
1592 ufunc = alloc_ufunc(name, namelen);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001593 if (ufunc == NULL)
1594 goto erret;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001595 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001596 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001597 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001598 ufunc->uf_refcount = 1;
1599 ufunc->uf_args = *newargs;
1600 newargs->ga_data = NULL;
1601 ufunc->uf_def_args = *default_args;
1602 default_args->ga_data = NULL;
1603 ufunc->uf_func_type = &t_func_any;
1604
1605 // error messages are for the first function line
1606 lnum_save = SOURCING_LNUM;
1607 SOURCING_LNUM = sourcing_lnum_top;
1608
1609 // parse argument types
h-eastb895b0f2023-09-24 15:46:31 +02001610 if (parse_argument_types(ufunc, argtypes, varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001611 {
1612 SOURCING_LNUM = lnum_save;
1613 goto erret;
1614 }
1615
1616 // parse the return type, if any
1617 if (parse_return_type(ufunc, ret_type) == FAIL)
1618 goto erret;
1619
1620 pt = ALLOC_CLEAR_ONE(partial_T);
1621 if (pt == NULL)
1622 goto erret;
1623 pt->pt_func = ufunc;
1624 pt->pt_refcount = 1;
1625
1626 ufunc->uf_lines = newlines;
1627 newlines.ga_data = NULL;
1628 if (sandbox)
1629 ufunc->uf_flags |= FC_SANDBOX;
1630 if (!ASCII_ISUPPER(*ufunc->uf_name))
1631 ufunc->uf_flags |= FC_VIM9;
1632 ufunc->uf_script_ctx = current_sctx;
1633 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1634 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1635 set_function_type(ufunc);
1636
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001637 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1638
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001639 rettv->vval.v_partial = pt;
1640 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001641 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001642 ret = OK;
1643
1644erret:
1645 if (lnum_save >= 0)
1646 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001647 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001648 if (newargs != NULL)
1649 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001650 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001651 if (ufunc != NULL)
1652 {
1653 func_clear(ufunc, TRUE);
1654 func_free(ufunc, TRUE);
1655 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001656 return ret;
1657}
1658
1659/*
1660 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001661 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001662 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001663 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1664 */
1665 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001666get_lambda_tv(
1667 char_u **arg,
1668 typval_T *rettv,
1669 int types_optional,
1670 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001671{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001672 int evaluate = evalarg != NULL
1673 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001674 garray_T newargs;
1675 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001676 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001677 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001678 garray_T default_args;
h-eastb895b0f2023-09-24 15:46:31 +02001679 garray_T arg_objm;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001680 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001681 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001682 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001683 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001684 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001685 char_u *s;
1686 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001687 int *old_eval_lavars = eval_lavars_used;
1688 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001689 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001690 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001691 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001692 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001693 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001694 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001695
Bram Moolenaar4525a572022-02-13 11:57:33 +00001696 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001697 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001698
1699 ga_init(&newargs);
1700 ga_init(&newlines);
1701
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001702 // First, check if this is really a lambda expression. "->" or "=>" must
1703 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001704 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001705 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001706 types_optional ? &argtypes : NULL, types_optional,
1707 types_optional ? &arg_objm : NULL, evalarg,
1708 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001709 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001710 {
1711 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001712 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001713 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001714 ga_clear(&arg_objm);
1715 }
Bram Moolenaar0346b792021-01-31 22:18:29 +01001716 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001717 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001718
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001719 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001720 if (evaluate)
1721 pnewargs = &newargs;
1722 else
1723 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001724 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001725 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
h-eastb895b0f2023-09-24 15:46:31 +02001726 types_optional ? &argtypes : NULL, types_optional,
1727 types_optional ? &arg_objm : NULL, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001728 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001729 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001730 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001731 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001732 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001733 {
1734 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001735 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001736 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001737 ga_clear(&arg_objm);
1738 }
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001739 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001740 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001741 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001742 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001743
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001744 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1745 if (ret_type != NULL)
1746 {
1747 ret_type = vim_strsave(ret_type);
1748 tofree2 = ret_type;
1749 }
1750
Bram Moolenaare38eab22019-12-05 21:50:01 +01001751 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001752 if (evaluate)
1753 eval_lavars_used = &eval_lavars;
1754
Bram Moolenaar65c44152020-12-24 15:14:01 +01001755 *arg = skipwhite_and_linebreak(*arg, evalarg);
1756
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001757 // Recognize "{" as the start of a function body.
1758 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001759 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001760 if (evalarg == NULL)
1761 // cannot happen?
1762 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001763 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001764 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1765 types_optional ? &argtypes : NULL, varargs,
1766 &default_args, ret_type) == FAIL)
1767 goto errret;
1768 goto theend;
1769 }
1770 if (default_args.ga_len > 0)
1771 {
1772 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001773 goto errret;
1774 }
1775
Bram Moolenaare38eab22019-12-05 21:50:01 +01001776 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001777 start = *arg;
1778 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001779 if (ret == FAIL)
1780 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001781
Bram Moolenaar65c44152020-12-24 15:14:01 +01001782 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001783 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001784 *arg = skipwhite_and_linebreak(*arg, evalarg);
1785 if (**arg != '}')
1786 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001787 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001788 goto errret;
1789 }
1790 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001791 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001792
1793 if (evaluate)
1794 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001795 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001796 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001797 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001798 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001799 char_u *name = get_lambda_name();
John Marriottb32800f2025-02-01 15:25:34 +01001800 size_t namelen = get_lambda_name_len();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001801
John Marriottb32800f2025-02-01 15:25:34 +01001802 fp = alloc_ufunc(name, namelen);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001803 if (fp == NULL)
1804 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001805 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001806 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001807 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001808 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001809
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001810 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001811 if (ga_grow(&newlines, 1) == FAIL)
1812 goto errret;
1813
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001814 // If there are line breaks, we need to split up the string.
1815 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001816 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001817 line_end = end;
1818
1819 // Add "return " before the expression (or the first line).
1820 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001821 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001822 if (p == NULL)
1823 goto errret;
1824 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1825 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001826 vim_strncpy(p + 7, start, line_end - start);
1827
1828 if (line_end != end)
1829 {
1830 // Add more lines, split by line breaks. Thus is used when a
1831 // lambda with { cmds } is encountered.
1832 while (*line_end == '\n')
1833 {
1834 if (ga_grow(&newlines, 1) == FAIL)
1835 goto errret;
1836 start = line_end + 1;
1837 line_end = vim_strchr(start, '\n');
1838 if (line_end == NULL)
1839 line_end = end;
1840 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1841 vim_strnsave(start, line_end - start);
1842 }
1843 }
1844
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001845 if (strstr((char *)p + 7, "a:") == NULL)
1846 // No a: variables are used for sure.
1847 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001848
1849 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001850 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001851 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001852 if (types_optional)
1853 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001854 if (parse_argument_types(fp, &argtypes,
h-eastb895b0f2023-09-24 15:46:31 +02001855 vim9script && varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001856 goto errret;
1857 if (ret_type != NULL)
1858 {
1859 fp->uf_ret_type = parse_type(&ret_type,
1860 &fp->uf_type_list, TRUE);
1861 if (fp->uf_ret_type == NULL)
1862 goto errret;
1863 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001864 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001865 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001866 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001867
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001868 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001869 if (current_funccal != NULL && eval_lavars)
1870 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001871 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001872 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001873 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001874 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001875
1876#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001877 if (prof_def_func())
1878 func_do_profile(fp);
1879#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001880 if (sandbox)
1881 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001882 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001883 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001884 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001885 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001886 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001887 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001888 // Use the line number of the arguments.
1889 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001890
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001891 function_using_block_scopes(fp, evalarg->eval_cstack);
1892
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001893 pt->pt_func = fp;
1894 pt->pt_refcount = 1;
1895 rettv->vval.v_partial = pt;
1896 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001897
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001898 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001899 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001900
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001901theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001902 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001903 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001904 if (types_optional)
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001905 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001906 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001907 ga_clear(&arg_objm);
1908 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001909
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001910 return OK;
1911
1912errret:
1913 ga_clear_strings(&newargs);
1914 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001915 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001916 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001917 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001918 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001919 ga_clear(&arg_objm);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001920 }
John Marriottb32800f2025-02-01 15:25:34 +01001921 if (fp != NULL)
1922 {
1923 vim_free(fp->uf_arg_types);
1924 vim_free(fp->uf_name_exp);
1925 vim_free(fp);
1926 }
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001927 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001928 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001929 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001930 return FAIL;
1931}
1932
1933/*
1934 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1935 * name it contains, otherwise return "name".
1936 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1937 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001938 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1939 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001940 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001941 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001942 */
1943 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001944deref_func_name(
1945 char_u *name,
1946 int *lenp,
1947 partial_T **partialp,
1948 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001949 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001950 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001951 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001952{
1953 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001954 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001955 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001956 char_u *s = NULL;
1957 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001958 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001959
1960 if (partialp != NULL)
1961 *partialp = NULL;
1962
1963 cc = name[*lenp];
1964 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001965
Bram Moolenaar71f21932022-01-07 18:20:55 +00001966 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001967 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001968 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001969 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001970 tv = &v->di_tv;
1971 }
1972 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1973 {
1974 imported_T *import;
1975 char_u *p = name;
1976 int len = *lenp;
1977
1978 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001979 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001980 p = name + 2;
1981 len -= 2;
1982 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001983 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001984
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001985 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001986 if (import != NULL)
1987 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001988 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001989 if (new_function)
1990 semsg(_(e_redefining_imported_item_str), name);
1991 else
1992 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001993 name[len] = cc;
1994 *lenp = 0;
1995 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001996 }
1997 }
1998
1999 if (tv != NULL)
2000 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002001 if (found_var != NULL)
2002 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01002003 if (tv->v_type == VAR_FUNC)
2004 {
2005 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002006 {
2007 *lenp = 0;
2008 return (char_u *)""; // just in case
2009 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01002010 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002011 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002012 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002013
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01002014 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002015 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01002016 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002017
2018 if (pt == NULL)
2019 {
2020 *lenp = 0;
2021 return (char_u *)""; // just in case
2022 }
2023 if (partialp != NULL)
2024 *partialp = pt;
2025 s = partial_name(pt);
2026 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002027 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002028
2029 if (s != NULL)
2030 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01002031 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002032 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002033 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002034
2035 if (sv != NULL)
2036 *type = sv->sv_type;
2037 }
2038 return s;
2039 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002040 }
2041
2042 return name;
2043}
2044
2045/*
2046 * Give an error message with a function name. Handle <SNR> things.
2047 * "ermsg" is to be passed without translation, use N_() instead of _().
2048 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002049 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002050emsg_funcname(char *ermsg, char_u *name)
2051{
Bram Moolenaar54598062022-01-13 12:05:09 +00002052 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002053
Bram Moolenaar54598062022-01-13 12:05:09 +00002054 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002055 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002056 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002057 if (p != name)
2058 vim_free(p);
2059}
2060
2061/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002062 * Get function arguments at "*arg" and advance it.
2063 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01002064 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002065 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002066 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002067get_func_arguments(
2068 char_u **arg,
2069 evalarg_T *evalarg,
2070 int partial_argc,
2071 typval_T *argvars,
Ernie Raelb077b582023-12-14 20:11:44 +01002072 int *argcount,
2073 int is_builtin)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002074{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002075 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002076 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002077 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002078 int evaluate = evalarg == NULL
2079 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002080
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002081 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002082 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02002083 // skip the '(' or ',' and possibly line breaks
2084 argp = skipwhite_and_linebreak(argp + 1, evalarg);
2085
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002086 if (*argp == ')' || *argp == ',' || *argp == NUL)
2087 break;
Ernie Raelb077b582023-12-14 20:11:44 +01002088
2089 int arg_idx = *argcount;
2090 if (eval1(&argp, &argvars[arg_idx], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002091 {
2092 ret = FAIL;
2093 break;
2094 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002095 ++*argcount;
Ernie Raelb077b582023-12-14 20:11:44 +01002096 if (!is_builtin && check_typval_is_value(&argvars[arg_idx]) == FAIL)
2097 {
2098 ret = FAIL;
2099 break;
2100 }
2101
Bram Moolenaar9d489562020-07-30 20:08:50 +02002102 // The comma should come right after the argument, but this wasn't
2103 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002104 if (vim9script)
2105 {
2106 if (*argp != ',' && *skipwhite(argp) == ',')
2107 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002108 if (evaluate)
2109 semsg(_(e_no_white_space_allowed_before_str_str),
2110 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002111 ret = FAIL;
2112 break;
2113 }
2114 }
2115 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002116 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002117 if (*argp != ',')
2118 break;
Christian Brabandt00cb2472023-09-05 20:46:25 +02002119 if (vim9script && !IS_WHITE_NL_OR_NUL(argp[1]))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002120 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002121 if (evaluate)
2122 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02002123 ret = FAIL;
2124 break;
2125 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002126 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002127
Bram Moolenaare6b53242020-07-01 17:28:33 +02002128 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002129 if (*argp == ')')
2130 ++argp;
2131 else
2132 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002133 *arg = argp;
2134 return ret;
2135}
2136
2137/*
2138 * Call a function and put the result in "rettv".
2139 * Return OK or FAIL.
2140 */
2141 int
2142get_func_tv(
2143 char_u *name, // name of the function
2144 int len, // length of "name" or -1 to use strlen()
2145 typval_T *rettv,
2146 char_u **arg, // argument, pointing to the '('
2147 evalarg_T *evalarg, // for line continuation
2148 funcexe_T *funcexe) // various values
2149{
2150 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002151 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002152 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
2153 int argcount = 0; // number of arguments found
2154 int vim9script = in_vim9script();
2155 int evaluate = evalarg == NULL
2156 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
2157
2158 argp = *arg;
2159 ret = get_func_arguments(&argp, evalarg,
2160 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
Ernie Raelb077b582023-12-14 20:11:44 +01002161 argvars, &argcount, builtin_function(name, -1));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002162
2163 if (ret == OK)
2164 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002165 int i = 0;
2166 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002167
2168 if (get_vim_var_nr(VV_TESTING))
2169 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002170 // Prepare for calling test_garbagecollect_now(), need to know
2171 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002172 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002173 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002174 for (i = 0; i < argcount; ++i)
2175 if (ga_grow(&funcargs, 1) == OK)
2176 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
2177 &argvars[i];
2178 }
2179
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002180 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00002181 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002182 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002183 // An error in a builtin function does not return FAIL, but we do
2184 // want to abort further processing if an error was given.
2185 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002186 clear_tv(rettv);
2187 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002188
2189 funcargs.ga_len -= i;
2190 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002191 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002192 {
2193 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002194 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002195 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00002196 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002197 }
2198
2199 while (--argcount >= 0)
2200 clear_tv(&argvars[argcount]);
2201
Bram Moolenaar4525a572022-02-13 11:57:33 +00002202 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02002203 *arg = argp;
2204 else
2205 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002206 return ret;
2207}
2208
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002209/*
2210 * Return TRUE if "p" starts with "<SID>" or "s:".
2211 * Only works if eval_fname_script() returned non-zero for "p"!
2212 */
2213 static int
2214eval_fname_sid(char_u *p)
2215{
2216 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
2217}
2218
2219/*
2220 * In a script change <SID>name() and s:name() to K_SNR 123_name().
2221 * Change <SNR>123_name() to K_SNR 123_name().
2222 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002223 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002224 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002225 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00002226fname_trans_sid(
2227 char_u *name,
2228 char_u *fname_buf,
2229 char_u **tofree,
2230 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002231{
John Marriottb32800f2025-02-01 15:25:34 +01002232 char_u *script_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002233 char_u *fname;
John Marriottb32800f2025-02-01 15:25:34 +01002234 size_t fnamelen;
2235 size_t fname_buflen;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002236
John Marriottb32800f2025-02-01 15:25:34 +01002237 script_name = name + eval_fname_script(name);
2238 if (script_name == name)
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002239 return name; // no prefix
2240
2241 fname_buf[0] = K_SPECIAL;
2242 fname_buf[1] = KS_EXTRA;
2243 fname_buf[2] = (int)KE_SNR;
John Marriottb32800f2025-02-01 15:25:34 +01002244 fname_buflen = 3;
2245 if (!eval_fname_sid(name)) // "<SID>" or "s:"
2246 fname_buf[fname_buflen] = NUL;
2247 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002248 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002249 if (current_sctx.sc_sid <= 0)
2250 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002251 else
2252 {
John Marriottb32800f2025-02-01 15:25:34 +01002253 fname_buflen += vim_snprintf((char *)fname_buf + 3,
2254 FLEN_FIXED - 3,
2255 "%ld_",
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002256 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002257 }
2258 }
John Marriottb32800f2025-02-01 15:25:34 +01002259 fnamelen = fname_buflen + STRLEN(script_name);
2260 if (fnamelen < FLEN_FIXED)
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002261 {
John Marriottb32800f2025-02-01 15:25:34 +01002262 STRCPY(fname_buf + fname_buflen, script_name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002263 fname = fname_buf;
2264 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002265 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002266 {
John Marriottb32800f2025-02-01 15:25:34 +01002267 fname = alloc(fnamelen + 1);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002268 if (fname == NULL)
2269 *error = FCERR_OTHER;
2270 else
2271 {
2272 *tofree = fname;
John Marriottb32800f2025-02-01 15:25:34 +01002273 vim_snprintf((char *)fname, fnamelen + 1, "%s%s", fname_buf, script_name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002274 }
2275 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002276 return fname;
2277}
2278
2279/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002280 * Concatenate the script ID and function name into "<SNR>99_name".
2281 * "buffer" must have size MAX_FUNC_NAME_LEN.
2282 */
2283 void
2284func_name_with_sid(char_u *name, int sid, char_u *buffer)
2285{
2286 // A script-local function is stored as "<SNR>99_name".
2287 buffer[0] = K_SPECIAL;
2288 buffer[1] = KS_EXTRA;
2289 buffer[2] = (int)KE_SNR;
2290 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2291 (long)sid, name);
2292}
2293
2294/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295 * Find a function "name" in script "sid".
2296 */
2297 static ufunc_T *
2298find_func_with_sid(char_u *name, int sid)
2299{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002300 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002301 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002302
Bram Moolenaarb8822442022-01-11 15:24:05 +00002303 if (!SCRIPT_ID_VALID(sid))
2304 return NULL; // not in a script
2305
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002306 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002307 hi = hash_find(&func_hashtab, buffer);
2308 if (!HASHITEM_EMPTY(hi))
2309 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002310 return NULL;
2311}
2312
2313/*
2314 * Find a function "name" in script "sid" prefixing the autoload prefix.
2315 */
2316 static ufunc_T *
2317find_func_with_prefix(char_u *name, int sid)
2318{
2319 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002320 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002321 scriptitem_T *si;
2322
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002323 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002324 return NULL; // already has the prefix
2325 if (!SCRIPT_ID_VALID(sid))
2326 return NULL; // not in a script
2327 si = SCRIPT_ITEM(sid);
2328 if (si->sn_autoload_prefix != NULL)
2329 {
2330 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2331 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002332 char_u *namep;
2333
2334 // skip a "<SNR>99_" prefix
2335 namep = untrans_function_name(name);
2336 if (namep == NULL)
2337 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002338
2339 // An exported function in an autoload script is stored as
2340 // "dir#path#name".
2341 if (len < sizeof(buffer))
2342 auto_name = buffer;
2343 else
2344 auto_name = alloc(len);
2345 if (auto_name != NULL)
2346 {
2347 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002348 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002349 hi = hash_find(&func_hashtab, auto_name);
2350 if (auto_name != buffer)
2351 vim_free(auto_name);
2352 if (!HASHITEM_EMPTY(hi))
2353 return HI2UF(hi);
2354 }
2355 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002356
2357 return NULL;
2358}
2359
2360/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002361 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002362 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2363 * functions.
2364 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002365 * Return NULL for unknown function.
2366 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002367 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002368find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002369{
2370 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002373 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002375 // Find script-local function before global one.
2376 if (in_vim9script() && eval_isnamec1(*name)
2377 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002378 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002379 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2380 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002381 if (func != NULL)
2382 return func;
2383 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002384 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2385 {
2386 char_u *p = name + 5;
2387 long sid;
2388
2389 // printable "<SNR>123_Name" form
2390 sid = getdigits(&p);
2391 if (*p == '_')
2392 {
2393 func = find_func_with_sid(p + 1, (int)sid);
2394 if (func != NULL)
2395 return func;
2396 }
2397 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002399
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002400 if ((flags & FFED_NO_GLOBAL) == 0)
2401 {
2402 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002403 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002404 if (!HASHITEM_EMPTY(hi))
2405 return HI2UF(hi);
2406 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407
Bram Moolenaarb8822442022-01-11 15:24:05 +00002408 // Find autoload function if this is an autoload script.
Yegappan Lakshmanan6289f912025-01-14 17:13:36 +01002409 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
Bram Moolenaarb8822442022-01-11 15:24:05 +00002410 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002411}
2412
2413/*
2414 * Find a function by name, return pointer to it in ufuncs.
2415 * "cctx" is passed in a :def function to find imported functions.
2416 * Return NULL for unknown or dead function.
2417 */
2418 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002419find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002421 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002422
2423 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2424 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002425 return NULL;
2426}
2427
2428/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002429 * Return TRUE if "ufunc" is a global function.
2430 */
2431 int
2432func_is_global(ufunc_T *ufunc)
2433{
2434 return ufunc->uf_name[0] != K_SPECIAL;
2435}
2436
2437/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002438 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2439 */
2440 int
2441func_requires_g_prefix(ufunc_T *ufunc)
2442{
John Marriottb32800f2025-02-01 15:25:34 +01002443 return func_is_global(ufunc)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002444 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002445 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01002446 && !SAFE_isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002447}
2448
2449/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002450 * Copy the function name of "fp" to buffer "buf".
2451 * "buf" must be able to hold the function name plus three bytes.
2452 * Takes care of script-local function names.
2453 */
John Marriottb32800f2025-02-01 15:25:34 +01002454 static int
2455cat_func_name(char_u *buf, size_t bufsize, ufunc_T *fp)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002456{
John Marriottb32800f2025-02-01 15:25:34 +01002457 int len;
2458
Bram Moolenaar0f769812020-09-12 18:32:34 +02002459 if (!func_is_global(fp))
John Marriottb32800f2025-02-01 15:25:34 +01002460 len = vim_snprintf((char *)buf, bufsize, "<SNR>%s", fp->uf_name + 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002461 else
John Marriottb32800f2025-02-01 15:25:34 +01002462 len = vim_snprintf((char *)buf, bufsize, "%s", fp->uf_name);
2463
2464 return (len >= (int)bufsize) ? (int)bufsize - 1 : len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002465}
2466
2467/*
2468 * Add a number variable "name" to dict "dp" with value "nr".
2469 */
2470 static void
2471add_nr_var(
2472 dict_T *dp,
2473 dictitem_T *v,
2474 char *name,
2475 varnumber_T nr)
2476{
2477 STRCPY(v->di_key, name);
2478 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002479 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002480 v->di_tv.v_type = VAR_NUMBER;
2481 v->di_tv.v_lock = VAR_FIXED;
2482 v->di_tv.vval.v_number = nr;
2483}
2484
2485/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002486 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002487 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002488 static void
2489free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002490{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002491 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002492
Bram Moolenaarca16c602022-09-06 18:57:08 +01002493 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002494 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002495 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002496
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002497 // When garbage collecting a funccall_T may be freed before the
2498 // function that references it, clear its uf_scoped field.
2499 // The function may have been redefined and point to another
2500 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002501 if (fp != NULL && fp->uf_scoped == fc)
2502 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002503 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002504 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002505
Bram Moolenaarca16c602022-09-06 18:57:08 +01002506 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002507 vim_free(fc);
2508}
2509
2510/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002511 * Free "fc" and what it contains.
2512 * Can be called only when "fc" is kept beyond the period of it called,
2513 * i.e. after cleanup_function_call(fc).
2514 */
2515 static void
2516free_funccal_contents(funccall_T *fc)
2517{
2518 listitem_T *li;
2519
2520 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002521 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002522
2523 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002524 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002525
2526 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002527 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002528 clear_tv(&li->li_tv);
2529
2530 free_funccal(fc);
2531}
2532
2533/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002534 * Handle the last part of returning from a function: free the local hashtable.
2535 * Unless it is still in use by a closure.
2536 */
2537 static void
2538cleanup_function_call(funccall_T *fc)
2539{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002540 int may_free_fc = fc->fc_refcount <= 0;
2541 int free_fc = TRUE;
2542
Bram Moolenaarca16c602022-09-06 18:57:08 +01002543 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002544
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002545 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002546 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2547 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002548 else
2549 free_fc = FALSE;
2550
2551 // If the a:000 list and the l: and a: dicts are not referenced and
2552 // there is no closure using it, we can free the funccall_T and what's
2553 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002554 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2555 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002556 else
2557 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002558 int todo;
2559 hashitem_T *hi;
2560 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002561
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002562 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002563
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002564 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002565 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002566 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002567 {
2568 if (!HASHITEM_EMPTY(hi))
2569 {
2570 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002571 di = HI2DI(hi);
2572 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002573 }
2574 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002575 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002576
Bram Moolenaarca16c602022-09-06 18:57:08 +01002577 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2578 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002579 else
2580 {
2581 listitem_T *li;
2582
2583 free_fc = FALSE;
2584
2585 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002586 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002587 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002588 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002589
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002590 if (free_fc)
2591 free_funccal(fc);
2592 else
2593 {
2594 static int made_copy = 0;
2595
2596 // "fc" is still in use. This can happen when returning "a:000",
2597 // assigning "l:" to a global variable or defining a closure.
2598 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002599 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002600 previous_funccal = fc;
2601
2602 if (want_garbage_collect)
2603 // If garbage collector is ready, clear count.
2604 made_copy = 0;
2605 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002606 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002607 // We have made a lot of copies, worth 4 Mbyte. This can happen
2608 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002609 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002610 // too much memory.
2611 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002612 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002613 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002614 }
2615}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002616
2617/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002618 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2619 */
2620 static int
2621numbered_function(char_u *name)
2622{
Keith Thompson184f71c2024-01-04 21:19:04 +01002623 return SAFE_isdigit(*name)
2624 || (name[0] == 'g' && name[1] == ':' && SAFE_isdigit(name[2]));
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002625}
2626
2627/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002628 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002629 * 1. ordinary names, function defined with :function or :def;
2630 * can start with "<SNR>123_" literally or with K_SPECIAL.
2631 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002632 * For the first we only count the name stored in func_hashtab as a reference,
2633 * using function() does not count as a reference, because the function is
2634 * looked up by name.
2635 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002636 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002637func_name_refcount(char_u *name)
2638{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002639 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002640}
2641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642/*
2643 * Unreference "fc": decrement the reference count and free it when it
2644 * becomes zero. "fp" is detached from "fc".
2645 * When "force" is TRUE we are exiting.
2646 */
2647 static void
2648funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2649{
2650 funccall_T **pfc;
2651 int i;
2652
2653 if (fc == NULL)
2654 return;
2655
2656 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002657 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2658 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2659 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2660 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 {
2662 if (fc == *pfc)
2663 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002664 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 free_funccal_contents(fc);
2666 return;
2667 }
2668 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002669 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2670 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2671 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672}
2673
2674/*
2675 * Remove the function from the function hashtable. If the function was
2676 * deleted while it still has references this was already done.
2677 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2678 */
2679 static int
2680func_remove(ufunc_T *fp)
2681{
2682 hashitem_T *hi;
2683
2684 // Return if it was already virtually deleted.
2685 if (fp->uf_flags & FC_DEAD)
2686 return FALSE;
2687
2688 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002689 if (HASHITEM_EMPTY(hi))
2690 return FALSE;
2691
2692 // When there is a def-function index do not actually remove the
2693 // function, so we can find the index when defining the function again.
2694 // Do remove it when it's a copy.
2695 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002696 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002697 fp->uf_flags |= FC_DEAD;
2698 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002700 hash_remove(&func_hashtab, hi, "remove function");
2701 fp->uf_flags |= FC_DELETED;
2702 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703}
2704
2705 static void
2706func_clear_items(ufunc_T *fp)
2707{
2708 ga_clear_strings(&(fp->uf_args));
2709 ga_clear_strings(&(fp->uf_def_args));
2710 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002712 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002713 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01002714 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002715
2716 // Increment the refcount of this function to avoid it being freed
2717 // recursively when the partial is freed.
2718 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002719 partial_unref(fp->uf_partial);
2720 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002721 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002722
2723#ifdef FEAT_LUA
2724 if (fp->uf_cb_free != NULL)
2725 {
2726 fp->uf_cb_free(fp->uf_cb_state);
2727 fp->uf_cb_free = NULL;
2728 }
2729
2730 fp->uf_cb_state = NULL;
2731 fp->uf_cb = NULL;
2732#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733#ifdef FEAT_PROFILE
2734 VIM_CLEAR(fp->uf_tml_count);
2735 VIM_CLEAR(fp->uf_tml_total);
2736 VIM_CLEAR(fp->uf_tml_self);
2737#endif
2738}
2739
2740/*
2741 * Free all things that a function contains. Does not free the function
2742 * itself, use func_free() for that.
2743 * When "force" is TRUE we are exiting.
2744 */
2745 static void
2746func_clear(ufunc_T *fp, int force)
2747{
2748 if (fp->uf_cleared)
2749 return;
2750 fp->uf_cleared = TRUE;
2751
2752 // clear this function
2753 func_clear_items(fp);
2754 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002755 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756}
2757
2758/*
2759 * Free a function and remove it from the list of functions. Does not free
2760 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002761 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002762 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002764 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002765func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766{
2767 // Only remove it when not done already, otherwise we would remove a newer
2768 // version of the function with the same name.
2769 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2770 func_remove(fp);
2771
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002772 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002773 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002774 if (fp->uf_dfunc_idx > 0)
2775 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002776 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002778 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002779 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002780 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781}
2782
2783/*
2784 * Free all things that a function contains and free the function itself.
2785 * When "force" is TRUE we are exiting.
2786 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002787 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788func_clear_free(ufunc_T *fp, int force)
2789{
2790 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002791 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2792 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002793 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002794 else
2795 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796}
2797
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002798/*
2799 * Copy already defined function "lambda" to a new function with name "global".
2800 * This is for when a compiled function defines a global function.
2801 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002802 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002803copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002804 char_u *lambda,
2805 char_u *global,
2806 loopvarinfo_T *loopvarinfo,
2807 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002808{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002809 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002810 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002811
2812 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002813 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002814 semsg(_(e_lambda_function_not_found_str), lambda);
2815 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002816 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002817
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002818 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002819 if (fp != NULL)
2820 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002821 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002822 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002823 return FAIL;
2824 }
2825
John Marriottb32800f2025-02-01 15:25:34 +01002826 fp = alloc_ufunc(global, STRLEN(global));
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002827 if (fp == NULL)
2828 return FAIL;
2829
2830 fp->uf_varargs = ufunc->uf_varargs;
2831 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2832 fp->uf_def_status = ufunc->uf_def_status;
2833 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2834 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2835 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2836 == FAIL
2837 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2838 goto failed;
2839
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002840 if (ufunc->uf_arg_types != NULL)
2841 {
2842 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2843 if (fp->uf_arg_types == NULL)
2844 goto failed;
2845 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2846 sizeof(type_T *) * fp->uf_args.ga_len);
2847 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002848 if (ufunc->uf_va_name != NULL)
2849 {
2850 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2851 if (fp->uf_va_name == NULL)
2852 goto failed;
2853 }
2854 fp->uf_ret_type = ufunc->uf_ret_type;
2855
2856 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002857
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002858 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002859
2860 // the referenced dfunc_T is now used one more time
2861 link_def_function(fp);
2862
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002863 // Create a partial to store the context of the function where it was
2864 // instantiated. Only needs to be done once. Do this on the original
2865 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002866 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2867 {
2868 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2869
2870 if (pt == NULL)
2871 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002872 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002873 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002874 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002875 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002876 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002877 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002878 }
2879
2880 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002881
2882failed:
2883 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002884 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002885}
2886
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002887static int funcdepth = 0;
2888
2889/*
2890 * Increment the function call depth count.
2891 * Return FAIL when going over 'maxfuncdepth'.
2892 * Otherwise return OK, must call funcdepth_decrement() later!
2893 */
2894 int
2895funcdepth_increment(void)
2896{
2897 if (funcdepth >= p_mfd)
2898 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002899 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002900 return FAIL;
2901 }
2902 ++funcdepth;
2903 return OK;
2904}
2905
2906 void
2907funcdepth_decrement(void)
2908{
2909 --funcdepth;
2910}
2911
2912/*
2913 * Get the current function call depth.
2914 */
2915 int
2916funcdepth_get(void)
2917{
2918 return funcdepth;
2919}
2920
2921/*
2922 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002923 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002924 * times as funcdepth_increment().
2925 */
2926 void
2927funcdepth_restore(int depth)
2928{
2929 funcdepth = depth;
2930}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002931
2932/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002933 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2934 * "rettv".
2935 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2936 * Returns NULL when allocation fails.
2937 */
2938 funccall_T *
2939create_funccal(ufunc_T *fp, typval_T *rettv)
2940{
2941 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2942
2943 if (fc == NULL)
2944 return NULL;
2945 fc->fc_caller = current_funccal;
2946 current_funccal = fc;
2947 fc->fc_func = fp;
2948 func_ptr_ref(fp);
2949 fc->fc_rettv = rettv;
2950 return fc;
2951}
2952
2953/*
2954 * To be called when returning from a compiled function; restores
2955 * current_funccal.
2956 */
2957 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002958remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002959{
2960 funccall_T *fc = current_funccal;
2961
2962 current_funccal = fc->fc_caller;
2963 free_funccal(fc);
2964}
2965
2966/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002967 * Call a user function.
2968 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002969 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002970call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002971 ufunc_T *fp, // pointer to function
2972 int argcount, // nr of args
2973 typval_T *argvars, // arguments
2974 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002975 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002976 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002977{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002978 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002979 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002980 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002981 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002982 funccall_T *fc;
2983 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002984 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002985 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002986 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002987 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002988 int i;
2989 int ai;
2990 int islambda = FALSE;
2991 char_u numbuf[NUMBUFLEN];
2992 char_u *name;
John Marriottb32800f2025-02-01 15:25:34 +01002993 size_t namelen;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002994 typval_T *tv_to_free[MAX_FUNC_ARGS];
2995 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002996#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002997 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002998#endif
ichizok7e5fe382023-04-15 13:17:50 +01002999 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003000
Bram Moolenaarb2049902021-01-24 12:53:53 +01003001#ifdef FEAT_PROFILE
3002 CLEAR_FIELD(profile_info);
3003#endif
3004
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003005 // If depth of calling is getting too high, don't execute the function.
3006 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003007 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003008 rettv->v_type = VAR_NUMBER;
3009 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003010 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003011 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003012
Bram Moolenaare38eab22019-12-05 21:50:01 +01003013 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003014
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01003015 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01003016 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003017 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003018 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003019 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01003020 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
3021 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003022 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01003023 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003024
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003025 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01003027#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01003028 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003029#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003030 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01003031#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003032 if (do_profiling == PROF_YES)
3033 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01003034#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003035 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003036 if (call_def_function(fp, argcount, argvars, 0,
3037 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
3038 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003039 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01003040#ifdef FEAT_PROFILE
3041 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01003042 || (caller != NULL && caller->uf_profiling)))
3043 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01003044#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01003045 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003046 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003047 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 }
3049
Bram Moolenaar38453522021-11-28 22:00:12 +00003050 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003051
3052 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01003053 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
3054 * variables with names up to VAR_SHORT_LEN long. This avoids having to
3055 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003056 */
3057 /*
3058 * Init l: variables.
3059 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01003060 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003061 if (selfdict != NULL)
3062 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003063 // Set l:self to "selfdict". Use "name" to avoid a warning from
3064 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01003065 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003066 name = v->di_key;
3067 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01003068 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003069 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003070 v->di_tv.v_type = VAR_DICT;
3071 v->di_tv.v_lock = 0;
3072 v->di_tv.vval.v_dict = selfdict;
3073 ++selfdict->dv_refcount;
3074 }
3075
3076 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003077 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003078 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003079 * Set a:000 to a list with room for the "..." arguments.
3080 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01003081 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003082 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01003083 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003084 (varnumber_T)(argcount >= fp->uf_args.ga_len
3085 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01003086 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003087 if ((fp->uf_flags & FC_NOARGS) == 0)
3088 {
3089 // Use "name" to avoid a warning from some compiler that checks the
3090 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01003091 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003092 name = v->di_key;
3093 STRCPY(name, "000");
3094 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003095 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003096 v->di_tv.v_type = VAR_LIST;
3097 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003098 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003099 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01003100 CLEAR_FIELD(fc->fc_l_varlist);
3101 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
3102 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003103
3104 /*
3105 * Set a:firstline to "firstline" and a:lastline to "lastline".
3106 * Set a:name to named arguments.
3107 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003108 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003109 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003110 if ((fp->uf_flags & FC_NOARGS) == 0)
3111 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003112 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
3113 "firstline", (varnumber_T)funcexe->fe_firstline);
3114 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
3115 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003116 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003117 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003118 {
3119 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003120 typval_T def_rettv;
3121 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003122
3123 ai = i - fp->uf_args.ga_len;
3124 if (ai < 0)
3125 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003126 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003127 name = FUNCARG(fp, i);
3128 if (islambda)
3129 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003130
3131 // evaluate named argument default expression
3132 isdefault = ai + fp->uf_def_args.ga_len >= 0
3133 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
3134 && argvars[i].vval.v_number == VVAL_NONE));
3135 if (isdefault)
3136 {
3137 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003138
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003139 def_rettv.v_type = VAR_NUMBER;
3140 def_rettv.vval.v_number = -1;
3141
3142 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
3143 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003144 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003145 {
3146 default_arg_err = 1;
3147 break;
3148 }
3149 }
John Marriottb32800f2025-02-01 15:25:34 +01003150
3151 namelen = STRLEN(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003152 }
3153 else
3154 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003155 if ((fp->uf_flags & FC_NOARGS) != 0)
3156 // Bail out if no a: arguments used (in lambda).
3157 break;
3158
Bram Moolenaare38eab22019-12-05 21:50:01 +01003159 // "..." argument a:1, a:2, etc.
John Marriottb32800f2025-02-01 15:25:34 +01003160 namelen = vim_snprintf((char *)numbuf, sizeof(numbuf), "%d", ai + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003161 name = numbuf;
3162 }
John Marriottb32800f2025-02-01 15:25:34 +01003163 if (fixvar_idx < FIXVAR_CNT && namelen <= VAR_SHORT_LEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003164 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003165 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003166 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003167 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003168 }
3169 else
3170 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003171 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003172 if (v == NULL)
3173 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003174 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003175 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003176
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003177 // Note: the values are copied directly to avoid alloc/free.
3178 // "argvars" must have VAR_FIXED for v_lock.
3179 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003180 v->di_tv.v_lock = VAR_FIXED;
3181
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003182 if (isdefault)
3183 // Need to free this later, no matter where it's stored.
3184 tv_to_free[tv_to_free_len++] = &v->di_tv;
3185
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003186 if (addlocal)
3187 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003188 // Named arguments should be accessed without the "a:" prefix in
3189 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003190 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003191 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003192 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003193 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003194 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003195
3196 if (ai >= 0 && ai < MAX_FUNC_ARGS)
3197 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003198 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003199
3200 li->li_tv = argvars[i];
3201 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003202 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003203 }
3204 }
3205
Bram Moolenaare38eab22019-12-05 21:50:01 +01003206 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003207 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003208
3209 if (fp->uf_flags & FC_SANDBOX)
3210 {
3211 using_sandbox = TRUE;
3212 ++sandbox;
3213 }
3214
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003215 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003216 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003217 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003218 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003219 ++no_wait_return;
3220 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003221
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003222 smsg(_("calling %s"), SOURCING_NAME);
3223 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003224 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003225 char_u buf[MSG_BUF_LEN];
3226 char_u numbuf2[NUMBUFLEN];
3227 char_u *tofree;
3228 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003229
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003230 msg_puts("(");
3231 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003232 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003233 if (i > 0)
3234 msg_puts(", ");
3235 if (argvars[i].v_type == VAR_NUMBER)
3236 msg_outnum((long)argvars[i].vval.v_number);
3237 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003238 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003239 // Do not want errors such as E724 here.
3240 ++emsg_off;
3241 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3242 --emsg_off;
3243 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003244 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003245 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003246 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003247 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3248 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003249 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003250 msg_puts((char *)s);
3251 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003252 }
3253 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003254 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003255 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003256 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003257 msg_puts("\n"); // don't overwrite this either
3258
3259 verbose_leave_scroll();
3260 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003261 }
3262#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003263 if (do_profiling == PROF_YES)
3264 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003265 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003266#endif
3267
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003268 // "legacy" does not apply to commands in the function
3269 sticky_cmdmod_flags = 0;
3270
Bram Moolenaar98aff652022-09-06 21:02:35 +01003271 // If called from a compiled :def function the execution context must be
3272 // hidden, any deferred functions need to be added to the function being
3273 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003274 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003275
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003276 save_current_sctx = current_sctx;
3277 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003278 save_did_emsg = did_emsg;
3279 did_emsg = FALSE;
3280
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003281 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003282 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003283 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003284 retval = FCERR_FAILED;
3285 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003286 else if (islambda)
3287 {
3288 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3289
3290 // A Lambda always has the command "return {expr}". It is much faster
3291 // to evaluate {expr} directly.
3292 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003293 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003294 --ex_nesting_level;
3295 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003296 else
3297 // call do_cmdline() to execute the lines
3298 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003299 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3300
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003301 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003302 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003303
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003304 if (RedrawingDisabled > 0)
3305 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003306
Bram Moolenaare38eab22019-12-05 21:50:01 +01003307 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003308 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3309 {
3310 clear_tv(rettv);
3311 rettv->v_type = VAR_NUMBER;
3312 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003313
3314 // In corner cases returning a "failed" value is not backwards
3315 // compatible. Only do this for Vim9 script.
3316 if (in_vim9script())
3317 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003318 }
3319
3320#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003321 if (do_profiling == PROF_YES)
3322 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003323 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003324
3325 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3326 profile_may_end_func(&profile_info, fp, caller);
3327 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003328#endif
3329
Bram Moolenaare38eab22019-12-05 21:50:01 +01003330 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003331 if (p_verbose >= 12)
3332 {
3333 ++no_wait_return;
3334 verbose_enter_scroll();
3335
3336 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003337 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003338 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003339 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003340 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003341 else
3342 {
3343 char_u buf[MSG_BUF_LEN];
3344 char_u numbuf2[NUMBUFLEN];
3345 char_u *tofree;
3346 char_u *s;
3347
Bram Moolenaare38eab22019-12-05 21:50:01 +01003348 // The value may be very long. Skip the middle part, so that we
3349 // have some idea how it starts and ends. smsg() would always
3350 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003351 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003352 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003353 --emsg_off;
3354 if (s != NULL)
3355 {
3356 if (vim_strsize(s) > MSG_BUF_CLEN)
3357 {
3358 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3359 s = buf;
3360 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003361 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003362 vim_free(tofree);
3363 }
3364 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003365 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003366
3367 verbose_leave_scroll();
3368 --no_wait_return;
3369 }
3370
ichizok7e5fe382023-04-15 13:17:50 +01003371 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003372 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003373 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003374 restore_current_ectx(save_current_ectx);
3375
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003376#ifdef FEAT_PROFILE
3377 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003378 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003379#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003380 if (using_sandbox)
3381 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003382 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003383
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003384 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003385 {
3386 ++no_wait_return;
3387 verbose_enter_scroll();
3388
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003389 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003390 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003391
3392 verbose_leave_scroll();
3393 --no_wait_return;
3394 }
3395
3396 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003397 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003398 for (i = 0; i < tv_to_free_len; ++i)
3399 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003400 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003401
3402 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003403}
3404
3405/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003406 * Check the argument count for user function "fp".
3407 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3408 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003409 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003410check_user_func_argcount(ufunc_T *fp, int argcount)
3411{
3412 int regular_args = fp->uf_args.ga_len;
3413
3414 if (argcount < regular_args - fp->uf_def_args.ga_len)
3415 return FCERR_TOOFEW;
3416 else if (!has_varargs(fp) && argcount > regular_args)
3417 return FCERR_TOOMANY;
3418 return FCERR_UNKNOWN;
3419}
3420
3421/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003423 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003424 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425call_user_func_check(
3426 ufunc_T *fp,
3427 int argcount,
3428 typval_T *argvars,
3429 typval_T *rettv,
3430 funcexe_T *funcexe,
3431 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003432{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003433 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003434
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003435#ifdef FEAT_LUA
3436 if (fp->uf_flags & FC_CFUNC)
3437 {
3438 cfunc_T cb = fp->uf_cb;
3439
3440 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3441 }
3442#endif
3443
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003444 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3445 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003446 error = check_user_func_argcount(fp, argcount);
3447 if (error != FCERR_UNKNOWN)
3448 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003449
Bram Moolenaar50824712020-12-20 21:10:17 +01003450 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003451 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003453 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003455 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003456 int did_save_redo = FALSE;
3457 save_redo_T save_redo;
3458
3459 /*
3460 * Call the user function.
3461 * Save and restore search patterns, script variables and
3462 * redo buffer.
3463 */
3464 save_search_patterns();
3465 if (!ins_compl_active())
3466 {
3467 saveRedobuff(&save_redo);
3468 did_save_redo = TRUE;
3469 }
3470 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003471 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003472 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3474 // Function was unreferenced while being used, free it now.
3475 func_clear_free(fp, FALSE);
3476 if (did_save_redo)
3477 restoreRedobuff(&save_redo);
3478 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003479 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003480
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003482}
3483
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003484static funccal_entry_T *funccal_stack = NULL;
3485
3486/*
3487 * Save the current function call pointer, and set it to NULL.
3488 * Used when executing autocommands and for ":source".
3489 */
3490 void
3491save_funccal(funccal_entry_T *entry)
3492{
3493 entry->top_funccal = current_funccal;
3494 entry->next = funccal_stack;
3495 funccal_stack = entry;
3496 current_funccal = NULL;
3497}
3498
3499 void
3500restore_funccal(void)
3501{
3502 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003503 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003504 else
3505 {
3506 current_funccal = funccal_stack->top_funccal;
3507 funccal_stack = funccal_stack->next;
3508 }
3509}
3510
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003511 funccall_T *
3512get_current_funccal(void)
3513{
3514 return current_funccal;
3515}
3516
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003517/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003518 * Return TRUE when currently at the script level:
3519 * - not in a function
3520 * - not executing an autocommand
3521 * Note that when an autocommand sources a script the result is FALSE;
3522 */
3523 int
3524at_script_level(void)
3525{
3526 return current_funccal == NULL && autocmd_match == NULL;
3527}
3528
3529/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003530 * Mark all functions of script "sid" as deleted.
3531 */
3532 void
3533delete_script_functions(int sid)
3534{
3535 hashitem_T *hi;
3536 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003537 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003538 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003539 size_t len;
3540
3541 buf[0] = K_SPECIAL;
3542 buf[1] = KS_EXTRA;
3543 buf[2] = (int)KE_SNR;
John Marriottb32800f2025-02-01 15:25:34 +01003544 len = 3 + vim_snprintf((char *)buf + 3, sizeof(buf) - 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003545
Bram Moolenaarce658352020-07-31 23:47:12 +02003546 while (todo > 0)
3547 {
3548 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003549 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003550 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003551 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003552 fp = HI2UF(hi);
3553 if (STRNCMP(fp->uf_name, buf, len) == 0)
3554 {
3555 int changed = func_hashtab.ht_changed;
3556
3557 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003558
3559 if (fp->uf_calls > 0)
3560 {
3561 // Function is executing, don't free it but do remove
3562 // it from the hashtable.
3563 if (func_remove(fp))
3564 fp->uf_refcount--;
3565 }
3566 else
3567 {
3568 func_clear(fp, TRUE);
3569 // When clearing a function another function can be
3570 // cleared as a side effect. When that happens start
3571 // over.
3572 if (changed != func_hashtab.ht_changed)
3573 break;
3574 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003575 }
3576 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003577 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003578 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003579}
3580
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003581#if defined(EXITFREE) || defined(PROTO)
3582 void
3583free_all_functions(void)
3584{
3585 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003586 ufunc_T *fp;
3587 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003588 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003589 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003590
Bram Moolenaare38eab22019-12-05 21:50:01 +01003591 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003592 while (current_funccal != NULL)
3593 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003594 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003595 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003596 if (current_funccal == NULL && funccal_stack != NULL)
3597 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003598 }
3599
Bram Moolenaare38eab22019-12-05 21:50:01 +01003600 // First clear what the functions contain. Since this may lower the
3601 // reference count of a function, it may also free a function and change
3602 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003603 while (todo > 0)
3604 {
3605 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003606 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003607 if (!HASHITEM_EMPTY(hi))
3608 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 // clear the def function index now
3610 fp = HI2UF(hi);
3611 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003612 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003613
Bram Moolenaare38eab22019-12-05 21:50:01 +01003614 // Only free functions that are not refcounted, those are
3615 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003616 if (func_name_refcount(fp->uf_name))
3617 ++skipped;
3618 else
3619 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003620 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003621 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003622 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003623 {
3624 skipped = 0;
3625 break;
3626 }
3627 }
3628 --todo;
3629 }
3630 }
3631
Bram Moolenaare38eab22019-12-05 21:50:01 +01003632 // Now actually free the functions. Need to start all over every time,
3633 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003634 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003635 while (func_hashtab.ht_used > skipped)
3636 {
3637 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003638 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003639 if (!HASHITEM_EMPTY(hi))
3640 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003641 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003642 // Only free functions that are not refcounted, those are
3643 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003644 fp = HI2UF(hi);
3645 if (func_name_refcount(fp->uf_name))
3646 ++skipped;
3647 else
3648 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003649 if (func_free(fp, FALSE) == OK)
3650 {
3651 skipped = 0;
3652 break;
3653 }
3654 // did not actually free it
3655 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003656 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003657 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003658 }
3659 if (skipped == 0)
3660 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661
3662 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003663}
3664#endif
3665
3666/*
3667 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003668 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3669 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003670 * "len" is the length of "name", or -1 for NUL terminated.
3671 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003672 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003673builtin_function(char_u *name, int len)
3674{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003675 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003676
Bram Moolenaara26b9702020-04-18 19:53:28 +02003677 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003678 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003679 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3680 {
3681 if (name[i] == AUTOLOAD_CHAR)
3682 return FALSE;
3683 if (!eval_isnamec(name[i]))
3684 {
3685 // "name.something" is not a builtin function
3686 if (name[i] == '.')
3687 return FALSE;
3688 break;
3689 }
3690 }
3691 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003692}
3693
3694 int
3695func_call(
3696 char_u *name,
3697 typval_T *args,
3698 partial_T *partial,
3699 dict_T *selfdict,
3700 typval_T *rettv)
3701{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003702 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003703 listitem_T *item;
3704 typval_T argv[MAX_FUNC_ARGS + 1];
3705 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003706 int r = 0;
3707
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003708 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003709 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003710 {
3711 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3712 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003713 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003714 break;
3715 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003716 // Make a copy of each argument. This is needed to be able to set
3717 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003718 copy_tv(&item->li_tv, &argv[argc++]);
3719 }
3720
3721 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003722 {
3723 funcexe_T funcexe;
3724
Bram Moolenaara80faa82020-04-12 19:37:17 +02003725 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003726 funcexe.fe_firstline = curwin->w_cursor.lnum;
3727 funcexe.fe_lastline = curwin->w_cursor.lnum;
3728 funcexe.fe_evaluate = TRUE;
3729 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003730 if (partial != NULL)
3731 {
3732 funcexe.fe_object = partial->pt_obj;
3733 if (funcexe.fe_object != NULL)
3734 ++funcexe.fe_object->obj_refcount;
3735 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003736 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003737 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3738 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003739
Bram Moolenaare38eab22019-12-05 21:50:01 +01003740 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003741 while (argc > 0)
3742 clear_tv(&argv[--argc]);
3743
3744 return r;
3745}
3746
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003747static int callback_depth = 0;
3748
3749 int
3750get_callback_depth(void)
3751{
3752 return callback_depth;
3753}
3754
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003755/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003756 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003757 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003758 */
3759 int
3760call_callback(
3761 callback_T *callback,
3762 int len, // length of "name" or -1 to use strlen()
3763 typval_T *rettv, // return value goes here
3764 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003765 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003766 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003767{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003768 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003769 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003770
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003771 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3772 return FAIL;
Christian Brabandt47510f32023-10-15 09:56:16 +02003773
zeertzjqfe583b12023-12-21 16:59:26 +01003774 if (callback_depth > p_mfd)
Christian Brabandt47510f32023-10-15 09:56:16 +02003775 {
3776 emsg(_(e_command_too_recursive));
3777 return FAIL;
3778 }
3779
Bram Moolenaara80faa82020-04-12 19:37:17 +02003780 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003781 funcexe.fe_evaluate = TRUE;
3782 funcexe.fe_partial = callback->cb_partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003783 if (callback->cb_partial != NULL)
3784 {
3785 funcexe.fe_object = callback->cb_partial->pt_obj;
3786 if (funcexe.fe_object != NULL)
3787 ++funcexe.fe_object->obj_refcount;
3788 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003789 ++callback_depth;
3790 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3791 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003792
3793 // When a :def function was called that uses :try an error would be turned
3794 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003795 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003796 {
3797 need_rethrow = FALSE;
3798 handle_did_throw();
3799 }
3800
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003801 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003802}
3803
3804/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003805 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003806 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003807 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3808 */
3809 varnumber_T
3810call_callback_retnr(
3811 callback_T *callback,
3812 int argcount, // number of "argvars"
3813 typval_T *argvars) // vars for arguments, must have "argcount"
3814 // PLUS ONE elements!
3815{
3816 typval_T rettv;
3817 varnumber_T retval;
3818
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003819 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003820 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003821
3822 retval = tv_get_number_chk(&rettv, NULL);
3823 clear_tv(&rettv);
3824 return retval;
3825}
3826
3827/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 * Give an error message for the result of a function.
3829 * Nothing if "error" is FCERR_NONE.
3830 */
3831 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003832user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833{
3834 switch (error)
3835 {
3836 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003837 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003838 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003839 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003840 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 break;
3842 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003843 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 break;
3845 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003846 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 break;
3848 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003849 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 break;
3851 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003852 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 break;
3854 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003855 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 break;
3857 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003858 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3859 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003861 case FCERR_OTHER:
3862 case FCERR_FAILED:
3863 // assume the error message was already given
3864 break;
3865 case FCERR_NONE:
3866 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 }
3868}
3869
3870/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003871 * Check the argument types "argvars[argcount]" for "name" using the
3872 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3873 * is already included in "argvars[]".
3874 * Will do nothing if "funcexe->fe_check_type" is NULL or
3875 * "funcexe->fe_evaluate" is FALSE;
3876 * Returns an FCERR_ value.
3877 */
3878 static funcerror_T
3879may_check_argument_types(
3880 funcexe_T *funcexe,
3881 typval_T *argvars,
3882 int argcount,
3883 int base_included,
3884 char_u *name)
3885{
3886 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3887 {
3888 // Check that the argument types are OK for the types of the funcref.
3889 if (check_argument_types(funcexe->fe_check_type,
3890 argvars, argcount,
3891 base_included ? NULL : funcexe->fe_basetv,
3892 name) == FAIL)
3893 return FCERR_OTHER;
3894 }
3895 return FCERR_NONE;
3896}
3897
3898/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003899 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003900 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003901 * Return FAIL when the function can't be called, OK otherwise.
3902 * Also returns OK when an error was encountered while executing the function.
3903 */
3904 int
3905call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003906 char_u *funcname, // name of the function
3907 int len, // length of "name" or -1 to use strlen()
3908 typval_T *rettv, // return value goes here
3909 int argcount_in, // number of "argvars"
3910 typval_T *argvars_in, // vars for arguments, must have "argcount"
3911 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003912 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003913{
3914 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003915 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003916 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003917 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003918 char_u fname_buf[FLEN_FIXED + 1];
3919 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003920 char_u *fname = NULL;
3921 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003922 int argcount = argcount_in;
3923 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003924 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003925 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003926 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003927 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003928 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003929 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003930 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003931 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003932
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003933 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3934 // even when call_func() returns FAIL.
3935 rettv->v_type = VAR_UNKNOWN;
3936
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003937 if (partial != NULL)
3938 fp = partial->pt_func;
3939 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003940 fp = funcexe->fe_ufunc;
3941
3942 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003943 {
3944 // Make a copy of the name, if it comes from a funcref variable it
3945 // could be changed or deleted in the called function.
3946 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3947 if (name == NULL)
3948 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003949
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003950 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3951 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003952
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003953 if (funcexe->fe_doesrange != NULL)
3954 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003955
3956 if (partial != NULL)
3957 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003958 // When the function has a partial with a dict and there is a dict
3959 // argument, use the dict argument. That is backwards compatible.
3960 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003961 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003962 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003963 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003964 {
3965 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003966 {
3967 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3968 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003969 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003970 goto theend;
3971 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003972 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003973 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003974 for (i = 0; i < argcount_in; ++i)
3975 argv[i + argv_clear] = argvars_in[i];
3976 argvars = argv;
3977 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003978
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003979 if (funcexe->fe_check_type != NULL
3980 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003981 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003982 // Now funcexe->fe_check_type is missing the added arguments,
3983 // make a copy of the type with the correction.
3984 check_type = *funcexe->fe_check_type;
3985 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003986 check_type.tt_args = check_type_args;
3987 CLEAR_FIELD(check_type_args);
3988 for (i = 0; i < check_type.tt_argcount; ++i)
3989 check_type_args[i + partial->pt_argc] =
3990 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003991 check_type.tt_argcount += partial->pt_argc;
3992 check_type.tt_min_argcount += partial->pt_argc;
3993 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003994 }
3995 }
3996
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003997 if (error == FCERR_NONE)
3998 // check the argument types if possible
3999 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
4000 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004001
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004002 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004003 {
4004 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02004005 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004006
Bram Moolenaar333894b2020-08-01 18:53:07 +02004007 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02004008 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02004009 {
4010 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004011 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02004012 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004013
Bram Moolenaare38eab22019-12-05 21:50:01 +01004014 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004015 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01004016 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004017
Bram Moolenaarf10806b2020-04-02 18:34:35 +02004018 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004019 {
4020 /*
4021 * User defined function.
4022 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02004023 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004024 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004025 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004026 if (fp != NULL && !is_global && in_vim9script()
4027 && func_requires_g_prefix(fp))
4028 // In Vim9 script g: is required to find a global
4029 // non-autoload function.
4030 fp = NULL;
4031 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004032
Bram Moolenaare38eab22019-12-05 21:50:01 +01004033 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004034 if (fp == NULL
4035 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004036 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004037 && !aborting())
4038 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004039 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004040 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004041 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004042 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004043 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
4044 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004045 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004046 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004047 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02004048 if (fp == NULL)
4049 {
4050 char_u *p = untrans_function_name(rfname);
4051
4052 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02004053 // Don't do this if the name starts with "s:".
4054 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004055 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004056 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004057
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004058 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01004059 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004060 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004061 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004062 int need_arg_check = FALSE;
4063 if (funcexe->fe_check_type == NULL)
4064 {
4065 funcexe->fe_check_type = fp->uf_func_type;
4066 need_arg_check = TRUE;
4067 }
4068
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004069 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004070 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01004071 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004072 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004073 argv_clear, fp);
4074 need_arg_check = TRUE;
4075 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02004076
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004077 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004078 {
4079 // Method call: base->Method()
4080 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004081 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004082 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004083 argvars = argv;
4084 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004085 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004086 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004087
Bram Moolenaar2ba51232023-05-15 16:22:38 +01004088 // Check the argument types now that the function type and all
4089 // argument values are known, if not done above.
4090 if (need_arg_check)
4091 error = may_check_argument_types(funcexe, argvars, argcount,
4092 TRUE, (name != NULL) ? name : funcname);
4093 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
4094 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004096 }
4097 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004098 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004099 {
4100 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004101 * expr->method(): Find the method name in the table, call its
4102 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02004103 */
4104 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004105 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004106 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004107 else
4108 {
4109 /*
4110 * Find the function name in the table, call its implementation.
4111 */
4112 error = call_internal_func(fname, argcount, argvars, rettv);
4113 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02004114
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004115 /*
4116 * The function call (or "FuncUndefined" autocommand sequence) might
4117 * have been aborted by an error, an interrupt, or an explicitly thrown
4118 * exception that has not been caught so far. This situation can be
4119 * tested for by calling aborting(). For an error in an internal
4120 * function or for the "E132" error in call_user_func(), however, the
4121 * throw point at which the "force_abort" flag (temporarily reset by
4122 * emsg()) is normally updated has not been reached yet. We need to
4123 * update that flag first to make aborting() reliable.
4124 */
4125 update_force_abort();
4126 }
Bram Moolenaaref140542019-12-31 21:27:13 +01004127 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004128 ret = OK;
4129
Bram Moolenaar4c054e92019-11-10 00:13:50 +01004130theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004131 /*
4132 * Report an error unless the argument evaluation or function call has been
4133 * cancelled due to an aborting error, an interrupt, or an exception.
4134 */
4135 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004136 user_func_error(error, (name != NULL) ? name : funcname,
4137 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004138
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004139 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004140 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004141 clear_tv(&argv[--argv_clear + argv_base]);
4142
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004143 vim_free(tofree);
4144 vim_free(name);
4145
4146 return ret;
4147}
4148
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004149/*
4150 * Call a function without arguments, partial or dict.
4151 * This is like call_func() when the call is only "FuncName()".
4152 * To be used by "expr" options.
4153 * Returns NOTDONE when the function could not be found.
4154 */
4155 int
4156call_simple_func(
4157 char_u *funcname, // name of the function
zeertzjqc1ed7882024-08-01 22:46:54 +02004158 size_t len, // length of "name"
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004159 typval_T *rettv) // return value goes here
4160{
4161 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00004162 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004163 char_u fname_buf[FLEN_FIXED + 1];
4164 char_u *tofree = NULL;
4165 char_u *name;
4166 char_u *fname;
4167 char_u *rfname;
4168 int is_global = FALSE;
4169 ufunc_T *fp;
4170
4171 rettv->v_type = VAR_NUMBER; // default rettv is number zero
4172 rettv->vval.v_number = 0;
4173
4174 // Make a copy of the name, an option can be changed in the function.
4175 name = vim_strnsave(funcname, len);
4176 if (name == NULL)
4177 return ret;
4178
4179 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
4180
4181 // Skip "g:" before a function name.
4182 if (fname[0] == 'g' && fname[1] == ':')
4183 {
4184 is_global = TRUE;
4185 rfname = fname + 2;
4186 }
4187 else
4188 rfname = fname;
4189 fp = find_func(rfname, is_global);
4190 if (fp != NULL && !is_global && in_vim9script()
4191 && func_requires_g_prefix(fp))
4192 // In Vim9 script g: is required to find a global non-autoload
4193 // function.
4194 fp = NULL;
4195 if (fp == NULL)
4196 ret = NOTDONE;
4197 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
4198 error = FCERR_DELETED;
4199 else if (fp != NULL)
4200 {
4201 typval_T argvars[1];
4202 funcexe_T funcexe;
4203
4204 argvars[0].v_type = VAR_UNKNOWN;
4205 CLEAR_FIELD(funcexe);
4206 funcexe.fe_evaluate = TRUE;
4207
4208 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
4209 if (error == FCERR_NONE)
4210 ret = OK;
4211 }
4212
4213 user_func_error(error, name, FALSE);
4214 vim_free(tofree);
4215 vim_free(name);
4216
4217 return ret;
4218}
4219
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004220 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004221printable_func_name(ufunc_T *fp)
4222{
4223 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4224}
4225
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004226/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004227 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4228 * TRUE. Otherwise return FALSE.
4229 */
4230 static int
4231function_list_modified(int prev_ht_changed)
4232{
4233 if (prev_ht_changed != func_hashtab.ht_changed)
4234 {
4235 emsg(_(e_function_list_was_modified));
4236 return TRUE;
4237 }
4238 return FALSE;
4239}
4240
4241/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004242 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004243 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004244 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004245list_func_head(ufunc_T *fp, int indent)
4246{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004247 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004248 int j;
4249
4250 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004251
4252 // a timer at the more prompt may have deleted the function
4253 if (function_list_modified(prev_ht_changed))
4254 return FAIL;
4255
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004256 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004257 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004258 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004259 msg_puts("def ");
4260 else
4261 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004262 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004263 msg_putchar('(');
4264 for (j = 0; j < fp->uf_args.ga_len; ++j)
4265 {
4266 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004267 msg_puts(", ");
4268 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 if (fp->uf_arg_types != NULL)
4270 {
4271 char *tofree;
4272
4273 msg_puts(": ");
4274 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4275 vim_free(tofree);
4276 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004277 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4278 {
4279 msg_puts(" = ");
4280 msg_puts(((char **)(fp->uf_def_args.ga_data))
4281 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4282 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004283 }
4284 if (fp->uf_varargs)
4285 {
4286 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004287 msg_puts(", ");
4288 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004289 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 if (fp->uf_va_name != NULL)
4291 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004292 if (!fp->uf_varargs)
4293 {
4294 if (j)
4295 msg_puts(", ");
4296 msg_puts("...");
4297 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004299 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300 {
4301 char *tofree;
4302
4303 msg_puts(": ");
4304 msg_puts(type_name(fp->uf_va_type, &tofree));
4305 vim_free(tofree);
4306 }
4307 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004308 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004309
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004310 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004311 {
4312 if (fp->uf_ret_type != &t_void)
4313 {
4314 char *tofree;
4315
4316 msg_puts(": ");
4317 msg_puts(type_name(fp->uf_ret_type, &tofree));
4318 vim_free(tofree);
4319 }
4320 }
4321 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004322 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004323 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004324 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004325 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004326 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004327 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004328 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004329 msg_clr_eos();
4330 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004331 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004332
4333 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004334}
4335
4336/*
4337 * Get a function name, translating "<SID>" and "<SNR>".
4338 * Also handles a Funcref in a List or Dictionary.
4339 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004340 * Set "*is_global" to TRUE when the function must be global, unless
4341 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004342 * flags:
4343 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004344 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004345 * TFN_QUIET: be quiet
4346 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004347 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004348 * Advances "pp" to just after the function name (if no error).
4349 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004350 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004351trans_function_name(
4352 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004353 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004354 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004355 int flags)
4356{
4357 return trans_function_name_ext(pp, is_global, skip, flags,
4358 NULL, NULL, NULL, NULL);
4359}
4360
4361/*
4362 * trans_function_name() with extra arguments.
4363 * "fdp", "partial", "type" and "ufunc" can be NULL.
4364 */
Yegappan Lakshmanana04003a2024-10-27 21:54:11 +01004365 static char_u *
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004366trans_function_name_ext(
4367 char_u **pp,
4368 int *is_global,
4369 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004370 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004371 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004372 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004373 type_T **type, // return: type of funcref
4374 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004375{
4376 char_u *name = NULL;
4377 char_u *start;
4378 char_u *end;
4379 int lead;
4380 char_u sid_buf[20];
4381 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004383 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004384 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004385 int vim9script = in_vim9script();
4386 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004387
4388 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004389 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004390 start = *pp;
4391
Bram Moolenaare38eab22019-12-05 21:50:01 +01004392 // Check for hard coded <SNR>: already translated function ID (from a user
4393 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004394 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4395 && (*pp)[2] == (int)KE_SNR)
4396 {
4397 *pp += 3;
4398 len = get_id_len(pp) + 3;
4399 return vim_strnsave(start, len);
4400 }
4401
Bram Moolenaare38eab22019-12-05 21:50:01 +01004402 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4403 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004404 lead = eval_fname_script(start);
4405 if (lead > 2)
4406 start += lead;
4407
Bram Moolenaare38eab22019-12-05 21:50:01 +01004408 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004409 end = get_lval(start, NULL, &lv, FALSE, skip,
4410 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004411 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004412 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004413 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004414 {
4415 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004416 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004417 goto theend;
4418 }
4419 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4420 {
4421 /*
4422 * Report an invalid expression in braces, unless the expression
4423 * evaluation has been cancelled due to an aborting error, an
4424 * interrupt, or an exception.
4425 */
4426 if (!aborting())
4427 {
4428 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004429 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004430 }
4431 else
4432 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4433 goto theend;
4434 }
4435
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004436 if (lv.ll_ufunc != NULL)
4437 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004438 if (ufunc != NULL)
4439 *ufunc = lv.ll_ufunc;
John Marriottb32800f2025-02-01 15:25:34 +01004440 name = vim_strnsave(lv.ll_ufunc->uf_name, lv.ll_ufunc->uf_namelen);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004441 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004442 goto theend;
4443 }
4444
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004445 if (lv.ll_tv != NULL)
4446 {
4447 if (fdp != NULL)
4448 {
4449 fdp->fd_dict = lv.ll_dict;
4450 fdp->fd_newkey = lv.ll_newkey;
4451 lv.ll_newkey = NULL;
4452 fdp->fd_di = lv.ll_di;
4453 }
4454 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4455 {
4456 name = vim_strsave(lv.ll_tv->vval.v_string);
4457 *pp = end;
4458 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004459 else if (lv.ll_tv->v_type == VAR_CLASS
4460 && lv.ll_tv->vval.v_class != NULL)
4461 {
4462 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4463 *pp = end;
4464 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004465 else if (lv.ll_tv->v_type == VAR_PARTIAL
4466 && lv.ll_tv->vval.v_partial != NULL)
4467 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004468 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004469 *pp = end;
4470 if (partial != NULL)
4471 *partial = lv.ll_tv->vval.v_partial;
4472 }
4473 else
4474 {
4475 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4476 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004477 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004478 else
4479 *pp = end;
4480 name = NULL;
4481 }
4482 goto theend;
4483 }
4484
4485 if (lv.ll_name == NULL)
4486 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004487 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004488 *pp = end;
4489 goto theend;
4490 }
4491
Bram Moolenaare38eab22019-12-05 21:50:01 +01004492 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004493 if (lv.ll_exp_name != NULL)
4494 {
4495 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004496 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004497 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004498 if (name == lv.ll_exp_name)
4499 name = NULL;
4500 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004501 else if (lv.ll_sid > 0)
4502 {
4503 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4504 int cc = *lv.ll_name_end;
4505
4506 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4507 *lv.ll_name_end = NUL;
4508 if (si->sn_autoload_prefix != NULL)
4509 {
4510 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4511 }
4512 else
4513 {
4514 sid_buf[0] = K_SPECIAL;
4515 sid_buf[1] = KS_EXTRA;
4516 sid_buf[2] = (int)KE_SNR;
4517 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004518 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004519 name = concat_str(sid_buf, lv.ll_name);
4520 }
4521 *lv.ll_name_end = cc;
4522 *pp = end;
4523 goto theend;
4524 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004525 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004526 {
4527 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004528 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004529 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004530 if (name == *pp)
4531 name = NULL;
4532 }
4533 if (name != NULL)
4534 {
4535 name = vim_strsave(name);
4536 *pp = end;
John Marriottb32800f2025-02-01 15:25:34 +01004537 if (name != NULL && STRNCMP(name, "<SNR>", 5) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004538 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004539 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004540 name[0] = K_SPECIAL;
4541 name[1] = KS_EXTRA;
4542 name[2] = (int)KE_SNR;
4543 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4544 }
4545 goto theend;
4546 }
4547
4548 if (lv.ll_exp_name != NULL)
4549 {
4550 len = (int)STRLEN(lv.ll_exp_name);
4551 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4552 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4553 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004554 // When there was "s:" already or the name expanded to get a
4555 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004556 lv.ll_name += 2;
4557 len -= 2;
4558 lead = 2;
4559 }
4560 }
4561 else
4562 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004563 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004564 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004565 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004566 if (lv.ll_name[0] == 'g')
4567 {
4568 if (is_global != NULL)
4569 {
4570 *is_global = TRUE;
4571 }
4572 else
4573 {
4574 // dropping "g:" without setting "is_global" won't work in
4575 // Vim9script, put it back later
4576 prefix_g = TRUE;
4577 extra = 2;
4578 }
4579 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004580 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004581 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004582 len = (int)(end - lv.ll_name);
4583 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004584 if (len <= 0)
4585 {
4586 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004587 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004588 goto theend;
4589 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004590
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004591 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004592 // starts with a lower case character: dict.func(). Or when in a class.
4593 vim9_local = ASCII_ISUPPER(*start) && vim9script
4594 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004596 /*
4597 * Copy the function name to allocated memory.
4598 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4599 * Accept <SNR>123_name() outside a script.
4600 */
4601 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004602 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004603 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004604 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004605 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004606 {
Kota Kato948a3892022-08-16 16:09:59 +01004607 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4608 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004609 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004610 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004611 goto theend;
4612 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004614 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004615 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004617 || eval_fname_sid(*pp))
4618 {
John Marriottb32800f2025-02-01 15:25:34 +01004619 size_t sid_buflen;
4620
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004622 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004623 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004624 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004625 goto theend;
4626 }
John Marriottb32800f2025-02-01 15:25:34 +01004627 sid_buflen = vim_snprintf((char *)sid_buf, sizeof(sid_buf), "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004628 if (vim9_local)
John Marriottb32800f2025-02-01 15:25:34 +01004629 extra = 3 + (int)sid_buflen;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004630 else
John Marriottb32800f2025-02-01 15:25:34 +01004631 lead += (int)sid_buflen;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004632 }
4633 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004634 // The function name must start with an upper case letter (unless it is a
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004635 // Vim9 class new() function or a Vim9 class private method or one of the
4636 // supported Vim9 object builtin functions)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004637 else if (!(flags & TFN_INT)
4638 && (builtin_function(lv.ll_name, len)
4639 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004640 && !((flags & TFN_IN_CLASS)
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004641 && (is_valid_builtin_obj_methodname(lv.ll_name)
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004642 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004643 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004644 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004645 : e_function_name_must_start_with_capital_or_s_str),
4646 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004647 goto theend;
4648 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004649 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004650 {
4651 char_u *cp = vim_strchr(lv.ll_name, ':');
4652
4653 if (cp != NULL && cp < end)
4654 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004655 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004656 goto theend;
4657 }
4658 }
4659
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004660 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004661 if (name != NULL)
4662 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004663 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004664 {
4665 name[0] = K_SPECIAL;
4666 name[1] = KS_EXTRA;
4667 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004668 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004669 STRCPY(name + 3, sid_buf);
4670 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004671 else if (prefix_g)
4672 {
4673 name[0] = 'g';
4674 name[1] = ':';
4675 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4677 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004678 }
4679 *pp = end;
4680
4681theend:
4682 clear_lval(&lv);
4683 return name;
4684}
4685
4686/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004687 * Assuming "name" is the result of trans_function_name() and it was prefixed
4688 * to use the script-local name, return the unmodified name (points into
4689 * "name"). Otherwise return NULL.
4690 * This can be used to first search for a script-local function and fall back
4691 * to the global function if not found.
4692 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004693 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004694untrans_function_name(char_u *name)
4695{
4696 char_u *p;
4697
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004698 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004699 {
4700 p = vim_strchr(name, '_');
4701 if (p != NULL)
4702 return p + 1;
4703 }
4704 return NULL;
4705}
4706
4707/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004708 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4709 * current script ID and returns the expanded function name. The caller should
4710 * free the returned name. If not called from a script context or the function
4711 * name doesn't start with these prefixes, then returns NULL.
4712 * This doesn't check whether the script-local function exists or not.
4713 */
4714 char_u *
4715get_scriptlocal_funcname(char_u *funcname)
4716{
4717 char sid_buf[25];
John Marriottb32800f2025-02-01 15:25:34 +01004718 size_t sid_buflen;
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004719 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004720 char_u *newname;
John Marriottb32800f2025-02-01 15:25:34 +01004721 size_t newnamesize;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004722 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004723
4724 if (funcname == NULL)
4725 return NULL;
4726
4727 if (STRNCMP(funcname, "s:", 2) != 0
4728 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004729 {
4730 ufunc_T *ufunc;
4731
4732 // The function name does not have a script-local prefix. Try finding
4733 // it when in a Vim9 script and there is no "g:" prefix.
4734 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4735 return NULL;
4736 ufunc = find_func(funcname, FALSE);
4737 if (ufunc == NULL || func_is_global(ufunc)
4738 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4739 return NULL;
4740 ++p;
4741 off = 0;
4742 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004743 else
4744 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004745
4746 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4747 {
4748 emsg(_(e_using_sid_not_in_script_context));
4749 return NULL;
4750 }
4751 // Expand s: prefix into <SNR>nr_<name>
John Marriottb32800f2025-02-01 15:25:34 +01004752 sid_buflen = vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004753 (long)current_sctx.sc_sid);
John Marriottb32800f2025-02-01 15:25:34 +01004754 newnamesize = sid_buflen + STRLEN(p + off) + 1;
4755 newname = alloc(newnamesize);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004756 if (newname == NULL)
4757 return NULL;
John Marriottb32800f2025-02-01 15:25:34 +01004758 vim_snprintf((char *)newname, newnamesize, "%s%s", sid_buf, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004759
4760 return newname;
4761}
4762
4763/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004764 * Return script-local "fname" with the 3-byte sequence replaced by
4765 * printable <SNR> in allocated memory.
4766 */
4767 char_u *
4768alloc_printable_func_name(char_u *fname)
4769{
4770 char_u *n = alloc(STRLEN(fname + 3) + 6);
4771
4772 if (n != NULL)
4773 {
4774 STRCPY(n, "<SNR>");
4775 STRCPY(n + 5, fname + 3);
4776 }
4777 return n;
4778}
4779
4780/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004781 * Call trans_function_name(), except that a lambda is returned as-is.
4782 * Returns the name in allocated memory.
4783 */
4784 char_u *
4785save_function_name(
4786 char_u **name,
4787 int *is_global,
4788 int skip,
4789 int flags,
4790 funcdict_T *fudi)
4791{
4792 char_u *p = *name;
4793 char_u *saved;
4794
4795 if (STRNCMP(p, "<lambda>", 8) == 0)
4796 {
4797 p += 8;
4798 (void)getdigits(&p);
4799 saved = vim_strnsave(*name, p - *name);
4800 if (fudi != NULL)
4801 CLEAR_POINTER(fudi);
4802 }
4803 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004804 saved = trans_function_name_ext(&p, is_global, skip,
4805 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004806 *name = p;
4807 return saved;
4808}
4809
4810/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004811 * List functions. When "regmatch" is NULL all of then.
4812 * Otherwise functions matching "regmatch".
4813 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004814 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004815list_functions(regmatch_T *regmatch)
4816{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004817 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004818 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004819 hashitem_T *hi;
4820
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004821 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004822 {
4823 if (!HASHITEM_EMPTY(hi))
4824 {
4825 ufunc_T *fp = HI2UF(hi);
4826
4827 --todo;
4828 if ((fp->uf_flags & FC_DEAD) == 0
4829 && (regmatch == NULL
4830 ? !message_filtered(fp->uf_name)
4831 && !func_name_refcount(fp->uf_name)
Keith Thompson184f71c2024-01-04 21:19:04 +01004832 : !SAFE_isdigit(*fp->uf_name)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004833 && vim_regexec(regmatch, fp->uf_name, 0)))
4834 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004835 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004836 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004837 if (function_list_modified(prev_ht_changed))
4838 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004839 }
4840 }
4841 }
4842}
4843
4844/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004845 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004846 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4847 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004848 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004849 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4850 * With CF_INTERFACE the function is define inside an interface, only the
4851 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004852 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004853 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004854 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004855define_function(
4856 exarg_T *eap,
4857 char_u *name_arg,
4858 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004859 int class_flags,
4860 ocmember_T *obj_members,
4861 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004862{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004863 int j;
4864 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004865 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004866 char_u *name = name_arg;
John Marriottb32800f2025-02-01 15:25:34 +01004867 size_t namelen = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004868 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004869 char_u *p;
4870 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004871 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004872 char_u *line_arg = NULL;
4873 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004875 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004876 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004877 garray_T newlines;
4878 int varargs = FALSE;
4879 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004880 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004881 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004882 int fp_allocated = FALSE;
4883 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004884 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004885 dictitem_T *v;
4886 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004887 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004888 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004889 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004890 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004891 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004892 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004893
4894 /*
4895 * ":function" without argument: list functions.
4896 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004897 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004898 {
4899 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004900 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004901 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004902 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004903 }
4904
4905 /*
4906 * ":function /pat": list functions matching pattern.
4907 */
4908 if (*eap->arg == '/')
4909 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004910 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004911 if (!eap->skip)
4912 {
4913 regmatch_T regmatch;
4914
4915 c = *p;
4916 *p = NUL;
4917 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4918 *p = c;
4919 if (regmatch.regprog != NULL)
4920 {
4921 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004922 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004923 vim_regfree(regmatch.regprog);
4924 }
4925 }
4926 if (*p == '/')
4927 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004928 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004929 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004930 }
4931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004932 ga_init(&newargs);
4933 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004934 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004935 ga_init(&default_args);
4936
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004937 /*
4938 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004939 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004940 * "name" == func, "fudi.fd_dict" == NULL
4941 * dict.func new dictionary entry
4942 * "name" == NULL, "fudi.fd_dict" set,
4943 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4944 * dict.func existing dict entry with a Funcref
4945 * "name" == func, "fudi.fd_dict" set,
4946 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4947 * dict.func existing dict entry that's not a Funcref
4948 * "name" == NULL, "fudi.fd_dict" set,
4949 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4950 * s:func script-local function name
4951 * g:func global function name, same as "func"
4952 */
4953 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004954 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004955 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004956 // nested function, argument is (args).
4957 paren = TRUE;
4958 CLEAR_FIELD(fudi);
4959 }
4960 else
4961 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004962 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004963 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004964 if (p[0] == 's' && p[1] == ':')
4965 {
4966 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4967 return NULL;
4968 }
4969 p = to_name_end(p, TRUE);
4970 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4971 {
4972 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4973 eap->arg);
4974 return NULL;
4975 }
4976 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004977 }
4978
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004979 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004980 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004981 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004982 paren = (vim_strchr(p, '(') != NULL);
4983 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004984 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004985 /*
4986 * Return on an invalid expression in braces, unless the expression
4987 * evaluation has been cancelled due to an aborting error, an
4988 * interrupt, or an exception.
4989 */
4990 if (!aborting())
4991 {
4992 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004993 semsg(_(e_key_not_present_in_dictionary_str),
4994 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004995 vim_free(fudi.fd_newkey);
4996 return NULL;
4997 }
4998 else
4999 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005000 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00005001
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00005002 // For "export def FuncName()" in an autoload script the function name
5003 // is stored with the legacy autoload name "dir#script#FuncName" so
5004 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00005005 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005006 {
5007 char_u *prefixed = may_prefix_autoload(name);
5008
5009 if (prefixed != NULL && prefixed != name)
5010 {
5011 vim_free(name);
5012 name = prefixed;
5013 }
5014 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00005015 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00005016 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00005017 {
5018 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
5019 goto ret_free;
5020 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005021 }
5022
Bram Moolenaare38eab22019-12-05 21:50:01 +01005023 // An error in a function call during evaluation of an expression in magic
5024 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005025 saved_did_emsg = did_emsg;
5026 did_emsg = FALSE;
5027
5028 /*
5029 * ":function func" with only function name: list function.
5030 */
5031 if (!paren)
5032 {
5033 if (!ends_excmd(*skipwhite(p)))
5034 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00005035 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005036 goto ret_free;
5037 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005038 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005039 if (eap->nextcmd != NULL)
5040 *p = NUL;
5041 if (!eap->skip && !got_int)
5042 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005043 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005044 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
5045 {
5046 char_u *up = untrans_function_name(name);
5047
5048 // With Vim9 script the name was made script-local, if not
5049 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02005050 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005051 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02005052 }
5053
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005054 if (fp != NULL)
5055 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00005056 // Check no function was added or removed from a timer, e.g. at
5057 // the more prompt. "fp" may then be invalid.
5058 int prev_ht_changed = func_hashtab.ht_changed;
5059
5060 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005061 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00005062 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
5063 {
5064 if (FUNCLINE(fp, j) == NULL)
5065 continue;
5066 msg_putchar('\n');
5067 msg_outnum((long)(j + 1));
5068 if (j < 9)
5069 msg_putchar(' ');
5070 if (j < 99)
5071 msg_putchar(' ');
5072 if (function_list_modified(prev_ht_changed))
5073 break;
5074 msg_prt_line(FUNCLINE(fp, j), FALSE);
5075 out_flush(); // show a line at a time
5076 ui_breakcheck();
5077 }
5078 if (!got_int)
5079 {
5080 msg_putchar('\n');
5081 if (!function_list_modified(prev_ht_changed))
5082 {
5083 if (fp->uf_def_status != UF_NOT_COMPILED)
5084 msg_puts(" enddef");
5085 else
5086 msg_puts(" endfunction");
5087 }
5088 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005089 }
5090 }
5091 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00005092 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005093 }
5094 goto ret_free;
5095 }
5096
5097 /*
5098 * ":function name(arg1, arg2)" Define function.
5099 */
5100 p = skipwhite(p);
5101 if (*p != '(')
5102 {
5103 if (!eap->skip)
5104 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005105 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005106 goto ret_free;
5107 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005108 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005109 if (vim_strchr(p, '(') != NULL)
5110 p = vim_strchr(p, '(');
5111 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005112
Bram Moolenaar4efd9942021-01-24 21:14:20 +01005113 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
5114 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01005115 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01005116 goto ret_free;
5117 }
5118
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02005119 // In Vim9 script only global functions can be redefined.
5120 if (vim9script && eap->forceit && !is_global)
5121 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005122 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02005123 goto ret_free;
5124 }
5125
Bram Moolenaar04935fb2022-01-08 16:19:22 +00005126 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005127
Bram Moolenaar04b12692020-05-04 23:24:44 +02005128 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005129 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005130 // Check the name of the function. Unless it's a dictionary function
5131 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005132 if (name != NULL)
5133 arg = name;
5134 else
5135 arg = fudi.fd_newkey;
5136 if (arg != NULL && (fudi.fd_di == NULL
5137 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
5138 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
5139 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00005140 char_u *name_base = arg;
5141 int i;
5142
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005143 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005144 {
5145 name_base = vim_strchr(arg, '_');
5146 if (name_base == NULL)
5147 name_base = arg + 3;
5148 else
5149 ++name_base;
5150 }
5151 for (i = 0; name_base[i] != NUL && (i == 0
5152 ? eval_isnamec1(name_base[i])
5153 : eval_isnamec(name_base[i])); ++i)
5154 ;
5155 if (name_base[i] != NUL)
zeertzjq6a04bf52024-03-16 09:39:06 +01005156 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005157 emsg_funcname(e_invalid_argument_str, arg);
zeertzjq6a04bf52024-03-16 09:39:06 +01005158 goto ret_free;
5159 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00005160
5161 // In Vim9 script a function cannot have the same name as a
5162 // variable.
5163 if (vim9script && *arg == K_SPECIAL
John Marriottb32800f2025-02-01 15:25:34 +01005164 && eval_variable(name_base, i, 0, NULL,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00005165 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00005166 + EVAL_VAR_NO_FUNC) == OK)
5167 {
5168 semsg(_(e_redefining_script_item_str), name_base);
5169 goto ret_free;
5170 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005171 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005172 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005173 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005174 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005175 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00005176 goto ret_free;
5177 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005178 }
5179
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005180 // This may get more lines and make the pointers into the first line
5181 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01005182 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005183 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00005184 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02005185 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005186 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00005187 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005188 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01005189 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005190
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005191 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005192 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005194 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005195 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005196 if (*p != ':')
5197 {
5198 semsg(_(e_no_white_space_allowed_before_colon_str), p);
5199 p = skipwhite(p);
5200 }
5201 else if (!IS_WHITE_OR_NUL(p[1]))
5202 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005203 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02005204 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005205 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005206 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005207 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01005208 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005209 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005210 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005211 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005212 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005213 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02005214 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005215 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005216 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02005217 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005218 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005219 else
5220 // find extra arguments "range", "dict", "abort" and "closure"
5221 for (;;)
5222 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01005223 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005224 p = skipwhite(p);
5225 if (STRNCMP(p, "range", 5) == 0)
5226 {
5227 flags |= FC_RANGE;
5228 p += 5;
5229 }
5230 else if (STRNCMP(p, "dict", 4) == 0)
5231 {
5232 flags |= FC_DICT;
5233 p += 4;
5234 }
5235 else if (STRNCMP(p, "abort", 5) == 0)
5236 {
5237 flags |= FC_ABORT;
5238 p += 5;
5239 }
5240 else if (STRNCMP(p, "closure", 7) == 0)
5241 {
5242 flags |= FC_CLOSURE;
5243 p += 7;
5244 if (current_funccal == NULL)
5245 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005246 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005247 name == NULL ? (char_u *)"" : name);
5248 goto erret;
5249 }
5250 }
5251 else
5252 break;
5253 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005254
Bram Moolenaare38eab22019-12-05 21:50:01 +01005255 // When there is a line break use what follows for the function body.
5256 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005257 if (*p == '\n')
5258 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005259 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005260 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5261 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005262 && !(VIM_ISWHITE(*whitep) && *p == '#'
5263 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005264 && !eap->skip
5265 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005266 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005267
5268 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005269 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5270 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005271 */
5272 if (KeyTyped)
5273 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005274 // Check if the function already exists, don't let the user type the
5275 // whole function before telling him it doesn't work! For a script we
5276 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005277 if (!eap->skip && !eap->forceit)
5278 {
5279 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005280 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005281 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005282 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005283 }
5284
5285 if (!eap->skip && did_emsg)
5286 goto erret;
5287
Bram Moolenaare38eab22019-12-05 21:50:01 +01005288 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005289 cmdline_row = msg_row;
5290 }
5291
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005292 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005293 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005294
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005295 // Do not define the function when getting the body fails and when
5296 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005297 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005298 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005299 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5300 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005301 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005302 goto erret;
5303
5304 /*
5305 * If there are no errors, add the function
5306 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005307 if (fudi.fd_dict != NULL)
5308 {
John Marriottb32800f2025-02-01 15:25:34 +01005309 char numbuf[NUMBUFLEN];
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005310
5311 fp = NULL;
5312 if (fudi.fd_newkey == NULL && !eap->forceit)
5313 {
5314 emsg(_(e_dictionary_entry_already_exists));
5315 goto erret;
5316 }
5317 if (fudi.fd_di == NULL)
5318 {
5319 // Can't add a function to a locked dictionary
5320 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5321 goto erret;
5322 }
5323 // Can't change an existing function if it is locked
5324 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5325 goto erret;
5326
5327 // Give the function a sequential number. Can only be used with a
5328 // Funcref!
5329 vim_free(name);
John Marriottb32800f2025-02-01 15:25:34 +01005330 namelen = vim_snprintf(numbuf, sizeof(numbuf), "%d", ++func_nr);
5331 name = vim_strnsave((char_u *)numbuf, namelen);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005332 if (name == NULL)
5333 goto erret;
5334 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005335 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005336 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005337 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005338 char_u *find_name = name;
5339 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005340 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005341
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005342 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005343 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005344 var_conflict = TRUE;
5345
5346 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5347 {
5348 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5349
5350 if (si->sn_autoload_prefix != NULL)
5351 {
5352 if (is_export)
5353 {
5354 find_name = name + STRLEN(si->sn_autoload_prefix);
5355 v = find_var(find_name, &ht, TRUE);
5356 if (v != NULL)
5357 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005358 // Only check if the function already exists in the script,
5359 // global functions can be shadowed.
5360 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005361 }
5362 else
5363 {
5364 char_u *prefixed = may_prefix_autoload(name);
5365
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005366 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005367 {
5368 v = find_var(prefixed, &ht, TRUE);
5369 if (v != NULL)
5370 var_conflict = TRUE;
5371 vim_free(prefixed);
5372 }
5373 }
5374 }
5375 }
5376 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005377 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005378 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005379 goto erret;
5380 }
5381
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005382 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005383 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005384 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005385 char_u *uname = untrans_function_name(name);
5386
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005387 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005388 }
5389
5390 if (fp != NULL || import != NULL)
5391 {
5392 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005393
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005394 // Function can be replaced with "function!" and when sourcing the
5395 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005396 // A name that is used by an import can not be overruled.
5397 if (import != NULL
5398 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005399 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005400 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005401 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005402 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005403 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005404 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005405 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005406 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
zeertzjq04d2a3f2025-02-02 19:03:17 +01005407 goto errret_keep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005408 }
5409 if (fp->uf_calls > 0)
5410 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005411 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005412 e_cannot_redefine_function_str_it_is_in_use, name);
zeertzjq04d2a3f2025-02-02 19:03:17 +01005413 goto errret_keep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005414 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005415 if (fp->uf_refcount > 1)
5416 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005417 // This function is referenced somewhere, don't redefine it but
5418 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005419 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005420 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005421 fp = NULL;
5422 overwrite = TRUE;
5423 }
5424 else
5425 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005426 char_u *exp_name = fp->uf_name_exp;
5427
5428 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005429 VIM_CLEAR(name);
John Marriottb32800f2025-02-01 15:25:34 +01005430 namelen = 0;
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005431 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005432 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005433 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005434 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005435#ifdef FEAT_PROFILE
5436 fp->uf_profiling = FALSE;
5437 fp->uf_prof_initialized = FALSE;
5438#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005439 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005440 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005441 }
5442 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005443
5444 if (fp == NULL)
5445 {
5446 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5447 {
5448 int slen, plen;
5449 char_u *scriptname;
5450
Bram Moolenaare38eab22019-12-05 21:50:01 +01005451 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005452 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005453 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005454 {
5455 scriptname = autoload_name(name);
5456 if (scriptname != NULL)
5457 {
5458 p = vim_strchr(scriptname, '/');
5459 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005460 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005461 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005462 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005463 j = OK;
5464 vim_free(scriptname);
5465 }
5466 }
5467 if (j == FAIL)
5468 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005469 linenr_T save_lnum = SOURCING_LNUM;
5470
5471 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005472 semsg(_(e_function_name_does_not_match_script_file_name_str),
5473 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005474 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005475 goto erret;
5476 }
5477 }
5478
John Marriottb32800f2025-02-01 15:25:34 +01005479 if (namelen == 0)
5480 namelen = STRLEN(name);
5481 fp = alloc_ufunc(name, namelen);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005482 if (fp == NULL)
5483 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005484 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005485
5486 if (fudi.fd_dict != NULL)
5487 {
5488 if (fudi.fd_di == NULL)
5489 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005490 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005491 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5492 if (fudi.fd_di == NULL)
5493 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005494 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005495 goto erret;
5496 }
5497 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5498 {
5499 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005500 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005501 goto erret;
5502 }
5503 }
5504 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005505 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005506 clear_tv(&fudi.fd_di->di_tv);
5507 fudi.fd_di->di_tv.v_type = VAR_FUNC;
John Marriottb32800f2025-02-01 15:25:34 +01005508 fudi.fd_di->di_tv.vval.v_string = vim_strnsave(name, namelen);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005509
Bram Moolenaare38eab22019-12-05 21:50:01 +01005510 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005511 flags |= FC_DICT;
5512 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005513 }
5514 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005515 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005516 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005517 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005518
5519 if (eap->cmdidx == CMD_def)
5520 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005521 int lnum_save = SOURCING_LNUM;
5522 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005523
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005524 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005525
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005526 // error messages are for the first function line
5527 SOURCING_LNUM = sourcing_lnum_top;
5528
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005529 // The function may use script variables from the context.
5530 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005531
Yegappan Lakshmanand79ea472025-01-20 21:09:13 +01005532 // The argument types and the return type may use an imported type.
5533 // In that case, the imported file will be sourced. To avoid treating
5534 // everything in the imported file as exported, temporarily reset
5535 // is_export.
5536 int save_is_export = is_export;
5537 is_export = FALSE;
5538
h-eastb895b0f2023-09-24 15:46:31 +02005539 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5540 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005541 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005542 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005543 free_fp = fp_allocated;
Yegappan Lakshmanand79ea472025-01-20 21:09:13 +01005544 is_export = save_is_export;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005545 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005546 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005547 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005548
5549 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005550 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005551 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005552 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005553 free_fp = fp_allocated;
Yegappan Lakshmanand79ea472025-01-20 21:09:13 +01005554 is_export = save_is_export;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005555 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005556 }
Yegappan Lakshmanand79ea472025-01-20 21:09:13 +01005557 is_export = save_is_export;
Bram Moolenaar09689a02020-05-09 22:50:08 +02005558 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005559 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005560 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005561 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005562
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005563 if (fp_allocated)
5564 {
5565 // insert the new function in the function list
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005566 if (overwrite)
5567 {
5568 hi = hash_find(&func_hashtab, name);
5569 hi->hi_key = UF2HIKEY(fp);
5570 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005571 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005572 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005573 {
5574 free_fp = TRUE;
5575 goto erret;
5576 }
5577 fp->uf_refcount = 1;
5578 }
5579
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005580 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005581 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005582 if ((flags & FC_CLOSURE) != 0)
5583 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005584 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005585 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005586 }
5587 else
5588 fp->uf_scoped = NULL;
5589
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005590#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005591 if (prof_def_func())
5592 func_do_profile(fp);
5593#endif
5594 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005595 if (sandbox)
5596 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005597 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005598 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005599 fp->uf_flags = flags;
5600 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005601 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005602 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005603 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005604 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005605 if (is_export)
5606 {
5607 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005608 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005609 is_export = FALSE;
5610 }
5611
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005612 if (eap->cmdidx == CMD_def)
5613 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005614 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5615 // :func does not use Vim9 script syntax, even in a Vim9 script file
5616 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005617
Yegappan Lakshmanan5715a722024-05-03 18:24:07 +02005618 // If test_override('defcompile' 1) is used, then compile the function now
5619 if (eap->cmdidx == CMD_def && override_defcompile)
5620 defcompile_function(fp, NULL);
5621
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005622 goto ret_free;
5623
5624erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005625 if (fp != NULL)
5626 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005627 // these were set to "newargs" and "default_args", which are cleared
5628 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005629 ga_init(&fp->uf_args);
5630 ga_init(&fp->uf_def_args);
5631 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005632errret_2:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005633 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005634 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005635 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005636 VIM_CLEAR(fp->uf_va_name);
John Marriottb32800f2025-02-01 15:25:34 +01005637 VIM_CLEAR(fp->uf_name_exp);
Christian Brabandt0f287912023-12-11 17:53:25 +01005638 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005639 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005640 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005641 VIM_CLEAR(fp);
zeertzjq04d2a3f2025-02-02 19:03:17 +01005642errret_keep:
5643 ga_clear_strings(&newargs);
5644 ga_clear_strings(&default_args);
5645 ga_clear_strings(&newlines);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005646ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005647 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005648 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005649 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005650 if (name != name_arg)
5651 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005652 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005653 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005654
5655 return fp;
5656}
5657
5658/*
5659 * ":function"
5660 */
5661 void
5662ex_function(exarg_T *eap)
5663{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005664 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005665
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005666 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005667 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005668 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005669}
5670
LemonBoy48b7d052024-07-09 18:24:59 +02005671 int
5672get_func_arity(char_u *name, int *required, int *optional, int *varargs)
5673{
5674 ufunc_T *ufunc = NULL;
5675 int argcount = 0;
5676 int min_argcount = 0;
5677 int idx;
5678
5679 idx = find_internal_func(name);
5680 if (idx >= 0)
5681 {
5682 internal_func_get_argcount(idx, &argcount, &min_argcount);
5683 *varargs = FALSE;
5684 }
5685 else
5686 {
5687 char_u fname_buf[FLEN_FIXED + 1];
5688 char_u *tofree = NULL;
5689 funcerror_T error = FCERR_NONE;
5690 char_u *fname;
5691
5692 // May need to translate <SNR>123_ to K_SNR.
5693 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
5694 if (error == FCERR_NONE)
5695 ufunc = find_func(fname, FALSE);
5696 vim_free(tofree);
5697
5698 if (ufunc == NULL)
5699 return FAIL;
5700
5701 argcount = ufunc->uf_args.ga_len;
5702 min_argcount = ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len;
5703 *varargs = has_varargs(ufunc);
5704 }
5705
5706 *required = min_argcount;
5707 *optional = argcount - min_argcount;
5708
5709 return OK;
5710}
5711
Bram Moolenaar822ba242020-05-24 23:00:18 +02005712/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005713 * Find a function by name, including "<lambda>123".
5714 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005715 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005716 * Return NULL if not found.
5717 */
5718 ufunc_T *
5719find_func_by_name(char_u *name, compiletype_T *compile_type)
5720{
5721 char_u *arg = name;
5722 char_u *fname;
5723 ufunc_T *ufunc;
5724 int is_global = FALSE;
5725
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005726 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5727 {
5728 *compile_type = CT_PROFILE;
5729 arg = skipwhite(arg + 7);
5730 }
5731 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5732 {
5733 *compile_type = CT_DEBUG;
5734 arg = skipwhite(arg + 5);
5735 }
5736
5737 if (STRNCMP(arg, "<lambda>", 8) == 0)
5738 {
5739 arg += 8;
5740 (void)getdigits(&arg);
5741 fname = vim_strnsave(name, arg - name);
5742 }
5743 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005744 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005745 // First try finding a method in a class, trans_function_name() will
5746 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005747 ufunc = find_class_func(&arg);
5748 if (ufunc != NULL)
5749 return ufunc;
5750
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005751 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005752 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005753 NULL, NULL, NULL, &ufunc);
5754 if (ufunc != NULL)
5755 {
5756 vim_free(fname);
5757 return ufunc;
5758 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005759 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005760 if (fname == NULL)
5761 {
5762 semsg(_(e_invalid_argument_str), name);
5763 return NULL;
5764 }
5765 if (!ends_excmd2(name, arg))
5766 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005767 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005768 emsg(ex_errmsg(e_trailing_characters_str, arg));
5769 return NULL;
5770 }
5771
5772 ufunc = find_func(fname, is_global);
5773 if (ufunc == NULL)
5774 {
5775 char_u *p = untrans_function_name(fname);
5776
5777 if (p != NULL)
5778 // Try again without making it script-local.
5779 ufunc = find_func(p, FALSE);
5780 }
5781 vim_free(fname);
5782 if (ufunc == NULL)
5783 semsg(_(e_cannot_find_function_str), name);
5784 return ufunc;
5785}
5786
5787/*
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005788 * Compile the :def function "ufunc". If "cl" is not NULL, then compile the
5789 * class or object method "ufunc" in "cl".
5790 */
5791 void
5792defcompile_function(ufunc_T *ufunc, class_T *cl)
5793{
5794 compiletype_T compile_type = CT_NONE;
5795
5796 if (func_needs_compiling(ufunc, compile_type))
5797 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5798 else
5799 smsg(_("Function %s%s%s does not need compiling"),
5800 cl != NULL ? cl->class_name : (char_u *)"",
5801 cl != NULL ? (char_u *)"." : (char_u *)"",
5802 ufunc->uf_name);
5803}
5804
5805/*
5806 * Compile all the :def functions defined in the current script
5807 */
5808 static void
5809defcompile_funcs_in_script(void)
5810{
5811 long todo = (long)func_hashtab.ht_used;
5812 int changed = func_hashtab.ht_changed;
5813 hashitem_T *hi;
5814
5815 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5816 {
5817 if (!HASHITEM_EMPTY(hi))
5818 {
5819 --todo;
5820 ufunc_T *ufunc = HI2UF(hi);
5821 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5822 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5823 && (ufunc->uf_flags & FC_DEAD) == 0)
5824 {
5825 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5826
5827 if (func_hashtab.ht_changed != changed)
5828 {
5829 // a function has been added or removed, need to start
5830 // over
5831 todo = (long)func_hashtab.ht_used;
5832 changed = func_hashtab.ht_changed;
5833 hi = func_hashtab.ht_array;
5834 --hi;
5835 }
5836 }
5837 }
5838 }
5839}
5840
5841/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005842 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005843 * be compiled or the one specified by the argument.
5844 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005845 */
5846 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005847ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005848{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005849 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005850 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005851 typval_T tv;
5852
5853 if (is_class_name(eap->arg, &tv))
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005854 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005855 class_T *cl = tv.vval.v_class;
5856
5857 if (cl != NULL)
5858 defcompile_class(cl);
5859 }
5860 else
5861 {
5862 compiletype_T compile_type = CT_NONE;
5863 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
5864 if (ufunc != NULL)
5865 defcompile_function(ufunc, NULL);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005866 }
5867 }
5868 else
5869 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005870 defcompile_funcs_in_script();
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005871
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005872 // compile all the class defined in the current script
5873 defcompile_classes_in_script();
Bram Moolenaar822ba242020-05-24 23:00:18 +02005874 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005875}
5876
5877/*
5878 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5879 * Return 2 if "p" starts with "s:".
5880 * Return 0 otherwise.
5881 */
5882 int
5883eval_fname_script(char_u *p)
5884{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005885 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5886 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005887 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5888 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5889 return 5;
5890 if (p[0] == 's' && p[1] == ':')
5891 return 2;
5892 return 0;
5893}
5894
5895 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005896translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005897{
5898 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005899 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005900 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005901}
5902
5903/*
5904 * Return TRUE when "ufunc" has old-style "..." varargs
5905 * or named varargs "...name: type".
5906 */
5907 int
5908has_varargs(ufunc_T *ufunc)
5909{
5910 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005911}
5912
5913/*
5914 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005915 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005916 */
5917 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005918function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005919{
5920 char_u *nm = name;
5921 char_u *p;
5922 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005923 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005924 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005925
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005926 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5927 if (no_deref)
5928 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005929 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005930 nm = skipwhite(nm);
5931
Bram Moolenaare38eab22019-12-05 21:50:01 +01005932 // Only accept "funcname", "funcname ", "funcname (..." and
5933 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005934 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005935 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005936 vim_free(p);
5937 return n;
5938}
5939
Bram Moolenaar113e1072019-01-20 15:30:40 +01005940#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005941 char_u *
5942get_expanded_name(char_u *name, int check)
5943{
5944 char_u *nm = name;
5945 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005946 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005947
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005948 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005949
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005950 if (p != NULL && *nm == NUL
5951 && (!check || translated_function_exists(p, is_global)))
5952 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005953
5954 vim_free(p);
5955 return NULL;
5956}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005957#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005958
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005959/*
5960 * Function given to ExpandGeneric() to obtain the list of user defined
5961 * function names.
5962 */
5963 char_u *
5964get_user_func_name(expand_T *xp, int idx)
5965{
5966 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005967 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005968 static hashitem_T *hi;
5969 ufunc_T *fp;
5970
5971 if (idx == 0)
5972 {
5973 done = 0;
5974 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005975 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005976 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005977 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005978 {
John Marriottb32800f2025-02-01 15:25:34 +01005979 int len;
5980
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005981 if (done++ > 0)
5982 ++hi;
5983 while (HASHITEM_EMPTY(hi))
5984 ++hi;
5985 fp = HI2UF(hi);
5986
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005987 // don't show dead, dict and lambda functions
5988 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005989 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005990 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005991
John Marriottb32800f2025-02-01 15:25:34 +01005992 if (fp->uf_namelen + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005993 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005994
John Marriottb32800f2025-02-01 15:25:34 +01005995 len = cat_func_name(IObuff, IOSIZE, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005996 if (xp->xp_context != EXPAND_USER_FUNC
5997 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005998 {
John Marriottb32800f2025-02-01 15:25:34 +01005999 STRCPY(IObuff + len, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006000 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
John Marriottb32800f2025-02-01 15:25:34 +01006001 {
6002 ++len;
6003 STRCPY(IObuff + len, ")");
6004 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006005 }
6006 return IObuff;
6007 }
6008 return NULL;
6009}
6010
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006011/*
Bram Moolenaar83677162023-01-08 19:54:10 +00006012 * Make a copy of a function.
6013 * Intended to be used for a function defined on a base class that has a copy
6014 * on the child class.
6015 * The copy has uf_refcount set to one.
6016 * Returns NULL when out of memory.
6017 */
6018 ufunc_T *
6019copy_function(ufunc_T *fp)
6020{
John Marriottb32800f2025-02-01 15:25:34 +01006021 ufunc_T *ufunc = alloc_ufunc(fp->uf_name, fp->uf_namelen);
Bram Moolenaar83677162023-01-08 19:54:10 +00006022 if (ufunc == NULL)
6023 return NULL;
6024
6025 // Most things can just be copied.
6026 *ufunc = *fp;
6027
6028 ufunc->uf_def_status = UF_TO_BE_COMPILED;
6029 ufunc->uf_dfunc_idx = 0;
6030 ufunc->uf_class = NULL;
6031
6032 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
6033 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
6034
6035 if (ufunc->uf_arg_types != NULL)
6036 {
6037 // "uf_arg_types" is an allocated array, make a copy.
6038 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
6039 if (at != NULL)
6040 {
6041 mch_memmove(at, ufunc->uf_arg_types,
6042 sizeof(type_T *) * ufunc->uf_args.ga_len);
6043 ufunc->uf_arg_types = at;
6044 }
6045 }
6046
6047 // TODO: how about the types themselves? they can be freed when the
6048 // original function is freed:
6049 // type_T **uf_arg_types;
6050 // type_T *uf_ret_type;
6051
Ernie Rael114ec812023-06-04 18:11:35 +01006052 // make uf_type_list empty
6053 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00006054
6055 // TODO: partial_T *uf_partial;
6056
6057 if (ufunc->uf_va_name != NULL)
6058 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
6059
6060 // TODO:
6061 // type_T *uf_va_type;
6062 // type_T *uf_func_type;
6063
6064 ufunc->uf_block_depth = 0;
6065 ufunc->uf_block_ids = NULL;
6066
6067 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
6068
6069 ufunc->uf_refcount = 1;
Bram Moolenaar83677162023-01-08 19:54:10 +00006070
6071 return ufunc;
6072}
6073
6074/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006075 * ":delfunction {name}"
6076 */
6077 void
6078ex_delfunction(exarg_T *eap)
6079{
6080 ufunc_T *fp = NULL;
6081 char_u *p;
6082 char_u *name;
6083 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006084 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006085
6086 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00006087 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
6088 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006089 vim_free(fudi.fd_newkey);
6090 if (name == NULL)
6091 {
6092 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00006093 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006094 return;
6095 }
6096 if (!ends_excmd(*skipwhite(p)))
6097 {
6098 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00006099 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006100 return;
6101 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006102 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006103 if (eap->nextcmd != NULL)
6104 *p = NUL;
6105
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006106 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02006107 {
6108 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006109 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02006110 vim_free(name);
6111 return;
6112 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006113 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006114 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006115 vim_free(name);
6116
6117 if (!eap->skip)
6118 {
6119 if (fp == NULL)
6120 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02006121 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00006122 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006123 return;
6124 }
6125 if (fp->uf_calls > 0)
6126 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006127 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006128 return;
6129 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006130 if (fp->uf_flags & FC_VIM9)
6131 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006132 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006133 return;
6134 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006135
6136 if (fudi.fd_dict != NULL)
6137 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006138 // Delete the dict item that refers to the function, it will
6139 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00006140 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006141 }
6142 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006143 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006144 // A normal function (not a numbered function or lambda) has a
6145 // refcount of 1 for the entry in the hashtable. When deleting
6146 // it and the refcount is more than one, it should be kept.
6147 // A numbered function and lambda should be kept if the refcount is
6148 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006149 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006150 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006151 // Function is still referenced somewhere. Don't free it but
6152 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006153 if (func_remove(fp))
6154 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006155 }
6156 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01006157 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006158 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006159 }
6160}
6161
6162/*
6163 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006164 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006165 */
6166 void
6167func_unref(char_u *name)
6168{
Bram Moolenaar97baee82016-07-26 20:46:08 +02006169 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006170
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006171 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006172 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006173 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006174 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006175 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006176#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006177 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006178#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01006179 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006180 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006181 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006182}
6183
6184/*
6185 * Unreference a Function: decrement the reference count and free it when it
6186 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006187 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006188 */
6189 void
6190func_ptr_unref(ufunc_T *fp)
6191{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006192 if (fp != NULL && (--fp->uf_refcount <= 0
6193 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
6194 && fp->uf_partial->pt_refcount <= 1
6195 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02006196 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006197 // Only delete it when it's not being used. Otherwise it's done
6198 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02006199 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01006200 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006201 }
6202}
6203
6204/*
6205 * Count a reference to a Function.
6206 */
6207 void
6208func_ref(char_u *name)
6209{
6210 ufunc_T *fp;
6211
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006212 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006213 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006214 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006215 if (fp != NULL)
6216 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006217 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01006218 // Only give an error for a numbered function.
6219 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01006220 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006221}
6222
6223/*
6224 * Count a reference to a Function.
6225 */
6226 void
6227func_ptr_ref(ufunc_T *fp)
6228{
6229 if (fp != NULL)
6230 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006231}
6232
6233/*
6234 * Return TRUE if items in "fc" do not have "copyID". That means they are not
6235 * referenced from anywhere that is in use.
6236 */
6237 static int
6238can_free_funccal(funccall_T *fc, int copyID)
6239{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006240 return (fc->fc_l_varlist.lv_copyID != copyID
6241 && fc->fc_l_vars.dv_copyID != copyID
6242 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006243 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006244}
6245
6246/*
6247 * ":return [expr]"
6248 */
6249 void
6250ex_return(exarg_T *eap)
6251{
6252 char_u *arg = eap->arg;
6253 typval_T rettv;
6254 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006255 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006256
6257 if (current_funccal == NULL)
6258 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006259 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006260 return;
6261 }
6262
Bram Moolenaar844fb642021-10-23 13:32:30 +01006263 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006264 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
6265
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006266 if (eap->skip)
6267 ++emsg_skip;
6268
6269 eap->nextcmd = NULL;
6270 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006271 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006272 {
6273 if (!eap->skip)
6274 returning = do_return(eap, FALSE, TRUE, &rettv);
6275 else
6276 clear_tv(&rettv);
6277 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01006278 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006279 else if (!eap->skip)
6280 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006281 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01006282 update_force_abort();
6283
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006284 /*
6285 * Return unless the expression evaluation has been cancelled due to an
6286 * aborting error, an interrupt, or an exception.
6287 */
6288 if (!aborting())
6289 returning = do_return(eap, FALSE, TRUE, NULL);
6290 }
6291
Bram Moolenaare38eab22019-12-05 21:50:01 +01006292 // When skipping or the return gets pending, advance to the next command
6293 // in this line (!returning). Otherwise, ignore the rest of the line.
6294 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006295 if (returning)
6296 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006297 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02006298 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006299
6300 if (eap->skip)
6301 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02006302 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006303}
6304
zeertzjq5299c092023-04-12 20:48:16 +01006305/*
6306 * Lower level implementation of "call". Only called when not skipping.
6307 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006308 static int
6309ex_call_inner(
6310 exarg_T *eap,
6311 char_u *name,
6312 char_u **arg,
6313 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006314 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006315 evalarg_T *evalarg)
6316{
6317 linenr_T lnum;
6318 int doesrange;
6319 typval_T rettv;
6320 int failed = FALSE;
6321
zeertzjq5299c092023-04-12 20:48:16 +01006322 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006323 for ( ; lnum <= eap->line2; ++lnum)
6324 {
6325 funcexe_T funcexe;
6326
zeertzjq5299c092023-04-12 20:48:16 +01006327 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006328 {
6329 if (lnum > curbuf->b_ml.ml_line_count)
6330 {
6331 // If the function deleted lines or switched to another buffer
6332 // the line number may become invalid.
6333 emsg(_(e_invalid_range));
6334 break;
6335 }
6336 curwin->w_cursor.lnum = lnum;
6337 curwin->w_cursor.col = 0;
6338 curwin->w_cursor.coladd = 0;
6339 }
6340 *arg = startarg;
6341
6342 funcexe = *funcexe_init;
6343 funcexe.fe_doesrange = &doesrange;
6344 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6345 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6346 {
6347 failed = TRUE;
6348 break;
6349 }
6350 if (has_watchexpr())
6351 dbg_check_breakpoint(eap);
6352
6353 // Handle a function returning a Funcref, Dictionary or List.
6354 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006355 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006356 {
6357 failed = TRUE;
6358 break;
6359 }
6360
6361 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006362 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006363 break;
6364
6365 // Stop when immediately aborting on error, or when an interrupt
6366 // occurred or an exception was thrown but not caught.
6367 // get_func_tv() returned OK, so that the check for trailing
6368 // characters below is executed.
6369 if (aborting())
6370 break;
6371 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006372 return failed;
6373}
6374
6375/*
6376 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6377 * Returns FAIL or OK.
6378 */
6379 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006380ex_defer_inner(
6381 char_u *name,
6382 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006383 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006384 partial_T *partial,
6385 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006386{
6387 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006388 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006389 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006390 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006391
6392 if (current_funccal == NULL)
6393 {
6394 semsg(_(e_str_not_inside_function), "defer");
6395 return FAIL;
6396 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006397 if (partial != NULL)
6398 {
6399 if (partial->pt_dict != NULL)
6400 {
6401 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6402 return FAIL;
6403 }
6404 if (partial->pt_argc > 0)
6405 {
6406 int i;
6407
6408 partial_argc = partial->pt_argc;
6409 for (i = 0; i < partial_argc; ++i)
6410 copy_tv(&partial->pt_argv[i], &argvars[i]);
6411 }
6412 }
Ernie Raelb077b582023-12-14 20:11:44 +01006413 int is_builtin = builtin_function(name, -1);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006414 r = get_func_arguments(arg, evalarg, FALSE,
Ernie Raelb077b582023-12-14 20:11:44 +01006415 argvars + partial_argc, &argcount, is_builtin);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006416 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006417
6418 if (r == OK)
6419 {
6420 if (type != NULL)
6421 {
6422 // Check that the arguments are OK for the types of the funcref.
6423 r = check_argument_types(type, argvars, argcount, NULL, name);
6424 }
Ernie Raelb077b582023-12-14 20:11:44 +01006425 else if (is_builtin)
Bram Moolenaar16900322022-09-08 19:51:45 +01006426 {
6427 int idx = find_internal_func(name);
6428
6429 if (idx < 0)
6430 {
6431 emsg_funcname(e_unknown_function_str, name);
6432 r = FAIL;
6433 }
6434 else if (check_internal_func(idx, argcount) == -1)
6435 r = FAIL;
6436 }
6437 else
6438 {
6439 ufunc_T *ufunc = find_func(name, FALSE);
6440
6441 // we tolerate an unknown function here, it might be defined later
6442 if (ufunc != NULL)
6443 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006444 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006445 if (error != FCERR_UNKNOWN)
6446 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006447 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006448 r = FAIL;
6449 }
6450 }
6451 }
6452 }
6453
Bram Moolenaar86d87252022-09-05 21:21:25 +01006454 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006455 {
6456 while (--argcount >= 0)
6457 clear_tv(&argvars[argcount]);
6458 return FAIL;
6459 }
6460 return add_defer(name, argcount, argvars);
6461}
6462
6463/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006464 * Return TRUE if currently inside a function call.
6465 * Give an error message and return FALSE when not.
6466 */
6467 int
6468can_add_defer(void)
6469{
6470 if (!in_def_function() && get_current_funccal() == NULL)
6471 {
6472 semsg(_(e_str_not_inside_function), "defer");
6473 return FALSE;
6474 }
6475 return TRUE;
6476}
6477
6478/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006479 * Add a deferred call for "name" with arguments "argvars[argcount]".
6480 * Consumes "argvars[]".
6481 * Caller must check that in_def_function() returns TRUE or current_funccal is
6482 * not NULL.
6483 * Returns OK or FAIL.
6484 */
6485 int
6486add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6487{
6488 char_u *saved_name = vim_strsave(name);
6489 int argcount = argcount_arg;
6490 defer_T *dr;
6491 int ret = FAIL;
6492
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006493 if (saved_name == NULL)
6494 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006495 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006496 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006497 if (add_defer_function(saved_name, argcount, argvars) == OK)
6498 argcount = 0;
6499 }
6500 else
6501 {
6502 if (current_funccal->fc_defer.ga_itemsize == 0)
6503 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6504 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6505 goto theend;
6506 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6507 + current_funccal->fc_defer.ga_len++;
6508 dr->dr_name = saved_name;
6509 dr->dr_argcount = argcount;
6510 while (argcount > 0)
6511 {
6512 --argcount;
6513 dr->dr_argvars[argcount] = argvars[argcount];
6514 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006515 }
6516 ret = OK;
6517
6518theend:
6519 while (--argcount >= 0)
6520 clear_tv(&argvars[argcount]);
6521 return ret;
6522}
6523
6524/*
6525 * Invoked after a functions has finished: invoke ":defer" functions.
6526 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006527 static void
6528handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006529{
6530 int idx;
6531
Bram Moolenaar58779852022-09-06 18:31:14 +01006532 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006533 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006534 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006535
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006536 if (dr->dr_name == NULL)
6537 // already being called, can happen if function does ":qa"
6538 continue;
6539
6540 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006541 CLEAR_FIELD(funcexe);
6542 funcexe.fe_evaluate = TRUE;
6543
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006544 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006545 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006546
6547 char_u *name = dr->dr_name;
6548 dr->dr_name = NULL;
6549
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006550 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006551 // first statement in the function will be executed (because of the
6552 // exception). So save and restore the try/catch/throw exception
6553 // state.
6554 exception_state_T estate;
6555 exception_state_save(&estate);
6556 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006557
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006558 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6559
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006560 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006561
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006562 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006563 vim_free(name);
6564 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006565 clear_tv(&dr->dr_argvars[i]);
6566 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006567 ga_clear(&funccal->fc_defer);
6568}
6569
zeertzjq960cf912023-04-18 21:52:54 +01006570 static void
6571invoke_funccall_defer(funccall_T *fc)
6572{
6573 if (fc->fc_ectx != NULL)
6574 {
6575 // :def function
6576 unwind_def_callstack(fc->fc_ectx);
6577 may_invoke_defer_funcs(fc->fc_ectx);
6578 }
6579 else
6580 {
6581 // legacy function
6582 handle_defer_one(fc);
6583 }
6584}
6585
Bram Moolenaar58779852022-09-06 18:31:14 +01006586/*
6587 * Called when exiting: call all defer functions.
6588 */
6589 void
6590invoke_all_defer(void)
6591{
zeertzjq1be4b812023-04-19 14:21:24 +01006592 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6593 invoke_funccall_defer(fc);
6594
zeertzjq960cf912023-04-18 21:52:54 +01006595 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6596 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6597 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006598}
6599
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006600/*
6601 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006602 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006603 */
6604 void
6605ex_call(exarg_T *eap)
6606{
6607 char_u *arg = eap->arg;
6608 char_u *startarg;
6609 char_u *name;
6610 char_u *tofree;
6611 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006612 int failed = FALSE;
6613 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006614 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006615 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006616 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006617 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006618 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006619 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006620
Bram Moolenaare6b53242020-07-01 17:28:33 +02006621 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006622 if (eap->skip)
6623 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006624 typval_T rettv;
6625
Bram Moolenaare38eab22019-12-05 21:50:01 +01006626 // trans_function_name() doesn't work well when skipping, use eval0()
6627 // instead to skip to any following command, e.g. for:
6628 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006629 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006630 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006631 clear_tv(&rettv);
6632 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006633 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006634 return;
6635 }
6636
zeertzjq5299c092023-04-12 20:48:16 +01006637 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006638 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006639 if (fudi.fd_newkey != NULL)
6640 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006641 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006642 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006643 vim_free(fudi.fd_newkey);
6644 }
6645 if (tofree == NULL)
6646 return;
6647
Bram Moolenaare38eab22019-12-05 21:50:01 +01006648 // Increase refcount on dictionary, it could get deleted when evaluating
6649 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006650 if (fudi.fd_dict != NULL)
6651 ++fudi.fd_dict->dv_refcount;
6652
Bram Moolenaare38eab22019-12-05 21:50:01 +01006653 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6654 // contents. For VAR_PARTIAL get its partial, unless we already have one
6655 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006656 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006657 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006658 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006659 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006660
Bram Moolenaare38eab22019-12-05 21:50:01 +01006661 // Skip white space to allow ":call func ()". Not good, but required for
6662 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006663 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006664 if (*startarg != '(')
6665 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006666 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006667 goto end;
6668 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006669 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006670 {
6671 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6672 goto end;
6673 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006674
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006675 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006676 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006677 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006678 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006679 }
6680 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006681 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006682 funcexe_T funcexe;
6683
Bram Moolenaara80faa82020-04-12 19:37:17 +02006684 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006685 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006686 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006687 funcexe.fe_partial = partial;
6688 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006689 funcexe.fe_firstline = eap->line1;
6690 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006691 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006692 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006693 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006694 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006695
Bram Moolenaar1d341892021-09-18 15:25:52 +02006696 // When inside :try we need to check for following "| catch" or "| endtry".
6697 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006698 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006699 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006700 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006701 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006702 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006703 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006704 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006705 {
6706 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006707 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006708 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006709 }
6710 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006711 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006712 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006713 // Must be after using "arg", it may point into memory cleared here.
6714 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006715
6716end:
6717 dict_unref(fudi.fd_dict);
6718 vim_free(tofree);
6719}
6720
6721/*
6722 * Return from a function. Possibly makes the return pending. Also called
6723 * for a pending return at the ":endtry" or after returning from an extra
6724 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6725 * when called due to a ":return" command. "rettv" may point to a typval_T
6726 * with the return rettv. Returns TRUE when the return can be carried out,
6727 * FALSE when the return gets pending.
6728 */
6729 int
6730do_return(
6731 exarg_T *eap,
6732 int reanimate,
6733 int is_cmd,
6734 void *rettv)
6735{
6736 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006737 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006738
6739 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006740 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006741 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006742
6743 /*
6744 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6745 * not in its finally clause (which then is to be executed next) is found.
6746 * In this case, make the ":return" pending for execution at the ":endtry".
6747 * Otherwise, return normally.
6748 */
6749 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6750 if (idx >= 0)
6751 {
6752 cstack->cs_pending[idx] = CSTP_RETURN;
6753
6754 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006755 // A pending return again gets pending. "rettv" points to an
6756 // allocated variable with the rettv of the original ":return"'s
6757 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006758 cstack->cs_rettv[idx] = rettv;
6759 else
6760 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006761 // When undoing a return in order to make it pending, get the stored
6762 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006763 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006764 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006765
6766 if (rettv != NULL)
6767 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006768 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006769 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6770 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6771 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006772 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006773 }
6774 else
6775 cstack->cs_rettv[idx] = NULL;
6776
6777 if (reanimate)
6778 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006779 // The pending return value could be overwritten by a ":return"
6780 // without argument in a finally clause; reset the default
6781 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006782 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6783 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006784 }
6785 }
6786 report_make_pending(CSTP_RETURN, rettv);
6787 }
6788 else
6789 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006790 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006791
Bram Moolenaare38eab22019-12-05 21:50:01 +01006792 // If the return is carried out now, store the return value. For
6793 // a return immediately after reanimation, the value is already
6794 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006795 if (!reanimate && rettv != NULL)
6796 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006797 clear_tv(current_funccal->fc_rettv);
6798 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006799 if (!is_cmd)
6800 vim_free(rettv);
6801 }
6802 }
6803
6804 return idx < 0;
6805}
6806
6807/*
6808 * Free the variable with a pending return value.
6809 */
6810 void
6811discard_pending_return(void *rettv)
6812{
6813 free_tv((typval_T *)rettv);
6814}
6815
6816/*
6817 * Generate a return command for producing the value of "rettv". The result
6818 * is an allocated string. Used by report_pending() for verbose messages.
6819 */
6820 char_u *
6821get_return_cmd(void *rettv)
6822{
6823 char_u *s = NULL;
zeertzjq21012302025-02-02 08:55:57 +01006824 char_u *tofree = NULL;
6825 char_u numbuf[NUMBUFLEN];
John Marriottb32800f2025-02-01 15:25:34 +01006826 size_t slen = 0;
6827 size_t IObufflen;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006828
6829 if (rettv != NULL)
6830 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6831 if (s == NULL)
6832 s = (char_u *)"";
John Marriottb32800f2025-02-01 15:25:34 +01006833 else
6834 slen = STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006835
6836 STRCPY(IObuff, ":return ");
6837 STRNCPY(IObuff + 8, s, IOSIZE - 8);
John Marriottb32800f2025-02-01 15:25:34 +01006838 IObufflen = 8 + slen;
zeertzjq21012302025-02-02 08:55:57 +01006839 if (IObufflen >= IOSIZE)
John Marriottb32800f2025-02-01 15:25:34 +01006840 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006841 STRCPY(IObuff + IOSIZE - 4, "...");
zeertzjq21012302025-02-02 08:55:57 +01006842 IObufflen = IOSIZE - 1;
John Marriottb32800f2025-02-01 15:25:34 +01006843 }
zeertzjq21012302025-02-02 08:55:57 +01006844 vim_free(tofree);
John Marriottb32800f2025-02-01 15:25:34 +01006845 return vim_strnsave(IObuff, IObufflen);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006846}
6847
6848/*
6849 * Get next function line.
6850 * Called by do_cmdline() to get the next line.
6851 * Returns allocated string, or NULL for end of function.
6852 */
6853 char_u *
6854get_func_line(
6855 int c UNUSED,
6856 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006857 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006858 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006859{
6860 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006861 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006862 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006863 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006864
Bram Moolenaare38eab22019-12-05 21:50:01 +01006865 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006866 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006867 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006868 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006869 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006870 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006871 }
6872#ifdef FEAT_PROFILE
6873 if (do_profiling == PROF_YES)
6874 func_line_end(cookie);
6875#endif
6876
6877 gap = &fp->uf_lines;
6878 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006879 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006880 retval = NULL;
6881 else
6882 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006883 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006884 while (fcp->fc_linenr < gap->ga_len
6885 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6886 ++fcp->fc_linenr;
6887 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006888 retval = NULL;
6889 else
6890 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006891 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6892 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006893#ifdef FEAT_PROFILE
6894 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006895 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006896#endif
6897 }
6898 }
6899
Bram Moolenaare38eab22019-12-05 21:50:01 +01006900 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006901 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006902 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006903 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006904 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006905 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006906 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006907 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006908 }
6909
6910 return retval;
6911}
6912
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006913/*
6914 * Return TRUE if the currently active function should be ended, because a
6915 * return was encountered or an error occurred. Used inside a ":while".
6916 */
6917 int
6918func_has_ended(void *cookie)
6919{
6920 funccall_T *fcp = (funccall_T *)cookie;
6921
Bram Moolenaare38eab22019-12-05 21:50:01 +01006922 // Ignore the "abort" flag if the abortion behavior has been changed due to
6923 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006924 return (((fcp->fc_func->uf_flags & FC_ABORT)
6925 && did_emsg && !aborted_in_try())
6926 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006927}
6928
6929/*
6930 * return TRUE if cookie indicates a function which "abort"s on errors.
6931 */
6932 int
6933func_has_abort(
6934 void *cookie)
6935{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006936 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006937}
6938
6939
6940/*
6941 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6942 * Don't do this when "Func" is already a partial that was bound
6943 * explicitly (pt_auto is FALSE).
6944 * Changes "rettv" in-place.
6945 * Returns the updated "selfdict_in".
6946 */
6947 dict_T *
6948make_partial(dict_T *selfdict_in, typval_T *rettv)
6949{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006950 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006951 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006952 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006953 dict_T *selfdict = selfdict_in;
6954
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006955 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6956 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006957 fp = rettv->vval.v_partial->pt_func;
6958 else
6959 {
6960 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006961 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006962 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006963 if (fname == NULL)
6964 {
6965 // There is no point binding a dict to a NULL function, just create
6966 // a function reference.
6967 rettv->v_type = VAR_FUNC;
6968 rettv->vval.v_string = NULL;
6969 }
6970 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006971 {
6972 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006973 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006974
6975 // Translate "s:func" to the stored function name.
6976 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6977 fp = find_func(fname, FALSE);
6978 vim_free(tofree);
6979 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006980 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006981
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006982 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006983 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006984 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006985
6986 if (pt != NULL)
6987 {
6988 pt->pt_refcount = 1;
6989 pt->pt_dict = selfdict;
6990 pt->pt_auto = TRUE;
6991 selfdict = NULL;
6992 if (rettv->v_type == VAR_FUNC)
6993 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006994 // Just a function: Take over the function name and use
6995 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006996 pt->pt_name = rettv->vval.v_string;
6997 }
6998 else
6999 {
7000 partial_T *ret_pt = rettv->vval.v_partial;
7001 int i;
7002
Bram Moolenaare38eab22019-12-05 21:50:01 +01007003 // Partial: copy the function name, use selfdict and copy
7004 // args. Can't take over name or args, the partial might
7005 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007006 if (ret_pt->pt_name != NULL)
7007 {
7008 pt->pt_name = vim_strsave(ret_pt->pt_name);
7009 func_ref(pt->pt_name);
7010 }
7011 else
7012 {
7013 pt->pt_func = ret_pt->pt_func;
7014 func_ptr_ref(pt->pt_func);
7015 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007016 if (ret_pt->pt_argc > 0)
7017 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007018 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007019 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01007020 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007021 pt->pt_argc = 0;
7022 else
7023 {
7024 pt->pt_argc = ret_pt->pt_argc;
7025 for (i = 0; i < pt->pt_argc; i++)
7026 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
7027 }
7028 }
7029 partial_unref(ret_pt);
7030 }
7031 rettv->v_type = VAR_PARTIAL;
7032 rettv->vval.v_partial = pt;
7033 }
7034 }
7035 return selfdict;
7036}
7037
7038/*
7039 * Return the name of the executed function.
7040 */
7041 char_u *
7042func_name(void *cookie)
7043{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007044 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007045}
7046
7047/*
7048 * Return the address holding the next breakpoint line for a funccall cookie.
7049 */
7050 linenr_T *
7051func_breakpoint(void *cookie)
7052{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007053 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007054}
7055
7056/*
7057 * Return the address holding the debug tick for a funccall cookie.
7058 */
7059 int *
7060func_dbg_tick(void *cookie)
7061{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007062 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007063}
7064
7065/*
7066 * Return the nesting level for a funccall cookie.
7067 */
7068 int
7069func_level(void *cookie)
7070{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007071 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007072}
7073
7074/*
7075 * Return TRUE when a function was ended by a ":return" command.
7076 */
7077 int
7078current_func_returned(void)
7079{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007080 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007081}
7082
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007083 int
7084free_unref_funccal(int copyID, int testing)
7085{
7086 int did_free = FALSE;
7087 int did_free_funccal = FALSE;
7088 funccall_T *fc, **pfc;
7089
7090 for (pfc = &previous_funccal; *pfc != NULL; )
7091 {
7092 if (can_free_funccal(*pfc, copyID))
7093 {
7094 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007095 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01007096 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007097 did_free = TRUE;
7098 did_free_funccal = TRUE;
7099 }
7100 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01007101 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007102 }
7103 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01007104 // When a funccal was freed some more items might be garbage
7105 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007106 (void)garbage_collect(testing);
7107
7108 return did_free;
7109}
7110
7111/*
Bram Moolenaarba209902016-08-24 22:06:38 +02007112 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007113 */
7114 static funccall_T *
7115get_funccal(void)
7116{
7117 int i;
7118 funccall_T *funccal;
7119 funccall_T *temp_funccal;
7120
7121 funccal = current_funccal;
7122 if (debug_backtrace_level > 0)
7123 {
7124 for (i = 0; i < debug_backtrace_level; i++)
7125 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007126 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007127 if (temp_funccal)
7128 funccal = temp_funccal;
7129 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01007130 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007131 debug_backtrace_level = i;
7132 }
7133 }
7134 return funccal;
7135}
7136
7137/*
7138 * Return the hashtable used for local variables in the current funccal.
7139 * Return NULL if there is no current funccal.
7140 */
7141 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007142get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007143{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007144 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007145 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007146 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007147}
7148
7149/*
7150 * Return the l: scope variable.
7151 * Return NULL if there is no current funccal.
7152 */
7153 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007154get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007155{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007156 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007157 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007158 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007159}
7160
7161/*
7162 * Return the hashtable used for argument in the current funccal.
7163 * Return NULL if there is no current funccal.
7164 */
7165 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007166get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007167{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007168 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007169 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007170 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007171}
7172
7173/*
7174 * Return the a: scope variable.
7175 * Return NULL if there is no current funccal.
7176 */
7177 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007178get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007179{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007180 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007181 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007182 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007183}
7184
7185/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007186 * List function variables, if there is a function.
7187 */
7188 void
7189list_func_vars(int *first)
7190{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007191 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
7192 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01007193 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007194}
7195
7196/*
7197 * If "ht" is the hashtable for local variables in the current funccal, return
7198 * the dict that contains it.
7199 * Otherwise return NULL.
7200 */
7201 dict_T *
7202get_current_funccal_dict(hashtab_T *ht)
7203{
7204 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01007205 && ht == &current_funccal->fc_l_vars.dv_hashtab)
7206 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007207 return NULL;
7208}
7209
7210/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007211 * Search hashitem in parent scope.
7212 */
7213 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007214find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007215{
7216 funccall_T *old_current_funccal = current_funccal;
7217 hashtab_T *ht;
7218 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007219 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007220
Bram Moolenaarca16c602022-09-06 18:57:08 +01007221 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007222 return NULL;
7223
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02007224 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01007225 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02007226 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007227 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007228 ht = find_var_ht(name, &varname);
7229 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02007230 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007231 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02007232 if (!HASHITEM_EMPTY(hi))
7233 {
7234 *pht = ht;
7235 break;
7236 }
7237 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007238 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02007239 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007240 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007241 }
7242 current_funccal = old_current_funccal;
7243
7244 return hi;
7245}
7246
7247/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007248 * Search variable in parent scope.
7249 */
7250 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007251find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007252{
7253 dictitem_T *v = NULL;
7254 funccall_T *old_current_funccal = current_funccal;
7255 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007256 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007257
Bram Moolenaarca16c602022-09-06 18:57:08 +01007258 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007259 return NULL;
7260
Bram Moolenaare38eab22019-12-05 21:50:01 +01007261 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01007262 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007263 while (current_funccal)
7264 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007265 ht = find_var_ht(name, &varname);
7266 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007267 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007268 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007269 if (v != NULL)
7270 break;
7271 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007272 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007273 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007274 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007275 }
7276 current_funccal = old_current_funccal;
7277
7278 return v;
7279}
7280
7281/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007282 * Set "copyID + 1" in previous_funccal and callers.
7283 */
7284 int
7285set_ref_in_previous_funccal(int copyID)
7286{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007287 funccall_T *fc;
7288
Bram Moolenaarca16c602022-09-06 18:57:08 +01007289 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007290 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007291 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007292 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
7293 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
7294 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007295 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007296 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007297 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007298}
7299
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007300 static int
7301set_ref_in_funccal(funccall_T *fc, int copyID)
7302{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007303 if (fc->fc_copyID != copyID)
7304 {
7305 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007306 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
7307 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
7308 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
7309 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007310 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007311 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007312 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007313}
7314
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007315/*
7316 * Set "copyID" in all local vars and arguments in the call stack.
7317 */
7318 int
7319set_ref_in_call_stack(int copyID)
7320{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007321 funccall_T *fc;
7322 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007323
Bram Moolenaarca16c602022-09-06 18:57:08 +01007324 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007325 if (set_ref_in_funccal(fc, copyID))
7326 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007327
7328 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007329 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007330 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007331 if (set_ref_in_funccal(fc, copyID))
7332 return TRUE;
7333 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007334}
7335
7336/*
7337 * Set "copyID" in all functions available by name.
7338 */
7339 int
7340set_ref_in_functions(int copyID)
7341{
7342 int todo;
7343 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007344 ufunc_T *fp;
7345
7346 todo = (int)func_hashtab.ht_used;
7347 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007348 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007349 if (!HASHITEM_EMPTY(hi))
7350 {
7351 --todo;
7352 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007353 if (!func_name_refcount(fp->uf_name)
7354 && set_ref_in_func(NULL, fp, copyID))
7355 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007356 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007357 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007358 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007359}
7360
7361/*
7362 * Set "copyID" in all function arguments.
7363 */
7364 int
7365set_ref_in_func_args(int copyID)
7366{
7367 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007368
7369 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007370 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7371 copyID, NULL, NULL))
7372 return TRUE;
7373 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007374}
7375
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007376/*
7377 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007378 * Returns TRUE if setting references failed somehow.
7379 */
7380 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007381set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007382{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007383 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007384 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007385 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007386 char_u fname_buf[FLEN_FIXED + 1];
7387 char_u *tofree = NULL;
7388 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007389 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007390
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007391 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007392 return FALSE;
7393
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007394 if (fp_in == NULL)
7395 {
7396 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007397 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007398 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007399 if (fp != NULL)
7400 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007401 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007402 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007403 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007404
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007405 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007406 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007407}
7408
Bram Moolenaare38eab22019-12-05 21:50:01 +01007409#endif // FEAT_EVAL