blob: 758b9eac5fa2580b9fc6b6fd7ed8629978800c30 [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
39func_init()
40{
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 Moolenaar9f1a39a2022-01-08 15:39:39 +0000195 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 Moolenaarffdaca92022-12-09 21:41:48 +0000218 int in_class, // TRUE when inside a class
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.
528 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
529 [fp->uf_args.ga_len - 1];
530 --fp->uf_args.ga_len;
531 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 Moolenaar801ab062020-06-25 19:27:56 +0200644#if defined(FEAT_LUA) || defined(PROTO)
645/*
646 * Registers a native C callback which can be called from Vim script.
647 * Returns the name of the Vim script function.
648 */
649 char_u *
650register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
651{
652 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200653 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200654
655 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
656 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200657 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200658
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200659 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200660 fp->uf_refcount = 1;
661 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000662 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200663 fp->uf_calls = 0;
664 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200665 fp->uf_cb = cb;
666 fp->uf_cb_free = cb_free;
667 fp->uf_cb_state = state;
668
669 set_ufunc_name(fp, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000670 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200671
672 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200673}
674#endif
675
Bram Moolenaar04b12692020-05-04 23:24:44 +0200676/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100677 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100678 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100679 * If "white_error" is not NULL check for correct use of white space and set
680 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100681 * Return NULL if no valid arrow found.
682 */
683 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100684skip_arrow(
685 char_u *start,
686 int equal_arrow,
687 char_u **ret_type,
688 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100689{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100690 char_u *s = start;
691 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100692
693 if (equal_arrow)
694 {
695 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100696 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100697 if (white_error != NULL && !VIM_ISWHITE(s[1]))
698 {
699 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100700 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100701 return NULL;
702 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100703 s = skipwhite(s + 1);
704 *ret_type = s;
705 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100706 if (s == *ret_type)
707 {
708 emsg(_(e_missing_return_type));
709 return NULL;
710 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100711 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100712 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100713 s = skipwhite(s);
714 if (*s != '=')
715 return NULL;
716 ++s;
717 }
718 if (*s != '>')
719 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100720 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
721 || !IS_WHITE_OR_NUL(s[1])))
722 {
723 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100724 semsg(_(e_white_space_required_before_and_after_str_at_str),
725 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100726 return NULL;
727 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100728 return skipwhite(s + 1);
729}
730
731/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100732 * Check if "*cmd" points to a function command and if so advance "*cmd" and
733 * return TRUE.
734 * Otherwise return FALSE;
735 * Do not consider "function(" to be a command.
736 */
737 static int
738is_function_cmd(char_u **cmd)
739{
740 char_u *p = *cmd;
741
742 if (checkforcmd(&p, "function", 2))
743 {
744 if (*p == '(')
745 return FALSE;
746 *cmd = p;
747 return TRUE;
748 }
749 return FALSE;
750}
751
752/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200753 * Called when defining a function: The context may be needed for script
754 * variables declared in a block that is visible now but not when the function
755 * is compiled or called later.
756 */
757 static void
758function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
759{
760 if (cstack != NULL && cstack->cs_idx >= 0)
761 {
762 int count = cstack->cs_idx + 1;
763 int i;
764
765 fp->uf_block_ids = ALLOC_MULT(int, count);
766 if (fp->uf_block_ids != NULL)
767 {
768 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
769 sizeof(int) * count);
770 fp->uf_block_depth = count;
771 }
772
773 // Set flag in each block to indicate a function was defined. This
774 // is used to keep the variable when leaving the block, see
775 // hide_script_var().
776 for (i = 0; i <= cstack->cs_idx; ++i)
777 cstack->cs_flags[i] |= CSF_FUNC_DEF;
778 }
779}
780
781/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100782 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200783 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100784 * "newlines" must already have been initialized.
785 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
786 */
787 static int
788get_function_body(
789 exarg_T *eap,
790 garray_T *newlines,
791 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000792 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100793{
794 linenr_T sourcing_lnum_top = SOURCING_LNUM;
795 linenr_T sourcing_lnum_off;
796 int saved_wait_return = need_wait_return;
797 char_u *line_arg = line_arg_in;
798 int vim9_function = eap->cmdidx == CMD_def
799 || eap->cmdidx == CMD_block;
800#define MAX_FUNC_NESTING 50
801 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200802 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100803 int nesting = 0;
804 getline_opt_T getline_options;
805 int indent = 2;
806 char_u *skip_until = NULL;
807 int ret = FAIL;
808 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200809 int heredoc_concat_len = 0;
810 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100811 char_u *heredoc_trimmed = NULL;
812
Bram Moolenaar20677332021-06-06 17:02:53 +0200813 ga_init2(&heredoc_ga, 1, 500);
814
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100815 // Detect having skipped over comment lines to find the return
816 // type. Add NULL lines to keep the line count correct.
817 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
818 if (SOURCING_LNUM < sourcing_lnum_off)
819 {
820 sourcing_lnum_off -= SOURCING_LNUM;
821 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
822 goto theend;
823 while (sourcing_lnum_off-- > 0)
824 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
825 }
826
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200827 nesting_def[0] = vim9_function;
828 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100829 getline_options = vim9_function
830 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
831 for (;;)
832 {
833 char_u *theline;
834 char_u *p;
835 char_u *arg;
836
837 if (KeyTyped)
838 {
839 msg_scroll = TRUE;
840 saved_wait_return = FALSE;
841 }
842 need_wait_return = FALSE;
843
844 if (line_arg != NULL)
845 {
846 // Use eap->arg, split up in parts by line breaks.
847 theline = line_arg;
848 p = vim_strchr(theline, '\n');
849 if (p == NULL)
850 line_arg += STRLEN(line_arg);
851 else
852 {
853 *p = NUL;
854 line_arg = p + 1;
855 }
856 }
857 else
858 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000859 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100860 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100861 }
862 if (KeyTyped)
863 lines_left = Rows - 1;
864 if (theline == NULL)
865 {
866 // Use the start of the function for the line number.
867 SOURCING_LNUM = sourcing_lnum_top;
868 if (skip_until != NULL)
869 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200870 else if (nesting_inline[nesting])
871 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100872 else if (eap->cmdidx == CMD_def)
873 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100874 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000875 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100876 goto theend;
877 }
878
879 // Detect line continuation: SOURCING_LNUM increased more than one.
880 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
881 if (SOURCING_LNUM < sourcing_lnum_off)
882 sourcing_lnum_off -= SOURCING_LNUM;
883 else
884 sourcing_lnum_off = 0;
885
886 if (skip_until != NULL)
887 {
888 // Don't check for ":endfunc"/":enddef" between
889 // * ":append" and "."
890 // * ":python <<EOF" and "EOF"
891 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
892 if (heredoc_trimmed == NULL
893 || (is_heredoc && skipwhite(theline) == theline)
894 || STRNCMP(theline, heredoc_trimmed,
895 STRLEN(heredoc_trimmed)) == 0)
896 {
897 if (heredoc_trimmed == NULL)
898 p = theline;
899 else if (is_heredoc)
900 p = skipwhite(theline) == theline
901 ? theline : theline + STRLEN(heredoc_trimmed);
902 else
903 p = theline + STRLEN(heredoc_trimmed);
904 if (STRCMP(p, skip_until) == 0)
905 {
906 VIM_CLEAR(skip_until);
907 VIM_CLEAR(heredoc_trimmed);
908 getline_options = vim9_function
909 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
910 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200911
912 if (heredoc_concat_len > 0)
913 {
914 // Replace the starting line with all the concatenated
915 // lines.
916 ga_concat(&heredoc_ga, theline);
917 vim_free(((char_u **)(newlines->ga_data))[
918 heredoc_concat_len - 1]);
919 ((char_u **)(newlines->ga_data))[
920 heredoc_concat_len - 1] = heredoc_ga.ga_data;
921 ga_init(&heredoc_ga);
922 heredoc_concat_len = 0;
923 theline += STRLEN(theline); // skip the "EOF"
924 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100925 }
926 }
927 }
928 else
929 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200930 int c;
931 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +0000932 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100933
934 // skip ':' and blanks
935 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
936 ;
937
938 // Check for "endfunction", "enddef" or "}".
939 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +0000940 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200941 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100942 ? *p == '}'
943 : (checkforcmd(&p, nesting_def[nesting]
944 ? "enddef" : "endfunction", 4)
945 && *p != ':'))
946 {
Bram Moolenaarb2175222022-03-05 20:24:41 +0000947 if (!nesting_inline[nesting] && nesting_def[nesting]
948 && p < cmd + 6)
949 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100950 if (nesting-- == 0)
951 {
952 char_u *nextcmd = NULL;
953
954 if (*p == '|' || *p == '}')
955 nextcmd = p + 1;
956 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
957 nextcmd = line_arg;
958 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100959 && (vim9_function || p_verbose > 0))
960 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200961 SOURCING_LNUM = sourcing_lnum_top
962 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100963 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000964 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100965 else
966 give_warning2((char_u *)
967 _("W22: Text found after :endfunction: %s"),
968 p, TRUE);
969 }
970 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100971 {
972 // Another command follows. If the line came from "eap"
973 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000974 // change "eap->cmdlinep" to point to the last fetched
975 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100976 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000977 if (lines_to_free->ga_len > 0
978 && *eap->cmdlinep !=
979 ((char_u **)lines_to_free->ga_data)
980 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100981 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000982 // *cmdlinep will be freed later, thus remove the
983 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100984 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000985 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
986 [lines_to_free->ga_len - 1];
987 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100988 }
989 }
990 break;
991 }
992 }
993
994 // Check for mismatched "endfunc" or "enddef".
995 // We don't check for "def" inside "func" thus we also can't check
996 // for "enddef".
997 // We continue to find the end of the function, although we might
998 // not find it.
999 else if (nesting_def[nesting])
1000 {
1001 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1002 emsg(_(e_mismatched_endfunction));
1003 }
1004 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1005 emsg(_(e_mismatched_enddef));
1006
1007 // Increase indent inside "if", "while", "for" and "try", decrease
1008 // at "end".
1009 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1010 indent -= 2;
1011 else if (STRNCMP(p, "if", 2) == 0
1012 || STRNCMP(p, "wh", 2) == 0
1013 || STRNCMP(p, "for", 3) == 0
1014 || STRNCMP(p, "try", 3) == 0)
1015 indent += 2;
1016
1017 // Check for defining a function inside this function.
1018 // Only recognize "def" inside "def", not inside "function",
1019 // For backwards compatibility, see Test_function_python().
1020 c = *p;
1021 if (is_function_cmd(&p)
1022 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1023 {
1024 if (*p == '!')
1025 p = skipwhite(p + 1);
1026 p += eval_fname_script(p);
1027 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL,
1028 NULL, NULL));
1029 if (*skipwhite(p) == '(')
1030 {
1031 if (nesting == MAX_FUNC_NESTING - 1)
1032 emsg(_(e_function_nesting_too_deep));
1033 else
1034 {
1035 ++nesting;
1036 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001037 nesting_inline[nesting] = FALSE;
1038 indent += 2;
1039 }
1040 }
1041 }
1042
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001043 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001044 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001045 // Not a comment line: check for nested inline function.
1046 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001047 while (end > p && VIM_ISWHITE(*end))
1048 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001049 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001050 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001051 int is_block;
1052
1053 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001054 --end;
1055 while (end > p && VIM_ISWHITE(*end))
1056 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001057 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1058 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001059 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001060 char_u *s = p;
1061
1062 // check for line starting with "au" for :autocmd or
1063 // "com" for :command, these can use a {} block
1064 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1065 || checkforcmd_noparen(&s, "command", 3);
1066 }
1067
1068 if (is_block)
1069 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001070 if (nesting == MAX_FUNC_NESTING - 1)
1071 emsg(_(e_function_nesting_too_deep));
1072 else
1073 {
1074 ++nesting;
1075 nesting_def[nesting] = TRUE;
1076 nesting_inline[nesting] = TRUE;
1077 indent += 2;
1078 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001079 }
1080 }
1081 }
1082
1083 // Check for ":append", ":change", ":insert". Not for :def.
1084 p = skip_range(p, FALSE, NULL);
1085 if (!vim9_function
1086 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1087 || (p[0] == 'c'
1088 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1089 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1090 && (STRNCMP(&p[3], "nge", 3) != 0
1091 || !ASCII_ISALPHA(p[6])))))))
1092 || (p[0] == 'i'
1093 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1094 && (!ASCII_ISALPHA(p[2])
1095 || (p[2] == 's'
1096 && (!ASCII_ISALPHA(p[3])
1097 || p[3] == 'e'))))))))
1098 skip_until = vim_strsave((char_u *)".");
1099
1100 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1101 arg = skipwhite(skiptowhite(p));
1102 if (arg[0] == '<' && arg[1] =='<'
1103 && ((p[0] == 'p' && p[1] == 'y'
1104 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1105 || ((p[2] == '3' || p[2] == 'x')
1106 && !ASCII_ISALPHA(p[3]))))
1107 || (p[0] == 'p' && p[1] == 'e'
1108 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1109 || (p[0] == 't' && p[1] == 'c'
1110 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1111 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1112 && !ASCII_ISALPHA(p[3]))
1113 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1114 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1115 || (p[0] == 'm' && p[1] == 'z'
1116 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1117 ))
1118 {
1119 // ":python <<" continues until a dot, like ":append"
1120 p = skipwhite(arg + 2);
1121 if (STRNCMP(p, "trim", 4) == 0)
1122 {
1123 // Ignore leading white space.
1124 p = skipwhite(p + 4);
1125 heredoc_trimmed = vim_strnsave(theline,
1126 skipwhite(theline) - theline);
1127 }
1128 if (*p == NUL)
1129 skip_until = vim_strsave((char_u *)".");
1130 else
1131 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1132 getline_options = GETLINE_NONE;
1133 is_heredoc = TRUE;
Bram Moolenaard881d152022-05-13 13:50:36 +01001134 if (eap->cmdidx == CMD_def && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001135 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001136 }
1137
Bram Moolenaard881d152022-05-13 13:50:36 +01001138 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001139 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001140 // Check for ":cmd v =<< [trim] EOF"
1141 // and ":cmd [a, b] =<< [trim] EOF"
1142 // and "lines =<< [trim] EOF" for Vim9
1143 // Where "cmd" can be "let", "var", "final" or "const".
1144 arg = skipwhite(skiptowhite(p));
1145 if (*arg == '[')
1146 arg = vim_strchr(arg, ']');
1147 if (arg != NULL)
1148 {
1149 int found = (eap->cmdidx == CMD_def && arg[0] == '='
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001150 && arg[1] == '<' && arg[2] =='<');
1151
Bram Moolenaard881d152022-05-13 13:50:36 +01001152 if (!found)
1153 // skip over the argument after "cmd"
1154 arg = skipwhite(skiptowhite(arg));
1155 if (found || (arg[0] == '=' && arg[1] == '<'
1156 && arg[2] =='<'
1157 && (checkforcmd(&p, "let", 2)
1158 || checkforcmd(&p, "var", 3)
1159 || checkforcmd(&p, "final", 5)
1160 || checkforcmd(&p, "const", 5))))
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001161 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001162 p = skipwhite(arg + 3);
1163 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001164 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001165 if (STRNCMP(p, "trim", 4) == 0)
1166 {
1167 // Ignore leading white space.
1168 p = skipwhite(p + 4);
1169 heredoc_trimmed = vim_strnsave(theline,
1170 skipwhite(theline) - theline);
1171 continue;
1172 }
1173 if (STRNCMP(p, "eval", 4) == 0)
1174 {
1175 // Ignore leading white space.
1176 p = skipwhite(p + 4);
1177 continue;
1178 }
1179 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001180 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001181 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1182 getline_options = GETLINE_NONE;
1183 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001184 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001185 }
1186 }
1187 }
1188
1189 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001190 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001191 goto theend;
1192
Bram Moolenaar20677332021-06-06 17:02:53 +02001193 if (heredoc_concat_len > 0)
1194 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001195 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001196 // to be used for the instruction later.
1197 ga_concat(&heredoc_ga, theline);
1198 ga_concat(&heredoc_ga, (char_u *)"\n");
1199 p = vim_strsave((char_u *)"");
1200 }
1201 else
1202 {
1203 // Copy the line to newly allocated memory. get_one_sourceline()
1204 // allocates 250 bytes per line, this saves 80% on average. The
1205 // cost is an extra alloc/free.
1206 p = vim_strsave(theline);
1207 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001208 if (p == NULL)
1209 goto theend;
1210 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1211
1212 // Add NULL lines for continuation lines, so that the line count is
1213 // equal to the index in the growarray.
1214 while (sourcing_lnum_off-- > 0)
1215 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1216
1217 // Check for end of eap->arg.
1218 if (line_arg != NULL && *line_arg == NUL)
1219 line_arg = NULL;
1220 }
1221
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001222 // Return OK when no error was detected.
1223 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001224 ret = OK;
1225
1226theend:
1227 vim_free(skip_until);
1228 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001229 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001230 need_wait_return |= saved_wait_return;
1231 return ret;
1232}
1233
1234/*
1235 * Handle the body of a lambda. *arg points to the "{", process statements
1236 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001237 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001238 * When successful "rettv" is set to a funcref.
1239 */
1240 static int
1241lambda_function_body(
1242 char_u **arg,
1243 typval_T *rettv,
1244 evalarg_T *evalarg,
1245 garray_T *newargs,
1246 garray_T *argtypes,
1247 int varargs,
1248 garray_T *default_args,
1249 char_u *ret_type)
1250{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001251 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001252 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001253 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001254 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001255 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001256 exarg_T eap;
1257 garray_T newlines;
1258 char_u *cmdline = NULL;
1259 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001260 partial_T *pt;
1261 char_u *name;
1262 int lnum_save = -1;
1263 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1264
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001265 *arg = skipwhite(*arg + 1);
1266 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001267 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001268 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001269 return FAIL;
1270 }
1271
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001272 CLEAR_FIELD(eap);
1273 eap.cmdidx = CMD_block;
1274 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001275 eap.cmdlinep = &cmdline;
1276 eap.skip = !evaluate;
1277 if (evalarg->eval_cctx != NULL)
1278 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1279 else
1280 {
1281 eap.getline = evalarg->eval_getline;
1282 eap.cookie = evalarg->eval_cookie;
1283 }
1284
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001285 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001286 if (get_function_body(&eap, &newlines, NULL,
1287 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001288 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001289
1290 // When inside a lambda must add the function lines to evalarg.eval_ga.
1291 evalarg->eval_break_count += newlines.ga_len;
1292 if (gap->ga_itemsize > 0)
1293 {
1294 int idx;
1295 char_u *last;
1296 size_t plen;
1297 char_u *pnl;
1298
1299 for (idx = 0; idx < newlines.ga_len; ++idx)
1300 {
1301 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1302
Bram Moolenaarecb66452021-05-18 15:09:18 +02001303 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001304 goto erret;
1305
1306 // Going to concatenate the lines after parsing. For an empty or
1307 // comment line use an empty string.
1308 // Insert NL characters at the start of each line, the string will
1309 // be split again later in .get_lambda_tv().
1310 if (*p == NUL || vim9_comment_start(p))
1311 p = (char_u *)"";
1312 plen = STRLEN(p);
1313 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1314 if (pnl != NULL)
1315 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001316 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1317 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001318 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001319 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001320 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001321 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001322 // more is following after the "}", which was skipped
1323 last = cmdline;
1324 else
1325 // nothing is following the "}"
1326 last = (char_u *)"}";
1327 plen = STRLEN(last);
1328 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1329 if (pnl != NULL)
1330 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001331 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1332 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001333 }
1334
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001335 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001336 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001337 garray_T *tfgap = &evalarg->eval_tofree_ga;
1338
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001339 // Something comes after the "}".
1340 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001341
1342 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001343 if (ga_grow(tfgap, 1) == OK)
1344 {
1345 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1346 evalarg->eval_using_cmdline = TRUE;
1347 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001348 }
1349 else
1350 *arg = (char_u *)"";
1351
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001352 if (!evaluate)
1353 {
1354 ret = OK;
1355 goto erret;
1356 }
1357
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001358 name = get_lambda_name();
1359 ufunc = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
1360 if (ufunc == NULL)
1361 goto erret;
1362 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001363 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001364 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001365 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001366 ufunc->uf_refcount = 1;
1367 ufunc->uf_args = *newargs;
1368 newargs->ga_data = NULL;
1369 ufunc->uf_def_args = *default_args;
1370 default_args->ga_data = NULL;
1371 ufunc->uf_func_type = &t_func_any;
1372
1373 // error messages are for the first function line
1374 lnum_save = SOURCING_LNUM;
1375 SOURCING_LNUM = sourcing_lnum_top;
1376
1377 // parse argument types
1378 if (parse_argument_types(ufunc, argtypes, varargs) == FAIL)
1379 {
1380 SOURCING_LNUM = lnum_save;
1381 goto erret;
1382 }
1383
1384 // parse the return type, if any
1385 if (parse_return_type(ufunc, ret_type) == FAIL)
1386 goto erret;
1387
1388 pt = ALLOC_CLEAR_ONE(partial_T);
1389 if (pt == NULL)
1390 goto erret;
1391 pt->pt_func = ufunc;
1392 pt->pt_refcount = 1;
1393
1394 ufunc->uf_lines = newlines;
1395 newlines.ga_data = NULL;
1396 if (sandbox)
1397 ufunc->uf_flags |= FC_SANDBOX;
1398 if (!ASCII_ISUPPER(*ufunc->uf_name))
1399 ufunc->uf_flags |= FC_VIM9;
1400 ufunc->uf_script_ctx = current_sctx;
1401 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1402 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1403 set_function_type(ufunc);
1404
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001405 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1406
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001407 rettv->vval.v_partial = pt;
1408 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001409 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001410 ret = OK;
1411
1412erret:
1413 if (lnum_save >= 0)
1414 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001415 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001416 if (newargs != NULL)
1417 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001418 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001419 if (ufunc != NULL)
1420 {
1421 func_clear(ufunc, TRUE);
1422 func_free(ufunc, TRUE);
1423 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001424 return ret;
1425}
1426
1427/*
1428 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001429 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001430 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001431 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1432 */
1433 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001434get_lambda_tv(
1435 char_u **arg,
1436 typval_T *rettv,
1437 int types_optional,
1438 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001439{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001440 int evaluate = evalarg != NULL
1441 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001442 garray_T newargs;
1443 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001444 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001445 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001446 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001447 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001448 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001449 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001450 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001451 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001452 char_u *s;
1453 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001454 int *old_eval_lavars = eval_lavars_used;
1455 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001456 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001457 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001458 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001459 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001460 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001461 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001462
Bram Moolenaar4525a572022-02-13 11:57:33 +00001463 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001464 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001465
1466 ga_init(&newargs);
1467 ga_init(&newlines);
1468
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001469 // First, check if this is really a lambda expression. "->" or "=>" must
1470 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001471 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001472 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001473 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001474 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001475 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001476 {
1477 if (types_optional)
1478 ga_clear_strings(&argtypes);
Bram Moolenaar0346b792021-01-31 22:18:29 +01001479 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001480 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001481
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001482 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001483 if (evaluate)
1484 pnewargs = &newargs;
1485 else
1486 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001487 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001488 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001489 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001490 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001491 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001492 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001493 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001494 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001495 {
1496 if (types_optional)
1497 ga_clear_strings(&argtypes);
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001498 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001499 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001500 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001501 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001502
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001503 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1504 if (ret_type != NULL)
1505 {
1506 ret_type = vim_strsave(ret_type);
1507 tofree2 = ret_type;
1508 }
1509
Bram Moolenaare38eab22019-12-05 21:50:01 +01001510 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001511 if (evaluate)
1512 eval_lavars_used = &eval_lavars;
1513
Bram Moolenaar65c44152020-12-24 15:14:01 +01001514 *arg = skipwhite_and_linebreak(*arg, evalarg);
1515
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001516 // Recognize "{" as the start of a function body.
1517 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001518 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001519 if (evalarg == NULL)
1520 // cannot happen?
1521 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001522 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001523 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1524 types_optional ? &argtypes : NULL, varargs,
1525 &default_args, ret_type) == FAIL)
1526 goto errret;
1527 goto theend;
1528 }
1529 if (default_args.ga_len > 0)
1530 {
1531 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001532 goto errret;
1533 }
1534
Bram Moolenaare38eab22019-12-05 21:50:01 +01001535 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001536 start = *arg;
1537 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001538 if (ret == FAIL)
1539 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001540
Bram Moolenaar65c44152020-12-24 15:14:01 +01001541 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001542 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001543 *arg = skipwhite_and_linebreak(*arg, evalarg);
1544 if (**arg != '}')
1545 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001546 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001547 goto errret;
1548 }
1549 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001550 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001551
1552 if (evaluate)
1553 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001554 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001555 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001556 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001557 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001558 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001559
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001560 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001561 if (fp == NULL)
1562 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001563 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001564 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001565 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001566 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001567
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001568 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001569 if (ga_grow(&newlines, 1) == FAIL)
1570 goto errret;
1571
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001572 // If there are line breaks, we need to split up the string.
1573 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001574 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001575 line_end = end;
1576
1577 // Add "return " before the expression (or the first line).
1578 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001579 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001580 if (p == NULL)
1581 goto errret;
1582 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1583 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001584 vim_strncpy(p + 7, start, line_end - start);
1585
1586 if (line_end != end)
1587 {
1588 // Add more lines, split by line breaks. Thus is used when a
1589 // lambda with { cmds } is encountered.
1590 while (*line_end == '\n')
1591 {
1592 if (ga_grow(&newlines, 1) == FAIL)
1593 goto errret;
1594 start = line_end + 1;
1595 line_end = vim_strchr(start, '\n');
1596 if (line_end == NULL)
1597 line_end = end;
1598 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1599 vim_strnsave(start, line_end - start);
1600 }
1601 }
1602
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001603 if (strstr((char *)p + 7, "a:") == NULL)
1604 // No a: variables are used for sure.
1605 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001606
1607 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001608 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001609 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001610 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001611 if (types_optional)
1612 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001613 if (parse_argument_types(fp, &argtypes,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001614 vim9script && varargs) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001615 goto errret;
1616 if (ret_type != NULL)
1617 {
1618 fp->uf_ret_type = parse_type(&ret_type,
1619 &fp->uf_type_list, TRUE);
1620 if (fp->uf_ret_type == NULL)
1621 goto errret;
1622 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001623 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001624 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001625 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001626
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001627 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001628 if (current_funccal != NULL && eval_lavars)
1629 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001630 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001631 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001632 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001633 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001634
1635#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001636 if (prof_def_func())
1637 func_do_profile(fp);
1638#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001639 if (sandbox)
1640 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001641 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001642 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001643 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001644 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001645 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001646 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001647 // Use the line number of the arguments.
1648 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001649
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001650 function_using_block_scopes(fp, evalarg->eval_cstack);
1651
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001652 pt->pt_func = fp;
1653 pt->pt_refcount = 1;
1654 rettv->vval.v_partial = pt;
1655 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001656
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001657 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001658 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001659
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001660theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001661 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001662 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001663 if (types_optional)
1664 ga_clear_strings(&argtypes);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001665
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001666 return OK;
1667
1668errret:
1669 ga_clear_strings(&newargs);
1670 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001671 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001672 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001673 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001674 ga_clear_strings(&argtypes);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001675 if (fp != NULL)
1676 vim_free(fp->uf_arg_types);
1677 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001678 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001679 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001680 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001681 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001682 return FAIL;
1683}
1684
1685/*
1686 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1687 * name it contains, otherwise return "name".
1688 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1689 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001690 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1691 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001692 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001693 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001694 */
1695 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001696deref_func_name(
1697 char_u *name,
1698 int *lenp,
1699 partial_T **partialp,
1700 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001701 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001702 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001703 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001704{
1705 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001706 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001707 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001708 char_u *s = NULL;
1709 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001710 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001711
1712 if (partialp != NULL)
1713 *partialp = NULL;
1714
1715 cc = name[*lenp];
1716 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001717
Bram Moolenaar71f21932022-01-07 18:20:55 +00001718 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001719 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001720 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001721 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001722 tv = &v->di_tv;
1723 }
1724 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1725 {
1726 imported_T *import;
1727 char_u *p = name;
1728 int len = *lenp;
1729
1730 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001731 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001732 p = name + 2;
1733 len -= 2;
1734 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001735 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001736
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001737 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001738 if (import != NULL)
1739 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001740 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001741 if (new_function)
1742 semsg(_(e_redefining_imported_item_str), name);
1743 else
1744 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001745 name[len] = cc;
1746 *lenp = 0;
1747 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001748 }
1749 }
1750
1751 if (tv != NULL)
1752 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001753 if (found_var != NULL)
1754 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001755 if (tv->v_type == VAR_FUNC)
1756 {
1757 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001758 {
1759 *lenp = 0;
1760 return (char_u *)""; // just in case
1761 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001762 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001763 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001764 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001765
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001766 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001767 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001768 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001769
1770 if (pt == NULL)
1771 {
1772 *lenp = 0;
1773 return (char_u *)""; // just in case
1774 }
1775 if (partialp != NULL)
1776 *partialp = pt;
1777 s = partial_name(pt);
1778 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001779 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001780
1781 if (s != NULL)
1782 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001783 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001784 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001785 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001786
1787 if (sv != NULL)
1788 *type = sv->sv_type;
1789 }
1790 return s;
1791 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001792 }
1793
1794 return name;
1795}
1796
1797/*
1798 * Give an error message with a function name. Handle <SNR> things.
1799 * "ermsg" is to be passed without translation, use N_() instead of _().
1800 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001801 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001802emsg_funcname(char *ermsg, char_u *name)
1803{
Bram Moolenaar54598062022-01-13 12:05:09 +00001804 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001805
Bram Moolenaar54598062022-01-13 12:05:09 +00001806 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001807 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001808 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001809 if (p != name)
1810 vim_free(p);
1811}
1812
1813/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001814 * Get function arguments at "*arg" and advance it.
1815 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001816 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001817 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001818 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001819get_func_arguments(
1820 char_u **arg,
1821 evalarg_T *evalarg,
1822 int partial_argc,
1823 typval_T *argvars,
1824 int *argcount)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001825{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001826 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001827 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001828 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001829 int evaluate = evalarg == NULL
1830 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001831
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001832 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001833 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001834 // skip the '(' or ',' and possibly line breaks
1835 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1836
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001837 if (*argp == ')' || *argp == ',' || *argp == NUL)
1838 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001839 if (eval1(&argp, &argvars[*argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001840 {
1841 ret = FAIL;
1842 break;
1843 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001844 ++*argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001845 // The comma should come right after the argument, but this wasn't
1846 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001847 if (vim9script)
1848 {
1849 if (*argp != ',' && *skipwhite(argp) == ',')
1850 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001851 if (evaluate)
1852 semsg(_(e_no_white_space_allowed_before_str_str),
1853 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001854 ret = FAIL;
1855 break;
1856 }
1857 }
1858 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001859 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001860 if (*argp != ',')
1861 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001862 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
1863 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001864 if (evaluate)
1865 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001866 ret = FAIL;
1867 break;
1868 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001869 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001870
Bram Moolenaare6b53242020-07-01 17:28:33 +02001871 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001872 if (*argp == ')')
1873 ++argp;
1874 else
1875 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001876 *arg = argp;
1877 return ret;
1878}
1879
1880/*
1881 * Call a function and put the result in "rettv".
1882 * Return OK or FAIL.
1883 */
1884 int
1885get_func_tv(
1886 char_u *name, // name of the function
1887 int len, // length of "name" or -1 to use strlen()
1888 typval_T *rettv,
1889 char_u **arg, // argument, pointing to the '('
1890 evalarg_T *evalarg, // for line continuation
1891 funcexe_T *funcexe) // various values
1892{
1893 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001894 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001895 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1896 int argcount = 0; // number of arguments found
1897 int vim9script = in_vim9script();
1898 int evaluate = evalarg == NULL
1899 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
1900
1901 argp = *arg;
1902 ret = get_func_arguments(&argp, evalarg,
1903 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
1904 argvars, &argcount);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001905
1906 if (ret == OK)
1907 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001908 int i = 0;
1909 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001910
1911 if (get_vim_var_nr(VV_TESTING))
1912 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001913 // Prepare for calling test_garbagecollect_now(), need to know
1914 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001915 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001916 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001917 for (i = 0; i < argcount; ++i)
1918 if (ga_grow(&funcargs, 1) == OK)
1919 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1920 &argvars[i];
1921 }
1922
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001923 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00001924 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001925 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001926 // An error in a builtin function does not return FAIL, but we do
1927 // want to abort further processing if an error was given.
1928 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001929 clear_tv(rettv);
1930 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001931
1932 funcargs.ga_len -= i;
1933 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001934 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001935 {
1936 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001937 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001938 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001939 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001940 }
1941
1942 while (--argcount >= 0)
1943 clear_tv(&argvars[argcount]);
1944
Bram Moolenaar4525a572022-02-13 11:57:33 +00001945 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02001946 *arg = argp;
1947 else
1948 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001949 return ret;
1950}
1951
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001952/*
1953 * Return TRUE if "p" starts with "<SID>" or "s:".
1954 * Only works if eval_fname_script() returned non-zero for "p"!
1955 */
1956 static int
1957eval_fname_sid(char_u *p)
1958{
1959 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
1960}
1961
1962/*
1963 * In a script change <SID>name() and s:name() to K_SNR 123_name().
1964 * Change <SNR>123_name() to K_SNR 123_name().
1965 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001966 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001967 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001968 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001969fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
1970{
1971 int llen;
1972 char_u *fname;
1973 int i;
1974
1975 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001976 if (llen == 0)
1977 return name; // no prefix
1978
1979 fname_buf[0] = K_SPECIAL;
1980 fname_buf[1] = KS_EXTRA;
1981 fname_buf[2] = (int)KE_SNR;
1982 i = 3;
1983 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001984 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001985 if (current_sctx.sc_sid <= 0)
1986 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001987 else
1988 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001989 sprintf((char *)fname_buf + 3, "%ld_",
1990 (long)current_sctx.sc_sid);
1991 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001992 }
1993 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001994 if (i + STRLEN(name + llen) < FLEN_FIXED)
1995 {
1996 STRCPY(fname_buf + i, name + llen);
1997 fname = fname_buf;
1998 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001999 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002000 {
2001 fname = alloc(i + STRLEN(name + llen) + 1);
2002 if (fname == NULL)
2003 *error = FCERR_OTHER;
2004 else
2005 {
2006 *tofree = fname;
2007 mch_memmove(fname, fname_buf, (size_t)i);
2008 STRCPY(fname + i, name + llen);
2009 }
2010 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002011 return fname;
2012}
2013
2014/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002015 * Concatenate the script ID and function name into "<SNR>99_name".
2016 * "buffer" must have size MAX_FUNC_NAME_LEN.
2017 */
2018 void
2019func_name_with_sid(char_u *name, int sid, char_u *buffer)
2020{
2021 // A script-local function is stored as "<SNR>99_name".
2022 buffer[0] = K_SPECIAL;
2023 buffer[1] = KS_EXTRA;
2024 buffer[2] = (int)KE_SNR;
2025 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2026 (long)sid, name);
2027}
2028
2029/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002030 * Find a function "name" in script "sid".
2031 */
2032 static ufunc_T *
2033find_func_with_sid(char_u *name, int sid)
2034{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002035 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002036 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002037
Bram Moolenaarb8822442022-01-11 15:24:05 +00002038 if (!SCRIPT_ID_VALID(sid))
2039 return NULL; // not in a script
2040
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002041 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 hi = hash_find(&func_hashtab, buffer);
2043 if (!HASHITEM_EMPTY(hi))
2044 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002045 return NULL;
2046}
2047
2048/*
2049 * Find a function "name" in script "sid" prefixing the autoload prefix.
2050 */
2051 static ufunc_T *
2052find_func_with_prefix(char_u *name, int sid)
2053{
2054 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002055 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002056 scriptitem_T *si;
2057
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002058 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002059 return NULL; // already has the prefix
2060 if (!SCRIPT_ID_VALID(sid))
2061 return NULL; // not in a script
2062 si = SCRIPT_ITEM(sid);
2063 if (si->sn_autoload_prefix != NULL)
2064 {
2065 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2066 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002067 char_u *namep;
2068
2069 // skip a "<SNR>99_" prefix
2070 namep = untrans_function_name(name);
2071 if (namep == NULL)
2072 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002073
2074 // An exported function in an autoload script is stored as
2075 // "dir#path#name".
2076 if (len < sizeof(buffer))
2077 auto_name = buffer;
2078 else
2079 auto_name = alloc(len);
2080 if (auto_name != NULL)
2081 {
2082 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002083 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002084 hi = hash_find(&func_hashtab, auto_name);
2085 if (auto_name != buffer)
2086 vim_free(auto_name);
2087 if (!HASHITEM_EMPTY(hi))
2088 return HI2UF(hi);
2089 }
2090 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002091
2092 return NULL;
2093}
2094
2095/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002096 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002097 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2098 * functions.
2099 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002100 * Return NULL for unknown function.
2101 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002102 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002103find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002104{
2105 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002106 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002108 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002110 // Find script-local function before global one.
2111 if (in_vim9script() && eval_isnamec1(*name)
2112 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002114 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2115 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002116 if (func != NULL)
2117 return func;
2118 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002119 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2120 {
2121 char_u *p = name + 5;
2122 long sid;
2123
2124 // printable "<SNR>123_Name" form
2125 sid = getdigits(&p);
2126 if (*p == '_')
2127 {
2128 func = find_func_with_sid(p + 1, (int)sid);
2129 if (func != NULL)
2130 return func;
2131 }
2132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002133 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002134
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002135 if ((flags & FFED_NO_GLOBAL) == 0)
2136 {
2137 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002138 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002139 if (!HASHITEM_EMPTY(hi))
2140 return HI2UF(hi);
2141 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142
Bram Moolenaarb8822442022-01-11 15:24:05 +00002143 // Find autoload function if this is an autoload script.
2144 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2145 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002146}
2147
2148/*
2149 * Find a function by name, return pointer to it in ufuncs.
2150 * "cctx" is passed in a :def function to find imported functions.
2151 * Return NULL for unknown or dead function.
2152 */
2153 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002154find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002156 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157
2158 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2159 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002160 return NULL;
2161}
2162
2163/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002164 * Return TRUE if "ufunc" is a global function.
2165 */
2166 int
2167func_is_global(ufunc_T *ufunc)
2168{
2169 return ufunc->uf_name[0] != K_SPECIAL;
2170}
2171
2172/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002173 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2174 */
2175 int
2176func_requires_g_prefix(ufunc_T *ufunc)
2177{
2178 return ufunc->uf_name[0] != K_SPECIAL
2179 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002180 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
2181 && !isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002182}
2183
2184/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002185 * Copy the function name of "fp" to buffer "buf".
2186 * "buf" must be able to hold the function name plus three bytes.
2187 * Takes care of script-local function names.
2188 */
2189 static void
2190cat_func_name(char_u *buf, ufunc_T *fp)
2191{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002192 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002193 {
2194 STRCPY(buf, "<SNR>");
2195 STRCAT(buf, fp->uf_name + 3);
2196 }
2197 else
2198 STRCPY(buf, fp->uf_name);
2199}
2200
2201/*
2202 * Add a number variable "name" to dict "dp" with value "nr".
2203 */
2204 static void
2205add_nr_var(
2206 dict_T *dp,
2207 dictitem_T *v,
2208 char *name,
2209 varnumber_T nr)
2210{
2211 STRCPY(v->di_key, name);
2212 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002213 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002214 v->di_tv.v_type = VAR_NUMBER;
2215 v->di_tv.v_lock = VAR_FIXED;
2216 v->di_tv.vval.v_number = nr;
2217}
2218
2219/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002220 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002221 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002222 static void
2223free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002224{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002225 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002226
Bram Moolenaarca16c602022-09-06 18:57:08 +01002227 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002228 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002229 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002230
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002231 // When garbage collecting a funccall_T may be freed before the
2232 // function that references it, clear its uf_scoped field.
2233 // The function may have been redefined and point to another
2234 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002235 if (fp != NULL && fp->uf_scoped == fc)
2236 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002237 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002238 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002239
Bram Moolenaarca16c602022-09-06 18:57:08 +01002240 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002241 vim_free(fc);
2242}
2243
2244/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002245 * Free "fc" and what it contains.
2246 * Can be called only when "fc" is kept beyond the period of it called,
2247 * i.e. after cleanup_function_call(fc).
2248 */
2249 static void
2250free_funccal_contents(funccall_T *fc)
2251{
2252 listitem_T *li;
2253
2254 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002255 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002256
2257 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002258 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002259
2260 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002261 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002262 clear_tv(&li->li_tv);
2263
2264 free_funccal(fc);
2265}
2266
2267/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002268 * Handle the last part of returning from a function: free the local hashtable.
2269 * Unless it is still in use by a closure.
2270 */
2271 static void
2272cleanup_function_call(funccall_T *fc)
2273{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002274 int may_free_fc = fc->fc_refcount <= 0;
2275 int free_fc = TRUE;
2276
Bram Moolenaarca16c602022-09-06 18:57:08 +01002277 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002278
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002279 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002280 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2281 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002282 else
2283 free_fc = FALSE;
2284
2285 // If the a:000 list and the l: and a: dicts are not referenced and
2286 // there is no closure using it, we can free the funccall_T and what's
2287 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002288 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2289 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002290 else
2291 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002292 int todo;
2293 hashitem_T *hi;
2294 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002295
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002296 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002297
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002298 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002299 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
2300 for (hi = fc->fc_l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002301 {
2302 if (!HASHITEM_EMPTY(hi))
2303 {
2304 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002305 di = HI2DI(hi);
2306 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002307 }
2308 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002309 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002310
Bram Moolenaarca16c602022-09-06 18:57:08 +01002311 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2312 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002313 else
2314 {
2315 listitem_T *li;
2316
2317 free_fc = FALSE;
2318
2319 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002320 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002321 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002322 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002323
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002324 if (free_fc)
2325 free_funccal(fc);
2326 else
2327 {
2328 static int made_copy = 0;
2329
2330 // "fc" is still in use. This can happen when returning "a:000",
2331 // assigning "l:" to a global variable or defining a closure.
2332 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002333 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002334 previous_funccal = fc;
2335
2336 if (want_garbage_collect)
2337 // If garbage collector is ready, clear count.
2338 made_copy = 0;
2339 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002340 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002341 // We have made a lot of copies, worth 4 Mbyte. This can happen
2342 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002343 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002344 // too much memory.
2345 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002346 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002347 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002348 }
2349}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002350
2351/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002352 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2353 */
2354 static int
2355numbered_function(char_u *name)
2356{
2357 return isdigit(*name)
2358 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2359}
2360
2361/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002362 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002363 * 1. ordinary names, function defined with :function or :def;
2364 * can start with "<SNR>123_" literally or with K_SPECIAL.
2365 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002366 * For the first we only count the name stored in func_hashtab as a reference,
2367 * using function() does not count as a reference, because the function is
2368 * looked up by name.
2369 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002370 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002371func_name_refcount(char_u *name)
2372{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002373 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002374}
2375
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002376/*
2377 * Unreference "fc": decrement the reference count and free it when it
2378 * becomes zero. "fp" is detached from "fc".
2379 * When "force" is TRUE we are exiting.
2380 */
2381 static void
2382funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2383{
2384 funccall_T **pfc;
2385 int i;
2386
2387 if (fc == NULL)
2388 return;
2389
2390 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002391 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2392 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2393 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2394 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 {
2396 if (fc == *pfc)
2397 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002398 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399 free_funccal_contents(fc);
2400 return;
2401 }
2402 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002403 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2404 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2405 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406}
2407
2408/*
2409 * Remove the function from the function hashtable. If the function was
2410 * deleted while it still has references this was already done.
2411 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2412 */
2413 static int
2414func_remove(ufunc_T *fp)
2415{
2416 hashitem_T *hi;
2417
2418 // Return if it was already virtually deleted.
2419 if (fp->uf_flags & FC_DEAD)
2420 return FALSE;
2421
2422 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
2423 if (!HASHITEM_EMPTY(hi))
2424 {
2425 // When there is a def-function index do not actually remove the
2426 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002427 // Do remove it when it's a copy.
2428 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaarc970e422021-03-17 15:03:04 +01002429 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002430 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01002431 return FALSE;
2432 }
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002433 hash_remove(&func_hashtab, hi, "remove function");
Bram Moolenaarc970e422021-03-17 15:03:04 +01002434 fp->uf_flags |= FC_DELETED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 return TRUE;
2436 }
2437 return FALSE;
2438}
2439
2440 static void
2441func_clear_items(ufunc_T *fp)
2442{
2443 ga_clear_strings(&(fp->uf_args));
2444 ga_clear_strings(&(fp->uf_def_args));
2445 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002447 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002448 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002449 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002450
2451 // Increment the refcount of this function to avoid it being freed
2452 // recursively when the partial is freed.
2453 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002454 partial_unref(fp->uf_partial);
2455 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002456 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002457
2458#ifdef FEAT_LUA
2459 if (fp->uf_cb_free != NULL)
2460 {
2461 fp->uf_cb_free(fp->uf_cb_state);
2462 fp->uf_cb_free = NULL;
2463 }
2464
2465 fp->uf_cb_state = NULL;
2466 fp->uf_cb = NULL;
2467#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468#ifdef FEAT_PROFILE
2469 VIM_CLEAR(fp->uf_tml_count);
2470 VIM_CLEAR(fp->uf_tml_total);
2471 VIM_CLEAR(fp->uf_tml_self);
2472#endif
2473}
2474
2475/*
2476 * Free all things that a function contains. Does not free the function
2477 * itself, use func_free() for that.
2478 * When "force" is TRUE we are exiting.
2479 */
2480 static void
2481func_clear(ufunc_T *fp, int force)
2482{
2483 if (fp->uf_cleared)
2484 return;
2485 fp->uf_cleared = TRUE;
2486
2487 // clear this function
2488 func_clear_items(fp);
2489 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002490 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002491}
2492
2493/*
2494 * Free a function and remove it from the list of functions. Does not free
2495 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002496 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002497 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002499 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002500func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002501{
2502 // Only remove it when not done already, otherwise we would remove a newer
2503 // version of the function with the same name.
2504 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2505 func_remove(fp);
2506
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002507 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002508 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002509 if (fp->uf_dfunc_idx > 0)
2510 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002511 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002513 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002514 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002515 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516}
2517
2518/*
2519 * Free all things that a function contains and free the function itself.
2520 * When "force" is TRUE we are exiting.
2521 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002522 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523func_clear_free(ufunc_T *fp, int force)
2524{
2525 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002526 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2527 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002528 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002529 else
2530 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531}
2532
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002533/*
2534 * Copy already defined function "lambda" to a new function with name "global".
2535 * This is for when a compiled function defines a global function.
2536 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002537 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002538copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002539 char_u *lambda,
2540 char_u *global,
2541 loopvarinfo_T *loopvarinfo,
2542 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002543{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002544 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002545 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002546
2547 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002548 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002549 semsg(_(e_lambda_function_not_found_str), lambda);
2550 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002551 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002552
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002553 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002554 if (fp != NULL)
2555 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002556 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002557 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002558 return FAIL;
2559 }
2560
2561 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
2562 if (fp == NULL)
2563 return FAIL;
2564
2565 fp->uf_varargs = ufunc->uf_varargs;
2566 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2567 fp->uf_def_status = ufunc->uf_def_status;
2568 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2569 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2570 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2571 == FAIL
2572 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2573 goto failed;
2574
2575 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
2576 : vim_strsave(ufunc->uf_name_exp);
2577 if (ufunc->uf_arg_types != NULL)
2578 {
2579 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2580 if (fp->uf_arg_types == NULL)
2581 goto failed;
2582 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2583 sizeof(type_T *) * fp->uf_args.ga_len);
2584 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002585 if (ufunc->uf_va_name != NULL)
2586 {
2587 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2588 if (fp->uf_va_name == NULL)
2589 goto failed;
2590 }
2591 fp->uf_ret_type = ufunc->uf_ret_type;
2592
2593 fp->uf_refcount = 1;
2594 STRCPY(fp->uf_name, global);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002595 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002596
2597 // the referenced dfunc_T is now used one more time
2598 link_def_function(fp);
2599
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002600 // Create a partial to store the context of the function where it was
2601 // instantiated. Only needs to be done once. Do this on the original
2602 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002603 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2604 {
2605 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2606
2607 if (pt == NULL)
2608 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002609 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002610 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002611 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002612 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002613 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002614 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002615 }
2616
2617 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002618
2619failed:
2620 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002621 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002622}
2623
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002624static int funcdepth = 0;
2625
2626/*
2627 * Increment the function call depth count.
2628 * Return FAIL when going over 'maxfuncdepth'.
2629 * Otherwise return OK, must call funcdepth_decrement() later!
2630 */
2631 int
2632funcdepth_increment(void)
2633{
2634 if (funcdepth >= p_mfd)
2635 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002636 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002637 return FAIL;
2638 }
2639 ++funcdepth;
2640 return OK;
2641}
2642
2643 void
2644funcdepth_decrement(void)
2645{
2646 --funcdepth;
2647}
2648
2649/*
2650 * Get the current function call depth.
2651 */
2652 int
2653funcdepth_get(void)
2654{
2655 return funcdepth;
2656}
2657
2658/*
2659 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002660 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002661 * times as funcdepth_increment().
2662 */
2663 void
2664funcdepth_restore(int depth)
2665{
2666 funcdepth = depth;
2667}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002668
2669/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002670 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2671 * "rettv".
2672 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2673 * Returns NULL when allocation fails.
2674 */
2675 funccall_T *
2676create_funccal(ufunc_T *fp, typval_T *rettv)
2677{
2678 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2679
2680 if (fc == NULL)
2681 return NULL;
2682 fc->fc_caller = current_funccal;
2683 current_funccal = fc;
2684 fc->fc_func = fp;
2685 func_ptr_ref(fp);
2686 fc->fc_rettv = rettv;
2687 return fc;
2688}
2689
2690/*
2691 * To be called when returning from a compiled function; restores
2692 * current_funccal.
2693 */
2694 void
2695remove_funccal()
2696{
2697 funccall_T *fc = current_funccal;
2698
2699 current_funccal = fc->fc_caller;
2700 free_funccal(fc);
2701}
2702
2703/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002704 * Call a user function.
2705 */
2706 static void
2707call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002708 ufunc_T *fp, // pointer to function
2709 int argcount, // nr of args
2710 typval_T *argvars, // arguments
2711 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002712 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002713 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002714{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002715 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002716 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002717 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002718 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002719 funccall_T *fc;
2720 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002721 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002722 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002723 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002724 int i;
2725 int ai;
2726 int islambda = FALSE;
2727 char_u numbuf[NUMBUFLEN];
2728 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002729 typval_T *tv_to_free[MAX_FUNC_ARGS];
2730 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002731#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002732 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002733#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01002734 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002735
Bram Moolenaarb2049902021-01-24 12:53:53 +01002736#ifdef FEAT_PROFILE
2737 CLEAR_FIELD(profile_info);
2738#endif
2739
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002740 // If depth of calling is getting too high, don't execute the function.
2741 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002742 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002743 rettv->v_type = VAR_NUMBER;
2744 rettv->vval.v_number = -1;
2745 return;
2746 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002747
Bram Moolenaare38eab22019-12-05 21:50:01 +01002748 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002749
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002750 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002751 if (fc == NULL)
2752 return;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002753 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002754 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002755 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2756 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002757 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002758 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002759
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002760 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002762#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002763 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002764#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002765 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002766#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002767 if (do_profiling == PROF_YES)
2768 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002769#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002770 sticky_cmdmod_flags = 0;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00002771 call_def_function(fp, argcount, argvars, 0,
2772 funcexe->fe_partial, funcexe->fe_object, fc, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002773 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002774#ifdef FEAT_PROFILE
2775 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002776 || (caller != NULL && caller->uf_profiling)))
2777 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002778#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002779 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002780 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781 return;
2782 }
2783
Bram Moolenaar38453522021-11-28 22:00:12 +00002784 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002785
2786 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002787 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2788 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2789 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002790 */
2791 /*
2792 * Init l: variables.
2793 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002794 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002795 if (selfdict != NULL)
2796 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002797 // Set l:self to "selfdict". Use "name" to avoid a warning from
2798 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002799 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002800 name = v->di_key;
2801 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002802 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002803 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002804 v->di_tv.v_type = VAR_DICT;
2805 v->di_tv.v_lock = 0;
2806 v->di_tv.vval.v_dict = selfdict;
2807 ++selfdict->dv_refcount;
2808 }
2809
2810 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002811 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002812 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002813 * Set a:000 to a list with room for the "..." arguments.
2814 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002815 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002816 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002817 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002818 (varnumber_T)(argcount >= fp->uf_args.ga_len
2819 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002820 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002821 if ((fp->uf_flags & FC_NOARGS) == 0)
2822 {
2823 // Use "name" to avoid a warning from some compiler that checks the
2824 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002825 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002826 name = v->di_key;
2827 STRCPY(name, "000");
2828 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002829 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002830 v->di_tv.v_type = VAR_LIST;
2831 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002832 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002833 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002834 CLEAR_FIELD(fc->fc_l_varlist);
2835 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2836 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002837
2838 /*
2839 * Set a:firstline to "firstline" and a:lastline to "lastline".
2840 * Set a:name to named arguments.
2841 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002842 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002843 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002844 if ((fp->uf_flags & FC_NOARGS) == 0)
2845 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002846 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2847 "firstline", (varnumber_T)funcexe->fe_firstline);
2848 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2849 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002850 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002851 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002852 {
2853 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002854 typval_T def_rettv;
2855 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002856
2857 ai = i - fp->uf_args.ga_len;
2858 if (ai < 0)
2859 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002860 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002861 name = FUNCARG(fp, i);
2862 if (islambda)
2863 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002864
2865 // evaluate named argument default expression
2866 isdefault = ai + fp->uf_def_args.ga_len >= 0
2867 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2868 && argvars[i].vval.v_number == VVAL_NONE));
2869 if (isdefault)
2870 {
2871 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002872
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002873 def_rettv.v_type = VAR_NUMBER;
2874 def_rettv.vval.v_number = -1;
2875
2876 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2877 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002878 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002879 {
2880 default_arg_err = 1;
2881 break;
2882 }
2883 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002884 }
2885 else
2886 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002887 if ((fp->uf_flags & FC_NOARGS) != 0)
2888 // Bail out if no a: arguments used (in lambda).
2889 break;
2890
Bram Moolenaare38eab22019-12-05 21:50:01 +01002891 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002892 sprintf((char *)numbuf, "%d", ai + 1);
2893 name = numbuf;
2894 }
2895 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2896 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002897 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002898 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002899 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002900 }
2901 else
2902 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002903 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002904 if (v == NULL)
2905 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002906 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002907 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002908
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002909 // Note: the values are copied directly to avoid alloc/free.
2910 // "argvars" must have VAR_FIXED for v_lock.
2911 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002912 v->di_tv.v_lock = VAR_FIXED;
2913
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002914 if (isdefault)
2915 // Need to free this later, no matter where it's stored.
2916 tv_to_free[tv_to_free_len++] = &v->di_tv;
2917
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002918 if (addlocal)
2919 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002920 // Named arguments should be accessed without the "a:" prefix in
2921 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002922 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002923 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002924 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002925 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002926 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002927
2928 if (ai >= 0 && ai < MAX_FUNC_ARGS)
2929 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002930 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002931
2932 li->li_tv = argvars[i];
2933 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002934 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002935 }
2936 }
2937
Bram Moolenaare38eab22019-12-05 21:50:01 +01002938 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002939 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02002940
2941 if (fp->uf_flags & FC_SANDBOX)
2942 {
2943 using_sandbox = TRUE;
2944 ++sandbox;
2945 }
2946
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002947 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002948 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002949 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002950 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002951 ++no_wait_return;
2952 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002953
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002954 smsg(_("calling %s"), SOURCING_NAME);
2955 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002956 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002957 char_u buf[MSG_BUF_LEN];
2958 char_u numbuf2[NUMBUFLEN];
2959 char_u *tofree;
2960 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002961
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002962 msg_puts("(");
2963 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002964 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002965 if (i > 0)
2966 msg_puts(", ");
2967 if (argvars[i].v_type == VAR_NUMBER)
2968 msg_outnum((long)argvars[i].vval.v_number);
2969 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002970 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002971 // Do not want errors such as E724 here.
2972 ++emsg_off;
2973 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
2974 --emsg_off;
2975 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002976 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002977 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002978 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002979 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2980 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002981 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002982 msg_puts((char *)s);
2983 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002984 }
2985 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002986 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002987 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002988 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002989 msg_puts("\n"); // don't overwrite this either
2990
2991 verbose_leave_scroll();
2992 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002993 }
2994#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002995 if (do_profiling == PROF_YES)
2996 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01002997 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002998#endif
2999
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003000 // "legacy" does not apply to commands in the function
3001 sticky_cmdmod_flags = 0;
3002
Bram Moolenaar98aff652022-09-06 21:02:35 +01003003 // If called from a compiled :def function the execution context must be
3004 // hidden, any deferred functions need to be added to the function being
3005 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003006 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003007
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003008 save_current_sctx = current_sctx;
3009 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003010 save_did_emsg = did_emsg;
3011 did_emsg = FALSE;
3012
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003013 if (default_arg_err && (fp->uf_flags & FC_ABORT))
3014 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003015 else if (islambda)
3016 {
3017 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3018
3019 // A Lambda always has the command "return {expr}". It is much faster
3020 // to evaluate {expr} directly.
3021 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003022 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003023 --ex_nesting_level;
3024 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003025 else
3026 // call do_cmdline() to execute the lines
3027 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003028 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3029
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003030 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003031 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003032
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003033 --RedrawingDisabled;
3034
Bram Moolenaare38eab22019-12-05 21:50:01 +01003035 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003036 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3037 {
3038 clear_tv(rettv);
3039 rettv->v_type = VAR_NUMBER;
3040 rettv->vval.v_number = -1;
3041 }
3042
3043#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003044 if (do_profiling == PROF_YES)
3045 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003046 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003047
3048 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3049 profile_may_end_func(&profile_info, fp, caller);
3050 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003051#endif
3052
Bram Moolenaare38eab22019-12-05 21:50:01 +01003053 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003054 if (p_verbose >= 12)
3055 {
3056 ++no_wait_return;
3057 verbose_enter_scroll();
3058
3059 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003060 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003061 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003062 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003063 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003064 else
3065 {
3066 char_u buf[MSG_BUF_LEN];
3067 char_u numbuf2[NUMBUFLEN];
3068 char_u *tofree;
3069 char_u *s;
3070
Bram Moolenaare38eab22019-12-05 21:50:01 +01003071 // The value may be very long. Skip the middle part, so that we
3072 // have some idea how it starts and ends. smsg() would always
3073 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003074 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003075 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003076 --emsg_off;
3077 if (s != NULL)
3078 {
3079 if (vim_strsize(s) > MSG_BUF_CLEN)
3080 {
3081 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3082 s = buf;
3083 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003084 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003085 vim_free(tofree);
3086 }
3087 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003088 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003089
3090 verbose_leave_scroll();
3091 --no_wait_return;
3092 }
3093
Bram Moolenaare31ee862020-01-07 20:59:34 +01003094 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003095 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003096 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003097 restore_current_ectx(save_current_ectx);
3098
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003099#ifdef FEAT_PROFILE
3100 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003101 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003102#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003103 if (using_sandbox)
3104 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003105 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003106
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003107 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003108 {
3109 ++no_wait_return;
3110 verbose_enter_scroll();
3111
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003112 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003113 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003114
3115 verbose_leave_scroll();
3116 --no_wait_return;
3117 }
3118
3119 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003120 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003121 for (i = 0; i < tv_to_free_len; ++i)
3122 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003123 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003124}
3125
3126/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003127 * Check the argument count for user function "fp".
3128 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3129 */
3130 int
3131check_user_func_argcount(ufunc_T *fp, int argcount)
3132{
3133 int regular_args = fp->uf_args.ga_len;
3134
3135 if (argcount < regular_args - fp->uf_def_args.ga_len)
3136 return FCERR_TOOFEW;
3137 else if (!has_varargs(fp) && argcount > regular_args)
3138 return FCERR_TOOMANY;
3139 return FCERR_UNKNOWN;
3140}
3141
3142/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003144 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 int
3146call_user_func_check(
3147 ufunc_T *fp,
3148 int argcount,
3149 typval_T *argvars,
3150 typval_T *rettv,
3151 funcexe_T *funcexe,
3152 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003153{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003155
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003156#ifdef FEAT_LUA
3157 if (fp->uf_flags & FC_CFUNC)
3158 {
3159 cfunc_T cb = fp->uf_cb;
3160
3161 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3162 }
3163#endif
3164
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003165 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3166 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003167 error = check_user_func_argcount(fp, argcount);
3168 if (error != FCERR_UNKNOWN)
3169 return error;
3170 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 error = FCERR_DICT;
3172 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003173 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 int did_save_redo = FALSE;
3175 save_redo_T save_redo;
3176
3177 /*
3178 * Call the user function.
3179 * Save and restore search patterns, script variables and
3180 * redo buffer.
3181 */
3182 save_search_patterns();
3183 if (!ins_compl_active())
3184 {
3185 saveRedobuff(&save_redo);
3186 did_save_redo = TRUE;
3187 }
3188 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003189 call_user_func(fp, argcount, argvars, rettv, funcexe,
3190 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3192 // Function was unreferenced while being used, free it now.
3193 func_clear_free(fp, FALSE);
3194 if (did_save_redo)
3195 restoreRedobuff(&save_redo);
3196 restore_search_patterns();
3197 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003198 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003200}
3201
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003202static funccal_entry_T *funccal_stack = NULL;
3203
3204/*
3205 * Save the current function call pointer, and set it to NULL.
3206 * Used when executing autocommands and for ":source".
3207 */
3208 void
3209save_funccal(funccal_entry_T *entry)
3210{
3211 entry->top_funccal = current_funccal;
3212 entry->next = funccal_stack;
3213 funccal_stack = entry;
3214 current_funccal = NULL;
3215}
3216
3217 void
3218restore_funccal(void)
3219{
3220 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003221 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003222 else
3223 {
3224 current_funccal = funccal_stack->top_funccal;
3225 funccal_stack = funccal_stack->next;
3226 }
3227}
3228
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003229 funccall_T *
3230get_current_funccal(void)
3231{
3232 return current_funccal;
3233}
3234
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003235/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003236 * Return TRUE when currently at the script level:
3237 * - not in a function
3238 * - not executing an autocommand
3239 * Note that when an autocommand sources a script the result is FALSE;
3240 */
3241 int
3242at_script_level(void)
3243{
3244 return current_funccal == NULL && autocmd_match == NULL;
3245}
3246
3247/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003248 * Mark all functions of script "sid" as deleted.
3249 */
3250 void
3251delete_script_functions(int sid)
3252{
3253 hashitem_T *hi;
3254 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003255 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003256 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003257 size_t len;
3258
3259 buf[0] = K_SPECIAL;
3260 buf[1] = KS_EXTRA;
3261 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003262 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003263 len = STRLEN(buf);
3264
Bram Moolenaarce658352020-07-31 23:47:12 +02003265 while (todo > 0)
3266 {
3267 todo = func_hashtab.ht_used;
3268 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3269 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003270 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003271 fp = HI2UF(hi);
3272 if (STRNCMP(fp->uf_name, buf, len) == 0)
3273 {
3274 int changed = func_hashtab.ht_changed;
3275
3276 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003277
3278 if (fp->uf_calls > 0)
3279 {
3280 // Function is executing, don't free it but do remove
3281 // it from the hashtable.
3282 if (func_remove(fp))
3283 fp->uf_refcount--;
3284 }
3285 else
3286 {
3287 func_clear(fp, TRUE);
3288 // When clearing a function another function can be
3289 // cleared as a side effect. When that happens start
3290 // over.
3291 if (changed != func_hashtab.ht_changed)
3292 break;
3293 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003294 }
3295 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003296 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003297 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003298}
3299
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003300#if defined(EXITFREE) || defined(PROTO)
3301 void
3302free_all_functions(void)
3303{
3304 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003305 ufunc_T *fp;
3306 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003307 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003308 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003309
Bram Moolenaare38eab22019-12-05 21:50:01 +01003310 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003311 while (current_funccal != NULL)
3312 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003313 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003314 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003315 if (current_funccal == NULL && funccal_stack != NULL)
3316 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003317 }
3318
Bram Moolenaare38eab22019-12-05 21:50:01 +01003319 // First clear what the functions contain. Since this may lower the
3320 // reference count of a function, it may also free a function and change
3321 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003322 while (todo > 0)
3323 {
3324 todo = func_hashtab.ht_used;
3325 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3326 if (!HASHITEM_EMPTY(hi))
3327 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328 // clear the def function index now
3329 fp = HI2UF(hi);
3330 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003331 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332
Bram Moolenaare38eab22019-12-05 21:50:01 +01003333 // Only free functions that are not refcounted, those are
3334 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003335 if (func_name_refcount(fp->uf_name))
3336 ++skipped;
3337 else
3338 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003339 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003340 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003341 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003342 {
3343 skipped = 0;
3344 break;
3345 }
3346 }
3347 --todo;
3348 }
3349 }
3350
Bram Moolenaare38eab22019-12-05 21:50:01 +01003351 // Now actually free the functions. Need to start all over every time,
3352 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003353 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003354 while (func_hashtab.ht_used > skipped)
3355 {
3356 todo = func_hashtab.ht_used;
3357 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003358 if (!HASHITEM_EMPTY(hi))
3359 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003360 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003361 // Only free functions that are not refcounted, those are
3362 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003363 fp = HI2UF(hi);
3364 if (func_name_refcount(fp->uf_name))
3365 ++skipped;
3366 else
3367 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003368 if (func_free(fp, FALSE) == OK)
3369 {
3370 skipped = 0;
3371 break;
3372 }
3373 // did not actually free it
3374 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003375 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003376 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003377 }
3378 if (skipped == 0)
3379 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380
3381 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003382}
3383#endif
3384
3385/*
3386 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003387 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3388 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003389 * "len" is the length of "name", or -1 for NUL terminated.
3390 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003392builtin_function(char_u *name, int len)
3393{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003394 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003395
Bram Moolenaara26b9702020-04-18 19:53:28 +02003396 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003397 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003398 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3399 {
3400 if (name[i] == AUTOLOAD_CHAR)
3401 return FALSE;
3402 if (!eval_isnamec(name[i]))
3403 {
3404 // "name.something" is not a builtin function
3405 if (name[i] == '.')
3406 return FALSE;
3407 break;
3408 }
3409 }
3410 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003411}
3412
3413 int
3414func_call(
3415 char_u *name,
3416 typval_T *args,
3417 partial_T *partial,
3418 dict_T *selfdict,
3419 typval_T *rettv)
3420{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003421 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003422 listitem_T *item;
3423 typval_T argv[MAX_FUNC_ARGS + 1];
3424 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003425 int r = 0;
3426
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003427 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003428 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003429 {
3430 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3431 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003432 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003433 break;
3434 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003435 // Make a copy of each argument. This is needed to be able to set
3436 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003437 copy_tv(&item->li_tv, &argv[argc++]);
3438 }
3439
3440 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003441 {
3442 funcexe_T funcexe;
3443
Bram Moolenaara80faa82020-04-12 19:37:17 +02003444 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003445 funcexe.fe_firstline = curwin->w_cursor.lnum;
3446 funcexe.fe_lastline = curwin->w_cursor.lnum;
3447 funcexe.fe_evaluate = TRUE;
3448 funcexe.fe_partial = partial;
3449 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003450 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3451 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003452
Bram Moolenaare38eab22019-12-05 21:50:01 +01003453 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003454 while (argc > 0)
3455 clear_tv(&argv[--argc]);
3456
3457 return r;
3458}
3459
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003460static int callback_depth = 0;
3461
3462 int
3463get_callback_depth(void)
3464{
3465 return callback_depth;
3466}
3467
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003468/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003469 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003470 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003471 */
3472 int
3473call_callback(
3474 callback_T *callback,
3475 int len, // length of "name" or -1 to use strlen()
3476 typval_T *rettv, // return value goes here
3477 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003478 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003479 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003480{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003481 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003482 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003483
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003484 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3485 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02003486 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003487 funcexe.fe_evaluate = TRUE;
3488 funcexe.fe_partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003489 ++callback_depth;
3490 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3491 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003492
3493 // When a :def function was called that uses :try an error would be turned
3494 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003495 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003496 {
3497 need_rethrow = FALSE;
3498 handle_did_throw();
3499 }
3500
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003501 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003502}
3503
3504/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003505 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003506 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003507 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3508 */
3509 varnumber_T
3510call_callback_retnr(
3511 callback_T *callback,
3512 int argcount, // number of "argvars"
3513 typval_T *argvars) // vars for arguments, must have "argcount"
3514 // PLUS ONE elements!
3515{
3516 typval_T rettv;
3517 varnumber_T retval;
3518
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003519 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003520 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003521
3522 retval = tv_get_number_chk(&rettv, NULL);
3523 clear_tv(&rettv);
3524 return retval;
3525}
3526
3527/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003528 * Give an error message for the result of a function.
3529 * Nothing if "error" is FCERR_NONE.
3530 */
3531 void
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003532user_func_error(int error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533{
3534 switch (error)
3535 {
3536 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003537 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003538 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003539 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003540 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 break;
3542 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003543 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544 break;
3545 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003546 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 break;
3548 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003549 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 break;
3551 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003552 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 break;
3554 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003555 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 break;
3557 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003558 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3559 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 break;
3561 }
3562}
3563
3564/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003565 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003566 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003567 * Return FAIL when the function can't be called, OK otherwise.
3568 * Also returns OK when an error was encountered while executing the function.
3569 */
3570 int
3571call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003572 char_u *funcname, // name of the function
3573 int len, // length of "name" or -1 to use strlen()
3574 typval_T *rettv, // return value goes here
3575 int argcount_in, // number of "argvars"
3576 typval_T *argvars_in, // vars for arguments, must have "argcount"
3577 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003578 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003579{
3580 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01003581 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003582 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003583 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003584 char_u fname_buf[FLEN_FIXED + 1];
3585 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003586 char_u *fname = NULL;
3587 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003588 int argcount = argcount_in;
3589 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003590 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003591 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003592 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003593 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003594 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003595 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003596 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003597 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003598
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003599 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3600 // even when call_func() returns FAIL.
3601 rettv->v_type = VAR_UNKNOWN;
3602
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003603 if (partial != NULL)
3604 fp = partial->pt_func;
3605 if (fp == NULL)
3606 {
3607 // Make a copy of the name, if it comes from a funcref variable it
3608 // could be changed or deleted in the called function.
3609 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3610 if (name == NULL)
3611 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003612
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003613 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3614 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003615
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003616 if (funcexe->fe_doesrange != NULL)
3617 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003618
3619 if (partial != NULL)
3620 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003621 // When the function has a partial with a dict and there is a dict
3622 // argument, use the dict argument. That is backwards compatible.
3623 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003624 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003625 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003626 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003627 {
3628 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003629 {
3630 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3631 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003632 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003633 goto theend;
3634 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003635 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003636 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003637 for (i = 0; i < argcount_in; ++i)
3638 argv[i + argv_clear] = argvars_in[i];
3639 argvars = argv;
3640 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003641
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003642 if (funcexe->fe_check_type != NULL
3643 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003644 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003645 // Now funcexe->fe_check_type is missing the added arguments,
3646 // make a copy of the type with the correction.
3647 check_type = *funcexe->fe_check_type;
3648 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003649 check_type.tt_args = check_type_args;
3650 CLEAR_FIELD(check_type_args);
3651 for (i = 0; i < check_type.tt_argcount; ++i)
3652 check_type_args[i + partial->pt_argc] =
3653 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003654 check_type.tt_argcount += partial->pt_argc;
3655 check_type.tt_min_argcount += partial->pt_argc;
3656 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003657 }
3658 }
3659
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003660 if (error == FCERR_NONE && funcexe->fe_check_type != NULL
3661 && funcexe->fe_evaluate)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003662 {
3663 // Check that the argument types are OK for the types of the funcref.
Bram Moolenaarc84287d2022-01-16 18:06:21 +00003664 if (check_argument_types(funcexe->fe_check_type,
3665 argvars, argcount, funcexe->fe_basetv,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003666 (name != NULL) ? name : funcname) == FAIL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003667 error = FCERR_OTHER;
3668 }
3669
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003670 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003671 {
3672 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003673 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003674
Bram Moolenaar333894b2020-08-01 18:53:07 +02003675 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003676 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003677 {
3678 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003679 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003680 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003681
Bram Moolenaare38eab22019-12-05 21:50:01 +01003682 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003683 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003684 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003685
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003686 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003687 {
3688 /*
3689 * User defined function.
3690 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003691 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003692 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003693 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003694 if (fp != NULL && !is_global && in_vim9script()
3695 && func_requires_g_prefix(fp))
3696 // In Vim9 script g: is required to find a global
3697 // non-autoload function.
3698 fp = NULL;
3699 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003700
Bram Moolenaare38eab22019-12-05 21:50:01 +01003701 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003702 if (fp == NULL
3703 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003704 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003705 && !aborting())
3706 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003707 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003708 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003709 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003710 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003711 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3712 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003713 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003714 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003715 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003716 if (fp == NULL)
3717 {
3718 char_u *p = untrans_function_name(rfname);
3719
3720 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003721 // Don't do this if the name starts with "s:".
3722 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003723 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003724 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003725
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003726 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003727 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003728 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003729 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003730 if (funcexe->fe_argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003731 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003732 argcount = funcexe->fe_argv_func(argcount, argvars,
zeertzjq48db5da2022-09-16 12:10:03 +01003733 argv_clear, fp);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003734
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003735 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003736 {
3737 // Method call: base->Method()
3738 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003739 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003740 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003741 argvars = argv;
3742 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003743 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003744
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745 error = call_user_func_check(fp, argcount, argvars, rettv,
3746 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003747 }
3748 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003749 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003750 {
3751 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003752 * expr->method(): Find the method name in the table, call its
3753 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003754 */
3755 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003756 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003757 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003758 else
3759 {
3760 /*
3761 * Find the function name in the table, call its implementation.
3762 */
3763 error = call_internal_func(fname, argcount, argvars, rettv);
3764 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003765
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003766 /*
3767 * The function call (or "FuncUndefined" autocommand sequence) might
3768 * have been aborted by an error, an interrupt, or an explicitly thrown
3769 * exception that has not been caught so far. This situation can be
3770 * tested for by calling aborting(). For an error in an internal
3771 * function or for the "E132" error in call_user_func(), however, the
3772 * throw point at which the "force_abort" flag (temporarily reset by
3773 * emsg()) is normally updated has not been reached yet. We need to
3774 * update that flag first to make aborting() reliable.
3775 */
3776 update_force_abort();
3777 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003778 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003779 ret = OK;
3780
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003781theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003782 /*
3783 * Report an error unless the argument evaluation or function call has been
3784 * cancelled due to an aborting error, an interrupt, or an exception.
3785 */
3786 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003787 user_func_error(error, (name != NULL) ? name : funcname,
3788 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003789
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003790 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003791 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003792 clear_tv(&argv[--argv_clear + argv_base]);
3793
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003794 vim_free(tofree);
3795 vim_free(name);
3796
3797 return ret;
3798}
3799
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003800/*
3801 * Call a function without arguments, partial or dict.
3802 * This is like call_func() when the call is only "FuncName()".
3803 * To be used by "expr" options.
3804 * Returns NOTDONE when the function could not be found.
3805 */
3806 int
3807call_simple_func(
3808 char_u *funcname, // name of the function
3809 int len, // length of "name" or -1 to use strlen()
3810 typval_T *rettv) // return value goes here
3811{
3812 int ret = FAIL;
3813 int error = FCERR_NONE;
3814 char_u fname_buf[FLEN_FIXED + 1];
3815 char_u *tofree = NULL;
3816 char_u *name;
3817 char_u *fname;
3818 char_u *rfname;
3819 int is_global = FALSE;
3820 ufunc_T *fp;
3821
3822 rettv->v_type = VAR_NUMBER; // default rettv is number zero
3823 rettv->vval.v_number = 0;
3824
3825 // Make a copy of the name, an option can be changed in the function.
3826 name = vim_strnsave(funcname, len);
3827 if (name == NULL)
3828 return ret;
3829
3830 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3831
3832 // Skip "g:" before a function name.
3833 if (fname[0] == 'g' && fname[1] == ':')
3834 {
3835 is_global = TRUE;
3836 rfname = fname + 2;
3837 }
3838 else
3839 rfname = fname;
3840 fp = find_func(rfname, is_global);
3841 if (fp != NULL && !is_global && in_vim9script()
3842 && func_requires_g_prefix(fp))
3843 // In Vim9 script g: is required to find a global non-autoload
3844 // function.
3845 fp = NULL;
3846 if (fp == NULL)
3847 ret = NOTDONE;
3848 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
3849 error = FCERR_DELETED;
3850 else if (fp != NULL)
3851 {
3852 typval_T argvars[1];
3853 funcexe_T funcexe;
3854
3855 argvars[0].v_type = VAR_UNKNOWN;
3856 CLEAR_FIELD(funcexe);
3857 funcexe.fe_evaluate = TRUE;
3858
3859 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
3860 if (error == FCERR_NONE)
3861 ret = OK;
3862 }
3863
3864 user_func_error(error, name, FALSE);
3865 vim_free(tofree);
3866 vim_free(name);
3867
3868 return ret;
3869}
3870
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003871 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872printable_func_name(ufunc_T *fp)
3873{
3874 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
3875}
3876
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003877/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003878 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
3879 * TRUE. Otherwise return FALSE.
3880 */
3881 static int
3882function_list_modified(int prev_ht_changed)
3883{
3884 if (prev_ht_changed != func_hashtab.ht_changed)
3885 {
3886 emsg(_(e_function_list_was_modified));
3887 return TRUE;
3888 }
3889 return FALSE;
3890}
3891
3892/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003893 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003894 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003895 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003896list_func_head(ufunc_T *fp, int indent)
3897{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003898 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003899 int j;
3900
3901 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003902
3903 // a timer at the more prompt may have deleted the function
3904 if (function_list_modified(prev_ht_changed))
3905 return FAIL;
3906
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003907 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003908 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003909 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003910 msg_puts("def ");
3911 else
3912 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003914 msg_putchar('(');
3915 for (j = 0; j < fp->uf_args.ga_len; ++j)
3916 {
3917 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003918 msg_puts(", ");
3919 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920 if (fp->uf_arg_types != NULL)
3921 {
3922 char *tofree;
3923
3924 msg_puts(": ");
3925 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
3926 vim_free(tofree);
3927 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003928 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
3929 {
3930 msg_puts(" = ");
3931 msg_puts(((char **)(fp->uf_def_args.ga_data))
3932 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
3933 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003934 }
3935 if (fp->uf_varargs)
3936 {
3937 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003938 msg_puts(", ");
3939 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003940 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 if (fp->uf_va_name != NULL)
3942 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01003943 if (!fp->uf_varargs)
3944 {
3945 if (j)
3946 msg_puts(", ");
3947 msg_puts("...");
3948 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02003950 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 {
3952 char *tofree;
3953
3954 msg_puts(": ");
3955 msg_puts(type_name(fp->uf_va_type, &tofree));
3956 vim_free(tofree);
3957 }
3958 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003959 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003960
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003961 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003962 {
3963 if (fp->uf_ret_type != &t_void)
3964 {
3965 char *tofree;
3966
3967 msg_puts(": ");
3968 msg_puts(type_name(fp->uf_ret_type, &tofree));
3969 vim_free(tofree);
3970 }
3971 }
3972 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003973 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003974 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003975 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003976 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003977 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003978 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003979 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003980 msg_clr_eos();
3981 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003982 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003983
3984 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003985}
3986
3987/*
3988 * Get a function name, translating "<SID>" and "<SNR>".
3989 * Also handles a Funcref in a List or Dictionary.
3990 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003991 * Set "*is_global" to TRUE when the function must be global, unless
3992 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003993 * flags:
3994 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00003995 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003996 * TFN_QUIET: be quiet
3997 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003998 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003999 * Advances "pp" to just after the function name (if no error).
4000 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004001 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004002trans_function_name(
4003 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004004 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004005 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004006 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004007 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004008 partial_T **partial, // return: partial of a FuncRef
4009 type_T **type) // return: type of funcref if not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004010{
4011 char_u *name = NULL;
4012 char_u *start;
4013 char_u *end;
4014 int lead;
4015 char_u sid_buf[20];
4016 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004018 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004019 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004020 int vim9script = in_vim9script();
4021 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004022
4023 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004024 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004025 start = *pp;
4026
Bram Moolenaare38eab22019-12-05 21:50:01 +01004027 // Check for hard coded <SNR>: already translated function ID (from a user
4028 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004029 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4030 && (*pp)[2] == (int)KE_SNR)
4031 {
4032 *pp += 3;
4033 len = get_id_len(pp) + 3;
4034 return vim_strnsave(start, len);
4035 }
4036
Bram Moolenaare38eab22019-12-05 21:50:01 +01004037 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4038 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004039 lead = eval_fname_script(start);
4040 if (lead > 2)
4041 start += lead;
4042
Bram Moolenaare38eab22019-12-05 21:50:01 +01004043 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01004044 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004045 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004046 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004047 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004048 {
4049 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004050 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004051 goto theend;
4052 }
4053 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4054 {
4055 /*
4056 * Report an invalid expression in braces, unless the expression
4057 * evaluation has been cancelled due to an aborting error, an
4058 * interrupt, or an exception.
4059 */
4060 if (!aborting())
4061 {
4062 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004063 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004064 }
4065 else
4066 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4067 goto theend;
4068 }
4069
4070 if (lv.ll_tv != NULL)
4071 {
4072 if (fdp != NULL)
4073 {
4074 fdp->fd_dict = lv.ll_dict;
4075 fdp->fd_newkey = lv.ll_newkey;
4076 lv.ll_newkey = NULL;
4077 fdp->fd_di = lv.ll_di;
4078 }
4079 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4080 {
4081 name = vim_strsave(lv.ll_tv->vval.v_string);
4082 *pp = end;
4083 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004084 else if (lv.ll_tv->v_type == VAR_CLASS
4085 && lv.ll_tv->vval.v_class != NULL)
4086 {
4087 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4088 *pp = end;
4089 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004090 else if (lv.ll_tv->v_type == VAR_PARTIAL
4091 && lv.ll_tv->vval.v_partial != NULL)
4092 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004093 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004094 *pp = end;
4095 if (partial != NULL)
4096 *partial = lv.ll_tv->vval.v_partial;
4097 }
4098 else
4099 {
4100 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4101 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004102 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004103 else
4104 *pp = end;
4105 name = NULL;
4106 }
4107 goto theend;
4108 }
4109
4110 if (lv.ll_name == NULL)
4111 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004112 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004113 *pp = end;
4114 goto theend;
4115 }
4116
Bram Moolenaare38eab22019-12-05 21:50:01 +01004117 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004118 if (lv.ll_exp_name != NULL)
4119 {
4120 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004121 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004122 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004123 if (name == lv.ll_exp_name)
4124 name = NULL;
4125 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004126 else if (lv.ll_sid > 0)
4127 {
4128 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4129 int cc = *lv.ll_name_end;
4130
4131 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4132 *lv.ll_name_end = NUL;
4133 if (si->sn_autoload_prefix != NULL)
4134 {
4135 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4136 }
4137 else
4138 {
4139 sid_buf[0] = K_SPECIAL;
4140 sid_buf[1] = KS_EXTRA;
4141 sid_buf[2] = (int)KE_SNR;
4142 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004143 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004144 name = concat_str(sid_buf, lv.ll_name);
4145 }
4146 *lv.ll_name_end = cc;
4147 *pp = end;
4148 goto theend;
4149 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004150 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004151 {
4152 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004153 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004154 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004155 if (name == *pp)
4156 name = NULL;
4157 }
4158 if (name != NULL)
4159 {
4160 name = vim_strsave(name);
4161 *pp = end;
4162 if (STRNCMP(name, "<SNR>", 5) == 0)
4163 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004164 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004165 name[0] = K_SPECIAL;
4166 name[1] = KS_EXTRA;
4167 name[2] = (int)KE_SNR;
4168 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4169 }
4170 goto theend;
4171 }
4172
4173 if (lv.ll_exp_name != NULL)
4174 {
4175 len = (int)STRLEN(lv.ll_exp_name);
4176 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4177 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4178 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004179 // When there was "s:" already or the name expanded to get a
4180 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004181 lv.ll_name += 2;
4182 len -= 2;
4183 lead = 2;
4184 }
4185 }
4186 else
4187 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004188 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004189 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004190 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004191 if (lv.ll_name[0] == 'g')
4192 {
4193 if (is_global != NULL)
4194 {
4195 *is_global = TRUE;
4196 }
4197 else
4198 {
4199 // dropping "g:" without setting "is_global" won't work in
4200 // Vim9script, put it back later
4201 prefix_g = TRUE;
4202 extra = 2;
4203 }
4204 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004205 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004206 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004207 len = (int)(end - lv.ll_name);
4208 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004209 if (len <= 0)
4210 {
4211 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004212 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004213 goto theend;
4214 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004215
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004216 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004217 // starts with a lower case character: dict.func(). Or when in a class.
4218 vim9_local = ASCII_ISUPPER(*start) && vim9script
4219 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004221 /*
4222 * Copy the function name to allocated memory.
4223 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4224 * Accept <SNR>123_name() outside a script.
4225 */
4226 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004227 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004228 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004229 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004230 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004231 {
Kota Kato948a3892022-08-16 16:09:59 +01004232 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4233 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004234 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004235 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004236 goto theend;
4237 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004239 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004240 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004242 || eval_fname_sid(*pp))
4243 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004245 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004246 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004247 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004248 goto theend;
4249 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004250 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004251 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252 extra = 3 + (int)STRLEN(sid_buf);
4253 else
4254 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004255 }
4256 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004257 else if (!(flags & TFN_INT)
4258 && (builtin_function(lv.ll_name, len)
4259 || (vim9script && *lv.ll_name == '_'))
4260 && !((flags & TFN_IN_CLASS) && STRNCMP(lv.ll_name, "new", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004261 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004262 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004263 : e_function_name_must_start_with_capital_or_s_str),
4264 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004265 goto theend;
4266 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004267 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004268 {
4269 char_u *cp = vim_strchr(lv.ll_name, ':');
4270
4271 if (cp != NULL && cp < end)
4272 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004273 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004274 goto theend;
4275 }
4276 }
4277
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004279 if (name != NULL)
4280 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004281 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004282 {
4283 name[0] = K_SPECIAL;
4284 name[1] = KS_EXTRA;
4285 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004286 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004287 STRCPY(name + 3, sid_buf);
4288 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004289 else if (prefix_g)
4290 {
4291 name[0] = 'g';
4292 name[1] = ':';
4293 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4295 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004296 }
4297 *pp = end;
4298
4299theend:
4300 clear_lval(&lv);
4301 return name;
4302}
4303
4304/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004305 * Assuming "name" is the result of trans_function_name() and it was prefixed
4306 * to use the script-local name, return the unmodified name (points into
4307 * "name"). Otherwise return NULL.
4308 * This can be used to first search for a script-local function and fall back
4309 * to the global function if not found.
4310 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004311 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004312untrans_function_name(char_u *name)
4313{
4314 char_u *p;
4315
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004316 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004317 {
4318 p = vim_strchr(name, '_');
4319 if (p != NULL)
4320 return p + 1;
4321 }
4322 return NULL;
4323}
4324
4325/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004326 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4327 * current script ID and returns the expanded function name. The caller should
4328 * free the returned name. If not called from a script context or the function
4329 * name doesn't start with these prefixes, then returns NULL.
4330 * This doesn't check whether the script-local function exists or not.
4331 */
4332 char_u *
4333get_scriptlocal_funcname(char_u *funcname)
4334{
4335 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004336 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004337 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004338 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004339
4340 if (funcname == NULL)
4341 return NULL;
4342
4343 if (STRNCMP(funcname, "s:", 2) != 0
4344 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004345 {
4346 ufunc_T *ufunc;
4347
4348 // The function name does not have a script-local prefix. Try finding
4349 // it when in a Vim9 script and there is no "g:" prefix.
4350 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4351 return NULL;
4352 ufunc = find_func(funcname, FALSE);
4353 if (ufunc == NULL || func_is_global(ufunc)
4354 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4355 return NULL;
4356 ++p;
4357 off = 0;
4358 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004359 else
4360 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004361
4362 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4363 {
4364 emsg(_(e_using_sid_not_in_script_context));
4365 return NULL;
4366 }
4367 // Expand s: prefix into <SNR>nr_<name>
4368 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4369 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004370 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004371 if (newname == NULL)
4372 return NULL;
4373 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004374 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004375
4376 return newname;
4377}
4378
4379/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004380 * Return script-local "fname" with the 3-byte sequence replaced by
4381 * printable <SNR> in allocated memory.
4382 */
4383 char_u *
4384alloc_printable_func_name(char_u *fname)
4385{
4386 char_u *n = alloc(STRLEN(fname + 3) + 6);
4387
4388 if (n != NULL)
4389 {
4390 STRCPY(n, "<SNR>");
4391 STRCPY(n + 5, fname + 3);
4392 }
4393 return n;
4394}
4395
4396/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004397 * Call trans_function_name(), except that a lambda is returned as-is.
4398 * Returns the name in allocated memory.
4399 */
4400 char_u *
4401save_function_name(
4402 char_u **name,
4403 int *is_global,
4404 int skip,
4405 int flags,
4406 funcdict_T *fudi)
4407{
4408 char_u *p = *name;
4409 char_u *saved;
4410
4411 if (STRNCMP(p, "<lambda>", 8) == 0)
4412 {
4413 p += 8;
4414 (void)getdigits(&p);
4415 saved = vim_strnsave(*name, p - *name);
4416 if (fudi != NULL)
4417 CLEAR_POINTER(fudi);
4418 }
4419 else
4420 saved = trans_function_name(&p, is_global, skip,
4421 flags, fudi, NULL, NULL);
4422 *name = p;
4423 return saved;
4424}
4425
4426/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004427 * List functions. When "regmatch" is NULL all of then.
4428 * Otherwise functions matching "regmatch".
4429 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004430 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004431list_functions(regmatch_T *regmatch)
4432{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004433 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004434 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004435 hashitem_T *hi;
4436
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004437 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004438 {
4439 if (!HASHITEM_EMPTY(hi))
4440 {
4441 ufunc_T *fp = HI2UF(hi);
4442
4443 --todo;
4444 if ((fp->uf_flags & FC_DEAD) == 0
4445 && (regmatch == NULL
4446 ? !message_filtered(fp->uf_name)
4447 && !func_name_refcount(fp->uf_name)
4448 : !isdigit(*fp->uf_name)
4449 && vim_regexec(regmatch, fp->uf_name, 0)))
4450 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004451 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004452 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004453 if (function_list_modified(prev_ht_changed))
4454 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004455 }
4456 }
4457 }
4458}
4459
4460/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004461 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004462 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4463 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004464 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004465 * If "in_class" is TRUE then the function is defined inside a class.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004466 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004467 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004468 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004469define_function(
4470 exarg_T *eap,
4471 char_u *name_arg,
4472 garray_T *lines_to_free,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004473 int in_class)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004474{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004475 int j;
4476 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004477 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004478 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004479 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004480 char_u *p;
4481 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004482 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004483 char_u *line_arg = NULL;
4484 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004485 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004486 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004487 garray_T newlines;
4488 int varargs = FALSE;
4489 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004491 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004492 int fp_allocated = FALSE;
4493 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004494 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004495 dictitem_T *v;
4496 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004497 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004498 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004499 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004500 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004501 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004502 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004503
4504 /*
4505 * ":function" without argument: list functions.
4506 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004507 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004508 {
4509 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004510 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004511 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004512 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004513 }
4514
4515 /*
4516 * ":function /pat": list functions matching pattern.
4517 */
4518 if (*eap->arg == '/')
4519 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004520 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004521 if (!eap->skip)
4522 {
4523 regmatch_T regmatch;
4524
4525 c = *p;
4526 *p = NUL;
4527 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4528 *p = c;
4529 if (regmatch.regprog != NULL)
4530 {
4531 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004532 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004533 vim_regfree(regmatch.regprog);
4534 }
4535 }
4536 if (*p == '/')
4537 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004538 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004539 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004540 }
4541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542 ga_init(&newargs);
4543 ga_init(&argtypes);
4544 ga_init(&default_args);
4545
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004546 /*
4547 * Get the function name. There are these situations:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004548 * func normal function name, also when "in_class" is TRUE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004549 * "name" == func, "fudi.fd_dict" == NULL
4550 * dict.func new dictionary entry
4551 * "name" == NULL, "fudi.fd_dict" set,
4552 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4553 * dict.func existing dict entry with a Funcref
4554 * "name" == func, "fudi.fd_dict" set,
4555 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4556 * dict.func existing dict entry that's not a Funcref
4557 * "name" == NULL, "fudi.fd_dict" set,
4558 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4559 * s:func script-local function name
4560 * g:func global function name, same as "func"
4561 */
4562 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004563 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004564 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004565 // nested function, argument is (args).
4566 paren = TRUE;
4567 CLEAR_FIELD(fudi);
4568 }
4569 else
4570 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004571 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004572 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004573 if (p[0] == 's' && p[1] == ':')
4574 {
4575 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4576 return NULL;
4577 }
4578 p = to_name_end(p, TRUE);
4579 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4580 {
4581 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4582 eap->arg);
4583 return NULL;
4584 }
4585 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004586 }
4587
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004588 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004589 | (in_class ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004590 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004591 paren = (vim_strchr(p, '(') != NULL);
4592 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004593 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004594 /*
4595 * Return on an invalid expression in braces, unless the expression
4596 * evaluation has been cancelled due to an aborting error, an
4597 * interrupt, or an exception.
4598 */
4599 if (!aborting())
4600 {
4601 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004602 semsg(_(e_key_not_present_in_dictionary_str),
4603 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004604 vim_free(fudi.fd_newkey);
4605 return NULL;
4606 }
4607 else
4608 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004609 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004610
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004611 // For "export def FuncName()" in an autoload script the function name
4612 // is stored with the legacy autoload name "dir#script#FuncName" so
4613 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004614 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004615 {
4616 char_u *prefixed = may_prefix_autoload(name);
4617
4618 if (prefixed != NULL && prefixed != name)
4619 {
4620 vim_free(name);
4621 name = prefixed;
4622 }
4623 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004624 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004625 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004626 {
4627 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4628 goto ret_free;
4629 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004630 }
4631
Bram Moolenaare38eab22019-12-05 21:50:01 +01004632 // An error in a function call during evaluation of an expression in magic
4633 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004634 saved_did_emsg = did_emsg;
4635 did_emsg = FALSE;
4636
4637 /*
4638 * ":function func" with only function name: list function.
4639 */
4640 if (!paren)
4641 {
4642 if (!ends_excmd(*skipwhite(p)))
4643 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004644 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004645 goto ret_free;
4646 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004647 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004648 if (eap->nextcmd != NULL)
4649 *p = NUL;
4650 if (!eap->skip && !got_int)
4651 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004652 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004653 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4654 {
4655 char_u *up = untrans_function_name(name);
4656
4657 // With Vim9 script the name was made script-local, if not
4658 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004659 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004660 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004661 }
4662
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004663 if (fp != NULL)
4664 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004665 // Check no function was added or removed from a timer, e.g. at
4666 // the more prompt. "fp" may then be invalid.
4667 int prev_ht_changed = func_hashtab.ht_changed;
4668
4669 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004670 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004671 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4672 {
4673 if (FUNCLINE(fp, j) == NULL)
4674 continue;
4675 msg_putchar('\n');
4676 msg_outnum((long)(j + 1));
4677 if (j < 9)
4678 msg_putchar(' ');
4679 if (j < 99)
4680 msg_putchar(' ');
4681 if (function_list_modified(prev_ht_changed))
4682 break;
4683 msg_prt_line(FUNCLINE(fp, j), FALSE);
4684 out_flush(); // show a line at a time
4685 ui_breakcheck();
4686 }
4687 if (!got_int)
4688 {
4689 msg_putchar('\n');
4690 if (!function_list_modified(prev_ht_changed))
4691 {
4692 if (fp->uf_def_status != UF_NOT_COMPILED)
4693 msg_puts(" enddef");
4694 else
4695 msg_puts(" endfunction");
4696 }
4697 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004698 }
4699 }
4700 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004701 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004702 }
4703 goto ret_free;
4704 }
4705
4706 /*
4707 * ":function name(arg1, arg2)" Define function.
4708 */
4709 p = skipwhite(p);
4710 if (*p != '(')
4711 {
4712 if (!eap->skip)
4713 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004714 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004715 goto ret_free;
4716 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004717 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004718 if (vim_strchr(p, '(') != NULL)
4719 p = vim_strchr(p, '(');
4720 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004721
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004722 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4723 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004724 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004725 goto ret_free;
4726 }
4727
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004728 // In Vim9 script only global functions can be redefined.
4729 if (vim9script && eap->forceit && !is_global)
4730 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004731 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004732 goto ret_free;
4733 }
4734
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004735 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004736
Bram Moolenaar04b12692020-05-04 23:24:44 +02004737 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004738 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004739 // Check the name of the function. Unless it's a dictionary function
4740 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004741 if (name != NULL)
4742 arg = name;
4743 else
4744 arg = fudi.fd_newkey;
4745 if (arg != NULL && (fudi.fd_di == NULL
4746 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4747 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4748 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004749 char_u *name_base = arg;
4750 int i;
4751
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004752 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004753 {
4754 name_base = vim_strchr(arg, '_');
4755 if (name_base == NULL)
4756 name_base = arg + 3;
4757 else
4758 ++name_base;
4759 }
4760 for (i = 0; name_base[i] != NUL && (i == 0
4761 ? eval_isnamec1(name_base[i])
4762 : eval_isnamec(name_base[i])); ++i)
4763 ;
4764 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004765 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004766
4767 // In Vim9 script a function cannot have the same name as a
4768 // variable.
4769 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004770 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4771 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004772 + EVAL_VAR_NO_FUNC) == OK)
4773 {
4774 semsg(_(e_redefining_script_item_str), name_base);
4775 goto ret_free;
4776 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004777 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004778 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004779 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004780 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004781 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004782 goto ret_free;
4783 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004784 }
4785
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004786 // This may get more lines and make the pointers into the first line
4787 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004788 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004789 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004790 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004791 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004792 eap, in_class, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004793 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004794 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004795
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004797 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004799 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004800 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004801 if (*p != ':')
4802 {
4803 semsg(_(e_no_white_space_allowed_before_colon_str), p);
4804 p = skipwhite(p);
4805 }
4806 else if (!IS_WHITE_OR_NUL(p[1]))
4807 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02004809 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004810 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004811 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004812 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01004813 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004814 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004815 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004816 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004817 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004818 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02004819 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004820 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004821 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02004822 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004823 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824 else
4825 // find extra arguments "range", "dict", "abort" and "closure"
4826 for (;;)
4827 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01004828 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829 p = skipwhite(p);
4830 if (STRNCMP(p, "range", 5) == 0)
4831 {
4832 flags |= FC_RANGE;
4833 p += 5;
4834 }
4835 else if (STRNCMP(p, "dict", 4) == 0)
4836 {
4837 flags |= FC_DICT;
4838 p += 4;
4839 }
4840 else if (STRNCMP(p, "abort", 5) == 0)
4841 {
4842 flags |= FC_ABORT;
4843 p += 5;
4844 }
4845 else if (STRNCMP(p, "closure", 7) == 0)
4846 {
4847 flags |= FC_CLOSURE;
4848 p += 7;
4849 if (current_funccal == NULL)
4850 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004851 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852 name == NULL ? (char_u *)"" : name);
4853 goto erret;
4854 }
4855 }
4856 else
4857 break;
4858 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004859
Bram Moolenaare38eab22019-12-05 21:50:01 +01004860 // When there is a line break use what follows for the function body.
4861 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004862 if (*p == '\n')
4863 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004864 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02004865 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
4866 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01004867 && !(VIM_ISWHITE(*whitep) && *p == '#'
4868 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02004869 && !eap->skip
4870 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004871 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004872
4873 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004874 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
4875 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004876 */
4877 if (KeyTyped)
4878 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004879 // Check if the function already exists, don't let the user type the
4880 // whole function before telling him it doesn't work! For a script we
4881 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004882 if (!eap->skip && !eap->forceit)
4883 {
4884 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004885 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004886 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00004887 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004888 }
4889
4890 if (!eap->skip && did_emsg)
4891 goto erret;
4892
Bram Moolenaare38eab22019-12-05 21:50:01 +01004893 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004894 cmdline_row = msg_row;
4895 }
4896
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004897 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004898 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004899
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004900 // Do not define the function when getting the body fails and when
4901 // skipping.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004902 if (get_function_body(eap, &newlines, line_arg, lines_to_free) == FAIL
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004903 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004904 goto erret;
4905
4906 /*
4907 * If there are no errors, add the function
4908 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004909 if (fudi.fd_dict != NULL)
4910 {
4911 char numbuf[20];
4912
4913 fp = NULL;
4914 if (fudi.fd_newkey == NULL && !eap->forceit)
4915 {
4916 emsg(_(e_dictionary_entry_already_exists));
4917 goto erret;
4918 }
4919 if (fudi.fd_di == NULL)
4920 {
4921 // Can't add a function to a locked dictionary
4922 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
4923 goto erret;
4924 }
4925 // Can't change an existing function if it is locked
4926 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
4927 goto erret;
4928
4929 // Give the function a sequential number. Can only be used with a
4930 // Funcref!
4931 vim_free(name);
4932 sprintf(numbuf, "%d", ++func_nr);
4933 name = vim_strsave((char_u *)numbuf);
4934 if (name == NULL)
4935 goto erret;
4936 }
4937 else if (!in_class)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004938 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004939 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004940 char_u *find_name = name;
4941 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004942 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004944 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004945 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004946 var_conflict = TRUE;
4947
4948 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
4949 {
4950 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
4951
4952 if (si->sn_autoload_prefix != NULL)
4953 {
4954 if (is_export)
4955 {
4956 find_name = name + STRLEN(si->sn_autoload_prefix);
4957 v = find_var(find_name, &ht, TRUE);
4958 if (v != NULL)
4959 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004960 // Only check if the function already exists in the script,
4961 // global functions can be shadowed.
4962 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004963 }
4964 else
4965 {
4966 char_u *prefixed = may_prefix_autoload(name);
4967
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00004968 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004969 {
4970 v = find_var(prefixed, &ht, TRUE);
4971 if (v != NULL)
4972 var_conflict = TRUE;
4973 vim_free(prefixed);
4974 }
4975 }
4976 }
4977 }
4978 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004979 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004980 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004981 goto erret;
4982 }
4983
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004984 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02004985 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004986 {
Bram Moolenaareef21022020-08-01 22:16:43 +02004987 char_u *uname = untrans_function_name(name);
4988
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004989 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02004990 }
4991
4992 if (fp != NULL || import != NULL)
4993 {
4994 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004995
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004996 // Function can be replaced with "function!" and when sourcing the
4997 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02004998 // A name that is used by an import can not be overruled.
4999 if (import != NULL
5000 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005001 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005002 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005003 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005004 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005005 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005006 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005007 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005008 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005009 goto erret;
5010 }
5011 if (fp->uf_calls > 0)
5012 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005013 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005014 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005015 goto erret;
5016 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005017 if (fp->uf_refcount > 1)
5018 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005019 // This function is referenced somewhere, don't redefine it but
5020 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005021 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005022 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005023 fp = NULL;
5024 overwrite = TRUE;
5025 }
5026 else
5027 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005028 char_u *exp_name = fp->uf_name_exp;
5029
5030 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005031 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005032 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005033 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005034 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005035 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005036#ifdef FEAT_PROFILE
5037 fp->uf_profiling = FALSE;
5038 fp->uf_prof_initialized = FALSE;
5039#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005040 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005041 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005042 }
5043 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005044
5045 if (fp == NULL)
5046 {
5047 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5048 {
5049 int slen, plen;
5050 char_u *scriptname;
5051
Bram Moolenaare38eab22019-12-05 21:50:01 +01005052 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005053 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005054 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005055 {
5056 scriptname = autoload_name(name);
5057 if (scriptname != NULL)
5058 {
5059 p = vim_strchr(scriptname, '/');
5060 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005061 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005062 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005063 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005064 j = OK;
5065 vim_free(scriptname);
5066 }
5067 }
5068 if (j == FAIL)
5069 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005070 linenr_T save_lnum = SOURCING_LNUM;
5071
5072 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005073 semsg(_(e_function_name_does_not_match_script_file_name_str),
5074 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005075 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005076 goto erret;
5077 }
5078 }
5079
Bram Moolenaar47ed5532019-08-08 20:49:14 +02005080 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005081 if (fp == NULL)
5082 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005083 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005084
5085 if (fudi.fd_dict != NULL)
5086 {
5087 if (fudi.fd_di == NULL)
5088 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005089 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005090 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5091 if (fudi.fd_di == NULL)
5092 {
5093 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02005094 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005095 goto erret;
5096 }
5097 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5098 {
5099 vim_free(fudi.fd_di);
5100 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02005101 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005102 goto erret;
5103 }
5104 }
5105 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005106 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005107 clear_tv(&fudi.fd_di->di_tv);
5108 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005109 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005110
Bram Moolenaare38eab22019-12-05 21:50:01 +01005111 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005112 flags |= FC_DICT;
5113 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005114 }
5115 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005116 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005117 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005118 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005119
5120 if (eap->cmdidx == CMD_def)
5121 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005122 int lnum_save = SOURCING_LNUM;
5123 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005124
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005125 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005126
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005127 // error messages are for the first function line
5128 SOURCING_LNUM = sourcing_lnum_top;
5129
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005130 // The function may use script variables from the context.
5131 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005132
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005133 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005134 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005135 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005136 free_fp = fp_allocated;
5137 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005138 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005139 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005140
5141 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005142 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005143 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005144 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005145 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005146 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005147 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005148 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005149 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005150 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005151 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005152
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005153 if (fp_allocated)
5154 {
5155 // insert the new function in the function list
5156 set_ufunc_name(fp, name);
5157 if (overwrite)
5158 {
5159 hi = hash_find(&func_hashtab, name);
5160 hi->hi_key = UF2HIKEY(fp);
5161 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005162 else if (!in_class && hash_add(&func_hashtab,
5163 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005164 {
5165 free_fp = TRUE;
5166 goto erret;
5167 }
5168 fp->uf_refcount = 1;
5169 }
5170
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005171 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005172 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005173 if ((flags & FC_CLOSURE) != 0)
5174 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005175 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005176 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005177 }
5178 else
5179 fp->uf_scoped = NULL;
5180
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005181#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005182 if (prof_def_func())
5183 func_do_profile(fp);
5184#endif
5185 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005186 if (sandbox)
5187 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005188 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005189 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005190 fp->uf_flags = flags;
5191 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005192 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005193 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005194 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005195 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005196 if (is_export)
5197 {
5198 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005199 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200 is_export = FALSE;
5201 }
5202
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005203 if (eap->cmdidx == CMD_def)
5204 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005205 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5206 // :func does not use Vim9 script syntax, even in a Vim9 script file
5207 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005208
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005209 goto ret_free;
5210
5211erret:
5212 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005213 ga_clear_strings(&default_args);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005214 if (fp != NULL)
5215 {
5216 ga_init(&fp->uf_args);
5217 ga_init(&fp->uf_def_args);
5218 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005219errret_2:
5220 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005221 if (fp != NULL)
5222 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005223 if (free_fp)
5224 {
5225 vim_free(fp);
5226 fp = NULL;
5227 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005228ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005229 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005230 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005231 if (name != name_arg)
5232 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005233 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005234 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005235
5236 return fp;
5237}
5238
5239/*
5240 * ":function"
5241 */
5242 void
5243ex_function(exarg_T *eap)
5244{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005245 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005246
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005247 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005248 (void)define_function(eap, NULL, &lines_to_free, FALSE);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005249 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005250}
5251
5252/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005253 * Find a function by name, including "<lambda>123".
5254 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005255 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005256 * Return NULL if not found.
5257 */
5258 ufunc_T *
5259find_func_by_name(char_u *name, compiletype_T *compile_type)
5260{
5261 char_u *arg = name;
5262 char_u *fname;
5263 ufunc_T *ufunc;
5264 int is_global = FALSE;
5265
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005266 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5267 {
5268 *compile_type = CT_PROFILE;
5269 arg = skipwhite(arg + 7);
5270 }
5271 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5272 {
5273 *compile_type = CT_DEBUG;
5274 arg = skipwhite(arg + 5);
5275 }
5276
5277 if (STRNCMP(arg, "<lambda>", 8) == 0)
5278 {
5279 arg += 8;
5280 (void)getdigits(&arg);
5281 fname = vim_strnsave(name, arg - name);
5282 }
5283 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005284 {
5285 // First try finding a method in a class, find_func_by_name() will give
5286 // an error if the function is not found.
5287 ufunc = find_class_func(&arg);
5288 if (ufunc != NULL)
5289 return ufunc;
5290
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005291 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005292 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
5293 NULL, NULL, NULL);
5294 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005295 if (fname == NULL)
5296 {
5297 semsg(_(e_invalid_argument_str), name);
5298 return NULL;
5299 }
5300 if (!ends_excmd2(name, arg))
5301 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005302 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005303 emsg(ex_errmsg(e_trailing_characters_str, arg));
5304 return NULL;
5305 }
5306
5307 ufunc = find_func(fname, is_global);
5308 if (ufunc == NULL)
5309 {
5310 char_u *p = untrans_function_name(fname);
5311
5312 if (p != NULL)
5313 // Try again without making it script-local.
5314 ufunc = find_func(p, FALSE);
5315 }
5316 vim_free(fname);
5317 if (ufunc == NULL)
5318 semsg(_(e_cannot_find_function_str), name);
5319 return ufunc;
5320}
5321
5322/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005323 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005324 * be compiled or the one specified by the argument.
5325 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005326 */
5327 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005328ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005329{
Bram Moolenaar822ba242020-05-24 23:00:18 +02005330 ufunc_T *ufunc;
5331
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005332 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005333 {
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005334 compiletype_T compile_type = CT_NONE;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005335
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005336 ufunc = find_func_by_name(eap->arg, &compile_type);
5337 if (ufunc != NULL)
5338 {
5339 if (func_needs_compiling(ufunc, compile_type))
5340 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5341 else
5342 smsg(_("Function %s does not need compiling"), eap->arg);
5343 }
5344 }
5345 else
5346 {
5347 long todo = (long)func_hashtab.ht_used;
5348 int changed = func_hashtab.ht_changed;
5349 hashitem_T *hi;
5350
5351 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5352 {
5353 if (!HASHITEM_EMPTY(hi))
5354 {
5355 --todo;
5356 ufunc = HI2UF(hi);
5357 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5358 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5359 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005360 {
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005361 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5362
5363 if (func_hashtab.ht_changed != changed)
5364 {
5365 // a function has been added or removed, need to start
5366 // over
5367 todo = (long)func_hashtab.ht_used;
5368 changed = func_hashtab.ht_changed;
5369 hi = func_hashtab.ht_array;
5370 --hi;
5371 }
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005372 }
5373 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005374 }
5375 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005376}
5377
5378/*
5379 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5380 * Return 2 if "p" starts with "s:".
5381 * Return 0 otherwise.
5382 */
5383 int
5384eval_fname_script(char_u *p)
5385{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005386 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5387 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005388 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5389 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5390 return 5;
5391 if (p[0] == 's' && p[1] == ':')
5392 return 2;
5393 return 0;
5394}
5395
5396 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005397translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005398{
5399 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005400 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005401 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005402}
5403
5404/*
5405 * Return TRUE when "ufunc" has old-style "..." varargs
5406 * or named varargs "...name: type".
5407 */
5408 int
5409has_varargs(ufunc_T *ufunc)
5410{
5411 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005412}
5413
5414/*
5415 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005416 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005417 */
5418 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005419function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005420{
5421 char_u *nm = name;
5422 char_u *p;
5423 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005424 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005425 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005426
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005427 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5428 if (no_deref)
5429 flag |= TFN_NO_DEREF;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005430 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005431 nm = skipwhite(nm);
5432
Bram Moolenaare38eab22019-12-05 21:50:01 +01005433 // Only accept "funcname", "funcname ", "funcname (..." and
5434 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005435 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005436 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005437 vim_free(p);
5438 return n;
5439}
5440
Bram Moolenaar113e1072019-01-20 15:30:40 +01005441#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005442 char_u *
5443get_expanded_name(char_u *name, int check)
5444{
5445 char_u *nm = name;
5446 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005447 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005448
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005449 p = trans_function_name(&nm, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005450 TFN_INT|TFN_QUIET, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005451
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005452 if (p != NULL && *nm == NUL
5453 && (!check || translated_function_exists(p, is_global)))
5454 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005455
5456 vim_free(p);
5457 return NULL;
5458}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005459#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005460
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005461/*
5462 * Function given to ExpandGeneric() to obtain the list of user defined
5463 * function names.
5464 */
5465 char_u *
5466get_user_func_name(expand_T *xp, int idx)
5467{
5468 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005469 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005470 static hashitem_T *hi;
5471 ufunc_T *fp;
5472
5473 if (idx == 0)
5474 {
5475 done = 0;
5476 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005477 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005478 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005479 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005480 {
5481 if (done++ > 0)
5482 ++hi;
5483 while (HASHITEM_EMPTY(hi))
5484 ++hi;
5485 fp = HI2UF(hi);
5486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005487 // don't show dead, dict and lambda functions
5488 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005489 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005490 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005491
5492 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005493 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005494
5495 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005496 if (xp->xp_context != EXPAND_USER_FUNC
5497 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005498 {
5499 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005500 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005501 STRCAT(IObuff, ")");
5502 }
5503 return IObuff;
5504 }
5505 return NULL;
5506}
5507
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005508/*
5509 * ":delfunction {name}"
5510 */
5511 void
5512ex_delfunction(exarg_T *eap)
5513{
5514 ufunc_T *fp = NULL;
5515 char_u *p;
5516 char_u *name;
5517 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005518 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005519
5520 p = eap->arg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005521 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi,
5522 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005523 vim_free(fudi.fd_newkey);
5524 if (name == NULL)
5525 {
5526 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005527 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005528 return;
5529 }
5530 if (!ends_excmd(*skipwhite(p)))
5531 {
5532 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005533 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005534 return;
5535 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005536 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005537 if (eap->nextcmd != NULL)
5538 *p = NUL;
5539
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005540 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005541 {
5542 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005543 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005544 vim_free(name);
5545 return;
5546 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005547 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005548 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005549 vim_free(name);
5550
5551 if (!eap->skip)
5552 {
5553 if (fp == NULL)
5554 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005555 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005556 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005557 return;
5558 }
5559 if (fp->uf_calls > 0)
5560 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005561 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005562 return;
5563 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005564 if (fp->uf_flags & FC_VIM9)
5565 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005566 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005567 return;
5568 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005569
5570 if (fudi.fd_dict != NULL)
5571 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005572 // Delete the dict item that refers to the function, it will
5573 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00005574 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005575 }
5576 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005577 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005578 // A normal function (not a numbered function or lambda) has a
5579 // refcount of 1 for the entry in the hashtable. When deleting
5580 // it and the refcount is more than one, it should be kept.
5581 // A numbered function and lambda should be kept if the refcount is
5582 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005583 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005584 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005585 // Function is still referenced somewhere. Don't free it but
5586 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005587 if (func_remove(fp))
5588 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005589 }
5590 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005591 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005592 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005593 }
5594}
5595
5596/*
5597 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005598 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005599 */
5600 void
5601func_unref(char_u *name)
5602{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005603 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005604
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005605 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005606 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005607 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005608 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005609 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005610#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005611 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005612#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005613 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005614 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005615 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005616}
5617
5618/*
5619 * Unreference a Function: decrement the reference count and free it when it
5620 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005621 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005622 */
5623 void
5624func_ptr_unref(ufunc_T *fp)
5625{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005626 if (fp != NULL && (--fp->uf_refcount <= 0
5627 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5628 && fp->uf_partial->pt_refcount <= 1
5629 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005630 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005631 // Only delete it when it's not being used. Otherwise it's done
5632 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005633 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005634 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005635 }
5636}
5637
5638/*
5639 * Count a reference to a Function.
5640 */
5641 void
5642func_ref(char_u *name)
5643{
5644 ufunc_T *fp;
5645
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005646 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005647 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005648 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005649 if (fp != NULL)
5650 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005651 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005652 // Only give an error for a numbered function.
5653 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005654 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005655}
5656
5657/*
5658 * Count a reference to a Function.
5659 */
5660 void
5661func_ptr_ref(ufunc_T *fp)
5662{
5663 if (fp != NULL)
5664 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005665}
5666
5667/*
5668 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5669 * referenced from anywhere that is in use.
5670 */
5671 static int
5672can_free_funccal(funccall_T *fc, int copyID)
5673{
Bram Moolenaarca16c602022-09-06 18:57:08 +01005674 return (fc->fc_l_varlist.lv_copyID != copyID
5675 && fc->fc_l_vars.dv_copyID != copyID
5676 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005677 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005678}
5679
5680/*
5681 * ":return [expr]"
5682 */
5683 void
5684ex_return(exarg_T *eap)
5685{
5686 char_u *arg = eap->arg;
5687 typval_T rettv;
5688 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005689 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005690
5691 if (current_funccal == NULL)
5692 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005693 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005694 return;
5695 }
5696
Bram Moolenaar844fb642021-10-23 13:32:30 +01005697 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005698 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5699
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005700 if (eap->skip)
5701 ++emsg_skip;
5702
5703 eap->nextcmd = NULL;
5704 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005705 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005706 {
5707 if (!eap->skip)
5708 returning = do_return(eap, FALSE, TRUE, &rettv);
5709 else
5710 clear_tv(&rettv);
5711 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005712 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005713 else if (!eap->skip)
5714 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005715 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005716 update_force_abort();
5717
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005718 /*
5719 * Return unless the expression evaluation has been cancelled due to an
5720 * aborting error, an interrupt, or an exception.
5721 */
5722 if (!aborting())
5723 returning = do_return(eap, FALSE, TRUE, NULL);
5724 }
5725
Bram Moolenaare38eab22019-12-05 21:50:01 +01005726 // When skipping or the return gets pending, advance to the next command
5727 // in this line (!returning). Otherwise, ignore the rest of the line.
5728 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005729 if (returning)
5730 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005731 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02005732 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005733
5734 if (eap->skip)
5735 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02005736 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005737}
5738
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005739 static int
5740ex_call_inner(
5741 exarg_T *eap,
5742 char_u *name,
5743 char_u **arg,
5744 char_u *startarg,
5745 funcexe_T *funcexe_init,
5746 evalarg_T *evalarg)
5747{
5748 linenr_T lnum;
5749 int doesrange;
5750 typval_T rettv;
5751 int failed = FALSE;
5752
5753 /*
5754 * When skipping, evaluate the function once, to find the end of the
5755 * arguments.
5756 * When the function takes a range, this is discovered after the first
5757 * call, and the loop is broken.
5758 */
5759 if (eap->skip)
5760 {
5761 ++emsg_skip;
5762 lnum = eap->line2; // do it once, also with an invalid range
5763 }
5764 else
5765 lnum = eap->line1;
5766 for ( ; lnum <= eap->line2; ++lnum)
5767 {
5768 funcexe_T funcexe;
5769
5770 if (!eap->skip && eap->addr_count > 0)
5771 {
5772 if (lnum > curbuf->b_ml.ml_line_count)
5773 {
5774 // If the function deleted lines or switched to another buffer
5775 // the line number may become invalid.
5776 emsg(_(e_invalid_range));
5777 break;
5778 }
5779 curwin->w_cursor.lnum = lnum;
5780 curwin->w_cursor.col = 0;
5781 curwin->w_cursor.coladd = 0;
5782 }
5783 *arg = startarg;
5784
5785 funcexe = *funcexe_init;
5786 funcexe.fe_doesrange = &doesrange;
5787 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
5788 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
5789 {
5790 failed = TRUE;
5791 break;
5792 }
5793 if (has_watchexpr())
5794 dbg_check_breakpoint(eap);
5795
5796 // Handle a function returning a Funcref, Dictionary or List.
5797 if (handle_subscript(arg, NULL, &rettv,
5798 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
5799 {
5800 failed = TRUE;
5801 break;
5802 }
5803
5804 clear_tv(&rettv);
5805 if (doesrange || eap->skip)
5806 break;
5807
5808 // Stop when immediately aborting on error, or when an interrupt
5809 // occurred or an exception was thrown but not caught.
5810 // get_func_tv() returned OK, so that the check for trailing
5811 // characters below is executed.
5812 if (aborting())
5813 break;
5814 }
5815 if (eap->skip)
5816 --emsg_skip;
5817 return failed;
5818}
5819
5820/*
5821 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
5822 * Returns FAIL or OK.
5823 */
5824 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01005825ex_defer_inner(
5826 char_u *name,
5827 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01005828 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01005829 partial_T *partial,
5830 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005831{
5832 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01005833 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005834 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01005835 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005836
5837 if (current_funccal == NULL)
5838 {
5839 semsg(_(e_str_not_inside_function), "defer");
5840 return FAIL;
5841 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01005842 if (partial != NULL)
5843 {
5844 if (partial->pt_dict != NULL)
5845 {
5846 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
5847 return FAIL;
5848 }
5849 if (partial->pt_argc > 0)
5850 {
5851 int i;
5852
5853 partial_argc = partial->pt_argc;
5854 for (i = 0; i < partial_argc; ++i)
5855 copy_tv(&partial->pt_argv[i], &argvars[i]);
5856 }
5857 }
5858 r = get_func_arguments(arg, evalarg, FALSE,
5859 argvars + partial_argc, &argcount);
5860 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01005861
5862 if (r == OK)
5863 {
5864 if (type != NULL)
5865 {
5866 // Check that the arguments are OK for the types of the funcref.
5867 r = check_argument_types(type, argvars, argcount, NULL, name);
5868 }
5869 else if (builtin_function(name, -1))
5870 {
5871 int idx = find_internal_func(name);
5872
5873 if (idx < 0)
5874 {
5875 emsg_funcname(e_unknown_function_str, name);
5876 r = FAIL;
5877 }
5878 else if (check_internal_func(idx, argcount) == -1)
5879 r = FAIL;
5880 }
5881 else
5882 {
5883 ufunc_T *ufunc = find_func(name, FALSE);
5884
5885 // we tolerate an unknown function here, it might be defined later
5886 if (ufunc != NULL)
5887 {
5888 int error = check_user_func_argcount(ufunc, argcount);
5889
5890 if (error != FCERR_UNKNOWN)
5891 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01005892 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01005893 r = FAIL;
5894 }
5895 }
5896 }
5897 }
5898
Bram Moolenaar86d87252022-09-05 21:21:25 +01005899 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01005900 {
5901 while (--argcount >= 0)
5902 clear_tv(&argvars[argcount]);
5903 return FAIL;
5904 }
5905 return add_defer(name, argcount, argvars);
5906}
5907
5908/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01005909 * Return TRUE if currently inside a function call.
5910 * Give an error message and return FALSE when not.
5911 */
5912 int
5913can_add_defer(void)
5914{
5915 if (!in_def_function() && get_current_funccal() == NULL)
5916 {
5917 semsg(_(e_str_not_inside_function), "defer");
5918 return FALSE;
5919 }
5920 return TRUE;
5921}
5922
5923/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005924 * Add a deferred call for "name" with arguments "argvars[argcount]".
5925 * Consumes "argvars[]".
5926 * Caller must check that in_def_function() returns TRUE or current_funccal is
5927 * not NULL.
5928 * Returns OK or FAIL.
5929 */
5930 int
5931add_defer(char_u *name, int argcount_arg, typval_T *argvars)
5932{
5933 char_u *saved_name = vim_strsave(name);
5934 int argcount = argcount_arg;
5935 defer_T *dr;
5936 int ret = FAIL;
5937
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005938 if (saved_name == NULL)
5939 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01005940 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005941 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01005942 if (add_defer_function(saved_name, argcount, argvars) == OK)
5943 argcount = 0;
5944 }
5945 else
5946 {
5947 if (current_funccal->fc_defer.ga_itemsize == 0)
5948 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
5949 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
5950 goto theend;
5951 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
5952 + current_funccal->fc_defer.ga_len++;
5953 dr->dr_name = saved_name;
5954 dr->dr_argcount = argcount;
5955 while (argcount > 0)
5956 {
5957 --argcount;
5958 dr->dr_argvars[argcount] = argvars[argcount];
5959 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005960 }
5961 ret = OK;
5962
5963theend:
5964 while (--argcount >= 0)
5965 clear_tv(&argvars[argcount]);
5966 return ret;
5967}
5968
5969/*
5970 * Invoked after a functions has finished: invoke ":defer" functions.
5971 */
Bram Moolenaar58779852022-09-06 18:31:14 +01005972 static void
5973handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005974{
5975 int idx;
5976
Bram Moolenaar58779852022-09-06 18:31:14 +01005977 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005978 {
5979 funcexe_T funcexe;
5980 typval_T rettv;
Bram Moolenaar58779852022-09-06 18:31:14 +01005981 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005982 int i;
5983
5984 CLEAR_FIELD(funcexe);
5985 funcexe.fe_evaluate = TRUE;
5986
5987 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
5988 call_func(dr->dr_name, -1, &rettv,
5989 dr->dr_argcount, dr->dr_argvars, &funcexe);
5990 clear_tv(&rettv);
5991 vim_free(dr->dr_name);
5992 for (i = dr->dr_argcount - 1; i >= 0; --i)
5993 clear_tv(&dr->dr_argvars[i]);
5994 }
Bram Moolenaar58779852022-09-06 18:31:14 +01005995 ga_clear(&funccal->fc_defer);
5996}
5997
5998/*
5999 * Called when exiting: call all defer functions.
6000 */
6001 void
6002invoke_all_defer(void)
6003{
6004 funccall_T *funccal;
6005
Bram Moolenaarca16c602022-09-06 18:57:08 +01006006 for (funccal = current_funccal; funccal != NULL;
6007 funccal = funccal->fc_caller)
Bram Moolenaar58779852022-09-06 18:31:14 +01006008 if (funccal->fc_ectx != NULL)
6009 {
6010 // :def function
6011 unwind_def_callstack(funccal->fc_ectx);
6012 may_invoke_defer_funcs(funccal->fc_ectx);
6013 }
6014 else
6015 {
6016 // legacy function
6017 handle_defer_one(funccal);
6018 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006019}
6020
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006021/*
6022 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006023 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006024 */
6025 void
6026ex_call(exarg_T *eap)
6027{
6028 char_u *arg = eap->arg;
6029 char_u *startarg;
6030 char_u *name;
6031 char_u *tofree;
6032 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006033 int failed = FALSE;
6034 funcdict_T fudi;
6035 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006036 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006037 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006038 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006039 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006040
Bram Moolenaare6b53242020-07-01 17:28:33 +02006041 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006042 if (eap->skip)
6043 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006044 typval_T rettv;
6045
Bram Moolenaare38eab22019-12-05 21:50:01 +01006046 // trans_function_name() doesn't work well when skipping, use eval0()
6047 // instead to skip to any following command, e.g. for:
6048 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006049 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006050 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006051 clear_tv(&rettv);
6052 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006053 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006054 return;
6055 }
6056
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006057 tofree = trans_function_name(&arg, NULL, eap->skip, TFN_INT,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006058 &fudi, &partial, vim9script ? &type : NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006059 if (fudi.fd_newkey != NULL)
6060 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006061 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006062 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006063 vim_free(fudi.fd_newkey);
6064 }
6065 if (tofree == NULL)
6066 return;
6067
Bram Moolenaare38eab22019-12-05 21:50:01 +01006068 // Increase refcount on dictionary, it could get deleted when evaluating
6069 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006070 if (fudi.fd_dict != NULL)
6071 ++fudi.fd_dict->dv_refcount;
6072
Bram Moolenaare38eab22019-12-05 21:50:01 +01006073 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6074 // contents. For VAR_PARTIAL get its partial, unless we already have one
6075 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006076 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006077 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006078 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006079 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006080
Bram Moolenaare38eab22019-12-05 21:50:01 +01006081 // Skip white space to allow ":call func ()". Not good, but required for
6082 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006083 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006084 if (*startarg != '(')
6085 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006086 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006087 goto end;
6088 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006089 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006090 {
6091 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6092 goto end;
6093 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006094
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006095 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006096 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006097 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006098 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006099 }
6100 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006101 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006102 funcexe_T funcexe;
6103
Bram Moolenaara80faa82020-04-12 19:37:17 +02006104 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006105 funcexe.fe_check_type = type;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006106 funcexe.fe_partial = partial;
6107 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006108 funcexe.fe_firstline = eap->line1;
6109 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006110 funcexe.fe_found_var = found_var;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006111 funcexe.fe_evaluate = !eap->skip;
6112 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006113 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006114
Bram Moolenaar1d341892021-09-18 15:25:52 +02006115 // When inside :try we need to check for following "| catch" or "| endtry".
6116 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006117 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006118 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006119 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006120 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006121 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006122 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006123 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006124 {
6125 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006126 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006127 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006128 }
6129 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006130 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006131 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006132 // Must be after using "arg", it may point into memory cleared here.
6133 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006134
6135end:
6136 dict_unref(fudi.fd_dict);
6137 vim_free(tofree);
6138}
6139
6140/*
6141 * Return from a function. Possibly makes the return pending. Also called
6142 * for a pending return at the ":endtry" or after returning from an extra
6143 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6144 * when called due to a ":return" command. "rettv" may point to a typval_T
6145 * with the return rettv. Returns TRUE when the return can be carried out,
6146 * FALSE when the return gets pending.
6147 */
6148 int
6149do_return(
6150 exarg_T *eap,
6151 int reanimate,
6152 int is_cmd,
6153 void *rettv)
6154{
6155 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006156 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006157
6158 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006159 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006160 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006161
6162 /*
6163 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6164 * not in its finally clause (which then is to be executed next) is found.
6165 * In this case, make the ":return" pending for execution at the ":endtry".
6166 * Otherwise, return normally.
6167 */
6168 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6169 if (idx >= 0)
6170 {
6171 cstack->cs_pending[idx] = CSTP_RETURN;
6172
6173 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006174 // A pending return again gets pending. "rettv" points to an
6175 // allocated variable with the rettv of the original ":return"'s
6176 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006177 cstack->cs_rettv[idx] = rettv;
6178 else
6179 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006180 // When undoing a return in order to make it pending, get the stored
6181 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006182 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006183 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006184
6185 if (rettv != NULL)
6186 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006187 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006188 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6189 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6190 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006191 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006192 }
6193 else
6194 cstack->cs_rettv[idx] = NULL;
6195
6196 if (reanimate)
6197 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006198 // The pending return value could be overwritten by a ":return"
6199 // without argument in a finally clause; reset the default
6200 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006201 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6202 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006203 }
6204 }
6205 report_make_pending(CSTP_RETURN, rettv);
6206 }
6207 else
6208 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006209 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006210
Bram Moolenaare38eab22019-12-05 21:50:01 +01006211 // If the return is carried out now, store the return value. For
6212 // a return immediately after reanimation, the value is already
6213 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006214 if (!reanimate && rettv != NULL)
6215 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006216 clear_tv(current_funccal->fc_rettv);
6217 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006218 if (!is_cmd)
6219 vim_free(rettv);
6220 }
6221 }
6222
6223 return idx < 0;
6224}
6225
6226/*
6227 * Free the variable with a pending return value.
6228 */
6229 void
6230discard_pending_return(void *rettv)
6231{
6232 free_tv((typval_T *)rettv);
6233}
6234
6235/*
6236 * Generate a return command for producing the value of "rettv". The result
6237 * is an allocated string. Used by report_pending() for verbose messages.
6238 */
6239 char_u *
6240get_return_cmd(void *rettv)
6241{
6242 char_u *s = NULL;
6243 char_u *tofree = NULL;
6244 char_u numbuf[NUMBUFLEN];
6245
6246 if (rettv != NULL)
6247 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6248 if (s == NULL)
6249 s = (char_u *)"";
6250
6251 STRCPY(IObuff, ":return ");
6252 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6253 if (STRLEN(s) + 8 >= IOSIZE)
6254 STRCPY(IObuff + IOSIZE - 4, "...");
6255 vim_free(tofree);
6256 return vim_strsave(IObuff);
6257}
6258
6259/*
6260 * Get next function line.
6261 * Called by do_cmdline() to get the next line.
6262 * Returns allocated string, or NULL for end of function.
6263 */
6264 char_u *
6265get_func_line(
6266 int c UNUSED,
6267 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006268 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006269 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006270{
6271 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006272 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006273 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006274 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006275
Bram Moolenaare38eab22019-12-05 21:50:01 +01006276 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006277 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006278 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006279 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006280 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006281 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006282 }
6283#ifdef FEAT_PROFILE
6284 if (do_profiling == PROF_YES)
6285 func_line_end(cookie);
6286#endif
6287
6288 gap = &fp->uf_lines;
6289 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006290 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006291 retval = NULL;
6292 else
6293 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006294 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006295 while (fcp->fc_linenr < gap->ga_len
6296 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6297 ++fcp->fc_linenr;
6298 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006299 retval = NULL;
6300 else
6301 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006302 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6303 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006304#ifdef FEAT_PROFILE
6305 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006306 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006307#endif
6308 }
6309 }
6310
Bram Moolenaare38eab22019-12-05 21:50:01 +01006311 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006312 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006313 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006314 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006315 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006316 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006317 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006318 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006319 }
6320
6321 return retval;
6322}
6323
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006324/*
6325 * Return TRUE if the currently active function should be ended, because a
6326 * return was encountered or an error occurred. Used inside a ":while".
6327 */
6328 int
6329func_has_ended(void *cookie)
6330{
6331 funccall_T *fcp = (funccall_T *)cookie;
6332
Bram Moolenaare38eab22019-12-05 21:50:01 +01006333 // Ignore the "abort" flag if the abortion behavior has been changed due to
6334 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006335 return (((fcp->fc_func->uf_flags & FC_ABORT)
6336 && did_emsg && !aborted_in_try())
6337 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006338}
6339
6340/*
6341 * return TRUE if cookie indicates a function which "abort"s on errors.
6342 */
6343 int
6344func_has_abort(
6345 void *cookie)
6346{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006347 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006348}
6349
6350
6351/*
6352 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6353 * Don't do this when "Func" is already a partial that was bound
6354 * explicitly (pt_auto is FALSE).
6355 * Changes "rettv" in-place.
6356 * Returns the updated "selfdict_in".
6357 */
6358 dict_T *
6359make_partial(dict_T *selfdict_in, typval_T *rettv)
6360{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006361 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006362 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006363 char_u fname_buf[FLEN_FIXED + 1];
6364 int error;
6365 dict_T *selfdict = selfdict_in;
6366
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006367 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6368 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006369 fp = rettv->vval.v_partial->pt_func;
6370 else
6371 {
6372 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006373 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006374 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006375 if (fname == NULL)
6376 {
6377 // There is no point binding a dict to a NULL function, just create
6378 // a function reference.
6379 rettv->v_type = VAR_FUNC;
6380 rettv->vval.v_string = NULL;
6381 }
6382 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006383 {
6384 char_u *tofree = NULL;
6385
6386 // Translate "s:func" to the stored function name.
6387 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6388 fp = find_func(fname, FALSE);
6389 vim_free(tofree);
6390 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006391 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006392
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006393 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006394 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006395 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006396
6397 if (pt != NULL)
6398 {
6399 pt->pt_refcount = 1;
6400 pt->pt_dict = selfdict;
6401 pt->pt_auto = TRUE;
6402 selfdict = NULL;
6403 if (rettv->v_type == VAR_FUNC)
6404 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006405 // Just a function: Take over the function name and use
6406 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006407 pt->pt_name = rettv->vval.v_string;
6408 }
6409 else
6410 {
6411 partial_T *ret_pt = rettv->vval.v_partial;
6412 int i;
6413
Bram Moolenaare38eab22019-12-05 21:50:01 +01006414 // Partial: copy the function name, use selfdict and copy
6415 // args. Can't take over name or args, the partial might
6416 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006417 if (ret_pt->pt_name != NULL)
6418 {
6419 pt->pt_name = vim_strsave(ret_pt->pt_name);
6420 func_ref(pt->pt_name);
6421 }
6422 else
6423 {
6424 pt->pt_func = ret_pt->pt_func;
6425 func_ptr_ref(pt->pt_func);
6426 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006427 if (ret_pt->pt_argc > 0)
6428 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006429 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006430 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006431 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006432 pt->pt_argc = 0;
6433 else
6434 {
6435 pt->pt_argc = ret_pt->pt_argc;
6436 for (i = 0; i < pt->pt_argc; i++)
6437 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6438 }
6439 }
6440 partial_unref(ret_pt);
6441 }
6442 rettv->v_type = VAR_PARTIAL;
6443 rettv->vval.v_partial = pt;
6444 }
6445 }
6446 return selfdict;
6447}
6448
6449/*
6450 * Return the name of the executed function.
6451 */
6452 char_u *
6453func_name(void *cookie)
6454{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006455 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006456}
6457
6458/*
6459 * Return the address holding the next breakpoint line for a funccall cookie.
6460 */
6461 linenr_T *
6462func_breakpoint(void *cookie)
6463{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006464 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006465}
6466
6467/*
6468 * Return the address holding the debug tick for a funccall cookie.
6469 */
6470 int *
6471func_dbg_tick(void *cookie)
6472{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006473 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006474}
6475
6476/*
6477 * Return the nesting level for a funccall cookie.
6478 */
6479 int
6480func_level(void *cookie)
6481{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006482 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006483}
6484
6485/*
6486 * Return TRUE when a function was ended by a ":return" command.
6487 */
6488 int
6489current_func_returned(void)
6490{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006491 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006492}
6493
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006494 int
6495free_unref_funccal(int copyID, int testing)
6496{
6497 int did_free = FALSE;
6498 int did_free_funccal = FALSE;
6499 funccall_T *fc, **pfc;
6500
6501 for (pfc = &previous_funccal; *pfc != NULL; )
6502 {
6503 if (can_free_funccal(*pfc, copyID))
6504 {
6505 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006506 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006507 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006508 did_free = TRUE;
6509 did_free_funccal = TRUE;
6510 }
6511 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006512 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006513 }
6514 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006515 // When a funccal was freed some more items might be garbage
6516 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006517 (void)garbage_collect(testing);
6518
6519 return did_free;
6520}
6521
6522/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006523 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006524 */
6525 static funccall_T *
6526get_funccal(void)
6527{
6528 int i;
6529 funccall_T *funccal;
6530 funccall_T *temp_funccal;
6531
6532 funccal = current_funccal;
6533 if (debug_backtrace_level > 0)
6534 {
6535 for (i = 0; i < debug_backtrace_level; i++)
6536 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006537 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006538 if (temp_funccal)
6539 funccal = temp_funccal;
6540 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006541 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006542 debug_backtrace_level = i;
6543 }
6544 }
6545 return funccal;
6546}
6547
6548/*
6549 * Return the hashtable used for local variables in the current funccal.
6550 * Return NULL if there is no current funccal.
6551 */
6552 hashtab_T *
6553get_funccal_local_ht()
6554{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006555 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006556 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006557 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006558}
6559
6560/*
6561 * Return the l: scope variable.
6562 * Return NULL if there is no current funccal.
6563 */
6564 dictitem_T *
6565get_funccal_local_var()
6566{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006567 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006568 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006569 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006570}
6571
6572/*
6573 * Return the hashtable used for argument in the current funccal.
6574 * Return NULL if there is no current funccal.
6575 */
6576 hashtab_T *
6577get_funccal_args_ht()
6578{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006579 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006580 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006581 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006582}
6583
6584/*
6585 * Return the a: scope variable.
6586 * Return NULL if there is no current funccal.
6587 */
6588 dictitem_T *
6589get_funccal_args_var()
6590{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006591 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006592 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006593 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006594}
6595
6596/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006597 * List function variables, if there is a function.
6598 */
6599 void
6600list_func_vars(int *first)
6601{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006602 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
6603 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006604 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006605}
6606
6607/*
6608 * If "ht" is the hashtable for local variables in the current funccal, return
6609 * the dict that contains it.
6610 * Otherwise return NULL.
6611 */
6612 dict_T *
6613get_current_funccal_dict(hashtab_T *ht)
6614{
6615 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01006616 && ht == &current_funccal->fc_l_vars.dv_hashtab)
6617 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006618 return NULL;
6619}
6620
6621/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006622 * Search hashitem in parent scope.
6623 */
6624 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006625find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006626{
6627 funccall_T *old_current_funccal = current_funccal;
6628 hashtab_T *ht;
6629 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006630 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006631
Bram Moolenaarca16c602022-09-06 18:57:08 +01006632 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006633 return NULL;
6634
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006635 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006636 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006637 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006638 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006639 ht = find_var_ht(name, &varname);
6640 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006641 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006642 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006643 if (!HASHITEM_EMPTY(hi))
6644 {
6645 *pht = ht;
6646 break;
6647 }
6648 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006649 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02006650 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006651 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006652 }
6653 current_funccal = old_current_funccal;
6654
6655 return hi;
6656}
6657
6658/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006659 * Search variable in parent scope.
6660 */
6661 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006662find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006663{
6664 dictitem_T *v = NULL;
6665 funccall_T *old_current_funccal = current_funccal;
6666 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006667 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006668
Bram Moolenaarca16c602022-09-06 18:57:08 +01006669 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006670 return NULL;
6671
Bram Moolenaare38eab22019-12-05 21:50:01 +01006672 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01006673 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006674 while (current_funccal)
6675 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006676 ht = find_var_ht(name, &varname);
6677 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006678 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006679 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006680 if (v != NULL)
6681 break;
6682 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006683 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006684 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006685 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006686 }
6687 current_funccal = old_current_funccal;
6688
6689 return v;
6690}
6691
6692/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006693 * Set "copyID + 1" in previous_funccal and callers.
6694 */
6695 int
6696set_ref_in_previous_funccal(int copyID)
6697{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006698 funccall_T *fc;
6699
Bram Moolenaarca16c602022-09-06 18:57:08 +01006700 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006701 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006702 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006703 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
6704 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
6705 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006706 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006707 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006708 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006709}
6710
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006711 static int
6712set_ref_in_funccal(funccall_T *fc, int copyID)
6713{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006714 if (fc->fc_copyID != copyID)
6715 {
6716 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006717 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
6718 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
6719 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
6720 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006721 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006722 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006723 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006724}
6725
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006726/*
6727 * Set "copyID" in all local vars and arguments in the call stack.
6728 */
6729 int
6730set_ref_in_call_stack(int copyID)
6731{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006732 funccall_T *fc;
6733 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006734
Bram Moolenaarca16c602022-09-06 18:57:08 +01006735 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006736 if (set_ref_in_funccal(fc, copyID))
6737 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006738
6739 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006740 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006741 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006742 if (set_ref_in_funccal(fc, copyID))
6743 return TRUE;
6744 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006745}
6746
6747/*
6748 * Set "copyID" in all functions available by name.
6749 */
6750 int
6751set_ref_in_functions(int copyID)
6752{
6753 int todo;
6754 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006755 ufunc_T *fp;
6756
6757 todo = (int)func_hashtab.ht_used;
6758 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006759 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006760 if (!HASHITEM_EMPTY(hi))
6761 {
6762 --todo;
6763 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006764 if (!func_name_refcount(fp->uf_name)
6765 && set_ref_in_func(NULL, fp, copyID))
6766 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006767 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006768 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006769 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006770}
6771
6772/*
6773 * Set "copyID" in all function arguments.
6774 */
6775 int
6776set_ref_in_func_args(int copyID)
6777{
6778 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006779
6780 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006781 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
6782 copyID, NULL, NULL))
6783 return TRUE;
6784 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006785}
6786
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006787/*
6788 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006789 * Returns TRUE if setting references failed somehow.
6790 */
6791 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006792set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006793{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006794 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006795 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01006796 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006797 char_u fname_buf[FLEN_FIXED + 1];
6798 char_u *tofree = NULL;
6799 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006800 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006801
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006802 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006803 return FALSE;
6804
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006805 if (fp_in == NULL)
6806 {
6807 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006808 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006809 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006810 if (fp != NULL)
6811 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006812 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006813 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006814 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02006815
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006816 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006817 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006818}
6819
Bram Moolenaare38eab22019-12-05 21:50:01 +01006820#endif // FEAT_EVAL