blob: db16b68049e55f32c633f9ba34a004c2926e1429 [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);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020037
38 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +000039func_init(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020040{
41 hash_init(&func_hashtab);
42}
43
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020044/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020045 * Return the function hash table
46 */
47 hashtab_T *
48func_tbl_get(void)
49{
50 return &func_hashtab;
51}
52
53/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020054 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020055 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010056 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010057 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaardce24412022-02-08 20:35:30 +000058 * If "eap" is not NULL use it to check for an already declared name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010059 * Return a pointer to after the type.
60 * When something is wrong return "arg".
61 */
62 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010063one_function_arg(
64 char_u *arg,
65 garray_T *newargs,
66 garray_T *argtypes,
67 int types_optional,
h-eastb895b0f2023-09-24 15:46:31 +020068 garray_T *arg_objm,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010069 evalarg_T *evalarg,
Bram Moolenaardce24412022-02-08 20:35:30 +000070 exarg_T *eap,
Bram Moolenaar2a389082021-04-09 20:24:31 +020071 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010072 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073{
Bram Moolenaar6e949782020-04-13 17:21:00 +020074 char_u *p = arg;
75 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020076 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077
78 while (ASCII_ISALNUM(*p) || *p == '_')
79 ++p;
80 if (arg == p || isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020081 || (argtypes == NULL
82 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
83 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010084 {
85 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000086 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087 return arg;
88 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010089
Bram Moolenaarce723f32023-06-10 19:00:12 +010090 // Extra checks in Vim9 script.
91 if (!skip && argtypes != NULL)
92 {
93 int c = *p;
94 *p = NUL;
95 int r = check_reserved_name(arg, FALSE);
96 *p = c;
97 if (r == FAIL)
98 return arg;
99
100 // Cannot use script var name for argument. In function: also check
101 // local vars and arguments.
102 if (check_defined(arg, p - arg,
103 evalarg == NULL ? NULL : evalarg->eval_cctx,
Bram Moolenaardce24412022-02-08 20:35:30 +0000104 eap == NULL ? NULL : eap->cstack, TRUE) == FAIL)
Bram Moolenaarce723f32023-06-10 19:00:12 +0100105 return arg;
106 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +0100107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100108 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
109 return arg;
110 if (newargs != NULL)
111 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100112 int c;
113 int i;
114
115 c = *p;
116 *p = NUL;
117 arg_copy = vim_strsave(arg);
118 if (arg_copy == NULL)
119 {
120 *p = c;
121 return arg;
122 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200123 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200124 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200125 // Check for duplicate argument name.
126 for (i = 0; i < newargs->ga_len; ++i)
127 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
128 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000129 semsg(_(e_duplicate_argument_name_str), arg_copy);
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200130 vim_free(arg_copy);
131 return arg;
132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100133 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
134 newargs->ga_len++;
135
136 *p = c;
137 }
138
139 // get any type from "arg: type"
h-eastb895b0f2023-09-24 15:46:31 +0200140 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK)
141 && arg_objm != NULL && (skip || ga_grow(arg_objm, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100142 {
143 char_u *type = NULL;
144
Bram Moolenaar6e949782020-04-13 17:21:00 +0200145 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
146 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200147 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200148 arg_copy == NULL ? arg : arg_copy);
149 p = skipwhite(p);
150 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 if (*p == ':')
152 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200153 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100154 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200155 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100156 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200157 return arg;
158 }
159 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200160 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100161 if (!skip)
162 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100163 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200164 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200165 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200166 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200167 arg_copy == NULL ? arg : arg_copy);
168 return arg;
169 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100170 if (!skip)
171 {
172 if (type == NULL && types_optional)
173 // lambda arguments default to "any" type
Bram Moolenaar2a389082021-04-09 20:24:31 +0200174 type = vim_strsave((char_u *)
175 (is_vararg ? "list<any>" : "any"));
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100176 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
h-eastb895b0f2023-09-24 15:46:31 +0200177 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = FALSE;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 }
180
181 return p;
182}
183
184/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000185 * Handle line continuation in function arguments or body.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000186 * Get a next line, store it in "eap" if appropriate and put the line in
187 * "lines_to_free" to free the line later.
Bram Moolenaar7473a842021-12-28 17:55:26 +0000188 */
189 static char_u *
190get_function_line(
191 exarg_T *eap,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000192 garray_T *lines_to_free,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000193 int indent,
194 getline_opt_T getline_options)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000195{
196 char_u *theline;
197
198 if (eap->getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +0000199 theline = getcmdline(':', 0L, indent, 0);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000200 else
201 theline = eap->getline(':', eap->cookie, indent, getline_options);
202 if (theline != NULL)
203 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000204 if (lines_to_free->ga_len > 0
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000205 && eap->cmdlinep != NULL
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000206 && *eap->cmdlinep == ((char_u **)lines_to_free->ga_data)
207 [lines_to_free->ga_len - 1])
Bram Moolenaar7473a842021-12-28 17:55:26 +0000208 *eap->cmdlinep = theline;
Bram Moolenaarb5820102023-01-25 12:27:13 +0000209 (void)ga_add_string(lines_to_free, theline);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000210 }
211
212 return theline;
213}
214
215/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200216 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100217 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100218 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200219 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100220 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200221get_function_args(
222 char_u **argp,
223 char_u endchar,
224 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100225 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100226 int types_optional, // types optional if "argtypes" is not NULL
h-eastb895b0f2023-09-24 15:46:31 +0200227 garray_T *arg_objm, // NULL unless using :def
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100228 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200229 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200230 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200231 int skip,
Bram Moolenaardce24412022-02-08 20:35:30 +0000232 exarg_T *eap, // can be NULL
Bram Moolenaar554d0312023-01-05 19:59:18 +0000233 int in_class, // non-zero when inside a class or interface
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000234 garray_T *newlines, // function body lines
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000235 garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200236{
237 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100238 char_u *arg;
239 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200240 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200241 int any_default = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100242 char_u *whitep = *argp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200243
244 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000245 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000247 ga_init2(argtypes, sizeof(char_u *), 3);
h-eastb895b0f2023-09-24 15:46:31 +0200248 if (arg_objm != NULL)
249 ga_init2(arg_objm, sizeof(int8_T), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200250 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000251 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200252
253 if (varargs != NULL)
254 *varargs = FALSE;
255
256 /*
257 * Isolate the arguments: "arg1, arg2, ...)"
258 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100259 arg = skipwhite(*argp);
260 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200261 while (*p != endchar)
262 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200263 while (eap != NULL && eap->getline != NULL
264 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200265 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200266 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000267 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000268 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000269
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200270 if (theline == NULL)
271 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200272 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200273 p = skipwhite(theline);
274 }
275
276 if (mustend && *p != endchar)
277 {
278 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000279 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100280 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200281 }
282 if (*p == endchar)
283 break;
284
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200285 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
286 {
287 if (varargs != NULL)
288 *varargs = TRUE;
289 p += 3;
290 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100291
292 if (argtypes != NULL)
293 {
294 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200295 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100296 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100297 if (!skip)
298 emsg(_(e_missing_name_after_dots));
299 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100300 }
301
302 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100303 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200304 arg_objm, evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305 if (p == arg)
306 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100307 if (*skipwhite(p) == '=')
308 {
309 emsg(_(e_cannot_use_default_for_variable_arguments));
310 break;
311 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100312 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200313 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000314 else if (in_class && STRNCMP(p, "this.", 5) == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000315 {
316 // this.memberName
317 p += 5;
318 arg = p;
319 while (ASCII_ISALNUM(*p) || *p == '_')
320 ++p;
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000321 char_u *argend = p;
322
h-eastdb385522023-09-28 22:18:19 +0200323 // object variable this. can be used only in a constructor
324 if (STRNCMP(eap->arg, "new", 3) != 0)
325 {
326 c = *argend;
327 *argend = NUL;
328 semsg(_(e_cannot_use_an_object_variable_except_with_the_new_method_str), arg);
329 *argend = c;
330 break;
331 }
332
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000333 if (*skipwhite(p) == '=')
334 {
335 char_u *defval = skipwhite(skipwhite(p) + 1);
336 if (STRNCMP(defval, "v:none", 6) != 0)
337 {
338 semsg(_(e_constructor_default_value_must_be_vnone_str), p);
339 goto err_ret;
340 }
341 any_default = TRUE;
342 p = defval + 6;
343
344 if (ga_grow(default_args, 1) == FAIL)
345 goto err_ret;
346
347 char_u *expr = vim_strsave((char_u *)"v:none");
348 if (expr == NULL)
349 goto err_ret;
350 ((char_u **)(default_args->ga_data))
351 [default_args->ga_len] = expr;
352 default_args->ga_len++;
353 }
354 else if (any_default)
355 {
356 emsg(_(e_non_default_argument_follows_default_argument));
357 goto err_ret;
358 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000359
360 // TODO: check the argument is indeed a member
361 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
362 return FAIL;
363 if (newargs != NULL)
364 {
365 ((char_u **)(newargs->ga_data))[newargs->ga_len] =
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000366 vim_strnsave(arg, argend - arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000367 newargs->ga_len++;
368
h-eastb895b0f2023-09-24 15:46:31 +0200369 if (argtypes != NULL && ga_grow(argtypes, 1) == OK
370 && arg_objm != NULL && ga_grow(arg_objm, 1) == OK)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000371 {
372 // TODO: use the actual type
373 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] =
374 vim_strsave((char_u *)"any");
h-eastb895b0f2023-09-24 15:46:31 +0200375 ((int8_T *)arg_objm->ga_data)[arg_objm->ga_len++] = TRUE;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000376
377 // Add a line to the function body for the assignment.
378 if (ga_grow(newlines, 1) == OK)
379 {
380 // "this.name = name"
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000381 int len = 5 + (argend - arg) + 3 + (argend - arg) + 1;
382 if (any_default)
383 len += 14 + 10;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000384 char_u *assignment = alloc(len);
385 if (assignment != NULL)
386 {
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000387 c = *argend;
388 *argend = NUL;
389 if (any_default)
390 vim_snprintf((char *)assignment, len,
391 "ifargisset %d this.%s = %s",
392 default_args->ga_len - 1, arg, arg);
393 else
394 vim_snprintf((char *)assignment, len,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000395 "this.%s = %s", arg, arg);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000396 *argend = c;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000397 ((char_u **)(newlines->ga_data))[
398 newlines->ga_len++] = assignment;
399 }
400 }
401 }
402 }
403 if (*p == ',')
404 ++p;
405 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200406 else
407 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200408 char_u *np;
409
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200410 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100411 p = one_function_arg(p, newargs, argtypes, types_optional,
h-eastb895b0f2023-09-24 15:46:31 +0200412 arg_objm, evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200414 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200415
Bram Moolenaar015cf102021-06-26 21:52:02 +0200416 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200417 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200418 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200419 if (*np == '=' && np[1] != '=' && np[1] != '~'
420 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200421 {
422 typval_T rettv;
423
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100424 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200425 any_default = TRUE;
Bram Moolenaareb5adf12022-09-04 13:41:37 +0100426 p = skipwhite(np + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000427 char_u *expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200428 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200429 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200430 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200431 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200432 if (ga_grow(default_args, 1) == FAIL)
433 goto err_ret;
434
435 // trim trailing whitespace
436 while (p > expr && VIM_ISWHITE(p[-1]))
437 p--;
438 c = *p;
439 *p = NUL;
440 expr = vim_strsave(expr);
441 if (expr == NULL)
442 {
443 *p = c;
444 goto err_ret;
445 }
446 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200447 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200448 default_args->ga_len++;
449 *p = c;
450 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200451 }
452 else
453 mustend = TRUE;
454 }
455 else if (any_default)
456 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000457 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200458 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200459 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200460
461 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
462 {
463 // Be tolerant when skipping
464 if (!skip)
465 {
466 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
467 goto err_ret;
468 }
469 p = skipwhite(p);
470 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200471 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200472 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200473 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200474 // Don't give this error when skipping, it makes the "->" not
475 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100476 // Allow missing space after comma in legacy functions.
477 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200478 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
479 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100480 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200481 goto err_ret;
482 }
483 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200484 else
485 mustend = TRUE;
486 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200487 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200488 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200489 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200490
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200491 if (*p != endchar)
492 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100493 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200494
495 *argp = p;
496 return OK;
497
498err_ret:
499 if (newargs != NULL)
500 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200501 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200502 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200503 return FAIL;
504}
505
506/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100507 * Parse the argument types, filling "fp->uf_arg_types".
508 * Return OK or FAIL.
509 */
510 static int
h-eastb895b0f2023-09-24 15:46:31 +0200511parse_argument_types(
512 ufunc_T *fp,
513 garray_T *argtypes,
514 int varargs,
515 garray_T *arg_objm,
516 ocmember_T *obj_members,
517 int obj_member_count)
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100518{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200519 int len = 0;
520
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100521 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
522 if (argtypes->ga_len > 0)
523 {
524 // When "varargs" is set the last name/type goes into uf_va_name
525 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200526 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100527
528 if (len > 0)
529 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
530 if (fp->uf_arg_types != NULL)
531 {
532 int i;
533 type_T *type;
534
535 for (i = 0; i < len; ++ i)
536 {
537 char_u *p = ((char_u **)argtypes->ga_data)[i];
538
539 if (p == NULL)
540 // will get the type from the default value
541 type = &t_unknown;
542 else
h-eastb895b0f2023-09-24 15:46:31 +0200543 {
544 if (arg_objm != NULL && ((int8_T *)arg_objm->ga_data)[i])
545 {
546 char_u *aname = ((char_u **)fp->uf_args.ga_data)[i];
547
548 type = &t_any;
549 for (int om = 0; om < obj_member_count; ++om)
550 {
551 if (STRCMP(aname, obj_members[om].ocm_name) == 0)
552 {
553 type = obj_members[om].ocm_type;
554 break;
555 }
556 }
557 }
558 else
559 type = parse_type(&p, &fp->uf_type_list, TRUE);
560 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100561 if (type == NULL)
562 return FAIL;
563 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000564 if (i < fp->uf_args.ga_len
565 && (type->tt_type == VAR_FUNC
566 || type->tt_type == VAR_PARTIAL)
567 && var_wrong_func_name(
568 ((char_u **)fp->uf_args.ga_data)[i], TRUE))
569 return FAIL;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100570 }
571 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100572 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200573
574 if (varargs)
575 {
576 char_u *p;
577
578 // Move the last argument "...name: type" to uf_va_name and
579 // uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200580 --fp->uf_args.ga_len;
Bram Moolenaarf0571712023-01-04 13:16:20 +0000581 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len];
582 ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len] = NULL;
Bram Moolenaar2a389082021-04-09 20:24:31 +0200583 p = ((char_u **)argtypes->ga_data)[len];
584 if (p == NULL)
585 // TODO: get type from default value
586 fp->uf_va_type = &t_list_any;
587 else
588 {
589 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
590 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
591 {
592 semsg(_(e_variable_arguments_type_must_be_list_str),
593 ((char_u **)argtypes->ga_data)[len]);
594 return FAIL;
595 }
596 }
597 if (fp->uf_va_type == NULL)
598 return FAIL;
599 }
600
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100601 return OK;
602}
603
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100604 static int
605parse_return_type(ufunc_T *fp, char_u *ret_type)
606{
607 if (ret_type == NULL)
608 fp->uf_ret_type = &t_void;
609 else
610 {
611 char_u *p = ret_type;
612
613 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
614 if (fp->uf_ret_type == NULL)
615 {
616 fp->uf_ret_type = &t_void;
617 return FAIL;
618 }
619 }
620 return OK;
621}
622
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100623/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200624 * Register function "fp" as using "current_funccal" as its scope.
625 */
626 static int
627register_closure(ufunc_T *fp)
628{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200629 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100630 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200631 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200632 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200633 fp->uf_scoped = current_funccal;
634 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200635
Bram Moolenaarca16c602022-09-06 18:57:08 +0100636 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200637 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100638 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
639 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200640 return OK;
641}
642
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100643 static void
644set_ufunc_name(ufunc_T *fp, char_u *name)
645{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100646 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
647 // actually extends beyond the struct.
648 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100649
650 if (name[0] == K_SPECIAL)
651 {
652 fp->uf_name_exp = alloc(STRLEN(name) + 3);
653 if (fp->uf_name_exp != NULL)
654 {
655 STRCPY(fp->uf_name_exp, "<SNR>");
656 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
657 }
658 }
659}
660
Bram Moolenaar58016442016-07-31 18:30:22 +0200661/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100662 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
663 * return "buf" filled with a readable function name.
664 * Otherwise just return "name", thus the return value can always be used.
665 * "name" and "buf" may be equal.
666 */
667 char_u *
668make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
669{
670 size_t len;
671
672 if (name[0] != K_SPECIAL)
673 return name;
674 len = STRLEN(name);
675 if (len + 3 > bufsize)
676 return name;
677
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100678 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100679 mch_memmove(buf, "<SNR>", 5);
680 return buf;
681}
682
683/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200684 * Get a name for a lambda. Returned in static memory.
685 */
686 char_u *
687get_lambda_name(void)
688{
689 static char_u name[30];
690 static int lambda_no = 0;
691
692 sprintf((char*)name, "<lambda>%d", ++lambda_no);
693 return name;
694}
695
Bram Moolenaare01e5212023-01-08 20:31:18 +0000696/*
697 * Allocate a "ufunc_T" for a function called "name".
698 * Makes sure the size is right.
699 */
700 static ufunc_T *
701alloc_ufunc(char_u *name)
702{
703 // When the name is short we need to make sure we allocate enough bytes for
704 // the whole struct, including any padding.
705 size_t len = offsetof(ufunc_T, uf_name) + STRLEN(name) + 1;
706 return alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
707}
708
Bram Moolenaar801ab062020-06-25 19:27:56 +0200709#if defined(FEAT_LUA) || defined(PROTO)
710/*
711 * Registers a native C callback which can be called from Vim script.
712 * Returns the name of the Vim script function.
713 */
714 char_u *
715register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
716{
717 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200718 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200719
Bram Moolenaare01e5212023-01-08 20:31:18 +0000720 fp = alloc_ufunc(name);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200721 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200722 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200723
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200724 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200725 fp->uf_refcount = 1;
726 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000727 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200728 fp->uf_calls = 0;
729 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200730 fp->uf_cb = cb;
731 fp->uf_cb_free = cb_free;
732 fp->uf_cb_state = state;
733
734 set_ufunc_name(fp, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000735 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200736
737 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200738}
739#endif
740
Bram Moolenaar04b12692020-05-04 23:24:44 +0200741/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100742 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100743 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100744 * If "white_error" is not NULL check for correct use of white space and set
745 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100746 * Return NULL if no valid arrow found.
747 */
748 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100749skip_arrow(
750 char_u *start,
751 int equal_arrow,
752 char_u **ret_type,
753 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100754{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100755 char_u *s = start;
756 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100757
758 if (equal_arrow)
759 {
760 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100761 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100762 if (white_error != NULL && !VIM_ISWHITE(s[1]))
763 {
764 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100765 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100766 return NULL;
767 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100768 s = skipwhite(s + 1);
769 *ret_type = s;
770 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100771 if (s == *ret_type)
772 {
773 emsg(_(e_missing_return_type));
774 return NULL;
775 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100776 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100777 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100778 s = skipwhite(s);
779 if (*s != '=')
780 return NULL;
781 ++s;
782 }
783 if (*s != '>')
784 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100785 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
786 || !IS_WHITE_OR_NUL(s[1])))
787 {
788 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100789 semsg(_(e_white_space_required_before_and_after_str_at_str),
790 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100791 return NULL;
792 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100793 return skipwhite(s + 1);
794}
795
796/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100797 * Check if "*cmd" points to a function command and if so advance "*cmd" and
798 * return TRUE.
799 * Otherwise return FALSE;
800 * Do not consider "function(" to be a command.
801 */
802 static int
803is_function_cmd(char_u **cmd)
804{
805 char_u *p = *cmd;
806
807 if (checkforcmd(&p, "function", 2))
808 {
809 if (*p == '(')
810 return FALSE;
811 *cmd = p;
812 return TRUE;
813 }
814 return FALSE;
815}
816
817/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200818 * Called when defining a function: The context may be needed for script
819 * variables declared in a block that is visible now but not when the function
820 * is compiled or called later.
821 */
822 static void
823function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
824{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000825 if (cstack == NULL || cstack->cs_idx < 0)
826 return;
827
828 int count = cstack->cs_idx + 1;
829 int i;
830
831 fp->uf_block_ids = ALLOC_MULT(int, count);
832 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200833 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000834 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
835 sizeof(int) * count);
836 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200837 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000838
839 // Set flag in each block to indicate a function was defined. This
840 // is used to keep the variable when leaving the block, see
841 // hide_script_var().
842 for (i = 0; i <= cstack->cs_idx; ++i)
843 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200844}
845
846/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100847 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200848 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100849 * "newlines" must already have been initialized.
850 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
851 */
852 static int
853get_function_body(
854 exarg_T *eap,
855 garray_T *newlines,
856 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000857 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100858{
859 linenr_T sourcing_lnum_top = SOURCING_LNUM;
860 linenr_T sourcing_lnum_off;
861 int saved_wait_return = need_wait_return;
862 char_u *line_arg = line_arg_in;
863 int vim9_function = eap->cmdidx == CMD_def
864 || eap->cmdidx == CMD_block;
865#define MAX_FUNC_NESTING 50
866 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200867 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100868 int nesting = 0;
869 getline_opt_T getline_options;
870 int indent = 2;
871 char_u *skip_until = NULL;
872 int ret = FAIL;
873 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200874 int heredoc_concat_len = 0;
875 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100876 char_u *heredoc_trimmed = NULL;
877
Bram Moolenaar20677332021-06-06 17:02:53 +0200878 ga_init2(&heredoc_ga, 1, 500);
879
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100880 // Detect having skipped over comment lines to find the return
881 // type. Add NULL lines to keep the line count correct.
882 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
883 if (SOURCING_LNUM < sourcing_lnum_off)
884 {
885 sourcing_lnum_off -= SOURCING_LNUM;
886 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
887 goto theend;
888 while (sourcing_lnum_off-- > 0)
889 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
890 }
891
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200892 nesting_def[0] = vim9_function;
893 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100894 getline_options = vim9_function
895 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
896 for (;;)
897 {
898 char_u *theline;
899 char_u *p;
900 char_u *arg;
901
902 if (KeyTyped)
903 {
904 msg_scroll = TRUE;
905 saved_wait_return = FALSE;
906 }
907 need_wait_return = FALSE;
908
909 if (line_arg != NULL)
910 {
911 // Use eap->arg, split up in parts by line breaks.
912 theline = line_arg;
913 p = vim_strchr(theline, '\n');
914 if (p == NULL)
915 line_arg += STRLEN(line_arg);
916 else
917 {
918 *p = NUL;
919 line_arg = p + 1;
920 }
921 }
922 else
923 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000924 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100925 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100926 }
927 if (KeyTyped)
928 lines_left = Rows - 1;
929 if (theline == NULL)
930 {
931 // Use the start of the function for the line number.
932 SOURCING_LNUM = sourcing_lnum_top;
933 if (skip_until != NULL)
934 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200935 else if (nesting_inline[nesting])
936 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100937 else if (eap->cmdidx == CMD_def)
938 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100939 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000940 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100941 goto theend;
942 }
943
944 // Detect line continuation: SOURCING_LNUM increased more than one.
945 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
946 if (SOURCING_LNUM < sourcing_lnum_off)
947 sourcing_lnum_off -= SOURCING_LNUM;
948 else
949 sourcing_lnum_off = 0;
950
951 if (skip_until != NULL)
952 {
953 // Don't check for ":endfunc"/":enddef" between
954 // * ":append" and "."
955 // * ":python <<EOF" and "EOF"
956 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
957 if (heredoc_trimmed == NULL
958 || (is_heredoc && skipwhite(theline) == theline)
959 || STRNCMP(theline, heredoc_trimmed,
960 STRLEN(heredoc_trimmed)) == 0)
961 {
962 if (heredoc_trimmed == NULL)
963 p = theline;
964 else if (is_heredoc)
965 p = skipwhite(theline) == theline
966 ? theline : theline + STRLEN(heredoc_trimmed);
967 else
968 p = theline + STRLEN(heredoc_trimmed);
969 if (STRCMP(p, skip_until) == 0)
970 {
971 VIM_CLEAR(skip_until);
972 VIM_CLEAR(heredoc_trimmed);
973 getline_options = vim9_function
974 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
975 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200976
977 if (heredoc_concat_len > 0)
978 {
979 // Replace the starting line with all the concatenated
980 // lines.
981 ga_concat(&heredoc_ga, theline);
982 vim_free(((char_u **)(newlines->ga_data))[
983 heredoc_concat_len - 1]);
984 ((char_u **)(newlines->ga_data))[
985 heredoc_concat_len - 1] = heredoc_ga.ga_data;
986 ga_init(&heredoc_ga);
987 heredoc_concat_len = 0;
988 theline += STRLEN(theline); // skip the "EOF"
989 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100990 }
991 }
992 }
993 else
994 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200995 int c;
996 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +0000997 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100998
999 // skip ':' and blanks
1000 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
1001 ;
1002
1003 // Check for "endfunction", "enddef" or "}".
1004 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +00001005 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001006 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001007 ? *p == '}'
1008 : (checkforcmd(&p, nesting_def[nesting]
1009 ? "enddef" : "endfunction", 4)
1010 && *p != ':'))
1011 {
Bram Moolenaarb2175222022-03-05 20:24:41 +00001012 if (!nesting_inline[nesting] && nesting_def[nesting]
1013 && p < cmd + 6)
1014 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001015 if (nesting-- == 0)
1016 {
1017 char_u *nextcmd = NULL;
1018
1019 if (*p == '|' || *p == '}')
1020 nextcmd = p + 1;
1021 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
1022 nextcmd = line_arg;
1023 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001024 && (vim9_function || p_verbose > 0))
1025 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +02001026 SOURCING_LNUM = sourcing_lnum_top
1027 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001028 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +00001029 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +01001030 else
1031 give_warning2((char_u *)
1032 _("W22: Text found after :endfunction: %s"),
1033 p, TRUE);
1034 }
1035 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001036 {
1037 // Another command follows. If the line came from "eap"
1038 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001039 // change "eap->cmdlinep" to point to the last fetched
1040 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001041 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001042 if (lines_to_free->ga_len > 0
1043 && *eap->cmdlinep !=
1044 ((char_u **)lines_to_free->ga_data)
1045 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001046 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001047 // *cmdlinep will be freed later, thus remove the
1048 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001049 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001050 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
1051 [lines_to_free->ga_len - 1];
1052 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001053 }
1054 }
1055 break;
1056 }
1057 }
1058
1059 // Check for mismatched "endfunc" or "enddef".
1060 // We don't check for "def" inside "func" thus we also can't check
1061 // for "enddef".
1062 // We continue to find the end of the function, although we might
1063 // not find it.
1064 else if (nesting_def[nesting])
1065 {
1066 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1067 emsg(_(e_mismatched_endfunction));
1068 }
1069 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1070 emsg(_(e_mismatched_enddef));
1071
1072 // Increase indent inside "if", "while", "for" and "try", decrease
1073 // at "end".
1074 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1075 indent -= 2;
1076 else if (STRNCMP(p, "if", 2) == 0
1077 || STRNCMP(p, "wh", 2) == 0
1078 || STRNCMP(p, "for", 3) == 0
1079 || STRNCMP(p, "try", 3) == 0)
1080 indent += 2;
1081
1082 // Check for defining a function inside this function.
1083 // Only recognize "def" inside "def", not inside "function",
1084 // For backwards compatibility, see Test_function_python().
1085 c = *p;
1086 if (is_function_cmd(&p)
1087 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1088 {
1089 if (*p == '!')
1090 p = skipwhite(p + 1);
1091 p += eval_fname_script(p);
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001092 vim_free(trans_function_name(&p, NULL, TRUE, 0));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001093 if (*skipwhite(p) == '(')
1094 {
1095 if (nesting == MAX_FUNC_NESTING - 1)
1096 emsg(_(e_function_nesting_too_deep));
1097 else
1098 {
1099 ++nesting;
1100 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001101 nesting_inline[nesting] = FALSE;
1102 indent += 2;
1103 }
1104 }
1105 }
1106
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001107 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001108 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001109 // Not a comment line: check for nested inline function.
1110 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001111 while (end > p && VIM_ISWHITE(*end))
1112 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001113 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001114 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001115 int is_block;
1116
1117 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001118 --end;
1119 while (end > p && VIM_ISWHITE(*end))
1120 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001121 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1122 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001123 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001124 char_u *s = p;
1125
1126 // check for line starting with "au" for :autocmd or
1127 // "com" for :command, these can use a {} block
1128 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1129 || checkforcmd_noparen(&s, "command", 3);
1130 }
1131
1132 if (is_block)
1133 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001134 if (nesting == MAX_FUNC_NESTING - 1)
1135 emsg(_(e_function_nesting_too_deep));
1136 else
1137 {
1138 ++nesting;
1139 nesting_def[nesting] = TRUE;
1140 nesting_inline[nesting] = TRUE;
1141 indent += 2;
1142 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001143 }
1144 }
1145 }
1146
1147 // Check for ":append", ":change", ":insert". Not for :def.
1148 p = skip_range(p, FALSE, NULL);
1149 if (!vim9_function
1150 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1151 || (p[0] == 'c'
1152 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1153 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1154 && (STRNCMP(&p[3], "nge", 3) != 0
1155 || !ASCII_ISALPHA(p[6])))))))
1156 || (p[0] == 'i'
1157 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1158 && (!ASCII_ISALPHA(p[2])
1159 || (p[2] == 's'
1160 && (!ASCII_ISALPHA(p[3])
1161 || p[3] == 'e'))))))))
1162 skip_until = vim_strsave((char_u *)".");
1163
1164 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1165 arg = skipwhite(skiptowhite(p));
1166 if (arg[0] == '<' && arg[1] =='<'
1167 && ((p[0] == 'p' && p[1] == 'y'
1168 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1169 || ((p[2] == '3' || p[2] == 'x')
1170 && !ASCII_ISALPHA(p[3]))))
1171 || (p[0] == 'p' && p[1] == 'e'
1172 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1173 || (p[0] == 't' && p[1] == 'c'
1174 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1175 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1176 && !ASCII_ISALPHA(p[3]))
1177 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1178 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1179 || (p[0] == 'm' && p[1] == 'z'
1180 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1181 ))
1182 {
1183 // ":python <<" continues until a dot, like ":append"
1184 p = skipwhite(arg + 2);
1185 if (STRNCMP(p, "trim", 4) == 0)
1186 {
1187 // Ignore leading white space.
1188 p = skipwhite(p + 4);
1189 heredoc_trimmed = vim_strnsave(theline,
1190 skipwhite(theline) - theline);
1191 }
1192 if (*p == NUL)
1193 skip_until = vim_strsave((char_u *)".");
1194 else
1195 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1196 getline_options = GETLINE_NONE;
1197 is_heredoc = TRUE;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001198 if (vim9_function && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001199 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001200 }
1201
Bram Moolenaard881d152022-05-13 13:50:36 +01001202 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001203 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001204 // Check for ":cmd v =<< [trim] EOF"
1205 // and ":cmd [a, b] =<< [trim] EOF"
1206 // and "lines =<< [trim] EOF" for Vim9
1207 // Where "cmd" can be "let", "var", "final" or "const".
zeertzjqa93d9cd2023-05-02 16:25:47 +01001208 arg = p;
1209 if (checkforcmd(&arg, "let", 2)
1210 || checkforcmd(&arg, "var", 3)
1211 || checkforcmd(&arg, "final", 5)
1212 || checkforcmd(&arg, "const", 5)
1213 || vim9_function)
Bram Moolenaard881d152022-05-13 13:50:36 +01001214 {
zeertzjqa93d9cd2023-05-02 16:25:47 +01001215 while (vim_strchr((char_u *)"$@&", *arg) != NULL)
1216 ++arg;
1217 arg = skipwhite(find_name_end(arg, NULL, NULL,
1218 FNE_INCL_BR | FNE_ALLOW_CURLY));
1219 if (vim9_function && *arg == ':')
1220 arg = skipwhite(skip_type(skipwhite(arg + 1), FALSE));
1221 if (arg[0] == '=' && arg[1] == '<' && arg[2] =='<')
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001222 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001223 p = skipwhite(arg + 3);
1224 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001225 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001226 if (STRNCMP(p, "trim", 4) == 0)
1227 {
1228 // Ignore leading white space.
1229 p = skipwhite(p + 4);
1230 heredoc_trimmed = vim_strnsave(theline,
1231 skipwhite(theline) - theline);
1232 continue;
1233 }
1234 if (STRNCMP(p, "eval", 4) == 0)
1235 {
1236 // Ignore leading white space.
1237 p = skipwhite(p + 4);
1238 continue;
1239 }
1240 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001241 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001242 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1243 getline_options = GETLINE_NONE;
1244 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001245 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001246 }
1247 }
1248 }
1249
1250 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001251 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001252 goto theend;
1253
Bram Moolenaar20677332021-06-06 17:02:53 +02001254 if (heredoc_concat_len > 0)
1255 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001256 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001257 // to be used for the instruction later.
1258 ga_concat(&heredoc_ga, theline);
1259 ga_concat(&heredoc_ga, (char_u *)"\n");
1260 p = vim_strsave((char_u *)"");
1261 }
1262 else
1263 {
1264 // Copy the line to newly allocated memory. get_one_sourceline()
1265 // allocates 250 bytes per line, this saves 80% on average. The
1266 // cost is an extra alloc/free.
1267 p = vim_strsave(theline);
1268 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001269 if (p == NULL)
1270 goto theend;
1271 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1272
1273 // Add NULL lines for continuation lines, so that the line count is
1274 // equal to the index in the growarray.
1275 while (sourcing_lnum_off-- > 0)
1276 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1277
1278 // Check for end of eap->arg.
1279 if (line_arg != NULL && *line_arg == NUL)
1280 line_arg = NULL;
1281 }
1282
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001283 // Return OK when no error was detected.
1284 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001285 ret = OK;
1286
1287theend:
1288 vim_free(skip_until);
1289 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001290 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001291 need_wait_return |= saved_wait_return;
1292 return ret;
1293}
1294
1295/*
1296 * Handle the body of a lambda. *arg points to the "{", process statements
1297 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001298 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001299 * When successful "rettv" is set to a funcref.
1300 */
1301 static int
1302lambda_function_body(
1303 char_u **arg,
1304 typval_T *rettv,
1305 evalarg_T *evalarg,
1306 garray_T *newargs,
1307 garray_T *argtypes,
1308 int varargs,
1309 garray_T *default_args,
1310 char_u *ret_type)
1311{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001312 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001313 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001314 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001315 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001316 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001317 exarg_T eap;
1318 garray_T newlines;
1319 char_u *cmdline = NULL;
1320 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001321 partial_T *pt;
1322 char_u *name;
1323 int lnum_save = -1;
1324 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1325
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001326 *arg = skipwhite(*arg + 1);
1327 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001328 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001329 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001330 return FAIL;
1331 }
1332
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001333 CLEAR_FIELD(eap);
1334 eap.cmdidx = CMD_block;
1335 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001336 eap.cmdlinep = &cmdline;
1337 eap.skip = !evaluate;
1338 if (evalarg->eval_cctx != NULL)
1339 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1340 else
1341 {
1342 eap.getline = evalarg->eval_getline;
1343 eap.cookie = evalarg->eval_cookie;
1344 }
1345
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001346 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001347 if (get_function_body(&eap, &newlines, NULL,
1348 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001349 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001350
1351 // When inside a lambda must add the function lines to evalarg.eval_ga.
1352 evalarg->eval_break_count += newlines.ga_len;
1353 if (gap->ga_itemsize > 0)
1354 {
1355 int idx;
1356 char_u *last;
1357 size_t plen;
1358 char_u *pnl;
1359
1360 for (idx = 0; idx < newlines.ga_len; ++idx)
1361 {
1362 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1363
Bram Moolenaarecb66452021-05-18 15:09:18 +02001364 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001365 goto erret;
1366
1367 // Going to concatenate the lines after parsing. For an empty or
1368 // comment line use an empty string.
1369 // Insert NL characters at the start of each line, the string will
1370 // be split again later in .get_lambda_tv().
1371 if (*p == NUL || vim9_comment_start(p))
1372 p = (char_u *)"";
1373 plen = STRLEN(p);
1374 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1375 if (pnl != NULL)
1376 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001377 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1378 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001379 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001380 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001381 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001382 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001383 // more is following after the "}", which was skipped
1384 last = cmdline;
1385 else
1386 // nothing is following the "}"
1387 last = (char_u *)"}";
1388 plen = STRLEN(last);
1389 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1390 if (pnl != NULL)
1391 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001392 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1393 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001394 }
1395
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001396 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001397 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001398 garray_T *tfgap = &evalarg->eval_tofree_ga;
1399
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001400 // Something comes after the "}".
1401 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001402
1403 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001404 if (ga_grow(tfgap, 1) == OK)
1405 {
1406 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1407 evalarg->eval_using_cmdline = TRUE;
1408 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001409 }
1410 else
1411 *arg = (char_u *)"";
1412
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001413 if (!evaluate)
1414 {
1415 ret = OK;
1416 goto erret;
1417 }
1418
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001419 name = get_lambda_name();
Bram Moolenaare01e5212023-01-08 20:31:18 +00001420 ufunc = alloc_ufunc(name);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001421 if (ufunc == NULL)
1422 goto erret;
1423 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001424 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001425 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001426 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001427 ufunc->uf_refcount = 1;
1428 ufunc->uf_args = *newargs;
1429 newargs->ga_data = NULL;
1430 ufunc->uf_def_args = *default_args;
1431 default_args->ga_data = NULL;
1432 ufunc->uf_func_type = &t_func_any;
1433
1434 // error messages are for the first function line
1435 lnum_save = SOURCING_LNUM;
1436 SOURCING_LNUM = sourcing_lnum_top;
1437
1438 // parse argument types
h-eastb895b0f2023-09-24 15:46:31 +02001439 if (parse_argument_types(ufunc, argtypes, varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001440 {
1441 SOURCING_LNUM = lnum_save;
1442 goto erret;
1443 }
1444
1445 // parse the return type, if any
1446 if (parse_return_type(ufunc, ret_type) == FAIL)
1447 goto erret;
1448
1449 pt = ALLOC_CLEAR_ONE(partial_T);
1450 if (pt == NULL)
1451 goto erret;
1452 pt->pt_func = ufunc;
1453 pt->pt_refcount = 1;
1454
1455 ufunc->uf_lines = newlines;
1456 newlines.ga_data = NULL;
1457 if (sandbox)
1458 ufunc->uf_flags |= FC_SANDBOX;
1459 if (!ASCII_ISUPPER(*ufunc->uf_name))
1460 ufunc->uf_flags |= FC_VIM9;
1461 ufunc->uf_script_ctx = current_sctx;
1462 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1463 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1464 set_function_type(ufunc);
1465
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001466 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1467
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001468 rettv->vval.v_partial = pt;
1469 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001470 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001471 ret = OK;
1472
1473erret:
1474 if (lnum_save >= 0)
1475 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001476 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001477 if (newargs != NULL)
1478 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001479 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001480 if (ufunc != NULL)
1481 {
1482 func_clear(ufunc, TRUE);
1483 func_free(ufunc, TRUE);
1484 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001485 return ret;
1486}
1487
1488/*
1489 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001490 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001491 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001492 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1493 */
1494 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001495get_lambda_tv(
1496 char_u **arg,
1497 typval_T *rettv,
1498 int types_optional,
1499 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001500{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001501 int evaluate = evalarg != NULL
1502 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001503 garray_T newargs;
1504 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001505 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001506 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001507 garray_T default_args;
h-eastb895b0f2023-09-24 15:46:31 +02001508 garray_T arg_objm;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001509 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001510 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001511 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001512 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001513 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001514 char_u *s;
1515 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001516 int *old_eval_lavars = eval_lavars_used;
1517 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001518 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001519 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001520 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001521 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001522 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001523 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001524
Bram Moolenaar4525a572022-02-13 11:57:33 +00001525 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001526 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001527
1528 ga_init(&newargs);
1529 ga_init(&newlines);
1530
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001531 // First, check if this is really a lambda expression. "->" or "=>" must
1532 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001533 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001534 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
h-eastb895b0f2023-09-24 15:46:31 +02001535 types_optional ? &argtypes : NULL, types_optional,
1536 types_optional ? &arg_objm : NULL, evalarg,
1537 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001538 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001539 {
1540 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001541 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001542 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001543 ga_clear(&arg_objm);
1544 }
Bram Moolenaar0346b792021-01-31 22:18:29 +01001545 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001546 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001547
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001548 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001549 if (evaluate)
1550 pnewargs = &newargs;
1551 else
1552 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001553 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001554 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
h-eastb895b0f2023-09-24 15:46:31 +02001555 types_optional ? &argtypes : NULL, types_optional,
1556 types_optional ? &arg_objm : NULL, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001557 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001558 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001559 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001560 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001561 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001562 {
1563 if (types_optional)
h-eastb895b0f2023-09-24 15:46:31 +02001564 {
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001565 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02001566 ga_clear(&arg_objm);
1567 }
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001568 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001569 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001570 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001571 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001572
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001573 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1574 if (ret_type != NULL)
1575 {
1576 ret_type = vim_strsave(ret_type);
1577 tofree2 = ret_type;
1578 }
1579
Bram Moolenaare38eab22019-12-05 21:50:01 +01001580 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001581 if (evaluate)
1582 eval_lavars_used = &eval_lavars;
1583
Bram Moolenaar65c44152020-12-24 15:14:01 +01001584 *arg = skipwhite_and_linebreak(*arg, evalarg);
1585
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001586 // Recognize "{" as the start of a function body.
1587 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001588 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001589 if (evalarg == NULL)
1590 // cannot happen?
1591 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001592 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001593 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1594 types_optional ? &argtypes : NULL, varargs,
1595 &default_args, ret_type) == FAIL)
1596 goto errret;
1597 goto theend;
1598 }
1599 if (default_args.ga_len > 0)
1600 {
1601 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001602 goto errret;
1603 }
1604
Bram Moolenaare38eab22019-12-05 21:50:01 +01001605 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001606 start = *arg;
1607 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001608 if (ret == FAIL)
1609 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001610
Bram Moolenaar65c44152020-12-24 15:14:01 +01001611 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001612 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001613 *arg = skipwhite_and_linebreak(*arg, evalarg);
1614 if (**arg != '}')
1615 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001616 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001617 goto errret;
1618 }
1619 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001620 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001621
1622 if (evaluate)
1623 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001624 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001625 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001626 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001627 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001628 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001629
Bram Moolenaare01e5212023-01-08 20:31:18 +00001630 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001631 if (fp == NULL)
1632 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001633 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001634 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001635 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001636 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001637
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001638 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001639 if (ga_grow(&newlines, 1) == FAIL)
1640 goto errret;
1641
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001642 // If there are line breaks, we need to split up the string.
1643 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001644 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001645 line_end = end;
1646
1647 // Add "return " before the expression (or the first line).
1648 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001649 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001650 if (p == NULL)
1651 goto errret;
1652 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1653 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001654 vim_strncpy(p + 7, start, line_end - start);
1655
1656 if (line_end != end)
1657 {
1658 // Add more lines, split by line breaks. Thus is used when a
1659 // lambda with { cmds } is encountered.
1660 while (*line_end == '\n')
1661 {
1662 if (ga_grow(&newlines, 1) == FAIL)
1663 goto errret;
1664 start = line_end + 1;
1665 line_end = vim_strchr(start, '\n');
1666 if (line_end == NULL)
1667 line_end = end;
1668 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1669 vim_strnsave(start, line_end - start);
1670 }
1671 }
1672
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001673 if (strstr((char *)p + 7, "a:") == NULL)
1674 // No a: variables are used for sure.
1675 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001676
1677 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001678 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001679 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001680 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001681 if (types_optional)
1682 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001683 if (parse_argument_types(fp, &argtypes,
h-eastb895b0f2023-09-24 15:46:31 +02001684 vim9script && varargs, NULL, NULL, 0) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001685 goto errret;
1686 if (ret_type != NULL)
1687 {
1688 fp->uf_ret_type = parse_type(&ret_type,
1689 &fp->uf_type_list, TRUE);
1690 if (fp->uf_ret_type == NULL)
1691 goto errret;
1692 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001693 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001694 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001695 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001696
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001697 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001698 if (current_funccal != NULL && eval_lavars)
1699 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001700 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001701 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001702 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001703 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001704
1705#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001706 if (prof_def_func())
1707 func_do_profile(fp);
1708#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001709 if (sandbox)
1710 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001711 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001712 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001713 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001714 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001715 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001716 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001717 // Use the line number of the arguments.
1718 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001719
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001720 function_using_block_scopes(fp, evalarg->eval_cstack);
1721
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001722 pt->pt_func = fp;
1723 pt->pt_refcount = 1;
1724 rettv->vval.v_partial = pt;
1725 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001726
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001727 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001728 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001729
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001730theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001731 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001732 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001733 if (types_optional)
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001734 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001735 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001736 ga_clear(&arg_objm);
1737 }
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001738
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001739 return OK;
1740
1741errret:
1742 ga_clear_strings(&newargs);
1743 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001744 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001745 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001746 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001747 ga_clear_strings(&argtypes);
Yegappan Lakshmanan3aa11442023-09-25 12:13:17 +02001748 ga_clear(&arg_objm);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001749 if (fp != NULL)
1750 vim_free(fp->uf_arg_types);
1751 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001752 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001753 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001754 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001755 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001756 return FAIL;
1757}
1758
1759/*
1760 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1761 * name it contains, otherwise return "name".
1762 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1763 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001764 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1765 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001766 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001767 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001768 */
1769 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001770deref_func_name(
1771 char_u *name,
1772 int *lenp,
1773 partial_T **partialp,
1774 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001775 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001776 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001777 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001778{
1779 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001780 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001781 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001782 char_u *s = NULL;
1783 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001784 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001785
1786 if (partialp != NULL)
1787 *partialp = NULL;
1788
1789 cc = name[*lenp];
1790 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001791
Bram Moolenaar71f21932022-01-07 18:20:55 +00001792 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001793 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001794 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001795 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001796 tv = &v->di_tv;
1797 }
1798 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1799 {
1800 imported_T *import;
1801 char_u *p = name;
1802 int len = *lenp;
1803
1804 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001805 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001806 p = name + 2;
1807 len -= 2;
1808 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001809 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001810
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001811 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001812 if (import != NULL)
1813 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001814 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001815 if (new_function)
1816 semsg(_(e_redefining_imported_item_str), name);
1817 else
1818 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001819 name[len] = cc;
1820 *lenp = 0;
1821 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001822 }
1823 }
1824
1825 if (tv != NULL)
1826 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001827 if (found_var != NULL)
1828 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001829 if (tv->v_type == VAR_FUNC)
1830 {
1831 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001832 {
1833 *lenp = 0;
1834 return (char_u *)""; // just in case
1835 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001836 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001837 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001838 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001839
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001840 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001841 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001842 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001843
1844 if (pt == NULL)
1845 {
1846 *lenp = 0;
1847 return (char_u *)""; // just in case
1848 }
1849 if (partialp != NULL)
1850 *partialp = pt;
1851 s = partial_name(pt);
1852 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001853 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001854
1855 if (s != NULL)
1856 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001857 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001858 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001859 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001860
1861 if (sv != NULL)
1862 *type = sv->sv_type;
1863 }
1864 return s;
1865 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001866 }
1867
1868 return name;
1869}
1870
1871/*
1872 * Give an error message with a function name. Handle <SNR> things.
1873 * "ermsg" is to be passed without translation, use N_() instead of _().
1874 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001875 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001876emsg_funcname(char *ermsg, char_u *name)
1877{
Bram Moolenaar54598062022-01-13 12:05:09 +00001878 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001879
Bram Moolenaar54598062022-01-13 12:05:09 +00001880 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001881 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001882 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001883 if (p != name)
1884 vim_free(p);
1885}
1886
1887/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001888 * Get function arguments at "*arg" and advance it.
1889 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001890 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001891 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001892 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001893get_func_arguments(
1894 char_u **arg,
1895 evalarg_T *evalarg,
1896 int partial_argc,
1897 typval_T *argvars,
1898 int *argcount)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001899{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001900 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001901 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001902 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001903 int evaluate = evalarg == NULL
1904 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001905
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001906 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001907 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001908 // skip the '(' or ',' and possibly line breaks
1909 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1910
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001911 if (*argp == ')' || *argp == ',' || *argp == NUL)
1912 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001913 if (eval1(&argp, &argvars[*argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001914 {
1915 ret = FAIL;
1916 break;
1917 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001918 ++*argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001919 // The comma should come right after the argument, but this wasn't
1920 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001921 if (vim9script)
1922 {
1923 if (*argp != ',' && *skipwhite(argp) == ',')
1924 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001925 if (evaluate)
1926 semsg(_(e_no_white_space_allowed_before_str_str),
1927 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001928 ret = FAIL;
1929 break;
1930 }
1931 }
1932 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001933 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001934 if (*argp != ',')
1935 break;
Christian Brabandt00cb2472023-09-05 20:46:25 +02001936 if (vim9script && !IS_WHITE_NL_OR_NUL(argp[1]))
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001937 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001938 if (evaluate)
1939 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001940 ret = FAIL;
1941 break;
1942 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001943 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001944
Bram Moolenaare6b53242020-07-01 17:28:33 +02001945 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001946 if (*argp == ')')
1947 ++argp;
1948 else
1949 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001950 *arg = argp;
1951 return ret;
1952}
1953
1954/*
1955 * Call a function and put the result in "rettv".
1956 * Return OK or FAIL.
1957 */
1958 int
1959get_func_tv(
1960 char_u *name, // name of the function
1961 int len, // length of "name" or -1 to use strlen()
1962 typval_T *rettv,
1963 char_u **arg, // argument, pointing to the '('
1964 evalarg_T *evalarg, // for line continuation
1965 funcexe_T *funcexe) // various values
1966{
1967 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001968 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001969 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1970 int argcount = 0; // number of arguments found
1971 int vim9script = in_vim9script();
1972 int evaluate = evalarg == NULL
1973 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
1974
1975 argp = *arg;
1976 ret = get_func_arguments(&argp, evalarg,
1977 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
1978 argvars, &argcount);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001979
1980 if (ret == OK)
1981 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001982 int i = 0;
1983 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001984
1985 if (get_vim_var_nr(VV_TESTING))
1986 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001987 // Prepare for calling test_garbagecollect_now(), need to know
1988 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001989 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001990 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001991 for (i = 0; i < argcount; ++i)
1992 if (ga_grow(&funcargs, 1) == OK)
1993 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1994 &argvars[i];
1995 }
1996
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001997 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00001998 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001999 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02002000 // An error in a builtin function does not return FAIL, but we do
2001 // want to abort further processing if an error was given.
2002 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02002003 clear_tv(rettv);
2004 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002005
2006 funcargs.ga_len -= i;
2007 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00002008 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002009 {
2010 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00002011 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002012 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00002013 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002014 }
2015
2016 while (--argcount >= 0)
2017 clear_tv(&argvars[argcount]);
2018
Bram Moolenaar4525a572022-02-13 11:57:33 +00002019 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02002020 *arg = argp;
2021 else
2022 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002023 return ret;
2024}
2025
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002026/*
2027 * Return TRUE if "p" starts with "<SID>" or "s:".
2028 * Only works if eval_fname_script() returned non-zero for "p"!
2029 */
2030 static int
2031eval_fname_sid(char_u *p)
2032{
2033 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
2034}
2035
2036/*
2037 * In a script change <SID>name() and s:name() to K_SNR 123_name().
2038 * Change <SNR>123_name() to K_SNR 123_name().
2039 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002040 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002041 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01002042 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00002043fname_trans_sid(
2044 char_u *name,
2045 char_u *fname_buf,
2046 char_u **tofree,
2047 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002048{
2049 int llen;
2050 char_u *fname;
2051 int i;
2052
2053 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002054 if (llen == 0)
2055 return name; // no prefix
2056
2057 fname_buf[0] = K_SPECIAL;
2058 fname_buf[1] = KS_EXTRA;
2059 fname_buf[2] = (int)KE_SNR;
2060 i = 3;
2061 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002062 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002063 if (current_sctx.sc_sid <= 0)
2064 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002065 else
2066 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002067 sprintf((char *)fname_buf + 3, "%ld_",
2068 (long)current_sctx.sc_sid);
2069 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002070 }
2071 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002072 if (i + STRLEN(name + llen) < FLEN_FIXED)
2073 {
2074 STRCPY(fname_buf + i, name + llen);
2075 fname = fname_buf;
2076 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002077 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002078 {
2079 fname = alloc(i + STRLEN(name + llen) + 1);
2080 if (fname == NULL)
2081 *error = FCERR_OTHER;
2082 else
2083 {
2084 *tofree = fname;
2085 mch_memmove(fname, fname_buf, (size_t)i);
2086 STRCPY(fname + i, name + llen);
2087 }
2088 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002089 return fname;
2090}
2091
2092/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002093 * Concatenate the script ID and function name into "<SNR>99_name".
2094 * "buffer" must have size MAX_FUNC_NAME_LEN.
2095 */
2096 void
2097func_name_with_sid(char_u *name, int sid, char_u *buffer)
2098{
2099 // A script-local function is stored as "<SNR>99_name".
2100 buffer[0] = K_SPECIAL;
2101 buffer[1] = KS_EXTRA;
2102 buffer[2] = (int)KE_SNR;
2103 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2104 (long)sid, name);
2105}
2106
2107/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108 * Find a function "name" in script "sid".
2109 */
2110 static ufunc_T *
2111find_func_with_sid(char_u *name, int sid)
2112{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002113 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002114 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002115
Bram Moolenaarb8822442022-01-11 15:24:05 +00002116 if (!SCRIPT_ID_VALID(sid))
2117 return NULL; // not in a script
2118
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002119 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002120 hi = hash_find(&func_hashtab, buffer);
2121 if (!HASHITEM_EMPTY(hi))
2122 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002123 return NULL;
2124}
2125
2126/*
2127 * Find a function "name" in script "sid" prefixing the autoload prefix.
2128 */
2129 static ufunc_T *
2130find_func_with_prefix(char_u *name, int sid)
2131{
2132 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002133 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002134 scriptitem_T *si;
2135
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002136 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002137 return NULL; // already has the prefix
2138 if (!SCRIPT_ID_VALID(sid))
2139 return NULL; // not in a script
2140 si = SCRIPT_ITEM(sid);
2141 if (si->sn_autoload_prefix != NULL)
2142 {
2143 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2144 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002145 char_u *namep;
2146
2147 // skip a "<SNR>99_" prefix
2148 namep = untrans_function_name(name);
2149 if (namep == NULL)
2150 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002151
2152 // An exported function in an autoload script is stored as
2153 // "dir#path#name".
2154 if (len < sizeof(buffer))
2155 auto_name = buffer;
2156 else
2157 auto_name = alloc(len);
2158 if (auto_name != NULL)
2159 {
2160 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002161 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002162 hi = hash_find(&func_hashtab, auto_name);
2163 if (auto_name != buffer)
2164 vim_free(auto_name);
2165 if (!HASHITEM_EMPTY(hi))
2166 return HI2UF(hi);
2167 }
2168 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169
2170 return NULL;
2171}
2172
2173/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002174 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002175 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2176 * functions.
2177 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002178 * Return NULL for unknown function.
2179 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002180 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002181find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002182{
2183 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002184 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002186 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002188 // Find script-local function before global one.
2189 if (in_vim9script() && eval_isnamec1(*name)
2190 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002192 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2193 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002194 if (func != NULL)
2195 return func;
2196 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002197 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2198 {
2199 char_u *p = name + 5;
2200 long sid;
2201
2202 // printable "<SNR>123_Name" form
2203 sid = getdigits(&p);
2204 if (*p == '_')
2205 {
2206 func = find_func_with_sid(p + 1, (int)sid);
2207 if (func != NULL)
2208 return func;
2209 }
2210 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002211 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002212
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002213 if ((flags & FFED_NO_GLOBAL) == 0)
2214 {
2215 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002216 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002217 if (!HASHITEM_EMPTY(hi))
2218 return HI2UF(hi);
2219 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220
Bram Moolenaarb8822442022-01-11 15:24:05 +00002221 // Find autoload function if this is an autoload script.
2222 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2223 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224}
2225
2226/*
2227 * Find a function by name, return pointer to it in ufuncs.
2228 * "cctx" is passed in a :def function to find imported functions.
2229 * Return NULL for unknown or dead function.
2230 */
2231 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002232find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002233{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002234 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235
2236 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2237 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002238 return NULL;
2239}
2240
2241/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002242 * Return TRUE if "ufunc" is a global function.
2243 */
2244 int
2245func_is_global(ufunc_T *ufunc)
2246{
2247 return ufunc->uf_name[0] != K_SPECIAL;
2248}
2249
2250/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002251 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2252 */
2253 int
2254func_requires_g_prefix(ufunc_T *ufunc)
2255{
2256 return ufunc->uf_name[0] != K_SPECIAL
2257 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002258 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
2259 && !isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002260}
2261
2262/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002263 * Copy the function name of "fp" to buffer "buf".
2264 * "buf" must be able to hold the function name plus three bytes.
2265 * Takes care of script-local function names.
2266 */
2267 static void
2268cat_func_name(char_u *buf, ufunc_T *fp)
2269{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002270 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002271 {
2272 STRCPY(buf, "<SNR>");
2273 STRCAT(buf, fp->uf_name + 3);
2274 }
2275 else
2276 STRCPY(buf, fp->uf_name);
2277}
2278
2279/*
2280 * Add a number variable "name" to dict "dp" with value "nr".
2281 */
2282 static void
2283add_nr_var(
2284 dict_T *dp,
2285 dictitem_T *v,
2286 char *name,
2287 varnumber_T nr)
2288{
2289 STRCPY(v->di_key, name);
2290 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002291 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002292 v->di_tv.v_type = VAR_NUMBER;
2293 v->di_tv.v_lock = VAR_FIXED;
2294 v->di_tv.vval.v_number = nr;
2295}
2296
2297/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002298 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002299 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002300 static void
2301free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002302{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002303 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002304
Bram Moolenaarca16c602022-09-06 18:57:08 +01002305 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002306 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002307 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002308
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002309 // When garbage collecting a funccall_T may be freed before the
2310 // function that references it, clear its uf_scoped field.
2311 // The function may have been redefined and point to another
2312 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002313 if (fp != NULL && fp->uf_scoped == fc)
2314 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002315 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002316 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002317
Bram Moolenaarca16c602022-09-06 18:57:08 +01002318 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002319 vim_free(fc);
2320}
2321
2322/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002323 * Free "fc" and what it contains.
2324 * Can be called only when "fc" is kept beyond the period of it called,
2325 * i.e. after cleanup_function_call(fc).
2326 */
2327 static void
2328free_funccal_contents(funccall_T *fc)
2329{
2330 listitem_T *li;
2331
2332 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002333 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002334
2335 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002336 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002337
2338 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002339 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002340 clear_tv(&li->li_tv);
2341
2342 free_funccal(fc);
2343}
2344
2345/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002346 * Handle the last part of returning from a function: free the local hashtable.
2347 * Unless it is still in use by a closure.
2348 */
2349 static void
2350cleanup_function_call(funccall_T *fc)
2351{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002352 int may_free_fc = fc->fc_refcount <= 0;
2353 int free_fc = TRUE;
2354
Bram Moolenaarca16c602022-09-06 18:57:08 +01002355 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002356
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002357 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002358 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2359 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002360 else
2361 free_fc = FALSE;
2362
2363 // If the a:000 list and the l: and a: dicts are not referenced and
2364 // there is no closure using it, we can free the funccall_T and what's
2365 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002366 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2367 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002368 else
2369 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002370 int todo;
2371 hashitem_T *hi;
2372 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002373
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002374 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002375
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002376 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002377 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002378 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002379 {
2380 if (!HASHITEM_EMPTY(hi))
2381 {
2382 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002383 di = HI2DI(hi);
2384 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002385 }
2386 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002387 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002388
Bram Moolenaarca16c602022-09-06 18:57:08 +01002389 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2390 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002391 else
2392 {
2393 listitem_T *li;
2394
2395 free_fc = FALSE;
2396
2397 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002398 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002399 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002400 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002401
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002402 if (free_fc)
2403 free_funccal(fc);
2404 else
2405 {
2406 static int made_copy = 0;
2407
2408 // "fc" is still in use. This can happen when returning "a:000",
2409 // assigning "l:" to a global variable or defining a closure.
2410 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002411 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002412 previous_funccal = fc;
2413
2414 if (want_garbage_collect)
2415 // If garbage collector is ready, clear count.
2416 made_copy = 0;
2417 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002418 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002419 // We have made a lot of copies, worth 4 Mbyte. This can happen
2420 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002421 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002422 // too much memory.
2423 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002424 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002425 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002426 }
2427}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002428
2429/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002430 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2431 */
2432 static int
2433numbered_function(char_u *name)
2434{
2435 return isdigit(*name)
2436 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2437}
2438
2439/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002440 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002441 * 1. ordinary names, function defined with :function or :def;
2442 * can start with "<SNR>123_" literally or with K_SPECIAL.
2443 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002444 * For the first we only count the name stored in func_hashtab as a reference,
2445 * using function() does not count as a reference, because the function is
2446 * looked up by name.
2447 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002448 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002449func_name_refcount(char_u *name)
2450{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002451 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002452}
2453
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454/*
2455 * Unreference "fc": decrement the reference count and free it when it
2456 * becomes zero. "fp" is detached from "fc".
2457 * When "force" is TRUE we are exiting.
2458 */
2459 static void
2460funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2461{
2462 funccall_T **pfc;
2463 int i;
2464
2465 if (fc == NULL)
2466 return;
2467
2468 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002469 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2470 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2471 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2472 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 {
2474 if (fc == *pfc)
2475 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002476 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477 free_funccal_contents(fc);
2478 return;
2479 }
2480 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002481 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2482 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2483 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484}
2485
2486/*
2487 * Remove the function from the function hashtable. If the function was
2488 * deleted while it still has references this was already done.
2489 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2490 */
2491 static int
2492func_remove(ufunc_T *fp)
2493{
2494 hashitem_T *hi;
2495
2496 // Return if it was already virtually deleted.
2497 if (fp->uf_flags & FC_DEAD)
2498 return FALSE;
2499
2500 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002501 if (HASHITEM_EMPTY(hi))
2502 return FALSE;
2503
2504 // When there is a def-function index do not actually remove the
2505 // function, so we can find the index when defining the function again.
2506 // Do remove it when it's a copy.
2507 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002509 fp->uf_flags |= FC_DEAD;
2510 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002512 hash_remove(&func_hashtab, hi, "remove function");
2513 fp->uf_flags |= FC_DELETED;
2514 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515}
2516
2517 static void
2518func_clear_items(ufunc_T *fp)
2519{
2520 ga_clear_strings(&(fp->uf_args));
2521 ga_clear_strings(&(fp->uf_def_args));
2522 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002524 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002525 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002526 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002527
2528 // Increment the refcount of this function to avoid it being freed
2529 // recursively when the partial is freed.
2530 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002531 partial_unref(fp->uf_partial);
2532 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002533 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002534
2535#ifdef FEAT_LUA
2536 if (fp->uf_cb_free != NULL)
2537 {
2538 fp->uf_cb_free(fp->uf_cb_state);
2539 fp->uf_cb_free = NULL;
2540 }
2541
2542 fp->uf_cb_state = NULL;
2543 fp->uf_cb = NULL;
2544#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545#ifdef FEAT_PROFILE
2546 VIM_CLEAR(fp->uf_tml_count);
2547 VIM_CLEAR(fp->uf_tml_total);
2548 VIM_CLEAR(fp->uf_tml_self);
2549#endif
2550}
2551
2552/*
2553 * Free all things that a function contains. Does not free the function
2554 * itself, use func_free() for that.
2555 * When "force" is TRUE we are exiting.
2556 */
2557 static void
2558func_clear(ufunc_T *fp, int force)
2559{
2560 if (fp->uf_cleared)
2561 return;
2562 fp->uf_cleared = TRUE;
2563
2564 // clear this function
2565 func_clear_items(fp);
2566 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002567 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568}
2569
2570/*
2571 * Free a function and remove it from the list of functions. Does not free
2572 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002573 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002574 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002576 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002577func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578{
2579 // Only remove it when not done already, otherwise we would remove a newer
2580 // version of the function with the same name.
2581 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2582 func_remove(fp);
2583
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002584 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002585 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002586 if (fp->uf_dfunc_idx > 0)
2587 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002588 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002590 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002591 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002592 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593}
2594
2595/*
2596 * Free all things that a function contains and free the function itself.
2597 * When "force" is TRUE we are exiting.
2598 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002599 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600func_clear_free(ufunc_T *fp, int force)
2601{
2602 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002603 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2604 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002605 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002606 else
2607 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608}
2609
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002610/*
2611 * Copy already defined function "lambda" to a new function with name "global".
2612 * This is for when a compiled function defines a global function.
2613 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002614 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002615copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002616 char_u *lambda,
2617 char_u *global,
2618 loopvarinfo_T *loopvarinfo,
2619 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002620{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002621 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002622 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002623
2624 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002625 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002626 semsg(_(e_lambda_function_not_found_str), lambda);
2627 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002628 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002629
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002630 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002631 if (fp != NULL)
2632 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002633 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002634 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002635 return FAIL;
2636 }
2637
Bram Moolenaare01e5212023-01-08 20:31:18 +00002638 fp = alloc_ufunc(global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002639 if (fp == NULL)
2640 return FAIL;
2641
2642 fp->uf_varargs = ufunc->uf_varargs;
2643 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2644 fp->uf_def_status = ufunc->uf_def_status;
2645 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2646 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2647 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2648 == FAIL
2649 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2650 goto failed;
2651
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002652 if (ufunc->uf_arg_types != NULL)
2653 {
2654 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2655 if (fp->uf_arg_types == NULL)
2656 goto failed;
2657 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2658 sizeof(type_T *) * fp->uf_args.ga_len);
2659 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002660 if (ufunc->uf_va_name != NULL)
2661 {
2662 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2663 if (fp->uf_va_name == NULL)
2664 goto failed;
2665 }
2666 fp->uf_ret_type = ufunc->uf_ret_type;
2667
2668 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002669
2670 fp->uf_name_exp = NULL;
2671 set_ufunc_name(fp, global);
2672
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002673 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002674
2675 // the referenced dfunc_T is now used one more time
2676 link_def_function(fp);
2677
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002678 // Create a partial to store the context of the function where it was
2679 // instantiated. Only needs to be done once. Do this on the original
2680 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002681 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2682 {
2683 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2684
2685 if (pt == NULL)
2686 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002687 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002688 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002689 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002690 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002691 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002692 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002693 }
2694
2695 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002696
2697failed:
2698 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002699 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002700}
2701
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002702static int funcdepth = 0;
2703
2704/*
2705 * Increment the function call depth count.
2706 * Return FAIL when going over 'maxfuncdepth'.
2707 * Otherwise return OK, must call funcdepth_decrement() later!
2708 */
2709 int
2710funcdepth_increment(void)
2711{
2712 if (funcdepth >= p_mfd)
2713 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002714 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002715 return FAIL;
2716 }
2717 ++funcdepth;
2718 return OK;
2719}
2720
2721 void
2722funcdepth_decrement(void)
2723{
2724 --funcdepth;
2725}
2726
2727/*
2728 * Get the current function call depth.
2729 */
2730 int
2731funcdepth_get(void)
2732{
2733 return funcdepth;
2734}
2735
2736/*
2737 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002738 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002739 * times as funcdepth_increment().
2740 */
2741 void
2742funcdepth_restore(int depth)
2743{
2744 funcdepth = depth;
2745}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002746
2747/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002748 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2749 * "rettv".
2750 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2751 * Returns NULL when allocation fails.
2752 */
2753 funccall_T *
2754create_funccal(ufunc_T *fp, typval_T *rettv)
2755{
2756 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2757
2758 if (fc == NULL)
2759 return NULL;
2760 fc->fc_caller = current_funccal;
2761 current_funccal = fc;
2762 fc->fc_func = fp;
2763 func_ptr_ref(fp);
2764 fc->fc_rettv = rettv;
2765 return fc;
2766}
2767
2768/*
2769 * To be called when returning from a compiled function; restores
2770 * current_funccal.
2771 */
2772 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002773remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002774{
2775 funccall_T *fc = current_funccal;
2776
2777 current_funccal = fc->fc_caller;
2778 free_funccal(fc);
2779}
2780
2781/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002782 * Call a user function.
2783 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002784 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002785call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002786 ufunc_T *fp, // pointer to function
2787 int argcount, // nr of args
2788 typval_T *argvars, // arguments
2789 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002790 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002791 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002792{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002793 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002794 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002795 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002796 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002797 funccall_T *fc;
2798 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002799 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002800 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002801 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002802 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002803 int i;
2804 int ai;
2805 int islambda = FALSE;
2806 char_u numbuf[NUMBUFLEN];
2807 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002808 typval_T *tv_to_free[MAX_FUNC_ARGS];
2809 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002810#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002811 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002812#endif
ichizok7e5fe382023-04-15 13:17:50 +01002813 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002814
Bram Moolenaarb2049902021-01-24 12:53:53 +01002815#ifdef FEAT_PROFILE
2816 CLEAR_FIELD(profile_info);
2817#endif
2818
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002819 // If depth of calling is getting too high, don't execute the function.
2820 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002821 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002822 rettv->v_type = VAR_NUMBER;
2823 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002824 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002825 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002826
Bram Moolenaare38eab22019-12-05 21:50:01 +01002827 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002828
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002829 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002830 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00002831 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002832 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002833 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002834 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2835 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002836 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002837 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002838
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002839 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002841#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002842 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002843#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002844 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002845#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002846 if (do_profiling == PROF_YES)
2847 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002848#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002849 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002850 if (call_def_function(fp, argcount, argvars, 0,
2851 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
2852 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002853 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002854#ifdef FEAT_PROFILE
2855 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002856 || (caller != NULL && caller->uf_profiling)))
2857 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002858#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002859 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002860 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002861 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 }
2863
Bram Moolenaar38453522021-11-28 22:00:12 +00002864 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002865
2866 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002867 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2868 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2869 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002870 */
2871 /*
2872 * Init l: variables.
2873 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002874 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002875 if (selfdict != NULL)
2876 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002877 // Set l:self to "selfdict". Use "name" to avoid a warning from
2878 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002879 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002880 name = v->di_key;
2881 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002882 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002883 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002884 v->di_tv.v_type = VAR_DICT;
2885 v->di_tv.v_lock = 0;
2886 v->di_tv.vval.v_dict = selfdict;
2887 ++selfdict->dv_refcount;
2888 }
2889
2890 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002891 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002892 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002893 * Set a:000 to a list with room for the "..." arguments.
2894 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002895 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002896 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002897 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002898 (varnumber_T)(argcount >= fp->uf_args.ga_len
2899 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002900 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002901 if ((fp->uf_flags & FC_NOARGS) == 0)
2902 {
2903 // Use "name" to avoid a warning from some compiler that checks the
2904 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002905 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002906 name = v->di_key;
2907 STRCPY(name, "000");
2908 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002909 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002910 v->di_tv.v_type = VAR_LIST;
2911 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002912 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002913 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002914 CLEAR_FIELD(fc->fc_l_varlist);
2915 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2916 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002917
2918 /*
2919 * Set a:firstline to "firstline" and a:lastline to "lastline".
2920 * Set a:name to named arguments.
2921 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002922 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002923 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002924 if ((fp->uf_flags & FC_NOARGS) == 0)
2925 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002926 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2927 "firstline", (varnumber_T)funcexe->fe_firstline);
2928 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2929 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002930 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002931 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002932 {
2933 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002934 typval_T def_rettv;
2935 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002936
2937 ai = i - fp->uf_args.ga_len;
2938 if (ai < 0)
2939 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002940 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002941 name = FUNCARG(fp, i);
2942 if (islambda)
2943 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002944
2945 // evaluate named argument default expression
2946 isdefault = ai + fp->uf_def_args.ga_len >= 0
2947 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2948 && argvars[i].vval.v_number == VVAL_NONE));
2949 if (isdefault)
2950 {
2951 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002952
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002953 def_rettv.v_type = VAR_NUMBER;
2954 def_rettv.vval.v_number = -1;
2955
2956 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2957 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002958 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002959 {
2960 default_arg_err = 1;
2961 break;
2962 }
2963 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002964 }
2965 else
2966 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002967 if ((fp->uf_flags & FC_NOARGS) != 0)
2968 // Bail out if no a: arguments used (in lambda).
2969 break;
2970
Bram Moolenaare38eab22019-12-05 21:50:01 +01002971 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002972 sprintf((char *)numbuf, "%d", ai + 1);
2973 name = numbuf;
2974 }
2975 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2976 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002977 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002978 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002979 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002980 }
2981 else
2982 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002983 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002984 if (v == NULL)
2985 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002986 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002987 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002988
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002989 // Note: the values are copied directly to avoid alloc/free.
2990 // "argvars" must have VAR_FIXED for v_lock.
2991 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002992 v->di_tv.v_lock = VAR_FIXED;
2993
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002994 if (isdefault)
2995 // Need to free this later, no matter where it's stored.
2996 tv_to_free[tv_to_free_len++] = &v->di_tv;
2997
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002998 if (addlocal)
2999 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003000 // Named arguments should be accessed without the "a:" prefix in
3001 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003002 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003003 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003004 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003005 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003006 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003007
3008 if (ai >= 0 && ai < MAX_FUNC_ARGS)
3009 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003010 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003011
3012 li->li_tv = argvars[i];
3013 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003014 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003015 }
3016 }
3017
Bram Moolenaare38eab22019-12-05 21:50:01 +01003018 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003019 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02003020
3021 if (fp->uf_flags & FC_SANDBOX)
3022 {
3023 using_sandbox = TRUE;
3024 ++sandbox;
3025 }
3026
Bram Moolenaar25e0f582020-05-25 22:36:50 +02003027 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01003028 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003029 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003030 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003031 ++no_wait_return;
3032 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003033
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003034 smsg(_("calling %s"), SOURCING_NAME);
3035 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003036 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003037 char_u buf[MSG_BUF_LEN];
3038 char_u numbuf2[NUMBUFLEN];
3039 char_u *tofree;
3040 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003041
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003042 msg_puts("(");
3043 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003044 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003045 if (i > 0)
3046 msg_puts(", ");
3047 if (argvars[i].v_type == VAR_NUMBER)
3048 msg_outnum((long)argvars[i].vval.v_number);
3049 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003050 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003051 // Do not want errors such as E724 here.
3052 ++emsg_off;
3053 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
3054 --emsg_off;
3055 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003056 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003057 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003058 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003059 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3060 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003061 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003062 msg_puts((char *)s);
3063 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003064 }
3065 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003066 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003067 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003068 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003069 msg_puts("\n"); // don't overwrite this either
3070
3071 verbose_leave_scroll();
3072 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003073 }
3074#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003075 if (do_profiling == PROF_YES)
3076 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003077 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003078#endif
3079
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003080 // "legacy" does not apply to commands in the function
3081 sticky_cmdmod_flags = 0;
3082
Bram Moolenaar98aff652022-09-06 21:02:35 +01003083 // If called from a compiled :def function the execution context must be
3084 // hidden, any deferred functions need to be added to the function being
3085 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003086 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003087
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003088 save_current_sctx = current_sctx;
3089 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003090 save_did_emsg = did_emsg;
3091 did_emsg = FALSE;
3092
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003093 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003094 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003095 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003096 retval = FCERR_FAILED;
3097 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003098 else if (islambda)
3099 {
3100 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3101
3102 // A Lambda always has the command "return {expr}". It is much faster
3103 // to evaluate {expr} directly.
3104 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003105 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003106 --ex_nesting_level;
3107 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003108 else
3109 // call do_cmdline() to execute the lines
3110 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003111 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3112
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003113 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003114 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003115
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003116 if (RedrawingDisabled > 0)
3117 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003118
Bram Moolenaare38eab22019-12-05 21:50:01 +01003119 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003120 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3121 {
3122 clear_tv(rettv);
3123 rettv->v_type = VAR_NUMBER;
3124 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003125
3126 // In corner cases returning a "failed" value is not backwards
3127 // compatible. Only do this for Vim9 script.
3128 if (in_vim9script())
3129 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003130 }
3131
3132#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003133 if (do_profiling == PROF_YES)
3134 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003135 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003136
3137 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3138 profile_may_end_func(&profile_info, fp, caller);
3139 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003140#endif
3141
Bram Moolenaare38eab22019-12-05 21:50:01 +01003142 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003143 if (p_verbose >= 12)
3144 {
3145 ++no_wait_return;
3146 verbose_enter_scroll();
3147
3148 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003149 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003150 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003151 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003152 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003153 else
3154 {
3155 char_u buf[MSG_BUF_LEN];
3156 char_u numbuf2[NUMBUFLEN];
3157 char_u *tofree;
3158 char_u *s;
3159
Bram Moolenaare38eab22019-12-05 21:50:01 +01003160 // The value may be very long. Skip the middle part, so that we
3161 // have some idea how it starts and ends. smsg() would always
3162 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003163 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003164 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003165 --emsg_off;
3166 if (s != NULL)
3167 {
3168 if (vim_strsize(s) > MSG_BUF_CLEN)
3169 {
3170 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3171 s = buf;
3172 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003173 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003174 vim_free(tofree);
3175 }
3176 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003177 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003178
3179 verbose_leave_scroll();
3180 --no_wait_return;
3181 }
3182
ichizok7e5fe382023-04-15 13:17:50 +01003183 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003184 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003185 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003186 restore_current_ectx(save_current_ectx);
3187
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003188#ifdef FEAT_PROFILE
3189 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003190 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003191#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003192 if (using_sandbox)
3193 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003194 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003195
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003196 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003197 {
3198 ++no_wait_return;
3199 verbose_enter_scroll();
3200
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003201 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003202 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003203
3204 verbose_leave_scroll();
3205 --no_wait_return;
3206 }
3207
3208 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003209 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003210 for (i = 0; i < tv_to_free_len; ++i)
3211 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003212 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003213
3214 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003215}
3216
3217/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003218 * Check the argument count for user function "fp".
3219 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3220 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003221 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003222check_user_func_argcount(ufunc_T *fp, int argcount)
3223{
3224 int regular_args = fp->uf_args.ga_len;
3225
3226 if (argcount < regular_args - fp->uf_def_args.ga_len)
3227 return FCERR_TOOFEW;
3228 else if (!has_varargs(fp) && argcount > regular_args)
3229 return FCERR_TOOMANY;
3230 return FCERR_UNKNOWN;
3231}
3232
3233/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003235 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003236 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237call_user_func_check(
3238 ufunc_T *fp,
3239 int argcount,
3240 typval_T *argvars,
3241 typval_T *rettv,
3242 funcexe_T *funcexe,
3243 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003244{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003245 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003246
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003247#ifdef FEAT_LUA
3248 if (fp->uf_flags & FC_CFUNC)
3249 {
3250 cfunc_T cb = fp->uf_cb;
3251
3252 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3253 }
3254#endif
3255
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003256 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3257 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003258 error = check_user_func_argcount(fp, argcount);
3259 if (error != FCERR_UNKNOWN)
3260 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003261
Bram Moolenaar50824712020-12-20 21:10:17 +01003262 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003263 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003265 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003267 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 int did_save_redo = FALSE;
3269 save_redo_T save_redo;
3270
3271 /*
3272 * Call the user function.
3273 * Save and restore search patterns, script variables and
3274 * redo buffer.
3275 */
3276 save_search_patterns();
3277 if (!ins_compl_active())
3278 {
3279 saveRedobuff(&save_redo);
3280 did_save_redo = TRUE;
3281 }
3282 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003283 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003284 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3286 // Function was unreferenced while being used, free it now.
3287 func_clear_free(fp, FALSE);
3288 if (did_save_redo)
3289 restoreRedobuff(&save_redo);
3290 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003291 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003294}
3295
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003296static funccal_entry_T *funccal_stack = NULL;
3297
3298/*
3299 * Save the current function call pointer, and set it to NULL.
3300 * Used when executing autocommands and for ":source".
3301 */
3302 void
3303save_funccal(funccal_entry_T *entry)
3304{
3305 entry->top_funccal = current_funccal;
3306 entry->next = funccal_stack;
3307 funccal_stack = entry;
3308 current_funccal = NULL;
3309}
3310
3311 void
3312restore_funccal(void)
3313{
3314 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003315 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003316 else
3317 {
3318 current_funccal = funccal_stack->top_funccal;
3319 funccal_stack = funccal_stack->next;
3320 }
3321}
3322
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003323 funccall_T *
3324get_current_funccal(void)
3325{
3326 return current_funccal;
3327}
3328
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003329/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003330 * Return TRUE when currently at the script level:
3331 * - not in a function
3332 * - not executing an autocommand
3333 * Note that when an autocommand sources a script the result is FALSE;
3334 */
3335 int
3336at_script_level(void)
3337{
3338 return current_funccal == NULL && autocmd_match == NULL;
3339}
3340
3341/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003342 * Mark all functions of script "sid" as deleted.
3343 */
3344 void
3345delete_script_functions(int sid)
3346{
3347 hashitem_T *hi;
3348 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003349 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003350 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003351 size_t len;
3352
3353 buf[0] = K_SPECIAL;
3354 buf[1] = KS_EXTRA;
3355 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003356 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003357 len = STRLEN(buf);
3358
Bram Moolenaarce658352020-07-31 23:47:12 +02003359 while (todo > 0)
3360 {
3361 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003362 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003363 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003364 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003365 fp = HI2UF(hi);
3366 if (STRNCMP(fp->uf_name, buf, len) == 0)
3367 {
3368 int changed = func_hashtab.ht_changed;
3369
3370 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003371
3372 if (fp->uf_calls > 0)
3373 {
3374 // Function is executing, don't free it but do remove
3375 // it from the hashtable.
3376 if (func_remove(fp))
3377 fp->uf_refcount--;
3378 }
3379 else
3380 {
3381 func_clear(fp, TRUE);
3382 // When clearing a function another function can be
3383 // cleared as a side effect. When that happens start
3384 // over.
3385 if (changed != func_hashtab.ht_changed)
3386 break;
3387 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003388 }
3389 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003390 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003391 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003392}
3393
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003394#if defined(EXITFREE) || defined(PROTO)
3395 void
3396free_all_functions(void)
3397{
3398 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003399 ufunc_T *fp;
3400 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003401 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003402 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003403
Bram Moolenaare38eab22019-12-05 21:50:01 +01003404 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003405 while (current_funccal != NULL)
3406 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003407 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003408 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003409 if (current_funccal == NULL && funccal_stack != NULL)
3410 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003411 }
3412
Bram Moolenaare38eab22019-12-05 21:50:01 +01003413 // First clear what the functions contain. Since this may lower the
3414 // reference count of a function, it may also free a function and change
3415 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003416 while (todo > 0)
3417 {
3418 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003419 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003420 if (!HASHITEM_EMPTY(hi))
3421 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 // clear the def function index now
3423 fp = HI2UF(hi);
3424 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003425 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426
Bram Moolenaare38eab22019-12-05 21:50:01 +01003427 // Only free functions that are not refcounted, those are
3428 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003429 if (func_name_refcount(fp->uf_name))
3430 ++skipped;
3431 else
3432 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003433 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003434 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003435 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003436 {
3437 skipped = 0;
3438 break;
3439 }
3440 }
3441 --todo;
3442 }
3443 }
3444
Bram Moolenaare38eab22019-12-05 21:50:01 +01003445 // Now actually free the functions. Need to start all over every time,
3446 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003447 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003448 while (func_hashtab.ht_used > skipped)
3449 {
3450 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003451 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003452 if (!HASHITEM_EMPTY(hi))
3453 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003454 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003455 // Only free functions that are not refcounted, those are
3456 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003457 fp = HI2UF(hi);
3458 if (func_name_refcount(fp->uf_name))
3459 ++skipped;
3460 else
3461 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003462 if (func_free(fp, FALSE) == OK)
3463 {
3464 skipped = 0;
3465 break;
3466 }
3467 // did not actually free it
3468 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003469 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003470 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003471 }
3472 if (skipped == 0)
3473 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474
3475 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003476}
3477#endif
3478
3479/*
3480 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003481 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3482 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003483 * "len" is the length of "name", or -1 for NUL terminated.
3484 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003486builtin_function(char_u *name, int len)
3487{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003488 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003489
Bram Moolenaara26b9702020-04-18 19:53:28 +02003490 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003491 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003492 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3493 {
3494 if (name[i] == AUTOLOAD_CHAR)
3495 return FALSE;
3496 if (!eval_isnamec(name[i]))
3497 {
3498 // "name.something" is not a builtin function
3499 if (name[i] == '.')
3500 return FALSE;
3501 break;
3502 }
3503 }
3504 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003505}
3506
3507 int
3508func_call(
3509 char_u *name,
3510 typval_T *args,
3511 partial_T *partial,
3512 dict_T *selfdict,
3513 typval_T *rettv)
3514{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003515 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003516 listitem_T *item;
3517 typval_T argv[MAX_FUNC_ARGS + 1];
3518 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003519 int r = 0;
3520
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003521 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003522 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003523 {
3524 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3525 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003526 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003527 break;
3528 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003529 // Make a copy of each argument. This is needed to be able to set
3530 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003531 copy_tv(&item->li_tv, &argv[argc++]);
3532 }
3533
3534 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003535 {
3536 funcexe_T funcexe;
3537
Bram Moolenaara80faa82020-04-12 19:37:17 +02003538 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003539 funcexe.fe_firstline = curwin->w_cursor.lnum;
3540 funcexe.fe_lastline = curwin->w_cursor.lnum;
3541 funcexe.fe_evaluate = TRUE;
3542 funcexe.fe_partial = partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003543 if (partial != NULL)
3544 {
3545 funcexe.fe_object = partial->pt_obj;
3546 if (funcexe.fe_object != NULL)
3547 ++funcexe.fe_object->obj_refcount;
3548 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003549 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003550 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3551 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003552
Bram Moolenaare38eab22019-12-05 21:50:01 +01003553 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003554 while (argc > 0)
3555 clear_tv(&argv[--argc]);
3556
3557 return r;
3558}
3559
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003560static int callback_depth = 0;
3561
3562 int
3563get_callback_depth(void)
3564{
3565 return callback_depth;
3566}
3567
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003568/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003569 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003570 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003571 */
3572 int
3573call_callback(
3574 callback_T *callback,
3575 int len, // length of "name" or -1 to use strlen()
3576 typval_T *rettv, // return value goes here
3577 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003578 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003579 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003580{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003581 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003582 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003583
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003584 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3585 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02003586 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003587 funcexe.fe_evaluate = TRUE;
3588 funcexe.fe_partial = callback->cb_partial;
Yegappan Lakshmanan1ace49f2023-10-15 09:53:41 +02003589 if (callback->cb_partial != NULL)
3590 {
3591 funcexe.fe_object = callback->cb_partial->pt_obj;
3592 if (funcexe.fe_object != NULL)
3593 ++funcexe.fe_object->obj_refcount;
3594 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003595 ++callback_depth;
3596 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3597 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003598
3599 // When a :def function was called that uses :try an error would be turned
3600 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003601 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003602 {
3603 need_rethrow = FALSE;
3604 handle_did_throw();
3605 }
3606
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003607 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003608}
3609
3610/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003611 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003612 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003613 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3614 */
3615 varnumber_T
3616call_callback_retnr(
3617 callback_T *callback,
3618 int argcount, // number of "argvars"
3619 typval_T *argvars) // vars for arguments, must have "argcount"
3620 // PLUS ONE elements!
3621{
3622 typval_T rettv;
3623 varnumber_T retval;
3624
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003625 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003626 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003627
3628 retval = tv_get_number_chk(&rettv, NULL);
3629 clear_tv(&rettv);
3630 return retval;
3631}
3632
3633/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 * Give an error message for the result of a function.
3635 * Nothing if "error" is FCERR_NONE.
3636 */
3637 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003638user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639{
3640 switch (error)
3641 {
3642 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003643 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003644 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003645 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003646 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 break;
3648 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003649 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 break;
3651 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003652 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 break;
3654 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003655 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 break;
3657 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003658 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 break;
3660 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003661 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662 break;
3663 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003664 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3665 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003667 case FCERR_OTHER:
3668 case FCERR_FAILED:
3669 // assume the error message was already given
3670 break;
3671 case FCERR_NONE:
3672 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673 }
3674}
3675
3676/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003677 * Check the argument types "argvars[argcount]" for "name" using the
3678 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3679 * is already included in "argvars[]".
3680 * Will do nothing if "funcexe->fe_check_type" is NULL or
3681 * "funcexe->fe_evaluate" is FALSE;
3682 * Returns an FCERR_ value.
3683 */
3684 static funcerror_T
3685may_check_argument_types(
3686 funcexe_T *funcexe,
3687 typval_T *argvars,
3688 int argcount,
3689 int base_included,
3690 char_u *name)
3691{
3692 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3693 {
3694 // Check that the argument types are OK for the types of the funcref.
3695 if (check_argument_types(funcexe->fe_check_type,
3696 argvars, argcount,
3697 base_included ? NULL : funcexe->fe_basetv,
3698 name) == FAIL)
3699 return FCERR_OTHER;
3700 }
3701 return FCERR_NONE;
3702}
3703
3704/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003705 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003706 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003707 * Return FAIL when the function can't be called, OK otherwise.
3708 * Also returns OK when an error was encountered while executing the function.
3709 */
3710 int
3711call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003712 char_u *funcname, // name of the function
3713 int len, // length of "name" or -1 to use strlen()
3714 typval_T *rettv, // return value goes here
3715 int argcount_in, // number of "argvars"
3716 typval_T *argvars_in, // vars for arguments, must have "argcount"
3717 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003718 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003719{
3720 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003721 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003722 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003723 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003724 char_u fname_buf[FLEN_FIXED + 1];
3725 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003726 char_u *fname = NULL;
3727 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003728 int argcount = argcount_in;
3729 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003730 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003731 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003732 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003733 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003734 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003735 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003736 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003737 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003738
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003739 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3740 // even when call_func() returns FAIL.
3741 rettv->v_type = VAR_UNKNOWN;
3742
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003743 if (partial != NULL)
3744 fp = partial->pt_func;
3745 if (fp == NULL)
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01003746 fp = funcexe->fe_ufunc;
3747
3748 if (fp == NULL)
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003749 {
3750 // Make a copy of the name, if it comes from a funcref variable it
3751 // could be changed or deleted in the called function.
3752 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3753 if (name == NULL)
3754 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003755
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003756 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3757 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003758
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003759 if (funcexe->fe_doesrange != NULL)
3760 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003761
3762 if (partial != NULL)
3763 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003764 // When the function has a partial with a dict and there is a dict
3765 // argument, use the dict argument. That is backwards compatible.
3766 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003767 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003768 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003769 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003770 {
3771 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003772 {
3773 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3774 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003775 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003776 goto theend;
3777 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003778 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003779 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003780 for (i = 0; i < argcount_in; ++i)
3781 argv[i + argv_clear] = argvars_in[i];
3782 argvars = argv;
3783 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003784
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003785 if (funcexe->fe_check_type != NULL
3786 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003787 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003788 // Now funcexe->fe_check_type is missing the added arguments,
3789 // make a copy of the type with the correction.
3790 check_type = *funcexe->fe_check_type;
3791 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003792 check_type.tt_args = check_type_args;
3793 CLEAR_FIELD(check_type_args);
3794 for (i = 0; i < check_type.tt_argcount; ++i)
3795 check_type_args[i + partial->pt_argc] =
3796 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003797 check_type.tt_argcount += partial->pt_argc;
3798 check_type.tt_min_argcount += partial->pt_argc;
3799 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003800 }
3801 }
3802
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003803 if (error == FCERR_NONE)
3804 // check the argument types if possible
3805 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
3806 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003807
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003808 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003809 {
3810 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003811 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003812
Bram Moolenaar333894b2020-08-01 18:53:07 +02003813 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003814 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003815 {
3816 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003817 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003818 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003819
Bram Moolenaare38eab22019-12-05 21:50:01 +01003820 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003821 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003822 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003823
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003824 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003825 {
3826 /*
3827 * User defined function.
3828 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003829 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003830 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003831 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003832 if (fp != NULL && !is_global && in_vim9script()
3833 && func_requires_g_prefix(fp))
3834 // In Vim9 script g: is required to find a global
3835 // non-autoload function.
3836 fp = NULL;
3837 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003838
Bram Moolenaare38eab22019-12-05 21:50:01 +01003839 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003840 if (fp == NULL
3841 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003842 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003843 && !aborting())
3844 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003845 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003846 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003847 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003848 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003849 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3850 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003851 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003852 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003853 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003854 if (fp == NULL)
3855 {
3856 char_u *p = untrans_function_name(rfname);
3857
3858 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003859 // Don't do this if the name starts with "s:".
3860 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003861 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003862 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003863
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003864 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003865 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003866 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003867 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003868 int need_arg_check = FALSE;
3869 if (funcexe->fe_check_type == NULL)
3870 {
3871 funcexe->fe_check_type = fp->uf_func_type;
3872 need_arg_check = TRUE;
3873 }
3874
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003875 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003876 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003877 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003878 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003879 argv_clear, fp);
3880 need_arg_check = TRUE;
3881 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003882
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003883 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003884 {
3885 // Method call: base->Method()
3886 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003887 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003888 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003889 argvars = argv;
3890 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003891 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003892 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003893
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003894 // Check the argument types now that the function type and all
3895 // argument values are known, if not done above.
3896 if (need_arg_check)
3897 error = may_check_argument_types(funcexe, argvars, argcount,
3898 TRUE, (name != NULL) ? name : funcname);
3899 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
3900 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003902 }
3903 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003904 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003905 {
3906 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003907 * expr->method(): Find the method name in the table, call its
3908 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003909 */
3910 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003911 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003912 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003913 else
3914 {
3915 /*
3916 * Find the function name in the table, call its implementation.
3917 */
3918 error = call_internal_func(fname, argcount, argvars, rettv);
3919 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003920
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003921 /*
3922 * The function call (or "FuncUndefined" autocommand sequence) might
3923 * have been aborted by an error, an interrupt, or an explicitly thrown
3924 * exception that has not been caught so far. This situation can be
3925 * tested for by calling aborting(). For an error in an internal
3926 * function or for the "E132" error in call_user_func(), however, the
3927 * throw point at which the "force_abort" flag (temporarily reset by
3928 * emsg()) is normally updated has not been reached yet. We need to
3929 * update that flag first to make aborting() reliable.
3930 */
3931 update_force_abort();
3932 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003933 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003934 ret = OK;
3935
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003936theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003937 /*
3938 * Report an error unless the argument evaluation or function call has been
3939 * cancelled due to an aborting error, an interrupt, or an exception.
3940 */
3941 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003942 user_func_error(error, (name != NULL) ? name : funcname,
3943 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003944
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003945 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003946 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003947 clear_tv(&argv[--argv_clear + argv_base]);
3948
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003949 vim_free(tofree);
3950 vim_free(name);
3951
3952 return ret;
3953}
3954
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003955/*
3956 * Call a function without arguments, partial or dict.
3957 * This is like call_func() when the call is only "FuncName()".
3958 * To be used by "expr" options.
3959 * Returns NOTDONE when the function could not be found.
3960 */
3961 int
3962call_simple_func(
3963 char_u *funcname, // name of the function
3964 int len, // length of "name" or -1 to use strlen()
3965 typval_T *rettv) // return value goes here
3966{
3967 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003968 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003969 char_u fname_buf[FLEN_FIXED + 1];
3970 char_u *tofree = NULL;
3971 char_u *name;
3972 char_u *fname;
3973 char_u *rfname;
3974 int is_global = FALSE;
3975 ufunc_T *fp;
3976
3977 rettv->v_type = VAR_NUMBER; // default rettv is number zero
3978 rettv->vval.v_number = 0;
3979
3980 // Make a copy of the name, an option can be changed in the function.
3981 name = vim_strnsave(funcname, len);
3982 if (name == NULL)
3983 return ret;
3984
3985 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3986
3987 // Skip "g:" before a function name.
3988 if (fname[0] == 'g' && fname[1] == ':')
3989 {
3990 is_global = TRUE;
3991 rfname = fname + 2;
3992 }
3993 else
3994 rfname = fname;
3995 fp = find_func(rfname, is_global);
3996 if (fp != NULL && !is_global && in_vim9script()
3997 && func_requires_g_prefix(fp))
3998 // In Vim9 script g: is required to find a global non-autoload
3999 // function.
4000 fp = NULL;
4001 if (fp == NULL)
4002 ret = NOTDONE;
4003 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
4004 error = FCERR_DELETED;
4005 else if (fp != NULL)
4006 {
4007 typval_T argvars[1];
4008 funcexe_T funcexe;
4009
4010 argvars[0].v_type = VAR_UNKNOWN;
4011 CLEAR_FIELD(funcexe);
4012 funcexe.fe_evaluate = TRUE;
4013
4014 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
4015 if (error == FCERR_NONE)
4016 ret = OK;
4017 }
4018
4019 user_func_error(error, name, FALSE);
4020 vim_free(tofree);
4021 vim_free(name);
4022
4023 return ret;
4024}
4025
Bram Moolenaar682d0a12020-07-19 20:48:59 +02004026 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027printable_func_name(ufunc_T *fp)
4028{
4029 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
4030}
4031
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004032/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004033 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
4034 * TRUE. Otherwise return FALSE.
4035 */
4036 static int
4037function_list_modified(int prev_ht_changed)
4038{
4039 if (prev_ht_changed != func_hashtab.ht_changed)
4040 {
4041 emsg(_(e_function_list_was_modified));
4042 return TRUE;
4043 }
4044 return FALSE;
4045}
4046
4047/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004048 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004049 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004050 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004051list_func_head(ufunc_T *fp, int indent)
4052{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004053 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004054 int j;
4055
4056 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004057
4058 // a timer at the more prompt may have deleted the function
4059 if (function_list_modified(prev_ht_changed))
4060 return FAIL;
4061
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004062 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004063 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004064 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004065 msg_puts("def ");
4066 else
4067 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004069 msg_putchar('(');
4070 for (j = 0; j < fp->uf_args.ga_len; ++j)
4071 {
4072 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004073 msg_puts(", ");
4074 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 if (fp->uf_arg_types != NULL)
4076 {
4077 char *tofree;
4078
4079 msg_puts(": ");
4080 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4081 vim_free(tofree);
4082 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004083 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4084 {
4085 msg_puts(" = ");
4086 msg_puts(((char **)(fp->uf_def_args.ga_data))
4087 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4088 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004089 }
4090 if (fp->uf_varargs)
4091 {
4092 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004093 msg_puts(", ");
4094 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004095 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 if (fp->uf_va_name != NULL)
4097 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004098 if (!fp->uf_varargs)
4099 {
4100 if (j)
4101 msg_puts(", ");
4102 msg_puts("...");
4103 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004105 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 {
4107 char *tofree;
4108
4109 msg_puts(": ");
4110 msg_puts(type_name(fp->uf_va_type, &tofree));
4111 vim_free(tofree);
4112 }
4113 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004114 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004115
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004116 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004117 {
4118 if (fp->uf_ret_type != &t_void)
4119 {
4120 char *tofree;
4121
4122 msg_puts(": ");
4123 msg_puts(type_name(fp->uf_ret_type, &tofree));
4124 vim_free(tofree);
4125 }
4126 }
4127 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004128 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004129 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004130 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004131 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004132 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004133 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004134 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004135 msg_clr_eos();
4136 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004137 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004138
4139 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004140}
4141
4142/*
4143 * Get a function name, translating "<SID>" and "<SNR>".
4144 * Also handles a Funcref in a List or Dictionary.
4145 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004146 * Set "*is_global" to TRUE when the function must be global, unless
4147 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004148 * flags:
4149 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004150 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004151 * TFN_QUIET: be quiet
4152 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004153 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004154 * Advances "pp" to just after the function name (if no error).
4155 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004156 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004157trans_function_name(
4158 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004159 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004160 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004161 int flags)
4162{
4163 return trans_function_name_ext(pp, is_global, skip, flags,
4164 NULL, NULL, NULL, NULL);
4165}
4166
4167/*
4168 * trans_function_name() with extra arguments.
4169 * "fdp", "partial", "type" and "ufunc" can be NULL.
4170 */
4171 char_u *
4172trans_function_name_ext(
4173 char_u **pp,
4174 int *is_global,
4175 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004176 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004177 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004178 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004179 type_T **type, // return: type of funcref
4180 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004181{
4182 char_u *name = NULL;
4183 char_u *start;
4184 char_u *end;
4185 int lead;
4186 char_u sid_buf[20];
4187 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004188 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004189 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004190 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004191 int vim9script = in_vim9script();
4192 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004193
4194 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004195 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004196 start = *pp;
4197
Bram Moolenaare38eab22019-12-05 21:50:01 +01004198 // Check for hard coded <SNR>: already translated function ID (from a user
4199 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004200 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4201 && (*pp)[2] == (int)KE_SNR)
4202 {
4203 *pp += 3;
4204 len = get_id_len(pp) + 3;
4205 return vim_strnsave(start, len);
4206 }
4207
Bram Moolenaare38eab22019-12-05 21:50:01 +01004208 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4209 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004210 lead = eval_fname_script(start);
4211 if (lead > 2)
4212 start += lead;
4213
Bram Moolenaare38eab22019-12-05 21:50:01 +01004214 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004215 end = get_lval(start, NULL, &lv, FALSE, skip,
4216 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004217 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004218 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004219 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004220 {
4221 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004222 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004223 goto theend;
4224 }
4225 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4226 {
4227 /*
4228 * Report an invalid expression in braces, unless the expression
4229 * evaluation has been cancelled due to an aborting error, an
4230 * interrupt, or an exception.
4231 */
4232 if (!aborting())
4233 {
4234 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004235 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004236 }
4237 else
4238 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4239 goto theend;
4240 }
4241
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004242 if (lv.ll_ufunc != NULL)
4243 {
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004244 if (ufunc != NULL)
4245 *ufunc = lv.ll_ufunc;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004246 name = vim_strsave(lv.ll_ufunc->uf_name);
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01004247 *pp = end;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004248 goto theend;
4249 }
4250
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004251 if (lv.ll_tv != NULL)
4252 {
4253 if (fdp != NULL)
4254 {
4255 fdp->fd_dict = lv.ll_dict;
4256 fdp->fd_newkey = lv.ll_newkey;
4257 lv.ll_newkey = NULL;
4258 fdp->fd_di = lv.ll_di;
4259 }
4260 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4261 {
4262 name = vim_strsave(lv.ll_tv->vval.v_string);
4263 *pp = end;
4264 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004265 else if (lv.ll_tv->v_type == VAR_CLASS
4266 && lv.ll_tv->vval.v_class != NULL)
4267 {
4268 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4269 *pp = end;
4270 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004271 else if (lv.ll_tv->v_type == VAR_PARTIAL
4272 && lv.ll_tv->vval.v_partial != NULL)
4273 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004274 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004275 *pp = end;
4276 if (partial != NULL)
4277 *partial = lv.ll_tv->vval.v_partial;
4278 }
4279 else
4280 {
4281 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4282 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004283 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004284 else
4285 *pp = end;
4286 name = NULL;
4287 }
4288 goto theend;
4289 }
4290
4291 if (lv.ll_name == NULL)
4292 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004293 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004294 *pp = end;
4295 goto theend;
4296 }
4297
Bram Moolenaare38eab22019-12-05 21:50:01 +01004298 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004299 if (lv.ll_exp_name != NULL)
4300 {
4301 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004302 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004303 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004304 if (name == lv.ll_exp_name)
4305 name = NULL;
4306 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004307 else if (lv.ll_sid > 0)
4308 {
4309 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4310 int cc = *lv.ll_name_end;
4311
4312 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4313 *lv.ll_name_end = NUL;
4314 if (si->sn_autoload_prefix != NULL)
4315 {
4316 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4317 }
4318 else
4319 {
4320 sid_buf[0] = K_SPECIAL;
4321 sid_buf[1] = KS_EXTRA;
4322 sid_buf[2] = (int)KE_SNR;
4323 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004324 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004325 name = concat_str(sid_buf, lv.ll_name);
4326 }
4327 *lv.ll_name_end = cc;
4328 *pp = end;
4329 goto theend;
4330 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004331 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004332 {
4333 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004334 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004335 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004336 if (name == *pp)
4337 name = NULL;
4338 }
4339 if (name != NULL)
4340 {
4341 name = vim_strsave(name);
4342 *pp = end;
4343 if (STRNCMP(name, "<SNR>", 5) == 0)
4344 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004345 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004346 name[0] = K_SPECIAL;
4347 name[1] = KS_EXTRA;
4348 name[2] = (int)KE_SNR;
4349 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4350 }
4351 goto theend;
4352 }
4353
4354 if (lv.ll_exp_name != NULL)
4355 {
4356 len = (int)STRLEN(lv.ll_exp_name);
4357 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4358 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4359 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004360 // When there was "s:" already or the name expanded to get a
4361 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004362 lv.ll_name += 2;
4363 len -= 2;
4364 lead = 2;
4365 }
4366 }
4367 else
4368 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004369 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004370 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004371 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004372 if (lv.ll_name[0] == 'g')
4373 {
4374 if (is_global != NULL)
4375 {
4376 *is_global = TRUE;
4377 }
4378 else
4379 {
4380 // dropping "g:" without setting "is_global" won't work in
4381 // Vim9script, put it back later
4382 prefix_g = TRUE;
4383 extra = 2;
4384 }
4385 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004386 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004387 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004388 len = (int)(end - lv.ll_name);
4389 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004390 if (len <= 0)
4391 {
4392 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004393 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004394 goto theend;
4395 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004396
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004397 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004398 // starts with a lower case character: dict.func(). Or when in a class.
4399 vim9_local = ASCII_ISUPPER(*start) && vim9script
4400 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004402 /*
4403 * Copy the function name to allocated memory.
4404 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4405 * Accept <SNR>123_name() outside a script.
4406 */
4407 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004408 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004409 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004410 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004411 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004412 {
Kota Kato948a3892022-08-16 16:09:59 +01004413 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4414 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004415 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004416 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004417 goto theend;
4418 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004420 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004421 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004423 || eval_fname_sid(*pp))
4424 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004426 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004427 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004428 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004429 goto theend;
4430 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004431 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004432 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433 extra = 3 + (int)STRLEN(sid_buf);
4434 else
4435 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004436 }
4437 }
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004438 // The function name must start with an upper case letter (unless it is a
4439 // Vim9 class new() function or a Vim9 class private method)
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004440 else if (!(flags & TFN_INT)
4441 && (builtin_function(lv.ll_name, len)
4442 || (vim9script && *lv.ll_name == '_'))
Yegappan Lakshmanancd7293b2023-08-27 19:18:23 +02004443 && !((flags & TFN_IN_CLASS)
4444 && (STRNCMP(lv.ll_name, "new", 3) == 0
4445 || (*lv.ll_name == '_'))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004446 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004447 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004448 : e_function_name_must_start_with_capital_or_s_str),
4449 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004450 goto theend;
4451 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004452 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004453 {
4454 char_u *cp = vim_strchr(lv.ll_name, ':');
4455
4456 if (cp != NULL && cp < end)
4457 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004458 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004459 goto theend;
4460 }
4461 }
4462
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004464 if (name != NULL)
4465 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004466 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004467 {
4468 name[0] = K_SPECIAL;
4469 name[1] = KS_EXTRA;
4470 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004471 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004472 STRCPY(name + 3, sid_buf);
4473 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004474 else if (prefix_g)
4475 {
4476 name[0] = 'g';
4477 name[1] = ':';
4478 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4480 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004481 }
4482 *pp = end;
4483
4484theend:
4485 clear_lval(&lv);
4486 return name;
4487}
4488
4489/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004490 * Assuming "name" is the result of trans_function_name() and it was prefixed
4491 * to use the script-local name, return the unmodified name (points into
4492 * "name"). Otherwise return NULL.
4493 * This can be used to first search for a script-local function and fall back
4494 * to the global function if not found.
4495 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004496 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004497untrans_function_name(char_u *name)
4498{
4499 char_u *p;
4500
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004501 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004502 {
4503 p = vim_strchr(name, '_');
4504 if (p != NULL)
4505 return p + 1;
4506 }
4507 return NULL;
4508}
4509
4510/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004511 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4512 * current script ID and returns the expanded function name. The caller should
4513 * free the returned name. If not called from a script context or the function
4514 * name doesn't start with these prefixes, then returns NULL.
4515 * This doesn't check whether the script-local function exists or not.
4516 */
4517 char_u *
4518get_scriptlocal_funcname(char_u *funcname)
4519{
4520 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004521 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004522 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004523 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004524
4525 if (funcname == NULL)
4526 return NULL;
4527
4528 if (STRNCMP(funcname, "s:", 2) != 0
4529 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004530 {
4531 ufunc_T *ufunc;
4532
4533 // The function name does not have a script-local prefix. Try finding
4534 // it when in a Vim9 script and there is no "g:" prefix.
4535 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4536 return NULL;
4537 ufunc = find_func(funcname, FALSE);
4538 if (ufunc == NULL || func_is_global(ufunc)
4539 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4540 return NULL;
4541 ++p;
4542 off = 0;
4543 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004544 else
4545 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004546
4547 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4548 {
4549 emsg(_(e_using_sid_not_in_script_context));
4550 return NULL;
4551 }
4552 // Expand s: prefix into <SNR>nr_<name>
4553 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4554 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004555 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004556 if (newname == NULL)
4557 return NULL;
4558 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004559 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004560
4561 return newname;
4562}
4563
4564/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004565 * Return script-local "fname" with the 3-byte sequence replaced by
4566 * printable <SNR> in allocated memory.
4567 */
4568 char_u *
4569alloc_printable_func_name(char_u *fname)
4570{
4571 char_u *n = alloc(STRLEN(fname + 3) + 6);
4572
4573 if (n != NULL)
4574 {
4575 STRCPY(n, "<SNR>");
4576 STRCPY(n + 5, fname + 3);
4577 }
4578 return n;
4579}
4580
4581/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004582 * Call trans_function_name(), except that a lambda is returned as-is.
4583 * Returns the name in allocated memory.
4584 */
4585 char_u *
4586save_function_name(
4587 char_u **name,
4588 int *is_global,
4589 int skip,
4590 int flags,
4591 funcdict_T *fudi)
4592{
4593 char_u *p = *name;
4594 char_u *saved;
4595
4596 if (STRNCMP(p, "<lambda>", 8) == 0)
4597 {
4598 p += 8;
4599 (void)getdigits(&p);
4600 saved = vim_strnsave(*name, p - *name);
4601 if (fudi != NULL)
4602 CLEAR_POINTER(fudi);
4603 }
4604 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004605 saved = trans_function_name_ext(&p, is_global, skip,
4606 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004607 *name = p;
4608 return saved;
4609}
4610
4611/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004612 * List functions. When "regmatch" is NULL all of then.
4613 * Otherwise functions matching "regmatch".
4614 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004615 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004616list_functions(regmatch_T *regmatch)
4617{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004618 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004619 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004620 hashitem_T *hi;
4621
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004622 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004623 {
4624 if (!HASHITEM_EMPTY(hi))
4625 {
4626 ufunc_T *fp = HI2UF(hi);
4627
4628 --todo;
4629 if ((fp->uf_flags & FC_DEAD) == 0
4630 && (regmatch == NULL
4631 ? !message_filtered(fp->uf_name)
4632 && !func_name_refcount(fp->uf_name)
4633 : !isdigit(*fp->uf_name)
4634 && vim_regexec(regmatch, fp->uf_name, 0)))
4635 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004636 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004637 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004638 if (function_list_modified(prev_ht_changed))
4639 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004640 }
4641 }
4642 }
4643}
4644
4645/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004646 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004647 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4648 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004649 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004650 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4651 * With CF_INTERFACE the function is define inside an interface, only the
4652 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004653 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004654 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004655 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004656define_function(
4657 exarg_T *eap,
4658 char_u *name_arg,
4659 garray_T *lines_to_free,
h-eastb895b0f2023-09-24 15:46:31 +02004660 int class_flags,
4661 ocmember_T *obj_members,
4662 int obj_member_count)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004663{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004664 int j;
4665 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004666 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004667 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004668 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004669 char_u *p;
4670 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004671 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004672 char_u *line_arg = NULL;
4673 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674 garray_T argtypes;
h-eastb895b0f2023-09-24 15:46:31 +02004675 garray_T arg_objm;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004676 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004677 garray_T newlines;
4678 int varargs = FALSE;
4679 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004681 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004682 int fp_allocated = FALSE;
4683 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004684 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004685 dictitem_T *v;
4686 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004687 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004688 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004689 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004690 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004691 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004692 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004693
4694 /*
4695 * ":function" without argument: list functions.
4696 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004697 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004698 {
4699 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004700 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004701 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004702 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004703 }
4704
4705 /*
4706 * ":function /pat": list functions matching pattern.
4707 */
4708 if (*eap->arg == '/')
4709 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004710 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004711 if (!eap->skip)
4712 {
4713 regmatch_T regmatch;
4714
4715 c = *p;
4716 *p = NUL;
4717 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4718 *p = c;
4719 if (regmatch.regprog != NULL)
4720 {
4721 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004722 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004723 vim_regfree(regmatch.regprog);
4724 }
4725 }
4726 if (*p == '/')
4727 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004728 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004729 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004730 }
4731
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004732 ga_init(&newargs);
4733 ga_init(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02004734 ga_init(&arg_objm);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 ga_init(&default_args);
4736
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004737 /*
4738 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004739 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004740 * "name" == func, "fudi.fd_dict" == NULL
4741 * dict.func new dictionary entry
4742 * "name" == NULL, "fudi.fd_dict" set,
4743 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4744 * dict.func existing dict entry with a Funcref
4745 * "name" == func, "fudi.fd_dict" set,
4746 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4747 * dict.func existing dict entry that's not a Funcref
4748 * "name" == NULL, "fudi.fd_dict" set,
4749 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4750 * s:func script-local function name
4751 * g:func global function name, same as "func"
4752 */
4753 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004754 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004755 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004756 // nested function, argument is (args).
4757 paren = TRUE;
4758 CLEAR_FIELD(fudi);
4759 }
4760 else
4761 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004762 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004763 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004764 if (p[0] == 's' && p[1] == ':')
4765 {
4766 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4767 return NULL;
4768 }
4769 p = to_name_end(p, TRUE);
4770 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4771 {
4772 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4773 eap->arg);
4774 return NULL;
4775 }
4776 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004777 }
4778
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004779 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004780 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004781 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004782 paren = (vim_strchr(p, '(') != NULL);
4783 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004784 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004785 /*
4786 * Return on an invalid expression in braces, unless the expression
4787 * evaluation has been cancelled due to an aborting error, an
4788 * interrupt, or an exception.
4789 */
4790 if (!aborting())
4791 {
4792 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004793 semsg(_(e_key_not_present_in_dictionary_str),
4794 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004795 vim_free(fudi.fd_newkey);
4796 return NULL;
4797 }
4798 else
4799 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004800 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004801
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004802 // For "export def FuncName()" in an autoload script the function name
4803 // is stored with the legacy autoload name "dir#script#FuncName" so
4804 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004805 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004806 {
4807 char_u *prefixed = may_prefix_autoload(name);
4808
4809 if (prefixed != NULL && prefixed != name)
4810 {
4811 vim_free(name);
4812 name = prefixed;
4813 }
4814 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004815 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004816 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004817 {
4818 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4819 goto ret_free;
4820 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004821 }
4822
Bram Moolenaare38eab22019-12-05 21:50:01 +01004823 // An error in a function call during evaluation of an expression in magic
4824 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004825 saved_did_emsg = did_emsg;
4826 did_emsg = FALSE;
4827
4828 /*
4829 * ":function func" with only function name: list function.
4830 */
4831 if (!paren)
4832 {
4833 if (!ends_excmd(*skipwhite(p)))
4834 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004835 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004836 goto ret_free;
4837 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004838 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004839 if (eap->nextcmd != NULL)
4840 *p = NUL;
4841 if (!eap->skip && !got_int)
4842 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004843 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004844 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4845 {
4846 char_u *up = untrans_function_name(name);
4847
4848 // With Vim9 script the name was made script-local, if not
4849 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004850 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004851 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004852 }
4853
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004854 if (fp != NULL)
4855 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004856 // Check no function was added or removed from a timer, e.g. at
4857 // the more prompt. "fp" may then be invalid.
4858 int prev_ht_changed = func_hashtab.ht_changed;
4859
4860 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004861 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004862 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4863 {
4864 if (FUNCLINE(fp, j) == NULL)
4865 continue;
4866 msg_putchar('\n');
4867 msg_outnum((long)(j + 1));
4868 if (j < 9)
4869 msg_putchar(' ');
4870 if (j < 99)
4871 msg_putchar(' ');
4872 if (function_list_modified(prev_ht_changed))
4873 break;
4874 msg_prt_line(FUNCLINE(fp, j), FALSE);
4875 out_flush(); // show a line at a time
4876 ui_breakcheck();
4877 }
4878 if (!got_int)
4879 {
4880 msg_putchar('\n');
4881 if (!function_list_modified(prev_ht_changed))
4882 {
4883 if (fp->uf_def_status != UF_NOT_COMPILED)
4884 msg_puts(" enddef");
4885 else
4886 msg_puts(" endfunction");
4887 }
4888 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004889 }
4890 }
4891 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004892 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004893 }
4894 goto ret_free;
4895 }
4896
4897 /*
4898 * ":function name(arg1, arg2)" Define function.
4899 */
4900 p = skipwhite(p);
4901 if (*p != '(')
4902 {
4903 if (!eap->skip)
4904 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004905 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004906 goto ret_free;
4907 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004908 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004909 if (vim_strchr(p, '(') != NULL)
4910 p = vim_strchr(p, '(');
4911 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004912
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004913 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4914 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004915 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004916 goto ret_free;
4917 }
4918
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004919 // In Vim9 script only global functions can be redefined.
4920 if (vim9script && eap->forceit && !is_global)
4921 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004922 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004923 goto ret_free;
4924 }
4925
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004926 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004927
Bram Moolenaar04b12692020-05-04 23:24:44 +02004928 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004929 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004930 // Check the name of the function. Unless it's a dictionary function
4931 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004932 if (name != NULL)
4933 arg = name;
4934 else
4935 arg = fudi.fd_newkey;
4936 if (arg != NULL && (fudi.fd_di == NULL
4937 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4938 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4939 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004940 char_u *name_base = arg;
4941 int i;
4942
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004943 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004944 {
4945 name_base = vim_strchr(arg, '_');
4946 if (name_base == NULL)
4947 name_base = arg + 3;
4948 else
4949 ++name_base;
4950 }
4951 for (i = 0; name_base[i] != NUL && (i == 0
4952 ? eval_isnamec1(name_base[i])
4953 : eval_isnamec(name_base[i])); ++i)
4954 ;
4955 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004956 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004957
4958 // In Vim9 script a function cannot have the same name as a
4959 // variable.
4960 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004961 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4962 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004963 + EVAL_VAR_NO_FUNC) == OK)
4964 {
4965 semsg(_(e_redefining_script_item_str), name_base);
4966 goto ret_free;
4967 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004968 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004969 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004970 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004971 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004972 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004973 goto ret_free;
4974 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004975 }
4976
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004977 // This may get more lines and make the pointers into the first line
4978 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004979 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004980 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00004981 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
h-eastb895b0f2023-09-24 15:46:31 +02004982 eap->cmdidx == CMD_def ? &arg_objm : NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004983 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00004984 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004985 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004986 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004987
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004989 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004990 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004991 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004992 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004993 if (*p != ':')
4994 {
4995 semsg(_(e_no_white_space_allowed_before_colon_str), p);
4996 p = skipwhite(p);
4997 }
4998 else if (!IS_WHITE_OR_NUL(p[1]))
4999 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005000 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02005001 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005003 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02005004 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01005005 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005006 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005007 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005009 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005010 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02005011 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005012 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005013 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02005014 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005015 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005016 else
5017 // find extra arguments "range", "dict", "abort" and "closure"
5018 for (;;)
5019 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01005020 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005021 p = skipwhite(p);
5022 if (STRNCMP(p, "range", 5) == 0)
5023 {
5024 flags |= FC_RANGE;
5025 p += 5;
5026 }
5027 else if (STRNCMP(p, "dict", 4) == 0)
5028 {
5029 flags |= FC_DICT;
5030 p += 4;
5031 }
5032 else if (STRNCMP(p, "abort", 5) == 0)
5033 {
5034 flags |= FC_ABORT;
5035 p += 5;
5036 }
5037 else if (STRNCMP(p, "closure", 7) == 0)
5038 {
5039 flags |= FC_CLOSURE;
5040 p += 7;
5041 if (current_funccal == NULL)
5042 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005043 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005044 name == NULL ? (char_u *)"" : name);
5045 goto erret;
5046 }
5047 }
5048 else
5049 break;
5050 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005051
Bram Moolenaare38eab22019-12-05 21:50:01 +01005052 // When there is a line break use what follows for the function body.
5053 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005054 if (*p == '\n')
5055 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005056 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02005057 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
5058 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01005059 && !(VIM_ISWHITE(*whitep) && *p == '#'
5060 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02005061 && !eap->skip
5062 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00005063 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005064
5065 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
5067 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005068 */
5069 if (KeyTyped)
5070 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005071 // Check if the function already exists, don't let the user type the
5072 // whole function before telling him it doesn't work! For a script we
5073 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005074 if (!eap->skip && !eap->forceit)
5075 {
5076 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005077 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005078 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00005079 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005080 }
5081
5082 if (!eap->skip && did_emsg)
5083 goto erret;
5084
Bram Moolenaare38eab22019-12-05 21:50:01 +01005085 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005086 cmdline_row = msg_row;
5087 }
5088
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005089 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005090 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005091
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005092 // Do not define the function when getting the body fails and when
5093 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005094 if (((class_flags & CF_INTERFACE) == 0
Yegappan Lakshmanan7bcd25c2023-09-08 19:27:51 +02005095 && (class_flags & CF_ABSTRACT_METHOD) == 0
Bram Moolenaar554d0312023-01-05 19:59:18 +00005096 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5097 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005098 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005099 goto erret;
5100
5101 /*
5102 * If there are no errors, add the function
5103 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005104 if (fudi.fd_dict != NULL)
5105 {
5106 char numbuf[20];
5107
5108 fp = NULL;
5109 if (fudi.fd_newkey == NULL && !eap->forceit)
5110 {
5111 emsg(_(e_dictionary_entry_already_exists));
5112 goto erret;
5113 }
5114 if (fudi.fd_di == NULL)
5115 {
5116 // Can't add a function to a locked dictionary
5117 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5118 goto erret;
5119 }
5120 // Can't change an existing function if it is locked
5121 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5122 goto erret;
5123
5124 // Give the function a sequential number. Can only be used with a
5125 // Funcref!
5126 vim_free(name);
5127 sprintf(numbuf, "%d", ++func_nr);
5128 name = vim_strsave((char_u *)numbuf);
5129 if (name == NULL)
5130 goto erret;
5131 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005132 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005133 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005134 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005135 char_u *find_name = name;
5136 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005137 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005138
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005139 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005140 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005141 var_conflict = TRUE;
5142
5143 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5144 {
5145 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5146
5147 if (si->sn_autoload_prefix != NULL)
5148 {
5149 if (is_export)
5150 {
5151 find_name = name + STRLEN(si->sn_autoload_prefix);
5152 v = find_var(find_name, &ht, TRUE);
5153 if (v != NULL)
5154 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005155 // Only check if the function already exists in the script,
5156 // global functions can be shadowed.
5157 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005158 }
5159 else
5160 {
5161 char_u *prefixed = may_prefix_autoload(name);
5162
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005163 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005164 {
5165 v = find_var(prefixed, &ht, TRUE);
5166 if (v != NULL)
5167 var_conflict = TRUE;
5168 vim_free(prefixed);
5169 }
5170 }
5171 }
5172 }
5173 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005174 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005175 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005176 goto erret;
5177 }
5178
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005179 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005180 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005181 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005182 char_u *uname = untrans_function_name(name);
5183
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005184 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005185 }
5186
5187 if (fp != NULL || import != NULL)
5188 {
5189 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005190
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005191 // Function can be replaced with "function!" and when sourcing the
5192 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005193 // A name that is used by an import can not be overruled.
5194 if (import != NULL
5195 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005196 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005197 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005198 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005199 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005200 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005201 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005202 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005203 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005204 goto erret;
5205 }
5206 if (fp->uf_calls > 0)
5207 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005208 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005209 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005210 goto erret;
5211 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005212 if (fp->uf_refcount > 1)
5213 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005214 // This function is referenced somewhere, don't redefine it but
5215 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005216 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005217 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005218 fp = NULL;
5219 overwrite = TRUE;
5220 }
5221 else
5222 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005223 char_u *exp_name = fp->uf_name_exp;
5224
5225 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005226 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005227 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005228 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005229 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005230 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005231#ifdef FEAT_PROFILE
5232 fp->uf_profiling = FALSE;
5233 fp->uf_prof_initialized = FALSE;
5234#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005235 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005236 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005237 }
5238 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005239
5240 if (fp == NULL)
5241 {
5242 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5243 {
5244 int slen, plen;
5245 char_u *scriptname;
5246
Bram Moolenaare38eab22019-12-05 21:50:01 +01005247 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005248 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005249 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005250 {
5251 scriptname = autoload_name(name);
5252 if (scriptname != NULL)
5253 {
5254 p = vim_strchr(scriptname, '/');
5255 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005256 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005257 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005258 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005259 j = OK;
5260 vim_free(scriptname);
5261 }
5262 }
5263 if (j == FAIL)
5264 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005265 linenr_T save_lnum = SOURCING_LNUM;
5266
5267 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005268 semsg(_(e_function_name_does_not_match_script_file_name_str),
5269 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005270 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005271 goto erret;
5272 }
5273 }
5274
Bram Moolenaare01e5212023-01-08 20:31:18 +00005275 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005276 if (fp == NULL)
5277 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005278 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005279
5280 if (fudi.fd_dict != NULL)
5281 {
5282 if (fudi.fd_di == NULL)
5283 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005284 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005285 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5286 if (fudi.fd_di == NULL)
5287 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005288 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005289 goto erret;
5290 }
5291 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5292 {
5293 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005294 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005295 goto erret;
5296 }
5297 }
5298 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005299 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005300 clear_tv(&fudi.fd_di->di_tv);
5301 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005302 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005303
Bram Moolenaare38eab22019-12-05 21:50:01 +01005304 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005305 flags |= FC_DICT;
5306 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005307 }
5308 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005309 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005310 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005311 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005312
5313 if (eap->cmdidx == CMD_def)
5314 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005315 int lnum_save = SOURCING_LNUM;
5316 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005317
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005318 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005319
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005320 // error messages are for the first function line
5321 SOURCING_LNUM = sourcing_lnum_top;
5322
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005323 // The function may use script variables from the context.
5324 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005325
h-eastb895b0f2023-09-24 15:46:31 +02005326 if (parse_argument_types(fp, &argtypes, varargs, &arg_objm,
5327 obj_members, obj_member_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005328 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005329 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005330 free_fp = fp_allocated;
5331 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005332 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005333 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005334
5335 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005336 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005337 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005338 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005339 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005340 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005341 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005342 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005343 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005344 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005345 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005346
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005347 if (fp_allocated)
5348 {
5349 // insert the new function in the function list
5350 set_ufunc_name(fp, name);
5351 if (overwrite)
5352 {
5353 hi = hash_find(&func_hashtab, name);
5354 hi->hi_key = UF2HIKEY(fp);
5355 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005356 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005357 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005358 {
5359 free_fp = TRUE;
5360 goto erret;
5361 }
5362 fp->uf_refcount = 1;
5363 }
5364
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005365 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005366 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005367 if ((flags & FC_CLOSURE) != 0)
5368 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005369 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005370 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005371 }
5372 else
5373 fp->uf_scoped = NULL;
5374
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005375#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005376 if (prof_def_func())
5377 func_do_profile(fp);
5378#endif
5379 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005380 if (sandbox)
5381 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005382 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005383 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005384 fp->uf_flags = flags;
5385 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005386 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005387 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005388 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005389 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005390 if (is_export)
5391 {
5392 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005393 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005394 is_export = FALSE;
5395 }
5396
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005397 if (eap->cmdidx == CMD_def)
5398 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005399 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5400 // :func does not use Vim9 script syntax, even in a Vim9 script file
5401 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005402
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005403 goto ret_free;
5404
5405erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005406 if (fp != NULL)
5407 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005408 // these were set to "newargs" and "default_args", which are cleared
5409 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005410 ga_init(&fp->uf_args);
5411 ga_init(&fp->uf_def_args);
5412 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005413errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005414 ga_clear_strings(&newargs);
5415 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005416 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005417 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005418 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005419 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005420 VIM_CLEAR(fp->uf_va_name);
5421 clear_type_list(&fp->uf_type_list);
5422 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005423 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005424 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005425ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005426 ga_clear_strings(&argtypes);
h-eastb895b0f2023-09-24 15:46:31 +02005427 ga_clear(&arg_objm);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005428 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005429 if (name != name_arg)
5430 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005431 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005432 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005433
5434 return fp;
5435}
5436
5437/*
5438 * ":function"
5439 */
5440 void
5441ex_function(exarg_T *eap)
5442{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005443 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005444
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005445 ga_init2(&lines_to_free, sizeof(char_u *), 50);
h-eastb895b0f2023-09-24 15:46:31 +02005446 (void)define_function(eap, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005447 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005448}
5449
5450/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005451 * Find a function by name, including "<lambda>123".
5452 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005453 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005454 * Return NULL if not found.
5455 */
5456 ufunc_T *
5457find_func_by_name(char_u *name, compiletype_T *compile_type)
5458{
5459 char_u *arg = name;
5460 char_u *fname;
5461 ufunc_T *ufunc;
5462 int is_global = FALSE;
5463
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005464 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5465 {
5466 *compile_type = CT_PROFILE;
5467 arg = skipwhite(arg + 7);
5468 }
5469 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5470 {
5471 *compile_type = CT_DEBUG;
5472 arg = skipwhite(arg + 5);
5473 }
5474
5475 if (STRNCMP(arg, "<lambda>", 8) == 0)
5476 {
5477 arg += 8;
5478 (void)getdigits(&arg);
5479 fname = vim_strnsave(name, arg - name);
5480 }
5481 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005482 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005483 // First try finding a method in a class, trans_function_name() will
5484 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005485 ufunc = find_class_func(&arg);
5486 if (ufunc != NULL)
5487 return ufunc;
5488
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005489 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005490 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005491 NULL, NULL, NULL, &ufunc);
5492 if (ufunc != NULL)
5493 {
5494 vim_free(fname);
5495 return ufunc;
5496 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005497 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005498 if (fname == NULL)
5499 {
5500 semsg(_(e_invalid_argument_str), name);
5501 return NULL;
5502 }
5503 if (!ends_excmd2(name, arg))
5504 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005505 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005506 emsg(ex_errmsg(e_trailing_characters_str, arg));
5507 return NULL;
5508 }
5509
5510 ufunc = find_func(fname, is_global);
5511 if (ufunc == NULL)
5512 {
5513 char_u *p = untrans_function_name(fname);
5514
5515 if (p != NULL)
5516 // Try again without making it script-local.
5517 ufunc = find_func(p, FALSE);
5518 }
5519 vim_free(fname);
5520 if (ufunc == NULL)
5521 semsg(_(e_cannot_find_function_str), name);
5522 return ufunc;
5523}
5524
5525/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005526 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005527 * be compiled or the one specified by the argument.
5528 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005529 */
5530 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005531ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005532{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005533 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005534 {
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005535 compiletype_T compile_type = CT_NONE;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005536 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005537 if (ufunc != NULL)
5538 {
5539 if (func_needs_compiling(ufunc, compile_type))
5540 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5541 else
5542 smsg(_("Function %s does not need compiling"), eap->arg);
5543 }
5544 }
5545 else
5546 {
5547 long todo = (long)func_hashtab.ht_used;
5548 int changed = func_hashtab.ht_changed;
5549 hashitem_T *hi;
5550
5551 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5552 {
5553 if (!HASHITEM_EMPTY(hi))
5554 {
5555 --todo;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005556 ufunc_T *ufunc = HI2UF(hi);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005557 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5558 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5559 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005560 {
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005561 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5562
5563 if (func_hashtab.ht_changed != changed)
5564 {
5565 // a function has been added or removed, need to start
5566 // over
5567 todo = (long)func_hashtab.ht_used;
5568 changed = func_hashtab.ht_changed;
5569 hi = func_hashtab.ht_array;
5570 --hi;
5571 }
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005572 }
5573 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005574 }
5575 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005576}
5577
5578/*
5579 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5580 * Return 2 if "p" starts with "s:".
5581 * Return 0 otherwise.
5582 */
5583 int
5584eval_fname_script(char_u *p)
5585{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005586 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5587 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005588 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5589 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5590 return 5;
5591 if (p[0] == 's' && p[1] == ':')
5592 return 2;
5593 return 0;
5594}
5595
5596 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005597translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005598{
5599 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005600 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005601 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005602}
5603
5604/*
5605 * Return TRUE when "ufunc" has old-style "..." varargs
5606 * or named varargs "...name: type".
5607 */
5608 int
5609has_varargs(ufunc_T *ufunc)
5610{
5611 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005612}
5613
5614/*
5615 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005616 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005617 */
5618 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005619function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005620{
5621 char_u *nm = name;
5622 char_u *p;
5623 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005624 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005625 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005626
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005627 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5628 if (no_deref)
5629 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005630 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005631 nm = skipwhite(nm);
5632
Bram Moolenaare38eab22019-12-05 21:50:01 +01005633 // Only accept "funcname", "funcname ", "funcname (..." and
5634 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005635 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005636 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005637 vim_free(p);
5638 return n;
5639}
5640
Bram Moolenaar113e1072019-01-20 15:30:40 +01005641#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005642 char_u *
5643get_expanded_name(char_u *name, int check)
5644{
5645 char_u *nm = name;
5646 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005647 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005648
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005649 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005650
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005651 if (p != NULL && *nm == NUL
5652 && (!check || translated_function_exists(p, is_global)))
5653 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005654
5655 vim_free(p);
5656 return NULL;
5657}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005658#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005659
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005660/*
5661 * Function given to ExpandGeneric() to obtain the list of user defined
5662 * function names.
5663 */
5664 char_u *
5665get_user_func_name(expand_T *xp, int idx)
5666{
5667 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005668 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005669 static hashitem_T *hi;
5670 ufunc_T *fp;
5671
5672 if (idx == 0)
5673 {
5674 done = 0;
5675 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005676 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005677 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005678 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005679 {
5680 if (done++ > 0)
5681 ++hi;
5682 while (HASHITEM_EMPTY(hi))
5683 ++hi;
5684 fp = HI2UF(hi);
5685
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005686 // don't show dead, dict and lambda functions
5687 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005688 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005689 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005690
5691 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005692 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005693
5694 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005695 if (xp->xp_context != EXPAND_USER_FUNC
5696 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005697 {
5698 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005699 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005700 STRCAT(IObuff, ")");
5701 }
5702 return IObuff;
5703 }
5704 return NULL;
5705}
5706
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005707/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005708 * Make a copy of a function.
5709 * Intended to be used for a function defined on a base class that has a copy
5710 * on the child class.
5711 * The copy has uf_refcount set to one.
5712 * Returns NULL when out of memory.
5713 */
5714 ufunc_T *
5715copy_function(ufunc_T *fp)
5716{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005717 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005718 if (ufunc == NULL)
5719 return NULL;
5720
5721 // Most things can just be copied.
5722 *ufunc = *fp;
5723
5724 ufunc->uf_def_status = UF_TO_BE_COMPILED;
5725 ufunc->uf_dfunc_idx = 0;
5726 ufunc->uf_class = NULL;
5727
5728 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
5729 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
5730
5731 if (ufunc->uf_arg_types != NULL)
5732 {
5733 // "uf_arg_types" is an allocated array, make a copy.
5734 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
5735 if (at != NULL)
5736 {
5737 mch_memmove(at, ufunc->uf_arg_types,
5738 sizeof(type_T *) * ufunc->uf_args.ga_len);
5739 ufunc->uf_arg_types = at;
5740 }
5741 }
5742
5743 // TODO: how about the types themselves? they can be freed when the
5744 // original function is freed:
5745 // type_T **uf_arg_types;
5746 // type_T *uf_ret_type;
5747
Ernie Rael114ec812023-06-04 18:11:35 +01005748 // make uf_type_list empty
5749 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00005750
5751 // TODO: partial_T *uf_partial;
5752
5753 if (ufunc->uf_va_name != NULL)
5754 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
5755
5756 // TODO:
5757 // type_T *uf_va_type;
5758 // type_T *uf_func_type;
5759
5760 ufunc->uf_block_depth = 0;
5761 ufunc->uf_block_ids = NULL;
5762
5763 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
5764
5765 ufunc->uf_refcount = 1;
5766 ufunc->uf_name_exp = NULL;
5767 STRCPY(ufunc->uf_name, fp->uf_name);
5768
5769 return ufunc;
5770}
5771
5772/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005773 * ":delfunction {name}"
5774 */
5775 void
5776ex_delfunction(exarg_T *eap)
5777{
5778 ufunc_T *fp = NULL;
5779 char_u *p;
5780 char_u *name;
5781 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005782 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005783
5784 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005785 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
5786 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005787 vim_free(fudi.fd_newkey);
5788 if (name == NULL)
5789 {
5790 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005791 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005792 return;
5793 }
5794 if (!ends_excmd(*skipwhite(p)))
5795 {
5796 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005797 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005798 return;
5799 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005800 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005801 if (eap->nextcmd != NULL)
5802 *p = NUL;
5803
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005804 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005805 {
5806 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005807 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005808 vim_free(name);
5809 return;
5810 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005811 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005812 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005813 vim_free(name);
5814
5815 if (!eap->skip)
5816 {
5817 if (fp == NULL)
5818 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005819 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005820 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005821 return;
5822 }
5823 if (fp->uf_calls > 0)
5824 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005825 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005826 return;
5827 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005828 if (fp->uf_flags & FC_VIM9)
5829 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005830 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005831 return;
5832 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005833
5834 if (fudi.fd_dict != NULL)
5835 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005836 // Delete the dict item that refers to the function, it will
5837 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00005838 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005839 }
5840 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005841 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005842 // A normal function (not a numbered function or lambda) has a
5843 // refcount of 1 for the entry in the hashtable. When deleting
5844 // it and the refcount is more than one, it should be kept.
5845 // A numbered function and lambda should be kept if the refcount is
5846 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005847 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005848 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005849 // Function is still referenced somewhere. Don't free it but
5850 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005851 if (func_remove(fp))
5852 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005853 }
5854 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005855 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005856 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005857 }
5858}
5859
5860/*
5861 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005862 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005863 */
5864 void
5865func_unref(char_u *name)
5866{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005867 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005868
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005869 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005870 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005871 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005872 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005873 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005874#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005875 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005876#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005877 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005878 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005879 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005880}
5881
5882/*
5883 * Unreference a Function: decrement the reference count and free it when it
5884 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005885 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005886 */
5887 void
5888func_ptr_unref(ufunc_T *fp)
5889{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005890 if (fp != NULL && (--fp->uf_refcount <= 0
5891 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5892 && fp->uf_partial->pt_refcount <= 1
5893 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005894 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005895 // Only delete it when it's not being used. Otherwise it's done
5896 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005897 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005898 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005899 }
5900}
5901
5902/*
5903 * Count a reference to a Function.
5904 */
5905 void
5906func_ref(char_u *name)
5907{
5908 ufunc_T *fp;
5909
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005910 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005911 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005912 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005913 if (fp != NULL)
5914 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005915 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005916 // Only give an error for a numbered function.
5917 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005918 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005919}
5920
5921/*
5922 * Count a reference to a Function.
5923 */
5924 void
5925func_ptr_ref(ufunc_T *fp)
5926{
5927 if (fp != NULL)
5928 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005929}
5930
5931/*
5932 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5933 * referenced from anywhere that is in use.
5934 */
5935 static int
5936can_free_funccal(funccall_T *fc, int copyID)
5937{
Bram Moolenaarca16c602022-09-06 18:57:08 +01005938 return (fc->fc_l_varlist.lv_copyID != copyID
5939 && fc->fc_l_vars.dv_copyID != copyID
5940 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005941 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005942}
5943
5944/*
5945 * ":return [expr]"
5946 */
5947 void
5948ex_return(exarg_T *eap)
5949{
5950 char_u *arg = eap->arg;
5951 typval_T rettv;
5952 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005953 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005954
5955 if (current_funccal == NULL)
5956 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005957 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005958 return;
5959 }
5960
Bram Moolenaar844fb642021-10-23 13:32:30 +01005961 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005962 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5963
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005964 if (eap->skip)
5965 ++emsg_skip;
5966
5967 eap->nextcmd = NULL;
5968 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005969 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005970 {
5971 if (!eap->skip)
5972 returning = do_return(eap, FALSE, TRUE, &rettv);
5973 else
5974 clear_tv(&rettv);
5975 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005976 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005977 else if (!eap->skip)
5978 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005979 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005980 update_force_abort();
5981
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005982 /*
5983 * Return unless the expression evaluation has been cancelled due to an
5984 * aborting error, an interrupt, or an exception.
5985 */
5986 if (!aborting())
5987 returning = do_return(eap, FALSE, TRUE, NULL);
5988 }
5989
Bram Moolenaare38eab22019-12-05 21:50:01 +01005990 // When skipping or the return gets pending, advance to the next command
5991 // in this line (!returning). Otherwise, ignore the rest of the line.
5992 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005993 if (returning)
5994 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005995 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02005996 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005997
5998 if (eap->skip)
5999 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02006000 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006001}
6002
zeertzjq5299c092023-04-12 20:48:16 +01006003/*
6004 * Lower level implementation of "call". Only called when not skipping.
6005 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006006 static int
6007ex_call_inner(
6008 exarg_T *eap,
6009 char_u *name,
6010 char_u **arg,
6011 char_u *startarg,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006012 funcexe_T *funcexe_init,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006013 evalarg_T *evalarg)
6014{
6015 linenr_T lnum;
6016 int doesrange;
6017 typval_T rettv;
6018 int failed = FALSE;
6019
zeertzjq5299c092023-04-12 20:48:16 +01006020 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006021 for ( ; lnum <= eap->line2; ++lnum)
6022 {
6023 funcexe_T funcexe;
6024
zeertzjq5299c092023-04-12 20:48:16 +01006025 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006026 {
6027 if (lnum > curbuf->b_ml.ml_line_count)
6028 {
6029 // If the function deleted lines or switched to another buffer
6030 // the line number may become invalid.
6031 emsg(_(e_invalid_range));
6032 break;
6033 }
6034 curwin->w_cursor.lnum = lnum;
6035 curwin->w_cursor.col = 0;
6036 curwin->w_cursor.coladd = 0;
6037 }
6038 *arg = startarg;
6039
6040 funcexe = *funcexe_init;
6041 funcexe.fe_doesrange = &doesrange;
6042 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6043 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
6044 {
6045 failed = TRUE;
6046 break;
6047 }
6048 if (has_watchexpr())
6049 dbg_check_breakpoint(eap);
6050
6051 // Handle a function returning a Funcref, Dictionary or List.
6052 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01006053 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006054 {
6055 failed = TRUE;
6056 break;
6057 }
6058
6059 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01006060 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006061 break;
6062
6063 // Stop when immediately aborting on error, or when an interrupt
6064 // occurred or an exception was thrown but not caught.
6065 // get_func_tv() returned OK, so that the check for trailing
6066 // characters below is executed.
6067 if (aborting())
6068 break;
6069 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006070 return failed;
6071}
6072
6073/*
6074 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
6075 * Returns FAIL or OK.
6076 */
6077 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01006078ex_defer_inner(
6079 char_u *name,
6080 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01006081 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01006082 partial_T *partial,
6083 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006084{
6085 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01006086 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006087 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01006088 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006089
6090 if (current_funccal == NULL)
6091 {
6092 semsg(_(e_str_not_inside_function), "defer");
6093 return FAIL;
6094 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006095 if (partial != NULL)
6096 {
6097 if (partial->pt_dict != NULL)
6098 {
6099 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6100 return FAIL;
6101 }
6102 if (partial->pt_argc > 0)
6103 {
6104 int i;
6105
6106 partial_argc = partial->pt_argc;
6107 for (i = 0; i < partial_argc; ++i)
6108 copy_tv(&partial->pt_argv[i], &argvars[i]);
6109 }
6110 }
6111 r = get_func_arguments(arg, evalarg, FALSE,
6112 argvars + partial_argc, &argcount);
6113 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006114
6115 if (r == OK)
6116 {
6117 if (type != NULL)
6118 {
6119 // Check that the arguments are OK for the types of the funcref.
6120 r = check_argument_types(type, argvars, argcount, NULL, name);
6121 }
6122 else if (builtin_function(name, -1))
6123 {
6124 int idx = find_internal_func(name);
6125
6126 if (idx < 0)
6127 {
6128 emsg_funcname(e_unknown_function_str, name);
6129 r = FAIL;
6130 }
6131 else if (check_internal_func(idx, argcount) == -1)
6132 r = FAIL;
6133 }
6134 else
6135 {
6136 ufunc_T *ufunc = find_func(name, FALSE);
6137
6138 // we tolerate an unknown function here, it might be defined later
6139 if (ufunc != NULL)
6140 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006141 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006142 if (error != FCERR_UNKNOWN)
6143 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006144 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006145 r = FAIL;
6146 }
6147 }
6148 }
6149 }
6150
Bram Moolenaar86d87252022-09-05 21:21:25 +01006151 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006152 {
6153 while (--argcount >= 0)
6154 clear_tv(&argvars[argcount]);
6155 return FAIL;
6156 }
6157 return add_defer(name, argcount, argvars);
6158}
6159
6160/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006161 * Return TRUE if currently inside a function call.
6162 * Give an error message and return FALSE when not.
6163 */
6164 int
6165can_add_defer(void)
6166{
6167 if (!in_def_function() && get_current_funccal() == NULL)
6168 {
6169 semsg(_(e_str_not_inside_function), "defer");
6170 return FALSE;
6171 }
6172 return TRUE;
6173}
6174
6175/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006176 * Add a deferred call for "name" with arguments "argvars[argcount]".
6177 * Consumes "argvars[]".
6178 * Caller must check that in_def_function() returns TRUE or current_funccal is
6179 * not NULL.
6180 * Returns OK or FAIL.
6181 */
6182 int
6183add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6184{
6185 char_u *saved_name = vim_strsave(name);
6186 int argcount = argcount_arg;
6187 defer_T *dr;
6188 int ret = FAIL;
6189
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006190 if (saved_name == NULL)
6191 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006192 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006193 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006194 if (add_defer_function(saved_name, argcount, argvars) == OK)
6195 argcount = 0;
6196 }
6197 else
6198 {
6199 if (current_funccal->fc_defer.ga_itemsize == 0)
6200 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6201 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6202 goto theend;
6203 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6204 + current_funccal->fc_defer.ga_len++;
6205 dr->dr_name = saved_name;
6206 dr->dr_argcount = argcount;
6207 while (argcount > 0)
6208 {
6209 --argcount;
6210 dr->dr_argvars[argcount] = argvars[argcount];
6211 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006212 }
6213 ret = OK;
6214
6215theend:
6216 while (--argcount >= 0)
6217 clear_tv(&argvars[argcount]);
6218 return ret;
6219}
6220
6221/*
6222 * Invoked after a functions has finished: invoke ":defer" functions.
6223 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006224 static void
6225handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006226{
6227 int idx;
6228
Bram Moolenaar58779852022-09-06 18:31:14 +01006229 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006230 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006231 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006232
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006233 if (dr->dr_name == NULL)
6234 // already being called, can happen if function does ":qa"
6235 continue;
6236
6237 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006238 CLEAR_FIELD(funcexe);
6239 funcexe.fe_evaluate = TRUE;
6240
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006241 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006242 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006243
6244 char_u *name = dr->dr_name;
6245 dr->dr_name = NULL;
6246
6247 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6248
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006249 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006250 vim_free(name);
6251 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006252 clear_tv(&dr->dr_argvars[i]);
6253 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006254 ga_clear(&funccal->fc_defer);
6255}
6256
zeertzjq960cf912023-04-18 21:52:54 +01006257 static void
6258invoke_funccall_defer(funccall_T *fc)
6259{
6260 if (fc->fc_ectx != NULL)
6261 {
6262 // :def function
6263 unwind_def_callstack(fc->fc_ectx);
6264 may_invoke_defer_funcs(fc->fc_ectx);
6265 }
6266 else
6267 {
6268 // legacy function
6269 handle_defer_one(fc);
6270 }
6271}
6272
Bram Moolenaar58779852022-09-06 18:31:14 +01006273/*
6274 * Called when exiting: call all defer functions.
6275 */
6276 void
6277invoke_all_defer(void)
6278{
zeertzjq1be4b812023-04-19 14:21:24 +01006279 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6280 invoke_funccall_defer(fc);
6281
zeertzjq960cf912023-04-18 21:52:54 +01006282 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6283 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6284 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006285}
6286
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006287/*
6288 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006289 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006290 */
6291 void
6292ex_call(exarg_T *eap)
6293{
6294 char_u *arg = eap->arg;
6295 char_u *startarg;
6296 char_u *name;
6297 char_u *tofree;
6298 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006299 int failed = FALSE;
6300 funcdict_T fudi;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006301 ufunc_T *ufunc = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006302 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006303 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006304 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006305 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006306 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006307
Bram Moolenaare6b53242020-07-01 17:28:33 +02006308 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006309 if (eap->skip)
6310 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006311 typval_T rettv;
6312
Bram Moolenaare38eab22019-12-05 21:50:01 +01006313 // trans_function_name() doesn't work well when skipping, use eval0()
6314 // instead to skip to any following command, e.g. for:
6315 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006316 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006317 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006318 clear_tv(&rettv);
6319 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006320 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006321 return;
6322 }
6323
zeertzjq5299c092023-04-12 20:48:16 +01006324 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006325 &fudi, &partial, vim9script ? &type : NULL, &ufunc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006326 if (fudi.fd_newkey != NULL)
6327 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006328 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006329 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006330 vim_free(fudi.fd_newkey);
6331 }
6332 if (tofree == NULL)
6333 return;
6334
Bram Moolenaare38eab22019-12-05 21:50:01 +01006335 // Increase refcount on dictionary, it could get deleted when evaluating
6336 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006337 if (fudi.fd_dict != NULL)
6338 ++fudi.fd_dict->dv_refcount;
6339
Bram Moolenaare38eab22019-12-05 21:50:01 +01006340 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6341 // contents. For VAR_PARTIAL get its partial, unless we already have one
6342 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006343 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006344 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006345 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006346 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006347
Bram Moolenaare38eab22019-12-05 21:50:01 +01006348 // Skip white space to allow ":call func ()". Not good, but required for
6349 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006350 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006351 if (*startarg != '(')
6352 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006353 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006354 goto end;
6355 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006356 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006357 {
6358 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6359 goto end;
6360 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006361
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006362 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006363 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006364 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006365 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006366 }
6367 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006368 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006369 funcexe_T funcexe;
6370
Bram Moolenaara80faa82020-04-12 19:37:17 +02006371 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006372 funcexe.fe_check_type = type;
Bram Moolenaar5ca05fa2023-06-10 16:45:13 +01006373 funcexe.fe_ufunc = ufunc;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006374 funcexe.fe_partial = partial;
6375 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006376 funcexe.fe_firstline = eap->line1;
6377 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006378 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006379 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006380 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006381 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006382
Bram Moolenaar1d341892021-09-18 15:25:52 +02006383 // When inside :try we need to check for following "| catch" or "| endtry".
6384 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006385 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006386 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006387 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006388 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006389 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006390 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006391 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006392 {
6393 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006394 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006395 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006396 }
6397 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006398 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006399 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006400 // Must be after using "arg", it may point into memory cleared here.
6401 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006402
6403end:
6404 dict_unref(fudi.fd_dict);
6405 vim_free(tofree);
6406}
6407
6408/*
6409 * Return from a function. Possibly makes the return pending. Also called
6410 * for a pending return at the ":endtry" or after returning from an extra
6411 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6412 * when called due to a ":return" command. "rettv" may point to a typval_T
6413 * with the return rettv. Returns TRUE when the return can be carried out,
6414 * FALSE when the return gets pending.
6415 */
6416 int
6417do_return(
6418 exarg_T *eap,
6419 int reanimate,
6420 int is_cmd,
6421 void *rettv)
6422{
6423 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006424 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006425
6426 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006427 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006428 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006429
6430 /*
6431 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6432 * not in its finally clause (which then is to be executed next) is found.
6433 * In this case, make the ":return" pending for execution at the ":endtry".
6434 * Otherwise, return normally.
6435 */
6436 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6437 if (idx >= 0)
6438 {
6439 cstack->cs_pending[idx] = CSTP_RETURN;
6440
6441 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006442 // A pending return again gets pending. "rettv" points to an
6443 // allocated variable with the rettv of the original ":return"'s
6444 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006445 cstack->cs_rettv[idx] = rettv;
6446 else
6447 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006448 // When undoing a return in order to make it pending, get the stored
6449 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006450 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006451 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006452
6453 if (rettv != NULL)
6454 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006455 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006456 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6457 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6458 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006459 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006460 }
6461 else
6462 cstack->cs_rettv[idx] = NULL;
6463
6464 if (reanimate)
6465 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006466 // The pending return value could be overwritten by a ":return"
6467 // without argument in a finally clause; reset the default
6468 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006469 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6470 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006471 }
6472 }
6473 report_make_pending(CSTP_RETURN, rettv);
6474 }
6475 else
6476 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006477 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006478
Bram Moolenaare38eab22019-12-05 21:50:01 +01006479 // If the return is carried out now, store the return value. For
6480 // a return immediately after reanimation, the value is already
6481 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006482 if (!reanimate && rettv != NULL)
6483 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006484 clear_tv(current_funccal->fc_rettv);
6485 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006486 if (!is_cmd)
6487 vim_free(rettv);
6488 }
6489 }
6490
6491 return idx < 0;
6492}
6493
6494/*
6495 * Free the variable with a pending return value.
6496 */
6497 void
6498discard_pending_return(void *rettv)
6499{
6500 free_tv((typval_T *)rettv);
6501}
6502
6503/*
6504 * Generate a return command for producing the value of "rettv". The result
6505 * is an allocated string. Used by report_pending() for verbose messages.
6506 */
6507 char_u *
6508get_return_cmd(void *rettv)
6509{
6510 char_u *s = NULL;
6511 char_u *tofree = NULL;
6512 char_u numbuf[NUMBUFLEN];
6513
6514 if (rettv != NULL)
6515 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6516 if (s == NULL)
6517 s = (char_u *)"";
6518
6519 STRCPY(IObuff, ":return ");
6520 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6521 if (STRLEN(s) + 8 >= IOSIZE)
6522 STRCPY(IObuff + IOSIZE - 4, "...");
6523 vim_free(tofree);
6524 return vim_strsave(IObuff);
6525}
6526
6527/*
6528 * Get next function line.
6529 * Called by do_cmdline() to get the next line.
6530 * Returns allocated string, or NULL for end of function.
6531 */
6532 char_u *
6533get_func_line(
6534 int c UNUSED,
6535 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006536 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006537 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006538{
6539 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006540 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006541 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006542 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006543
Bram Moolenaare38eab22019-12-05 21:50:01 +01006544 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006545 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006546 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006547 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006548 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006549 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006550 }
6551#ifdef FEAT_PROFILE
6552 if (do_profiling == PROF_YES)
6553 func_line_end(cookie);
6554#endif
6555
6556 gap = &fp->uf_lines;
6557 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006558 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006559 retval = NULL;
6560 else
6561 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006562 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006563 while (fcp->fc_linenr < gap->ga_len
6564 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6565 ++fcp->fc_linenr;
6566 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006567 retval = NULL;
6568 else
6569 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006570 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6571 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006572#ifdef FEAT_PROFILE
6573 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006574 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006575#endif
6576 }
6577 }
6578
Bram Moolenaare38eab22019-12-05 21:50:01 +01006579 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006580 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006581 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006582 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006583 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006584 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006585 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006586 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006587 }
6588
6589 return retval;
6590}
6591
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006592/*
6593 * Return TRUE if the currently active function should be ended, because a
6594 * return was encountered or an error occurred. Used inside a ":while".
6595 */
6596 int
6597func_has_ended(void *cookie)
6598{
6599 funccall_T *fcp = (funccall_T *)cookie;
6600
Bram Moolenaare38eab22019-12-05 21:50:01 +01006601 // Ignore the "abort" flag if the abortion behavior has been changed due to
6602 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006603 return (((fcp->fc_func->uf_flags & FC_ABORT)
6604 && did_emsg && !aborted_in_try())
6605 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006606}
6607
6608/*
6609 * return TRUE if cookie indicates a function which "abort"s on errors.
6610 */
6611 int
6612func_has_abort(
6613 void *cookie)
6614{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006615 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006616}
6617
6618
6619/*
6620 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6621 * Don't do this when "Func" is already a partial that was bound
6622 * explicitly (pt_auto is FALSE).
6623 * Changes "rettv" in-place.
6624 * Returns the updated "selfdict_in".
6625 */
6626 dict_T *
6627make_partial(dict_T *selfdict_in, typval_T *rettv)
6628{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006629 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006630 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006631 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006632 dict_T *selfdict = selfdict_in;
6633
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006634 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6635 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006636 fp = rettv->vval.v_partial->pt_func;
6637 else
6638 {
6639 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006640 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006641 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006642 if (fname == NULL)
6643 {
6644 // There is no point binding a dict to a NULL function, just create
6645 // a function reference.
6646 rettv->v_type = VAR_FUNC;
6647 rettv->vval.v_string = NULL;
6648 }
6649 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006650 {
6651 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006652 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006653
6654 // Translate "s:func" to the stored function name.
6655 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6656 fp = find_func(fname, FALSE);
6657 vim_free(tofree);
6658 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006659 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006660
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006661 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006662 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006663 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006664
6665 if (pt != NULL)
6666 {
6667 pt->pt_refcount = 1;
6668 pt->pt_dict = selfdict;
6669 pt->pt_auto = TRUE;
6670 selfdict = NULL;
6671 if (rettv->v_type == VAR_FUNC)
6672 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006673 // Just a function: Take over the function name and use
6674 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006675 pt->pt_name = rettv->vval.v_string;
6676 }
6677 else
6678 {
6679 partial_T *ret_pt = rettv->vval.v_partial;
6680 int i;
6681
Bram Moolenaare38eab22019-12-05 21:50:01 +01006682 // Partial: copy the function name, use selfdict and copy
6683 // args. Can't take over name or args, the partial might
6684 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006685 if (ret_pt->pt_name != NULL)
6686 {
6687 pt->pt_name = vim_strsave(ret_pt->pt_name);
6688 func_ref(pt->pt_name);
6689 }
6690 else
6691 {
6692 pt->pt_func = ret_pt->pt_func;
6693 func_ptr_ref(pt->pt_func);
6694 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006695 if (ret_pt->pt_argc > 0)
6696 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006697 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006698 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006699 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006700 pt->pt_argc = 0;
6701 else
6702 {
6703 pt->pt_argc = ret_pt->pt_argc;
6704 for (i = 0; i < pt->pt_argc; i++)
6705 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6706 }
6707 }
6708 partial_unref(ret_pt);
6709 }
6710 rettv->v_type = VAR_PARTIAL;
6711 rettv->vval.v_partial = pt;
6712 }
6713 }
6714 return selfdict;
6715}
6716
6717/*
6718 * Return the name of the executed function.
6719 */
6720 char_u *
6721func_name(void *cookie)
6722{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006723 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006724}
6725
6726/*
6727 * Return the address holding the next breakpoint line for a funccall cookie.
6728 */
6729 linenr_T *
6730func_breakpoint(void *cookie)
6731{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006732 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006733}
6734
6735/*
6736 * Return the address holding the debug tick for a funccall cookie.
6737 */
6738 int *
6739func_dbg_tick(void *cookie)
6740{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006741 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006742}
6743
6744/*
6745 * Return the nesting level for a funccall cookie.
6746 */
6747 int
6748func_level(void *cookie)
6749{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006750 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006751}
6752
6753/*
6754 * Return TRUE when a function was ended by a ":return" command.
6755 */
6756 int
6757current_func_returned(void)
6758{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006759 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006760}
6761
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006762 int
6763free_unref_funccal(int copyID, int testing)
6764{
6765 int did_free = FALSE;
6766 int did_free_funccal = FALSE;
6767 funccall_T *fc, **pfc;
6768
6769 for (pfc = &previous_funccal; *pfc != NULL; )
6770 {
6771 if (can_free_funccal(*pfc, copyID))
6772 {
6773 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006774 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006775 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006776 did_free = TRUE;
6777 did_free_funccal = TRUE;
6778 }
6779 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006780 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006781 }
6782 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006783 // When a funccal was freed some more items might be garbage
6784 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006785 (void)garbage_collect(testing);
6786
6787 return did_free;
6788}
6789
6790/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006791 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006792 */
6793 static funccall_T *
6794get_funccal(void)
6795{
6796 int i;
6797 funccall_T *funccal;
6798 funccall_T *temp_funccal;
6799
6800 funccal = current_funccal;
6801 if (debug_backtrace_level > 0)
6802 {
6803 for (i = 0; i < debug_backtrace_level; i++)
6804 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006805 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006806 if (temp_funccal)
6807 funccal = temp_funccal;
6808 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006809 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006810 debug_backtrace_level = i;
6811 }
6812 }
6813 return funccal;
6814}
6815
6816/*
6817 * Return the hashtable used for local variables in the current funccal.
6818 * Return NULL if there is no current funccal.
6819 */
6820 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006821get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006822{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006823 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006824 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006825 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006826}
6827
6828/*
6829 * Return the l: scope variable.
6830 * Return NULL if there is no current funccal.
6831 */
6832 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006833get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006834{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006835 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006836 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006837 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006838}
6839
6840/*
6841 * Return the hashtable used for argument in the current funccal.
6842 * Return NULL if there is no current funccal.
6843 */
6844 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006845get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006846{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006847 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006848 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006849 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006850}
6851
6852/*
6853 * Return the a: scope variable.
6854 * Return NULL if there is no current funccal.
6855 */
6856 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006857get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006858{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006859 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006860 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006861 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006862}
6863
6864/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006865 * List function variables, if there is a function.
6866 */
6867 void
6868list_func_vars(int *first)
6869{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006870 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
6871 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006872 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006873}
6874
6875/*
6876 * If "ht" is the hashtable for local variables in the current funccal, return
6877 * the dict that contains it.
6878 * Otherwise return NULL.
6879 */
6880 dict_T *
6881get_current_funccal_dict(hashtab_T *ht)
6882{
6883 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01006884 && ht == &current_funccal->fc_l_vars.dv_hashtab)
6885 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006886 return NULL;
6887}
6888
6889/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006890 * Search hashitem in parent scope.
6891 */
6892 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006893find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006894{
6895 funccall_T *old_current_funccal = current_funccal;
6896 hashtab_T *ht;
6897 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006898 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006899
Bram Moolenaarca16c602022-09-06 18:57:08 +01006900 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006901 return NULL;
6902
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006903 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006904 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006905 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006906 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006907 ht = find_var_ht(name, &varname);
6908 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006909 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006910 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006911 if (!HASHITEM_EMPTY(hi))
6912 {
6913 *pht = ht;
6914 break;
6915 }
6916 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006917 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02006918 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006919 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006920 }
6921 current_funccal = old_current_funccal;
6922
6923 return hi;
6924}
6925
6926/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006927 * Search variable in parent scope.
6928 */
6929 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006930find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006931{
6932 dictitem_T *v = NULL;
6933 funccall_T *old_current_funccal = current_funccal;
6934 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006935 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006936
Bram Moolenaarca16c602022-09-06 18:57:08 +01006937 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006938 return NULL;
6939
Bram Moolenaare38eab22019-12-05 21:50:01 +01006940 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01006941 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006942 while (current_funccal)
6943 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006944 ht = find_var_ht(name, &varname);
6945 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006946 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006947 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006948 if (v != NULL)
6949 break;
6950 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006951 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006952 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006953 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006954 }
6955 current_funccal = old_current_funccal;
6956
6957 return v;
6958}
6959
6960/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006961 * Set "copyID + 1" in previous_funccal and callers.
6962 */
6963 int
6964set_ref_in_previous_funccal(int copyID)
6965{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006966 funccall_T *fc;
6967
Bram Moolenaarca16c602022-09-06 18:57:08 +01006968 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006969 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006970 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006971 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
6972 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
6973 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006974 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006975 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006976 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006977}
6978
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006979 static int
6980set_ref_in_funccal(funccall_T *fc, int copyID)
6981{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006982 if (fc->fc_copyID != copyID)
6983 {
6984 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006985 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
6986 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
6987 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
6988 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006989 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006990 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006991 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006992}
6993
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006994/*
6995 * Set "copyID" in all local vars and arguments in the call stack.
6996 */
6997 int
6998set_ref_in_call_stack(int copyID)
6999{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007000 funccall_T *fc;
7001 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007002
Bram Moolenaarca16c602022-09-06 18:57:08 +01007003 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007004 if (set_ref_in_funccal(fc, copyID))
7005 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02007006
7007 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007008 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01007009 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007010 if (set_ref_in_funccal(fc, copyID))
7011 return TRUE;
7012 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007013}
7014
7015/*
7016 * Set "copyID" in all functions available by name.
7017 */
7018 int
7019set_ref_in_functions(int copyID)
7020{
7021 int todo;
7022 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007023 ufunc_T *fp;
7024
7025 todo = (int)func_hashtab.ht_used;
7026 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007027 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007028 if (!HASHITEM_EMPTY(hi))
7029 {
7030 --todo;
7031 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007032 if (!func_name_refcount(fp->uf_name)
7033 && set_ref_in_func(NULL, fp, copyID))
7034 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007035 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007036 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007037 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007038}
7039
7040/*
7041 * Set "copyID" in all function arguments.
7042 */
7043 int
7044set_ref_in_func_args(int copyID)
7045{
7046 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007047
7048 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02007049 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7050 copyID, NULL, NULL))
7051 return TRUE;
7052 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007053}
7054
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007055/*
7056 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007057 * Returns TRUE if setting references failed somehow.
7058 */
7059 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007060set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007061{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007062 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007063 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00007064 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007065 char_u fname_buf[FLEN_FIXED + 1];
7066 char_u *tofree = NULL;
7067 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007068 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007069
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007070 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007071 return FALSE;
7072
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007073 if (fp_in == NULL)
7074 {
7075 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00007076 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007077 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007078 if (fp != NULL)
7079 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01007080 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007081 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007082 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02007083
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007084 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02007085 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007086}
7087
Bram Moolenaare38eab22019-12-05 21:50:01 +01007088#endif // FEAT_EVAL