blob: 5bf7e9558ed4d2f61c0721d75f6efa6549229bd5 [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)
174 // lambda arguments default to "any" type
Bram Moolenaar2a389082021-04-09 20:24:31 +0200175 type = vim_strsave((char_u *)
176 (is_vararg ? "list<any>" : "any"));
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100177 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
h-eastb895b0f2023-09-24 15:46:31 +0200178 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = FALSE;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100179 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100180 }
181
182 return p;
183}
184
185/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000186 * Handle line continuation in function arguments or body.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000187 * Get a next line, store it in "eap" if appropriate and put the line in
188 * "lines_to_free" to free the line later.
Bram Moolenaar7473a842021-12-28 17:55:26 +0000189 */
190 static char_u *
191get_function_line(
192 exarg_T *eap,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000193 garray_T *lines_to_free,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000194 int indent,
195 getline_opt_T getline_options)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000196{
197 char_u *theline;
198
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100199 if (eap->ea_getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +0000200 theline = getcmdline(':', 0L, indent, 0);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000201 else
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100202 theline = eap->ea_getline(':', eap->cookie, indent, getline_options);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000203 if (theline != NULL)
204 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000205 if (lines_to_free->ga_len > 0
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000206 && eap->cmdlinep != NULL
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000207 && *eap->cmdlinep == ((char_u **)lines_to_free->ga_data)
208 [lines_to_free->ga_len - 1])
Bram Moolenaar7473a842021-12-28 17:55:26 +0000209 *eap->cmdlinep = theline;
Bram Moolenaarb5820102023-01-25 12:27:13 +0000210 (void)ga_add_string(lines_to_free, theline);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000211 }
212
213 return theline;
214}
215
216/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200217 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100218 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100219 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200220 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100221 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200222get_function_args(
223 char_u **argp,
224 char_u endchar,
225 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100227 int types_optional, // types optional if "argtypes" is not NULL
h-eastb895b0f2023-09-24 15:46:31 +0200228 garray_T *arg_objm, // NULL unless using :def
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100229 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200230 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200231 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200232 int skip,
Bram Moolenaardce24412022-02-08 20:35:30 +0000233 exarg_T *eap, // can be NULL
Bram Moolenaar554d0312023-01-05 19:59:18 +0000234 int in_class, // non-zero when inside a class or interface
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000235 garray_T *newlines, // function body lines
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000236 garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200237{
238 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100239 char_u *arg;
240 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200241 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200242 int any_default = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100243 char_u *whitep = *argp;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100244 int need_expr = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200245
246 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000247 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100248 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000249 ga_init2(argtypes, sizeof(char_u *), 3);
h-eastb895b0f2023-09-24 15:46:31 +0200250 if (arg_objm != NULL)
251 ga_init2(arg_objm, sizeof(int8_T), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200252 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000253 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200254
255 if (varargs != NULL)
256 *varargs = FALSE;
257
258 /*
259 * Isolate the arguments: "arg1, arg2, ...)"
260 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100261 arg = skipwhite(*argp);
262 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200263 while (*p != endchar)
264 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100265 while (eap != NULL && eap->ea_getline != NULL
Bram Moolenaar2c330432020-04-13 14:41:35 +0200266 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200267 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200268 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000269 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000270 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000271
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200272 if (theline == NULL)
273 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200274 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200275 p = skipwhite(theline);
276 }
277
278 if (mustend && *p != endchar)
279 {
280 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000281 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100282 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200283 }
Christian Brabandte4a450a2023-12-08 20:57:38 +0100284 if (*p == endchar && !need_expr)
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200285 break;
286
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200287 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
288 {
289 if (varargs != NULL)
290 *varargs = TRUE;
291 p += 3;
292 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100293
294 if (argtypes != NULL)
295 {
296 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200297 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100298 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100299 if (!skip)
300 emsg(_(e_missing_name_after_dots));
301 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100302 }
303
304 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100305 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200306 arg_objm, evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100307 if (p == arg)
308 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100309 if (*skipwhite(p) == '=')
310 {
311 emsg(_(e_cannot_use_default_for_variable_arguments));
312 break;
313 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100314 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200315 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000316 else if (in_class && STRNCMP(p, "this.", 5) == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000317 {
318 // this.memberName
319 p += 5;
320 arg = p;
321 while (ASCII_ISALNUM(*p) || *p == '_')
322 ++p;
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000323 char_u *argend = p;
324
h-eastdb385522023-09-28 22:18:19 +0200325 // object variable this. can be used only in a constructor
326 if (STRNCMP(eap->arg, "new", 3) != 0)
327 {
328 c = *argend;
329 *argend = NUL;
330 semsg(_(e_cannot_use_an_object_variable_except_with_the_new_method_str), arg);
331 *argend = c;
332 break;
333 }
334
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000335 if (*skipwhite(p) == '=')
336 {
337 char_u *defval = skipwhite(skipwhite(p) + 1);
338 if (STRNCMP(defval, "v:none", 6) != 0)
339 {
340 semsg(_(e_constructor_default_value_must_be_vnone_str), p);
341 goto err_ret;
342 }
343 any_default = TRUE;
344 p = defval + 6;
345
346 if (ga_grow(default_args, 1) == FAIL)
347 goto err_ret;
348
349 char_u *expr = vim_strsave((char_u *)"v:none");
350 if (expr == NULL)
351 goto err_ret;
352 ((char_u **)(default_args->ga_data))
353 [default_args->ga_len] = expr;
354 default_args->ga_len++;
355 }
356 else if (any_default)
357 {
358 emsg(_(e_non_default_argument_follows_default_argument));
359 goto err_ret;
360 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000361
362 // TODO: check the argument is indeed a member
363 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
364 return FAIL;
365 if (newargs != NULL)
366 {
367 ((char_u **)(newargs->ga_data))[newargs->ga_len] =
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000368 vim_strnsave(arg, argend - arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000369 newargs->ga_len++;
370
h-eastb895b0f2023-09-24 15:46:31 +0200371 if (argtypes != NULL && ga_grow(argtypes, 1) == OK
372 && arg_objm != NULL && ga_grow(arg_objm, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000373 {
374 // TODO: use the actual type
375 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] =
376 vim_strsave((char_u *)"any");
h-eastb895b0f2023-09-24 15:46:31 +0200377 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000378
379 // Add a line to the function body for the assignment.
380 if (ga_grow(newlines, 1) == OK)
381 {
382 // "this.name = name"
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000383 int len = 5 + (argend - arg) + 3 + (argend - arg) + 1;
384 if (any_default)
385 len += 14 + 10;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000386 char_u *assignment = alloc(len);
387 if (assignment != NULL)
388 {
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000389 c = *argend;
390 *argend = NUL;
391 if (any_default)
392 vim_snprintf((char *)assignment, len,
393 "ifargisset %d this.%s = %s",
394 default_args->ga_len - 1, arg, arg);
395 else
396 vim_snprintf((char *)assignment, len,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000397 "this.%s = %s", arg, arg);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000398 *argend = c;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000399 ((char_u **)(newlines->ga_data))[
400 newlines->ga_len++] = assignment;
401 }
402 }
403 }
404 }
405 if (*p == ',')
406 ++p;
407 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200408 else
409 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200410 char_u *np;
411
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200412 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100413 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200414 arg_objm, evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100415 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200416 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200417
Bram Moolenaar015cf102021-06-26 21:52:02 +0200418 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200419 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200420 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200421 if (*np == '=' && np[1] != '=' && np[1] != '~'
422 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200423 {
424 typval_T rettv;
425
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100426 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200427 any_default = TRUE;
Bram Moolenaareb5adf12022-09-04 13:41:37 +0100428 p = skipwhite(np + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000429 char_u *expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200430 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200431 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200432 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200433 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200434 if (ga_grow(default_args, 1) == FAIL)
435 goto err_ret;
436
Christian Brabandte4a450a2023-12-08 20:57:38 +0100437 if (need_expr)
438 need_expr = FALSE;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200439 // trim trailing whitespace
440 while (p > expr && VIM_ISWHITE(p[-1]))
441 p--;
442 c = *p;
443 *p = NUL;
444 expr = vim_strsave(expr);
445 if (expr == NULL)
446 {
447 *p = c;
448 goto err_ret;
449 }
450 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200451 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200452 default_args->ga_len++;
453 *p = c;
454 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200455 }
456 else
Christian Brabandte4a450a2023-12-08 20:57:38 +0100457 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200458 mustend = TRUE;
Christian Brabandte4a450a2023-12-08 20:57:38 +0100459 if (*skipwhite(p) == NUL)
460 need_expr = TRUE;
461 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200462 }
463 else if (any_default)
464 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000465 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200466 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200467 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200468
469 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
470 {
471 // Be tolerant when skipping
472 if (!skip)
473 {
474 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
475 goto err_ret;
476 }
477 p = skipwhite(p);
478 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200479 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200480 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200481 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200482 // Don't give this error when skipping, it makes the "->" not
483 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100484 // Allow missing space after comma in legacy functions.
485 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200486 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
487 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100488 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200489 goto err_ret;
490 }
491 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200492 else
493 mustend = TRUE;
494 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200495 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200496 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200497 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200498
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200499 if (*p != endchar)
500 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100501 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200502
503 *argp = p;
504 return OK;
505
506err_ret:
507 if (newargs != NULL)
508 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200509 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200510 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200511 return FAIL;
512}
513
514/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100515 * Parse the argument types, filling "fp->uf_arg_types".
516 * Return OK or FAIL.
517 */
518 static int
h-eastb895b0f2023-09-24 15:46:31 +0200519parse_argument_types(
520 ufunc_T *fp,
521 garray_T *argtypes,
522 int varargs,
523 garray_T *arg_objm,
524 ocmember_T *obj_members,
525 int obj_member_count)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100526{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200527 int len = 0;
528
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100529 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
530 if (argtypes->ga_len > 0)
531 {
532 // When "varargs" is set the last name/type goes into uf_va_name
533 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200534 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100535
536 if (len > 0)
537 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
538 if (fp->uf_arg_types != NULL)
539 {
540 int i;
541 type_T *type;
542
543 for (i = 0; i < len; ++ i)
544 {
545 char_u *p = ((char_u **)argtypes->ga_data)[i];
546
547 if (p == NULL)
548 // will get the type from the default value
549 type = &t_unknown;
550 else
h-eastb895b0f2023-09-24 15:46:31 +0200551 {
552 if (arg_objm != NULL && ((int8_T *)arg_objm->ga_data)[i])
553 {
554 char_u *aname = ((char_u **)fp->uf_args.ga_data)[i];
555
556 type = &t_any;
557 for (int om = 0; om < obj_member_count; ++om)
558 {
Christian Brabandt915f3bf2024-04-05 20:12:19 +0200559 if (obj_members != NULL
560 && STRCMP(aname,
561 obj_members[om].ocm_name) == 0)
h-eastb895b0f2023-09-24 15:46:31 +0200562 {
563 type = obj_members[om].ocm_type;
564 break;
565 }
566 }
567 }
568 else
569 type = parse_type(&p, &fp->uf_type_list, TRUE);
570 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100571 if (type == NULL)
572 return FAIL;
573 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000574 if (i < fp->uf_args.ga_len
575 && (type->tt_type == VAR_FUNC
Yegappan Lakshmanan3e336502024-04-04 19:35:59 +0200576 || type->tt_type == VAR_PARTIAL))
577 {
578 char_u *name = ((char_u **)fp->uf_args.ga_data)[i];
579 if (obj_members != NULL && *name == '_')
580 // protected object method
581 name++;
582
583 if (var_wrong_func_name(name, TRUE))
584 return FAIL;
585 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100586 }
587 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100588 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200589
590 if (varargs)
591 {
592 char_u *p;
593
594 // Move the last argument "...name: type" to uf_va_name and
595 // uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200596 --fp->uf_args.ga_len;
Bram Moolenaarf0571712023-01-04 13:16:20 +0000597 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len];
598 ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len] = NULL;
Bram Moolenaar2a389082021-04-09 20:24:31 +0200599 p = ((char_u **)argtypes->ga_data)[len];
600 if (p == NULL)
601 // TODO: get type from default value
602 fp->uf_va_type = &t_list_any;
603 else
604 {
605 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
606 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
607 {
608 semsg(_(e_variable_arguments_type_must_be_list_str),
609 ((char_u **)argtypes->ga_data)[len]);
610 return FAIL;
611 }
612 }
613 if (fp->uf_va_type == NULL)
614 return FAIL;
615 }
616
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100617 return OK;
618}
619
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100620 static int
621parse_return_type(ufunc_T *fp, char_u *ret_type)
622{
623 if (ret_type == NULL)
624 fp->uf_ret_type = &t_void;
625 else
626 {
627 char_u *p = ret_type;
628
629 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
630 if (fp->uf_ret_type == NULL)
631 {
632 fp->uf_ret_type = &t_void;
633 return FAIL;
634 }
635 }
636 return OK;
637}
638
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100639/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200640 * Register function "fp" as using "current_funccal" as its scope.
641 */
642 static int
643register_closure(ufunc_T *fp)
644{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200645 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100646 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200647 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200648 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200649 fp->uf_scoped = current_funccal;
650 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200651
Bram Moolenaarca16c602022-09-06 18:57:08 +0100652 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200653 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100654 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
655 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200656 return OK;
657}
658
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100659 static void
660set_ufunc_name(ufunc_T *fp, char_u *name)
661{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100662 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
663 // actually extends beyond the struct.
664 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100665
666 if (name[0] == K_SPECIAL)
667 {
668 fp->uf_name_exp = alloc(STRLEN(name) + 3);
669 if (fp->uf_name_exp != NULL)
670 {
671 STRCPY(fp->uf_name_exp, "<SNR>");
672 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
673 }
674 }
675}
676
Bram Moolenaar58016442016-07-31 18:30:22 +0200677/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100678 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
679 * return "buf" filled with a readable function name.
680 * Otherwise just return "name", thus the return value can always be used.
681 * "name" and "buf" may be equal.
682 */
683 char_u *
684make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
685{
686 size_t len;
687
688 if (name[0] != K_SPECIAL)
689 return name;
690 len = STRLEN(name);
691 if (len + 3 > bufsize)
692 return name;
693
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100694 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100695 mch_memmove(buf, "<SNR>", 5);
696 return buf;
697}
698
699/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200700 * Get a name for a lambda. Returned in static memory.
701 */
702 char_u *
703get_lambda_name(void)
704{
705 static char_u name[30];
706 static int lambda_no = 0;
707
708 sprintf((char*)name, "<lambda>%d", ++lambda_no);
709 return name;
710}
711
Bram Moolenaare01e5212023-01-08 20:31:18 +0000712/*
713 * Allocate a "ufunc_T" for a function called "name".
714 * Makes sure the size is right.
715 */
716 static ufunc_T *
717alloc_ufunc(char_u *name)
718{
719 // When the name is short we need to make sure we allocate enough bytes for
720 // the whole struct, including any padding.
721 size_t len = offsetof(ufunc_T, uf_name) + STRLEN(name) + 1;
722 return alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
723}
724
Bram Moolenaar801ab062020-06-25 19:27:56 +0200725#if defined(FEAT_LUA) || defined(PROTO)
726/*
727 * Registers a native C callback which can be called from Vim script.
728 * Returns the name of the Vim script function.
729 */
730 char_u *
731register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
732{
733 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200734 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200735
Bram Moolenaare01e5212023-01-08 20:31:18 +0000736 fp = alloc_ufunc(name);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200737 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200738 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200739
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200740 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200741 fp->uf_refcount = 1;
742 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000743 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200744 fp->uf_calls = 0;
745 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200746 fp->uf_cb = cb;
747 fp->uf_cb_free = cb_free;
748 fp->uf_cb_state = state;
749
750 set_ufunc_name(fp, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000751 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200752
753 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200754}
755#endif
756
Bram Moolenaar04b12692020-05-04 23:24:44 +0200757/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100758 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100759 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100760 * If "white_error" is not NULL check for correct use of white space and set
761 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100762 * Return NULL if no valid arrow found.
763 */
764 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100765skip_arrow(
766 char_u *start,
767 int equal_arrow,
768 char_u **ret_type,
769 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100770{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100771 char_u *s = start;
772 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100773
774 if (equal_arrow)
775 {
776 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100777 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100778 if (white_error != NULL && !VIM_ISWHITE(s[1]))
779 {
780 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100781 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100782 return NULL;
783 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100784 s = skipwhite(s + 1);
785 *ret_type = s;
786 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100787 if (s == *ret_type)
788 {
789 emsg(_(e_missing_return_type));
790 return NULL;
791 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100792 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100793 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100794 s = skipwhite(s);
795 if (*s != '=')
796 return NULL;
797 ++s;
798 }
799 if (*s != '>')
800 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100801 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
802 || !IS_WHITE_OR_NUL(s[1])))
803 {
804 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100805 semsg(_(e_white_space_required_before_and_after_str_at_str),
806 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100807 return NULL;
808 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100809 return skipwhite(s + 1);
810}
811
812/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100813 * Check if "*cmd" points to a function command and if so advance "*cmd" and
814 * return TRUE.
815 * Otherwise return FALSE;
816 * Do not consider "function(" to be a command.
817 */
818 static int
819is_function_cmd(char_u **cmd)
820{
821 char_u *p = *cmd;
822
823 if (checkforcmd(&p, "function", 2))
824 {
825 if (*p == '(')
826 return FALSE;
827 *cmd = p;
828 return TRUE;
829 }
830 return FALSE;
831}
832
833/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200834 * Called when defining a function: The context may be needed for script
835 * variables declared in a block that is visible now but not when the function
836 * is compiled or called later.
837 */
838 static void
839function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
840{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000841 if (cstack == NULL || cstack->cs_idx < 0)
842 return;
843
844 int count = cstack->cs_idx + 1;
845 int i;
846
847 fp->uf_block_ids = ALLOC_MULT(int, count);
848 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200849 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000850 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
851 sizeof(int) * count);
852 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200853 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000854
855 // Set flag in each block to indicate a function was defined. This
856 // is used to keep the variable when leaving the block, see
857 // hide_script_var().
858 for (i = 0; i <= cstack->cs_idx; ++i)
859 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200860}
861
862/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100863 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200864 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100865 * "newlines" must already have been initialized.
866 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
867 */
868 static int
869get_function_body(
870 exarg_T *eap,
871 garray_T *newlines,
872 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000873 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100874{
875 linenr_T sourcing_lnum_top = SOURCING_LNUM;
876 linenr_T sourcing_lnum_off;
877 int saved_wait_return = need_wait_return;
878 char_u *line_arg = line_arg_in;
879 int vim9_function = eap->cmdidx == CMD_def
880 || eap->cmdidx == CMD_block;
881#define MAX_FUNC_NESTING 50
882 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200883 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100884 int nesting = 0;
885 getline_opt_T getline_options;
886 int indent = 2;
887 char_u *skip_until = NULL;
888 int ret = FAIL;
889 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200890 int heredoc_concat_len = 0;
891 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100892 char_u *heredoc_trimmed = NULL;
893
Bram Moolenaar20677332021-06-06 17:02:53 +0200894 ga_init2(&heredoc_ga, 1, 500);
895
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100896 // Detect having skipped over comment lines to find the return
897 // type. Add NULL lines to keep the line count correct.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100898 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100899 if (SOURCING_LNUM < sourcing_lnum_off)
900 {
901 sourcing_lnum_off -= SOURCING_LNUM;
902 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
903 goto theend;
904 while (sourcing_lnum_off-- > 0)
905 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
906 }
907
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200908 nesting_def[0] = vim9_function;
909 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100910 getline_options = vim9_function
911 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
912 for (;;)
913 {
914 char_u *theline;
915 char_u *p;
916 char_u *arg;
917
918 if (KeyTyped)
919 {
920 msg_scroll = TRUE;
921 saved_wait_return = FALSE;
922 }
923 need_wait_return = FALSE;
924
925 if (line_arg != NULL)
926 {
927 // Use eap->arg, split up in parts by line breaks.
928 theline = line_arg;
929 p = vim_strchr(theline, '\n');
930 if (p == NULL)
931 line_arg += STRLEN(line_arg);
932 else
933 {
934 *p = NUL;
935 line_arg = p + 1;
936 }
937 }
938 else
939 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000940 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100941 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100942 }
943 if (KeyTyped)
944 lines_left = Rows - 1;
945 if (theline == NULL)
946 {
947 // Use the start of the function for the line number.
948 SOURCING_LNUM = sourcing_lnum_top;
949 if (skip_until != NULL)
950 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200951 else if (nesting_inline[nesting])
952 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100953 else if (eap->cmdidx == CMD_def)
954 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100955 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000956 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100957 goto theend;
958 }
959
960 // Detect line continuation: SOURCING_LNUM increased more than one.
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +0100961 sourcing_lnum_off = get_sourced_lnum(eap->ea_getline, eap->cookie);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100962 if (SOURCING_LNUM < sourcing_lnum_off)
963 sourcing_lnum_off -= SOURCING_LNUM;
964 else
965 sourcing_lnum_off = 0;
966
967 if (skip_until != NULL)
968 {
969 // Don't check for ":endfunc"/":enddef" between
970 // * ":append" and "."
971 // * ":python <<EOF" and "EOF"
972 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
973 if (heredoc_trimmed == NULL
974 || (is_heredoc && skipwhite(theline) == theline)
975 || STRNCMP(theline, heredoc_trimmed,
976 STRLEN(heredoc_trimmed)) == 0)
977 {
978 if (heredoc_trimmed == NULL)
979 p = theline;
980 else if (is_heredoc)
981 p = skipwhite(theline) == theline
982 ? theline : theline + STRLEN(heredoc_trimmed);
983 else
984 p = theline + STRLEN(heredoc_trimmed);
985 if (STRCMP(p, skip_until) == 0)
986 {
987 VIM_CLEAR(skip_until);
988 VIM_CLEAR(heredoc_trimmed);
989 getline_options = vim9_function
990 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
991 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200992
993 if (heredoc_concat_len > 0)
994 {
995 // Replace the starting line with all the concatenated
996 // lines.
997 ga_concat(&heredoc_ga, theline);
998 vim_free(((char_u **)(newlines->ga_data))[
999 heredoc_concat_len - 1]);
1000 ((char_u **)(newlines->ga_data))[
1001 heredoc_concat_len - 1] = heredoc_ga.ga_data;
1002 ga_init(&heredoc_ga);
1003 heredoc_concat_len = 0;
1004 theline += STRLEN(theline); // skip the "EOF"
1005 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001006 }
1007 }
1008 }
1009 else
1010 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001011 int c;
1012 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +00001013 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001014
1015 // skip ':' and blanks
1016 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
1017 ;
1018
1019 // Check for "endfunction", "enddef" or "}".
1020 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +00001021 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001022 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001023 ? *p == '}'
1024 : (checkforcmd(&p, nesting_def[nesting]
1025 ? "enddef" : "endfunction", 4)
1026 && *p != ':'))
1027 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00001028 if (!nesting_inline[nesting] && nesting_def[nesting]
1029 && p < cmd + 6)
1030 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001031 if (nesting-- == 0)
1032 {
1033 char_u *nextcmd = NULL;
1034
1035 if (*p == '|' || *p == '}')
1036 nextcmd = p + 1;
1037 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
1038 nextcmd = line_arg;
1039 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001040 && (vim9_function || p_verbose > 0))
1041 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +02001042 SOURCING_LNUM = sourcing_lnum_top
1043 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001044 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +00001045 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001046 else
1047 give_warning2((char_u *)
1048 _("W22: Text found after :endfunction: %s"),
1049 p, TRUE);
1050 }
1051 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001052 {
1053 // Another command follows. If the line came from "eap"
1054 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001055 // change "eap->cmdlinep" to point to the last fetched
1056 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001057 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001058 if (lines_to_free->ga_len > 0
1059 && *eap->cmdlinep !=
1060 ((char_u **)lines_to_free->ga_data)
1061 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001062 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001063 // *cmdlinep will be freed later, thus remove the
1064 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001065 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001066 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
1067 [lines_to_free->ga_len - 1];
1068 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001069 }
1070 }
1071 break;
1072 }
1073 }
1074
1075 // Check for mismatched "endfunc" or "enddef".
1076 // We don't check for "def" inside "func" thus we also can't check
1077 // for "enddef".
1078 // We continue to find the end of the function, although we might
1079 // not find it.
1080 else if (nesting_def[nesting])
1081 {
1082 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1083 emsg(_(e_mismatched_endfunction));
1084 }
1085 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1086 emsg(_(e_mismatched_enddef));
1087
1088 // Increase indent inside "if", "while", "for" and "try", decrease
1089 // at "end".
1090 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1091 indent -= 2;
1092 else if (STRNCMP(p, "if", 2) == 0
1093 || STRNCMP(p, "wh", 2) == 0
1094 || STRNCMP(p, "for", 3) == 0
1095 || STRNCMP(p, "try", 3) == 0)
1096 indent += 2;
1097
1098 // Check for defining a function inside this function.
1099 // Only recognize "def" inside "def", not inside "function",
1100 // For backwards compatibility, see Test_function_python().
1101 c = *p;
1102 if (is_function_cmd(&p)
1103 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1104 {
1105 if (*p == '!')
1106 p = skipwhite(p + 1);
1107 p += eval_fname_script(p);
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001108 vim_free(trans_function_name(&p, NULL, TRUE, 0));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001109 if (*skipwhite(p) == '(')
1110 {
1111 if (nesting == MAX_FUNC_NESTING - 1)
1112 emsg(_(e_function_nesting_too_deep));
1113 else
1114 {
1115 ++nesting;
1116 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001117 nesting_inline[nesting] = FALSE;
1118 indent += 2;
1119 }
1120 }
1121 }
1122
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001123 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001124 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001125 // Not a comment line: check for nested inline function.
1126 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001127 while (end > p && VIM_ISWHITE(*end))
1128 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001129 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001130 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001131 int is_block;
1132
1133 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001134 --end;
1135 while (end > p && VIM_ISWHITE(*end))
1136 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001137 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1138 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001139 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001140 char_u *s = p;
1141
1142 // check for line starting with "au" for :autocmd or
1143 // "com" for :command, these can use a {} block
1144 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1145 || checkforcmd_noparen(&s, "command", 3);
1146 }
1147
1148 if (is_block)
1149 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001150 if (nesting == MAX_FUNC_NESTING - 1)
1151 emsg(_(e_function_nesting_too_deep));
1152 else
1153 {
1154 ++nesting;
1155 nesting_def[nesting] = TRUE;
1156 nesting_inline[nesting] = TRUE;
1157 indent += 2;
1158 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001159 }
1160 }
1161 }
1162
1163 // Check for ":append", ":change", ":insert". Not for :def.
1164 p = skip_range(p, FALSE, NULL);
1165 if (!vim9_function
1166 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1167 || (p[0] == 'c'
1168 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1169 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1170 && (STRNCMP(&p[3], "nge", 3) != 0
1171 || !ASCII_ISALPHA(p[6])))))))
1172 || (p[0] == 'i'
1173 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1174 && (!ASCII_ISALPHA(p[2])
1175 || (p[2] == 's'
1176 && (!ASCII_ISALPHA(p[3])
1177 || p[3] == 'e'))))))))
1178 skip_until = vim_strsave((char_u *)".");
1179
1180 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1181 arg = skipwhite(skiptowhite(p));
1182 if (arg[0] == '<' && arg[1] =='<'
1183 && ((p[0] == 'p' && p[1] == 'y'
1184 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1185 || ((p[2] == '3' || p[2] == 'x')
1186 && !ASCII_ISALPHA(p[3]))))
1187 || (p[0] == 'p' && p[1] == 'e'
1188 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1189 || (p[0] == 't' && p[1] == 'c'
1190 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1191 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1192 && !ASCII_ISALPHA(p[3]))
1193 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1194 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1195 || (p[0] == 'm' && p[1] == 'z'
1196 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1197 ))
1198 {
1199 // ":python <<" continues until a dot, like ":append"
1200 p = skipwhite(arg + 2);
1201 if (STRNCMP(p, "trim", 4) == 0)
1202 {
1203 // Ignore leading white space.
1204 p = skipwhite(p + 4);
1205 heredoc_trimmed = vim_strnsave(theline,
1206 skipwhite(theline) - theline);
1207 }
1208 if (*p == NUL)
1209 skip_until = vim_strsave((char_u *)".");
1210 else
1211 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1212 getline_options = GETLINE_NONE;
1213 is_heredoc = TRUE;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001214 if (vim9_function && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001215 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001216 }
1217
Bram Moolenaard881d152022-05-13 13:50:36 +01001218 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001219 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001220 // Check for ":cmd v =<< [trim] EOF"
1221 // and ":cmd [a, b] =<< [trim] EOF"
1222 // and "lines =<< [trim] EOF" for Vim9
1223 // Where "cmd" can be "let", "var", "final" or "const".
zeertzjqa93d9cd2023-05-02 16:25:47 +01001224 arg = p;
1225 if (checkforcmd(&arg, "let", 2)
1226 || checkforcmd(&arg, "var", 3)
1227 || checkforcmd(&arg, "final", 5)
1228 || checkforcmd(&arg, "const", 5)
1229 || vim9_function)
Bram Moolenaard881d152022-05-13 13:50:36 +01001230 {
zeertzjq9a91d2b2024-04-09 21:47:10 +02001231 int save_sc_version = current_sctx.sc_version;
1232 int var_count = 0;
1233 int semicolon = 0;
zeertzjq9a91d2b2024-04-09 21:47:10 +02001234
1235 current_sctx.sc_version
1236 = vim9_function ? SCRIPT_VERSION_VIM9 : 1;
zeertzjq1817ccd2024-04-10 17:37:47 +02001237 arg = skip_var_list(arg, TRUE, &var_count, &semicolon,
zeertzjq9a91d2b2024-04-09 21:47:10 +02001238 TRUE);
zeertzjq1817ccd2024-04-10 17:37:47 +02001239 if (arg != NULL)
1240 arg = skipwhite(arg);
zeertzjq9a91d2b2024-04-09 21:47:10 +02001241 current_sctx.sc_version = save_sc_version;
zeertzjq1817ccd2024-04-10 17:37:47 +02001242 if (arg != NULL && STRNCMP(arg, "=<<", 3) == 0)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001243 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001244 p = skipwhite(arg + 3);
1245 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001246 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001247 if (STRNCMP(p, "trim", 4) == 0)
1248 {
1249 // Ignore leading white space.
1250 p = skipwhite(p + 4);
1251 heredoc_trimmed = vim_strnsave(theline,
1252 skipwhite(theline) - theline);
1253 continue;
1254 }
1255 if (STRNCMP(p, "eval", 4) == 0)
1256 {
1257 // Ignore leading white space.
1258 p = skipwhite(p + 4);
1259 continue;
1260 }
1261 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001262 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001263 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1264 getline_options = GETLINE_NONE;
1265 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001266 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001267 }
1268 }
1269 }
1270
1271 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001272 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001273 goto theend;
1274
Bram Moolenaar20677332021-06-06 17:02:53 +02001275 if (heredoc_concat_len > 0)
1276 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001277 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001278 // to be used for the instruction later.
1279 ga_concat(&heredoc_ga, theline);
1280 ga_concat(&heredoc_ga, (char_u *)"\n");
1281 p = vim_strsave((char_u *)"");
1282 }
1283 else
1284 {
1285 // Copy the line to newly allocated memory. get_one_sourceline()
1286 // allocates 250 bytes per line, this saves 80% on average. The
1287 // cost is an extra alloc/free.
1288 p = vim_strsave(theline);
1289 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001290 if (p == NULL)
1291 goto theend;
1292 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1293
1294 // Add NULL lines for continuation lines, so that the line count is
1295 // equal to the index in the growarray.
1296 while (sourcing_lnum_off-- > 0)
1297 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1298
1299 // Check for end of eap->arg.
1300 if (line_arg != NULL && *line_arg == NUL)
1301 line_arg = NULL;
1302 }
1303
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001304 // Return OK when no error was detected.
1305 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001306 ret = OK;
1307
1308theend:
1309 vim_free(skip_until);
1310 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001311 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001312 need_wait_return |= saved_wait_return;
1313 return ret;
1314}
1315
1316/*
1317 * Handle the body of a lambda. *arg points to the "{", process statements
1318 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001319 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001320 * When successful "rettv" is set to a funcref.
1321 */
1322 static int
1323lambda_function_body(
1324 char_u **arg,
1325 typval_T *rettv,
1326 evalarg_T *evalarg,
1327 garray_T *newargs,
1328 garray_T *argtypes,
1329 int varargs,
1330 garray_T *default_args,
1331 char_u *ret_type)
1332{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001333 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001334 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001335 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001336 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001337 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001338 exarg_T eap;
1339 garray_T newlines;
1340 char_u *cmdline = NULL;
1341 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001342 partial_T *pt;
1343 char_u *name;
1344 int lnum_save = -1;
1345 linenr_T sourcing_lnum_top = SOURCING_LNUM;
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001346 char_u *line_arg = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001347
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001348 *arg = skipwhite(*arg + 1);
1349 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001350 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001351 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001352 return FAIL;
1353 }
1354
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001355 // When there is a line break use what follows for the lambda body.
1356 // Makes lambda body initializers work for object and enum member
1357 // variables.
1358 if (**arg == '\n')
1359 line_arg = *arg + 1;
1360
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001361 CLEAR_FIELD(eap);
1362 eap.cmdidx = CMD_block;
1363 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001364 eap.cmdlinep = &cmdline;
1365 eap.skip = !evaluate;
1366 if (evalarg->eval_cctx != NULL)
1367 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1368 else
1369 {
Zoltan Arpadffy6fdb6282023-12-19 20:53:07 +01001370 eap.ea_getline = evalarg->eval_getline;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001371 eap.cookie = evalarg->eval_cookie;
1372 }
1373
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001374 ga_init2(&newlines, sizeof(char_u *), 10);
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001375 if (get_function_body(&eap, &newlines, line_arg,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001376 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001377 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001378
1379 // When inside a lambda must add the function lines to evalarg.eval_ga.
1380 evalarg->eval_break_count += newlines.ga_len;
1381 if (gap->ga_itemsize > 0)
1382 {
1383 int idx;
1384 char_u *last;
1385 size_t plen;
1386 char_u *pnl;
1387
1388 for (idx = 0; idx < newlines.ga_len; ++idx)
1389 {
Yegappan Lakshmanan3fa8f772024-04-04 21:42:07 +02001390 char_u *p = ((char_u **)newlines.ga_data)[idx];
1391 if (p == NULL)
1392 // comment line in the lambda body
1393 continue;
1394
1395 p = skipwhite(p);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001396
Bram Moolenaarecb66452021-05-18 15:09:18 +02001397 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001398 goto erret;
1399
1400 // Going to concatenate the lines after parsing. For an empty or
1401 // comment line use an empty string.
1402 // Insert NL characters at the start of each line, the string will
1403 // be split again later in .get_lambda_tv().
1404 if (*p == NUL || vim9_comment_start(p))
1405 p = (char_u *)"";
1406 plen = STRLEN(p);
1407 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1408 if (pnl != NULL)
1409 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001410 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1411 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001412 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001413 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001414 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001415 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001416 // more is following after the "}", which was skipped
1417 last = cmdline;
1418 else
1419 // nothing is following the "}"
1420 last = (char_u *)"}";
1421 plen = STRLEN(last);
1422 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1423 if (pnl != NULL)
1424 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001425 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1426 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001427 }
1428
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001429 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001430 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001431 garray_T *tfgap = &evalarg->eval_tofree_ga;
1432
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001433 // Something comes after the "}".
1434 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001435
1436 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001437 if (ga_grow(tfgap, 1) == OK)
1438 {
1439 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1440 evalarg->eval_using_cmdline = TRUE;
1441 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001442 }
1443 else
1444 *arg = (char_u *)"";
1445
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001446 if (!evaluate)
1447 {
1448 ret = OK;
1449 goto erret;
1450 }
1451
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001452 name = get_lambda_name();
Bram Moolenaare01e5212023-01-08 20:31:18 +00001453 ufunc = alloc_ufunc(name);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001454 if (ufunc == NULL)
1455 goto erret;
1456 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001457 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001458 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001459 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001460 ufunc->uf_refcount = 1;
1461 ufunc->uf_args = *newargs;
1462 newargs->ga_data = NULL;
1463 ufunc->uf_def_args = *default_args;
1464 default_args->ga_data = NULL;
1465 ufunc->uf_func_type = &t_func_any;
1466
1467 // error messages are for the first function line
1468 lnum_save = SOURCING_LNUM;
1469 SOURCING_LNUM = sourcing_lnum_top;
1470
1471 // parse argument types
h-eastb895b0f2023-09-24 15:46:31 +02001472 if (parse_argument_types(ufunc, argtypes, varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001473 {
1474 SOURCING_LNUM = lnum_save;
1475 goto erret;
1476 }
1477
1478 // parse the return type, if any
1479 if (parse_return_type(ufunc, ret_type) == FAIL)
1480 goto erret;
1481
1482 pt = ALLOC_CLEAR_ONE(partial_T);
1483 if (pt == NULL)
1484 goto erret;
1485 pt->pt_func = ufunc;
1486 pt->pt_refcount = 1;
1487
1488 ufunc->uf_lines = newlines;
1489 newlines.ga_data = NULL;
1490 if (sandbox)
1491 ufunc->uf_flags |= FC_SANDBOX;
1492 if (!ASCII_ISUPPER(*ufunc->uf_name))
1493 ufunc->uf_flags |= FC_VIM9;
1494 ufunc->uf_script_ctx = current_sctx;
1495 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1496 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1497 set_function_type(ufunc);
1498
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001499 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1500
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001501 rettv->vval.v_partial = pt;
1502 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001503 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001504 ret = OK;
1505
1506erret:
1507 if (lnum_save >= 0)
1508 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001509 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001510 if (newargs != NULL)
1511 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001512 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001513 if (ufunc != NULL)
1514 {
1515 func_clear(ufunc, TRUE);
1516 func_free(ufunc, TRUE);
1517 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001518 return ret;
1519}
1520
1521/*
1522 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001523 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001524 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001525 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1526 */
1527 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001528get_lambda_tv(
1529 char_u **arg,
1530 typval_T *rettv,
1531 int types_optional,
1532 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001533{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001534 int evaluate = evalarg != NULL
1535 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001536 garray_T newargs;
1537 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001538 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001539 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001540 garray_T default_args;
h-eastb895b0f2023-09-24 15:46:31 +02001541 garray_T arg_objm;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001542 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001543 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001544 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001545 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001546 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001547 char_u *s;
1548 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001549 int *old_eval_lavars = eval_lavars_used;
1550 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001551 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001552 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001553 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001554 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001555 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001556 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001557
Bram Moolenaar4525a572022-02-13 11:57:33 +00001558 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001559 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001560
1561 ga_init(&newargs);
1562 ga_init(&newlines);
1563
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001564 // First, check if this is really a lambda expression. "->" or "=>" must
1565 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001566 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001567 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001568 types_optional ? &argtypes : NULL, types_optional,
1569 types_optional ? &arg_objm : NULL, evalarg,
1570 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001571 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001572 {
1573 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001574 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001575 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001576 ga_clear(&arg_objm);
1577 }
Bram Moolenaar0346b792021-01-31 22:18:29 +01001578 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001579 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001580
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001581 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001582 if (evaluate)
1583 pnewargs = &newargs;
1584 else
1585 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001586 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001587 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
h-eastb895b0f2023-09-24 15:46:31 +02001588 types_optional ? &argtypes : NULL, types_optional,
1589 types_optional ? &arg_objm : NULL, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001590 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001591 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001592 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001593 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001594 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001595 {
1596 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001597 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001598 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001599 ga_clear(&arg_objm);
1600 }
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001601 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001602 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001603 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001604 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001605
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001606 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1607 if (ret_type != NULL)
1608 {
1609 ret_type = vim_strsave(ret_type);
1610 tofree2 = ret_type;
1611 }
1612
Bram Moolenaare38eab22019-12-05 21:50:01 +01001613 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001614 if (evaluate)
1615 eval_lavars_used = &eval_lavars;
1616
Bram Moolenaar65c44152020-12-24 15:14:01 +01001617 *arg = skipwhite_and_linebreak(*arg, evalarg);
1618
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001619 // Recognize "{" as the start of a function body.
1620 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001621 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001622 if (evalarg == NULL)
1623 // cannot happen?
1624 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001625 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001626 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1627 types_optional ? &argtypes : NULL, varargs,
1628 &default_args, ret_type) == FAIL)
1629 goto errret;
1630 goto theend;
1631 }
1632 if (default_args.ga_len > 0)
1633 {
1634 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001635 goto errret;
1636 }
1637
Bram Moolenaare38eab22019-12-05 21:50:01 +01001638 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001639 start = *arg;
1640 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001641 if (ret == FAIL)
1642 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001643
Bram Moolenaar65c44152020-12-24 15:14:01 +01001644 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001645 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001646 *arg = skipwhite_and_linebreak(*arg, evalarg);
1647 if (**arg != '}')
1648 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001649 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001650 goto errret;
1651 }
1652 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001653 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001654
1655 if (evaluate)
1656 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001657 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001658 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001659 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001660 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001661 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001662
Bram Moolenaare01e5212023-01-08 20:31:18 +00001663 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001664 if (fp == NULL)
1665 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001666 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001667 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001668 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001669 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001670
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001671 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001672 if (ga_grow(&newlines, 1) == FAIL)
1673 goto errret;
1674
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001675 // If there are line breaks, we need to split up the string.
1676 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001677 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001678 line_end = end;
1679
1680 // Add "return " before the expression (or the first line).
1681 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001682 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001683 if (p == NULL)
1684 goto errret;
1685 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1686 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001687 vim_strncpy(p + 7, start, line_end - start);
1688
1689 if (line_end != end)
1690 {
1691 // Add more lines, split by line breaks. Thus is used when a
1692 // lambda with { cmds } is encountered.
1693 while (*line_end == '\n')
1694 {
1695 if (ga_grow(&newlines, 1) == FAIL)
1696 goto errret;
1697 start = line_end + 1;
1698 line_end = vim_strchr(start, '\n');
1699 if (line_end == NULL)
1700 line_end = end;
1701 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1702 vim_strnsave(start, line_end - start);
1703 }
1704 }
1705
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001706 if (strstr((char *)p + 7, "a:") == NULL)
1707 // No a: variables are used for sure.
1708 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001709
1710 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001711 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001712 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001713 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001714 if (types_optional)
1715 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001716 if (parse_argument_types(fp, &argtypes,
h-eastb895b0f2023-09-24 15:46:31 +02001717 vim9script && varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001718 goto errret;
1719 if (ret_type != NULL)
1720 {
1721 fp->uf_ret_type = parse_type(&ret_type,
1722 &fp->uf_type_list, TRUE);
1723 if (fp->uf_ret_type == NULL)
1724 goto errret;
1725 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001726 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001727 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001728 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001729
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001730 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001731 if (current_funccal != NULL && eval_lavars)
1732 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001733 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001734 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001735 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001736 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001737
1738#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001739 if (prof_def_func())
1740 func_do_profile(fp);
1741#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001742 if (sandbox)
1743 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001744 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001745 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001746 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001747 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001748 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001749 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001750 // Use the line number of the arguments.
1751 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001752
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001753 function_using_block_scopes(fp, evalarg->eval_cstack);
1754
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001755 pt->pt_func = fp;
1756 pt->pt_refcount = 1;
1757 rettv->vval.v_partial = pt;
1758 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001759
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001760 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001761 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001762
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001763theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001764 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001765 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001766 if (types_optional)
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001767 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001768 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001769 ga_clear(&arg_objm);
1770 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001771
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001772 return OK;
1773
1774errret:
1775 ga_clear_strings(&newargs);
1776 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001777 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001778 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001779 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001780 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001781 ga_clear(&arg_objm);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001782 if (fp != NULL)
1783 vim_free(fp->uf_arg_types);
1784 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001785 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001786 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001787 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001788 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001789 return FAIL;
1790}
1791
1792/*
1793 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1794 * name it contains, otherwise return "name".
1795 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1796 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001797 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1798 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001799 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001800 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001801 */
1802 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001803deref_func_name(
1804 char_u *name,
1805 int *lenp,
1806 partial_T **partialp,
1807 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001808 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001809 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001810 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001811{
1812 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001813 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001814 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001815 char_u *s = NULL;
1816 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001817 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001818
1819 if (partialp != NULL)
1820 *partialp = NULL;
1821
1822 cc = name[*lenp];
1823 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001824
Bram Moolenaar71f21932022-01-07 18:20:55 +00001825 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001826 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001827 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001828 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001829 tv = &v->di_tv;
1830 }
1831 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1832 {
1833 imported_T *import;
1834 char_u *p = name;
1835 int len = *lenp;
1836
1837 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001838 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001839 p = name + 2;
1840 len -= 2;
1841 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001842 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001843
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001844 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001845 if (import != NULL)
1846 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001847 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001848 if (new_function)
1849 semsg(_(e_redefining_imported_item_str), name);
1850 else
1851 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001852 name[len] = cc;
1853 *lenp = 0;
1854 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001855 }
1856 }
1857
1858 if (tv != NULL)
1859 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001860 if (found_var != NULL)
1861 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001862 if (tv->v_type == VAR_FUNC)
1863 {
1864 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001865 {
1866 *lenp = 0;
1867 return (char_u *)""; // just in case
1868 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001869 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001870 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001871 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001872
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001873 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001874 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001875 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001876
1877 if (pt == NULL)
1878 {
1879 *lenp = 0;
1880 return (char_u *)""; // just in case
1881 }
1882 if (partialp != NULL)
1883 *partialp = pt;
1884 s = partial_name(pt);
1885 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001886 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001887
1888 if (s != NULL)
1889 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001890 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001891 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001892 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001893
1894 if (sv != NULL)
1895 *type = sv->sv_type;
1896 }
1897 return s;
1898 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001899 }
1900
1901 return name;
1902}
1903
1904/*
1905 * Give an error message with a function name. Handle <SNR> things.
1906 * "ermsg" is to be passed without translation, use N_() instead of _().
1907 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001908 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001909emsg_funcname(char *ermsg, char_u *name)
1910{
Bram Moolenaar54598062022-01-13 12:05:09 +00001911 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001912
Bram Moolenaar54598062022-01-13 12:05:09 +00001913 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001914 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001915 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001916 if (p != name)
1917 vim_free(p);
1918}
1919
1920/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001921 * Get function arguments at "*arg" and advance it.
1922 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001923 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001924 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001925 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001926get_func_arguments(
1927 char_u **arg,
1928 evalarg_T *evalarg,
1929 int partial_argc,
1930 typval_T *argvars,
Ernie Raelb077b582023-12-14 20:11:44 +01001931 int *argcount,
1932 int is_builtin)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001933{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001934 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001935 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001936 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001937 int evaluate = evalarg == NULL
1938 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001939
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001940 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001941 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001942 // skip the '(' or ',' and possibly line breaks
1943 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1944
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001945 if (*argp == ')' || *argp == ',' || *argp == NUL)
1946 break;
Ernie Raelb077b582023-12-14 20:11:44 +01001947
1948 int arg_idx = *argcount;
1949 if (eval1(&argp, &argvars[arg_idx], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001950 {
1951 ret = FAIL;
1952 break;
1953 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001954 ++*argcount;
Ernie Raelb077b582023-12-14 20:11:44 +01001955 if (!is_builtin && check_typval_is_value(&argvars[arg_idx]) == FAIL)
1956 {
1957 ret = FAIL;
1958 break;
1959 }
1960
Bram Moolenaar9d489562020-07-30 20:08:50 +02001961 // The comma should come right after the argument, but this wasn't
1962 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001963 if (vim9script)
1964 {
1965 if (*argp != ',' && *skipwhite(argp) == ',')
1966 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001967 if (evaluate)
1968 semsg(_(e_no_white_space_allowed_before_str_str),
1969 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001970 ret = FAIL;
1971 break;
1972 }
1973 }
1974 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001975 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001976 if (*argp != ',')
1977 break;
Christian Brabandt00cb2472023-09-05 20:46:25 +02001978 if (vim9script && !IS_WHITE_NL_OR_NUL(argp[1]))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001979 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001980 if (evaluate)
1981 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001982 ret = FAIL;
1983 break;
1984 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001985 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001986
Bram Moolenaare6b53242020-07-01 17:28:33 +02001987 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001988 if (*argp == ')')
1989 ++argp;
1990 else
1991 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001992 *arg = argp;
1993 return ret;
1994}
1995
1996/*
1997 * Call a function and put the result in "rettv".
1998 * Return OK or FAIL.
1999 */
2000 int
2001get_func_tv(
2002 char_u *name, // name of the function
2003 int len, // length of "name" or -1 to use strlen()
2004 typval_T *rettv,
2005 char_u **arg, // argument, pointing to the '('
2006 evalarg_T *evalarg, // for line continuation
2007 funcexe_T *funcexe) // various values
2008{
2009 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002010 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01002011 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
2012 int argcount = 0; // number of arguments found
2013 int vim9script = in_vim9script();
2014 int evaluate = evalarg == NULL
2015 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
2016
2017 argp = *arg;
2018 ret = get_func_arguments(&argp, evalarg,
2019 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
Ernie Raelb077b582023-12-14 20:11:44 +01002020 argvars, &argcount, builtin_function(name, -1));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002021
2022 if (ret == OK)
2023 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002024 int i = 0;
2025 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002026
2027 if (get_vim_var_nr(VV_TESTING))
2028 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002029 // Prepare for calling test_garbagecollect_now(), need to know
2030 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002031 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00002032 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002033 for (i = 0; i < argcount; ++i)
2034 if (ga_grow(&funcargs, 1) == OK)
2035 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
2036 &argvars[i];
2037 }
2038
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002039 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00002040 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002041 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002042 // An error in a builtin function does not return FAIL, but we do
2043 // want to abort further processing if an error was given.
2044 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002045 clear_tv(rettv);
2046 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002047
2048 funcargs.ga_len -= i;
2049 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002050 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002051 {
2052 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002053 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002054 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00002055 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002056 }
2057
2058 while (--argcount >= 0)
2059 clear_tv(&argvars[argcount]);
2060
Bram Moolenaar4525a572022-02-13 11:57:33 +00002061 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02002062 *arg = argp;
2063 else
2064 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002065 return ret;
2066}
2067
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002068/*
2069 * Return TRUE if "p" starts with "<SID>" or "s:".
2070 * Only works if eval_fname_script() returned non-zero for "p"!
2071 */
2072 static int
2073eval_fname_sid(char_u *p)
2074{
2075 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
2076}
2077
2078/*
2079 * In a script change <SID>name() and s:name() to K_SNR 123_name().
2080 * Change <SNR>123_name() to K_SNR 123_name().
2081 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002082 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002083 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002084 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00002085fname_trans_sid(
2086 char_u *name,
2087 char_u *fname_buf,
2088 char_u **tofree,
2089 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002090{
2091 int llen;
2092 char_u *fname;
2093 int i;
2094
2095 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002096 if (llen == 0)
2097 return name; // no prefix
2098
2099 fname_buf[0] = K_SPECIAL;
2100 fname_buf[1] = KS_EXTRA;
2101 fname_buf[2] = (int)KE_SNR;
2102 i = 3;
2103 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002104 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002105 if (current_sctx.sc_sid <= 0)
2106 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002107 else
2108 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002109 sprintf((char *)fname_buf + 3, "%ld_",
2110 (long)current_sctx.sc_sid);
2111 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002112 }
2113 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002114 if (i + STRLEN(name + llen) < FLEN_FIXED)
2115 {
2116 STRCPY(fname_buf + i, name + llen);
2117 fname = fname_buf;
2118 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002119 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002120 {
2121 fname = alloc(i + STRLEN(name + llen) + 1);
2122 if (fname == NULL)
2123 *error = FCERR_OTHER;
2124 else
2125 {
2126 *tofree = fname;
2127 mch_memmove(fname, fname_buf, (size_t)i);
2128 STRCPY(fname + i, name + llen);
2129 }
2130 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002131 return fname;
2132}
2133
2134/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002135 * Concatenate the script ID and function name into "<SNR>99_name".
2136 * "buffer" must have size MAX_FUNC_NAME_LEN.
2137 */
2138 void
2139func_name_with_sid(char_u *name, int sid, char_u *buffer)
2140{
2141 // A script-local function is stored as "<SNR>99_name".
2142 buffer[0] = K_SPECIAL;
2143 buffer[1] = KS_EXTRA;
2144 buffer[2] = (int)KE_SNR;
2145 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2146 (long)sid, name);
2147}
2148
2149/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002150 * Find a function "name" in script "sid".
2151 */
2152 static ufunc_T *
2153find_func_with_sid(char_u *name, int sid)
2154{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002155 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002156 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157
Bram Moolenaarb8822442022-01-11 15:24:05 +00002158 if (!SCRIPT_ID_VALID(sid))
2159 return NULL; // not in a script
2160
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002161 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162 hi = hash_find(&func_hashtab, buffer);
2163 if (!HASHITEM_EMPTY(hi))
2164 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002165 return NULL;
2166}
2167
2168/*
2169 * Find a function "name" in script "sid" prefixing the autoload prefix.
2170 */
2171 static ufunc_T *
2172find_func_with_prefix(char_u *name, int sid)
2173{
2174 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002175 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002176 scriptitem_T *si;
2177
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002178 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002179 return NULL; // already has the prefix
2180 if (!SCRIPT_ID_VALID(sid))
2181 return NULL; // not in a script
2182 si = SCRIPT_ITEM(sid);
2183 if (si->sn_autoload_prefix != NULL)
2184 {
2185 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2186 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002187 char_u *namep;
2188
2189 // skip a "<SNR>99_" prefix
2190 namep = untrans_function_name(name);
2191 if (namep == NULL)
2192 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002193
2194 // An exported function in an autoload script is stored as
2195 // "dir#path#name".
2196 if (len < sizeof(buffer))
2197 auto_name = buffer;
2198 else
2199 auto_name = alloc(len);
2200 if (auto_name != NULL)
2201 {
2202 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002203 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002204 hi = hash_find(&func_hashtab, auto_name);
2205 if (auto_name != buffer)
2206 vim_free(auto_name);
2207 if (!HASHITEM_EMPTY(hi))
2208 return HI2UF(hi);
2209 }
2210 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002211
2212 return NULL;
2213}
2214
2215/*
Ernie Rael16409692024-07-31 22:18:11 +02002216 * Find a function by name, return pointer to it.
2217 * The name may be a local script variable, VAR_FUNC. or it may be a fully
2218 * qualified import name such as 'i_imp.FuncName'.
2219 *
2220 * When VAR_FUNC, the import might either direct or autoload.
2221 * When 'i_imp.FuncName' it is direct, autoload is rewritten as i_imp#FuncName
2222 * in f_call and subsequently found.
2223 */
2224 static ufunc_T *
2225find_func_imported(char_u *name, int flags)
2226{
2227 ufunc_T *func = NULL;
2228 char_u *dot = name; // Find a dot, '.', in the name
2229
2230 // Either run into '.' or the end of the string
2231 while (eval_isnamec(*dot))
2232 ++dot;
2233
2234 if (*dot == '.')
2235 {
2236 imported_T *import = find_imported(name, dot - name, FALSE);
2237 if (import != NULL)
2238 func = find_func_with_sid(dot + 1, import->imp_sid);
2239 }
2240 else if (*dot == NUL) // looking at the entire string
2241 {
2242 hashtab_T *ht = get_script_local_ht();
2243 if (ht != NULL)
2244 {
2245 hashitem_T *hi = hash_find(ht, name);
2246 if (!HASHITEM_EMPTY(hi))
2247 {
2248 dictitem_T *di = HI2DI(hi);
Ernie Raelcb90ea92024-08-19 21:45:23 +02002249 if (di->di_tv.v_type == VAR_FUNC
2250 && di->di_tv.vval.v_string != NULL)
Ernie Rael16409692024-07-31 22:18:11 +02002251 func = find_func_even_dead(di->di_tv.vval.v_string, flags);
2252 }
2253 }
2254 }
2255 return func;
2256}
2257
2258/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002259 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002260 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2261 * functions.
2262 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002263 * Return NULL for unknown function.
2264 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002265 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002266find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002267{
2268 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002271 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002272 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002273 // Find script-local function before global one.
2274 if (in_vim9script() && eval_isnamec1(*name)
2275 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002277 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2278 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002279 if (func != NULL)
2280 return func;
2281 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002282 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2283 {
2284 char_u *p = name + 5;
2285 long sid;
2286
2287 // printable "<SNR>123_Name" form
2288 sid = getdigits(&p);
2289 if (*p == '_')
2290 {
2291 func = find_func_with_sid(p + 1, (int)sid);
2292 if (func != NULL)
2293 return func;
2294 }
2295 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002297
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002298 if ((flags & FFED_NO_GLOBAL) == 0)
2299 {
2300 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002301 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002302 if (!HASHITEM_EMPTY(hi))
2303 return HI2UF(hi);
2304 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305
Bram Moolenaarb8822442022-01-11 15:24:05 +00002306 // Find autoload function if this is an autoload script.
Ernie Rael16409692024-07-31 22:18:11 +02002307 func = find_func_with_prefix(name[0] == 's' && name[1] == ':'
Bram Moolenaarb8822442022-01-11 15:24:05 +00002308 ? name + 2 : name, current_sctx.sc_sid);
Ernie Rael16409692024-07-31 22:18:11 +02002309 if (func != NULL)
2310 return func;
2311
2312 // Find a script-local "VAR_FUNC" or i_"imp.Func", so vim9script).
2313 if (in_vim9script())
2314 func = find_func_imported(name, flags);
2315 return func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316}
2317
2318/*
2319 * Find a function by name, return pointer to it in ufuncs.
2320 * "cctx" is passed in a :def function to find imported functions.
2321 * Return NULL for unknown or dead function.
2322 */
2323 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002324find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002326 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002327
2328 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2329 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002330 return NULL;
2331}
2332
2333/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002334 * Return TRUE if "ufunc" is a global function.
2335 */
2336 int
2337func_is_global(ufunc_T *ufunc)
2338{
2339 return ufunc->uf_name[0] != K_SPECIAL;
2340}
2341
2342/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002343 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2344 */
2345 int
2346func_requires_g_prefix(ufunc_T *ufunc)
2347{
2348 return ufunc->uf_name[0] != K_SPECIAL
2349 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002350 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01002351 && !SAFE_isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002352}
2353
2354/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002355 * Copy the function name of "fp" to buffer "buf".
2356 * "buf" must be able to hold the function name plus three bytes.
2357 * Takes care of script-local function names.
2358 */
2359 static void
2360cat_func_name(char_u *buf, ufunc_T *fp)
2361{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002362 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002363 {
2364 STRCPY(buf, "<SNR>");
2365 STRCAT(buf, fp->uf_name + 3);
2366 }
2367 else
2368 STRCPY(buf, fp->uf_name);
2369}
2370
2371/*
2372 * Add a number variable "name" to dict "dp" with value "nr".
2373 */
2374 static void
2375add_nr_var(
2376 dict_T *dp,
2377 dictitem_T *v,
2378 char *name,
2379 varnumber_T nr)
2380{
2381 STRCPY(v->di_key, name);
2382 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002383 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002384 v->di_tv.v_type = VAR_NUMBER;
2385 v->di_tv.v_lock = VAR_FIXED;
2386 v->di_tv.vval.v_number = nr;
2387}
2388
2389/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002390 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002391 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002392 static void
2393free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002394{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002395 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002396
Bram Moolenaarca16c602022-09-06 18:57:08 +01002397 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002398 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002399 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002400
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002401 // When garbage collecting a funccall_T may be freed before the
2402 // function that references it, clear its uf_scoped field.
2403 // The function may have been redefined and point to another
2404 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002405 if (fp != NULL && fp->uf_scoped == fc)
2406 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002407 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002408 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002409
Bram Moolenaarca16c602022-09-06 18:57:08 +01002410 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002411 vim_free(fc);
2412}
2413
2414/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002415 * Free "fc" and what it contains.
2416 * Can be called only when "fc" is kept beyond the period of it called,
2417 * i.e. after cleanup_function_call(fc).
2418 */
2419 static void
2420free_funccal_contents(funccall_T *fc)
2421{
2422 listitem_T *li;
2423
2424 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002425 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002426
2427 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002428 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002429
2430 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002431 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002432 clear_tv(&li->li_tv);
2433
2434 free_funccal(fc);
2435}
2436
2437/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002438 * Handle the last part of returning from a function: free the local hashtable.
2439 * Unless it is still in use by a closure.
2440 */
2441 static void
2442cleanup_function_call(funccall_T *fc)
2443{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002444 int may_free_fc = fc->fc_refcount <= 0;
2445 int free_fc = TRUE;
2446
Bram Moolenaarca16c602022-09-06 18:57:08 +01002447 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002448
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002449 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002450 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2451 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002452 else
2453 free_fc = FALSE;
2454
2455 // If the a:000 list and the l: and a: dicts are not referenced and
2456 // there is no closure using it, we can free the funccall_T and what's
2457 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002458 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2459 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002460 else
2461 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002462 int todo;
2463 hashitem_T *hi;
2464 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002465
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002466 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002467
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002468 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002469 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002470 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002471 {
2472 if (!HASHITEM_EMPTY(hi))
2473 {
2474 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002475 di = HI2DI(hi);
2476 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002477 }
2478 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002479 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002480
Bram Moolenaarca16c602022-09-06 18:57:08 +01002481 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2482 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002483 else
2484 {
2485 listitem_T *li;
2486
2487 free_fc = FALSE;
2488
2489 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002490 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002491 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002492 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002493
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002494 if (free_fc)
2495 free_funccal(fc);
2496 else
2497 {
2498 static int made_copy = 0;
2499
2500 // "fc" is still in use. This can happen when returning "a:000",
2501 // assigning "l:" to a global variable or defining a closure.
2502 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002503 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002504 previous_funccal = fc;
2505
2506 if (want_garbage_collect)
2507 // If garbage collector is ready, clear count.
2508 made_copy = 0;
2509 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002510 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002511 // We have made a lot of copies, worth 4 Mbyte. This can happen
2512 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002513 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002514 // too much memory.
2515 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002516 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002517 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002518 }
2519}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002520
2521/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002522 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2523 */
2524 static int
2525numbered_function(char_u *name)
2526{
Keith Thompson184f71c2024-01-04 21:19:04 +01002527 return SAFE_isdigit(*name)
2528 || (name[0] == 'g' && name[1] == ':' && SAFE_isdigit(name[2]));
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002529}
2530
2531/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002532 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002533 * 1. ordinary names, function defined with :function or :def;
2534 * can start with "<SNR>123_" literally or with K_SPECIAL.
2535 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002536 * For the first we only count the name stored in func_hashtab as a reference,
2537 * using function() does not count as a reference, because the function is
2538 * looked up by name.
2539 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002540 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002541func_name_refcount(char_u *name)
2542{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002543 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002544}
2545
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546/*
2547 * Unreference "fc": decrement the reference count and free it when it
2548 * becomes zero. "fp" is detached from "fc".
2549 * When "force" is TRUE we are exiting.
2550 */
2551 static void
2552funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2553{
2554 funccall_T **pfc;
2555 int i;
2556
2557 if (fc == NULL)
2558 return;
2559
2560 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002561 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2562 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2563 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2564 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002565 {
2566 if (fc == *pfc)
2567 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002568 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002569 free_funccal_contents(fc);
2570 return;
2571 }
2572 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002573 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2574 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2575 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576}
2577
2578/*
2579 * Remove the function from the function hashtable. If the function was
2580 * deleted while it still has references this was already done.
2581 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2582 */
2583 static int
2584func_remove(ufunc_T *fp)
2585{
2586 hashitem_T *hi;
2587
2588 // Return if it was already virtually deleted.
2589 if (fp->uf_flags & FC_DEAD)
2590 return FALSE;
2591
2592 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002593 if (HASHITEM_EMPTY(hi))
2594 return FALSE;
2595
2596 // When there is a def-function index do not actually remove the
2597 // function, so we can find the index when defining the function again.
2598 // Do remove it when it's a copy.
2599 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002601 fp->uf_flags |= FC_DEAD;
2602 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002604 hash_remove(&func_hashtab, hi, "remove function");
2605 fp->uf_flags |= FC_DELETED;
2606 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607}
2608
2609 static void
2610func_clear_items(ufunc_T *fp)
2611{
2612 ga_clear_strings(&(fp->uf_args));
2613 ga_clear_strings(&(fp->uf_def_args));
2614 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002616 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002617 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01002618 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002619
2620 // Increment the refcount of this function to avoid it being freed
2621 // recursively when the partial is freed.
2622 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002623 partial_unref(fp->uf_partial);
2624 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002625 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002626
2627#ifdef FEAT_LUA
2628 if (fp->uf_cb_free != NULL)
2629 {
2630 fp->uf_cb_free(fp->uf_cb_state);
2631 fp->uf_cb_free = NULL;
2632 }
2633
2634 fp->uf_cb_state = NULL;
2635 fp->uf_cb = NULL;
2636#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637#ifdef FEAT_PROFILE
2638 VIM_CLEAR(fp->uf_tml_count);
2639 VIM_CLEAR(fp->uf_tml_total);
2640 VIM_CLEAR(fp->uf_tml_self);
2641#endif
2642}
2643
2644/*
2645 * Free all things that a function contains. Does not free the function
2646 * itself, use func_free() for that.
2647 * When "force" is TRUE we are exiting.
2648 */
2649 static void
2650func_clear(ufunc_T *fp, int force)
2651{
2652 if (fp->uf_cleared)
2653 return;
2654 fp->uf_cleared = TRUE;
2655
2656 // clear this function
2657 func_clear_items(fp);
2658 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002659 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660}
2661
2662/*
2663 * Free a function and remove it from the list of functions. Does not free
2664 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002665 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002666 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002667 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002668 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002669func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670{
2671 // Only remove it when not done already, otherwise we would remove a newer
2672 // version of the function with the same name.
2673 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2674 func_remove(fp);
2675
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002676 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002677 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002678 if (fp->uf_dfunc_idx > 0)
2679 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002680 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002682 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002683 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002684 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685}
2686
2687/*
2688 * Free all things that a function contains and free the function itself.
2689 * When "force" is TRUE we are exiting.
2690 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002691 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692func_clear_free(ufunc_T *fp, int force)
2693{
2694 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002695 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2696 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002697 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002698 else
2699 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002700}
2701
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002702/*
2703 * Copy already defined function "lambda" to a new function with name "global".
2704 * This is for when a compiled function defines a global function.
2705 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002706 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002707copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002708 char_u *lambda,
2709 char_u *global,
2710 loopvarinfo_T *loopvarinfo,
2711 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002712{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002713 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002714 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002715
2716 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002717 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002718 semsg(_(e_lambda_function_not_found_str), lambda);
2719 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002720 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002721
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002722 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002723 if (fp != NULL)
2724 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002725 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002726 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002727 return FAIL;
2728 }
2729
Bram Moolenaare01e5212023-01-08 20:31:18 +00002730 fp = alloc_ufunc(global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002731 if (fp == NULL)
2732 return FAIL;
2733
2734 fp->uf_varargs = ufunc->uf_varargs;
2735 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2736 fp->uf_def_status = ufunc->uf_def_status;
2737 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2738 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2739 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2740 == FAIL
2741 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2742 goto failed;
2743
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002744 if (ufunc->uf_arg_types != NULL)
2745 {
2746 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2747 if (fp->uf_arg_types == NULL)
2748 goto failed;
2749 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2750 sizeof(type_T *) * fp->uf_args.ga_len);
2751 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002752 if (ufunc->uf_va_name != NULL)
2753 {
2754 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2755 if (fp->uf_va_name == NULL)
2756 goto failed;
2757 }
2758 fp->uf_ret_type = ufunc->uf_ret_type;
2759
2760 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002761
2762 fp->uf_name_exp = NULL;
2763 set_ufunc_name(fp, global);
2764
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002765 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002766
2767 // the referenced dfunc_T is now used one more time
2768 link_def_function(fp);
2769
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002770 // Create a partial to store the context of the function where it was
2771 // instantiated. Only needs to be done once. Do this on the original
2772 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002773 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2774 {
2775 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2776
2777 if (pt == NULL)
2778 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002779 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002780 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002781 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002782 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002783 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002784 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002785 }
2786
2787 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002788
2789failed:
2790 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002791 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002792}
2793
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002794static int funcdepth = 0;
2795
2796/*
2797 * Increment the function call depth count.
2798 * Return FAIL when going over 'maxfuncdepth'.
2799 * Otherwise return OK, must call funcdepth_decrement() later!
2800 */
2801 int
2802funcdepth_increment(void)
2803{
2804 if (funcdepth >= p_mfd)
2805 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002806 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002807 return FAIL;
2808 }
2809 ++funcdepth;
2810 return OK;
2811}
2812
2813 void
2814funcdepth_decrement(void)
2815{
2816 --funcdepth;
2817}
2818
2819/*
2820 * Get the current function call depth.
2821 */
2822 int
2823funcdepth_get(void)
2824{
2825 return funcdepth;
2826}
2827
2828/*
2829 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002830 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002831 * times as funcdepth_increment().
2832 */
2833 void
2834funcdepth_restore(int depth)
2835{
2836 funcdepth = depth;
2837}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002838
2839/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002840 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2841 * "rettv".
2842 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2843 * Returns NULL when allocation fails.
2844 */
2845 funccall_T *
2846create_funccal(ufunc_T *fp, typval_T *rettv)
2847{
2848 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2849
2850 if (fc == NULL)
2851 return NULL;
2852 fc->fc_caller = current_funccal;
2853 current_funccal = fc;
2854 fc->fc_func = fp;
2855 func_ptr_ref(fp);
2856 fc->fc_rettv = rettv;
2857 return fc;
2858}
2859
2860/*
2861 * To be called when returning from a compiled function; restores
2862 * current_funccal.
2863 */
2864 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002865remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002866{
2867 funccall_T *fc = current_funccal;
2868
2869 current_funccal = fc->fc_caller;
2870 free_funccal(fc);
2871}
2872
2873/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002874 * Call a user function.
2875 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002876 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002877call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002878 ufunc_T *fp, // pointer to function
2879 int argcount, // nr of args
2880 typval_T *argvars, // arguments
2881 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002882 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002883 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002884{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002885 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002886 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002887 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002888 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002889 funccall_T *fc;
2890 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002891 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002892 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002893 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002894 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002895 int i;
2896 int ai;
2897 int islambda = FALSE;
2898 char_u numbuf[NUMBUFLEN];
2899 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002900 typval_T *tv_to_free[MAX_FUNC_ARGS];
2901 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002902#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002903 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002904#endif
ichizok7e5fe382023-04-15 13:17:50 +01002905 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002906
Bram Moolenaarb2049902021-01-24 12:53:53 +01002907#ifdef FEAT_PROFILE
2908 CLEAR_FIELD(profile_info);
2909#endif
2910
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002911 // If depth of calling is getting too high, don't execute the function.
2912 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002913 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002914 rettv->v_type = VAR_NUMBER;
2915 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002916 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002917 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002918
Bram Moolenaare38eab22019-12-05 21:50:01 +01002919 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002920
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002921 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002922 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00002923 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002924 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002925 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002926 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2927 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002928 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002929 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002930
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002931 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002933#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002934 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002935#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002936 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002937#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002938 if (do_profiling == PROF_YES)
2939 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002940#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002941 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002942 if (call_def_function(fp, argcount, argvars, 0,
2943 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
2944 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002945 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002946#ifdef FEAT_PROFILE
2947 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002948 || (caller != NULL && caller->uf_profiling)))
2949 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002950#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002951 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002952 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002953 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954 }
2955
Bram Moolenaar38453522021-11-28 22:00:12 +00002956 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002957
2958 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002959 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2960 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2961 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002962 */
2963 /*
2964 * Init l: variables.
2965 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002966 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002967 if (selfdict != NULL)
2968 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002969 // Set l:self to "selfdict". Use "name" to avoid a warning from
2970 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002971 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002972 name = v->di_key;
2973 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002974 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002975 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002976 v->di_tv.v_type = VAR_DICT;
2977 v->di_tv.v_lock = 0;
2978 v->di_tv.vval.v_dict = selfdict;
2979 ++selfdict->dv_refcount;
2980 }
2981
2982 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002983 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002984 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002985 * Set a:000 to a list with room for the "..." arguments.
2986 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002987 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002988 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002989 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002990 (varnumber_T)(argcount >= fp->uf_args.ga_len
2991 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002992 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002993 if ((fp->uf_flags & FC_NOARGS) == 0)
2994 {
2995 // Use "name" to avoid a warning from some compiler that checks the
2996 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002997 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002998 name = v->di_key;
2999 STRCPY(name, "000");
3000 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003001 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003002 v->di_tv.v_type = VAR_LIST;
3003 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003004 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003005 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01003006 CLEAR_FIELD(fc->fc_l_varlist);
3007 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
3008 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003009
3010 /*
3011 * Set a:firstline to "firstline" and a:lastline to "lastline".
3012 * Set a:name to named arguments.
3013 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003014 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003015 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003016 if ((fp->uf_flags & FC_NOARGS) == 0)
3017 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003018 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
3019 "firstline", (varnumber_T)funcexe->fe_firstline);
3020 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
3021 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003022 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003023 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003024 {
3025 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003026 typval_T def_rettv;
3027 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003028
3029 ai = i - fp->uf_args.ga_len;
3030 if (ai < 0)
3031 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003032 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003033 name = FUNCARG(fp, i);
3034 if (islambda)
3035 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003036
3037 // evaluate named argument default expression
3038 isdefault = ai + fp->uf_def_args.ga_len >= 0
3039 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
3040 && argvars[i].vval.v_number == VVAL_NONE));
3041 if (isdefault)
3042 {
3043 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003044
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003045 def_rettv.v_type = VAR_NUMBER;
3046 def_rettv.vval.v_number = -1;
3047
3048 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
3049 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003050 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003051 {
3052 default_arg_err = 1;
3053 break;
3054 }
3055 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003056 }
3057 else
3058 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003059 if ((fp->uf_flags & FC_NOARGS) != 0)
3060 // Bail out if no a: arguments used (in lambda).
3061 break;
3062
Bram Moolenaare38eab22019-12-05 21:50:01 +01003063 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003064 sprintf((char *)numbuf, "%d", ai + 1);
3065 name = numbuf;
3066 }
3067 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
3068 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003069 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003070 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003071 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003072 }
3073 else
3074 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003075 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003076 if (v == NULL)
3077 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003078 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003079 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003080
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003081 // Note: the values are copied directly to avoid alloc/free.
3082 // "argvars" must have VAR_FIXED for v_lock.
3083 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003084 v->di_tv.v_lock = VAR_FIXED;
3085
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003086 if (isdefault)
3087 // Need to free this later, no matter where it's stored.
3088 tv_to_free[tv_to_free_len++] = &v->di_tv;
3089
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003090 if (addlocal)
3091 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003092 // Named arguments should be accessed without the "a:" prefix in
3093 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003094 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003095 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003096 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003097 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003098 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003099
3100 if (ai >= 0 && ai < MAX_FUNC_ARGS)
3101 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003102 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003103
3104 li->li_tv = argvars[i];
3105 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003106 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003107 }
3108 }
3109
Bram Moolenaare38eab22019-12-05 21:50:01 +01003110 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003111 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003112
3113 if (fp->uf_flags & FC_SANDBOX)
3114 {
3115 using_sandbox = TRUE;
3116 ++sandbox;
3117 }
3118
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003119 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003120 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003121 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003122 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003123 ++no_wait_return;
3124 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003125
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003126 smsg(_("calling %s"), SOURCING_NAME);
3127 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003128 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003129 char_u buf[MSG_BUF_LEN];
3130 char_u numbuf2[NUMBUFLEN];
3131 char_u *tofree;
3132 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003133
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003134 msg_puts("(");
3135 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003136 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003137 if (i > 0)
3138 msg_puts(", ");
3139 if (argvars[i].v_type == VAR_NUMBER)
3140 msg_outnum((long)argvars[i].vval.v_number);
3141 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003142 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003143 // Do not want errors such as E724 here.
3144 ++emsg_off;
3145 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3146 --emsg_off;
3147 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003148 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003149 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003150 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003151 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3152 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003153 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003154 msg_puts((char *)s);
3155 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003156 }
3157 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003158 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003159 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003160 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003161 msg_puts("\n"); // don't overwrite this either
3162
3163 verbose_leave_scroll();
3164 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003165 }
3166#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003167 if (do_profiling == PROF_YES)
3168 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003169 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003170#endif
3171
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003172 // "legacy" does not apply to commands in the function
3173 sticky_cmdmod_flags = 0;
3174
Bram Moolenaar98aff652022-09-06 21:02:35 +01003175 // If called from a compiled :def function the execution context must be
3176 // hidden, any deferred functions need to be added to the function being
3177 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003178 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003179
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003180 save_current_sctx = current_sctx;
3181 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003182 save_did_emsg = did_emsg;
3183 did_emsg = FALSE;
3184
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003185 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003186 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003187 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003188 retval = FCERR_FAILED;
3189 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003190 else if (islambda)
3191 {
3192 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3193
3194 // A Lambda always has the command "return {expr}". It is much faster
3195 // to evaluate {expr} directly.
3196 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003197 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003198 --ex_nesting_level;
3199 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003200 else
3201 // call do_cmdline() to execute the lines
3202 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003203 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3204
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003205 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003206 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003207
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003208 if (RedrawingDisabled > 0)
3209 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003210
Bram Moolenaare38eab22019-12-05 21:50:01 +01003211 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003212 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3213 {
3214 clear_tv(rettv);
3215 rettv->v_type = VAR_NUMBER;
3216 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003217
3218 // In corner cases returning a "failed" value is not backwards
3219 // compatible. Only do this for Vim9 script.
3220 if (in_vim9script())
3221 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003222 }
3223
3224#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003225 if (do_profiling == PROF_YES)
3226 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003227 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003228
3229 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3230 profile_may_end_func(&profile_info, fp, caller);
3231 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003232#endif
3233
Bram Moolenaare38eab22019-12-05 21:50:01 +01003234 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003235 if (p_verbose >= 12)
3236 {
3237 ++no_wait_return;
3238 verbose_enter_scroll();
3239
3240 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003241 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003242 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003243 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003244 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003245 else
3246 {
3247 char_u buf[MSG_BUF_LEN];
3248 char_u numbuf2[NUMBUFLEN];
3249 char_u *tofree;
3250 char_u *s;
3251
Bram Moolenaare38eab22019-12-05 21:50:01 +01003252 // The value may be very long. Skip the middle part, so that we
3253 // have some idea how it starts and ends. smsg() would always
3254 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003255 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003256 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003257 --emsg_off;
3258 if (s != NULL)
3259 {
3260 if (vim_strsize(s) > MSG_BUF_CLEN)
3261 {
3262 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3263 s = buf;
3264 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003265 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003266 vim_free(tofree);
3267 }
3268 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003269 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003270
3271 verbose_leave_scroll();
3272 --no_wait_return;
3273 }
3274
ichizok7e5fe382023-04-15 13:17:50 +01003275 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003276 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003277 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003278 restore_current_ectx(save_current_ectx);
3279
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003280#ifdef FEAT_PROFILE
3281 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003282 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003283#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003284 if (using_sandbox)
3285 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003286 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003287
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003288 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003289 {
3290 ++no_wait_return;
3291 verbose_enter_scroll();
3292
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003293 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003294 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003295
3296 verbose_leave_scroll();
3297 --no_wait_return;
3298 }
3299
3300 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003301 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003302 for (i = 0; i < tv_to_free_len; ++i)
3303 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003304 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003305
3306 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003307}
3308
3309/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003310 * Check the argument count for user function "fp".
3311 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3312 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003313 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003314check_user_func_argcount(ufunc_T *fp, int argcount)
3315{
3316 int regular_args = fp->uf_args.ga_len;
3317
3318 if (argcount < regular_args - fp->uf_def_args.ga_len)
3319 return FCERR_TOOFEW;
3320 else if (!has_varargs(fp) && argcount > regular_args)
3321 return FCERR_TOOMANY;
3322 return FCERR_UNKNOWN;
3323}
3324
3325/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003327 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003328 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329call_user_func_check(
3330 ufunc_T *fp,
3331 int argcount,
3332 typval_T *argvars,
3333 typval_T *rettv,
3334 funcexe_T *funcexe,
3335 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003336{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003337 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003338
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003339#ifdef FEAT_LUA
3340 if (fp->uf_flags & FC_CFUNC)
3341 {
3342 cfunc_T cb = fp->uf_cb;
3343
3344 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3345 }
3346#endif
3347
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003348 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3349 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003350 error = check_user_func_argcount(fp, argcount);
3351 if (error != FCERR_UNKNOWN)
3352 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003353
Bram Moolenaar50824712020-12-20 21:10:17 +01003354 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003355 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003357 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003359 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 int did_save_redo = FALSE;
3361 save_redo_T save_redo;
3362
3363 /*
3364 * Call the user function.
3365 * Save and restore search patterns, script variables and
3366 * redo buffer.
3367 */
3368 save_search_patterns();
3369 if (!ins_compl_active())
3370 {
3371 saveRedobuff(&save_redo);
3372 did_save_redo = TRUE;
3373 }
3374 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003375 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003376 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3378 // Function was unreferenced while being used, free it now.
3379 func_clear_free(fp, FALSE);
3380 if (did_save_redo)
3381 restoreRedobuff(&save_redo);
3382 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003383 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003384
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003386}
3387
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003388static funccal_entry_T *funccal_stack = NULL;
3389
3390/*
3391 * Save the current function call pointer, and set it to NULL.
3392 * Used when executing autocommands and for ":source".
3393 */
3394 void
3395save_funccal(funccal_entry_T *entry)
3396{
3397 entry->top_funccal = current_funccal;
3398 entry->next = funccal_stack;
3399 funccal_stack = entry;
3400 current_funccal = NULL;
3401}
3402
3403 void
3404restore_funccal(void)
3405{
3406 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003407 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003408 else
3409 {
3410 current_funccal = funccal_stack->top_funccal;
3411 funccal_stack = funccal_stack->next;
3412 }
3413}
3414
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003415 funccall_T *
3416get_current_funccal(void)
3417{
3418 return current_funccal;
3419}
3420
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003421/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003422 * Return TRUE when currently at the script level:
3423 * - not in a function
3424 * - not executing an autocommand
3425 * Note that when an autocommand sources a script the result is FALSE;
3426 */
3427 int
3428at_script_level(void)
3429{
3430 return current_funccal == NULL && autocmd_match == NULL;
3431}
3432
3433/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003434 * Mark all functions of script "sid" as deleted.
3435 */
3436 void
3437delete_script_functions(int sid)
3438{
3439 hashitem_T *hi;
3440 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003441 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003442 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003443 size_t len;
3444
3445 buf[0] = K_SPECIAL;
3446 buf[1] = KS_EXTRA;
3447 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003448 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003449 len = STRLEN(buf);
3450
Bram Moolenaarce658352020-07-31 23:47:12 +02003451 while (todo > 0)
3452 {
3453 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003454 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003455 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003456 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003457 fp = HI2UF(hi);
3458 if (STRNCMP(fp->uf_name, buf, len) == 0)
3459 {
3460 int changed = func_hashtab.ht_changed;
3461
3462 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003463
3464 if (fp->uf_calls > 0)
3465 {
3466 // Function is executing, don't free it but do remove
3467 // it from the hashtable.
3468 if (func_remove(fp))
3469 fp->uf_refcount--;
3470 }
3471 else
3472 {
3473 func_clear(fp, TRUE);
3474 // When clearing a function another function can be
3475 // cleared as a side effect. When that happens start
3476 // over.
3477 if (changed != func_hashtab.ht_changed)
3478 break;
3479 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003480 }
3481 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003482 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003483 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003484}
3485
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003486#if defined(EXITFREE) || defined(PROTO)
3487 void
3488free_all_functions(void)
3489{
3490 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003491 ufunc_T *fp;
3492 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003493 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003494 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003495
Bram Moolenaare38eab22019-12-05 21:50:01 +01003496 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003497 while (current_funccal != NULL)
3498 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003499 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003500 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003501 if (current_funccal == NULL && funccal_stack != NULL)
3502 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003503 }
3504
Bram Moolenaare38eab22019-12-05 21:50:01 +01003505 // First clear what the functions contain. Since this may lower the
3506 // reference count of a function, it may also free a function and change
3507 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003508 while (todo > 0)
3509 {
3510 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003511 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003512 if (!HASHITEM_EMPTY(hi))
3513 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 // clear the def function index now
3515 fp = HI2UF(hi);
3516 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003517 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518
Bram Moolenaare38eab22019-12-05 21:50:01 +01003519 // Only free functions that are not refcounted, those are
3520 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003521 if (func_name_refcount(fp->uf_name))
3522 ++skipped;
3523 else
3524 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003525 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003526 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003527 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003528 {
3529 skipped = 0;
3530 break;
3531 }
3532 }
3533 --todo;
3534 }
3535 }
3536
Bram Moolenaare38eab22019-12-05 21:50:01 +01003537 // Now actually free the functions. Need to start all over every time,
3538 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003539 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003540 while (func_hashtab.ht_used > skipped)
3541 {
3542 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003543 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003544 if (!HASHITEM_EMPTY(hi))
3545 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003546 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003547 // Only free functions that are not refcounted, those are
3548 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003549 fp = HI2UF(hi);
3550 if (func_name_refcount(fp->uf_name))
3551 ++skipped;
3552 else
3553 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003554 if (func_free(fp, FALSE) == OK)
3555 {
3556 skipped = 0;
3557 break;
3558 }
3559 // did not actually free it
3560 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003561 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003562 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003563 }
3564 if (skipped == 0)
3565 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566
3567 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003568}
3569#endif
3570
3571/*
3572 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003573 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3574 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003575 * "len" is the length of "name", or -1 for NUL terminated.
3576 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003578builtin_function(char_u *name, int len)
3579{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003580 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003581
Bram Moolenaara26b9702020-04-18 19:53:28 +02003582 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003583 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003584 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3585 {
3586 if (name[i] == AUTOLOAD_CHAR)
3587 return FALSE;
3588 if (!eval_isnamec(name[i]))
3589 {
3590 // "name.something" is not a builtin function
3591 if (name[i] == '.')
3592 return FALSE;
3593 break;
3594 }
3595 }
3596 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003597}
3598
3599 int
3600func_call(
3601 char_u *name,
3602 typval_T *args,
3603 partial_T *partial,
3604 dict_T *selfdict,
3605 typval_T *rettv)
3606{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003607 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003608 listitem_T *item;
3609 typval_T argv[MAX_FUNC_ARGS + 1];
3610 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003611 int r = 0;
3612
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003613 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003614 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003615 {
3616 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3617 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003618 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003619 break;
3620 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003621 // Make a copy of each argument. This is needed to be able to set
3622 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003623 copy_tv(&item->li_tv, &argv[argc++]);
3624 }
3625
3626 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003627 {
3628 funcexe_T funcexe;
3629
Bram Moolenaara80faa82020-04-12 19:37:17 +02003630 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003631 funcexe.fe_firstline = curwin->w_cursor.lnum;
3632 funcexe.fe_lastline = curwin->w_cursor.lnum;
3633 funcexe.fe_evaluate = TRUE;
3634 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003635 if (partial != NULL)
3636 {
3637 funcexe.fe_object = partial->pt_obj;
3638 if (funcexe.fe_object != NULL)
3639 ++funcexe.fe_object->obj_refcount;
3640 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003641 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003642 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3643 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003644
Bram Moolenaare38eab22019-12-05 21:50:01 +01003645 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003646 while (argc > 0)
3647 clear_tv(&argv[--argc]);
3648
3649 return r;
3650}
3651
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003652static int callback_depth = 0;
3653
3654 int
3655get_callback_depth(void)
3656{
3657 return callback_depth;
3658}
3659
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003660/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003661 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003662 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003663 */
3664 int
3665call_callback(
3666 callback_T *callback,
3667 int len, // length of "name" or -1 to use strlen()
3668 typval_T *rettv, // return value goes here
3669 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003670 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003671 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003672{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003673 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003674 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003675
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003676 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3677 return FAIL;
Christian Brabandt47510f32023-10-15 09:56:16 +02003678
zeertzjqfe583b12023-12-21 16:59:26 +01003679 if (callback_depth > p_mfd)
Christian Brabandt47510f32023-10-15 09:56:16 +02003680 {
3681 emsg(_(e_command_too_recursive));
3682 return FAIL;
3683 }
3684
Bram Moolenaara80faa82020-04-12 19:37:17 +02003685 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003686 funcexe.fe_evaluate = TRUE;
3687 funcexe.fe_partial = callback->cb_partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003688 if (callback->cb_partial != NULL)
3689 {
3690 funcexe.fe_object = callback->cb_partial->pt_obj;
3691 if (funcexe.fe_object != NULL)
3692 ++funcexe.fe_object->obj_refcount;
3693 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003694 ++callback_depth;
3695 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3696 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003697
3698 // When a :def function was called that uses :try an error would be turned
3699 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003700 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003701 {
3702 need_rethrow = FALSE;
3703 handle_did_throw();
3704 }
3705
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003706 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003707}
3708
3709/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003710 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003711 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003712 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3713 */
3714 varnumber_T
3715call_callback_retnr(
3716 callback_T *callback,
3717 int argcount, // number of "argvars"
3718 typval_T *argvars) // vars for arguments, must have "argcount"
3719 // PLUS ONE elements!
3720{
3721 typval_T rettv;
3722 varnumber_T retval;
3723
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003724 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003725 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003726
3727 retval = tv_get_number_chk(&rettv, NULL);
3728 clear_tv(&rettv);
3729 return retval;
3730}
3731
3732/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 * Give an error message for the result of a function.
3734 * Nothing if "error" is FCERR_NONE.
3735 */
3736 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003737user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738{
3739 switch (error)
3740 {
3741 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003742 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003743 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003744 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003745 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746 break;
3747 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003748 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749 break;
3750 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003751 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752 break;
3753 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003754 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003755 break;
3756 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003757 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 break;
3759 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003760 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761 break;
3762 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003763 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3764 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003766 case FCERR_OTHER:
3767 case FCERR_FAILED:
3768 // assume the error message was already given
3769 break;
3770 case FCERR_NONE:
3771 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 }
3773}
3774
3775/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003776 * Check the argument types "argvars[argcount]" for "name" using the
3777 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3778 * is already included in "argvars[]".
3779 * Will do nothing if "funcexe->fe_check_type" is NULL or
3780 * "funcexe->fe_evaluate" is FALSE;
3781 * Returns an FCERR_ value.
3782 */
3783 static funcerror_T
3784may_check_argument_types(
3785 funcexe_T *funcexe,
3786 typval_T *argvars,
3787 int argcount,
3788 int base_included,
3789 char_u *name)
3790{
3791 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3792 {
3793 // Check that the argument types are OK for the types of the funcref.
3794 if (check_argument_types(funcexe->fe_check_type,
3795 argvars, argcount,
3796 base_included ? NULL : funcexe->fe_basetv,
3797 name) == FAIL)
3798 return FCERR_OTHER;
3799 }
3800 return FCERR_NONE;
3801}
3802
3803/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003804 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003805 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003806 * Return FAIL when the function can't be called, OK otherwise.
3807 * Also returns OK when an error was encountered while executing the function.
3808 */
3809 int
3810call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003811 char_u *funcname, // name of the function
3812 int len, // length of "name" or -1 to use strlen()
3813 typval_T *rettv, // return value goes here
3814 int argcount_in, // number of "argvars"
3815 typval_T *argvars_in, // vars for arguments, must have "argcount"
3816 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003817 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003818{
3819 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003820 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003821 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003822 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003823 char_u fname_buf[FLEN_FIXED + 1];
3824 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003825 char_u *fname = NULL;
3826 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003827 int argcount = argcount_in;
3828 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003829 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003830 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003831 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003832 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003833 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003834 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003835 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003836 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003837
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003838 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3839 // even when call_func() returns FAIL.
3840 rettv->v_type = VAR_UNKNOWN;
3841
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003842 if (partial != NULL)
3843 fp = partial->pt_func;
3844 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003845 fp = funcexe->fe_ufunc;
3846
3847 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003848 {
3849 // Make a copy of the name, if it comes from a funcref variable it
3850 // could be changed or deleted in the called function.
3851 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3852 if (name == NULL)
3853 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003854
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003855 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3856 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003857
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003858 if (funcexe->fe_doesrange != NULL)
3859 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003860
3861 if (partial != NULL)
3862 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003863 // When the function has a partial with a dict and there is a dict
3864 // argument, use the dict argument. That is backwards compatible.
3865 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003866 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003867 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003868 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003869 {
3870 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003871 {
3872 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3873 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003874 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003875 goto theend;
3876 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003877 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003878 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003879 for (i = 0; i < argcount_in; ++i)
3880 argv[i + argv_clear] = argvars_in[i];
3881 argvars = argv;
3882 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003883
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003884 if (funcexe->fe_check_type != NULL
3885 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003886 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003887 // Now funcexe->fe_check_type is missing the added arguments,
3888 // make a copy of the type with the correction.
3889 check_type = *funcexe->fe_check_type;
3890 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003891 check_type.tt_args = check_type_args;
3892 CLEAR_FIELD(check_type_args);
3893 for (i = 0; i < check_type.tt_argcount; ++i)
3894 check_type_args[i + partial->pt_argc] =
3895 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003896 check_type.tt_argcount += partial->pt_argc;
3897 check_type.tt_min_argcount += partial->pt_argc;
3898 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003899 }
3900 }
3901
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003902 if (error == FCERR_NONE)
3903 // check the argument types if possible
3904 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
3905 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003906
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003907 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003908 {
3909 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003910 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003911
Bram Moolenaar333894b2020-08-01 18:53:07 +02003912 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003913 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003914 {
3915 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003916 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003917 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003918
Bram Moolenaare38eab22019-12-05 21:50:01 +01003919 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003920 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003921 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003922
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003923 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003924 {
3925 /*
3926 * User defined function.
3927 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003928 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003929 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003930 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003931 if (fp != NULL && !is_global && in_vim9script()
3932 && func_requires_g_prefix(fp))
3933 // In Vim9 script g: is required to find a global
3934 // non-autoload function.
3935 fp = NULL;
3936 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003937
Bram Moolenaare38eab22019-12-05 21:50:01 +01003938 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003939 if (fp == NULL
3940 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003941 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003942 && !aborting())
3943 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003944 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003945 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003946 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003947 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003948 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3949 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003950 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003951 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003952 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003953 if (fp == NULL)
3954 {
3955 char_u *p = untrans_function_name(rfname);
3956
3957 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003958 // Don't do this if the name starts with "s:".
3959 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003960 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003961 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003962
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003963 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003964 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003965 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003966 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003967 int need_arg_check = FALSE;
3968 if (funcexe->fe_check_type == NULL)
3969 {
3970 funcexe->fe_check_type = fp->uf_func_type;
3971 need_arg_check = TRUE;
3972 }
3973
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003974 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003975 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003976 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003977 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003978 argv_clear, fp);
3979 need_arg_check = TRUE;
3980 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003981
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003982 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003983 {
3984 // Method call: base->Method()
3985 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003986 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003987 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003988 argvars = argv;
3989 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003990 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003991 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003992
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003993 // Check the argument types now that the function type and all
3994 // argument values are known, if not done above.
3995 if (need_arg_check)
3996 error = may_check_argument_types(funcexe, argvars, argcount,
3997 TRUE, (name != NULL) ? name : funcname);
3998 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
3999 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004001 }
4002 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004003 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004004 {
4005 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02004006 * expr->method(): Find the method name in the table, call its
4007 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02004008 */
4009 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004010 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004011 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004012 else
4013 {
4014 /*
4015 * Find the function name in the table, call its implementation.
4016 */
4017 error = call_internal_func(fname, argcount, argvars, rettv);
4018 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02004019
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004020 /*
4021 * The function call (or "FuncUndefined" autocommand sequence) might
4022 * have been aborted by an error, an interrupt, or an explicitly thrown
4023 * exception that has not been caught so far. This situation can be
4024 * tested for by calling aborting(). For an error in an internal
4025 * function or for the "E132" error in call_user_func(), however, the
4026 * throw point at which the "force_abort" flag (temporarily reset by
4027 * emsg()) is normally updated has not been reached yet. We need to
4028 * update that flag first to make aborting() reliable.
4029 */
4030 update_force_abort();
4031 }
Bram Moolenaaref140542019-12-31 21:27:13 +01004032 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004033 ret = OK;
4034
Bram Moolenaar4c054e92019-11-10 00:13:50 +01004035theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004036 /*
4037 * Report an error unless the argument evaluation or function call has been
4038 * cancelled due to an aborting error, an interrupt, or an exception.
4039 */
4040 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004041 user_func_error(error, (name != NULL) ? name : funcname,
4042 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004043
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004044 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004045 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004046 clear_tv(&argv[--argv_clear + argv_base]);
4047
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004048 vim_free(tofree);
4049 vim_free(name);
4050
4051 return ret;
4052}
4053
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004054/*
4055 * Call a function without arguments, partial or dict.
4056 * This is like call_func() when the call is only "FuncName()".
4057 * To be used by "expr" options.
4058 * Returns NOTDONE when the function could not be found.
4059 */
4060 int
4061call_simple_func(
4062 char_u *funcname, // name of the function
zeertzjqc1ed7882024-08-01 22:46:54 +02004063 size_t len, // length of "name"
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004064 typval_T *rettv) // return value goes here
4065{
4066 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00004067 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004068 char_u fname_buf[FLEN_FIXED + 1];
4069 char_u *tofree = NULL;
4070 char_u *name;
4071 char_u *fname;
4072 char_u *rfname;
4073 int is_global = FALSE;
4074 ufunc_T *fp;
4075
4076 rettv->v_type = VAR_NUMBER; // default rettv is number zero
4077 rettv->vval.v_number = 0;
4078
4079 // Make a copy of the name, an option can be changed in the function.
4080 name = vim_strnsave(funcname, len);
4081 if (name == NULL)
4082 return ret;
4083
4084 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
4085
4086 // Skip "g:" before a function name.
4087 if (fname[0] == 'g' && fname[1] == ':')
4088 {
4089 is_global = TRUE;
4090 rfname = fname + 2;
4091 }
4092 else
4093 rfname = fname;
4094 fp = find_func(rfname, is_global);
4095 if (fp != NULL && !is_global && in_vim9script()
4096 && func_requires_g_prefix(fp))
4097 // In Vim9 script g: is required to find a global non-autoload
4098 // function.
4099 fp = NULL;
4100 if (fp == NULL)
4101 ret = NOTDONE;
4102 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
4103 error = FCERR_DELETED;
4104 else if (fp != NULL)
4105 {
4106 typval_T argvars[1];
4107 funcexe_T funcexe;
4108
4109 argvars[0].v_type = VAR_UNKNOWN;
4110 CLEAR_FIELD(funcexe);
4111 funcexe.fe_evaluate = TRUE;
4112
4113 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
4114 if (error == FCERR_NONE)
4115 ret = OK;
4116 }
4117
4118 user_func_error(error, name, FALSE);
4119 vim_free(tofree);
4120 vim_free(name);
4121
4122 return ret;
4123}
4124
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004125 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126printable_func_name(ufunc_T *fp)
4127{
4128 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4129}
4130
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004131/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004132 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4133 * TRUE. Otherwise return FALSE.
4134 */
4135 static int
4136function_list_modified(int prev_ht_changed)
4137{
4138 if (prev_ht_changed != func_hashtab.ht_changed)
4139 {
4140 emsg(_(e_function_list_was_modified));
4141 return TRUE;
4142 }
4143 return FALSE;
4144}
4145
4146/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004147 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004148 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004149 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004150list_func_head(ufunc_T *fp, int indent)
4151{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004152 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004153 int j;
4154
4155 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004156
4157 // a timer at the more prompt may have deleted the function
4158 if (function_list_modified(prev_ht_changed))
4159 return FAIL;
4160
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004161 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004162 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004163 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004164 msg_puts("def ");
4165 else
4166 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004167 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004168 msg_putchar('(');
4169 for (j = 0; j < fp->uf_args.ga_len; ++j)
4170 {
4171 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004172 msg_puts(", ");
4173 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174 if (fp->uf_arg_types != NULL)
4175 {
4176 char *tofree;
4177
4178 msg_puts(": ");
4179 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4180 vim_free(tofree);
4181 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004182 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4183 {
4184 msg_puts(" = ");
4185 msg_puts(((char **)(fp->uf_def_args.ga_data))
4186 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4187 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004188 }
4189 if (fp->uf_varargs)
4190 {
4191 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004192 msg_puts(", ");
4193 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004194 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004195 if (fp->uf_va_name != NULL)
4196 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004197 if (!fp->uf_varargs)
4198 {
4199 if (j)
4200 msg_puts(", ");
4201 msg_puts("...");
4202 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004203 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004204 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 {
4206 char *tofree;
4207
4208 msg_puts(": ");
4209 msg_puts(type_name(fp->uf_va_type, &tofree));
4210 vim_free(tofree);
4211 }
4212 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004213 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004214
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004215 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004216 {
4217 if (fp->uf_ret_type != &t_void)
4218 {
4219 char *tofree;
4220
4221 msg_puts(": ");
4222 msg_puts(type_name(fp->uf_ret_type, &tofree));
4223 vim_free(tofree);
4224 }
4225 }
4226 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004227 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004228 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004229 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004230 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004231 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004232 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004233 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004234 msg_clr_eos();
4235 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004236 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004237
4238 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004239}
4240
4241/*
4242 * Get a function name, translating "<SID>" and "<SNR>".
4243 * Also handles a Funcref in a List or Dictionary.
4244 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004245 * Set "*is_global" to TRUE when the function must be global, unless
4246 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004247 * flags:
4248 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004249 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004250 * TFN_QUIET: be quiet
4251 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004252 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004253 * Advances "pp" to just after the function name (if no error).
4254 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004255 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004256trans_function_name(
4257 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004258 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004259 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004260 int flags)
4261{
4262 return trans_function_name_ext(pp, is_global, skip, flags,
4263 NULL, NULL, NULL, NULL);
4264}
4265
4266/*
4267 * trans_function_name() with extra arguments.
4268 * "fdp", "partial", "type" and "ufunc" can be NULL.
4269 */
Yegappan Lakshmanana04003a2024-10-27 21:54:11 +01004270 static char_u *
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004271trans_function_name_ext(
4272 char_u **pp,
4273 int *is_global,
4274 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004275 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004276 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004277 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004278 type_T **type, // return: type of funcref
4279 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004280{
4281 char_u *name = NULL;
4282 char_u *start;
4283 char_u *end;
4284 int lead;
4285 char_u sid_buf[20];
4286 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004288 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004289 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004290 int vim9script = in_vim9script();
4291 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004292
4293 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004294 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004295 start = *pp;
4296
Bram Moolenaare38eab22019-12-05 21:50:01 +01004297 // Check for hard coded <SNR>: already translated function ID (from a user
4298 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004299 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4300 && (*pp)[2] == (int)KE_SNR)
4301 {
4302 *pp += 3;
4303 len = get_id_len(pp) + 3;
4304 return vim_strnsave(start, len);
4305 }
4306
Bram Moolenaare38eab22019-12-05 21:50:01 +01004307 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4308 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004309 lead = eval_fname_script(start);
4310 if (lead > 2)
4311 start += lead;
4312
Bram Moolenaare38eab22019-12-05 21:50:01 +01004313 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004314 end = get_lval(start, NULL, &lv, FALSE, skip,
4315 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004316 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004317 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004318 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004319 {
4320 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004321 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004322 goto theend;
4323 }
4324 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4325 {
4326 /*
4327 * Report an invalid expression in braces, unless the expression
4328 * evaluation has been cancelled due to an aborting error, an
4329 * interrupt, or an exception.
4330 */
4331 if (!aborting())
4332 {
4333 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004334 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004335 }
4336 else
4337 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4338 goto theend;
4339 }
4340
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004341 if (lv.ll_ufunc != NULL)
4342 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004343 if (ufunc != NULL)
4344 *ufunc = lv.ll_ufunc;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004345 name = vim_strsave(lv.ll_ufunc->uf_name);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004346 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004347 goto theend;
4348 }
4349
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004350 if (lv.ll_tv != NULL)
4351 {
4352 if (fdp != NULL)
4353 {
4354 fdp->fd_dict = lv.ll_dict;
4355 fdp->fd_newkey = lv.ll_newkey;
4356 lv.ll_newkey = NULL;
4357 fdp->fd_di = lv.ll_di;
4358 }
4359 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4360 {
4361 name = vim_strsave(lv.ll_tv->vval.v_string);
4362 *pp = end;
4363 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004364 else if (lv.ll_tv->v_type == VAR_CLASS
4365 && lv.ll_tv->vval.v_class != NULL)
4366 {
4367 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4368 *pp = end;
4369 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004370 else if (lv.ll_tv->v_type == VAR_PARTIAL
4371 && lv.ll_tv->vval.v_partial != NULL)
4372 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004373 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004374 *pp = end;
4375 if (partial != NULL)
4376 *partial = lv.ll_tv->vval.v_partial;
4377 }
4378 else
4379 {
4380 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4381 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004382 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004383 else
4384 *pp = end;
4385 name = NULL;
4386 }
4387 goto theend;
4388 }
4389
4390 if (lv.ll_name == NULL)
4391 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004392 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004393 *pp = end;
4394 goto theend;
4395 }
4396
Bram Moolenaare38eab22019-12-05 21:50:01 +01004397 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004398 if (lv.ll_exp_name != NULL)
4399 {
4400 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004401 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004402 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004403 if (name == lv.ll_exp_name)
4404 name = NULL;
4405 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004406 else if (lv.ll_sid > 0)
4407 {
4408 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4409 int cc = *lv.ll_name_end;
4410
4411 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4412 *lv.ll_name_end = NUL;
4413 if (si->sn_autoload_prefix != NULL)
4414 {
4415 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4416 }
4417 else
4418 {
4419 sid_buf[0] = K_SPECIAL;
4420 sid_buf[1] = KS_EXTRA;
4421 sid_buf[2] = (int)KE_SNR;
4422 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004423 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004424 name = concat_str(sid_buf, lv.ll_name);
4425 }
4426 *lv.ll_name_end = cc;
4427 *pp = end;
4428 goto theend;
4429 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004430 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004431 {
4432 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004433 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004434 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004435 if (name == *pp)
4436 name = NULL;
4437 }
4438 if (name != NULL)
4439 {
4440 name = vim_strsave(name);
4441 *pp = end;
4442 if (STRNCMP(name, "<SNR>", 5) == 0)
4443 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004444 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004445 name[0] = K_SPECIAL;
4446 name[1] = KS_EXTRA;
4447 name[2] = (int)KE_SNR;
4448 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4449 }
4450 goto theend;
4451 }
4452
4453 if (lv.ll_exp_name != NULL)
4454 {
4455 len = (int)STRLEN(lv.ll_exp_name);
4456 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4457 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4458 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004459 // When there was "s:" already or the name expanded to get a
4460 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004461 lv.ll_name += 2;
4462 len -= 2;
4463 lead = 2;
4464 }
4465 }
4466 else
4467 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004468 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004469 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004470 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004471 if (lv.ll_name[0] == 'g')
4472 {
4473 if (is_global != NULL)
4474 {
4475 *is_global = TRUE;
4476 }
4477 else
4478 {
4479 // dropping "g:" without setting "is_global" won't work in
4480 // Vim9script, put it back later
4481 prefix_g = TRUE;
4482 extra = 2;
4483 }
4484 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004485 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004486 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004487 len = (int)(end - lv.ll_name);
4488 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004489 if (len <= 0)
4490 {
4491 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004492 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004493 goto theend;
4494 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004495
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004496 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004497 // starts with a lower case character: dict.func(). Or when in a class.
4498 vim9_local = ASCII_ISUPPER(*start) && vim9script
4499 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004501 /*
4502 * Copy the function name to allocated memory.
4503 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4504 * Accept <SNR>123_name() outside a script.
4505 */
4506 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004507 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004508 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004509 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004510 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004511 {
Kota Kato948a3892022-08-16 16:09:59 +01004512 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4513 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004514 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004515 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004516 goto theend;
4517 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004519 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004520 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004521 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004522 || eval_fname_sid(*pp))
4523 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004524 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004525 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004526 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004527 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004528 goto theend;
4529 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004530 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004531 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004532 extra = 3 + (int)STRLEN(sid_buf);
4533 else
4534 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004535 }
4536 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004537 // The function name must start with an upper case letter (unless it is a
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004538 // Vim9 class new() function or a Vim9 class private method or one of the
4539 // supported Vim9 object builtin functions)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004540 else if (!(flags & TFN_INT)
4541 && (builtin_function(lv.ll_name, len)
4542 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004543 && !((flags & TFN_IN_CLASS)
Yegappan Lakshmanand3eae7b2024-03-03 16:26:58 +01004544 && (is_valid_builtin_obj_methodname(lv.ll_name)
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004545 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004546 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004547 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004548 : e_function_name_must_start_with_capital_or_s_str),
4549 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004550 goto theend;
4551 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004552 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004553 {
4554 char_u *cp = vim_strchr(lv.ll_name, ':');
4555
4556 if (cp != NULL && cp < end)
4557 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004558 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004559 goto theend;
4560 }
4561 }
4562
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004564 if (name != NULL)
4565 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004566 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004567 {
4568 name[0] = K_SPECIAL;
4569 name[1] = KS_EXTRA;
4570 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004571 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004572 STRCPY(name + 3, sid_buf);
4573 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004574 else if (prefix_g)
4575 {
4576 name[0] = 'g';
4577 name[1] = ':';
4578 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004579 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4580 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004581 }
4582 *pp = end;
4583
4584theend:
4585 clear_lval(&lv);
4586 return name;
4587}
4588
4589/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004590 * Assuming "name" is the result of trans_function_name() and it was prefixed
4591 * to use the script-local name, return the unmodified name (points into
4592 * "name"). Otherwise return NULL.
4593 * This can be used to first search for a script-local function and fall back
4594 * to the global function if not found.
4595 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004596 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004597untrans_function_name(char_u *name)
4598{
4599 char_u *p;
4600
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004601 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004602 {
4603 p = vim_strchr(name, '_');
4604 if (p != NULL)
4605 return p + 1;
4606 }
4607 return NULL;
4608}
4609
4610/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004611 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4612 * current script ID and returns the expanded function name. The caller should
4613 * free the returned name. If not called from a script context or the function
4614 * name doesn't start with these prefixes, then returns NULL.
4615 * This doesn't check whether the script-local function exists or not.
4616 */
4617 char_u *
4618get_scriptlocal_funcname(char_u *funcname)
4619{
4620 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004621 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004622 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004623 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004624
4625 if (funcname == NULL)
4626 return NULL;
4627
4628 if (STRNCMP(funcname, "s:", 2) != 0
4629 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004630 {
4631 ufunc_T *ufunc;
4632
4633 // The function name does not have a script-local prefix. Try finding
4634 // it when in a Vim9 script and there is no "g:" prefix.
4635 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4636 return NULL;
4637 ufunc = find_func(funcname, FALSE);
4638 if (ufunc == NULL || func_is_global(ufunc)
4639 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4640 return NULL;
4641 ++p;
4642 off = 0;
4643 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004644 else
4645 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004646
4647 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4648 {
4649 emsg(_(e_using_sid_not_in_script_context));
4650 return NULL;
4651 }
4652 // Expand s: prefix into <SNR>nr_<name>
4653 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4654 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004655 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004656 if (newname == NULL)
4657 return NULL;
4658 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004659 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004660
4661 return newname;
4662}
4663
4664/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004665 * Return script-local "fname" with the 3-byte sequence replaced by
4666 * printable <SNR> in allocated memory.
4667 */
4668 char_u *
4669alloc_printable_func_name(char_u *fname)
4670{
4671 char_u *n = alloc(STRLEN(fname + 3) + 6);
4672
4673 if (n != NULL)
4674 {
4675 STRCPY(n, "<SNR>");
4676 STRCPY(n + 5, fname + 3);
4677 }
4678 return n;
4679}
4680
4681/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004682 * Call trans_function_name(), except that a lambda is returned as-is.
4683 * Returns the name in allocated memory.
4684 */
4685 char_u *
4686save_function_name(
4687 char_u **name,
4688 int *is_global,
4689 int skip,
4690 int flags,
4691 funcdict_T *fudi)
4692{
4693 char_u *p = *name;
4694 char_u *saved;
4695
4696 if (STRNCMP(p, "<lambda>", 8) == 0)
4697 {
4698 p += 8;
4699 (void)getdigits(&p);
4700 saved = vim_strnsave(*name, p - *name);
4701 if (fudi != NULL)
4702 CLEAR_POINTER(fudi);
4703 }
4704 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004705 saved = trans_function_name_ext(&p, is_global, skip,
4706 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004707 *name = p;
4708 return saved;
4709}
4710
4711/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004712 * List functions. When "regmatch" is NULL all of then.
4713 * Otherwise functions matching "regmatch".
4714 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004715 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004716list_functions(regmatch_T *regmatch)
4717{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004718 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004719 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004720 hashitem_T *hi;
4721
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004722 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004723 {
4724 if (!HASHITEM_EMPTY(hi))
4725 {
4726 ufunc_T *fp = HI2UF(hi);
4727
4728 --todo;
4729 if ((fp->uf_flags & FC_DEAD) == 0
4730 && (regmatch == NULL
4731 ? !message_filtered(fp->uf_name)
4732 && !func_name_refcount(fp->uf_name)
Keith Thompson184f71c2024-01-04 21:19:04 +01004733 : !SAFE_isdigit(*fp->uf_name)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004734 && vim_regexec(regmatch, fp->uf_name, 0)))
4735 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004736 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004737 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004738 if (function_list_modified(prev_ht_changed))
4739 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004740 }
4741 }
4742 }
4743}
4744
4745/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004746 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004747 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4748 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004749 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004750 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4751 * With CF_INTERFACE the function is define inside an interface, only the
4752 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004753 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004754 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004755 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004756define_function(
4757 exarg_T *eap,
4758 char_u *name_arg,
4759 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004760 int class_flags,
4761 ocmember_T *obj_members,
4762 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004763{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004764 int j;
4765 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004766 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004767 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004768 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004769 char_u *p;
4770 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004771 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004772 char_u *line_arg = NULL;
4773 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004774 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004775 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004776 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004777 garray_T newlines;
4778 int varargs = FALSE;
4779 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004780 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004781 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004782 int fp_allocated = FALSE;
4783 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004784 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004785 dictitem_T *v;
4786 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004787 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004788 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004789 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004790 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004791 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004792 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004793
4794 /*
4795 * ":function" without argument: list functions.
4796 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004797 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004798 {
4799 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004800 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004801 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004802 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004803 }
4804
4805 /*
4806 * ":function /pat": list functions matching pattern.
4807 */
4808 if (*eap->arg == '/')
4809 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004810 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004811 if (!eap->skip)
4812 {
4813 regmatch_T regmatch;
4814
4815 c = *p;
4816 *p = NUL;
4817 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4818 *p = c;
4819 if (regmatch.regprog != NULL)
4820 {
4821 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004822 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004823 vim_regfree(regmatch.regprog);
4824 }
4825 }
4826 if (*p == '/')
4827 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004828 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004829 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004830 }
4831
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832 ga_init(&newargs);
4833 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004834 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004835 ga_init(&default_args);
4836
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004837 /*
4838 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004839 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004840 * "name" == func, "fudi.fd_dict" == NULL
4841 * dict.func new dictionary entry
4842 * "name" == NULL, "fudi.fd_dict" set,
4843 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4844 * dict.func existing dict entry with a Funcref
4845 * "name" == func, "fudi.fd_dict" set,
4846 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4847 * dict.func existing dict entry that's not a Funcref
4848 * "name" == NULL, "fudi.fd_dict" set,
4849 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4850 * s:func script-local function name
4851 * g:func global function name, same as "func"
4852 */
4853 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004854 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004855 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004856 // nested function, argument is (args).
4857 paren = TRUE;
4858 CLEAR_FIELD(fudi);
4859 }
4860 else
4861 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004862 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004863 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004864 if (p[0] == 's' && p[1] == ':')
4865 {
4866 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4867 return NULL;
4868 }
4869 p = to_name_end(p, TRUE);
4870 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4871 {
4872 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4873 eap->arg);
4874 return NULL;
4875 }
4876 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004877 }
4878
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004879 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004880 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004881 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004882 paren = (vim_strchr(p, '(') != NULL);
4883 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004884 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004885 /*
4886 * Return on an invalid expression in braces, unless the expression
4887 * evaluation has been cancelled due to an aborting error, an
4888 * interrupt, or an exception.
4889 */
4890 if (!aborting())
4891 {
4892 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004893 semsg(_(e_key_not_present_in_dictionary_str),
4894 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004895 vim_free(fudi.fd_newkey);
4896 return NULL;
4897 }
4898 else
4899 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004900 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004901
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004902 // For "export def FuncName()" in an autoload script the function name
4903 // is stored with the legacy autoload name "dir#script#FuncName" so
4904 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004905 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004906 {
4907 char_u *prefixed = may_prefix_autoload(name);
4908
4909 if (prefixed != NULL && prefixed != name)
4910 {
4911 vim_free(name);
4912 name = prefixed;
4913 }
4914 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004915 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004916 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004917 {
4918 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4919 goto ret_free;
4920 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004921 }
4922
Bram Moolenaare38eab22019-12-05 21:50:01 +01004923 // An error in a function call during evaluation of an expression in magic
4924 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004925 saved_did_emsg = did_emsg;
4926 did_emsg = FALSE;
4927
4928 /*
4929 * ":function func" with only function name: list function.
4930 */
4931 if (!paren)
4932 {
4933 if (!ends_excmd(*skipwhite(p)))
4934 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004935 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004936 goto ret_free;
4937 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004938 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004939 if (eap->nextcmd != NULL)
4940 *p = NUL;
4941 if (!eap->skip && !got_int)
4942 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004943 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004944 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4945 {
4946 char_u *up = untrans_function_name(name);
4947
4948 // With Vim9 script the name was made script-local, if not
4949 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004950 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004951 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004952 }
4953
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004954 if (fp != NULL)
4955 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004956 // Check no function was added or removed from a timer, e.g. at
4957 // the more prompt. "fp" may then be invalid.
4958 int prev_ht_changed = func_hashtab.ht_changed;
4959
4960 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004961 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004962 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4963 {
4964 if (FUNCLINE(fp, j) == NULL)
4965 continue;
4966 msg_putchar('\n');
4967 msg_outnum((long)(j + 1));
4968 if (j < 9)
4969 msg_putchar(' ');
4970 if (j < 99)
4971 msg_putchar(' ');
4972 if (function_list_modified(prev_ht_changed))
4973 break;
4974 msg_prt_line(FUNCLINE(fp, j), FALSE);
4975 out_flush(); // show a line at a time
4976 ui_breakcheck();
4977 }
4978 if (!got_int)
4979 {
4980 msg_putchar('\n');
4981 if (!function_list_modified(prev_ht_changed))
4982 {
4983 if (fp->uf_def_status != UF_NOT_COMPILED)
4984 msg_puts(" enddef");
4985 else
4986 msg_puts(" endfunction");
4987 }
4988 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004989 }
4990 }
4991 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004992 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004993 }
4994 goto ret_free;
4995 }
4996
4997 /*
4998 * ":function name(arg1, arg2)" Define function.
4999 */
5000 p = skipwhite(p);
5001 if (*p != '(')
5002 {
5003 if (!eap->skip)
5004 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005005 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005006 goto ret_free;
5007 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005008 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005009 if (vim_strchr(p, '(') != NULL)
5010 p = vim_strchr(p, '(');
5011 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005012
Bram Moolenaar4efd9942021-01-24 21:14:20 +01005013 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
5014 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01005015 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01005016 goto ret_free;
5017 }
5018
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02005019 // In Vim9 script only global functions can be redefined.
5020 if (vim9script && eap->forceit && !is_global)
5021 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005022 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02005023 goto ret_free;
5024 }
5025
Bram Moolenaar04935fb2022-01-08 16:19:22 +00005026 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005027
Bram Moolenaar04b12692020-05-04 23:24:44 +02005028 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005029 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005030 // Check the name of the function. Unless it's a dictionary function
5031 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005032 if (name != NULL)
5033 arg = name;
5034 else
5035 arg = fudi.fd_newkey;
5036 if (arg != NULL && (fudi.fd_di == NULL
5037 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
5038 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
5039 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00005040 char_u *name_base = arg;
5041 int i;
5042
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005043 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005044 {
5045 name_base = vim_strchr(arg, '_');
5046 if (name_base == NULL)
5047 name_base = arg + 3;
5048 else
5049 ++name_base;
5050 }
5051 for (i = 0; name_base[i] != NUL && (i == 0
5052 ? eval_isnamec1(name_base[i])
5053 : eval_isnamec(name_base[i])); ++i)
5054 ;
5055 if (name_base[i] != NUL)
zeertzjq6a04bf52024-03-16 09:39:06 +01005056 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005057 emsg_funcname(e_invalid_argument_str, arg);
zeertzjq6a04bf52024-03-16 09:39:06 +01005058 goto ret_free;
5059 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00005060
5061 // In Vim9 script a function cannot have the same name as a
5062 // variable.
5063 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00005064 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
5065 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00005066 + EVAL_VAR_NO_FUNC) == OK)
5067 {
5068 semsg(_(e_redefining_script_item_str), name_base);
5069 goto ret_free;
5070 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005071 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005072 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005073 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00005074 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00005075 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00005076 goto ret_free;
5077 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005078 }
5079
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005080 // This may get more lines and make the pointers into the first line
5081 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01005082 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005083 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00005084 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02005085 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01005086 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00005087 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005088 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01005089 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005090
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005091 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005092 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005093 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005094 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005095 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02005096 if (*p != ':')
5097 {
5098 semsg(_(e_no_white_space_allowed_before_colon_str), p);
5099 p = skipwhite(p);
5100 }
5101 else if (!IS_WHITE_OR_NUL(p[1]))
5102 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005103 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02005104 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005105 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005106 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005107 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01005108 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005109 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005110 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005111 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005112 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005113 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02005114 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005115 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005116 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02005117 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005118 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005119 else
5120 // find extra arguments "range", "dict", "abort" and "closure"
5121 for (;;)
5122 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01005123 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005124 p = skipwhite(p);
5125 if (STRNCMP(p, "range", 5) == 0)
5126 {
5127 flags |= FC_RANGE;
5128 p += 5;
5129 }
5130 else if (STRNCMP(p, "dict", 4) == 0)
5131 {
5132 flags |= FC_DICT;
5133 p += 4;
5134 }
5135 else if (STRNCMP(p, "abort", 5) == 0)
5136 {
5137 flags |= FC_ABORT;
5138 p += 5;
5139 }
5140 else if (STRNCMP(p, "closure", 7) == 0)
5141 {
5142 flags |= FC_CLOSURE;
5143 p += 7;
5144 if (current_funccal == NULL)
5145 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005146 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005147 name == NULL ? (char_u *)"" : name);
5148 goto erret;
5149 }
5150 }
5151 else
5152 break;
5153 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005154
Bram Moolenaare38eab22019-12-05 21:50:01 +01005155 // When there is a line break use what follows for the function body.
5156 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005157 if (*p == '\n')
5158 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005159 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005160 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5161 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005162 && !(VIM_ISWHITE(*whitep) && *p == '#'
5163 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005164 && !eap->skip
5165 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005166 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005167
5168 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005169 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5170 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005171 */
5172 if (KeyTyped)
5173 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005174 // Check if the function already exists, don't let the user type the
5175 // whole function before telling him it doesn't work! For a script we
5176 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005177 if (!eap->skip && !eap->forceit)
5178 {
5179 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005180 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005181 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005182 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005183 }
5184
5185 if (!eap->skip && did_emsg)
5186 goto erret;
5187
Bram Moolenaare38eab22019-12-05 21:50:01 +01005188 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005189 cmdline_row = msg_row;
5190 }
5191
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005192 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005193 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005194
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005195 // Do not define the function when getting the body fails and when
5196 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005197 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005198 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005199 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5200 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005201 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005202 goto erret;
5203
5204 /*
5205 * If there are no errors, add the function
5206 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005207 if (fudi.fd_dict != NULL)
5208 {
5209 char numbuf[20];
5210
5211 fp = NULL;
5212 if (fudi.fd_newkey == NULL && !eap->forceit)
5213 {
5214 emsg(_(e_dictionary_entry_already_exists));
5215 goto erret;
5216 }
5217 if (fudi.fd_di == NULL)
5218 {
5219 // Can't add a function to a locked dictionary
5220 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5221 goto erret;
5222 }
5223 // Can't change an existing function if it is locked
5224 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5225 goto erret;
5226
5227 // Give the function a sequential number. Can only be used with a
5228 // Funcref!
5229 vim_free(name);
5230 sprintf(numbuf, "%d", ++func_nr);
5231 name = vim_strsave((char_u *)numbuf);
5232 if (name == NULL)
5233 goto erret;
5234 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005235 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005236 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005237 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005238 char_u *find_name = name;
5239 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005240 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005241
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005242 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005243 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005244 var_conflict = TRUE;
5245
5246 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5247 {
5248 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5249
5250 if (si->sn_autoload_prefix != NULL)
5251 {
5252 if (is_export)
5253 {
5254 find_name = name + STRLEN(si->sn_autoload_prefix);
5255 v = find_var(find_name, &ht, TRUE);
5256 if (v != NULL)
5257 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005258 // Only check if the function already exists in the script,
5259 // global functions can be shadowed.
5260 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005261 }
5262 else
5263 {
5264 char_u *prefixed = may_prefix_autoload(name);
5265
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005266 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005267 {
5268 v = find_var(prefixed, &ht, TRUE);
5269 if (v != NULL)
5270 var_conflict = TRUE;
5271 vim_free(prefixed);
5272 }
5273 }
5274 }
5275 }
5276 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005277 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005278 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005279 goto erret;
5280 }
5281
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005282 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005283 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005284 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005285 char_u *uname = untrans_function_name(name);
5286
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005287 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005288 }
5289
5290 if (fp != NULL || import != NULL)
5291 {
5292 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005293
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005294 // Function can be replaced with "function!" and when sourcing the
5295 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005296 // A name that is used by an import can not be overruled.
5297 if (import != NULL
5298 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005299 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005300 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005301 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005302 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005303 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005304 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005305 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005306 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005307 goto erret;
5308 }
5309 if (fp->uf_calls > 0)
5310 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005311 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005312 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005313 goto erret;
5314 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005315 if (fp->uf_refcount > 1)
5316 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005317 // This function is referenced somewhere, don't redefine it but
5318 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005319 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005320 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005321 fp = NULL;
5322 overwrite = TRUE;
5323 }
5324 else
5325 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005326 char_u *exp_name = fp->uf_name_exp;
5327
5328 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005329 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005330 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005331 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005332 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005333 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005334#ifdef FEAT_PROFILE
5335 fp->uf_profiling = FALSE;
5336 fp->uf_prof_initialized = FALSE;
5337#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005338 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005339 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005340 }
5341 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005342
5343 if (fp == NULL)
5344 {
5345 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5346 {
5347 int slen, plen;
5348 char_u *scriptname;
5349
Bram Moolenaare38eab22019-12-05 21:50:01 +01005350 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005351 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005352 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005353 {
5354 scriptname = autoload_name(name);
5355 if (scriptname != NULL)
5356 {
5357 p = vim_strchr(scriptname, '/');
5358 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005359 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005360 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005361 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005362 j = OK;
5363 vim_free(scriptname);
5364 }
5365 }
5366 if (j == FAIL)
5367 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005368 linenr_T save_lnum = SOURCING_LNUM;
5369
5370 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005371 semsg(_(e_function_name_does_not_match_script_file_name_str),
5372 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005373 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005374 goto erret;
5375 }
5376 }
5377
Bram Moolenaare01e5212023-01-08 20:31:18 +00005378 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005379 if (fp == NULL)
5380 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005381 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005382
5383 if (fudi.fd_dict != NULL)
5384 {
5385 if (fudi.fd_di == NULL)
5386 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005387 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005388 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5389 if (fudi.fd_di == NULL)
5390 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005391 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005392 goto erret;
5393 }
5394 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5395 {
5396 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005397 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005398 goto erret;
5399 }
5400 }
5401 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005402 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005403 clear_tv(&fudi.fd_di->di_tv);
5404 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005405 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005406
Bram Moolenaare38eab22019-12-05 21:50:01 +01005407 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005408 flags |= FC_DICT;
5409 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005410 }
5411 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005412 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005413 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005414 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005415
5416 if (eap->cmdidx == CMD_def)
5417 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005418 int lnum_save = SOURCING_LNUM;
5419 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005420
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005421 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005422
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005423 // error messages are for the first function line
5424 SOURCING_LNUM = sourcing_lnum_top;
5425
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005426 // The function may use script variables from the context.
5427 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005428
h-eastb895b0f2023-09-24 15:46:31 +02005429 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5430 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005431 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005432 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005433 free_fp = fp_allocated;
5434 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005435 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005436 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005437
5438 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005439 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005440 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005441 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005442 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005443 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005444 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005445 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005446 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005447 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005448 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005450 if (fp_allocated)
5451 {
5452 // insert the new function in the function list
5453 set_ufunc_name(fp, name);
5454 if (overwrite)
5455 {
5456 hi = hash_find(&func_hashtab, name);
5457 hi->hi_key = UF2HIKEY(fp);
5458 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005459 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005460 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005461 {
5462 free_fp = TRUE;
5463 goto erret;
5464 }
5465 fp->uf_refcount = 1;
5466 }
5467
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005468 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005469 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005470 if ((flags & FC_CLOSURE) != 0)
5471 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005472 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005473 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005474 }
5475 else
5476 fp->uf_scoped = NULL;
5477
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005478#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005479 if (prof_def_func())
5480 func_do_profile(fp);
5481#endif
5482 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005483 if (sandbox)
5484 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005485 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005486 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005487 fp->uf_flags = flags;
5488 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005489 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005490 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005491 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005492 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005493 if (is_export)
5494 {
5495 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005496 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005497 is_export = FALSE;
5498 }
5499
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005500 if (eap->cmdidx == CMD_def)
5501 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005502 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5503 // :func does not use Vim9 script syntax, even in a Vim9 script file
5504 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005505
Yegappan Lakshmanan5715a722024-05-03 18:24:07 +02005506 // If test_override('defcompile' 1) is used, then compile the function now
5507 if (eap->cmdidx == CMD_def && override_defcompile)
5508 defcompile_function(fp, NULL);
5509
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005510 goto ret_free;
5511
5512erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005513 if (fp != NULL)
5514 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005515 // these were set to "newargs" and "default_args", which are cleared
5516 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005517 ga_init(&fp->uf_args);
5518 ga_init(&fp->uf_def_args);
5519 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005520errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005521 ga_clear_strings(&newargs);
5522 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005523 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005524 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005525 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005526 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005527 VIM_CLEAR(fp->uf_va_name);
Christian Brabandt0f287912023-12-11 17:53:25 +01005528 clear_func_type_list(&fp->uf_type_list, &fp->uf_func_type);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005529 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005530 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005531 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005532ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005533 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005534 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005535 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005536 if (name != name_arg)
5537 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005538 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005539 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005540
5541 return fp;
5542}
5543
5544/*
5545 * ":function"
5546 */
5547 void
5548ex_function(exarg_T *eap)
5549{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005550 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005551
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005552 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005553 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005554 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005555}
5556
LemonBoy48b7d052024-07-09 18:24:59 +02005557 int
5558get_func_arity(char_u *name, int *required, int *optional, int *varargs)
5559{
5560 ufunc_T *ufunc = NULL;
5561 int argcount = 0;
5562 int min_argcount = 0;
5563 int idx;
5564
5565 idx = find_internal_func(name);
5566 if (idx >= 0)
5567 {
5568 internal_func_get_argcount(idx, &argcount, &min_argcount);
5569 *varargs = FALSE;
5570 }
5571 else
5572 {
5573 char_u fname_buf[FLEN_FIXED + 1];
5574 char_u *tofree = NULL;
5575 funcerror_T error = FCERR_NONE;
5576 char_u *fname;
5577
5578 // May need to translate <SNR>123_ to K_SNR.
5579 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
5580 if (error == FCERR_NONE)
5581 ufunc = find_func(fname, FALSE);
5582 vim_free(tofree);
5583
5584 if (ufunc == NULL)
5585 return FAIL;
5586
5587 argcount = ufunc->uf_args.ga_len;
5588 min_argcount = ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len;
5589 *varargs = has_varargs(ufunc);
5590 }
5591
5592 *required = min_argcount;
5593 *optional = argcount - min_argcount;
5594
5595 return OK;
5596}
5597
Bram Moolenaar822ba242020-05-24 23:00:18 +02005598/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005599 * Find a function by name, including "<lambda>123".
5600 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005601 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005602 * Return NULL if not found.
5603 */
5604 ufunc_T *
5605find_func_by_name(char_u *name, compiletype_T *compile_type)
5606{
5607 char_u *arg = name;
5608 char_u *fname;
5609 ufunc_T *ufunc;
5610 int is_global = FALSE;
5611
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005612 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5613 {
5614 *compile_type = CT_PROFILE;
5615 arg = skipwhite(arg + 7);
5616 }
5617 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5618 {
5619 *compile_type = CT_DEBUG;
5620 arg = skipwhite(arg + 5);
5621 }
5622
5623 if (STRNCMP(arg, "<lambda>", 8) == 0)
5624 {
5625 arg += 8;
5626 (void)getdigits(&arg);
5627 fname = vim_strnsave(name, arg - name);
5628 }
5629 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005630 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005631 // First try finding a method in a class, trans_function_name() will
5632 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005633 ufunc = find_class_func(&arg);
5634 if (ufunc != NULL)
5635 return ufunc;
5636
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005637 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005638 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005639 NULL, NULL, NULL, &ufunc);
5640 if (ufunc != NULL)
5641 {
5642 vim_free(fname);
5643 return ufunc;
5644 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005645 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005646 if (fname == NULL)
5647 {
5648 semsg(_(e_invalid_argument_str), name);
5649 return NULL;
5650 }
5651 if (!ends_excmd2(name, arg))
5652 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005653 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005654 emsg(ex_errmsg(e_trailing_characters_str, arg));
5655 return NULL;
5656 }
5657
5658 ufunc = find_func(fname, is_global);
5659 if (ufunc == NULL)
5660 {
5661 char_u *p = untrans_function_name(fname);
5662
5663 if (p != NULL)
5664 // Try again without making it script-local.
5665 ufunc = find_func(p, FALSE);
5666 }
5667 vim_free(fname);
5668 if (ufunc == NULL)
5669 semsg(_(e_cannot_find_function_str), name);
5670 return ufunc;
5671}
5672
5673/*
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005674 * Compile the :def function "ufunc". If "cl" is not NULL, then compile the
5675 * class or object method "ufunc" in "cl".
5676 */
5677 void
5678defcompile_function(ufunc_T *ufunc, class_T *cl)
5679{
5680 compiletype_T compile_type = CT_NONE;
5681
5682 if (func_needs_compiling(ufunc, compile_type))
5683 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5684 else
5685 smsg(_("Function %s%s%s does not need compiling"),
5686 cl != NULL ? cl->class_name : (char_u *)"",
5687 cl != NULL ? (char_u *)"." : (char_u *)"",
5688 ufunc->uf_name);
5689}
5690
5691/*
5692 * Compile all the :def functions defined in the current script
5693 */
5694 static void
5695defcompile_funcs_in_script(void)
5696{
5697 long todo = (long)func_hashtab.ht_used;
5698 int changed = func_hashtab.ht_changed;
5699 hashitem_T *hi;
5700
5701 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5702 {
5703 if (!HASHITEM_EMPTY(hi))
5704 {
5705 --todo;
5706 ufunc_T *ufunc = HI2UF(hi);
5707 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5708 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5709 && (ufunc->uf_flags & FC_DEAD) == 0)
5710 {
5711 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5712
5713 if (func_hashtab.ht_changed != changed)
5714 {
5715 // a function has been added or removed, need to start
5716 // over
5717 todo = (long)func_hashtab.ht_used;
5718 changed = func_hashtab.ht_changed;
5719 hi = func_hashtab.ht_array;
5720 --hi;
5721 }
5722 }
5723 }
5724 }
5725}
5726
5727/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005728 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005729 * be compiled or the one specified by the argument.
5730 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005731 */
5732 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005733ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005734{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005735 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005736 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005737 typval_T tv;
5738
5739 if (is_class_name(eap->arg, &tv))
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005740 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005741 class_T *cl = tv.vval.v_class;
5742
5743 if (cl != NULL)
5744 defcompile_class(cl);
5745 }
5746 else
5747 {
5748 compiletype_T compile_type = CT_NONE;
5749 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
5750 if (ufunc != NULL)
5751 defcompile_function(ufunc, NULL);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005752 }
5753 }
5754 else
5755 {
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005756 defcompile_funcs_in_script();
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005757
Yegappan Lakshmanan4f32c832024-01-12 17:36:40 +01005758 // compile all the class defined in the current script
5759 defcompile_classes_in_script();
Bram Moolenaar822ba242020-05-24 23:00:18 +02005760 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005761}
5762
5763/*
5764 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5765 * Return 2 if "p" starts with "s:".
5766 * Return 0 otherwise.
5767 */
5768 int
5769eval_fname_script(char_u *p)
5770{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005771 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5772 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005773 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5774 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5775 return 5;
5776 if (p[0] == 's' && p[1] == ':')
5777 return 2;
5778 return 0;
5779}
5780
5781 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005782translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005783{
5784 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005785 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005786 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005787}
5788
5789/*
5790 * Return TRUE when "ufunc" has old-style "..." varargs
5791 * or named varargs "...name: type".
5792 */
5793 int
5794has_varargs(ufunc_T *ufunc)
5795{
5796 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005797}
5798
5799/*
5800 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005801 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005802 */
5803 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005804function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005805{
5806 char_u *nm = name;
5807 char_u *p;
5808 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005809 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005810 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005811
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005812 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5813 if (no_deref)
5814 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005815 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005816 nm = skipwhite(nm);
5817
Bram Moolenaare38eab22019-12-05 21:50:01 +01005818 // Only accept "funcname", "funcname ", "funcname (..." and
5819 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005820 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005821 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005822 vim_free(p);
5823 return n;
5824}
5825
Bram Moolenaar113e1072019-01-20 15:30:40 +01005826#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005827 char_u *
5828get_expanded_name(char_u *name, int check)
5829{
5830 char_u *nm = name;
5831 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005832 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005833
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005834 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005835
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005836 if (p != NULL && *nm == NUL
5837 && (!check || translated_function_exists(p, is_global)))
5838 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005839
5840 vim_free(p);
5841 return NULL;
5842}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005843#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005844
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005845/*
5846 * Function given to ExpandGeneric() to obtain the list of user defined
5847 * function names.
5848 */
5849 char_u *
5850get_user_func_name(expand_T *xp, int idx)
5851{
5852 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005853 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005854 static hashitem_T *hi;
5855 ufunc_T *fp;
5856
5857 if (idx == 0)
5858 {
5859 done = 0;
5860 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005861 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005862 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005863 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005864 {
5865 if (done++ > 0)
5866 ++hi;
5867 while (HASHITEM_EMPTY(hi))
5868 ++hi;
5869 fp = HI2UF(hi);
5870
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005871 // don't show dead, dict and lambda functions
5872 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005873 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005874 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005875
5876 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005877 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005878
5879 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005880 if (xp->xp_context != EXPAND_USER_FUNC
5881 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005882 {
5883 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005884 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005885 STRCAT(IObuff, ")");
5886 }
5887 return IObuff;
5888 }
5889 return NULL;
5890}
5891
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005892/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005893 * Make a copy of a function.
5894 * Intended to be used for a function defined on a base class that has a copy
5895 * on the child class.
5896 * The copy has uf_refcount set to one.
5897 * Returns NULL when out of memory.
5898 */
5899 ufunc_T *
5900copy_function(ufunc_T *fp)
5901{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005902 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005903 if (ufunc == NULL)
5904 return NULL;
5905
5906 // Most things can just be copied.
5907 *ufunc = *fp;
5908
5909 ufunc->uf_def_status = UF_TO_BE_COMPILED;
5910 ufunc->uf_dfunc_idx = 0;
5911 ufunc->uf_class = NULL;
5912
5913 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
5914 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
5915
5916 if (ufunc->uf_arg_types != NULL)
5917 {
5918 // "uf_arg_types" is an allocated array, make a copy.
5919 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
5920 if (at != NULL)
5921 {
5922 mch_memmove(at, ufunc->uf_arg_types,
5923 sizeof(type_T *) * ufunc->uf_args.ga_len);
5924 ufunc->uf_arg_types = at;
5925 }
5926 }
5927
5928 // TODO: how about the types themselves? they can be freed when the
5929 // original function is freed:
5930 // type_T **uf_arg_types;
5931 // type_T *uf_ret_type;
5932
Ernie Rael114ec812023-06-04 18:11:35 +01005933 // make uf_type_list empty
5934 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00005935
5936 // TODO: partial_T *uf_partial;
5937
5938 if (ufunc->uf_va_name != NULL)
5939 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
5940
5941 // TODO:
5942 // type_T *uf_va_type;
5943 // type_T *uf_func_type;
5944
5945 ufunc->uf_block_depth = 0;
5946 ufunc->uf_block_ids = NULL;
5947
5948 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
5949
5950 ufunc->uf_refcount = 1;
5951 ufunc->uf_name_exp = NULL;
5952 STRCPY(ufunc->uf_name, fp->uf_name);
5953
5954 return ufunc;
5955}
5956
5957/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005958 * ":delfunction {name}"
5959 */
5960 void
5961ex_delfunction(exarg_T *eap)
5962{
5963 ufunc_T *fp = NULL;
5964 char_u *p;
5965 char_u *name;
5966 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005967 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005968
5969 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005970 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
5971 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005972 vim_free(fudi.fd_newkey);
5973 if (name == NULL)
5974 {
5975 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005976 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005977 return;
5978 }
5979 if (!ends_excmd(*skipwhite(p)))
5980 {
5981 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005982 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005983 return;
5984 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005985 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005986 if (eap->nextcmd != NULL)
5987 *p = NUL;
5988
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005989 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005990 {
5991 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005992 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005993 vim_free(name);
5994 return;
5995 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005996 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005997 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005998 vim_free(name);
5999
6000 if (!eap->skip)
6001 {
6002 if (fp == NULL)
6003 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02006004 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00006005 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006006 return;
6007 }
6008 if (fp->uf_calls > 0)
6009 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006010 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006011 return;
6012 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006013 if (fp->uf_flags & FC_VIM9)
6014 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006015 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006016 return;
6017 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006018
6019 if (fudi.fd_dict != NULL)
6020 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006021 // Delete the dict item that refers to the function, it will
6022 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00006023 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006024 }
6025 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006026 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006027 // A normal function (not a numbered function or lambda) has a
6028 // refcount of 1 for the entry in the hashtable. When deleting
6029 // it and the refcount is more than one, it should be kept.
6030 // A numbered function and lambda should be kept if the refcount is
6031 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006032 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006033 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006034 // Function is still referenced somewhere. Don't free it but
6035 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006036 if (func_remove(fp))
6037 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006038 }
6039 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01006040 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006041 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006042 }
6043}
6044
6045/*
6046 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006047 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006048 */
6049 void
6050func_unref(char_u *name)
6051{
Bram Moolenaar97baee82016-07-26 20:46:08 +02006052 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006053
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006054 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006055 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006056 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006057 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006058 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006059#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006060 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006061#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01006062 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006063 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006064 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006065}
6066
6067/*
6068 * Unreference a Function: decrement the reference count and free it when it
6069 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006070 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006071 */
6072 void
6073func_ptr_unref(ufunc_T *fp)
6074{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01006075 if (fp != NULL && (--fp->uf_refcount <= 0
6076 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
6077 && fp->uf_partial->pt_refcount <= 1
6078 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02006079 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006080 // Only delete it when it's not being used. Otherwise it's done
6081 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02006082 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01006083 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006084 }
6085}
6086
6087/*
6088 * Count a reference to a Function.
6089 */
6090 void
6091func_ref(char_u *name)
6092{
6093 ufunc_T *fp;
6094
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02006095 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006096 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006097 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006098 if (fp != NULL)
6099 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00006100 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01006101 // Only give an error for a numbered function.
6102 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01006103 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006104}
6105
6106/*
6107 * Count a reference to a Function.
6108 */
6109 void
6110func_ptr_ref(ufunc_T *fp)
6111{
6112 if (fp != NULL)
6113 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006114}
6115
6116/*
6117 * Return TRUE if items in "fc" do not have "copyID". That means they are not
6118 * referenced from anywhere that is in use.
6119 */
6120 static int
6121can_free_funccal(funccall_T *fc, int copyID)
6122{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006123 return (fc->fc_l_varlist.lv_copyID != copyID
6124 && fc->fc_l_vars.dv_copyID != copyID
6125 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006126 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006127}
6128
6129/*
6130 * ":return [expr]"
6131 */
6132 void
6133ex_return(exarg_T *eap)
6134{
6135 char_u *arg = eap->arg;
6136 typval_T rettv;
6137 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006138 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006139
6140 if (current_funccal == NULL)
6141 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00006142 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006143 return;
6144 }
6145
Bram Moolenaar844fb642021-10-23 13:32:30 +01006146 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006147 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
6148
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006149 if (eap->skip)
6150 ++emsg_skip;
6151
6152 eap->nextcmd = NULL;
6153 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006154 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006155 {
6156 if (!eap->skip)
6157 returning = do_return(eap, FALSE, TRUE, &rettv);
6158 else
6159 clear_tv(&rettv);
6160 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01006161 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006162 else if (!eap->skip)
6163 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006164 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01006165 update_force_abort();
6166
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006167 /*
6168 * Return unless the expression evaluation has been cancelled due to an
6169 * aborting error, an interrupt, or an exception.
6170 */
6171 if (!aborting())
6172 returning = do_return(eap, FALSE, TRUE, NULL);
6173 }
6174
Bram Moolenaare38eab22019-12-05 21:50:01 +01006175 // When skipping or the return gets pending, advance to the next command
6176 // in this line (!returning). Otherwise, ignore the rest of the line.
6177 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006178 if (returning)
6179 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006180 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02006181 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006182
6183 if (eap->skip)
6184 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02006185 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006186}
6187
zeertzjq5299c092023-04-12 20:48:16 +01006188/*
6189 * Lower level implementation of "call". Only called when not skipping.
6190 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006191 static int
6192ex_call_inner(
6193 exarg_T *eap,
6194 char_u *name,
6195 char_u **arg,
6196 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006197 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006198 evalarg_T *evalarg)
6199{
6200 linenr_T lnum;
6201 int doesrange;
6202 typval_T rettv;
6203 int failed = FALSE;
6204
zeertzjq5299c092023-04-12 20:48:16 +01006205 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006206 for ( ; lnum <= eap->line2; ++lnum)
6207 {
6208 funcexe_T funcexe;
6209
zeertzjq5299c092023-04-12 20:48:16 +01006210 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006211 {
6212 if (lnum > curbuf->b_ml.ml_line_count)
6213 {
6214 // If the function deleted lines or switched to another buffer
6215 // the line number may become invalid.
6216 emsg(_(e_invalid_range));
6217 break;
6218 }
6219 curwin->w_cursor.lnum = lnum;
6220 curwin->w_cursor.col = 0;
6221 curwin->w_cursor.coladd = 0;
6222 }
6223 *arg = startarg;
6224
6225 funcexe = *funcexe_init;
6226 funcexe.fe_doesrange = &doesrange;
6227 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6228 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6229 {
6230 failed = TRUE;
6231 break;
6232 }
6233 if (has_watchexpr())
6234 dbg_check_breakpoint(eap);
6235
6236 // Handle a function returning a Funcref, Dictionary or List.
6237 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006238 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006239 {
6240 failed = TRUE;
6241 break;
6242 }
6243
6244 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006245 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006246 break;
6247
6248 // Stop when immediately aborting on error, or when an interrupt
6249 // occurred or an exception was thrown but not caught.
6250 // get_func_tv() returned OK, so that the check for trailing
6251 // characters below is executed.
6252 if (aborting())
6253 break;
6254 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006255 return failed;
6256}
6257
6258/*
6259 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6260 * Returns FAIL or OK.
6261 */
6262 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006263ex_defer_inner(
6264 char_u *name,
6265 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006266 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006267 partial_T *partial,
6268 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006269{
6270 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006271 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006272 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006273 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006274
6275 if (current_funccal == NULL)
6276 {
6277 semsg(_(e_str_not_inside_function), "defer");
6278 return FAIL;
6279 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006280 if (partial != NULL)
6281 {
6282 if (partial->pt_dict != NULL)
6283 {
6284 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6285 return FAIL;
6286 }
6287 if (partial->pt_argc > 0)
6288 {
6289 int i;
6290
6291 partial_argc = partial->pt_argc;
6292 for (i = 0; i < partial_argc; ++i)
6293 copy_tv(&partial->pt_argv[i], &argvars[i]);
6294 }
6295 }
Ernie Raelb077b582023-12-14 20:11:44 +01006296 int is_builtin = builtin_function(name, -1);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006297 r = get_func_arguments(arg, evalarg, FALSE,
Ernie Raelb077b582023-12-14 20:11:44 +01006298 argvars + partial_argc, &argcount, is_builtin);
Bram Moolenaar86d87252022-09-05 21:21:25 +01006299 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006300
6301 if (r == OK)
6302 {
6303 if (type != NULL)
6304 {
6305 // Check that the arguments are OK for the types of the funcref.
6306 r = check_argument_types(type, argvars, argcount, NULL, name);
6307 }
Ernie Raelb077b582023-12-14 20:11:44 +01006308 else if (is_builtin)
Bram Moolenaar16900322022-09-08 19:51:45 +01006309 {
6310 int idx = find_internal_func(name);
6311
6312 if (idx < 0)
6313 {
6314 emsg_funcname(e_unknown_function_str, name);
6315 r = FAIL;
6316 }
6317 else if (check_internal_func(idx, argcount) == -1)
6318 r = FAIL;
6319 }
6320 else
6321 {
6322 ufunc_T *ufunc = find_func(name, FALSE);
6323
6324 // we tolerate an unknown function here, it might be defined later
6325 if (ufunc != NULL)
6326 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006327 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006328 if (error != FCERR_UNKNOWN)
6329 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006330 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006331 r = FAIL;
6332 }
6333 }
6334 }
6335 }
6336
Bram Moolenaar86d87252022-09-05 21:21:25 +01006337 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006338 {
6339 while (--argcount >= 0)
6340 clear_tv(&argvars[argcount]);
6341 return FAIL;
6342 }
6343 return add_defer(name, argcount, argvars);
6344}
6345
6346/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006347 * Return TRUE if currently inside a function call.
6348 * Give an error message and return FALSE when not.
6349 */
6350 int
6351can_add_defer(void)
6352{
6353 if (!in_def_function() && get_current_funccal() == NULL)
6354 {
6355 semsg(_(e_str_not_inside_function), "defer");
6356 return FALSE;
6357 }
6358 return TRUE;
6359}
6360
6361/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006362 * Add a deferred call for "name" with arguments "argvars[argcount]".
6363 * Consumes "argvars[]".
6364 * Caller must check that in_def_function() returns TRUE or current_funccal is
6365 * not NULL.
6366 * Returns OK or FAIL.
6367 */
6368 int
6369add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6370{
6371 char_u *saved_name = vim_strsave(name);
6372 int argcount = argcount_arg;
6373 defer_T *dr;
6374 int ret = FAIL;
6375
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006376 if (saved_name == NULL)
6377 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006378 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006379 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006380 if (add_defer_function(saved_name, argcount, argvars) == OK)
6381 argcount = 0;
6382 }
6383 else
6384 {
6385 if (current_funccal->fc_defer.ga_itemsize == 0)
6386 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6387 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6388 goto theend;
6389 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6390 + current_funccal->fc_defer.ga_len++;
6391 dr->dr_name = saved_name;
6392 dr->dr_argcount = argcount;
6393 while (argcount > 0)
6394 {
6395 --argcount;
6396 dr->dr_argvars[argcount] = argvars[argcount];
6397 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006398 }
6399 ret = OK;
6400
6401theend:
6402 while (--argcount >= 0)
6403 clear_tv(&argvars[argcount]);
6404 return ret;
6405}
6406
6407/*
6408 * Invoked after a functions has finished: invoke ":defer" functions.
6409 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006410 static void
6411handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006412{
6413 int idx;
6414
Bram Moolenaar58779852022-09-06 18:31:14 +01006415 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006416 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006417 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006418
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006419 if (dr->dr_name == NULL)
6420 // already being called, can happen if function does ":qa"
6421 continue;
6422
6423 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006424 CLEAR_FIELD(funcexe);
6425 funcexe.fe_evaluate = TRUE;
6426
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006427 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006428 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006429
6430 char_u *name = dr->dr_name;
6431 dr->dr_name = NULL;
6432
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006433 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006434 // first statement in the function will be executed (because of the
6435 // exception). So save and restore the try/catch/throw exception
6436 // state.
6437 exception_state_T estate;
6438 exception_state_save(&estate);
6439 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006440
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006441 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6442
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02006443 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02006444
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006445 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006446 vim_free(name);
6447 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006448 clear_tv(&dr->dr_argvars[i]);
6449 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006450 ga_clear(&funccal->fc_defer);
6451}
6452
zeertzjq960cf912023-04-18 21:52:54 +01006453 static void
6454invoke_funccall_defer(funccall_T *fc)
6455{
6456 if (fc->fc_ectx != NULL)
6457 {
6458 // :def function
6459 unwind_def_callstack(fc->fc_ectx);
6460 may_invoke_defer_funcs(fc->fc_ectx);
6461 }
6462 else
6463 {
6464 // legacy function
6465 handle_defer_one(fc);
6466 }
6467}
6468
Bram Moolenaar58779852022-09-06 18:31:14 +01006469/*
6470 * Called when exiting: call all defer functions.
6471 */
6472 void
6473invoke_all_defer(void)
6474{
zeertzjq1be4b812023-04-19 14:21:24 +01006475 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6476 invoke_funccall_defer(fc);
6477
zeertzjq960cf912023-04-18 21:52:54 +01006478 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6479 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6480 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006481}
6482
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006483/*
6484 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006485 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006486 */
6487 void
6488ex_call(exarg_T *eap)
6489{
6490 char_u *arg = eap->arg;
6491 char_u *startarg;
6492 char_u *name;
6493 char_u *tofree;
6494 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006495 int failed = FALSE;
6496 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006497 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006498 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006499 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006500 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006501 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006502 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006503
Bram Moolenaare6b53242020-07-01 17:28:33 +02006504 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006505 if (eap->skip)
6506 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006507 typval_T rettv;
6508
Bram Moolenaare38eab22019-12-05 21:50:01 +01006509 // trans_function_name() doesn't work well when skipping, use eval0()
6510 // instead to skip to any following command, e.g. for:
6511 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006512 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006513 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006514 clear_tv(&rettv);
6515 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006516 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006517 return;
6518 }
6519
zeertzjq5299c092023-04-12 20:48:16 +01006520 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006521 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006522 if (fudi.fd_newkey != NULL)
6523 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006524 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006525 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006526 vim_free(fudi.fd_newkey);
6527 }
6528 if (tofree == NULL)
6529 return;
6530
Bram Moolenaare38eab22019-12-05 21:50:01 +01006531 // Increase refcount on dictionary, it could get deleted when evaluating
6532 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006533 if (fudi.fd_dict != NULL)
6534 ++fudi.fd_dict->dv_refcount;
6535
Bram Moolenaare38eab22019-12-05 21:50:01 +01006536 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6537 // contents. For VAR_PARTIAL get its partial, unless we already have one
6538 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006539 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006540 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006541 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006542 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006543
Bram Moolenaare38eab22019-12-05 21:50:01 +01006544 // Skip white space to allow ":call func ()". Not good, but required for
6545 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006546 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006547 if (*startarg != '(')
6548 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006549 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006550 goto end;
6551 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006552 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006553 {
6554 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6555 goto end;
6556 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006557
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006558 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006559 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006560 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006561 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006562 }
6563 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006564 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006565 funcexe_T funcexe;
6566
Bram Moolenaara80faa82020-04-12 19:37:17 +02006567 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006568 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006569 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006570 funcexe.fe_partial = partial;
6571 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006572 funcexe.fe_firstline = eap->line1;
6573 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006574 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006575 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006576 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006577 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006578
Bram Moolenaar1d341892021-09-18 15:25:52 +02006579 // When inside :try we need to check for following "| catch" or "| endtry".
6580 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006581 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006582 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006583 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006584 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006585 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006586 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006587 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006588 {
6589 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006590 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006591 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006592 }
6593 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006594 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006595 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006596 // Must be after using "arg", it may point into memory cleared here.
6597 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006598
6599end:
6600 dict_unref(fudi.fd_dict);
6601 vim_free(tofree);
6602}
6603
6604/*
6605 * Return from a function. Possibly makes the return pending. Also called
6606 * for a pending return at the ":endtry" or after returning from an extra
6607 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6608 * when called due to a ":return" command. "rettv" may point to a typval_T
6609 * with the return rettv. Returns TRUE when the return can be carried out,
6610 * FALSE when the return gets pending.
6611 */
6612 int
6613do_return(
6614 exarg_T *eap,
6615 int reanimate,
6616 int is_cmd,
6617 void *rettv)
6618{
6619 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006620 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006621
6622 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006623 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006624 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006625
6626 /*
6627 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6628 * not in its finally clause (which then is to be executed next) is found.
6629 * In this case, make the ":return" pending for execution at the ":endtry".
6630 * Otherwise, return normally.
6631 */
6632 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6633 if (idx >= 0)
6634 {
6635 cstack->cs_pending[idx] = CSTP_RETURN;
6636
6637 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006638 // A pending return again gets pending. "rettv" points to an
6639 // allocated variable with the rettv of the original ":return"'s
6640 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006641 cstack->cs_rettv[idx] = rettv;
6642 else
6643 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006644 // When undoing a return in order to make it pending, get the stored
6645 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006646 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006647 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006648
6649 if (rettv != NULL)
6650 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006651 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006652 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6653 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6654 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006655 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006656 }
6657 else
6658 cstack->cs_rettv[idx] = NULL;
6659
6660 if (reanimate)
6661 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006662 // The pending return value could be overwritten by a ":return"
6663 // without argument in a finally clause; reset the default
6664 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006665 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6666 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006667 }
6668 }
6669 report_make_pending(CSTP_RETURN, rettv);
6670 }
6671 else
6672 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006673 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006674
Bram Moolenaare38eab22019-12-05 21:50:01 +01006675 // If the return is carried out now, store the return value. For
6676 // a return immediately after reanimation, the value is already
6677 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006678 if (!reanimate && rettv != NULL)
6679 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006680 clear_tv(current_funccal->fc_rettv);
6681 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006682 if (!is_cmd)
6683 vim_free(rettv);
6684 }
6685 }
6686
6687 return idx < 0;
6688}
6689
6690/*
6691 * Free the variable with a pending return value.
6692 */
6693 void
6694discard_pending_return(void *rettv)
6695{
6696 free_tv((typval_T *)rettv);
6697}
6698
6699/*
6700 * Generate a return command for producing the value of "rettv". The result
6701 * is an allocated string. Used by report_pending() for verbose messages.
6702 */
6703 char_u *
6704get_return_cmd(void *rettv)
6705{
6706 char_u *s = NULL;
6707 char_u *tofree = NULL;
6708 char_u numbuf[NUMBUFLEN];
6709
6710 if (rettv != NULL)
6711 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6712 if (s == NULL)
6713 s = (char_u *)"";
6714
6715 STRCPY(IObuff, ":return ");
6716 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6717 if (STRLEN(s) + 8 >= IOSIZE)
6718 STRCPY(IObuff + IOSIZE - 4, "...");
6719 vim_free(tofree);
6720 return vim_strsave(IObuff);
6721}
6722
6723/*
6724 * Get next function line.
6725 * Called by do_cmdline() to get the next line.
6726 * Returns allocated string, or NULL for end of function.
6727 */
6728 char_u *
6729get_func_line(
6730 int c UNUSED,
6731 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006732 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006733 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006734{
6735 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006736 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006737 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006738 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006739
Bram Moolenaare38eab22019-12-05 21:50:01 +01006740 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006741 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006742 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006743 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006744 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006745 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006746 }
6747#ifdef FEAT_PROFILE
6748 if (do_profiling == PROF_YES)
6749 func_line_end(cookie);
6750#endif
6751
6752 gap = &fp->uf_lines;
6753 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006754 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006755 retval = NULL;
6756 else
6757 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006758 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006759 while (fcp->fc_linenr < gap->ga_len
6760 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6761 ++fcp->fc_linenr;
6762 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006763 retval = NULL;
6764 else
6765 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006766 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6767 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006768#ifdef FEAT_PROFILE
6769 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006770 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006771#endif
6772 }
6773 }
6774
Bram Moolenaare38eab22019-12-05 21:50:01 +01006775 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006776 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006777 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006778 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006779 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006780 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006781 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006782 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006783 }
6784
6785 return retval;
6786}
6787
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006788/*
6789 * Return TRUE if the currently active function should be ended, because a
6790 * return was encountered or an error occurred. Used inside a ":while".
6791 */
6792 int
6793func_has_ended(void *cookie)
6794{
6795 funccall_T *fcp = (funccall_T *)cookie;
6796
Bram Moolenaare38eab22019-12-05 21:50:01 +01006797 // Ignore the "abort" flag if the abortion behavior has been changed due to
6798 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006799 return (((fcp->fc_func->uf_flags & FC_ABORT)
6800 && did_emsg && !aborted_in_try())
6801 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006802}
6803
6804/*
6805 * return TRUE if cookie indicates a function which "abort"s on errors.
6806 */
6807 int
6808func_has_abort(
6809 void *cookie)
6810{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006811 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006812}
6813
6814
6815/*
6816 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6817 * Don't do this when "Func" is already a partial that was bound
6818 * explicitly (pt_auto is FALSE).
6819 * Changes "rettv" in-place.
6820 * Returns the updated "selfdict_in".
6821 */
6822 dict_T *
6823make_partial(dict_T *selfdict_in, typval_T *rettv)
6824{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006825 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006826 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006827 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006828 dict_T *selfdict = selfdict_in;
6829
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006830 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6831 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006832 fp = rettv->vval.v_partial->pt_func;
6833 else
6834 {
6835 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006836 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006837 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006838 if (fname == NULL)
6839 {
6840 // There is no point binding a dict to a NULL function, just create
6841 // a function reference.
6842 rettv->v_type = VAR_FUNC;
6843 rettv->vval.v_string = NULL;
6844 }
6845 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006846 {
6847 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006848 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006849
6850 // Translate "s:func" to the stored function name.
6851 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6852 fp = find_func(fname, FALSE);
6853 vim_free(tofree);
6854 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006855 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006856
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006857 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006858 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006859 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006860
6861 if (pt != NULL)
6862 {
6863 pt->pt_refcount = 1;
6864 pt->pt_dict = selfdict;
6865 pt->pt_auto = TRUE;
6866 selfdict = NULL;
6867 if (rettv->v_type == VAR_FUNC)
6868 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006869 // Just a function: Take over the function name and use
6870 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006871 pt->pt_name = rettv->vval.v_string;
6872 }
6873 else
6874 {
6875 partial_T *ret_pt = rettv->vval.v_partial;
6876 int i;
6877
Bram Moolenaare38eab22019-12-05 21:50:01 +01006878 // Partial: copy the function name, use selfdict and copy
6879 // args. Can't take over name or args, the partial might
6880 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006881 if (ret_pt->pt_name != NULL)
6882 {
6883 pt->pt_name = vim_strsave(ret_pt->pt_name);
6884 func_ref(pt->pt_name);
6885 }
6886 else
6887 {
6888 pt->pt_func = ret_pt->pt_func;
6889 func_ptr_ref(pt->pt_func);
6890 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006891 if (ret_pt->pt_argc > 0)
6892 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006893 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006894 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006895 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006896 pt->pt_argc = 0;
6897 else
6898 {
6899 pt->pt_argc = ret_pt->pt_argc;
6900 for (i = 0; i < pt->pt_argc; i++)
6901 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6902 }
6903 }
6904 partial_unref(ret_pt);
6905 }
6906 rettv->v_type = VAR_PARTIAL;
6907 rettv->vval.v_partial = pt;
6908 }
6909 }
6910 return selfdict;
6911}
6912
6913/*
6914 * Return the name of the executed function.
6915 */
6916 char_u *
6917func_name(void *cookie)
6918{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006919 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006920}
6921
6922/*
6923 * Return the address holding the next breakpoint line for a funccall cookie.
6924 */
6925 linenr_T *
6926func_breakpoint(void *cookie)
6927{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006928 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006929}
6930
6931/*
6932 * Return the address holding the debug tick for a funccall cookie.
6933 */
6934 int *
6935func_dbg_tick(void *cookie)
6936{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006937 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006938}
6939
6940/*
6941 * Return the nesting level for a funccall cookie.
6942 */
6943 int
6944func_level(void *cookie)
6945{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006946 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006947}
6948
6949/*
6950 * Return TRUE when a function was ended by a ":return" command.
6951 */
6952 int
6953current_func_returned(void)
6954{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006955 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006956}
6957
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006958 int
6959free_unref_funccal(int copyID, int testing)
6960{
6961 int did_free = FALSE;
6962 int did_free_funccal = FALSE;
6963 funccall_T *fc, **pfc;
6964
6965 for (pfc = &previous_funccal; *pfc != NULL; )
6966 {
6967 if (can_free_funccal(*pfc, copyID))
6968 {
6969 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006970 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006971 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006972 did_free = TRUE;
6973 did_free_funccal = TRUE;
6974 }
6975 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006976 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006977 }
6978 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006979 // When a funccal was freed some more items might be garbage
6980 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006981 (void)garbage_collect(testing);
6982
6983 return did_free;
6984}
6985
6986/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006987 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006988 */
6989 static funccall_T *
6990get_funccal(void)
6991{
6992 int i;
6993 funccall_T *funccal;
6994 funccall_T *temp_funccal;
6995
6996 funccal = current_funccal;
6997 if (debug_backtrace_level > 0)
6998 {
6999 for (i = 0; i < debug_backtrace_level; i++)
7000 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007001 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007002 if (temp_funccal)
7003 funccal = temp_funccal;
7004 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01007005 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007006 debug_backtrace_level = i;
7007 }
7008 }
7009 return funccal;
7010}
7011
7012/*
7013 * Return the hashtable used for local variables in the current funccal.
7014 * Return NULL if there is no current funccal.
7015 */
7016 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007017get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007018{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007019 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007020 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007021 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007022}
7023
7024/*
7025 * Return the l: scope variable.
7026 * Return NULL if there is no current funccal.
7027 */
7028 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007029get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007030{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007031 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007032 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007033 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007034}
7035
7036/*
7037 * Return the hashtable used for argument in the current funccal.
7038 * Return NULL if there is no current funccal.
7039 */
7040 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007041get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007042{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007043 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007044 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007045 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007046}
7047
7048/*
7049 * Return the a: scope variable.
7050 * Return NULL if there is no current funccal.
7051 */
7052 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00007053get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007054{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007055 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007056 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007057 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007058}
7059
7060/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007061 * List function variables, if there is a function.
7062 */
7063 void
7064list_func_vars(int *first)
7065{
Bram Moolenaarca16c602022-09-06 18:57:08 +01007066 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
7067 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01007068 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007069}
7070
7071/*
7072 * If "ht" is the hashtable for local variables in the current funccal, return
7073 * the dict that contains it.
7074 * Otherwise return NULL.
7075 */
7076 dict_T *
7077get_current_funccal_dict(hashtab_T *ht)
7078{
7079 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01007080 && ht == &current_funccal->fc_l_vars.dv_hashtab)
7081 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007082 return NULL;
7083}
7084
7085/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007086 * Search hashitem in parent scope.
7087 */
7088 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007089find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007090{
7091 funccall_T *old_current_funccal = current_funccal;
7092 hashtab_T *ht;
7093 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007094 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007095
Bram Moolenaarca16c602022-09-06 18:57:08 +01007096 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007097 return NULL;
7098
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02007099 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01007100 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02007101 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007102 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007103 ht = find_var_ht(name, &varname);
7104 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02007105 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007106 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02007107 if (!HASHITEM_EMPTY(hi))
7108 {
7109 *pht = ht;
7110 break;
7111 }
7112 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007113 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02007114 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007115 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02007116 }
7117 current_funccal = old_current_funccal;
7118
7119 return hi;
7120}
7121
7122/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007123 * Search variable in parent scope.
7124 */
7125 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007126find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007127{
7128 dictitem_T *v = NULL;
7129 funccall_T *old_current_funccal = current_funccal;
7130 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007131 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007132
Bram Moolenaarca16c602022-09-06 18:57:08 +01007133 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007134 return NULL;
7135
Bram Moolenaare38eab22019-12-05 21:50:01 +01007136 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01007137 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007138 while (current_funccal)
7139 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007140 ht = find_var_ht(name, &varname);
7141 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007142 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007143 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007144 if (v != NULL)
7145 break;
7146 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01007147 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007148 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007149 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007150 }
7151 current_funccal = old_current_funccal;
7152
7153 return v;
7154}
7155
7156/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007157 * Set "copyID + 1" in previous_funccal and callers.
7158 */
7159 int
7160set_ref_in_previous_funccal(int copyID)
7161{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007162 funccall_T *fc;
7163
Bram Moolenaarca16c602022-09-06 18:57:08 +01007164 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007165 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007166 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007167 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
7168 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
7169 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007170 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007171 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007172 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007173}
7174
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007175 static int
7176set_ref_in_funccal(funccall_T *fc, int copyID)
7177{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007178 if (fc->fc_copyID != copyID)
7179 {
7180 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01007181 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
7182 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
7183 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
7184 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007185 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007186 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007187 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007188}
7189
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007190/*
7191 * Set "copyID" in all local vars and arguments in the call stack.
7192 */
7193 int
7194set_ref_in_call_stack(int copyID)
7195{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007196 funccall_T *fc;
7197 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007198
Bram Moolenaarca16c602022-09-06 18:57:08 +01007199 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007200 if (set_ref_in_funccal(fc, copyID))
7201 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007202
7203 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007204 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007205 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007206 if (set_ref_in_funccal(fc, copyID))
7207 return TRUE;
7208 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007209}
7210
7211/*
7212 * Set "copyID" in all functions available by name.
7213 */
7214 int
7215set_ref_in_functions(int copyID)
7216{
7217 int todo;
7218 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007219 ufunc_T *fp;
7220
7221 todo = (int)func_hashtab.ht_used;
7222 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007223 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007224 if (!HASHITEM_EMPTY(hi))
7225 {
7226 --todo;
7227 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007228 if (!func_name_refcount(fp->uf_name)
7229 && set_ref_in_func(NULL, fp, copyID))
7230 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007231 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007232 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007233 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007234}
7235
7236/*
7237 * Set "copyID" in all function arguments.
7238 */
7239 int
7240set_ref_in_func_args(int copyID)
7241{
7242 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007243
7244 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007245 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7246 copyID, NULL, NULL))
7247 return TRUE;
7248 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007249}
7250
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007251/*
7252 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007253 * Returns TRUE if setting references failed somehow.
7254 */
7255 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007256set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007257{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007258 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007259 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007260 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007261 char_u fname_buf[FLEN_FIXED + 1];
7262 char_u *tofree = NULL;
7263 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007264 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007265
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007266 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007267 return FALSE;
7268
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007269 if (fp_in == NULL)
7270 {
7271 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007272 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007273 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007274 if (fp != NULL)
7275 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007276 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007277 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007278 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007279
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007280 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007281 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007282}
7283
Bram Moolenaare38eab22019-12-05 21:50:01 +01007284#endif // FEAT_EVAL