blob: 2575e7d06055aa389e311d58a8c567746d442c7d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +020011 * userfunc.c: User defined function support
Bram Moolenaara9b579f2016-07-17 18:29:19 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020017/*
18 * All user-defined functions are found in this hashtable.
19 */
20static hashtab_T func_hashtab;
21
Bram Moolenaare38eab22019-12-05 21:50:01 +010022// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020023static garray_T funcargs = GA_EMPTY;
24
Bram Moolenaar209b8e32019-03-14 13:43:24 +010025// pointer to funccal for currently active function
26static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020027
Bram Moolenaar209b8e32019-03-14 13:43:24 +010028// Pointer to list of previously used funccal, still around because some
29// item in it is still being used.
30static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020031
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020032static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +010033static void func_clear(ufunc_T *fp, int force);
34static int func_free(ufunc_T *fp, int force);
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +010035static char_u *untrans_function_name(char_u *name);
Bram Moolenaar58779852022-09-06 18:31:14 +010036static void handle_defer_one(funccall_T *funccal);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020037
38 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +000039func_init(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020040{
41 hash_init(&func_hashtab);
42}
43
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020044/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020045 * Return the function hash table
46 */
47 hashtab_T *
48func_tbl_get(void)
49{
50 return &func_hashtab;
51}
52
53/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020054 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020055 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010056 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010057 * If "evalarg" is not NULL use it to check for an already declared name.
Bram Moolenaardce24412022-02-08 20:35:30 +000058 * If "eap" is not NULL use it to check for an already declared name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010059 * Return a pointer to after the type.
60 * When something is wrong return "arg".
61 */
62 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010063one_function_arg(
64 char_u *arg,
65 garray_T *newargs,
66 garray_T *argtypes,
67 int types_optional,
Bram Moolenaar057e84a2021-02-28 16:55:11 +010068 evalarg_T *evalarg,
Bram Moolenaardce24412022-02-08 20:35:30 +000069 exarg_T *eap,
Bram Moolenaar2a389082021-04-09 20:24:31 +020070 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010071 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010072{
Bram Moolenaar6e949782020-04-13 17:21:00 +020073 char_u *p = arg;
74 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020075 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010076
77 while (ASCII_ISALNUM(*p) || *p == '_')
78 ++p;
79 if (arg == p || isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020080 || (argtypes == NULL
81 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
82 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083 {
84 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000085 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 return arg;
87 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010088
Bram Moolenaar057e84a2021-02-28 16:55:11 +010089 // Vim9 script: cannot use script var name for argument. In function: also
90 // check local vars and arguments.
91 if (!skip && argtypes != NULL && check_defined(arg, p - arg,
Bram Moolenaardce24412022-02-08 20:35:30 +000092 evalarg == NULL ? NULL : evalarg->eval_cctx,
93 eap == NULL ? NULL : eap->cstack, TRUE) == FAIL)
Bram Moolenaarb4893b82021-02-21 22:20:24 +010094 return arg;
Bram Moolenaarb4893b82021-02-21 22:20:24 +010095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
97 return arg;
98 if (newargs != NULL)
99 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100100 int c;
101 int i;
102
103 c = *p;
104 *p = NUL;
105 arg_copy = vim_strsave(arg);
106 if (arg_copy == NULL)
107 {
108 *p = c;
109 return arg;
110 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200111 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200112 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200113 // Check for duplicate argument name.
114 for (i = 0; i < newargs->ga_len; ++i)
115 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
116 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000117 semsg(_(e_duplicate_argument_name_str), arg_copy);
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200118 vim_free(arg_copy);
119 return arg;
120 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100121 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
122 newargs->ga_len++;
123
124 *p = c;
125 }
126
127 // get any type from "arg: type"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100128 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100129 {
130 char_u *type = NULL;
131
Bram Moolenaar6e949782020-04-13 17:21:00 +0200132 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
133 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200134 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200135 arg_copy == NULL ? arg : arg_copy);
136 p = skipwhite(p);
137 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138 if (*p == ':')
139 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200140 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100141 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200142 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100143 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200144 return arg;
145 }
146 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200147 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100148 if (!skip)
149 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100150 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200151 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200152 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200153 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200154 arg_copy == NULL ? arg : arg_copy);
155 return arg;
156 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100157 if (!skip)
158 {
159 if (type == NULL && types_optional)
160 // lambda arguments default to "any" type
Bram Moolenaar2a389082021-04-09 20:24:31 +0200161 type = vim_strsave((char_u *)
162 (is_vararg ? "list<any>" : "any"));
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100163 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
164 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100165 }
166
167 return p;
168}
169
170/*
Bram Moolenaar7473a842021-12-28 17:55:26 +0000171 * Handle line continuation in function arguments or body.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000172 * Get a next line, store it in "eap" if appropriate and put the line in
173 * "lines_to_free" to free the line later.
Bram Moolenaar7473a842021-12-28 17:55:26 +0000174 */
175 static char_u *
176get_function_line(
177 exarg_T *eap,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000178 garray_T *lines_to_free,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000179 int indent,
180 getline_opt_T getline_options)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000181{
182 char_u *theline;
183
184 if (eap->getline == NULL)
Bram Moolenaarc97f9a52021-12-28 20:59:56 +0000185 theline = getcmdline(':', 0L, indent, 0);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000186 else
187 theline = eap->getline(':', eap->cookie, indent, getline_options);
188 if (theline != NULL)
189 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000190 if (lines_to_free->ga_len > 0
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000191 && eap->cmdlinep != NULL
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000192 && *eap->cmdlinep == ((char_u **)lines_to_free->ga_data)
193 [lines_to_free->ga_len - 1])
Bram Moolenaar7473a842021-12-28 17:55:26 +0000194 *eap->cmdlinep = theline;
Bram Moolenaarb5820102023-01-25 12:27:13 +0000195 (void)ga_add_string(lines_to_free, theline);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000196 }
197
198 return theline;
199}
200
201/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200202 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100203 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100204 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200205 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100206 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200207get_function_args(
208 char_u **argp,
209 char_u endchar,
210 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100211 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100212 int types_optional, // types optional if "argtypes" is not NULL
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100213 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200214 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200215 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200216 int skip,
Bram Moolenaardce24412022-02-08 20:35:30 +0000217 exarg_T *eap, // can be NULL
Bram Moolenaar554d0312023-01-05 19:59:18 +0000218 int in_class, // non-zero when inside a class or interface
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000219 garray_T *newlines, // function body lines
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000220 garray_T *lines_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200221{
222 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100223 char_u *arg;
224 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200225 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200226 int any_default = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100227 char_u *whitep = *argp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200228
229 if (newargs != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000230 ga_init2(newargs, sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100231 if (argtypes != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000232 ga_init2(argtypes, sizeof(char_u *), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200233 if (!skip && default_args != NULL)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000234 ga_init2(default_args, sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200235
236 if (varargs != NULL)
237 *varargs = FALSE;
238
239 /*
240 * Isolate the arguments: "arg1, arg2, ...)"
241 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100242 arg = skipwhite(*argp);
243 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200244 while (*p != endchar)
245 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200246 while (eap != NULL && eap->getline != NULL
247 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200248 {
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200249 // End of the line, get the next one.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000250 char_u *theline = get_function_line(eap, lines_to_free, 0,
Bram Moolenaar11ceb7d2021-12-28 20:49:56 +0000251 GETLINE_CONCAT_CONT);
Bram Moolenaar7473a842021-12-28 17:55:26 +0000252
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200253 if (theline == NULL)
254 break;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200255 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200256 p = skipwhite(theline);
257 }
258
259 if (mustend && *p != endchar)
260 {
261 if (!skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000262 semsg(_(e_invalid_argument_str), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100263 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200264 }
265 if (*p == endchar)
266 break;
267
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200268 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
269 {
270 if (varargs != NULL)
271 *varargs = TRUE;
272 p += 3;
273 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274
275 if (argtypes != NULL)
276 {
277 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200278 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100280 if (!skip)
281 emsg(_(e_missing_name_after_dots));
282 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100283 }
284
285 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100286 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaardce24412022-02-08 20:35:30 +0000287 evalarg, eap, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100288 if (p == arg)
289 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100290 if (*skipwhite(p) == '=')
291 {
292 emsg(_(e_cannot_use_default_for_variable_arguments));
293 break;
294 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200296 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000297 else if (in_class && STRNCMP(p, "this.", 5) == 0)
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000298 {
299 // this.memberName
300 p += 5;
301 arg = p;
302 while (ASCII_ISALNUM(*p) || *p == '_')
303 ++p;
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000304 char_u *argend = p;
305
306 if (*skipwhite(p) == '=')
307 {
308 char_u *defval = skipwhite(skipwhite(p) + 1);
309 if (STRNCMP(defval, "v:none", 6) != 0)
310 {
311 semsg(_(e_constructor_default_value_must_be_vnone_str), p);
312 goto err_ret;
313 }
314 any_default = TRUE;
315 p = defval + 6;
316
317 if (ga_grow(default_args, 1) == FAIL)
318 goto err_ret;
319
320 char_u *expr = vim_strsave((char_u *)"v:none");
321 if (expr == NULL)
322 goto err_ret;
323 ((char_u **)(default_args->ga_data))
324 [default_args->ga_len] = expr;
325 default_args->ga_len++;
326 }
327 else if (any_default)
328 {
329 emsg(_(e_non_default_argument_follows_default_argument));
330 goto err_ret;
331 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000332
333 // TODO: check the argument is indeed a member
334 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
335 return FAIL;
336 if (newargs != NULL)
337 {
338 ((char_u **)(newargs->ga_data))[newargs->ga_len] =
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000339 vim_strnsave(arg, argend - arg);
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000340 newargs->ga_len++;
341
342 if (argtypes != NULL && ga_grow(argtypes, 1) == OK)
343 {
344 // TODO: use the actual type
345 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] =
346 vim_strsave((char_u *)"any");
347
348 // Add a line to the function body for the assignment.
349 if (ga_grow(newlines, 1) == OK)
350 {
351 // "this.name = name"
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000352 int len = 5 + (argend - arg) + 3 + (argend - arg) + 1;
353 if (any_default)
354 len += 14 + 10;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000355 char_u *assignment = alloc(len);
356 if (assignment != NULL)
357 {
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000358 c = *argend;
359 *argend = NUL;
360 if (any_default)
361 vim_snprintf((char *)assignment, len,
362 "ifargisset %d this.%s = %s",
363 default_args->ga_len - 1, arg, arg);
364 else
365 vim_snprintf((char *)assignment, len,
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000366 "this.%s = %s", arg, arg);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000367 *argend = c;
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000368 ((char_u **)(newlines->ga_data))[
369 newlines->ga_len++] = assignment;
370 }
371 }
372 }
373 }
374 if (*p == ',')
375 ++p;
376 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200377 else
378 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200379 char_u *np;
380
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200381 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100382 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaardce24412022-02-08 20:35:30 +0000383 evalarg, eap, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200385 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200386
Bram Moolenaar015cf102021-06-26 21:52:02 +0200387 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200388 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200389 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200390 if (*np == '=' && np[1] != '=' && np[1] != '~'
391 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200392 {
393 typval_T rettv;
394
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100395 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200396 any_default = TRUE;
Bram Moolenaareb5adf12022-09-04 13:41:37 +0100397 p = skipwhite(np + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +0000398 char_u *expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200399 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200400 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200401 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200402 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200403 if (ga_grow(default_args, 1) == FAIL)
404 goto err_ret;
405
406 // trim trailing whitespace
407 while (p > expr && VIM_ISWHITE(p[-1]))
408 p--;
409 c = *p;
410 *p = NUL;
411 expr = vim_strsave(expr);
412 if (expr == NULL)
413 {
414 *p = c;
415 goto err_ret;
416 }
417 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200418 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200419 default_args->ga_len++;
420 *p = c;
421 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200422 }
423 else
424 mustend = TRUE;
425 }
426 else if (any_default)
427 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000428 emsg(_(e_non_default_argument_follows_default_argument));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200429 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200430 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200431
432 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
433 {
434 // Be tolerant when skipping
435 if (!skip)
436 {
437 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
438 goto err_ret;
439 }
440 p = skipwhite(p);
441 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200442 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200443 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200444 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200445 // Don't give this error when skipping, it makes the "->" not
446 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100447 // Allow missing space after comma in legacy functions.
448 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200449 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
450 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100451 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200452 goto err_ret;
453 }
454 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200455 else
456 mustend = TRUE;
457 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200458 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200459 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200460 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200461
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200462 if (*p != endchar)
463 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100464 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200465
466 *argp = p;
467 return OK;
468
469err_ret:
470 if (newargs != NULL)
471 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200472 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200473 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200474 return FAIL;
475}
476
477/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100478 * Parse the argument types, filling "fp->uf_arg_types".
479 * Return OK or FAIL.
480 */
481 static int
482parse_argument_types(ufunc_T *fp, garray_T *argtypes, int varargs)
483{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200484 int len = 0;
485
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100486 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
487 if (argtypes->ga_len > 0)
488 {
489 // When "varargs" is set the last name/type goes into uf_va_name
490 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200491 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100492
493 if (len > 0)
494 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
495 if (fp->uf_arg_types != NULL)
496 {
497 int i;
498 type_T *type;
499
500 for (i = 0; i < len; ++ i)
501 {
502 char_u *p = ((char_u **)argtypes->ga_data)[i];
503
504 if (p == NULL)
505 // will get the type from the default value
506 type = &t_unknown;
507 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100508 type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100509 if (type == NULL)
510 return FAIL;
511 fp->uf_arg_types[i] = type;
Bram Moolenaar1d9cef72022-03-17 16:30:03 +0000512 if (i < fp->uf_args.ga_len
513 && (type->tt_type == VAR_FUNC
514 || type->tt_type == VAR_PARTIAL)
515 && var_wrong_func_name(
516 ((char_u **)fp->uf_args.ga_data)[i], TRUE))
517 return FAIL;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100518 }
519 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100520 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200521
522 if (varargs)
523 {
524 char_u *p;
525
526 // Move the last argument "...name: type" to uf_va_name and
527 // uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200528 --fp->uf_args.ga_len;
Bram Moolenaarf0571712023-01-04 13:16:20 +0000529 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len];
530 ((char_u **)fp->uf_args.ga_data)[fp->uf_args.ga_len] = NULL;
Bram Moolenaar2a389082021-04-09 20:24:31 +0200531 p = ((char_u **)argtypes->ga_data)[len];
532 if (p == NULL)
533 // TODO: get type from default value
534 fp->uf_va_type = &t_list_any;
535 else
536 {
537 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
538 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
539 {
540 semsg(_(e_variable_arguments_type_must_be_list_str),
541 ((char_u **)argtypes->ga_data)[len]);
542 return FAIL;
543 }
544 }
545 if (fp->uf_va_type == NULL)
546 return FAIL;
547 }
548
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100549 return OK;
550}
551
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100552 static int
553parse_return_type(ufunc_T *fp, char_u *ret_type)
554{
555 if (ret_type == NULL)
556 fp->uf_ret_type = &t_void;
557 else
558 {
559 char_u *p = ret_type;
560
561 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
562 if (fp->uf_ret_type == NULL)
563 {
564 fp->uf_ret_type = &t_void;
565 return FAIL;
566 }
567 }
568 return OK;
569}
570
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100571/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200572 * Register function "fp" as using "current_funccal" as its scope.
573 */
574 static int
575register_closure(ufunc_T *fp)
576{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200577 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100578 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200579 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200580 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200581 fp->uf_scoped = current_funccal;
582 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200583
Bram Moolenaarca16c602022-09-06 18:57:08 +0100584 if (ga_grow(&current_funccal->fc_ufuncs, 1) == FAIL)
Bram Moolenaar58016442016-07-31 18:30:22 +0200585 return FAIL;
Bram Moolenaarca16c602022-09-06 18:57:08 +0100586 ((ufunc_T **)current_funccal->fc_ufuncs.ga_data)
587 [current_funccal->fc_ufuncs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200588 return OK;
589}
590
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100591 static void
592set_ufunc_name(ufunc_T *fp, char_u *name)
593{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100594 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
595 // actually extends beyond the struct.
596 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100597
598 if (name[0] == K_SPECIAL)
599 {
600 fp->uf_name_exp = alloc(STRLEN(name) + 3);
601 if (fp->uf_name_exp != NULL)
602 {
603 STRCPY(fp->uf_name_exp, "<SNR>");
604 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
605 }
606 }
607}
608
Bram Moolenaar58016442016-07-31 18:30:22 +0200609/*
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100610 * If "name" starts with K_SPECIAL and "buf[bufsize]" is big enough
611 * return "buf" filled with a readable function name.
612 * Otherwise just return "name", thus the return value can always be used.
613 * "name" and "buf" may be equal.
614 */
615 char_u *
616make_ufunc_name_readable(char_u *name, char_u *buf, size_t bufsize)
617{
618 size_t len;
619
620 if (name[0] != K_SPECIAL)
621 return name;
622 len = STRLEN(name);
623 if (len + 3 > bufsize)
624 return name;
625
Bram Moolenaar96e08e02022-03-31 21:40:33 +0100626 mch_memmove(buf + 5, name + 3, len - 2); // Include trailing NUL
Bram Moolenaara6c18d32022-03-31 20:02:56 +0100627 mch_memmove(buf, "<SNR>", 5);
628 return buf;
629}
630
631/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200632 * Get a name for a lambda. Returned in static memory.
633 */
634 char_u *
635get_lambda_name(void)
636{
637 static char_u name[30];
638 static int lambda_no = 0;
639
640 sprintf((char*)name, "<lambda>%d", ++lambda_no);
641 return name;
642}
643
Bram Moolenaare01e5212023-01-08 20:31:18 +0000644/*
645 * Allocate a "ufunc_T" for a function called "name".
646 * Makes sure the size is right.
647 */
648 static ufunc_T *
649alloc_ufunc(char_u *name)
650{
651 // When the name is short we need to make sure we allocate enough bytes for
652 // the whole struct, including any padding.
653 size_t len = offsetof(ufunc_T, uf_name) + STRLEN(name) + 1;
654 return alloc_clear(len < sizeof(ufunc_T) ? sizeof(ufunc_T) : len);
655}
656
Bram Moolenaar801ab062020-06-25 19:27:56 +0200657#if defined(FEAT_LUA) || defined(PROTO)
658/*
659 * Registers a native C callback which can be called from Vim script.
660 * Returns the name of the Vim script function.
661 */
662 char_u *
663register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
664{
665 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200666 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200667
Bram Moolenaare01e5212023-01-08 20:31:18 +0000668 fp = alloc_ufunc(name);
Bram Moolenaar801ab062020-06-25 19:27:56 +0200669 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200670 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200671
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200672 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200673 fp->uf_refcount = 1;
674 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000675 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200676 fp->uf_calls = 0;
677 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200678 fp->uf_cb = cb;
679 fp->uf_cb_free = cb_free;
680 fp->uf_cb_state = state;
681
682 set_ufunc_name(fp, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000683 hash_add(&func_hashtab, UF2HIKEY(fp), "add C function");
Bram Moolenaar801ab062020-06-25 19:27:56 +0200684
685 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200686}
687#endif
688
Bram Moolenaar04b12692020-05-04 23:24:44 +0200689/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100690 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100691 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100692 * If "white_error" is not NULL check for correct use of white space and set
693 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100694 * Return NULL if no valid arrow found.
695 */
696 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100697skip_arrow(
698 char_u *start,
699 int equal_arrow,
700 char_u **ret_type,
701 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100702{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100703 char_u *s = start;
704 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100705
706 if (equal_arrow)
707 {
708 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100709 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100710 if (white_error != NULL && !VIM_ISWHITE(s[1]))
711 {
712 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100713 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100714 return NULL;
715 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100716 s = skipwhite(s + 1);
717 *ret_type = s;
718 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100719 if (s == *ret_type)
720 {
721 emsg(_(e_missing_return_type));
722 return NULL;
723 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100724 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100725 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100726 s = skipwhite(s);
727 if (*s != '=')
728 return NULL;
729 ++s;
730 }
731 if (*s != '>')
732 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100733 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
734 || !IS_WHITE_OR_NUL(s[1])))
735 {
736 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100737 semsg(_(e_white_space_required_before_and_after_str_at_str),
738 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100739 return NULL;
740 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100741 return skipwhite(s + 1);
742}
743
744/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100745 * Check if "*cmd" points to a function command and if so advance "*cmd" and
746 * return TRUE.
747 * Otherwise return FALSE;
748 * Do not consider "function(" to be a command.
749 */
750 static int
751is_function_cmd(char_u **cmd)
752{
753 char_u *p = *cmd;
754
755 if (checkforcmd(&p, "function", 2))
756 {
757 if (*p == '(')
758 return FALSE;
759 *cmd = p;
760 return TRUE;
761 }
762 return FALSE;
763}
764
765/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200766 * Called when defining a function: The context may be needed for script
767 * variables declared in a block that is visible now but not when the function
768 * is compiled or called later.
769 */
770 static void
771function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
772{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000773 if (cstack == NULL || cstack->cs_idx < 0)
774 return;
775
776 int count = cstack->cs_idx + 1;
777 int i;
778
779 fp->uf_block_ids = ALLOC_MULT(int, count);
780 if (fp->uf_block_ids != NULL)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200781 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000782 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
783 sizeof(int) * count);
784 fp->uf_block_depth = count;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200785 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000786
787 // Set flag in each block to indicate a function was defined. This
788 // is used to keep the variable when leaving the block, see
789 // hide_script_var().
790 for (i = 0; i <= cstack->cs_idx; ++i)
791 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200792}
793
794/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100795 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200796 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100797 * "newlines" must already have been initialized.
798 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
799 */
800 static int
801get_function_body(
802 exarg_T *eap,
803 garray_T *newlines,
804 char_u *line_arg_in,
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000805 garray_T *lines_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100806{
807 linenr_T sourcing_lnum_top = SOURCING_LNUM;
808 linenr_T sourcing_lnum_off;
809 int saved_wait_return = need_wait_return;
810 char_u *line_arg = line_arg_in;
811 int vim9_function = eap->cmdidx == CMD_def
812 || eap->cmdidx == CMD_block;
813#define MAX_FUNC_NESTING 50
814 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200815 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100816 int nesting = 0;
817 getline_opt_T getline_options;
818 int indent = 2;
819 char_u *skip_until = NULL;
820 int ret = FAIL;
821 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200822 int heredoc_concat_len = 0;
823 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100824 char_u *heredoc_trimmed = NULL;
825
Bram Moolenaar20677332021-06-06 17:02:53 +0200826 ga_init2(&heredoc_ga, 1, 500);
827
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100828 // Detect having skipped over comment lines to find the return
829 // type. Add NULL lines to keep the line count correct.
830 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
831 if (SOURCING_LNUM < sourcing_lnum_off)
832 {
833 sourcing_lnum_off -= SOURCING_LNUM;
834 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
835 goto theend;
836 while (sourcing_lnum_off-- > 0)
837 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
838 }
839
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200840 nesting_def[0] = vim9_function;
841 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100842 getline_options = vim9_function
843 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
844 for (;;)
845 {
846 char_u *theline;
847 char_u *p;
848 char_u *arg;
849
850 if (KeyTyped)
851 {
852 msg_scroll = TRUE;
853 saved_wait_return = FALSE;
854 }
855 need_wait_return = FALSE;
856
857 if (line_arg != NULL)
858 {
859 // Use eap->arg, split up in parts by line breaks.
860 theline = line_arg;
861 p = vim_strchr(theline, '\n');
862 if (p == NULL)
863 line_arg += STRLEN(line_arg);
864 else
865 {
866 *p = NUL;
867 line_arg = p + 1;
868 }
869 }
870 else
871 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000872 theline = get_function_line(eap, lines_to_free, indent,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100873 getline_options);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100874 }
875 if (KeyTyped)
876 lines_left = Rows - 1;
877 if (theline == NULL)
878 {
879 // Use the start of the function for the line number.
880 SOURCING_LNUM = sourcing_lnum_top;
881 if (skip_until != NULL)
882 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200883 else if (nesting_inline[nesting])
884 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100885 else if (eap->cmdidx == CMD_def)
886 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100887 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000888 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100889 goto theend;
890 }
891
892 // Detect line continuation: SOURCING_LNUM increased more than one.
893 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
894 if (SOURCING_LNUM < sourcing_lnum_off)
895 sourcing_lnum_off -= SOURCING_LNUM;
896 else
897 sourcing_lnum_off = 0;
898
899 if (skip_until != NULL)
900 {
901 // Don't check for ":endfunc"/":enddef" between
902 // * ":append" and "."
903 // * ":python <<EOF" and "EOF"
904 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
905 if (heredoc_trimmed == NULL
906 || (is_heredoc && skipwhite(theline) == theline)
907 || STRNCMP(theline, heredoc_trimmed,
908 STRLEN(heredoc_trimmed)) == 0)
909 {
910 if (heredoc_trimmed == NULL)
911 p = theline;
912 else if (is_heredoc)
913 p = skipwhite(theline) == theline
914 ? theline : theline + STRLEN(heredoc_trimmed);
915 else
916 p = theline + STRLEN(heredoc_trimmed);
917 if (STRCMP(p, skip_until) == 0)
918 {
919 VIM_CLEAR(skip_until);
920 VIM_CLEAR(heredoc_trimmed);
921 getline_options = vim9_function
922 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
923 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200924
925 if (heredoc_concat_len > 0)
926 {
927 // Replace the starting line with all the concatenated
928 // lines.
929 ga_concat(&heredoc_ga, theline);
930 vim_free(((char_u **)(newlines->ga_data))[
931 heredoc_concat_len - 1]);
932 ((char_u **)(newlines->ga_data))[
933 heredoc_concat_len - 1] = heredoc_ga.ga_data;
934 ga_init(&heredoc_ga);
935 heredoc_concat_len = 0;
936 theline += STRLEN(theline); // skip the "EOF"
937 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100938 }
939 }
940 }
941 else
942 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200943 int c;
944 char_u *end;
Bram Moolenaarb2175222022-03-05 20:24:41 +0000945 char_u *cmd;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100946
947 // skip ':' and blanks
948 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
949 ;
950
951 // Check for "endfunction", "enddef" or "}".
952 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaarb2175222022-03-05 20:24:41 +0000953 cmd = p;
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200954 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100955 ? *p == '}'
956 : (checkforcmd(&p, nesting_def[nesting]
957 ? "enddef" : "endfunction", 4)
958 && *p != ':'))
959 {
Bram Moolenaarb2175222022-03-05 20:24:41 +0000960 if (!nesting_inline[nesting] && nesting_def[nesting]
961 && p < cmd + 6)
962 semsg(_(e_command_cannot_be_shortened_str), "enddef");
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100963 if (nesting-- == 0)
964 {
965 char_u *nextcmd = NULL;
966
967 if (*p == '|' || *p == '}')
968 nextcmd = p + 1;
969 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
970 nextcmd = line_arg;
971 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100972 && (vim9_function || p_verbose > 0))
973 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200974 SOURCING_LNUM = sourcing_lnum_top
975 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100976 if (eap->cmdidx == CMD_def)
Bram Moolenaar7473a842021-12-28 17:55:26 +0000977 semsg(_(e_text_found_after_str_str), "enddef", p);
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100978 else
979 give_warning2((char_u *)
980 _("W22: Text found after :endfunction: %s"),
981 p, TRUE);
982 }
983 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100984 {
985 // Another command follows. If the line came from "eap"
986 // we can simply point into it, otherwise we need to
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000987 // change "eap->cmdlinep" to point to the last fetched
988 // line.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100989 eap->nextcmd = nextcmd;
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000990 if (lines_to_free->ga_len > 0
991 && *eap->cmdlinep !=
992 ((char_u **)lines_to_free->ga_data)
993 [lines_to_free->ga_len - 1])
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100994 {
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000995 // *cmdlinep will be freed later, thus remove the
996 // line from lines_to_free.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100997 vim_free(*eap->cmdlinep);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +0000998 *eap->cmdlinep = ((char_u **)lines_to_free->ga_data)
999 [lines_to_free->ga_len - 1];
1000 --lines_to_free->ga_len;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001001 }
1002 }
1003 break;
1004 }
1005 }
1006
1007 // Check for mismatched "endfunc" or "enddef".
1008 // We don't check for "def" inside "func" thus we also can't check
1009 // for "enddef".
1010 // We continue to find the end of the function, although we might
1011 // not find it.
1012 else if (nesting_def[nesting])
1013 {
1014 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
1015 emsg(_(e_mismatched_endfunction));
1016 }
1017 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
1018 emsg(_(e_mismatched_enddef));
1019
1020 // Increase indent inside "if", "while", "for" and "try", decrease
1021 // at "end".
1022 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
1023 indent -= 2;
1024 else if (STRNCMP(p, "if", 2) == 0
1025 || STRNCMP(p, "wh", 2) == 0
1026 || STRNCMP(p, "for", 3) == 0
1027 || STRNCMP(p, "try", 3) == 0)
1028 indent += 2;
1029
1030 // Check for defining a function inside this function.
1031 // Only recognize "def" inside "def", not inside "function",
1032 // For backwards compatibility, see Test_function_python().
1033 c = *p;
1034 if (is_function_cmd(&p)
1035 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
1036 {
1037 if (*p == '!')
1038 p = skipwhite(p + 1);
1039 p += eval_fname_script(p);
1040 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL,
1041 NULL, NULL));
1042 if (*skipwhite(p) == '(')
1043 {
1044 if (nesting == MAX_FUNC_NESTING - 1)
1045 emsg(_(e_function_nesting_too_deep));
1046 else
1047 {
1048 ++nesting;
1049 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001050 nesting_inline[nesting] = FALSE;
1051 indent += 2;
1052 }
1053 }
1054 }
1055
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001056 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001057 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001058 // Not a comment line: check for nested inline function.
1059 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001060 while (end > p && VIM_ISWHITE(*end))
1061 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +02001062 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001063 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001064 int is_block;
1065
1066 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001067 --end;
1068 while (end > p && VIM_ISWHITE(*end))
1069 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001070 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
1071 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +02001072 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +02001073 char_u *s = p;
1074
1075 // check for line starting with "au" for :autocmd or
1076 // "com" for :command, these can use a {} block
1077 is_block = checkforcmd_noparen(&s, "autocmd", 2)
1078 || checkforcmd_noparen(&s, "command", 3);
1079 }
1080
1081 if (is_block)
1082 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +02001083 if (nesting == MAX_FUNC_NESTING - 1)
1084 emsg(_(e_function_nesting_too_deep));
1085 else
1086 {
1087 ++nesting;
1088 nesting_def[nesting] = TRUE;
1089 nesting_inline[nesting] = TRUE;
1090 indent += 2;
1091 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001092 }
1093 }
1094 }
1095
1096 // Check for ":append", ":change", ":insert". Not for :def.
1097 p = skip_range(p, FALSE, NULL);
1098 if (!vim9_function
1099 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
1100 || (p[0] == 'c'
1101 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
1102 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
1103 && (STRNCMP(&p[3], "nge", 3) != 0
1104 || !ASCII_ISALPHA(p[6])))))))
1105 || (p[0] == 'i'
1106 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
1107 && (!ASCII_ISALPHA(p[2])
1108 || (p[2] == 's'
1109 && (!ASCII_ISALPHA(p[3])
1110 || p[3] == 'e'))))))))
1111 skip_until = vim_strsave((char_u *)".");
1112
1113 // Check for ":python <<EOF", ":tcl <<EOF", etc.
1114 arg = skipwhite(skiptowhite(p));
1115 if (arg[0] == '<' && arg[1] =='<'
1116 && ((p[0] == 'p' && p[1] == 'y'
1117 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
1118 || ((p[2] == '3' || p[2] == 'x')
1119 && !ASCII_ISALPHA(p[3]))))
1120 || (p[0] == 'p' && p[1] == 'e'
1121 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
1122 || (p[0] == 't' && p[1] == 'c'
1123 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
1124 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
1125 && !ASCII_ISALPHA(p[3]))
1126 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
1127 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
1128 || (p[0] == 'm' && p[1] == 'z'
1129 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
1130 ))
1131 {
1132 // ":python <<" continues until a dot, like ":append"
1133 p = skipwhite(arg + 2);
1134 if (STRNCMP(p, "trim", 4) == 0)
1135 {
1136 // Ignore leading white space.
1137 p = skipwhite(p + 4);
1138 heredoc_trimmed = vim_strnsave(theline,
1139 skipwhite(theline) - theline);
1140 }
1141 if (*p == NUL)
1142 skip_until = vim_strsave((char_u *)".");
1143 else
1144 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1145 getline_options = GETLINE_NONE;
1146 is_heredoc = TRUE;
Bram Moolenaard881d152022-05-13 13:50:36 +01001147 if (eap->cmdidx == CMD_def && nesting == 0)
Bram Moolenaar20677332021-06-06 17:02:53 +02001148 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001149 }
1150
Bram Moolenaard881d152022-05-13 13:50:36 +01001151 if (!is_heredoc)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001152 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001153 // Check for ":cmd v =<< [trim] EOF"
1154 // and ":cmd [a, b] =<< [trim] EOF"
1155 // and "lines =<< [trim] EOF" for Vim9
1156 // Where "cmd" can be "let", "var", "final" or "const".
1157 arg = skipwhite(skiptowhite(p));
1158 if (*arg == '[')
1159 arg = vim_strchr(arg, ']');
1160 if (arg != NULL)
1161 {
1162 int found = (eap->cmdidx == CMD_def && arg[0] == '='
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001163 && arg[1] == '<' && arg[2] =='<');
1164
Bram Moolenaard881d152022-05-13 13:50:36 +01001165 if (!found)
1166 // skip over the argument after "cmd"
1167 arg = skipwhite(skiptowhite(arg));
1168 if (found || (arg[0] == '=' && arg[1] == '<'
1169 && arg[2] =='<'
1170 && (checkforcmd(&p, "let", 2)
1171 || checkforcmd(&p, "var", 3)
1172 || checkforcmd(&p, "final", 5)
1173 || checkforcmd(&p, "const", 5))))
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001174 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001175 p = skipwhite(arg + 3);
1176 while (TRUE)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001177 {
Bram Moolenaard881d152022-05-13 13:50:36 +01001178 if (STRNCMP(p, "trim", 4) == 0)
1179 {
1180 // Ignore leading white space.
1181 p = skipwhite(p + 4);
1182 heredoc_trimmed = vim_strnsave(theline,
1183 skipwhite(theline) - theline);
1184 continue;
1185 }
1186 if (STRNCMP(p, "eval", 4) == 0)
1187 {
1188 // Ignore leading white space.
1189 p = skipwhite(p + 4);
1190 continue;
1191 }
1192 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +01001193 }
Bram Moolenaard881d152022-05-13 13:50:36 +01001194 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1195 getline_options = GETLINE_NONE;
1196 is_heredoc = TRUE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001197 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001198 }
1199 }
1200 }
1201
1202 // Add the line to the function.
Yegappan Lakshmanan7c7e19c2022-04-09 11:09:07 +01001203 if (ga_grow_id(newlines, 1 + sourcing_lnum_off, aid_get_func) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001204 goto theend;
1205
Bram Moolenaar20677332021-06-06 17:02:53 +02001206 if (heredoc_concat_len > 0)
1207 {
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001208 // For a :def function "python << EOF" concatenates all the lines,
Bram Moolenaar20677332021-06-06 17:02:53 +02001209 // to be used for the instruction later.
1210 ga_concat(&heredoc_ga, theline);
1211 ga_concat(&heredoc_ga, (char_u *)"\n");
1212 p = vim_strsave((char_u *)"");
1213 }
1214 else
1215 {
1216 // Copy the line to newly allocated memory. get_one_sourceline()
1217 // allocates 250 bytes per line, this saves 80% on average. The
1218 // cost is an extra alloc/free.
1219 p = vim_strsave(theline);
1220 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001221 if (p == NULL)
1222 goto theend;
1223 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1224
1225 // Add NULL lines for continuation lines, so that the line count is
1226 // equal to the index in the growarray.
1227 while (sourcing_lnum_off-- > 0)
1228 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1229
1230 // Check for end of eap->arg.
1231 if (line_arg != NULL && *line_arg == NUL)
1232 line_arg = NULL;
1233 }
1234
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001235 // Return OK when no error was detected.
1236 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001237 ret = OK;
1238
1239theend:
1240 vim_free(skip_until);
1241 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001242 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001243 need_wait_return |= saved_wait_return;
1244 return ret;
1245}
1246
1247/*
1248 * Handle the body of a lambda. *arg points to the "{", process statements
1249 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001250 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001251 * When successful "rettv" is set to a funcref.
1252 */
1253 static int
1254lambda_function_body(
1255 char_u **arg,
1256 typval_T *rettv,
1257 evalarg_T *evalarg,
1258 garray_T *newargs,
1259 garray_T *argtypes,
1260 int varargs,
1261 garray_T *default_args,
1262 char_u *ret_type)
1263{
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001264 char_u *start = *arg;
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001265 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001266 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001267 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001268 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001269 exarg_T eap;
1270 garray_T newlines;
1271 char_u *cmdline = NULL;
1272 int ret = FAIL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001273 partial_T *pt;
1274 char_u *name;
1275 int lnum_save = -1;
1276 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1277
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001278 *arg = skipwhite(*arg + 1);
1279 if (**arg == '|' || !ends_excmd2(start, *arg))
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001280 {
Bram Moolenaarf5f4e852022-09-22 22:03:14 +01001281 semsg(_(e_trailing_characters_str), *arg);
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001282 return FAIL;
1283 }
1284
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001285 CLEAR_FIELD(eap);
1286 eap.cmdidx = CMD_block;
1287 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001288 eap.cmdlinep = &cmdline;
1289 eap.skip = !evaluate;
1290 if (evalarg->eval_cctx != NULL)
1291 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1292 else
1293 {
1294 eap.getline = evalarg->eval_getline;
1295 eap.cookie = evalarg->eval_cookie;
1296 }
1297
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001298 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00001299 if (get_function_body(&eap, &newlines, NULL,
1300 &evalarg->eval_tofree_ga) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001301 goto erret;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001302
1303 // When inside a lambda must add the function lines to evalarg.eval_ga.
1304 evalarg->eval_break_count += newlines.ga_len;
1305 if (gap->ga_itemsize > 0)
1306 {
1307 int idx;
1308 char_u *last;
1309 size_t plen;
1310 char_u *pnl;
1311
1312 for (idx = 0; idx < newlines.ga_len; ++idx)
1313 {
1314 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1315
Bram Moolenaarecb66452021-05-18 15:09:18 +02001316 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001317 goto erret;
1318
1319 // Going to concatenate the lines after parsing. For an empty or
1320 // comment line use an empty string.
1321 // Insert NL characters at the start of each line, the string will
1322 // be split again later in .get_lambda_tv().
1323 if (*p == NUL || vim9_comment_start(p))
1324 p = (char_u *)"";
1325 plen = STRLEN(p);
1326 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1327 if (pnl != NULL)
1328 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001329 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1330 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001331 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001332 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001333 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001334 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001335 // more is following after the "}", which was skipped
1336 last = cmdline;
1337 else
1338 // nothing is following the "}"
1339 last = (char_u *)"}";
1340 plen = STRLEN(last);
1341 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1342 if (pnl != NULL)
1343 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001344 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1345 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001346 }
1347
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001348 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001349 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001350 garray_T *tfgap = &evalarg->eval_tofree_ga;
1351
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001352 // Something comes after the "}".
1353 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001354
1355 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001356 if (ga_grow(tfgap, 1) == OK)
1357 {
1358 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1359 evalarg->eval_using_cmdline = TRUE;
1360 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001361 }
1362 else
1363 *arg = (char_u *)"";
1364
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001365 if (!evaluate)
1366 {
1367 ret = OK;
1368 goto erret;
1369 }
1370
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001371 name = get_lambda_name();
Bram Moolenaare01e5212023-01-08 20:31:18 +00001372 ufunc = alloc_ufunc(name);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001373 if (ufunc == NULL)
1374 goto erret;
1375 set_ufunc_name(ufunc, name);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001376 if (hash_add(&func_hashtab, UF2HIKEY(ufunc), "add function") == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001377 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001378 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001379 ufunc->uf_refcount = 1;
1380 ufunc->uf_args = *newargs;
1381 newargs->ga_data = NULL;
1382 ufunc->uf_def_args = *default_args;
1383 default_args->ga_data = NULL;
1384 ufunc->uf_func_type = &t_func_any;
1385
1386 // error messages are for the first function line
1387 lnum_save = SOURCING_LNUM;
1388 SOURCING_LNUM = sourcing_lnum_top;
1389
1390 // parse argument types
1391 if (parse_argument_types(ufunc, argtypes, varargs) == FAIL)
1392 {
1393 SOURCING_LNUM = lnum_save;
1394 goto erret;
1395 }
1396
1397 // parse the return type, if any
1398 if (parse_return_type(ufunc, ret_type) == FAIL)
1399 goto erret;
1400
1401 pt = ALLOC_CLEAR_ONE(partial_T);
1402 if (pt == NULL)
1403 goto erret;
1404 pt->pt_func = ufunc;
1405 pt->pt_refcount = 1;
1406
1407 ufunc->uf_lines = newlines;
1408 newlines.ga_data = NULL;
1409 if (sandbox)
1410 ufunc->uf_flags |= FC_SANDBOX;
1411 if (!ASCII_ISUPPER(*ufunc->uf_name))
1412 ufunc->uf_flags |= FC_VIM9;
1413 ufunc->uf_script_ctx = current_sctx;
1414 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1415 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1416 set_function_type(ufunc);
1417
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001418 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1419
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001420 rettv->vval.v_partial = pt;
1421 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001422 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001423 ret = OK;
1424
1425erret:
1426 if (lnum_save >= 0)
1427 SOURCING_LNUM = lnum_save;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001428 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001429 if (newargs != NULL)
1430 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001431 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001432 if (ufunc != NULL)
1433 {
1434 func_clear(ufunc, TRUE);
1435 func_free(ufunc, TRUE);
1436 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001437 return ret;
1438}
1439
1440/*
1441 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001442 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001443 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001444 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1445 */
1446 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001447get_lambda_tv(
1448 char_u **arg,
1449 typval_T *rettv,
1450 int types_optional,
1451 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001452{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001453 int evaluate = evalarg != NULL
1454 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001455 garray_T newargs;
1456 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001457 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001458 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001459 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001460 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001461 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001462 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001463 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001464 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001465 char_u *s;
1466 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001467 int *old_eval_lavars = eval_lavars_used;
1468 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001469 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001470 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001471 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001472 int called_emsg_start = called_emsg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001473 int vim9script = in_vim9script();
Bram Moolenaar521bf322022-05-06 15:47:07 +01001474 long start_lnum = SOURCING_LNUM;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001475
Bram Moolenaar4525a572022-02-13 11:57:33 +00001476 if (equal_arrow && !vim9script)
Bram Moolenaar65c44152020-12-24 15:14:01 +01001477 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001478
1479 ga_init(&newargs);
1480 ga_init(&newlines);
1481
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001482 // First, check if this is really a lambda expression. "->" or "=>" must
1483 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001484 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001485 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001486 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001487 NULL, &default_args, TRUE, NULL, FALSE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001488 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001489 {
1490 if (types_optional)
1491 ga_clear_strings(&argtypes);
Bram Moolenaar0346b792021-01-31 22:18:29 +01001492 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001493 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001494
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001495 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001496 if (evaluate)
1497 pnewargs = &newargs;
1498 else
1499 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001500 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001501 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001502 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001503 &varargs, &default_args,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00001504 FALSE, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001505 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001506 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001507 equal_arrow || vim9script ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001508 {
1509 if (types_optional)
1510 ga_clear_strings(&argtypes);
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001511 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001512 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001513 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001514 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001515
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001516 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1517 if (ret_type != NULL)
1518 {
1519 ret_type = vim_strsave(ret_type);
1520 tofree2 = ret_type;
1521 }
1522
Bram Moolenaare38eab22019-12-05 21:50:01 +01001523 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001524 if (evaluate)
1525 eval_lavars_used = &eval_lavars;
1526
Bram Moolenaar65c44152020-12-24 15:14:01 +01001527 *arg = skipwhite_and_linebreak(*arg, evalarg);
1528
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001529 // Recognize "{" as the start of a function body.
1530 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001531 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001532 if (evalarg == NULL)
1533 // cannot happen?
1534 goto theend;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001535 SOURCING_LNUM = start_lnum; // used for where lambda is defined
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001536 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1537 types_optional ? &argtypes : NULL, varargs,
1538 &default_args, ret_type) == FAIL)
1539 goto errret;
1540 goto theend;
1541 }
1542 if (default_args.ga_len > 0)
1543 {
1544 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001545 goto errret;
1546 }
1547
Bram Moolenaare38eab22019-12-05 21:50:01 +01001548 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001549 start = *arg;
1550 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001551 if (ret == FAIL)
1552 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001553
Bram Moolenaar65c44152020-12-24 15:14:01 +01001554 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001555 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001556 *arg = skipwhite_and_linebreak(*arg, evalarg);
1557 if (**arg != '}')
1558 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001559 semsg(_(e_expected_right_curly_str), *arg);
Bram Moolenaar65c44152020-12-24 15:14:01 +01001560 goto errret;
1561 }
1562 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001563 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001564
1565 if (evaluate)
1566 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001567 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001568 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001569 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001570 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001571 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001572
Bram Moolenaare01e5212023-01-08 20:31:18 +00001573 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001574 if (fp == NULL)
1575 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001576 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001577 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001578 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001579 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001580
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001581 ga_init2(&newlines, sizeof(char_u *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001582 if (ga_grow(&newlines, 1) == FAIL)
1583 goto errret;
1584
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001585 // If there are line breaks, we need to split up the string.
1586 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001587 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001588 line_end = end;
1589
1590 // Add "return " before the expression (or the first line).
1591 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001592 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001593 if (p == NULL)
1594 goto errret;
1595 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1596 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001597 vim_strncpy(p + 7, start, line_end - start);
1598
1599 if (line_end != end)
1600 {
1601 // Add more lines, split by line breaks. Thus is used when a
1602 // lambda with { cmds } is encountered.
1603 while (*line_end == '\n')
1604 {
1605 if (ga_grow(&newlines, 1) == FAIL)
1606 goto errret;
1607 start = line_end + 1;
1608 line_end = vim_strchr(start, '\n');
1609 if (line_end == NULL)
1610 line_end = end;
1611 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1612 vim_strnsave(start, line_end - start);
1613 }
1614 }
1615
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001616 if (strstr((char *)p + 7, "a:") == NULL)
1617 // No a: variables are used for sure.
1618 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001619
1620 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001621 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001622 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001623 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001624 if (types_optional)
1625 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001626 if (parse_argument_types(fp, &argtypes,
Bram Moolenaar4525a572022-02-13 11:57:33 +00001627 vim9script && varargs) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001628 goto errret;
1629 if (ret_type != NULL)
1630 {
1631 fp->uf_ret_type = parse_type(&ret_type,
1632 &fp->uf_type_list, TRUE);
1633 if (fp->uf_ret_type == NULL)
1634 goto errret;
1635 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001636 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001637 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001638 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001639
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001640 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001641 if (current_funccal != NULL && eval_lavars)
1642 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001643 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001644 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001645 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001646 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001647
1648#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001649 if (prof_def_func())
1650 func_do_profile(fp);
1651#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001652 if (sandbox)
1653 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001654 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001655 // uf_args.ga_len. In Vim9 script "...name" has to be used.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001656 fp->uf_varargs = !vim9script || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001657 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001658 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001659 fp->uf_script_ctx = current_sctx;
Bram Moolenaar521bf322022-05-06 15:47:07 +01001660 // Use the line number of the arguments.
1661 fp->uf_script_ctx.sc_lnum += start_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001662
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001663 function_using_block_scopes(fp, evalarg->eval_cstack);
1664
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001665 pt->pt_func = fp;
1666 pt->pt_refcount = 1;
1667 rettv->vval.v_partial = pt;
1668 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001669
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001670 hash_add(&func_hashtab, UF2HIKEY(fp), "add lambda");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001671 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001672
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001673theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001674 eval_lavars_used = old_eval_lavars;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001675 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001676 if (types_optional)
1677 ga_clear_strings(&argtypes);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001678
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001679 return OK;
1680
1681errret:
1682 ga_clear_strings(&newargs);
1683 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001684 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001685 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001686 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001687 ga_clear_strings(&argtypes);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001688 if (fp != NULL)
1689 vim_free(fp->uf_arg_types);
1690 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001691 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001692 vim_free(pt);
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001693 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001694 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001695 return FAIL;
1696}
1697
1698/*
1699 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1700 * name it contains, otherwise return "name".
1701 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1702 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001703 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1704 * type of the variable.
Bram Moolenaar937610b2022-01-19 17:21:29 +00001705 * If "new_function" is TRUE the name is for a new function.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001706 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001707 */
1708 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001709deref_func_name(
1710 char_u *name,
1711 int *lenp,
1712 partial_T **partialp,
1713 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001714 int no_autoload,
Bram Moolenaar937610b2022-01-19 17:21:29 +00001715 int new_function,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001716 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001717{
1718 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001719 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001720 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001721 char_u *s = NULL;
1722 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001723 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001724
1725 if (partialp != NULL)
1726 *partialp = NULL;
1727
1728 cc = name[*lenp];
1729 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001730
Bram Moolenaar71f21932022-01-07 18:20:55 +00001731 v = find_var_also_in_script(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001732 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001733 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001734 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001735 tv = &v->di_tv;
1736 }
1737 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1738 {
1739 imported_T *import;
1740 char_u *p = name;
1741 int len = *lenp;
1742
1743 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001744 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001745 p = name + 2;
1746 len -= 2;
1747 }
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001748 import = find_imported(p, len, FALSE);
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001749
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001750 // imported function from another script
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001751 if (import != NULL)
1752 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001753 name[len] = NUL;
Bram Moolenaar937610b2022-01-19 17:21:29 +00001754 if (new_function)
1755 semsg(_(e_redefining_imported_item_str), name);
1756 else
1757 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001758 name[len] = cc;
1759 *lenp = 0;
1760 return (char_u *)""; // just in case
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001761 }
1762 }
1763
1764 if (tv != NULL)
1765 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001766 if (found_var != NULL)
1767 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001768 if (tv->v_type == VAR_FUNC)
1769 {
1770 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001771 {
1772 *lenp = 0;
1773 return (char_u *)""; // just in case
1774 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001775 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001776 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001777 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001778
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001779 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001780 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001781 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001782
1783 if (pt == NULL)
1784 {
1785 *lenp = 0;
1786 return (char_u *)""; // just in case
1787 }
1788 if (partialp != NULL)
1789 *partialp = pt;
1790 s = partial_name(pt);
1791 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001792 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001793
1794 if (s != NULL)
1795 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001796 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001797 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001798 svar_T *sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001799
1800 if (sv != NULL)
1801 *type = sv->sv_type;
1802 }
1803 return s;
1804 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001805 }
1806
1807 return name;
1808}
1809
1810/*
1811 * Give an error message with a function name. Handle <SNR> things.
1812 * "ermsg" is to be passed without translation, use N_() instead of _().
1813 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001814 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001815emsg_funcname(char *ermsg, char_u *name)
1816{
Bram Moolenaar54598062022-01-13 12:05:09 +00001817 char_u *p = name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001818
Bram Moolenaar54598062022-01-13 12:05:09 +00001819 if (name[0] == K_SPECIAL && name[1] != NUL && name[2] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001820 p = concat_str((char_u *)"<SNR>", name + 3);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001821 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001822 if (p != name)
1823 vim_free(p);
1824}
1825
1826/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001827 * Get function arguments at "*arg" and advance it.
1828 * Return them in "*argvars[MAX_FUNC_ARGS + 1]" and the count in "argcount".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001829 * On failure FAIL is returned but the "argvars[argcount]" are still set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001830 */
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001831 int
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001832get_func_arguments(
1833 char_u **arg,
1834 evalarg_T *evalarg,
1835 int partial_argc,
1836 typval_T *argvars,
1837 int *argcount)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001838{
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001839 char_u *argp = *arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001840 int ret = OK;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001841 int vim9script = in_vim9script();
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001842 int evaluate = evalarg == NULL
1843 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001844
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001845 while (*argcount < MAX_FUNC_ARGS - partial_argc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001846 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001847 // skip the '(' or ',' and possibly line breaks
1848 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1849
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001850 if (*argp == ')' || *argp == ',' || *argp == NUL)
1851 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001852 if (eval1(&argp, &argvars[*argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001853 {
1854 ret = FAIL;
1855 break;
1856 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001857 ++*argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001858 // The comma should come right after the argument, but this wasn't
1859 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001860 if (vim9script)
1861 {
1862 if (*argp != ',' && *skipwhite(argp) == ',')
1863 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001864 if (evaluate)
1865 semsg(_(e_no_white_space_allowed_before_str_str),
1866 ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001867 ret = FAIL;
1868 break;
1869 }
1870 }
1871 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001872 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001873 if (*argp != ',')
1874 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001875 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
1876 {
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001877 if (evaluate)
1878 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001879 ret = FAIL;
1880 break;
1881 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001882 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001883
Bram Moolenaare6b53242020-07-01 17:28:33 +02001884 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001885 if (*argp == ')')
1886 ++argp;
1887 else
1888 ret = FAIL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001889 *arg = argp;
1890 return ret;
1891}
1892
1893/*
1894 * Call a function and put the result in "rettv".
1895 * Return OK or FAIL.
1896 */
1897 int
1898get_func_tv(
1899 char_u *name, // name of the function
1900 int len, // length of "name" or -1 to use strlen()
1901 typval_T *rettv,
1902 char_u **arg, // argument, pointing to the '('
1903 evalarg_T *evalarg, // for line continuation
1904 funcexe_T *funcexe) // various values
1905{
1906 char_u *argp;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001907 int ret;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001908 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1909 int argcount = 0; // number of arguments found
1910 int vim9script = in_vim9script();
1911 int evaluate = evalarg == NULL
1912 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
1913
1914 argp = *arg;
1915 ret = get_func_arguments(&argp, evalarg,
1916 (funcexe->fe_partial == NULL ? 0 : funcexe->fe_partial->pt_argc),
1917 argvars, &argcount);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001918
1919 if (ret == OK)
1920 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001921 int i = 0;
1922 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001923
1924 if (get_vim_var_nr(VV_TESTING))
1925 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001926 // Prepare for calling test_garbagecollect_now(), need to know
1927 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001928 if (funcargs.ga_itemsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +00001929 ga_init2(&funcargs, sizeof(typval_T *), 50);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001930 for (i = 0; i < argcount; ++i)
1931 if (ga_grow(&funcargs, 1) == OK)
1932 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1933 &argvars[i];
1934 }
1935
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001936 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar4525a572022-02-13 11:57:33 +00001937 if (vim9script && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001938 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001939 // An error in a builtin function does not return FAIL, but we do
1940 // want to abort further processing if an error was given.
1941 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001942 clear_tv(rettv);
1943 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001944
1945 funcargs.ga_len -= i;
1946 }
Bram Moolenaar5e6b9882022-01-10 18:50:52 +00001947 else if (!aborting() && evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001948 {
1949 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001950 emsg_funcname(e_too_many_arguments_for_function_str_2, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001951 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001952 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001953 }
1954
1955 while (--argcount >= 0)
1956 clear_tv(&argvars[argcount]);
1957
Bram Moolenaar4525a572022-02-13 11:57:33 +00001958 if (vim9script)
Bram Moolenaar8294d492020-08-10 22:40:56 +02001959 *arg = argp;
1960 else
1961 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001962 return ret;
1963}
1964
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001965/*
1966 * Return TRUE if "p" starts with "<SID>" or "s:".
1967 * Only works if eval_fname_script() returned non-zero for "p"!
1968 */
1969 static int
1970eval_fname_sid(char_u *p)
1971{
1972 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
1973}
1974
1975/*
1976 * In a script change <SID>name() and s:name() to K_SNR 123_name().
1977 * Change <SNR>123_name() to K_SNR 123_name().
1978 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001979 * and set "tofree".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001980 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001981 char_u *
Bram Moolenaar0917e862023-02-18 14:42:44 +00001982fname_trans_sid(
1983 char_u *name,
1984 char_u *fname_buf,
1985 char_u **tofree,
1986 funcerror_T *error)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001987{
1988 int llen;
1989 char_u *fname;
1990 int i;
1991
1992 llen = eval_fname_script(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001993 if (llen == 0)
1994 return name; // no prefix
1995
1996 fname_buf[0] = K_SPECIAL;
1997 fname_buf[1] = KS_EXTRA;
1998 fname_buf[2] = (int)KE_SNR;
1999 i = 3;
2000 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002001 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002002 if (current_sctx.sc_sid <= 0)
2003 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002004 else
2005 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002006 sprintf((char *)fname_buf + 3, "%ld_",
2007 (long)current_sctx.sc_sid);
2008 i = (int)STRLEN(fname_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002009 }
2010 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002011 if (i + STRLEN(name + llen) < FLEN_FIXED)
2012 {
2013 STRCPY(fname_buf + i, name + llen);
2014 fname = fname_buf;
2015 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002016 else
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01002017 {
2018 fname = alloc(i + STRLEN(name + llen) + 1);
2019 if (fname == NULL)
2020 *error = FCERR_OTHER;
2021 else
2022 {
2023 *tofree = fname;
2024 mch_memmove(fname, fname_buf, (size_t)i);
2025 STRCPY(fname + i, name + llen);
2026 }
2027 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002028 return fname;
2029}
2030
2031/*
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002032 * Concatenate the script ID and function name into "<SNR>99_name".
2033 * "buffer" must have size MAX_FUNC_NAME_LEN.
2034 */
2035 void
2036func_name_with_sid(char_u *name, int sid, char_u *buffer)
2037{
2038 // A script-local function is stored as "<SNR>99_name".
2039 buffer[0] = K_SPECIAL;
2040 buffer[1] = KS_EXTRA;
2041 buffer[2] = (int)KE_SNR;
2042 vim_snprintf((char *)buffer + 3, MAX_FUNC_NAME_LEN - 3, "%ld_%s",
2043 (long)sid, name);
2044}
2045
2046/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002047 * Find a function "name" in script "sid".
2048 */
2049 static ufunc_T *
2050find_func_with_sid(char_u *name, int sid)
2051{
Bram Moolenaarb8822442022-01-11 15:24:05 +00002052 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002053 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002054
Bram Moolenaarb8822442022-01-11 15:24:05 +00002055 if (!SCRIPT_ID_VALID(sid))
2056 return NULL; // not in a script
2057
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002058 func_name_with_sid(name, sid, buffer);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002059 hi = hash_find(&func_hashtab, buffer);
2060 if (!HASHITEM_EMPTY(hi))
2061 return HI2UF(hi);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002062 return NULL;
2063}
2064
2065/*
2066 * Find a function "name" in script "sid" prefixing the autoload prefix.
2067 */
2068 static ufunc_T *
2069find_func_with_prefix(char_u *name, int sid)
2070{
2071 hashitem_T *hi;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002072 char_u buffer[MAX_FUNC_NAME_LEN];
Bram Moolenaarb8822442022-01-11 15:24:05 +00002073 scriptitem_T *si;
2074
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002075 if (vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaarb8822442022-01-11 15:24:05 +00002076 return NULL; // already has the prefix
2077 if (!SCRIPT_ID_VALID(sid))
2078 return NULL; // not in a script
2079 si = SCRIPT_ITEM(sid);
2080 if (si->sn_autoload_prefix != NULL)
2081 {
2082 size_t len = STRLEN(si->sn_autoload_prefix) + STRLEN(name) + 1;
2083 char_u *auto_name;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002084 char_u *namep;
2085
2086 // skip a "<SNR>99_" prefix
2087 namep = untrans_function_name(name);
2088 if (namep == NULL)
2089 namep = name;
Bram Moolenaarb8822442022-01-11 15:24:05 +00002090
2091 // An exported function in an autoload script is stored as
2092 // "dir#path#name".
2093 if (len < sizeof(buffer))
2094 auto_name = buffer;
2095 else
2096 auto_name = alloc(len);
2097 if (auto_name != NULL)
2098 {
2099 vim_snprintf((char *)auto_name, len, "%s%s",
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00002100 si->sn_autoload_prefix, namep);
Bram Moolenaarb8822442022-01-11 15:24:05 +00002101 hi = hash_find(&func_hashtab, auto_name);
2102 if (auto_name != buffer)
2103 vim_free(auto_name);
2104 if (!HASHITEM_EMPTY(hi))
2105 return HI2UF(hi);
2106 }
2107 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108
2109 return NULL;
2110}
2111
2112/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002113 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002114 * When "flags" has FFED_IS_GLOBAL don't find script-local or imported
2115 * functions.
2116 * When "flags" has "FFED_NO_GLOBAL" don't find global functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002117 * Return NULL for unknown function.
2118 */
Bram Moolenaarce658352020-07-31 23:47:12 +02002119 ufunc_T *
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002120find_func_even_dead(char_u *name, int flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002121{
2122 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123 ufunc_T *func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002125 if ((flags & FFED_IS_GLOBAL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002126 {
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002127 // Find script-local function before global one.
2128 if (in_vim9script() && eval_isnamec1(*name)
2129 && (name[1] != ':' || *name == 's'))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002130 {
Bram Moolenaar33b968d2021-12-13 11:31:04 +00002131 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
2132 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002133 if (func != NULL)
2134 return func;
2135 }
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00002136 if (in_vim9script() && STRNCMP(name, "<SNR>", 5) == 0)
2137 {
2138 char_u *p = name + 5;
2139 long sid;
2140
2141 // printable "<SNR>123_Name" form
2142 sid = getdigits(&p);
2143 if (*p == '_')
2144 {
2145 func = find_func_with_sid(p + 1, (int)sid);
2146 if (func != NULL)
2147 return func;
2148 }
2149 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002150 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002151
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002152 if ((flags & FFED_NO_GLOBAL) == 0)
2153 {
2154 hi = hash_find(&func_hashtab,
Bram Moolenaara26b9702020-04-18 19:53:28 +02002155 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002156 if (!HASHITEM_EMPTY(hi))
2157 return HI2UF(hi);
2158 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159
Bram Moolenaarb8822442022-01-11 15:24:05 +00002160 // Find autoload function if this is an autoload script.
2161 return find_func_with_prefix(name[0] == 's' && name[1] == ':'
2162 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163}
2164
2165/*
2166 * Find a function by name, return pointer to it in ufuncs.
2167 * "cctx" is passed in a :def function to find imported functions.
2168 * Return NULL for unknown or dead function.
2169 */
2170 ufunc_T *
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002171find_func(char_u *name, int is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002172{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002173 ufunc_T *fp = find_func_even_dead(name, is_global ? FFED_IS_GLOBAL : 0);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002174
2175 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
2176 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002177 return NULL;
2178}
2179
2180/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02002181 * Return TRUE if "ufunc" is a global function.
2182 */
2183 int
2184func_is_global(ufunc_T *ufunc)
2185{
2186 return ufunc->uf_name[0] != K_SPECIAL;
2187}
2188
2189/*
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002190 * Return TRUE if "ufunc" must be called with a g: prefix in Vim9 script.
2191 */
2192 int
2193func_requires_g_prefix(ufunc_T *ufunc)
2194{
2195 return ufunc->uf_name[0] != K_SPECIAL
2196 && (ufunc->uf_flags & FC_LAMBDA) == 0
Bram Moolenaarcfb4d4f2022-09-30 19:19:04 +01002197 && vim_strchr(ufunc->uf_name, AUTOLOAD_CHAR) == NULL
2198 && !isdigit(ufunc->uf_name[0]);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002199}
2200
2201/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002202 * Copy the function name of "fp" to buffer "buf".
2203 * "buf" must be able to hold the function name plus three bytes.
2204 * Takes care of script-local function names.
2205 */
2206 static void
2207cat_func_name(char_u *buf, ufunc_T *fp)
2208{
Bram Moolenaar0f769812020-09-12 18:32:34 +02002209 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002210 {
2211 STRCPY(buf, "<SNR>");
2212 STRCAT(buf, fp->uf_name + 3);
2213 }
2214 else
2215 STRCPY(buf, fp->uf_name);
2216}
2217
2218/*
2219 * Add a number variable "name" to dict "dp" with value "nr".
2220 */
2221 static void
2222add_nr_var(
2223 dict_T *dp,
2224 dictitem_T *v,
2225 char *name,
2226 varnumber_T nr)
2227{
2228 STRCPY(v->di_key, name);
2229 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002230 hash_add(&dp->dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002231 v->di_tv.v_type = VAR_NUMBER;
2232 v->di_tv.v_lock = VAR_FIXED;
2233 v->di_tv.vval.v_number = nr;
2234}
2235
2236/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002237 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002238 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002239 static void
2240free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002241{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002242 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002243
Bram Moolenaarca16c602022-09-06 18:57:08 +01002244 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002245 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002246 ufunc_T *fp = ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002247
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002248 // When garbage collecting a funccall_T may be freed before the
2249 // function that references it, clear its uf_scoped field.
2250 // The function may have been redefined and point to another
2251 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002252 if (fp != NULL && fp->uf_scoped == fc)
2253 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002254 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002255 ga_clear(&fc->fc_ufuncs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002256
Bram Moolenaarca16c602022-09-06 18:57:08 +01002257 func_ptr_unref(fc->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002258 vim_free(fc);
2259}
2260
2261/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002262 * Free "fc" and what it contains.
2263 * Can be called only when "fc" is kept beyond the period of it called,
2264 * i.e. after cleanup_function_call(fc).
2265 */
2266 static void
2267free_funccal_contents(funccall_T *fc)
2268{
2269 listitem_T *li;
2270
2271 // Free all l: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002272 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002273
2274 // Free all a: variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002275 vars_clear(&fc->fc_l_avars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002276
2277 // Free the a:000 variables.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002278 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002279 clear_tv(&li->li_tv);
2280
2281 free_funccal(fc);
2282}
2283
2284/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002285 * Handle the last part of returning from a function: free the local hashtable.
2286 * Unless it is still in use by a closure.
2287 */
2288 static void
2289cleanup_function_call(funccall_T *fc)
2290{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002291 int may_free_fc = fc->fc_refcount <= 0;
2292 int free_fc = TRUE;
2293
Bram Moolenaarca16c602022-09-06 18:57:08 +01002294 current_funccal = fc->fc_caller;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002295
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002296 // Free all l: variables if not referred.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002297 if (may_free_fc && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT)
2298 vars_clear(&fc->fc_l_vars.dv_hashtab);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002299 else
2300 free_fc = FALSE;
2301
2302 // If the a:000 list and the l: and a: dicts are not referenced and
2303 // there is no closure using it, we can free the funccall_T and what's
2304 // in it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002305 if (may_free_fc && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)
2306 vars_clear_ext(&fc->fc_l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002307 else
2308 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002309 int todo;
2310 hashitem_T *hi;
2311 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002312
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002313 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002314
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002315 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002316 todo = (int)fc->fc_l_avars.dv_hashtab.ht_used;
2317 for (hi = fc->fc_l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002318 {
2319 if (!HASHITEM_EMPTY(hi))
2320 {
2321 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002322 di = HI2DI(hi);
2323 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002324 }
2325 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002326 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002327
Bram Moolenaarca16c602022-09-06 18:57:08 +01002328 if (may_free_fc && fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2329 fc->fc_l_varlist.lv_first = NULL;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002330 else
2331 {
2332 listitem_T *li;
2333
2334 free_fc = FALSE;
2335
2336 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002337 FOR_ALL_LIST_ITEMS(&fc->fc_l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002338 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002339 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002340
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002341 if (free_fc)
2342 free_funccal(fc);
2343 else
2344 {
2345 static int made_copy = 0;
2346
2347 // "fc" is still in use. This can happen when returning "a:000",
2348 // assigning "l:" to a global variable or defining a closure.
2349 // Link "fc" in the list for garbage collection later.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002350 fc->fc_caller = previous_funccal;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002351 previous_funccal = fc;
2352
2353 if (want_garbage_collect)
2354 // If garbage collector is ready, clear count.
2355 made_copy = 0;
2356 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002357 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002358 // We have made a lot of copies, worth 4 Mbyte. This can happen
2359 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002360 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002361 // too much memory.
2362 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002363 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002364 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002365 }
2366}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002367
2368/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002369 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2370 */
2371 static int
2372numbered_function(char_u *name)
2373{
2374 return isdigit(*name)
2375 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2376}
2377
2378/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002379 * There are two kinds of function names:
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002380 * 1. ordinary names, function defined with :function or :def;
2381 * can start with "<SNR>123_" literally or with K_SPECIAL.
2382 * 2. Numbered functions and lambdas: "<lambda>123"
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002383 * For the first we only count the name stored in func_hashtab as a reference,
2384 * using function() does not count as a reference, because the function is
2385 * looked up by name.
2386 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002387 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002388func_name_refcount(char_u *name)
2389{
Bram Moolenaarfb43cfc2022-03-11 18:54:17 +00002390 return numbered_function(name) || (name[0] == '<' && name[1] == 'l');
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002391}
2392
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393/*
2394 * Unreference "fc": decrement the reference count and free it when it
2395 * becomes zero. "fp" is detached from "fc".
2396 * When "force" is TRUE we are exiting.
2397 */
2398 static void
2399funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2400{
2401 funccall_T **pfc;
2402 int i;
2403
2404 if (fc == NULL)
2405 return;
2406
2407 if (--fc->fc_refcount <= 0 && (force || (
Bram Moolenaarca16c602022-09-06 18:57:08 +01002408 fc->fc_l_varlist.lv_refcount == DO_NOT_FREE_CNT
2409 && fc->fc_l_vars.dv_refcount == DO_NOT_FREE_CNT
2410 && fc->fc_l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2411 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->fc_caller)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 {
2413 if (fc == *pfc)
2414 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002415 *pfc = fc->fc_caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 free_funccal_contents(fc);
2417 return;
2418 }
2419 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002420 for (i = 0; i < fc->fc_ufuncs.ga_len; ++i)
2421 if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp)
2422 ((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423}
2424
2425/*
2426 * Remove the function from the function hashtable. If the function was
2427 * deleted while it still has references this was already done.
2428 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2429 */
2430 static int
2431func_remove(ufunc_T *fp)
2432{
2433 hashitem_T *hi;
2434
2435 // Return if it was already virtually deleted.
2436 if (fp->uf_flags & FC_DEAD)
2437 return FALSE;
2438
2439 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002440 if (HASHITEM_EMPTY(hi))
2441 return FALSE;
2442
2443 // When there is a def-function index do not actually remove the
2444 // function, so we can find the index when defining the function again.
2445 // Do remove it when it's a copy.
2446 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002448 fp->uf_flags |= FC_DEAD;
2449 return FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00002451 hash_remove(&func_hashtab, hi, "remove function");
2452 fp->uf_flags |= FC_DELETED;
2453 return TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454}
2455
2456 static void
2457func_clear_items(ufunc_T *fp)
2458{
2459 ga_clear_strings(&(fp->uf_args));
2460 ga_clear_strings(&(fp->uf_def_args));
2461 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002462 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002463 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002464 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002465 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002466
2467 // Increment the refcount of this function to avoid it being freed
2468 // recursively when the partial is freed.
2469 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002470 partial_unref(fp->uf_partial);
2471 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002472 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002473
2474#ifdef FEAT_LUA
2475 if (fp->uf_cb_free != NULL)
2476 {
2477 fp->uf_cb_free(fp->uf_cb_state);
2478 fp->uf_cb_free = NULL;
2479 }
2480
2481 fp->uf_cb_state = NULL;
2482 fp->uf_cb = NULL;
2483#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484#ifdef FEAT_PROFILE
2485 VIM_CLEAR(fp->uf_tml_count);
2486 VIM_CLEAR(fp->uf_tml_total);
2487 VIM_CLEAR(fp->uf_tml_self);
2488#endif
2489}
2490
2491/*
2492 * Free all things that a function contains. Does not free the function
2493 * itself, use func_free() for that.
2494 * When "force" is TRUE we are exiting.
2495 */
2496 static void
2497func_clear(ufunc_T *fp, int force)
2498{
2499 if (fp->uf_cleared)
2500 return;
2501 fp->uf_cleared = TRUE;
2502
2503 // clear this function
2504 func_clear_items(fp);
2505 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002506 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507}
2508
2509/*
2510 * Free a function and remove it from the list of functions. Does not free
2511 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002512 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002513 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002515 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002516func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517{
2518 // Only remove it when not done already, otherwise we would remove a newer
2519 // version of the function with the same name.
2520 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2521 func_remove(fp);
2522
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002523 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002524 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002525 if (fp->uf_dfunc_idx > 0)
2526 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002527 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002529 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002530 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002531 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532}
2533
2534/*
2535 * Free all things that a function contains and free the function itself.
2536 * When "force" is TRUE we are exiting.
2537 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002538 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539func_clear_free(ufunc_T *fp, int force)
2540{
2541 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002542 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2543 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002544 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002545 else
2546 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547}
2548
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002549/*
2550 * Copy already defined function "lambda" to a new function with name "global".
2551 * This is for when a compiled function defines a global function.
2552 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002553 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002554copy_lambda_to_global_func(
Bram Moolenaarcc341812022-09-19 15:54:34 +01002555 char_u *lambda,
2556 char_u *global,
2557 loopvarinfo_T *loopvarinfo,
2558 ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002559{
Bram Moolenaaracc4b562022-01-24 13:54:45 +00002560 ufunc_T *ufunc = find_func_even_dead(lambda, FFED_IS_GLOBAL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002561 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002562
2563 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002564 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002565 semsg(_(e_lambda_function_not_found_str), lambda);
2566 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002567 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002568
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002569 fp = find_func(global, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002570 if (fp != NULL)
2571 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002572 // TODO: handle ! to overwrite
Bram Moolenaar1a992222021-12-31 17:25:48 +00002573 semsg(_(e_function_str_already_exists_add_bang_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002574 return FAIL;
2575 }
2576
Bram Moolenaare01e5212023-01-08 20:31:18 +00002577 fp = alloc_ufunc(global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002578 if (fp == NULL)
2579 return FAIL;
2580
2581 fp->uf_varargs = ufunc->uf_varargs;
2582 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2583 fp->uf_def_status = ufunc->uf_def_status;
2584 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2585 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2586 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2587 == FAIL
2588 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2589 goto failed;
2590
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002591 if (ufunc->uf_arg_types != NULL)
2592 {
2593 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2594 if (fp->uf_arg_types == NULL)
2595 goto failed;
2596 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2597 sizeof(type_T *) * fp->uf_args.ga_len);
2598 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002599 if (ufunc->uf_va_name != NULL)
2600 {
2601 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2602 if (fp->uf_va_name == NULL)
2603 goto failed;
2604 }
2605 fp->uf_ret_type = ufunc->uf_ret_type;
2606
2607 fp->uf_refcount = 1;
Bram Moolenaarc8ab30a2023-01-09 11:35:47 +00002608
2609 fp->uf_name_exp = NULL;
2610 set_ufunc_name(fp, global);
2611
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002612 hash_add(&func_hashtab, UF2HIKEY(fp), "copy lambda");
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002613
2614 // the referenced dfunc_T is now used one more time
2615 link_def_function(fp);
2616
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002617 // Create a partial to store the context of the function where it was
2618 // instantiated. Only needs to be done once. Do this on the original
2619 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002620 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2621 {
2622 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2623
2624 if (pt == NULL)
2625 goto failed;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002626 if (fill_partial_and_closure(pt, ufunc, loopvarinfo, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002627 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002628 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002629 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002630 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002631 ufunc->uf_partial = pt;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002632 }
2633
2634 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002635
2636failed:
2637 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002638 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002639}
2640
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002641static int funcdepth = 0;
2642
2643/*
2644 * Increment the function call depth count.
2645 * Return FAIL when going over 'maxfuncdepth'.
2646 * Otherwise return OK, must call funcdepth_decrement() later!
2647 */
2648 int
2649funcdepth_increment(void)
2650{
2651 if (funcdepth >= p_mfd)
2652 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002653 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002654 return FAIL;
2655 }
2656 ++funcdepth;
2657 return OK;
2658}
2659
2660 void
2661funcdepth_decrement(void)
2662{
2663 --funcdepth;
2664}
2665
2666/*
2667 * Get the current function call depth.
2668 */
2669 int
2670funcdepth_get(void)
2671{
2672 return funcdepth;
2673}
2674
2675/*
2676 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002677 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002678 * times as funcdepth_increment().
2679 */
2680 void
2681funcdepth_restore(int depth)
2682{
2683 funcdepth = depth;
2684}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002685
2686/*
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002687 * Allocate a funccall_T, link it in current_funccal and fill in "fp" and
2688 * "rettv".
2689 * Must be followed by one call to remove_funccal() or cleanup_function_call().
2690 * Returns NULL when allocation fails.
2691 */
2692 funccall_T *
2693create_funccal(ufunc_T *fp, typval_T *rettv)
2694{
2695 funccall_T *fc = ALLOC_CLEAR_ONE(funccall_T);
2696
2697 if (fc == NULL)
2698 return NULL;
2699 fc->fc_caller = current_funccal;
2700 current_funccal = fc;
2701 fc->fc_func = fp;
2702 func_ptr_ref(fp);
2703 fc->fc_rettv = rettv;
2704 return fc;
2705}
2706
2707/*
2708 * To be called when returning from a compiled function; restores
2709 * current_funccal.
2710 */
2711 void
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00002712remove_funccal(void)
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002713{
2714 funccall_T *fc = current_funccal;
2715
2716 current_funccal = fc->fc_caller;
2717 free_funccal(fc);
2718}
2719
2720/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002721 * Call a user function.
2722 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00002723 static funcerror_T
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002724call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002725 ufunc_T *fp, // pointer to function
2726 int argcount, // nr of args
2727 typval_T *argvars, // arguments
2728 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002729 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002730 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002731{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002732 sctx_T save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01002733 ectx_T *save_current_ectx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002734 int using_sandbox = FALSE;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002735 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002736 funccall_T *fc;
2737 int save_did_emsg;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002738 funcerror_T retval = FCERR_NONE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002739 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002740 dictitem_T *v;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002741 int fixvar_idx = 0; // index in fc_fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002742 int i;
2743 int ai;
2744 int islambda = FALSE;
2745 char_u numbuf[NUMBUFLEN];
2746 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002747 typval_T *tv_to_free[MAX_FUNC_ARGS];
2748 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002749#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002750 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002751#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01002752 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002753
Bram Moolenaarb2049902021-01-24 12:53:53 +01002754#ifdef FEAT_PROFILE
2755 CLEAR_FIELD(profile_info);
2756#endif
2757
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002758 // If depth of calling is getting too high, don't execute the function.
2759 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002760 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002761 rettv->v_type = VAR_NUMBER;
2762 rettv->vval.v_number = -1;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002763 return FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002764 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002765
Bram Moolenaare38eab22019-12-05 21:50:01 +01002766 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002767
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002768 fc = create_funccal(fp, rettv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002769 if (fc == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00002770 return FCERR_OTHER;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002771 fc->fc_level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002772 // Check if this function has a breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002773 fc->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2774 fc->fc_dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002775 // Set up fields for closure.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002776 ga_init2(&fc->fc_ufuncs, sizeof(ufunc_T *), 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002777
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002778 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002779 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002780#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01002781 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01002782#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002783 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002784#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002785 if (do_profiling == PROF_YES)
2786 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002787#endif
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002788 sticky_cmdmod_flags = 0;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002789 if (call_def_function(fp, argcount, argvars, 0,
2790 funcexe->fe_partial, funcexe->fe_object, fc, rettv) == FAIL)
2791 retval = FCERR_FAILED;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002792 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002793#ifdef FEAT_PROFILE
2794 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002795 || (caller != NULL && caller->uf_profiling)))
2796 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002797#endif
Bram Moolenaar9667b2c2022-09-07 17:28:09 +01002798 remove_funccal();
Bram Moolenaarcdf04852022-02-12 22:13:06 +00002799 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaar0917e862023-02-18 14:42:44 +00002800 return retval;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 }
2802
Bram Moolenaar38453522021-11-28 22:00:12 +00002803 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002804
2805 /*
Bram Moolenaar98aff652022-09-06 21:02:35 +01002806 * Note about using fc->fc_fixvar[]: This is an array of FIXVAR_CNT
2807 * variables with names up to VAR_SHORT_LEN long. This avoids having to
2808 * alloc/free each argument variable and saves a lot of time.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002809 */
2810 /*
2811 * Init l: variables.
2812 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002813 init_var_dict(&fc->fc_l_vars, &fc->fc_l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002814 if (selfdict != NULL)
2815 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002816 // Set l:self to "selfdict". Use "name" to avoid a warning from
2817 // some compiler that checks the destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002818 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002819 name = v->di_key;
2820 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002821 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002822 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "set self dictionary");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002823 v->di_tv.v_type = VAR_DICT;
2824 v->di_tv.v_lock = 0;
2825 v->di_tv.vval.v_dict = selfdict;
2826 ++selfdict->dv_refcount;
2827 }
2828
2829 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002830 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002831 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002832 * Set a:000 to a list with room for the "..." arguments.
2833 */
Bram Moolenaarca16c602022-09-06 18:57:08 +01002834 init_var_dict(&fc->fc_l_avars, &fc->fc_l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002835 if ((fp->uf_flags & FC_NOARGS) == 0)
Bram Moolenaarca16c602022-09-06 18:57:08 +01002836 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002837 (varnumber_T)(argcount >= fp->uf_args.ga_len
2838 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaarca16c602022-09-06 18:57:08 +01002839 fc->fc_l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002840 if ((fp->uf_flags & FC_NOARGS) == 0)
2841 {
2842 // Use "name" to avoid a warning from some compiler that checks the
2843 // destination size.
Bram Moolenaarca16c602022-09-06 18:57:08 +01002844 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002845 name = v->di_key;
2846 STRCPY(name, "000");
2847 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002848 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "function argument");
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002849 v->di_tv.v_type = VAR_LIST;
2850 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002851 v->di_tv.vval.v_list = &fc->fc_l_varlist;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002852 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01002853 CLEAR_FIELD(fc->fc_l_varlist);
2854 fc->fc_l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2855 fc->fc_l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002856
2857 /*
2858 * Set a:firstline to "firstline" and a:lastline to "lastline".
2859 * Set a:name to named arguments.
2860 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002861 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002862 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002863 if ((fp->uf_flags & FC_NOARGS) == 0)
2864 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002865 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2866 "firstline", (varnumber_T)funcexe->fe_firstline);
2867 add_nr_var(&fc->fc_l_avars, &fc->fc_fixvar[fixvar_idx++].var,
2868 "lastline", (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002869 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002870 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002871 {
2872 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002873 typval_T def_rettv;
2874 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002875
2876 ai = i - fp->uf_args.ga_len;
2877 if (ai < 0)
2878 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002879 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002880 name = FUNCARG(fp, i);
2881 if (islambda)
2882 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002883
2884 // evaluate named argument default expression
2885 isdefault = ai + fp->uf_def_args.ga_len >= 0
2886 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2887 && argvars[i].vval.v_number == VVAL_NONE));
2888 if (isdefault)
2889 {
2890 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002891
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002892 def_rettv.v_type = VAR_NUMBER;
2893 def_rettv.vval.v_number = -1;
2894
2895 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2896 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002897 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002898 {
2899 default_arg_err = 1;
2900 break;
2901 }
2902 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002903 }
2904 else
2905 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002906 if ((fp->uf_flags & FC_NOARGS) != 0)
2907 // Bail out if no a: arguments used (in lambda).
2908 break;
2909
Bram Moolenaare38eab22019-12-05 21:50:01 +01002910 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002911 sprintf((char *)numbuf, "%d", ai + 1);
2912 name = numbuf;
2913 }
2914 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2915 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002916 v = &fc->fc_fixvar[fixvar_idx++].var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002917 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002918 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002919 }
2920 else
2921 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002922 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002923 if (v == NULL)
2924 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002925 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002926 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002927
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002928 // Note: the values are copied directly to avoid alloc/free.
2929 // "argvars" must have VAR_FIXED for v_lock.
2930 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002931 v->di_tv.v_lock = VAR_FIXED;
2932
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002933 if (isdefault)
2934 // Need to free this later, no matter where it's stored.
2935 tv_to_free[tv_to_free_len++] = &v->di_tv;
2936
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002937 if (addlocal)
2938 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002939 // Named arguments should be accessed without the "a:" prefix in
2940 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002941 copy_tv(&v->di_tv, &v->di_tv);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002942 hash_add(&fc->fc_l_vars.dv_hashtab, DI2HIKEY(v), "local variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002943 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002944 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002945 hash_add(&fc->fc_l_avars.dv_hashtab, DI2HIKEY(v), "add variable");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002946
2947 if (ai >= 0 && ai < MAX_FUNC_ARGS)
2948 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01002949 listitem_T *li = &fc->fc_l_listitems[ai];
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002950
2951 li->li_tv = argvars[i];
2952 li->li_tv.v_lock = VAR_FIXED;
Bram Moolenaarca16c602022-09-06 18:57:08 +01002953 list_append(&fc->fc_l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002954 }
2955 }
2956
Bram Moolenaare38eab22019-12-05 21:50:01 +01002957 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002958 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02002959
2960 if (fp->uf_flags & FC_SANDBOX)
2961 {
2962 using_sandbox = TRUE;
2963 ++sandbox;
2964 }
2965
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002966 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002967 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002968 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002969 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002970 ++no_wait_return;
2971 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002972
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002973 smsg(_("calling %s"), SOURCING_NAME);
2974 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002975 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002976 char_u buf[MSG_BUF_LEN];
2977 char_u numbuf2[NUMBUFLEN];
2978 char_u *tofree;
2979 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002980
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002981 msg_puts("(");
2982 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002983 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002984 if (i > 0)
2985 msg_puts(", ");
2986 if (argvars[i].v_type == VAR_NUMBER)
2987 msg_outnum((long)argvars[i].vval.v_number);
2988 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002989 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002990 // Do not want errors such as E724 here.
2991 ++emsg_off;
2992 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
2993 --emsg_off;
2994 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002995 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002996 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002997 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002998 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2999 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003000 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003001 msg_puts((char *)s);
3002 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003003 }
3004 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003005 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003006 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003007 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003008 msg_puts("\n"); // don't overwrite this either
3009
3010 verbose_leave_scroll();
3011 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003012 }
3013#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003014 if (do_profiling == PROF_YES)
3015 profile_may_start_func(&profile_info, fp,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003016 fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003017#endif
3018
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003019 // "legacy" does not apply to commands in the function
3020 sticky_cmdmod_flags = 0;
3021
Bram Moolenaar98aff652022-09-06 21:02:35 +01003022 // If called from a compiled :def function the execution context must be
3023 // hidden, any deferred functions need to be added to the function being
3024 // executed here.
dundargocc57b5bc2022-11-02 13:30:51 +00003025 save_current_ectx = clear_current_ectx();
Bram Moolenaar98aff652022-09-06 21:02:35 +01003026
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003027 save_current_sctx = current_sctx;
3028 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003029 save_did_emsg = did_emsg;
3030 did_emsg = FALSE;
3031
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003032 if (default_arg_err && (fp->uf_flags & FC_ABORT))
Bram Moolenaar0917e862023-02-18 14:42:44 +00003033 {
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003034 did_emsg = TRUE;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003035 retval = FCERR_FAILED;
3036 }
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003037 else if (islambda)
3038 {
3039 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
3040
3041 // A Lambda always has the command "return {expr}". It is much faster
3042 // to evaluate {expr} directly.
3043 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003044 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003045 --ex_nesting_level;
3046 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003047 else
3048 // call do_cmdline() to execute the lines
3049 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003050 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
3051
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003052 // Invoke functions added with ":defer".
Bram Moolenaar58779852022-09-06 18:31:14 +01003053 handle_defer_one(current_funccal);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003054
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003055 --RedrawingDisabled;
3056
Bram Moolenaare38eab22019-12-05 21:50:01 +01003057 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003058 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
3059 {
3060 clear_tv(rettv);
3061 rettv->v_type = VAR_NUMBER;
3062 rettv->vval.v_number = -1;
Bram Moolenaard1149752023-02-18 15:31:53 +00003063
3064 // In corner cases returning a "failed" value is not backwards
3065 // compatible. Only do this for Vim9 script.
3066 if (in_vim9script())
3067 retval = FCERR_FAILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003068 }
3069
3070#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01003071 if (do_profiling == PROF_YES)
3072 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003073 ufunc_T *caller = fc->fc_caller == NULL ? NULL : fc->fc_caller->fc_func;
Bram Moolenaar12d26532021-02-19 19:13:21 +01003074
3075 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
3076 profile_may_end_func(&profile_info, fp, caller);
3077 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003078#endif
3079
Bram Moolenaare38eab22019-12-05 21:50:01 +01003080 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003081 if (p_verbose >= 12)
3082 {
3083 ++no_wait_return;
3084 verbose_enter_scroll();
3085
3086 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003087 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaarca16c602022-09-06 18:57:08 +01003088 else if (fc->fc_rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003089 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaarca16c602022-09-06 18:57:08 +01003090 (long)fc->fc_rettv->vval.v_number);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003091 else
3092 {
3093 char_u buf[MSG_BUF_LEN];
3094 char_u numbuf2[NUMBUFLEN];
3095 char_u *tofree;
3096 char_u *s;
3097
Bram Moolenaare38eab22019-12-05 21:50:01 +01003098 // The value may be very long. Skip the middle part, so that we
3099 // have some idea how it starts and ends. smsg() would always
3100 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003101 ++emsg_off;
Bram Moolenaarca16c602022-09-06 18:57:08 +01003102 s = tv2string(fc->fc_rettv, &tofree, numbuf2, 0);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003103 --emsg_off;
3104 if (s != NULL)
3105 {
3106 if (vim_strsize(s) > MSG_BUF_CLEN)
3107 {
3108 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
3109 s = buf;
3110 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003111 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003112 vim_free(tofree);
3113 }
3114 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003115 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003116
3117 verbose_leave_scroll();
3118 --no_wait_return;
3119 }
3120
Bram Moolenaare31ee862020-01-07 20:59:34 +01003121 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003122 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003123 current_sctx = save_current_sctx;
Bram Moolenaar98aff652022-09-06 21:02:35 +01003124 restore_current_ectx(save_current_ectx);
3125
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003126#ifdef FEAT_PROFILE
3127 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01003128 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003129#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02003130 if (using_sandbox)
3131 --sandbox;
Bram Moolenaarcdf04852022-02-12 22:13:06 +00003132 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003133
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003134 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003135 {
3136 ++no_wait_return;
3137 verbose_enter_scroll();
3138
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003139 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003140 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003141
3142 verbose_leave_scroll();
3143 --no_wait_return;
3144 }
3145
3146 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003147 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02003148 for (i = 0; i < tv_to_free_len; ++i)
3149 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003150 cleanup_function_call(fc);
Bram Moolenaar0917e862023-02-18 14:42:44 +00003151
3152 return retval;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003153}
3154
3155/*
Bram Moolenaar50824712020-12-20 21:10:17 +01003156 * Check the argument count for user function "fp".
3157 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
3158 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003159 funcerror_T
Bram Moolenaar50824712020-12-20 21:10:17 +01003160check_user_func_argcount(ufunc_T *fp, int argcount)
3161{
3162 int regular_args = fp->uf_args.ga_len;
3163
3164 if (argcount < regular_args - fp->uf_def_args.ga_len)
3165 return FCERR_TOOFEW;
3166 else if (!has_varargs(fp) && argcount > regular_args)
3167 return FCERR_TOOMANY;
3168 return FCERR_UNKNOWN;
3169}
3170
3171/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003173 */
Bram Moolenaar0917e862023-02-18 14:42:44 +00003174 funcerror_T
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175call_user_func_check(
3176 ufunc_T *fp,
3177 int argcount,
3178 typval_T *argvars,
3179 typval_T *rettv,
3180 funcexe_T *funcexe,
3181 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003182{
Bram Moolenaar0917e862023-02-18 14:42:44 +00003183 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003184
Bram Moolenaar7d149f82022-06-17 19:23:34 +01003185#ifdef FEAT_LUA
3186 if (fp->uf_flags & FC_CFUNC)
3187 {
3188 cfunc_T cb = fp->uf_cb;
3189
3190 return (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3191 }
3192#endif
3193
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003194 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
3195 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01003196 error = check_user_func_argcount(fp, argcount);
3197 if (error != FCERR_UNKNOWN)
3198 return error;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003199
Bram Moolenaar50824712020-12-20 21:10:17 +01003200 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar0917e862023-02-18 14:42:44 +00003201 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 error = FCERR_DICT;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003203 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003205 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 int did_save_redo = FALSE;
3207 save_redo_T save_redo;
3208
3209 /*
3210 * Call the user function.
3211 * Save and restore search patterns, script variables and
3212 * redo buffer.
3213 */
3214 save_search_patterns();
3215 if (!ins_compl_active())
3216 {
3217 saveRedobuff(&save_redo);
3218 did_save_redo = TRUE;
3219 }
3220 ++fp->uf_calls;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003221 error = call_user_func(fp, argcount, argvars, rettv, funcexe,
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003222 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
3224 // Function was unreferenced while being used, free it now.
3225 func_clear_free(fp, FALSE);
3226 if (did_save_redo)
3227 restoreRedobuff(&save_redo);
3228 restore_search_patterns();
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003229 }
Bram Moolenaar0917e862023-02-18 14:42:44 +00003230
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003232}
3233
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003234static funccal_entry_T *funccal_stack = NULL;
3235
3236/*
3237 * Save the current function call pointer, and set it to NULL.
3238 * Used when executing autocommands and for ":source".
3239 */
3240 void
3241save_funccal(funccal_entry_T *entry)
3242{
3243 entry->top_funccal = current_funccal;
3244 entry->next = funccal_stack;
3245 funccal_stack = entry;
3246 current_funccal = NULL;
3247}
3248
3249 void
3250restore_funccal(void)
3251{
3252 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003253 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003254 else
3255 {
3256 current_funccal = funccal_stack->top_funccal;
3257 funccal_stack = funccal_stack->next;
3258 }
3259}
3260
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02003261 funccall_T *
3262get_current_funccal(void)
3263{
3264 return current_funccal;
3265}
3266
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003267/*
Bram Moolenaara749a422022-02-12 19:52:25 +00003268 * Return TRUE when currently at the script level:
3269 * - not in a function
3270 * - not executing an autocommand
3271 * Note that when an autocommand sources a script the result is FALSE;
3272 */
3273 int
3274at_script_level(void)
3275{
3276 return current_funccal == NULL && autocmd_match == NULL;
3277}
3278
3279/*
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003280 * Mark all functions of script "sid" as deleted.
3281 */
3282 void
3283delete_script_functions(int sid)
3284{
3285 hashitem_T *hi;
3286 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02003287 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003288 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003289 size_t len;
3290
3291 buf[0] = K_SPECIAL;
3292 buf[1] = KS_EXTRA;
3293 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02003294 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003295 len = STRLEN(buf);
3296
Bram Moolenaarce658352020-07-31 23:47:12 +02003297 while (todo > 0)
3298 {
3299 todo = func_hashtab.ht_used;
3300 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3301 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003302 {
Bram Moolenaarce658352020-07-31 23:47:12 +02003303 fp = HI2UF(hi);
3304 if (STRNCMP(fp->uf_name, buf, len) == 0)
3305 {
3306 int changed = func_hashtab.ht_changed;
3307
3308 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01003309
3310 if (fp->uf_calls > 0)
3311 {
3312 // Function is executing, don't free it but do remove
3313 // it from the hashtable.
3314 if (func_remove(fp))
3315 fp->uf_refcount--;
3316 }
3317 else
3318 {
3319 func_clear(fp, TRUE);
3320 // When clearing a function another function can be
3321 // cleared as a side effect. When that happens start
3322 // over.
3323 if (changed != func_hashtab.ht_changed)
3324 break;
3325 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003326 }
3327 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003328 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003329 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003330}
3331
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003332#if defined(EXITFREE) || defined(PROTO)
3333 void
3334free_all_functions(void)
3335{
3336 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003337 ufunc_T *fp;
3338 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003339 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003340 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003341
Bram Moolenaare38eab22019-12-05 21:50:01 +01003342 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003343 while (current_funccal != NULL)
3344 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01003345 clear_tv(current_funccal->fc_rettv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02003346 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003347 if (current_funccal == NULL && funccal_stack != NULL)
3348 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003349 }
3350
Bram Moolenaare38eab22019-12-05 21:50:01 +01003351 // First clear what the functions contain. Since this may lower the
3352 // reference count of a function, it may also free a function and change
3353 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003354 while (todo > 0)
3355 {
3356 todo = func_hashtab.ht_used;
3357 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3358 if (!HASHITEM_EMPTY(hi))
3359 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 // clear the def function index now
3361 fp = HI2UF(hi);
3362 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003363 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364
Bram Moolenaare38eab22019-12-05 21:50:01 +01003365 // Only free functions that are not refcounted, those are
3366 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003367 if (func_name_refcount(fp->uf_name))
3368 ++skipped;
3369 else
3370 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003371 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003372 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003373 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003374 {
3375 skipped = 0;
3376 break;
3377 }
3378 }
3379 --todo;
3380 }
3381 }
3382
Bram Moolenaare38eab22019-12-05 21:50:01 +01003383 // Now actually free the functions. Need to start all over every time,
3384 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003385 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003386 while (func_hashtab.ht_used > skipped)
3387 {
3388 todo = func_hashtab.ht_used;
3389 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003390 if (!HASHITEM_EMPTY(hi))
3391 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003392 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003393 // Only free functions that are not refcounted, those are
3394 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003395 fp = HI2UF(hi);
3396 if (func_name_refcount(fp->uf_name))
3397 ++skipped;
3398 else
3399 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003400 if (func_free(fp, FALSE) == OK)
3401 {
3402 skipped = 0;
3403 break;
3404 }
3405 // did not actually free it
3406 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003407 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003408 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003409 }
3410 if (skipped == 0)
3411 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412
3413 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003414}
3415#endif
3416
3417/*
3418 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar15d16352022-01-17 20:09:08 +00003419 * lower case letter, doesn't contain AUTOLOAD_CHAR or ':', no "." after the
3420 * name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003421 * "len" is the length of "name", or -1 for NUL terminated.
3422 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003424builtin_function(char_u *name, int len)
3425{
Bram Moolenaar15d16352022-01-17 20:09:08 +00003426 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003427
Bram Moolenaara26b9702020-04-18 19:53:28 +02003428 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003429 return FALSE;
Bram Moolenaar15d16352022-01-17 20:09:08 +00003430 for (i = 0; name[i] != NUL && (len < 0 || i < len); ++i)
3431 {
3432 if (name[i] == AUTOLOAD_CHAR)
3433 return FALSE;
3434 if (!eval_isnamec(name[i]))
3435 {
3436 // "name.something" is not a builtin function
3437 if (name[i] == '.')
3438 return FALSE;
3439 break;
3440 }
3441 }
3442 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003443}
3444
3445 int
3446func_call(
3447 char_u *name,
3448 typval_T *args,
3449 partial_T *partial,
3450 dict_T *selfdict,
3451 typval_T *rettv)
3452{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003453 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003454 listitem_T *item;
3455 typval_T argv[MAX_FUNC_ARGS + 1];
3456 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003457 int r = 0;
3458
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003459 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003460 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003461 {
3462 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3463 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003464 emsg(_(e_too_many_arguments));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003465 break;
3466 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003467 // Make a copy of each argument. This is needed to be able to set
3468 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003469 copy_tv(&item->li_tv, &argv[argc++]);
3470 }
3471
3472 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003473 {
3474 funcexe_T funcexe;
3475
Bram Moolenaara80faa82020-04-12 19:37:17 +02003476 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003477 funcexe.fe_firstline = curwin->w_cursor.lnum;
3478 funcexe.fe_lastline = curwin->w_cursor.lnum;
3479 funcexe.fe_evaluate = TRUE;
3480 funcexe.fe_partial = partial;
3481 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003482 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3483 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003484
Bram Moolenaare38eab22019-12-05 21:50:01 +01003485 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003486 while (argc > 0)
3487 clear_tv(&argv[--argc]);
3488
3489 return r;
3490}
3491
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003492static int callback_depth = 0;
3493
3494 int
3495get_callback_depth(void)
3496{
3497 return callback_depth;
3498}
3499
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003500/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003501 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003502 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003503 */
3504 int
3505call_callback(
3506 callback_T *callback,
3507 int len, // length of "name" or -1 to use strlen()
3508 typval_T *rettv, // return value goes here
3509 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003510 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003511 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003512{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003513 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003514 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003515
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003516 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3517 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02003518 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003519 funcexe.fe_evaluate = TRUE;
3520 funcexe.fe_partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003521 ++callback_depth;
3522 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3523 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003524
3525 // When a :def function was called that uses :try an error would be turned
3526 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003527 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003528 {
3529 need_rethrow = FALSE;
3530 handle_did_throw();
3531 }
3532
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003533 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003534}
3535
3536/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003537 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003538 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003539 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3540 */
3541 varnumber_T
3542call_callback_retnr(
3543 callback_T *callback,
3544 int argcount, // number of "argvars"
3545 typval_T *argvars) // vars for arguments, must have "argcount"
3546 // PLUS ONE elements!
3547{
3548 typval_T rettv;
3549 varnumber_T retval;
3550
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003551 if (call_callback(callback, -1, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003552 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003553
3554 retval = tv_get_number_chk(&rettv, NULL);
3555 clear_tv(&rettv);
3556 return retval;
3557}
3558
3559/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003560 * Give an error message for the result of a function.
3561 * Nothing if "error" is FCERR_NONE.
3562 */
3563 void
Bram Moolenaar0917e862023-02-18 14:42:44 +00003564user_func_error(funcerror_T error, char_u *name, int found_var)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565{
3566 switch (error)
3567 {
3568 case FCERR_UNKNOWN:
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003569 if (found_var)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003570 emsg_funcname(e_not_callable_type_str, name);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003571 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003572 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 break;
3574 case FCERR_NOTMETHOD:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003575 emsg_funcname(e_cannot_use_function_as_method_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 break;
3577 case FCERR_DELETED:
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003578 emsg_funcname(e_function_was_deleted_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579 break;
3580 case FCERR_TOOMANY:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003581 emsg_funcname(e_too_many_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582 break;
3583 case FCERR_TOOFEW:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003584 emsg_funcname(e_not_enough_arguments_for_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 break;
3586 case FCERR_SCRIPT:
Bram Moolenaara6c18d32022-03-31 20:02:56 +01003587 emsg_funcname(e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003588 break;
3589 case FCERR_DICT:
Bram Moolenaara6f79292022-01-04 21:30:47 +00003590 emsg_funcname(e_calling_dict_function_without_dictionary_str,
3591 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592 break;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003593 case FCERR_OTHER:
3594 case FCERR_FAILED:
3595 // assume the error message was already given
3596 break;
3597 case FCERR_NONE:
3598 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599 }
3600}
3601
3602/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003603 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003604 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003605 * Return FAIL when the function can't be called, OK otherwise.
3606 * Also returns OK when an error was encountered while executing the function.
3607 */
3608 int
3609call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003610 char_u *funcname, // name of the function
3611 int len, // length of "name" or -1 to use strlen()
3612 typval_T *rettv, // return value goes here
3613 int argcount_in, // number of "argvars"
3614 typval_T *argvars_in, // vars for arguments, must have "argcount"
3615 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003616 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003617{
3618 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003619 funcerror_T error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003620 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003621 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003622 char_u fname_buf[FLEN_FIXED + 1];
3623 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003624 char_u *fname = NULL;
3625 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003626 int argcount = argcount_in;
3627 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003628 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003629 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003630 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003631 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003632 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003633 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003634 type_T check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003635 type_T *check_type_args[MAX_FUNC_ARGS];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003636
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003637 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3638 // even when call_func() returns FAIL.
3639 rettv->v_type = VAR_UNKNOWN;
3640
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003641 if (partial != NULL)
3642 fp = partial->pt_func;
3643 if (fp == NULL)
3644 {
3645 // Make a copy of the name, if it comes from a funcref variable it
3646 // could be changed or deleted in the called function.
3647 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3648 if (name == NULL)
3649 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003650
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003651 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3652 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003653
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003654 if (funcexe->fe_doesrange != NULL)
3655 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003656
3657 if (partial != NULL)
3658 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003659 // When the function has a partial with a dict and there is a dict
3660 // argument, use the dict argument. That is backwards compatible.
3661 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003662 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003663 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003664 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003665 {
3666 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003667 {
3668 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3669 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003670 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003671 goto theend;
3672 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003673 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003674 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003675 for (i = 0; i < argcount_in; ++i)
3676 argv[i + argv_clear] = argvars_in[i];
3677 argvars = argv;
3678 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003679
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003680 if (funcexe->fe_check_type != NULL
3681 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003682 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003683 // Now funcexe->fe_check_type is missing the added arguments,
3684 // make a copy of the type with the correction.
3685 check_type = *funcexe->fe_check_type;
3686 funcexe->fe_check_type = &check_type;
Bram Moolenaar13789bf2021-12-30 13:29:00 +00003687 check_type.tt_args = check_type_args;
3688 CLEAR_FIELD(check_type_args);
3689 for (i = 0; i < check_type.tt_argcount; ++i)
3690 check_type_args[i + partial->pt_argc] =
3691 check_type.tt_args[i];
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003692 check_type.tt_argcount += partial->pt_argc;
3693 check_type.tt_min_argcount += partial->pt_argc;
3694 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003695 }
3696 }
3697
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003698 if (error == FCERR_NONE && funcexe->fe_check_type != NULL
3699 && funcexe->fe_evaluate)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003700 {
3701 // Check that the argument types are OK for the types of the funcref.
Bram Moolenaarc84287d2022-01-16 18:06:21 +00003702 if (check_argument_types(funcexe->fe_check_type,
3703 argvars, argcount, funcexe->fe_basetv,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003704 (name != NULL) ? name : funcname) == FAIL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003705 error = FCERR_OTHER;
3706 }
3707
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003708 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003709 {
3710 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003711 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003712
Bram Moolenaar333894b2020-08-01 18:53:07 +02003713 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003714 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003715 {
3716 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003717 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003718 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003719
Bram Moolenaare38eab22019-12-05 21:50:01 +01003720 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003721 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003722 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003723
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003724 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003725 {
3726 /*
3727 * User defined function.
3728 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003729 if (fp == NULL)
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003730 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003731 fp = find_func(rfname, is_global);
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003732 if (fp != NULL && !is_global && in_vim9script()
3733 && func_requires_g_prefix(fp))
3734 // In Vim9 script g: is required to find a global
3735 // non-autoload function.
3736 fp = NULL;
3737 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003738
Bram Moolenaare38eab22019-12-05 21:50:01 +01003739 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003740 if (fp == NULL
3741 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003742 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003743 && !aborting())
3744 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003745 // executed an autocommand, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003746 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003747 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003748 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003749 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3750 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003751 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003752 fp = find_func(rfname, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003753 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003754 if (fp == NULL)
3755 {
3756 char_u *p = untrans_function_name(rfname);
3757
3758 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003759 // Don't do this if the name starts with "s:".
3760 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003761 fp = find_func(p, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003762 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003763
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003764 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003765 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003766 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003767 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003768 if (funcexe->fe_argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003769 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003770 argcount = funcexe->fe_argv_func(argcount, argvars,
zeertzjq48db5da2022-09-16 12:10:03 +01003771 argv_clear, fp);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003772
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003773 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003774 {
3775 // Method call: base->Method()
3776 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003777 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003778 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003779 argvars = argv;
3780 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003781 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 error = call_user_func_check(fp, argcount, argvars, rettv,
3784 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003785 }
3786 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003787 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003788 {
3789 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003790 * expr->method(): Find the method name in the table, call its
3791 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003792 */
3793 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003794 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003795 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003796 else
3797 {
3798 /*
3799 * Find the function name in the table, call its implementation.
3800 */
3801 error = call_internal_func(fname, argcount, argvars, rettv);
3802 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003803
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003804 /*
3805 * The function call (or "FuncUndefined" autocommand sequence) might
3806 * have been aborted by an error, an interrupt, or an explicitly thrown
3807 * exception that has not been caught so far. This situation can be
3808 * tested for by calling aborting(). For an error in an internal
3809 * function or for the "E132" error in call_user_func(), however, the
3810 * throw point at which the "force_abort" flag (temporarily reset by
3811 * emsg()) is normally updated has not been reached yet. We need to
3812 * update that flag first to make aborting() reliable.
3813 */
3814 update_force_abort();
3815 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003816 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003817 ret = OK;
3818
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003819theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003820 /*
3821 * Report an error unless the argument evaluation or function call has been
3822 * cancelled due to an aborting error, an interrupt, or an exception.
3823 */
3824 if (!aborting())
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003825 user_func_error(error, (name != NULL) ? name : funcname,
3826 funcexe->fe_found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003827
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003828 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003829 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003830 clear_tv(&argv[--argv_clear + argv_base]);
3831
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003832 vim_free(tofree);
3833 vim_free(name);
3834
3835 return ret;
3836}
3837
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003838/*
3839 * Call a function without arguments, partial or dict.
3840 * This is like call_func() when the call is only "FuncName()".
3841 * To be used by "expr" options.
3842 * Returns NOTDONE when the function could not be found.
3843 */
3844 int
3845call_simple_func(
3846 char_u *funcname, // name of the function
3847 int len, // length of "name" or -1 to use strlen()
3848 typval_T *rettv) // return value goes here
3849{
3850 int ret = FAIL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00003851 funcerror_T error = FCERR_NONE;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01003852 char_u fname_buf[FLEN_FIXED + 1];
3853 char_u *tofree = NULL;
3854 char_u *name;
3855 char_u *fname;
3856 char_u *rfname;
3857 int is_global = FALSE;
3858 ufunc_T *fp;
3859
3860 rettv->v_type = VAR_NUMBER; // default rettv is number zero
3861 rettv->vval.v_number = 0;
3862
3863 // Make a copy of the name, an option can be changed in the function.
3864 name = vim_strnsave(funcname, len);
3865 if (name == NULL)
3866 return ret;
3867
3868 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3869
3870 // Skip "g:" before a function name.
3871 if (fname[0] == 'g' && fname[1] == ':')
3872 {
3873 is_global = TRUE;
3874 rfname = fname + 2;
3875 }
3876 else
3877 rfname = fname;
3878 fp = find_func(rfname, is_global);
3879 if (fp != NULL && !is_global && in_vim9script()
3880 && func_requires_g_prefix(fp))
3881 // In Vim9 script g: is required to find a global non-autoload
3882 // function.
3883 fp = NULL;
3884 if (fp == NULL)
3885 ret = NOTDONE;
3886 else if (fp != NULL && (fp->uf_flags & FC_DELETED))
3887 error = FCERR_DELETED;
3888 else if (fp != NULL)
3889 {
3890 typval_T argvars[1];
3891 funcexe_T funcexe;
3892
3893 argvars[0].v_type = VAR_UNKNOWN;
3894 CLEAR_FIELD(funcexe);
3895 funcexe.fe_evaluate = TRUE;
3896
3897 error = call_user_func_check(fp, 0, argvars, rettv, &funcexe, NULL);
3898 if (error == FCERR_NONE)
3899 ret = OK;
3900 }
3901
3902 user_func_error(error, name, FALSE);
3903 vim_free(tofree);
3904 vim_free(name);
3905
3906 return ret;
3907}
3908
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003909 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003910printable_func_name(ufunc_T *fp)
3911{
3912 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
3913}
3914
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003915/*
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003916 * When "prev_ht_changed" does not equal "ht_changed" give an error and return
3917 * TRUE. Otherwise return FALSE.
3918 */
3919 static int
3920function_list_modified(int prev_ht_changed)
3921{
3922 if (prev_ht_changed != func_hashtab.ht_changed)
3923 {
3924 emsg(_(e_function_list_was_modified));
3925 return TRUE;
3926 }
3927 return FALSE;
3928}
3929
3930/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003931 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003932 */
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003933 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003934list_func_head(ufunc_T *fp, int indent)
3935{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003936 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003937 int j;
3938
3939 msg_start();
Bram Moolenaar398a26f2022-11-13 22:13:33 +00003940
3941 // a timer at the more prompt may have deleted the function
3942 if (function_list_modified(prev_ht_changed))
3943 return FAIL;
3944
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003945 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003946 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003947 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003948 msg_puts("def ");
3949 else
3950 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003952 msg_putchar('(');
3953 for (j = 0; j < fp->uf_args.ga_len; ++j)
3954 {
3955 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003956 msg_puts(", ");
3957 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958 if (fp->uf_arg_types != NULL)
3959 {
3960 char *tofree;
3961
3962 msg_puts(": ");
3963 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
3964 vim_free(tofree);
3965 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003966 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
3967 {
3968 msg_puts(" = ");
3969 msg_puts(((char **)(fp->uf_def_args.ga_data))
3970 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
3971 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003972 }
3973 if (fp->uf_varargs)
3974 {
3975 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003976 msg_puts(", ");
3977 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003978 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 if (fp->uf_va_name != NULL)
3980 {
Bram Moolenaar521bf322022-05-06 15:47:07 +01003981 if (!fp->uf_varargs)
3982 {
3983 if (j)
3984 msg_puts(", ");
3985 msg_puts("...");
3986 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02003988 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 {
3990 char *tofree;
3991
3992 msg_puts(": ");
3993 msg_puts(type_name(fp->uf_va_type, &tofree));
3994 vim_free(tofree);
3995 }
3996 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003997 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003998
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003999 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01004000 {
4001 if (fp->uf_ret_type != &t_void)
4002 {
4003 char *tofree;
4004
4005 msg_puts(": ");
4006 msg_puts(type_name(fp->uf_ret_type, &tofree));
4007 vim_free(tofree);
4008 }
4009 }
4010 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004011 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004012 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004013 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004014 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004015 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004016 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004017 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004018 msg_clr_eos();
4019 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004020 last_set_msg(fp->uf_script_ctx);
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004021
4022 return OK;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004023}
4024
4025/*
4026 * Get a function name, translating "<SID>" and "<SNR>".
4027 * Also handles a Funcref in a List or Dictionary.
4028 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004029 * Set "*is_global" to TRUE when the function must be global, unless
4030 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004031 * flags:
4032 * TFN_INT: internal function name OK
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004033 * TFN_IN_CLASS: function in a class
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004034 * TFN_QUIET: be quiet
4035 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004036 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004037 * Advances "pp" to just after the function name (if no error).
4038 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004039 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004040trans_function_name(
4041 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004042 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004043 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004044 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01004045 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004046 partial_T **partial, // return: partial of a FuncRef
4047 type_T **type) // return: type of funcref if not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004048{
4049 char_u *name = NULL;
4050 char_u *start;
4051 char_u *end;
4052 int lead;
4053 char_u sid_buf[20];
4054 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 int extra = 0;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004056 int prefix_g = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004057 lval_T lv;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004058 int vim9script = in_vim9script();
4059 int vim9_local;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004060
4061 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02004062 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004063 start = *pp;
4064
Bram Moolenaare38eab22019-12-05 21:50:01 +01004065 // Check for hard coded <SNR>: already translated function ID (from a user
4066 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004067 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
4068 && (*pp)[2] == (int)KE_SNR)
4069 {
4070 *pp += 3;
4071 len = get_id_len(pp) + 3;
4072 return vim_strnsave(start, len);
4073 }
4074
Bram Moolenaare38eab22019-12-05 21:50:01 +01004075 // A name starting with "<SID>" or "<SNR>" is local to a script. But
4076 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004077 lead = eval_fname_script(start);
4078 if (lead > 2)
4079 start += lead;
4080
Bram Moolenaare38eab22019-12-05 21:50:01 +01004081 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01004082 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004083 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004084 if (end == start || (vim9script && end != NULL
Bram Moolenaare6a42002022-01-21 10:32:58 +00004085 && end[-1] == AUTOLOAD_CHAR && *end == '('))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004086 {
4087 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004088 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004089 goto theend;
4090 }
4091 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
4092 {
4093 /*
4094 * Report an invalid expression in braces, unless the expression
4095 * evaluation has been cancelled due to an aborting error, an
4096 * interrupt, or an exception.
4097 */
4098 if (!aborting())
4099 {
4100 if (end != NULL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004101 semsg(_(e_invalid_argument_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004102 }
4103 else
4104 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
4105 goto theend;
4106 }
4107
4108 if (lv.ll_tv != NULL)
4109 {
4110 if (fdp != NULL)
4111 {
4112 fdp->fd_dict = lv.ll_dict;
4113 fdp->fd_newkey = lv.ll_newkey;
4114 lv.ll_newkey = NULL;
4115 fdp->fd_di = lv.ll_di;
4116 }
4117 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
4118 {
4119 name = vim_strsave(lv.ll_tv->vval.v_string);
4120 *pp = end;
4121 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00004122 else if (lv.ll_tv->v_type == VAR_CLASS
4123 && lv.ll_tv->vval.v_class != NULL)
4124 {
4125 name = vim_strsave(lv.ll_tv->vval.v_class->class_name);
4126 *pp = end;
4127 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004128 else if (lv.ll_tv->v_type == VAR_PARTIAL
4129 && lv.ll_tv->vval.v_partial != NULL)
4130 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004131 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004132 *pp = end;
4133 if (partial != NULL)
4134 *partial = lv.ll_tv->vval.v_partial;
4135 }
4136 else
4137 {
4138 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
4139 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaara6f79292022-01-04 21:30:47 +00004140 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004141 else
4142 *pp = end;
4143 name = NULL;
4144 }
4145 goto theend;
4146 }
4147
4148 if (lv.ll_name == NULL)
4149 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004150 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004151 *pp = end;
4152 goto theend;
4153 }
4154
Bram Moolenaare38eab22019-12-05 21:50:01 +01004155 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004156 if (lv.ll_exp_name != NULL)
4157 {
4158 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004159 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004160 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004161 if (name == lv.ll_exp_name)
4162 name = NULL;
4163 }
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004164 else if (lv.ll_sid > 0)
4165 {
4166 scriptitem_T *si = SCRIPT_ITEM(lv.ll_sid);
4167 int cc = *lv.ll_name_end;
4168
4169 // function in another script. Prefix <SNR>99_ or the autoload prefix.
4170 *lv.ll_name_end = NUL;
4171 if (si->sn_autoload_prefix != NULL)
4172 {
4173 name = concat_str(si->sn_autoload_prefix, lv.ll_name);
4174 }
4175 else
4176 {
4177 sid_buf[0] = K_SPECIAL;
4178 sid_buf[1] = KS_EXTRA;
4179 sid_buf[2] = (int)KE_SNR;
4180 vim_snprintf((char *)sid_buf + 3, sizeof(sid_buf) - 3,
Bram Moolenaar62aec932022-01-29 21:45:34 +00004181 "%ld_", (long)lv.ll_sid);
Bram Moolenaarf111cdf2022-01-12 12:48:17 +00004182 name = concat_str(sid_buf, lv.ll_name);
4183 }
4184 *lv.ll_name_end = cc;
4185 *pp = end;
4186 goto theend;
4187 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004188 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004189 {
4190 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004191 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar937610b2022-01-19 17:21:29 +00004192 flags & TFN_NO_AUTOLOAD, flags & TFN_NEW_FUNC, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004193 if (name == *pp)
4194 name = NULL;
4195 }
4196 if (name != NULL)
4197 {
4198 name = vim_strsave(name);
4199 *pp = end;
4200 if (STRNCMP(name, "<SNR>", 5) == 0)
4201 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004202 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004203 name[0] = K_SPECIAL;
4204 name[1] = KS_EXTRA;
4205 name[2] = (int)KE_SNR;
4206 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
4207 }
4208 goto theend;
4209 }
4210
4211 if (lv.ll_exp_name != NULL)
4212 {
4213 len = (int)STRLEN(lv.ll_exp_name);
4214 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
4215 && STRNCMP(lv.ll_name, "s:", 2) == 0)
4216 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004217 // When there was "s:" already or the name expanded to get a
4218 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004219 lv.ll_name += 2;
4220 len -= 2;
4221 lead = 2;
4222 }
4223 }
4224 else
4225 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004226 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004227 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004228 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004229 if (lv.ll_name[0] == 'g')
4230 {
4231 if (is_global != NULL)
4232 {
4233 *is_global = TRUE;
4234 }
4235 else
4236 {
4237 // dropping "g:" without setting "is_global" won't work in
4238 // Vim9script, put it back later
4239 prefix_g = TRUE;
4240 extra = 2;
4241 }
4242 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004243 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004244 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004245 len = (int)(end - lv.ll_name);
4246 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004247 if (len <= 0)
4248 {
4249 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004250 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02004251 goto theend;
4252 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004253
Bram Moolenaar17f700a2020-12-19 21:23:42 +01004254 // In Vim9 script a user function is script-local by default, unless it
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004255 // starts with a lower case character: dict.func(). Or when in a class.
4256 vim9_local = ASCII_ISUPPER(*start) && vim9script
4257 && (flags & TFN_IN_CLASS) == 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004259 /*
4260 * Copy the function name to allocated memory.
4261 * Accept <SID>name() inside a script, translate into <SNR>123_name().
4262 * Accept <SNR>123_name() outside a script.
4263 */
4264 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004265 lead = 0; // do nothing
Bram Moolenaar4525a572022-02-13 11:57:33 +00004266 else if (lead > 0 || vim9_local)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004267 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004268 if (!vim9_local)
Bram Moolenaar3787f262022-02-07 21:54:01 +00004269 {
Kota Kato948a3892022-08-16 16:09:59 +01004270 if (vim9script && lead == 2 && !ASCII_ISUPPER(*lv.ll_name)
4271 && current_script_is_vim9())
Bram Moolenaar3787f262022-02-07 21:54:01 +00004272 {
Bram Moolenaar72981ac2022-07-29 19:50:41 +01004273 semsg(_(e_function_name_must_start_with_capital_str), start);
Bram Moolenaar3787f262022-02-07 21:54:01 +00004274 goto theend;
4275 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 lead = 3;
Bram Moolenaar3787f262022-02-07 21:54:01 +00004277 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004278 if (vim9_local || (lv.ll_exp_name != NULL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004280 || eval_fname_sid(*pp))
4281 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004283 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004284 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00004285 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004286 goto theend;
4287 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004288 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004289 if (vim9_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004290 extra = 3 + (int)STRLEN(sid_buf);
4291 else
4292 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004293 }
4294 }
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004295 else if (!(flags & TFN_INT)
4296 && (builtin_function(lv.ll_name, len)
4297 || (vim9script && *lv.ll_name == '_'))
4298 && !((flags & TFN_IN_CLASS) && STRNCMP(lv.ll_name, "new", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004299 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004300 semsg(_(vim9script ? e_function_name_must_start_with_capital_str
Bram Moolenaar3787f262022-02-07 21:54:01 +00004301 : e_function_name_must_start_with_capital_or_s_str),
4302 start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004303 goto theend;
4304 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004305 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004306 {
4307 char_u *cp = vim_strchr(lv.ll_name, ':');
4308
4309 if (cp != NULL && cp < end)
4310 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004311 semsg(_(e_function_name_cannot_contain_colon_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004312 goto theend;
4313 }
4314 }
4315
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004317 if (name != NULL)
4318 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004319 if (!skip && (lead > 0 || vim9_local))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004320 {
4321 name[0] = K_SPECIAL;
4322 name[1] = KS_EXTRA;
4323 name[2] = (int)KE_SNR;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004324 if (vim9_local || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004325 STRCPY(name + 3, sid_buf);
4326 }
Bram Moolenaar848fadd2022-01-30 15:28:30 +00004327 else if (prefix_g)
4328 {
4329 name[0] = 'g';
4330 name[1] = ':';
4331 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
4333 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004334 }
4335 *pp = end;
4336
4337theend:
4338 clear_lval(&lv);
4339 return name;
4340}
4341
4342/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02004343 * Assuming "name" is the result of trans_function_name() and it was prefixed
4344 * to use the script-local name, return the unmodified name (points into
4345 * "name"). Otherwise return NULL.
4346 * This can be used to first search for a script-local function and fall back
4347 * to the global function if not found.
4348 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01004349 static char_u *
Bram Moolenaara26b9702020-04-18 19:53:28 +02004350untrans_function_name(char_u *name)
4351{
4352 char_u *p;
4353
Bram Moolenaareb6880b2020-07-12 17:07:05 +02004354 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02004355 {
4356 p = vim_strchr(name, '_');
4357 if (p != NULL)
4358 return p + 1;
4359 }
4360 return NULL;
4361}
4362
4363/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004364 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
4365 * current script ID and returns the expanded function name. The caller should
4366 * free the returned name. If not called from a script context or the function
4367 * name doesn't start with these prefixes, then returns NULL.
4368 * This doesn't check whether the script-local function exists or not.
4369 */
4370 char_u *
4371get_scriptlocal_funcname(char_u *funcname)
4372{
4373 char sid_buf[25];
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004374 int off;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004375 char_u *newname;
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004376 char_u *p = funcname;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004377
4378 if (funcname == NULL)
4379 return NULL;
4380
4381 if (STRNCMP(funcname, "s:", 2) != 0
4382 && STRNCMP(funcname, "<SID>", 5) != 0)
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004383 {
4384 ufunc_T *ufunc;
4385
4386 // The function name does not have a script-local prefix. Try finding
4387 // it when in a Vim9 script and there is no "g:" prefix.
4388 if (!in_vim9script() || STRNCMP(funcname, "g:", 2) == 0)
4389 return NULL;
4390 ufunc = find_func(funcname, FALSE);
4391 if (ufunc == NULL || func_is_global(ufunc)
4392 || (p = vim_strchr(ufunc->uf_name, '_')) == NULL)
4393 return NULL;
4394 ++p;
4395 off = 0;
4396 }
Bram Moolenaare89bfd22022-02-18 18:34:45 +00004397 else
4398 off = *funcname == 's' ? 2 : 5;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004399
4400 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
4401 {
4402 emsg(_(e_using_sid_not_in_script_context));
4403 return NULL;
4404 }
4405 // Expand s: prefix into <SNR>nr_<name>
4406 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
4407 (long)current_sctx.sc_sid);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004408 newname = alloc(STRLEN(sid_buf) + STRLEN(p + off) + 1);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004409 if (newname == NULL)
4410 return NULL;
4411 STRCPY(newname, sid_buf);
Bram Moolenaar1fca5f32022-02-18 17:50:47 +00004412 STRCAT(newname, p + off);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004413
4414 return newname;
4415}
4416
4417/*
Bram Moolenaarc2f17f72022-02-21 13:13:50 +00004418 * Return script-local "fname" with the 3-byte sequence replaced by
4419 * printable <SNR> in allocated memory.
4420 */
4421 char_u *
4422alloc_printable_func_name(char_u *fname)
4423{
4424 char_u *n = alloc(STRLEN(fname + 3) + 6);
4425
4426 if (n != NULL)
4427 {
4428 STRCPY(n, "<SNR>");
4429 STRCPY(n + 5, fname + 3);
4430 }
4431 return n;
4432}
4433
4434/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004435 * Call trans_function_name(), except that a lambda is returned as-is.
4436 * Returns the name in allocated memory.
4437 */
4438 char_u *
4439save_function_name(
4440 char_u **name,
4441 int *is_global,
4442 int skip,
4443 int flags,
4444 funcdict_T *fudi)
4445{
4446 char_u *p = *name;
4447 char_u *saved;
4448
4449 if (STRNCMP(p, "<lambda>", 8) == 0)
4450 {
4451 p += 8;
4452 (void)getdigits(&p);
4453 saved = vim_strnsave(*name, p - *name);
4454 if (fudi != NULL)
4455 CLEAR_POINTER(fudi);
4456 }
4457 else
4458 saved = trans_function_name(&p, is_global, skip,
4459 flags, fudi, NULL, NULL);
4460 *name = p;
4461 return saved;
4462}
4463
4464/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004465 * List functions. When "regmatch" is NULL all of then.
4466 * Otherwise functions matching "regmatch".
4467 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004468 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004469list_functions(regmatch_T *regmatch)
4470{
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004471 int prev_ht_changed = func_hashtab.ht_changed;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004472 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004473 hashitem_T *hi;
4474
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004475 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004476 {
4477 if (!HASHITEM_EMPTY(hi))
4478 {
4479 ufunc_T *fp = HI2UF(hi);
4480
4481 --todo;
4482 if ((fp->uf_flags & FC_DEAD) == 0
4483 && (regmatch == NULL
4484 ? !message_filtered(fp->uf_name)
4485 && !func_name_refcount(fp->uf_name)
4486 : !isdigit(*fp->uf_name)
4487 && vim_regexec(regmatch, fp->uf_name, 0)))
4488 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004489 if (list_func_head(fp, FALSE) == FAIL)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004490 return;
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004491 if (function_list_modified(prev_ht_changed))
4492 return;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004493 }
4494 }
4495 }
4496}
4497
4498/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02004499 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004500 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
4501 * the function name.
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00004502 * "lines_to_free" is a list of strings to be freed later.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004503 * If "class_flags" has CF_CLASS then the function is defined inside a class.
4504 * With CF_INTERFACE the function is define inside an interface, only the
4505 * ":def"/":function" line is expected, no function body.
Bram Moolenaar04b12692020-05-04 23:24:44 +02004506 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004507 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02004508 ufunc_T *
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004509define_function(
4510 exarg_T *eap,
4511 char_u *name_arg,
4512 garray_T *lines_to_free,
Bram Moolenaar554d0312023-01-05 19:59:18 +00004513 int class_flags)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004514{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004515 int j;
4516 int c;
Bram Moolenaar57033102022-01-30 19:37:52 +00004517 int saved_did_emsg = FALSE;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004518 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004519 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004520 char_u *p;
4521 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004522 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004523 char_u *line_arg = NULL;
4524 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004526 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004527 garray_T newlines;
4528 int varargs = FALSE;
4529 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004530 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004531 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004532 int fp_allocated = FALSE;
4533 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004534 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004535 dictitem_T *v;
4536 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004537 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004538 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004539 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004540 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004541 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004542 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004543
4544 /*
4545 * ":function" without argument: list functions.
4546 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004547 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004548 {
4549 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004550 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004551 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004552 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004553 }
4554
4555 /*
4556 * ":function /pat": list functions matching pattern.
4557 */
4558 if (*eap->arg == '/')
4559 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004560 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004561 if (!eap->skip)
4562 {
4563 regmatch_T regmatch;
4564
4565 c = *p;
4566 *p = NUL;
4567 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4568 *p = c;
4569 if (regmatch.regprog != NULL)
4570 {
4571 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004572 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004573 vim_regfree(regmatch.regprog);
4574 }
4575 }
4576 if (*p == '/')
4577 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004578 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004579 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004580 }
4581
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004582 ga_init(&newargs);
4583 ga_init(&argtypes);
4584 ga_init(&default_args);
4585
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004586 /*
4587 * Get the function name. There are these situations:
Bram Moolenaar554d0312023-01-05 19:59:18 +00004588 * func normal function name, also when "class_flags" is non-zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004589 * "name" == func, "fudi.fd_dict" == NULL
4590 * dict.func new dictionary entry
4591 * "name" == NULL, "fudi.fd_dict" set,
4592 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4593 * dict.func existing dict entry with a Funcref
4594 * "name" == func, "fudi.fd_dict" set,
4595 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4596 * dict.func existing dict entry that's not a Funcref
4597 * "name" == NULL, "fudi.fd_dict" set,
4598 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4599 * s:func script-local function name
4600 * g:func global function name, same as "func"
4601 */
4602 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004603 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004604 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004605 // nested function, argument is (args).
4606 paren = TRUE;
4607 CLEAR_FIELD(fudi);
4608 }
4609 else
4610 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004611 if (vim9script)
Bram Moolenaara749a422022-02-12 19:52:25 +00004612 {
Bram Moolenaardea5ab02022-02-23 22:12:02 +00004613 if (p[0] == 's' && p[1] == ':')
4614 {
4615 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), p);
4616 return NULL;
4617 }
4618 p = to_name_end(p, TRUE);
4619 if (*skipwhite(p) == '.' && vim_strchr(p, '(') != NULL)
4620 {
4621 semsg(_(e_cannot_define_dict_func_in_vim9_script_str),
4622 eap->arg);
4623 return NULL;
4624 }
4625 p = eap->arg;
Bram Moolenaara749a422022-02-12 19:52:25 +00004626 }
4627
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004628 int tfn_flags = TFN_NO_AUTOLOAD | TFN_NEW_FUNC
Bram Moolenaar554d0312023-01-05 19:59:18 +00004629 | (class_flags != 0 ? TFN_IN_CLASS : 0);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004630 name = save_function_name(&p, &is_global, eap->skip, tfn_flags, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004631 paren = (vim_strchr(p, '(') != NULL);
4632 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004633 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004634 /*
4635 * Return on an invalid expression in braces, unless the expression
4636 * evaluation has been cancelled due to an aborting error, an
4637 * interrupt, or an exception.
4638 */
4639 if (!aborting())
4640 {
4641 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004642 semsg(_(e_key_not_present_in_dictionary_str),
4643 fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004644 vim_free(fudi.fd_newkey);
4645 return NULL;
4646 }
4647 else
4648 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004649 }
Bram Moolenaardc4451d2022-01-09 21:36:37 +00004650
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004651 // For "export def FuncName()" in an autoload script the function name
4652 // is stored with the legacy autoload name "dir#script#FuncName" so
4653 // that it can also be found in legacy script.
Bram Moolenaar78a70532022-01-13 13:24:34 +00004654 if (is_export && name != NULL)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004655 {
4656 char_u *prefixed = may_prefix_autoload(name);
4657
4658 if (prefixed != NULL && prefixed != name)
4659 {
4660 vim_free(name);
4661 name = prefixed;
4662 }
4663 }
Bram Moolenaar8164f6e2022-02-06 13:08:41 +00004664 else if (paren && vim9script && name != NULL
Bram Moolenaar48a60482022-01-31 11:44:48 +00004665 && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaard8fe6d32022-01-30 18:40:44 +00004666 {
4667 emsg(_(e_cannot_use_name_with_hash_in_vim9_script_use_export_instead));
4668 goto ret_free;
4669 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004670 }
4671
Bram Moolenaare38eab22019-12-05 21:50:01 +01004672 // An error in a function call during evaluation of an expression in magic
4673 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004674 saved_did_emsg = did_emsg;
4675 did_emsg = FALSE;
4676
4677 /*
4678 * ":function func" with only function name: list function.
4679 */
4680 if (!paren)
4681 {
4682 if (!ends_excmd(*skipwhite(p)))
4683 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004684 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004685 goto ret_free;
4686 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004687 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004688 if (eap->nextcmd != NULL)
4689 *p = NUL;
4690 if (!eap->skip && !got_int)
4691 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004692 fp = find_func(name, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004693 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4694 {
4695 char_u *up = untrans_function_name(name);
4696
4697 // With Vim9 script the name was made script-local, if not
4698 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004699 if (up != NULL)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004700 fp = find_func(up, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004701 }
4702
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004703 if (fp != NULL)
4704 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004705 // Check no function was added or removed from a timer, e.g. at
4706 // the more prompt. "fp" may then be invalid.
4707 int prev_ht_changed = func_hashtab.ht_changed;
4708
4709 if (list_func_head(fp, TRUE) == OK)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004710 {
Bram Moolenaar398a26f2022-11-13 22:13:33 +00004711 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4712 {
4713 if (FUNCLINE(fp, j) == NULL)
4714 continue;
4715 msg_putchar('\n');
4716 msg_outnum((long)(j + 1));
4717 if (j < 9)
4718 msg_putchar(' ');
4719 if (j < 99)
4720 msg_putchar(' ');
4721 if (function_list_modified(prev_ht_changed))
4722 break;
4723 msg_prt_line(FUNCLINE(fp, j), FALSE);
4724 out_flush(); // show a line at a time
4725 ui_breakcheck();
4726 }
4727 if (!got_int)
4728 {
4729 msg_putchar('\n');
4730 if (!function_list_modified(prev_ht_changed))
4731 {
4732 if (fp->uf_def_status != UF_NOT_COMPILED)
4733 msg_puts(" enddef");
4734 else
4735 msg_puts(" endfunction");
4736 }
4737 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004738 }
4739 }
4740 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004741 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004742 }
4743 goto ret_free;
4744 }
4745
4746 /*
4747 * ":function name(arg1, arg2)" Define function.
4748 */
4749 p = skipwhite(p);
4750 if (*p != '(')
4751 {
4752 if (!eap->skip)
4753 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004754 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004755 goto ret_free;
4756 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004757 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004758 if (vim_strchr(p, '(') != NULL)
4759 p = vim_strchr(p, '(');
4760 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004761
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004762 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4763 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004764 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004765 goto ret_free;
4766 }
4767
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004768 // In Vim9 script only global functions can be redefined.
4769 if (vim9script && eap->forceit && !is_global)
4770 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004771 emsg(_(e_no_bang_allowed));
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004772 goto ret_free;
4773 }
4774
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004775 ga_init2(&newlines, sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004776
Bram Moolenaar04b12692020-05-04 23:24:44 +02004777 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004778 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004779 // Check the name of the function. Unless it's a dictionary function
4780 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004781 if (name != NULL)
4782 arg = name;
4783 else
4784 arg = fudi.fd_newkey;
4785 if (arg != NULL && (fudi.fd_di == NULL
4786 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4787 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4788 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004789 char_u *name_base = arg;
4790 int i;
4791
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004792 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004793 {
4794 name_base = vim_strchr(arg, '_');
4795 if (name_base == NULL)
4796 name_base = arg + 3;
4797 else
4798 ++name_base;
4799 }
4800 for (i = 0; name_base[i] != NUL && (i == 0
4801 ? eval_isnamec1(name_base[i])
4802 : eval_isnamec(name_base[i])); ++i)
4803 ;
4804 if (name_base[i] != NUL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004805 emsg_funcname(e_invalid_argument_str, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004806
4807 // In Vim9 script a function cannot have the same name as a
4808 // variable.
4809 if (vim9script && *arg == K_SPECIAL
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004810 && eval_variable(name_base, (int)STRLEN(name_base), 0, NULL,
4811 NULL, EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
Bram Moolenaar052ff292021-12-11 13:54:46 +00004812 + EVAL_VAR_NO_FUNC) == OK)
4813 {
4814 semsg(_(e_redefining_script_item_str), name_base);
4815 goto ret_free;
4816 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004817 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004818 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004819 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004820 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +00004821 emsg(_(e_cannot_use_g_here));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004822 goto ret_free;
4823 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004824 }
4825
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004826 // This may get more lines and make the pointers into the first line
4827 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004828 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarf0571712023-01-04 13:16:20 +00004830 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004831 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar554d0312023-01-05 19:59:18 +00004832 eap, class_flags, &newlines, lines_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004833 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004834 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004835
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004836 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004837 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004838 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004839 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004840 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004841 if (*p != ':')
4842 {
4843 semsg(_(e_no_white_space_allowed_before_colon_str), p);
4844 p = skipwhite(p);
4845 }
4846 else if (!IS_WHITE_OR_NUL(p[1]))
4847 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004848 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02004849 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004851 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004852 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01004853 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004854 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004855 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004856 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004857 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004858 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02004859 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004860 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004861 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02004862 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004863 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004864 else
4865 // find extra arguments "range", "dict", "abort" and "closure"
4866 for (;;)
4867 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01004868 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004869 p = skipwhite(p);
4870 if (STRNCMP(p, "range", 5) == 0)
4871 {
4872 flags |= FC_RANGE;
4873 p += 5;
4874 }
4875 else if (STRNCMP(p, "dict", 4) == 0)
4876 {
4877 flags |= FC_DICT;
4878 p += 4;
4879 }
4880 else if (STRNCMP(p, "abort", 5) == 0)
4881 {
4882 flags |= FC_ABORT;
4883 p += 5;
4884 }
4885 else if (STRNCMP(p, "closure", 7) == 0)
4886 {
4887 flags |= FC_CLOSURE;
4888 p += 7;
4889 if (current_funccal == NULL)
4890 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00004891 emsg_funcname(e_closure_function_should_not_be_at_top_level_str,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892 name == NULL ? (char_u *)"" : name);
4893 goto erret;
4894 }
4895 }
4896 else
4897 break;
4898 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004899
Bram Moolenaare38eab22019-12-05 21:50:01 +01004900 // When there is a line break use what follows for the function body.
4901 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004902 if (*p == '\n')
4903 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004904 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02004905 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
4906 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01004907 && !(VIM_ISWHITE(*whitep) && *p == '#'
4908 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02004909 && !eap->skip
4910 && !did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004911 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004912
4913 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004914 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
4915 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004916 */
4917 if (KeyTyped)
4918 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004919 // Check if the function already exists, don't let the user type the
4920 // whole function before telling him it doesn't work! For a script we
4921 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004922 if (!eap->skip && !eap->forceit)
4923 {
4924 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaara6f79292022-01-04 21:30:47 +00004925 emsg(_(e_dictionary_entry_already_exists));
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00004926 else if (name != NULL && find_func(name, is_global) != NULL)
Bram Moolenaar1a992222021-12-31 17:25:48 +00004927 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004928 }
4929
4930 if (!eap->skip && did_emsg)
4931 goto erret;
4932
Bram Moolenaare38eab22019-12-05 21:50:01 +01004933 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004934 cmdline_row = msg_row;
4935 }
4936
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004937 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004938 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004939
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004940 // Do not define the function when getting the body fails and when
4941 // skipping.
Bram Moolenaar554d0312023-01-05 19:59:18 +00004942 if (((class_flags & CF_INTERFACE) == 0
4943 && get_function_body(eap, &newlines, line_arg, lines_to_free)
4944 == FAIL)
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004945 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004946 goto erret;
4947
4948 /*
4949 * If there are no errors, add the function
4950 */
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004951 if (fudi.fd_dict != NULL)
4952 {
4953 char numbuf[20];
4954
4955 fp = NULL;
4956 if (fudi.fd_newkey == NULL && !eap->forceit)
4957 {
4958 emsg(_(e_dictionary_entry_already_exists));
4959 goto erret;
4960 }
4961 if (fudi.fd_di == NULL)
4962 {
4963 // Can't add a function to a locked dictionary
4964 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
4965 goto erret;
4966 }
4967 // Can't change an existing function if it is locked
4968 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
4969 goto erret;
4970
4971 // Give the function a sequential number. Can only be used with a
4972 // Funcref!
4973 vim_free(name);
4974 sprintf(numbuf, "%d", ++func_nr);
4975 name = vim_strsave((char_u *)numbuf);
4976 if (name == NULL)
4977 goto erret;
4978 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00004979 else if (class_flags == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004980 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981 hashtab_T *ht;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004982 char_u *find_name = name;
4983 int var_conflict = FALSE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00004984 int ffed_flags = is_global ? FFED_IS_GLOBAL : 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004985
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004986 v = find_var(name, &ht, TRUE);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004987 if (v != NULL && (vim9script || v->di_tv.v_type == VAR_FUNC))
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00004988 var_conflict = TRUE;
4989
4990 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
4991 {
4992 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
4993
4994 if (si->sn_autoload_prefix != NULL)
4995 {
4996 if (is_export)
4997 {
4998 find_name = name + STRLEN(si->sn_autoload_prefix);
4999 v = find_var(find_name, &ht, TRUE);
5000 if (v != NULL)
5001 var_conflict = TRUE;
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005002 // Only check if the function already exists in the script,
5003 // global functions can be shadowed.
5004 ffed_flags |= FFED_NO_GLOBAL;
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005005 }
5006 else
5007 {
5008 char_u *prefixed = may_prefix_autoload(name);
5009
Bram Moolenaar9383a3a2022-02-25 21:35:17 +00005010 if (prefixed != NULL && prefixed != name)
Bram Moolenaar9c7cae62022-01-20 19:10:25 +00005011 {
5012 v = find_var(prefixed, &ht, TRUE);
5013 if (v != NULL)
5014 var_conflict = TRUE;
5015 vim_free(prefixed);
5016 }
5017 }
5018 }
5019 }
5020 if (var_conflict)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005021 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005022 emsg_funcname(e_function_name_conflicts_with_variable_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005023 goto erret;
5024 }
5025
Bram Moolenaaracc4b562022-01-24 13:54:45 +00005026 fp = find_func_even_dead(find_name, ffed_flags);
Bram Moolenaareef21022020-08-01 22:16:43 +02005027 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005028 {
Bram Moolenaareef21022020-08-01 22:16:43 +02005029 char_u *uname = untrans_function_name(name);
5030
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005031 import = find_imported(uname == NULL ? name : uname, 0, FALSE);
Bram Moolenaareef21022020-08-01 22:16:43 +02005032 }
5033
5034 if (fp != NULL || import != NULL)
5035 {
5036 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005037
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005038 // Function can be replaced with "function!" and when sourcing the
5039 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02005040 // A name that is used by an import can not be overruled.
5041 if (import != NULL
5042 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005043 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02005044 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005045 {
Bram Moolenaard604d782021-11-20 21:46:20 +00005046 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02005047 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02005048 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02005049 else
Bram Moolenaar1a992222021-12-31 17:25:48 +00005050 emsg_funcname(e_function_str_already_exists_add_bang_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005051 goto erret;
5052 }
5053 if (fp->uf_calls > 0)
5054 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01005055 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00005056 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005057 goto erret;
5058 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005059 if (fp->uf_refcount > 1)
5060 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005061 // This function is referenced somewhere, don't redefine it but
5062 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005063 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005064 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005065 fp = NULL;
5066 overwrite = TRUE;
5067 }
5068 else
5069 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005070 char_u *exp_name = fp->uf_name_exp;
5071
5072 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01005073 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005074 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005075 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01005076 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005077 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02005078#ifdef FEAT_PROFILE
5079 fp->uf_profiling = FALSE;
5080 fp->uf_prof_initialized = FALSE;
5081#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01005082 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005083 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005084 }
5085 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005086
5087 if (fp == NULL)
5088 {
5089 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
5090 {
5091 int slen, plen;
5092 char_u *scriptname;
5093
Bram Moolenaare38eab22019-12-05 21:50:01 +01005094 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005095 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005096 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005097 {
5098 scriptname = autoload_name(name);
5099 if (scriptname != NULL)
5100 {
5101 p = vim_strchr(scriptname, '/');
5102 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005103 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005104 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005105 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005106 j = OK;
5107 vim_free(scriptname);
5108 }
5109 }
5110 if (j == FAIL)
5111 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005112 linenr_T save_lnum = SOURCING_LNUM;
5113
5114 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaar677658a2022-01-05 16:09:06 +00005115 semsg(_(e_function_name_does_not_match_script_file_name_str),
5116 name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02005117 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005118 goto erret;
5119 }
5120 }
5121
Bram Moolenaare01e5212023-01-08 20:31:18 +00005122 fp = alloc_ufunc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005123 if (fp == NULL)
5124 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005125 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005126
5127 if (fudi.fd_dict != NULL)
5128 {
5129 if (fudi.fd_di == NULL)
5130 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005131 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005132 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
5133 if (fudi.fd_di == NULL)
5134 {
5135 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02005136 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005137 goto erret;
5138 }
5139 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
5140 {
5141 vim_free(fudi.fd_di);
5142 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02005143 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005144 goto erret;
5145 }
5146 }
5147 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005148 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005149 clear_tv(&fudi.fd_di->di_tv);
5150 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005151 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005152
Bram Moolenaare38eab22019-12-05 21:50:01 +01005153 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005154 flags |= FC_DICT;
5155 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005156 }
5157 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02005158 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005159 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02005160 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005161
5162 if (eap->cmdidx == CMD_def)
5163 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02005164 int lnum_save = SOURCING_LNUM;
5165 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005166
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005167 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02005168
Bram Moolenaarbfe12042020-02-04 21:54:07 +01005169 // error messages are for the first function line
5170 SOURCING_LNUM = sourcing_lnum_top;
5171
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02005172 // The function may use script variables from the context.
5173 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02005174
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005175 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005176 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005177 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005178 free_fp = fp_allocated;
5179 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01005181 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005182
5183 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005184 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005185 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005186 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005187 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01005188 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005189 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02005190 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005191 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005192 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02005193 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005194
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005195 if (fp_allocated)
5196 {
5197 // insert the new function in the function list
5198 set_ufunc_name(fp, name);
5199 if (overwrite)
5200 {
5201 hi = hash_find(&func_hashtab, name);
5202 hi->hi_key = UF2HIKEY(fp);
5203 }
Bram Moolenaar554d0312023-01-05 19:59:18 +00005204 else if (class_flags == 0 && hash_add(&func_hashtab,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005205 UF2HIKEY(fp), "add function") == FAIL)
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005206 {
5207 free_fp = TRUE;
5208 goto erret;
5209 }
5210 fp->uf_refcount = 1;
5211 }
5212
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005213 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005214 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005215 if ((flags & FC_CLOSURE) != 0)
5216 {
Bram Moolenaar58016442016-07-31 18:30:22 +02005217 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005218 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005219 }
5220 else
5221 fp->uf_scoped = NULL;
5222
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005223#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005224 if (prof_def_func())
5225 func_do_profile(fp);
5226#endif
5227 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02005228 if (sandbox)
5229 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02005230 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005231 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005232 fp->uf_flags = flags;
5233 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01005234 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005235 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02005236 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02005237 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005238 if (is_export)
5239 {
5240 fp->uf_flags |= FC_EXPORT;
dundargocc57b5bc2022-11-02 13:30:51 +00005241 // let do_one_cmd() know the export worked.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005242 is_export = FALSE;
5243 }
5244
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005245 if (eap->cmdidx == CMD_def)
5246 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02005247 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
5248 // :func does not use Vim9 script syntax, even in a Vim9 script file
5249 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02005250
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005251 goto ret_free;
5252
5253erret:
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005254 if (fp != NULL)
5255 {
Bram Moolenaarf0571712023-01-04 13:16:20 +00005256 // these were set to "newargs" and "default_args", which are cleared
5257 // below
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005258 ga_init(&fp->uf_args);
5259 ga_init(&fp->uf_def_args);
5260 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005261errret_2:
Bram Moolenaarf0571712023-01-04 13:16:20 +00005262 ga_clear_strings(&newargs);
5263 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005264 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005265 if (fp != NULL)
Bram Moolenaarf0571712023-01-04 13:16:20 +00005266 {
Bram Moolenaar31842cd2021-02-12 22:10:21 +01005267 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarf0571712023-01-04 13:16:20 +00005268 VIM_CLEAR(fp->uf_va_name);
5269 clear_type_list(&fp->uf_type_list);
5270 }
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005271 if (free_fp)
5272 {
5273 vim_free(fp);
5274 fp = NULL;
5275 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005276ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01005277 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005278 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02005279 if (name != name_arg)
5280 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02005281 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005282 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02005283
5284 return fp;
5285}
5286
5287/*
5288 * ":function"
5289 */
5290 void
5291ex_function(exarg_T *eap)
5292{
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005293 garray_T lines_to_free;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00005294
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005295 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar554d0312023-01-05 19:59:18 +00005296 (void)define_function(eap, NULL, &lines_to_free, 0);
Bram Moolenaar9f1a39a2022-01-08 15:39:39 +00005297 ga_clear_strings(&lines_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02005298}
5299
5300/*
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005301 * Find a function by name, including "<lambda>123".
5302 * Check for "profile" and "debug" arguments and set"compile_type".
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005303 * Caller should initialize "compile_type" to CT_NONE.
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005304 * Return NULL if not found.
5305 */
5306 ufunc_T *
5307find_func_by_name(char_u *name, compiletype_T *compile_type)
5308{
5309 char_u *arg = name;
5310 char_u *fname;
5311 ufunc_T *ufunc;
5312 int is_global = FALSE;
5313
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005314 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
5315 {
5316 *compile_type = CT_PROFILE;
5317 arg = skipwhite(arg + 7);
5318 }
5319 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
5320 {
5321 *compile_type = CT_DEBUG;
5322 arg = skipwhite(arg + 5);
5323 }
5324
5325 if (STRNCMP(arg, "<lambda>", 8) == 0)
5326 {
5327 arg += 8;
5328 (void)getdigits(&arg);
5329 fname = vim_strnsave(name, arg - name);
5330 }
5331 else
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005332 {
5333 // First try finding a method in a class, find_func_by_name() will give
5334 // an error if the function is not found.
5335 ufunc = find_class_func(&arg);
5336 if (ufunc != NULL)
5337 return ufunc;
5338
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005339 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005340 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DECL,
5341 NULL, NULL, NULL);
5342 }
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005343 if (fname == NULL)
5344 {
5345 semsg(_(e_invalid_argument_str), name);
5346 return NULL;
5347 }
5348 if (!ends_excmd2(name, arg))
5349 {
Bram Moolenaar1a56ea82022-05-21 16:28:42 +01005350 vim_free(fname);
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005351 emsg(ex_errmsg(e_trailing_characters_str, arg));
5352 return NULL;
5353 }
5354
5355 ufunc = find_func(fname, is_global);
5356 if (ufunc == NULL)
5357 {
5358 char_u *p = untrans_function_name(fname);
5359
5360 if (p != NULL)
5361 // Try again without making it script-local.
5362 ufunc = find_func(p, FALSE);
5363 }
5364 vim_free(fname);
5365 if (ufunc == NULL)
5366 semsg(_(e_cannot_find_function_str), name);
5367 return ufunc;
5368}
5369
5370/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02005371 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005372 * be compiled or the one specified by the argument.
5373 * Skips dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02005374 */
5375 void
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005376ex_defcompile(exarg_T *eap)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005377{
Bram Moolenaar822ba242020-05-24 23:00:18 +02005378 ufunc_T *ufunc;
5379
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005380 if (*eap->arg != NUL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02005381 {
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01005382 compiletype_T compile_type = CT_NONE;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005383
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005384 ufunc = find_func_by_name(eap->arg, &compile_type);
5385 if (ufunc != NULL)
5386 {
5387 if (func_needs_compiling(ufunc, compile_type))
5388 (void)compile_def_function(ufunc, FALSE, compile_type, NULL);
5389 else
5390 smsg(_("Function %s does not need compiling"), eap->arg);
5391 }
5392 }
5393 else
5394 {
5395 long todo = (long)func_hashtab.ht_used;
5396 int changed = func_hashtab.ht_changed;
5397 hashitem_T *hi;
5398
5399 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
5400 {
5401 if (!HASHITEM_EMPTY(hi))
5402 {
5403 --todo;
5404 ufunc = HI2UF(hi);
5405 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
5406 && ufunc->uf_def_status == UF_TO_BE_COMPILED
5407 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005408 {
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01005409 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
5410
5411 if (func_hashtab.ht_changed != changed)
5412 {
5413 // a function has been added or removed, need to start
5414 // over
5415 todo = (long)func_hashtab.ht_used;
5416 changed = func_hashtab.ht_changed;
5417 hi = func_hashtab.ht_array;
5418 --hi;
5419 }
Bram Moolenaarebc3de62020-05-26 11:08:28 +02005420 }
5421 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02005422 }
5423 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005424}
5425
5426/*
5427 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
5428 * Return 2 if "p" starts with "s:".
5429 * Return 0 otherwise.
5430 */
5431 int
5432eval_fname_script(char_u *p)
5433{
Bram Moolenaare38eab22019-12-05 21:50:01 +01005434 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
5435 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005436 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
5437 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
5438 return 5;
5439 if (p[0] == 's' && p[1] == ':')
5440 return 2;
5441 return 0;
5442}
5443
5444 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005445translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005446{
5447 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005448 return has_internal_func(name);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005449 return find_func(name, is_global) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005450}
5451
5452/*
5453 * Return TRUE when "ufunc" has old-style "..." varargs
5454 * or named varargs "...name: type".
5455 */
5456 int
5457has_varargs(ufunc_T *ufunc)
5458{
5459 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005460}
5461
5462/*
5463 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005464 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005465 */
5466 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005467function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005468{
5469 char_u *nm = name;
5470 char_u *p;
5471 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005472 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005473 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005474
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02005475 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
5476 if (no_deref)
5477 flag |= TFN_NO_DEREF;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005478 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005479 nm = skipwhite(nm);
5480
Bram Moolenaare38eab22019-12-05 21:50:01 +01005481 // Only accept "funcname", "funcname ", "funcname (..." and
5482 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005483 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005484 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005485 vim_free(p);
5486 return n;
5487}
5488
Bram Moolenaar113e1072019-01-20 15:30:40 +01005489#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005490 char_u *
5491get_expanded_name(char_u *name, int check)
5492{
5493 char_u *nm = name;
5494 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005495 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005496
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005497 p = trans_function_name(&nm, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005498 TFN_INT|TFN_QUIET, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005499
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005500 if (p != NULL && *nm == NUL
5501 && (!check || translated_function_exists(p, is_global)))
5502 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005503
5504 vim_free(p);
5505 return NULL;
5506}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005507#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005508
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005509/*
5510 * Function given to ExpandGeneric() to obtain the list of user defined
5511 * function names.
5512 */
5513 char_u *
5514get_user_func_name(expand_T *xp, int idx)
5515{
5516 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005517 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005518 static hashitem_T *hi;
5519 ufunc_T *fp;
5520
5521 if (idx == 0)
5522 {
5523 done = 0;
5524 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005525 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005526 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02005527 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005528 {
5529 if (done++ > 0)
5530 ++hi;
5531 while (HASHITEM_EMPTY(hi))
5532 ++hi;
5533 fp = HI2UF(hi);
5534
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005535 // don't show dead, dict and lambda functions
5536 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02005537 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005538 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005539
5540 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005541 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005542
5543 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02005544 if (xp->xp_context != EXPAND_USER_FUNC
5545 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005546 {
5547 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005548 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005549 STRCAT(IObuff, ")");
5550 }
5551 return IObuff;
5552 }
5553 return NULL;
5554}
5555
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005556/*
Bram Moolenaar83677162023-01-08 19:54:10 +00005557 * Make a copy of a function.
5558 * Intended to be used for a function defined on a base class that has a copy
5559 * on the child class.
5560 * The copy has uf_refcount set to one.
5561 * Returns NULL when out of memory.
5562 */
5563 ufunc_T *
5564copy_function(ufunc_T *fp)
5565{
Bram Moolenaare01e5212023-01-08 20:31:18 +00005566 ufunc_T *ufunc = alloc_ufunc(fp->uf_name);
Bram Moolenaar83677162023-01-08 19:54:10 +00005567 if (ufunc == NULL)
5568 return NULL;
5569
5570 // Most things can just be copied.
5571 *ufunc = *fp;
5572
5573 ufunc->uf_def_status = UF_TO_BE_COMPILED;
5574 ufunc->uf_dfunc_idx = 0;
5575 ufunc->uf_class = NULL;
5576
5577 ga_copy_strings(&fp->uf_args, &ufunc->uf_args);
5578 ga_copy_strings(&fp->uf_def_args, &ufunc->uf_def_args);
5579
5580 if (ufunc->uf_arg_types != NULL)
5581 {
5582 // "uf_arg_types" is an allocated array, make a copy.
5583 type_T **at = ALLOC_CLEAR_MULT(type_T *, ufunc->uf_args.ga_len);
5584 if (at != NULL)
5585 {
5586 mch_memmove(at, ufunc->uf_arg_types,
5587 sizeof(type_T *) * ufunc->uf_args.ga_len);
5588 ufunc->uf_arg_types = at;
5589 }
5590 }
5591
5592 // TODO: how about the types themselves? they can be freed when the
5593 // original function is freed:
5594 // type_T **uf_arg_types;
5595 // type_T *uf_ret_type;
5596
5597 ufunc->uf_type_list.ga_len = 0;
5598 ufunc->uf_type_list.ga_data = NULL;
5599
5600 // TODO: partial_T *uf_partial;
5601
5602 if (ufunc->uf_va_name != NULL)
5603 ufunc->uf_va_name = vim_strsave(ufunc->uf_va_name);
5604
5605 // TODO:
5606 // type_T *uf_va_type;
5607 // type_T *uf_func_type;
5608
5609 ufunc->uf_block_depth = 0;
5610 ufunc->uf_block_ids = NULL;
5611
5612 ga_copy_strings(&fp->uf_lines, &ufunc->uf_lines);
5613
5614 ufunc->uf_refcount = 1;
5615 ufunc->uf_name_exp = NULL;
5616 STRCPY(ufunc->uf_name, fp->uf_name);
5617
5618 return ufunc;
5619}
5620
5621/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005622 * ":delfunction {name}"
5623 */
5624 void
5625ex_delfunction(exarg_T *eap)
5626{
5627 ufunc_T *fp = NULL;
5628 char_u *p;
5629 char_u *name;
5630 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005631 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005632
5633 p = eap->arg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005634 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi,
5635 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005636 vim_free(fudi.fd_newkey);
5637 if (name == NULL)
5638 {
5639 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaara6f79292022-01-04 21:30:47 +00005640 emsg(_(e_funcref_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005641 return;
5642 }
5643 if (!ends_excmd(*skipwhite(p)))
5644 {
5645 vim_free(name);
Bram Moolenaar74409f62022-01-01 15:58:22 +00005646 semsg(_(e_trailing_characters_str), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005647 return;
5648 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02005649 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005650 if (eap->nextcmd != NULL)
5651 *p = NUL;
5652
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005653 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005654 {
5655 if (!eap->skip)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005656 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaarddfc0512021-09-06 20:56:56 +02005657 vim_free(name);
5658 return;
5659 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005660 if (!eap->skip)
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005661 fp = find_func(name, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005662 vim_free(name);
5663
5664 if (!eap->skip)
5665 {
5666 if (fp == NULL)
5667 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02005668 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00005669 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005670 return;
5671 }
5672 if (fp->uf_calls > 0)
5673 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005674 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005675 return;
5676 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005677 if (fp->uf_flags & FC_VIM9)
5678 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005679 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005680 return;
5681 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005682
5683 if (fudi.fd_dict != NULL)
5684 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005685 // Delete the dict item that refers to the function, it will
5686 // invoke func_unref() and possibly delete the function.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00005687 dictitem_remove(fudi.fd_dict, fudi.fd_di, "delfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005688 }
5689 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005690 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005691 // A normal function (not a numbered function or lambda) has a
5692 // refcount of 1 for the entry in the hashtable. When deleting
5693 // it and the refcount is more than one, it should be kept.
5694 // A numbered function and lambda should be kept if the refcount is
5695 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005696 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005697 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005698 // Function is still referenced somewhere. Don't free it but
5699 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005700 if (func_remove(fp))
5701 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005702 }
5703 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005704 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005705 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005706 }
5707}
5708
5709/*
5710 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005711 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005712 */
5713 void
5714func_unref(char_u *name)
5715{
Bram Moolenaar97baee82016-07-26 20:46:08 +02005716 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005717
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005718 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005719 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005720 fp = find_func(name, FALSE);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005721 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005722 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005723#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005724 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005725#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01005726 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005727 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005728 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005729}
5730
5731/*
5732 * Unreference a Function: decrement the reference count and free it when it
5733 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005734 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005735 */
5736 void
5737func_ptr_unref(ufunc_T *fp)
5738{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01005739 if (fp != NULL && (--fp->uf_refcount <= 0
5740 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
5741 && fp->uf_partial->pt_refcount <= 1
5742 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02005743 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005744 // Only delete it when it's not being used. Otherwise it's done
5745 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02005746 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01005747 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005748 }
5749}
5750
5751/*
5752 * Count a reference to a Function.
5753 */
5754 void
5755func_ref(char_u *name)
5756{
5757 ufunc_T *fp;
5758
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02005759 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005760 return;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00005761 fp = find_func(name, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005762 if (fp != NULL)
5763 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00005764 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01005765 // Only give an error for a numbered function.
5766 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01005767 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005768}
5769
5770/*
5771 * Count a reference to a Function.
5772 */
5773 void
5774func_ptr_ref(ufunc_T *fp)
5775{
5776 if (fp != NULL)
5777 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005778}
5779
5780/*
5781 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5782 * referenced from anywhere that is in use.
5783 */
5784 static int
5785can_free_funccal(funccall_T *fc, int copyID)
5786{
Bram Moolenaarca16c602022-09-06 18:57:08 +01005787 return (fc->fc_l_varlist.lv_copyID != copyID
5788 && fc->fc_l_vars.dv_copyID != copyID
5789 && fc->fc_l_avars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005790 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005791}
5792
5793/*
5794 * ":return [expr]"
5795 */
5796 void
5797ex_return(exarg_T *eap)
5798{
5799 char_u *arg = eap->arg;
5800 typval_T rettv;
5801 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005802 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005803
5804 if (current_funccal == NULL)
5805 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005806 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005807 return;
5808 }
5809
Bram Moolenaar844fb642021-10-23 13:32:30 +01005810 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005811 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5812
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005813 if (eap->skip)
5814 ++emsg_skip;
5815
5816 eap->nextcmd = NULL;
5817 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005818 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005819 {
5820 if (!eap->skip)
5821 returning = do_return(eap, FALSE, TRUE, &rettv);
5822 else
5823 clear_tv(&rettv);
5824 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005825 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005826 else if (!eap->skip)
5827 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005828 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005829 update_force_abort();
5830
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005831 /*
5832 * Return unless the expression evaluation has been cancelled due to an
5833 * aborting error, an interrupt, or an exception.
5834 */
5835 if (!aborting())
5836 returning = do_return(eap, FALSE, TRUE, NULL);
5837 }
5838
Bram Moolenaare38eab22019-12-05 21:50:01 +01005839 // When skipping or the return gets pending, advance to the next command
5840 // in this line (!returning). Otherwise, ignore the rest of the line.
5841 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005842 if (returning)
5843 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005844 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02005845 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005846
5847 if (eap->skip)
5848 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02005849 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005850}
5851
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005852 static int
5853ex_call_inner(
5854 exarg_T *eap,
5855 char_u *name,
5856 char_u **arg,
5857 char_u *startarg,
5858 funcexe_T *funcexe_init,
5859 evalarg_T *evalarg)
5860{
5861 linenr_T lnum;
5862 int doesrange;
5863 typval_T rettv;
5864 int failed = FALSE;
5865
5866 /*
5867 * When skipping, evaluate the function once, to find the end of the
5868 * arguments.
5869 * When the function takes a range, this is discovered after the first
5870 * call, and the loop is broken.
5871 */
5872 if (eap->skip)
5873 {
5874 ++emsg_skip;
5875 lnum = eap->line2; // do it once, also with an invalid range
5876 }
5877 else
5878 lnum = eap->line1;
5879 for ( ; lnum <= eap->line2; ++lnum)
5880 {
5881 funcexe_T funcexe;
5882
5883 if (!eap->skip && eap->addr_count > 0)
5884 {
5885 if (lnum > curbuf->b_ml.ml_line_count)
5886 {
5887 // If the function deleted lines or switched to another buffer
5888 // the line number may become invalid.
5889 emsg(_(e_invalid_range));
5890 break;
5891 }
5892 curwin->w_cursor.lnum = lnum;
5893 curwin->w_cursor.col = 0;
5894 curwin->w_cursor.coladd = 0;
5895 }
5896 *arg = startarg;
5897
5898 funcexe = *funcexe_init;
5899 funcexe.fe_doesrange = &doesrange;
5900 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
5901 if (get_func_tv(name, -1, &rettv, arg, evalarg, &funcexe) == FAIL)
5902 {
5903 failed = TRUE;
5904 break;
5905 }
5906 if (has_watchexpr())
5907 dbg_check_breakpoint(eap);
5908
5909 // Handle a function returning a Funcref, Dictionary or List.
5910 if (handle_subscript(arg, NULL, &rettv,
5911 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
5912 {
5913 failed = TRUE;
5914 break;
5915 }
5916
5917 clear_tv(&rettv);
5918 if (doesrange || eap->skip)
5919 break;
5920
5921 // Stop when immediately aborting on error, or when an interrupt
5922 // occurred or an exception was thrown but not caught.
5923 // get_func_tv() returned OK, so that the check for trailing
5924 // characters below is executed.
5925 if (aborting())
5926 break;
5927 }
5928 if (eap->skip)
5929 --emsg_skip;
5930 return failed;
5931}
5932
5933/*
5934 * Core part of ":defer func(arg)". "arg" points to the "(" and is advanced.
5935 * Returns FAIL or OK.
5936 */
5937 static int
Bram Moolenaar86d87252022-09-05 21:21:25 +01005938ex_defer_inner(
5939 char_u *name,
5940 char_u **arg,
Bram Moolenaar16900322022-09-08 19:51:45 +01005941 type_T *type,
Bram Moolenaar86d87252022-09-05 21:21:25 +01005942 partial_T *partial,
5943 evalarg_T *evalarg)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005944{
5945 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
Bram Moolenaar86d87252022-09-05 21:21:25 +01005946 int partial_argc = 0; // number of partial arguments
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005947 int argcount = 0; // number of arguments found
Bram Moolenaar86d87252022-09-05 21:21:25 +01005948 int r;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005949
5950 if (current_funccal == NULL)
5951 {
5952 semsg(_(e_str_not_inside_function), "defer");
5953 return FAIL;
5954 }
Bram Moolenaar86d87252022-09-05 21:21:25 +01005955 if (partial != NULL)
5956 {
5957 if (partial->pt_dict != NULL)
5958 {
5959 emsg(_(e_cannot_use_partial_with_dictionary_for_defer));
5960 return FAIL;
5961 }
5962 if (partial->pt_argc > 0)
5963 {
5964 int i;
5965
5966 partial_argc = partial->pt_argc;
5967 for (i = 0; i < partial_argc; ++i)
5968 copy_tv(&partial->pt_argv[i], &argvars[i]);
5969 }
5970 }
5971 r = get_func_arguments(arg, evalarg, FALSE,
5972 argvars + partial_argc, &argcount);
5973 argcount += partial_argc;
Bram Moolenaar16900322022-09-08 19:51:45 +01005974
5975 if (r == OK)
5976 {
5977 if (type != NULL)
5978 {
5979 // Check that the arguments are OK for the types of the funcref.
5980 r = check_argument_types(type, argvars, argcount, NULL, name);
5981 }
5982 else if (builtin_function(name, -1))
5983 {
5984 int idx = find_internal_func(name);
5985
5986 if (idx < 0)
5987 {
5988 emsg_funcname(e_unknown_function_str, name);
5989 r = FAIL;
5990 }
5991 else if (check_internal_func(idx, argcount) == -1)
5992 r = FAIL;
5993 }
5994 else
5995 {
5996 ufunc_T *ufunc = find_func(name, FALSE);
5997
5998 // we tolerate an unknown function here, it might be defined later
5999 if (ufunc != NULL)
6000 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00006001 funcerror_T error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar16900322022-09-08 19:51:45 +01006002 if (error != FCERR_UNKNOWN)
6003 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01006004 user_func_error(error, name, FALSE);
Bram Moolenaar16900322022-09-08 19:51:45 +01006005 r = FAIL;
6006 }
6007 }
6008 }
6009 }
6010
Bram Moolenaar86d87252022-09-05 21:21:25 +01006011 if (r == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01006012 {
6013 while (--argcount >= 0)
6014 clear_tv(&argvars[argcount]);
6015 return FAIL;
6016 }
6017 return add_defer(name, argcount, argvars);
6018}
6019
6020/*
Bram Moolenaar6f14da12022-09-07 21:30:44 +01006021 * Return TRUE if currently inside a function call.
6022 * Give an error message and return FALSE when not.
6023 */
6024 int
6025can_add_defer(void)
6026{
6027 if (!in_def_function() && get_current_funccal() == NULL)
6028 {
6029 semsg(_(e_str_not_inside_function), "defer");
6030 return FALSE;
6031 }
6032 return TRUE;
6033}
6034
6035/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01006036 * Add a deferred call for "name" with arguments "argvars[argcount]".
6037 * Consumes "argvars[]".
6038 * Caller must check that in_def_function() returns TRUE or current_funccal is
6039 * not NULL.
6040 * Returns OK or FAIL.
6041 */
6042 int
6043add_defer(char_u *name, int argcount_arg, typval_T *argvars)
6044{
6045 char_u *saved_name = vim_strsave(name);
6046 int argcount = argcount_arg;
6047 defer_T *dr;
6048 int ret = FAIL;
6049
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006050 if (saved_name == NULL)
6051 goto theend;
Bram Moolenaar806a2732022-09-04 15:40:36 +01006052 if (in_def_function())
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006053 {
Bram Moolenaar806a2732022-09-04 15:40:36 +01006054 if (add_defer_function(saved_name, argcount, argvars) == OK)
6055 argcount = 0;
6056 }
6057 else
6058 {
6059 if (current_funccal->fc_defer.ga_itemsize == 0)
6060 ga_init2(&current_funccal->fc_defer, sizeof(defer_T), 10);
6061 if (ga_grow(&current_funccal->fc_defer, 1) == FAIL)
6062 goto theend;
6063 dr = ((defer_T *)current_funccal->fc_defer.ga_data)
6064 + current_funccal->fc_defer.ga_len++;
6065 dr->dr_name = saved_name;
6066 dr->dr_argcount = argcount;
6067 while (argcount > 0)
6068 {
6069 --argcount;
6070 dr->dr_argvars[argcount] = argvars[argcount];
6071 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006072 }
6073 ret = OK;
6074
6075theend:
6076 while (--argcount >= 0)
6077 clear_tv(&argvars[argcount]);
6078 return ret;
6079}
6080
6081/*
6082 * Invoked after a functions has finished: invoke ":defer" functions.
6083 */
Bram Moolenaar58779852022-09-06 18:31:14 +01006084 static void
6085handle_defer_one(funccall_T *funccal)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006086{
6087 int idx;
6088
Bram Moolenaar58779852022-09-06 18:31:14 +01006089 for (idx = funccal->fc_defer.ga_len - 1; idx >= 0; --idx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006090 {
6091 funcexe_T funcexe;
6092 typval_T rettv;
Bram Moolenaar58779852022-09-06 18:31:14 +01006093 defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006094 int i;
6095
6096 CLEAR_FIELD(funcexe);
6097 funcexe.fe_evaluate = TRUE;
6098
6099 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
6100 call_func(dr->dr_name, -1, &rettv,
6101 dr->dr_argcount, dr->dr_argvars, &funcexe);
6102 clear_tv(&rettv);
6103 vim_free(dr->dr_name);
6104 for (i = dr->dr_argcount - 1; i >= 0; --i)
6105 clear_tv(&dr->dr_argvars[i]);
6106 }
Bram Moolenaar58779852022-09-06 18:31:14 +01006107 ga_clear(&funccal->fc_defer);
6108}
6109
6110/*
6111 * Called when exiting: call all defer functions.
6112 */
6113 void
6114invoke_all_defer(void)
6115{
6116 funccall_T *funccal;
6117
Bram Moolenaarca16c602022-09-06 18:57:08 +01006118 for (funccal = current_funccal; funccal != NULL;
6119 funccal = funccal->fc_caller)
Bram Moolenaar58779852022-09-06 18:31:14 +01006120 if (funccal->fc_ectx != NULL)
6121 {
6122 // :def function
6123 unwind_def_callstack(funccal->fc_ectx);
6124 may_invoke_defer_funcs(funccal->fc_ectx);
6125 }
6126 else
6127 {
6128 // legacy function
6129 handle_defer_one(funccal);
6130 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006131}
6132
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006133/*
6134 * ":1,25call func(arg1, arg2)" function call.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006135 * ":defer func(arg1, arg2)" deferred function call.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006136 */
6137 void
6138ex_call(exarg_T *eap)
6139{
6140 char_u *arg = eap->arg;
6141 char_u *startarg;
6142 char_u *name;
6143 char_u *tofree;
6144 int len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006145 int failed = FALSE;
6146 funcdict_T fudi;
6147 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006148 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006149 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006150 int found_var = FALSE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00006151 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006152
Bram Moolenaare6b53242020-07-01 17:28:33 +02006153 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006154 if (eap->skip)
6155 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006156 typval_T rettv;
6157
Bram Moolenaare38eab22019-12-05 21:50:01 +01006158 // trans_function_name() doesn't work well when skipping, use eval0()
6159 // instead to skip to any following command, e.g. for:
6160 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006161 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006162 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006163 clear_tv(&rettv);
6164 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02006165 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006166 return;
6167 }
6168
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006169 tofree = trans_function_name(&arg, NULL, eap->skip, TFN_INT,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006170 &fudi, &partial, vim9script ? &type : NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006171 if (fudi.fd_newkey != NULL)
6172 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006173 // Still need to give an error message for missing key.
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00006174 semsg(_(e_key_not_present_in_dictionary_str), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006175 vim_free(fudi.fd_newkey);
6176 }
6177 if (tofree == NULL)
6178 return;
6179
Bram Moolenaare38eab22019-12-05 21:50:01 +01006180 // Increase refcount on dictionary, it could get deleted when evaluating
6181 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006182 if (fudi.fd_dict != NULL)
6183 ++fudi.fd_dict->dv_refcount;
6184
Bram Moolenaare38eab22019-12-05 21:50:01 +01006185 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
6186 // contents. For VAR_PARTIAL get its partial, unless we already have one
6187 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006188 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006189 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar4525a572022-02-13 11:57:33 +00006190 vim9script && type == NULL ? &type : NULL,
Bram Moolenaar937610b2022-01-19 17:21:29 +00006191 FALSE, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006192
Bram Moolenaare38eab22019-12-05 21:50:01 +01006193 // Skip white space to allow ":call func ()". Not good, but required for
6194 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006195 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006196 if (*startarg != '(')
6197 {
Bram Moolenaare1242042021-12-16 20:56:57 +00006198 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006199 goto end;
6200 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00006201 if (vim9script && startarg > arg)
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02006202 {
6203 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
6204 goto end;
6205 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006206
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006207 if (eap->cmdidx == CMD_defer)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006208 {
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006209 arg = startarg;
Bram Moolenaar16900322022-09-08 19:51:45 +01006210 failed = ex_defer_inner(name, &arg, type, partial, &evalarg) == FAIL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006211 }
6212 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006213 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02006214 funcexe_T funcexe;
6215
Bram Moolenaara80faa82020-04-12 19:37:17 +02006216 CLEAR_FIELD(funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006217 funcexe.fe_check_type = type;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00006218 funcexe.fe_partial = partial;
6219 funcexe.fe_selfdict = fudi.fd_dict;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006220 funcexe.fe_firstline = eap->line1;
6221 funcexe.fe_lastline = eap->line2;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00006222 funcexe.fe_found_var = found_var;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006223 funcexe.fe_evaluate = !eap->skip;
6224 failed = ex_call_inner(eap, name, &arg, startarg, &funcexe, &evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006225 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006226
Bram Moolenaar1d341892021-09-18 15:25:52 +02006227 // When inside :try we need to check for following "| catch" or "| endtry".
6228 // Not when there was an error, but do check if an exception was thrown.
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006229 if ((!aborting() || did_throw) && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006230 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006231 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02006232 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02006233 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006234 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02006235 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006236 {
6237 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00006238 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01006239 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006240 }
6241 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02006242 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006243 }
Bram Moolenaara929c922022-04-18 15:21:17 +01006244 // Must be after using "arg", it may point into memory cleared here.
6245 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006246
6247end:
6248 dict_unref(fudi.fd_dict);
6249 vim_free(tofree);
6250}
6251
6252/*
6253 * Return from a function. Possibly makes the return pending. Also called
6254 * for a pending return at the ":endtry" or after returning from an extra
6255 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
6256 * when called due to a ":return" command. "rettv" may point to a typval_T
6257 * with the return rettv. Returns TRUE when the return can be carried out,
6258 * FALSE when the return gets pending.
6259 */
6260 int
6261do_return(
6262 exarg_T *eap,
6263 int reanimate,
6264 int is_cmd,
6265 void *rettv)
6266{
6267 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01006268 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006269
6270 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006271 // Undo the return.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006272 current_funccal->fc_returned = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006273
6274 /*
6275 * Cleanup (and inactivate) conditionals, but stop when a try conditional
6276 * not in its finally clause (which then is to be executed next) is found.
6277 * In this case, make the ":return" pending for execution at the ":endtry".
6278 * Otherwise, return normally.
6279 */
6280 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
6281 if (idx >= 0)
6282 {
6283 cstack->cs_pending[idx] = CSTP_RETURN;
6284
6285 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006286 // A pending return again gets pending. "rettv" points to an
6287 // allocated variable with the rettv of the original ":return"'s
6288 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006289 cstack->cs_rettv[idx] = rettv;
6290 else
6291 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006292 // When undoing a return in order to make it pending, get the stored
6293 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006294 if (reanimate)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006295 rettv = current_funccal->fc_rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006296
6297 if (rettv != NULL)
6298 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006299 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006300 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
6301 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
6302 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02006303 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006304 }
6305 else
6306 cstack->cs_rettv[idx] = NULL;
6307
6308 if (reanimate)
6309 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006310 // The pending return value could be overwritten by a ":return"
6311 // without argument in a finally clause; reset the default
6312 // return value.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006313 current_funccal->fc_rettv->v_type = VAR_NUMBER;
6314 current_funccal->fc_rettv->vval.v_number = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006315 }
6316 }
6317 report_make_pending(CSTP_RETURN, rettv);
6318 }
6319 else
6320 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006321 current_funccal->fc_returned = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006322
Bram Moolenaare38eab22019-12-05 21:50:01 +01006323 // If the return is carried out now, store the return value. For
6324 // a return immediately after reanimation, the value is already
6325 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006326 if (!reanimate && rettv != NULL)
6327 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006328 clear_tv(current_funccal->fc_rettv);
6329 *current_funccal->fc_rettv = *(typval_T *)rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006330 if (!is_cmd)
6331 vim_free(rettv);
6332 }
6333 }
6334
6335 return idx < 0;
6336}
6337
6338/*
6339 * Free the variable with a pending return value.
6340 */
6341 void
6342discard_pending_return(void *rettv)
6343{
6344 free_tv((typval_T *)rettv);
6345}
6346
6347/*
6348 * Generate a return command for producing the value of "rettv". The result
6349 * is an allocated string. Used by report_pending() for verbose messages.
6350 */
6351 char_u *
6352get_return_cmd(void *rettv)
6353{
6354 char_u *s = NULL;
6355 char_u *tofree = NULL;
6356 char_u numbuf[NUMBUFLEN];
6357
6358 if (rettv != NULL)
6359 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
6360 if (s == NULL)
6361 s = (char_u *)"";
6362
6363 STRCPY(IObuff, ":return ");
6364 STRNCPY(IObuff + 8, s, IOSIZE - 8);
6365 if (STRLEN(s) + 8 >= IOSIZE)
6366 STRCPY(IObuff + IOSIZE - 4, "...");
6367 vim_free(tofree);
6368 return vim_strsave(IObuff);
6369}
6370
6371/*
6372 * Get next function line.
6373 * Called by do_cmdline() to get the next line.
6374 * Returns allocated string, or NULL for end of function.
6375 */
6376 char_u *
6377get_func_line(
6378 int c UNUSED,
6379 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02006380 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02006381 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006382{
6383 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006384 ufunc_T *fp = fcp->fc_func;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006385 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01006386 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006387
Bram Moolenaare38eab22019-12-05 21:50:01 +01006388 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006389 if (fcp->fc_dbg_tick != debug_tick)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006390 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006391 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006392 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006393 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006394 }
6395#ifdef FEAT_PROFILE
6396 if (do_profiling == PROF_YES)
6397 func_line_end(cookie);
6398#endif
6399
6400 gap = &fp->uf_lines;
6401 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaarca16c602022-09-06 18:57:08 +01006402 || fcp->fc_returned)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006403 retval = NULL;
6404 else
6405 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006406 // Skip NULL lines (continuation lines).
Bram Moolenaarca16c602022-09-06 18:57:08 +01006407 while (fcp->fc_linenr < gap->ga_len
6408 && ((char_u **)(gap->ga_data))[fcp->fc_linenr] == NULL)
6409 ++fcp->fc_linenr;
6410 if (fcp->fc_linenr >= gap->ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006411 retval = NULL;
6412 else
6413 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006414 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->fc_linenr++]);
6415 SOURCING_LNUM = fcp->fc_linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006416#ifdef FEAT_PROFILE
6417 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01006418 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006419#endif
6420 }
6421 }
6422
Bram Moolenaare38eab22019-12-05 21:50:01 +01006423 // Did we encounter a breakpoint?
Bram Moolenaarca16c602022-09-06 18:57:08 +01006424 if (fcp->fc_breakpoint != 0 && fcp->fc_breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006425 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006426 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01006427 // Find next breakpoint.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006428 fcp->fc_breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01006429 SOURCING_LNUM);
Bram Moolenaarca16c602022-09-06 18:57:08 +01006430 fcp->fc_dbg_tick = debug_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006431 }
6432
6433 return retval;
6434}
6435
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006436/*
6437 * Return TRUE if the currently active function should be ended, because a
6438 * return was encountered or an error occurred. Used inside a ":while".
6439 */
6440 int
6441func_has_ended(void *cookie)
6442{
6443 funccall_T *fcp = (funccall_T *)cookie;
6444
Bram Moolenaare38eab22019-12-05 21:50:01 +01006445 // Ignore the "abort" flag if the abortion behavior has been changed due to
6446 // an error inside a try conditional.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006447 return (((fcp->fc_func->uf_flags & FC_ABORT)
6448 && did_emsg && !aborted_in_try())
6449 || fcp->fc_returned);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006450}
6451
6452/*
6453 * return TRUE if cookie indicates a function which "abort"s on errors.
6454 */
6455 int
6456func_has_abort(
6457 void *cookie)
6458{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006459 return ((funccall_T *)cookie)->fc_func->uf_flags & FC_ABORT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006460}
6461
6462
6463/*
6464 * Turn "dict.Func" into a partial for "Func" bound to "dict".
6465 * Don't do this when "Func" is already a partial that was bound
6466 * explicitly (pt_auto is FALSE).
6467 * Changes "rettv" in-place.
6468 * Returns the updated "selfdict_in".
6469 */
6470 dict_T *
6471make_partial(dict_T *selfdict_in, typval_T *rettv)
6472{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006473 char_u *fname;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006474 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006475 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006476 dict_T *selfdict = selfdict_in;
6477
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006478 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial != NULL
6479 && rettv->vval.v_partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006480 fp = rettv->vval.v_partial->pt_func;
6481 else
6482 {
6483 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006484 : rettv->vval.v_partial == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006485 : rettv->vval.v_partial->pt_name;
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006486 if (fname == NULL)
6487 {
6488 // There is no point binding a dict to a NULL function, just create
6489 // a function reference.
6490 rettv->v_type = VAR_FUNC;
6491 rettv->vval.v_string = NULL;
6492 }
6493 else
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006494 {
6495 char_u *tofree = NULL;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006496 funcerror_T error;
Bram Moolenaar673bcb12022-03-08 16:52:24 +00006497
6498 // Translate "s:func" to the stored function name.
6499 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
6500 fp = find_func(fname, FALSE);
6501 vim_free(tofree);
6502 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006503 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006504
Bram Moolenaared0c62e2022-03-08 19:43:55 +00006505 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006506 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006507 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006508
6509 if (pt != NULL)
6510 {
6511 pt->pt_refcount = 1;
6512 pt->pt_dict = selfdict;
6513 pt->pt_auto = TRUE;
6514 selfdict = NULL;
6515 if (rettv->v_type == VAR_FUNC)
6516 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01006517 // Just a function: Take over the function name and use
6518 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006519 pt->pt_name = rettv->vval.v_string;
6520 }
6521 else
6522 {
6523 partial_T *ret_pt = rettv->vval.v_partial;
6524 int i;
6525
Bram Moolenaare38eab22019-12-05 21:50:01 +01006526 // Partial: copy the function name, use selfdict and copy
6527 // args. Can't take over name or args, the partial might
6528 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006529 if (ret_pt->pt_name != NULL)
6530 {
6531 pt->pt_name = vim_strsave(ret_pt->pt_name);
6532 func_ref(pt->pt_name);
6533 }
6534 else
6535 {
6536 pt->pt_func = ret_pt->pt_func;
6537 func_ptr_ref(pt->pt_func);
6538 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006539 if (ret_pt->pt_argc > 0)
6540 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006541 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006542 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006543 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006544 pt->pt_argc = 0;
6545 else
6546 {
6547 pt->pt_argc = ret_pt->pt_argc;
6548 for (i = 0; i < pt->pt_argc; i++)
6549 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
6550 }
6551 }
6552 partial_unref(ret_pt);
6553 }
6554 rettv->v_type = VAR_PARTIAL;
6555 rettv->vval.v_partial = pt;
6556 }
6557 }
6558 return selfdict;
6559}
6560
6561/*
6562 * Return the name of the executed function.
6563 */
6564 char_u *
6565func_name(void *cookie)
6566{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006567 return ((funccall_T *)cookie)->fc_func->uf_name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006568}
6569
6570/*
6571 * Return the address holding the next breakpoint line for a funccall cookie.
6572 */
6573 linenr_T *
6574func_breakpoint(void *cookie)
6575{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006576 return &((funccall_T *)cookie)->fc_breakpoint;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006577}
6578
6579/*
6580 * Return the address holding the debug tick for a funccall cookie.
6581 */
6582 int *
6583func_dbg_tick(void *cookie)
6584{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006585 return &((funccall_T *)cookie)->fc_dbg_tick;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006586}
6587
6588/*
6589 * Return the nesting level for a funccall cookie.
6590 */
6591 int
6592func_level(void *cookie)
6593{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006594 return ((funccall_T *)cookie)->fc_level;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006595}
6596
6597/*
6598 * Return TRUE when a function was ended by a ":return" command.
6599 */
6600 int
6601current_func_returned(void)
6602{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006603 return current_funccal->fc_returned;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006604}
6605
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006606 int
6607free_unref_funccal(int copyID, int testing)
6608{
6609 int did_free = FALSE;
6610 int did_free_funccal = FALSE;
6611 funccall_T *fc, **pfc;
6612
6613 for (pfc = &previous_funccal; *pfc != NULL; )
6614 {
6615 if (can_free_funccal(*pfc, copyID))
6616 {
6617 fc = *pfc;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006618 *pfc = fc->fc_caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01006619 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006620 did_free = TRUE;
6621 did_free_funccal = TRUE;
6622 }
6623 else
Bram Moolenaarca16c602022-09-06 18:57:08 +01006624 pfc = &(*pfc)->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006625 }
6626 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01006627 // When a funccal was freed some more items might be garbage
6628 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006629 (void)garbage_collect(testing);
6630
6631 return did_free;
6632}
6633
6634/*
Bram Moolenaarba209902016-08-24 22:06:38 +02006635 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006636 */
6637 static funccall_T *
6638get_funccal(void)
6639{
6640 int i;
6641 funccall_T *funccal;
6642 funccall_T *temp_funccal;
6643
6644 funccal = current_funccal;
6645 if (debug_backtrace_level > 0)
6646 {
6647 for (i = 0; i < debug_backtrace_level; i++)
6648 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006649 temp_funccal = funccal->fc_caller;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006650 if (temp_funccal)
6651 funccal = temp_funccal;
6652 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01006653 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006654 debug_backtrace_level = i;
6655 }
6656 }
6657 return funccal;
6658}
6659
6660/*
6661 * Return the hashtable used for local variables in the current funccal.
6662 * Return NULL if there is no current funccal.
6663 */
6664 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006665get_funccal_local_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006666{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006667 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006668 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006669 return &get_funccal()->fc_l_vars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006670}
6671
6672/*
6673 * Return the l: scope variable.
6674 * Return NULL if there is no current funccal.
6675 */
6676 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006677get_funccal_local_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006678{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006679 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006680 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006681 return &get_funccal()->fc_l_vars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006682}
6683
6684/*
6685 * Return the hashtable used for argument in the current funccal.
6686 * Return NULL if there is no current funccal.
6687 */
6688 hashtab_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006689get_funccal_args_ht(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006690{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006691 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006692 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006693 return &get_funccal()->fc_l_avars.dv_hashtab;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006694}
6695
6696/*
6697 * Return the a: scope variable.
6698 * Return NULL if there is no current funccal.
6699 */
6700 dictitem_T *
Yegappan Lakshmanana23a11b2023-02-21 14:27:41 +00006701get_funccal_args_var(void)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006702{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006703 if (current_funccal == NULL || current_funccal->fc_l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006704 return NULL;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006705 return &get_funccal()->fc_l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006706}
6707
6708/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006709 * List function variables, if there is a function.
6710 */
6711 void
6712list_func_vars(int *first)
6713{
Bram Moolenaarca16c602022-09-06 18:57:08 +01006714 if (current_funccal != NULL && current_funccal->fc_l_vars.dv_refcount > 0)
6715 list_hashtable_vars(&current_funccal->fc_l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01006716 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006717}
6718
6719/*
6720 * If "ht" is the hashtable for local variables in the current funccal, return
6721 * the dict that contains it.
6722 * Otherwise return NULL.
6723 */
6724 dict_T *
6725get_current_funccal_dict(hashtab_T *ht)
6726{
6727 if (current_funccal != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01006728 && ht == &current_funccal->fc_l_vars.dv_hashtab)
6729 return &current_funccal->fc_l_vars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006730 return NULL;
6731}
6732
6733/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006734 * Search hashitem in parent scope.
6735 */
6736 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006737find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006738{
6739 funccall_T *old_current_funccal = current_funccal;
6740 hashtab_T *ht;
6741 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006742 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006743
Bram Moolenaarca16c602022-09-06 18:57:08 +01006744 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006745 return NULL;
6746
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02006747 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaarca16c602022-09-06 18:57:08 +01006748 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02006749 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006750 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006751 ht = find_var_ht(name, &varname);
6752 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02006753 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006754 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02006755 if (!HASHITEM_EMPTY(hi))
6756 {
6757 *pht = ht;
6758 break;
6759 }
6760 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006761 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar58016442016-07-31 18:30:22 +02006762 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006763 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02006764 }
6765 current_funccal = old_current_funccal;
6766
6767 return hi;
6768}
6769
6770/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006771 * Search variable in parent scope.
6772 */
6773 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006774find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006775{
6776 dictitem_T *v = NULL;
6777 funccall_T *old_current_funccal = current_funccal;
6778 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006779 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006780
Bram Moolenaarca16c602022-09-06 18:57:08 +01006781 if (current_funccal == NULL || current_funccal->fc_func->uf_scoped == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006782 return NULL;
6783
Bram Moolenaare38eab22019-12-05 21:50:01 +01006784 // Search in parent scope which is possible to reference from lambda
Bram Moolenaarca16c602022-09-06 18:57:08 +01006785 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006786 while (current_funccal)
6787 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006788 ht = find_var_ht(name, &varname);
6789 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006790 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02006791 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006792 if (v != NULL)
6793 break;
6794 }
Bram Moolenaarca16c602022-09-06 18:57:08 +01006795 if (current_funccal == current_funccal->fc_func->uf_scoped)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006796 break;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006797 current_funccal = current_funccal->fc_func->uf_scoped;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006798 }
6799 current_funccal = old_current_funccal;
6800
6801 return v;
6802}
6803
6804/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006805 * Set "copyID + 1" in previous_funccal and callers.
6806 */
6807 int
6808set_ref_in_previous_funccal(int copyID)
6809{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006810 funccall_T *fc;
6811
Bram Moolenaarca16c602022-09-06 18:57:08 +01006812 for (fc = previous_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006813 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006814 fc->fc_copyID = copyID + 1;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006815 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID + 1, NULL)
6816 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID + 1, NULL)
6817 || set_ref_in_list_items(&fc->fc_l_varlist, copyID + 1, NULL))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006818 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006819 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006820 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006821}
6822
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006823 static int
6824set_ref_in_funccal(funccall_T *fc, int copyID)
6825{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006826 if (fc->fc_copyID != copyID)
6827 {
6828 fc->fc_copyID = copyID;
Bram Moolenaarca16c602022-09-06 18:57:08 +01006829 if (set_ref_in_ht(&fc->fc_l_vars.dv_hashtab, copyID, NULL)
6830 || set_ref_in_ht(&fc->fc_l_avars.dv_hashtab, copyID, NULL)
6831 || set_ref_in_list_items(&fc->fc_l_varlist, copyID, NULL)
6832 || set_ref_in_func(NULL, fc->fc_func, copyID))
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006833 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006834 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006835 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006836}
6837
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006838/*
6839 * Set "copyID" in all local vars and arguments in the call stack.
6840 */
6841 int
6842set_ref_in_call_stack(int copyID)
6843{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006844 funccall_T *fc;
6845 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006846
Bram Moolenaarca16c602022-09-06 18:57:08 +01006847 for (fc = current_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006848 if (set_ref_in_funccal(fc, copyID))
6849 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02006850
6851 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006852 for (entry = funccal_stack; entry != NULL; entry = entry->next)
Bram Moolenaarca16c602022-09-06 18:57:08 +01006853 for (fc = entry->top_funccal; fc != NULL; fc = fc->fc_caller)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006854 if (set_ref_in_funccal(fc, copyID))
6855 return TRUE;
6856 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006857}
6858
6859/*
6860 * Set "copyID" in all functions available by name.
6861 */
6862 int
6863set_ref_in_functions(int copyID)
6864{
6865 int todo;
6866 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006867 ufunc_T *fp;
6868
6869 todo = (int)func_hashtab.ht_used;
6870 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006871 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006872 if (!HASHITEM_EMPTY(hi))
6873 {
6874 --todo;
6875 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006876 if (!func_name_refcount(fp->uf_name)
6877 && set_ref_in_func(NULL, fp, copyID))
6878 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006879 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006880 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006881 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006882}
6883
6884/*
6885 * Set "copyID" in all function arguments.
6886 */
6887 int
6888set_ref_in_func_args(int copyID)
6889{
6890 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006891
6892 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02006893 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
6894 copyID, NULL, NULL))
6895 return TRUE;
6896 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006897}
6898
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006899/*
6900 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006901 * Returns TRUE if setting references failed somehow.
6902 */
6903 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006904set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006905{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006906 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006907 funccall_T *fc;
Bram Moolenaar0917e862023-02-18 14:42:44 +00006908 funcerror_T error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006909 char_u fname_buf[FLEN_FIXED + 1];
6910 char_u *tofree = NULL;
6911 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006912 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006913
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006914 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006915 return FALSE;
6916
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006917 if (fp_in == NULL)
6918 {
6919 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006920 fp = find_func(fname, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02006921 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006922 if (fp != NULL)
6923 {
Bram Moolenaarca16c602022-09-06 18:57:08 +01006924 for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006925 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006926 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02006927
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006928 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02006929 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02006930}
6931
Bram Moolenaare38eab22019-12-05 21:50:01 +01006932#endif // FEAT_EVAL