blob: 878e07f91fea96234d03ff0297cb0a328ffb4399 [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;
Bram Moolenaard881d152022-05-13 13:50:36 +01001146 if (eap->cmdidx == CMD_def && 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".
1156 arg = skipwhite(skiptowhite(p));
1157 if (*arg == '[')
1158 arg = vim_strchr(arg, ']');
1159 if (arg != NULL)
1160 {
1161 int found = (eap->cmdidx == CMD_def && arg[0] == '='
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001162 && arg[1] == '<' && arg[2] =='<');
1163
Bram Moolenaard881d152022-05-13 13:50:36 +01001164 if (!found)
1165 // skip over the argument after "cmd"
1166 arg = skipwhite(skiptowhite(arg));
1167 if (found || (arg[0] == '=' && arg[1] == '<'
1168 && arg[2] =='<'
1169 && (checkforcmd(&p, "let", 2)
1170 || checkforcmd(&p, "var", 3)
1171 || checkforcmd(&p, "final", 5)
1172 || checkforcmd(&p, "const", 5))))
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001173 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001174 p = skipwhite(arg + 3);
1175 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001176 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001177 if (STRNCMP(p, "trim", 4) == 0)
1178 {
1179 // Ignore leading white space.
1180 p = skipwhite(p + 4);
1181 heredoc_trimmed = vim_strnsave(theline,
1182 skipwhite(theline) - theline);
1183 continue;
1184 }
1185 if (STRNCMP(p, "eval", 4) == 0)
1186 {
1187 // Ignore leading white space.
1188 p = skipwhite(p + 4);
1189 continue;
1190 }
1191 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001192 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001193 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1194 getline_options = GETLINE_NONE;
1195 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001196 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001197 }
1198 }
1199 }
1200
1201 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001202 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001203 goto theend;
1204
Bram Moolenaar20677332021-06-06 17:02:53 +02001205 if (heredoc_concat_len > 0)
1206 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001207 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001208 // to be used for the instruction later.
1209 ga_concat(&heredoc_ga, theline);
1210 ga_concat(&heredoc_ga, (char_u *)"\n");
1211 p = vim_strsave((char_u *)"");
1212 }
1213 else
1214 {
1215 // Copy the line to newly allocated memory. get_one_sourceline()
1216 // allocates 250 bytes per line, this saves 80% on average. The
1217 // cost is an extra alloc/free.
1218 p = vim_strsave(theline);
1219 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001220 if (p == NULL)
1221 goto theend;
1222 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1223
1224 // Add NULL lines for continuation lines, so that the line count is
1225 // equal to the index in the growarray.
1226 while (sourcing_lnum_off-- > 0)
1227 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1228
1229 // Check for end of eap->arg.
1230 if (line_arg != NULL && *line_arg == NUL)
1231 line_arg = NULL;
1232 }
1233
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001234 // Return OK when no error was detected.
1235 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001236 ret = OK;
1237
1238theend:
1239 vim_free(skip_until);
1240 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001241 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001242 need_wait_return |= saved_wait_return;
1243 return ret;
1244}
1245
1246/*
1247 * Handle the body of a lambda. *arg points to the "{", process statements
1248 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001249 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001250 * When successful "rettv" is set to a funcref.
1251 */
1252 static int
1253lambda_function_body(
1254 char_u **arg,
1255 typval_T *rettv,
1256 evalarg_T *evalarg,
1257 garray_T *newargs,
1258 garray_T *argtypes,
1259 int varargs,
1260 garray_T *default_args,
1261 char_u *ret_type)
1262{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001263 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001264 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001265 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001266 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001267 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001268 exarg_T eap;
1269 garray_T newlines;
1270 char_u *cmdline = NULL;
1271 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001272 partial_T *pt;
1273 char_u *name;
1274 int lnum_save = -1;
1275 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1276
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001277 *arg = skipwhite(*arg + 1);
1278 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001279 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001280 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001281 return FAIL;
1282 }
1283
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001284 CLEAR_FIELD(eap);
1285 eap.cmdidx = CMD_block;
1286 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001287 eap.cmdlinep = &cmdline;
1288 eap.skip = !evaluate;
1289 if (evalarg->eval_cctx != NULL)
1290 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1291 else
1292 {
1293 eap.getline = evalarg->eval_getline;
1294 eap.cookie = evalarg->eval_cookie;
1295 }
1296
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001297 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001298 if (get_function_body(&eap, &newlines, NULL,
1299 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001300 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001301
1302 // When inside a lambda must add the function lines to evalarg.eval_ga.
1303 evalarg->eval_break_count += newlines.ga_len;
1304 if (gap->ga_itemsize > 0)
1305 {
1306 int idx;
1307 char_u *last;
1308 size_t plen;
1309 char_u *pnl;
1310
1311 for (idx = 0; idx < newlines.ga_len; ++idx)
1312 {
1313 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1314
Bram Moolenaarecb66452021-05-18 15:09:18 +02001315 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001316 goto erret;
1317
1318 // Going to concatenate the lines after parsing. For an empty or
1319 // comment line use an empty string.
1320 // Insert NL characters at the start of each line, the string will
1321 // be split again later in .get_lambda_tv().
1322 if (*p == NUL || vim9_comment_start(p))
1323 p = (char_u *)"";
1324 plen = STRLEN(p);
1325 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1326 if (pnl != NULL)
1327 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001328 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1329 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001330 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001331 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001332 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001333 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001334 // more is following after the "}", which was skipped
1335 last = cmdline;
1336 else
1337 // nothing is following the "}"
1338 last = (char_u *)"}";
1339 plen = STRLEN(last);
1340 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1341 if (pnl != NULL)
1342 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001343 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1344 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001345 }
1346
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001347 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001348 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001349 garray_T *tfgap = &evalarg->eval_tofree_ga;
1350
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001351 // Something comes after the "}".
1352 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001353
1354 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001355 if (ga_grow(tfgap, 1) == OK)
1356 {
1357 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1358 evalarg->eval_using_cmdline = TRUE;
1359 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001360 }
1361 else
1362 *arg = (char_u *)"";
1363
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001364 if (!evaluate)
1365 {
1366 ret = OK;
1367 goto erret;
1368 }
1369
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001370 name = get_lambda_name();
Bram Moolenaare01e5212023-01-08 20:31:18 +00001371 ufunc = alloc_ufunc(name);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001372 if (ufunc == NULL)
1373 goto erret;
1374 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001375 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001376 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001377 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001378 ufunc->uf_refcount = 1;
1379 ufunc->uf_args = *newargs;
1380 newargs->ga_data = NULL;
1381 ufunc->uf_def_args = *default_args;
1382 default_args->ga_data = NULL;
1383 ufunc->uf_func_type = &t_func_any;
1384
1385 // error messages are for the first function line
1386 lnum_save = SOURCING_LNUM;
1387 SOURCING_LNUM = sourcing_lnum_top;
1388
1389 // parse argument types
1390 if (parse_argument_types(ufunc, argtypes, varargs) == FAIL)
1391 {
1392 SOURCING_LNUM = lnum_save;
1393 goto erret;
1394 }
1395
1396 // parse the return type, if any
1397 if (parse_return_type(ufunc, ret_type) == FAIL)
1398 goto erret;
1399
1400 pt = ALLOC_CLEAR_ONE(partial_T);
1401 if (pt == NULL)
1402 goto erret;
1403 pt->pt_func = ufunc;
1404 pt->pt_refcount = 1;
1405
1406 ufunc->uf_lines = newlines;
1407 newlines.ga_data = NULL;
1408 if (sandbox)
1409 ufunc->uf_flags |= FC_SANDBOX;
1410 if (!ASCII_ISUPPER(*ufunc->uf_name))
1411 ufunc->uf_flags |= FC_VIM9;
1412 ufunc->uf_script_ctx = current_sctx;
1413 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1414 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1415 set_function_type(ufunc);
1416
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001417 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1418
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001419 rettv->vval.v_partial = pt;
1420 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001421 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001422 ret = OK;
1423
1424erret:
1425 if (lnum_save >= 0)
1426 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001427 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001428 if (newargs != NULL)
1429 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001430 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001431 if (ufunc != NULL)
1432 {
1433 func_clear(ufunc, TRUE);
1434 func_free(ufunc, TRUE);
1435 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001436 return ret;
1437}
1438
1439/*
1440 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001441 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001442 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001443 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1444 */
1445 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001446get_lambda_tv(
1447 char_u **arg,
1448 typval_T *rettv,
1449 int types_optional,
1450 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001451{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001452 int evaluate = evalarg != NULL
1453 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001454 garray_T newargs;
1455 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001456 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001457 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001458 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001459 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001460 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001461 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001462 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001463 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001464 char_u *s;
1465 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001466 int *old_eval_lavars = eval_lavars_used;
1467 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001468 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001469 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001470 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001471 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001472 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001473 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001474
Bram Moolenaar4525a572022-02-13 11:57:33 +00001475 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001476 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001477
1478 ga_init(&newargs);
1479 ga_init(&newlines);
1480
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001481 // First, check if this is really a lambda expression. "->" or "=>" must
1482 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001483 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001484 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001485 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001486 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001487 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001488 {
1489 if (types_optional)
1490 ga_clear_strings(&argtypes);
Bram Moolenaar0346b792021-01-31 22:18:29 +01001491 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001492 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001493
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001494 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001495 if (evaluate)
1496 pnewargs = &newargs;
1497 else
1498 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001499 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001500 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001501 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001502 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001503 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001504 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001505 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001506 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001507 {
1508 if (types_optional)
1509 ga_clear_strings(&argtypes);
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001510 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001511 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001512 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001513 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001514
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001515 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1516 if (ret_type != NULL)
1517 {
1518 ret_type = vim_strsave(ret_type);
1519 tofree2 = ret_type;
1520 }
1521
Bram Moolenaare38eab22019-12-05 21:50:01 +01001522 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001523 if (evaluate)
1524 eval_lavars_used = &eval_lavars;
1525
Bram Moolenaar65c44152020-12-24 15:14:01 +01001526 *arg = skipwhite_and_linebreak(*arg, evalarg);
1527
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001528 // Recognize "{" as the start of a function body.
1529 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001530 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001531 if (evalarg == NULL)
1532 // cannot happen?
1533 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001534 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001535 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1536 types_optional ? &argtypes : NULL, varargs,
1537 &default_args, ret_type) == FAIL)
1538 goto errret;
1539 goto theend;
1540 }
1541 if (default_args.ga_len > 0)
1542 {
1543 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001544 goto errret;
1545 }
1546
Bram Moolenaare38eab22019-12-05 21:50:01 +01001547 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001548 start = *arg;
1549 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001550 if (ret == FAIL)
1551 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001552
Bram Moolenaar65c44152020-12-24 15:14:01 +01001553 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001554 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001555 *arg = skipwhite_and_linebreak(*arg, evalarg);
1556 if (**arg != '}')
1557 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001558 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001559 goto errret;
1560 }
1561 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001562 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001563
1564 if (evaluate)
1565 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001566 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001567 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001568 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001569 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001570 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001571
Bram Moolenaare01e5212023-01-08 20:31:18 +00001572 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001573 if (fp == NULL)
1574 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001575 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001576 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001577 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001578 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001579
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001580 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001581 if (ga_grow(&newlines, 1) == FAIL)
1582 goto errret;
1583
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001584 // If there are line breaks, we need to split up the string.
1585 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001586 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001587 line_end = end;
1588
1589 // Add "return " before the expression (or the first line).
1590 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001591 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001592 if (p == NULL)
1593 goto errret;
1594 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1595 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001596 vim_strncpy(p + 7, start, line_end - start);
1597
1598 if (line_end != end)
1599 {
1600 // Add more lines, split by line breaks. Thus is used when a
1601 // lambda with { cmds } is encountered.
1602 while (*line_end == '\n')
1603 {
1604 if (ga_grow(&newlines, 1) == FAIL)
1605 goto errret;
1606 start = line_end + 1;
1607 line_end = vim_strchr(start, '\n');
1608 if (line_end == NULL)
1609 line_end = end;
1610 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1611 vim_strnsave(start, line_end - start);
1612 }
1613 }
1614
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001615 if (strstr((char *)p + 7, "a:") == NULL)
1616 // No a: variables are used for sure.
1617 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001618
1619 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001620 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001621 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001622 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001623 if (types_optional)
1624 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001625 if (parse_argument_types(fp, &argtypes,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001626 vim9script && varargs) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001627 goto errret;
1628 if (ret_type != NULL)
1629 {
1630 fp->uf_ret_type = parse_type(&ret_type,
1631 &fp->uf_type_list, TRUE);
1632 if (fp->uf_ret_type == NULL)
1633 goto errret;
1634 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001635 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001636 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001637 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001638
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001639 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001640 if (current_funccal != NULL && eval_lavars)
1641 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001642 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001643 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001644 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001645 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001646
1647#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001648 if (prof_def_func())
1649 func_do_profile(fp);
1650#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001651 if (sandbox)
1652 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001653 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001654 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001655 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001656 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001657 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001658 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001659 // Use the line number of the arguments.
1660 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001661
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001662 function_using_block_scopes(fp, evalarg->eval_cstack);
1663
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001664 pt->pt_func = fp;
1665 pt->pt_refcount = 1;
1666 rettv->vval.v_partial = pt;
1667 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001668
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001669 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001670 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001671
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001672theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001673 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001674 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001675 if (types_optional)
1676 ga_clear_strings(&argtypes);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001677
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001678 return OK;
1679
1680errret:
1681 ga_clear_strings(&newargs);
1682 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001683 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001684 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001685 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001686 ga_clear_strings(&argtypes);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001687 if (fp != NULL)
1688 vim_free(fp->uf_arg_types);
1689 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001690 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001691 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001692 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001693 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001694 return FAIL;
1695}
1696
1697/*
1698 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1699 * name it contains, otherwise return "name".
1700 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1701 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001702 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1703 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001704 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001705 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001706 */
1707 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001708deref_func_name(
1709 char_u *name,
1710 int *lenp,
1711 partial_T **partialp,
1712 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001713 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001714 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001715 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001716{
1717 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001718 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001719 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001720 char_u *s = NULL;
1721 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001722 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001723
1724 if (partialp != NULL)
1725 *partialp = NULL;
1726
1727 cc = name[*lenp];
1728 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001729
Bram Moolenaar71f21932022-01-07 18:20:55 +00001730 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001731 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001732 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001733 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001734 tv = &v->di_tv;
1735 }
1736 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1737 {
1738 imported_T *import;
1739 char_u *p = name;
1740 int len = *lenp;
1741
1742 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001743 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001744 p = name + 2;
1745 len -= 2;
1746 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001747 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001748
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001749 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001750 if (import != NULL)
1751 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001752 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001753 if (new_function)
1754 semsg(_(e_redefining_imported_item_str), name);
1755 else
1756 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001757 name[len] = cc;
1758 *lenp = 0;
1759 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001760 }
1761 }
1762
1763 if (tv != NULL)
1764 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001765 if (found_var != NULL)
1766 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001767 if (tv->v_type == VAR_FUNC)
1768 {
1769 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001770 {
1771 *lenp = 0;
1772 return (char_u *)""; // just in case
1773 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001774 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001775 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001776 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001777
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001778 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001779 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001780 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001781
1782 if (pt == NULL)
1783 {
1784 *lenp = 0;
1785 return (char_u *)""; // just in case
1786 }
1787 if (partialp != NULL)
1788 *partialp = pt;
1789 s = partial_name(pt);
1790 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001791 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001792
1793 if (s != NULL)
1794 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001795 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001796 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001797 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001798
1799 if (sv != NULL)
1800 *type = sv->sv_type;
1801 }
1802 return s;
1803 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001804 }
1805
1806 return name;
1807}
1808
1809/*
1810 * Give an error message with a function name. Handle <SNR> things.
1811 * "ermsg" is to be passed without translation, use N_() instead of _().
1812 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001813 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001814emsg_funcname(char *ermsg, char_u *name)
1815{
Bram Moolenaar54598062022-01-13 12:05:09 +00001816 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001817
Bram Moolenaar54598062022-01-13 12:05:09 +00001818 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001819 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001820 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001821 if (p != name)
1822 vim_free(p);
1823}
1824
1825/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001826 * Get function arguments at "*arg" and advance it.
1827 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001828 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001829 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001830 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001831get_func_arguments(
1832 char_u **arg,
1833 evalarg_T *evalarg,
1834 int partial_argc,
1835 typval_T *argvars,
1836 int *argcount)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001837{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001838 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001839 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001840 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001841 int evaluate = evalarg == NULL
1842 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001843
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001844 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001845 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001846 // skip the '(' or ',' and possibly line breaks
1847 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1848
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001849 if (*argp == ')' || *argp == ',' || *argp == NUL)
1850 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001851 if (eval1(&argp, &argvars[*argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001852 {
1853 ret = FAIL;
1854 break;
1855 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001856 ++*argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001857 // The comma should come right after the argument, but this wasn't
1858 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001859 if (vim9script)
1860 {
1861 if (*argp != ',' && *skipwhite(argp) == ',')
1862 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001863 if (evaluate)
1864 semsg(_(e_no_white_space_allowed_before_str_str),
1865 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001866 ret = FAIL;
1867 break;
1868 }
1869 }
1870 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001871 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001872 if (*argp != ',')
1873 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001874 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
1875 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001876 if (evaluate)
1877 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001878 ret = FAIL;
1879 break;
1880 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001881 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001882
Bram Moolenaare6b53242020-07-01 17:28:33 +02001883 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001884 if (*argp == ')')
1885 ++argp;
1886 else
1887 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001888 *arg = argp;
1889 return ret;
1890}
1891
1892/*
1893 * Call a function and put the result in "rettv".
1894 * Return OK or FAIL.
1895 */
1896 int
1897get_func_tv(
1898 char_u *name, // name of the function
1899 int len, // length of "name" or -1 to use strlen()
1900 typval_T *rettv,
1901 char_u **arg, // argument, pointing to the '('
1902 evalarg_T *evalarg, // for line continuation
1903 funcexe_T *funcexe) // various values
1904{
1905 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001906 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001907 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1908 int argcount = 0; // number of arguments found
1909 int vim9script = in_vim9script();
1910 int evaluate = evalarg == NULL
1911 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
1912
1913 argp = *arg;
1914 ret = get_func_arguments(&argp, evalarg,
1915 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
1916 argvars, &argcount);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001917
1918 if (ret == OK)
1919 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001920 int i = 0;
1921 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001922
1923 if (get_vim_var_nr(VV_TESTING))
1924 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001925 // Prepare for calling test_garbagecollect_now(), need to know
1926 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001927 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001928 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001929 for (i = 0; i < argcount; ++i)
1930 if (ga_grow(&funcargs, 1) == OK)
1931 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1932 &argvars[i];
1933 }
1934
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001935 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00001936 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001937 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001938 // An error in a builtin function does not return FAIL, but we do
1939 // want to abort further processing if an error was given.
1940 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001941 clear_tv(rettv);
1942 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001943
1944 funcargs.ga_len -= i;
1945 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001946 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001947 {
1948 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001949 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001950 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001951 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001952 }
1953
1954 while (--argcount >= 0)
1955 clear_tv(&argvars[argcount]);
1956
Bram Moolenaar4525a572022-02-13 11:57:33 +00001957 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02001958 *arg = argp;
1959 else
1960 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001961 return ret;
1962}
1963
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001964/*
1965 * Return TRUE if "p" starts with "<SID>" or "s:".
1966 * Only works if eval_fname_script() returned non-zero for "p"!
1967 */
1968 static int
1969eval_fname_sid(char_u *p)
1970{
1971 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
1972}
1973
1974/*
1975 * In a script change <SID>name() and s:name() to K_SNR 123_name().
1976 * Change <SNR>123_name() to K_SNR 123_name().
1977 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001978 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001979 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001980 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00001981fname_trans_sid(
1982 char_u *name,
1983 char_u *fname_buf,
1984 char_u **tofree,
1985 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001986{
1987 int llen;
1988 char_u *fname;
1989 int i;
1990
1991 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001992 if (llen == 0)
1993 return name; // no prefix
1994
1995 fname_buf[0] = K_SPECIAL;
1996 fname_buf[1] = KS_EXTRA;
1997 fname_buf[2] = (int)KE_SNR;
1998 i = 3;
1999 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002000 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002001 if (current_sctx.sc_sid <= 0)
2002 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002003 else
2004 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002005 sprintf((char *)fname_buf + 3, "%ld_",
2006 (long)current_sctx.sc_sid);
2007 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002008 }
2009 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002010 if (i + STRLEN(name + llen) < FLEN_FIXED)
2011 {
2012 STRCPY(fname_buf + i, name + llen);
2013 fname = fname_buf;
2014 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002015 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002016 {
2017 fname = alloc(i + STRLEN(name + llen) + 1);
2018 if (fname == NULL)
2019 *error = FCERR_OTHER;
2020 else
2021 {
2022 *tofree = fname;
2023 mch_memmove(fname, fname_buf, (size_t)i);
2024 STRCPY(fname + i, name + llen);
2025 }
2026 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002027 return fname;
2028}
2029
2030/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002031 * Concatenate the script ID and function name into "<SNR>99_name".
2032 * "buffer" must have size MAX_FUNC_NAME_LEN.
2033 */
2034 void
2035func_name_with_sid(char_u *name, int sid, char_u *buffer)
2036{
2037 // A script-local function is stored as "<SNR>99_name".
2038 buffer[0] = K_SPECIAL;
2039 buffer[1] = KS_EXTRA;
2040 buffer[2] = (int)KE_SNR;
2041 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2042 (long)sid, name);
2043}
2044
2045/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046 * Find a function "name" in script "sid".
2047 */
2048 static ufunc_T *
2049find_func_with_sid(char_u *name, int sid)
2050{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002051 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002052 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002053
Bram Moolenaarb8822442022-01-11 15:24:05 +00002054 if (!SCRIPT_ID_VALID(sid))
2055 return NULL; // not in a script
2056
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002057 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058 hi = hash_find(&func_hashtab, buffer);
2059 if (!HASHITEM_EMPTY(hi))
2060 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002061 return NULL;
2062}
2063
2064/*
2065 * Find a function "name" in script "sid" prefixing the autoload prefix.
2066 */
2067 static ufunc_T *
2068find_func_with_prefix(char_u *name, int sid)
2069{
2070 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002071 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002072 scriptitem_T *si;
2073
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002074 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002075 return NULL; // already has the prefix
2076 if (!SCRIPT_ID_VALID(sid))
2077 return NULL; // not in a script
2078 si = SCRIPT_ITEM(sid);
2079 if (si->sn_autoload_prefix != NULL)
2080 {
2081 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2082 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002083 char_u *namep;
2084
2085 // skip a "<SNR>99_" prefix
2086 namep = untrans_function_name(name);
2087 if (namep == NULL)
2088 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002089
2090 // An exported function in an autoload script is stored as
2091 // "dir#path#name".
2092 if (len < sizeof(buffer))
2093 auto_name = buffer;
2094 else
2095 auto_name = alloc(len);
2096 if (auto_name != NULL)
2097 {
2098 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002099 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002100 hi = hash_find(&func_hashtab, auto_name);
2101 if (auto_name != buffer)
2102 vim_free(auto_name);
2103 if (!HASHITEM_EMPTY(hi))
2104 return HI2UF(hi);
2105 }
2106 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107
2108 return NULL;
2109}
2110
2111/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002112 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002113 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2114 * functions.
2115 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002116 * Return NULL for unknown function.
2117 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002118 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002119find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002120{
2121 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002124 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002126 // Find script-local function before global one.
2127 if (in_vim9script() && eval_isnamec1(*name)
2128 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002129 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002130 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2131 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002132 if (func != NULL)
2133 return func;
2134 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002135 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2136 {
2137 char_u *p = name + 5;
2138 long sid;
2139
2140 // printable "<SNR>123_Name" form
2141 sid = getdigits(&p);
2142 if (*p == '_')
2143 {
2144 func = find_func_with_sid(p + 1, (int)sid);
2145 if (func != NULL)
2146 return func;
2147 }
2148 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002149 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002150
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002151 if ((flags & FFED_NO_GLOBAL) == 0)
2152 {
2153 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002154 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002155 if (!HASHITEM_EMPTY(hi))
2156 return HI2UF(hi);
2157 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002158
Bram Moolenaarb8822442022-01-11 15:24:05 +00002159 // Find autoload function if this is an autoload script.
2160 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2161 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162}
2163
2164/*
2165 * Find a function by name, return pointer to it in ufuncs.
2166 * "cctx" is passed in a :def function to find imported functions.
2167 * Return NULL for unknown or dead function.
2168 */
2169 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002170find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002172 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002173
2174 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2175 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002176 return NULL;
2177}
2178
2179/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002180 * Return TRUE if "ufunc" is a global function.
2181 */
2182 int
2183func_is_global(ufunc_T *ufunc)
2184{
2185 return ufunc->uf_name[0] != K_SPECIAL;
2186}
2187
2188/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002189 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2190 */
2191 int
2192func_requires_g_prefix(ufunc_T *ufunc)
2193{
2194 return ufunc->uf_name[0] != K_SPECIAL
2195 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002196 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
2197 && !isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002198}
2199
2200/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002201 * Copy the function name of "fp" to buffer "buf".
2202 * "buf" must be able to hold the function name plus three bytes.
2203 * Takes care of script-local function names.
2204 */
2205 static void
2206cat_func_name(char_u *buf, ufunc_T *fp)
2207{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002208 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002209 {
2210 STRCPY(buf, "<SNR>");
2211 STRCAT(buf, fp->uf_name + 3);
2212 }
2213 else
2214 STRCPY(buf, fp->uf_name);
2215}
2216
2217/*
2218 * Add a number variable "name" to dict "dp" with value "nr".
2219 */
2220 static void
2221add_nr_var(
2222 dict_T *dp,
2223 dictitem_T *v,
2224 char *name,
2225 varnumber_T nr)
2226{
2227 STRCPY(v->di_key, name);
2228 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002229 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002230 v->di_tv.v_type = VAR_NUMBER;
2231 v->di_tv.v_lock = VAR_FIXED;
2232 v->di_tv.vval.v_number = nr;
2233}
2234
2235/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002236 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002237 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002238 static void
2239free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002240{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002241 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002242
Bram Moolenaarca16c602022-09-06 18:57:08 +01002243 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002244 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002245 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002246
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002247 // When garbage collecting a funccall_T may be freed before the
2248 // function that references it, clear its uf_scoped field.
2249 // The function may have been redefined and point to another
2250 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002251 if (fp != NULL && fp->uf_scoped == fc)
2252 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002253 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002254 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002255
Bram Moolenaarca16c602022-09-06 18:57:08 +01002256 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002257 vim_free(fc);
2258}
2259
2260/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002261 * Free "fc" and what it contains.
2262 * Can be called only when "fc" is kept beyond the period of it called,
2263 * i.e. after cleanup_function_call(fc).
2264 */
2265 static void
2266free_funccal_contents(funccall_T *fc)
2267{
2268 listitem_T *li;
2269
2270 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002271 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002272
2273 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002274 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002275
2276 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002277 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002278 clear_tv(&li->li_tv);
2279
2280 free_funccal(fc);
2281}
2282
2283/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002284 * Handle the last part of returning from a function: free the local hashtable.
2285 * Unless it is still in use by a closure.
2286 */
2287 static void
2288cleanup_function_call(funccall_T *fc)
2289{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002290 int may_free_fc = fc->fc_refcount <= 0;
2291 int free_fc = TRUE;
2292
Bram Moolenaarca16c602022-09-06 18:57:08 +01002293 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002294
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002295 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002296 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2297 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002298 else
2299 free_fc = FALSE;
2300
2301 // If the a:000 list and the l: and a: dicts are not referenced and
2302 // there is no closure using it, we can free the funccall_T and what's
2303 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002304 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2305 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002306 else
2307 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002308 int todo;
2309 hashitem_T *hi;
2310 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002311
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002312 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002313
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002314 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002315 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002316 FOR_ALL_HASHTAB_ITEMS(&fc->fc_l_avars.dv_hashtab, hi, todo)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002317 {
2318 if (!HASHITEM_EMPTY(hi))
2319 {
2320 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002321 di = HI2DI(hi);
2322 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002323 }
2324 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002325 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002326
Bram Moolenaarca16c602022-09-06 18:57:08 +01002327 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2328 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002329 else
2330 {
2331 listitem_T *li;
2332
2333 free_fc = FALSE;
2334
2335 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002336 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002337 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002338 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002339
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002340 if (free_fc)
2341 free_funccal(fc);
2342 else
2343 {
2344 static int made_copy = 0;
2345
2346 // "fc" is still in use. This can happen when returning "a:000",
2347 // assigning "l:" to a global variable or defining a closure.
2348 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002349 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002350 previous_funccal = fc;
2351
2352 if (want_garbage_collect)
2353 // If garbage collector is ready, clear count.
2354 made_copy = 0;
2355 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002356 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002357 // We have made a lot of copies, worth 4 Mbyte. This can happen
2358 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002359 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002360 // too much memory.
2361 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002362 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002363 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002364 }
2365}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002366
2367/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002368 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2369 */
2370 static int
2371numbered_function(char_u *name)
2372{
2373 return isdigit(*name)
2374 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2375}
2376
2377/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002378 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002379 * 1. ordinary names, function defined with :function or :def;
2380 * can start with "<SNR>123_" literally or with K_SPECIAL.
2381 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002382 * For the first we only count the name stored in func_hashtab as a reference,
2383 * using function() does not count as a reference, because the function is
2384 * looked up by name.
2385 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002386 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002387func_name_refcount(char_u *name)
2388{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002389 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002390}
2391
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392/*
2393 * Unreference "fc": decrement the reference count and free it when it
2394 * becomes zero. "fp" is detached from "fc".
2395 * When "force" is TRUE we are exiting.
2396 */
2397 static void
2398funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2399{
2400 funccall_T **pfc;
2401 int i;
2402
2403 if (fc == NULL)
2404 return;
2405
2406 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002407 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2408 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2409 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2410 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002411 {
2412 if (fc == *pfc)
2413 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002414 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415 free_funccal_contents(fc);
2416 return;
2417 }
2418 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002419 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2420 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2421 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002422}
2423
2424/*
2425 * Remove the function from the function hashtable. If the function was
2426 * deleted while it still has references this was already done.
2427 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2428 */
2429 static int
2430func_remove(ufunc_T *fp)
2431{
2432 hashitem_T *hi;
2433
2434 // Return if it was already virtually deleted.
2435 if (fp->uf_flags & FC_DEAD)
2436 return FALSE;
2437
2438 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002439 if (HASHITEM_EMPTY(hi))
2440 return FALSE;
2441
2442 // When there is a def-function index do not actually remove the
2443 // function, so we can find the index when defining the function again.
2444 // Do remove it when it's a copy.
2445 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002447 fp->uf_flags |= FC_DEAD;
2448 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002450 hash_remove(&func_hashtab, hi, "remove function");
2451 fp->uf_flags |= FC_DELETED;
2452 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002453}
2454
2455 static void
2456func_clear_items(ufunc_T *fp)
2457{
2458 ga_clear_strings(&(fp->uf_args));
2459 ga_clear_strings(&(fp->uf_def_args));
2460 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002461 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002462 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002463 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002464 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002465
2466 // Increment the refcount of this function to avoid it being freed
2467 // recursively when the partial is freed.
2468 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002469 partial_unref(fp->uf_partial);
2470 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002471 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002472
2473#ifdef FEAT_LUA
2474 if (fp->uf_cb_free != NULL)
2475 {
2476 fp->uf_cb_free(fp->uf_cb_state);
2477 fp->uf_cb_free = NULL;
2478 }
2479
2480 fp->uf_cb_state = NULL;
2481 fp->uf_cb = NULL;
2482#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483#ifdef FEAT_PROFILE
2484 VIM_CLEAR(fp->uf_tml_count);
2485 VIM_CLEAR(fp->uf_tml_total);
2486 VIM_CLEAR(fp->uf_tml_self);
2487#endif
2488}
2489
2490/*
2491 * Free all things that a function contains. Does not free the function
2492 * itself, use func_free() for that.
2493 * When "force" is TRUE we are exiting.
2494 */
2495 static void
2496func_clear(ufunc_T *fp, int force)
2497{
2498 if (fp->uf_cleared)
2499 return;
2500 fp->uf_cleared = TRUE;
2501
2502 // clear this function
2503 func_clear_items(fp);
2504 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002505 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506}
2507
2508/*
2509 * Free a function and remove it from the list of functions. Does not free
2510 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002511 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002512 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002514 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002515func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516{
2517 // Only remove it when not done already, otherwise we would remove a newer
2518 // version of the function with the same name.
2519 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2520 func_remove(fp);
2521
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002522 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002523 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002524 if (fp->uf_dfunc_idx > 0)
2525 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002526 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002528 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002529 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002530 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531}
2532
2533/*
2534 * Free all things that a function contains and free the function itself.
2535 * When "force" is TRUE we are exiting.
2536 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002537 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538func_clear_free(ufunc_T *fp, int force)
2539{
2540 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002541 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2542 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002543 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002544 else
2545 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546}
2547
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002548/*
2549 * Copy already defined function "lambda" to a new function with name "global".
2550 * This is for when a compiled function defines a global function.
2551 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002552 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002553copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002554 char_u *lambda,
2555 char_u *global,
2556 loopvarinfo_T *loopvarinfo,
2557 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002558{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002559 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002560 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002561
2562 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002563 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002564 semsg(_(e_lambda_function_not_found_str), lambda);
2565 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002566 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002567
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002568 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002569 if (fp != NULL)
2570 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002571 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002572 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002573 return FAIL;
2574 }
2575
Bram Moolenaare01e5212023-01-08 20:31:18 +00002576 fp = alloc_ufunc(global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002577 if (fp == NULL)
2578 return FAIL;
2579
2580 fp->uf_varargs = ufunc->uf_varargs;
2581 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2582 fp->uf_def_status = ufunc->uf_def_status;
2583 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2584 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2585 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2586 == FAIL
2587 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2588 goto failed;
2589
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002590 if (ufunc->uf_arg_types != NULL)
2591 {
2592 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2593 if (fp->uf_arg_types == NULL)
2594 goto failed;
2595 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2596 sizeof(type_T *) * fp->uf_args.ga_len);
2597 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002598 if (ufunc->uf_va_name != NULL)
2599 {
2600 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2601 if (fp->uf_va_name == NULL)
2602 goto failed;
2603 }
2604 fp->uf_ret_type = ufunc->uf_ret_type;
2605
2606 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002607
2608 fp->uf_name_exp = NULL;
2609 set_ufunc_name(fp, global);
2610
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002611 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002612
2613 // the referenced dfunc_T is now used one more time
2614 link_def_function(fp);
2615
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002616 // Create a partial to store the context of the function where it was
2617 // instantiated. Only needs to be done once. Do this on the original
2618 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002619 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2620 {
2621 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2622
2623 if (pt == NULL)
2624 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002625 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002626 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002627 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002628 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002629 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002630 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002631 }
2632
2633 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002634
2635failed:
2636 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002637 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002638}
2639
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002640static int funcdepth = 0;
2641
2642/*
2643 * Increment the function call depth count.
2644 * Return FAIL when going over 'maxfuncdepth'.
2645 * Otherwise return OK, must call funcdepth_decrement() later!
2646 */
2647 int
2648funcdepth_increment(void)
2649{
2650 if (funcdepth >= p_mfd)
2651 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002652 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002653 return FAIL;
2654 }
2655 ++funcdepth;
2656 return OK;
2657}
2658
2659 void
2660funcdepth_decrement(void)
2661{
2662 --funcdepth;
2663}
2664
2665/*
2666 * Get the current function call depth.
2667 */
2668 int
2669funcdepth_get(void)
2670{
2671 return funcdepth;
2672}
2673
2674/*
2675 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002676 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002677 * times as funcdepth_increment().
2678 */
2679 void
2680funcdepth_restore(int depth)
2681{
2682 funcdepth = depth;
2683}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002684
2685/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002686 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2687 * "rettv".
2688 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2689 * Returns NULL when allocation fails.
2690 */
2691 funccall_T *
2692create_funccal(ufunc_T *fp, typval_T *rettv)
2693{
2694 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2695
2696 if (fc == NULL)
2697 return NULL;
2698 fc->fc_caller = current_funccal;
2699 current_funccal = fc;
2700 fc->fc_func = fp;
2701 func_ptr_ref(fp);
2702 fc->fc_rettv = rettv;
2703 return fc;
2704}
2705
2706/*
2707 * To be called when returning from a compiled function; restores
2708 * current_funccal.
2709 */
2710 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002711remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002712{
2713 funccall_T *fc = current_funccal;
2714
2715 current_funccal = fc->fc_caller;
2716 free_funccal(fc);
2717}
2718
2719/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002720 * Call a user function.
2721 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002722 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002723call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002724 ufunc_T *fp, // pointer to function
2725 int argcount, // nr of args
2726 typval_T *argvars, // arguments
2727 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002728 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002729 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002730{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002731 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002732 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002733 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002734 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002735 funccall_T *fc;
2736 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002737 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002738 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002739 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002740 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002741 int i;
2742 int ai;
2743 int islambda = FALSE;
2744 char_u numbuf[NUMBUFLEN];
2745 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002746 typval_T *tv_to_free[MAX_FUNC_ARGS];
2747 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002748#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002749 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002750#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01002751 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002752
Bram Moolenaarb2049902021-01-24 12:53:53 +01002753#ifdef FEAT_PROFILE
2754 CLEAR_FIELD(profile_info);
2755#endif
2756
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002757 // If depth of calling is getting too high, don't execute the function.
2758 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002759 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002760 rettv->v_type = VAR_NUMBER;
2761 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002762 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002763 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002764
Bram Moolenaare38eab22019-12-05 21:50:01 +01002765 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002766
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002767 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002768 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00002769 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002770 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002771 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002772 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2773 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002774 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002775 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002776
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002777 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002779#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002780 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002781#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002782 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002783#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002784 if (do_profiling == PROF_YES)
2785 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002786#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002787 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002788 if (call_def_function(fp, argcount, argvars, 0,
2789 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
2790 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002791 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002792#ifdef FEAT_PROFILE
2793 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002794 || (caller != NULL && caller->uf_profiling)))
2795 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002796#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002797 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002798 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002799 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 }
2801
Bram Moolenaar38453522021-11-28 22:00:12 +00002802 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002803
2804 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002805 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2806 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2807 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002808 */
2809 /*
2810 * Init l: variables.
2811 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002812 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002813 if (selfdict != NULL)
2814 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002815 // Set l:self to "selfdict". Use "name" to avoid a warning from
2816 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002817 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002818 name = v->di_key;
2819 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002820 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002821 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002822 v->di_tv.v_type = VAR_DICT;
2823 v->di_tv.v_lock = 0;
2824 v->di_tv.vval.v_dict = selfdict;
2825 ++selfdict->dv_refcount;
2826 }
2827
2828 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002829 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002830 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002831 * Set a:000 to a list with room for the "..." arguments.
2832 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002833 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002834 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002835 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002836 (varnumber_T)(argcount >= fp->uf_args.ga_len
2837 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002838 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002839 if ((fp->uf_flags & FC_NOARGS) == 0)
2840 {
2841 // Use "name" to avoid a warning from some compiler that checks the
2842 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002843 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002844 name = v->di_key;
2845 STRCPY(name, "000");
2846 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002847 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002848 v->di_tv.v_type = VAR_LIST;
2849 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002850 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002851 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002852 CLEAR_FIELD(fc->fc_l_varlist);
2853 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2854 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002855
2856 /*
2857 * Set a:firstline to "firstline" and a:lastline to "lastline".
2858 * Set a:name to named arguments.
2859 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002860 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002861 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002862 if ((fp->uf_flags & FC_NOARGS) == 0)
2863 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002864 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2865 "firstline", (varnumber_T)funcexe->fe_firstline);
2866 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2867 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002868 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002869 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002870 {
2871 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002872 typval_T def_rettv;
2873 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002874
2875 ai = i - fp->uf_args.ga_len;
2876 if (ai < 0)
2877 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002878 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002879 name = FUNCARG(fp, i);
2880 if (islambda)
2881 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002882
2883 // evaluate named argument default expression
2884 isdefault = ai + fp->uf_def_args.ga_len >= 0
2885 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2886 && argvars[i].vval.v_number == VVAL_NONE));
2887 if (isdefault)
2888 {
2889 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002890
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002891 def_rettv.v_type = VAR_NUMBER;
2892 def_rettv.vval.v_number = -1;
2893
2894 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2895 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002896 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002897 {
2898 default_arg_err = 1;
2899 break;
2900 }
2901 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002902 }
2903 else
2904 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002905 if ((fp->uf_flags & FC_NOARGS) != 0)
2906 // Bail out if no a: arguments used (in lambda).
2907 break;
2908
Bram Moolenaare38eab22019-12-05 21:50:01 +01002909 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002910 sprintf((char *)numbuf, "%d", ai + 1);
2911 name = numbuf;
2912 }
2913 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2914 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002915 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002916 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002917 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002918 }
2919 else
2920 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002921 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002922 if (v == NULL)
2923 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002924 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002925 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002926
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002927 // Note: the values are copied directly to avoid alloc/free.
2928 // "argvars" must have VAR_FIXED for v_lock.
2929 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002930 v->di_tv.v_lock = VAR_FIXED;
2931
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002932 if (isdefault)
2933 // Need to free this later, no matter where it's stored.
2934 tv_to_free[tv_to_free_len++] = &v->di_tv;
2935
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002936 if (addlocal)
2937 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002938 // Named arguments should be accessed without the "a:" prefix in
2939 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002940 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002941 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002942 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002943 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002944 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002945
2946 if (ai >= 0 && ai < MAX_FUNC_ARGS)
2947 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002948 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002949
2950 li->li_tv = argvars[i];
2951 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002952 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002953 }
2954 }
2955
Bram Moolenaare38eab22019-12-05 21:50:01 +01002956 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002957 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02002958
2959 if (fp->uf_flags & FC_SANDBOX)
2960 {
2961 using_sandbox = TRUE;
2962 ++sandbox;
2963 }
2964
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002965 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002966 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002967 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002968 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002969 ++no_wait_return;
2970 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002971
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002972 smsg(_("calling %s"), SOURCING_NAME);
2973 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002974 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002975 char_u buf[MSG_BUF_LEN];
2976 char_u numbuf2[NUMBUFLEN];
2977 char_u *tofree;
2978 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002979
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002980 msg_puts("(");
2981 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002982 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002983 if (i > 0)
2984 msg_puts(", ");
2985 if (argvars[i].v_type == VAR_NUMBER)
2986 msg_outnum((long)argvars[i].vval.v_number);
2987 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002988 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002989 // Do not want errors such as E724 here.
2990 ++emsg_off;
2991 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
2992 --emsg_off;
2993 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002994 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002995 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002996 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002997 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2998 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002999 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003000 msg_puts((char *)s);
3001 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003002 }
3003 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003004 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003005 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003006 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003007 msg_puts("\n"); // don't overwrite this either
3008
3009 verbose_leave_scroll();
3010 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003011 }
3012#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003013 if (do_profiling == PROF_YES)
3014 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003015 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003016#endif
3017
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003018 // "legacy" does not apply to commands in the function
3019 sticky_cmdmod_flags = 0;
3020
Bram Moolenaar98aff652022-09-06 21:02:35 +01003021 // If called from a compiled :def function the execution context must be
3022 // hidden, any deferred functions need to be added to the function being
3023 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003024 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003025
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003026 save_current_sctx = current_sctx;
3027 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003028 save_did_emsg = did_emsg;
3029 did_emsg = FALSE;
3030
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003031 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003032 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003033 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003034 retval = FCERR_FAILED;
3035 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003036 else if (islambda)
3037 {
3038 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3039
3040 // A Lambda always has the command "return {expr}". It is much faster
3041 // to evaluate {expr} directly.
3042 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003043 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003044 --ex_nesting_level;
3045 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003046 else
3047 // call do_cmdline() to execute the lines
3048 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003049 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3050
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003051 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003052 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003053
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003054 --RedrawingDisabled;
3055
Bram Moolenaare38eab22019-12-05 21:50:01 +01003056 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003057 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3058 {
3059 clear_tv(rettv);
3060 rettv->v_type = VAR_NUMBER;
3061 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003062
3063 // In corner cases returning a "failed" value is not backwards
3064 // compatible. Only do this for Vim9 script.
3065 if (in_vim9script())
3066 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003067 }
3068
3069#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003070 if (do_profiling == PROF_YES)
3071 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003072 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003073
3074 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3075 profile_may_end_func(&profile_info, fp, caller);
3076 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003077#endif
3078
Bram Moolenaare38eab22019-12-05 21:50:01 +01003079 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003080 if (p_verbose >= 12)
3081 {
3082 ++no_wait_return;
3083 verbose_enter_scroll();
3084
3085 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003086 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003087 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003088 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003089 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003090 else
3091 {
3092 char_u buf[MSG_BUF_LEN];
3093 char_u numbuf2[NUMBUFLEN];
3094 char_u *tofree;
3095 char_u *s;
3096
Bram Moolenaare38eab22019-12-05 21:50:01 +01003097 // The value may be very long. Skip the middle part, so that we
3098 // have some idea how it starts and ends. smsg() would always
3099 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003100 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003101 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003102 --emsg_off;
3103 if (s != NULL)
3104 {
3105 if (vim_strsize(s) > MSG_BUF_CLEN)
3106 {
3107 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3108 s = buf;
3109 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003110 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003111 vim_free(tofree);
3112 }
3113 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003114 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003115
3116 verbose_leave_scroll();
3117 --no_wait_return;
3118 }
3119
Bram Moolenaare31ee862020-01-07 20:59:34 +01003120 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003121 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003122 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003123 restore_current_ectx(save_current_ectx);
3124
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003125#ifdef FEAT_PROFILE
3126 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003127 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003128#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003129 if (using_sandbox)
3130 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003131 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003132
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003133 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003134 {
3135 ++no_wait_return;
3136 verbose_enter_scroll();
3137
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003138 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003139 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003140
3141 verbose_leave_scroll();
3142 --no_wait_return;
3143 }
3144
3145 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003146 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003147 for (i = 0; i < tv_to_free_len; ++i)
3148 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003149 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003150
3151 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003152}
3153
3154/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003155 * Check the argument count for user function "fp".
3156 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3157 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003158 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003159check_user_func_argcount(ufunc_T *fp, int argcount)
3160{
3161 int regular_args = fp->uf_args.ga_len;
3162
3163 if (argcount < regular_args - fp->uf_def_args.ga_len)
3164 return FCERR_TOOFEW;
3165 else if (!has_varargs(fp) && argcount > regular_args)
3166 return FCERR_TOOMANY;
3167 return FCERR_UNKNOWN;
3168}
3169
3170/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003172 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003173 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174call_user_func_check(
3175 ufunc_T *fp,
3176 int argcount,
3177 typval_T *argvars,
3178 typval_T *rettv,
3179 funcexe_T *funcexe,
3180 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003181{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003182 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003183
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003184#ifdef FEAT_LUA
3185 if (fp->uf_flags & FC_CFUNC)
3186 {
3187 cfunc_T cb = fp->uf_cb;
3188
3189 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3190 }
3191#endif
3192
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003193 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3194 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003195 error = check_user_func_argcount(fp, argcount);
3196 if (error != FCERR_UNKNOWN)
3197 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003198
Bram Moolenaar50824712020-12-20 21:10:17 +01003199 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003200 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003202 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003204 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 int did_save_redo = FALSE;
3206 save_redo_T save_redo;
3207
3208 /*
3209 * Call the user function.
3210 * Save and restore search patterns, script variables and
3211 * redo buffer.
3212 */
3213 save_search_patterns();
3214 if (!ins_compl_active())
3215 {
3216 saveRedobuff(&save_redo);
3217 did_save_redo = TRUE;
3218 }
3219 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003220 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003221 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3223 // Function was unreferenced while being used, free it now.
3224 func_clear_free(fp, FALSE);
3225 if (did_save_redo)
3226 restoreRedobuff(&save_redo);
3227 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003228 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003229
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003231}
3232
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003233static funccal_entry_T *funccal_stack = NULL;
3234
3235/*
3236 * Save the current function call pointer, and set it to NULL.
3237 * Used when executing autocommands and for ":source".
3238 */
3239 void
3240save_funccal(funccal_entry_T *entry)
3241{
3242 entry->top_funccal = current_funccal;
3243 entry->next = funccal_stack;
3244 funccal_stack = entry;
3245 current_funccal = NULL;
3246}
3247
3248 void
3249restore_funccal(void)
3250{
3251 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003252 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003253 else
3254 {
3255 current_funccal = funccal_stack->top_funccal;
3256 funccal_stack = funccal_stack->next;
3257 }
3258}
3259
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003260 funccall_T *
3261get_current_funccal(void)
3262{
3263 return current_funccal;
3264}
3265
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003266/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003267 * Return TRUE when currently at the script level:
3268 * - not in a function
3269 * - not executing an autocommand
3270 * Note that when an autocommand sources a script the result is FALSE;
3271 */
3272 int
3273at_script_level(void)
3274{
3275 return current_funccal == NULL && autocmd_match == NULL;
3276}
3277
3278/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003279 * Mark all functions of script "sid" as deleted.
3280 */
3281 void
3282delete_script_functions(int sid)
3283{
3284 hashitem_T *hi;
3285 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003286 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003287 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003288 size_t len;
3289
3290 buf[0] = K_SPECIAL;
3291 buf[1] = KS_EXTRA;
3292 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003293 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003294 len = STRLEN(buf);
3295
Bram Moolenaarce658352020-07-31 23:47:12 +02003296 while (todo > 0)
3297 {
3298 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003299 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaarce658352020-07-31 23:47:12 +02003300 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003301 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003302 fp = HI2UF(hi);
3303 if (STRNCMP(fp->uf_name, buf, len) == 0)
3304 {
3305 int changed = func_hashtab.ht_changed;
3306
3307 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003308
3309 if (fp->uf_calls > 0)
3310 {
3311 // Function is executing, don't free it but do remove
3312 // it from the hashtable.
3313 if (func_remove(fp))
3314 fp->uf_refcount--;
3315 }
3316 else
3317 {
3318 func_clear(fp, TRUE);
3319 // When clearing a function another function can be
3320 // cleared as a side effect. When that happens start
3321 // over.
3322 if (changed != func_hashtab.ht_changed)
3323 break;
3324 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003325 }
3326 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003327 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003328 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003329}
3330
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003331#if defined(EXITFREE) || defined(PROTO)
3332 void
3333free_all_functions(void)
3334{
3335 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003336 ufunc_T *fp;
3337 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003338 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003339 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003340
Bram Moolenaare38eab22019-12-05 21:50:01 +01003341 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003342 while (current_funccal != NULL)
3343 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003344 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003345 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003346 if (current_funccal == NULL && funccal_stack != NULL)
3347 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003348 }
3349
Bram Moolenaare38eab22019-12-05 21:50:01 +01003350 // First clear what the functions contain. Since this may lower the
3351 // reference count of a function, it may also free a function and change
3352 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003353 while (todo > 0)
3354 {
3355 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003356 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003357 if (!HASHITEM_EMPTY(hi))
3358 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 // clear the def function index now
3360 fp = HI2UF(hi);
3361 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003362 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363
Bram Moolenaare38eab22019-12-05 21:50:01 +01003364 // Only free functions that are not refcounted, those are
3365 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003366 if (func_name_refcount(fp->uf_name))
3367 ++skipped;
3368 else
3369 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003370 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003371 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003372 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003373 {
3374 skipped = 0;
3375 break;
3376 }
3377 }
3378 --todo;
3379 }
3380 }
3381
Bram Moolenaare38eab22019-12-05 21:50:01 +01003382 // Now actually free the functions. Need to start all over every time,
3383 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003384 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003385 while (func_hashtab.ht_used > skipped)
3386 {
3387 todo = func_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003388 FOR_ALL_HASHTAB_ITEMS(&func_hashtab, hi, todo)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003389 if (!HASHITEM_EMPTY(hi))
3390 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003391 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003392 // Only free functions that are not refcounted, those are
3393 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003394 fp = HI2UF(hi);
3395 if (func_name_refcount(fp->uf_name))
3396 ++skipped;
3397 else
3398 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003399 if (func_free(fp, FALSE) == OK)
3400 {
3401 skipped = 0;
3402 break;
3403 }
3404 // did not actually free it
3405 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003406 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003407 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003408 }
3409 if (skipped == 0)
3410 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411
3412 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003413}
3414#endif
3415
3416/*
3417 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003418 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3419 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003420 * "len" is the length of "name", or -1 for NUL terminated.
3421 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003423builtin_function(char_u *name, int len)
3424{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003425 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003426
Bram Moolenaara26b9702020-04-18 19:53:28 +02003427 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003428 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003429 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3430 {
3431 if (name[i] == AUTOLOAD_CHAR)
3432 return FALSE;
3433 if (!eval_isnamec(name[i]))
3434 {
3435 // "name.something" is not a builtin function
3436 if (name[i] == '.')
3437 return FALSE;
3438 break;
3439 }
3440 }
3441 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003442}
3443
3444 int
3445func_call(
3446 char_u *name,
3447 typval_T *args,
3448 partial_T *partial,
3449 dict_T *selfdict,
3450 typval_T *rettv)
3451{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003452 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003453 listitem_T *item;
3454 typval_T argv[MAX_FUNC_ARGS + 1];
3455 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003456 int r = 0;
3457
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003458 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003459 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003460 {
3461 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3462 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003463 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003464 break;
3465 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003466 // Make a copy of each argument. This is needed to be able to set
3467 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003468 copy_tv(&item->li_tv, &argv[argc++]);
3469 }
3470
3471 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003472 {
3473 funcexe_T funcexe;
3474
Bram Moolenaara80faa82020-04-12 19:37:17 +02003475 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003476 funcexe.fe_firstline = curwin->w_cursor.lnum;
3477 funcexe.fe_lastline = curwin->w_cursor.lnum;
3478 funcexe.fe_evaluate = TRUE;
3479 funcexe.fe_partial = partial;
3480 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003481 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3482 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003483
Bram Moolenaare38eab22019-12-05 21:50:01 +01003484 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003485 while (argc > 0)
3486 clear_tv(&argv[--argc]);
3487
3488 return r;
3489}
3490
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003491static int callback_depth = 0;
3492
3493 int
3494get_callback_depth(void)
3495{
3496 return callback_depth;
3497}
3498
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003499/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003500 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003501 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003502 */
3503 int
3504call_callback(
3505 callback_T *callback,
3506 int len, // length of "name" or -1 to use strlen()
3507 typval_T *rettv, // return value goes here
3508 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003509 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003510 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003511{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003512 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003513 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003514
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003515 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3516 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02003517 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003518 funcexe.fe_evaluate = TRUE;
3519 funcexe.fe_partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003520 ++callback_depth;
3521 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3522 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003523
3524 // When a :def function was called that uses :try an error would be turned
3525 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003526 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003527 {
3528 need_rethrow = FALSE;
3529 handle_did_throw();
3530 }
3531
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003532 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003533}
3534
3535/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003536 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003537 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003538 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3539 */
3540 varnumber_T
3541call_callback_retnr(
3542 callback_T *callback,
3543 int argcount, // number of "argvars"
3544 typval_T *argvars) // vars for arguments, must have "argcount"
3545 // PLUS ONE elements!
3546{
3547 typval_T rettv;
3548 varnumber_T retval;
3549
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003550 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003551 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003552
3553 retval = tv_get_number_chk(&rettv, NULL);
3554 clear_tv(&rettv);
3555 return retval;
3556}
3557
3558/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 * Give an error message for the result of a function.
3560 * Nothing if "error" is FCERR_NONE.
3561 */
3562 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003563user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564{
3565 switch (error)
3566 {
3567 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003568 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003569 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003570 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003571 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 break;
3573 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003574 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 break;
3576 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003577 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578 break;
3579 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003580 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581 break;
3582 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003583 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 break;
3585 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003586 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587 break;
3588 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003589 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3590 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003592 case FCERR_OTHER:
3593 case FCERR_FAILED:
3594 // assume the error message was already given
3595 break;
3596 case FCERR_NONE:
3597 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 }
3599}
3600
3601/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003602 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003603 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003604 * Return FAIL when the function can't be called, OK otherwise.
3605 * Also returns OK when an error was encountered while executing the function.
3606 */
3607 int
3608call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003609 char_u *funcname, // name of the function
3610 int len, // length of "name" or -1 to use strlen()
3611 typval_T *rettv, // return value goes here
3612 int argcount_in, // number of "argvars"
3613 typval_T *argvars_in, // vars for arguments, must have "argcount"
3614 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003615 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003616{
3617 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003618 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003619 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003620 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003621 char_u fname_buf[FLEN_FIXED + 1];
3622 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003623 char_u *fname = NULL;
3624 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003625 int argcount = argcount_in;
3626 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003627 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003628 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003629 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003630 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003631 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003632 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003633 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003634 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003635
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003636 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3637 // even when call_func() returns FAIL.
3638 rettv->v_type = VAR_UNKNOWN;
3639
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003640 if (partial != NULL)
3641 fp = partial->pt_func;
3642 if (fp == NULL)
3643 {
3644 // Make a copy of the name, if it comes from a funcref variable it
3645 // could be changed or deleted in the called function.
3646 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3647 if (name == NULL)
3648 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003649
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003650 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3651 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003652
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003653 if (funcexe->fe_doesrange != NULL)
3654 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003655
3656 if (partial != NULL)
3657 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003658 // When the function has a partial with a dict and there is a dict
3659 // argument, use the dict argument. That is backwards compatible.
3660 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003661 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003662 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003663 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003664 {
3665 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003666 {
3667 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3668 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003669 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003670 goto theend;
3671 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003672 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003673 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003674 for (i = 0; i < argcount_in; ++i)
3675 argv[i + argv_clear] = argvars_in[i];
3676 argvars = argv;
3677 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003678
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003679 if (funcexe->fe_check_type != NULL
3680 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003681 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003682 // Now funcexe->fe_check_type is missing the added arguments,
3683 // make a copy of the type with the correction.
3684 check_type = *funcexe->fe_check_type;
3685 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003686 check_type.tt_args = check_type_args;
3687 CLEAR_FIELD(check_type_args);
3688 for (i = 0; i < check_type.tt_argcount; ++i)
3689 check_type_args[i + partial->pt_argc] =
3690 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003691 check_type.tt_argcount += partial->pt_argc;
3692 check_type.tt_min_argcount += partial->pt_argc;
3693 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003694 }
3695 }
3696
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003697 if (error == FCERR_NONE && funcexe->fe_check_type != NULL
3698 && funcexe->fe_evaluate)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003699 {
3700 // Check that the argument types are OK for the types of the funcref.
Bram Moolenaarc84287d2022-01-16 18:06:21 +00003701 if (check_argument_types(funcexe->fe_check_type,
3702 argvars, argcount, funcexe->fe_basetv,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003703 (name != NULL) ? name : funcname) == FAIL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003704 error = FCERR_OTHER;
3705 }
3706
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003707 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003708 {
3709 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003710 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003711
Bram Moolenaar333894b2020-08-01 18:53:07 +02003712 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003713 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003714 {
3715 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003716 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003717 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003718
Bram Moolenaare38eab22019-12-05 21:50:01 +01003719 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003720 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003721 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003722
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003723 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003724 {
3725 /*
3726 * User defined function.
3727 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003728 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003729 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003730 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003731 if (fp != NULL && !is_global && in_vim9script()
3732 && func_requires_g_prefix(fp))
3733 // In Vim9 script g: is required to find a global
3734 // non-autoload function.
3735 fp = NULL;
3736 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003737
Bram Moolenaare38eab22019-12-05 21:50:01 +01003738 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003739 if (fp == NULL
3740 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003741 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003742 && !aborting())
3743 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003744 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003745 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003746 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003747 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003748 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3749 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003750 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003751 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003752 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003753 if (fp == NULL)
3754 {
3755 char_u *p = untrans_function_name(rfname);
3756
3757 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003758 // Don't do this if the name starts with "s:".
3759 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003760 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003761 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003762
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003763 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003764 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003765 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003766 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003767 if (funcexe->fe_argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003768 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003769 argcount = funcexe->fe_argv_func(argcount, argvars,
zeertzjq48db5da2022-09-16 12:10:03 +01003770 argv_clear, fp);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003771
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003772 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003773 {
3774 // Method call: base->Method()
3775 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003776 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003777 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003778 argvars = argv;
3779 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003780 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003781
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782 error = call_user_func_check(fp, argcount, argvars, rettv,
3783 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003784 }
3785 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003786 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003787 {
3788 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003789 * expr->method(): Find the method name in the table, call its
3790 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003791 */
3792 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003793 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003794 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003795 else
3796 {
3797 /*
3798 * Find the function name in the table, call its implementation.
3799 */
3800 error = call_internal_func(fname, argcount, argvars, rettv);
3801 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003802
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003803 /*
3804 * The function call (or "FuncUndefined" autocommand sequence) might
3805 * have been aborted by an error, an interrupt, or an explicitly thrown
3806 * exception that has not been caught so far. This situation can be
3807 * tested for by calling aborting(). For an error in an internal
3808 * function or for the "E132" error in call_user_func(), however, the
3809 * throw point at which the "force_abort" flag (temporarily reset by
3810 * emsg()) is normally updated has not been reached yet. We need to
3811 * update that flag first to make aborting() reliable.
3812 */
3813 update_force_abort();
3814 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003815 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003816 ret = OK;
3817
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003818theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003819 /*
3820 * Report an error unless the argument evaluation or function call has been
3821 * cancelled due to an aborting error, an interrupt, or an exception.
3822 */
3823 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003824 user_func_error(error, (name != NULL) ? name : funcname,
3825 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003826
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003827 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003828 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003829 clear_tv(&argv[--argv_clear + argv_base]);
3830
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003831 vim_free(tofree);
3832 vim_free(name);
3833
3834 return ret;
3835}
3836
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003837/*
3838 * Call a function without arguments, partial or dict.
3839 * This is like call_func() when the call is only "FuncName()".
3840 * To be used by "expr" options.
3841 * Returns NOTDONE when the function could not be found.
3842 */
3843 int
3844call_simple_func(
3845 char_u *funcname, // name of the function
3846 int len, // length of "name" or -1 to use strlen()
3847 typval_T *rettv) // return value goes here
3848{
3849 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003850 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003851 char_u fname_buf[FLEN_FIXED + 1];
3852 char_u *tofree = NULL;
3853 char_u *name;
3854 char_u *fname;
3855 char_u *rfname;
3856 int is_global = FALSE;
3857 ufunc_T *fp;
3858
3859 rettv->v_type = VAR_NUMBER; // default rettv is number zero
3860 rettv->vval.v_number = 0;
3861
3862 // Make a copy of the name, an option can be changed in the function.
3863 name = vim_strnsave(funcname, len);
3864 if (name == NULL)
3865 return ret;
3866
3867 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3868
3869 // Skip "g:" before a function name.
3870 if (fname[0] == 'g' && fname[1] == ':')
3871 {
3872 is_global = TRUE;
3873 rfname = fname + 2;
3874 }
3875 else
3876 rfname = fname;
3877 fp = find_func(rfname, is_global);
3878 if (fp != NULL && !is_global && in_vim9script()
3879 && func_requires_g_prefix(fp))
3880 // In Vim9 script g: is required to find a global non-autoload
3881 // function.
3882 fp = NULL;
3883 if (fp == NULL)
3884 ret = NOTDONE;
3885 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
3886 error = FCERR_DELETED;
3887 else if (fp != NULL)
3888 {
3889 typval_T argvars[1];
3890 funcexe_T funcexe;
3891
3892 argvars[0].v_type = VAR_UNKNOWN;
3893 CLEAR_FIELD(funcexe);
3894 funcexe.fe_evaluate = TRUE;
3895
3896 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
3897 if (error == FCERR_NONE)
3898 ret = OK;
3899 }
3900
3901 user_func_error(error, name, FALSE);
3902 vim_free(tofree);
3903 vim_free(name);
3904
3905 return ret;
3906}
3907
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003908 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003909printable_func_name(ufunc_T *fp)
3910{
3911 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
3912}
3913
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003914/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003915 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
3916 * TRUE. Otherwise return FALSE.
3917 */
3918 static int
3919function_list_modified(int prev_ht_changed)
3920{
3921 if (prev_ht_changed != func_hashtab.ht_changed)
3922 {
3923 emsg(_(e_function_list_was_modified));
3924 return TRUE;
3925 }
3926 return FALSE;
3927}
3928
3929/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003930 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003931 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003932 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003933list_func_head(ufunc_T *fp, int indent)
3934{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003935 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003936 int j;
3937
3938 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003939
3940 // a timer at the more prompt may have deleted the function
3941 if (function_list_modified(prev_ht_changed))
3942 return FAIL;
3943
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003944 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003945 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003946 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003947 msg_puts("def ");
3948 else
3949 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003951 msg_putchar('(');
3952 for (j = 0; j < fp->uf_args.ga_len; ++j)
3953 {
3954 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003955 msg_puts(", ");
3956 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 if (fp->uf_arg_types != NULL)
3958 {
3959 char *tofree;
3960
3961 msg_puts(": ");
3962 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
3963 vim_free(tofree);
3964 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003965 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
3966 {
3967 msg_puts(" = ");
3968 msg_puts(((char **)(fp->uf_def_args.ga_data))
3969 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
3970 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003971 }
3972 if (fp->uf_varargs)
3973 {
3974 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003975 msg_puts(", ");
3976 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003977 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003978 if (fp->uf_va_name != NULL)
3979 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01003980 if (!fp->uf_varargs)
3981 {
3982 if (j)
3983 msg_puts(", ");
3984 msg_puts("...");
3985 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02003987 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988 {
3989 char *tofree;
3990
3991 msg_puts(": ");
3992 msg_puts(type_name(fp->uf_va_type, &tofree));
3993 vim_free(tofree);
3994 }
3995 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003996 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003997
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003998 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003999 {
4000 if (fp->uf_ret_type != &t_void)
4001 {
4002 char *tofree;
4003
4004 msg_puts(": ");
4005 msg_puts(type_name(fp->uf_ret_type, &tofree));
4006 vim_free(tofree);
4007 }
4008 }
4009 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004010 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004011 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004012 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004013 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004014 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004015 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004016 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004017 msg_clr_eos();
4018 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004019 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004020
4021 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004022}
4023
4024/*
4025 * Get a function name, translating "<SID>" and "<SNR>".
4026 * Also handles a Funcref in a List or Dictionary.
4027 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004028 * Set "*is_global" to TRUE when the function must be global, unless
4029 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004030 * flags:
4031 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004032 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004033 * TFN_QUIET: be quiet
4034 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004035 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004036 * Advances "pp" to just after the function name (if no error).
4037 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004038 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004039trans_function_name(
4040 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004041 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004042 int skip, // only find the end, don't evaluate
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004043 int flags)
4044{
4045 return trans_function_name_ext(pp, is_global, skip, flags,
4046 NULL, NULL, NULL, NULL);
4047}
4048
4049/*
4050 * trans_function_name() with extra arguments.
4051 * "fdp", "partial", "type" and "ufunc" can be NULL.
4052 */
4053 char_u *
4054trans_function_name_ext(
4055 char_u **pp,
4056 int *is_global,
4057 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004058 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004059 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004060 partial_T **partial, // return: partial of a FuncRef
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004061 type_T **type, // return: type of funcref
4062 ufunc_T **ufunc) // return: function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004063{
4064 char_u *name = NULL;
4065 char_u *start;
4066 char_u *end;
4067 int lead;
4068 char_u sid_buf[20];
4069 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004071 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004072 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004073 int vim9script = in_vim9script();
4074 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004075
4076 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004077 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004078 start = *pp;
4079
Bram Moolenaare38eab22019-12-05 21:50:01 +01004080 // Check for hard coded <SNR>: already translated function ID (from a user
4081 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004082 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4083 && (*pp)[2] == (int)KE_SNR)
4084 {
4085 *pp += 3;
4086 len = get_id_len(pp) + 3;
4087 return vim_strnsave(start, len);
4088 }
4089
Bram Moolenaare38eab22019-12-05 21:50:01 +01004090 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4091 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004092 lead = eval_fname_script(start);
4093 if (lead > 2)
4094 start += lead;
4095
Bram Moolenaare38eab22019-12-05 21:50:01 +01004096 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004097 end = get_lval(start, NULL, &lv, FALSE, skip,
4098 flags | GLV_READ_ONLY | GLV_PREFER_FUNC,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004099 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004100 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004101 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004102 {
4103 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004104 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004105 goto theend;
4106 }
4107 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4108 {
4109 /*
4110 * Report an invalid expression in braces, unless the expression
4111 * evaluation has been cancelled due to an aborting error, an
4112 * interrupt, or an exception.
4113 */
4114 if (!aborting())
4115 {
4116 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004117 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004118 }
4119 else
4120 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4121 goto theend;
4122 }
4123
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004124 if (lv.ll_ufunc != NULL)
4125 {
4126 *ufunc = lv.ll_ufunc;
4127 name = vim_strsave(lv.ll_ufunc->uf_name);
4128 goto theend;
4129 }
4130
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004131 if (lv.ll_tv != NULL)
4132 {
4133 if (fdp != NULL)
4134 {
4135 fdp->fd_dict = lv.ll_dict;
4136 fdp->fd_newkey = lv.ll_newkey;
4137 lv.ll_newkey = NULL;
4138 fdp->fd_di = lv.ll_di;
4139 }
4140 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4141 {
4142 name = vim_strsave(lv.ll_tv->vval.v_string);
4143 *pp = end;
4144 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004145 else if (lv.ll_tv->v_type == VAR_CLASS
4146 && lv.ll_tv->vval.v_class != NULL)
4147 {
4148 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4149 *pp = end;
4150 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004151 else if (lv.ll_tv->v_type == VAR_PARTIAL
4152 && lv.ll_tv->vval.v_partial != NULL)
4153 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004154 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004155 *pp = end;
4156 if (partial != NULL)
4157 *partial = lv.ll_tv->vval.v_partial;
4158 }
4159 else
4160 {
4161 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4162 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004163 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004164 else
4165 *pp = end;
4166 name = NULL;
4167 }
4168 goto theend;
4169 }
4170
4171 if (lv.ll_name == NULL)
4172 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004173 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004174 *pp = end;
4175 goto theend;
4176 }
4177
Bram Moolenaare38eab22019-12-05 21:50:01 +01004178 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004179 if (lv.ll_exp_name != NULL)
4180 {
4181 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004182 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004183 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004184 if (name == lv.ll_exp_name)
4185 name = NULL;
4186 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004187 else if (lv.ll_sid > 0)
4188 {
4189 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4190 int cc = *lv.ll_name_end;
4191
4192 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4193 *lv.ll_name_end = NUL;
4194 if (si->sn_autoload_prefix != NULL)
4195 {
4196 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4197 }
4198 else
4199 {
4200 sid_buf[0] = K_SPECIAL;
4201 sid_buf[1] = KS_EXTRA;
4202 sid_buf[2] = (int)KE_SNR;
4203 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004204 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004205 name = concat_str(sid_buf, lv.ll_name);
4206 }
4207 *lv.ll_name_end = cc;
4208 *pp = end;
4209 goto theend;
4210 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004211 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004212 {
4213 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004214 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004215 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004216 if (name == *pp)
4217 name = NULL;
4218 }
4219 if (name != NULL)
4220 {
4221 name = vim_strsave(name);
4222 *pp = end;
4223 if (STRNCMP(name, "<SNR>", 5) == 0)
4224 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004225 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004226 name[0] = K_SPECIAL;
4227 name[1] = KS_EXTRA;
4228 name[2] = (int)KE_SNR;
4229 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4230 }
4231 goto theend;
4232 }
4233
4234 if (lv.ll_exp_name != NULL)
4235 {
4236 len = (int)STRLEN(lv.ll_exp_name);
4237 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4238 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4239 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004240 // When there was "s:" already or the name expanded to get a
4241 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004242 lv.ll_name += 2;
4243 len -= 2;
4244 lead = 2;
4245 }
4246 }
4247 else
4248 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004249 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004250 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004251 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004252 if (lv.ll_name[0] == 'g')
4253 {
4254 if (is_global != NULL)
4255 {
4256 *is_global = TRUE;
4257 }
4258 else
4259 {
4260 // dropping "g:" without setting "is_global" won't work in
4261 // Vim9script, put it back later
4262 prefix_g = TRUE;
4263 extra = 2;
4264 }
4265 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004266 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004267 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004268 len = (int)(end - lv.ll_name);
4269 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004270 if (len <= 0)
4271 {
4272 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004273 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004274 goto theend;
4275 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004276
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004277 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004278 // starts with a lower case character: dict.func(). Or when in a class.
4279 vim9_local = ASCII_ISUPPER(*start) && vim9script
4280 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004282 /*
4283 * Copy the function name to allocated memory.
4284 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4285 * Accept <SNR>123_name() outside a script.
4286 */
4287 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004288 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004289 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004290 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004291 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004292 {
Kota Kato948a3892022-08-16 16:09:59 +01004293 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4294 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004295 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004296 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004297 goto theend;
4298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004300 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004301 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004303 || eval_fname_sid(*pp))
4304 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004306 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004307 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004308 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004309 goto theend;
4310 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004311 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004312 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 extra = 3 + (int)STRLEN(sid_buf);
4314 else
4315 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004316 }
4317 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004318 else if (!(flags & TFN_INT)
4319 && (builtin_function(lv.ll_name, len)
4320 || (vim9script && *lv.ll_name == '_'))
4321 && !((flags & TFN_IN_CLASS) && STRNCMP(lv.ll_name, "new", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004322 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004323 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004324 : e_function_name_must_start_with_capital_or_s_str),
4325 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004326 goto theend;
4327 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004328 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004329 {
4330 char_u *cp = vim_strchr(lv.ll_name, ':');
4331
4332 if (cp != NULL && cp < end)
4333 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004334 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004335 goto theend;
4336 }
4337 }
4338
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004340 if (name != NULL)
4341 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004342 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004343 {
4344 name[0] = K_SPECIAL;
4345 name[1] = KS_EXTRA;
4346 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004347 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004348 STRCPY(name + 3, sid_buf);
4349 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004350 else if (prefix_g)
4351 {
4352 name[0] = 'g';
4353 name[1] = ':';
4354 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4356 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004357 }
4358 *pp = end;
4359
4360theend:
4361 clear_lval(&lv);
4362 return name;
4363}
4364
4365/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004366 * Assuming "name" is the result of trans_function_name() and it was prefixed
4367 * to use the script-local name, return the unmodified name (points into
4368 * "name"). Otherwise return NULL.
4369 * This can be used to first search for a script-local function and fall back
4370 * to the global function if not found.
4371 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004372 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004373untrans_function_name(char_u *name)
4374{
4375 char_u *p;
4376
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004377 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004378 {
4379 p = vim_strchr(name, '_');
4380 if (p != NULL)
4381 return p + 1;
4382 }
4383 return NULL;
4384}
4385
4386/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004387 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4388 * current script ID and returns the expanded function name. The caller should
4389 * free the returned name. If not called from a script context or the function
4390 * name doesn't start with these prefixes, then returns NULL.
4391 * This doesn't check whether the script-local function exists or not.
4392 */
4393 char_u *
4394get_scriptlocal_funcname(char_u *funcname)
4395{
4396 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004397 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004398 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004399 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004400
4401 if (funcname == NULL)
4402 return NULL;
4403
4404 if (STRNCMP(funcname, "s:", 2) != 0
4405 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004406 {
4407 ufunc_T *ufunc;
4408
4409 // The function name does not have a script-local prefix. Try finding
4410 // it when in a Vim9 script and there is no "g:" prefix.
4411 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4412 return NULL;
4413 ufunc = find_func(funcname, FALSE);
4414 if (ufunc == NULL || func_is_global(ufunc)
4415 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4416 return NULL;
4417 ++p;
4418 off = 0;
4419 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004420 else
4421 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004422
4423 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4424 {
4425 emsg(_(e_using_sid_not_in_script_context));
4426 return NULL;
4427 }
4428 // Expand s: prefix into <SNR>nr_<name>
4429 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4430 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004431 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004432 if (newname == NULL)
4433 return NULL;
4434 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004435 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004436
4437 return newname;
4438}
4439
4440/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004441 * Return script-local "fname" with the 3-byte sequence replaced by
4442 * printable <SNR> in allocated memory.
4443 */
4444 char_u *
4445alloc_printable_func_name(char_u *fname)
4446{
4447 char_u *n = alloc(STRLEN(fname + 3) + 6);
4448
4449 if (n != NULL)
4450 {
4451 STRCPY(n, "<SNR>");
4452 STRCPY(n + 5, fname + 3);
4453 }
4454 return n;
4455}
4456
4457/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004458 * Call trans_function_name(), except that a lambda is returned as-is.
4459 * Returns the name in allocated memory.
4460 */
4461 char_u *
4462save_function_name(
4463 char_u **name,
4464 int *is_global,
4465 int skip,
4466 int flags,
4467 funcdict_T *fudi)
4468{
4469 char_u *p = *name;
4470 char_u *saved;
4471
4472 if (STRNCMP(p, "<lambda>", 8) == 0)
4473 {
4474 p += 8;
4475 (void)getdigits(&p);
4476 saved = vim_strnsave(*name, p - *name);
4477 if (fudi != NULL)
4478 CLEAR_POINTER(fudi);
4479 }
4480 else
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00004481 saved = trans_function_name_ext(&p, is_global, skip,
4482 flags, fudi, NULL, NULL, NULL);
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004483 *name = p;
4484 return saved;
4485}
4486
4487/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004488 * List functions. When "regmatch" is NULL all of then.
4489 * Otherwise functions matching "regmatch".
4490 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004491 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004492list_functions(regmatch_T *regmatch)
4493{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004494 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004495 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004496 hashitem_T *hi;
4497
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004498 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004499 {
4500 if (!HASHITEM_EMPTY(hi))
4501 {
4502 ufunc_T *fp = HI2UF(hi);
4503
4504 --todo;
4505 if ((fp->uf_flags & FC_DEAD) == 0
4506 && (regmatch == NULL
4507 ? !message_filtered(fp->uf_name)
4508 && !func_name_refcount(fp->uf_name)
4509 : !isdigit(*fp->uf_name)
4510 && vim_regexec(regmatch, fp->uf_name, 0)))
4511 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004512 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004513 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004514 if (function_list_modified(prev_ht_changed))
4515 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004516 }
4517 }
4518 }
4519}
4520
4521/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004522 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004523 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4524 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004525 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004526 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4527 * With CF_INTERFACE the function is define inside an interface, only the
4528 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004529 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004530 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004531 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004532define_function(
4533 exarg_T *eap,
4534 char_u *name_arg,
4535 garray_T *lines_to_free,
Bram Moolenaar554d0312023-01-05 19:59:18 +00004536 int class_flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004537{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004538 int j;
4539 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004540 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004541 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004542 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004543 char_u *p;
4544 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004545 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004546 char_u *line_arg = NULL;
4547 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004549 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004550 garray_T newlines;
4551 int varargs = FALSE;
4552 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004554 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004555 int fp_allocated = FALSE;
4556 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004557 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004558 dictitem_T *v;
4559 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004560 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004561 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004562 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004563 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004564 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004565 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004566
4567 /*
4568 * ":function" without argument: list functions.
4569 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004570 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004571 {
4572 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004573 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004574 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004575 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004576 }
4577
4578 /*
4579 * ":function /pat": list functions matching pattern.
4580 */
4581 if (*eap->arg == '/')
4582 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004583 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004584 if (!eap->skip)
4585 {
4586 regmatch_T regmatch;
4587
4588 c = *p;
4589 *p = NUL;
4590 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4591 *p = c;
4592 if (regmatch.regprog != NULL)
4593 {
4594 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004595 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004596 vim_regfree(regmatch.regprog);
4597 }
4598 }
4599 if (*p == '/')
4600 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004601 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004602 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004603 }
4604
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605 ga_init(&newargs);
4606 ga_init(&argtypes);
4607 ga_init(&default_args);
4608
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004609 /*
4610 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004611 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004612 * "name" == func, "fudi.fd_dict" == NULL
4613 * dict.func new dictionary entry
4614 * "name" == NULL, "fudi.fd_dict" set,
4615 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4616 * dict.func existing dict entry with a Funcref
4617 * "name" == func, "fudi.fd_dict" set,
4618 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4619 * dict.func existing dict entry that's not a Funcref
4620 * "name" == NULL, "fudi.fd_dict" set,
4621 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4622 * s:func script-local function name
4623 * g:func global function name, same as "func"
4624 */
4625 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004626 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004627 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004628 // nested function, argument is (args).
4629 paren = TRUE;
4630 CLEAR_FIELD(fudi);
4631 }
4632 else
4633 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004634 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004635 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004636 if (p[0] == 's' && p[1] == ':')
4637 {
4638 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4639 return NULL;
4640 }
4641 p = to_name_end(p, TRUE);
4642 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4643 {
4644 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4645 eap->arg);
4646 return NULL;
4647 }
4648 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004649 }
4650
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004651 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004652 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004653 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004654 paren = (vim_strchr(p, '(') != NULL);
4655 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004656 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004657 /*
4658 * Return on an invalid expression in braces, unless the expression
4659 * evaluation has been cancelled due to an aborting error, an
4660 * interrupt, or an exception.
4661 */
4662 if (!aborting())
4663 {
4664 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004665 semsg(_(e_key_not_present_in_dictionary_str),
4666 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004667 vim_free(fudi.fd_newkey);
4668 return NULL;
4669 }
4670 else
4671 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004672 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004673
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004674 // For "export def FuncName()" in an autoload script the function name
4675 // is stored with the legacy autoload name "dir#script#FuncName" so
4676 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004677 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004678 {
4679 char_u *prefixed = may_prefix_autoload(name);
4680
4681 if (prefixed != NULL && prefixed != name)
4682 {
4683 vim_free(name);
4684 name = prefixed;
4685 }
4686 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004687 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004688 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004689 {
4690 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4691 goto ret_free;
4692 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004693 }
4694
Bram Moolenaare38eab22019-12-05 21:50:01 +01004695 // An error in a function call during evaluation of an expression in magic
4696 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004697 saved_did_emsg = did_emsg;
4698 did_emsg = FALSE;
4699
4700 /*
4701 * ":function func" with only function name: list function.
4702 */
4703 if (!paren)
4704 {
4705 if (!ends_excmd(*skipwhite(p)))
4706 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004707 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004708 goto ret_free;
4709 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004710 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004711 if (eap->nextcmd != NULL)
4712 *p = NUL;
4713 if (!eap->skip && !got_int)
4714 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004715 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004716 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4717 {
4718 char_u *up = untrans_function_name(name);
4719
4720 // With Vim9 script the name was made script-local, if not
4721 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004722 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004723 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004724 }
4725
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004726 if (fp != NULL)
4727 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004728 // Check no function was added or removed from a timer, e.g. at
4729 // the more prompt. "fp" may then be invalid.
4730 int prev_ht_changed = func_hashtab.ht_changed;
4731
4732 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004733 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004734 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4735 {
4736 if (FUNCLINE(fp, j) == NULL)
4737 continue;
4738 msg_putchar('\n');
4739 msg_outnum((long)(j + 1));
4740 if (j < 9)
4741 msg_putchar(' ');
4742 if (j < 99)
4743 msg_putchar(' ');
4744 if (function_list_modified(prev_ht_changed))
4745 break;
4746 msg_prt_line(FUNCLINE(fp, j), FALSE);
4747 out_flush(); // show a line at a time
4748 ui_breakcheck();
4749 }
4750 if (!got_int)
4751 {
4752 msg_putchar('\n');
4753 if (!function_list_modified(prev_ht_changed))
4754 {
4755 if (fp->uf_def_status != UF_NOT_COMPILED)
4756 msg_puts(" enddef");
4757 else
4758 msg_puts(" endfunction");
4759 }
4760 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004761 }
4762 }
4763 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004764 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004765 }
4766 goto ret_free;
4767 }
4768
4769 /*
4770 * ":function name(arg1, arg2)" Define function.
4771 */
4772 p = skipwhite(p);
4773 if (*p != '(')
4774 {
4775 if (!eap->skip)
4776 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004777 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004778 goto ret_free;
4779 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004780 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004781 if (vim_strchr(p, '(') != NULL)
4782 p = vim_strchr(p, '(');
4783 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004784
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004785 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4786 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004787 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004788 goto ret_free;
4789 }
4790
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004791 // In Vim9 script only global functions can be redefined.
4792 if (vim9script && eap->forceit && !is_global)
4793 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004794 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004795 goto ret_free;
4796 }
4797
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004798 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004799
Bram Moolenaar04b12692020-05-04 23:24:44 +02004800 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004801 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004802 // Check the name of the function. Unless it's a dictionary function
4803 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004804 if (name != NULL)
4805 arg = name;
4806 else
4807 arg = fudi.fd_newkey;
4808 if (arg != NULL && (fudi.fd_di == NULL
4809 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4810 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4811 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004812 char_u *name_base = arg;
4813 int i;
4814
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004815 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004816 {
4817 name_base = vim_strchr(arg, '_');
4818 if (name_base == NULL)
4819 name_base = arg + 3;
4820 else
4821 ++name_base;
4822 }
4823 for (i = 0; name_base[i] != NUL && (i == 0
4824 ? eval_isnamec1(name_base[i])
4825 : eval_isnamec(name_base[i])); ++i)
4826 ;
4827 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004828 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004829
4830 // In Vim9 script a function cannot have the same name as a
4831 // variable.
4832 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004833 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4834 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004835 + EVAL_VAR_NO_FUNC) == OK)
4836 {
4837 semsg(_(e_redefining_script_item_str), name_base);
4838 goto ret_free;
4839 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004840 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004841 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004842 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004843 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004844 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004845 goto ret_free;
4846 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004847 }
4848
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004849 // This may get more lines and make the pointers into the first line
4850 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004851 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00004853 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004854 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00004855 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004856 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004857 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004858
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004859 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004860 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004862 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004863 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004864 if (*p != ':')
4865 {
4866 semsg(_(e_no_white_space_allowed_before_colon_str), p);
4867 p = skipwhite(p);
4868 }
4869 else if (!IS_WHITE_OR_NUL(p[1]))
4870 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02004872 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004873 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004874 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004875 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01004876 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004878 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004879 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004880 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004881 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02004882 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004883 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004884 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02004885 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004886 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887 else
4888 // find extra arguments "range", "dict", "abort" and "closure"
4889 for (;;)
4890 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01004891 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892 p = skipwhite(p);
4893 if (STRNCMP(p, "range", 5) == 0)
4894 {
4895 flags |= FC_RANGE;
4896 p += 5;
4897 }
4898 else if (STRNCMP(p, "dict", 4) == 0)
4899 {
4900 flags |= FC_DICT;
4901 p += 4;
4902 }
4903 else if (STRNCMP(p, "abort", 5) == 0)
4904 {
4905 flags |= FC_ABORT;
4906 p += 5;
4907 }
4908 else if (STRNCMP(p, "closure", 7) == 0)
4909 {
4910 flags |= FC_CLOSURE;
4911 p += 7;
4912 if (current_funccal == NULL)
4913 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004914 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004915 name == NULL ? (char_u *)"" : name);
4916 goto erret;
4917 }
4918 }
4919 else
4920 break;
4921 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004922
Bram Moolenaare38eab22019-12-05 21:50:01 +01004923 // When there is a line break use what follows for the function body.
4924 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004925 if (*p == '\n')
4926 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004927 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02004928 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
4929 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01004930 && !(VIM_ISWHITE(*whitep) && *p == '#'
4931 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02004932 && !eap->skip
4933 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004934 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004935
4936 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004937 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
4938 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004939 */
4940 if (KeyTyped)
4941 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004942 // Check if the function already exists, don't let the user type the
4943 // whole function before telling him it doesn't work! For a script we
4944 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004945 if (!eap->skip && !eap->forceit)
4946 {
4947 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004948 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004949 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00004950 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004951 }
4952
4953 if (!eap->skip && did_emsg)
4954 goto erret;
4955
Bram Moolenaare38eab22019-12-05 21:50:01 +01004956 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004957 cmdline_row = msg_row;
4958 }
4959
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004960 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004961 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004962
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004963 // Do not define the function when getting the body fails and when
4964 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004965 if (((class_flags & CF_INTERFACE) == 0
4966 && get_function_body(eap, &newlines, line_arg, lines_to_free)
4967 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004968 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004969 goto erret;
4970
4971 /*
4972 * If there are no errors, add the function
4973 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004974 if (fudi.fd_dict != NULL)
4975 {
4976 char numbuf[20];
4977
4978 fp = NULL;
4979 if (fudi.fd_newkey == NULL && !eap->forceit)
4980 {
4981 emsg(_(e_dictionary_entry_already_exists));
4982 goto erret;
4983 }
4984 if (fudi.fd_di == NULL)
4985 {
4986 // Can't add a function to a locked dictionary
4987 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
4988 goto erret;
4989 }
4990 // Can't change an existing function if it is locked
4991 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
4992 goto erret;
4993
4994 // Give the function a sequential number. Can only be used with a
4995 // Funcref!
4996 vim_free(name);
4997 sprintf(numbuf, "%d", ++func_nr);
4998 name = vim_strsave((char_u *)numbuf);
4999 if (name == NULL)
5000 goto erret;
5001 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005002 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005003 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005004 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005005 char_u *find_name = name;
5006 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005007 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005009 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00005010 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005011 var_conflict = TRUE;
5012
5013 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
5014 {
5015 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
5016
5017 if (si->sn_autoload_prefix != NULL)
5018 {
5019 if (is_export)
5020 {
5021 find_name = name + STRLEN(si->sn_autoload_prefix);
5022 v = find_var(find_name, &ht, TRUE);
5023 if (v != NULL)
5024 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005025 // Only check if the function already exists in the script,
5026 // global functions can be shadowed.
5027 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005028 }
5029 else
5030 {
5031 char_u *prefixed = may_prefix_autoload(name);
5032
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005033 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005034 {
5035 v = find_var(prefixed, &ht, TRUE);
5036 if (v != NULL)
5037 var_conflict = TRUE;
5038 vim_free(prefixed);
5039 }
5040 }
5041 }
5042 }
5043 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005044 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005045 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005046 goto erret;
5047 }
5048
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005049 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005050 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005051 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005052 char_u *uname = untrans_function_name(name);
5053
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005054 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005055 }
5056
5057 if (fp != NULL || import != NULL)
5058 {
5059 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005060
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005061 // Function can be replaced with "function!" and when sourcing the
5062 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005063 // A name that is used by an import can not be overruled.
5064 if (import != NULL
5065 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005066 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005067 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005068 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005069 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005070 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005071 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005072 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005073 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005074 goto erret;
5075 }
5076 if (fp->uf_calls > 0)
5077 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005078 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005079 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005080 goto erret;
5081 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005082 if (fp->uf_refcount > 1)
5083 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005084 // This function is referenced somewhere, don't redefine it but
5085 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005086 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005087 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005088 fp = NULL;
5089 overwrite = TRUE;
5090 }
5091 else
5092 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005093 char_u *exp_name = fp->uf_name_exp;
5094
5095 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005096 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005097 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005098 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005099 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005100 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005101#ifdef FEAT_PROFILE
5102 fp->uf_profiling = FALSE;
5103 fp->uf_prof_initialized = FALSE;
5104#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005105 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005106 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005107 }
5108 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005109
5110 if (fp == NULL)
5111 {
5112 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5113 {
5114 int slen, plen;
5115 char_u *scriptname;
5116
Bram Moolenaare38eab22019-12-05 21:50:01 +01005117 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005118 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005119 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005120 {
5121 scriptname = autoload_name(name);
5122 if (scriptname != NULL)
5123 {
5124 p = vim_strchr(scriptname, '/');
5125 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005126 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005127 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005128 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005129 j = OK;
5130 vim_free(scriptname);
5131 }
5132 }
5133 if (j == FAIL)
5134 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005135 linenr_T save_lnum = SOURCING_LNUM;
5136
5137 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005138 semsg(_(e_function_name_does_not_match_script_file_name_str),
5139 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005140 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005141 goto erret;
5142 }
5143 }
5144
Bram Moolenaare01e5212023-01-08 20:31:18 +00005145 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005146 if (fp == NULL)
5147 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005148 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005149
5150 if (fudi.fd_dict != NULL)
5151 {
5152 if (fudi.fd_di == NULL)
5153 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005154 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005155 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5156 if (fudi.fd_di == NULL)
5157 {
5158 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02005159 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005160 goto erret;
5161 }
5162 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5163 {
5164 vim_free(fudi.fd_di);
5165 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02005166 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005167 goto erret;
5168 }
5169 }
5170 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005171 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005172 clear_tv(&fudi.fd_di->di_tv);
5173 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005174 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005175
Bram Moolenaare38eab22019-12-05 21:50:01 +01005176 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005177 flags |= FC_DICT;
5178 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005179 }
5180 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005181 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005182 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005183 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005184
5185 if (eap->cmdidx == CMD_def)
5186 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005187 int lnum_save = SOURCING_LNUM;
5188 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005189
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005190 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005191
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005192 // error messages are for the first function line
5193 SOURCING_LNUM = sourcing_lnum_top;
5194
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005195 // The function may use script variables from the context.
5196 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005197
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005198 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005199 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005200 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005201 free_fp = fp_allocated;
5202 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005203 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005204 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005205
5206 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005207 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005208 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005209 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005210 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005211 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005212 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005213 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005214 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005215 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005216 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005217
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005218 if (fp_allocated)
5219 {
5220 // insert the new function in the function list
5221 set_ufunc_name(fp, name);
5222 if (overwrite)
5223 {
5224 hi = hash_find(&func_hashtab, name);
5225 hi->hi_key = UF2HIKEY(fp);
5226 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005227 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005228 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005229 {
5230 free_fp = TRUE;
5231 goto erret;
5232 }
5233 fp->uf_refcount = 1;
5234 }
5235
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005236 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005237 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005238 if ((flags & FC_CLOSURE) != 0)
5239 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005240 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005241 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005242 }
5243 else
5244 fp->uf_scoped = NULL;
5245
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005246#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005247 if (prof_def_func())
5248 func_do_profile(fp);
5249#endif
5250 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005251 if (sandbox)
5252 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005253 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005254 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005255 fp->uf_flags = flags;
5256 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005257 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005258 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005259 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005260 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005261 if (is_export)
5262 {
5263 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005264 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005265 is_export = FALSE;
5266 }
5267
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005268 if (eap->cmdidx == CMD_def)
5269 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005270 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5271 // :func does not use Vim9 script syntax, even in a Vim9 script file
5272 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005273
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005274 goto ret_free;
5275
5276erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005277 if (fp != NULL)
5278 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005279 // these were set to "newargs" and "default_args", which are cleared
5280 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005281 ga_init(&fp->uf_args);
5282 ga_init(&fp->uf_def_args);
5283 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005284errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005285 ga_clear_strings(&newargs);
5286 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005287 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005288 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005289 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005290 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005291 VIM_CLEAR(fp->uf_va_name);
5292 clear_type_list(&fp->uf_type_list);
5293 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005294 if (free_fp)
5295 {
5296 vim_free(fp);
5297 fp = NULL;
5298 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005299ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005300 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005301 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005302 if (name != name_arg)
5303 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005304 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005305 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005306
5307 return fp;
5308}
5309
5310/*
5311 * ":function"
5312 */
5313 void
5314ex_function(exarg_T *eap)
5315{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005316 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005317
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005318 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar554d0312023-01-05 19:59:18 +00005319 (void)define_function(eap, NULL, &lines_to_free, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005320 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005321}
5322
5323/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005324 * Find a function by name, including "<lambda>123".
5325 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005326 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005327 * Return NULL if not found.
5328 */
5329 ufunc_T *
5330find_func_by_name(char_u *name, compiletype_T *compile_type)
5331{
5332 char_u *arg = name;
5333 char_u *fname;
5334 ufunc_T *ufunc;
5335 int is_global = FALSE;
5336
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005337 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5338 {
5339 *compile_type = CT_PROFILE;
5340 arg = skipwhite(arg + 7);
5341 }
5342 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5343 {
5344 *compile_type = CT_DEBUG;
5345 arg = skipwhite(arg + 5);
5346 }
5347
5348 if (STRNCMP(arg, "<lambda>", 8) == 0)
5349 {
5350 arg += 8;
5351 (void)getdigits(&arg);
5352 fname = vim_strnsave(name, arg - name);
5353 }
5354 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005355 {
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005356 // First try finding a method in a class, trans_function_name() will
5357 // give an error if the function is not found.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005358 ufunc = find_class_func(&arg);
5359 if (ufunc != NULL)
5360 return ufunc;
5361
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005362 fname = trans_function_name_ext(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005363 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005364 NULL, NULL, NULL, &ufunc);
5365 if (ufunc != NULL)
5366 {
5367 vim_free(fname);
5368 return ufunc;
5369 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005370 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005371 if (fname == NULL)
5372 {
5373 semsg(_(e_invalid_argument_str), name);
5374 return NULL;
5375 }
5376 if (!ends_excmd2(name, arg))
5377 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005378 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005379 emsg(ex_errmsg(e_trailing_characters_str, arg));
5380 return NULL;
5381 }
5382
5383 ufunc = find_func(fname, is_global);
5384 if (ufunc == NULL)
5385 {
5386 char_u *p = untrans_function_name(fname);
5387
5388 if (p != NULL)
5389 // Try again without making it script-local.
5390 ufunc = find_func(p, FALSE);
5391 }
5392 vim_free(fname);
5393 if (ufunc == NULL)
5394 semsg(_(e_cannot_find_function_str), name);
5395 return ufunc;
5396}
5397
5398/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005399 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005400 * be compiled or the one specified by the argument.
5401 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005402 */
5403 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005404ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005405{
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005406 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005407 {
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005408 compiletype_T compile_type = CT_NONE;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005409 ufunc_T *ufunc = find_func_by_name(eap->arg, &compile_type);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005410 if (ufunc != NULL)
5411 {
5412 if (func_needs_compiling(ufunc, compile_type))
5413 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5414 else
5415 smsg(_("Function %s does not need compiling"), eap->arg);
5416 }
5417 }
5418 else
5419 {
5420 long todo = (long)func_hashtab.ht_used;
5421 int changed = func_hashtab.ht_changed;
5422 hashitem_T *hi;
5423
5424 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5425 {
5426 if (!HASHITEM_EMPTY(hi))
5427 {
5428 --todo;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005429 ufunc_T *ufunc = HI2UF(hi);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005430 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5431 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5432 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005433 {
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005434 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5435
5436 if (func_hashtab.ht_changed != changed)
5437 {
5438 // a function has been added or removed, need to start
5439 // over
5440 todo = (long)func_hashtab.ht_used;
5441 changed = func_hashtab.ht_changed;
5442 hi = func_hashtab.ht_array;
5443 --hi;
5444 }
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005445 }
5446 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005447 }
5448 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005449}
5450
5451/*
5452 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5453 * Return 2 if "p" starts with "s:".
5454 * Return 0 otherwise.
5455 */
5456 int
5457eval_fname_script(char_u *p)
5458{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005459 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5460 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005461 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5462 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5463 return 5;
5464 if (p[0] == 's' && p[1] == ':')
5465 return 2;
5466 return 0;
5467}
5468
5469 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005470translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005471{
5472 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005473 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005474 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005475}
5476
5477/*
5478 * Return TRUE when "ufunc" has old-style "..." varargs
5479 * or named varargs "...name: type".
5480 */
5481 int
5482has_varargs(ufunc_T *ufunc)
5483{
5484 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005485}
5486
5487/*
5488 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005489 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005490 */
5491 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005492function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005493{
5494 char_u *nm = name;
5495 char_u *p;
5496 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005497 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005498 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005499
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005500 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5501 if (no_deref)
5502 flag |= TFN_NO_DEREF;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005503 p = trans_function_name(&nm, &is_global, FALSE, flag);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005504 nm = skipwhite(nm);
5505
Bram Moolenaare38eab22019-12-05 21:50:01 +01005506 // Only accept "funcname", "funcname ", "funcname (..." and
5507 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005508 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005509 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005510 vim_free(p);
5511 return n;
5512}
5513
Bram Moolenaar113e1072019-01-20 15:30:40 +01005514#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005515 char_u *
5516get_expanded_name(char_u *name, int check)
5517{
5518 char_u *nm = name;
5519 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005520 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005521
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005522 p = trans_function_name(&nm, &is_global, FALSE, TFN_INT|TFN_QUIET);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005523
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005524 if (p != NULL && *nm == NUL
5525 && (!check || translated_function_exists(p, is_global)))
5526 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005527
5528 vim_free(p);
5529 return NULL;
5530}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005531#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005532
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005533/*
5534 * Function given to ExpandGeneric() to obtain the list of user defined
5535 * function names.
5536 */
5537 char_u *
5538get_user_func_name(expand_T *xp, int idx)
5539{
5540 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005541 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005542 static hashitem_T *hi;
5543 ufunc_T *fp;
5544
5545 if (idx == 0)
5546 {
5547 done = 0;
5548 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005549 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005550 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005551 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005552 {
5553 if (done++ > 0)
5554 ++hi;
5555 while (HASHITEM_EMPTY(hi))
5556 ++hi;
5557 fp = HI2UF(hi);
5558
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005559 // don't show dead, dict and lambda functions
5560 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005561 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005562 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005563
5564 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005565 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005566
5567 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005568 if (xp->xp_context != EXPAND_USER_FUNC
5569 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005570 {
5571 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005572 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005573 STRCAT(IObuff, ")");
5574 }
5575 return IObuff;
5576 }
5577 return NULL;
5578}
5579
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005580/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005581 * Make a copy of a function.
5582 * Intended to be used for a function defined on a base class that has a copy
5583 * on the child class.
5584 * The copy has uf_refcount set to one.
5585 * Returns NULL when out of memory.
5586 */
5587 ufunc_T *
5588copy_function(ufunc_T *fp)
5589{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005590 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005591 if (ufunc == NULL)
5592 return NULL;
5593
5594 // Most things can just be copied.
5595 *ufunc = *fp;
5596
5597 ufunc->uf_def_status = UF_TO_BE_COMPILED;
5598 ufunc->uf_dfunc_idx = 0;
5599 ufunc->uf_class = NULL;
5600
5601 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
5602 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
5603
5604 if (ufunc->uf_arg_types != NULL)
5605 {
5606 // "uf_arg_types" is an allocated array, make a copy.
5607 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
5608 if (at != NULL)
5609 {
5610 mch_memmove(at, ufunc->uf_arg_types,
5611 sizeof(type_T *) * ufunc->uf_args.ga_len);
5612 ufunc->uf_arg_types = at;
5613 }
5614 }
5615
5616 // TODO: how about the types themselves? they can be freed when the
5617 // original function is freed:
5618 // type_T **uf_arg_types;
5619 // type_T *uf_ret_type;
5620
5621 ufunc->uf_type_list.ga_len = 0;
5622 ufunc->uf_type_list.ga_data = NULL;
5623
5624 // TODO: partial_T *uf_partial;
5625
5626 if (ufunc->uf_va_name != NULL)
5627 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
5628
5629 // TODO:
5630 // type_T *uf_va_type;
5631 // type_T *uf_func_type;
5632
5633 ufunc->uf_block_depth = 0;
5634 ufunc->uf_block_ids = NULL;
5635
5636 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
5637
5638 ufunc->uf_refcount = 1;
5639 ufunc->uf_name_exp = NULL;
5640 STRCPY(ufunc->uf_name, fp->uf_name);
5641
5642 return ufunc;
5643}
5644
5645/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005646 * ":delfunction {name}"
5647 */
5648 void
5649ex_delfunction(exarg_T *eap)
5650{
5651 ufunc_T *fp = NULL;
5652 char_u *p;
5653 char_u *name;
5654 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005655 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005656
5657 p = eap->arg;
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00005658 name = trans_function_name_ext(&p, &is_global, eap->skip, 0, &fudi,
5659 NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005660 vim_free(fudi.fd_newkey);
5661 if (name == NULL)
5662 {
5663 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005664 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005665 return;
5666 }
5667 if (!ends_excmd(*skipwhite(p)))
5668 {
5669 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005670 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005671 return;
5672 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005673 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005674 if (eap->nextcmd != NULL)
5675 *p = NUL;
5676
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005677 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005678 {
5679 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005680 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005681 vim_free(name);
5682 return;
5683 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005684 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005685 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005686 vim_free(name);
5687
5688 if (!eap->skip)
5689 {
5690 if (fp == NULL)
5691 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005692 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005693 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005694 return;
5695 }
5696 if (fp->uf_calls > 0)
5697 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005698 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005699 return;
5700 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005701 if (fp->uf_flags & FC_VIM9)
5702 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005703 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005704 return;
5705 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005706
5707 if (fudi.fd_dict != NULL)
5708 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005709 // Delete the dict item that refers to the function, it will
5710 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00005711 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005712 }
5713 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005714 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005715 // A normal function (not a numbered function or lambda) has a
5716 // refcount of 1 for the entry in the hashtable. When deleting
5717 // it and the refcount is more than one, it should be kept.
5718 // A numbered function and lambda should be kept if the refcount is
5719 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005720 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005721 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005722 // Function is still referenced somewhere. Don't free it but
5723 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005724 if (func_remove(fp))
5725 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005726 }
5727 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005728 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005729 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005730 }
5731}
5732
5733/*
5734 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005735 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005736 */
5737 void
5738func_unref(char_u *name)
5739{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005740 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005741
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005742 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005743 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005744 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005745 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005746 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005747#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005748 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005749#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005750 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005751 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005752 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005753}
5754
5755/*
5756 * Unreference a Function: decrement the reference count and free it when it
5757 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005758 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005759 */
5760 void
5761func_ptr_unref(ufunc_T *fp)
5762{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005763 if (fp != NULL && (--fp->uf_refcount <= 0
5764 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5765 && fp->uf_partial->pt_refcount <= 1
5766 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005767 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005768 // Only delete it when it's not being used. Otherwise it's done
5769 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005770 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005771 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005772 }
5773}
5774
5775/*
5776 * Count a reference to a Function.
5777 */
5778 void
5779func_ref(char_u *name)
5780{
5781 ufunc_T *fp;
5782
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005783 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005784 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005785 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005786 if (fp != NULL)
5787 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005788 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005789 // Only give an error for a numbered function.
5790 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005791 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005792}
5793
5794/*
5795 * Count a reference to a Function.
5796 */
5797 void
5798func_ptr_ref(ufunc_T *fp)
5799{
5800 if (fp != NULL)
5801 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005802}
5803
5804/*
5805 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5806 * referenced from anywhere that is in use.
5807 */
5808 static int
5809can_free_funccal(funccall_T *fc, int copyID)
5810{
Bram Moolenaarca16c602022-09-06 18:57:08 +01005811 return (fc->fc_l_varlist.lv_copyID != copyID
5812 && fc->fc_l_vars.dv_copyID != copyID
5813 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005814 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005815}
5816
5817/*
5818 * ":return [expr]"
5819 */
5820 void
5821ex_return(exarg_T *eap)
5822{
5823 char_u *arg = eap->arg;
5824 typval_T rettv;
5825 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005826 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005827
5828 if (current_funccal == NULL)
5829 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005830 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005831 return;
5832 }
5833
Bram Moolenaar844fb642021-10-23 13:32:30 +01005834 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005835 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5836
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005837 if (eap->skip)
5838 ++emsg_skip;
5839
5840 eap->nextcmd = NULL;
5841 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005842 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005843 {
5844 if (!eap->skip)
5845 returning = do_return(eap, FALSE, TRUE, &rettv);
5846 else
5847 clear_tv(&rettv);
5848 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005849 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005850 else if (!eap->skip)
5851 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005852 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005853 update_force_abort();
5854
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005855 /*
5856 * Return unless the expression evaluation has been cancelled due to an
5857 * aborting error, an interrupt, or an exception.
5858 */
5859 if (!aborting())
5860 returning = do_return(eap, FALSE, TRUE, NULL);
5861 }
5862
Bram Moolenaare38eab22019-12-05 21:50:01 +01005863 // When skipping or the return gets pending, advance to the next command
5864 // in this line (!returning). Otherwise, ignore the rest of the line.
5865 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005866 if (returning)
5867 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005868 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02005869 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005870
5871 if (eap->skip)
5872 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02005873 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005874}
5875
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005876 static int
5877ex_call_inner(
5878 exarg_T *eap,
5879 char_u *name,
5880 char_u **arg,
5881 char_u *startarg,
5882 funcexe_T *funcexe_init,
5883 evalarg_T *evalarg)
5884{
5885 linenr_T lnum;
5886 int doesrange;
5887 typval_T rettv;
5888 int failed = FALSE;
5889
5890 /*
5891 * When skipping, evaluate the function once, to find the end of the
5892 * arguments.
5893 * When the function takes a range, this is discovered after the first
5894 * call, and the loop is broken.
5895 */
5896 if (eap->skip)
5897 {
5898 ++emsg_skip;
5899 lnum = eap->line2; // do it once, also with an invalid range
5900 }
5901 else
5902 lnum = eap->line1;
5903 for ( ; lnum <= eap->line2; ++lnum)
5904 {
5905 funcexe_T funcexe;
5906
5907 if (!eap->skip && eap->addr_count > 0)
5908 {
5909 if (lnum > curbuf->b_ml.ml_line_count)
5910 {
5911 // If the function deleted lines or switched to another buffer
5912 // the line number may become invalid.
5913 emsg(_(e_invalid_range));
5914 break;
5915 }
5916 curwin->w_cursor.lnum = lnum;
5917 curwin->w_cursor.col = 0;
5918 curwin->w_cursor.coladd = 0;
5919 }
5920 *arg = startarg;
5921
5922 funcexe = *funcexe_init;
5923 funcexe.fe_doesrange = &doesrange;
5924 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
5925 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
5926 {
5927 failed = TRUE;
5928 break;
5929 }
5930 if (has_watchexpr())
5931 dbg_check_breakpoint(eap);
5932
5933 // Handle a function returning a Funcref, Dictionary or List.
5934 if (handle_subscript(arg, NULL, &rettv,
5935 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
5936 {
5937 failed = TRUE;
5938 break;
5939 }
5940
5941 clear_tv(&rettv);
5942 if (doesrange || eap->skip)
5943 break;
5944
5945 // Stop when immediately aborting on error, or when an interrupt
5946 // occurred or an exception was thrown but not caught.
5947 // get_func_tv() returned OK, so that the check for trailing
5948 // characters below is executed.
5949 if (aborting())
5950 break;
5951 }
5952 if (eap->skip)
5953 --emsg_skip;
5954 return failed;
5955}
5956
5957/*
5958 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
5959 * Returns FAIL or OK.
5960 */
5961 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01005962ex_defer_inner(
5963 char_u *name,
5964 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01005965 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01005966 partial_T *partial,
5967 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005968{
5969 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01005970 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005971 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01005972 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005973
5974 if (current_funccal == NULL)
5975 {
5976 semsg(_(e_str_not_inside_function), "defer");
5977 return FAIL;
5978 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01005979 if (partial != NULL)
5980 {
5981 if (partial->pt_dict != NULL)
5982 {
5983 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
5984 return FAIL;
5985 }
5986 if (partial->pt_argc > 0)
5987 {
5988 int i;
5989
5990 partial_argc = partial->pt_argc;
5991 for (i = 0; i < partial_argc; ++i)
5992 copy_tv(&partial->pt_argv[i], &argvars[i]);
5993 }
5994 }
5995 r = get_func_arguments(arg, evalarg, FALSE,
5996 argvars + partial_argc, &argcount);
5997 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01005998
5999 if (r == OK)
6000 {
6001 if (type != NULL)
6002 {
6003 // Check that the arguments are OK for the types of the funcref.
6004 r = check_argument_types(type, argvars, argcount, NULL, name);
6005 }
6006 else if (builtin_function(name, -1))
6007 {
6008 int idx = find_internal_func(name);
6009
6010 if (idx < 0)
6011 {
6012 emsg_funcname(e_unknown_function_str, name);
6013 r = FAIL;
6014 }
6015 else if (check_internal_func(idx, argcount) == -1)
6016 r = FAIL;
6017 }
6018 else
6019 {
6020 ufunc_T *ufunc = find_func(name, FALSE);
6021
6022 // we tolerate an unknown function here, it might be defined later
6023 if (ufunc != NULL)
6024 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006025 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006026 if (error != FCERR_UNKNOWN)
6027 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006028 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006029 r = FAIL;
6030 }
6031 }
6032 }
6033 }
6034
Bram Moolenaar86d87252022-09-05 21:21:25 +01006035 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006036 {
6037 while (--argcount >= 0)
6038 clear_tv(&argvars[argcount]);
6039 return FAIL;
6040 }
6041 return add_defer(name, argcount, argvars);
6042}
6043
6044/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006045 * Return TRUE if currently inside a function call.
6046 * Give an error message and return FALSE when not.
6047 */
6048 int
6049can_add_defer(void)
6050{
6051 if (!in_def_function() && get_current_funccal() == NULL)
6052 {
6053 semsg(_(e_str_not_inside_function), "defer");
6054 return FALSE;
6055 }
6056 return TRUE;
6057}
6058
6059/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006060 * Add a deferred call for "name" with arguments "argvars[argcount]".
6061 * Consumes "argvars[]".
6062 * Caller must check that in_def_function() returns TRUE or current_funccal is
6063 * not NULL.
6064 * Returns OK or FAIL.
6065 */
6066 int
6067add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6068{
6069 char_u *saved_name = vim_strsave(name);
6070 int argcount = argcount_arg;
6071 defer_T *dr;
6072 int ret = FAIL;
6073
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006074 if (saved_name == NULL)
6075 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006076 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006077 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006078 if (add_defer_function(saved_name, argcount, argvars) == OK)
6079 argcount = 0;
6080 }
6081 else
6082 {
6083 if (current_funccal->fc_defer.ga_itemsize == 0)
6084 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6085 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6086 goto theend;
6087 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6088 + current_funccal->fc_defer.ga_len++;
6089 dr->dr_name = saved_name;
6090 dr->dr_argcount = argcount;
6091 while (argcount > 0)
6092 {
6093 --argcount;
6094 dr->dr_argvars[argcount] = argvars[argcount];
6095 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006096 }
6097 ret = OK;
6098
6099theend:
6100 while (--argcount >= 0)
6101 clear_tv(&argvars[argcount]);
6102 return ret;
6103}
6104
6105/*
6106 * Invoked after a functions has finished: invoke ":defer" functions.
6107 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006108 static void
6109handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006110{
6111 int idx;
6112
Bram Moolenaar58779852022-09-06 18:31:14 +01006113 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006114 {
6115 funcexe_T funcexe;
6116 typval_T rettv;
Bram Moolenaar58779852022-09-06 18:31:14 +01006117 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006118 int i;
6119
6120 CLEAR_FIELD(funcexe);
6121 funcexe.fe_evaluate = TRUE;
6122
6123 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6124 call_func(dr->dr_name, -1, &rettv,
6125 dr->dr_argcount, dr->dr_argvars, &funcexe);
6126 clear_tv(&rettv);
6127 vim_free(dr->dr_name);
6128 for (i = dr->dr_argcount - 1; i >= 0; --i)
6129 clear_tv(&dr->dr_argvars[i]);
6130 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006131 ga_clear(&funccal->fc_defer);
6132}
6133
6134/*
6135 * Called when exiting: call all defer functions.
6136 */
6137 void
6138invoke_all_defer(void)
6139{
6140 funccall_T *funccal;
6141
Bram Moolenaarca16c602022-09-06 18:57:08 +01006142 for (funccal = current_funccal; funccal != NULL;
6143 funccal = funccal->fc_caller)
Bram Moolenaar58779852022-09-06 18:31:14 +01006144 if (funccal->fc_ectx != NULL)
6145 {
6146 // :def function
6147 unwind_def_callstack(funccal->fc_ectx);
6148 may_invoke_defer_funcs(funccal->fc_ectx);
6149 }
6150 else
6151 {
6152 // legacy function
6153 handle_defer_one(funccal);
6154 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006155}
6156
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006157/*
6158 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006159 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006160 */
6161 void
6162ex_call(exarg_T *eap)
6163{
6164 char_u *arg = eap->arg;
6165 char_u *startarg;
6166 char_u *name;
6167 char_u *tofree;
6168 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006169 int failed = FALSE;
6170 funcdict_T fudi;
6171 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006172 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006173 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006174 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006175 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006176
Bram Moolenaare6b53242020-07-01 17:28:33 +02006177 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006178 if (eap->skip)
6179 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006180 typval_T rettv;
6181
Bram Moolenaare38eab22019-12-05 21:50:01 +01006182 // trans_function_name() doesn't work well when skipping, use eval0()
6183 // instead to skip to any following command, e.g. for:
6184 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006185 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006186 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006187 clear_tv(&rettv);
6188 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006189 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006190 return;
6191 }
6192
Bram Moolenaar99a7c0d2023-02-21 19:55:14 +00006193 tofree = trans_function_name_ext(&arg, NULL, eap->skip, TFN_INT,
6194 &fudi, &partial, vim9script ? &type : NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006195 if (fudi.fd_newkey != NULL)
6196 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006197 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006198 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006199 vim_free(fudi.fd_newkey);
6200 }
6201 if (tofree == NULL)
6202 return;
6203
Bram Moolenaare38eab22019-12-05 21:50:01 +01006204 // Increase refcount on dictionary, it could get deleted when evaluating
6205 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006206 if (fudi.fd_dict != NULL)
6207 ++fudi.fd_dict->dv_refcount;
6208
Bram Moolenaare38eab22019-12-05 21:50:01 +01006209 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6210 // contents. For VAR_PARTIAL get its partial, unless we already have one
6211 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006212 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006213 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006214 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006215 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006216
Bram Moolenaare38eab22019-12-05 21:50:01 +01006217 // Skip white space to allow ":call func ()". Not good, but required for
6218 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006219 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006220 if (*startarg != '(')
6221 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006222 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006223 goto end;
6224 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006225 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006226 {
6227 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6228 goto end;
6229 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006230
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006231 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006232 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006233 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006234 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006235 }
6236 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006237 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006238 funcexe_T funcexe;
6239
Bram Moolenaara80faa82020-04-12 19:37:17 +02006240 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006241 funcexe.fe_check_type = type;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006242 funcexe.fe_partial = partial;
6243 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006244 funcexe.fe_firstline = eap->line1;
6245 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006246 funcexe.fe_found_var = found_var;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006247 funcexe.fe_evaluate = !eap->skip;
6248 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006249 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006250
Bram Moolenaar1d341892021-09-18 15:25:52 +02006251 // When inside :try we need to check for following "| catch" or "| endtry".
6252 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006253 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006254 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006255 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006256 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006257 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006258 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006259 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006260 {
6261 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006262 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006263 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006264 }
6265 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006266 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006267 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006268 // Must be after using "arg", it may point into memory cleared here.
6269 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006270
6271end:
6272 dict_unref(fudi.fd_dict);
6273 vim_free(tofree);
6274}
6275
6276/*
6277 * Return from a function. Possibly makes the return pending. Also called
6278 * for a pending return at the ":endtry" or after returning from an extra
6279 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6280 * when called due to a ":return" command. "rettv" may point to a typval_T
6281 * with the return rettv. Returns TRUE when the return can be carried out,
6282 * FALSE when the return gets pending.
6283 */
6284 int
6285do_return(
6286 exarg_T *eap,
6287 int reanimate,
6288 int is_cmd,
6289 void *rettv)
6290{
6291 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006292 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006293
6294 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006295 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006296 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006297
6298 /*
6299 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6300 * not in its finally clause (which then is to be executed next) is found.
6301 * In this case, make the ":return" pending for execution at the ":endtry".
6302 * Otherwise, return normally.
6303 */
6304 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6305 if (idx >= 0)
6306 {
6307 cstack->cs_pending[idx] = CSTP_RETURN;
6308
6309 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006310 // A pending return again gets pending. "rettv" points to an
6311 // allocated variable with the rettv of the original ":return"'s
6312 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006313 cstack->cs_rettv[idx] = rettv;
6314 else
6315 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006316 // When undoing a return in order to make it pending, get the stored
6317 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006318 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006319 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006320
6321 if (rettv != NULL)
6322 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006323 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006324 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6325 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6326 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006327 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006328 }
6329 else
6330 cstack->cs_rettv[idx] = NULL;
6331
6332 if (reanimate)
6333 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006334 // The pending return value could be overwritten by a ":return"
6335 // without argument in a finally clause; reset the default
6336 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006337 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6338 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006339 }
6340 }
6341 report_make_pending(CSTP_RETURN, rettv);
6342 }
6343 else
6344 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006345 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006346
Bram Moolenaare38eab22019-12-05 21:50:01 +01006347 // If the return is carried out now, store the return value. For
6348 // a return immediately after reanimation, the value is already
6349 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006350 if (!reanimate && rettv != NULL)
6351 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006352 clear_tv(current_funccal->fc_rettv);
6353 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006354 if (!is_cmd)
6355 vim_free(rettv);
6356 }
6357 }
6358
6359 return idx < 0;
6360}
6361
6362/*
6363 * Free the variable with a pending return value.
6364 */
6365 void
6366discard_pending_return(void *rettv)
6367{
6368 free_tv((typval_T *)rettv);
6369}
6370
6371/*
6372 * Generate a return command for producing the value of "rettv". The result
6373 * is an allocated string. Used by report_pending() for verbose messages.
6374 */
6375 char_u *
6376get_return_cmd(void *rettv)
6377{
6378 char_u *s = NULL;
6379 char_u *tofree = NULL;
6380 char_u numbuf[NUMBUFLEN];
6381
6382 if (rettv != NULL)
6383 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6384 if (s == NULL)
6385 s = (char_u *)"";
6386
6387 STRCPY(IObuff, ":return ");
6388 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6389 if (STRLEN(s) + 8 >= IOSIZE)
6390 STRCPY(IObuff + IOSIZE - 4, "...");
6391 vim_free(tofree);
6392 return vim_strsave(IObuff);
6393}
6394
6395/*
6396 * Get next function line.
6397 * Called by do_cmdline() to get the next line.
6398 * Returns allocated string, or NULL for end of function.
6399 */
6400 char_u *
6401get_func_line(
6402 int c UNUSED,
6403 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006404 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006405 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006406{
6407 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006408 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006409 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006410 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006411
Bram Moolenaare38eab22019-12-05 21:50:01 +01006412 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006413 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006414 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006415 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006416 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006417 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006418 }
6419#ifdef FEAT_PROFILE
6420 if (do_profiling == PROF_YES)
6421 func_line_end(cookie);
6422#endif
6423
6424 gap = &fp->uf_lines;
6425 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006426 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006427 retval = NULL;
6428 else
6429 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006430 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006431 while (fcp->fc_linenr < gap->ga_len
6432 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6433 ++fcp->fc_linenr;
6434 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006435 retval = NULL;
6436 else
6437 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006438 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6439 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006440#ifdef FEAT_PROFILE
6441 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006442 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006443#endif
6444 }
6445 }
6446
Bram Moolenaare38eab22019-12-05 21:50:01 +01006447 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006448 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006449 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006450 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006451 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006452 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006453 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006454 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006455 }
6456
6457 return retval;
6458}
6459
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006460/*
6461 * Return TRUE if the currently active function should be ended, because a
6462 * return was encountered or an error occurred. Used inside a ":while".
6463 */
6464 int
6465func_has_ended(void *cookie)
6466{
6467 funccall_T *fcp = (funccall_T *)cookie;
6468
Bram Moolenaare38eab22019-12-05 21:50:01 +01006469 // Ignore the "abort" flag if the abortion behavior has been changed due to
6470 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006471 return (((fcp->fc_func->uf_flags & FC_ABORT)
6472 && did_emsg && !aborted_in_try())
6473 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006474}
6475
6476/*
6477 * return TRUE if cookie indicates a function which "abort"s on errors.
6478 */
6479 int
6480func_has_abort(
6481 void *cookie)
6482{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006483 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006484}
6485
6486
6487/*
6488 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6489 * Don't do this when "Func" is already a partial that was bound
6490 * explicitly (pt_auto is FALSE).
6491 * Changes "rettv" in-place.
6492 * Returns the updated "selfdict_in".
6493 */
6494 dict_T *
6495make_partial(dict_T *selfdict_in, typval_T *rettv)
6496{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006497 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006498 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006499 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006500 dict_T *selfdict = selfdict_in;
6501
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006502 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6503 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006504 fp = rettv->vval.v_partial->pt_func;
6505 else
6506 {
6507 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006508 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006509 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006510 if (fname == NULL)
6511 {
6512 // There is no point binding a dict to a NULL function, just create
6513 // a function reference.
6514 rettv->v_type = VAR_FUNC;
6515 rettv->vval.v_string = NULL;
6516 }
6517 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006518 {
6519 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006520 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006521
6522 // Translate "s:func" to the stored function name.
6523 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6524 fp = find_func(fname, FALSE);
6525 vim_free(tofree);
6526 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006527 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006528
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006529 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006530 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006531 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006532
6533 if (pt != NULL)
6534 {
6535 pt->pt_refcount = 1;
6536 pt->pt_dict = selfdict;
6537 pt->pt_auto = TRUE;
6538 selfdict = NULL;
6539 if (rettv->v_type == VAR_FUNC)
6540 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006541 // Just a function: Take over the function name and use
6542 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006543 pt->pt_name = rettv->vval.v_string;
6544 }
6545 else
6546 {
6547 partial_T *ret_pt = rettv->vval.v_partial;
6548 int i;
6549
Bram Moolenaare38eab22019-12-05 21:50:01 +01006550 // Partial: copy the function name, use selfdict and copy
6551 // args. Can't take over name or args, the partial might
6552 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006553 if (ret_pt->pt_name != NULL)
6554 {
6555 pt->pt_name = vim_strsave(ret_pt->pt_name);
6556 func_ref(pt->pt_name);
6557 }
6558 else
6559 {
6560 pt->pt_func = ret_pt->pt_func;
6561 func_ptr_ref(pt->pt_func);
6562 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006563 if (ret_pt->pt_argc > 0)
6564 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006565 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006566 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006567 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006568 pt->pt_argc = 0;
6569 else
6570 {
6571 pt->pt_argc = ret_pt->pt_argc;
6572 for (i = 0; i < pt->pt_argc; i++)
6573 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6574 }
6575 }
6576 partial_unref(ret_pt);
6577 }
6578 rettv->v_type = VAR_PARTIAL;
6579 rettv->vval.v_partial = pt;
6580 }
6581 }
6582 return selfdict;
6583}
6584
6585/*
6586 * Return the name of the executed function.
6587 */
6588 char_u *
6589func_name(void *cookie)
6590{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006591 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006592}
6593
6594/*
6595 * Return the address holding the next breakpoint line for a funccall cookie.
6596 */
6597 linenr_T *
6598func_breakpoint(void *cookie)
6599{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006600 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006601}
6602
6603/*
6604 * Return the address holding the debug tick for a funccall cookie.
6605 */
6606 int *
6607func_dbg_tick(void *cookie)
6608{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006609 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006610}
6611
6612/*
6613 * Return the nesting level for a funccall cookie.
6614 */
6615 int
6616func_level(void *cookie)
6617{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006618 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006619}
6620
6621/*
6622 * Return TRUE when a function was ended by a ":return" command.
6623 */
6624 int
6625current_func_returned(void)
6626{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006627 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006628}
6629
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006630 int
6631free_unref_funccal(int copyID, int testing)
6632{
6633 int did_free = FALSE;
6634 int did_free_funccal = FALSE;
6635 funccall_T *fc, **pfc;
6636
6637 for (pfc = &previous_funccal; *pfc != NULL; )
6638 {
6639 if (can_free_funccal(*pfc, copyID))
6640 {
6641 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006642 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006643 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006644 did_free = TRUE;
6645 did_free_funccal = TRUE;
6646 }
6647 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006648 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006649 }
6650 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006651 // When a funccal was freed some more items might be garbage
6652 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006653 (void)garbage_collect(testing);
6654
6655 return did_free;
6656}
6657
6658/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006659 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006660 */
6661 static funccall_T *
6662get_funccal(void)
6663{
6664 int i;
6665 funccall_T *funccal;
6666 funccall_T *temp_funccal;
6667
6668 funccal = current_funccal;
6669 if (debug_backtrace_level > 0)
6670 {
6671 for (i = 0; i < debug_backtrace_level; i++)
6672 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006673 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006674 if (temp_funccal)
6675 funccal = temp_funccal;
6676 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006677 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006678 debug_backtrace_level = i;
6679 }
6680 }
6681 return funccal;
6682}
6683
6684/*
6685 * Return the hashtable used for local variables in the current funccal.
6686 * Return NULL if there is no current funccal.
6687 */
6688 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006689get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006690{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006691 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006692 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006693 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006694}
6695
6696/*
6697 * Return the l: scope variable.
6698 * Return NULL if there is no current funccal.
6699 */
6700 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006701get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006702{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006703 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006704 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006705 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006706}
6707
6708/*
6709 * Return the hashtable used for argument in the current funccal.
6710 * Return NULL if there is no current funccal.
6711 */
6712 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006713get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006714{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006715 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006716 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006717 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006718}
6719
6720/*
6721 * Return the a: scope variable.
6722 * Return NULL if there is no current funccal.
6723 */
6724 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006725get_funccal_args_var(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_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006730}
6731
6732/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006733 * List function variables, if there is a function.
6734 */
6735 void
6736list_func_vars(int *first)
6737{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006738 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
6739 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006740 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006741}
6742
6743/*
6744 * If "ht" is the hashtable for local variables in the current funccal, return
6745 * the dict that contains it.
6746 * Otherwise return NULL.
6747 */
6748 dict_T *
6749get_current_funccal_dict(hashtab_T *ht)
6750{
6751 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01006752 && ht == &current_funccal->fc_l_vars.dv_hashtab)
6753 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006754 return NULL;
6755}
6756
6757/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006758 * Search hashitem in parent scope.
6759 */
6760 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006761find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006762{
6763 funccall_T *old_current_funccal = current_funccal;
6764 hashtab_T *ht;
6765 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006766 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006767
Bram Moolenaarca16c602022-09-06 18:57:08 +01006768 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006769 return NULL;
6770
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006771 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006772 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006773 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006774 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006775 ht = find_var_ht(name, &varname);
6776 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006777 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006778 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006779 if (!HASHITEM_EMPTY(hi))
6780 {
6781 *pht = ht;
6782 break;
6783 }
6784 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006785 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02006786 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006787 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006788 }
6789 current_funccal = old_current_funccal;
6790
6791 return hi;
6792}
6793
6794/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006795 * Search variable in parent scope.
6796 */
6797 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006798find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006799{
6800 dictitem_T *v = NULL;
6801 funccall_T *old_current_funccal = current_funccal;
6802 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006803 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006804
Bram Moolenaarca16c602022-09-06 18:57:08 +01006805 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006806 return NULL;
6807
Bram Moolenaare38eab22019-12-05 21:50:01 +01006808 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01006809 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006810 while (current_funccal)
6811 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006812 ht = find_var_ht(name, &varname);
6813 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006814 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006815 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006816 if (v != NULL)
6817 break;
6818 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006819 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006820 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006821 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006822 }
6823 current_funccal = old_current_funccal;
6824
6825 return v;
6826}
6827
6828/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006829 * Set "copyID + 1" in previous_funccal and callers.
6830 */
6831 int
6832set_ref_in_previous_funccal(int copyID)
6833{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006834 funccall_T *fc;
6835
Bram Moolenaarca16c602022-09-06 18:57:08 +01006836 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006837 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006838 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006839 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
6840 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
6841 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006842 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006843 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006844 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006845}
6846
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006847 static int
6848set_ref_in_funccal(funccall_T *fc, int copyID)
6849{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006850 if (fc->fc_copyID != copyID)
6851 {
6852 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006853 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
6854 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
6855 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
6856 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006857 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006858 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006859 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006860}
6861
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006862/*
6863 * Set "copyID" in all local vars and arguments in the call stack.
6864 */
6865 int
6866set_ref_in_call_stack(int copyID)
6867{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006868 funccall_T *fc;
6869 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006870
Bram Moolenaarca16c602022-09-06 18:57:08 +01006871 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006872 if (set_ref_in_funccal(fc, copyID))
6873 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006874
6875 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006876 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006877 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006878 if (set_ref_in_funccal(fc, copyID))
6879 return TRUE;
6880 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006881}
6882
6883/*
6884 * Set "copyID" in all functions available by name.
6885 */
6886 int
6887set_ref_in_functions(int copyID)
6888{
6889 int todo;
6890 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006891 ufunc_T *fp;
6892
6893 todo = (int)func_hashtab.ht_used;
6894 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006895 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006896 if (!HASHITEM_EMPTY(hi))
6897 {
6898 --todo;
6899 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006900 if (!func_name_refcount(fp->uf_name)
6901 && set_ref_in_func(NULL, fp, copyID))
6902 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006903 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006904 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006905 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006906}
6907
6908/*
6909 * Set "copyID" in all function arguments.
6910 */
6911 int
6912set_ref_in_func_args(int copyID)
6913{
6914 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006915
6916 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006917 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
6918 copyID, NULL, NULL))
6919 return TRUE;
6920 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006921}
6922
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006923/*
6924 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006925 * Returns TRUE if setting references failed somehow.
6926 */
6927 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006928set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006929{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006930 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006931 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006932 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006933 char_u fname_buf[FLEN_FIXED + 1];
6934 char_u *tofree = NULL;
6935 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006936 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006937
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006938 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006939 return FALSE;
6940
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006941 if (fp_in == NULL)
6942 {
6943 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006944 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006945 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006946 if (fp != NULL)
6947 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006948 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006949 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006950 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02006951
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006952 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006953 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006954}
6955
Bram Moolenaare38eab22019-12-05 21:50:01 +01006956#endif // FEAT_EVAL