blob: 8facd2fdfd238e943479726d378d95297cfa69d7 [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,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010068 evalarg_T *evalarg,
Bram Moolenaardce24412022-02-08 20:35:30 +000069 exarg_T *eap,
Bram Moolenaar2a389082021-04-09 20:24:31 +020070 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010071 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010072{
Bram Moolenaar6e949782020-04-13 17:21:00 +020073 char_u *p = arg;
74 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020075 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010076
77 while (ASCII_ISALNUM(*p) || *p == '_')
78 ++p;
79 if (arg == p || isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020080 || (argtypes == NULL
81 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
82 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083 {
84 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000085 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 return arg;
87 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010088
Bram Moolenaar057e84a2021-02-28 16:55:11 +010089 // Vim9 script: cannot use script var name for argument. In function: also
90 // check local vars and arguments.
91 if (!skip && argtypes != NULL && check_defined(arg, p - arg,
Bram Moolenaardce24412022-02-08 20:35:30 +000092 evalarg == NULL ? NULL : evalarg->eval_cctx,
93 eap == NULL ? NULL : eap->cstack, TRUE) == FAIL)
Bram Moolenaarb4893b82021-02-21 22:20:24 +010094 return arg;
Bram Moolenaarb4893b82021-02-21 22:20:24 +010095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
97 return arg;
98 if (newargs != NULL)
99 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100100 int c;
101 int i;
102
103 c = *p;
104 *p = NUL;
105 arg_copy = vim_strsave(arg);
106 if (arg_copy == NULL)
107 {
108 *p = c;
109 return arg;
110 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200111 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200112 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200113 // Check for duplicate argument name.
114 for (i = 0; i < newargs->ga_len; ++i)
115 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
116 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000117 semsg(_(e_duplicate_argument_name_str), arg_copy);
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200118 vim_free(arg_copy);
119 return arg;
120 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100121 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
122 newargs->ga_len++;
123
124 *p = c;
125 }
126
127 // get any type from "arg: type"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100128 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100129 {
130 char_u *type = NULL;
131
Bram Moolenaar6e949782020-04-13 17:21:00 +0200132 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
133 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200134 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200135 arg_copy == NULL ? arg : arg_copy);
136 p = skipwhite(p);
137 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138 if (*p == ':')
139 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200140 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100141 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200142 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100143 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200144 return arg;
145 }
146 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200147 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100148 if (!skip)
149 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100150 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200151 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200152 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200153 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200154 arg_copy == NULL ? arg : arg_copy);
155 return arg;
156 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100157 if (!skip)
158 {
159 if (type == NULL && types_optional)
160 // lambda arguments default to "any" type
Bram Moolenaar2a389082021-04-09 20:24:31 +0200161 type = vim_strsave((char_u *)
162 (is_vararg ? "list<any>" : "any"));
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100163 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
164 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100165 }
166
167 return p;
168}
169
170/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000171 * Handle line continuation in function arguments or body.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000172 * Get a next line, store it in "eap" if appropriate and put the line in
173 * "lines_to_free" to free the line later.
Bram Moolenaar7473a842021-12-28 17:55:26 +0000174 */
175 static char_u *
176get_function_line(
177 exarg_T *eap,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000178 garray_T *lines_to_free,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000179 int indent,
180 getline_opt_T getline_options)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000181{
182 char_u *theline;
183
184 if (eap->getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +0000185 theline = getcmdline(':', 0L, indent, 0);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000186 else
187 theline = eap->getline(':', eap->cookie, indent, getline_options);
188 if (theline != NULL)
189 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000190 if (lines_to_free->ga_len > 0
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000191 && eap->cmdlinep != NULL
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000192 && *eap->cmdlinep == ((char_u **)lines_to_free->ga_data)
193 [lines_to_free->ga_len - 1])
Bram Moolenaar7473a842021-12-28 17:55:26 +0000194 *eap->cmdlinep = theline;
Bram Moolenaarb5820102023-01-25 12:27:13 +0000195 (void)ga_add_string(lines_to_free, theline);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000196 }
197
198 return theline;
199}
200
201/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200202 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100203 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100204 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200205 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100206 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200207get_function_args(
208 char_u **argp,
209 char_u endchar,
210 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100212 int types_optional, // types optional if "argtypes" is not NULL
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100213 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200214 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200215 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200216 int skip,
Bram Moolenaardce24412022-02-08 20:35:30 +0000217 exarg_T *eap, // can be NULL
Bram Moolenaar554d0312023-01-05 19:59:18 +0000218 int in_class, // non-zero when inside a class or interface
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000219 garray_T *newlines, // function body lines
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000220 garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200221{
222 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100223 char_u *arg;
224 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200225 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200226 int any_default = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100227 char_u *whitep = *argp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200228
229 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000230 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100231 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000232 ga_init2(argtypes, sizeof(char_u *), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200233 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000234 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200235
236 if (varargs != NULL)
237 *varargs = FALSE;
238
239 /*
240 * Isolate the arguments: "arg1, arg2, ...)"
241 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100242 arg = skipwhite(*argp);
243 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200244 while (*p != endchar)
245 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200246 while (eap != NULL && eap->getline != NULL
247 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200248 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200249 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000250 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000251 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000252
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200253 if (theline == NULL)
254 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200255 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200256 p = skipwhite(theline);
257 }
258
259 if (mustend && *p != endchar)
260 {
261 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000262 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100263 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200264 }
265 if (*p == endchar)
266 break;
267
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200268 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
269 {
270 if (varargs != NULL)
271 *varargs = TRUE;
272 p += 3;
273 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274
275 if (argtypes != NULL)
276 {
277 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200278 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100280 if (!skip)
281 emsg(_(e_missing_name_after_dots));
282 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100283 }
284
285 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100286 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaardce24412022-02-08 20:35:30 +0000287 evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100288 if (p == arg)
289 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100290 if (*skipwhite(p) == '=')
291 {
292 emsg(_(e_cannot_use_default_for_variable_arguments));
293 break;
294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200296 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000297 else if (in_class && STRNCMP(p, "this.", 5) == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000298 {
299 // this.memberName
300 p += 5;
301 arg = p;
302 while (ASCII_ISALNUM(*p) || *p == '_')
303 ++p;
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000304 char_u *argend = p;
305
306 if (*skipwhite(p) == '=')
307 {
308 char_u *defval = skipwhite(skipwhite(p) + 1);
309 if (STRNCMP(defval, "v:none", 6) != 0)
310 {
311 semsg(_(e_constructor_default_value_must_be_vnone_str), p);
312 goto err_ret;
313 }
314 any_default = TRUE;
315 p = defval + 6;
316
317 if (ga_grow(default_args, 1) == FAIL)
318 goto err_ret;
319
320 char_u *expr = vim_strsave((char_u *)"v:none");
321 if (expr == NULL)
322 goto err_ret;
323 ((char_u **)(default_args->ga_data))
324 [default_args->ga_len] = expr;
325 default_args->ga_len++;
326 }
327 else if (any_default)
328 {
329 emsg(_(e_non_default_argument_follows_default_argument));
330 goto err_ret;
331 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000332
333 // TODO: check the argument is indeed a member
334 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
335 return FAIL;
336 if (newargs != NULL)
337 {
338 ((char_u **)(newargs->ga_data))[newargs->ga_len] =
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000339 vim_strnsave(arg, argend - arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000340 newargs->ga_len++;
341
342 if (argtypes != NULL && ga_grow(argtypes, 1) == OK)
343 {
344 // TODO: use the actual type
345 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] =
346 vim_strsave((char_u *)"any");
347
348 // Add a line to the function body for the assignment.
349 if (ga_grow(newlines, 1) == OK)
350 {
351 // "this.name = name"
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000352 int len = 5 + (argend - arg) + 3 + (argend - arg) + 1;
353 if (any_default)
354 len += 14 + 10;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000355 char_u *assignment = alloc(len);
356 if (assignment != NULL)
357 {
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000358 c = *argend;
359 *argend = NUL;
360 if (any_default)
361 vim_snprintf((char *)assignment, len,
362 "ifargisset %d this.%s = %s",
363 default_args->ga_len - 1, arg, arg);
364 else
365 vim_snprintf((char *)assignment, len,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000366 "this.%s = %s", arg, arg);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000367 *argend = c;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000368 ((char_u **)(newlines->ga_data))[
369 newlines->ga_len++] = assignment;
370 }
371 }
372 }
373 }
374 if (*p == ',')
375 ++p;
376 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200377 else
378 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200379 char_u *np;
380
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200381 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100382 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaardce24412022-02-08 20:35:30 +0000383 evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200385 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200386
Bram Moolenaar015cf102021-06-26 21:52:02 +0200387 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200388 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200389 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200390 if (*np == '=' && np[1] != '=' && np[1] != '~'
391 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200392 {
393 typval_T rettv;
394
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100395 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200396 any_default = TRUE;
Bram Moolenaareb5adf12022-09-04 13:41:37 +0100397 p = skipwhite(np + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000398 char_u *expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200399 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200400 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200401 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200402 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200403 if (ga_grow(default_args, 1) == FAIL)
404 goto err_ret;
405
406 // trim trailing whitespace
407 while (p > expr && VIM_ISWHITE(p[-1]))
408 p--;
409 c = *p;
410 *p = NUL;
411 expr = vim_strsave(expr);
412 if (expr == NULL)
413 {
414 *p = c;
415 goto err_ret;
416 }
417 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200418 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200419 default_args->ga_len++;
420 *p = c;
421 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200422 }
423 else
424 mustend = TRUE;
425 }
426 else if (any_default)
427 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000428 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200429 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200430 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200431
432 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
433 {
434 // Be tolerant when skipping
435 if (!skip)
436 {
437 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
438 goto err_ret;
439 }
440 p = skipwhite(p);
441 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200442 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200443 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200444 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200445 // Don't give this error when skipping, it makes the "->" not
446 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100447 // Allow missing space after comma in legacy functions.
448 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200449 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
450 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100451 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200452 goto err_ret;
453 }
454 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200455 else
456 mustend = TRUE;
457 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200458 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200459 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200460 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200461
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200462 if (*p != endchar)
463 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100464 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200465
466 *argp = p;
467 return OK;
468
469err_ret:
470 if (newargs != NULL)
471 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200472 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200473 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200474 return FAIL;
475}
476
477/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100478 * Parse the argument types, filling "fp->uf_arg_types".
479 * Return OK or FAIL.
480 */
481 static int
482parse_argument_types(ufunc_T *fp, garray_T *argtypes, int varargs)
483{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200484 int len = 0;
485
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100486 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
487 if (argtypes->ga_len > 0)
488 {
489 // When "varargs" is set the last name/type goes into uf_va_name
490 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200491 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100492
493 if (len > 0)
494 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
495 if (fp->uf_arg_types != NULL)
496 {
497 int i;
498 type_T *type;
499
500 for (i = 0; i < len; ++ i)
501 {
502 char_u *p = ((char_u **)argtypes->ga_data)[i];
503
504 if (p == NULL)
505 // will get the type from the default value
506 type = &t_unknown;
507 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100508 type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100509 if (type == NULL)
510 return FAIL;
511 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000512 if (i < fp->uf_args.ga_len
513 && (type->tt_type == VAR_FUNC
514 || type->tt_type == VAR_PARTIAL)
515 && var_wrong_func_name(
516 ((char_u **)fp->uf_args.ga_data)[i], TRUE))
517 return FAIL;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100518 }
519 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100520 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200521
522 if (varargs)
523 {
524 char_u *p;
525
526 // Move the last argument "...name: type" to uf_va_name and
527 // uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200528 --fp->uf_args.ga_len;
Bram Moolenaarf0571712023-01-04 13:16:20 +0000529 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len];
530 ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len] = NULL;
Bram Moolenaar2a389082021-04-09 20:24:31 +0200531 p = ((char_u **)argtypes->ga_data)[len];
532 if (p == NULL)
533 // TODO: get type from default value
534 fp->uf_va_type = &t_list_any;
535 else
536 {
537 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
538 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
539 {
540 semsg(_(e_variable_arguments_type_must_be_list_str),
541 ((char_u **)argtypes->ga_data)[len]);
542 return FAIL;
543 }
544 }
545 if (fp->uf_va_type == NULL)
546 return FAIL;
547 }
548
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100549 return OK;
550}
551
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100552 static int
553parse_return_type(ufunc_T *fp, char_u *ret_type)
554{
555 if (ret_type == NULL)
556 fp->uf_ret_type = &t_void;
557 else
558 {
559 char_u *p = ret_type;
560
561 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
562 if (fp->uf_ret_type == NULL)
563 {
564 fp->uf_ret_type = &t_void;
565 return FAIL;
566 }
567 }
568 return OK;
569}
570
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100571/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200572 * Register function "fp" as using "current_funccal" as its scope.
573 */
574 static int
575register_closure(ufunc_T *fp)
576{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200577 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100578 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200579 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200580 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200581 fp->uf_scoped = current_funccal;
582 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200583
Bram Moolenaarca16c602022-09-06 18:57:08 +0100584 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200585 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100586 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
587 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200588 return OK;
589}
590
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100591 static void
592set_ufunc_name(ufunc_T *fp, char_u *name)
593{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100594 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
595 // actually extends beyond the struct.
596 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100597
598 if (name[0] == K_SPECIAL)
599 {
600 fp->uf_name_exp = alloc(STRLEN(name) + 3);
601 if (fp->uf_name_exp != NULL)
602 {
603 STRCPY(fp->uf_name_exp, "<SNR>");
604 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
605 }
606 }
607}
608
Bram Moolenaar58016442016-07-31 18:30:22 +0200609/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100610 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
611 * return "buf" filled with a readable function name.
612 * Otherwise just return "name", thus the return value can always be used.
613 * "name" and "buf" may be equal.
614 */
615 char_u *
616make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
617{
618 size_t len;
619
620 if (name[0] != K_SPECIAL)
621 return name;
622 len = STRLEN(name);
623 if (len + 3 > bufsize)
624 return name;
625
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100626 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100627 mch_memmove(buf, "<SNR>", 5);
628 return buf;
629}
630
631/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200632 * Get a name for a lambda. Returned in static memory.
633 */
634 char_u *
635get_lambda_name(void)
636{
637 static char_u name[30];
638 static int lambda_no = 0;
639
640 sprintf((char*)name, "<lambda>%d", ++lambda_no);
641 return name;
642}
643
Bram Moolenaare01e5212023-01-08 20:31:18 +0000644/*
645 * Allocate a "ufunc_T" for a function called "name".
646 * Makes sure the size is right.
647 */
648 static ufunc_T *
649alloc_ufunc(char_u *name)
650{
651 // When the name is short we need to make sure we allocate enough bytes for
652 // the whole struct, including any padding.
653 size_t len = offsetof(ufunc_T, uf_name) + STRLEN(name) + 1;
654 return alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
655}
656
Bram Moolenaar801ab062020-06-25 19:27:56 +0200657#if defined(FEAT_LUA) || defined(PROTO)
658/*
659 * Registers a native C callback which can be called from Vim script.
660 * Returns the name of the Vim script function.
661 */
662 char_u *
663register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
664{
665 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200666 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200667
Bram Moolenaare01e5212023-01-08 20:31:18 +0000668 fp = alloc_ufunc(name);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200669 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200670 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200671
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200672 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200673 fp->uf_refcount = 1;
674 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000675 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200676 fp->uf_calls = 0;
677 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200678 fp->uf_cb = cb;
679 fp->uf_cb_free = cb_free;
680 fp->uf_cb_state = state;
681
682 set_ufunc_name(fp, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000683 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200684
685 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200686}
687#endif
688
Bram Moolenaar04b12692020-05-04 23:24:44 +0200689/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100690 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100691 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100692 * If "white_error" is not NULL check for correct use of white space and set
693 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100694 * Return NULL if no valid arrow found.
695 */
696 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100697skip_arrow(
698 char_u *start,
699 int equal_arrow,
700 char_u **ret_type,
701 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100702{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100703 char_u *s = start;
704 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100705
706 if (equal_arrow)
707 {
708 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100709 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100710 if (white_error != NULL && !VIM_ISWHITE(s[1]))
711 {
712 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100713 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100714 return NULL;
715 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100716 s = skipwhite(s + 1);
717 *ret_type = s;
718 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100719 if (s == *ret_type)
720 {
721 emsg(_(e_missing_return_type));
722 return NULL;
723 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100724 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100725 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100726 s = skipwhite(s);
727 if (*s != '=')
728 return NULL;
729 ++s;
730 }
731 if (*s != '>')
732 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100733 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
734 || !IS_WHITE_OR_NUL(s[1])))
735 {
736 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100737 semsg(_(e_white_space_required_before_and_after_str_at_str),
738 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100739 return NULL;
740 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100741 return skipwhite(s + 1);
742}
743
744/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100745 * Check if "*cmd" points to a function command and if so advance "*cmd" and
746 * return TRUE.
747 * Otherwise return FALSE;
748 * Do not consider "function(" to be a command.
749 */
750 static int
751is_function_cmd(char_u **cmd)
752{
753 char_u *p = *cmd;
754
755 if (checkforcmd(&p, "function", 2))
756 {
757 if (*p == '(')
758 return FALSE;
759 *cmd = p;
760 return TRUE;
761 }
762 return FALSE;
763}
764
765/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200766 * Called when defining a function: The context may be needed for script
767 * variables declared in a block that is visible now but not when the function
768 * is compiled or called later.
769 */
770 static void
771function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
772{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000773 if (cstack == NULL || cstack->cs_idx < 0)
774 return;
775
776 int count = cstack->cs_idx + 1;
777 int i;
778
779 fp->uf_block_ids = ALLOC_MULT(int, count);
780 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200781 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000782 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
783 sizeof(int) * count);
784 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200785 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000786
787 // Set flag in each block to indicate a function was defined. This
788 // is used to keep the variable when leaving the block, see
789 // hide_script_var().
790 for (i = 0; i <= cstack->cs_idx; ++i)
791 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200792}
793
794/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100795 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200796 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100797 * "newlines" must already have been initialized.
798 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
799 */
800 static int
801get_function_body(
802 exarg_T *eap,
803 garray_T *newlines,
804 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000805 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100806{
807 linenr_T sourcing_lnum_top = SOURCING_LNUM;
808 linenr_T sourcing_lnum_off;
809 int saved_wait_return = need_wait_return;
810 char_u *line_arg = line_arg_in;
811 int vim9_function = eap->cmdidx == CMD_def
812 || eap->cmdidx == CMD_block;
813#define MAX_FUNC_NESTING 50
814 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200815 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100816 int nesting = 0;
817 getline_opt_T getline_options;
818 int indent = 2;
819 char_u *skip_until = NULL;
820 int ret = FAIL;
821 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200822 int heredoc_concat_len = 0;
823 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100824 char_u *heredoc_trimmed = NULL;
825
Bram Moolenaar20677332021-06-06 17:02:53 +0200826 ga_init2(&heredoc_ga, 1, 500);
827
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100828 // Detect having skipped over comment lines to find the return
829 // type. Add NULL lines to keep the line count correct.
830 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
831 if (SOURCING_LNUM < sourcing_lnum_off)
832 {
833 sourcing_lnum_off -= SOURCING_LNUM;
834 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
835 goto theend;
836 while (sourcing_lnum_off-- > 0)
837 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
838 }
839
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200840 nesting_def[0] = vim9_function;
841 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100842 getline_options = vim9_function
843 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
844 for (;;)
845 {
846 char_u *theline;
847 char_u *p;
848 char_u *arg;
849
850 if (KeyTyped)
851 {
852 msg_scroll = TRUE;
853 saved_wait_return = FALSE;
854 }
855 need_wait_return = FALSE;
856
857 if (line_arg != NULL)
858 {
859 // Use eap->arg, split up in parts by line breaks.
860 theline = line_arg;
861 p = vim_strchr(theline, '\n');
862 if (p == NULL)
863 line_arg += STRLEN(line_arg);
864 else
865 {
866 *p = NUL;
867 line_arg = p + 1;
868 }
869 }
870 else
871 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000872 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100873 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100874 }
875 if (KeyTyped)
876 lines_left = Rows - 1;
877 if (theline == NULL)
878 {
879 // Use the start of the function for the line number.
880 SOURCING_LNUM = sourcing_lnum_top;
881 if (skip_until != NULL)
882 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200883 else if (nesting_inline[nesting])
884 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100885 else if (eap->cmdidx == CMD_def)
886 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100887 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000888 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100889 goto theend;
890 }
891
892 // Detect line continuation: SOURCING_LNUM increased more than one.
893 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
894 if (SOURCING_LNUM < sourcing_lnum_off)
895 sourcing_lnum_off -= SOURCING_LNUM;
896 else
897 sourcing_lnum_off = 0;
898
899 if (skip_until != NULL)
900 {
901 // Don't check for ":endfunc"/":enddef" between
902 // * ":append" and "."
903 // * ":python <<EOF" and "EOF"
904 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
905 if (heredoc_trimmed == NULL
906 || (is_heredoc && skipwhite(theline) == theline)
907 || STRNCMP(theline, heredoc_trimmed,
908 STRLEN(heredoc_trimmed)) == 0)
909 {
910 if (heredoc_trimmed == NULL)
911 p = theline;
912 else if (is_heredoc)
913 p = skipwhite(theline) == theline
914 ? theline : theline + STRLEN(heredoc_trimmed);
915 else
916 p = theline + STRLEN(heredoc_trimmed);
917 if (STRCMP(p, skip_until) == 0)
918 {
919 VIM_CLEAR(skip_until);
920 VIM_CLEAR(heredoc_trimmed);
921 getline_options = vim9_function
922 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
923 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200924
925 if (heredoc_concat_len > 0)
926 {
927 // Replace the starting line with all the concatenated
928 // lines.
929 ga_concat(&heredoc_ga, theline);
930 vim_free(((char_u **)(newlines->ga_data))[
931 heredoc_concat_len - 1]);
932 ((char_u **)(newlines->ga_data))[
933 heredoc_concat_len - 1] = heredoc_ga.ga_data;
934 ga_init(&heredoc_ga);
935 heredoc_concat_len = 0;
936 theline += STRLEN(theline); // skip the "EOF"
937 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100938 }
939 }
940 }
941 else
942 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200943 int c;
944 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +0000945 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100946
947 // skip ':' and blanks
948 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
949 ;
950
951 // Check for "endfunction", "enddef" or "}".
952 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +0000953 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200954 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100955 ? *p == '}'
956 : (checkforcmd(&p, nesting_def[nesting]
957 ? "enddef" : "endfunction", 4)
958 && *p != ':'))
959 {
Bram Moolenaarb2175222022-03-05 20:24:41 +0000960 if (!nesting_inline[nesting] && nesting_def[nesting]
961 && p < cmd + 6)
962 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100963 if (nesting-- == 0)
964 {
965 char_u *nextcmd = NULL;
966
967 if (*p == '|' || *p == '}')
968 nextcmd = p + 1;
969 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
970 nextcmd = line_arg;
971 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100972 && (vim9_function || p_verbose > 0))
973 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200974 SOURCING_LNUM = sourcing_lnum_top
975 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100976 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000977 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100978 else
979 give_warning2((char_u *)
980 _("W22: Text found after :endfunction: %s"),
981 p, TRUE);
982 }
983 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100984 {
985 // Another command follows. If the line came from "eap"
986 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000987 // change "eap->cmdlinep" to point to the last fetched
988 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100989 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000990 if (lines_to_free->ga_len > 0
991 && *eap->cmdlinep !=
992 ((char_u **)lines_to_free->ga_data)
993 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100994 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000995 // *cmdlinep will be freed later, thus remove the
996 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100997 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000998 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
999 [lines_to_free->ga_len - 1];
1000 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001001 }
1002 }
1003 break;
1004 }
1005 }
1006
1007 // Check for mismatched "endfunc" or "enddef".
1008 // We don't check for "def" inside "func" thus we also can't check
1009 // for "enddef".
1010 // We continue to find the end of the function, although we might
1011 // not find it.
1012 else if (nesting_def[nesting])
1013 {
1014 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1015 emsg(_(e_mismatched_endfunction));
1016 }
1017 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1018 emsg(_(e_mismatched_enddef));
1019
1020 // Increase indent inside "if", "while", "for" and "try", decrease
1021 // at "end".
1022 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1023 indent -= 2;
1024 else if (STRNCMP(p, "if", 2) == 0
1025 || STRNCMP(p, "wh", 2) == 0
1026 || STRNCMP(p, "for", 3) == 0
1027 || STRNCMP(p, "try", 3) == 0)
1028 indent += 2;
1029
1030 // Check for defining a function inside this function.
1031 // Only recognize "def" inside "def", not inside "function",
1032 // For backwards compatibility, see Test_function_python().
1033 c = *p;
1034 if (is_function_cmd(&p)
1035 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1036 {
1037 if (*p == '!')
1038 p = skipwhite(p + 1);
1039 p += eval_fname_script(p);
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00001040 vim_free(trans_function_name(&p, NULL, TRUE, 0));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001041 if (*skipwhite(p) == '(')
1042 {
1043 if (nesting == MAX_FUNC_NESTING - 1)
1044 emsg(_(e_function_nesting_too_deep));
1045 else
1046 {
1047 ++nesting;
1048 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001049 nesting_inline[nesting] = FALSE;
1050 indent += 2;
1051 }
1052 }
1053 }
1054
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001055 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001056 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001057 // Not a comment line: check for nested inline function.
1058 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001059 while (end > p && VIM_ISWHITE(*end))
1060 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001061 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001062 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001063 int is_block;
1064
1065 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001066 --end;
1067 while (end > p && VIM_ISWHITE(*end))
1068 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001069 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1070 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001071 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001072 char_u *s = p;
1073
1074 // check for line starting with "au" for :autocmd or
1075 // "com" for :command, these can use a {} block
1076 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1077 || checkforcmd_noparen(&s, "command", 3);
1078 }
1079
1080 if (is_block)
1081 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001082 if (nesting == MAX_FUNC_NESTING - 1)
1083 emsg(_(e_function_nesting_too_deep));
1084 else
1085 {
1086 ++nesting;
1087 nesting_def[nesting] = TRUE;
1088 nesting_inline[nesting] = TRUE;
1089 indent += 2;
1090 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001091 }
1092 }
1093 }
1094
1095 // Check for ":append", ":change", ":insert". Not for :def.
1096 p = skip_range(p, FALSE, NULL);
1097 if (!vim9_function
1098 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1099 || (p[0] == 'c'
1100 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1101 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1102 && (STRNCMP(&p[3], "nge", 3) != 0
1103 || !ASCII_ISALPHA(p[6])))))))
1104 || (p[0] == 'i'
1105 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1106 && (!ASCII_ISALPHA(p[2])
1107 || (p[2] == 's'
1108 && (!ASCII_ISALPHA(p[3])
1109 || p[3] == 'e'))))))))
1110 skip_until = vim_strsave((char_u *)".");
1111
1112 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1113 arg = skipwhite(skiptowhite(p));
1114 if (arg[0] == '<' && arg[1] =='<'
1115 && ((p[0] == 'p' && p[1] == 'y'
1116 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1117 || ((p[2] == '3' || p[2] == 'x')
1118 && !ASCII_ISALPHA(p[3]))))
1119 || (p[0] == 'p' && p[1] == 'e'
1120 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1121 || (p[0] == 't' && p[1] == 'c'
1122 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1123 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1124 && !ASCII_ISALPHA(p[3]))
1125 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1126 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1127 || (p[0] == 'm' && p[1] == 'z'
1128 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1129 ))
1130 {
1131 // ":python <<" continues until a dot, like ":append"
1132 p = skipwhite(arg + 2);
1133 if (STRNCMP(p, "trim", 4) == 0)
1134 {
1135 // Ignore leading white space.
1136 p = skipwhite(p + 4);
1137 heredoc_trimmed = vim_strnsave(theline,
1138 skipwhite(theline) - theline);
1139 }
1140 if (*p == NUL)
1141 skip_until = vim_strsave((char_u *)".");
1142 else
1143 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1144 getline_options = GETLINE_NONE;
1145 is_heredoc = TRUE;
zeertzjqa93d9cd2023-05-02 16:25:47 +01001146 if (vim9_function && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001147 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001148 }
1149
Bram Moolenaard881d152022-05-13 13:50:36 +01001150 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001151 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001152 // Check for ":cmd v =<< [trim] EOF"
1153 // and ":cmd [a, b] =<< [trim] EOF"
1154 // and "lines =<< [trim] EOF" for Vim9
1155 // Where "cmd" can be "let", "var", "final" or "const".
zeertzjqa93d9cd2023-05-02 16:25:47 +01001156 arg = p;
1157 if (checkforcmd(&arg, "let", 2)
1158 || checkforcmd(&arg, "var", 3)
1159 || checkforcmd(&arg, "final", 5)
1160 || checkforcmd(&arg, "const", 5)
1161 || vim9_function)
Bram Moolenaard881d152022-05-13 13:50:36 +01001162 {
zeertzjqa93d9cd2023-05-02 16:25:47 +01001163 while (vim_strchr((char_u *)"$@&", *arg) != NULL)
1164 ++arg;
1165 arg = skipwhite(find_name_end(arg, NULL, NULL,
1166 FNE_INCL_BR | FNE_ALLOW_CURLY));
1167 if (vim9_function && *arg == ':')
1168 arg = skipwhite(skip_type(skipwhite(arg + 1), FALSE));
1169 if (arg[0] == '=' && arg[1] == '<' && arg[2] =='<')
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001170 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001171 p = skipwhite(arg + 3);
1172 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001173 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001174 if (STRNCMP(p, "trim", 4) == 0)
1175 {
1176 // Ignore leading white space.
1177 p = skipwhite(p + 4);
1178 heredoc_trimmed = vim_strnsave(theline,
1179 skipwhite(theline) - theline);
1180 continue;
1181 }
1182 if (STRNCMP(p, "eval", 4) == 0)
1183 {
1184 // Ignore leading white space.
1185 p = skipwhite(p + 4);
1186 continue;
1187 }
1188 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001189 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001190 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1191 getline_options = GETLINE_NONE;
1192 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001193 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001194 }
1195 }
1196 }
1197
1198 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001199 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001200 goto theend;
1201
Bram Moolenaar20677332021-06-06 17:02:53 +02001202 if (heredoc_concat_len > 0)
1203 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001204 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001205 // to be used for the instruction later.
1206 ga_concat(&heredoc_ga, theline);
1207 ga_concat(&heredoc_ga, (char_u *)"\n");
1208 p = vim_strsave((char_u *)"");
1209 }
1210 else
1211 {
1212 // Copy the line to newly allocated memory. get_one_sourceline()
1213 // allocates 250 bytes per line, this saves 80% on average. The
1214 // cost is an extra alloc/free.
1215 p = vim_strsave(theline);
1216 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001217 if (p == NULL)
1218 goto theend;
1219 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1220
1221 // Add NULL lines for continuation lines, so that the line count is
1222 // equal to the index in the growarray.
1223 while (sourcing_lnum_off-- > 0)
1224 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1225
1226 // Check for end of eap->arg.
1227 if (line_arg != NULL && *line_arg == NUL)
1228 line_arg = NULL;
1229 }
1230
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001231 // Return OK when no error was detected.
1232 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001233 ret = OK;
1234
1235theend:
1236 vim_free(skip_until);
1237 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001238 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001239 need_wait_return |= saved_wait_return;
1240 return ret;
1241}
1242
1243/*
1244 * Handle the body of a lambda. *arg points to the "{", process statements
1245 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001246 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001247 * When successful "rettv" is set to a funcref.
1248 */
1249 static int
1250lambda_function_body(
1251 char_u **arg,
1252 typval_T *rettv,
1253 evalarg_T *evalarg,
1254 garray_T *newargs,
1255 garray_T *argtypes,
1256 int varargs,
1257 garray_T *default_args,
1258 char_u *ret_type)
1259{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001260 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001261 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001262 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001263 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001264 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001265 exarg_T eap;
1266 garray_T newlines;
1267 char_u *cmdline = NULL;
1268 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001269 partial_T *pt;
1270 char_u *name;
1271 int lnum_save = -1;
1272 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1273
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001274 *arg = skipwhite(*arg + 1);
1275 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001276 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001277 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001278 return FAIL;
1279 }
1280
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001281 CLEAR_FIELD(eap);
1282 eap.cmdidx = CMD_block;
1283 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001284 eap.cmdlinep = &cmdline;
1285 eap.skip = !evaluate;
1286 if (evalarg->eval_cctx != NULL)
1287 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1288 else
1289 {
1290 eap.getline = evalarg->eval_getline;
1291 eap.cookie = evalarg->eval_cookie;
1292 }
1293
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001294 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001295 if (get_function_body(&eap, &newlines, NULL,
1296 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001297 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001298
1299 // When inside a lambda must add the function lines to evalarg.eval_ga.
1300 evalarg->eval_break_count += newlines.ga_len;
1301 if (gap->ga_itemsize > 0)
1302 {
1303 int idx;
1304 char_u *last;
1305 size_t plen;
1306 char_u *pnl;
1307
1308 for (idx = 0; idx < newlines.ga_len; ++idx)
1309 {
1310 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1311
Bram Moolenaarecb66452021-05-18 15:09:18 +02001312 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001313 goto erret;
1314
1315 // Going to concatenate the lines after parsing. For an empty or
1316 // comment line use an empty string.
1317 // Insert NL characters at the start of each line, the string will
1318 // be split again later in .get_lambda_tv().
1319 if (*p == NUL || vim9_comment_start(p))
1320 p = (char_u *)"";
1321 plen = STRLEN(p);
1322 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1323 if (pnl != NULL)
1324 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001325 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1326 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001327 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001328 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001329 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001330 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001331 // more is following after the "}", which was skipped
1332 last = cmdline;
1333 else
1334 // nothing is following the "}"
1335 last = (char_u *)"}";
1336 plen = STRLEN(last);
1337 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1338 if (pnl != NULL)
1339 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001340 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1341 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001342 }
1343
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001344 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001345 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001346 garray_T *tfgap = &evalarg->eval_tofree_ga;
1347
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001348 // Something comes after the "}".
1349 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001350
1351 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001352 if (ga_grow(tfgap, 1) == OK)
1353 {
1354 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1355 evalarg->eval_using_cmdline = TRUE;
1356 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001357 }
1358 else
1359 *arg = (char_u *)"";
1360
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001361 if (!evaluate)
1362 {
1363 ret = OK;
1364 goto erret;
1365 }
1366
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001367 name = get_lambda_name();
Bram Moolenaare01e5212023-01-08 20:31:18 +00001368 ufunc = alloc_ufunc(name);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001369 if (ufunc == NULL)
1370 goto erret;
1371 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001372 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001373 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001374 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001375 ufunc->uf_refcount = 1;
1376 ufunc->uf_args = *newargs;
1377 newargs->ga_data = NULL;
1378 ufunc->uf_def_args = *default_args;
1379 default_args->ga_data = NULL;
1380 ufunc->uf_func_type = &t_func_any;
1381
1382 // error messages are for the first function line
1383 lnum_save = SOURCING_LNUM;
1384 SOURCING_LNUM = sourcing_lnum_top;
1385
1386 // parse argument types
1387 if (parse_argument_types(ufunc, argtypes, varargs) == FAIL)
1388 {
1389 SOURCING_LNUM = lnum_save;
1390 goto erret;
1391 }
1392
1393 // parse the return type, if any
1394 if (parse_return_type(ufunc, ret_type) == FAIL)
1395 goto erret;
1396
1397 pt = ALLOC_CLEAR_ONE(partial_T);
1398 if (pt == NULL)
1399 goto erret;
1400 pt->pt_func = ufunc;
1401 pt->pt_refcount = 1;
1402
1403 ufunc->uf_lines = newlines;
1404 newlines.ga_data = NULL;
1405 if (sandbox)
1406 ufunc->uf_flags |= FC_SANDBOX;
1407 if (!ASCII_ISUPPER(*ufunc->uf_name))
1408 ufunc->uf_flags |= FC_VIM9;
1409 ufunc->uf_script_ctx = current_sctx;
1410 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1411 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1412 set_function_type(ufunc);
1413
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001414 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1415
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001416 rettv->vval.v_partial = pt;
1417 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001418 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001419 ret = OK;
1420
1421erret:
1422 if (lnum_save >= 0)
1423 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001424 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001425 if (newargs != NULL)
1426 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001427 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001428 if (ufunc != NULL)
1429 {
1430 func_clear(ufunc, TRUE);
1431 func_free(ufunc, TRUE);
1432 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001433 return ret;
1434}
1435
1436/*
1437 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001438 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001439 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001440 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1441 */
1442 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001443get_lambda_tv(
1444 char_u **arg,
1445 typval_T *rettv,
1446 int types_optional,
1447 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001448{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001449 int evaluate = evalarg != NULL
1450 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001451 garray_T newargs;
1452 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001453 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001454 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001455 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001456 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001457 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001458 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001459 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001460 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001461 char_u *s;
1462 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001463 int *old_eval_lavars = eval_lavars_used;
1464 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001465 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001466 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001467 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001468 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001469 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001470 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001471
Bram Moolenaar4525a572022-02-13 11:57:33 +00001472 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001473 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001474
1475 ga_init(&newargs);
1476 ga_init(&newlines);
1477
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001478 // First, check if this is really a lambda expression. "->" or "=>" must
1479 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001480 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001481 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001482 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001483 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001484 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001485 {
1486 if (types_optional)
1487 ga_clear_strings(&argtypes);
Bram Moolenaar0346b792021-01-31 22:18:29 +01001488 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001489 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001490
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001491 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001492 if (evaluate)
1493 pnewargs = &newargs;
1494 else
1495 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001496 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001497 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001498 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001499 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001500 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001501 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001502 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001503 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001504 {
1505 if (types_optional)
1506 ga_clear_strings(&argtypes);
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001507 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001508 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001509 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001510 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001511
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001512 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1513 if (ret_type != NULL)
1514 {
1515 ret_type = vim_strsave(ret_type);
1516 tofree2 = ret_type;
1517 }
1518
Bram Moolenaare38eab22019-12-05 21:50:01 +01001519 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001520 if (evaluate)
1521 eval_lavars_used = &eval_lavars;
1522
Bram Moolenaar65c44152020-12-24 15:14:01 +01001523 *arg = skipwhite_and_linebreak(*arg, evalarg);
1524
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001525 // Recognize "{" as the start of a function body.
1526 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001527 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001528 if (evalarg == NULL)
1529 // cannot happen?
1530 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001531 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001532 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1533 types_optional ? &argtypes : NULL, varargs,
1534 &default_args, ret_type) == FAIL)
1535 goto errret;
1536 goto theend;
1537 }
1538 if (default_args.ga_len > 0)
1539 {
1540 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001541 goto errret;
1542 }
1543
Bram Moolenaare38eab22019-12-05 21:50:01 +01001544 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001545 start = *arg;
1546 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001547 if (ret == FAIL)
1548 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001549
Bram Moolenaar65c44152020-12-24 15:14:01 +01001550 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001551 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001552 *arg = skipwhite_and_linebreak(*arg, evalarg);
1553 if (**arg != '}')
1554 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001555 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001556 goto errret;
1557 }
1558 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001559 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001560
1561 if (evaluate)
1562 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001563 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001564 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001565 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001566 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001567 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001568
Bram Moolenaare01e5212023-01-08 20:31:18 +00001569 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001570 if (fp == NULL)
1571 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001572 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001573 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001574 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001575 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001576
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001577 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001578 if (ga_grow(&newlines, 1) == FAIL)
1579 goto errret;
1580
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001581 // If there are line breaks, we need to split up the string.
1582 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001583 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001584 line_end = end;
1585
1586 // Add "return " before the expression (or the first line).
1587 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001588 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001589 if (p == NULL)
1590 goto errret;
1591 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1592 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001593 vim_strncpy(p + 7, start, line_end - start);
1594
1595 if (line_end != end)
1596 {
1597 // Add more lines, split by line breaks. Thus is used when a
1598 // lambda with { cmds } is encountered.
1599 while (*line_end == '\n')
1600 {
1601 if (ga_grow(&newlines, 1) == FAIL)
1602 goto errret;
1603 start = line_end + 1;
1604 line_end = vim_strchr(start, '\n');
1605 if (line_end == NULL)
1606 line_end = end;
1607 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1608 vim_strnsave(start, line_end - start);
1609 }
1610 }
1611
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001612 if (strstr((char *)p + 7, "a:") == NULL)
1613 // No a: variables are used for sure.
1614 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001615
1616 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001617 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001618 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001619 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001620 if (types_optional)
1621 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001622 if (parse_argument_types(fp, &argtypes,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001623 vim9script && varargs) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001624 goto errret;
1625 if (ret_type != NULL)
1626 {
1627 fp->uf_ret_type = parse_type(&ret_type,
1628 &fp->uf_type_list, TRUE);
1629 if (fp->uf_ret_type == NULL)
1630 goto errret;
1631 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001632 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001633 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001634 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001635
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001636 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001637 if (current_funccal != NULL && eval_lavars)
1638 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001639 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001640 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001641 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001642 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001643
1644#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001645 if (prof_def_func())
1646 func_do_profile(fp);
1647#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001648 if (sandbox)
1649 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001650 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001651 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001652 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001653 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001654 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001655 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001656 // Use the line number of the arguments.
1657 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001658
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001659 function_using_block_scopes(fp, evalarg->eval_cstack);
1660
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001661 pt->pt_func = fp;
1662 pt->pt_refcount = 1;
1663 rettv->vval.v_partial = pt;
1664 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001665
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001666 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001667 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001668
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001669theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001670 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001671 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001672 if (types_optional)
1673 ga_clear_strings(&argtypes);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001674
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001675 return OK;
1676
1677errret:
1678 ga_clear_strings(&newargs);
1679 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001680 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001681 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001682 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001683 ga_clear_strings(&argtypes);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001684 if (fp != NULL)
1685 vim_free(fp->uf_arg_types);
1686 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001687 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001688 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001689 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001690 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001691 return FAIL;
1692}
1693
1694/*
1695 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1696 * name it contains, otherwise return "name".
1697 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1698 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001699 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1700 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001701 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001702 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001703 */
1704 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001705deref_func_name(
1706 char_u *name,
1707 int *lenp,
1708 partial_T **partialp,
1709 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001710 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001711 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001712 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001713{
1714 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001715 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001716 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001717 char_u *s = NULL;
1718 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001719 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001720
1721 if (partialp != NULL)
1722 *partialp = NULL;
1723
1724 cc = name[*lenp];
1725 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001726
Bram Moolenaar71f21932022-01-07 18:20:55 +00001727 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001728 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001729 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001730 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001731 tv = &v->di_tv;
1732 }
1733 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1734 {
1735 imported_T *import;
1736 char_u *p = name;
1737 int len = *lenp;
1738
1739 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001740 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001741 p = name + 2;
1742 len -= 2;
1743 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001744 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001745
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001746 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001747 if (import != NULL)
1748 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001749 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001750 if (new_function)
1751 semsg(_(e_redefining_imported_item_str), name);
1752 else
1753 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001754 name[len] = cc;
1755 *lenp = 0;
1756 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001757 }
1758 }
1759
1760 if (tv != NULL)
1761 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001762 if (found_var != NULL)
1763 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001764 if (tv->v_type == VAR_FUNC)
1765 {
1766 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001767 {
1768 *lenp = 0;
1769 return (char_u *)""; // just in case
1770 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001771 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001772 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001773 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001774
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001775 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001776 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001777 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001778
1779 if (pt == NULL)
1780 {
1781 *lenp = 0;
1782 return (char_u *)""; // just in case
1783 }
1784 if (partialp != NULL)
1785 *partialp = pt;
1786 s = partial_name(pt);
1787 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001788 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001789
1790 if (s != NULL)
1791 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001792 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001793 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001794 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001795
1796 if (sv != NULL)
1797 *type = sv->sv_type;
1798 }
1799 return s;
1800 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001801 }
1802
1803 return name;
1804}
1805
1806/*
1807 * Give an error message with a function name. Handle <SNR> things.
1808 * "ermsg" is to be passed without translation, use N_() instead of _().
1809 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001810 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001811emsg_funcname(char *ermsg, char_u *name)
1812{
Bram Moolenaar54598062022-01-13 12:05:09 +00001813 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001814
Bram Moolenaar54598062022-01-13 12:05:09 +00001815 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001816 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001817 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001818 if (p != name)
1819 vim_free(p);
1820}
1821
1822/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001823 * Get function arguments at "*arg" and advance it.
1824 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001825 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001826 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001827 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001828get_func_arguments(
1829 char_u **arg,
1830 evalarg_T *evalarg,
1831 int partial_argc,
1832 typval_T *argvars,
1833 int *argcount)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001834{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001835 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001836 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001837 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001838 int evaluate = evalarg == NULL
1839 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001840
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001841 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001842 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001843 // skip the '(' or ',' and possibly line breaks
1844 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1845
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001846 if (*argp == ')' || *argp == ',' || *argp == NUL)
1847 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001848 if (eval1(&argp, &argvars[*argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001849 {
1850 ret = FAIL;
1851 break;
1852 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001853 ++*argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001854 // The comma should come right after the argument, but this wasn't
1855 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001856 if (vim9script)
1857 {
1858 if (*argp != ',' && *skipwhite(argp) == ',')
1859 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001860 if (evaluate)
1861 semsg(_(e_no_white_space_allowed_before_str_str),
1862 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001863 ret = FAIL;
1864 break;
1865 }
1866 }
1867 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001868 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001869 if (*argp != ',')
1870 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001871 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
1872 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001873 if (evaluate)
1874 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001875 ret = FAIL;
1876 break;
1877 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001878 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001879
Bram Moolenaare6b53242020-07-01 17:28:33 +02001880 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001881 if (*argp == ')')
1882 ++argp;
1883 else
1884 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001885 *arg = argp;
1886 return ret;
1887}
1888
1889/*
1890 * Call a function and put the result in "rettv".
1891 * Return OK or FAIL.
1892 */
1893 int
1894get_func_tv(
1895 char_u *name, // name of the function
1896 int len, // length of "name" or -1 to use strlen()
1897 typval_T *rettv,
1898 char_u **arg, // argument, pointing to the '('
1899 evalarg_T *evalarg, // for line continuation
1900 funcexe_T *funcexe) // various values
1901{
1902 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001903 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001904 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1905 int argcount = 0; // number of arguments found
1906 int vim9script = in_vim9script();
1907 int evaluate = evalarg == NULL
1908 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
1909
1910 argp = *arg;
1911 ret = get_func_arguments(&argp, evalarg,
1912 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
1913 argvars, &argcount);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001914
1915 if (ret == OK)
1916 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001917 int i = 0;
1918 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001919
1920 if (get_vim_var_nr(VV_TESTING))
1921 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001922 // Prepare for calling test_garbagecollect_now(), need to know
1923 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001924 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001925 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001926 for (i = 0; i < argcount; ++i)
1927 if (ga_grow(&funcargs, 1) == OK)
1928 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1929 &argvars[i];
1930 }
1931
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001932 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00001933 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001934 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001935 // An error in a builtin function does not return FAIL, but we do
1936 // want to abort further processing if an error was given.
1937 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001938 clear_tv(rettv);
1939 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001940
1941 funcargs.ga_len -= i;
1942 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001943 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001944 {
1945 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001946 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001947 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001948 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001949 }
1950
1951 while (--argcount >= 0)
1952 clear_tv(&argvars[argcount]);
1953
Bram Moolenaar4525a572022-02-13 11:57:33 +00001954 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02001955 *arg = argp;
1956 else
1957 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001958 return ret;
1959}
1960
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001961/*
1962 * Return TRUE if "p" starts with "<SID>" or "s:".
1963 * Only works if eval_fname_script() returned non-zero for "p"!
1964 */
1965 static int
1966eval_fname_sid(char_u *p)
1967{
1968 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
1969}
1970
1971/*
1972 * In a script change <SID>name() and s:name() to K_SNR 123_name().
1973 * Change <SNR>123_name() to K_SNR 123_name().
1974 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001975 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001976 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001977 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00001978fname_trans_sid(
1979 char_u *name,
1980 char_u *fname_buf,
1981 char_u **tofree,
1982 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001983{
1984 int llen;
1985 char_u *fname;
1986 int i;
1987
1988 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001989 if (llen == 0)
1990 return name; // no prefix
1991
1992 fname_buf[0] = K_SPECIAL;
1993 fname_buf[1] = KS_EXTRA;
1994 fname_buf[2] = (int)KE_SNR;
1995 i = 3;
1996 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001997 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001998 if (current_sctx.sc_sid <= 0)
1999 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002000 else
2001 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002002 sprintf((char *)fname_buf + 3, "%ld_",
2003 (long)current_sctx.sc_sid);
2004 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002005 }
2006 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002007 if (i + STRLEN(name + llen) < FLEN_FIXED)
2008 {
2009 STRCPY(fname_buf + i, name + llen);
2010 fname = fname_buf;
2011 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002012 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002013 {
2014 fname = alloc(i + STRLEN(name + llen) + 1);
2015 if (fname == NULL)
2016 *error = FCERR_OTHER;
2017 else
2018 {
2019 *tofree = fname;
2020 mch_memmove(fname, fname_buf, (size_t)i);
2021 STRCPY(fname + i, name + llen);
2022 }
2023 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002024 return fname;
2025}
2026
2027/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002028 * Concatenate the script ID and function name into "<SNR>99_name".
2029 * "buffer" must have size MAX_FUNC_NAME_LEN.
2030 */
2031 void
2032func_name_with_sid(char_u *name, int sid, char_u *buffer)
2033{
2034 // A script-local function is stored as "<SNR>99_name".
2035 buffer[0] = K_SPECIAL;
2036 buffer[1] = KS_EXTRA;
2037 buffer[2] = (int)KE_SNR;
2038 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2039 (long)sid, name);
2040}
2041
2042/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002043 * Find a function "name" in script "sid".
2044 */
2045 static ufunc_T *
2046find_func_with_sid(char_u *name, int sid)
2047{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002048 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002049 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050
Bram Moolenaarb8822442022-01-11 15:24:05 +00002051 if (!SCRIPT_ID_VALID(sid))
2052 return NULL; // not in a script
2053
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002054 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002055 hi = hash_find(&func_hashtab, buffer);
2056 if (!HASHITEM_EMPTY(hi))
2057 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002058 return NULL;
2059}
2060
2061/*
2062 * Find a function "name" in script "sid" prefixing the autoload prefix.
2063 */
2064 static ufunc_T *
2065find_func_with_prefix(char_u *name, int sid)
2066{
2067 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002068 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002069 scriptitem_T *si;
2070
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002071 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002072 return NULL; // already has the prefix
2073 if (!SCRIPT_ID_VALID(sid))
2074 return NULL; // not in a script
2075 si = SCRIPT_ITEM(sid);
2076 if (si->sn_autoload_prefix != NULL)
2077 {
2078 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2079 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002080 char_u *namep;
2081
2082 // skip a "<SNR>99_" prefix
2083 namep = untrans_function_name(name);
2084 if (namep == NULL)
2085 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002086
2087 // An exported function in an autoload script is stored as
2088 // "dir#path#name".
2089 if (len < sizeof(buffer))
2090 auto_name = buffer;
2091 else
2092 auto_name = alloc(len);
2093 if (auto_name != NULL)
2094 {
2095 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002096 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002097 hi = hash_find(&func_hashtab, auto_name);
2098 if (auto_name != buffer)
2099 vim_free(auto_name);
2100 if (!HASHITEM_EMPTY(hi))
2101 return HI2UF(hi);
2102 }
2103 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104
2105 return NULL;
2106}
2107
2108/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002109 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002110 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2111 * functions.
2112 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002113 * Return NULL for unknown function.
2114 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002115 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002116find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002117{
2118 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002120
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002121 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002123 // Find script-local function before global one.
2124 if (in_vim9script() && eval_isnamec1(*name)
2125 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002126 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002127 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2128 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002129 if (func != NULL)
2130 return func;
2131 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002132 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2133 {
2134 char_u *p = name + 5;
2135 long sid;
2136
2137 // printable "<SNR>123_Name" form
2138 sid = getdigits(&p);
2139 if (*p == '_')
2140 {
2141 func = find_func_with_sid(p + 1, (int)sid);
2142 if (func != NULL)
2143 return func;
2144 }
2145 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002146 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002147
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002148 if ((flags & FFED_NO_GLOBAL) == 0)
2149 {
2150 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002151 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002152 if (!HASHITEM_EMPTY(hi))
2153 return HI2UF(hi);
2154 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155
Bram Moolenaarb8822442022-01-11 15:24:05 +00002156 // Find autoload function if this is an autoload script.
2157 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2158 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159}
2160
2161/*
2162 * Find a function by name, return pointer to it in ufuncs.
2163 * "cctx" is passed in a :def function to find imported functions.
2164 * Return NULL for unknown or dead function.
2165 */
2166 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002167find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002169 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002170
2171 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2172 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002173 return NULL;
2174}
2175
2176/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002177 * Return TRUE if "ufunc" is a global function.
2178 */
2179 int
2180func_is_global(ufunc_T *ufunc)
2181{
2182 return ufunc->uf_name[0] != K_SPECIAL;
2183}
2184
2185/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002186 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2187 */
2188 int
2189func_requires_g_prefix(ufunc_T *ufunc)
2190{
2191 return ufunc->uf_name[0] != K_SPECIAL
2192 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002193 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
2194 && !isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002195}
2196
2197/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002198 * Copy the function name of "fp" to buffer "buf".
2199 * "buf" must be able to hold the function name plus three bytes.
2200 * Takes care of script-local function names.
2201 */
2202 static void
2203cat_func_name(char_u *buf, ufunc_T *fp)
2204{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002205 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002206 {
2207 STRCPY(buf, "<SNR>");
2208 STRCAT(buf, fp->uf_name + 3);
2209 }
2210 else
2211 STRCPY(buf, fp->uf_name);
2212}
2213
2214/*
2215 * Add a number variable "name" to dict "dp" with value "nr".
2216 */
2217 static void
2218add_nr_var(
2219 dict_T *dp,
2220 dictitem_T *v,
2221 char *name,
2222 varnumber_T nr)
2223{
2224 STRCPY(v->di_key, name);
2225 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002226 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002227 v->di_tv.v_type = VAR_NUMBER;
2228 v->di_tv.v_lock = VAR_FIXED;
2229 v->di_tv.vval.v_number = nr;
2230}
2231
2232/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002233 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002234 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002235 static void
2236free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002237{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002238 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002239
Bram Moolenaarca16c602022-09-06 18:57:08 +01002240 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002241 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002242 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002243
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002244 // When garbage collecting a funccall_T may be freed before the
2245 // function that references it, clear its uf_scoped field.
2246 // The function may have been redefined and point to another
2247 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002248 if (fp != NULL && fp->uf_scoped == fc)
2249 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002250 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002251 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002252
Bram Moolenaarca16c602022-09-06 18:57:08 +01002253 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002254 vim_free(fc);
2255}
2256
2257/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002258 * Free "fc" and what it contains.
2259 * Can be called only when "fc" is kept beyond the period of it called,
2260 * i.e. after cleanup_function_call(fc).
2261 */
2262 static void
2263free_funccal_contents(funccall_T *fc)
2264{
2265 listitem_T *li;
2266
2267 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002268 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002269
2270 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002271 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002272
2273 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002274 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002275 clear_tv(&li->li_tv);
2276
2277 free_funccal(fc);
2278}
2279
2280/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002281 * Handle the last part of returning from a function: free the local hashtable.
2282 * Unless it is still in use by a closure.
2283 */
2284 static void
2285cleanup_function_call(funccall_T *fc)
2286{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002287 int may_free_fc = fc->fc_refcount <= 0;
2288 int free_fc = TRUE;
2289
Bram Moolenaarca16c602022-09-06 18:57:08 +01002290 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002291
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002292 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002293 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2294 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002295 else
2296 free_fc = FALSE;
2297
2298 // If the a:000 list and the l: and a: dicts are not referenced and
2299 // there is no closure using it, we can free the funccall_T and what's
2300 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002301 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2302 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002303 else
2304 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002305 int todo;
2306 hashitem_T *hi;
2307 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002308
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002309 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002310
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002311 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002312 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002313 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002314 {
2315 if (!HASHITEM_EMPTY(hi))
2316 {
2317 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002318 di = HI2DI(hi);
2319 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002320 }
2321 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002322 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002323
Bram Moolenaarca16c602022-09-06 18:57:08 +01002324 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2325 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002326 else
2327 {
2328 listitem_T *li;
2329
2330 free_fc = FALSE;
2331
2332 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002333 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002334 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002335 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002336
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002337 if (free_fc)
2338 free_funccal(fc);
2339 else
2340 {
2341 static int made_copy = 0;
2342
2343 // "fc" is still in use. This can happen when returning "a:000",
2344 // assigning "l:" to a global variable or defining a closure.
2345 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002346 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002347 previous_funccal = fc;
2348
2349 if (want_garbage_collect)
2350 // If garbage collector is ready, clear count.
2351 made_copy = 0;
2352 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002353 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002354 // We have made a lot of copies, worth 4 Mbyte. This can happen
2355 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002356 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002357 // too much memory.
2358 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002359 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002360 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002361 }
2362}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002363
2364/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002365 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2366 */
2367 static int
2368numbered_function(char_u *name)
2369{
2370 return isdigit(*name)
2371 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2372}
2373
2374/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002375 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002376 * 1. ordinary names, function defined with :function or :def;
2377 * can start with "<SNR>123_" literally or with K_SPECIAL.
2378 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002379 * For the first we only count the name stored in func_hashtab as a reference,
2380 * using function() does not count as a reference, because the function is
2381 * looked up by name.
2382 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002383 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002384func_name_refcount(char_u *name)
2385{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002386 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002387}
2388
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389/*
2390 * Unreference "fc": decrement the reference count and free it when it
2391 * becomes zero. "fp" is detached from "fc".
2392 * When "force" is TRUE we are exiting.
2393 */
2394 static void
2395funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2396{
2397 funccall_T **pfc;
2398 int i;
2399
2400 if (fc == NULL)
2401 return;
2402
2403 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002404 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2405 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2406 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2407 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408 {
2409 if (fc == *pfc)
2410 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002411 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 free_funccal_contents(fc);
2413 return;
2414 }
2415 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002416 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2417 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2418 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419}
2420
2421/*
2422 * Remove the function from the function hashtable. If the function was
2423 * deleted while it still has references this was already done.
2424 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2425 */
2426 static int
2427func_remove(ufunc_T *fp)
2428{
2429 hashitem_T *hi;
2430
2431 // Return if it was already virtually deleted.
2432 if (fp->uf_flags & FC_DEAD)
2433 return FALSE;
2434
2435 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002436 if (HASHITEM_EMPTY(hi))
2437 return FALSE;
2438
2439 // When there is a def-function index do not actually remove the
2440 // function, so we can find the index when defining the function again.
2441 // Do remove it when it's a copy.
2442 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002444 fp->uf_flags |= FC_DEAD;
2445 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002447 hash_remove(&func_hashtab, hi, "remove function");
2448 fp->uf_flags |= FC_DELETED;
2449 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450}
2451
2452 static void
2453func_clear_items(ufunc_T *fp)
2454{
2455 ga_clear_strings(&(fp->uf_args));
2456 ga_clear_strings(&(fp->uf_def_args));
2457 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002459 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002460 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002461 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002462
2463 // Increment the refcount of this function to avoid it being freed
2464 // recursively when the partial is freed.
2465 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002466 partial_unref(fp->uf_partial);
2467 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002468 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002469
2470#ifdef FEAT_LUA
2471 if (fp->uf_cb_free != NULL)
2472 {
2473 fp->uf_cb_free(fp->uf_cb_state);
2474 fp->uf_cb_free = NULL;
2475 }
2476
2477 fp->uf_cb_state = NULL;
2478 fp->uf_cb = NULL;
2479#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480#ifdef FEAT_PROFILE
2481 VIM_CLEAR(fp->uf_tml_count);
2482 VIM_CLEAR(fp->uf_tml_total);
2483 VIM_CLEAR(fp->uf_tml_self);
2484#endif
2485}
2486
2487/*
2488 * Free all things that a function contains. Does not free the function
2489 * itself, use func_free() for that.
2490 * When "force" is TRUE we are exiting.
2491 */
2492 static void
2493func_clear(ufunc_T *fp, int force)
2494{
2495 if (fp->uf_cleared)
2496 return;
2497 fp->uf_cleared = TRUE;
2498
2499 // clear this function
2500 func_clear_items(fp);
2501 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002502 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503}
2504
2505/*
2506 * Free a function and remove it from the list of functions. Does not free
2507 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002508 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002509 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002511 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002512func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513{
2514 // Only remove it when not done already, otherwise we would remove a newer
2515 // version of the function with the same name.
2516 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2517 func_remove(fp);
2518
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002519 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002520 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002521 if (fp->uf_dfunc_idx > 0)
2522 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002523 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002525 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002526 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002527 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528}
2529
2530/*
2531 * Free all things that a function contains and free the function itself.
2532 * When "force" is TRUE we are exiting.
2533 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002534 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535func_clear_free(ufunc_T *fp, int force)
2536{
2537 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002538 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2539 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002540 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002541 else
2542 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543}
2544
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002545/*
2546 * Copy already defined function "lambda" to a new function with name "global".
2547 * This is for when a compiled function defines a global function.
2548 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002549 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002550copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002551 char_u *lambda,
2552 char_u *global,
2553 loopvarinfo_T *loopvarinfo,
2554 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002555{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002556 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002557 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002558
2559 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002560 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002561 semsg(_(e_lambda_function_not_found_str), lambda);
2562 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002563 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002564
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002565 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002566 if (fp != NULL)
2567 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002568 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002569 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002570 return FAIL;
2571 }
2572
Bram Moolenaare01e5212023-01-08 20:31:18 +00002573 fp = alloc_ufunc(global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002574 if (fp == NULL)
2575 return FAIL;
2576
2577 fp->uf_varargs = ufunc->uf_varargs;
2578 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2579 fp->uf_def_status = ufunc->uf_def_status;
2580 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2581 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2582 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2583 == FAIL
2584 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2585 goto failed;
2586
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002587 if (ufunc->uf_arg_types != NULL)
2588 {
2589 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2590 if (fp->uf_arg_types == NULL)
2591 goto failed;
2592 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2593 sizeof(type_T *) * fp->uf_args.ga_len);
2594 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002595 if (ufunc->uf_va_name != NULL)
2596 {
2597 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2598 if (fp->uf_va_name == NULL)
2599 goto failed;
2600 }
2601 fp->uf_ret_type = ufunc->uf_ret_type;
2602
2603 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002604
2605 fp->uf_name_exp = NULL;
2606 set_ufunc_name(fp, global);
2607
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002608 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002609
2610 // the referenced dfunc_T is now used one more time
2611 link_def_function(fp);
2612
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002613 // Create a partial to store the context of the function where it was
2614 // instantiated. Only needs to be done once. Do this on the original
2615 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002616 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2617 {
2618 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2619
2620 if (pt == NULL)
2621 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002622 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002623 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002624 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002625 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002626 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002627 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002628 }
2629
2630 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002631
2632failed:
2633 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002634 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002635}
2636
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002637static int funcdepth = 0;
2638
2639/*
2640 * Increment the function call depth count.
2641 * Return FAIL when going over 'maxfuncdepth'.
2642 * Otherwise return OK, must call funcdepth_decrement() later!
2643 */
2644 int
2645funcdepth_increment(void)
2646{
2647 if (funcdepth >= p_mfd)
2648 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002649 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002650 return FAIL;
2651 }
2652 ++funcdepth;
2653 return OK;
2654}
2655
2656 void
2657funcdepth_decrement(void)
2658{
2659 --funcdepth;
2660}
2661
2662/*
2663 * Get the current function call depth.
2664 */
2665 int
2666funcdepth_get(void)
2667{
2668 return funcdepth;
2669}
2670
2671/*
2672 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002673 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002674 * times as funcdepth_increment().
2675 */
2676 void
2677funcdepth_restore(int depth)
2678{
2679 funcdepth = depth;
2680}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002681
2682/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002683 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2684 * "rettv".
2685 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2686 * Returns NULL when allocation fails.
2687 */
2688 funccall_T *
2689create_funccal(ufunc_T *fp, typval_T *rettv)
2690{
2691 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2692
2693 if (fc == NULL)
2694 return NULL;
2695 fc->fc_caller = current_funccal;
2696 current_funccal = fc;
2697 fc->fc_func = fp;
2698 func_ptr_ref(fp);
2699 fc->fc_rettv = rettv;
2700 return fc;
2701}
2702
2703/*
2704 * To be called when returning from a compiled function; restores
2705 * current_funccal.
2706 */
2707 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002708remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002709{
2710 funccall_T *fc = current_funccal;
2711
2712 current_funccal = fc->fc_caller;
2713 free_funccal(fc);
2714}
2715
2716/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002717 * Call a user function.
2718 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002719 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002720call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002721 ufunc_T *fp, // pointer to function
2722 int argcount, // nr of args
2723 typval_T *argvars, // arguments
2724 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002725 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002726 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002727{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002728 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002729 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002730 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002731 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002732 funccall_T *fc;
2733 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002734 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002735 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002736 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002737 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002738 int i;
2739 int ai;
2740 int islambda = FALSE;
2741 char_u numbuf[NUMBUFLEN];
2742 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002743 typval_T *tv_to_free[MAX_FUNC_ARGS];
2744 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002745#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002746 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002747#endif
ichizok7e5fe382023-04-15 13:17:50 +01002748 ESTACK_CHECK_DECLARATION;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002749
Bram Moolenaarb2049902021-01-24 12:53:53 +01002750#ifdef FEAT_PROFILE
2751 CLEAR_FIELD(profile_info);
2752#endif
2753
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002754 // If depth of calling is getting too high, don't execute the function.
2755 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002756 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002757 rettv->v_type = VAR_NUMBER;
2758 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002759 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002760 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002761
Bram Moolenaare38eab22019-12-05 21:50:01 +01002762 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002763
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002764 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002765 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00002766 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002767 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002768 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002769 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2770 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002771 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002772 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002773
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002774 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002776#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002777 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002778#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002779 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002780#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002781 if (do_profiling == PROF_YES)
2782 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002783#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002784 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002785 if (call_def_function(fp, argcount, argvars, 0,
2786 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
2787 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002788 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002789#ifdef FEAT_PROFILE
2790 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002791 || (caller != NULL && caller->uf_profiling)))
2792 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002793#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002794 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002795 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002796 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797 }
2798
Bram Moolenaar38453522021-11-28 22:00:12 +00002799 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002800
2801 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002802 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2803 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2804 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002805 */
2806 /*
2807 * Init l: variables.
2808 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002809 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002810 if (selfdict != NULL)
2811 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002812 // Set l:self to "selfdict". Use "name" to avoid a warning from
2813 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002814 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002815 name = v->di_key;
2816 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002817 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002818 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002819 v->di_tv.v_type = VAR_DICT;
2820 v->di_tv.v_lock = 0;
2821 v->di_tv.vval.v_dict = selfdict;
2822 ++selfdict->dv_refcount;
2823 }
2824
2825 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002826 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002827 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002828 * Set a:000 to a list with room for the "..." arguments.
2829 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002830 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002831 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002832 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002833 (varnumber_T)(argcount >= fp->uf_args.ga_len
2834 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002835 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002836 if ((fp->uf_flags & FC_NOARGS) == 0)
2837 {
2838 // Use "name" to avoid a warning from some compiler that checks the
2839 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002840 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002841 name = v->di_key;
2842 STRCPY(name, "000");
2843 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002844 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002845 v->di_tv.v_type = VAR_LIST;
2846 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002847 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002848 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002849 CLEAR_FIELD(fc->fc_l_varlist);
2850 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2851 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002852
2853 /*
2854 * Set a:firstline to "firstline" and a:lastline to "lastline".
2855 * Set a:name to named arguments.
2856 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002857 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002858 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002859 if ((fp->uf_flags & FC_NOARGS) == 0)
2860 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002861 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2862 "firstline", (varnumber_T)funcexe->fe_firstline);
2863 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2864 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002865 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002866 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002867 {
2868 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002869 typval_T def_rettv;
2870 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002871
2872 ai = i - fp->uf_args.ga_len;
2873 if (ai < 0)
2874 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002875 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002876 name = FUNCARG(fp, i);
2877 if (islambda)
2878 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002879
2880 // evaluate named argument default expression
2881 isdefault = ai + fp->uf_def_args.ga_len >= 0
2882 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2883 && argvars[i].vval.v_number == VVAL_NONE));
2884 if (isdefault)
2885 {
2886 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002887
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002888 def_rettv.v_type = VAR_NUMBER;
2889 def_rettv.vval.v_number = -1;
2890
2891 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2892 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002893 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002894 {
2895 default_arg_err = 1;
2896 break;
2897 }
2898 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002899 }
2900 else
2901 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002902 if ((fp->uf_flags & FC_NOARGS) != 0)
2903 // Bail out if no a: arguments used (in lambda).
2904 break;
2905
Bram Moolenaare38eab22019-12-05 21:50:01 +01002906 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002907 sprintf((char *)numbuf, "%d", ai + 1);
2908 name = numbuf;
2909 }
2910 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2911 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002912 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002913 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002914 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002915 }
2916 else
2917 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002918 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002919 if (v == NULL)
2920 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002921 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002922 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002923
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002924 // Note: the values are copied directly to avoid alloc/free.
2925 // "argvars" must have VAR_FIXED for v_lock.
2926 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002927 v->di_tv.v_lock = VAR_FIXED;
2928
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002929 if (isdefault)
2930 // Need to free this later, no matter where it's stored.
2931 tv_to_free[tv_to_free_len++] = &v->di_tv;
2932
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002933 if (addlocal)
2934 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002935 // Named arguments should be accessed without the "a:" prefix in
2936 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002937 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002938 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002939 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002940 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002941 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002942
2943 if (ai >= 0 && ai < MAX_FUNC_ARGS)
2944 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002945 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002946
2947 li->li_tv = argvars[i];
2948 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002949 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002950 }
2951 }
2952
Bram Moolenaare38eab22019-12-05 21:50:01 +01002953 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002954 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02002955
2956 if (fp->uf_flags & FC_SANDBOX)
2957 {
2958 using_sandbox = TRUE;
2959 ++sandbox;
2960 }
2961
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002962 estack_push_ufunc(fp, 1);
ichizok7e5fe382023-04-15 13:17:50 +01002963 ESTACK_CHECK_SETUP;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002964 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002965 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002966 ++no_wait_return;
2967 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002968
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002969 smsg(_("calling %s"), SOURCING_NAME);
2970 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002971 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002972 char_u buf[MSG_BUF_LEN];
2973 char_u numbuf2[NUMBUFLEN];
2974 char_u *tofree;
2975 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002976
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002977 msg_puts("(");
2978 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002979 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002980 if (i > 0)
2981 msg_puts(", ");
2982 if (argvars[i].v_type == VAR_NUMBER)
2983 msg_outnum((long)argvars[i].vval.v_number);
2984 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002985 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002986 // Do not want errors such as E724 here.
2987 ++emsg_off;
2988 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
2989 --emsg_off;
2990 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002991 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002992 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002993 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002994 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2995 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002996 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002997 msg_puts((char *)s);
2998 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002999 }
3000 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003001 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003002 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003003 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003004 msg_puts("\n"); // don't overwrite this either
3005
3006 verbose_leave_scroll();
3007 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003008 }
3009#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003010 if (do_profiling == PROF_YES)
3011 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003012 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003013#endif
3014
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003015 // "legacy" does not apply to commands in the function
3016 sticky_cmdmod_flags = 0;
3017
Bram Moolenaar98aff652022-09-06 21:02:35 +01003018 // If called from a compiled :def function the execution context must be
3019 // hidden, any deferred functions need to be added to the function being
3020 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003021 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003022
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003023 save_current_sctx = current_sctx;
3024 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003025 save_did_emsg = did_emsg;
3026 did_emsg = FALSE;
3027
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003028 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003029 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003030 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003031 retval = FCERR_FAILED;
3032 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003033 else if (islambda)
3034 {
3035 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3036
3037 // A Lambda always has the command "return {expr}". It is much faster
3038 // to evaluate {expr} directly.
3039 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003040 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003041 --ex_nesting_level;
3042 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003043 else
3044 // call do_cmdline() to execute the lines
3045 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003046 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3047
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003048 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003049 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003050
Bram Moolenaar79cdf022023-05-20 14:07:00 +01003051 if (RedrawingDisabled > 0)
3052 --RedrawingDisabled;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003053
Bram Moolenaare38eab22019-12-05 21:50:01 +01003054 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003055 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3056 {
3057 clear_tv(rettv);
3058 rettv->v_type = VAR_NUMBER;
3059 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003060
3061 // In corner cases returning a "failed" value is not backwards
3062 // compatible. Only do this for Vim9 script.
3063 if (in_vim9script())
3064 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003065 }
3066
3067#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003068 if (do_profiling == PROF_YES)
3069 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003070 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003071
3072 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3073 profile_may_end_func(&profile_info, fp, caller);
3074 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003075#endif
3076
Bram Moolenaare38eab22019-12-05 21:50:01 +01003077 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003078 if (p_verbose >= 12)
3079 {
3080 ++no_wait_return;
3081 verbose_enter_scroll();
3082
3083 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003084 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003085 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003086 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003087 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003088 else
3089 {
3090 char_u buf[MSG_BUF_LEN];
3091 char_u numbuf2[NUMBUFLEN];
3092 char_u *tofree;
3093 char_u *s;
3094
Bram Moolenaare38eab22019-12-05 21:50:01 +01003095 // The value may be very long. Skip the middle part, so that we
3096 // have some idea how it starts and ends. smsg() would always
3097 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003098 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003099 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003100 --emsg_off;
3101 if (s != NULL)
3102 {
3103 if (vim_strsize(s) > MSG_BUF_CLEN)
3104 {
3105 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3106 s = buf;
3107 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003108 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003109 vim_free(tofree);
3110 }
3111 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003112 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003113
3114 verbose_leave_scroll();
3115 --no_wait_return;
3116 }
3117
ichizok7e5fe382023-04-15 13:17:50 +01003118 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003119 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003120 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003121 restore_current_ectx(save_current_ectx);
3122
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003123#ifdef FEAT_PROFILE
3124 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003125 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003126#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003127 if (using_sandbox)
3128 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003129 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003130
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003131 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003132 {
3133 ++no_wait_return;
3134 verbose_enter_scroll();
3135
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003136 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003137 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003138
3139 verbose_leave_scroll();
3140 --no_wait_return;
3141 }
3142
3143 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003144 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003145 for (i = 0; i < tv_to_free_len; ++i)
3146 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003147 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003148
3149 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003150}
3151
3152/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003153 * Check the argument count for user function "fp".
3154 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3155 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003156 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003157check_user_func_argcount(ufunc_T *fp, int argcount)
3158{
3159 int regular_args = fp->uf_args.ga_len;
3160
3161 if (argcount < regular_args - fp->uf_def_args.ga_len)
3162 return FCERR_TOOFEW;
3163 else if (!has_varargs(fp) && argcount > regular_args)
3164 return FCERR_TOOMANY;
3165 return FCERR_UNKNOWN;
3166}
3167
3168/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003170 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003171 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172call_user_func_check(
3173 ufunc_T *fp,
3174 int argcount,
3175 typval_T *argvars,
3176 typval_T *rettv,
3177 funcexe_T *funcexe,
3178 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003179{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003180 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003181
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003182#ifdef FEAT_LUA
3183 if (fp->uf_flags & FC_CFUNC)
3184 {
3185 cfunc_T cb = fp->uf_cb;
3186
3187 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3188 }
3189#endif
3190
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003191 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3192 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003193 error = check_user_func_argcount(fp, argcount);
3194 if (error != FCERR_UNKNOWN)
3195 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003196
Bram Moolenaar50824712020-12-20 21:10:17 +01003197 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003198 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003200 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003202 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 int did_save_redo = FALSE;
3204 save_redo_T save_redo;
3205
3206 /*
3207 * Call the user function.
3208 * Save and restore search patterns, script variables and
3209 * redo buffer.
3210 */
3211 save_search_patterns();
3212 if (!ins_compl_active())
3213 {
3214 saveRedobuff(&save_redo);
3215 did_save_redo = TRUE;
3216 }
3217 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003218 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003219 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3221 // Function was unreferenced while being used, free it now.
3222 func_clear_free(fp, FALSE);
3223 if (did_save_redo)
3224 restoreRedobuff(&save_redo);
3225 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003226 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003229}
3230
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003231static funccal_entry_T *funccal_stack = NULL;
3232
3233/*
3234 * Save the current function call pointer, and set it to NULL.
3235 * Used when executing autocommands and for ":source".
3236 */
3237 void
3238save_funccal(funccal_entry_T *entry)
3239{
3240 entry->top_funccal = current_funccal;
3241 entry->next = funccal_stack;
3242 funccal_stack = entry;
3243 current_funccal = NULL;
3244}
3245
3246 void
3247restore_funccal(void)
3248{
3249 if (funccal_stack == NULL)
Bram Moolenaar097c5372023-05-24 21:02:24 +01003250 internal_error("restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003251 else
3252 {
3253 current_funccal = funccal_stack->top_funccal;
3254 funccal_stack = funccal_stack->next;
3255 }
3256}
3257
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003258 funccall_T *
3259get_current_funccal(void)
3260{
3261 return current_funccal;
3262}
3263
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003264/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003265 * Return TRUE when currently at the script level:
3266 * - not in a function
3267 * - not executing an autocommand
3268 * Note that when an autocommand sources a script the result is FALSE;
3269 */
3270 int
3271at_script_level(void)
3272{
3273 return current_funccal == NULL && autocmd_match == NULL;
3274}
3275
3276/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003277 * Mark all functions of script "sid" as deleted.
3278 */
3279 void
3280delete_script_functions(int sid)
3281{
3282 hashitem_T *hi;
3283 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003284 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003285 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003286 size_t len;
3287
3288 buf[0] = K_SPECIAL;
3289 buf[1] = KS_EXTRA;
3290 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003291 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003292 len = STRLEN(buf);
3293
Bram Moolenaarce658352020-07-31 23:47:12 +02003294 while (todo > 0)
3295 {
3296 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003297 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003298 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003299 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003300 fp = HI2UF(hi);
3301 if (STRNCMP(fp->uf_name, buf, len) == 0)
3302 {
3303 int changed = func_hashtab.ht_changed;
3304
3305 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003306
3307 if (fp->uf_calls > 0)
3308 {
3309 // Function is executing, don't free it but do remove
3310 // it from the hashtable.
3311 if (func_remove(fp))
3312 fp->uf_refcount--;
3313 }
3314 else
3315 {
3316 func_clear(fp, TRUE);
3317 // When clearing a function another function can be
3318 // cleared as a side effect. When that happens start
3319 // over.
3320 if (changed != func_hashtab.ht_changed)
3321 break;
3322 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003323 }
3324 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003325 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003326 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003327}
3328
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003329#if defined(EXITFREE) || defined(PROTO)
3330 void
3331free_all_functions(void)
3332{
3333 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003334 ufunc_T *fp;
3335 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003336 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003337 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003338
Bram Moolenaare38eab22019-12-05 21:50:01 +01003339 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003340 while (current_funccal != NULL)
3341 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003342 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003343 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003344 if (current_funccal == NULL && funccal_stack != NULL)
3345 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003346 }
3347
Bram Moolenaare38eab22019-12-05 21:50:01 +01003348 // First clear what the functions contain. Since this may lower the
3349 // reference count of a function, it may also free a function and change
3350 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003351 while (todo > 0)
3352 {
3353 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003354 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003355 if (!HASHITEM_EMPTY(hi))
3356 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 // clear the def function index now
3358 fp = HI2UF(hi);
3359 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003360 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361
Bram Moolenaare38eab22019-12-05 21:50:01 +01003362 // Only free functions that are not refcounted, those are
3363 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003364 if (func_name_refcount(fp->uf_name))
3365 ++skipped;
3366 else
3367 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003368 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003369 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003370 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003371 {
3372 skipped = 0;
3373 break;
3374 }
3375 }
3376 --todo;
3377 }
3378 }
3379
Bram Moolenaare38eab22019-12-05 21:50:01 +01003380 // Now actually free the functions. Need to start all over every time,
3381 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003382 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003383 while (func_hashtab.ht_used > skipped)
3384 {
3385 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003386 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003387 if (!HASHITEM_EMPTY(hi))
3388 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003389 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003390 // Only free functions that are not refcounted, those are
3391 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003392 fp = HI2UF(hi);
3393 if (func_name_refcount(fp->uf_name))
3394 ++skipped;
3395 else
3396 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003397 if (func_free(fp, FALSE) == OK)
3398 {
3399 skipped = 0;
3400 break;
3401 }
3402 // did not actually free it
3403 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003404 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003405 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003406 }
3407 if (skipped == 0)
3408 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409
3410 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003411}
3412#endif
3413
3414/*
3415 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003416 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3417 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003418 * "len" is the length of "name", or -1 for NUL terminated.
3419 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003421builtin_function(char_u *name, int len)
3422{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003423 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003424
Bram Moolenaara26b9702020-04-18 19:53:28 +02003425 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003426 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003427 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3428 {
3429 if (name[i] == AUTOLOAD_CHAR)
3430 return FALSE;
3431 if (!eval_isnamec(name[i]))
3432 {
3433 // "name.something" is not a builtin function
3434 if (name[i] == '.')
3435 return FALSE;
3436 break;
3437 }
3438 }
3439 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003440}
3441
3442 int
3443func_call(
3444 char_u *name,
3445 typval_T *args,
3446 partial_T *partial,
3447 dict_T *selfdict,
3448 typval_T *rettv)
3449{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003450 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003451 listitem_T *item;
3452 typval_T argv[MAX_FUNC_ARGS + 1];
3453 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003454 int r = 0;
3455
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003456 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003457 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003458 {
3459 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3460 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003461 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003462 break;
3463 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003464 // Make a copy of each argument. This is needed to be able to set
3465 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003466 copy_tv(&item->li_tv, &argv[argc++]);
3467 }
3468
3469 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003470 {
3471 funcexe_T funcexe;
3472
Bram Moolenaara80faa82020-04-12 19:37:17 +02003473 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003474 funcexe.fe_firstline = curwin->w_cursor.lnum;
3475 funcexe.fe_lastline = curwin->w_cursor.lnum;
3476 funcexe.fe_evaluate = TRUE;
3477 funcexe.fe_partial = partial;
3478 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003479 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3480 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003481
Bram Moolenaare38eab22019-12-05 21:50:01 +01003482 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003483 while (argc > 0)
3484 clear_tv(&argv[--argc]);
3485
3486 return r;
3487}
3488
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003489static int callback_depth = 0;
3490
3491 int
3492get_callback_depth(void)
3493{
3494 return callback_depth;
3495}
3496
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003497/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003498 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003499 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003500 */
3501 int
3502call_callback(
3503 callback_T *callback,
3504 int len, // length of "name" or -1 to use strlen()
3505 typval_T *rettv, // return value goes here
3506 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003507 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003508 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003509{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003510 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003511 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003512
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003513 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3514 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02003515 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003516 funcexe.fe_evaluate = TRUE;
3517 funcexe.fe_partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003518 ++callback_depth;
3519 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3520 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003521
3522 // When a :def function was called that uses :try an error would be turned
3523 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003524 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003525 {
3526 need_rethrow = FALSE;
3527 handle_did_throw();
3528 }
3529
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003530 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003531}
3532
3533/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003534 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003535 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003536 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3537 */
3538 varnumber_T
3539call_callback_retnr(
3540 callback_T *callback,
3541 int argcount, // number of "argvars"
3542 typval_T *argvars) // vars for arguments, must have "argcount"
3543 // PLUS ONE elements!
3544{
3545 typval_T rettv;
3546 varnumber_T retval;
3547
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003548 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003549 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003550
3551 retval = tv_get_number_chk(&rettv, NULL);
3552 clear_tv(&rettv);
3553 return retval;
3554}
3555
3556/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557 * Give an error message for the result of a function.
3558 * Nothing if "error" is FCERR_NONE.
3559 */
3560 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003561user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562{
3563 switch (error)
3564 {
3565 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003566 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003567 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003568 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003569 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570 break;
3571 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003572 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 break;
3574 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003575 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 break;
3577 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003578 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579 break;
3580 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003581 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582 break;
3583 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003584 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 break;
3586 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003587 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3588 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003590 case FCERR_OTHER:
3591 case FCERR_FAILED:
3592 // assume the error message was already given
3593 break;
3594 case FCERR_NONE:
3595 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 }
3597}
3598
3599/*
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003600 * Check the argument types "argvars[argcount]" for "name" using the
3601 * information in "funcexe". When "base_included" then "funcexe->fe_basetv"
3602 * is already included in "argvars[]".
3603 * Will do nothing if "funcexe->fe_check_type" is NULL or
3604 * "funcexe->fe_evaluate" is FALSE;
3605 * Returns an FCERR_ value.
3606 */
3607 static funcerror_T
3608may_check_argument_types(
3609 funcexe_T *funcexe,
3610 typval_T *argvars,
3611 int argcount,
3612 int base_included,
3613 char_u *name)
3614{
3615 if (funcexe->fe_check_type != NULL && funcexe->fe_evaluate)
3616 {
3617 // Check that the argument types are OK for the types of the funcref.
3618 if (check_argument_types(funcexe->fe_check_type,
3619 argvars, argcount,
3620 base_included ? NULL : funcexe->fe_basetv,
3621 name) == FAIL)
3622 return FCERR_OTHER;
3623 }
3624 return FCERR_NONE;
3625}
3626
3627/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003628 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003629 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003630 * Return FAIL when the function can't be called, OK otherwise.
3631 * Also returns OK when an error was encountered while executing the function.
3632 */
3633 int
3634call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003635 char_u *funcname, // name of the function
3636 int len, // length of "name" or -1 to use strlen()
3637 typval_T *rettv, // return value goes here
3638 int argcount_in, // number of "argvars"
3639 typval_T *argvars_in, // vars for arguments, must have "argcount"
3640 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003641 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003642{
3643 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003644 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003645 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003646 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003647 char_u fname_buf[FLEN_FIXED + 1];
3648 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003649 char_u *fname = NULL;
3650 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003651 int argcount = argcount_in;
3652 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003653 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003654 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003655 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003656 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003657 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003658 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003659 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003660 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003661
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003662 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3663 // even when call_func() returns FAIL.
3664 rettv->v_type = VAR_UNKNOWN;
3665
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003666 if (partial != NULL)
3667 fp = partial->pt_func;
3668 if (fp == NULL)
3669 {
3670 // Make a copy of the name, if it comes from a funcref variable it
3671 // could be changed or deleted in the called function.
3672 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3673 if (name == NULL)
3674 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003675
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003676 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3677 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003678
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003679 if (funcexe->fe_doesrange != NULL)
3680 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003681
3682 if (partial != NULL)
3683 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003684 // When the function has a partial with a dict and there is a dict
3685 // argument, use the dict argument. That is backwards compatible.
3686 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003687 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003688 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003689 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003690 {
3691 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003692 {
3693 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3694 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003695 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003696 goto theend;
3697 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003698 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003699 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003700 for (i = 0; i < argcount_in; ++i)
3701 argv[i + argv_clear] = argvars_in[i];
3702 argvars = argv;
3703 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003704
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003705 if (funcexe->fe_check_type != NULL
3706 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003707 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003708 // Now funcexe->fe_check_type is missing the added arguments,
3709 // make a copy of the type with the correction.
3710 check_type = *funcexe->fe_check_type;
3711 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003712 check_type.tt_args = check_type_args;
3713 CLEAR_FIELD(check_type_args);
3714 for (i = 0; i < check_type.tt_argcount; ++i)
3715 check_type_args[i + partial->pt_argc] =
3716 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003717 check_type.tt_argcount += partial->pt_argc;
3718 check_type.tt_min_argcount += partial->pt_argc;
3719 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003720 }
3721 }
3722
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003723 if (error == FCERR_NONE)
3724 // check the argument types if possible
3725 error = may_check_argument_types(funcexe, argvars, argcount, FALSE,
3726 (name != NULL) ? name : funcname);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003727
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003728 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003729 {
3730 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003731 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003732
Bram Moolenaar333894b2020-08-01 18:53:07 +02003733 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003734 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003735 {
3736 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003737 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003738 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003739
Bram Moolenaare38eab22019-12-05 21:50:01 +01003740 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003741 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003742 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003743
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003744 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003745 {
3746 /*
3747 * User defined function.
3748 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003749 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003750 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003751 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003752 if (fp != NULL && !is_global && in_vim9script()
3753 && func_requires_g_prefix(fp))
3754 // In Vim9 script g: is required to find a global
3755 // non-autoload function.
3756 fp = NULL;
3757 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003758
Bram Moolenaare38eab22019-12-05 21:50:01 +01003759 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003760 if (fp == NULL
3761 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003762 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003763 && !aborting())
3764 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003765 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003766 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003767 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003768 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003769 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3770 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003771 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003772 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003773 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003774 if (fp == NULL)
3775 {
3776 char_u *p = untrans_function_name(rfname);
3777
3778 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003779 // Don't do this if the name starts with "s:".
3780 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003781 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003782 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003783
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003784 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003785 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003786 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003787 {
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003788 int need_arg_check = FALSE;
3789 if (funcexe->fe_check_type == NULL)
3790 {
3791 funcexe->fe_check_type = fp->uf_func_type;
3792 need_arg_check = TRUE;
3793 }
3794
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003795 if (funcexe->fe_argv_func != NULL)
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003796 {
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003797 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003798 argcount = funcexe->fe_argv_func(argcount, argvars,
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003799 argv_clear, fp);
3800 need_arg_check = TRUE;
3801 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003802
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003803 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003804 {
3805 // Method call: base->Method()
3806 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003807 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003808 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003809 argvars = argv;
3810 argv_base = 1;
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003811 need_arg_check = TRUE;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003812 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003813
Bram Moolenaar2ba51232023-05-15 16:22:38 +01003814 // Check the argument types now that the function type and all
3815 // argument values are known, if not done above.
3816 if (need_arg_check)
3817 error = may_check_argument_types(funcexe, argvars, argcount,
3818 TRUE, (name != NULL) ? name : funcname);
3819 if (error == FCERR_NONE || error == FCERR_UNKNOWN)
3820 error = call_user_func_check(fp, argcount, argvars, rettv,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003822 }
3823 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003824 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003825 {
3826 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003827 * expr->method(): Find the method name in the table, call its
3828 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003829 */
3830 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003831 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003832 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003833 else
3834 {
3835 /*
3836 * Find the function name in the table, call its implementation.
3837 */
3838 error = call_internal_func(fname, argcount, argvars, rettv);
3839 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003840
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003841 /*
3842 * The function call (or "FuncUndefined" autocommand sequence) might
3843 * have been aborted by an error, an interrupt, or an explicitly thrown
3844 * exception that has not been caught so far. This situation can be
3845 * tested for by calling aborting(). For an error in an internal
3846 * function or for the "E132" error in call_user_func(), however, the
3847 * throw point at which the "force_abort" flag (temporarily reset by
3848 * emsg()) is normally updated has not been reached yet. We need to
3849 * update that flag first to make aborting() reliable.
3850 */
3851 update_force_abort();
3852 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003853 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003854 ret = OK;
3855
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003856theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003857 /*
3858 * Report an error unless the argument evaluation or function call has been
3859 * cancelled due to an aborting error, an interrupt, or an exception.
3860 */
3861 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003862 user_func_error(error, (name != NULL) ? name : funcname,
3863 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003864
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003865 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003866 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003867 clear_tv(&argv[--argv_clear + argv_base]);
3868
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003869 vim_free(tofree);
3870 vim_free(name);
3871
3872 return ret;
3873}
3874
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003875/*
3876 * Call a function without arguments, partial or dict.
3877 * This is like call_func() when the call is only "FuncName()".
3878 * To be used by "expr" options.
3879 * Returns NOTDONE when the function could not be found.
3880 */
3881 int
3882call_simple_func(
3883 char_u *funcname, // name of the function
3884 int len, // length of "name" or -1 to use strlen()
3885 typval_T *rettv) // return value goes here
3886{
3887 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003888 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003889 char_u fname_buf[FLEN_FIXED + 1];
3890 char_u *tofree = NULL;
3891 char_u *name;
3892 char_u *fname;
3893 char_u *rfname;
3894 int is_global = FALSE;
3895 ufunc_T *fp;
3896
3897 rettv->v_type = VAR_NUMBER; // default rettv is number zero
3898 rettv->vval.v_number = 0;
3899
3900 // Make a copy of the name, an option can be changed in the function.
3901 name = vim_strnsave(funcname, len);
3902 if (name == NULL)
3903 return ret;
3904
3905 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3906
3907 // Skip "g:" before a function name.
3908 if (fname[0] == 'g' && fname[1] == ':')
3909 {
3910 is_global = TRUE;
3911 rfname = fname + 2;
3912 }
3913 else
3914 rfname = fname;
3915 fp = find_func(rfname, is_global);
3916 if (fp != NULL && !is_global && in_vim9script()
3917 && func_requires_g_prefix(fp))
3918 // In Vim9 script g: is required to find a global non-autoload
3919 // function.
3920 fp = NULL;
3921 if (fp == NULL)
3922 ret = NOTDONE;
3923 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
3924 error = FCERR_DELETED;
3925 else if (fp != NULL)
3926 {
3927 typval_T argvars[1];
3928 funcexe_T funcexe;
3929
3930 argvars[0].v_type = VAR_UNKNOWN;
3931 CLEAR_FIELD(funcexe);
3932 funcexe.fe_evaluate = TRUE;
3933
3934 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
3935 if (error == FCERR_NONE)
3936 ret = OK;
3937 }
3938
3939 user_func_error(error, name, FALSE);
3940 vim_free(tofree);
3941 vim_free(name);
3942
3943 return ret;
3944}
3945
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003946 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947printable_func_name(ufunc_T *fp)
3948{
3949 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
3950}
3951
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003952/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003953 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
3954 * TRUE. Otherwise return FALSE.
3955 */
3956 static int
3957function_list_modified(int prev_ht_changed)
3958{
3959 if (prev_ht_changed != func_hashtab.ht_changed)
3960 {
3961 emsg(_(e_function_list_was_modified));
3962 return TRUE;
3963 }
3964 return FALSE;
3965}
3966
3967/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003968 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003969 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003970 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003971list_func_head(ufunc_T *fp, int indent)
3972{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003973 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003974 int j;
3975
3976 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003977
3978 // a timer at the more prompt may have deleted the function
3979 if (function_list_modified(prev_ht_changed))
3980 return FAIL;
3981
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003982 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003983 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003984 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003985 msg_puts("def ");
3986 else
3987 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003989 msg_putchar('(');
3990 for (j = 0; j < fp->uf_args.ga_len; ++j)
3991 {
3992 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003993 msg_puts(", ");
3994 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 if (fp->uf_arg_types != NULL)
3996 {
3997 char *tofree;
3998
3999 msg_puts(": ");
4000 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
4001 vim_free(tofree);
4002 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004003 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
4004 {
4005 msg_puts(" = ");
4006 msg_puts(((char **)(fp->uf_def_args.ga_data))
4007 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
4008 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004009 }
4010 if (fp->uf_varargs)
4011 {
4012 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004013 msg_puts(", ");
4014 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004015 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 if (fp->uf_va_name != NULL)
4017 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01004018 if (!fp->uf_varargs)
4019 {
4020 if (j)
4021 msg_puts(", ");
4022 msg_puts("...");
4023 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02004025 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 {
4027 char *tofree;
4028
4029 msg_puts(": ");
4030 msg_puts(type_name(fp->uf_va_type, &tofree));
4031 vim_free(tofree);
4032 }
4033 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004034 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004035
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004036 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004037 {
4038 if (fp->uf_ret_type != &t_void)
4039 {
4040 char *tofree;
4041
4042 msg_puts(": ");
4043 msg_puts(type_name(fp->uf_ret_type, &tofree));
4044 vim_free(tofree);
4045 }
4046 }
4047 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004048 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004049 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004050 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004051 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004052 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004053 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004054 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004055 msg_clr_eos();
4056 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004057 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004058
4059 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004060}
4061
4062/*
4063 * Get a function name, translating "<SID>" and "<SNR>".
4064 * Also handles a Funcref in a List or Dictionary.
4065 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004066 * Set "*is_global" to TRUE when the function must be global, unless
4067 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004068 * flags:
4069 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004070 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004071 * TFN_QUIET: be quiet
4072 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004073 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004074 * Advances "pp" to just after the function name (if no error).
4075 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004076 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004077trans_function_name(
4078 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004079 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004080 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004081 int flags)
4082{
4083 return trans_function_name_ext(pp, is_global, skip, flags,
4084 NULL, NULL, NULL, NULL);
4085}
4086
4087/*
4088 * trans_function_name() with extra arguments.
4089 * "fdp", "partial", "type" and "ufunc" can be NULL.
4090 */
4091 char_u *
4092trans_function_name_ext(
4093 char_u **pp,
4094 int *is_global,
4095 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004096 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004097 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004098 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004099 type_T **type, // return: type of funcref
4100 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004101{
4102 char_u *name = NULL;
4103 char_u *start;
4104 char_u *end;
4105 int lead;
4106 char_u sid_buf[20];
4107 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004109 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004110 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004111 int vim9script = in_vim9script();
4112 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004113
4114 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004115 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004116 start = *pp;
4117
Bram Moolenaare38eab22019-12-05 21:50:01 +01004118 // Check for hard coded <SNR>: already translated function ID (from a user
4119 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004120 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4121 && (*pp)[2] == (int)KE_SNR)
4122 {
4123 *pp += 3;
4124 len = get_id_len(pp) + 3;
4125 return vim_strnsave(start, len);
4126 }
4127
Bram Moolenaare38eab22019-12-05 21:50:01 +01004128 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4129 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004130 lead = eval_fname_script(start);
4131 if (lead > 2)
4132 start += lead;
4133
Bram Moolenaare38eab22019-12-05 21:50:01 +01004134 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004135 end = get_lval(start, NULL, &lv, FALSE, skip,
4136 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004137 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004138 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004139 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004140 {
4141 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004142 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004143 goto theend;
4144 }
4145 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4146 {
4147 /*
4148 * Report an invalid expression in braces, unless the expression
4149 * evaluation has been cancelled due to an aborting error, an
4150 * interrupt, or an exception.
4151 */
4152 if (!aborting())
4153 {
4154 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004155 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004156 }
4157 else
4158 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4159 goto theend;
4160 }
4161
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004162 if (lv.ll_ufunc != NULL)
4163 {
4164 *ufunc = lv.ll_ufunc;
4165 name = vim_strsave(lv.ll_ufunc->uf_name);
4166 goto theend;
4167 }
4168
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004169 if (lv.ll_tv != NULL)
4170 {
4171 if (fdp != NULL)
4172 {
4173 fdp->fd_dict = lv.ll_dict;
4174 fdp->fd_newkey = lv.ll_newkey;
4175 lv.ll_newkey = NULL;
4176 fdp->fd_di = lv.ll_di;
4177 }
4178 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4179 {
4180 name = vim_strsave(lv.ll_tv->vval.v_string);
4181 *pp = end;
4182 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004183 else if (lv.ll_tv->v_type == VAR_CLASS
4184 && lv.ll_tv->vval.v_class != NULL)
4185 {
4186 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4187 *pp = end;
4188 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004189 else if (lv.ll_tv->v_type == VAR_PARTIAL
4190 && lv.ll_tv->vval.v_partial != NULL)
4191 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004192 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004193 *pp = end;
4194 if (partial != NULL)
4195 *partial = lv.ll_tv->vval.v_partial;
4196 }
4197 else
4198 {
4199 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4200 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004201 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004202 else
4203 *pp = end;
4204 name = NULL;
4205 }
4206 goto theend;
4207 }
4208
4209 if (lv.ll_name == NULL)
4210 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004211 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004212 *pp = end;
4213 goto theend;
4214 }
4215
Bram Moolenaare38eab22019-12-05 21:50:01 +01004216 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004217 if (lv.ll_exp_name != NULL)
4218 {
4219 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004220 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004221 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004222 if (name == lv.ll_exp_name)
4223 name = NULL;
4224 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004225 else if (lv.ll_sid > 0)
4226 {
4227 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4228 int cc = *lv.ll_name_end;
4229
4230 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4231 *lv.ll_name_end = NUL;
4232 if (si->sn_autoload_prefix != NULL)
4233 {
4234 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4235 }
4236 else
4237 {
4238 sid_buf[0] = K_SPECIAL;
4239 sid_buf[1] = KS_EXTRA;
4240 sid_buf[2] = (int)KE_SNR;
4241 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004242 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004243 name = concat_str(sid_buf, lv.ll_name);
4244 }
4245 *lv.ll_name_end = cc;
4246 *pp = end;
4247 goto theend;
4248 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004249 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004250 {
4251 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004252 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004253 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004254 if (name == *pp)
4255 name = NULL;
4256 }
4257 if (name != NULL)
4258 {
4259 name = vim_strsave(name);
4260 *pp = end;
4261 if (STRNCMP(name, "<SNR>", 5) == 0)
4262 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004263 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004264 name[0] = K_SPECIAL;
4265 name[1] = KS_EXTRA;
4266 name[2] = (int)KE_SNR;
4267 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4268 }
4269 goto theend;
4270 }
4271
4272 if (lv.ll_exp_name != NULL)
4273 {
4274 len = (int)STRLEN(lv.ll_exp_name);
4275 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4276 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4277 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004278 // When there was "s:" already or the name expanded to get a
4279 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004280 lv.ll_name += 2;
4281 len -= 2;
4282 lead = 2;
4283 }
4284 }
4285 else
4286 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004287 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004288 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004289 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004290 if (lv.ll_name[0] == 'g')
4291 {
4292 if (is_global != NULL)
4293 {
4294 *is_global = TRUE;
4295 }
4296 else
4297 {
4298 // dropping "g:" without setting "is_global" won't work in
4299 // Vim9script, put it back later
4300 prefix_g = TRUE;
4301 extra = 2;
4302 }
4303 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004304 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004305 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004306 len = (int)(end - lv.ll_name);
4307 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004308 if (len <= 0)
4309 {
4310 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004311 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004312 goto theend;
4313 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004314
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004315 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004316 // starts with a lower case character: dict.func(). Or when in a class.
4317 vim9_local = ASCII_ISUPPER(*start) && vim9script
4318 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004320 /*
4321 * Copy the function name to allocated memory.
4322 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4323 * Accept <SNR>123_name() outside a script.
4324 */
4325 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004326 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004327 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004328 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004329 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004330 {
Kota Kato948a3892022-08-16 16:09:59 +01004331 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4332 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004333 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004334 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004335 goto theend;
4336 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004338 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004339 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004341 || eval_fname_sid(*pp))
4342 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004344 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004345 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004346 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004347 goto theend;
4348 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004349 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004350 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 extra = 3 + (int)STRLEN(sid_buf);
4352 else
4353 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004354 }
4355 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004356 else if (!(flags & TFN_INT)
4357 && (builtin_function(lv.ll_name, len)
4358 || (vim9script && *lv.ll_name == '_'))
4359 && !((flags & TFN_IN_CLASS) && STRNCMP(lv.ll_name, "new", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004360 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004361 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004362 : e_function_name_must_start_with_capital_or_s_str),
4363 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004364 goto theend;
4365 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004366 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004367 {
4368 char_u *cp = vim_strchr(lv.ll_name, ':');
4369
4370 if (cp != NULL && cp < end)
4371 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004372 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004373 goto theend;
4374 }
4375 }
4376
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004378 if (name != NULL)
4379 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004380 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004381 {
4382 name[0] = K_SPECIAL;
4383 name[1] = KS_EXTRA;
4384 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004385 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004386 STRCPY(name + 3, sid_buf);
4387 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004388 else if (prefix_g)
4389 {
4390 name[0] = 'g';
4391 name[1] = ':';
4392 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4394 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004395 }
4396 *pp = end;
4397
4398theend:
4399 clear_lval(&lv);
4400 return name;
4401}
4402
4403/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004404 * Assuming "name" is the result of trans_function_name() and it was prefixed
4405 * to use the script-local name, return the unmodified name (points into
4406 * "name"). Otherwise return NULL.
4407 * This can be used to first search for a script-local function and fall back
4408 * to the global function if not found.
4409 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004410 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004411untrans_function_name(char_u *name)
4412{
4413 char_u *p;
4414
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004415 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004416 {
4417 p = vim_strchr(name, '_');
4418 if (p != NULL)
4419 return p + 1;
4420 }
4421 return NULL;
4422}
4423
4424/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004425 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4426 * current script ID and returns the expanded function name. The caller should
4427 * free the returned name. If not called from a script context or the function
4428 * name doesn't start with these prefixes, then returns NULL.
4429 * This doesn't check whether the script-local function exists or not.
4430 */
4431 char_u *
4432get_scriptlocal_funcname(char_u *funcname)
4433{
4434 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004435 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004436 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004437 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004438
4439 if (funcname == NULL)
4440 return NULL;
4441
4442 if (STRNCMP(funcname, "s:", 2) != 0
4443 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004444 {
4445 ufunc_T *ufunc;
4446
4447 // The function name does not have a script-local prefix. Try finding
4448 // it when in a Vim9 script and there is no "g:" prefix.
4449 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4450 return NULL;
4451 ufunc = find_func(funcname, FALSE);
4452 if (ufunc == NULL || func_is_global(ufunc)
4453 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4454 return NULL;
4455 ++p;
4456 off = 0;
4457 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004458 else
4459 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004460
4461 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4462 {
4463 emsg(_(e_using_sid_not_in_script_context));
4464 return NULL;
4465 }
4466 // Expand s: prefix into <SNR>nr_<name>
4467 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4468 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004469 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004470 if (newname == NULL)
4471 return NULL;
4472 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004473 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004474
4475 return newname;
4476}
4477
4478/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004479 * Return script-local "fname" with the 3-byte sequence replaced by
4480 * printable <SNR> in allocated memory.
4481 */
4482 char_u *
4483alloc_printable_func_name(char_u *fname)
4484{
4485 char_u *n = alloc(STRLEN(fname + 3) + 6);
4486
4487 if (n != NULL)
4488 {
4489 STRCPY(n, "<SNR>");
4490 STRCPY(n + 5, fname + 3);
4491 }
4492 return n;
4493}
4494
4495/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004496 * Call trans_function_name(), except that a lambda is returned as-is.
4497 * Returns the name in allocated memory.
4498 */
4499 char_u *
4500save_function_name(
4501 char_u **name,
4502 int *is_global,
4503 int skip,
4504 int flags,
4505 funcdict_T *fudi)
4506{
4507 char_u *p = *name;
4508 char_u *saved;
4509
4510 if (STRNCMP(p, "<lambda>", 8) == 0)
4511 {
4512 p += 8;
4513 (void)getdigits(&p);
4514 saved = vim_strnsave(*name, p - *name);
4515 if (fudi != NULL)
4516 CLEAR_POINTER(fudi);
4517 }
4518 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004519 saved = trans_function_name_ext(&p, is_global, skip,
4520 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004521 *name = p;
4522 return saved;
4523}
4524
4525/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004526 * List functions. When "regmatch" is NULL all of then.
4527 * Otherwise functions matching "regmatch".
4528 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004529 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004530list_functions(regmatch_T *regmatch)
4531{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004532 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004533 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004534 hashitem_T *hi;
4535
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004536 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004537 {
4538 if (!HASHITEM_EMPTY(hi))
4539 {
4540 ufunc_T *fp = HI2UF(hi);
4541
4542 --todo;
4543 if ((fp->uf_flags & FC_DEAD) == 0
4544 && (regmatch == NULL
4545 ? !message_filtered(fp->uf_name)
4546 && !func_name_refcount(fp->uf_name)
4547 : !isdigit(*fp->uf_name)
4548 && vim_regexec(regmatch, fp->uf_name, 0)))
4549 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004550 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004551 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004552 if (function_list_modified(prev_ht_changed))
4553 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004554 }
4555 }
4556 }
4557}
4558
4559/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004560 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004561 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4562 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004563 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004564 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4565 * With CF_INTERFACE the function is define inside an interface, only the
4566 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004567 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004568 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004569 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004570define_function(
4571 exarg_T *eap,
4572 char_u *name_arg,
4573 garray_T *lines_to_free,
Bram Moolenaar554d0312023-01-05 19:59:18 +00004574 int class_flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004575{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004576 int j;
4577 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004578 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004579 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004580 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004581 char_u *p;
4582 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004583 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004584 char_u *line_arg = NULL;
4585 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004586 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004587 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004588 garray_T newlines;
4589 int varargs = FALSE;
4590 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004591 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004592 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004593 int fp_allocated = FALSE;
4594 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004595 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004596 dictitem_T *v;
4597 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004598 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004599 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004600 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004601 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004602 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004603 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004604
4605 /*
4606 * ":function" without argument: list functions.
4607 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004608 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004609 {
4610 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004611 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004612 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004613 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004614 }
4615
4616 /*
4617 * ":function /pat": list functions matching pattern.
4618 */
4619 if (*eap->arg == '/')
4620 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004621 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004622 if (!eap->skip)
4623 {
4624 regmatch_T regmatch;
4625
4626 c = *p;
4627 *p = NUL;
4628 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4629 *p = c;
4630 if (regmatch.regprog != NULL)
4631 {
4632 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004633 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004634 vim_regfree(regmatch.regprog);
4635 }
4636 }
4637 if (*p == '/')
4638 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004639 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004640 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004641 }
4642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 ga_init(&newargs);
4644 ga_init(&argtypes);
4645 ga_init(&default_args);
4646
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004647 /*
4648 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004649 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004650 * "name" == func, "fudi.fd_dict" == NULL
4651 * dict.func new dictionary entry
4652 * "name" == NULL, "fudi.fd_dict" set,
4653 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4654 * dict.func existing dict entry with a Funcref
4655 * "name" == func, "fudi.fd_dict" set,
4656 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4657 * dict.func existing dict entry that's not a Funcref
4658 * "name" == NULL, "fudi.fd_dict" set,
4659 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4660 * s:func script-local function name
4661 * g:func global function name, same as "func"
4662 */
4663 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004664 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004665 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004666 // nested function, argument is (args).
4667 paren = TRUE;
4668 CLEAR_FIELD(fudi);
4669 }
4670 else
4671 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004672 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004673 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004674 if (p[0] == 's' && p[1] == ':')
4675 {
4676 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4677 return NULL;
4678 }
4679 p = to_name_end(p, TRUE);
4680 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4681 {
4682 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4683 eap->arg);
4684 return NULL;
4685 }
4686 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004687 }
4688
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004689 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004690 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004691 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004692 paren = (vim_strchr(p, '(') != NULL);
4693 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004694 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004695 /*
4696 * Return on an invalid expression in braces, unless the expression
4697 * evaluation has been cancelled due to an aborting error, an
4698 * interrupt, or an exception.
4699 */
4700 if (!aborting())
4701 {
4702 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004703 semsg(_(e_key_not_present_in_dictionary_str),
4704 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004705 vim_free(fudi.fd_newkey);
4706 return NULL;
4707 }
4708 else
4709 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004710 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004711
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004712 // For "export def FuncName()" in an autoload script the function name
4713 // is stored with the legacy autoload name "dir#script#FuncName" so
4714 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004715 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004716 {
4717 char_u *prefixed = may_prefix_autoload(name);
4718
4719 if (prefixed != NULL && prefixed != name)
4720 {
4721 vim_free(name);
4722 name = prefixed;
4723 }
4724 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004725 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004726 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004727 {
4728 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4729 goto ret_free;
4730 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004731 }
4732
Bram Moolenaare38eab22019-12-05 21:50:01 +01004733 // An error in a function call during evaluation of an expression in magic
4734 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004735 saved_did_emsg = did_emsg;
4736 did_emsg = FALSE;
4737
4738 /*
4739 * ":function func" with only function name: list function.
4740 */
4741 if (!paren)
4742 {
4743 if (!ends_excmd(*skipwhite(p)))
4744 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004745 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004746 goto ret_free;
4747 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004748 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004749 if (eap->nextcmd != NULL)
4750 *p = NUL;
4751 if (!eap->skip && !got_int)
4752 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004753 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004754 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4755 {
4756 char_u *up = untrans_function_name(name);
4757
4758 // With Vim9 script the name was made script-local, if not
4759 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004760 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004761 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004762 }
4763
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004764 if (fp != NULL)
4765 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004766 // Check no function was added or removed from a timer, e.g. at
4767 // the more prompt. "fp" may then be invalid.
4768 int prev_ht_changed = func_hashtab.ht_changed;
4769
4770 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004771 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004772 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4773 {
4774 if (FUNCLINE(fp, j) == NULL)
4775 continue;
4776 msg_putchar('\n');
4777 msg_outnum((long)(j + 1));
4778 if (j < 9)
4779 msg_putchar(' ');
4780 if (j < 99)
4781 msg_putchar(' ');
4782 if (function_list_modified(prev_ht_changed))
4783 break;
4784 msg_prt_line(FUNCLINE(fp, j), FALSE);
4785 out_flush(); // show a line at a time
4786 ui_breakcheck();
4787 }
4788 if (!got_int)
4789 {
4790 msg_putchar('\n');
4791 if (!function_list_modified(prev_ht_changed))
4792 {
4793 if (fp->uf_def_status != UF_NOT_COMPILED)
4794 msg_puts(" enddef");
4795 else
4796 msg_puts(" endfunction");
4797 }
4798 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004799 }
4800 }
4801 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004802 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004803 }
4804 goto ret_free;
4805 }
4806
4807 /*
4808 * ":function name(arg1, arg2)" Define function.
4809 */
4810 p = skipwhite(p);
4811 if (*p != '(')
4812 {
4813 if (!eap->skip)
4814 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004815 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004816 goto ret_free;
4817 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004818 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004819 if (vim_strchr(p, '(') != NULL)
4820 p = vim_strchr(p, '(');
4821 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004822
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004823 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4824 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004825 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004826 goto ret_free;
4827 }
4828
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004829 // In Vim9 script only global functions can be redefined.
4830 if (vim9script && eap->forceit && !is_global)
4831 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004832 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004833 goto ret_free;
4834 }
4835
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004836 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004837
Bram Moolenaar04b12692020-05-04 23:24:44 +02004838 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004839 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004840 // Check the name of the function. Unless it's a dictionary function
4841 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004842 if (name != NULL)
4843 arg = name;
4844 else
4845 arg = fudi.fd_newkey;
4846 if (arg != NULL && (fudi.fd_di == NULL
4847 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4848 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4849 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004850 char_u *name_base = arg;
4851 int i;
4852
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004853 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004854 {
4855 name_base = vim_strchr(arg, '_');
4856 if (name_base == NULL)
4857 name_base = arg + 3;
4858 else
4859 ++name_base;
4860 }
4861 for (i = 0; name_base[i] != NUL && (i == 0
4862 ? eval_isnamec1(name_base[i])
4863 : eval_isnamec(name_base[i])); ++i)
4864 ;
4865 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004866 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004867
4868 // In Vim9 script a function cannot have the same name as a
4869 // variable.
4870 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004871 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4872 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004873 + EVAL_VAR_NO_FUNC) == OK)
4874 {
4875 semsg(_(e_redefining_script_item_str), name_base);
4876 goto ret_free;
4877 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004878 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004879 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004880 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004881 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004882 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004883 goto ret_free;
4884 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004885 }
4886
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004887 // This may get more lines and make the pointers into the first line
4888 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004889 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004890 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00004891 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004892 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00004893 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004894 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004895 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004896
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004898 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004899 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004900 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004901 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004902 if (*p != ':')
4903 {
4904 semsg(_(e_no_white_space_allowed_before_colon_str), p);
4905 p = skipwhite(p);
4906 }
4907 else if (!IS_WHITE_OR_NUL(p[1]))
4908 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02004910 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004911 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004912 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004913 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01004914 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004915 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004916 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004918 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004919 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02004920 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004921 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004922 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02004923 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004924 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925 else
4926 // find extra arguments "range", "dict", "abort" and "closure"
4927 for (;;)
4928 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01004929 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004930 p = skipwhite(p);
4931 if (STRNCMP(p, "range", 5) == 0)
4932 {
4933 flags |= FC_RANGE;
4934 p += 5;
4935 }
4936 else if (STRNCMP(p, "dict", 4) == 0)
4937 {
4938 flags |= FC_DICT;
4939 p += 4;
4940 }
4941 else if (STRNCMP(p, "abort", 5) == 0)
4942 {
4943 flags |= FC_ABORT;
4944 p += 5;
4945 }
4946 else if (STRNCMP(p, "closure", 7) == 0)
4947 {
4948 flags |= FC_CLOSURE;
4949 p += 7;
4950 if (current_funccal == NULL)
4951 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004952 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004953 name == NULL ? (char_u *)"" : name);
4954 goto erret;
4955 }
4956 }
4957 else
4958 break;
4959 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004960
Bram Moolenaare38eab22019-12-05 21:50:01 +01004961 // When there is a line break use what follows for the function body.
4962 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004963 if (*p == '\n')
4964 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004965 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02004966 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
4967 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01004968 && !(VIM_ISWHITE(*whitep) && *p == '#'
4969 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02004970 && !eap->skip
4971 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004972 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004973
4974 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004975 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
4976 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004977 */
4978 if (KeyTyped)
4979 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004980 // Check if the function already exists, don't let the user type the
4981 // whole function before telling him it doesn't work! For a script we
4982 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004983 if (!eap->skip && !eap->forceit)
4984 {
4985 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004986 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004987 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00004988 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004989 }
4990
4991 if (!eap->skip && did_emsg)
4992 goto erret;
4993
Bram Moolenaare38eab22019-12-05 21:50:01 +01004994 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004995 cmdline_row = msg_row;
4996 }
4997
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004998 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004999 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005000
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005001 // Do not define the function when getting the body fails and when
5002 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00005003 if (((class_flags & CF_INTERFACE) == 0
5004 && get_function_body(eap, &newlines, line_arg, lines_to_free)
5005 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02005006 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005007 goto erret;
5008
5009 /*
5010 * If there are no errors, add the function
5011 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005012 if (fudi.fd_dict != NULL)
5013 {
5014 char numbuf[20];
5015
5016 fp = NULL;
5017 if (fudi.fd_newkey == NULL && !eap->forceit)
5018 {
5019 emsg(_(e_dictionary_entry_already_exists));
5020 goto erret;
5021 }
5022 if (fudi.fd_di == NULL)
5023 {
5024 // Can't add a function to a locked dictionary
5025 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
5026 goto erret;
5027 }
5028 // Can't change an existing function if it is locked
5029 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
5030 goto erret;
5031
5032 // Give the function a sequential number. Can only be used with a
5033 // Funcref!
5034 vim_free(name);
5035 sprintf(numbuf, "%d", ++func_nr);
5036 name = vim_strsave((char_u *)numbuf);
5037 if (name == NULL)
5038 goto erret;
5039 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005040 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005041 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005042 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005043 char_u *find_name = name;
5044 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005045 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005046
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005047 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005048 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005049 var_conflict = TRUE;
5050
5051 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5052 {
5053 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5054
5055 if (si->sn_autoload_prefix != NULL)
5056 {
5057 if (is_export)
5058 {
5059 find_name = name + STRLEN(si->sn_autoload_prefix);
5060 v = find_var(find_name, &ht, TRUE);
5061 if (v != NULL)
5062 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005063 // Only check if the function already exists in the script,
5064 // global functions can be shadowed.
5065 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005066 }
5067 else
5068 {
5069 char_u *prefixed = may_prefix_autoload(name);
5070
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005071 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005072 {
5073 v = find_var(prefixed, &ht, TRUE);
5074 if (v != NULL)
5075 var_conflict = TRUE;
5076 vim_free(prefixed);
5077 }
5078 }
5079 }
5080 }
5081 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005082 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005083 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005084 goto erret;
5085 }
5086
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005087 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005088 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005089 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005090 char_u *uname = untrans_function_name(name);
5091
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005092 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005093 }
5094
5095 if (fp != NULL || import != NULL)
5096 {
5097 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005098
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005099 // Function can be replaced with "function!" and when sourcing the
5100 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005101 // A name that is used by an import can not be overruled.
5102 if (import != NULL
5103 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005104 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005105 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005106 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005107 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005108 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005109 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005110 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005111 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005112 goto erret;
5113 }
5114 if (fp->uf_calls > 0)
5115 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005116 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005117 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005118 goto erret;
5119 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005120 if (fp->uf_refcount > 1)
5121 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005122 // This function is referenced somewhere, don't redefine it but
5123 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005124 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005125 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005126 fp = NULL;
5127 overwrite = TRUE;
5128 }
5129 else
5130 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005131 char_u *exp_name = fp->uf_name_exp;
5132
5133 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005134 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005135 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005136 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005137 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005138 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005139#ifdef FEAT_PROFILE
5140 fp->uf_profiling = FALSE;
5141 fp->uf_prof_initialized = FALSE;
5142#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005143 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005144 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005145 }
5146 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005147
5148 if (fp == NULL)
5149 {
5150 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5151 {
5152 int slen, plen;
5153 char_u *scriptname;
5154
Bram Moolenaare38eab22019-12-05 21:50:01 +01005155 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005156 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005157 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005158 {
5159 scriptname = autoload_name(name);
5160 if (scriptname != NULL)
5161 {
5162 p = vim_strchr(scriptname, '/');
5163 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005164 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005165 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005166 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005167 j = OK;
5168 vim_free(scriptname);
5169 }
5170 }
5171 if (j == FAIL)
5172 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005173 linenr_T save_lnum = SOURCING_LNUM;
5174
5175 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005176 semsg(_(e_function_name_does_not_match_script_file_name_str),
5177 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005178 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005179 goto erret;
5180 }
5181 }
5182
Bram Moolenaare01e5212023-01-08 20:31:18 +00005183 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005184 if (fp == NULL)
5185 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005186 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005187
5188 if (fudi.fd_dict != NULL)
5189 {
5190 if (fudi.fd_di == NULL)
5191 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005192 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005193 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5194 if (fudi.fd_di == NULL)
5195 {
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005196 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005197 goto erret;
5198 }
5199 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5200 {
5201 vim_free(fudi.fd_di);
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005202 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005203 goto erret;
5204 }
5205 }
5206 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005207 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005208 clear_tv(&fudi.fd_di->di_tv);
5209 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005210 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005211
Bram Moolenaare38eab22019-12-05 21:50:01 +01005212 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005213 flags |= FC_DICT;
5214 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005215 }
5216 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005217 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005219 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005220
5221 if (eap->cmdidx == CMD_def)
5222 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005223 int lnum_save = SOURCING_LNUM;
5224 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005225
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005226 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005227
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005228 // error messages are for the first function line
5229 SOURCING_LNUM = sourcing_lnum_top;
5230
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005231 // The function may use script variables from the context.
5232 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005233
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005234 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005235 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005236 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005237 free_fp = fp_allocated;
5238 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005239 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005240 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005241
5242 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005243 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005244 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005245 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005246 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005247 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005248 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005249 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005250 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005251 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005252 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005253
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005254 if (fp_allocated)
5255 {
5256 // insert the new function in the function list
5257 set_ufunc_name(fp, name);
5258 if (overwrite)
5259 {
5260 hi = hash_find(&func_hashtab, name);
5261 hi->hi_key = UF2HIKEY(fp);
5262 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005263 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005264 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005265 {
5266 free_fp = TRUE;
5267 goto erret;
5268 }
5269 fp->uf_refcount = 1;
5270 }
5271
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005272 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005273 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005274 if ((flags & FC_CLOSURE) != 0)
5275 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005276 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005277 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005278 }
5279 else
5280 fp->uf_scoped = NULL;
5281
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005282#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005283 if (prof_def_func())
5284 func_do_profile(fp);
5285#endif
5286 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005287 if (sandbox)
5288 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005289 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005290 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005291 fp->uf_flags = flags;
5292 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005293 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005294 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005295 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005296 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005297 if (is_export)
5298 {
5299 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005300 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005301 is_export = FALSE;
5302 }
5303
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005304 if (eap->cmdidx == CMD_def)
5305 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005306 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5307 // :func does not use Vim9 script syntax, even in a Vim9 script file
5308 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005309
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005310 goto ret_free;
5311
5312erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005313 if (fp != NULL)
5314 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005315 // these were set to "newargs" and "default_args", which are cleared
5316 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005317 ga_init(&fp->uf_args);
5318 ga_init(&fp->uf_def_args);
5319 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005320errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005321 ga_clear_strings(&newargs);
5322 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005323 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005324 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005325 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005326 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005327 VIM_CLEAR(fp->uf_va_name);
5328 clear_type_list(&fp->uf_type_list);
5329 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005330 if (free_fp)
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00005331 VIM_CLEAR(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005332ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005333 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005334 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005335 if (name != name_arg)
5336 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005337 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005338 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005339
5340 return fp;
5341}
5342
5343/*
5344 * ":function"
5345 */
5346 void
5347ex_function(exarg_T *eap)
5348{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005349 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005350
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005351 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar554d0312023-01-05 19:59:18 +00005352 (void)define_function(eap, NULL, &lines_to_free, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005353 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005354}
5355
5356/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005357 * Find a function by name, including "<lambda>123".
5358 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005359 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005360 * Return NULL if not found.
5361 */
5362 ufunc_T *
5363find_func_by_name(char_u *name, compiletype_T *compile_type)
5364{
5365 char_u *arg = name;
5366 char_u *fname;
5367 ufunc_T *ufunc;
5368 int is_global = FALSE;
5369
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005370 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5371 {
5372 *compile_type = CT_PROFILE;
5373 arg = skipwhite(arg + 7);
5374 }
5375 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5376 {
5377 *compile_type = CT_DEBUG;
5378 arg = skipwhite(arg + 5);
5379 }
5380
5381 if (STRNCMP(arg, "<lambda>", 8) == 0)
5382 {
5383 arg += 8;
5384 (void)getdigits(&arg);
5385 fname = vim_strnsave(name, arg - name);
5386 }
5387 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005388 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005389 // First try finding a method in a class, trans_function_name() will
5390 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005391 ufunc = find_class_func(&arg);
5392 if (ufunc != NULL)
5393 return ufunc;
5394
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005395 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005396 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005397 NULL, NULL, NULL, &ufunc);
5398 if (ufunc != NULL)
5399 {
5400 vim_free(fname);
5401 return ufunc;
5402 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005403 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005404 if (fname == NULL)
5405 {
5406 semsg(_(e_invalid_argument_str), name);
5407 return NULL;
5408 }
5409 if (!ends_excmd2(name, arg))
5410 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005411 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005412 emsg(ex_errmsg(e_trailing_characters_str, arg));
5413 return NULL;
5414 }
5415
5416 ufunc = find_func(fname, is_global);
5417 if (ufunc == NULL)
5418 {
5419 char_u *p = untrans_function_name(fname);
5420
5421 if (p != NULL)
5422 // Try again without making it script-local.
5423 ufunc = find_func(p, FALSE);
5424 }
5425 vim_free(fname);
5426 if (ufunc == NULL)
5427 semsg(_(e_cannot_find_function_str), name);
5428 return ufunc;
5429}
5430
5431/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005432 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005433 * be compiled or the one specified by the argument.
5434 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005435 */
5436 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005437ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005438{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005439 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005440 {
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005441 compiletype_T compile_type = CT_NONE;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005442 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005443 if (ufunc != NULL)
5444 {
5445 if (func_needs_compiling(ufunc, compile_type))
5446 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5447 else
5448 smsg(_("Function %s does not need compiling"), eap->arg);
5449 }
5450 }
5451 else
5452 {
5453 long todo = (long)func_hashtab.ht_used;
5454 int changed = func_hashtab.ht_changed;
5455 hashitem_T *hi;
5456
5457 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5458 {
5459 if (!HASHITEM_EMPTY(hi))
5460 {
5461 --todo;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005462 ufunc_T *ufunc = HI2UF(hi);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005463 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5464 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5465 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005466 {
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005467 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5468
5469 if (func_hashtab.ht_changed != changed)
5470 {
5471 // a function has been added or removed, need to start
5472 // over
5473 todo = (long)func_hashtab.ht_used;
5474 changed = func_hashtab.ht_changed;
5475 hi = func_hashtab.ht_array;
5476 --hi;
5477 }
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005478 }
5479 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005480 }
5481 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005482}
5483
5484/*
5485 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5486 * Return 2 if "p" starts with "s:".
5487 * Return 0 otherwise.
5488 */
5489 int
5490eval_fname_script(char_u *p)
5491{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005492 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5493 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005494 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5495 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5496 return 5;
5497 if (p[0] == 's' && p[1] == ':')
5498 return 2;
5499 return 0;
5500}
5501
5502 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005503translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005504{
5505 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005506 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005507 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005508}
5509
5510/*
5511 * Return TRUE when "ufunc" has old-style "..." varargs
5512 * or named varargs "...name: type".
5513 */
5514 int
5515has_varargs(ufunc_T *ufunc)
5516{
5517 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005518}
5519
5520/*
5521 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005522 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005523 */
5524 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005525function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005526{
5527 char_u *nm = name;
5528 char_u *p;
5529 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005530 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005531 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005532
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005533 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5534 if (no_deref)
5535 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005536 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005537 nm = skipwhite(nm);
5538
Bram Moolenaare38eab22019-12-05 21:50:01 +01005539 // Only accept "funcname", "funcname ", "funcname (..." and
5540 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005541 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005542 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005543 vim_free(p);
5544 return n;
5545}
5546
Bram Moolenaar113e1072019-01-20 15:30:40 +01005547#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005548 char_u *
5549get_expanded_name(char_u *name, int check)
5550{
5551 char_u *nm = name;
5552 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005553 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005554
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005555 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005556
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005557 if (p != NULL && *nm == NUL
5558 && (!check || translated_function_exists(p, is_global)))
5559 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005560
5561 vim_free(p);
5562 return NULL;
5563}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005564#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005565
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005566/*
5567 * Function given to ExpandGeneric() to obtain the list of user defined
5568 * function names.
5569 */
5570 char_u *
5571get_user_func_name(expand_T *xp, int idx)
5572{
5573 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005574 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005575 static hashitem_T *hi;
5576 ufunc_T *fp;
5577
5578 if (idx == 0)
5579 {
5580 done = 0;
5581 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005582 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005583 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005584 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005585 {
5586 if (done++ > 0)
5587 ++hi;
5588 while (HASHITEM_EMPTY(hi))
5589 ++hi;
5590 fp = HI2UF(hi);
5591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005592 // don't show dead, dict and lambda functions
5593 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005594 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005595 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005596
5597 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005598 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005599
5600 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005601 if (xp->xp_context != EXPAND_USER_FUNC
5602 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005603 {
5604 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005605 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005606 STRCAT(IObuff, ")");
5607 }
5608 return IObuff;
5609 }
5610 return NULL;
5611}
5612
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005613/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005614 * Make a copy of a function.
5615 * Intended to be used for a function defined on a base class that has a copy
5616 * on the child class.
5617 * The copy has uf_refcount set to one.
5618 * Returns NULL when out of memory.
5619 */
5620 ufunc_T *
5621copy_function(ufunc_T *fp)
5622{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005623 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005624 if (ufunc == NULL)
5625 return NULL;
5626
5627 // Most things can just be copied.
5628 *ufunc = *fp;
5629
5630 ufunc->uf_def_status = UF_TO_BE_COMPILED;
5631 ufunc->uf_dfunc_idx = 0;
5632 ufunc->uf_class = NULL;
5633
5634 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
5635 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
5636
5637 if (ufunc->uf_arg_types != NULL)
5638 {
5639 // "uf_arg_types" is an allocated array, make a copy.
5640 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
5641 if (at != NULL)
5642 {
5643 mch_memmove(at, ufunc->uf_arg_types,
5644 sizeof(type_T *) * ufunc->uf_args.ga_len);
5645 ufunc->uf_arg_types = at;
5646 }
5647 }
5648
5649 // TODO: how about the types themselves? they can be freed when the
5650 // original function is freed:
5651 // type_T **uf_arg_types;
5652 // type_T *uf_ret_type;
5653
Ernie Rael114ec812023-06-04 18:11:35 +01005654 // make uf_type_list empty
5655 ga_init(&ufunc->uf_type_list);
Bram Moolenaar83677162023-01-08 19:54:10 +00005656
5657 // TODO: partial_T *uf_partial;
5658
5659 if (ufunc->uf_va_name != NULL)
5660 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
5661
5662 // TODO:
5663 // type_T *uf_va_type;
5664 // type_T *uf_func_type;
5665
5666 ufunc->uf_block_depth = 0;
5667 ufunc->uf_block_ids = NULL;
5668
5669 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
5670
5671 ufunc->uf_refcount = 1;
5672 ufunc->uf_name_exp = NULL;
5673 STRCPY(ufunc->uf_name, fp->uf_name);
5674
5675 return ufunc;
5676}
5677
5678/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005679 * ":delfunction {name}"
5680 */
5681 void
5682ex_delfunction(exarg_T *eap)
5683{
5684 ufunc_T *fp = NULL;
5685 char_u *p;
5686 char_u *name;
5687 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005688 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005689
5690 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005691 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
5692 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005693 vim_free(fudi.fd_newkey);
5694 if (name == NULL)
5695 {
5696 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005697 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005698 return;
5699 }
5700 if (!ends_excmd(*skipwhite(p)))
5701 {
5702 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005703 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005704 return;
5705 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005706 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005707 if (eap->nextcmd != NULL)
5708 *p = NUL;
5709
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005710 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005711 {
5712 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005713 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005714 vim_free(name);
5715 return;
5716 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005717 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005718 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005719 vim_free(name);
5720
5721 if (!eap->skip)
5722 {
5723 if (fp == NULL)
5724 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005725 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005726 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005727 return;
5728 }
5729 if (fp->uf_calls > 0)
5730 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005731 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005732 return;
5733 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005734 if (fp->uf_flags & FC_VIM9)
5735 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005736 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005737 return;
5738 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005739
5740 if (fudi.fd_dict != NULL)
5741 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005742 // Delete the dict item that refers to the function, it will
5743 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00005744 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005745 }
5746 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005747 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005748 // A normal function (not a numbered function or lambda) has a
5749 // refcount of 1 for the entry in the hashtable. When deleting
5750 // it and the refcount is more than one, it should be kept.
5751 // A numbered function and lambda should be kept if the refcount is
5752 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005753 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005754 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005755 // Function is still referenced somewhere. Don't free it but
5756 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005757 if (func_remove(fp))
5758 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005759 }
5760 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005761 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005762 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005763 }
5764}
5765
5766/*
5767 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005768 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005769 */
5770 void
5771func_unref(char_u *name)
5772{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005773 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005774
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005775 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005776 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005777 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005778 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005779 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005780#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005781 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005782#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005783 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005784 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005785 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005786}
5787
5788/*
5789 * Unreference a Function: decrement the reference count and free it when it
5790 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005791 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005792 */
5793 void
5794func_ptr_unref(ufunc_T *fp)
5795{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005796 if (fp != NULL && (--fp->uf_refcount <= 0
5797 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5798 && fp->uf_partial->pt_refcount <= 1
5799 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005800 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005801 // Only delete it when it's not being used. Otherwise it's done
5802 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005803 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005804 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005805 }
5806}
5807
5808/*
5809 * Count a reference to a Function.
5810 */
5811 void
5812func_ref(char_u *name)
5813{
5814 ufunc_T *fp;
5815
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005816 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005817 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005818 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005819 if (fp != NULL)
5820 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005821 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005822 // Only give an error for a numbered function.
5823 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005824 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005825}
5826
5827/*
5828 * Count a reference to a Function.
5829 */
5830 void
5831func_ptr_ref(ufunc_T *fp)
5832{
5833 if (fp != NULL)
5834 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005835}
5836
5837/*
5838 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5839 * referenced from anywhere that is in use.
5840 */
5841 static int
5842can_free_funccal(funccall_T *fc, int copyID)
5843{
Bram Moolenaarca16c602022-09-06 18:57:08 +01005844 return (fc->fc_l_varlist.lv_copyID != copyID
5845 && fc->fc_l_vars.dv_copyID != copyID
5846 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005847 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005848}
5849
5850/*
5851 * ":return [expr]"
5852 */
5853 void
5854ex_return(exarg_T *eap)
5855{
5856 char_u *arg = eap->arg;
5857 typval_T rettv;
5858 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005859 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005860
5861 if (current_funccal == NULL)
5862 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005863 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005864 return;
5865 }
5866
Bram Moolenaar844fb642021-10-23 13:32:30 +01005867 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005868 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5869
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005870 if (eap->skip)
5871 ++emsg_skip;
5872
5873 eap->nextcmd = NULL;
5874 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005875 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005876 {
5877 if (!eap->skip)
5878 returning = do_return(eap, FALSE, TRUE, &rettv);
5879 else
5880 clear_tv(&rettv);
5881 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005882 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005883 else if (!eap->skip)
5884 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005885 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005886 update_force_abort();
5887
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005888 /*
5889 * Return unless the expression evaluation has been cancelled due to an
5890 * aborting error, an interrupt, or an exception.
5891 */
5892 if (!aborting())
5893 returning = do_return(eap, FALSE, TRUE, NULL);
5894 }
5895
Bram Moolenaare38eab22019-12-05 21:50:01 +01005896 // When skipping or the return gets pending, advance to the next command
5897 // in this line (!returning). Otherwise, ignore the rest of the line.
5898 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005899 if (returning)
5900 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005901 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02005902 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005903
5904 if (eap->skip)
5905 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02005906 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005907}
5908
zeertzjq5299c092023-04-12 20:48:16 +01005909/*
5910 * Lower level implementation of "call". Only called when not skipping.
5911 */
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005912 static int
5913ex_call_inner(
5914 exarg_T *eap,
5915 char_u *name,
5916 char_u **arg,
5917 char_u *startarg,
5918 funcexe_T *funcexe_init,
5919 evalarg_T *evalarg)
5920{
5921 linenr_T lnum;
5922 int doesrange;
5923 typval_T rettv;
5924 int failed = FALSE;
5925
zeertzjq5299c092023-04-12 20:48:16 +01005926 lnum = eap->line1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005927 for ( ; lnum <= eap->line2; ++lnum)
5928 {
5929 funcexe_T funcexe;
5930
zeertzjq5299c092023-04-12 20:48:16 +01005931 if (eap->addr_count > 0)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005932 {
5933 if (lnum > curbuf->b_ml.ml_line_count)
5934 {
5935 // If the function deleted lines or switched to another buffer
5936 // the line number may become invalid.
5937 emsg(_(e_invalid_range));
5938 break;
5939 }
5940 curwin->w_cursor.lnum = lnum;
5941 curwin->w_cursor.col = 0;
5942 curwin->w_cursor.coladd = 0;
5943 }
5944 *arg = startarg;
5945
5946 funcexe = *funcexe_init;
5947 funcexe.fe_doesrange = &doesrange;
5948 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
5949 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
5950 {
5951 failed = TRUE;
5952 break;
5953 }
5954 if (has_watchexpr())
5955 dbg_check_breakpoint(eap);
5956
5957 // Handle a function returning a Funcref, Dictionary or List.
5958 if (handle_subscript(arg, NULL, &rettv,
zeertzjq5299c092023-04-12 20:48:16 +01005959 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005960 {
5961 failed = TRUE;
5962 break;
5963 }
5964
5965 clear_tv(&rettv);
zeertzjq5299c092023-04-12 20:48:16 +01005966 if (doesrange)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005967 break;
5968
5969 // Stop when immediately aborting on error, or when an interrupt
5970 // occurred or an exception was thrown but not caught.
5971 // get_func_tv() returned OK, so that the check for trailing
5972 // characters below is executed.
5973 if (aborting())
5974 break;
5975 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005976 return failed;
5977}
5978
5979/*
5980 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
5981 * Returns FAIL or OK.
5982 */
5983 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01005984ex_defer_inner(
5985 char_u *name,
5986 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01005987 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01005988 partial_T *partial,
5989 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005990{
5991 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01005992 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005993 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01005994 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005995
5996 if (current_funccal == NULL)
5997 {
5998 semsg(_(e_str_not_inside_function), "defer");
5999 return FAIL;
6000 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01006001 if (partial != NULL)
6002 {
6003 if (partial->pt_dict != NULL)
6004 {
6005 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
6006 return FAIL;
6007 }
6008 if (partial->pt_argc > 0)
6009 {
6010 int i;
6011
6012 partial_argc = partial->pt_argc;
6013 for (i = 0; i < partial_argc; ++i)
6014 copy_tv(&partial->pt_argv[i], &argvars[i]);
6015 }
6016 }
6017 r = get_func_arguments(arg, evalarg, FALSE,
6018 argvars + partial_argc, &argcount);
6019 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01006020
6021 if (r == OK)
6022 {
6023 if (type != NULL)
6024 {
6025 // Check that the arguments are OK for the types of the funcref.
6026 r = check_argument_types(type, argvars, argcount, NULL, name);
6027 }
6028 else if (builtin_function(name, -1))
6029 {
6030 int idx = find_internal_func(name);
6031
6032 if (idx < 0)
6033 {
6034 emsg_funcname(e_unknown_function_str, name);
6035 r = FAIL;
6036 }
6037 else if (check_internal_func(idx, argcount) == -1)
6038 r = FAIL;
6039 }
6040 else
6041 {
6042 ufunc_T *ufunc = find_func(name, FALSE);
6043
6044 // we tolerate an unknown function here, it might be defined later
6045 if (ufunc != NULL)
6046 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006047 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006048 if (error != FCERR_UNKNOWN)
6049 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006050 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006051 r = FAIL;
6052 }
6053 }
6054 }
6055 }
6056
Bram Moolenaar86d87252022-09-05 21:21:25 +01006057 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006058 {
6059 while (--argcount >= 0)
6060 clear_tv(&argvars[argcount]);
6061 return FAIL;
6062 }
6063 return add_defer(name, argcount, argvars);
6064}
6065
6066/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006067 * Return TRUE if currently inside a function call.
6068 * Give an error message and return FALSE when not.
6069 */
6070 int
6071can_add_defer(void)
6072{
6073 if (!in_def_function() && get_current_funccal() == NULL)
6074 {
6075 semsg(_(e_str_not_inside_function), "defer");
6076 return FALSE;
6077 }
6078 return TRUE;
6079}
6080
6081/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006082 * Add a deferred call for "name" with arguments "argvars[argcount]".
6083 * Consumes "argvars[]".
6084 * Caller must check that in_def_function() returns TRUE or current_funccal is
6085 * not NULL.
6086 * Returns OK or FAIL.
6087 */
6088 int
6089add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6090{
6091 char_u *saved_name = vim_strsave(name);
6092 int argcount = argcount_arg;
6093 defer_T *dr;
6094 int ret = FAIL;
6095
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006096 if (saved_name == NULL)
6097 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006098 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006099 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006100 if (add_defer_function(saved_name, argcount, argvars) == OK)
6101 argcount = 0;
6102 }
6103 else
6104 {
6105 if (current_funccal->fc_defer.ga_itemsize == 0)
6106 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6107 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6108 goto theend;
6109 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6110 + current_funccal->fc_defer.ga_len++;
6111 dr->dr_name = saved_name;
6112 dr->dr_argcount = argcount;
6113 while (argcount > 0)
6114 {
6115 --argcount;
6116 dr->dr_argvars[argcount] = argvars[argcount];
6117 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006118 }
6119 ret = OK;
6120
6121theend:
6122 while (--argcount >= 0)
6123 clear_tv(&argvars[argcount]);
6124 return ret;
6125}
6126
6127/*
6128 * Invoked after a functions has finished: invoke ":defer" functions.
6129 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006130 static void
6131handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006132{
6133 int idx;
6134
Bram Moolenaar58779852022-09-06 18:31:14 +01006135 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006136 {
Bram Moolenaar58779852022-09-06 18:31:14 +01006137 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006138
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006139 if (dr->dr_name == NULL)
6140 // already being called, can happen if function does ":qa"
6141 continue;
6142
6143 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006144 CLEAR_FIELD(funcexe);
6145 funcexe.fe_evaluate = TRUE;
6146
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006147 typval_T rettv;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006148 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006149
6150 char_u *name = dr->dr_name;
6151 dr->dr_name = NULL;
6152
6153 call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe);
6154
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006155 clear_tv(&rettv);
Bram Moolenaar42994bf2023-04-17 19:23:45 +01006156 vim_free(name);
6157 for (int i = dr->dr_argcount - 1; i >= 0; --i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006158 clear_tv(&dr->dr_argvars[i]);
6159 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006160 ga_clear(&funccal->fc_defer);
6161}
6162
zeertzjq960cf912023-04-18 21:52:54 +01006163 static void
6164invoke_funccall_defer(funccall_T *fc)
6165{
6166 if (fc->fc_ectx != NULL)
6167 {
6168 // :def function
6169 unwind_def_callstack(fc->fc_ectx);
6170 may_invoke_defer_funcs(fc->fc_ectx);
6171 }
6172 else
6173 {
6174 // legacy function
6175 handle_defer_one(fc);
6176 }
6177}
6178
Bram Moolenaar58779852022-09-06 18:31:14 +01006179/*
6180 * Called when exiting: call all defer functions.
6181 */
6182 void
6183invoke_all_defer(void)
6184{
zeertzjq1be4b812023-04-19 14:21:24 +01006185 for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller)
6186 invoke_funccall_defer(fc);
6187
zeertzjq960cf912023-04-18 21:52:54 +01006188 for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next)
6189 for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller)
6190 invoke_funccall_defer(fc);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006191}
6192
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006193/*
6194 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006195 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006196 */
6197 void
6198ex_call(exarg_T *eap)
6199{
6200 char_u *arg = eap->arg;
6201 char_u *startarg;
6202 char_u *name;
6203 char_u *tofree;
6204 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006205 int failed = FALSE;
6206 funcdict_T fudi;
6207 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006208 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006209 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006210 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006211 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006212
Bram Moolenaare6b53242020-07-01 17:28:33 +02006213 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006214 if (eap->skip)
6215 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006216 typval_T rettv;
6217
Bram Moolenaare38eab22019-12-05 21:50:01 +01006218 // trans_function_name() doesn't work well when skipping, use eval0()
6219 // instead to skip to any following command, e.g. for:
6220 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006221 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006222 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006223 clear_tv(&rettv);
6224 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006225 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006226 return;
6227 }
6228
zeertzjq5299c092023-04-12 20:48:16 +01006229 tofree = trans_function_name_ext(&arg, NULL, FALSE, TFN_INT,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00006230 &fudi, &partial, vim9script ? &type : NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006231 if (fudi.fd_newkey != NULL)
6232 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006233 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006234 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006235 vim_free(fudi.fd_newkey);
6236 }
6237 if (tofree == NULL)
6238 return;
6239
Bram Moolenaare38eab22019-12-05 21:50:01 +01006240 // Increase refcount on dictionary, it could get deleted when evaluating
6241 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006242 if (fudi.fd_dict != NULL)
6243 ++fudi.fd_dict->dv_refcount;
6244
Bram Moolenaare38eab22019-12-05 21:50:01 +01006245 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6246 // contents. For VAR_PARTIAL get its partial, unless we already have one
6247 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006248 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006249 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006250 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006251 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006252
Bram Moolenaare38eab22019-12-05 21:50:01 +01006253 // Skip white space to allow ":call func ()". Not good, but required for
6254 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006255 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006256 if (*startarg != '(')
6257 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006258 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006259 goto end;
6260 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006261 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006262 {
6263 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6264 goto end;
6265 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006266
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006267 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006268 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006269 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006270 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006271 }
6272 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006273 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006274 funcexe_T funcexe;
6275
Bram Moolenaara80faa82020-04-12 19:37:17 +02006276 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006277 funcexe.fe_check_type = type;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006278 funcexe.fe_partial = partial;
6279 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006280 funcexe.fe_firstline = eap->line1;
6281 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006282 funcexe.fe_found_var = found_var;
zeertzjq5299c092023-04-12 20:48:16 +01006283 funcexe.fe_evaluate = TRUE;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006284 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006285 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006286
Bram Moolenaar1d341892021-09-18 15:25:52 +02006287 // When inside :try we need to check for following "| catch" or "| endtry".
6288 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006289 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006290 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006291 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006292 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006293 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006294 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006295 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006296 {
6297 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006298 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006299 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006300 }
6301 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006302 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006303 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006304 // Must be after using "arg", it may point into memory cleared here.
6305 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006306
6307end:
6308 dict_unref(fudi.fd_dict);
6309 vim_free(tofree);
6310}
6311
6312/*
6313 * Return from a function. Possibly makes the return pending. Also called
6314 * for a pending return at the ":endtry" or after returning from an extra
6315 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6316 * when called due to a ":return" command. "rettv" may point to a typval_T
6317 * with the return rettv. Returns TRUE when the return can be carried out,
6318 * FALSE when the return gets pending.
6319 */
6320 int
6321do_return(
6322 exarg_T *eap,
6323 int reanimate,
6324 int is_cmd,
6325 void *rettv)
6326{
6327 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006328 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006329
6330 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006331 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006332 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006333
6334 /*
6335 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6336 * not in its finally clause (which then is to be executed next) is found.
6337 * In this case, make the ":return" pending for execution at the ":endtry".
6338 * Otherwise, return normally.
6339 */
6340 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6341 if (idx >= 0)
6342 {
6343 cstack->cs_pending[idx] = CSTP_RETURN;
6344
6345 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006346 // A pending return again gets pending. "rettv" points to an
6347 // allocated variable with the rettv of the original ":return"'s
6348 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006349 cstack->cs_rettv[idx] = rettv;
6350 else
6351 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006352 // When undoing a return in order to make it pending, get the stored
6353 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006354 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006355 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006356
6357 if (rettv != NULL)
6358 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006359 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006360 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6361 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6362 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006363 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006364 }
6365 else
6366 cstack->cs_rettv[idx] = NULL;
6367
6368 if (reanimate)
6369 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006370 // The pending return value could be overwritten by a ":return"
6371 // without argument in a finally clause; reset the default
6372 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006373 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6374 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006375 }
6376 }
6377 report_make_pending(CSTP_RETURN, rettv);
6378 }
6379 else
6380 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006381 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006382
Bram Moolenaare38eab22019-12-05 21:50:01 +01006383 // If the return is carried out now, store the return value. For
6384 // a return immediately after reanimation, the value is already
6385 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006386 if (!reanimate && rettv != NULL)
6387 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006388 clear_tv(current_funccal->fc_rettv);
6389 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006390 if (!is_cmd)
6391 vim_free(rettv);
6392 }
6393 }
6394
6395 return idx < 0;
6396}
6397
6398/*
6399 * Free the variable with a pending return value.
6400 */
6401 void
6402discard_pending_return(void *rettv)
6403{
6404 free_tv((typval_T *)rettv);
6405}
6406
6407/*
6408 * Generate a return command for producing the value of "rettv". The result
6409 * is an allocated string. Used by report_pending() for verbose messages.
6410 */
6411 char_u *
6412get_return_cmd(void *rettv)
6413{
6414 char_u *s = NULL;
6415 char_u *tofree = NULL;
6416 char_u numbuf[NUMBUFLEN];
6417
6418 if (rettv != NULL)
6419 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6420 if (s == NULL)
6421 s = (char_u *)"";
6422
6423 STRCPY(IObuff, ":return ");
6424 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6425 if (STRLEN(s) + 8 >= IOSIZE)
6426 STRCPY(IObuff + IOSIZE - 4, "...");
6427 vim_free(tofree);
6428 return vim_strsave(IObuff);
6429}
6430
6431/*
6432 * Get next function line.
6433 * Called by do_cmdline() to get the next line.
6434 * Returns allocated string, or NULL for end of function.
6435 */
6436 char_u *
6437get_func_line(
6438 int c UNUSED,
6439 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006440 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006441 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006442{
6443 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006444 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006445 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006446 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006447
Bram Moolenaare38eab22019-12-05 21:50:01 +01006448 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006449 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006450 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006451 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006452 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006453 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006454 }
6455#ifdef FEAT_PROFILE
6456 if (do_profiling == PROF_YES)
6457 func_line_end(cookie);
6458#endif
6459
6460 gap = &fp->uf_lines;
6461 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006462 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006463 retval = NULL;
6464 else
6465 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006466 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006467 while (fcp->fc_linenr < gap->ga_len
6468 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6469 ++fcp->fc_linenr;
6470 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006471 retval = NULL;
6472 else
6473 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006474 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6475 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006476#ifdef FEAT_PROFILE
6477 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006478 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006479#endif
6480 }
6481 }
6482
Bram Moolenaare38eab22019-12-05 21:50:01 +01006483 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006484 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006485 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006486 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006487 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006488 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006489 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006490 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006491 }
6492
6493 return retval;
6494}
6495
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006496/*
6497 * Return TRUE if the currently active function should be ended, because a
6498 * return was encountered or an error occurred. Used inside a ":while".
6499 */
6500 int
6501func_has_ended(void *cookie)
6502{
6503 funccall_T *fcp = (funccall_T *)cookie;
6504
Bram Moolenaare38eab22019-12-05 21:50:01 +01006505 // Ignore the "abort" flag if the abortion behavior has been changed due to
6506 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006507 return (((fcp->fc_func->uf_flags & FC_ABORT)
6508 && did_emsg && !aborted_in_try())
6509 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006510}
6511
6512/*
6513 * return TRUE if cookie indicates a function which "abort"s on errors.
6514 */
6515 int
6516func_has_abort(
6517 void *cookie)
6518{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006519 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006520}
6521
6522
6523/*
6524 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6525 * Don't do this when "Func" is already a partial that was bound
6526 * explicitly (pt_auto is FALSE).
6527 * Changes "rettv" in-place.
6528 * Returns the updated "selfdict_in".
6529 */
6530 dict_T *
6531make_partial(dict_T *selfdict_in, typval_T *rettv)
6532{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006533 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006534 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006535 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006536 dict_T *selfdict = selfdict_in;
6537
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006538 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6539 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006540 fp = rettv->vval.v_partial->pt_func;
6541 else
6542 {
6543 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006544 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006545 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006546 if (fname == NULL)
6547 {
6548 // There is no point binding a dict to a NULL function, just create
6549 // a function reference.
6550 rettv->v_type = VAR_FUNC;
6551 rettv->vval.v_string = NULL;
6552 }
6553 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006554 {
6555 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006556 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006557
6558 // Translate "s:func" to the stored function name.
6559 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6560 fp = find_func(fname, FALSE);
6561 vim_free(tofree);
6562 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006563 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006564
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006565 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006566 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006567 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006568
6569 if (pt != NULL)
6570 {
6571 pt->pt_refcount = 1;
6572 pt->pt_dict = selfdict;
6573 pt->pt_auto = TRUE;
6574 selfdict = NULL;
6575 if (rettv->v_type == VAR_FUNC)
6576 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006577 // Just a function: Take over the function name and use
6578 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006579 pt->pt_name = rettv->vval.v_string;
6580 }
6581 else
6582 {
6583 partial_T *ret_pt = rettv->vval.v_partial;
6584 int i;
6585
Bram Moolenaare38eab22019-12-05 21:50:01 +01006586 // Partial: copy the function name, use selfdict and copy
6587 // args. Can't take over name or args, the partial might
6588 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006589 if (ret_pt->pt_name != NULL)
6590 {
6591 pt->pt_name = vim_strsave(ret_pt->pt_name);
6592 func_ref(pt->pt_name);
6593 }
6594 else
6595 {
6596 pt->pt_func = ret_pt->pt_func;
6597 func_ptr_ref(pt->pt_func);
6598 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006599 if (ret_pt->pt_argc > 0)
6600 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006601 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006602 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006603 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006604 pt->pt_argc = 0;
6605 else
6606 {
6607 pt->pt_argc = ret_pt->pt_argc;
6608 for (i = 0; i < pt->pt_argc; i++)
6609 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6610 }
6611 }
6612 partial_unref(ret_pt);
6613 }
6614 rettv->v_type = VAR_PARTIAL;
6615 rettv->vval.v_partial = pt;
6616 }
6617 }
6618 return selfdict;
6619}
6620
6621/*
6622 * Return the name of the executed function.
6623 */
6624 char_u *
6625func_name(void *cookie)
6626{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006627 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006628}
6629
6630/*
6631 * Return the address holding the next breakpoint line for a funccall cookie.
6632 */
6633 linenr_T *
6634func_breakpoint(void *cookie)
6635{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006636 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006637}
6638
6639/*
6640 * Return the address holding the debug tick for a funccall cookie.
6641 */
6642 int *
6643func_dbg_tick(void *cookie)
6644{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006645 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006646}
6647
6648/*
6649 * Return the nesting level for a funccall cookie.
6650 */
6651 int
6652func_level(void *cookie)
6653{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006654 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006655}
6656
6657/*
6658 * Return TRUE when a function was ended by a ":return" command.
6659 */
6660 int
6661current_func_returned(void)
6662{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006663 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006664}
6665
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006666 int
6667free_unref_funccal(int copyID, int testing)
6668{
6669 int did_free = FALSE;
6670 int did_free_funccal = FALSE;
6671 funccall_T *fc, **pfc;
6672
6673 for (pfc = &previous_funccal; *pfc != NULL; )
6674 {
6675 if (can_free_funccal(*pfc, copyID))
6676 {
6677 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006678 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006679 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006680 did_free = TRUE;
6681 did_free_funccal = TRUE;
6682 }
6683 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006684 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006685 }
6686 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006687 // When a funccal was freed some more items might be garbage
6688 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006689 (void)garbage_collect(testing);
6690
6691 return did_free;
6692}
6693
6694/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006695 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006696 */
6697 static funccall_T *
6698get_funccal(void)
6699{
6700 int i;
6701 funccall_T *funccal;
6702 funccall_T *temp_funccal;
6703
6704 funccal = current_funccal;
6705 if (debug_backtrace_level > 0)
6706 {
6707 for (i = 0; i < debug_backtrace_level; i++)
6708 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006709 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006710 if (temp_funccal)
6711 funccal = temp_funccal;
6712 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006713 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006714 debug_backtrace_level = i;
6715 }
6716 }
6717 return funccal;
6718}
6719
6720/*
6721 * Return the hashtable used for local variables in the current funccal.
6722 * Return NULL if there is no current funccal.
6723 */
6724 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006725get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006726{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006727 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006728 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006729 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006730}
6731
6732/*
6733 * Return the l: scope variable.
6734 * Return NULL if there is no current funccal.
6735 */
6736 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006737get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006738{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006739 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006740 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006741 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006742}
6743
6744/*
6745 * Return the hashtable used for argument in the current funccal.
6746 * Return NULL if there is no current funccal.
6747 */
6748 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006749get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006750{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006751 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006752 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006753 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006754}
6755
6756/*
6757 * Return the a: scope variable.
6758 * Return NULL if there is no current funccal.
6759 */
6760 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006761get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006762{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006763 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006764 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006765 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006766}
6767
6768/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006769 * List function variables, if there is a function.
6770 */
6771 void
6772list_func_vars(int *first)
6773{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006774 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
6775 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006776 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006777}
6778
6779/*
6780 * If "ht" is the hashtable for local variables in the current funccal, return
6781 * the dict that contains it.
6782 * Otherwise return NULL.
6783 */
6784 dict_T *
6785get_current_funccal_dict(hashtab_T *ht)
6786{
6787 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01006788 && ht == &current_funccal->fc_l_vars.dv_hashtab)
6789 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006790 return NULL;
6791}
6792
6793/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006794 * Search hashitem in parent scope.
6795 */
6796 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006797find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006798{
6799 funccall_T *old_current_funccal = current_funccal;
6800 hashtab_T *ht;
6801 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006802 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006803
Bram Moolenaarca16c602022-09-06 18:57:08 +01006804 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006805 return NULL;
6806
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006807 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006808 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006809 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006810 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006811 ht = find_var_ht(name, &varname);
6812 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006813 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006814 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006815 if (!HASHITEM_EMPTY(hi))
6816 {
6817 *pht = ht;
6818 break;
6819 }
6820 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006821 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02006822 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006823 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006824 }
6825 current_funccal = old_current_funccal;
6826
6827 return hi;
6828}
6829
6830/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006831 * Search variable in parent scope.
6832 */
6833 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006834find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006835{
6836 dictitem_T *v = NULL;
6837 funccall_T *old_current_funccal = current_funccal;
6838 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006839 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006840
Bram Moolenaarca16c602022-09-06 18:57:08 +01006841 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006842 return NULL;
6843
Bram Moolenaare38eab22019-12-05 21:50:01 +01006844 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01006845 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006846 while (current_funccal)
6847 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006848 ht = find_var_ht(name, &varname);
6849 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006850 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006851 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006852 if (v != NULL)
6853 break;
6854 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006855 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006856 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006857 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006858 }
6859 current_funccal = old_current_funccal;
6860
6861 return v;
6862}
6863
6864/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006865 * Set "copyID + 1" in previous_funccal and callers.
6866 */
6867 int
6868set_ref_in_previous_funccal(int copyID)
6869{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006870 funccall_T *fc;
6871
Bram Moolenaarca16c602022-09-06 18:57:08 +01006872 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006873 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006874 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006875 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
6876 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
6877 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006878 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006879 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006880 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006881}
6882
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006883 static int
6884set_ref_in_funccal(funccall_T *fc, int copyID)
6885{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006886 if (fc->fc_copyID != copyID)
6887 {
6888 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006889 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
6890 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
6891 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
6892 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006893 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006894 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006895 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006896}
6897
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006898/*
6899 * Set "copyID" in all local vars and arguments in the call stack.
6900 */
6901 int
6902set_ref_in_call_stack(int copyID)
6903{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006904 funccall_T *fc;
6905 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006906
Bram Moolenaarca16c602022-09-06 18:57:08 +01006907 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006908 if (set_ref_in_funccal(fc, copyID))
6909 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006910
6911 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006912 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006913 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006914 if (set_ref_in_funccal(fc, copyID))
6915 return TRUE;
6916 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006917}
6918
6919/*
6920 * Set "copyID" in all functions available by name.
6921 */
6922 int
6923set_ref_in_functions(int copyID)
6924{
6925 int todo;
6926 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006927 ufunc_T *fp;
6928
6929 todo = (int)func_hashtab.ht_used;
6930 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006931 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006932 if (!HASHITEM_EMPTY(hi))
6933 {
6934 --todo;
6935 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006936 if (!func_name_refcount(fp->uf_name)
6937 && set_ref_in_func(NULL, fp, copyID))
6938 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006939 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006940 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006941 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006942}
6943
6944/*
6945 * Set "copyID" in all function arguments.
6946 */
6947 int
6948set_ref_in_func_args(int copyID)
6949{
6950 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006951
6952 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006953 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
6954 copyID, NULL, NULL))
6955 return TRUE;
6956 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006957}
6958
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006959/*
6960 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006961 * Returns TRUE if setting references failed somehow.
6962 */
6963 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006964set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006965{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006966 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006967 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006968 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006969 char_u fname_buf[FLEN_FIXED + 1];
6970 char_u *tofree = NULL;
6971 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006972 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006973
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006974 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006975 return FALSE;
6976
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006977 if (fp_in == NULL)
6978 {
6979 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006980 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006981 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006982 if (fp != NULL)
6983 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006984 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006985 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006986 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02006987
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006988 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006989 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006990}
6991
Bram Moolenaare38eab22019-12-05 21:50:01 +01006992#endif // FEAT_EVAL