blob: 8f662917407530cba84167e8493a5bc441069471 [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 Moolenaara9b579f2016-07-17 18:29:19 +020032static char *e_funcdict = N_("E717: Dictionary entry already exists");
33static char *e_funcref = N_("E718: Funcref required");
Bram Moolenaara9b579f2016-07-17 18:29:19 +020034
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020035static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +010036static void func_clear(ufunc_T *fp, int force);
37static int func_free(ufunc_T *fp, int force);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020038
39 void
40func_init()
41{
42 hash_init(&func_hashtab);
43}
44
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020045/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020046 * Return the function hash table
47 */
48 hashtab_T *
49func_tbl_get(void)
50{
51 return &func_hashtab;
52}
53
54/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020055 * Get one function argument.
Bram Moolenaar51e7e782021-04-10 17:46:52 +020056 * If "argtypes" is not NULL also get the type: "arg: type" (:def function).
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010057 * If "types_optional" is TRUE a missing type is OK, use "any".
Bram Moolenaar057e84a2021-02-28 16:55:11 +010058 * If "evalarg" 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 Moolenaar2a389082021-04-09 20:24:31 +020069 int is_vararg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010070 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010071{
Bram Moolenaar6e949782020-04-13 17:21:00 +020072 char_u *p = arg;
73 char_u *arg_copy = NULL;
Bram Moolenaar51e7e782021-04-10 17:46:52 +020074 int is_underscore = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010075
76 while (ASCII_ISALNUM(*p) || *p == '_')
77 ++p;
78 if (arg == p || isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020079 || (argtypes == NULL
80 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
81 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082 {
83 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +000084 semsg(_(e_illegal_argument_str), arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085 return arg;
86 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010087
Bram Moolenaar057e84a2021-02-28 16:55:11 +010088 // Vim9 script: cannot use script var name for argument. In function: also
89 // check local vars and arguments.
90 if (!skip && argtypes != NULL && check_defined(arg, p - arg,
91 evalarg == NULL ? NULL : evalarg->eval_cctx, TRUE) == FAIL)
Bram Moolenaarb4893b82021-02-21 22:20:24 +010092 return arg;
Bram Moolenaarb4893b82021-02-21 22:20:24 +010093
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010094 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
95 return arg;
96 if (newargs != NULL)
97 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098 int c;
99 int i;
100
101 c = *p;
102 *p = NUL;
103 arg_copy = vim_strsave(arg);
104 if (arg_copy == NULL)
105 {
106 *p = c;
107 return arg;
108 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200109 is_underscore = arg_copy[0] == '_' && arg_copy[1] == NUL;
Bram Moolenaar87795932021-04-10 18:21:30 +0200110 if (argtypes == NULL || !is_underscore)
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200111 // Check for duplicate argument name.
112 for (i = 0; i < newargs->ga_len; ++i)
113 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
114 {
115 semsg(_("E853: Duplicate argument name: %s"), arg_copy);
116 vim_free(arg_copy);
117 return arg;
118 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100119 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
120 newargs->ga_len++;
121
122 *p = c;
123 }
124
125 // get any type from "arg: type"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100126 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100127 {
128 char_u *type = NULL;
129
Bram Moolenaar6e949782020-04-13 17:21:00 +0200130 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
131 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200132 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200133 arg_copy == NULL ? arg : arg_copy);
134 p = skipwhite(p);
135 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100136 if (*p == ':')
137 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200138 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100139 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200140 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100141 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200142 return arg;
143 }
144 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200145 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100146 if (!skip)
147 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100148 }
Bram Moolenaar51e7e782021-04-10 17:46:52 +0200149 else if (*skipwhite(p) != '=' && !types_optional && !is_underscore)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200150 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200151 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200152 arg_copy == NULL ? arg : arg_copy);
153 return arg;
154 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100155 if (!skip)
156 {
157 if (type == NULL && types_optional)
158 // lambda arguments default to "any" type
Bram Moolenaar2a389082021-04-09 20:24:31 +0200159 type = vim_strsave((char_u *)
160 (is_vararg ? "list<any>" : "any"));
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100161 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
162 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100163 }
164
165 return p;
166}
167
168/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200169 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100170 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100171 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200172 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100173 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200174get_function_args(
175 char_u **argp,
176 char_u endchar,
177 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100178 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100179 int types_optional, // types optional if "argtypes" is not NULL
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100180 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200181 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200182 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200183 int skip,
184 exarg_T *eap,
185 char_u **line_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200186{
187 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100188 char_u *arg;
189 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200190 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200191 int any_default = FALSE;
192 char_u *expr;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100193 char_u *whitep = *argp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200194
195 if (newargs != NULL)
196 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100197 if (argtypes != NULL)
198 ga_init2(argtypes, (int)sizeof(char_u *), 3);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200199 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200200 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200201
202 if (varargs != NULL)
203 *varargs = FALSE;
204
205 /*
206 * Isolate the arguments: "arg1, arg2, ...)"
207 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100208 arg = skipwhite(*argp);
209 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200210 while (*p != endchar)
211 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200212 while (eap != NULL && eap->getline != NULL
213 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200214 {
215 char_u *theline;
216
217 // End of the line, get the next one.
218 theline = eap->getline(':', eap->cookie, 0, TRUE);
219 if (theline == NULL)
220 break;
221 vim_free(*line_to_free);
222 *line_to_free = theline;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200223 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200224 p = skipwhite(theline);
225 }
226
227 if (mustend && *p != endchar)
228 {
229 if (!skip)
230 semsg(_(e_invarg2), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100231 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200232 }
233 if (*p == endchar)
234 break;
235
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200236 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
237 {
238 if (varargs != NULL)
239 *varargs = TRUE;
240 p += 3;
241 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100242
243 if (argtypes != NULL)
244 {
245 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200246 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100247 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100248 if (!skip)
249 emsg(_(e_missing_name_after_dots));
250 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100251 }
252
253 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100254 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaar2a389082021-04-09 20:24:31 +0200255 evalarg, TRUE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256 if (p == arg)
257 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100258 if (*skipwhite(p) == '=')
259 {
260 emsg(_(e_cannot_use_default_for_variable_arguments));
261 break;
262 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100263 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200264 }
265 else
266 {
Bram Moolenaar015cf102021-06-26 21:52:02 +0200267 char_u *np;
268
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200269 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100270 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaar2a389082021-04-09 20:24:31 +0200271 evalarg, FALSE, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100272 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200273 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200274
Bram Moolenaar015cf102021-06-26 21:52:02 +0200275 // Recognize " = expr" but not " == expr". A lambda can have
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200276 // "(a = expr" but "(a == expr" and "(a =~ expr" are not a lambda.
Bram Moolenaar015cf102021-06-26 21:52:02 +0200277 np = skipwhite(p);
Bram Moolenaar98f9a5f2021-06-26 22:22:38 +0200278 if (*np == '=' && np[1] != '=' && np[1] != '~'
279 && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200280 {
281 typval_T rettv;
282
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100283 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200284 any_default = TRUE;
285 p = skipwhite(p) + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200286 whitep = p;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200287 p = skipwhite(p);
288 expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200289 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200290 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200291 if (!skip)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200292 {
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200293 if (ga_grow(default_args, 1) == FAIL)
294 goto err_ret;
295
296 // trim trailing whitespace
297 while (p > expr && VIM_ISWHITE(p[-1]))
298 p--;
299 c = *p;
300 *p = NUL;
301 expr = vim_strsave(expr);
302 if (expr == NULL)
303 {
304 *p = c;
305 goto err_ret;
306 }
307 ((char_u **)(default_args->ga_data))
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200308 [default_args->ga_len] = expr;
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200309 default_args->ga_len++;
310 *p = c;
311 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200312 }
313 else
314 mustend = TRUE;
315 }
316 else if (any_default)
317 {
318 emsg(_("E989: Non-default argument follows default argument"));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200319 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200320 }
Bram Moolenaar86cdb8a2021-04-06 19:01:03 +0200321
322 if (VIM_ISWHITE(*p) && *skipwhite(p) == ',')
323 {
324 // Be tolerant when skipping
325 if (!skip)
326 {
327 semsg(_(e_no_white_space_allowed_before_str_str), ",", p);
328 goto err_ret;
329 }
330 p = skipwhite(p);
331 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200332 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200333 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200334 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200335 // Don't give this error when skipping, it makes the "->" not
336 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100337 // Allow missing space after comma in legacy functions.
338 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200339 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
340 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100341 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200342 goto err_ret;
343 }
344 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200345 else
346 mustend = TRUE;
347 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200348 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200349 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200350 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200351
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200352 if (*p != endchar)
353 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100354 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200355
356 *argp = p;
357 return OK;
358
359err_ret:
360 if (newargs != NULL)
361 ga_clear_strings(newargs);
Bram Moolenaare3ffaa62021-06-26 22:17:35 +0200362 if (!skip && default_args != NULL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200363 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200364 return FAIL;
365}
366
367/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100368 * Parse the argument types, filling "fp->uf_arg_types".
369 * Return OK or FAIL.
370 */
371 static int
372parse_argument_types(ufunc_T *fp, garray_T *argtypes, int varargs)
373{
Bram Moolenaar2a389082021-04-09 20:24:31 +0200374 int len = 0;
375
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100376 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
377 if (argtypes->ga_len > 0)
378 {
379 // When "varargs" is set the last name/type goes into uf_va_name
380 // and uf_va_type.
Bram Moolenaar2a389082021-04-09 20:24:31 +0200381 len = argtypes->ga_len - (varargs ? 1 : 0);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100382
383 if (len > 0)
384 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
385 if (fp->uf_arg_types != NULL)
386 {
387 int i;
388 type_T *type;
389
390 for (i = 0; i < len; ++ i)
391 {
392 char_u *p = ((char_u **)argtypes->ga_data)[i];
393
394 if (p == NULL)
395 // will get the type from the default value
396 type = &t_unknown;
397 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100398 type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100399 if (type == NULL)
400 return FAIL;
401 fp->uf_arg_types[i] = type;
402 }
403 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100404 }
Bram Moolenaar2a389082021-04-09 20:24:31 +0200405
406 if (varargs)
407 {
408 char_u *p;
409
410 // Move the last argument "...name: type" to uf_va_name and
411 // uf_va_type.
412 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
413 [fp->uf_args.ga_len - 1];
414 --fp->uf_args.ga_len;
415 p = ((char_u **)argtypes->ga_data)[len];
416 if (p == NULL)
417 // TODO: get type from default value
418 fp->uf_va_type = &t_list_any;
419 else
420 {
421 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
422 if (fp->uf_va_type != NULL && fp->uf_va_type->tt_type != VAR_LIST)
423 {
424 semsg(_(e_variable_arguments_type_must_be_list_str),
425 ((char_u **)argtypes->ga_data)[len]);
426 return FAIL;
427 }
428 }
429 if (fp->uf_va_type == NULL)
430 return FAIL;
431 }
432
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100433 return OK;
434}
435
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100436 static int
437parse_return_type(ufunc_T *fp, char_u *ret_type)
438{
439 if (ret_type == NULL)
440 fp->uf_ret_type = &t_void;
441 else
442 {
443 char_u *p = ret_type;
444
445 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
446 if (fp->uf_ret_type == NULL)
447 {
448 fp->uf_ret_type = &t_void;
449 return FAIL;
450 }
451 }
452 return OK;
453}
454
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100455/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200456 * Register function "fp" as using "current_funccal" as its scope.
457 */
458 static int
459register_closure(ufunc_T *fp)
460{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200461 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100462 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200463 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200464 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200465 fp->uf_scoped = current_funccal;
466 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200467
Bram Moolenaar58016442016-07-31 18:30:22 +0200468 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
469 return FAIL;
470 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
471 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200472 return OK;
473}
474
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100475 static void
476set_ufunc_name(ufunc_T *fp, char_u *name)
477{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100478 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
479 // actually extends beyond the struct.
480 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100481
482 if (name[0] == K_SPECIAL)
483 {
484 fp->uf_name_exp = alloc(STRLEN(name) + 3);
485 if (fp->uf_name_exp != NULL)
486 {
487 STRCPY(fp->uf_name_exp, "<SNR>");
488 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
489 }
490 }
491}
492
Bram Moolenaar58016442016-07-31 18:30:22 +0200493/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200494 * Get a name for a lambda. Returned in static memory.
495 */
496 char_u *
497get_lambda_name(void)
498{
499 static char_u name[30];
500 static int lambda_no = 0;
501
502 sprintf((char*)name, "<lambda>%d", ++lambda_no);
503 return name;
504}
505
Bram Moolenaar801ab062020-06-25 19:27:56 +0200506#if defined(FEAT_LUA) || defined(PROTO)
507/*
508 * Registers a native C callback which can be called from Vim script.
509 * Returns the name of the Vim script function.
510 */
511 char_u *
512register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
513{
514 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200515 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200516
517 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
518 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200519 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200520
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200521 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200522 fp->uf_refcount = 1;
523 fp->uf_varargs = TRUE;
Bram Moolenaar38453522021-11-28 22:00:12 +0000524 fp->uf_flags = FC_CFUNC | FC_LAMBDA;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200525 fp->uf_calls = 0;
526 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200527 fp->uf_cb = cb;
528 fp->uf_cb_free = cb_free;
529 fp->uf_cb_state = state;
530
531 set_ufunc_name(fp, name);
532 hash_add(&func_hashtab, UF2HIKEY(fp));
533
534 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200535}
536#endif
537
Bram Moolenaar04b12692020-05-04 23:24:44 +0200538/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100539 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100540 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100541 * If "white_error" is not NULL check for correct use of white space and set
542 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100543 * Return NULL if no valid arrow found.
544 */
545 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100546skip_arrow(
547 char_u *start,
548 int equal_arrow,
549 char_u **ret_type,
550 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100551{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100552 char_u *s = start;
553 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100554
555 if (equal_arrow)
556 {
557 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100558 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100559 if (white_error != NULL && !VIM_ISWHITE(s[1]))
560 {
561 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100562 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100563 return NULL;
564 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100565 s = skipwhite(s + 1);
566 *ret_type = s;
567 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100568 if (s == *ret_type)
569 {
570 emsg(_(e_missing_return_type));
571 return NULL;
572 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100573 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100574 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100575 s = skipwhite(s);
576 if (*s != '=')
577 return NULL;
578 ++s;
579 }
580 if (*s != '>')
581 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100582 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
583 || !IS_WHITE_OR_NUL(s[1])))
584 {
585 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100586 semsg(_(e_white_space_required_before_and_after_str_at_str),
587 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100588 return NULL;
589 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100590 return skipwhite(s + 1);
591}
592
593/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100594 * Check if "*cmd" points to a function command and if so advance "*cmd" and
595 * return TRUE.
596 * Otherwise return FALSE;
597 * Do not consider "function(" to be a command.
598 */
599 static int
600is_function_cmd(char_u **cmd)
601{
602 char_u *p = *cmd;
603
604 if (checkforcmd(&p, "function", 2))
605 {
606 if (*p == '(')
607 return FALSE;
608 *cmd = p;
609 return TRUE;
610 }
611 return FALSE;
612}
613
614/*
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200615 * Called when defining a function: The context may be needed for script
616 * variables declared in a block that is visible now but not when the function
617 * is compiled or called later.
618 */
619 static void
620function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
621{
622 if (cstack != NULL && cstack->cs_idx >= 0)
623 {
624 int count = cstack->cs_idx + 1;
625 int i;
626
627 fp->uf_block_ids = ALLOC_MULT(int, count);
628 if (fp->uf_block_ids != NULL)
629 {
630 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
631 sizeof(int) * count);
632 fp->uf_block_depth = count;
633 }
634
635 // Set flag in each block to indicate a function was defined. This
636 // is used to keep the variable when leaving the block, see
637 // hide_script_var().
638 for (i = 0; i <= cstack->cs_idx; ++i)
639 cstack->cs_flags[i] |= CSF_FUNC_DEF;
640 }
641}
642
643/*
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100644 * Read the body of a function, put every line in "newlines".
Bram Moolenaar074f84c2021-05-18 11:47:44 +0200645 * This stops at "}", "endfunction" or "enddef".
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100646 * "newlines" must already have been initialized.
647 * "eap->cmdidx" is CMD_function, CMD_def or CMD_block;
648 */
649 static int
650get_function_body(
651 exarg_T *eap,
652 garray_T *newlines,
653 char_u *line_arg_in,
654 char_u **line_to_free)
655{
656 linenr_T sourcing_lnum_top = SOURCING_LNUM;
657 linenr_T sourcing_lnum_off;
658 int saved_wait_return = need_wait_return;
659 char_u *line_arg = line_arg_in;
660 int vim9_function = eap->cmdidx == CMD_def
661 || eap->cmdidx == CMD_block;
662#define MAX_FUNC_NESTING 50
663 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200664 char nesting_inline[MAX_FUNC_NESTING];
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100665 int nesting = 0;
666 getline_opt_T getline_options;
667 int indent = 2;
668 char_u *skip_until = NULL;
669 int ret = FAIL;
670 int is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200671 int heredoc_concat_len = 0;
672 garray_T heredoc_ga;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100673 char_u *heredoc_trimmed = NULL;
674
Bram Moolenaar20677332021-06-06 17:02:53 +0200675 ga_init2(&heredoc_ga, 1, 500);
676
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100677 // Detect having skipped over comment lines to find the return
678 // type. Add NULL lines to keep the line count correct.
679 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
680 if (SOURCING_LNUM < sourcing_lnum_off)
681 {
682 sourcing_lnum_off -= SOURCING_LNUM;
683 if (ga_grow(newlines, sourcing_lnum_off) == FAIL)
684 goto theend;
685 while (sourcing_lnum_off-- > 0)
686 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
687 }
688
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200689 nesting_def[0] = vim9_function;
690 nesting_inline[0] = eap->cmdidx == CMD_block;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100691 getline_options = vim9_function
692 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
693 for (;;)
694 {
695 char_u *theline;
696 char_u *p;
697 char_u *arg;
698
699 if (KeyTyped)
700 {
701 msg_scroll = TRUE;
702 saved_wait_return = FALSE;
703 }
704 need_wait_return = FALSE;
705
706 if (line_arg != NULL)
707 {
708 // Use eap->arg, split up in parts by line breaks.
709 theline = line_arg;
710 p = vim_strchr(theline, '\n');
711 if (p == NULL)
712 line_arg += STRLEN(line_arg);
713 else
714 {
715 *p = NUL;
716 line_arg = p + 1;
717 }
718 }
719 else
720 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100721 if (eap->getline == NULL)
722 theline = getcmdline(':', 0L, indent, getline_options);
723 else
724 theline = eap->getline(':', eap->cookie, indent,
725 getline_options);
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +0000726 if (*eap->cmdlinep == *line_to_free)
727 *eap->cmdlinep = theline;
728 vim_free(*line_to_free);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100729 *line_to_free = theline;
730 }
731 if (KeyTyped)
732 lines_left = Rows - 1;
733 if (theline == NULL)
734 {
735 // Use the start of the function for the line number.
736 SOURCING_LNUM = sourcing_lnum_top;
737 if (skip_until != NULL)
738 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200739 else if (nesting_inline[nesting])
740 emsg(_(e_missing_end_block));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100741 else if (eap->cmdidx == CMD_def)
742 emsg(_(e_missing_enddef));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100743 else
Bram Moolenaarc553a212021-12-26 20:20:34 +0000744 emsg(_(e_missing_endfunction));
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100745 goto theend;
746 }
747
748 // Detect line continuation: SOURCING_LNUM increased more than one.
749 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
750 if (SOURCING_LNUM < sourcing_lnum_off)
751 sourcing_lnum_off -= SOURCING_LNUM;
752 else
753 sourcing_lnum_off = 0;
754
755 if (skip_until != NULL)
756 {
757 // Don't check for ":endfunc"/":enddef" between
758 // * ":append" and "."
759 // * ":python <<EOF" and "EOF"
760 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
761 if (heredoc_trimmed == NULL
762 || (is_heredoc && skipwhite(theline) == theline)
763 || STRNCMP(theline, heredoc_trimmed,
764 STRLEN(heredoc_trimmed)) == 0)
765 {
766 if (heredoc_trimmed == NULL)
767 p = theline;
768 else if (is_heredoc)
769 p = skipwhite(theline) == theline
770 ? theline : theline + STRLEN(heredoc_trimmed);
771 else
772 p = theline + STRLEN(heredoc_trimmed);
773 if (STRCMP(p, skip_until) == 0)
774 {
775 VIM_CLEAR(skip_until);
776 VIM_CLEAR(heredoc_trimmed);
777 getline_options = vim9_function
778 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
779 is_heredoc = FALSE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200780
781 if (heredoc_concat_len > 0)
782 {
783 // Replace the starting line with all the concatenated
784 // lines.
785 ga_concat(&heredoc_ga, theline);
786 vim_free(((char_u **)(newlines->ga_data))[
787 heredoc_concat_len - 1]);
788 ((char_u **)(newlines->ga_data))[
789 heredoc_concat_len - 1] = heredoc_ga.ga_data;
790 ga_init(&heredoc_ga);
791 heredoc_concat_len = 0;
792 theline += STRLEN(theline); // skip the "EOF"
793 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100794 }
795 }
796 }
797 else
798 {
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200799 int c;
800 char_u *end;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100801
802 // skip ':' and blanks
803 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
804 ;
805
806 // Check for "endfunction", "enddef" or "}".
807 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200808 if (nesting_inline[nesting]
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100809 ? *p == '}'
810 : (checkforcmd(&p, nesting_def[nesting]
811 ? "enddef" : "endfunction", 4)
812 && *p != ':'))
813 {
814 if (nesting-- == 0)
815 {
816 char_u *nextcmd = NULL;
817
818 if (*p == '|' || *p == '}')
819 nextcmd = p + 1;
820 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
821 nextcmd = line_arg;
822 else if (*p != NUL && *p != (vim9_function ? '#' : '"')
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100823 && (vim9_function || p_verbose > 0))
824 {
Bram Moolenaar4bba16d2021-08-15 19:28:05 +0200825 SOURCING_LNUM = sourcing_lnum_top
826 + newlines->ga_len + 1;
Bram Moolenaar49f1e9e2021-03-22 20:49:02 +0100827 if (eap->cmdidx == CMD_def)
828 semsg(_(e_text_found_after_enddef_str), p);
829 else
830 give_warning2((char_u *)
831 _("W22: Text found after :endfunction: %s"),
832 p, TRUE);
833 }
834 if (nextcmd != NULL && *skipwhite(nextcmd) != NUL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100835 {
836 // Another command follows. If the line came from "eap"
837 // we can simply point into it, otherwise we need to
838 // change "eap->cmdlinep".
839 eap->nextcmd = nextcmd;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +0000840 if (*line_to_free != NULL
841 && *eap->cmdlinep != *line_to_free)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100842 {
843 vim_free(*eap->cmdlinep);
844 *eap->cmdlinep = *line_to_free;
845 *line_to_free = NULL;
846 }
847 }
848 break;
849 }
850 }
851
852 // Check for mismatched "endfunc" or "enddef".
853 // We don't check for "def" inside "func" thus we also can't check
854 // for "enddef".
855 // We continue to find the end of the function, although we might
856 // not find it.
857 else if (nesting_def[nesting])
858 {
859 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
860 emsg(_(e_mismatched_endfunction));
861 }
862 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
863 emsg(_(e_mismatched_enddef));
864
865 // Increase indent inside "if", "while", "for" and "try", decrease
866 // at "end".
867 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
868 indent -= 2;
869 else if (STRNCMP(p, "if", 2) == 0
870 || STRNCMP(p, "wh", 2) == 0
871 || STRNCMP(p, "for", 3) == 0
872 || STRNCMP(p, "try", 3) == 0)
873 indent += 2;
874
875 // Check for defining a function inside this function.
876 // Only recognize "def" inside "def", not inside "function",
877 // For backwards compatibility, see Test_function_python().
878 c = *p;
879 if (is_function_cmd(&p)
880 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
881 {
882 if (*p == '!')
883 p = skipwhite(p + 1);
884 p += eval_fname_script(p);
885 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL,
886 NULL, NULL));
887 if (*skipwhite(p) == '(')
888 {
889 if (nesting == MAX_FUNC_NESTING - 1)
890 emsg(_(e_function_nesting_too_deep));
891 else
892 {
893 ++nesting;
894 nesting_def[nesting] = (c == 'd');
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200895 nesting_inline[nesting] = FALSE;
896 indent += 2;
897 }
898 }
899 }
900
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200901 if (nesting_def[nesting] ? *p != '#' : *p != '"')
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200902 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200903 // Not a comment line: check for nested inline function.
904 end = p + STRLEN(p) - 1;
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200905 while (end > p && VIM_ISWHITE(*end))
906 --end;
Bram Moolenaar13212572021-08-01 22:01:30 +0200907 if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200908 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200909 int is_block;
910
911 // check for trailing "=> {": start of an inline function
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200912 --end;
913 while (end > p && VIM_ISWHITE(*end))
914 --end;
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200915 is_block = end > p + 2 && end[-1] == '=' && end[0] == '>';
916 if (!is_block)
Bram Moolenaar5245beb2021-07-15 22:03:50 +0200917 {
Bram Moolenaare4db17f2021-08-01 21:19:43 +0200918 char_u *s = p;
919
920 // check for line starting with "au" for :autocmd or
921 // "com" for :command, these can use a {} block
922 is_block = checkforcmd_noparen(&s, "autocmd", 2)
923 || checkforcmd_noparen(&s, "command", 3);
924 }
925
926 if (is_block)
927 {
Bram Moolenaarac2cd2b2021-07-19 21:04:23 +0200928 if (nesting == MAX_FUNC_NESTING - 1)
929 emsg(_(e_function_nesting_too_deep));
930 else
931 {
932 ++nesting;
933 nesting_def[nesting] = TRUE;
934 nesting_inline[nesting] = TRUE;
935 indent += 2;
936 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100937 }
938 }
939 }
940
941 // Check for ":append", ":change", ":insert". Not for :def.
942 p = skip_range(p, FALSE, NULL);
943 if (!vim9_function
944 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
945 || (p[0] == 'c'
946 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
947 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
948 && (STRNCMP(&p[3], "nge", 3) != 0
949 || !ASCII_ISALPHA(p[6])))))))
950 || (p[0] == 'i'
951 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
952 && (!ASCII_ISALPHA(p[2])
953 || (p[2] == 's'
954 && (!ASCII_ISALPHA(p[3])
955 || p[3] == 'e'))))))))
956 skip_until = vim_strsave((char_u *)".");
957
958 // Check for ":python <<EOF", ":tcl <<EOF", etc.
959 arg = skipwhite(skiptowhite(p));
960 if (arg[0] == '<' && arg[1] =='<'
961 && ((p[0] == 'p' && p[1] == 'y'
962 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
963 || ((p[2] == '3' || p[2] == 'x')
964 && !ASCII_ISALPHA(p[3]))))
965 || (p[0] == 'p' && p[1] == 'e'
966 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
967 || (p[0] == 't' && p[1] == 'c'
968 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
969 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
970 && !ASCII_ISALPHA(p[3]))
971 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
972 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
973 || (p[0] == 'm' && p[1] == 'z'
974 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
975 ))
976 {
977 // ":python <<" continues until a dot, like ":append"
978 p = skipwhite(arg + 2);
979 if (STRNCMP(p, "trim", 4) == 0)
980 {
981 // Ignore leading white space.
982 p = skipwhite(p + 4);
983 heredoc_trimmed = vim_strnsave(theline,
984 skipwhite(theline) - theline);
985 }
986 if (*p == NUL)
987 skip_until = vim_strsave((char_u *)".");
988 else
989 skip_until = vim_strnsave(p, skiptowhite(p) - p);
990 getline_options = GETLINE_NONE;
991 is_heredoc = TRUE;
Bram Moolenaar20677332021-06-06 17:02:53 +0200992 if (eap->cmdidx == CMD_def)
993 heredoc_concat_len = newlines->ga_len + 1;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +0100994 }
995
996 // Check for ":cmd v =<< [trim] EOF"
997 // and ":cmd [a, b] =<< [trim] EOF"
998 // and "lines =<< [trim] EOF" for Vim9
999 // Where "cmd" can be "let", "var", "final" or "const".
1000 arg = skipwhite(skiptowhite(p));
1001 if (*arg == '[')
1002 arg = vim_strchr(arg, ']');
1003 if (arg != NULL)
1004 {
1005 int found = (eap->cmdidx == CMD_def && arg[0] == '='
1006 && arg[1] == '<' && arg[2] =='<');
1007
1008 if (!found)
1009 // skip over the argument after "cmd"
1010 arg = skipwhite(skiptowhite(arg));
1011 if (found || (arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
1012 && (checkforcmd(&p, "let", 2)
1013 || checkforcmd(&p, "var", 3)
1014 || checkforcmd(&p, "final", 5)
1015 || checkforcmd(&p, "const", 5))))
1016 {
1017 p = skipwhite(arg + 3);
1018 if (STRNCMP(p, "trim", 4) == 0)
1019 {
1020 // Ignore leading white space.
1021 p = skipwhite(p + 4);
1022 heredoc_trimmed = vim_strnsave(theline,
1023 skipwhite(theline) - theline);
1024 }
1025 skip_until = vim_strnsave(p, skiptowhite(p) - p);
1026 getline_options = GETLINE_NONE;
1027 is_heredoc = TRUE;
1028 }
1029 }
1030 }
1031
1032 // Add the line to the function.
1033 if (ga_grow(newlines, 1 + sourcing_lnum_off) == FAIL)
1034 goto theend;
1035
Bram Moolenaar20677332021-06-06 17:02:53 +02001036 if (heredoc_concat_len > 0)
1037 {
1038 // For a :def function "python << EOF" concatenats all the lines,
1039 // to be used for the instruction later.
1040 ga_concat(&heredoc_ga, theline);
1041 ga_concat(&heredoc_ga, (char_u *)"\n");
1042 p = vim_strsave((char_u *)"");
1043 }
1044 else
1045 {
1046 // Copy the line to newly allocated memory. get_one_sourceline()
1047 // allocates 250 bytes per line, this saves 80% on average. The
1048 // cost is an extra alloc/free.
1049 p = vim_strsave(theline);
1050 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001051 if (p == NULL)
1052 goto theend;
1053 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = p;
1054
1055 // Add NULL lines for continuation lines, so that the line count is
1056 // equal to the index in the growarray.
1057 while (sourcing_lnum_off-- > 0)
1058 ((char_u **)(newlines->ga_data))[newlines->ga_len++] = NULL;
1059
1060 // Check for end of eap->arg.
1061 if (line_arg != NULL && *line_arg == NUL)
1062 line_arg = NULL;
1063 }
1064
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001065 // Return OK when no error was detected.
1066 if (!did_emsg)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001067 ret = OK;
1068
1069theend:
1070 vim_free(skip_until);
1071 vim_free(heredoc_trimmed);
Bram Moolenaar20677332021-06-06 17:02:53 +02001072 vim_free(heredoc_ga.ga_data);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001073 need_wait_return |= saved_wait_return;
1074 return ret;
1075}
1076
1077/*
1078 * Handle the body of a lambda. *arg points to the "{", process statements
1079 * until the matching "}".
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001080 * When not evaluating "newargs" is NULL.
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001081 * When successful "rettv" is set to a funcref.
1082 */
1083 static int
1084lambda_function_body(
1085 char_u **arg,
1086 typval_T *rettv,
1087 evalarg_T *evalarg,
1088 garray_T *newargs,
1089 garray_T *argtypes,
1090 int varargs,
1091 garray_T *default_args,
1092 char_u *ret_type)
1093{
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001094 int evaluate = (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001095 garray_T *gap = &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +02001096 garray_T *freegap = &evalarg->eval_freega;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001097 ufunc_T *ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001098 exarg_T eap;
1099 garray_T newlines;
1100 char_u *cmdline = NULL;
1101 int ret = FAIL;
1102 char_u *line_to_free = NULL;
1103 partial_T *pt;
1104 char_u *name;
1105 int lnum_save = -1;
1106 linenr_T sourcing_lnum_top = SOURCING_LNUM;
1107
Bram Moolenaare98f60a2021-03-22 18:22:30 +01001108 if (!ends_excmd2(*arg, skipwhite(*arg + 1)))
1109 {
1110 semsg(_(e_trailing_arg), *arg + 1);
1111 return FAIL;
1112 }
1113
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001114 CLEAR_FIELD(eap);
1115 eap.cmdidx = CMD_block;
1116 eap.forceit = FALSE;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001117 eap.cmdlinep = &cmdline;
1118 eap.skip = !evaluate;
1119 if (evalarg->eval_cctx != NULL)
1120 fill_exarg_from_cctx(&eap, evalarg->eval_cctx);
1121 else
1122 {
1123 eap.getline = evalarg->eval_getline;
1124 eap.cookie = evalarg->eval_cookie;
1125 }
1126
1127 ga_init2(&newlines, (int)sizeof(char_u *), 10);
1128 if (get_function_body(&eap, &newlines, NULL, &line_to_free) == FAIL)
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001129 {
1130 vim_free(cmdline);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001131 goto erret;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001132 }
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001133
1134 // When inside a lambda must add the function lines to evalarg.eval_ga.
1135 evalarg->eval_break_count += newlines.ga_len;
1136 if (gap->ga_itemsize > 0)
1137 {
1138 int idx;
1139 char_u *last;
1140 size_t plen;
1141 char_u *pnl;
1142
1143 for (idx = 0; idx < newlines.ga_len; ++idx)
1144 {
1145 char_u *p = skipwhite(((char_u **)newlines.ga_data)[idx]);
1146
Bram Moolenaarecb66452021-05-18 15:09:18 +02001147 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001148 goto erret;
1149
1150 // Going to concatenate the lines after parsing. For an empty or
1151 // comment line use an empty string.
1152 // Insert NL characters at the start of each line, the string will
1153 // be split again later in .get_lambda_tv().
1154 if (*p == NUL || vim9_comment_start(p))
1155 p = (char_u *)"";
1156 plen = STRLEN(p);
1157 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1158 if (pnl != NULL)
1159 mch_memmove(pnl + 1, p, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001160 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1161 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001162 }
Bram Moolenaarecb66452021-05-18 15:09:18 +02001163 if (ga_grow(gap, 1) == FAIL || ga_grow(freegap, 1) == FAIL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001164 goto erret;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001165 if (eap.nextcmd != NULL)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001166 // more is following after the "}", which was skipped
1167 last = cmdline;
1168 else
1169 // nothing is following the "}"
1170 last = (char_u *)"}";
1171 plen = STRLEN(last);
1172 pnl = vim_strnsave((char_u *)"\n", plen + 1);
1173 if (pnl != NULL)
1174 mch_memmove(pnl + 1, last, plen + 1);
Bram Moolenaarecb66452021-05-18 15:09:18 +02001175 ((char_u **)gap->ga_data)[gap->ga_len++] = pnl;
1176 ((char_u **)freegap->ga_data)[freegap->ga_len++] = pnl;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001177 }
1178
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001179 if (eap.nextcmd != NULL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001180 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01001181 garray_T *tfgap = &evalarg->eval_tofree_ga;
1182
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001183 // Something comes after the "}".
1184 *arg = eap.nextcmd;
Bram Moolenaar67da21a2021-03-21 22:12:34 +01001185
1186 // "arg" points into cmdline, need to keep the line and free it later.
Bram Moolenaar844fb642021-10-23 13:32:30 +01001187 if (ga_grow(tfgap, 1) == OK)
1188 {
1189 ((char_u **)(tfgap->ga_data))[tfgap->ga_len++] = cmdline;
1190 evalarg->eval_using_cmdline = TRUE;
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00001191 if (cmdline == line_to_free)
1192 line_to_free = NULL;
Bram Moolenaar844fb642021-10-23 13:32:30 +01001193 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001194 }
1195 else
1196 *arg = (char_u *)"";
1197
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001198 if (!evaluate)
1199 {
1200 ret = OK;
1201 goto erret;
1202 }
1203
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001204 name = get_lambda_name();
1205 ufunc = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
1206 if (ufunc == NULL)
1207 goto erret;
1208 set_ufunc_name(ufunc, name);
1209 if (hash_add(&func_hashtab, UF2HIKEY(ufunc)) == FAIL)
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001210 goto erret;
Bram Moolenaar38453522021-11-28 22:00:12 +00001211 ufunc->uf_flags = FC_LAMBDA;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001212 ufunc->uf_refcount = 1;
1213 ufunc->uf_args = *newargs;
1214 newargs->ga_data = NULL;
1215 ufunc->uf_def_args = *default_args;
1216 default_args->ga_data = NULL;
1217 ufunc->uf_func_type = &t_func_any;
1218
1219 // error messages are for the first function line
1220 lnum_save = SOURCING_LNUM;
1221 SOURCING_LNUM = sourcing_lnum_top;
1222
1223 // parse argument types
1224 if (parse_argument_types(ufunc, argtypes, varargs) == FAIL)
1225 {
1226 SOURCING_LNUM = lnum_save;
1227 goto erret;
1228 }
1229
1230 // parse the return type, if any
1231 if (parse_return_type(ufunc, ret_type) == FAIL)
1232 goto erret;
1233
1234 pt = ALLOC_CLEAR_ONE(partial_T);
1235 if (pt == NULL)
1236 goto erret;
1237 pt->pt_func = ufunc;
1238 pt->pt_refcount = 1;
1239
1240 ufunc->uf_lines = newlines;
1241 newlines.ga_data = NULL;
1242 if (sandbox)
1243 ufunc->uf_flags |= FC_SANDBOX;
1244 if (!ASCII_ISUPPER(*ufunc->uf_name))
1245 ufunc->uf_flags |= FC_VIM9;
1246 ufunc->uf_script_ctx = current_sctx;
1247 ufunc->uf_script_ctx_version = current_sctx.sc_version;
1248 ufunc->uf_script_ctx.sc_lnum += sourcing_lnum_top;
1249 set_function_type(ufunc);
1250
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001251 function_using_block_scopes(ufunc, evalarg->eval_cstack);
1252
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001253 rettv->vval.v_partial = pt;
1254 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001255 ufunc = NULL;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001256 ret = OK;
1257
1258erret:
1259 if (lnum_save >= 0)
1260 SOURCING_LNUM = lnum_save;
1261 vim_free(line_to_free);
1262 ga_clear_strings(&newlines);
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001263 if (newargs != NULL)
1264 ga_clear_strings(newargs);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001265 ga_clear_strings(default_args);
Bram Moolenaar79efa2e2021-03-27 15:40:11 +01001266 if (ufunc != NULL)
1267 {
1268 func_clear(ufunc, TRUE);
1269 func_free(ufunc, TRUE);
1270 }
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001271 return ret;
1272}
1273
1274/*
1275 * Parse a lambda expression and get a Funcref from "*arg" into "rettv".
Bram Moolenaar65c44152020-12-24 15:14:01 +01001276 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001277 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001278 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
1279 */
1280 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001281get_lambda_tv(
1282 char_u **arg,
1283 typval_T *rettv,
1284 int types_optional,
1285 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001286{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001287 int evaluate = evalarg != NULL
1288 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001289 garray_T newargs;
1290 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001291 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001292 garray_T argtypes;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001293 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001294 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001295 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001296 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001297 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001298 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001299 char_u *s;
1300 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001301 int *old_eval_lavars = eval_lavars_used;
1302 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001303 char_u *tofree1 = NULL;
1304 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001305 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001306 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +01001307 int called_emsg_start = called_emsg;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001308
1309 if (equal_arrow && !in_vim9script())
1310 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001311
1312 ga_init(&newargs);
1313 ga_init(&newlines);
1314
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001315 // First, check if this is really a lambda expression. "->" or "=>" must
1316 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +01001317 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001318 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001319 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar14ded112021-06-26 19:25:49 +02001320 NULL, &default_args, TRUE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001321 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001322 {
1323 if (types_optional)
1324 ga_clear_strings(&argtypes);
Bram Moolenaar0346b792021-01-31 22:18:29 +01001325 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001326 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001327
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001328 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001329 if (evaluate)
1330 pnewargs = &newargs;
1331 else
1332 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +01001333 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +01001334 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001335 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001336 &varargs, &default_args,
1337 FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001338 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001339 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaare462f522020-12-27 14:43:30 +01001340 equal_arrow || in_vim9script() ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001341 {
1342 if (types_optional)
1343 ga_clear_strings(&argtypes);
Bram Moolenaare5730bd2020-12-25 22:30:16 +01001344 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001345 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +01001346 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01001347 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001348
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001349 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
1350 if (ret_type != NULL)
1351 {
1352 ret_type = vim_strsave(ret_type);
1353 tofree2 = ret_type;
1354 }
1355
Bram Moolenaare38eab22019-12-05 21:50:01 +01001356 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001357 if (evaluate)
1358 eval_lavars_used = &eval_lavars;
1359
Bram Moolenaar65c44152020-12-24 15:14:01 +01001360 *arg = skipwhite_and_linebreak(*arg, evalarg);
1361
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001362 // Recognize "{" as the start of a function body.
1363 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +01001364 {
Bram Moolenaarfed9e832021-04-10 21:38:38 +02001365 if (evalarg == NULL)
1366 // cannot happen?
1367 goto theend;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001368 if (lambda_function_body(arg, rettv, evalarg, pnewargs,
1369 types_optional ? &argtypes : NULL, varargs,
1370 &default_args, ret_type) == FAIL)
1371 goto errret;
1372 goto theend;
1373 }
1374 if (default_args.ga_len > 0)
1375 {
1376 emsg(_(e_cannot_use_default_values_in_lambda));
Bram Moolenaar65c44152020-12-24 15:14:01 +01001377 goto errret;
1378 }
1379
Bram Moolenaare38eab22019-12-05 21:50:01 +01001380 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001381 start = *arg;
1382 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001383 if (ret == FAIL)
1384 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001385 if (evalarg != NULL)
1386 {
1387 // avoid that the expression gets freed when another line break follows
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001388 tofree1 = evalarg->eval_tofree;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001389 evalarg->eval_tofree = NULL;
1390 }
1391
Bram Moolenaar65c44152020-12-24 15:14:01 +01001392 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +01001393 {
Bram Moolenaar65c44152020-12-24 15:14:01 +01001394 *arg = skipwhite_and_linebreak(*arg, evalarg);
1395 if (**arg != '}')
1396 {
1397 semsg(_("E451: Expected }: %s"), *arg);
1398 goto errret;
1399 }
1400 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +01001401 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001402
1403 if (evaluate)
1404 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001405 int len;
Bram Moolenaar38453522021-11-28 22:00:12 +00001406 int flags = FC_LAMBDA;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001407 char_u *p;
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001408 char_u *line_end;
Bram Moolenaar04b12692020-05-04 23:24:44 +02001409 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001410
Bram Moolenaar47ed5532019-08-08 20:49:14 +02001411 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001412 if (fp == NULL)
1413 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001414 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001415 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001416 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001417 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001418
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001419 ga_init2(&newlines, (int)sizeof(char_u *), 1);
1420 if (ga_grow(&newlines, 1) == FAIL)
1421 goto errret;
1422
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001423 // If there are line breaks, we need to split up the string.
1424 line_end = vim_strchr(start, '\n');
Bram Moolenaar68686342021-07-28 15:54:54 +02001425 if (line_end == NULL || line_end > end)
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001426 line_end = end;
1427
1428 // Add "return " before the expression (or the first line).
1429 len = 7 + (int)(line_end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001430 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001431 if (p == NULL)
1432 goto errret;
1433 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
1434 STRCPY(p, "return ");
Bram Moolenaar074f84c2021-05-18 11:47:44 +02001435 vim_strncpy(p + 7, start, line_end - start);
1436
1437 if (line_end != end)
1438 {
1439 // Add more lines, split by line breaks. Thus is used when a
1440 // lambda with { cmds } is encountered.
1441 while (*line_end == '\n')
1442 {
1443 if (ga_grow(&newlines, 1) == FAIL)
1444 goto errret;
1445 start = line_end + 1;
1446 line_end = vim_strchr(start, '\n');
1447 if (line_end == NULL)
1448 line_end = end;
1449 ((char_u **)(newlines.ga_data))[newlines.ga_len++] =
1450 vim_strnsave(start, line_end - start);
1451 }
1452 }
1453
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001454 if (strstr((char *)p + 7, "a:") == NULL)
1455 // No a: variables are used for sure.
1456 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001457
1458 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001459 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001460 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001461 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001462 if (types_optional)
1463 {
Bram Moolenaar2a389082021-04-09 20:24:31 +02001464 if (parse_argument_types(fp, &argtypes,
1465 in_vim9script() && varargs) == FAIL)
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001466 goto errret;
1467 if (ret_type != NULL)
1468 {
1469 fp->uf_ret_type = parse_type(&ret_type,
1470 &fp->uf_type_list, TRUE);
1471 if (fp->uf_ret_type == NULL)
1472 goto errret;
1473 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +01001474 else
Bram Moolenaara9931532021-06-12 15:58:16 +02001475 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001476 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001477
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001478 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001479 if (current_funccal != NULL && eval_lavars)
1480 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001481 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +02001482 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001483 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001484 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001485
1486#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001487 if (prof_def_func())
1488 func_do_profile(fp);
1489#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001490 if (sandbox)
1491 flags |= FC_SANDBOX;
Bram Moolenaar767034c2021-04-09 17:24:52 +02001492 // In legacy script a lambda can be called with more args than
Bram Moolenaar2a389082021-04-09 20:24:31 +02001493 // uf_args.ga_len. In Vim9 script "...name" has to be used.
1494 fp->uf_varargs = !in_vim9script() || varargs;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001495 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001496 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001497 fp->uf_script_ctx = current_sctx;
Bram Moolenaara755fdb2021-11-20 21:35:41 +00001498 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len + 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001499
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001500 function_using_block_scopes(fp, evalarg->eval_cstack);
1501
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001502 pt->pt_func = fp;
1503 pt->pt_refcount = 1;
1504 rettv->vval.v_partial = pt;
1505 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +01001506
1507 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001508 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001509
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001510theend:
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001511 eval_lavars_used = old_eval_lavars;
Bram Moolenaarefaaaa62020-07-08 22:24:09 +02001512 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001513 evalarg->eval_tofree = tofree1;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001514 else
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001515 vim_free(tofree1);
1516 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001517 if (types_optional)
1518 ga_clear_strings(&argtypes);
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001519
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001520 return OK;
1521
1522errret:
1523 ga_clear_strings(&newargs);
1524 ga_clear_strings(&newlines);
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01001525 ga_clear_strings(&default_args);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001526 if (types_optional)
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001527 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01001528 ga_clear_strings(&argtypes);
Bram Moolenaar15bbb8f2021-05-24 15:45:29 +02001529 if (fp != NULL)
1530 vim_free(fp->uf_arg_types);
1531 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001532 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +01001533 vim_free(pt);
Bram Moolenaarefaaaa62020-07-08 22:24:09 +02001534 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001535 evalarg->eval_tofree = tofree1;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02001536 else
Bram Moolenaarf898f7c2021-01-16 18:09:52 +01001537 vim_free(tofree1);
1538 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001539 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001540 return FAIL;
1541}
1542
1543/*
1544 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
1545 * name it contains, otherwise return "name".
1546 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
1547 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001548 * If "type" is not NULL and a Vim9 script-local variable is found look up the
1549 * type of the variable.
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001550 * If "found_var" is not NULL and a variable was found set it to TRUE.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001551 */
1552 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001553deref_func_name(
1554 char_u *name,
1555 int *lenp,
1556 partial_T **partialp,
1557 type_T **type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001558 int no_autoload,
1559 int *found_var)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001560{
1561 dictitem_T *v;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001562 typval_T *tv = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001563 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001564 char_u *s = NULL;
1565 hashtab_T *ht;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001566 int did_type = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001567
1568 if (partialp != NULL)
1569 *partialp = NULL;
1570
1571 cc = name[*lenp];
1572 name[*lenp] = NUL;
Bram Moolenaar20677332021-06-06 17:02:53 +02001573
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001574 v = find_var(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001575 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001576 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001577 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001578 tv = &v->di_tv;
1579 }
1580 else if (in_vim9script() || STRNCMP(name, "s:", 2) == 0)
1581 {
1582 imported_T *import;
1583 char_u *p = name;
1584 int len = *lenp;
1585
1586 if (STRNCMP(name, "s:", 2) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001587 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001588 p = name + 2;
1589 len -= 2;
1590 }
1591 import = find_imported(p, len, NULL);
1592
1593 // imported variable from another script
1594 if (import != NULL)
1595 {
1596 if (import->imp_funcname != NULL)
1597 {
1598 s = import->imp_funcname;
1599 *lenp = (int)STRLEN(s);
1600 return s;
1601 }
Bram Moolenaarf8a79fc2021-12-14 12:06:16 +00001602 if (import->imp_flags & IMP_FLAGS_STAR)
1603 {
1604 name[len] = NUL;
1605 semsg(_(e_cannot_use_str_itself_it_is_imported_with_star),
1606 name);
1607 name[len] = cc;
1608 *lenp = 0;
1609 return (char_u *)""; // just in case
1610 }
1611 else
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001612 {
1613 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
1614 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1615 + import->imp_var_vals_idx;
1616 tv = sv->sv_tv;
1617 if (type != NULL)
1618 *type = sv->sv_type;
1619 did_type = TRUE;
1620 }
1621 }
1622 }
1623
1624 if (tv != NULL)
1625 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001626 if (found_var != NULL)
1627 *found_var = TRUE;
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001628 if (tv->v_type == VAR_FUNC)
1629 {
1630 if (tv->vval.v_string == NULL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001631 {
1632 *lenp = 0;
1633 return (char_u *)""; // just in case
1634 }
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001635 s = tv->vval.v_string;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001636 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001637 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001638
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001639 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001640 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001641 partial_T *pt = tv->vval.v_partial;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001642
1643 if (pt == NULL)
1644 {
1645 *lenp = 0;
1646 return (char_u *)""; // just in case
1647 }
1648 if (partialp != NULL)
1649 *partialp = pt;
1650 s = partial_name(pt);
1651 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001652 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001653
1654 if (s != NULL)
1655 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001656 if (!did_type && type != NULL && ht == get_script_local_ht())
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001657 {
Bram Moolenaar5fe07d22021-10-22 22:17:53 +01001658 svar_T *sv = find_typval_in_script(tv);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001659
1660 if (sv != NULL)
1661 *type = sv->sv_type;
1662 }
1663 return s;
1664 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001665 }
1666
1667 return name;
1668}
1669
1670/*
1671 * Give an error message with a function name. Handle <SNR> things.
1672 * "ermsg" is to be passed without translation, use N_() instead of _().
1673 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001674 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001675emsg_funcname(char *ermsg, char_u *name)
1676{
1677 char_u *p;
1678
1679 if (*name == K_SPECIAL)
1680 p = concat_str((char_u *)"<SNR>", name + 3);
1681 else
1682 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001683 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001684 if (p != name)
1685 vim_free(p);
1686}
1687
1688/*
1689 * Allocate a variable for the result of a function.
1690 * Return OK or FAIL.
1691 */
1692 int
1693get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001694 char_u *name, // name of the function
1695 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001696 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001697 char_u **arg, // argument, pointing to the '('
Bram Moolenaare6b53242020-07-01 17:28:33 +02001698 evalarg_T *evalarg, // for line continuation
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001699 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001700{
1701 char_u *argp;
1702 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001703 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
1704 int argcount = 0; // number of arguments found
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001705 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001706
1707 /*
1708 * Get the arguments.
1709 */
1710 argp = *arg;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001711 while (argcount < MAX_FUNC_ARGS - (funcexe->fe_partial == NULL ? 0
1712 : funcexe->fe_partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001713 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02001714 // skip the '(' or ',' and possibly line breaks
1715 argp = skipwhite_and_linebreak(argp + 1, evalarg);
1716
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001717 if (*argp == ')' || *argp == ',' || *argp == NUL)
1718 break;
Bram Moolenaare6b53242020-07-01 17:28:33 +02001719 if (eval1(&argp, &argvars[argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001720 {
1721 ret = FAIL;
1722 break;
1723 }
1724 ++argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +02001725 // The comma should come right after the argument, but this wasn't
1726 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001727 if (vim9script)
1728 {
1729 if (*argp != ',' && *skipwhite(argp) == ',')
1730 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001731 semsg(_(e_no_white_space_allowed_before_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001732 ret = FAIL;
1733 break;
1734 }
1735 }
1736 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02001737 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001738 if (*argp != ',')
1739 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001740 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
1741 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001742 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001743 ret = FAIL;
1744 break;
1745 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001746 }
Bram Moolenaare6b53242020-07-01 17:28:33 +02001747 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001748 if (*argp == ')')
1749 ++argp;
1750 else
1751 ret = FAIL;
1752
1753 if (ret == OK)
1754 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001755 int i = 0;
1756 int did_emsg_before = did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001757
1758 if (get_vim_var_nr(VV_TESTING))
1759 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001760 // Prepare for calling test_garbagecollect_now(), need to know
1761 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001762 if (funcargs.ga_itemsize == 0)
1763 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
1764 for (i = 0; i < argcount; ++i)
1765 if (ga_grow(&funcargs, 1) == OK)
1766 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
1767 &argvars[i];
1768 }
1769
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001770 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001771 if (in_vim9script() && did_emsg > did_emsg_before)
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001772 {
Bram Moolenaar327d3ee2021-07-28 19:34:14 +02001773 // An error in a builtin function does not return FAIL, but we do
1774 // want to abort further processing if an error was given.
1775 ret = FAIL;
Bram Moolenaar6e850a62021-07-28 22:21:23 +02001776 clear_tv(rettv);
1777 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001778
1779 funcargs.ga_len -= i;
1780 }
1781 else if (!aborting())
1782 {
1783 if (argcount == MAX_FUNC_ARGS)
1784 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
1785 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00001786 emsg_funcname(e_invalid_arguments_for_function_str, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001787 }
1788
1789 while (--argcount >= 0)
1790 clear_tv(&argvars[argcount]);
1791
Bram Moolenaar8294d492020-08-10 22:40:56 +02001792 if (in_vim9script())
1793 *arg = argp;
1794 else
1795 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001796 return ret;
1797}
1798
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001799/*
1800 * Return TRUE if "p" starts with "<SID>" or "s:".
1801 * Only works if eval_fname_script() returned non-zero for "p"!
1802 */
1803 static int
1804eval_fname_sid(char_u *p)
1805{
1806 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
1807}
1808
1809/*
1810 * In a script change <SID>name() and s:name() to K_SNR 123_name().
1811 * Change <SNR>123_name() to K_SNR 123_name().
1812 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
1813 * (slow).
1814 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +01001815 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001816fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
1817{
1818 int llen;
1819 char_u *fname;
1820 int i;
1821
1822 llen = eval_fname_script(name);
1823 if (llen > 0)
1824 {
1825 fname_buf[0] = K_SPECIAL;
1826 fname_buf[1] = KS_EXTRA;
1827 fname_buf[2] = (int)KE_SNR;
1828 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001829 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001830 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001831 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +01001832 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001833 else
1834 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +02001835 sprintf((char *)fname_buf + 3, "%ld_",
1836 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001837 i = (int)STRLEN(fname_buf);
1838 }
1839 }
1840 if (i + STRLEN(name + llen) < FLEN_FIXED)
1841 {
1842 STRCPY(fname_buf + i, name + llen);
1843 fname = fname_buf;
1844 }
1845 else
1846 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001847 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001848 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +01001849 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001850 else
1851 {
1852 *tofree = fname;
1853 mch_memmove(fname, fname_buf, (size_t)i);
1854 STRCPY(fname + i, name + llen);
1855 }
1856 }
1857 }
1858 else
1859 fname = name;
1860 return fname;
1861}
1862
1863/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001864 * Find a function "name" in script "sid".
1865 */
1866 static ufunc_T *
1867find_func_with_sid(char_u *name, int sid)
1868{
1869 hashitem_T *hi;
1870 char_u buffer[200];
1871
1872 buffer[0] = K_SPECIAL;
1873 buffer[1] = KS_EXTRA;
1874 buffer[2] = (int)KE_SNR;
1875 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
1876 (long)sid, name);
1877 hi = hash_find(&func_hashtab, buffer);
1878 if (!HASHITEM_EMPTY(hi))
1879 return HI2UF(hi);
1880
1881 return NULL;
1882}
1883
1884/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001885 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001886 * When "is_global" is true don't find script-local or imported functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001887 * Return NULL for unknown function.
1888 */
Bram Moolenaarce658352020-07-31 23:47:12 +02001889 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001890find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001891{
1892 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893 ufunc_T *func;
1894 imported_T *imported;
1895
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001896 if (!is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001897 {
Bram Moolenaar333894b2020-08-01 18:53:07 +02001898 char_u *after_script = NULL;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001899 long sid = 0;
Bram Moolenaar33b968d2021-12-13 11:31:04 +00001900 int find_script_local = in_vim9script() && eval_isnamec1(*name)
1901 && (name[1] != ':' || *name == 's');
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902
Bram Moolenaar95006e32020-08-29 17:47:08 +02001903 if (find_script_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001905 // Find script-local function before global one.
Bram Moolenaar33b968d2021-12-13 11:31:04 +00001906 func = find_func_with_sid(name[0] == 's' && name[1] == ':'
1907 ? name + 2 : name, current_sctx.sc_sid);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001908 if (func != NULL)
1909 return func;
1910 }
1911
Bram Moolenaarefa94442020-08-08 22:16:00 +02001912 if (name[0] == K_SPECIAL
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001913 && name[1] == KS_EXTRA
1914 && name[2] == KE_SNR)
1915 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001916 // Caller changes s: to <SNR>99_name.
1917
1918 after_script = name + 3;
1919 sid = getdigits(&after_script);
Bram Moolenaarefa94442020-08-08 22:16:00 +02001920 if (*after_script == '_')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001921 ++after_script;
1922 else
1923 after_script = NULL;
1924 }
Bram Moolenaar95006e32020-08-29 17:47:08 +02001925 if (find_script_local || after_script != NULL)
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001926 {
1927 // Find imported function before global one.
Bram Moolenaarefa94442020-08-08 22:16:00 +02001928 if (after_script != NULL && sid != current_sctx.sc_sid)
1929 imported = find_imported_in_script(after_script, 0, sid);
1930 else
1931 imported = find_imported(after_script == NULL
1932 ? name : after_script, 0, cctx);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001933 if (imported != NULL && imported->imp_funcname != NULL)
1934 {
1935 hi = hash_find(&func_hashtab, imported->imp_funcname);
1936 if (!HASHITEM_EMPTY(hi))
1937 return HI2UF(hi);
1938 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001939 }
1940 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001941
Bram Moolenaara26b9702020-04-18 19:53:28 +02001942 hi = hash_find(&func_hashtab,
1943 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001944 if (!HASHITEM_EMPTY(hi))
1945 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001946
1947 return NULL;
1948}
1949
1950/*
1951 * Find a function by name, return pointer to it in ufuncs.
1952 * "cctx" is passed in a :def function to find imported functions.
1953 * Return NULL for unknown or dead function.
1954 */
1955 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001956find_func(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001957{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001958 ufunc_T *fp = find_func_even_dead(name, is_global, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959
1960 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
1961 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001962 return NULL;
1963}
1964
1965/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02001966 * Return TRUE if "ufunc" is a global function.
1967 */
1968 int
1969func_is_global(ufunc_T *ufunc)
1970{
1971 return ufunc->uf_name[0] != K_SPECIAL;
1972}
1973
1974/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001975 * Copy the function name of "fp" to buffer "buf".
1976 * "buf" must be able to hold the function name plus three bytes.
1977 * Takes care of script-local function names.
1978 */
1979 static void
1980cat_func_name(char_u *buf, ufunc_T *fp)
1981{
Bram Moolenaar0f769812020-09-12 18:32:34 +02001982 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001983 {
1984 STRCPY(buf, "<SNR>");
1985 STRCAT(buf, fp->uf_name + 3);
1986 }
1987 else
1988 STRCPY(buf, fp->uf_name);
1989}
1990
1991/*
1992 * Add a number variable "name" to dict "dp" with value "nr".
1993 */
1994 static void
1995add_nr_var(
1996 dict_T *dp,
1997 dictitem_T *v,
1998 char *name,
1999 varnumber_T nr)
2000{
2001 STRCPY(v->di_key, name);
2002 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2003 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
2004 v->di_tv.v_type = VAR_NUMBER;
2005 v->di_tv.v_lock = VAR_FIXED;
2006 v->di_tv.vval.v_number = nr;
2007}
2008
2009/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002010 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002011 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002012 static void
2013free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002014{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002015 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002016
2017 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
2018 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002019 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002020
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002021 // When garbage collecting a funccall_T may be freed before the
2022 // function that references it, clear its uf_scoped field.
2023 // The function may have been redefined and point to another
2024 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002025 if (fp != NULL && fp->uf_scoped == fc)
2026 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002027 }
Bram Moolenaar58016442016-07-31 18:30:22 +02002028 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002029
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002030 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002031 vim_free(fc);
2032}
2033
2034/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002035 * Free "fc" and what it contains.
2036 * Can be called only when "fc" is kept beyond the period of it called,
2037 * i.e. after cleanup_function_call(fc).
2038 */
2039 static void
2040free_funccal_contents(funccall_T *fc)
2041{
2042 listitem_T *li;
2043
2044 // Free all l: variables.
2045 vars_clear(&fc->l_vars.dv_hashtab);
2046
2047 // Free all a: variables.
2048 vars_clear(&fc->l_avars.dv_hashtab);
2049
2050 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002051 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002052 clear_tv(&li->li_tv);
2053
2054 free_funccal(fc);
2055}
2056
2057/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02002058 * Handle the last part of returning from a function: free the local hashtable.
2059 * Unless it is still in use by a closure.
2060 */
2061 static void
2062cleanup_function_call(funccall_T *fc)
2063{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002064 int may_free_fc = fc->fc_refcount <= 0;
2065 int free_fc = TRUE;
2066
Bram Moolenaar6914c642017-04-01 21:21:30 +02002067 current_funccal = fc->caller;
2068
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002069 // Free all l: variables if not referred.
2070 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
2071 vars_clear(&fc->l_vars.dv_hashtab);
2072 else
2073 free_fc = FALSE;
2074
2075 // If the a:000 list and the l: and a: dicts are not referenced and
2076 // there is no closure using it, we can free the funccall_T and what's
2077 // in it.
2078 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
2079 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002080 else
2081 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002082 int todo;
2083 hashitem_T *hi;
2084 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002085
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002086 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02002087
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002088 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +02002089 todo = (int)fc->l_avars.dv_hashtab.ht_used;
2090 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
2091 {
2092 if (!HASHITEM_EMPTY(hi))
2093 {
2094 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002095 di = HI2DI(hi);
2096 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002097 }
2098 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002099 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002100
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002101 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
2102 fc->l_varlist.lv_first = NULL;
2103 else
2104 {
2105 listitem_T *li;
2106
2107 free_fc = FALSE;
2108
2109 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002110 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02002111 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002112 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002113
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002114 if (free_fc)
2115 free_funccal(fc);
2116 else
2117 {
2118 static int made_copy = 0;
2119
2120 // "fc" is still in use. This can happen when returning "a:000",
2121 // assigning "l:" to a global variable or defining a closure.
2122 // Link "fc" in the list for garbage collection later.
2123 fc->caller = previous_funccal;
2124 previous_funccal = fc;
2125
2126 if (want_garbage_collect)
2127 // If garbage collector is ready, clear count.
2128 made_copy = 0;
2129 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002130 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002131 // We have made a lot of copies, worth 4 Mbyte. This can happen
2132 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002133 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002134 // too much memory.
2135 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01002136 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002137 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02002138 }
2139}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002140
2141/*
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002142 * Return TRUE if "name" is a numbered function, ignoring a "g:" prefix.
2143 */
2144 static int
2145numbered_function(char_u *name)
2146{
2147 return isdigit(*name)
2148 || (name[0] == 'g' && name[1] == ':' && isdigit(name[2]));
2149}
2150
2151/*
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002152 * There are two kinds of function names:
2153 * 1. ordinary names, function defined with :function or :def
2154 * 2. numbered functions and lambdas
2155 * For the first we only count the name stored in func_hashtab as a reference,
2156 * using function() does not count as a reference, because the function is
2157 * looked up by name.
2158 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002159 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002160func_name_refcount(char_u *name)
2161{
Bram Moolenaar57bc2332021-12-15 12:06:43 +00002162 return numbered_function(name) || *name == '<';
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002163}
2164
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002165/*
2166 * Unreference "fc": decrement the reference count and free it when it
2167 * becomes zero. "fp" is detached from "fc".
2168 * When "force" is TRUE we are exiting.
2169 */
2170 static void
2171funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
2172{
2173 funccall_T **pfc;
2174 int i;
2175
2176 if (fc == NULL)
2177 return;
2178
2179 if (--fc->fc_refcount <= 0 && (force || (
2180 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
2181 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
2182 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
2183 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
2184 {
2185 if (fc == *pfc)
2186 {
2187 *pfc = fc->caller;
2188 free_funccal_contents(fc);
2189 return;
2190 }
2191 }
2192 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
2193 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
2194 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
2195}
2196
2197/*
2198 * Remove the function from the function hashtable. If the function was
2199 * deleted while it still has references this was already done.
2200 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
2201 */
2202 static int
2203func_remove(ufunc_T *fp)
2204{
2205 hashitem_T *hi;
2206
2207 // Return if it was already virtually deleted.
2208 if (fp->uf_flags & FC_DEAD)
2209 return FALSE;
2210
2211 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
2212 if (!HASHITEM_EMPTY(hi))
2213 {
2214 // When there is a def-function index do not actually remove the
2215 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002216 // Do remove it when it's a copy.
2217 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaarc970e422021-03-17 15:03:04 +01002218 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01002220 return FALSE;
2221 }
2222 hash_remove(&func_hashtab, hi);
2223 fp->uf_flags |= FC_DELETED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224 return TRUE;
2225 }
2226 return FALSE;
2227}
2228
2229 static void
2230func_clear_items(ufunc_T *fp)
2231{
2232 ga_clear_strings(&(fp->uf_args));
2233 ga_clear_strings(&(fp->uf_def_args));
2234 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002236 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01002237 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02002238 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002239
2240 // Increment the refcount of this function to avoid it being freed
2241 // recursively when the partial is freed.
2242 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002243 partial_unref(fp->uf_partial);
2244 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002245 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002246
2247#ifdef FEAT_LUA
2248 if (fp->uf_cb_free != NULL)
2249 {
2250 fp->uf_cb_free(fp->uf_cb_state);
2251 fp->uf_cb_free = NULL;
2252 }
2253
2254 fp->uf_cb_state = NULL;
2255 fp->uf_cb = NULL;
2256#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257#ifdef FEAT_PROFILE
2258 VIM_CLEAR(fp->uf_tml_count);
2259 VIM_CLEAR(fp->uf_tml_total);
2260 VIM_CLEAR(fp->uf_tml_self);
2261#endif
2262}
2263
2264/*
2265 * Free all things that a function contains. Does not free the function
2266 * itself, use func_free() for that.
2267 * When "force" is TRUE we are exiting.
2268 */
2269 static void
2270func_clear(ufunc_T *fp, int force)
2271{
2272 if (fp->uf_cleared)
2273 return;
2274 fp->uf_cleared = TRUE;
2275
2276 // clear this function
2277 func_clear_items(fp);
2278 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002279 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002280}
2281
2282/*
2283 * Free a function and remove it from the list of functions. Does not free
2284 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002285 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02002286 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002287 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02002288 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002289func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290{
2291 // Only remove it when not done already, otherwise we would remove a newer
2292 // version of the function with the same name.
2293 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
2294 func_remove(fp);
2295
Bram Moolenaarf7779c62020-05-03 15:38:16 +02002296 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002297 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002298 if (fp->uf_dfunc_idx > 0)
2299 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002300 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02002302 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02002303 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02002304 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305}
2306
2307/*
2308 * Free all things that a function contains and free the function itself.
2309 * When "force" is TRUE we are exiting.
2310 */
Bram Moolenaar2336c372021-12-06 15:06:54 +00002311 void
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312func_clear_free(ufunc_T *fp, int force)
2313{
2314 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02002315 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
2316 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002317 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02002318 else
2319 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320}
2321
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002322/*
2323 * Copy already defined function "lambda" to a new function with name "global".
2324 * This is for when a compiled function defines a global function.
2325 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002326 int
2327copy_func(char_u *lambda, char_u *global, ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002328{
2329 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01002330 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002331
2332 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002333 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002334 semsg(_(e_lambda_function_not_found_str), lambda);
2335 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002336 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002337
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002338 fp = find_func(global, TRUE, NULL);
2339 if (fp != NULL)
2340 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002341 // TODO: handle ! to overwrite
Bram Moolenaarc553a212021-12-26 20:20:34 +00002342 semsg(_(e_function_str_already_exists_add_excl_to_replace), global);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002343 return FAIL;
2344 }
2345
2346 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
2347 if (fp == NULL)
2348 return FAIL;
2349
2350 fp->uf_varargs = ufunc->uf_varargs;
2351 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
2352 fp->uf_def_status = ufunc->uf_def_status;
2353 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
2354 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
2355 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
2356 == FAIL
2357 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
2358 goto failed;
2359
2360 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
2361 : vim_strsave(ufunc->uf_name_exp);
2362 if (ufunc->uf_arg_types != NULL)
2363 {
2364 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
2365 if (fp->uf_arg_types == NULL)
2366 goto failed;
2367 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
2368 sizeof(type_T *) * fp->uf_args.ga_len);
2369 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002370 if (ufunc->uf_va_name != NULL)
2371 {
2372 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
2373 if (fp->uf_va_name == NULL)
2374 goto failed;
2375 }
2376 fp->uf_ret_type = ufunc->uf_ret_type;
2377
2378 fp->uf_refcount = 1;
2379 STRCPY(fp->uf_name, global);
2380 hash_add(&func_hashtab, UF2HIKEY(fp));
2381
2382 // the referenced dfunc_T is now used one more time
2383 link_def_function(fp);
2384
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002385 // Create a partial to store the context of the function where it was
2386 // instantiated. Only needs to be done once. Do this on the original
2387 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002388 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
2389 {
2390 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2391
2392 if (pt == NULL)
2393 goto failed;
2394 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002395 {
2396 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002397 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002398 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002399 ufunc->uf_partial = pt;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002400 --pt->pt_refcount; // not actually referenced here
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002401 }
2402
2403 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002404
2405failed:
2406 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002407 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002408}
2409
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002410static int funcdepth = 0;
2411
2412/*
2413 * Increment the function call depth count.
2414 * Return FAIL when going over 'maxfuncdepth'.
2415 * Otherwise return OK, must call funcdepth_decrement() later!
2416 */
2417 int
2418funcdepth_increment(void)
2419{
2420 if (funcdepth >= p_mfd)
2421 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00002422 emsg(_(e_function_call_depth_is_higher_than_macfuncdepth));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002423 return FAIL;
2424 }
2425 ++funcdepth;
2426 return OK;
2427}
2428
2429 void
2430funcdepth_decrement(void)
2431{
2432 --funcdepth;
2433}
2434
2435/*
2436 * Get the current function call depth.
2437 */
2438 int
2439funcdepth_get(void)
2440{
2441 return funcdepth;
2442}
2443
2444/*
2445 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002446 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002447 * times as funcdepth_increment().
2448 */
2449 void
2450funcdepth_restore(int depth)
2451{
2452 funcdepth = depth;
2453}
Bram Moolenaar6914c642017-04-01 21:21:30 +02002454
2455/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002456 * Call a user function.
2457 */
2458 static void
2459call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01002460 ufunc_T *fp, // pointer to function
2461 int argcount, // nr of args
2462 typval_T *argvars, // arguments
2463 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002464 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01002465 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002466{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002467 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02002468 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002469 funccall_T *fc;
2470 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002471 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002472 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002473 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002474 int i;
2475 int ai;
2476 int islambda = FALSE;
2477 char_u numbuf[NUMBUFLEN];
2478 char_u *name;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002479 typval_T *tv_to_free[MAX_FUNC_ARGS];
2480 int tv_to_free_len = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002481#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01002482 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002483#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01002484 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002485
Bram Moolenaarb2049902021-01-24 12:53:53 +01002486#ifdef FEAT_PROFILE
2487 CLEAR_FIELD(profile_info);
2488#endif
2489
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002490 // If depth of calling is getting too high, don't execute the function.
2491 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002492 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002493 rettv->v_type = VAR_NUMBER;
2494 rettv->vval.v_number = -1;
2495 return;
2496 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002497
Bram Moolenaare38eab22019-12-05 21:50:01 +01002498 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002499
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002500 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01002501 if (fc == NULL)
2502 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002503 fc->caller = current_funccal;
2504 current_funccal = fc;
2505 fc->func = fp;
2506 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002507 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002508 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002509 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
2510 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002511 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002512 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002513 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002514
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002515 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01002517#ifdef FEAT_PROFILE
2518 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2519#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002520 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01002521#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002522 if (do_profiling == PROF_YES)
2523 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002524#endif
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002525 call_def_function(fp, argcount, argvars, funcexe->fe_partial, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002526 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01002527#ifdef FEAT_PROFILE
2528 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01002529 || (caller != NULL && caller->uf_profiling)))
2530 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01002531#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 current_funccal = fc->caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 free_funccal(fc);
2534 return;
2535 }
2536
Bram Moolenaar38453522021-11-28 22:00:12 +00002537 islambda = fp->uf_flags & FC_LAMBDA;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002538
2539 /*
2540 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
2541 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
2542 * each argument variable and saves a lot of time.
2543 */
2544 /*
2545 * Init l: variables.
2546 */
2547 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
2548 if (selfdict != NULL)
2549 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002550 // Set l:self to "selfdict". Use "name" to avoid a warning from
2551 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002552 v = &fc->fixvar[fixvar_idx++].var;
2553 name = v->di_key;
2554 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01002555 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002556 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
2557 v->di_tv.v_type = VAR_DICT;
2558 v->di_tv.v_lock = 0;
2559 v->di_tv.vval.v_dict = selfdict;
2560 ++selfdict->dv_refcount;
2561 }
2562
2563 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002564 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002565 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002566 * Set a:000 to a list with room for the "..." arguments.
2567 */
2568 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002569 if ((fp->uf_flags & FC_NOARGS) == 0)
2570 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002571 (varnumber_T)(argcount >= fp->uf_args.ga_len
2572 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01002573 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002574 if ((fp->uf_flags & FC_NOARGS) == 0)
2575 {
2576 // Use "name" to avoid a warning from some compiler that checks the
2577 // destination size.
2578 v = &fc->fixvar[fixvar_idx++].var;
2579 name = v->di_key;
2580 STRCPY(name, "000");
2581 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2582 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
2583 v->di_tv.v_type = VAR_LIST;
2584 v->di_tv.v_lock = VAR_FIXED;
2585 v->di_tv.vval.v_list = &fc->l_varlist;
2586 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02002587 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002588 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
2589 fc->l_varlist.lv_lock = VAR_FIXED;
2590
2591 /*
2592 * Set a:firstline to "firstline" and a:lastline to "lastline".
2593 * Set a:name to named arguments.
2594 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002595 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002596 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002597 if ((fp->uf_flags & FC_NOARGS) == 0)
2598 {
2599 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002600 (varnumber_T)funcexe->fe_firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002601 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002602 (varnumber_T)funcexe->fe_lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002603 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002604 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002605 {
2606 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002607 typval_T def_rettv;
2608 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002609
2610 ai = i - fp->uf_args.ga_len;
2611 if (ai < 0)
2612 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002613 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002614 name = FUNCARG(fp, i);
2615 if (islambda)
2616 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002617
2618 // evaluate named argument default expression
2619 isdefault = ai + fp->uf_def_args.ga_len >= 0
2620 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
2621 && argvars[i].vval.v_number == VVAL_NONE));
2622 if (isdefault)
2623 {
2624 char_u *default_expr = NULL;
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002625
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002626 def_rettv.v_type = VAR_NUMBER;
2627 def_rettv.vval.v_number = -1;
2628
2629 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
2630 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002631 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002632 {
2633 default_arg_err = 1;
2634 break;
2635 }
2636 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002637 }
2638 else
2639 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002640 if ((fp->uf_flags & FC_NOARGS) != 0)
2641 // Bail out if no a: arguments used (in lambda).
2642 break;
2643
Bram Moolenaare38eab22019-12-05 21:50:01 +01002644 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002645 sprintf((char *)numbuf, "%d", ai + 1);
2646 name = numbuf;
2647 }
2648 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
2649 {
2650 v = &fc->fixvar[fixvar_idx++].var;
2651 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002652 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002653 }
2654 else
2655 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002656 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002657 if (v == NULL)
2658 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002659 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002660 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002661
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02002662 // Note: the values are copied directly to avoid alloc/free.
2663 // "argvars" must have VAR_FIXED for v_lock.
2664 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002665 v->di_tv.v_lock = VAR_FIXED;
2666
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002667 if (isdefault)
2668 // Need to free this later, no matter where it's stored.
2669 tv_to_free[tv_to_free_len++] = &v->di_tv;
2670
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002671 if (addlocal)
2672 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002673 // Named arguments should be accessed without the "a:" prefix in
2674 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002675 copy_tv(&v->di_tv, &v->di_tv);
2676 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002677 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002678 else
2679 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002680
2681 if (ai >= 0 && ai < MAX_FUNC_ARGS)
2682 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01002683 listitem_T *li = &fc->l_listitems[ai];
2684
2685 li->li_tv = argvars[i];
2686 li->li_tv.v_lock = VAR_FIXED;
2687 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002688 }
2689 }
2690
Bram Moolenaare38eab22019-12-05 21:50:01 +01002691 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002692 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02002693
2694 if (fp->uf_flags & FC_SANDBOX)
2695 {
2696 using_sandbox = TRUE;
2697 ++sandbox;
2698 }
2699
Bram Moolenaar25e0f582020-05-25 22:36:50 +02002700 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01002701 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002702 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002703 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002704 ++no_wait_return;
2705 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002706
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002707 smsg(_("calling %s"), SOURCING_NAME);
2708 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002709 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002710 char_u buf[MSG_BUF_LEN];
2711 char_u numbuf2[NUMBUFLEN];
2712 char_u *tofree;
2713 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002714
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002715 msg_puts("(");
2716 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002717 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002718 if (i > 0)
2719 msg_puts(", ");
2720 if (argvars[i].v_type == VAR_NUMBER)
2721 msg_outnum((long)argvars[i].vval.v_number);
2722 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002723 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002724 // Do not want errors such as E724 here.
2725 ++emsg_off;
2726 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
2727 --emsg_off;
2728 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002729 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002730 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002731 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002732 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2733 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002734 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002735 msg_puts((char *)s);
2736 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002737 }
2738 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002739 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002740 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002741 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002742 msg_puts("\n"); // don't overwrite this either
2743
2744 verbose_leave_scroll();
2745 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002746 }
2747#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002748 if (do_profiling == PROF_YES)
2749 profile_may_start_func(&profile_info, fp,
2750 fc->caller == NULL ? NULL : fc->caller->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002751#endif
2752
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002753 save_current_sctx = current_sctx;
2754 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002755 save_did_emsg = did_emsg;
2756 did_emsg = FALSE;
2757
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002758 if (default_arg_err && (fp->uf_flags & FC_ABORT))
2759 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002760 else if (islambda)
2761 {
2762 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
2763
2764 // A Lambda always has the command "return {expr}". It is much faster
2765 // to evaluate {expr} directly.
2766 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002767 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002768 --ex_nesting_level;
2769 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002770 else
2771 // call do_cmdline() to execute the lines
2772 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002773 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
2774
2775 --RedrawingDisabled;
2776
Bram Moolenaare38eab22019-12-05 21:50:01 +01002777 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002778 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
2779 {
2780 clear_tv(rettv);
2781 rettv->v_type = VAR_NUMBER;
2782 rettv->vval.v_number = -1;
2783 }
2784
2785#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01002786 if (do_profiling == PROF_YES)
2787 {
2788 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
2789
2790 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
2791 profile_may_end_func(&profile_info, fp, caller);
2792 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002793#endif
2794
Bram Moolenaare38eab22019-12-05 21:50:01 +01002795 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002796 if (p_verbose >= 12)
2797 {
2798 ++no_wait_return;
2799 verbose_enter_scroll();
2800
2801 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002802 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002803 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002804 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002805 (long)fc->rettv->vval.v_number);
2806 else
2807 {
2808 char_u buf[MSG_BUF_LEN];
2809 char_u numbuf2[NUMBUFLEN];
2810 char_u *tofree;
2811 char_u *s;
2812
Bram Moolenaare38eab22019-12-05 21:50:01 +01002813 // The value may be very long. Skip the middle part, so that we
2814 // have some idea how it starts and ends. smsg() would always
2815 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002816 ++emsg_off;
2817 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
2818 --emsg_off;
2819 if (s != NULL)
2820 {
2821 if (vim_strsize(s) > MSG_BUF_CLEN)
2822 {
2823 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
2824 s = buf;
2825 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002826 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002827 vim_free(tofree);
2828 }
2829 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002830 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002831
2832 verbose_leave_scroll();
2833 --no_wait_return;
2834 }
2835
Bram Moolenaare31ee862020-01-07 20:59:34 +01002836 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002837 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002838 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002839#ifdef FEAT_PROFILE
2840 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01002841 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002842#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02002843 if (using_sandbox)
2844 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002845
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002846 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002847 {
2848 ++no_wait_return;
2849 verbose_enter_scroll();
2850
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002851 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002852 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002853
2854 verbose_leave_scroll();
2855 --no_wait_return;
2856 }
2857
2858 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002859 funcdepth_decrement();
Bram Moolenaarb47bed22021-04-14 17:06:43 +02002860 for (i = 0; i < tv_to_free_len; ++i)
2861 clear_tv(tv_to_free[i]);
Bram Moolenaar6914c642017-04-01 21:21:30 +02002862 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002863}
2864
2865/*
Bram Moolenaar50824712020-12-20 21:10:17 +01002866 * Check the argument count for user function "fp".
2867 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
2868 */
2869 int
2870check_user_func_argcount(ufunc_T *fp, int argcount)
2871{
2872 int regular_args = fp->uf_args.ga_len;
2873
2874 if (argcount < regular_args - fp->uf_def_args.ga_len)
2875 return FCERR_TOOFEW;
2876 else if (!has_varargs(fp) && argcount > regular_args)
2877 return FCERR_TOOMANY;
2878 return FCERR_UNKNOWN;
2879}
2880
2881/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002882 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002883 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 int
2885call_user_func_check(
2886 ufunc_T *fp,
2887 int argcount,
2888 typval_T *argvars,
2889 typval_T *rettv,
2890 funcexe_T *funcexe,
2891 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002892{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002894
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002895 if (fp->uf_flags & FC_RANGE && funcexe->fe_doesrange != NULL)
2896 *funcexe->fe_doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01002897 error = check_user_func_argcount(fp, argcount);
2898 if (error != FCERR_UNKNOWN)
2899 return error;
2900 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 error = FCERR_DICT;
2902 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002903 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 int did_save_redo = FALSE;
2905 save_redo_T save_redo;
2906
2907 /*
2908 * Call the user function.
2909 * Save and restore search patterns, script variables and
2910 * redo buffer.
2911 */
2912 save_search_patterns();
2913 if (!ins_compl_active())
2914 {
2915 saveRedobuff(&save_redo);
2916 did_save_redo = TRUE;
2917 }
2918 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002919 call_user_func(fp, argcount, argvars, rettv, funcexe,
2920 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
2922 // Function was unreferenced while being used, free it now.
2923 func_clear_free(fp, FALSE);
2924 if (did_save_redo)
2925 restoreRedobuff(&save_redo);
2926 restore_search_patterns();
2927 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002928 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002930}
2931
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002932static funccal_entry_T *funccal_stack = NULL;
2933
2934/*
2935 * Save the current function call pointer, and set it to NULL.
2936 * Used when executing autocommands and for ":source".
2937 */
2938 void
2939save_funccal(funccal_entry_T *entry)
2940{
2941 entry->top_funccal = current_funccal;
2942 entry->next = funccal_stack;
2943 funccal_stack = entry;
2944 current_funccal = NULL;
2945}
2946
2947 void
2948restore_funccal(void)
2949{
2950 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002951 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002952 else
2953 {
2954 current_funccal = funccal_stack->top_funccal;
2955 funccal_stack = funccal_stack->next;
2956 }
2957}
2958
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02002959 funccall_T *
2960get_current_funccal(void)
2961{
2962 return current_funccal;
2963}
2964
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002965/*
2966 * Mark all functions of script "sid" as deleted.
2967 */
2968 void
2969delete_script_functions(int sid)
2970{
2971 hashitem_T *hi;
2972 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02002973 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002974 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002975 size_t len;
2976
2977 buf[0] = K_SPECIAL;
2978 buf[1] = KS_EXTRA;
2979 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002980 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002981 len = STRLEN(buf);
2982
Bram Moolenaarce658352020-07-31 23:47:12 +02002983 while (todo > 0)
2984 {
2985 todo = func_hashtab.ht_used;
2986 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2987 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002988 {
Bram Moolenaarce658352020-07-31 23:47:12 +02002989 fp = HI2UF(hi);
2990 if (STRNCMP(fp->uf_name, buf, len) == 0)
2991 {
2992 int changed = func_hashtab.ht_changed;
2993
2994 fp->uf_flags |= FC_DEAD;
Bram Moolenaarc970e422021-03-17 15:03:04 +01002995
2996 if (fp->uf_calls > 0)
2997 {
2998 // Function is executing, don't free it but do remove
2999 // it from the hashtable.
3000 if (func_remove(fp))
3001 fp->uf_refcount--;
3002 }
3003 else
3004 {
3005 func_clear(fp, TRUE);
3006 // When clearing a function another function can be
3007 // cleared as a side effect. When that happens start
3008 // over.
3009 if (changed != func_hashtab.ht_changed)
3010 break;
3011 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003012 }
3013 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003014 }
Bram Moolenaarce658352020-07-31 23:47:12 +02003015 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003016}
3017
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003018#if defined(EXITFREE) || defined(PROTO)
3019 void
3020free_all_functions(void)
3021{
3022 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003023 ufunc_T *fp;
3024 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003025 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003026 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003027
Bram Moolenaare38eab22019-12-05 21:50:01 +01003028 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02003029 while (current_funccal != NULL)
3030 {
3031 clear_tv(current_funccal->rettv);
3032 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02003033 if (current_funccal == NULL && funccal_stack != NULL)
3034 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02003035 }
3036
Bram Moolenaare38eab22019-12-05 21:50:01 +01003037 // First clear what the functions contain. Since this may lower the
3038 // reference count of a function, it may also free a function and change
3039 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003040 while (todo > 0)
3041 {
3042 todo = func_hashtab.ht_used;
3043 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
3044 if (!HASHITEM_EMPTY(hi))
3045 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 // clear the def function index now
3047 fp = HI2UF(hi);
3048 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003049 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050
Bram Moolenaare38eab22019-12-05 21:50:01 +01003051 // Only free functions that are not refcounted, those are
3052 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003053 if (func_name_refcount(fp->uf_name))
3054 ++skipped;
3055 else
3056 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003057 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003058 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003059 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003060 {
3061 skipped = 0;
3062 break;
3063 }
3064 }
3065 --todo;
3066 }
3067 }
3068
Bram Moolenaare38eab22019-12-05 21:50:01 +01003069 // Now actually free the functions. Need to start all over every time,
3070 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003071 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003072 while (func_hashtab.ht_used > skipped)
3073 {
3074 todo = func_hashtab.ht_used;
3075 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003076 if (!HASHITEM_EMPTY(hi))
3077 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02003078 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003079 // Only free functions that are not refcounted, those are
3080 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02003081 fp = HI2UF(hi);
3082 if (func_name_refcount(fp->uf_name))
3083 ++skipped;
3084 else
3085 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02003086 if (func_free(fp, FALSE) == OK)
3087 {
3088 skipped = 0;
3089 break;
3090 }
3091 // did not actually free it
3092 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02003093 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003094 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02003095 }
3096 if (skipped == 0)
3097 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098
3099 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003100}
3101#endif
3102
3103/*
3104 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02003105 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003106 * "len" is the length of "name", or -1 for NUL terminated.
3107 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003109builtin_function(char_u *name, int len)
3110{
3111 char_u *p;
3112
Bram Moolenaara26b9702020-04-18 19:53:28 +02003113 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003114 return FALSE;
3115 p = vim_strchr(name, AUTOLOAD_CHAR);
3116 return p == NULL || (len > 0 && p > name + len);
3117}
3118
3119 int
3120func_call(
3121 char_u *name,
3122 typval_T *args,
3123 partial_T *partial,
3124 dict_T *selfdict,
3125 typval_T *rettv)
3126{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01003127 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003128 listitem_T *item;
3129 typval_T argv[MAX_FUNC_ARGS + 1];
3130 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003131 int r = 0;
3132
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02003133 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02003134 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003135 {
3136 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
3137 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003138 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003139 break;
3140 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003141 // Make a copy of each argument. This is needed to be able to set
3142 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003143 copy_tv(&item->li_tv, &argv[argc++]);
3144 }
3145
3146 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003147 {
3148 funcexe_T funcexe;
3149
Bram Moolenaara80faa82020-04-12 19:37:17 +02003150 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003151 funcexe.fe_firstline = curwin->w_cursor.lnum;
3152 funcexe.fe_lastline = curwin->w_cursor.lnum;
3153 funcexe.fe_evaluate = TRUE;
3154 funcexe.fe_partial = partial;
3155 funcexe.fe_selfdict = selfdict;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003156 r = call_func(name, -1, rettv, argc, argv, &funcexe);
3157 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003158
Bram Moolenaare38eab22019-12-05 21:50:01 +01003159 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003160 while (argc > 0)
3161 clear_tv(&argv[--argc]);
3162
3163 return r;
3164}
3165
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003166static int callback_depth = 0;
3167
3168 int
3169get_callback_depth(void)
3170{
3171 return callback_depth;
3172}
3173
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003174/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003175 * Invoke call_func() with a callback.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003176 * Returns FAIL if the callback could not be called.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003177 */
3178 int
3179call_callback(
3180 callback_T *callback,
3181 int len, // length of "name" or -1 to use strlen()
3182 typval_T *rettv, // return value goes here
3183 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003184 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003185 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003186{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003187 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003188 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003189
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003190 if (callback->cb_name == NULL || *callback->cb_name == NUL)
3191 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02003192 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003193 funcexe.fe_evaluate = TRUE;
3194 funcexe.fe_partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003195 ++callback_depth;
3196 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
3197 --callback_depth;
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003198
3199 // When a :def function was called that uses :try an error would be turned
3200 // into an exception. Need to give the error here.
Bram Moolenaar80d60912021-12-13 19:14:52 +00003201 if (need_rethrow && current_exception != NULL && trylevel == 0)
Bram Moolenaar3b309f12021-12-13 18:19:55 +00003202 {
3203 need_rethrow = FALSE;
3204 handle_did_throw();
3205 }
3206
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02003207 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02003208}
3209
3210/*
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003211 * call the 'callback' function and return the result as a number.
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003212 * Returns -2 when calling the function fails. Uses argv[0] to argv[argc - 1]
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003213 * for the function arguments. argv[argc] should have type VAR_UNKNOWN.
3214 */
3215 varnumber_T
3216call_callback_retnr(
3217 callback_T *callback,
3218 int argcount, // number of "argvars"
3219 typval_T *argvars) // vars for arguments, must have "argcount"
3220 // PLUS ONE elements!
3221{
3222 typval_T rettv;
3223 varnumber_T retval;
3224
3225 if (call_callback(callback, 0, &rettv, argcount, argvars) == FAIL)
Yegappan Lakshmanan4dc24eb2021-12-07 12:23:57 +00003226 return -2;
Yegappan Lakshmanan8658c752021-12-03 11:09:29 +00003227
3228 retval = tv_get_number_chk(&rettv, NULL);
3229 clear_tv(&rettv);
3230 return retval;
3231}
3232
3233/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 * Give an error message for the result of a function.
3235 * Nothing if "error" is FCERR_NONE.
3236 */
3237 void
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003238user_func_error(int error, char_u *name, funcexe_T *funcexe)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239{
3240 switch (error)
3241 {
3242 case FCERR_UNKNOWN:
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003243 if (funcexe->fe_found_var)
3244 semsg(_(e_not_callable_type_str), name);
3245 else
Bram Moolenaare1242042021-12-16 20:56:57 +00003246 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 break;
3248 case FCERR_NOTMETHOD:
3249 emsg_funcname(
3250 N_("E276: Cannot use function as a method: %s"), name);
3251 break;
3252 case FCERR_DELETED:
Bram Moolenaarc553a212021-12-26 20:20:34 +00003253 emsg_funcname(e_func_deleted, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 break;
3255 case FCERR_TOOMANY:
Bram Moolenaare1242042021-12-16 20:56:57 +00003256 emsg_funcname((char *)e_too_many_arguments_for_function_str,
3257 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 break;
3259 case FCERR_TOOFEW:
Bram Moolenaare1242042021-12-16 20:56:57 +00003260 emsg_funcname((char *)e_not_enough_arguments_for_function_str,
3261 name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 break;
3263 case FCERR_SCRIPT:
3264 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00003265 e_using_sid_not_in_script_context_str, name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 break;
3267 case FCERR_DICT:
3268 emsg_funcname(
3269 N_("E725: Calling dict function without Dictionary: %s"),
3270 name);
3271 break;
3272 }
3273}
3274
3275/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003276 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003277 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003278 * Return FAIL when the function can't be called, OK otherwise.
3279 * Also returns OK when an error was encountered while executing the function.
3280 */
3281 int
3282call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003283 char_u *funcname, // name of the function
3284 int len, // length of "name" or -1 to use strlen()
3285 typval_T *rettv, // return value goes here
3286 int argcount_in, // number of "argvars"
3287 typval_T *argvars_in, // vars for arguments, must have "argcount"
3288 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003289 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003290{
3291 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01003292 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003293 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003294 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003295 char_u fname_buf[FLEN_FIXED + 1];
3296 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003297 char_u *fname = NULL;
3298 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003299 int argcount = argcount_in;
3300 typval_T *argvars = argvars_in;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003301 dict_T *selfdict = funcexe->fe_selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003302 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003303 // "funcexe->fe_basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003304 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003305 int argv_base = 0;
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003306 partial_T *partial = funcexe->fe_partial;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003307 type_T check_type;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003308
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02003309 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
3310 // even when call_func() returns FAIL.
3311 rettv->v_type = VAR_UNKNOWN;
3312
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003313 if (partial != NULL)
3314 fp = partial->pt_func;
3315 if (fp == NULL)
3316 {
3317 // Make a copy of the name, if it comes from a funcref variable it
3318 // could be changed or deleted in the called function.
3319 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
3320 if (name == NULL)
3321 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003322
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003323 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3324 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003325
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003326 if (funcexe->fe_doesrange != NULL)
3327 *funcexe->fe_doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003328
3329 if (partial != NULL)
3330 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003331 // When the function has a partial with a dict and there is a dict
3332 // argument, use the dict argument. That is backwards compatible.
3333 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003334 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003335 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01003336 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003337 {
3338 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003339 {
3340 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
3341 {
Bram Moolenaaref140542019-12-31 21:27:13 +01003342 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003343 goto theend;
3344 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003345 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003346 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003347 for (i = 0; i < argcount_in; ++i)
3348 argv[i + argv_clear] = argvars_in[i];
3349 argvars = argv;
3350 argcount = partial->pt_argc + argcount_in;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003351
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003352 if (funcexe->fe_check_type != NULL
3353 && funcexe->fe_check_type->tt_argcount != -1)
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003354 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003355 // Now funcexe->fe_check_type is missing the added arguments,
3356 // make a copy of the type with the correction.
3357 check_type = *funcexe->fe_check_type;
3358 funcexe->fe_check_type = &check_type;
Bram Moolenaar97f227d2021-07-04 20:20:52 +02003359 check_type.tt_argcount += partial->pt_argc;
3360 check_type.tt_min_argcount += partial->pt_argc;
3361 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003362 }
3363 }
3364
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003365 if (error == FCERR_NONE && funcexe->fe_check_type != NULL
3366 && funcexe->fe_evaluate)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003367 {
3368 // Check that the argument types are OK for the types of the funcref.
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003369 if (check_argument_types(funcexe->fe_check_type, argvars, argcount,
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01003370 (name != NULL) ? name : funcname) == FAIL)
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003371 error = FCERR_OTHER;
3372 }
3373
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003374 if (error == FCERR_NONE && funcexe->fe_evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003375 {
3376 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003377 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003378
Bram Moolenaar333894b2020-08-01 18:53:07 +02003379 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003380 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02003381 {
3382 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003383 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02003384 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003385
Bram Moolenaare38eab22019-12-05 21:50:01 +01003386 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003387 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01003388 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003389
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003390 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003391 {
3392 /*
3393 * User defined function.
3394 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02003395 if (fp == NULL)
Bram Moolenaar333894b2020-08-01 18:53:07 +02003396 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003397
Bram Moolenaare38eab22019-12-05 21:50:01 +01003398 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003399 if (fp == NULL
3400 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003401 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003402 && !aborting())
3403 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003404 // executed an autocommand, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02003405 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003406 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003407 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003408 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
3409 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003410 // loaded a package, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02003411 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003412 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003413 if (fp == NULL)
3414 {
3415 char_u *p = untrans_function_name(rfname);
3416
3417 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02003418 // Don't do this if the name starts with "s:".
3419 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaar333894b2020-08-01 18:53:07 +02003420 fp = find_func(p, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003421 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003422
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003423 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01003424 error = FCERR_DELETED;
Bram Moolenaar801ab062020-06-25 19:27:56 +02003425#ifdef FEAT_LUA
3426 else if (fp != NULL && (fp->uf_flags & FC_CFUNC))
3427 {
3428 cfunc_T cb = fp->uf_cb;
3429
3430 error = (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
3431 }
3432#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003433 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003434 {
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003435 if (funcexe->fe_argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01003436 // postponed filling in the arguments, do it now
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003437 argcount = funcexe->fe_argv_func(argcount, argvars,
3438 argv_clear, fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02003439
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003440 if (funcexe->fe_basetv != NULL)
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003441 {
3442 // Method call: base->Method()
3443 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003444 argv[0] = *funcexe->fe_basetv;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003445 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003446 argvars = argv;
3447 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003448 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 error = call_user_func_check(fp, argcount, argvars, rettv,
3451 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003452 }
3453 }
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003454 else if (funcexe->fe_basetv != NULL)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003455 {
3456 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003457 * expr->method(): Find the method name in the table, call its
3458 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02003459 */
3460 error = call_internal_method(fname, argcount, argvars, rettv,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00003461 funcexe->fe_basetv);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003462 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003463 else
3464 {
3465 /*
3466 * Find the function name in the table, call its implementation.
3467 */
3468 error = call_internal_func(fname, argcount, argvars, rettv);
3469 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02003470
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003471 /*
3472 * The function call (or "FuncUndefined" autocommand sequence) might
3473 * have been aborted by an error, an interrupt, or an explicitly thrown
3474 * exception that has not been caught so far. This situation can be
3475 * tested for by calling aborting(). For an error in an internal
3476 * function or for the "E132" error in call_user_func(), however, the
3477 * throw point at which the "force_abort" flag (temporarily reset by
3478 * emsg()) is normally updated has not been reached yet. We need to
3479 * update that flag first to make aborting() reliable.
3480 */
3481 update_force_abort();
3482 }
Bram Moolenaaref140542019-12-31 21:27:13 +01003483 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003484 ret = OK;
3485
Bram Moolenaar4c054e92019-11-10 00:13:50 +01003486theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003487 /*
3488 * Report an error unless the argument evaluation or function call has been
3489 * cancelled due to an aborting error, an interrupt, or an exception.
3490 */
3491 if (!aborting())
3492 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003493 user_func_error(error, (name != NULL) ? name : funcname, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003494 }
3495
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003496 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003497 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003498 clear_tv(&argv[--argv_clear + argv_base]);
3499
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003500 vim_free(tofree);
3501 vim_free(name);
3502
3503 return ret;
3504}
3505
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003506 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507printable_func_name(ufunc_T *fp)
3508{
3509 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
3510}
3511
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003512/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003513 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003514 */
3515 static void
3516list_func_head(ufunc_T *fp, int indent)
3517{
3518 int j;
3519
3520 msg_start();
3521 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003522 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003523 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003524 msg_puts("def ");
3525 else
3526 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003528 msg_putchar('(');
3529 for (j = 0; j < fp->uf_args.ga_len; ++j)
3530 {
3531 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003532 msg_puts(", ");
3533 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 if (fp->uf_arg_types != NULL)
3535 {
3536 char *tofree;
3537
3538 msg_puts(": ");
3539 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
3540 vim_free(tofree);
3541 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003542 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
3543 {
3544 msg_puts(" = ");
3545 msg_puts(((char **)(fp->uf_def_args.ga_data))
3546 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
3547 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003548 }
3549 if (fp->uf_varargs)
3550 {
3551 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003552 msg_puts(", ");
3553 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003554 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 if (fp->uf_va_name != NULL)
3556 {
3557 if (j)
3558 msg_puts(", ");
3559 msg_puts("...");
3560 msg_puts((char *)fp->uf_va_name);
Bram Moolenaar2a389082021-04-09 20:24:31 +02003561 if (fp->uf_va_type != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 {
3563 char *tofree;
3564
3565 msg_puts(": ");
3566 msg_puts(type_name(fp->uf_va_type, &tofree));
3567 vim_free(tofree);
3568 }
3569 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003570 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003571
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003572 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01003573 {
3574 if (fp->uf_ret_type != &t_void)
3575 {
3576 char *tofree;
3577
3578 msg_puts(": ");
3579 msg_puts(type_name(fp->uf_ret_type, &tofree));
3580 vim_free(tofree);
3581 }
3582 }
3583 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003584 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003585 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003586 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003587 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003588 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003589 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003590 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003591 msg_clr_eos();
3592 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003593 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003594}
3595
3596/*
3597 * Get a function name, translating "<SID>" and "<SNR>".
3598 * Also handles a Funcref in a List or Dictionary.
3599 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003600 * Set "*is_global" to TRUE when the function must be global, unless
3601 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003602 * flags:
3603 * TFN_INT: internal function name OK
3604 * TFN_QUIET: be quiet
3605 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003606 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003607 * Advances "pp" to just after the function name (if no error).
3608 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003609 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003610trans_function_name(
3611 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003612 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003613 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003614 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01003615 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003616 partial_T **partial, // return: partial of a FuncRef
3617 type_T **type) // return: type of funcref if not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003618{
3619 char_u *name = NULL;
3620 char_u *start;
3621 char_u *end;
3622 int lead;
3623 char_u sid_buf[20];
3624 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003626 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 int vim9script;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003628
3629 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02003630 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003631 start = *pp;
3632
Bram Moolenaare38eab22019-12-05 21:50:01 +01003633 // Check for hard coded <SNR>: already translated function ID (from a user
3634 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003635 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
3636 && (*pp)[2] == (int)KE_SNR)
3637 {
3638 *pp += 3;
3639 len = get_id_len(pp) + 3;
3640 return vim_strnsave(start, len);
3641 }
3642
Bram Moolenaare38eab22019-12-05 21:50:01 +01003643 // A name starting with "<SID>" or "<SNR>" is local to a script. But
3644 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003645 lead = eval_fname_script(start);
3646 if (lead > 2)
3647 start += lead;
3648
Bram Moolenaare38eab22019-12-05 21:50:01 +01003649 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01003650 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003651 lead > 2 ? 0 : FNE_CHECK_START);
3652 if (end == start)
3653 {
3654 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00003655 emsg(_(e_function_name_required));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003656 goto theend;
3657 }
3658 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
3659 {
3660 /*
3661 * Report an invalid expression in braces, unless the expression
3662 * evaluation has been cancelled due to an aborting error, an
3663 * interrupt, or an exception.
3664 */
3665 if (!aborting())
3666 {
3667 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003668 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003669 }
3670 else
3671 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
3672 goto theend;
3673 }
3674
3675 if (lv.ll_tv != NULL)
3676 {
3677 if (fdp != NULL)
3678 {
3679 fdp->fd_dict = lv.ll_dict;
3680 fdp->fd_newkey = lv.ll_newkey;
3681 lv.ll_newkey = NULL;
3682 fdp->fd_di = lv.ll_di;
3683 }
3684 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
3685 {
3686 name = vim_strsave(lv.ll_tv->vval.v_string);
3687 *pp = end;
3688 }
3689 else if (lv.ll_tv->v_type == VAR_PARTIAL
3690 && lv.ll_tv->vval.v_partial != NULL)
3691 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003692 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003693 *pp = end;
3694 if (partial != NULL)
3695 *partial = lv.ll_tv->vval.v_partial;
3696 }
3697 else
3698 {
3699 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
3700 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003701 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003702 else
3703 *pp = end;
3704 name = NULL;
3705 }
3706 goto theend;
3707 }
3708
3709 if (lv.ll_name == NULL)
3710 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003711 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003712 *pp = end;
3713 goto theend;
3714 }
3715
Bram Moolenaare38eab22019-12-05 21:50:01 +01003716 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003717 if (lv.ll_exp_name != NULL)
3718 {
3719 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003720 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003721 flags & TFN_NO_AUTOLOAD, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003722 if (name == lv.ll_exp_name)
3723 name = NULL;
3724 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003725 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003726 {
3727 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003728 name = deref_func_name(*pp, &len, partial, type,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003729 flags & TFN_NO_AUTOLOAD, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003730 if (name == *pp)
3731 name = NULL;
3732 }
3733 if (name != NULL)
3734 {
3735 name = vim_strsave(name);
3736 *pp = end;
3737 if (STRNCMP(name, "<SNR>", 5) == 0)
3738 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003739 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003740 name[0] = K_SPECIAL;
3741 name[1] = KS_EXTRA;
3742 name[2] = (int)KE_SNR;
3743 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
3744 }
3745 goto theend;
3746 }
3747
3748 if (lv.ll_exp_name != NULL)
3749 {
3750 len = (int)STRLEN(lv.ll_exp_name);
3751 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
3752 && STRNCMP(lv.ll_name, "s:", 2) == 0)
3753 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003754 // When there was "s:" already or the name expanded to get a
3755 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003756 lv.ll_name += 2;
3757 len -= 2;
3758 lead = 2;
3759 }
3760 }
3761 else
3762 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003763 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003764 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003765 {
3766 if (is_global != NULL && lv.ll_name[0] == 'g')
3767 *is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003768 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003769 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003770 len = (int)(end - lv.ll_name);
3771 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003772 if (len <= 0)
3773 {
3774 if (!skip)
Bram Moolenaarc553a212021-12-26 20:20:34 +00003775 emsg(_(e_function_name_required));
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02003776 goto theend;
3777 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003778
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003779 // In Vim9 script a user function is script-local by default, unless it
3780 // starts with a lower case character: dict.func().
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003781 vim9script = ASCII_ISUPPER(*start) && in_vim9script();
Bram Moolenaar17f700a2020-12-19 21:23:42 +01003782 if (vim9script)
3783 {
3784 char_u *p;
3785
3786 // SomeScript#func() is a global function.
3787 for (p = start; *p != NUL && *p != '('; ++p)
3788 if (*p == AUTOLOAD_CHAR)
3789 vim9script = FALSE;
3790 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003792 /*
3793 * Copy the function name to allocated memory.
3794 * Accept <SID>name() inside a script, translate into <SNR>123_name().
3795 * Accept <SNR>123_name() outside a script.
3796 */
3797 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003798 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003800 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 if (!vim9script)
3802 lead = 3;
3803 if (vim9script || (lv.ll_exp_name != NULL
3804 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003805 || eval_fname_sid(*pp))
3806 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003808 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003809 {
Bram Moolenaar40bcec12021-12-05 22:19:27 +00003810 emsg(_(e_using_sid_not_in_script_context));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003811 goto theend;
3812 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003813 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 if (vim9script)
3815 extra = 3 + (int)STRLEN(sid_buf);
3816 else
3817 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003818 }
3819 }
Bram Moolenaar22f17a22021-06-21 20:48:58 +02003820 else if (!(flags & TFN_INT) && (builtin_function(lv.ll_name, len)
3821 || (in_vim9script() && *lv.ll_name == '_')))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003822 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00003823 semsg(_(e_function_name_must_start_with_capital_or_s_str), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003824 goto theend;
3825 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003826 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003827 {
3828 char_u *cp = vim_strchr(lv.ll_name, ':');
3829
3830 if (cp != NULL && cp < end)
3831 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003832 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003833 goto theend;
3834 }
3835 }
3836
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003838 if (name != NULL)
3839 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01003840 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003841 {
3842 name[0] = K_SPECIAL;
3843 name[1] = KS_EXTRA;
3844 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003846 STRCPY(name + 3, sid_buf);
3847 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
3849 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003850 }
3851 *pp = end;
3852
3853theend:
3854 clear_lval(&lv);
3855 return name;
3856}
3857
3858/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02003859 * Assuming "name" is the result of trans_function_name() and it was prefixed
3860 * to use the script-local name, return the unmodified name (points into
3861 * "name"). Otherwise return NULL.
3862 * This can be used to first search for a script-local function and fall back
3863 * to the global function if not found.
3864 */
3865 char_u *
3866untrans_function_name(char_u *name)
3867{
3868 char_u *p;
3869
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003870 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02003871 {
3872 p = vim_strchr(name, '_');
3873 if (p != NULL)
3874 return p + 1;
3875 }
3876 return NULL;
3877}
3878
3879/*
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00003880 * If the 'funcname' starts with "s:" or "<SID>", then expands it to the
3881 * current script ID and returns the expanded function name. The caller should
3882 * free the returned name. If not called from a script context or the function
3883 * name doesn't start with these prefixes, then returns NULL.
3884 * This doesn't check whether the script-local function exists or not.
3885 */
3886 char_u *
3887get_scriptlocal_funcname(char_u *funcname)
3888{
3889 char sid_buf[25];
3890 int off;
3891 char_u *newname;
3892
3893 if (funcname == NULL)
3894 return NULL;
3895
3896 if (STRNCMP(funcname, "s:", 2) != 0
3897 && STRNCMP(funcname, "<SID>", 5) != 0)
3898 // The function name is not a script-local function name
3899 return NULL;
3900
3901 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
3902 {
3903 emsg(_(e_using_sid_not_in_script_context));
3904 return NULL;
3905 }
3906 // Expand s: prefix into <SNR>nr_<name>
3907 vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
3908 (long)current_sctx.sc_sid);
3909 off = *funcname == 's' ? 2 : 5;
3910 newname = alloc(STRLEN(sid_buf) + STRLEN(funcname + off) + 1);
3911 if (newname == NULL)
3912 return NULL;
3913 STRCPY(newname, sid_buf);
3914 STRCAT(newname, funcname + off);
3915
3916 return newname;
3917}
3918
3919/*
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00003920 * Call trans_function_name(), except that a lambda is returned as-is.
3921 * Returns the name in allocated memory.
3922 */
3923 char_u *
3924save_function_name(
3925 char_u **name,
3926 int *is_global,
3927 int skip,
3928 int flags,
3929 funcdict_T *fudi)
3930{
3931 char_u *p = *name;
3932 char_u *saved;
3933
3934 if (STRNCMP(p, "<lambda>", 8) == 0)
3935 {
3936 p += 8;
3937 (void)getdigits(&p);
3938 saved = vim_strnsave(*name, p - *name);
3939 if (fudi != NULL)
3940 CLEAR_POINTER(fudi);
3941 }
3942 else
3943 saved = trans_function_name(&p, is_global, skip,
3944 flags, fudi, NULL, NULL);
3945 *name = p;
3946 return saved;
3947}
3948
3949/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003950 * List functions. When "regmatch" is NULL all of then.
3951 * Otherwise functions matching "regmatch".
3952 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003953 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003954list_functions(regmatch_T *regmatch)
3955{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003956 int changed = func_hashtab.ht_changed;
3957 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003958 hashitem_T *hi;
3959
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003960 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003961 {
3962 if (!HASHITEM_EMPTY(hi))
3963 {
3964 ufunc_T *fp = HI2UF(hi);
3965
3966 --todo;
3967 if ((fp->uf_flags & FC_DEAD) == 0
3968 && (regmatch == NULL
3969 ? !message_filtered(fp->uf_name)
3970 && !func_name_refcount(fp->uf_name)
3971 : !isdigit(*fp->uf_name)
3972 && vim_regexec(regmatch, fp->uf_name, 0)))
3973 {
3974 list_func_head(fp, FALSE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003975 if (changed != func_hashtab.ht_changed)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003976 {
3977 emsg(_("E454: function list was modified"));
3978 return;
3979 }
3980 }
3981 }
3982 }
3983}
3984
3985/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02003986 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003987 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
3988 * the function name.
Bram Moolenaar04b12692020-05-04 23:24:44 +02003989 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003990 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02003991 ufunc_T *
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00003992define_function(exarg_T *eap, char_u *name_arg, char_u **line_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003993{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003994 int j;
3995 int c;
3996 int saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003997 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003998 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003999 char_u *p;
4000 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004001 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004002 char_u *line_arg = NULL;
4003 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004005 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004006 garray_T newlines;
4007 int varargs = FALSE;
4008 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004010 ufunc_T *fp = NULL;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004011 int fp_allocated = FALSE;
4012 int free_fp = FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004013 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004014 dictitem_T *v;
4015 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004016 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004017 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004018 hashitem_T *hi;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004019 linenr_T sourcing_lnum_top;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004020 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02004021 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004022
4023 /*
4024 * ":function" without argument: list functions.
4025 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004026 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004027 {
4028 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004029 list_functions(NULL);
Bram Moolenaar63b91732021-08-05 20:40:03 +02004030 set_nextcmd(eap, eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004031 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004032 }
4033
4034 /*
4035 * ":function /pat": list functions matching pattern.
4036 */
4037 if (*eap->arg == '/')
4038 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02004039 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004040 if (!eap->skip)
4041 {
4042 regmatch_T regmatch;
4043
4044 c = *p;
4045 *p = NUL;
4046 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
4047 *p = c;
4048 if (regmatch.regprog != NULL)
4049 {
4050 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02004051 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004052 vim_regfree(regmatch.regprog);
4053 }
4054 }
4055 if (*p == '/')
4056 ++p;
Bram Moolenaar63b91732021-08-05 20:40:03 +02004057 set_nextcmd(eap, p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004058 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004059 }
4060
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 ga_init(&newargs);
4062 ga_init(&argtypes);
4063 ga_init(&default_args);
4064
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004065 /*
4066 * Get the function name. There are these situations:
4067 * func normal function name
4068 * "name" == func, "fudi.fd_dict" == NULL
4069 * dict.func new dictionary entry
4070 * "name" == NULL, "fudi.fd_dict" set,
4071 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
4072 * dict.func existing dict entry with a Funcref
4073 * "name" == func, "fudi.fd_dict" set,
4074 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4075 * dict.func existing dict entry that's not a Funcref
4076 * "name" == NULL, "fudi.fd_dict" set,
4077 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
4078 * s:func script-local function name
4079 * g:func global function name, same as "func"
4080 */
4081 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004082 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004083 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004084 // nested function, argument is (args).
4085 paren = TRUE;
4086 CLEAR_FIELD(fudi);
4087 }
4088 else
4089 {
Bram Moolenaareba3b7f2021-11-30 18:25:08 +00004090 name = save_function_name(&p, &is_global, eap->skip,
4091 TFN_NO_AUTOLOAD, &fudi);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004092 paren = (vim_strchr(p, '(') != NULL);
4093 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004094 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02004095 /*
4096 * Return on an invalid expression in braces, unless the expression
4097 * evaluation has been cancelled due to an aborting error, an
4098 * interrupt, or an exception.
4099 */
4100 if (!aborting())
4101 {
4102 if (!eap->skip && fudi.fd_newkey != NULL)
4103 semsg(_(e_dictkey), fudi.fd_newkey);
4104 vim_free(fudi.fd_newkey);
4105 return NULL;
4106 }
4107 else
4108 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004109 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004110 }
4111
Bram Moolenaare38eab22019-12-05 21:50:01 +01004112 // An error in a function call during evaluation of an expression in magic
4113 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004114 saved_did_emsg = did_emsg;
4115 did_emsg = FALSE;
4116
4117 /*
4118 * ":function func" with only function name: list function.
4119 */
4120 if (!paren)
4121 {
4122 if (!ends_excmd(*skipwhite(p)))
4123 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004124 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004125 goto ret_free;
4126 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004127 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004128 if (eap->nextcmd != NULL)
4129 *p = NUL;
4130 if (!eap->skip && !got_int)
4131 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004132 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004133 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
4134 {
4135 char_u *up = untrans_function_name(name);
4136
4137 // With Vim9 script the name was made script-local, if not
4138 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02004139 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004140 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02004141 }
4142
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004143 if (fp != NULL)
4144 {
4145 list_func_head(fp, TRUE);
4146 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
4147 {
4148 if (FUNCLINE(fp, j) == NULL)
4149 continue;
4150 msg_putchar('\n');
4151 msg_outnum((long)(j + 1));
4152 if (j < 9)
4153 msg_putchar(' ');
4154 if (j < 99)
4155 msg_putchar(' ');
4156 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004157 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004158 ui_breakcheck();
4159 }
4160 if (!got_int)
4161 {
4162 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004163 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 msg_puts(" enddef");
4165 else
4166 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004167 }
4168 }
4169 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004170 emsg_funcname(e_undefined_function_str, eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004171 }
4172 goto ret_free;
4173 }
4174
4175 /*
4176 * ":function name(arg1, arg2)" Define function.
4177 */
4178 p = skipwhite(p);
4179 if (*p != '(')
4180 {
4181 if (!eap->skip)
4182 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004183 semsg(_(e_missing_paren_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004184 goto ret_free;
4185 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004186 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004187 if (vim_strchr(p, '(') != NULL)
4188 p = vim_strchr(p, '(');
4189 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004190
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004191 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
4192 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01004193 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01004194 goto ret_free;
4195 }
4196
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02004197 // In Vim9 script only global functions can be redefined.
4198 if (vim9script && eap->forceit && !is_global)
4199 {
4200 emsg(_(e_nobang));
4201 goto ret_free;
4202 }
4203
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004204 ga_init2(&newlines, (int)sizeof(char_u *), 10);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004205
Bram Moolenaar04b12692020-05-04 23:24:44 +02004206 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004207 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004208 // Check the name of the function. Unless it's a dictionary function
4209 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004210 if (name != NULL)
4211 arg = name;
4212 else
4213 arg = fudi.fd_newkey;
4214 if (arg != NULL && (fudi.fd_di == NULL
4215 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
4216 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
4217 {
Bram Moolenaar052ff292021-12-11 13:54:46 +00004218 char_u *name_base = arg;
4219 int i;
4220
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004221 if (*arg == K_SPECIAL)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004222 {
4223 name_base = vim_strchr(arg, '_');
4224 if (name_base == NULL)
4225 name_base = arg + 3;
4226 else
4227 ++name_base;
4228 }
4229 for (i = 0; name_base[i] != NUL && (i == 0
4230 ? eval_isnamec1(name_base[i])
4231 : eval_isnamec(name_base[i])); ++i)
4232 ;
4233 if (name_base[i] != NUL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004234 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaar052ff292021-12-11 13:54:46 +00004235
4236 // In Vim9 script a function cannot have the same name as a
4237 // variable.
4238 if (vim9script && *arg == K_SPECIAL
Mike Williams1821d142021-12-15 16:38:33 +00004239 && eval_variable(name_base, (int)STRLEN(name_base), NULL, NULL,
Bram Moolenaar052ff292021-12-11 13:54:46 +00004240 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT
4241 + EVAL_VAR_NO_FUNC) == OK)
4242 {
4243 semsg(_(e_redefining_script_item_str), name_base);
4244 goto ret_free;
4245 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004246 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004247 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004248 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaar052ff292021-12-11 13:54:46 +00004249 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004250 emsg(_("E862: Cannot use g: here"));
Bram Moolenaar052ff292021-12-11 13:54:46 +00004251 goto ret_free;
4252 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004253 }
4254
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004255 // This may get more lines and make the pointers into the first line
4256 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01004257 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004259 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01004260 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00004261 eap, line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004262 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01004263 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004264
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004266 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004267 // find the return type: :def Func(): type
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004268 if (*skipwhite(p) == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004269 {
Bram Moolenaar33ea9fd2021-08-08 19:07:37 +02004270 if (*p != ':')
4271 {
4272 semsg(_(e_no_white_space_allowed_before_colon_str), p);
4273 p = skipwhite(p);
4274 }
4275 else if (!IS_WHITE_OR_NUL(p[1]))
4276 semsg(_(e_white_space_required_after_str_str), ":", p);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02004278 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004280 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004281 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01004282 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004284 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004286 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004287 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02004288 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004289 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004290 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02004291 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004292 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 else
4294 // find extra arguments "range", "dict", "abort" and "closure"
4295 for (;;)
4296 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01004297 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 p = skipwhite(p);
4299 if (STRNCMP(p, "range", 5) == 0)
4300 {
4301 flags |= FC_RANGE;
4302 p += 5;
4303 }
4304 else if (STRNCMP(p, "dict", 4) == 0)
4305 {
4306 flags |= FC_DICT;
4307 p += 4;
4308 }
4309 else if (STRNCMP(p, "abort", 5) == 0)
4310 {
4311 flags |= FC_ABORT;
4312 p += 5;
4313 }
4314 else if (STRNCMP(p, "closure", 7) == 0)
4315 {
4316 flags |= FC_CLOSURE;
4317 p += 7;
4318 if (current_funccal == NULL)
4319 {
4320 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
4321 name == NULL ? (char_u *)"" : name);
4322 goto erret;
4323 }
4324 }
4325 else
4326 break;
4327 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004328
Bram Moolenaare38eab22019-12-05 21:50:01 +01004329 // When there is a line break use what follows for the function body.
4330 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004331 if (*p == '\n')
4332 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004333 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02004334 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
4335 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01004336 && !(VIM_ISWHITE(*whitep) && *p == '#'
4337 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02004338 && !eap->skip
4339 && !did_emsg)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004340 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004341
4342 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
4344 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004345 */
4346 if (KeyTyped)
4347 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004348 // Check if the function already exists, don't let the user type the
4349 // whole function before telling him it doesn't work! For a script we
4350 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004351 if (!eap->skip && !eap->forceit)
4352 {
4353 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004354 emsg(_(e_funcdict));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004355 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004356 emsg_funcname(e_function_str_already_exists_add_excl_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004357 }
4358
4359 if (!eap->skip && did_emsg)
4360 goto erret;
4361
Bram Moolenaare38eab22019-12-05 21:50:01 +01004362 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004363 cmdline_row = msg_row;
4364 }
4365
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004366 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004367 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004368
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004369 // Do not define the function when getting the body fails and when
4370 // skipping.
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00004371 if (get_function_body(eap, &newlines, line_arg, line_to_free) == FAIL
Bram Moolenaard87c21a2021-05-18 13:40:33 +02004372 || eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004373 goto erret;
4374
4375 /*
4376 * If there are no errors, add the function
4377 */
4378 if (fudi.fd_dict == NULL)
4379 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380 hashtab_T *ht;
4381
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004382 v = find_var(name, &ht, TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004383 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
4384 {
4385 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
4386 name);
4387 goto erret;
4388 }
4389
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004390 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaareef21022020-08-01 22:16:43 +02004391 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004392 {
Bram Moolenaareef21022020-08-01 22:16:43 +02004393 char_u *uname = untrans_function_name(name);
4394
4395 import = find_imported(uname == NULL ? name : uname, 0, NULL);
4396 }
4397
4398 if (fp != NULL || import != NULL)
4399 {
4400 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004402 // Function can be replaced with "function!" and when sourcing the
4403 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02004404 // A name that is used by an import can not be overruled.
4405 if (import != NULL
4406 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004407 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02004408 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004409 {
Bram Moolenaard604d782021-11-20 21:46:20 +00004410 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaareef21022020-08-01 22:16:43 +02004411 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02004412 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02004413 else
Bram Moolenaarc553a212021-12-26 20:20:34 +00004414 emsg_funcname(e_function_str_already_exists_add_excl_to_replace, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004415 goto erret;
4416 }
4417 if (fp->uf_calls > 0)
4418 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01004419 emsg_funcname(
Bram Moolenaarc553a212021-12-26 20:20:34 +00004420 e_cannot_redefine_function_str_it_is_in_use, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004421 goto erret;
4422 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004423 if (fp->uf_refcount > 1)
4424 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004425 // This function is referenced somewhere, don't redefine it but
4426 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004427 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004428 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004429 fp = NULL;
4430 overwrite = TRUE;
4431 }
4432 else
4433 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004434 char_u *exp_name = fp->uf_name_exp;
4435
4436 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01004437 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004438 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004439 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01004440 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02004442#ifdef FEAT_PROFILE
4443 fp->uf_profiling = FALSE;
4444 fp->uf_prof_initialized = FALSE;
4445#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01004446 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004447 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004448 }
4449 }
4450 else
4451 {
4452 char numbuf[20];
4453
4454 fp = NULL;
4455 if (fudi.fd_newkey == NULL && !eap->forceit)
4456 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004457 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004458 goto erret;
4459 }
4460 if (fudi.fd_di == NULL)
4461 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004462 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02004463 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004464 goto erret;
4465 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004466 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02004467 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004468 goto erret;
4469
Bram Moolenaare38eab22019-12-05 21:50:01 +01004470 // Give the function a sequential number. Can only be used with a
4471 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004472 vim_free(name);
4473 sprintf(numbuf, "%d", ++func_nr);
4474 name = vim_strsave((char_u *)numbuf);
4475 if (name == NULL)
4476 goto erret;
4477 }
4478
4479 if (fp == NULL)
4480 {
4481 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
4482 {
4483 int slen, plen;
4484 char_u *scriptname;
4485
Bram Moolenaare38eab22019-12-05 21:50:01 +01004486 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004487 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004488 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004489 {
4490 scriptname = autoload_name(name);
4491 if (scriptname != NULL)
4492 {
4493 p = vim_strchr(scriptname, '/');
4494 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004495 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004496 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004497 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004498 j = OK;
4499 vim_free(scriptname);
4500 }
4501 }
4502 if (j == FAIL)
4503 {
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004504 linenr_T save_lnum = SOURCING_LNUM;
4505
4506 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004507 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaarf48b2fa2021-04-12 22:02:36 +02004508 SOURCING_LNUM = save_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004509 goto erret;
4510 }
4511 }
4512
Bram Moolenaar47ed5532019-08-08 20:49:14 +02004513 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004514 if (fp == NULL)
4515 goto erret;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004516 fp_allocated = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004517
4518 if (fudi.fd_dict != NULL)
4519 {
4520 if (fudi.fd_di == NULL)
4521 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004522 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004523 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
4524 if (fudi.fd_di == NULL)
4525 {
4526 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004527 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004528 goto erret;
4529 }
4530 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
4531 {
4532 vim_free(fudi.fd_di);
4533 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02004534 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004535 goto erret;
4536 }
4537 }
4538 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004539 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004540 clear_tv(&fudi.fd_di->di_tv);
4541 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004542 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004543
Bram Moolenaare38eab22019-12-05 21:50:01 +01004544 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004545 flags |= FC_DICT;
4546 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004547 }
4548 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004549 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004550 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004551 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552
4553 if (eap->cmdidx == CMD_def)
4554 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02004555 int lnum_save = SOURCING_LNUM;
4556 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004557
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004558 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004559
Bram Moolenaarbfe12042020-02-04 21:54:07 +01004560 // error messages are for the first function line
4561 SOURCING_LNUM = sourcing_lnum_top;
4562
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02004563 // The function may use script variables from the context.
4564 function_using_block_scopes(fp, cstack);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004565
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004566 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004567 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004568 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004569 free_fp = fp_allocated;
4570 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01004572 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573
4574 // parse the return type, if any
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004575 if (parse_return_type(fp, ret_type) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 {
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004577 SOURCING_LNUM = lnum_save;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004578 free_fp = fp_allocated;
Bram Moolenaar7a6eaa02021-03-21 20:53:29 +01004579 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02004581 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004582 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02004583 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02004584 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004586 if (fp_allocated)
4587 {
4588 // insert the new function in the function list
4589 set_ufunc_name(fp, name);
4590 if (overwrite)
4591 {
4592 hi = hash_find(&func_hashtab, name);
4593 hi->hi_key = UF2HIKEY(fp);
4594 }
4595 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
4596 {
4597 free_fp = TRUE;
4598 goto erret;
4599 }
4600 fp->uf_refcount = 1;
4601 }
4602
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004603 fp->uf_lines = newlines;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004604 newlines.ga_data = NULL;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004605 if ((flags & FC_CLOSURE) != 0)
4606 {
Bram Moolenaar58016442016-07-31 18:30:22 +02004607 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004608 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004609 }
4610 else
4611 fp->uf_scoped = NULL;
4612
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004613#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004614 if (prof_def_func())
4615 func_do_profile(fp);
4616#endif
4617 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02004618 if (sandbox)
4619 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02004620 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004621 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004622 fp->uf_flags = flags;
4623 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01004624 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004625 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02004626 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02004627 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628 if (is_export)
4629 {
4630 fp->uf_flags |= FC_EXPORT;
4631 // let ex_export() know the export worked.
4632 is_export = FALSE;
4633 }
4634
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004635 if (eap->cmdidx == CMD_def)
4636 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02004637 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
4638 // :func does not use Vim9 script syntax, even in a Vim9 script file
4639 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02004640
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004641 goto ret_free;
4642
4643erret:
4644 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02004645 ga_clear_strings(&default_args);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004646 if (fp != NULL)
4647 {
4648 ga_init(&fp->uf_args);
4649 ga_init(&fp->uf_def_args);
4650 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004651errret_2:
4652 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01004653 if (fp != NULL)
4654 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004655 if (free_fp)
4656 {
4657 vim_free(fp);
4658 fp = NULL;
4659 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004660ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01004661 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004662 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02004663 if (name != name_arg)
4664 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02004665 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004666 did_emsg |= saved_did_emsg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004667
4668 return fp;
4669}
4670
4671/*
4672 * ":function"
4673 */
4674 void
4675ex_function(exarg_T *eap)
4676{
Bram Moolenaar9c23f9b2021-12-26 14:23:22 +00004677 char_u *line_to_free = NULL;
4678
4679 (void)define_function(eap, NULL, &line_to_free);
4680 vim_free(line_to_free);
Bram Moolenaar822ba242020-05-24 23:00:18 +02004681}
4682
4683/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02004684 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarb2049902021-01-24 12:53:53 +01004685 * be compiled. Except dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02004686 */
4687 void
4688ex_defcompile(exarg_T *eap UNUSED)
4689{
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004690 long todo = (long)func_hashtab.ht_used;
4691 int changed = func_hashtab.ht_changed;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004692 hashitem_T *hi;
4693 ufunc_T *ufunc;
4694
4695 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
4696 {
4697 if (!HASHITEM_EMPTY(hi))
4698 {
4699 --todo;
4700 ufunc = HI2UF(hi);
4701 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
Bram Moolenaar96f8f492020-09-09 17:08:51 +02004702 && ufunc->uf_def_status == UF_TO_BE_COMPILED
4703 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004704 {
Bram Moolenaare99d4222021-06-13 14:01:26 +02004705 (void)compile_def_function(ufunc, FALSE, CT_NONE, NULL);
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004706
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004707 if (func_hashtab.ht_changed != changed)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004708 {
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004709 // a function has been added or removed, need to start over
4710 todo = (long)func_hashtab.ht_used;
4711 changed = func_hashtab.ht_changed;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004712 hi = func_hashtab.ht_array;
Bram Moolenaar285b1892020-05-26 11:37:26 +02004713 --hi;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004714 }
4715 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02004716 }
4717 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004718}
4719
4720/*
4721 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
4722 * Return 2 if "p" starts with "s:".
4723 * Return 0 otherwise.
4724 */
4725 int
4726eval_fname_script(char_u *p)
4727{
Bram Moolenaare38eab22019-12-05 21:50:01 +01004728 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
4729 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004730 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
4731 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
4732 return 5;
4733 if (p[0] == 's' && p[1] == ':')
4734 return 2;
4735 return 0;
4736}
4737
4738 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004739translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004740{
4741 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004742 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004743 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744}
4745
4746/*
4747 * Return TRUE when "ufunc" has old-style "..." varargs
4748 * or named varargs "...name: type".
4749 */
4750 int
4751has_varargs(ufunc_T *ufunc)
4752{
4753 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004754}
4755
4756/*
4757 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004758 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004759 */
4760 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004761function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004762{
4763 char_u *nm = name;
4764 char_u *p;
4765 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004766 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004767 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004768
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004769 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
4770 if (no_deref)
4771 flag |= TFN_NO_DEREF;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004772 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004773 nm = skipwhite(nm);
4774
Bram Moolenaare38eab22019-12-05 21:50:01 +01004775 // Only accept "funcname", "funcname ", "funcname (..." and
4776 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004777 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004778 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004779 vim_free(p);
4780 return n;
4781}
4782
Bram Moolenaar113e1072019-01-20 15:30:40 +01004783#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004784 char_u *
4785get_expanded_name(char_u *name, int check)
4786{
4787 char_u *nm = name;
4788 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004789 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004790
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004791 p = trans_function_name(&nm, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004792 TFN_INT|TFN_QUIET, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004793
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004794 if (p != NULL && *nm == NUL
4795 && (!check || translated_function_exists(p, is_global)))
4796 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004797
4798 vim_free(p);
4799 return NULL;
4800}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004801#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004802
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004803/*
4804 * Function given to ExpandGeneric() to obtain the list of user defined
4805 * function names.
4806 */
4807 char_u *
4808get_user_func_name(expand_T *xp, int idx)
4809{
4810 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004811 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004812 static hashitem_T *hi;
4813 ufunc_T *fp;
4814
4815 if (idx == 0)
4816 {
4817 done = 0;
4818 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004819 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004820 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004821 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004822 {
4823 if (done++ > 0)
4824 ++hi;
4825 while (HASHITEM_EMPTY(hi))
4826 ++hi;
4827 fp = HI2UF(hi);
4828
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829 // don't show dead, dict and lambda functions
4830 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02004831 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004832 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004833
4834 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004835 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004836
4837 cat_func_name(IObuff, fp);
naohiro ono9aecf792021-08-28 15:56:06 +02004838 if (xp->xp_context != EXPAND_USER_FUNC
4839 && xp->xp_context != EXPAND_DISASSEMBLE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004840 {
4841 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004843 STRCAT(IObuff, ")");
4844 }
4845 return IObuff;
4846 }
4847 return NULL;
4848}
4849
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004850/*
4851 * ":delfunction {name}"
4852 */
4853 void
4854ex_delfunction(exarg_T *eap)
4855{
4856 ufunc_T *fp = NULL;
4857 char_u *p;
4858 char_u *name;
4859 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004860 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004861
4862 p = eap->arg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004863 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi,
4864 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004865 vim_free(fudi.fd_newkey);
4866 if (name == NULL)
4867 {
4868 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004869 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004870 return;
4871 }
4872 if (!ends_excmd(*skipwhite(p)))
4873 {
4874 vim_free(name);
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004875 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004876 return;
4877 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02004878 set_nextcmd(eap, p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004879 if (eap->nextcmd != NULL)
4880 *p = NUL;
4881
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004882 if (numbered_function(name) && fudi.fd_dict == NULL)
Bram Moolenaarddfc0512021-09-06 20:56:56 +02004883 {
4884 if (!eap->skip)
4885 semsg(_(e_invarg2), eap->arg);
4886 vim_free(name);
4887 return;
4888 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004889 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004890 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004891 vim_free(name);
4892
4893 if (!eap->skip)
4894 {
4895 if (fp == NULL)
4896 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02004897 if (!eap->forceit)
Bram Moolenaarc553a212021-12-26 20:20:34 +00004898 semsg(_(e_unknown_function_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004899 return;
4900 }
4901 if (fp->uf_calls > 0)
4902 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00004903 semsg(_(e_cannot_delete_function_str_it_is_in_use), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004904 return;
4905 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004906 if (fp->uf_flags & FC_VIM9)
4907 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004908 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004909 return;
4910 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004911
4912 if (fudi.fd_dict != NULL)
4913 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004914 // Delete the dict item that refers to the function, it will
4915 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004916 dictitem_remove(fudi.fd_dict, fudi.fd_di);
4917 }
4918 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004919 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004920 // A normal function (not a numbered function or lambda) has a
4921 // refcount of 1 for the entry in the hashtable. When deleting
4922 // it and the refcount is more than one, it should be kept.
4923 // A numbered function and lambda should be kept if the refcount is
4924 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004925 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004926 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004927 // Function is still referenced somewhere. Don't free it but
4928 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004929 if (func_remove(fp))
4930 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004931 }
4932 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004933 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004934 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004935 }
4936}
4937
4938/*
4939 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004940 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004941 */
4942 void
4943func_unref(char_u *name)
4944{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004945 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004946
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004947 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004948 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004949 fp = find_func(name, FALSE, NULL);
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004950 if (fp == NULL && numbered_function(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004951 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004952#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004953 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004954#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01004955 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004956 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004957 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004958}
4959
4960/*
4961 * Unreference a Function: decrement the reference count and free it when it
4962 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004963 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004964 */
4965 void
4966func_ptr_unref(ufunc_T *fp)
4967{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004968 if (fp != NULL && (--fp->uf_refcount <= 0
4969 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
4970 && fp->uf_partial->pt_refcount <= 1
4971 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02004972 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004973 // Only delete it when it's not being used. Otherwise it's done
4974 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02004975 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004976 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004977 }
4978}
4979
4980/*
4981 * Count a reference to a Function.
4982 */
4983 void
4984func_ref(char_u *name)
4985{
4986 ufunc_T *fp;
4987
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004988 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004989 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004990 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004991 if (fp != NULL)
4992 ++fp->uf_refcount;
Bram Moolenaar57bc2332021-12-15 12:06:43 +00004993 else if (numbered_function(name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01004994 // Only give an error for a numbered function.
4995 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01004996 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004997}
4998
4999/*
5000 * Count a reference to a Function.
5001 */
5002 void
5003func_ptr_ref(ufunc_T *fp)
5004{
5005 if (fp != NULL)
5006 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005007}
5008
5009/*
5010 * Return TRUE if items in "fc" do not have "copyID". That means they are not
5011 * referenced from anywhere that is in use.
5012 */
5013 static int
5014can_free_funccal(funccall_T *fc, int copyID)
5015{
5016 return (fc->l_varlist.lv_copyID != copyID
5017 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005018 && fc->l_avars.dv_copyID != copyID
5019 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005020}
5021
5022/*
5023 * ":return [expr]"
5024 */
5025 void
5026ex_return(exarg_T *eap)
5027{
5028 char_u *arg = eap->arg;
5029 typval_T rettv;
5030 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005031 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005032
5033 if (current_funccal == NULL)
5034 {
Bram Moolenaarc553a212021-12-26 20:20:34 +00005035 emsg(_(e_return_not_inside_function));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005036 return;
5037 }
5038
Bram Moolenaar844fb642021-10-23 13:32:30 +01005039 init_evalarg(&evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02005040 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
5041
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005042 if (eap->skip)
5043 ++emsg_skip;
5044
5045 eap->nextcmd = NULL;
5046 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005047 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005048 {
5049 if (!eap->skip)
5050 returning = do_return(eap, FALSE, TRUE, &rettv);
5051 else
5052 clear_tv(&rettv);
5053 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01005054 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005055 else if (!eap->skip)
5056 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005057 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01005058 update_force_abort();
5059
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005060 /*
5061 * Return unless the expression evaluation has been cancelled due to an
5062 * aborting error, an interrupt, or an exception.
5063 */
5064 if (!aborting())
5065 returning = do_return(eap, FALSE, TRUE, NULL);
5066 }
5067
Bram Moolenaare38eab22019-12-05 21:50:01 +01005068 // When skipping or the return gets pending, advance to the next command
5069 // in this line (!returning). Otherwise, ignore the rest of the line.
5070 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005071 if (returning)
5072 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005073 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaar63b91732021-08-05 20:40:03 +02005074 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005075
5076 if (eap->skip)
5077 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02005078 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005079}
5080
5081/*
5082 * ":1,25call func(arg1, arg2)" function call.
5083 */
5084 void
5085ex_call(exarg_T *eap)
5086{
5087 char_u *arg = eap->arg;
5088 char_u *startarg;
5089 char_u *name;
5090 char_u *tofree;
5091 int len;
5092 typval_T rettv;
5093 linenr_T lnum;
5094 int doesrange;
5095 int failed = FALSE;
5096 funcdict_T fudi;
5097 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005098 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005099 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005100 int found_var = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005101
Bram Moolenaare6b53242020-07-01 17:28:33 +02005102 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005103 if (eap->skip)
5104 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005105 // trans_function_name() doesn't work well when skipping, use eval0()
5106 // instead to skip to any following command, e.g. for:
5107 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005108 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005109 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005110 clear_tv(&rettv);
5111 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005112 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005113 return;
5114 }
5115
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005116 tofree = trans_function_name(&arg, NULL, eap->skip, TFN_INT,
5117 &fudi, &partial, in_vim9script() ? &type : NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005118 if (fudi.fd_newkey != NULL)
5119 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005120 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005121 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005122 vim_free(fudi.fd_newkey);
5123 }
5124 if (tofree == NULL)
5125 return;
5126
Bram Moolenaare38eab22019-12-05 21:50:01 +01005127 // Increase refcount on dictionary, it could get deleted when evaluating
5128 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005129 if (fudi.fd_dict != NULL)
5130 ++fudi.fd_dict->dv_refcount;
5131
Bram Moolenaare38eab22019-12-05 21:50:01 +01005132 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
5133 // contents. For VAR_PARTIAL get its partial, unless we already have one
5134 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005135 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01005136 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005137 in_vim9script() && type == NULL ? &type : NULL, FALSE, &found_var);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005138
Bram Moolenaare38eab22019-12-05 21:50:01 +01005139 // Skip white space to allow ":call func ()". Not good, but required for
5140 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005141 startarg = skipwhite(arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005142 if (*startarg != '(')
5143 {
Bram Moolenaare1242042021-12-16 20:56:57 +00005144 semsg(_(e_missing_parenthesis_str), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005145 goto end;
5146 }
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02005147 if (in_vim9script() && startarg > arg)
5148 {
5149 semsg(_(e_no_white_space_allowed_before_str_str), "(", eap->arg);
5150 goto end;
5151 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005152
5153 /*
5154 * When skipping, evaluate the function once, to find the end of the
5155 * arguments.
5156 * When the function takes a range, this is discovered after the first
5157 * call, and the loop is broken.
5158 */
5159 if (eap->skip)
5160 {
5161 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005162 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005163 }
5164 else
5165 lnum = eap->line1;
5166 for ( ; lnum <= eap->line2; ++lnum)
5167 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005168 funcexe_T funcexe;
5169
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005170 if (!eap->skip && eap->addr_count > 0)
5171 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01005172 if (lnum > curbuf->b_ml.ml_line_count)
5173 {
5174 // If the function deleted lines or switched to another buffer
5175 // the line number may become invalid.
Bram Moolenaar108010a2021-06-27 22:03:33 +02005176 emsg(_(e_invalid_range));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01005177 break;
5178 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005179 curwin->w_cursor.lnum = lnum;
5180 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005181 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005182 }
5183 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02005184
Bram Moolenaara80faa82020-04-12 19:37:17 +02005185 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00005186 funcexe.fe_firstline = eap->line1;
5187 funcexe.fe_lastline = eap->line2;
5188 funcexe.fe_doesrange = &doesrange;
5189 funcexe.fe_evaluate = !eap->skip;
5190 funcexe.fe_partial = partial;
5191 funcexe.fe_selfdict = fudi.fd_dict;
5192 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00005193 funcexe.fe_found_var = found_var;
Bram Moolenaar01dd6c32021-09-05 16:36:23 +02005194 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaare6b53242020-07-01 17:28:33 +02005195 if (get_func_tv(name, -1, &rettv, &arg, &evalarg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005196 {
5197 failed = TRUE;
5198 break;
5199 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01005200 if (has_watchexpr())
5201 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005202
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005203 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaare40fbc22020-06-27 18:06:45 +02005204 if (handle_subscript(&arg, &rettv,
5205 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005206 {
5207 failed = TRUE;
5208 break;
5209 }
5210
5211 clear_tv(&rettv);
5212 if (doesrange || eap->skip)
5213 break;
5214
Bram Moolenaare38eab22019-12-05 21:50:01 +01005215 // Stop when immediately aborting on error, or when an interrupt
5216 // occurred or an exception was thrown but not caught.
5217 // get_func_tv() returned OK, so that the check for trailing
5218 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005219 if (aborting())
5220 break;
5221 }
5222 if (eap->skip)
5223 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02005224 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005225
Bram Moolenaar1d341892021-09-18 15:25:52 +02005226 // When inside :try we need to check for following "| catch" or "| endtry".
5227 // Not when there was an error, but do check if an exception was thrown.
5228 if ((!aborting() || did_throw)
5229 && (!failed || eap->cstack->cs_trylevel > 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005230 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005231 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02005232 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02005233 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005234 {
Bram Moolenaar1d341892021-09-18 15:25:52 +02005235 if (!failed && !aborting())
Bram Moolenaar40d9da22020-02-17 10:01:24 +01005236 {
5237 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02005238 semsg(_(e_trailing_arg), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01005239 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005240 }
5241 else
Bram Moolenaar63b91732021-08-05 20:40:03 +02005242 set_nextcmd(eap, arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005243 }
5244
5245end:
5246 dict_unref(fudi.fd_dict);
5247 vim_free(tofree);
5248}
5249
5250/*
5251 * Return from a function. Possibly makes the return pending. Also called
5252 * for a pending return at the ":endtry" or after returning from an extra
5253 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
5254 * when called due to a ":return" command. "rettv" may point to a typval_T
5255 * with the return rettv. Returns TRUE when the return can be carried out,
5256 * FALSE when the return gets pending.
5257 */
5258 int
5259do_return(
5260 exarg_T *eap,
5261 int reanimate,
5262 int is_cmd,
5263 void *rettv)
5264{
5265 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01005266 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005267
5268 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005269 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005270 current_funccal->returned = FALSE;
5271
5272 /*
5273 * Cleanup (and inactivate) conditionals, but stop when a try conditional
5274 * not in its finally clause (which then is to be executed next) is found.
5275 * In this case, make the ":return" pending for execution at the ":endtry".
5276 * Otherwise, return normally.
5277 */
5278 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
5279 if (idx >= 0)
5280 {
5281 cstack->cs_pending[idx] = CSTP_RETURN;
5282
5283 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005284 // A pending return again gets pending. "rettv" points to an
5285 // allocated variable with the rettv of the original ":return"'s
5286 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005287 cstack->cs_rettv[idx] = rettv;
5288 else
5289 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005290 // When undoing a return in order to make it pending, get the stored
5291 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005292 if (reanimate)
5293 rettv = current_funccal->rettv;
5294
5295 if (rettv != NULL)
5296 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005297 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005298 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
5299 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
5300 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02005301 emsg(_(e_out_of_memory));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005302 }
5303 else
5304 cstack->cs_rettv[idx] = NULL;
5305
5306 if (reanimate)
5307 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005308 // The pending return value could be overwritten by a ":return"
5309 // without argument in a finally clause; reset the default
5310 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005311 current_funccal->rettv->v_type = VAR_NUMBER;
5312 current_funccal->rettv->vval.v_number = 0;
5313 }
5314 }
5315 report_make_pending(CSTP_RETURN, rettv);
5316 }
5317 else
5318 {
5319 current_funccal->returned = TRUE;
5320
Bram Moolenaare38eab22019-12-05 21:50:01 +01005321 // If the return is carried out now, store the return value. For
5322 // a return immediately after reanimation, the value is already
5323 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005324 if (!reanimate && rettv != NULL)
5325 {
5326 clear_tv(current_funccal->rettv);
5327 *current_funccal->rettv = *(typval_T *)rettv;
5328 if (!is_cmd)
5329 vim_free(rettv);
5330 }
5331 }
5332
5333 return idx < 0;
5334}
5335
5336/*
5337 * Free the variable with a pending return value.
5338 */
5339 void
5340discard_pending_return(void *rettv)
5341{
5342 free_tv((typval_T *)rettv);
5343}
5344
5345/*
5346 * Generate a return command for producing the value of "rettv". The result
5347 * is an allocated string. Used by report_pending() for verbose messages.
5348 */
5349 char_u *
5350get_return_cmd(void *rettv)
5351{
5352 char_u *s = NULL;
5353 char_u *tofree = NULL;
5354 char_u numbuf[NUMBUFLEN];
5355
5356 if (rettv != NULL)
5357 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
5358 if (s == NULL)
5359 s = (char_u *)"";
5360
5361 STRCPY(IObuff, ":return ");
5362 STRNCPY(IObuff + 8, s, IOSIZE - 8);
5363 if (STRLEN(s) + 8 >= IOSIZE)
5364 STRCPY(IObuff + IOSIZE - 4, "...");
5365 vim_free(tofree);
5366 return vim_strsave(IObuff);
5367}
5368
5369/*
5370 * Get next function line.
5371 * Called by do_cmdline() to get the next line.
5372 * Returns allocated string, or NULL for end of function.
5373 */
5374 char_u *
5375get_func_line(
5376 int c UNUSED,
5377 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02005378 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02005379 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005380{
5381 funccall_T *fcp = (funccall_T *)cookie;
5382 ufunc_T *fp = fcp->func;
5383 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005384 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005385
Bram Moolenaare38eab22019-12-05 21:50:01 +01005386 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005387 if (fcp->dbg_tick != debug_tick)
5388 {
5389 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005390 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005391 fcp->dbg_tick = debug_tick;
5392 }
5393#ifdef FEAT_PROFILE
5394 if (do_profiling == PROF_YES)
5395 func_line_end(cookie);
5396#endif
5397
5398 gap = &fp->uf_lines;
5399 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5400 || fcp->returned)
5401 retval = NULL;
5402 else
5403 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005404 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005405 while (fcp->linenr < gap->ga_len
5406 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
5407 ++fcp->linenr;
5408 if (fcp->linenr >= gap->ga_len)
5409 retval = NULL;
5410 else
5411 {
5412 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005413 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005414#ifdef FEAT_PROFILE
5415 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01005416 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005417#endif
5418 }
5419 }
5420
Bram Moolenaare38eab22019-12-05 21:50:01 +01005421 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005422 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005423 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005424 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01005425 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005426 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01005427 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005428 fcp->dbg_tick = debug_tick;
5429 }
5430
5431 return retval;
5432}
5433
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005434/*
5435 * Return TRUE if the currently active function should be ended, because a
5436 * return was encountered or an error occurred. Used inside a ":while".
5437 */
5438 int
5439func_has_ended(void *cookie)
5440{
5441 funccall_T *fcp = (funccall_T *)cookie;
5442
Bram Moolenaare38eab22019-12-05 21:50:01 +01005443 // Ignore the "abort" flag if the abortion behavior has been changed due to
5444 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005445 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
5446 || fcp->returned);
5447}
5448
5449/*
5450 * return TRUE if cookie indicates a function which "abort"s on errors.
5451 */
5452 int
5453func_has_abort(
5454 void *cookie)
5455{
5456 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
5457}
5458
5459
5460/*
5461 * Turn "dict.Func" into a partial for "Func" bound to "dict".
5462 * Don't do this when "Func" is already a partial that was bound
5463 * explicitly (pt_auto is FALSE).
5464 * Changes "rettv" in-place.
5465 * Returns the updated "selfdict_in".
5466 */
5467 dict_T *
5468make_partial(dict_T *selfdict_in, typval_T *rettv)
5469{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005470 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005471 char_u *tofree = NULL;
5472 ufunc_T *fp;
5473 char_u fname_buf[FLEN_FIXED + 1];
5474 int error;
5475 dict_T *selfdict = selfdict_in;
5476
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005477 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
5478 fp = rettv->vval.v_partial->pt_func;
5479 else
5480 {
5481 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
5482 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01005483 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005484 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005485 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005486 vim_free(tofree);
5487 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005488
5489 if (fp != NULL && (fp->uf_flags & FC_DICT))
5490 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005491 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005492
5493 if (pt != NULL)
5494 {
5495 pt->pt_refcount = 1;
5496 pt->pt_dict = selfdict;
5497 pt->pt_auto = TRUE;
5498 selfdict = NULL;
5499 if (rettv->v_type == VAR_FUNC)
5500 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01005501 // Just a function: Take over the function name and use
5502 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005503 pt->pt_name = rettv->vval.v_string;
5504 }
5505 else
5506 {
5507 partial_T *ret_pt = rettv->vval.v_partial;
5508 int i;
5509
Bram Moolenaare38eab22019-12-05 21:50:01 +01005510 // Partial: copy the function name, use selfdict and copy
5511 // args. Can't take over name or args, the partial might
5512 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005513 if (ret_pt->pt_name != NULL)
5514 {
5515 pt->pt_name = vim_strsave(ret_pt->pt_name);
5516 func_ref(pt->pt_name);
5517 }
5518 else
5519 {
5520 pt->pt_func = ret_pt->pt_func;
5521 func_ptr_ref(pt->pt_func);
5522 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005523 if (ret_pt->pt_argc > 0)
5524 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005525 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005526 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005527 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005528 pt->pt_argc = 0;
5529 else
5530 {
5531 pt->pt_argc = ret_pt->pt_argc;
5532 for (i = 0; i < pt->pt_argc; i++)
5533 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
5534 }
5535 }
5536 partial_unref(ret_pt);
5537 }
5538 rettv->v_type = VAR_PARTIAL;
5539 rettv->vval.v_partial = pt;
5540 }
5541 }
5542 return selfdict;
5543}
5544
5545/*
5546 * Return the name of the executed function.
5547 */
5548 char_u *
5549func_name(void *cookie)
5550{
5551 return ((funccall_T *)cookie)->func->uf_name;
5552}
5553
5554/*
5555 * Return the address holding the next breakpoint line for a funccall cookie.
5556 */
5557 linenr_T *
5558func_breakpoint(void *cookie)
5559{
5560 return &((funccall_T *)cookie)->breakpoint;
5561}
5562
5563/*
5564 * Return the address holding the debug tick for a funccall cookie.
5565 */
5566 int *
5567func_dbg_tick(void *cookie)
5568{
5569 return &((funccall_T *)cookie)->dbg_tick;
5570}
5571
5572/*
5573 * Return the nesting level for a funccall cookie.
5574 */
5575 int
5576func_level(void *cookie)
5577{
5578 return ((funccall_T *)cookie)->level;
5579}
5580
5581/*
5582 * Return TRUE when a function was ended by a ":return" command.
5583 */
5584 int
5585current_func_returned(void)
5586{
5587 return current_funccal->returned;
5588}
5589
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005590 int
5591free_unref_funccal(int copyID, int testing)
5592{
5593 int did_free = FALSE;
5594 int did_free_funccal = FALSE;
5595 funccall_T *fc, **pfc;
5596
5597 for (pfc = &previous_funccal; *pfc != NULL; )
5598 {
5599 if (can_free_funccal(*pfc, copyID))
5600 {
5601 fc = *pfc;
5602 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01005603 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005604 did_free = TRUE;
5605 did_free_funccal = TRUE;
5606 }
5607 else
5608 pfc = &(*pfc)->caller;
5609 }
5610 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01005611 // When a funccal was freed some more items might be garbage
5612 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005613 (void)garbage_collect(testing);
5614
5615 return did_free;
5616}
5617
5618/*
Bram Moolenaarba209902016-08-24 22:06:38 +02005619 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005620 */
5621 static funccall_T *
5622get_funccal(void)
5623{
5624 int i;
5625 funccall_T *funccal;
5626 funccall_T *temp_funccal;
5627
5628 funccal = current_funccal;
5629 if (debug_backtrace_level > 0)
5630 {
5631 for (i = 0; i < debug_backtrace_level; i++)
5632 {
5633 temp_funccal = funccal->caller;
5634 if (temp_funccal)
5635 funccal = temp_funccal;
5636 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01005637 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005638 debug_backtrace_level = i;
5639 }
5640 }
5641 return funccal;
5642}
5643
5644/*
5645 * Return the hashtable used for local variables in the current funccal.
5646 * Return NULL if there is no current funccal.
5647 */
5648 hashtab_T *
5649get_funccal_local_ht()
5650{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005651 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005652 return NULL;
5653 return &get_funccal()->l_vars.dv_hashtab;
5654}
5655
5656/*
5657 * Return the l: scope variable.
5658 * Return NULL if there is no current funccal.
5659 */
5660 dictitem_T *
5661get_funccal_local_var()
5662{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005663 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005664 return NULL;
5665 return &get_funccal()->l_vars_var;
5666}
5667
5668/*
5669 * Return the hashtable used for argument in the current funccal.
5670 * Return NULL if there is no current funccal.
5671 */
5672 hashtab_T *
5673get_funccal_args_ht()
5674{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005675 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005676 return NULL;
5677 return &get_funccal()->l_avars.dv_hashtab;
5678}
5679
5680/*
5681 * Return the a: scope variable.
5682 * Return NULL if there is no current funccal.
5683 */
5684 dictitem_T *
5685get_funccal_args_var()
5686{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005687 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005688 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01005689 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005690}
5691
5692/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005693 * List function variables, if there is a function.
5694 */
5695 void
5696list_func_vars(int *first)
5697{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005698 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005699 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01005700 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005701}
5702
5703/*
5704 * If "ht" is the hashtable for local variables in the current funccal, return
5705 * the dict that contains it.
5706 * Otherwise return NULL.
5707 */
5708 dict_T *
5709get_current_funccal_dict(hashtab_T *ht)
5710{
5711 if (current_funccal != NULL
5712 && ht == &current_funccal->l_vars.dv_hashtab)
5713 return &current_funccal->l_vars;
5714 return NULL;
5715}
5716
5717/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005718 * Search hashitem in parent scope.
5719 */
5720 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005721find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005722{
5723 funccall_T *old_current_funccal = current_funccal;
5724 hashtab_T *ht;
5725 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005726 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005727
5728 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
5729 return NULL;
5730
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02005731 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005732 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02005733 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005734 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005735 ht = find_var_ht(name, &varname);
5736 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02005737 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005738 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02005739 if (!HASHITEM_EMPTY(hi))
5740 {
5741 *pht = ht;
5742 break;
5743 }
5744 }
5745 if (current_funccal == current_funccal->func->uf_scoped)
5746 break;
5747 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005748 }
5749 current_funccal = old_current_funccal;
5750
5751 return hi;
5752}
5753
5754/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005755 * Search variable in parent scope.
5756 */
5757 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005758find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005759{
5760 dictitem_T *v = NULL;
5761 funccall_T *old_current_funccal = current_funccal;
5762 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005763 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005764
5765 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
5766 return NULL;
5767
Bram Moolenaare38eab22019-12-05 21:50:01 +01005768 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005769 current_funccal = current_funccal->func->uf_scoped;
5770 while (current_funccal)
5771 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005772 ht = find_var_ht(name, &varname);
5773 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005774 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005775 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005776 if (v != NULL)
5777 break;
5778 }
5779 if (current_funccal == current_funccal->func->uf_scoped)
5780 break;
5781 current_funccal = current_funccal->func->uf_scoped;
5782 }
5783 current_funccal = old_current_funccal;
5784
5785 return v;
5786}
5787
5788/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005789 * Set "copyID + 1" in previous_funccal and callers.
5790 */
5791 int
5792set_ref_in_previous_funccal(int copyID)
5793{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005794 funccall_T *fc;
5795
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005796 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005797 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005798 fc->fc_copyID = copyID + 1;
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005799 if (set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
5800 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
5801 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL))
5802 return TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005803 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005804 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005805}
5806
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005807 static int
5808set_ref_in_funccal(funccall_T *fc, int copyID)
5809{
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005810 if (fc->fc_copyID != copyID)
5811 {
5812 fc->fc_copyID = copyID;
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005813 if (set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
5814 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
5815 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
5816 || set_ref_in_func(NULL, fc->func, copyID))
5817 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005818 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005819 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005820}
5821
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005822/*
5823 * Set "copyID" in all local vars and arguments in the call stack.
5824 */
5825 int
5826set_ref_in_call_stack(int copyID)
5827{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005828 funccall_T *fc;
5829 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005830
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005831 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5832 if (set_ref_in_funccal(fc, copyID))
5833 return TRUE;
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005834
5835 // Also go through the funccal_stack.
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005836 for (entry = funccal_stack; entry != NULL; entry = entry->next)
5837 for (fc = entry->top_funccal; fc != NULL; fc = fc->caller)
5838 if (set_ref_in_funccal(fc, copyID))
5839 return TRUE;
5840 return FALSE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005841}
5842
5843/*
5844 * Set "copyID" in all functions available by name.
5845 */
5846 int
5847set_ref_in_functions(int copyID)
5848{
5849 int todo;
5850 hashitem_T *hi = NULL;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005851 ufunc_T *fp;
5852
5853 todo = (int)func_hashtab.ht_used;
5854 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005855 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005856 if (!HASHITEM_EMPTY(hi))
5857 {
5858 --todo;
5859 fp = HI2UF(hi);
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005860 if (!func_name_refcount(fp->uf_name)
5861 && set_ref_in_func(NULL, fp, copyID))
5862 return TRUE;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005863 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005864 }
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005865 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005866}
5867
5868/*
5869 * Set "copyID" in all function arguments.
5870 */
5871 int
5872set_ref_in_func_args(int copyID)
5873{
5874 int i;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005875
5876 for (i = 0; i < funcargs.ga_len; ++i)
Bram Moolenaar20cc5282021-07-03 16:33:16 +02005877 if (set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
5878 copyID, NULL, NULL))
5879 return TRUE;
5880 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005881}
5882
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005883/*
5884 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005885 * Returns TRUE if setting references failed somehow.
5886 */
5887 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005888set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005889{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005890 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005891 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01005892 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005893 char_u fname_buf[FLEN_FIXED + 1];
5894 char_u *tofree = NULL;
5895 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005896 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005897
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005898 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005899 return FALSE;
5900
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005901 if (fp_in == NULL)
5902 {
5903 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005904 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005905 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005906 if (fp != NULL)
5907 {
5908 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005909 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005910 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02005911
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005912 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005913 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005914}
5915
Bram Moolenaare38eab22019-12-05 21:50:01 +01005916#endif // FEAT_EVAL