blob: e89b8858d331549b3bc0430eb50bd96c7c263bdf [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
32static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
33static char *e_funcdict = N_("E717: Dictionary entry already exists");
34static char *e_funcref = N_("E718: Funcref required");
35static char *e_nofunc = N_("E130: Unknown function: %s");
36
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020037static void funccal_unref(funccall_T *fc, 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.
56 * If "argtypes" is not NULL also get the type: "arg: type".
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 Moolenaarb4d16cb2020-11-05 18:45:46 +010069 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010070{
Bram Moolenaar6e949782020-04-13 17:21:00 +020071 char_u *p = arg;
72 char_u *arg_copy = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073
74 while (ASCII_ISALNUM(*p) || *p == '_')
75 ++p;
76 if (arg == p || isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020077 || (argtypes == NULL
78 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
79 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010080 {
81 if (!skip)
82 semsg(_("E125: Illegal argument: %s"), arg);
83 return arg;
84 }
Bram Moolenaarb4893b82021-02-21 22:20:24 +010085
Bram Moolenaar057e84a2021-02-28 16:55:11 +010086 // Vim9 script: cannot use script var name for argument. In function: also
87 // check local vars and arguments.
88 if (!skip && argtypes != NULL && check_defined(arg, p - arg,
89 evalarg == NULL ? NULL : evalarg->eval_cctx, TRUE) == FAIL)
Bram Moolenaarb4893b82021-02-21 22:20:24 +010090 return arg;
Bram Moolenaarb4893b82021-02-21 22:20:24 +010091
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010092 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
93 return arg;
94 if (newargs != NULL)
95 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 int c;
97 int i;
98
99 c = *p;
100 *p = NUL;
101 arg_copy = vim_strsave(arg);
102 if (arg_copy == NULL)
103 {
104 *p = c;
105 return arg;
106 }
107
108 // Check for duplicate argument name.
109 for (i = 0; i < newargs->ga_len; ++i)
110 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
111 {
112 semsg(_("E853: Duplicate argument name: %s"), arg_copy);
113 vim_free(arg_copy);
114 return arg;
115 }
116 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
117 newargs->ga_len++;
118
119 *p = c;
120 }
121
122 // get any type from "arg: type"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100123 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 {
125 char_u *type = NULL;
126
Bram Moolenaar6e949782020-04-13 17:21:00 +0200127 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
128 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200129 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200130 arg_copy == NULL ? arg : arg_copy);
131 p = skipwhite(p);
132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100133 if (*p == ':')
134 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200135 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100136 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200137 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100138 semsg(_(e_white_space_required_after_str_str), ":", p - 1);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200139 return arg;
140 }
141 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200142 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100143 if (!skip)
144 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100145 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100146 else if (*skipwhite(p) != '=' && !types_optional)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200147 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200148 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200149 arg_copy == NULL ? arg : arg_copy);
150 return arg;
151 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100152 if (!skip)
153 {
154 if (type == NULL && types_optional)
155 // lambda arguments default to "any" type
156 type = vim_strsave((char_u *)"any");
157 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
158 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100159 }
160
161 return p;
162}
163
164/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200165 * Get function arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100166 * "argp" should point to just after the "(", possibly to white space.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100167 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200168 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100169 static int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200170get_function_args(
171 char_u **argp,
172 char_u endchar,
173 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100174 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100175 int types_optional, // types optional if "argtypes" is not NULL
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100176 evalarg_T *evalarg, // context or NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200177 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200178 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200179 int skip,
180 exarg_T *eap,
181 char_u **line_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200182{
183 int mustend = FALSE;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100184 char_u *arg;
185 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200186 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200187 int any_default = FALSE;
188 char_u *expr;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100189 char_u *whitep = *argp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200190
191 if (newargs != NULL)
192 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100193 if (argtypes != NULL)
194 ga_init2(argtypes, (int)sizeof(char_u *), 3);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200195 if (default_args != NULL)
196 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200197
198 if (varargs != NULL)
199 *varargs = FALSE;
200
201 /*
202 * Isolate the arguments: "arg1, arg2, ...)"
203 */
Bram Moolenaarcef12702021-01-04 14:09:43 +0100204 arg = skipwhite(*argp);
205 p = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200206 while (*p != endchar)
207 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200208 while (eap != NULL && eap->getline != NULL
209 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200210 {
211 char_u *theline;
212
213 // End of the line, get the next one.
214 theline = eap->getline(':', eap->cookie, 0, TRUE);
215 if (theline == NULL)
216 break;
217 vim_free(*line_to_free);
218 *line_to_free = theline;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200219 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200220 p = skipwhite(theline);
221 }
222
223 if (mustend && *p != endchar)
224 {
225 if (!skip)
226 semsg(_(e_invarg2), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100227 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200228 }
229 if (*p == endchar)
230 break;
231
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200232 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
233 {
234 if (varargs != NULL)
235 *varargs = TRUE;
236 p += 3;
237 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238
239 if (argtypes != NULL)
240 {
241 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200242 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100243 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100244 if (!skip)
245 emsg(_(e_missing_name_after_dots));
246 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100247 }
248
249 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100250 p = one_function_arg(p, newargs, argtypes, types_optional,
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100251 evalarg, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252 if (p == arg)
253 break;
Bram Moolenaar4f53b792021-02-07 15:59:49 +0100254 if (*skipwhite(p) == '=')
255 {
256 emsg(_(e_cannot_use_default_for_variable_arguments));
257 break;
258 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100259 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200260 }
261 else
262 {
263 arg = p;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100264 p = one_function_arg(p, newargs, argtypes, types_optional,
265 evalarg, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100266 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200267 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200268
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200269 if (*skipwhite(p) == '=' && default_args != NULL)
270 {
271 typval_T rettv;
272
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100273 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200274 any_default = TRUE;
275 p = skipwhite(p) + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200276 whitep = p;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200277 p = skipwhite(p);
278 expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200279 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200280 {
281 if (ga_grow(default_args, 1) == FAIL)
282 goto err_ret;
283
284 // trim trailing whitespace
285 while (p > expr && VIM_ISWHITE(p[-1]))
286 p--;
287 c = *p;
288 *p = NUL;
289 expr = vim_strsave(expr);
290 if (expr == NULL)
291 {
292 *p = c;
293 goto err_ret;
294 }
295 ((char_u **)(default_args->ga_data))
296 [default_args->ga_len] = expr;
297 default_args->ga_len++;
298 *p = c;
299 }
300 else
301 mustend = TRUE;
302 }
303 else if (any_default)
304 {
305 emsg(_("E989: Non-default argument follows default argument"));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200306 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200307 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200308 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200309 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200310 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200311 // Don't give this error when skipping, it makes the "->" not
312 // found in "{k,v -> x}" and give a confusing error.
Bram Moolenaar608d78f2021-03-06 22:33:12 +0100313 // Allow missing space after comma in legacy functions.
314 if (!skip && argtypes != NULL
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200315 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
316 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100317 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200318 goto err_ret;
319 }
320 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200321 else
322 mustend = TRUE;
323 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200324 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200325 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200326 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200327
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200328 if (*p != endchar)
329 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100330 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200331
332 *argp = p;
333 return OK;
334
335err_ret:
336 if (newargs != NULL)
337 ga_clear_strings(newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200338 if (default_args != NULL)
339 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200340 return FAIL;
341}
342
343/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100344 * Parse the argument types, filling "fp->uf_arg_types".
345 * Return OK or FAIL.
346 */
347 static int
348parse_argument_types(ufunc_T *fp, garray_T *argtypes, int varargs)
349{
350 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
351 if (argtypes->ga_len > 0)
352 {
353 // When "varargs" is set the last name/type goes into uf_va_name
354 // and uf_va_type.
355 int len = argtypes->ga_len - (varargs ? 1 : 0);
356
357 if (len > 0)
358 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
359 if (fp->uf_arg_types != NULL)
360 {
361 int i;
362 type_T *type;
363
364 for (i = 0; i < len; ++ i)
365 {
366 char_u *p = ((char_u **)argtypes->ga_data)[i];
367
368 if (p == NULL)
369 // will get the type from the default value
370 type = &t_unknown;
371 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100372 type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100373 if (type == NULL)
374 return FAIL;
375 fp->uf_arg_types[i] = type;
376 }
377 }
378 if (varargs)
379 {
380 char_u *p;
381
382 // Move the last argument "...name: type" to uf_va_name and
383 // uf_va_type.
384 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
385 [fp->uf_args.ga_len - 1];
386 --fp->uf_args.ga_len;
387 p = ((char_u **)argtypes->ga_data)[len];
388 if (p == NULL)
389 // todo: get type from default value
390 fp->uf_va_type = &t_any;
391 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100392 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100393 if (fp->uf_va_type == NULL)
394 return FAIL;
395 }
396 }
397 return OK;
398}
399
400/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200401 * Register function "fp" as using "current_funccal" as its scope.
402 */
403 static int
404register_closure(ufunc_T *fp)
405{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200406 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100407 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200408 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200409 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200410 fp->uf_scoped = current_funccal;
411 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200412
Bram Moolenaar58016442016-07-31 18:30:22 +0200413 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
414 return FAIL;
415 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
416 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200417 return OK;
418}
419
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100420 static void
421set_ufunc_name(ufunc_T *fp, char_u *name)
422{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100423 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
424 // actually extends beyond the struct.
425 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100426
427 if (name[0] == K_SPECIAL)
428 {
429 fp->uf_name_exp = alloc(STRLEN(name) + 3);
430 if (fp->uf_name_exp != NULL)
431 {
432 STRCPY(fp->uf_name_exp, "<SNR>");
433 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
434 }
435 }
436}
437
Bram Moolenaar58016442016-07-31 18:30:22 +0200438/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200439 * Get a name for a lambda. Returned in static memory.
440 */
441 char_u *
442get_lambda_name(void)
443{
444 static char_u name[30];
445 static int lambda_no = 0;
446
447 sprintf((char*)name, "<lambda>%d", ++lambda_no);
448 return name;
449}
450
Bram Moolenaar801ab062020-06-25 19:27:56 +0200451#if defined(FEAT_LUA) || defined(PROTO)
452/*
453 * Registers a native C callback which can be called from Vim script.
454 * Returns the name of the Vim script function.
455 */
456 char_u *
457register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
458{
459 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200460 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200461
462 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
463 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200464 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200465
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200466 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200467 fp->uf_refcount = 1;
468 fp->uf_varargs = TRUE;
469 fp->uf_flags = FC_CFUNC;
470 fp->uf_calls = 0;
471 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200472 fp->uf_cb = cb;
473 fp->uf_cb_free = cb_free;
474 fp->uf_cb_state = state;
475
476 set_ufunc_name(fp, name);
477 hash_add(&func_hashtab, UF2HIKEY(fp));
478
479 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200480}
481#endif
482
Bram Moolenaar04b12692020-05-04 23:24:44 +0200483/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100484 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100485 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100486 * If "white_error" is not NULL check for correct use of white space and set
487 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100488 * Return NULL if no valid arrow found.
489 */
490 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100491skip_arrow(
492 char_u *start,
493 int equal_arrow,
494 char_u **ret_type,
495 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100496{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100497 char_u *s = start;
498 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100499
500 if (equal_arrow)
501 {
502 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100503 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100504 if (white_error != NULL && !VIM_ISWHITE(s[1]))
505 {
506 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100507 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100508 return NULL;
509 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100510 s = skipwhite(s + 1);
511 *ret_type = s;
512 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100513 if (s == *ret_type)
514 {
515 emsg(_(e_missing_return_type));
516 return NULL;
517 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100518 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100519 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100520 s = skipwhite(s);
521 if (*s != '=')
522 return NULL;
523 ++s;
524 }
525 if (*s != '>')
526 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100527 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
528 || !IS_WHITE_OR_NUL(s[1])))
529 {
530 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100531 semsg(_(e_white_space_required_before_and_after_str_at_str),
532 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100533 return NULL;
534 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100535 return skipwhite(s + 1);
536}
537
538/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200539 * Parse a lambda expression and get a Funcref from "*arg".
Bram Moolenaar65c44152020-12-24 15:14:01 +0100540 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100541 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200542 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
543 */
544 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100545get_lambda_tv(
546 char_u **arg,
547 typval_T *rettv,
548 int types_optional,
549 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200550{
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200551 int evaluate = evalarg != NULL
552 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200553 garray_T newargs;
554 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200555 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100556 garray_T argtypes;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200557 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100558 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200559 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100560 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200561 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200562 char_u *s;
563 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200564 int *old_eval_lavars = eval_lavars_used;
565 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +0100566 char_u *tofree1 = NULL;
567 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100568 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100569 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +0100570 int called_emsg_start = called_emsg;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100571
572 if (equal_arrow && !in_vim9script())
573 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200574
575 ga_init(&newargs);
576 ga_init(&newlines);
577
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100578 // First, check if this is really a lambda expression. "->" or "=>" must
579 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100580 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100581 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100582 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100583 NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100584 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100585 {
586 if (types_optional)
587 ga_clear_strings(&argtypes);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100588 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100589 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200590
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100591 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200592 if (evaluate)
593 pnewargs = &newargs;
594 else
595 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100596 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100597 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100598 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100599 &varargs, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100600 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100601 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaare462f522020-12-27 14:43:30 +0100602 equal_arrow || in_vim9script() ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100603 {
604 if (types_optional)
605 ga_clear_strings(&argtypes);
Bram Moolenaare5730bd2020-12-25 22:30:16 +0100606 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100607 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100608 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100609 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200610
Bram Moolenaarf898f7c2021-01-16 18:09:52 +0100611 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
612 if (ret_type != NULL)
613 {
614 ret_type = vim_strsave(ret_type);
615 tofree2 = ret_type;
616 }
617
Bram Moolenaare38eab22019-12-05 21:50:01 +0100618 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200619 if (evaluate)
620 eval_lavars_used = &eval_lavars;
621
Bram Moolenaar65c44152020-12-24 15:14:01 +0100622 *arg = skipwhite_and_linebreak(*arg, evalarg);
623
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100624 // Recognize "{" as the start of a function body.
625 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +0100626 {
627 // TODO: process the function body upto the "}".
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100628 // Return type is required then.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100629 emsg("Lambda function body not supported yet");
630 goto errret;
631 }
632
Bram Moolenaare38eab22019-12-05 21:50:01 +0100633 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200634 start = *arg;
635 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200636 if (ret == FAIL)
637 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200638 if (evalarg != NULL)
639 {
640 // avoid that the expression gets freed when another line break follows
Bram Moolenaarf898f7c2021-01-16 18:09:52 +0100641 tofree1 = evalarg->eval_tofree;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200642 evalarg->eval_tofree = NULL;
643 }
644
Bram Moolenaar65c44152020-12-24 15:14:01 +0100645 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +0100646 {
Bram Moolenaar65c44152020-12-24 15:14:01 +0100647 *arg = skipwhite_and_linebreak(*arg, evalarg);
648 if (**arg != '}')
649 {
650 semsg(_("E451: Expected }: %s"), *arg);
651 goto errret;
652 }
653 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +0100654 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200655
656 if (evaluate)
657 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200658 int len;
659 int flags = 0;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200660 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200661 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200662
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200663 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200664 if (fp == NULL)
665 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200666 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200667 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200668 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200669 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200670
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200671 ga_init2(&newlines, (int)sizeof(char_u *), 1);
672 if (ga_grow(&newlines, 1) == FAIL)
673 goto errret;
674
Bram Moolenaare38eab22019-12-05 21:50:01 +0100675 // Add "return " before the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200676 len = 7 + (int)(end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200677 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200678 if (p == NULL)
679 goto errret;
680 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
681 STRCPY(p, "return ");
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200682 vim_strncpy(p + 7, start, end - start);
Bram Moolenaarf10806b2020-04-02 18:34:35 +0200683 if (strstr((char *)p + 7, "a:") == NULL)
684 // No a: variables are used for sure.
685 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200686
687 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100688 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200689 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200690 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100691 if (types_optional)
692 {
693 if (parse_argument_types(fp, &argtypes, FALSE) == FAIL)
694 goto errret;
695 if (ret_type != NULL)
696 {
697 fp->uf_ret_type = parse_type(&ret_type,
698 &fp->uf_type_list, TRUE);
699 if (fp->uf_ret_type == NULL)
700 goto errret;
701 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +0100702 else
Bram Moolenaar328eac22021-01-07 19:23:08 +0100703 fp->uf_ret_type = &t_any;
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100704 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100705
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200706 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200707 if (current_funccal != NULL && eval_lavars)
708 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200709 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200710 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200711 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200712 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200713
714#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200715 if (prof_def_func())
716 func_do_profile(fp);
717#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200718 if (sandbox)
719 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200721 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200722 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200723 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200724 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100725 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200726
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200727 pt->pt_func = fp;
728 pt->pt_refcount = 1;
729 rettv->vval.v_partial = pt;
730 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100731
732 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200733 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200734
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200735 eval_lavars_used = old_eval_lavars;
Bram Moolenaarefaaaa62020-07-08 22:24:09 +0200736 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaarf898f7c2021-01-16 18:09:52 +0100737 evalarg->eval_tofree = tofree1;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200738 else
Bram Moolenaarf898f7c2021-01-16 18:09:52 +0100739 vim_free(tofree1);
740 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100741 if (types_optional)
742 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200743 return OK;
744
745errret:
746 ga_clear_strings(&newargs);
747 ga_clear_strings(&newlines);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100748 if (types_optional)
749 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200750 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100751 vim_free(pt);
Bram Moolenaarefaaaa62020-07-08 22:24:09 +0200752 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaarf898f7c2021-01-16 18:09:52 +0100753 evalarg->eval_tofree = tofree1;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200754 else
Bram Moolenaarf898f7c2021-01-16 18:09:52 +0100755 vim_free(tofree1);
756 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200757 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200758 return FAIL;
759}
760
761/*
762 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
763 * name it contains, otherwise return "name".
764 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
765 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100766 * If "type" is not NULL and a Vim9 script-local variable is found look up the
767 * type of the variable.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200768 */
769 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100770deref_func_name(
771 char_u *name,
772 int *lenp,
773 partial_T **partialp,
774 type_T **type,
775 int no_autoload)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200776{
777 dictitem_T *v;
778 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100779 char_u *s = NULL;
780 hashtab_T *ht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200781
782 if (partialp != NULL)
783 *partialp = NULL;
784
785 cc = name[*lenp];
786 name[*lenp] = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100787 v = find_var(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200788 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100789 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200790 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100791 if (v->di_tv.v_type == VAR_FUNC)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200792 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100793 if (v->di_tv.vval.v_string == NULL)
794 {
795 *lenp = 0;
796 return (char_u *)""; // just in case
797 }
798 s = v->di_tv.vval.v_string;
799 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200800 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200801
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100802 if (v->di_tv.v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200803 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100804 partial_T *pt = v->di_tv.vval.v_partial;
805
806 if (pt == NULL)
807 {
808 *lenp = 0;
809 return (char_u *)""; // just in case
810 }
811 if (partialp != NULL)
812 *partialp = pt;
813 s = partial_name(pt);
814 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200815 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100816
817 if (s != NULL)
818 {
819 if (type != NULL && ht == get_script_local_ht())
820 {
821 svar_T *sv = find_typval_in_script(&v->di_tv);
822
823 if (sv != NULL)
824 *type = sv->sv_type;
825 }
826 return s;
827 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200828 }
829
830 return name;
831}
832
833/*
834 * Give an error message with a function name. Handle <SNR> things.
835 * "ermsg" is to be passed without translation, use N_() instead of _().
836 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100837 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200838emsg_funcname(char *ermsg, char_u *name)
839{
840 char_u *p;
841
842 if (*name == K_SPECIAL)
843 p = concat_str((char_u *)"<SNR>", name + 3);
844 else
845 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100846 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200847 if (p != name)
848 vim_free(p);
849}
850
851/*
852 * Allocate a variable for the result of a function.
853 * Return OK or FAIL.
854 */
855 int
856get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200857 char_u *name, // name of the function
858 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200859 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200860 char_u **arg, // argument, pointing to the '('
Bram Moolenaare6b53242020-07-01 17:28:33 +0200861 evalarg_T *evalarg, // for line continuation
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200862 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200863{
864 char_u *argp;
865 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100866 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
867 int argcount = 0; // number of arguments found
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200868 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200869
870 /*
871 * Get the arguments.
872 */
873 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200874 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
875 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200876 {
Bram Moolenaare6b53242020-07-01 17:28:33 +0200877 // skip the '(' or ',' and possibly line breaks
878 argp = skipwhite_and_linebreak(argp + 1, evalarg);
879
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200880 if (*argp == ')' || *argp == ',' || *argp == NUL)
881 break;
Bram Moolenaare6b53242020-07-01 17:28:33 +0200882 if (eval1(&argp, &argvars[argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200883 {
884 ret = FAIL;
885 break;
886 }
887 ++argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +0200888 // The comma should come right after the argument, but this wasn't
889 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200890 if (vim9script)
891 {
892 if (*argp != ',' && *skipwhite(argp) == ',')
893 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +0100894 semsg(_(e_no_white_space_allowed_before_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200895 ret = FAIL;
896 break;
897 }
898 }
899 else
Bram Moolenaar9d489562020-07-30 20:08:50 +0200900 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200901 if (*argp != ',')
902 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200903 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
904 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100905 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200906 ret = FAIL;
907 break;
908 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200909 }
Bram Moolenaare6b53242020-07-01 17:28:33 +0200910 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200911 if (*argp == ')')
912 ++argp;
913 else
914 ret = FAIL;
915
916 if (ret == OK)
917 {
918 int i = 0;
919
920 if (get_vim_var_nr(VV_TESTING))
921 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100922 // Prepare for calling test_garbagecollect_now(), need to know
923 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200924 if (funcargs.ga_itemsize == 0)
925 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
926 for (i = 0; i < argcount; ++i)
927 if (ga_grow(&funcargs, 1) == OK)
928 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
929 &argvars[i];
930 }
931
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200932 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200933
934 funcargs.ga_len -= i;
935 }
936 else if (!aborting())
937 {
938 if (argcount == MAX_FUNC_ARGS)
939 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
940 else
941 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
942 }
943
944 while (--argcount >= 0)
945 clear_tv(&argvars[argcount]);
946
Bram Moolenaar8294d492020-08-10 22:40:56 +0200947 if (in_vim9script())
948 *arg = argp;
949 else
950 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200951 return ret;
952}
953
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200954/*
955 * Return TRUE if "p" starts with "<SID>" or "s:".
956 * Only works if eval_fname_script() returned non-zero for "p"!
957 */
958 static int
959eval_fname_sid(char_u *p)
960{
961 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
962}
963
964/*
965 * In a script change <SID>name() and s:name() to K_SNR 123_name().
966 * Change <SNR>123_name() to K_SNR 123_name().
967 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
968 * (slow).
969 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100970 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200971fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
972{
973 int llen;
974 char_u *fname;
975 int i;
976
977 llen = eval_fname_script(name);
978 if (llen > 0)
979 {
980 fname_buf[0] = K_SPECIAL;
981 fname_buf[1] = KS_EXTRA;
982 fname_buf[2] = (int)KE_SNR;
983 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100984 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200985 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200986 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100987 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200988 else
989 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200990 sprintf((char *)fname_buf + 3, "%ld_",
991 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200992 i = (int)STRLEN(fname_buf);
993 }
994 }
995 if (i + STRLEN(name + llen) < FLEN_FIXED)
996 {
997 STRCPY(fname_buf + i, name + llen);
998 fname = fname_buf;
999 }
1000 else
1001 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001002 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001003 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +01001004 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001005 else
1006 {
1007 *tofree = fname;
1008 mch_memmove(fname, fname_buf, (size_t)i);
1009 STRCPY(fname + i, name + llen);
1010 }
1011 }
1012 }
1013 else
1014 fname = name;
1015 return fname;
1016}
1017
1018/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001019 * Find a function "name" in script "sid".
1020 */
1021 static ufunc_T *
1022find_func_with_sid(char_u *name, int sid)
1023{
1024 hashitem_T *hi;
1025 char_u buffer[200];
1026
1027 buffer[0] = K_SPECIAL;
1028 buffer[1] = KS_EXTRA;
1029 buffer[2] = (int)KE_SNR;
1030 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
1031 (long)sid, name);
1032 hi = hash_find(&func_hashtab, buffer);
1033 if (!HASHITEM_EMPTY(hi))
1034 return HI2UF(hi);
1035
1036 return NULL;
1037}
1038
1039/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001040 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001041 * When "is_global" is true don't find script-local or imported functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001042 * Return NULL for unknown function.
1043 */
Bram Moolenaarce658352020-07-31 23:47:12 +02001044 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001045find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001046{
1047 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001048 ufunc_T *func;
1049 imported_T *imported;
1050
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001051 if (!is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001052 {
Bram Moolenaar333894b2020-08-01 18:53:07 +02001053 char_u *after_script = NULL;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001054 long sid = 0;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001055 int find_script_local = in_vim9script()
1056 && eval_isnamec1(*name) && name[1] != ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057
Bram Moolenaar95006e32020-08-29 17:47:08 +02001058 if (find_script_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001060 // Find script-local function before global one.
1061 func = find_func_with_sid(name, current_sctx.sc_sid);
1062 if (func != NULL)
1063 return func;
1064 }
1065
Bram Moolenaarefa94442020-08-08 22:16:00 +02001066 if (name[0] == K_SPECIAL
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001067 && name[1] == KS_EXTRA
1068 && name[2] == KE_SNR)
1069 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001070 // Caller changes s: to <SNR>99_name.
1071
1072 after_script = name + 3;
1073 sid = getdigits(&after_script);
Bram Moolenaarefa94442020-08-08 22:16:00 +02001074 if (*after_script == '_')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001075 ++after_script;
1076 else
1077 after_script = NULL;
1078 }
Bram Moolenaar95006e32020-08-29 17:47:08 +02001079 if (find_script_local || after_script != NULL)
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001080 {
1081 // Find imported function before global one.
Bram Moolenaarefa94442020-08-08 22:16:00 +02001082 if (after_script != NULL && sid != current_sctx.sc_sid)
1083 imported = find_imported_in_script(after_script, 0, sid);
1084 else
1085 imported = find_imported(after_script == NULL
1086 ? name : after_script, 0, cctx);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001087 if (imported != NULL && imported->imp_funcname != NULL)
1088 {
1089 hi = hash_find(&func_hashtab, imported->imp_funcname);
1090 if (!HASHITEM_EMPTY(hi))
1091 return HI2UF(hi);
1092 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001093 }
1094 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001095
Bram Moolenaara26b9702020-04-18 19:53:28 +02001096 hi = hash_find(&func_hashtab,
1097 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001098 if (!HASHITEM_EMPTY(hi))
1099 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001100
1101 return NULL;
1102}
1103
1104/*
1105 * Find a function by name, return pointer to it in ufuncs.
1106 * "cctx" is passed in a :def function to find imported functions.
1107 * Return NULL for unknown or dead function.
1108 */
1109 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001110find_func(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001111{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001112 ufunc_T *fp = find_func_even_dead(name, is_global, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001113
1114 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
1115 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001116 return NULL;
1117}
1118
1119/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02001120 * Return TRUE if "ufunc" is a global function.
1121 */
1122 int
1123func_is_global(ufunc_T *ufunc)
1124{
1125 return ufunc->uf_name[0] != K_SPECIAL;
1126}
1127
1128/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001129 * Copy the function name of "fp" to buffer "buf".
1130 * "buf" must be able to hold the function name plus three bytes.
1131 * Takes care of script-local function names.
1132 */
1133 static void
1134cat_func_name(char_u *buf, ufunc_T *fp)
1135{
Bram Moolenaar0f769812020-09-12 18:32:34 +02001136 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001137 {
1138 STRCPY(buf, "<SNR>");
1139 STRCAT(buf, fp->uf_name + 3);
1140 }
1141 else
1142 STRCPY(buf, fp->uf_name);
1143}
1144
1145/*
1146 * Add a number variable "name" to dict "dp" with value "nr".
1147 */
1148 static void
1149add_nr_var(
1150 dict_T *dp,
1151 dictitem_T *v,
1152 char *name,
1153 varnumber_T nr)
1154{
1155 STRCPY(v->di_key, name);
1156 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1157 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
1158 v->di_tv.v_type = VAR_NUMBER;
1159 v->di_tv.v_lock = VAR_FIXED;
1160 v->di_tv.vval.v_number = nr;
1161}
1162
1163/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001164 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001165 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001166 static void
1167free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001168{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001169 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001170
1171 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1172 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001173 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001174
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001175 // When garbage collecting a funccall_T may be freed before the
1176 // function that references it, clear its uf_scoped field.
1177 // The function may have been redefined and point to another
1178 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001179 if (fp != NULL && fp->uf_scoped == fc)
1180 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001181 }
Bram Moolenaar58016442016-07-31 18:30:22 +02001182 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001183
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001184 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001185 vim_free(fc);
1186}
1187
1188/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001189 * Free "fc" and what it contains.
1190 * Can be called only when "fc" is kept beyond the period of it called,
1191 * i.e. after cleanup_function_call(fc).
1192 */
1193 static void
1194free_funccal_contents(funccall_T *fc)
1195{
1196 listitem_T *li;
1197
1198 // Free all l: variables.
1199 vars_clear(&fc->l_vars.dv_hashtab);
1200
1201 // Free all a: variables.
1202 vars_clear(&fc->l_avars.dv_hashtab);
1203
1204 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001205 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001206 clear_tv(&li->li_tv);
1207
1208 free_funccal(fc);
1209}
1210
1211/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02001212 * Handle the last part of returning from a function: free the local hashtable.
1213 * Unless it is still in use by a closure.
1214 */
1215 static void
1216cleanup_function_call(funccall_T *fc)
1217{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001218 int may_free_fc = fc->fc_refcount <= 0;
1219 int free_fc = TRUE;
1220
Bram Moolenaar6914c642017-04-01 21:21:30 +02001221 current_funccal = fc->caller;
1222
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001223 // Free all l: variables if not referred.
1224 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
1225 vars_clear(&fc->l_vars.dv_hashtab);
1226 else
1227 free_fc = FALSE;
1228
1229 // If the a:000 list and the l: and a: dicts are not referenced and
1230 // there is no closure using it, we can free the funccall_T and what's
1231 // in it.
1232 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
1233 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02001234 else
1235 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001236 int todo;
1237 hashitem_T *hi;
1238 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02001239
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001240 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02001241
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001242 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001243 todo = (int)fc->l_avars.dv_hashtab.ht_used;
1244 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
1245 {
1246 if (!HASHITEM_EMPTY(hi))
1247 {
1248 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001249 di = HI2DI(hi);
1250 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02001251 }
1252 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001253 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02001254
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001255 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
1256 fc->l_varlist.lv_first = NULL;
1257 else
1258 {
1259 listitem_T *li;
1260
1261 free_fc = FALSE;
1262
1263 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001264 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02001265 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001266 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001267
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001268 if (free_fc)
1269 free_funccal(fc);
1270 else
1271 {
1272 static int made_copy = 0;
1273
1274 // "fc" is still in use. This can happen when returning "a:000",
1275 // assigning "l:" to a global variable or defining a closure.
1276 // Link "fc" in the list for garbage collection later.
1277 fc->caller = previous_funccal;
1278 previous_funccal = fc;
1279
1280 if (want_garbage_collect)
1281 // If garbage collector is ready, clear count.
1282 made_copy = 0;
1283 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001284 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001285 // We have made a lot of copies, worth 4 Mbyte. This can happen
1286 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01001287 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001288 // too much memory.
1289 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01001290 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001291 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02001292 }
1293}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001294
1295/*
1296 * There are two kinds of function names:
1297 * 1. ordinary names, function defined with :function or :def
1298 * 2. numbered functions and lambdas
1299 * For the first we only count the name stored in func_hashtab as a reference,
1300 * using function() does not count as a reference, because the function is
1301 * looked up by name.
1302 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02001303 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001304func_name_refcount(char_u *name)
1305{
1306 return isdigit(*name) || *name == '<';
1307}
1308
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309/*
1310 * Unreference "fc": decrement the reference count and free it when it
1311 * becomes zero. "fp" is detached from "fc".
1312 * When "force" is TRUE we are exiting.
1313 */
1314 static void
1315funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
1316{
1317 funccall_T **pfc;
1318 int i;
1319
1320 if (fc == NULL)
1321 return;
1322
1323 if (--fc->fc_refcount <= 0 && (force || (
1324 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
1325 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
1326 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
1327 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
1328 {
1329 if (fc == *pfc)
1330 {
1331 *pfc = fc->caller;
1332 free_funccal_contents(fc);
1333 return;
1334 }
1335 }
1336 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1337 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
1338 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
1339}
1340
1341/*
1342 * Remove the function from the function hashtable. If the function was
1343 * deleted while it still has references this was already done.
1344 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
1345 */
1346 static int
1347func_remove(ufunc_T *fp)
1348{
1349 hashitem_T *hi;
1350
1351 // Return if it was already virtually deleted.
1352 if (fp->uf_flags & FC_DEAD)
1353 return FALSE;
1354
1355 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
1356 if (!HASHITEM_EMPTY(hi))
1357 {
1358 // When there is a def-function index do not actually remove the
1359 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001360 // Do remove it when it's a copy.
1361 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 fp->uf_flags |= FC_DEAD;
1363 else
1364 hash_remove(&func_hashtab, hi);
1365 return TRUE;
1366 }
1367 return FALSE;
1368}
1369
1370 static void
1371func_clear_items(ufunc_T *fp)
1372{
1373 ga_clear_strings(&(fp->uf_args));
1374 ga_clear_strings(&(fp->uf_def_args));
1375 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001377 VIM_CLEAR(fp->uf_def_arg_idx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001378 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001379 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02001380 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001381
1382 // Increment the refcount of this function to avoid it being freed
1383 // recursively when the partial is freed.
1384 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001385 partial_unref(fp->uf_partial);
1386 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001387 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02001388
1389#ifdef FEAT_LUA
1390 if (fp->uf_cb_free != NULL)
1391 {
1392 fp->uf_cb_free(fp->uf_cb_state);
1393 fp->uf_cb_free = NULL;
1394 }
1395
1396 fp->uf_cb_state = NULL;
1397 fp->uf_cb = NULL;
1398#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399#ifdef FEAT_PROFILE
1400 VIM_CLEAR(fp->uf_tml_count);
1401 VIM_CLEAR(fp->uf_tml_total);
1402 VIM_CLEAR(fp->uf_tml_self);
1403#endif
1404}
1405
1406/*
1407 * Free all things that a function contains. Does not free the function
1408 * itself, use func_free() for that.
1409 * When "force" is TRUE we are exiting.
1410 */
1411 static void
1412func_clear(ufunc_T *fp, int force)
1413{
1414 if (fp->uf_cleared)
1415 return;
1416 fp->uf_cleared = TRUE;
1417
1418 // clear this function
1419 func_clear_items(fp);
1420 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001421 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001422}
1423
1424/*
1425 * Free a function and remove it from the list of functions. Does not free
1426 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001427 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02001428 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02001430 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001431func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432{
1433 // Only remove it when not done already, otherwise we would remove a newer
1434 // version of the function with the same name.
1435 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1436 func_remove(fp);
1437
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001438 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001439 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001440 if (fp->uf_dfunc_idx > 0)
1441 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001442 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001443 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02001444 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001445 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02001446 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447}
1448
1449/*
1450 * Free all things that a function contains and free the function itself.
1451 * When "force" is TRUE we are exiting.
1452 */
1453 static void
1454func_clear_free(ufunc_T *fp, int force)
1455{
1456 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001457 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
1458 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001459 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02001460 else
1461 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462}
1463
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001464/*
1465 * Copy already defined function "lambda" to a new function with name "global".
1466 * This is for when a compiled function defines a global function.
1467 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001468 int
1469copy_func(char_u *lambda, char_u *global, ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001470{
1471 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01001472 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001473
1474 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001475 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001476 semsg(_(e_lambda_function_not_found_str), lambda);
1477 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001478 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001479
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001480 fp = find_func(global, TRUE, NULL);
1481 if (fp != NULL)
1482 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001483 // TODO: handle ! to overwrite
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001484 semsg(_(e_funcexts), global);
1485 return FAIL;
1486 }
1487
1488 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
1489 if (fp == NULL)
1490 return FAIL;
1491
1492 fp->uf_varargs = ufunc->uf_varargs;
1493 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
1494 fp->uf_def_status = ufunc->uf_def_status;
1495 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
1496 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
1497 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
1498 == FAIL
1499 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
1500 goto failed;
1501
1502 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
1503 : vim_strsave(ufunc->uf_name_exp);
1504 if (ufunc->uf_arg_types != NULL)
1505 {
1506 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
1507 if (fp->uf_arg_types == NULL)
1508 goto failed;
1509 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
1510 sizeof(type_T *) * fp->uf_args.ga_len);
1511 }
1512 if (ufunc->uf_def_arg_idx != NULL)
1513 {
1514 fp->uf_def_arg_idx = ALLOC_MULT(int, fp->uf_def_args.ga_len + 1);
1515 if (fp->uf_def_arg_idx == NULL)
1516 goto failed;
1517 mch_memmove(fp->uf_def_arg_idx, ufunc->uf_def_arg_idx,
1518 sizeof(int) * fp->uf_def_args.ga_len + 1);
1519 }
1520 if (ufunc->uf_va_name != NULL)
1521 {
1522 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
1523 if (fp->uf_va_name == NULL)
1524 goto failed;
1525 }
1526 fp->uf_ret_type = ufunc->uf_ret_type;
1527
1528 fp->uf_refcount = 1;
1529 STRCPY(fp->uf_name, global);
1530 hash_add(&func_hashtab, UF2HIKEY(fp));
1531
1532 // the referenced dfunc_T is now used one more time
1533 link_def_function(fp);
1534
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001535 // Create a partial to store the context of the function where it was
1536 // instantiated. Only needs to be done once. Do this on the original
1537 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001538 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
1539 {
1540 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
1541
1542 if (pt == NULL)
1543 goto failed;
1544 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001545 {
1546 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001547 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001548 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001549 ufunc->uf_partial = pt;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001550 --pt->pt_refcount; // not actually referenced here
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001551 }
1552
1553 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001554
1555failed:
1556 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001557 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001558}
1559
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001560static int funcdepth = 0;
1561
1562/*
1563 * Increment the function call depth count.
1564 * Return FAIL when going over 'maxfuncdepth'.
1565 * Otherwise return OK, must call funcdepth_decrement() later!
1566 */
1567 int
1568funcdepth_increment(void)
1569{
1570 if (funcdepth >= p_mfd)
1571 {
1572 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
1573 return FAIL;
1574 }
1575 ++funcdepth;
1576 return OK;
1577}
1578
1579 void
1580funcdepth_decrement(void)
1581{
1582 --funcdepth;
1583}
1584
1585/*
1586 * Get the current function call depth.
1587 */
1588 int
1589funcdepth_get(void)
1590{
1591 return funcdepth;
1592}
1593
1594/*
1595 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001596 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001597 * times as funcdepth_increment().
1598 */
1599 void
1600funcdepth_restore(int depth)
1601{
1602 funcdepth = depth;
1603}
Bram Moolenaar6914c642017-04-01 21:21:30 +02001604
1605/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001606 * Call a user function.
1607 */
1608 static void
1609call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001610 ufunc_T *fp, // pointer to function
1611 int argcount, // nr of args
1612 typval_T *argvars, // arguments
1613 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001614 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01001615 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001616{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001617 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001618 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001619 funccall_T *fc;
1620 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001621 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001622 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001623 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001624 int i;
1625 int ai;
1626 int islambda = FALSE;
1627 char_u numbuf[NUMBUFLEN];
1628 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001629#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001630 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001631#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001632 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001633
Bram Moolenaarb2049902021-01-24 12:53:53 +01001634#ifdef FEAT_PROFILE
1635 CLEAR_FIELD(profile_info);
1636#endif
1637
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001638 // If depth of calling is getting too high, don't execute the function.
1639 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001640 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001641 rettv->v_type = VAR_NUMBER;
1642 rettv->vval.v_number = -1;
1643 return;
1644 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001645
Bram Moolenaare38eab22019-12-05 21:50:01 +01001646 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001647
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001648 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001649 if (fc == NULL)
1650 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001651 fc->caller = current_funccal;
1652 current_funccal = fc;
1653 fc->func = fp;
1654 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001655 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001656 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001657 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1658 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001659 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001660 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001661 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001662
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001663 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001664 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01001665#ifdef FEAT_PROFILE
1666 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
1667#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001668 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01001669#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001670 if (do_profiling == PROF_YES)
1671 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01001672#endif
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001673 call_def_function(fp, argcount, argvars, funcexe->partial, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001674 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01001675#ifdef FEAT_PROFILE
1676 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01001677 || (caller != NULL && caller->uf_profiling)))
1678 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01001679#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 current_funccal = fc->caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681 free_funccal(fc);
1682 return;
1683 }
1684
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001685 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1686 islambda = TRUE;
1687
1688 /*
1689 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1690 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1691 * each argument variable and saves a lot of time.
1692 */
1693 /*
1694 * Init l: variables.
1695 */
1696 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1697 if (selfdict != NULL)
1698 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001699 // Set l:self to "selfdict". Use "name" to avoid a warning from
1700 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001701 v = &fc->fixvar[fixvar_idx++].var;
1702 name = v->di_key;
1703 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001704 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001705 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1706 v->di_tv.v_type = VAR_DICT;
1707 v->di_tv.v_lock = 0;
1708 v->di_tv.vval.v_dict = selfdict;
1709 ++selfdict->dv_refcount;
1710 }
1711
1712 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001713 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001714 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001715 * Set a:000 to a list with room for the "..." arguments.
1716 */
1717 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001718 if ((fp->uf_flags & FC_NOARGS) == 0)
1719 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001720 (varnumber_T)(argcount >= fp->uf_args.ga_len
1721 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001722 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001723 if ((fp->uf_flags & FC_NOARGS) == 0)
1724 {
1725 // Use "name" to avoid a warning from some compiler that checks the
1726 // destination size.
1727 v = &fc->fixvar[fixvar_idx++].var;
1728 name = v->di_key;
1729 STRCPY(name, "000");
1730 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1731 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1732 v->di_tv.v_type = VAR_LIST;
1733 v->di_tv.v_lock = VAR_FIXED;
1734 v->di_tv.vval.v_list = &fc->l_varlist;
1735 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001736 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001737 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1738 fc->l_varlist.lv_lock = VAR_FIXED;
1739
1740 /*
1741 * Set a:firstline to "firstline" and a:lastline to "lastline".
1742 * Set a:name to named arguments.
1743 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001744 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001745 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001746 if ((fp->uf_flags & FC_NOARGS) == 0)
1747 {
1748 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001749 (varnumber_T)funcexe->firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001750 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001751 (varnumber_T)funcexe->lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001752 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001753 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001754 {
1755 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001756 typval_T def_rettv;
1757 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001758
1759 ai = i - fp->uf_args.ga_len;
1760 if (ai < 0)
1761 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001762 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001763 name = FUNCARG(fp, i);
1764 if (islambda)
1765 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001766
1767 // evaluate named argument default expression
1768 isdefault = ai + fp->uf_def_args.ga_len >= 0
1769 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1770 && argvars[i].vval.v_number == VVAL_NONE));
1771 if (isdefault)
1772 {
1773 char_u *default_expr = NULL;
1774 def_rettv.v_type = VAR_NUMBER;
1775 def_rettv.vval.v_number = -1;
1776
1777 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1778 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001779 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001780 {
1781 default_arg_err = 1;
1782 break;
1783 }
1784 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001785 }
1786 else
1787 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001788 if ((fp->uf_flags & FC_NOARGS) != 0)
1789 // Bail out if no a: arguments used (in lambda).
1790 break;
1791
Bram Moolenaare38eab22019-12-05 21:50:01 +01001792 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001793 sprintf((char *)numbuf, "%d", ai + 1);
1794 name = numbuf;
1795 }
1796 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1797 {
1798 v = &fc->fixvar[fixvar_idx++].var;
1799 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001800 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001801 }
1802 else
1803 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001804 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001805 if (v == NULL)
1806 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001807 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001808 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001809
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001810 // Note: the values are copied directly to avoid alloc/free.
1811 // "argvars" must have VAR_FIXED for v_lock.
1812 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001813 v->di_tv.v_lock = VAR_FIXED;
1814
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001815 if (addlocal)
1816 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001817 // Named arguments should be accessed without the "a:" prefix in
1818 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001819 copy_tv(&v->di_tv, &v->di_tv);
1820 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001821 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001822 else
1823 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001824
1825 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1826 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001827 listitem_T *li = &fc->l_listitems[ai];
1828
1829 li->li_tv = argvars[i];
1830 li->li_tv.v_lock = VAR_FIXED;
1831 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001832 }
1833 }
1834
Bram Moolenaare38eab22019-12-05 21:50:01 +01001835 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001836 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001837
1838 if (fp->uf_flags & FC_SANDBOX)
1839 {
1840 using_sandbox = TRUE;
1841 ++sandbox;
1842 }
1843
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001844 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001845 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001846 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001847 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001848 ++no_wait_return;
1849 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001850
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001851 smsg(_("calling %s"), SOURCING_NAME);
1852 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001853 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001854 char_u buf[MSG_BUF_LEN];
1855 char_u numbuf2[NUMBUFLEN];
1856 char_u *tofree;
1857 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001858
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001859 msg_puts("(");
1860 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001861 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001862 if (i > 0)
1863 msg_puts(", ");
1864 if (argvars[i].v_type == VAR_NUMBER)
1865 msg_outnum((long)argvars[i].vval.v_number);
1866 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001867 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001868 // Do not want errors such as E724 here.
1869 ++emsg_off;
1870 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1871 --emsg_off;
1872 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001873 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001874 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001875 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001876 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1877 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001878 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001879 msg_puts((char *)s);
1880 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001881 }
1882 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001883 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001884 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001885 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001886 msg_puts("\n"); // don't overwrite this either
1887
1888 verbose_leave_scroll();
1889 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001890 }
1891#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001892 if (do_profiling == PROF_YES)
1893 profile_may_start_func(&profile_info, fp,
1894 fc->caller == NULL ? NULL : fc->caller->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001895#endif
1896
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001897 save_current_sctx = current_sctx;
1898 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001899 save_did_emsg = did_emsg;
1900 did_emsg = FALSE;
1901
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001902 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1903 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001904 else if (islambda)
1905 {
1906 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
1907
1908 // A Lambda always has the command "return {expr}". It is much faster
1909 // to evaluate {expr} directly.
1910 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001911 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001912 --ex_nesting_level;
1913 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001914 else
1915 // call do_cmdline() to execute the lines
1916 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001917 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1918
1919 --RedrawingDisabled;
1920
Bram Moolenaare38eab22019-12-05 21:50:01 +01001921 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001922 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1923 {
1924 clear_tv(rettv);
1925 rettv->v_type = VAR_NUMBER;
1926 rettv->vval.v_number = -1;
1927 }
1928
1929#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001930 if (do_profiling == PROF_YES)
1931 {
1932 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
1933
1934 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
1935 profile_may_end_func(&profile_info, fp, caller);
1936 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001937#endif
1938
Bram Moolenaare38eab22019-12-05 21:50:01 +01001939 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001940 if (p_verbose >= 12)
1941 {
1942 ++no_wait_return;
1943 verbose_enter_scroll();
1944
1945 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001946 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001947 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001948 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001949 (long)fc->rettv->vval.v_number);
1950 else
1951 {
1952 char_u buf[MSG_BUF_LEN];
1953 char_u numbuf2[NUMBUFLEN];
1954 char_u *tofree;
1955 char_u *s;
1956
Bram Moolenaare38eab22019-12-05 21:50:01 +01001957 // The value may be very long. Skip the middle part, so that we
1958 // have some idea how it starts and ends. smsg() would always
1959 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001960 ++emsg_off;
1961 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1962 --emsg_off;
1963 if (s != NULL)
1964 {
1965 if (vim_strsize(s) > MSG_BUF_CLEN)
1966 {
1967 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1968 s = buf;
1969 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001970 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001971 vim_free(tofree);
1972 }
1973 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001974 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001975
1976 verbose_leave_scroll();
1977 --no_wait_return;
1978 }
1979
Bram Moolenaare31ee862020-01-07 20:59:34 +01001980 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001981 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001982 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001983#ifdef FEAT_PROFILE
1984 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001985 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001986#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001987 if (using_sandbox)
1988 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001989
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001990 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001991 {
1992 ++no_wait_return;
1993 verbose_enter_scroll();
1994
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001995 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001996 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001997
1998 verbose_leave_scroll();
1999 --no_wait_return;
2000 }
2001
2002 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002003 funcdepth_decrement();
Bram Moolenaar6914c642017-04-01 21:21:30 +02002004 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002005}
2006
2007/*
Bram Moolenaar50824712020-12-20 21:10:17 +01002008 * Check the argument count for user function "fp".
2009 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
2010 */
2011 int
2012check_user_func_argcount(ufunc_T *fp, int argcount)
2013{
2014 int regular_args = fp->uf_args.ga_len;
2015
2016 if (argcount < regular_args - fp->uf_def_args.ga_len)
2017 return FCERR_TOOFEW;
2018 else if (!has_varargs(fp) && argcount > regular_args)
2019 return FCERR_TOOMANY;
2020 return FCERR_UNKNOWN;
2021}
2022
2023/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002025 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002026 int
2027call_user_func_check(
2028 ufunc_T *fp,
2029 int argcount,
2030 typval_T *argvars,
2031 typval_T *rettv,
2032 funcexe_T *funcexe,
2033 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002034{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002036
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002037 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
2038 *funcexe->doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01002039 error = check_user_func_argcount(fp, argcount);
2040 if (error != FCERR_UNKNOWN)
2041 return error;
2042 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002043 error = FCERR_DICT;
2044 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002045 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046 int did_save_redo = FALSE;
2047 save_redo_T save_redo;
2048
2049 /*
2050 * Call the user function.
2051 * Save and restore search patterns, script variables and
2052 * redo buffer.
2053 */
2054 save_search_patterns();
2055 if (!ins_compl_active())
2056 {
2057 saveRedobuff(&save_redo);
2058 did_save_redo = TRUE;
2059 }
2060 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002061 call_user_func(fp, argcount, argvars, rettv, funcexe,
2062 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
2064 // Function was unreferenced while being used, free it now.
2065 func_clear_free(fp, FALSE);
2066 if (did_save_redo)
2067 restoreRedobuff(&save_redo);
2068 restore_search_patterns();
2069 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002070 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002071 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002072}
2073
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002074static funccal_entry_T *funccal_stack = NULL;
2075
2076/*
2077 * Save the current function call pointer, and set it to NULL.
2078 * Used when executing autocommands and for ":source".
2079 */
2080 void
2081save_funccal(funccal_entry_T *entry)
2082{
2083 entry->top_funccal = current_funccal;
2084 entry->next = funccal_stack;
2085 funccal_stack = entry;
2086 current_funccal = NULL;
2087}
2088
2089 void
2090restore_funccal(void)
2091{
2092 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002093 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002094 else
2095 {
2096 current_funccal = funccal_stack->top_funccal;
2097 funccal_stack = funccal_stack->next;
2098 }
2099}
2100
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02002101 funccall_T *
2102get_current_funccal(void)
2103{
2104 return current_funccal;
2105}
2106
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002107/*
2108 * Mark all functions of script "sid" as deleted.
2109 */
2110 void
2111delete_script_functions(int sid)
2112{
2113 hashitem_T *hi;
2114 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02002115 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002116 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002117 size_t len;
2118
2119 buf[0] = K_SPECIAL;
2120 buf[1] = KS_EXTRA;
2121 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002122 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002123 len = STRLEN(buf);
2124
Bram Moolenaarce658352020-07-31 23:47:12 +02002125 while (todo > 0)
2126 {
2127 todo = func_hashtab.ht_used;
2128 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2129 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002130 {
Bram Moolenaarce658352020-07-31 23:47:12 +02002131 fp = HI2UF(hi);
2132 if (STRNCMP(fp->uf_name, buf, len) == 0)
2133 {
2134 int changed = func_hashtab.ht_changed;
2135
2136 fp->uf_flags |= FC_DEAD;
2137 func_clear(fp, TRUE);
2138 // When clearing a function another function can be cleared
2139 // as a side effect. When that happens start over.
2140 if (changed != func_hashtab.ht_changed)
2141 break;
2142 }
2143 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002144 }
Bram Moolenaarce658352020-07-31 23:47:12 +02002145 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002146}
2147
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002148#if defined(EXITFREE) || defined(PROTO)
2149 void
2150free_all_functions(void)
2151{
2152 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002153 ufunc_T *fp;
2154 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002155 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002156 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002157
Bram Moolenaare38eab22019-12-05 21:50:01 +01002158 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02002159 while (current_funccal != NULL)
2160 {
2161 clear_tv(current_funccal->rettv);
2162 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002163 if (current_funccal == NULL && funccal_stack != NULL)
2164 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02002165 }
2166
Bram Moolenaare38eab22019-12-05 21:50:01 +01002167 // First clear what the functions contain. Since this may lower the
2168 // reference count of a function, it may also free a function and change
2169 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002170 while (todo > 0)
2171 {
2172 todo = func_hashtab.ht_used;
2173 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2174 if (!HASHITEM_EMPTY(hi))
2175 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002176 // clear the def function index now
2177 fp = HI2UF(hi);
2178 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002179 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180
Bram Moolenaare38eab22019-12-05 21:50:01 +01002181 // Only free functions that are not refcounted, those are
2182 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002183 if (func_name_refcount(fp->uf_name))
2184 ++skipped;
2185 else
2186 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002187 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002188 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002189 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002190 {
2191 skipped = 0;
2192 break;
2193 }
2194 }
2195 --todo;
2196 }
2197 }
2198
Bram Moolenaare38eab22019-12-05 21:50:01 +01002199 // Now actually free the functions. Need to start all over every time,
2200 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002201 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002202 while (func_hashtab.ht_used > skipped)
2203 {
2204 todo = func_hashtab.ht_used;
2205 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002206 if (!HASHITEM_EMPTY(hi))
2207 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02002208 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002209 // Only free functions that are not refcounted, those are
2210 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02002211 fp = HI2UF(hi);
2212 if (func_name_refcount(fp->uf_name))
2213 ++skipped;
2214 else
2215 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02002216 if (func_free(fp, FALSE) == OK)
2217 {
2218 skipped = 0;
2219 break;
2220 }
2221 // did not actually free it
2222 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002223 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002224 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02002225 }
2226 if (skipped == 0)
2227 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228
2229 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002230}
2231#endif
2232
2233/*
2234 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02002235 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002236 * "len" is the length of "name", or -1 for NUL terminated.
2237 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002239builtin_function(char_u *name, int len)
2240{
2241 char_u *p;
2242
Bram Moolenaara26b9702020-04-18 19:53:28 +02002243 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002244 return FALSE;
2245 p = vim_strchr(name, AUTOLOAD_CHAR);
2246 return p == NULL || (len > 0 && p > name + len);
2247}
2248
2249 int
2250func_call(
2251 char_u *name,
2252 typval_T *args,
2253 partial_T *partial,
2254 dict_T *selfdict,
2255 typval_T *rettv)
2256{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002257 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002258 listitem_T *item;
2259 typval_T argv[MAX_FUNC_ARGS + 1];
2260 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002261 int r = 0;
2262
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002263 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002264 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002265 {
2266 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
2267 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002268 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002269 break;
2270 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002271 // Make a copy of each argument. This is needed to be able to set
2272 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002273 copy_tv(&item->li_tv, &argv[argc++]);
2274 }
2275
2276 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002277 {
2278 funcexe_T funcexe;
2279
Bram Moolenaara80faa82020-04-12 19:37:17 +02002280 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002281 funcexe.firstline = curwin->w_cursor.lnum;
2282 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002283 funcexe.evaluate = TRUE;
2284 funcexe.partial = partial;
2285 funcexe.selfdict = selfdict;
2286 r = call_func(name, -1, rettv, argc, argv, &funcexe);
2287 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002288
Bram Moolenaare38eab22019-12-05 21:50:01 +01002289 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002290 while (argc > 0)
2291 clear_tv(&argv[--argc]);
2292
2293 return r;
2294}
2295
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002296static int callback_depth = 0;
2297
2298 int
2299get_callback_depth(void)
2300{
2301 return callback_depth;
2302}
2303
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002304/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002305 * Invoke call_func() with a callback.
2306 */
2307 int
2308call_callback(
2309 callback_T *callback,
2310 int len, // length of "name" or -1 to use strlen()
2311 typval_T *rettv, // return value goes here
2312 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002313 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002314 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002315{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002316 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002317 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002318
Bram Moolenaara80faa82020-04-12 19:37:17 +02002319 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002320 funcexe.evaluate = TRUE;
2321 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002322 ++callback_depth;
2323 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
2324 --callback_depth;
2325 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002326}
2327
2328/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002329 * Give an error message for the result of a function.
2330 * Nothing if "error" is FCERR_NONE.
2331 */
2332 void
2333user_func_error(int error, char_u *name)
2334{
2335 switch (error)
2336 {
2337 case FCERR_UNKNOWN:
2338 emsg_funcname(e_unknownfunc, name);
2339 break;
2340 case FCERR_NOTMETHOD:
2341 emsg_funcname(
2342 N_("E276: Cannot use function as a method: %s"), name);
2343 break;
2344 case FCERR_DELETED:
2345 emsg_funcname(N_(e_func_deleted), name);
2346 break;
2347 case FCERR_TOOMANY:
2348 emsg_funcname((char *)e_toomanyarg, name);
2349 break;
2350 case FCERR_TOOFEW:
2351 emsg_funcname((char *)e_toofewarg, name);
2352 break;
2353 case FCERR_SCRIPT:
2354 emsg_funcname(
2355 N_("E120: Using <SID> not in a script context: %s"), name);
2356 break;
2357 case FCERR_DICT:
2358 emsg_funcname(
2359 N_("E725: Calling dict function without Dictionary: %s"),
2360 name);
2361 break;
2362 }
2363}
2364
2365/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002366 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002367 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002368 * Return FAIL when the function can't be called, OK otherwise.
2369 * Also returns OK when an error was encountered while executing the function.
2370 */
2371 int
2372call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02002373 char_u *funcname, // name of the function
2374 int len, // length of "name" or -1 to use strlen()
2375 typval_T *rettv, // return value goes here
2376 int argcount_in, // number of "argvars"
2377 typval_T *argvars_in, // vars for arguments, must have "argcount"
2378 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002379 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002380{
2381 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01002382 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002383 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002384 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002385 char_u fname_buf[FLEN_FIXED + 1];
2386 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002387 char_u *fname = NULL;
2388 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002389 int argcount = argcount_in;
2390 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002391 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002392 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
2393 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002394 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002395 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002396 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002397
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02002398 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
2399 // even when call_func() returns FAIL.
2400 rettv->v_type = VAR_UNKNOWN;
2401
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002402 if (partial != NULL)
2403 fp = partial->pt_func;
2404 if (fp == NULL)
2405 {
2406 // Make a copy of the name, if it comes from a funcref variable it
2407 // could be changed or deleted in the called function.
2408 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
2409 if (name == NULL)
2410 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002411
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002412 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
2413 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002414
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002415 if (funcexe->doesrange != NULL)
2416 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002417
2418 if (partial != NULL)
2419 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002420 // When the function has a partial with a dict and there is a dict
2421 // argument, use the dict argument. That is backwards compatible.
2422 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002423 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002424 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01002425 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002426 {
2427 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002428 {
2429 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
2430 {
Bram Moolenaaref140542019-12-31 21:27:13 +01002431 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002432 goto theend;
2433 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002434 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002435 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002436 for (i = 0; i < argcount_in; ++i)
2437 argv[i + argv_clear] = argvars_in[i];
2438 argvars = argv;
2439 argcount = partial->pt_argc + argcount_in;
2440 }
2441 }
2442
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002443 if (error == FCERR_NONE && funcexe->check_type != NULL && funcexe->evaluate)
2444 {
2445 // Check that the argument types are OK for the types of the funcref.
2446 if (check_argument_types(funcexe->check_type, argvars, argcount,
2447 name) == FAIL)
2448 error = FCERR_OTHER;
2449 }
2450
Bram Moolenaaref140542019-12-31 21:27:13 +01002451 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002452 {
2453 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02002454 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002455
Bram Moolenaar333894b2020-08-01 18:53:07 +02002456 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002457 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02002458 {
2459 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002460 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02002461 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002462
Bram Moolenaare38eab22019-12-05 21:50:01 +01002463 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002464 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01002465 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002466
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002467 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002468 {
2469 /*
2470 * User defined function.
2471 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002472 if (fp == NULL)
Bram Moolenaar333894b2020-08-01 18:53:07 +02002473 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002474
Bram Moolenaare38eab22019-12-05 21:50:01 +01002475 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002476 if (fp == NULL
2477 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002478 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002479 && !aborting())
2480 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002481 // executed an autocommand, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02002482 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002483 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002484 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002485 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
2486 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002487 // loaded a package, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02002488 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002489 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02002490 if (fp == NULL)
2491 {
2492 char_u *p = untrans_function_name(rfname);
2493
2494 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02002495 // Don't do this if the name starts with "s:".
2496 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaar333894b2020-08-01 18:53:07 +02002497 fp = find_func(p, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002498 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002499
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002500 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01002501 error = FCERR_DELETED;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002502#ifdef FEAT_LUA
2503 else if (fp != NULL && (fp->uf_flags & FC_CFUNC))
2504 {
2505 cfunc_T cb = fp->uf_cb;
2506
2507 error = (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
2508 }
2509#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002510 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002511 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002512 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01002513 // postponed filling in the arguments, do it now
2514 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002515 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002516
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002517 if (funcexe->basetv != NULL)
2518 {
2519 // Method call: base->Method()
2520 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
2521 argv[0] = *funcexe->basetv;
2522 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002523 argvars = argv;
2524 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002525 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002526
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 error = call_user_func_check(fp, argcount, argvars, rettv,
2528 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002529 }
2530 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002531 else if (funcexe->basetv != NULL)
2532 {
2533 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002534 * expr->method(): Find the method name in the table, call its
2535 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02002536 */
2537 error = call_internal_method(fname, argcount, argvars, rettv,
2538 funcexe->basetv);
2539 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002540 else
2541 {
2542 /*
2543 * Find the function name in the table, call its implementation.
2544 */
2545 error = call_internal_func(fname, argcount, argvars, rettv);
2546 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02002547
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002548 /*
2549 * The function call (or "FuncUndefined" autocommand sequence) might
2550 * have been aborted by an error, an interrupt, or an explicitly thrown
2551 * exception that has not been caught so far. This situation can be
2552 * tested for by calling aborting(). For an error in an internal
2553 * function or for the "E132" error in call_user_func(), however, the
2554 * throw point at which the "force_abort" flag (temporarily reset by
2555 * emsg()) is normally updated has not been reached yet. We need to
2556 * update that flag first to make aborting() reliable.
2557 */
2558 update_force_abort();
2559 }
Bram Moolenaaref140542019-12-31 21:27:13 +01002560 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002561 ret = OK;
2562
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002563theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002564 /*
2565 * Report an error unless the argument evaluation or function call has been
2566 * cancelled due to an aborting error, an interrupt, or an exception.
2567 */
2568 if (!aborting())
2569 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002570 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002571 }
2572
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002573 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002574 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002575 clear_tv(&argv[--argv_clear + argv_base]);
2576
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002577 vim_free(tofree);
2578 vim_free(name);
2579
2580 return ret;
2581}
2582
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002583 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584printable_func_name(ufunc_T *fp)
2585{
2586 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
2587}
2588
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002589/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002590 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002591 */
2592 static void
2593list_func_head(ufunc_T *fp, int indent)
2594{
2595 int j;
2596
2597 msg_start();
2598 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002599 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002600 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002601 msg_puts("def ");
2602 else
2603 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002605 msg_putchar('(');
2606 for (j = 0; j < fp->uf_args.ga_len; ++j)
2607 {
2608 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002609 msg_puts(", ");
2610 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 if (fp->uf_arg_types != NULL)
2612 {
2613 char *tofree;
2614
2615 msg_puts(": ");
2616 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
2617 vim_free(tofree);
2618 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002619 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
2620 {
2621 msg_puts(" = ");
2622 msg_puts(((char **)(fp->uf_def_args.ga_data))
2623 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
2624 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002625 }
2626 if (fp->uf_varargs)
2627 {
2628 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002629 msg_puts(", ");
2630 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002631 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 if (fp->uf_va_name != NULL)
2633 {
2634 if (j)
2635 msg_puts(", ");
2636 msg_puts("...");
2637 msg_puts((char *)fp->uf_va_name);
2638 if (fp->uf_va_type)
2639 {
2640 char *tofree;
2641
2642 msg_puts(": ");
2643 msg_puts(type_name(fp->uf_va_type, &tofree));
2644 vim_free(tofree);
2645 }
2646 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002647 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002648
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002649 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002650 {
2651 if (fp->uf_ret_type != &t_void)
2652 {
2653 char *tofree;
2654
2655 msg_puts(": ");
2656 msg_puts(type_name(fp->uf_ret_type, &tofree));
2657 vim_free(tofree);
2658 }
2659 }
2660 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002661 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002662 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002663 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002664 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002665 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002666 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002667 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002668 msg_clr_eos();
2669 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002670 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002671}
2672
2673/*
2674 * Get a function name, translating "<SID>" and "<SNR>".
2675 * Also handles a Funcref in a List or Dictionary.
2676 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002677 * Set "*is_global" to TRUE when the function must be global, unless
2678 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002679 * flags:
2680 * TFN_INT: internal function name OK
2681 * TFN_QUIET: be quiet
2682 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002683 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002684 * Advances "pp" to just after the function name (if no error).
2685 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002686 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002687trans_function_name(
2688 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002689 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002690 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002691 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002692 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002693 partial_T **partial, // return: partial of a FuncRef
2694 type_T **type) // return: type of funcref if not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002695{
2696 char_u *name = NULL;
2697 char_u *start;
2698 char_u *end;
2699 int lead;
2700 char_u sid_buf[20];
2701 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002703 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 int vim9script;
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002705 static char *e_function_name = N_("E129: Function name required");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002706
2707 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02002708 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002709 start = *pp;
2710
Bram Moolenaare38eab22019-12-05 21:50:01 +01002711 // Check for hard coded <SNR>: already translated function ID (from a user
2712 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002713 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2714 && (*pp)[2] == (int)KE_SNR)
2715 {
2716 *pp += 3;
2717 len = get_id_len(pp) + 3;
2718 return vim_strnsave(start, len);
2719 }
2720
Bram Moolenaare38eab22019-12-05 21:50:01 +01002721 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2722 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002723 lead = eval_fname_script(start);
2724 if (lead > 2)
2725 start += lead;
2726
Bram Moolenaare38eab22019-12-05 21:50:01 +01002727 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002728 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002729 lead > 2 ? 0 : FNE_CHECK_START);
2730 if (end == start)
2731 {
2732 if (!skip)
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002733 emsg(_(e_function_name));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002734 goto theend;
2735 }
2736 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2737 {
2738 /*
2739 * Report an invalid expression in braces, unless the expression
2740 * evaluation has been cancelled due to an aborting error, an
2741 * interrupt, or an exception.
2742 */
2743 if (!aborting())
2744 {
2745 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002746 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002747 }
2748 else
2749 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2750 goto theend;
2751 }
2752
2753 if (lv.ll_tv != NULL)
2754 {
2755 if (fdp != NULL)
2756 {
2757 fdp->fd_dict = lv.ll_dict;
2758 fdp->fd_newkey = lv.ll_newkey;
2759 lv.ll_newkey = NULL;
2760 fdp->fd_di = lv.ll_di;
2761 }
2762 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2763 {
2764 name = vim_strsave(lv.ll_tv->vval.v_string);
2765 *pp = end;
2766 }
2767 else if (lv.ll_tv->v_type == VAR_PARTIAL
2768 && lv.ll_tv->vval.v_partial != NULL)
2769 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002770 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002771 *pp = end;
2772 if (partial != NULL)
2773 *partial = lv.ll_tv->vval.v_partial;
2774 }
2775 else
2776 {
2777 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2778 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002779 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002780 else
2781 *pp = end;
2782 name = NULL;
2783 }
2784 goto theend;
2785 }
2786
2787 if (lv.ll_name == NULL)
2788 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002789 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002790 *pp = end;
2791 goto theend;
2792 }
2793
Bram Moolenaare38eab22019-12-05 21:50:01 +01002794 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002795 if (lv.ll_exp_name != NULL)
2796 {
2797 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002798 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002799 flags & TFN_NO_AUTOLOAD);
2800 if (name == lv.ll_exp_name)
2801 name = NULL;
2802 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002803 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002804 {
2805 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002806 name = deref_func_name(*pp, &len, partial, type,
2807 flags & TFN_NO_AUTOLOAD);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002808 if (name == *pp)
2809 name = NULL;
2810 }
2811 if (name != NULL)
2812 {
2813 name = vim_strsave(name);
2814 *pp = end;
2815 if (STRNCMP(name, "<SNR>", 5) == 0)
2816 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002817 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002818 name[0] = K_SPECIAL;
2819 name[1] = KS_EXTRA;
2820 name[2] = (int)KE_SNR;
2821 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2822 }
2823 goto theend;
2824 }
2825
2826 if (lv.ll_exp_name != NULL)
2827 {
2828 len = (int)STRLEN(lv.ll_exp_name);
2829 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2830 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2831 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002832 // When there was "s:" already or the name expanded to get a
2833 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002834 lv.ll_name += 2;
2835 len -= 2;
2836 lead = 2;
2837 }
2838 }
2839 else
2840 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002841 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002842 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002843 {
2844 if (is_global != NULL && lv.ll_name[0] == 'g')
2845 *is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002846 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002847 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002848 len = (int)(end - lv.ll_name);
2849 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002850 if (len <= 0)
2851 {
2852 if (!skip)
2853 emsg(_(e_function_name));
2854 goto theend;
2855 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002856
Bram Moolenaar17f700a2020-12-19 21:23:42 +01002857 // In Vim9 script a user function is script-local by default, unless it
2858 // starts with a lower case character: dict.func().
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002859 vim9script = ASCII_ISUPPER(*start) && in_vim9script();
Bram Moolenaar17f700a2020-12-19 21:23:42 +01002860 if (vim9script)
2861 {
2862 char_u *p;
2863
2864 // SomeScript#func() is a global function.
2865 for (p = start; *p != NUL && *p != '('; ++p)
2866 if (*p == AUTOLOAD_CHAR)
2867 vim9script = FALSE;
2868 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002870 /*
2871 * Copy the function name to allocated memory.
2872 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2873 * Accept <SNR>123_name() outside a script.
2874 */
2875 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002876 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002878 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002879 if (!vim9script)
2880 lead = 3;
2881 if (vim9script || (lv.ll_exp_name != NULL
2882 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002883 || eval_fname_sid(*pp))
2884 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002886 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002887 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002888 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002889 goto theend;
2890 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002891 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 if (vim9script)
2893 extra = 3 + (int)STRLEN(sid_buf);
2894 else
2895 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002896 }
2897 }
2898 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2899 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002900 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002901 start);
2902 goto theend;
2903 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002904 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002905 {
2906 char_u *cp = vim_strchr(lv.ll_name, ':');
2907
2908 if (cp != NULL && cp < end)
2909 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002910 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002911 goto theend;
2912 }
2913 }
2914
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002916 if (name != NULL)
2917 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002918 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002919 {
2920 name[0] = K_SPECIAL;
2921 name[1] = KS_EXTRA;
2922 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002924 STRCPY(name + 3, sid_buf);
2925 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2927 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002928 }
2929 *pp = end;
2930
2931theend:
2932 clear_lval(&lv);
2933 return name;
2934}
2935
2936/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02002937 * Assuming "name" is the result of trans_function_name() and it was prefixed
2938 * to use the script-local name, return the unmodified name (points into
2939 * "name"). Otherwise return NULL.
2940 * This can be used to first search for a script-local function and fall back
2941 * to the global function if not found.
2942 */
2943 char_u *
2944untrans_function_name(char_u *name)
2945{
2946 char_u *p;
2947
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002948 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02002949 {
2950 p = vim_strchr(name, '_');
2951 if (p != NULL)
2952 return p + 1;
2953 }
2954 return NULL;
2955}
2956
2957/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002958 * List functions. When "regmatch" is NULL all of then.
2959 * Otherwise functions matching "regmatch".
2960 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002961 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002962list_functions(regmatch_T *regmatch)
2963{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002964 int changed = func_hashtab.ht_changed;
2965 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002966 hashitem_T *hi;
2967
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002968 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002969 {
2970 if (!HASHITEM_EMPTY(hi))
2971 {
2972 ufunc_T *fp = HI2UF(hi);
2973
2974 --todo;
2975 if ((fp->uf_flags & FC_DEAD) == 0
2976 && (regmatch == NULL
2977 ? !message_filtered(fp->uf_name)
2978 && !func_name_refcount(fp->uf_name)
2979 : !isdigit(*fp->uf_name)
2980 && vim_regexec(regmatch, fp->uf_name, 0)))
2981 {
2982 list_func_head(fp, FALSE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002983 if (changed != func_hashtab.ht_changed)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002984 {
2985 emsg(_("E454: function list was modified"));
2986 return;
2987 }
2988 }
2989 }
2990 }
2991}
2992
2993/*
Bram Moolenaaradc8e442020-12-31 18:28:18 +01002994 * Check if "*cmd" points to a function command and if so advance "*cmd" and
2995 * return TRUE.
2996 * Otherwise return FALSE;
2997 * Do not consider "function(" to be a command.
2998 */
2999 static int
3000is_function_cmd(char_u **cmd)
3001{
3002 char_u *p = *cmd;
3003
3004 if (checkforcmd(&p, "function", 2))
3005 {
3006 if (*p == '(')
3007 return FALSE;
3008 *cmd = p;
3009 return TRUE;
3010 }
3011 return FALSE;
3012}
3013
3014/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02003015 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003016 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
3017 * the function name.
Bram Moolenaar04b12692020-05-04 23:24:44 +02003018 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003019 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02003020 ufunc_T *
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003021define_function(exarg_T *eap, char_u *name_arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003022{
3023 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02003024 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003025 int j;
3026 int c;
3027 int saved_did_emsg;
3028 int saved_wait_return = need_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003029 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003030 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003031 char_u *p;
3032 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01003033 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003034 char_u *line_arg = NULL;
3035 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003037 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003038 garray_T newlines;
3039 int varargs = FALSE;
3040 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003042 ufunc_T *fp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003043 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003044 int indent;
3045 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046#define MAX_FUNC_NESTING 50
3047 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003048 dictitem_T *v;
3049 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003050 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003051 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003052 hashitem_T *hi;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01003053 getline_opt_T getline_options;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003054 linenr_T sourcing_lnum_off;
3055 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003056 int is_heredoc = FALSE;
3057 char_u *skip_until = NULL;
3058 char_u *heredoc_trimmed = NULL;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003059 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02003060 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003061
3062 /*
3063 * ":function" without argument: list functions.
3064 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003065 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003066 {
3067 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003068 list_functions(NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003069 eap->nextcmd = check_nextcmd(eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003070 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003071 }
3072
3073 /*
3074 * ":function /pat": list functions matching pattern.
3075 */
3076 if (*eap->arg == '/')
3077 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003078 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003079 if (!eap->skip)
3080 {
3081 regmatch_T regmatch;
3082
3083 c = *p;
3084 *p = NUL;
3085 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
3086 *p = c;
3087 if (regmatch.regprog != NULL)
3088 {
3089 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003090 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003091 vim_regfree(regmatch.regprog);
3092 }
3093 }
3094 if (*p == '/')
3095 ++p;
3096 eap->nextcmd = check_nextcmd(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003097 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003098 }
3099
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 ga_init(&newargs);
3101 ga_init(&argtypes);
3102 ga_init(&default_args);
3103
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003104 /*
3105 * Get the function name. There are these situations:
3106 * func normal function name
3107 * "name" == func, "fudi.fd_dict" == NULL
3108 * dict.func new dictionary entry
3109 * "name" == NULL, "fudi.fd_dict" set,
3110 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
3111 * dict.func existing dict entry with a Funcref
3112 * "name" == func, "fudi.fd_dict" set,
3113 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
3114 * dict.func existing dict entry that's not a Funcref
3115 * "name" == NULL, "fudi.fd_dict" set,
3116 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
3117 * s:func script-local function name
3118 * g:func global function name, same as "func"
3119 */
3120 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003121 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003122 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02003123 // nested function, argument is (args).
3124 paren = TRUE;
3125 CLEAR_FIELD(fudi);
3126 }
3127 else
3128 {
Bram Moolenaarb6571982021-01-08 22:24:19 +01003129 if (STRNCMP(p, "<lambda>", 8) == 0)
3130 {
3131 p += 8;
3132 (void)getdigits(&p);
3133 name = vim_strnsave(eap->arg, p - eap->arg);
3134 CLEAR_FIELD(fudi);
3135 }
3136 else
3137 name = trans_function_name(&p, &is_global, eap->skip,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003138 TFN_NO_AUTOLOAD, &fudi, NULL, NULL);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003139 paren = (vim_strchr(p, '(') != NULL);
3140 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003141 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02003142 /*
3143 * Return on an invalid expression in braces, unless the expression
3144 * evaluation has been cancelled due to an aborting error, an
3145 * interrupt, or an exception.
3146 */
3147 if (!aborting())
3148 {
3149 if (!eap->skip && fudi.fd_newkey != NULL)
3150 semsg(_(e_dictkey), fudi.fd_newkey);
3151 vim_free(fudi.fd_newkey);
3152 return NULL;
3153 }
3154 else
3155 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003156 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003157 }
3158
Bram Moolenaare38eab22019-12-05 21:50:01 +01003159 // An error in a function call during evaluation of an expression in magic
3160 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003161 saved_did_emsg = did_emsg;
3162 did_emsg = FALSE;
3163
3164 /*
3165 * ":function func" with only function name: list function.
3166 */
3167 if (!paren)
3168 {
3169 if (!ends_excmd(*skipwhite(p)))
3170 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003171 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003172 goto ret_free;
3173 }
3174 eap->nextcmd = check_nextcmd(p);
3175 if (eap->nextcmd != NULL)
3176 *p = NUL;
3177 if (!eap->skip && !got_int)
3178 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003179 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003180 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
3181 {
3182 char_u *up = untrans_function_name(name);
3183
3184 // With Vim9 script the name was made script-local, if not
3185 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02003186 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003187 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003188 }
3189
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003190 if (fp != NULL)
3191 {
3192 list_func_head(fp, TRUE);
3193 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
3194 {
3195 if (FUNCLINE(fp, j) == NULL)
3196 continue;
3197 msg_putchar('\n');
3198 msg_outnum((long)(j + 1));
3199 if (j < 9)
3200 msg_putchar(' ');
3201 if (j < 99)
3202 msg_putchar(' ');
3203 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003204 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003205 ui_breakcheck();
3206 }
3207 if (!got_int)
3208 {
3209 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003210 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 msg_puts(" enddef");
3212 else
3213 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003214 }
3215 }
3216 else
Bram Moolenaara26b9702020-04-18 19:53:28 +02003217 emsg_funcname(N_("E123: Undefined function: %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003218 }
3219 goto ret_free;
3220 }
3221
3222 /*
3223 * ":function name(arg1, arg2)" Define function.
3224 */
3225 p = skipwhite(p);
3226 if (*p != '(')
3227 {
3228 if (!eap->skip)
3229 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003230 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003231 goto ret_free;
3232 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003233 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003234 if (vim_strchr(p, '(') != NULL)
3235 p = vim_strchr(p, '(');
3236 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003237
Bram Moolenaar4efd9942021-01-24 21:14:20 +01003238 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
3239 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003240 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01003241 goto ret_free;
3242 }
3243
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02003244 // In Vim9 script only global functions can be redefined.
3245 if (vim9script && eap->forceit && !is_global)
3246 {
3247 emsg(_(e_nobang));
3248 goto ret_free;
3249 }
3250
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003251 ga_init2(&newlines, (int)sizeof(char_u *), 3);
3252
Bram Moolenaar04b12692020-05-04 23:24:44 +02003253 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003254 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003255 // Check the name of the function. Unless it's a dictionary function
3256 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003257 if (name != NULL)
3258 arg = name;
3259 else
3260 arg = fudi.fd_newkey;
3261 if (arg != NULL && (fudi.fd_di == NULL
3262 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
3263 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
3264 {
3265 if (*arg == K_SPECIAL)
3266 j = 3;
3267 else
3268 j = 0;
3269 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
3270 : eval_isnamec(arg[j])))
3271 ++j;
3272 if (arg[j] != NUL)
3273 emsg_funcname((char *)e_invarg2, arg);
3274 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003275 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003276 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003277 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003278 }
3279
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003280 // This may get more lines and make the pointers into the first line
3281 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01003282 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003284 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01003285 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003286 eap, &line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003287 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01003288 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003291 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 // find the return type: :def Func(): type
3293 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003294 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02003296 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003298 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003299 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01003300 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003302 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003304 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003305 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02003306 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003307 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003308 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02003309 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003310 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311 else
3312 // find extra arguments "range", "dict", "abort" and "closure"
3313 for (;;)
3314 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01003315 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 p = skipwhite(p);
3317 if (STRNCMP(p, "range", 5) == 0)
3318 {
3319 flags |= FC_RANGE;
3320 p += 5;
3321 }
3322 else if (STRNCMP(p, "dict", 4) == 0)
3323 {
3324 flags |= FC_DICT;
3325 p += 4;
3326 }
3327 else if (STRNCMP(p, "abort", 5) == 0)
3328 {
3329 flags |= FC_ABORT;
3330 p += 5;
3331 }
3332 else if (STRNCMP(p, "closure", 7) == 0)
3333 {
3334 flags |= FC_CLOSURE;
3335 p += 7;
3336 if (current_funccal == NULL)
3337 {
3338 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
3339 name == NULL ? (char_u *)"" : name);
3340 goto erret;
3341 }
3342 }
3343 else
3344 break;
3345 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003346
Bram Moolenaare38eab22019-12-05 21:50:01 +01003347 // When there is a line break use what follows for the function body.
3348 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003349 if (*p == '\n')
3350 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003351 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02003352 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
3353 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01003354 && !(VIM_ISWHITE(*whitep) && *p == '#'
3355 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02003356 && !eap->skip
3357 && !did_emsg)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003358 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003359
3360 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
3362 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003363 */
3364 if (KeyTyped)
3365 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003366 // Check if the function already exists, don't let the user type the
3367 // whole function before telling him it doesn't work! For a script we
3368 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003369 if (!eap->skip && !eap->forceit)
3370 {
3371 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003372 emsg(_(e_funcdict));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003373 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003374 emsg_funcname(e_funcexts, name);
3375 }
3376
3377 if (!eap->skip && did_emsg)
3378 goto erret;
3379
Bram Moolenaare38eab22019-12-05 21:50:01 +01003380 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003381 cmdline_row = msg_row;
3382 }
3383
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003384 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003385 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003386
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02003387 // Detect having skipped over comment lines to find the return
3388 // type. Add NULL lines to keep the line count correct.
3389 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
3390 if (SOURCING_LNUM < sourcing_lnum_off)
3391 {
3392 sourcing_lnum_off -= SOURCING_LNUM;
3393 if (ga_grow(&newlines, sourcing_lnum_off) == FAIL)
3394 goto erret;
3395 while (sourcing_lnum_off-- > 0)
3396 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3397 }
3398
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003399 indent = 2;
3400 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01003402 getline_options = eap->cmdidx == CMD_def
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01003403 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003404 for (;;)
3405 {
3406 if (KeyTyped)
3407 {
3408 msg_scroll = TRUE;
3409 saved_wait_return = FALSE;
3410 }
3411 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003412
3413 if (line_arg != NULL)
3414 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003415 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003416 theline = line_arg;
3417 p = vim_strchr(theline, '\n');
3418 if (p == NULL)
3419 line_arg += STRLEN(line_arg);
3420 else
3421 {
3422 *p = NUL;
3423 line_arg = p + 1;
3424 }
3425 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003426 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02003427 {
3428 vim_free(line_to_free);
3429 if (eap->getline == NULL)
Bram Moolenaar66250c92020-08-20 15:02:42 +02003430 theline = getcmdline(':', 0L, indent, getline_options);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003431 else
Bram Moolenaar66250c92020-08-20 15:02:42 +02003432 theline = eap->getline(':', eap->cookie, indent,
3433 getline_options);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003434 line_to_free = theline;
3435 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003436 if (KeyTyped)
3437 lines_left = Rows - 1;
3438 if (theline == NULL)
3439 {
Bram Moolenaarb8ba9b92021-01-01 18:54:34 +01003440 // Use the start of the function for the line number.
3441 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003442 if (skip_until != NULL)
3443 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
3444 else if (eap->cmdidx == CMD_def)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003445 emsg(_(e_missing_enddef));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 else
3447 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003448 goto erret;
3449 }
3450
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003451 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003452 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003453 if (SOURCING_LNUM < sourcing_lnum_off)
3454 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003455 else
3456 sourcing_lnum_off = 0;
3457
3458 if (skip_until != NULL)
3459 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003461 // * ":append" and "."
3462 // * ":python <<EOF" and "EOF"
3463 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
3464 if (heredoc_trimmed == NULL
3465 || (is_heredoc && skipwhite(theline) == theline)
3466 || STRNCMP(theline, heredoc_trimmed,
3467 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003468 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003469 if (heredoc_trimmed == NULL)
3470 p = theline;
3471 else if (is_heredoc)
3472 p = skipwhite(theline) == theline
3473 ? theline : theline + STRLEN(heredoc_trimmed);
3474 else
3475 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02003476 if (STRCMP(p, skip_until) == 0)
3477 {
3478 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003479 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01003480 getline_options = eap->cmdidx == CMD_def
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01003481 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003482 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003483 }
3484 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003485 }
3486 else
3487 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003488 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01003489 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003490 ;
3491
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492 // Check for "endfunction" or "enddef".
Bram Moolenaar832ea892021-01-08 21:55:26 +01003493 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 if (checkforcmd(&p, nesting_def[nesting]
Bram Moolenaar832ea892021-01-08 21:55:26 +01003495 ? "enddef" : "endfunction", 4)
3496 && *p != ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003497 {
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003498 if (nesting-- == 0)
Bram Moolenaar53564f72017-06-24 14:48:11 +02003499 {
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003500 char_u *nextcmd = NULL;
3501
3502 if (*p == '|')
3503 nextcmd = p + 1;
3504 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
3505 nextcmd = line_arg;
3506 else if (*p != NUL && *p != '"' && p_verbose > 0)
3507 give_warning2(eap->cmdidx == CMD_def
3508 ? (char_u *)_("W1001: Text found after :enddef: %s")
3509 : (char_u *)_("W22: Text found after :endfunction: %s"),
3510 p, TRUE);
3511 if (nextcmd != NULL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02003512 {
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003513 // Another command follows. If the line came from "eap"
3514 // we can simply point into it, otherwise we need to
3515 // change "eap->cmdlinep".
3516 eap->nextcmd = nextcmd;
3517 if (line_to_free != NULL)
3518 {
3519 vim_free(*eap->cmdlinep);
3520 *eap->cmdlinep = line_to_free;
3521 line_to_free = NULL;
3522 }
Bram Moolenaar53564f72017-06-24 14:48:11 +02003523 }
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003524 break;
Bram Moolenaar53564f72017-06-24 14:48:11 +02003525 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003526 }
3527
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003528 // Check for mismatched "endfunc" or "enddef".
3529 // We don't check for "def" inside "func" thus we also can't check
3530 // for "enddef".
3531 // We continue to find the end of the function, although we might
3532 // not find it.
3533 else if (nesting_def[nesting])
3534 {
Bram Moolenaar832ea892021-01-08 21:55:26 +01003535 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003536 emsg(_(e_mismatched_endfunction));
3537 }
3538 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
3539 emsg(_(e_mismatched_enddef));
3540
Bram Moolenaare38eab22019-12-05 21:50:01 +01003541 // Increase indent inside "if", "while", "for" and "try", decrease
3542 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003544 indent -= 2;
3545 else if (STRNCMP(p, "if", 2) == 0
3546 || STRNCMP(p, "wh", 2) == 0
3547 || STRNCMP(p, "for", 3) == 0
3548 || STRNCMP(p, "try", 3) == 0)
3549 indent += 2;
3550
Bram Moolenaare38eab22019-12-05 21:50:01 +01003551 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01003552 // Only recognize "def" inside "def", not inside "function",
3553 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554 c = *p;
Bram Moolenaaradc8e442020-12-31 18:28:18 +01003555 if (is_function_cmd(&p)
Bram Moolenaar673660a2020-01-26 16:50:05 +01003556 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003557 {
3558 if (*p == '!')
3559 p = skipwhite(p + 1);
3560 p += eval_fname_script(p);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003561 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL,
3562 NULL, NULL));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003563 if (*skipwhite(p) == '(')
3564 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 if (nesting == MAX_FUNC_NESTING - 1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003566 emsg(_(e_function_nesting_too_deep));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567 else
3568 {
3569 ++nesting;
3570 nesting_def[nesting] = (c == 'd');
3571 indent += 2;
3572 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003573 }
3574 }
3575
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003576 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003577 p = skip_range(p, FALSE, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003578 if (eap->cmdidx != CMD_def
3579 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01003580 || (p[0] == 'c'
3581 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
3582 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
3583 && (STRNCMP(&p[3], "nge", 3) != 0
3584 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003585 || (p[0] == 'i'
3586 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003587 && (!ASCII_ISALPHA(p[2])
3588 || (p[2] == 's'
3589 && (!ASCII_ISALPHA(p[3])
3590 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003591 skip_until = vim_strsave((char_u *)".");
3592
Bram Moolenaare38eab22019-12-05 21:50:01 +01003593 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003594 arg = skipwhite(skiptowhite(p));
3595 if (arg[0] == '<' && arg[1] =='<'
3596 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003597 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
3598 || ((p[2] == '3' || p[2] == 'x')
3599 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003600 || (p[0] == 'p' && p[1] == 'e'
3601 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
3602 || (p[0] == 't' && p[1] == 'c'
3603 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
3604 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
3605 && !ASCII_ISALPHA(p[3]))
3606 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
3607 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
3608 || (p[0] == 'm' && p[1] == 'z'
3609 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
3610 ))
3611 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003612 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003613 p = skipwhite(arg + 2);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003614 if (STRNCMP(p, "trim", 4) == 0)
3615 {
3616 // Ignore leading white space.
3617 p = skipwhite(p + 4);
3618 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003619 skipwhite(theline) - theline);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003620 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003621 if (*p == NUL)
3622 skip_until = vim_strsave((char_u *)".");
3623 else
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003624 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003625 getline_options = GETLINE_NONE;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003626 is_heredoc = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003627 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003628
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003629 // Check for ":cmd v =<< [trim] EOF"
3630 // and ":cmd [a, b] =<< [trim] EOF"
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003631 // and "lines =<< [trim] EOF" for Vim9
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003632 // Where "cmd" can be "let", "var", "final" or "const".
Bram Moolenaar8471e572019-05-19 21:37:18 +02003633 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003634 if (*arg == '[')
3635 arg = vim_strchr(arg, ']');
3636 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003637 {
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003638 int found = (eap->cmdidx == CMD_def && arg[0] == '='
3639 && arg[1] == '<' && arg[2] =='<');
3640
3641 if (!found)
3642 // skip over the argument after "cmd"
3643 arg = skipwhite(skiptowhite(arg));
3644 if (found || (arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003645 && (checkforcmd(&p, "let", 2)
3646 || checkforcmd(&p, "var", 3)
3647 || checkforcmd(&p, "final", 5)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003648 || checkforcmd(&p, "const", 5))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02003649 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003650 p = skipwhite(arg + 3);
3651 if (STRNCMP(p, "trim", 4) == 0)
3652 {
3653 // Ignore leading white space.
3654 p = skipwhite(p + 4);
3655 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003656 skipwhite(theline) - theline);
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003657 }
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003658 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003659 getline_options = GETLINE_NONE;
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003660 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003661 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003662 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003663 }
3664
Bram Moolenaare38eab22019-12-05 21:50:01 +01003665 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003666 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003667 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003668
Bram Moolenaare38eab22019-12-05 21:50:01 +01003669 // Copy the line to newly allocated memory. get_one_sourceline()
3670 // allocates 250 bytes per line, this saves 80% on average. The cost
3671 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003672 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003673 if (p == NULL)
3674 goto erret;
3675 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003676
Bram Moolenaare38eab22019-12-05 21:50:01 +01003677 // Add NULL lines for continuation lines, so that the line count is
3678 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003679 while (sourcing_lnum_off-- > 0)
3680 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3681
Bram Moolenaare38eab22019-12-05 21:50:01 +01003682 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003683 if (line_arg != NULL && *line_arg == NUL)
3684 line_arg = NULL;
3685 }
3686
Bram Moolenaare38eab22019-12-05 21:50:01 +01003687 // Don't define the function when skipping commands or when an error was
3688 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003689 if (eap->skip || did_emsg)
3690 goto erret;
3691
3692 /*
3693 * If there are no errors, add the function
3694 */
3695 if (fudi.fd_dict == NULL)
3696 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 hashtab_T *ht;
3698
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003699 v = find_var(name, &ht, TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003700 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
3701 {
3702 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
3703 name);
3704 goto erret;
3705 }
3706
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003707 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaareef21022020-08-01 22:16:43 +02003708 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003709 {
Bram Moolenaareef21022020-08-01 22:16:43 +02003710 char_u *uname = untrans_function_name(name);
3711
3712 import = find_imported(uname == NULL ? name : uname, 0, NULL);
3713 }
3714
3715 if (fp != NULL || import != NULL)
3716 {
3717 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003719 // Function can be replaced with "function!" and when sourcing the
3720 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02003721 // A name that is used by an import can not be overruled.
3722 if (import != NULL
3723 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003724 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02003725 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003726 {
Bram Moolenaareef21022020-08-01 22:16:43 +02003727 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003728 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02003729 else
3730 emsg_funcname(e_funcexts, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003731 goto erret;
3732 }
3733 if (fp->uf_calls > 0)
3734 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003735 emsg_funcname(
3736 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003737 name);
3738 goto erret;
3739 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003740 if (fp->uf_refcount > 1)
3741 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003742 // This function is referenced somewhere, don't redefine it but
3743 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003744 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003745 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003746 fp = NULL;
3747 overwrite = TRUE;
3748 }
3749 else
3750 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003751 char_u *exp_name = fp->uf_name_exp;
3752
3753 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01003754 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003755 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003756 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003757 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003759#ifdef FEAT_PROFILE
3760 fp->uf_profiling = FALSE;
3761 fp->uf_prof_initialized = FALSE;
3762#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01003763 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003764 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003765 }
3766 }
3767 else
3768 {
3769 char numbuf[20];
3770
3771 fp = NULL;
3772 if (fudi.fd_newkey == NULL && !eap->forceit)
3773 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003774 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003775 goto erret;
3776 }
3777 if (fudi.fd_di == NULL)
3778 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003779 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02003780 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003781 goto erret;
3782 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003783 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02003784 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003785 goto erret;
3786
Bram Moolenaare38eab22019-12-05 21:50:01 +01003787 // Give the function a sequential number. Can only be used with a
3788 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003789 vim_free(name);
3790 sprintf(numbuf, "%d", ++func_nr);
3791 name = vim_strsave((char_u *)numbuf);
3792 if (name == NULL)
3793 goto erret;
3794 }
3795
3796 if (fp == NULL)
3797 {
3798 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3799 {
3800 int slen, plen;
3801 char_u *scriptname;
3802
Bram Moolenaare38eab22019-12-05 21:50:01 +01003803 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003804 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003805 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003806 {
3807 scriptname = autoload_name(name);
3808 if (scriptname != NULL)
3809 {
3810 p = vim_strchr(scriptname, '/');
3811 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003812 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003813 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003814 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003815 j = OK;
3816 vim_free(scriptname);
3817 }
3818 }
3819 if (j == FAIL)
3820 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003821 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003822 goto erret;
3823 }
3824 }
3825
Bram Moolenaar47ed5532019-08-08 20:49:14 +02003826 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003827 if (fp == NULL)
3828 goto erret;
3829
3830 if (fudi.fd_dict != NULL)
3831 {
3832 if (fudi.fd_di == NULL)
3833 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003834 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003835 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
3836 if (fudi.fd_di == NULL)
3837 {
3838 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003839 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003840 goto erret;
3841 }
3842 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
3843 {
3844 vim_free(fudi.fd_di);
3845 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003846 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003847 goto erret;
3848 }
3849 }
3850 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003851 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003852 clear_tv(&fudi.fd_di->di_tv);
3853 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003854 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003855
Bram Moolenaare38eab22019-12-05 21:50:01 +01003856 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003857 flags |= FC_DICT;
3858 }
3859
Bram Moolenaare38eab22019-12-05 21:50:01 +01003860 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003861 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003862 if (overwrite)
3863 {
3864 hi = hash_find(&func_hashtab, name);
3865 hi->hi_key = UF2HIKEY(fp);
3866 }
3867 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003868 {
3869 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003870 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003871 goto erret;
3872 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003873 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003874 }
3875 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003876 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003877 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003878 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879
3880 if (eap->cmdidx == CMD_def)
3881 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003882 int lnum_save = SOURCING_LNUM;
3883 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003884
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003885 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003886
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003887 // error messages are for the first function line
3888 SOURCING_LNUM = sourcing_lnum_top;
3889
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003890 if (cstack != NULL && cstack->cs_idx >= 0)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003891 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003892 int count = cstack->cs_idx + 1;
3893 int i;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003894
3895 // The block context may be needed for script variables declared in
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003896 // a block that is visible now but not when the function is called
3897 // later.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003898 fp->uf_block_ids = ALLOC_MULT(int, count);
3899 if (fp->uf_block_ids != NULL)
3900 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003901 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003902 sizeof(int) * count);
3903 fp->uf_block_depth = count;
3904 }
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003905
3906 // Set flag in each block to indicate a function was defined. This
3907 // is used to keep the variable when leaving the block, see
3908 // hide_script_var().
3909 for (i = 0; i <= cstack->cs_idx; ++i)
3910 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003911 }
3912
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003913 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003915 SOURCING_LNUM = lnum_save;
3916 goto errret_2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003918 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919
3920 // parse the return type, if any
3921 if (ret_type == NULL)
3922 fp->uf_ret_type = &t_void;
3923 else
3924 {
3925 p = ret_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01003926 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaar648ea762021-01-15 19:04:32 +01003927 if (fp->uf_ret_type == NULL)
3928 {
3929 fp->uf_ret_type = &t_void;
3930 SOURCING_LNUM = lnum_save;
3931 goto erret;
3932 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02003934 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003935 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003936 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003937 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003939 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003940 if ((flags & FC_CLOSURE) != 0)
3941 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003942 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003943 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003944 }
3945 else
3946 fp->uf_scoped = NULL;
3947
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003948#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003949 if (prof_def_func())
3950 func_do_profile(fp);
3951#endif
3952 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003953 if (sandbox)
3954 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003955 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003956 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003957 fp->uf_flags = flags;
3958 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003959 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003960 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02003961 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003962 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 if (is_export)
3964 {
3965 fp->uf_flags |= FC_EXPORT;
3966 // let ex_export() know the export worked.
3967 is_export = FALSE;
3968 }
3969
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003970 if (eap->cmdidx == CMD_def)
3971 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02003972 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
3973 // :func does not use Vim9 script syntax, even in a Vim9 script file
3974 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003975
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003976 goto ret_free;
3977
3978erret:
3979 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003980 ga_clear_strings(&default_args);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01003981 if (fp != NULL)
3982 {
3983 ga_init(&fp->uf_args);
3984 ga_init(&fp->uf_def_args);
3985 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003986errret_2:
3987 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01003988 if (fp != NULL)
3989 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003990ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003991 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003992 vim_free(skip_until);
Bram Moolenaarfffdf472020-12-13 21:16:55 +01003993 vim_free(heredoc_trimmed);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003994 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003995 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003996 if (name != name_arg)
3997 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003998 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003999 did_emsg |= saved_did_emsg;
4000 need_wait_return |= saved_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004001
4002 return fp;
4003}
4004
4005/*
4006 * ":function"
4007 */
4008 void
4009ex_function(exarg_T *eap)
4010{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004011 (void)define_function(eap, NULL);
Bram Moolenaar822ba242020-05-24 23:00:18 +02004012}
4013
4014/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02004015 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarb2049902021-01-24 12:53:53 +01004016 * be compiled. Except dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02004017 */
4018 void
4019ex_defcompile(exarg_T *eap UNUSED)
4020{
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004021 long todo = (long)func_hashtab.ht_used;
4022 int changed = func_hashtab.ht_changed;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004023 hashitem_T *hi;
4024 ufunc_T *ufunc;
4025
4026 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
4027 {
4028 if (!HASHITEM_EMPTY(hi))
4029 {
4030 --todo;
4031 ufunc = HI2UF(hi);
4032 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
Bram Moolenaar96f8f492020-09-09 17:08:51 +02004033 && ufunc->uf_def_status == UF_TO_BE_COMPILED
4034 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004035 {
Bram Moolenaardc167462021-02-20 20:26:16 +01004036 (void)compile_def_function(ufunc, FALSE, FALSE, NULL);
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004037
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004038 if (func_hashtab.ht_changed != changed)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004039 {
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004040 // a function has been added or removed, need to start over
4041 todo = (long)func_hashtab.ht_used;
4042 changed = func_hashtab.ht_changed;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004043 hi = func_hashtab.ht_array;
Bram Moolenaar285b1892020-05-26 11:37:26 +02004044 --hi;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004045 }
4046 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02004047 }
4048 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004049}
4050
4051/*
4052 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
4053 * Return 2 if "p" starts with "s:".
4054 * Return 0 otherwise.
4055 */
4056 int
4057eval_fname_script(char_u *p)
4058{
Bram Moolenaare38eab22019-12-05 21:50:01 +01004059 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
4060 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004061 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
4062 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
4063 return 5;
4064 if (p[0] == 's' && p[1] == ':')
4065 return 2;
4066 return 0;
4067}
4068
4069 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004070translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004071{
4072 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004073 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004074 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075}
4076
4077/*
4078 * Return TRUE when "ufunc" has old-style "..." varargs
4079 * or named varargs "...name: type".
4080 */
4081 int
4082has_varargs(ufunc_T *ufunc)
4083{
4084 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004085}
4086
4087/*
4088 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004089 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004090 */
4091 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004092function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004093{
4094 char_u *nm = name;
4095 char_u *p;
4096 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004097 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004098 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004099
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004100 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
4101 if (no_deref)
4102 flag |= TFN_NO_DEREF;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004103 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004104 nm = skipwhite(nm);
4105
Bram Moolenaare38eab22019-12-05 21:50:01 +01004106 // Only accept "funcname", "funcname ", "funcname (..." and
4107 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004108 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004109 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004110 vim_free(p);
4111 return n;
4112}
4113
Bram Moolenaar113e1072019-01-20 15:30:40 +01004114#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004115 char_u *
4116get_expanded_name(char_u *name, int check)
4117{
4118 char_u *nm = name;
4119 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004120 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004121
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004122 p = trans_function_name(&nm, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004123 TFN_INT|TFN_QUIET, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004124
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004125 if (p != NULL && *nm == NUL
4126 && (!check || translated_function_exists(p, is_global)))
4127 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004128
4129 vim_free(p);
4130 return NULL;
4131}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004132#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004133
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004134/*
4135 * Function given to ExpandGeneric() to obtain the list of user defined
4136 * function names.
4137 */
4138 char_u *
4139get_user_func_name(expand_T *xp, int idx)
4140{
4141 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004142 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004143 static hashitem_T *hi;
4144 ufunc_T *fp;
4145
4146 if (idx == 0)
4147 {
4148 done = 0;
4149 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004150 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004151 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004152 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004153 {
4154 if (done++ > 0)
4155 ++hi;
4156 while (HASHITEM_EMPTY(hi))
4157 ++hi;
4158 fp = HI2UF(hi);
4159
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 // don't show dead, dict and lambda functions
4161 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02004162 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004164
4165 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004166 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004167
4168 cat_func_name(IObuff, fp);
4169 if (xp->xp_context != EXPAND_USER_FUNC)
4170 {
4171 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004172 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004173 STRCAT(IObuff, ")");
4174 }
4175 return IObuff;
4176 }
4177 return NULL;
4178}
4179
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004180/*
4181 * ":delfunction {name}"
4182 */
4183 void
4184ex_delfunction(exarg_T *eap)
4185{
4186 ufunc_T *fp = NULL;
4187 char_u *p;
4188 char_u *name;
4189 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004190 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004191
4192 p = eap->arg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004193 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi,
4194 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004195 vim_free(fudi.fd_newkey);
4196 if (name == NULL)
4197 {
4198 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004199 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004200 return;
4201 }
4202 if (!ends_excmd(*skipwhite(p)))
4203 {
4204 vim_free(name);
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004205 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004206 return;
4207 }
4208 eap->nextcmd = check_nextcmd(p);
4209 if (eap->nextcmd != NULL)
4210 *p = NUL;
4211
4212 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004213 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004214 vim_free(name);
4215
4216 if (!eap->skip)
4217 {
4218 if (fp == NULL)
4219 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02004220 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004221 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004222 return;
4223 }
4224 if (fp->uf_calls > 0)
4225 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004226 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004227 return;
4228 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004229 if (fp->uf_flags & FC_VIM9)
4230 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004231 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004232 return;
4233 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004234
4235 if (fudi.fd_dict != NULL)
4236 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004237 // Delete the dict item that refers to the function, it will
4238 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004239 dictitem_remove(fudi.fd_dict, fudi.fd_di);
4240 }
4241 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004242 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004243 // A normal function (not a numbered function or lambda) has a
4244 // refcount of 1 for the entry in the hashtable. When deleting
4245 // it and the refcount is more than one, it should be kept.
4246 // A numbered function and lambda should be kept if the refcount is
4247 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004248 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004249 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004250 // Function is still referenced somewhere. Don't free it but
4251 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004252 if (func_remove(fp))
4253 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004254 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004255 }
4256 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004257 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004258 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004259 }
4260}
4261
4262/*
4263 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004264 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004265 */
4266 void
4267func_unref(char_u *name)
4268{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004269 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004270
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004271 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004272 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004273 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004274 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004275 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004276#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004277 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004278#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01004279 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004280 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004281 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004282}
4283
4284/*
4285 * Unreference a Function: decrement the reference count and free it when it
4286 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004287 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004288 */
4289 void
4290func_ptr_unref(ufunc_T *fp)
4291{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004292 if (fp != NULL && (--fp->uf_refcount <= 0
4293 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
4294 && fp->uf_partial->pt_refcount <= 1
4295 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02004296 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004297 // Only delete it when it's not being used. Otherwise it's done
4298 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02004299 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004300 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004301 }
4302}
4303
4304/*
4305 * Count a reference to a Function.
4306 */
4307 void
4308func_ref(char_u *name)
4309{
4310 ufunc_T *fp;
4311
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004312 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004313 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004314 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004315 if (fp != NULL)
4316 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004317 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01004318 // Only give an error for a numbered function.
4319 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01004320 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004321}
4322
4323/*
4324 * Count a reference to a Function.
4325 */
4326 void
4327func_ptr_ref(ufunc_T *fp)
4328{
4329 if (fp != NULL)
4330 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004331}
4332
4333/*
4334 * Return TRUE if items in "fc" do not have "copyID". That means they are not
4335 * referenced from anywhere that is in use.
4336 */
4337 static int
4338can_free_funccal(funccall_T *fc, int copyID)
4339{
4340 return (fc->l_varlist.lv_copyID != copyID
4341 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004342 && fc->l_avars.dv_copyID != copyID
4343 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004344}
4345
4346/*
4347 * ":return [expr]"
4348 */
4349 void
4350ex_return(exarg_T *eap)
4351{
4352 char_u *arg = eap->arg;
4353 typval_T rettv;
4354 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004355 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004356
4357 if (current_funccal == NULL)
4358 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004359 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004360 return;
4361 }
4362
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004363 CLEAR_FIELD(evalarg);
4364 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
4365
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004366 if (eap->skip)
4367 ++emsg_skip;
4368
4369 eap->nextcmd = NULL;
4370 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004371 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004372 {
4373 if (!eap->skip)
4374 returning = do_return(eap, FALSE, TRUE, &rettv);
4375 else
4376 clear_tv(&rettv);
4377 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004378 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004379 else if (!eap->skip)
4380 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004381 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01004382 update_force_abort();
4383
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004384 /*
4385 * Return unless the expression evaluation has been cancelled due to an
4386 * aborting error, an interrupt, or an exception.
4387 */
4388 if (!aborting())
4389 returning = do_return(eap, FALSE, TRUE, NULL);
4390 }
4391
Bram Moolenaare38eab22019-12-05 21:50:01 +01004392 // When skipping or the return gets pending, advance to the next command
4393 // in this line (!returning). Otherwise, ignore the rest of the line.
4394 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004395 if (returning)
4396 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004397 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004398 eap->nextcmd = check_nextcmd(arg);
4399
4400 if (eap->skip)
4401 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02004402 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004403}
4404
4405/*
4406 * ":1,25call func(arg1, arg2)" function call.
4407 */
4408 void
4409ex_call(exarg_T *eap)
4410{
4411 char_u *arg = eap->arg;
4412 char_u *startarg;
4413 char_u *name;
4414 char_u *tofree;
4415 int len;
4416 typval_T rettv;
4417 linenr_T lnum;
4418 int doesrange;
4419 int failed = FALSE;
4420 funcdict_T fudi;
4421 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004422 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004423 type_T *type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004424
Bram Moolenaare6b53242020-07-01 17:28:33 +02004425 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004426 if (eap->skip)
4427 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004428 // trans_function_name() doesn't work well when skipping, use eval0()
4429 // instead to skip to any following command, e.g. for:
4430 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004431 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004432 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004433 clear_tv(&rettv);
4434 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004435 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004436 return;
4437 }
4438
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004439 tofree = trans_function_name(&arg, NULL, eap->skip, TFN_INT,
4440 &fudi, &partial, in_vim9script() ? &type : NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004441 if (fudi.fd_newkey != NULL)
4442 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004443 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004444 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004445 vim_free(fudi.fd_newkey);
4446 }
4447 if (tofree == NULL)
4448 return;
4449
Bram Moolenaare38eab22019-12-05 21:50:01 +01004450 // Increase refcount on dictionary, it could get deleted when evaluating
4451 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004452 if (fudi.fd_dict != NULL)
4453 ++fudi.fd_dict->dv_refcount;
4454
Bram Moolenaare38eab22019-12-05 21:50:01 +01004455 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
4456 // contents. For VAR_PARTIAL get its partial, unless we already have one
4457 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004458 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004459 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
4460 in_vim9script() && type == NULL ? &type : NULL, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004461
Bram Moolenaare38eab22019-12-05 21:50:01 +01004462 // Skip white space to allow ":call func ()". Not good, but required for
4463 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004464 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004465 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004466
4467 if (*startarg != '(')
4468 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004470 goto end;
4471 }
4472
4473 /*
4474 * When skipping, evaluate the function once, to find the end of the
4475 * arguments.
4476 * When the function takes a range, this is discovered after the first
4477 * call, and the loop is broken.
4478 */
4479 if (eap->skip)
4480 {
4481 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004482 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004483 }
4484 else
4485 lnum = eap->line1;
4486 for ( ; lnum <= eap->line2; ++lnum)
4487 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004488 funcexe_T funcexe;
4489
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004490 if (!eap->skip && eap->addr_count > 0)
4491 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004492 if (lnum > curbuf->b_ml.ml_line_count)
4493 {
4494 // If the function deleted lines or switched to another buffer
4495 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004496 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004497 break;
4498 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004499 curwin->w_cursor.lnum = lnum;
4500 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004501 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004502 }
4503 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004504
Bram Moolenaara80faa82020-04-12 19:37:17 +02004505 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004506 funcexe.firstline = eap->line1;
4507 funcexe.lastline = eap->line2;
4508 funcexe.doesrange = &doesrange;
4509 funcexe.evaluate = !eap->skip;
4510 funcexe.partial = partial;
4511 funcexe.selfdict = fudi.fd_dict;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004512 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004513 if (get_func_tv(name, -1, &rettv, &arg, &evalarg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004514 {
4515 failed = TRUE;
4516 break;
4517 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01004518 if (has_watchexpr())
4519 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004520
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004521 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004522 if (handle_subscript(&arg, &rettv,
4523 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004524 {
4525 failed = TRUE;
4526 break;
4527 }
4528
4529 clear_tv(&rettv);
4530 if (doesrange || eap->skip)
4531 break;
4532
Bram Moolenaare38eab22019-12-05 21:50:01 +01004533 // Stop when immediately aborting on error, or when an interrupt
4534 // occurred or an exception was thrown but not caught.
4535 // get_func_tv() returned OK, so that the check for trailing
4536 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004537 if (aborting())
4538 break;
4539 }
4540 if (eap->skip)
4541 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004542 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004543
Bram Moolenaare51bb172020-02-16 19:42:23 +01004544 // When inside :try we need to check for following "| catch".
4545 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004546 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004547 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02004548 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004549 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004550 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004551 if (!failed)
4552 {
4553 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004554 semsg(_(e_trailing_arg), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004555 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004556 }
4557 else
4558 eap->nextcmd = check_nextcmd(arg);
4559 }
4560
4561end:
4562 dict_unref(fudi.fd_dict);
4563 vim_free(tofree);
4564}
4565
4566/*
4567 * Return from a function. Possibly makes the return pending. Also called
4568 * for a pending return at the ":endtry" or after returning from an extra
4569 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
4570 * when called due to a ":return" command. "rettv" may point to a typval_T
4571 * with the return rettv. Returns TRUE when the return can be carried out,
4572 * FALSE when the return gets pending.
4573 */
4574 int
4575do_return(
4576 exarg_T *eap,
4577 int reanimate,
4578 int is_cmd,
4579 void *rettv)
4580{
4581 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01004582 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004583
4584 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004585 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004586 current_funccal->returned = FALSE;
4587
4588 /*
4589 * Cleanup (and inactivate) conditionals, but stop when a try conditional
4590 * not in its finally clause (which then is to be executed next) is found.
4591 * In this case, make the ":return" pending for execution at the ":endtry".
4592 * Otherwise, return normally.
4593 */
4594 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
4595 if (idx >= 0)
4596 {
4597 cstack->cs_pending[idx] = CSTP_RETURN;
4598
4599 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004600 // A pending return again gets pending. "rettv" points to an
4601 // allocated variable with the rettv of the original ":return"'s
4602 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004603 cstack->cs_rettv[idx] = rettv;
4604 else
4605 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004606 // When undoing a return in order to make it pending, get the stored
4607 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004608 if (reanimate)
4609 rettv = current_funccal->rettv;
4610
4611 if (rettv != NULL)
4612 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004613 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004614 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
4615 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
4616 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004617 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004618 }
4619 else
4620 cstack->cs_rettv[idx] = NULL;
4621
4622 if (reanimate)
4623 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004624 // The pending return value could be overwritten by a ":return"
4625 // without argument in a finally clause; reset the default
4626 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004627 current_funccal->rettv->v_type = VAR_NUMBER;
4628 current_funccal->rettv->vval.v_number = 0;
4629 }
4630 }
4631 report_make_pending(CSTP_RETURN, rettv);
4632 }
4633 else
4634 {
4635 current_funccal->returned = TRUE;
4636
Bram Moolenaare38eab22019-12-05 21:50:01 +01004637 // If the return is carried out now, store the return value. For
4638 // a return immediately after reanimation, the value is already
4639 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004640 if (!reanimate && rettv != NULL)
4641 {
4642 clear_tv(current_funccal->rettv);
4643 *current_funccal->rettv = *(typval_T *)rettv;
4644 if (!is_cmd)
4645 vim_free(rettv);
4646 }
4647 }
4648
4649 return idx < 0;
4650}
4651
4652/*
4653 * Free the variable with a pending return value.
4654 */
4655 void
4656discard_pending_return(void *rettv)
4657{
4658 free_tv((typval_T *)rettv);
4659}
4660
4661/*
4662 * Generate a return command for producing the value of "rettv". The result
4663 * is an allocated string. Used by report_pending() for verbose messages.
4664 */
4665 char_u *
4666get_return_cmd(void *rettv)
4667{
4668 char_u *s = NULL;
4669 char_u *tofree = NULL;
4670 char_u numbuf[NUMBUFLEN];
4671
4672 if (rettv != NULL)
4673 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
4674 if (s == NULL)
4675 s = (char_u *)"";
4676
4677 STRCPY(IObuff, ":return ");
4678 STRNCPY(IObuff + 8, s, IOSIZE - 8);
4679 if (STRLEN(s) + 8 >= IOSIZE)
4680 STRCPY(IObuff + IOSIZE - 4, "...");
4681 vim_free(tofree);
4682 return vim_strsave(IObuff);
4683}
4684
4685/*
4686 * Get next function line.
4687 * Called by do_cmdline() to get the next line.
4688 * Returns allocated string, or NULL for end of function.
4689 */
4690 char_u *
4691get_func_line(
4692 int c UNUSED,
4693 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02004694 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004695 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004696{
4697 funccall_T *fcp = (funccall_T *)cookie;
4698 ufunc_T *fp = fcp->func;
4699 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004700 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004701
Bram Moolenaare38eab22019-12-05 21:50:01 +01004702 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004703 if (fcp->dbg_tick != debug_tick)
4704 {
4705 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004706 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004707 fcp->dbg_tick = debug_tick;
4708 }
4709#ifdef FEAT_PROFILE
4710 if (do_profiling == PROF_YES)
4711 func_line_end(cookie);
4712#endif
4713
4714 gap = &fp->uf_lines;
4715 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4716 || fcp->returned)
4717 retval = NULL;
4718 else
4719 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004720 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004721 while (fcp->linenr < gap->ga_len
4722 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
4723 ++fcp->linenr;
4724 if (fcp->linenr >= gap->ga_len)
4725 retval = NULL;
4726 else
4727 {
4728 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004729 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004730#ifdef FEAT_PROFILE
4731 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01004732 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004733#endif
4734 }
4735 }
4736
Bram Moolenaare38eab22019-12-05 21:50:01 +01004737 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004738 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004739 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004740 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004741 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004742 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004743 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004744 fcp->dbg_tick = debug_tick;
4745 }
4746
4747 return retval;
4748}
4749
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004750/*
4751 * Return TRUE if the currently active function should be ended, because a
4752 * return was encountered or an error occurred. Used inside a ":while".
4753 */
4754 int
4755func_has_ended(void *cookie)
4756{
4757 funccall_T *fcp = (funccall_T *)cookie;
4758
Bram Moolenaare38eab22019-12-05 21:50:01 +01004759 // Ignore the "abort" flag if the abortion behavior has been changed due to
4760 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004761 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4762 || fcp->returned);
4763}
4764
4765/*
4766 * return TRUE if cookie indicates a function which "abort"s on errors.
4767 */
4768 int
4769func_has_abort(
4770 void *cookie)
4771{
4772 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
4773}
4774
4775
4776/*
4777 * Turn "dict.Func" into a partial for "Func" bound to "dict".
4778 * Don't do this when "Func" is already a partial that was bound
4779 * explicitly (pt_auto is FALSE).
4780 * Changes "rettv" in-place.
4781 * Returns the updated "selfdict_in".
4782 */
4783 dict_T *
4784make_partial(dict_T *selfdict_in, typval_T *rettv)
4785{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004786 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004787 char_u *tofree = NULL;
4788 ufunc_T *fp;
4789 char_u fname_buf[FLEN_FIXED + 1];
4790 int error;
4791 dict_T *selfdict = selfdict_in;
4792
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004793 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
4794 fp = rettv->vval.v_partial->pt_func;
4795 else
4796 {
4797 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
4798 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004799 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004800 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004801 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004802 vim_free(tofree);
4803 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004804
4805 if (fp != NULL && (fp->uf_flags & FC_DICT))
4806 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004807 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004808
4809 if (pt != NULL)
4810 {
4811 pt->pt_refcount = 1;
4812 pt->pt_dict = selfdict;
4813 pt->pt_auto = TRUE;
4814 selfdict = NULL;
4815 if (rettv->v_type == VAR_FUNC)
4816 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004817 // Just a function: Take over the function name and use
4818 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004819 pt->pt_name = rettv->vval.v_string;
4820 }
4821 else
4822 {
4823 partial_T *ret_pt = rettv->vval.v_partial;
4824 int i;
4825
Bram Moolenaare38eab22019-12-05 21:50:01 +01004826 // Partial: copy the function name, use selfdict and copy
4827 // args. Can't take over name or args, the partial might
4828 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004829 if (ret_pt->pt_name != NULL)
4830 {
4831 pt->pt_name = vim_strsave(ret_pt->pt_name);
4832 func_ref(pt->pt_name);
4833 }
4834 else
4835 {
4836 pt->pt_func = ret_pt->pt_func;
4837 func_ptr_ref(pt->pt_func);
4838 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004839 if (ret_pt->pt_argc > 0)
4840 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004841 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004842 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004843 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004844 pt->pt_argc = 0;
4845 else
4846 {
4847 pt->pt_argc = ret_pt->pt_argc;
4848 for (i = 0; i < pt->pt_argc; i++)
4849 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
4850 }
4851 }
4852 partial_unref(ret_pt);
4853 }
4854 rettv->v_type = VAR_PARTIAL;
4855 rettv->vval.v_partial = pt;
4856 }
4857 }
4858 return selfdict;
4859}
4860
4861/*
4862 * Return the name of the executed function.
4863 */
4864 char_u *
4865func_name(void *cookie)
4866{
4867 return ((funccall_T *)cookie)->func->uf_name;
4868}
4869
4870/*
4871 * Return the address holding the next breakpoint line for a funccall cookie.
4872 */
4873 linenr_T *
4874func_breakpoint(void *cookie)
4875{
4876 return &((funccall_T *)cookie)->breakpoint;
4877}
4878
4879/*
4880 * Return the address holding the debug tick for a funccall cookie.
4881 */
4882 int *
4883func_dbg_tick(void *cookie)
4884{
4885 return &((funccall_T *)cookie)->dbg_tick;
4886}
4887
4888/*
4889 * Return the nesting level for a funccall cookie.
4890 */
4891 int
4892func_level(void *cookie)
4893{
4894 return ((funccall_T *)cookie)->level;
4895}
4896
4897/*
4898 * Return TRUE when a function was ended by a ":return" command.
4899 */
4900 int
4901current_func_returned(void)
4902{
4903 return current_funccal->returned;
4904}
4905
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004906 int
4907free_unref_funccal(int copyID, int testing)
4908{
4909 int did_free = FALSE;
4910 int did_free_funccal = FALSE;
4911 funccall_T *fc, **pfc;
4912
4913 for (pfc = &previous_funccal; *pfc != NULL; )
4914 {
4915 if (can_free_funccal(*pfc, copyID))
4916 {
4917 fc = *pfc;
4918 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01004919 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004920 did_free = TRUE;
4921 did_free_funccal = TRUE;
4922 }
4923 else
4924 pfc = &(*pfc)->caller;
4925 }
4926 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004927 // When a funccal was freed some more items might be garbage
4928 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004929 (void)garbage_collect(testing);
4930
4931 return did_free;
4932}
4933
4934/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004935 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004936 */
4937 static funccall_T *
4938get_funccal(void)
4939{
4940 int i;
4941 funccall_T *funccal;
4942 funccall_T *temp_funccal;
4943
4944 funccal = current_funccal;
4945 if (debug_backtrace_level > 0)
4946 {
4947 for (i = 0; i < debug_backtrace_level; i++)
4948 {
4949 temp_funccal = funccal->caller;
4950 if (temp_funccal)
4951 funccal = temp_funccal;
4952 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004953 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004954 debug_backtrace_level = i;
4955 }
4956 }
4957 return funccal;
4958}
4959
4960/*
4961 * Return the hashtable used for local variables in the current funccal.
4962 * Return NULL if there is no current funccal.
4963 */
4964 hashtab_T *
4965get_funccal_local_ht()
4966{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004967 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004968 return NULL;
4969 return &get_funccal()->l_vars.dv_hashtab;
4970}
4971
4972/*
4973 * Return the l: scope variable.
4974 * Return NULL if there is no current funccal.
4975 */
4976 dictitem_T *
4977get_funccal_local_var()
4978{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004979 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004980 return NULL;
4981 return &get_funccal()->l_vars_var;
4982}
4983
4984/*
4985 * Return the hashtable used for argument in the current funccal.
4986 * Return NULL if there is no current funccal.
4987 */
4988 hashtab_T *
4989get_funccal_args_ht()
4990{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004991 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004992 return NULL;
4993 return &get_funccal()->l_avars.dv_hashtab;
4994}
4995
4996/*
4997 * Return the a: scope variable.
4998 * Return NULL if there is no current funccal.
4999 */
5000 dictitem_T *
5001get_funccal_args_var()
5002{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005003 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005004 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01005005 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005006}
5007
5008/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005009 * List function variables, if there is a function.
5010 */
5011 void
5012list_func_vars(int *first)
5013{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005014 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005015 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01005016 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005017}
5018
5019/*
5020 * If "ht" is the hashtable for local variables in the current funccal, return
5021 * the dict that contains it.
5022 * Otherwise return NULL.
5023 */
5024 dict_T *
5025get_current_funccal_dict(hashtab_T *ht)
5026{
5027 if (current_funccal != NULL
5028 && ht == &current_funccal->l_vars.dv_hashtab)
5029 return &current_funccal->l_vars;
5030 return NULL;
5031}
5032
5033/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005034 * Search hashitem in parent scope.
5035 */
5036 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005037find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005038{
5039 funccall_T *old_current_funccal = current_funccal;
5040 hashtab_T *ht;
5041 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005042 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005043
5044 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
5045 return NULL;
5046
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02005047 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005048 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02005049 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005050 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005051 ht = find_var_ht(name, &varname);
5052 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02005053 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005054 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02005055 if (!HASHITEM_EMPTY(hi))
5056 {
5057 *pht = ht;
5058 break;
5059 }
5060 }
5061 if (current_funccal == current_funccal->func->uf_scoped)
5062 break;
5063 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005064 }
5065 current_funccal = old_current_funccal;
5066
5067 return hi;
5068}
5069
5070/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005071 * Search variable in parent scope.
5072 */
5073 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005074find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005075{
5076 dictitem_T *v = NULL;
5077 funccall_T *old_current_funccal = current_funccal;
5078 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005079 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005080
5081 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
5082 return NULL;
5083
Bram Moolenaare38eab22019-12-05 21:50:01 +01005084 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005085 current_funccal = current_funccal->func->uf_scoped;
5086 while (current_funccal)
5087 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005088 ht = find_var_ht(name, &varname);
5089 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005090 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005091 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005092 if (v != NULL)
5093 break;
5094 }
5095 if (current_funccal == current_funccal->func->uf_scoped)
5096 break;
5097 current_funccal = current_funccal->func->uf_scoped;
5098 }
5099 current_funccal = old_current_funccal;
5100
5101 return v;
5102}
5103
5104/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005105 * Set "copyID + 1" in previous_funccal and callers.
5106 */
5107 int
5108set_ref_in_previous_funccal(int copyID)
5109{
5110 int abort = FALSE;
5111 funccall_T *fc;
5112
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005113 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005114 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005115 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005116 abort = abort
5117 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
5118 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005119 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005120 }
5121 return abort;
5122}
5123
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005124 static int
5125set_ref_in_funccal(funccall_T *fc, int copyID)
5126{
5127 int abort = FALSE;
5128
5129 if (fc->fc_copyID != copyID)
5130 {
5131 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005132 abort = abort
5133 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
5134 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005135 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005136 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005137 }
5138 return abort;
5139}
5140
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005141/*
5142 * Set "copyID" in all local vars and arguments in the call stack.
5143 */
5144 int
5145set_ref_in_call_stack(int copyID)
5146{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005147 int abort = FALSE;
5148 funccall_T *fc;
5149 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005150
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005151 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005152 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005153
5154 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005155 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
5156 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005157 abort = abort || set_ref_in_funccal(fc, copyID);
5158
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005159 return abort;
5160}
5161
5162/*
5163 * Set "copyID" in all functions available by name.
5164 */
5165 int
5166set_ref_in_functions(int copyID)
5167{
5168 int todo;
5169 hashitem_T *hi = NULL;
5170 int abort = FALSE;
5171 ufunc_T *fp;
5172
5173 todo = (int)func_hashtab.ht_used;
5174 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005175 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005176 if (!HASHITEM_EMPTY(hi))
5177 {
5178 --todo;
5179 fp = HI2UF(hi);
5180 if (!func_name_refcount(fp->uf_name))
5181 abort = abort || set_ref_in_func(NULL, fp, copyID);
5182 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005183 }
5184 return abort;
5185}
5186
5187/*
5188 * Set "copyID" in all function arguments.
5189 */
5190 int
5191set_ref_in_func_args(int copyID)
5192{
5193 int i;
5194 int abort = FALSE;
5195
5196 for (i = 0; i < funcargs.ga_len; ++i)
5197 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
5198 copyID, NULL, NULL);
5199 return abort;
5200}
5201
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005202/*
5203 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005204 * Returns TRUE if setting references failed somehow.
5205 */
5206 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005207set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005208{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005209 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005210 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01005211 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005212 char_u fname_buf[FLEN_FIXED + 1];
5213 char_u *tofree = NULL;
5214 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005215 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005216
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005217 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005218 return FALSE;
5219
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005220 if (fp_in == NULL)
5221 {
5222 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005223 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005224 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005225 if (fp != NULL)
5226 {
5227 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005228 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005229 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02005230
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005231 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005232 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005233}
5234
Bram Moolenaare38eab22019-12-05 21:50:01 +01005235#endif // FEAT_EVAL