blob: 04c2641203f6bd4132f4632a5dec518bf3c68474 [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.
313 if (!skip && in_vim9script()
314 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
315 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100316 semsg(_(e_white_space_required_after_str_str), ",", p - 1);
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200317 goto err_ret;
318 }
319 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200320 else
321 mustend = TRUE;
322 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200323 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200324 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200325 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200326
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200327 if (*p != endchar)
328 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100329 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200330
331 *argp = p;
332 return OK;
333
334err_ret:
335 if (newargs != NULL)
336 ga_clear_strings(newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200337 if (default_args != NULL)
338 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200339 return FAIL;
340}
341
342/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100343 * Parse the argument types, filling "fp->uf_arg_types".
344 * Return OK or FAIL.
345 */
346 static int
347parse_argument_types(ufunc_T *fp, garray_T *argtypes, int varargs)
348{
349 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
350 if (argtypes->ga_len > 0)
351 {
352 // When "varargs" is set the last name/type goes into uf_va_name
353 // and uf_va_type.
354 int len = argtypes->ga_len - (varargs ? 1 : 0);
355
356 if (len > 0)
357 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
358 if (fp->uf_arg_types != NULL)
359 {
360 int i;
361 type_T *type;
362
363 for (i = 0; i < len; ++ i)
364 {
365 char_u *p = ((char_u **)argtypes->ga_data)[i];
366
367 if (p == NULL)
368 // will get the type from the default value
369 type = &t_unknown;
370 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100371 type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100372 if (type == NULL)
373 return FAIL;
374 fp->uf_arg_types[i] = type;
375 }
376 }
377 if (varargs)
378 {
379 char_u *p;
380
381 // Move the last argument "...name: type" to uf_va_name and
382 // uf_va_type.
383 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
384 [fp->uf_args.ga_len - 1];
385 --fp->uf_args.ga_len;
386 p = ((char_u **)argtypes->ga_data)[len];
387 if (p == NULL)
388 // todo: get type from default value
389 fp->uf_va_type = &t_any;
390 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100391 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100392 if (fp->uf_va_type == NULL)
393 return FAIL;
394 }
395 }
396 return OK;
397}
398
399/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200400 * Register function "fp" as using "current_funccal" as its scope.
401 */
402 static int
403register_closure(ufunc_T *fp)
404{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200405 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100406 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200407 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200408 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200409 fp->uf_scoped = current_funccal;
410 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200411
Bram Moolenaar58016442016-07-31 18:30:22 +0200412 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
413 return FAIL;
414 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
415 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200416 return OK;
417}
418
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100419 static void
420set_ufunc_name(ufunc_T *fp, char_u *name)
421{
Bram Moolenaar7b6903f2021-02-03 19:31:29 +0100422 // Add a type cast to avoid a warning for an overflow, the uf_name[] array
423 // actually extends beyond the struct.
424 STRCPY((void *)fp->uf_name, name);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100425
426 if (name[0] == K_SPECIAL)
427 {
428 fp->uf_name_exp = alloc(STRLEN(name) + 3);
429 if (fp->uf_name_exp != NULL)
430 {
431 STRCPY(fp->uf_name_exp, "<SNR>");
432 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
433 }
434 }
435}
436
Bram Moolenaar58016442016-07-31 18:30:22 +0200437/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200438 * Get a name for a lambda. Returned in static memory.
439 */
440 char_u *
441get_lambda_name(void)
442{
443 static char_u name[30];
444 static int lambda_no = 0;
445
446 sprintf((char*)name, "<lambda>%d", ++lambda_no);
447 return name;
448}
449
Bram Moolenaar801ab062020-06-25 19:27:56 +0200450#if defined(FEAT_LUA) || defined(PROTO)
451/*
452 * Registers a native C callback which can be called from Vim script.
453 * Returns the name of the Vim script function.
454 */
455 char_u *
456register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
457{
458 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200459 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200460
461 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
462 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200463 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200464
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200465 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200466 fp->uf_refcount = 1;
467 fp->uf_varargs = TRUE;
468 fp->uf_flags = FC_CFUNC;
469 fp->uf_calls = 0;
470 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200471 fp->uf_cb = cb;
472 fp->uf_cb_free = cb_free;
473 fp->uf_cb_state = state;
474
475 set_ufunc_name(fp, name);
476 hash_add(&func_hashtab, UF2HIKEY(fp));
477
478 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200479}
480#endif
481
Bram Moolenaar04b12692020-05-04 23:24:44 +0200482/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100483 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100484 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100485 * If "white_error" is not NULL check for correct use of white space and set
486 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100487 * Return NULL if no valid arrow found.
488 */
489 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100490skip_arrow(
491 char_u *start,
492 int equal_arrow,
493 char_u **ret_type,
494 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100495{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100496 char_u *s = start;
497 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100498
499 if (equal_arrow)
500 {
501 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100502 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100503 if (white_error != NULL && !VIM_ISWHITE(s[1]))
504 {
505 *white_error = TRUE;
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100506 semsg(_(e_white_space_required_after_str_str), ":", s);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100507 return NULL;
508 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100509 s = skipwhite(s + 1);
510 *ret_type = s;
511 s = skip_type(s, TRUE);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100512 if (s == *ret_type)
513 {
514 emsg(_(e_missing_return_type));
515 return NULL;
516 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100517 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100518 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100519 s = skipwhite(s);
520 if (*s != '=')
521 return NULL;
522 ++s;
523 }
524 if (*s != '>')
525 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100526 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
527 || !IS_WHITE_OR_NUL(s[1])))
528 {
529 *white_error = TRUE;
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100530 semsg(_(e_white_space_required_before_and_after_str_at_str),
531 equal_arrow ? "=>" : "->", bef);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100532 return NULL;
533 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100534 return skipwhite(s + 1);
535}
536
537/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200538 * Parse a lambda expression and get a Funcref from "*arg".
Bram Moolenaar65c44152020-12-24 15:14:01 +0100539 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100540 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200541 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
542 */
543 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100544get_lambda_tv(
545 char_u **arg,
546 typval_T *rettv,
547 int types_optional,
548 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200549{
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200550 int evaluate = evalarg != NULL
551 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200552 garray_T newargs;
553 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200554 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100555 garray_T argtypes;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200556 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100557 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200558 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100559 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200560 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200561 char_u *s;
562 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200563 int *old_eval_lavars = eval_lavars_used;
564 int eval_lavars = FALSE;
Bram Moolenaarf898f7c2021-01-16 18:09:52 +0100565 char_u *tofree1 = NULL;
566 char_u *tofree2 = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100567 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100568 int white_error = FALSE;
Bram Moolenaar0346b792021-01-31 22:18:29 +0100569 int called_emsg_start = called_emsg;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100570
571 if (equal_arrow && !in_vim9script())
572 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200573
574 ga_init(&newargs);
575 ga_init(&newlines);
576
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100577 // First, check if this is really a lambda expression. "->" or "=>" must
578 // be found after the arguments.
Bram Moolenaarcef12702021-01-04 14:09:43 +0100579 s = *arg + 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100580 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100581 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100582 NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100583 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100584 {
585 if (types_optional)
586 ga_clear_strings(&argtypes);
Bram Moolenaar0346b792021-01-31 22:18:29 +0100587 return called_emsg == called_emsg_start ? NOTDONE : FAIL;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100588 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200589
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100590 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200591 if (evaluate)
592 pnewargs = &newargs;
593 else
594 pnewargs = NULL;
Bram Moolenaarcef12702021-01-04 14:09:43 +0100595 *arg += 1;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100596 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100597 types_optional ? &argtypes : NULL, types_optional, evalarg,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100598 &varargs, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100599 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100600 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaare462f522020-12-27 14:43:30 +0100601 equal_arrow || in_vim9script() ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100602 {
603 if (types_optional)
604 ga_clear_strings(&argtypes);
Bram Moolenaare5730bd2020-12-25 22:30:16 +0100605 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100606 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100607 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100608 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200609
Bram Moolenaarf898f7c2021-01-16 18:09:52 +0100610 // Skipping over linebreaks may make "ret_type" invalid, make a copy.
611 if (ret_type != NULL)
612 {
613 ret_type = vim_strsave(ret_type);
614 tofree2 = ret_type;
615 }
616
Bram Moolenaare38eab22019-12-05 21:50:01 +0100617 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200618 if (evaluate)
619 eval_lavars_used = &eval_lavars;
620
Bram Moolenaar65c44152020-12-24 15:14:01 +0100621 *arg = skipwhite_and_linebreak(*arg, evalarg);
622
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100623 // Recognize "{" as the start of a function body.
624 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +0100625 {
626 // TODO: process the function body upto the "}".
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100627 // Return type is required then.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100628 emsg("Lambda function body not supported yet");
629 goto errret;
630 }
631
Bram Moolenaare38eab22019-12-05 21:50:01 +0100632 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200633 start = *arg;
634 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200635 if (ret == FAIL)
636 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200637 if (evalarg != NULL)
638 {
639 // avoid that the expression gets freed when another line break follows
Bram Moolenaarf898f7c2021-01-16 18:09:52 +0100640 tofree1 = evalarg->eval_tofree;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200641 evalarg->eval_tofree = NULL;
642 }
643
Bram Moolenaar65c44152020-12-24 15:14:01 +0100644 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +0100645 {
Bram Moolenaar65c44152020-12-24 15:14:01 +0100646 *arg = skipwhite_and_linebreak(*arg, evalarg);
647 if (**arg != '}')
648 {
649 semsg(_("E451: Expected }: %s"), *arg);
650 goto errret;
651 }
652 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +0100653 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200654
655 if (evaluate)
656 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200657 int len;
658 int flags = 0;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200659 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200660 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200661
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200662 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200663 if (fp == NULL)
664 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200665 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200666 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200667 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200668 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200669
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200670 ga_init2(&newlines, (int)sizeof(char_u *), 1);
671 if (ga_grow(&newlines, 1) == FAIL)
672 goto errret;
673
Bram Moolenaare38eab22019-12-05 21:50:01 +0100674 // Add "return " before the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200675 len = 7 + (int)(end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200676 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200677 if (p == NULL)
678 goto errret;
679 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
680 STRCPY(p, "return ");
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200681 vim_strncpy(p + 7, start, end - start);
Bram Moolenaarf10806b2020-04-02 18:34:35 +0200682 if (strstr((char *)p + 7, "a:") == NULL)
683 // No a: variables are used for sure.
684 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200685
686 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100687 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200688 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200689 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100690 if (types_optional)
691 {
692 if (parse_argument_types(fp, &argtypes, FALSE) == FAIL)
693 goto errret;
694 if (ret_type != NULL)
695 {
696 fp->uf_ret_type = parse_type(&ret_type,
697 &fp->uf_type_list, TRUE);
698 if (fp->uf_ret_type == NULL)
699 goto errret;
700 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +0100701 else
Bram Moolenaar328eac22021-01-07 19:23:08 +0100702 fp->uf_ret_type = &t_any;
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100703 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100704
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200705 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200706 if (current_funccal != NULL && eval_lavars)
707 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200708 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200709 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200710 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200711 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200712
713#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200714 if (prof_def_func())
715 func_do_profile(fp);
716#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200717 if (sandbox)
718 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100719 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200720 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200721 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200722 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200723 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100724 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200725
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200726 pt->pt_func = fp;
727 pt->pt_refcount = 1;
728 rettv->vval.v_partial = pt;
729 rettv->v_type = VAR_PARTIAL;
Bram Moolenaar057e84a2021-02-28 16:55:11 +0100730
731 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200732 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200733
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200734 eval_lavars_used = old_eval_lavars;
Bram Moolenaarefaaaa62020-07-08 22:24:09 +0200735 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaarf898f7c2021-01-16 18:09:52 +0100736 evalarg->eval_tofree = tofree1;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200737 else
Bram Moolenaarf898f7c2021-01-16 18:09:52 +0100738 vim_free(tofree1);
739 vim_free(tofree2);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100740 if (types_optional)
741 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200742 return OK;
743
744errret:
745 ga_clear_strings(&newargs);
746 ga_clear_strings(&newlines);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100747 if (types_optional)
748 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200749 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100750 vim_free(pt);
Bram Moolenaarefaaaa62020-07-08 22:24:09 +0200751 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaarf898f7c2021-01-16 18:09:52 +0100752 evalarg->eval_tofree = tofree1;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200753 else
Bram Moolenaarf898f7c2021-01-16 18:09:52 +0100754 vim_free(tofree1);
755 vim_free(tofree2);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200756 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200757 return FAIL;
758}
759
760/*
761 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
762 * name it contains, otherwise return "name".
763 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
764 * "partialp".
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100765 * If "type" is not NULL and a Vim9 script-local variable is found look up the
766 * type of the variable.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200767 */
768 char_u *
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100769deref_func_name(
770 char_u *name,
771 int *lenp,
772 partial_T **partialp,
773 type_T **type,
774 int no_autoload)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200775{
776 dictitem_T *v;
777 int cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100778 char_u *s = NULL;
779 hashtab_T *ht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200780
781 if (partialp != NULL)
782 *partialp = NULL;
783
784 cc = name[*lenp];
785 name[*lenp] = NUL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100786 v = find_var(name, &ht, no_autoload);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200787 name[*lenp] = cc;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100788 if (v != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200789 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100790 if (v->di_tv.v_type == VAR_FUNC)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200791 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100792 if (v->di_tv.vval.v_string == NULL)
793 {
794 *lenp = 0;
795 return (char_u *)""; // just in case
796 }
797 s = v->di_tv.vval.v_string;
798 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200799 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200800
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100801 if (v->di_tv.v_type == VAR_PARTIAL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200802 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100803 partial_T *pt = v->di_tv.vval.v_partial;
804
805 if (pt == NULL)
806 {
807 *lenp = 0;
808 return (char_u *)""; // just in case
809 }
810 if (partialp != NULL)
811 *partialp = pt;
812 s = partial_name(pt);
813 *lenp = (int)STRLEN(s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200814 }
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100815
816 if (s != NULL)
817 {
818 if (type != NULL && ht == get_script_local_ht())
819 {
820 svar_T *sv = find_typval_in_script(&v->di_tv);
821
822 if (sv != NULL)
823 *type = sv->sv_type;
824 }
825 return s;
826 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200827 }
828
829 return name;
830}
831
832/*
833 * Give an error message with a function name. Handle <SNR> things.
834 * "ermsg" is to be passed without translation, use N_() instead of _().
835 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100836 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200837emsg_funcname(char *ermsg, char_u *name)
838{
839 char_u *p;
840
841 if (*name == K_SPECIAL)
842 p = concat_str((char_u *)"<SNR>", name + 3);
843 else
844 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100845 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200846 if (p != name)
847 vim_free(p);
848}
849
850/*
851 * Allocate a variable for the result of a function.
852 * Return OK or FAIL.
853 */
854 int
855get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200856 char_u *name, // name of the function
857 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200858 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200859 char_u **arg, // argument, pointing to the '('
Bram Moolenaare6b53242020-07-01 17:28:33 +0200860 evalarg_T *evalarg, // for line continuation
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200861 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200862{
863 char_u *argp;
864 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100865 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
866 int argcount = 0; // number of arguments found
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200867 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200868
869 /*
870 * Get the arguments.
871 */
872 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200873 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
874 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200875 {
Bram Moolenaare6b53242020-07-01 17:28:33 +0200876 // skip the '(' or ',' and possibly line breaks
877 argp = skipwhite_and_linebreak(argp + 1, evalarg);
878
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200879 if (*argp == ')' || *argp == ',' || *argp == NUL)
880 break;
Bram Moolenaare6b53242020-07-01 17:28:33 +0200881 if (eval1(&argp, &argvars[argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200882 {
883 ret = FAIL;
884 break;
885 }
886 ++argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +0200887 // The comma should come right after the argument, but this wasn't
888 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200889 if (vim9script)
890 {
891 if (*argp != ',' && *skipwhite(argp) == ',')
892 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +0100893 semsg(_(e_no_white_space_allowed_before_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200894 ret = FAIL;
895 break;
896 }
897 }
898 else
Bram Moolenaar9d489562020-07-30 20:08:50 +0200899 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200900 if (*argp != ',')
901 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200902 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
903 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100904 semsg(_(e_white_space_required_after_str_str), ",", argp);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200905 ret = FAIL;
906 break;
907 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200908 }
Bram Moolenaare6b53242020-07-01 17:28:33 +0200909 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200910 if (*argp == ')')
911 ++argp;
912 else
913 ret = FAIL;
914
915 if (ret == OK)
916 {
917 int i = 0;
918
919 if (get_vim_var_nr(VV_TESTING))
920 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100921 // Prepare for calling test_garbagecollect_now(), need to know
922 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200923 if (funcargs.ga_itemsize == 0)
924 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
925 for (i = 0; i < argcount; ++i)
926 if (ga_grow(&funcargs, 1) == OK)
927 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
928 &argvars[i];
929 }
930
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200931 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200932
933 funcargs.ga_len -= i;
934 }
935 else if (!aborting())
936 {
937 if (argcount == MAX_FUNC_ARGS)
938 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
939 else
940 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
941 }
942
943 while (--argcount >= 0)
944 clear_tv(&argvars[argcount]);
945
Bram Moolenaar8294d492020-08-10 22:40:56 +0200946 if (in_vim9script())
947 *arg = argp;
948 else
949 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200950 return ret;
951}
952
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200953/*
954 * Return TRUE if "p" starts with "<SID>" or "s:".
955 * Only works if eval_fname_script() returned non-zero for "p"!
956 */
957 static int
958eval_fname_sid(char_u *p)
959{
960 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
961}
962
963/*
964 * In a script change <SID>name() and s:name() to K_SNR 123_name().
965 * Change <SNR>123_name() to K_SNR 123_name().
966 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
967 * (slow).
968 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100969 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200970fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
971{
972 int llen;
973 char_u *fname;
974 int i;
975
976 llen = eval_fname_script(name);
977 if (llen > 0)
978 {
979 fname_buf[0] = K_SPECIAL;
980 fname_buf[1] = KS_EXTRA;
981 fname_buf[2] = (int)KE_SNR;
982 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100983 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200984 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200985 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100986 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200987 else
988 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200989 sprintf((char *)fname_buf + 3, "%ld_",
990 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200991 i = (int)STRLEN(fname_buf);
992 }
993 }
994 if (i + STRLEN(name + llen) < FLEN_FIXED)
995 {
996 STRCPY(fname_buf + i, name + llen);
997 fname = fname_buf;
998 }
999 else
1000 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02001001 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001002 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +01001003 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001004 else
1005 {
1006 *tofree = fname;
1007 mch_memmove(fname, fname_buf, (size_t)i);
1008 STRCPY(fname + i, name + llen);
1009 }
1010 }
1011 }
1012 else
1013 fname = name;
1014 return fname;
1015}
1016
1017/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001018 * Find a function "name" in script "sid".
1019 */
1020 static ufunc_T *
1021find_func_with_sid(char_u *name, int sid)
1022{
1023 hashitem_T *hi;
1024 char_u buffer[200];
1025
1026 buffer[0] = K_SPECIAL;
1027 buffer[1] = KS_EXTRA;
1028 buffer[2] = (int)KE_SNR;
1029 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
1030 (long)sid, name);
1031 hi = hash_find(&func_hashtab, buffer);
1032 if (!HASHITEM_EMPTY(hi))
1033 return HI2UF(hi);
1034
1035 return NULL;
1036}
1037
1038/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001039 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001040 * When "is_global" is true don't find script-local or imported functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001041 * Return NULL for unknown function.
1042 */
Bram Moolenaarce658352020-07-31 23:47:12 +02001043 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001044find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001045{
1046 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001047 ufunc_T *func;
1048 imported_T *imported;
1049
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001050 if (!is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051 {
Bram Moolenaar333894b2020-08-01 18:53:07 +02001052 char_u *after_script = NULL;
Bram Moolenaarefa94442020-08-08 22:16:00 +02001053 long sid = 0;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001054 int find_script_local = in_vim9script()
1055 && eval_isnamec1(*name) && name[1] != ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056
Bram Moolenaar95006e32020-08-29 17:47:08 +02001057 if (find_script_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001058 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001059 // Find script-local function before global one.
1060 func = find_func_with_sid(name, current_sctx.sc_sid);
1061 if (func != NULL)
1062 return func;
1063 }
1064
Bram Moolenaarefa94442020-08-08 22:16:00 +02001065 if (name[0] == K_SPECIAL
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001066 && name[1] == KS_EXTRA
1067 && name[2] == KE_SNR)
1068 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001069 // Caller changes s: to <SNR>99_name.
1070
1071 after_script = name + 3;
1072 sid = getdigits(&after_script);
Bram Moolenaarefa94442020-08-08 22:16:00 +02001073 if (*after_script == '_')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001074 ++after_script;
1075 else
1076 after_script = NULL;
1077 }
Bram Moolenaar95006e32020-08-29 17:47:08 +02001078 if (find_script_local || after_script != NULL)
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001079 {
1080 // Find imported function before global one.
Bram Moolenaarefa94442020-08-08 22:16:00 +02001081 if (after_script != NULL && sid != current_sctx.sc_sid)
1082 imported = find_imported_in_script(after_script, 0, sid);
1083 else
1084 imported = find_imported(after_script == NULL
1085 ? name : after_script, 0, cctx);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001086 if (imported != NULL && imported->imp_funcname != NULL)
1087 {
1088 hi = hash_find(&func_hashtab, imported->imp_funcname);
1089 if (!HASHITEM_EMPTY(hi))
1090 return HI2UF(hi);
1091 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001092 }
1093 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001094
Bram Moolenaara26b9702020-04-18 19:53:28 +02001095 hi = hash_find(&func_hashtab,
1096 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001097 if (!HASHITEM_EMPTY(hi))
1098 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001099
1100 return NULL;
1101}
1102
1103/*
1104 * Find a function by name, return pointer to it in ufuncs.
1105 * "cctx" is passed in a :def function to find imported functions.
1106 * Return NULL for unknown or dead function.
1107 */
1108 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001109find_func(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001111 ufunc_T *fp = find_func_even_dead(name, is_global, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001112
1113 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
1114 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001115 return NULL;
1116}
1117
1118/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02001119 * Return TRUE if "ufunc" is a global function.
1120 */
1121 int
1122func_is_global(ufunc_T *ufunc)
1123{
1124 return ufunc->uf_name[0] != K_SPECIAL;
1125}
1126
1127/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001128 * Copy the function name of "fp" to buffer "buf".
1129 * "buf" must be able to hold the function name plus three bytes.
1130 * Takes care of script-local function names.
1131 */
1132 static void
1133cat_func_name(char_u *buf, ufunc_T *fp)
1134{
Bram Moolenaar0f769812020-09-12 18:32:34 +02001135 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001136 {
1137 STRCPY(buf, "<SNR>");
1138 STRCAT(buf, fp->uf_name + 3);
1139 }
1140 else
1141 STRCPY(buf, fp->uf_name);
1142}
1143
1144/*
1145 * Add a number variable "name" to dict "dp" with value "nr".
1146 */
1147 static void
1148add_nr_var(
1149 dict_T *dp,
1150 dictitem_T *v,
1151 char *name,
1152 varnumber_T nr)
1153{
1154 STRCPY(v->di_key, name);
1155 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1156 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
1157 v->di_tv.v_type = VAR_NUMBER;
1158 v->di_tv.v_lock = VAR_FIXED;
1159 v->di_tv.vval.v_number = nr;
1160}
1161
1162/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001163 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001164 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001165 static void
1166free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001167{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001168 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001169
1170 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1171 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001172 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001173
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001174 // When garbage collecting a funccall_T may be freed before the
1175 // function that references it, clear its uf_scoped field.
1176 // The function may have been redefined and point to another
1177 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001178 if (fp != NULL && fp->uf_scoped == fc)
1179 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001180 }
Bram Moolenaar58016442016-07-31 18:30:22 +02001181 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001182
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001183 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001184 vim_free(fc);
1185}
1186
1187/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001188 * Free "fc" and what it contains.
1189 * Can be called only when "fc" is kept beyond the period of it called,
1190 * i.e. after cleanup_function_call(fc).
1191 */
1192 static void
1193free_funccal_contents(funccall_T *fc)
1194{
1195 listitem_T *li;
1196
1197 // Free all l: variables.
1198 vars_clear(&fc->l_vars.dv_hashtab);
1199
1200 // Free all a: variables.
1201 vars_clear(&fc->l_avars.dv_hashtab);
1202
1203 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001204 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001205 clear_tv(&li->li_tv);
1206
1207 free_funccal(fc);
1208}
1209
1210/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02001211 * Handle the last part of returning from a function: free the local hashtable.
1212 * Unless it is still in use by a closure.
1213 */
1214 static void
1215cleanup_function_call(funccall_T *fc)
1216{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001217 int may_free_fc = fc->fc_refcount <= 0;
1218 int free_fc = TRUE;
1219
Bram Moolenaar6914c642017-04-01 21:21:30 +02001220 current_funccal = fc->caller;
1221
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001222 // Free all l: variables if not referred.
1223 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
1224 vars_clear(&fc->l_vars.dv_hashtab);
1225 else
1226 free_fc = FALSE;
1227
1228 // If the a:000 list and the l: and a: dicts are not referenced and
1229 // there is no closure using it, we can free the funccall_T and what's
1230 // in it.
1231 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
1232 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02001233 else
1234 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001235 int todo;
1236 hashitem_T *hi;
1237 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02001238
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001239 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02001240
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001241 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001242 todo = (int)fc->l_avars.dv_hashtab.ht_used;
1243 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
1244 {
1245 if (!HASHITEM_EMPTY(hi))
1246 {
1247 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001248 di = HI2DI(hi);
1249 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02001250 }
1251 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001252 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02001253
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001254 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
1255 fc->l_varlist.lv_first = NULL;
1256 else
1257 {
1258 listitem_T *li;
1259
1260 free_fc = FALSE;
1261
1262 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001263 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02001264 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001265 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001266
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001267 if (free_fc)
1268 free_funccal(fc);
1269 else
1270 {
1271 static int made_copy = 0;
1272
1273 // "fc" is still in use. This can happen when returning "a:000",
1274 // assigning "l:" to a global variable or defining a closure.
1275 // Link "fc" in the list for garbage collection later.
1276 fc->caller = previous_funccal;
1277 previous_funccal = fc;
1278
1279 if (want_garbage_collect)
1280 // If garbage collector is ready, clear count.
1281 made_copy = 0;
1282 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001283 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001284 // We have made a lot of copies, worth 4 Mbyte. This can happen
1285 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01001286 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001287 // too much memory.
1288 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01001289 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001290 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02001291 }
1292}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001293
1294/*
1295 * There are two kinds of function names:
1296 * 1. ordinary names, function defined with :function or :def
1297 * 2. numbered functions and lambdas
1298 * For the first we only count the name stored in func_hashtab as a reference,
1299 * using function() does not count as a reference, because the function is
1300 * looked up by name.
1301 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02001302 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001303func_name_refcount(char_u *name)
1304{
1305 return isdigit(*name) || *name == '<';
1306}
1307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001308/*
1309 * Unreference "fc": decrement the reference count and free it when it
1310 * becomes zero. "fp" is detached from "fc".
1311 * When "force" is TRUE we are exiting.
1312 */
1313 static void
1314funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
1315{
1316 funccall_T **pfc;
1317 int i;
1318
1319 if (fc == NULL)
1320 return;
1321
1322 if (--fc->fc_refcount <= 0 && (force || (
1323 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
1324 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
1325 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
1326 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
1327 {
1328 if (fc == *pfc)
1329 {
1330 *pfc = fc->caller;
1331 free_funccal_contents(fc);
1332 return;
1333 }
1334 }
1335 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1336 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
1337 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
1338}
1339
1340/*
1341 * Remove the function from the function hashtable. If the function was
1342 * deleted while it still has references this was already done.
1343 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
1344 */
1345 static int
1346func_remove(ufunc_T *fp)
1347{
1348 hashitem_T *hi;
1349
1350 // Return if it was already virtually deleted.
1351 if (fp->uf_flags & FC_DEAD)
1352 return FALSE;
1353
1354 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
1355 if (!HASHITEM_EMPTY(hi))
1356 {
1357 // When there is a def-function index do not actually remove the
1358 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001359 // Do remove it when it's a copy.
1360 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 fp->uf_flags |= FC_DEAD;
1362 else
1363 hash_remove(&func_hashtab, hi);
1364 return TRUE;
1365 }
1366 return FALSE;
1367}
1368
1369 static void
1370func_clear_items(ufunc_T *fp)
1371{
1372 ga_clear_strings(&(fp->uf_args));
1373 ga_clear_strings(&(fp->uf_def_args));
1374 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001375 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001376 VIM_CLEAR(fp->uf_def_arg_idx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001377 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001378 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02001379 clear_type_list(&fp->uf_type_list);
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001380
1381 // Increment the refcount of this function to avoid it being freed
1382 // recursively when the partial is freed.
1383 fp->uf_refcount += 3;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001384 partial_unref(fp->uf_partial);
1385 fp->uf_partial = NULL;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001386 fp->uf_refcount -= 3;
Bram Moolenaar801ab062020-06-25 19:27:56 +02001387
1388#ifdef FEAT_LUA
1389 if (fp->uf_cb_free != NULL)
1390 {
1391 fp->uf_cb_free(fp->uf_cb_state);
1392 fp->uf_cb_free = NULL;
1393 }
1394
1395 fp->uf_cb_state = NULL;
1396 fp->uf_cb = NULL;
1397#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398#ifdef FEAT_PROFILE
1399 VIM_CLEAR(fp->uf_tml_count);
1400 VIM_CLEAR(fp->uf_tml_total);
1401 VIM_CLEAR(fp->uf_tml_self);
1402#endif
1403}
1404
1405/*
1406 * Free all things that a function contains. Does not free the function
1407 * itself, use func_free() for that.
1408 * When "force" is TRUE we are exiting.
1409 */
1410 static void
1411func_clear(ufunc_T *fp, int force)
1412{
1413 if (fp->uf_cleared)
1414 return;
1415 fp->uf_cleared = TRUE;
1416
1417 // clear this function
1418 func_clear_items(fp);
1419 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001420 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421}
1422
1423/*
1424 * Free a function and remove it from the list of functions. Does not free
1425 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001426 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02001427 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001428 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02001429 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001430func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001431{
1432 // Only remove it when not done already, otherwise we would remove a newer
1433 // version of the function with the same name.
1434 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1435 func_remove(fp);
1436
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001437 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001438 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001439 if (fp->uf_dfunc_idx > 0)
1440 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001441 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02001443 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001444 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02001445 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001446}
1447
1448/*
1449 * Free all things that a function contains and free the function itself.
1450 * When "force" is TRUE we are exiting.
1451 */
1452 static void
1453func_clear_free(ufunc_T *fp, int force)
1454{
1455 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001456 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
1457 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001458 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02001459 else
1460 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461}
1462
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001463/*
1464 * Copy already defined function "lambda" to a new function with name "global".
1465 * This is for when a compiled function defines a global function.
1466 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001467 int
1468copy_func(char_u *lambda, char_u *global, ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001469{
1470 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01001471 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001472
1473 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001474 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001475 semsg(_(e_lambda_function_not_found_str), lambda);
1476 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001477 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001478
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001479 fp = find_func(global, TRUE, NULL);
1480 if (fp != NULL)
1481 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001482 // TODO: handle ! to overwrite
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001483 semsg(_(e_funcexts), global);
1484 return FAIL;
1485 }
1486
1487 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
1488 if (fp == NULL)
1489 return FAIL;
1490
1491 fp->uf_varargs = ufunc->uf_varargs;
1492 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
1493 fp->uf_def_status = ufunc->uf_def_status;
1494 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
1495 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
1496 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
1497 == FAIL
1498 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
1499 goto failed;
1500
1501 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
1502 : vim_strsave(ufunc->uf_name_exp);
1503 if (ufunc->uf_arg_types != NULL)
1504 {
1505 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
1506 if (fp->uf_arg_types == NULL)
1507 goto failed;
1508 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
1509 sizeof(type_T *) * fp->uf_args.ga_len);
1510 }
1511 if (ufunc->uf_def_arg_idx != NULL)
1512 {
1513 fp->uf_def_arg_idx = ALLOC_MULT(int, fp->uf_def_args.ga_len + 1);
1514 if (fp->uf_def_arg_idx == NULL)
1515 goto failed;
1516 mch_memmove(fp->uf_def_arg_idx, ufunc->uf_def_arg_idx,
1517 sizeof(int) * fp->uf_def_args.ga_len + 1);
1518 }
1519 if (ufunc->uf_va_name != NULL)
1520 {
1521 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
1522 if (fp->uf_va_name == NULL)
1523 goto failed;
1524 }
1525 fp->uf_ret_type = ufunc->uf_ret_type;
1526
1527 fp->uf_refcount = 1;
1528 STRCPY(fp->uf_name, global);
1529 hash_add(&func_hashtab, UF2HIKEY(fp));
1530
1531 // the referenced dfunc_T is now used one more time
1532 link_def_function(fp);
1533
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001534 // Create a partial to store the context of the function where it was
1535 // instantiated. Only needs to be done once. Do this on the original
1536 // function, "dfunc->df_ufunc" will point to it.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001537 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
1538 {
1539 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
1540
1541 if (pt == NULL)
1542 goto failed;
1543 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001544 {
1545 vim_free(pt);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001546 goto failed;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001547 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001548 ufunc->uf_partial = pt;
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001549 --pt->pt_refcount; // not actually referenced here
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001550 }
1551
1552 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001553
1554failed:
1555 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001556 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001557}
1558
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001559static int funcdepth = 0;
1560
1561/*
1562 * Increment the function call depth count.
1563 * Return FAIL when going over 'maxfuncdepth'.
1564 * Otherwise return OK, must call funcdepth_decrement() later!
1565 */
1566 int
1567funcdepth_increment(void)
1568{
1569 if (funcdepth >= p_mfd)
1570 {
1571 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
1572 return FAIL;
1573 }
1574 ++funcdepth;
1575 return OK;
1576}
1577
1578 void
1579funcdepth_decrement(void)
1580{
1581 --funcdepth;
1582}
1583
1584/*
1585 * Get the current function call depth.
1586 */
1587 int
1588funcdepth_get(void)
1589{
1590 return funcdepth;
1591}
1592
1593/*
1594 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001595 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001596 * times as funcdepth_increment().
1597 */
1598 void
1599funcdepth_restore(int depth)
1600{
1601 funcdepth = depth;
1602}
Bram Moolenaar6914c642017-04-01 21:21:30 +02001603
1604/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001605 * Call a user function.
1606 */
1607 static void
1608call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001609 ufunc_T *fp, // pointer to function
1610 int argcount, // nr of args
1611 typval_T *argvars, // arguments
1612 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001613 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01001614 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001615{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001616 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001617 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001618 funccall_T *fc;
1619 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001620 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001621 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001622 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001623 int i;
1624 int ai;
1625 int islambda = FALSE;
1626 char_u numbuf[NUMBUFLEN];
1627 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001628#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001629 profinfo_T profile_info;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001630#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001631 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001632
Bram Moolenaarb2049902021-01-24 12:53:53 +01001633#ifdef FEAT_PROFILE
1634 CLEAR_FIELD(profile_info);
1635#endif
1636
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001637 // If depth of calling is getting too high, don't execute the function.
1638 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001639 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001640 rettv->v_type = VAR_NUMBER;
1641 rettv->vval.v_number = -1;
1642 return;
1643 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001644
Bram Moolenaare38eab22019-12-05 21:50:01 +01001645 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001646
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001647 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001648 if (fc == NULL)
1649 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001650 fc->caller = current_funccal;
1651 current_funccal = fc;
1652 fc->func = fp;
1653 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001654 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001655 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001656 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1657 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001658 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001659 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001660 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001661
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001662 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001663 {
Bram Moolenaar12d26532021-02-19 19:13:21 +01001664#ifdef FEAT_PROFILE
1665 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
1666#endif
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001667 // Execute the function, possibly compiling it first.
Bram Moolenaarb2049902021-01-24 12:53:53 +01001668#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001669 if (do_profiling == PROF_YES)
1670 profile_may_start_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01001671#endif
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001672 call_def_function(fp, argcount, argvars, funcexe->partial, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001673 funcdepth_decrement();
Bram Moolenaarb2049902021-01-24 12:53:53 +01001674#ifdef FEAT_PROFILE
1675 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar12d26532021-02-19 19:13:21 +01001676 || (caller != NULL && caller->uf_profiling)))
1677 profile_may_end_func(&profile_info, fp, caller);
Bram Moolenaarb2049902021-01-24 12:53:53 +01001678#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 current_funccal = fc->caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 free_funccal(fc);
1681 return;
1682 }
1683
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001684 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1685 islambda = TRUE;
1686
1687 /*
1688 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1689 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1690 * each argument variable and saves a lot of time.
1691 */
1692 /*
1693 * Init l: variables.
1694 */
1695 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1696 if (selfdict != NULL)
1697 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001698 // Set l:self to "selfdict". Use "name" to avoid a warning from
1699 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001700 v = &fc->fixvar[fixvar_idx++].var;
1701 name = v->di_key;
1702 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001703 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001704 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1705 v->di_tv.v_type = VAR_DICT;
1706 v->di_tv.v_lock = 0;
1707 v->di_tv.vval.v_dict = selfdict;
1708 ++selfdict->dv_refcount;
1709 }
1710
1711 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001712 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001713 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001714 * Set a:000 to a list with room for the "..." arguments.
1715 */
1716 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001717 if ((fp->uf_flags & FC_NOARGS) == 0)
1718 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001719 (varnumber_T)(argcount >= fp->uf_args.ga_len
1720 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001721 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001722 if ((fp->uf_flags & FC_NOARGS) == 0)
1723 {
1724 // Use "name" to avoid a warning from some compiler that checks the
1725 // destination size.
1726 v = &fc->fixvar[fixvar_idx++].var;
1727 name = v->di_key;
1728 STRCPY(name, "000");
1729 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1730 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1731 v->di_tv.v_type = VAR_LIST;
1732 v->di_tv.v_lock = VAR_FIXED;
1733 v->di_tv.vval.v_list = &fc->l_varlist;
1734 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001735 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001736 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1737 fc->l_varlist.lv_lock = VAR_FIXED;
1738
1739 /*
1740 * Set a:firstline to "firstline" and a:lastline to "lastline".
1741 * Set a:name to named arguments.
1742 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001743 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001744 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001745 if ((fp->uf_flags & FC_NOARGS) == 0)
1746 {
1747 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001748 (varnumber_T)funcexe->firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001749 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001750 (varnumber_T)funcexe->lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001751 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001752 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001753 {
1754 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001755 typval_T def_rettv;
1756 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001757
1758 ai = i - fp->uf_args.ga_len;
1759 if (ai < 0)
1760 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001761 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001762 name = FUNCARG(fp, i);
1763 if (islambda)
1764 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001765
1766 // evaluate named argument default expression
1767 isdefault = ai + fp->uf_def_args.ga_len >= 0
1768 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1769 && argvars[i].vval.v_number == VVAL_NONE));
1770 if (isdefault)
1771 {
1772 char_u *default_expr = NULL;
1773 def_rettv.v_type = VAR_NUMBER;
1774 def_rettv.vval.v_number = -1;
1775
1776 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1777 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001778 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001779 {
1780 default_arg_err = 1;
1781 break;
1782 }
1783 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001784 }
1785 else
1786 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001787 if ((fp->uf_flags & FC_NOARGS) != 0)
1788 // Bail out if no a: arguments used (in lambda).
1789 break;
1790
Bram Moolenaare38eab22019-12-05 21:50:01 +01001791 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001792 sprintf((char *)numbuf, "%d", ai + 1);
1793 name = numbuf;
1794 }
1795 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1796 {
1797 v = &fc->fixvar[fixvar_idx++].var;
1798 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001799 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001800 }
1801 else
1802 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001803 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001804 if (v == NULL)
1805 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001806 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001807 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001808
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001809 // Note: the values are copied directly to avoid alloc/free.
1810 // "argvars" must have VAR_FIXED for v_lock.
1811 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001812 v->di_tv.v_lock = VAR_FIXED;
1813
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001814 if (addlocal)
1815 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001816 // Named arguments should be accessed without the "a:" prefix in
1817 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001818 copy_tv(&v->di_tv, &v->di_tv);
1819 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001820 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001821 else
1822 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001823
1824 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1825 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001826 listitem_T *li = &fc->l_listitems[ai];
1827
1828 li->li_tv = argvars[i];
1829 li->li_tv.v_lock = VAR_FIXED;
1830 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001831 }
1832 }
1833
Bram Moolenaare38eab22019-12-05 21:50:01 +01001834 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001835 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001836
1837 if (fp->uf_flags & FC_SANDBOX)
1838 {
1839 using_sandbox = TRUE;
1840 ++sandbox;
1841 }
1842
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001843 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001844 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001845 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001846 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001847 ++no_wait_return;
1848 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001849
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001850 smsg(_("calling %s"), SOURCING_NAME);
1851 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001852 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001853 char_u buf[MSG_BUF_LEN];
1854 char_u numbuf2[NUMBUFLEN];
1855 char_u *tofree;
1856 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001857
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001858 msg_puts("(");
1859 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001860 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001861 if (i > 0)
1862 msg_puts(", ");
1863 if (argvars[i].v_type == VAR_NUMBER)
1864 msg_outnum((long)argvars[i].vval.v_number);
1865 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001866 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001867 // Do not want errors such as E724 here.
1868 ++emsg_off;
1869 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1870 --emsg_off;
1871 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001872 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001873 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001874 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001875 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1876 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001877 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001878 msg_puts((char *)s);
1879 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001880 }
1881 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001882 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001883 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001884 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001885 msg_puts("\n"); // don't overwrite this either
1886
1887 verbose_leave_scroll();
1888 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001889 }
1890#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001891 if (do_profiling == PROF_YES)
1892 profile_may_start_func(&profile_info, fp,
1893 fc->caller == NULL ? NULL : fc->caller->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001894#endif
1895
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001896 save_current_sctx = current_sctx;
1897 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001898 save_did_emsg = did_emsg;
1899 did_emsg = FALSE;
1900
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001901 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1902 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001903 else if (islambda)
1904 {
1905 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
1906
1907 // A Lambda always has the command "return {expr}". It is much faster
1908 // to evaluate {expr} directly.
1909 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001910 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001911 --ex_nesting_level;
1912 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001913 else
1914 // call do_cmdline() to execute the lines
1915 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001916 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1917
1918 --RedrawingDisabled;
1919
Bram Moolenaare38eab22019-12-05 21:50:01 +01001920 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001921 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1922 {
1923 clear_tv(rettv);
1924 rettv->v_type = VAR_NUMBER;
1925 rettv->vval.v_number = -1;
1926 }
1927
1928#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001929 if (do_profiling == PROF_YES)
1930 {
1931 ufunc_T *caller = fc->caller == NULL ? NULL : fc->caller->func;
1932
1933 if (fp->uf_profiling || (caller != NULL && caller->uf_profiling))
1934 profile_may_end_func(&profile_info, fp, caller);
1935 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001936#endif
1937
Bram Moolenaare38eab22019-12-05 21:50:01 +01001938 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001939 if (p_verbose >= 12)
1940 {
1941 ++no_wait_return;
1942 verbose_enter_scroll();
1943
1944 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001945 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001946 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001947 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001948 (long)fc->rettv->vval.v_number);
1949 else
1950 {
1951 char_u buf[MSG_BUF_LEN];
1952 char_u numbuf2[NUMBUFLEN];
1953 char_u *tofree;
1954 char_u *s;
1955
Bram Moolenaare38eab22019-12-05 21:50:01 +01001956 // The value may be very long. Skip the middle part, so that we
1957 // have some idea how it starts and ends. smsg() would always
1958 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001959 ++emsg_off;
1960 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1961 --emsg_off;
1962 if (s != NULL)
1963 {
1964 if (vim_strsize(s) > MSG_BUF_CLEN)
1965 {
1966 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1967 s = buf;
1968 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001969 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001970 vim_free(tofree);
1971 }
1972 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001973 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001974
1975 verbose_leave_scroll();
1976 --no_wait_return;
1977 }
1978
Bram Moolenaare31ee862020-01-07 20:59:34 +01001979 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001980 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001981 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001982#ifdef FEAT_PROFILE
1983 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001984 script_prof_restore(&profile_info.pi_wait_start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001985#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001986 if (using_sandbox)
1987 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001988
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001989 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001990 {
1991 ++no_wait_return;
1992 verbose_enter_scroll();
1993
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001994 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001995 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001996
1997 verbose_leave_scroll();
1998 --no_wait_return;
1999 }
2000
2001 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01002002 funcdepth_decrement();
Bram Moolenaar6914c642017-04-01 21:21:30 +02002003 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002004}
2005
2006/*
Bram Moolenaar50824712020-12-20 21:10:17 +01002007 * Check the argument count for user function "fp".
2008 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
2009 */
2010 int
2011check_user_func_argcount(ufunc_T *fp, int argcount)
2012{
2013 int regular_args = fp->uf_args.ga_len;
2014
2015 if (argcount < regular_args - fp->uf_def_args.ga_len)
2016 return FCERR_TOOFEW;
2017 else if (!has_varargs(fp) && argcount > regular_args)
2018 return FCERR_TOOMANY;
2019 return FCERR_UNKNOWN;
2020}
2021
2022/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002024 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 int
2026call_user_func_check(
2027 ufunc_T *fp,
2028 int argcount,
2029 typval_T *argvars,
2030 typval_T *rettv,
2031 funcexe_T *funcexe,
2032 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002033{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002034 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002035
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002036 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
2037 *funcexe->doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01002038 error = check_user_func_argcount(fp, argcount);
2039 if (error != FCERR_UNKNOWN)
2040 return error;
2041 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 error = FCERR_DICT;
2043 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002044 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002045 int did_save_redo = FALSE;
2046 save_redo_T save_redo;
2047
2048 /*
2049 * Call the user function.
2050 * Save and restore search patterns, script variables and
2051 * redo buffer.
2052 */
2053 save_search_patterns();
2054 if (!ins_compl_active())
2055 {
2056 saveRedobuff(&save_redo);
2057 did_save_redo = TRUE;
2058 }
2059 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002060 call_user_func(fp, argcount, argvars, rettv, funcexe,
2061 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002062 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
2063 // Function was unreferenced while being used, free it now.
2064 func_clear_free(fp, FALSE);
2065 if (did_save_redo)
2066 restoreRedobuff(&save_redo);
2067 restore_search_patterns();
2068 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002069 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002071}
2072
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002073static funccal_entry_T *funccal_stack = NULL;
2074
2075/*
2076 * Save the current function call pointer, and set it to NULL.
2077 * Used when executing autocommands and for ":source".
2078 */
2079 void
2080save_funccal(funccal_entry_T *entry)
2081{
2082 entry->top_funccal = current_funccal;
2083 entry->next = funccal_stack;
2084 funccal_stack = entry;
2085 current_funccal = NULL;
2086}
2087
2088 void
2089restore_funccal(void)
2090{
2091 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002092 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002093 else
2094 {
2095 current_funccal = funccal_stack->top_funccal;
2096 funccal_stack = funccal_stack->next;
2097 }
2098}
2099
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02002100 funccall_T *
2101get_current_funccal(void)
2102{
2103 return current_funccal;
2104}
2105
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002106/*
2107 * Mark all functions of script "sid" as deleted.
2108 */
2109 void
2110delete_script_functions(int sid)
2111{
2112 hashitem_T *hi;
2113 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02002114 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002115 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002116 size_t len;
2117
2118 buf[0] = K_SPECIAL;
2119 buf[1] = KS_EXTRA;
2120 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002121 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002122 len = STRLEN(buf);
2123
Bram Moolenaarce658352020-07-31 23:47:12 +02002124 while (todo > 0)
2125 {
2126 todo = func_hashtab.ht_used;
2127 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2128 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002129 {
Bram Moolenaarce658352020-07-31 23:47:12 +02002130 fp = HI2UF(hi);
2131 if (STRNCMP(fp->uf_name, buf, len) == 0)
2132 {
2133 int changed = func_hashtab.ht_changed;
2134
2135 fp->uf_flags |= FC_DEAD;
2136 func_clear(fp, TRUE);
2137 // When clearing a function another function can be cleared
2138 // as a side effect. When that happens start over.
2139 if (changed != func_hashtab.ht_changed)
2140 break;
2141 }
2142 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002143 }
Bram Moolenaarce658352020-07-31 23:47:12 +02002144 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002145}
2146
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002147#if defined(EXITFREE) || defined(PROTO)
2148 void
2149free_all_functions(void)
2150{
2151 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002152 ufunc_T *fp;
2153 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002154 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002155 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002156
Bram Moolenaare38eab22019-12-05 21:50:01 +01002157 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02002158 while (current_funccal != NULL)
2159 {
2160 clear_tv(current_funccal->rettv);
2161 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002162 if (current_funccal == NULL && funccal_stack != NULL)
2163 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02002164 }
2165
Bram Moolenaare38eab22019-12-05 21:50:01 +01002166 // First clear what the functions contain. Since this may lower the
2167 // reference count of a function, it may also free a function and change
2168 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002169 while (todo > 0)
2170 {
2171 todo = func_hashtab.ht_used;
2172 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2173 if (!HASHITEM_EMPTY(hi))
2174 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175 // clear the def function index now
2176 fp = HI2UF(hi);
2177 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002178 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002179
Bram Moolenaare38eab22019-12-05 21:50:01 +01002180 // Only free functions that are not refcounted, those are
2181 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002182 if (func_name_refcount(fp->uf_name))
2183 ++skipped;
2184 else
2185 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002186 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002187 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002188 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002189 {
2190 skipped = 0;
2191 break;
2192 }
2193 }
2194 --todo;
2195 }
2196 }
2197
Bram Moolenaare38eab22019-12-05 21:50:01 +01002198 // Now actually free the functions. Need to start all over every time,
2199 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002200 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002201 while (func_hashtab.ht_used > skipped)
2202 {
2203 todo = func_hashtab.ht_used;
2204 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002205 if (!HASHITEM_EMPTY(hi))
2206 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02002207 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002208 // Only free functions that are not refcounted, those are
2209 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02002210 fp = HI2UF(hi);
2211 if (func_name_refcount(fp->uf_name))
2212 ++skipped;
2213 else
2214 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02002215 if (func_free(fp, FALSE) == OK)
2216 {
2217 skipped = 0;
2218 break;
2219 }
2220 // did not actually free it
2221 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002222 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002223 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02002224 }
2225 if (skipped == 0)
2226 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227
2228 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002229}
2230#endif
2231
2232/*
2233 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02002234 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002235 * "len" is the length of "name", or -1 for NUL terminated.
2236 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002237 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002238builtin_function(char_u *name, int len)
2239{
2240 char_u *p;
2241
Bram Moolenaara26b9702020-04-18 19:53:28 +02002242 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002243 return FALSE;
2244 p = vim_strchr(name, AUTOLOAD_CHAR);
2245 return p == NULL || (len > 0 && p > name + len);
2246}
2247
2248 int
2249func_call(
2250 char_u *name,
2251 typval_T *args,
2252 partial_T *partial,
2253 dict_T *selfdict,
2254 typval_T *rettv)
2255{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002256 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002257 listitem_T *item;
2258 typval_T argv[MAX_FUNC_ARGS + 1];
2259 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002260 int r = 0;
2261
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002262 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002263 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002264 {
2265 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
2266 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002267 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002268 break;
2269 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002270 // Make a copy of each argument. This is needed to be able to set
2271 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002272 copy_tv(&item->li_tv, &argv[argc++]);
2273 }
2274
2275 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002276 {
2277 funcexe_T funcexe;
2278
Bram Moolenaara80faa82020-04-12 19:37:17 +02002279 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002280 funcexe.firstline = curwin->w_cursor.lnum;
2281 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002282 funcexe.evaluate = TRUE;
2283 funcexe.partial = partial;
2284 funcexe.selfdict = selfdict;
2285 r = call_func(name, -1, rettv, argc, argv, &funcexe);
2286 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002287
Bram Moolenaare38eab22019-12-05 21:50:01 +01002288 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002289 while (argc > 0)
2290 clear_tv(&argv[--argc]);
2291
2292 return r;
2293}
2294
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002295static int callback_depth = 0;
2296
2297 int
2298get_callback_depth(void)
2299{
2300 return callback_depth;
2301}
2302
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002303/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002304 * Invoke call_func() with a callback.
2305 */
2306 int
2307call_callback(
2308 callback_T *callback,
2309 int len, // length of "name" or -1 to use strlen()
2310 typval_T *rettv, // return value goes here
2311 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002312 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002313 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002314{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002315 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002316 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002317
Bram Moolenaara80faa82020-04-12 19:37:17 +02002318 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002319 funcexe.evaluate = TRUE;
2320 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002321 ++callback_depth;
2322 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
2323 --callback_depth;
2324 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002325}
2326
2327/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002328 * Give an error message for the result of a function.
2329 * Nothing if "error" is FCERR_NONE.
2330 */
2331 void
2332user_func_error(int error, char_u *name)
2333{
2334 switch (error)
2335 {
2336 case FCERR_UNKNOWN:
2337 emsg_funcname(e_unknownfunc, name);
2338 break;
2339 case FCERR_NOTMETHOD:
2340 emsg_funcname(
2341 N_("E276: Cannot use function as a method: %s"), name);
2342 break;
2343 case FCERR_DELETED:
2344 emsg_funcname(N_(e_func_deleted), name);
2345 break;
2346 case FCERR_TOOMANY:
2347 emsg_funcname((char *)e_toomanyarg, name);
2348 break;
2349 case FCERR_TOOFEW:
2350 emsg_funcname((char *)e_toofewarg, name);
2351 break;
2352 case FCERR_SCRIPT:
2353 emsg_funcname(
2354 N_("E120: Using <SID> not in a script context: %s"), name);
2355 break;
2356 case FCERR_DICT:
2357 emsg_funcname(
2358 N_("E725: Calling dict function without Dictionary: %s"),
2359 name);
2360 break;
2361 }
2362}
2363
2364/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002365 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002366 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002367 * Return FAIL when the function can't be called, OK otherwise.
2368 * Also returns OK when an error was encountered while executing the function.
2369 */
2370 int
2371call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02002372 char_u *funcname, // name of the function
2373 int len, // length of "name" or -1 to use strlen()
2374 typval_T *rettv, // return value goes here
2375 int argcount_in, // number of "argvars"
2376 typval_T *argvars_in, // vars for arguments, must have "argcount"
2377 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002378 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002379{
2380 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01002381 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002382 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002383 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002384 char_u fname_buf[FLEN_FIXED + 1];
2385 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002386 char_u *fname = NULL;
2387 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002388 int argcount = argcount_in;
2389 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002390 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002391 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
2392 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002393 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002394 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002395 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002396
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02002397 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
2398 // even when call_func() returns FAIL.
2399 rettv->v_type = VAR_UNKNOWN;
2400
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002401 if (partial != NULL)
2402 fp = partial->pt_func;
2403 if (fp == NULL)
2404 {
2405 // Make a copy of the name, if it comes from a funcref variable it
2406 // could be changed or deleted in the called function.
2407 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
2408 if (name == NULL)
2409 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002410
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002411 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
2412 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002413
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002414 if (funcexe->doesrange != NULL)
2415 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002416
2417 if (partial != NULL)
2418 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002419 // When the function has a partial with a dict and there is a dict
2420 // argument, use the dict argument. That is backwards compatible.
2421 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002422 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002423 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01002424 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002425 {
2426 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002427 {
2428 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
2429 {
Bram Moolenaaref140542019-12-31 21:27:13 +01002430 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002431 goto theend;
2432 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002433 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002434 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002435 for (i = 0; i < argcount_in; ++i)
2436 argv[i + argv_clear] = argvars_in[i];
2437 argvars = argv;
2438 argcount = partial->pt_argc + argcount_in;
2439 }
2440 }
2441
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002442 if (error == FCERR_NONE && funcexe->check_type != NULL && funcexe->evaluate)
2443 {
2444 // Check that the argument types are OK for the types of the funcref.
2445 if (check_argument_types(funcexe->check_type, argvars, argcount,
2446 name) == FAIL)
2447 error = FCERR_OTHER;
2448 }
2449
Bram Moolenaaref140542019-12-31 21:27:13 +01002450 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002451 {
2452 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02002453 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002454
Bram Moolenaar333894b2020-08-01 18:53:07 +02002455 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002456 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02002457 {
2458 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002459 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02002460 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002461
Bram Moolenaare38eab22019-12-05 21:50:01 +01002462 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002463 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01002464 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002465
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002466 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002467 {
2468 /*
2469 * User defined function.
2470 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002471 if (fp == NULL)
Bram Moolenaar333894b2020-08-01 18:53:07 +02002472 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002473
Bram Moolenaare38eab22019-12-05 21:50:01 +01002474 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002475 if (fp == NULL
2476 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002477 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002478 && !aborting())
2479 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002480 // executed an autocommand, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02002481 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002482 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002483 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002484 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
2485 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002486 // loaded a package, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02002487 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002488 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02002489 if (fp == NULL)
2490 {
2491 char_u *p = untrans_function_name(rfname);
2492
2493 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02002494 // Don't do this if the name starts with "s:".
2495 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaar333894b2020-08-01 18:53:07 +02002496 fp = find_func(p, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002497 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002498
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002499 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01002500 error = FCERR_DELETED;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002501#ifdef FEAT_LUA
2502 else if (fp != NULL && (fp->uf_flags & FC_CFUNC))
2503 {
2504 cfunc_T cb = fp->uf_cb;
2505
2506 error = (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
2507 }
2508#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002509 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002510 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002511 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01002512 // postponed filling in the arguments, do it now
2513 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002514 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002515
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002516 if (funcexe->basetv != NULL)
2517 {
2518 // Method call: base->Method()
2519 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
2520 argv[0] = *funcexe->basetv;
2521 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002522 argvars = argv;
2523 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002524 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002525
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526 error = call_user_func_check(fp, argcount, argvars, rettv,
2527 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002528 }
2529 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002530 else if (funcexe->basetv != NULL)
2531 {
2532 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002533 * expr->method(): Find the method name in the table, call its
2534 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02002535 */
2536 error = call_internal_method(fname, argcount, argvars, rettv,
2537 funcexe->basetv);
2538 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002539 else
2540 {
2541 /*
2542 * Find the function name in the table, call its implementation.
2543 */
2544 error = call_internal_func(fname, argcount, argvars, rettv);
2545 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02002546
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002547 /*
2548 * The function call (or "FuncUndefined" autocommand sequence) might
2549 * have been aborted by an error, an interrupt, or an explicitly thrown
2550 * exception that has not been caught so far. This situation can be
2551 * tested for by calling aborting(). For an error in an internal
2552 * function or for the "E132" error in call_user_func(), however, the
2553 * throw point at which the "force_abort" flag (temporarily reset by
2554 * emsg()) is normally updated has not been reached yet. We need to
2555 * update that flag first to make aborting() reliable.
2556 */
2557 update_force_abort();
2558 }
Bram Moolenaaref140542019-12-31 21:27:13 +01002559 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002560 ret = OK;
2561
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002562theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002563 /*
2564 * Report an error unless the argument evaluation or function call has been
2565 * cancelled due to an aborting error, an interrupt, or an exception.
2566 */
2567 if (!aborting())
2568 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002569 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002570 }
2571
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002572 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002573 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002574 clear_tv(&argv[--argv_clear + argv_base]);
2575
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002576 vim_free(tofree);
2577 vim_free(name);
2578
2579 return ret;
2580}
2581
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002582 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583printable_func_name(ufunc_T *fp)
2584{
2585 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
2586}
2587
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002588/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002589 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002590 */
2591 static void
2592list_func_head(ufunc_T *fp, int indent)
2593{
2594 int j;
2595
2596 msg_start();
2597 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002598 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002599 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002600 msg_puts("def ");
2601 else
2602 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002604 msg_putchar('(');
2605 for (j = 0; j < fp->uf_args.ga_len; ++j)
2606 {
2607 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002608 msg_puts(", ");
2609 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610 if (fp->uf_arg_types != NULL)
2611 {
2612 char *tofree;
2613
2614 msg_puts(": ");
2615 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
2616 vim_free(tofree);
2617 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002618 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
2619 {
2620 msg_puts(" = ");
2621 msg_puts(((char **)(fp->uf_def_args.ga_data))
2622 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
2623 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002624 }
2625 if (fp->uf_varargs)
2626 {
2627 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002628 msg_puts(", ");
2629 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002630 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631 if (fp->uf_va_name != NULL)
2632 {
2633 if (j)
2634 msg_puts(", ");
2635 msg_puts("...");
2636 msg_puts((char *)fp->uf_va_name);
2637 if (fp->uf_va_type)
2638 {
2639 char *tofree;
2640
2641 msg_puts(": ");
2642 msg_puts(type_name(fp->uf_va_type, &tofree));
2643 vim_free(tofree);
2644 }
2645 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002646 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002647
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002648 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002649 {
2650 if (fp->uf_ret_type != &t_void)
2651 {
2652 char *tofree;
2653
2654 msg_puts(": ");
2655 msg_puts(type_name(fp->uf_ret_type, &tofree));
2656 vim_free(tofree);
2657 }
2658 }
2659 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002660 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002661 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002662 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002663 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002664 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002665 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002666 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002667 msg_clr_eos();
2668 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002669 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002670}
2671
2672/*
2673 * Get a function name, translating "<SID>" and "<SNR>".
2674 * Also handles a Funcref in a List or Dictionary.
2675 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002676 * Set "*is_global" to TRUE when the function must be global, unless
2677 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002678 * flags:
2679 * TFN_INT: internal function name OK
2680 * TFN_QUIET: be quiet
2681 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002682 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002683 * Advances "pp" to just after the function name (if no error).
2684 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002685 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002686trans_function_name(
2687 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002688 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002689 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002690 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002691 funcdict_T *fdp, // return: info about dictionary used
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002692 partial_T **partial, // return: partial of a FuncRef
2693 type_T **type) // return: type of funcref if not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002694{
2695 char_u *name = NULL;
2696 char_u *start;
2697 char_u *end;
2698 int lead;
2699 char_u sid_buf[20];
2700 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002702 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703 int vim9script;
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002704 static char *e_function_name = N_("E129: Function name required");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002705
2706 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02002707 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002708 start = *pp;
2709
Bram Moolenaare38eab22019-12-05 21:50:01 +01002710 // Check for hard coded <SNR>: already translated function ID (from a user
2711 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002712 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2713 && (*pp)[2] == (int)KE_SNR)
2714 {
2715 *pp += 3;
2716 len = get_id_len(pp) + 3;
2717 return vim_strnsave(start, len);
2718 }
2719
Bram Moolenaare38eab22019-12-05 21:50:01 +01002720 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2721 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002722 lead = eval_fname_script(start);
2723 if (lead > 2)
2724 start += lead;
2725
Bram Moolenaare38eab22019-12-05 21:50:01 +01002726 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002727 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002728 lead > 2 ? 0 : FNE_CHECK_START);
2729 if (end == start)
2730 {
2731 if (!skip)
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002732 emsg(_(e_function_name));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002733 goto theend;
2734 }
2735 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2736 {
2737 /*
2738 * Report an invalid expression in braces, unless the expression
2739 * evaluation has been cancelled due to an aborting error, an
2740 * interrupt, or an exception.
2741 */
2742 if (!aborting())
2743 {
2744 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002745 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002746 }
2747 else
2748 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2749 goto theend;
2750 }
2751
2752 if (lv.ll_tv != NULL)
2753 {
2754 if (fdp != NULL)
2755 {
2756 fdp->fd_dict = lv.ll_dict;
2757 fdp->fd_newkey = lv.ll_newkey;
2758 lv.ll_newkey = NULL;
2759 fdp->fd_di = lv.ll_di;
2760 }
2761 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2762 {
2763 name = vim_strsave(lv.ll_tv->vval.v_string);
2764 *pp = end;
2765 }
2766 else if (lv.ll_tv->v_type == VAR_PARTIAL
2767 && lv.ll_tv->vval.v_partial != NULL)
2768 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002769 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002770 *pp = end;
2771 if (partial != NULL)
2772 *partial = lv.ll_tv->vval.v_partial;
2773 }
2774 else
2775 {
2776 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2777 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002778 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002779 else
2780 *pp = end;
2781 name = NULL;
2782 }
2783 goto theend;
2784 }
2785
2786 if (lv.ll_name == NULL)
2787 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002788 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002789 *pp = end;
2790 goto theend;
2791 }
2792
Bram Moolenaare38eab22019-12-05 21:50:01 +01002793 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002794 if (lv.ll_exp_name != NULL)
2795 {
2796 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002797 name = deref_func_name(lv.ll_exp_name, &len, partial, type,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002798 flags & TFN_NO_AUTOLOAD);
2799 if (name == lv.ll_exp_name)
2800 name = NULL;
2801 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002802 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002803 {
2804 len = (int)(end - *pp);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002805 name = deref_func_name(*pp, &len, partial, type,
2806 flags & TFN_NO_AUTOLOAD);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002807 if (name == *pp)
2808 name = NULL;
2809 }
2810 if (name != NULL)
2811 {
2812 name = vim_strsave(name);
2813 *pp = end;
2814 if (STRNCMP(name, "<SNR>", 5) == 0)
2815 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002816 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002817 name[0] = K_SPECIAL;
2818 name[1] = KS_EXTRA;
2819 name[2] = (int)KE_SNR;
2820 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2821 }
2822 goto theend;
2823 }
2824
2825 if (lv.ll_exp_name != NULL)
2826 {
2827 len = (int)STRLEN(lv.ll_exp_name);
2828 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2829 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2830 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002831 // When there was "s:" already or the name expanded to get a
2832 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002833 lv.ll_name += 2;
2834 len -= 2;
2835 lead = 2;
2836 }
2837 }
2838 else
2839 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002840 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002841 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002842 {
2843 if (is_global != NULL && lv.ll_name[0] == 'g')
2844 *is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002845 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002846 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002847 len = (int)(end - lv.ll_name);
2848 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002849 if (len <= 0)
2850 {
2851 if (!skip)
2852 emsg(_(e_function_name));
2853 goto theend;
2854 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002855
Bram Moolenaar17f700a2020-12-19 21:23:42 +01002856 // In Vim9 script a user function is script-local by default, unless it
2857 // starts with a lower case character: dict.func().
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002858 vim9script = ASCII_ISUPPER(*start) && in_vim9script();
Bram Moolenaar17f700a2020-12-19 21:23:42 +01002859 if (vim9script)
2860 {
2861 char_u *p;
2862
2863 // SomeScript#func() is a global function.
2864 for (p = start; *p != NUL && *p != '('; ++p)
2865 if (*p == AUTOLOAD_CHAR)
2866 vim9script = FALSE;
2867 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002869 /*
2870 * Copy the function name to allocated memory.
2871 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2872 * Accept <SNR>123_name() outside a script.
2873 */
2874 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002875 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002877 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878 if (!vim9script)
2879 lead = 3;
2880 if (vim9script || (lv.ll_exp_name != NULL
2881 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002882 || eval_fname_sid(*pp))
2883 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002885 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002886 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002887 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002888 goto theend;
2889 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002890 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 if (vim9script)
2892 extra = 3 + (int)STRLEN(sid_buf);
2893 else
2894 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002895 }
2896 }
2897 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2898 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002899 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002900 start);
2901 goto theend;
2902 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002903 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002904 {
2905 char_u *cp = vim_strchr(lv.ll_name, ':');
2906
2907 if (cp != NULL && cp < end)
2908 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002909 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002910 goto theend;
2911 }
2912 }
2913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002915 if (name != NULL)
2916 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002917 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002918 {
2919 name[0] = K_SPECIAL;
2920 name[1] = KS_EXTRA;
2921 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002923 STRCPY(name + 3, sid_buf);
2924 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2926 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002927 }
2928 *pp = end;
2929
2930theend:
2931 clear_lval(&lv);
2932 return name;
2933}
2934
2935/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02002936 * Assuming "name" is the result of trans_function_name() and it was prefixed
2937 * to use the script-local name, return the unmodified name (points into
2938 * "name"). Otherwise return NULL.
2939 * This can be used to first search for a script-local function and fall back
2940 * to the global function if not found.
2941 */
2942 char_u *
2943untrans_function_name(char_u *name)
2944{
2945 char_u *p;
2946
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002947 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02002948 {
2949 p = vim_strchr(name, '_');
2950 if (p != NULL)
2951 return p + 1;
2952 }
2953 return NULL;
2954}
2955
2956/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002957 * List functions. When "regmatch" is NULL all of then.
2958 * Otherwise functions matching "regmatch".
2959 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002960 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002961list_functions(regmatch_T *regmatch)
2962{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002963 int changed = func_hashtab.ht_changed;
2964 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002965 hashitem_T *hi;
2966
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002967 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002968 {
2969 if (!HASHITEM_EMPTY(hi))
2970 {
2971 ufunc_T *fp = HI2UF(hi);
2972
2973 --todo;
2974 if ((fp->uf_flags & FC_DEAD) == 0
2975 && (regmatch == NULL
2976 ? !message_filtered(fp->uf_name)
2977 && !func_name_refcount(fp->uf_name)
2978 : !isdigit(*fp->uf_name)
2979 && vim_regexec(regmatch, fp->uf_name, 0)))
2980 {
2981 list_func_head(fp, FALSE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002982 if (changed != func_hashtab.ht_changed)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002983 {
2984 emsg(_("E454: function list was modified"));
2985 return;
2986 }
2987 }
2988 }
2989 }
2990}
2991
2992/*
Bram Moolenaaradc8e442020-12-31 18:28:18 +01002993 * Check if "*cmd" points to a function command and if so advance "*cmd" and
2994 * return TRUE.
2995 * Otherwise return FALSE;
2996 * Do not consider "function(" to be a command.
2997 */
2998 static int
2999is_function_cmd(char_u **cmd)
3000{
3001 char_u *p = *cmd;
3002
3003 if (checkforcmd(&p, "function", 2))
3004 {
3005 if (*p == '(')
3006 return FALSE;
3007 *cmd = p;
3008 return TRUE;
3009 }
3010 return FALSE;
3011}
3012
3013/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02003014 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003015 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
3016 * the function name.
Bram Moolenaar04b12692020-05-04 23:24:44 +02003017 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003018 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02003019 ufunc_T *
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003020define_function(exarg_T *eap, char_u *name_arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003021{
3022 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02003023 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003024 int j;
3025 int c;
3026 int saved_did_emsg;
3027 int saved_wait_return = need_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003028 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003029 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003030 char_u *p;
3031 char_u *arg;
Bram Moolenaarcef12702021-01-04 14:09:43 +01003032 char_u *whitep;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003033 char_u *line_arg = NULL;
3034 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003036 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003037 garray_T newlines;
3038 int varargs = FALSE;
3039 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003041 ufunc_T *fp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003042 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003043 int indent;
3044 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045#define MAX_FUNC_NESTING 50
3046 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003047 dictitem_T *v;
3048 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003049 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003050 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003051 hashitem_T *hi;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01003052 getline_opt_T getline_options;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003053 linenr_T sourcing_lnum_off;
3054 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003055 int is_heredoc = FALSE;
3056 char_u *skip_until = NULL;
3057 char_u *heredoc_trimmed = NULL;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003058 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02003059 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003060
3061 /*
3062 * ":function" without argument: list functions.
3063 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02003064 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003065 {
3066 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003067 list_functions(NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003068 eap->nextcmd = check_nextcmd(eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003069 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003070 }
3071
3072 /*
3073 * ":function /pat": list functions matching pattern.
3074 */
3075 if (*eap->arg == '/')
3076 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003077 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003078 if (!eap->skip)
3079 {
3080 regmatch_T regmatch;
3081
3082 c = *p;
3083 *p = NUL;
3084 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
3085 *p = c;
3086 if (regmatch.regprog != NULL)
3087 {
3088 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003089 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003090 vim_regfree(regmatch.regprog);
3091 }
3092 }
3093 if (*p == '/')
3094 ++p;
3095 eap->nextcmd = check_nextcmd(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003096 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003097 }
3098
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 ga_init(&newargs);
3100 ga_init(&argtypes);
3101 ga_init(&default_args);
3102
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003103 /*
3104 * Get the function name. There are these situations:
3105 * func normal function name
3106 * "name" == func, "fudi.fd_dict" == NULL
3107 * dict.func new dictionary entry
3108 * "name" == NULL, "fudi.fd_dict" set,
3109 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
3110 * dict.func existing dict entry with a Funcref
3111 * "name" == func, "fudi.fd_dict" set,
3112 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
3113 * dict.func existing dict entry that's not a Funcref
3114 * "name" == NULL, "fudi.fd_dict" set,
3115 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
3116 * s:func script-local function name
3117 * g:func global function name, same as "func"
3118 */
3119 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003120 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003121 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02003122 // nested function, argument is (args).
3123 paren = TRUE;
3124 CLEAR_FIELD(fudi);
3125 }
3126 else
3127 {
Bram Moolenaarb6571982021-01-08 22:24:19 +01003128 if (STRNCMP(p, "<lambda>", 8) == 0)
3129 {
3130 p += 8;
3131 (void)getdigits(&p);
3132 name = vim_strnsave(eap->arg, p - eap->arg);
3133 CLEAR_FIELD(fudi);
3134 }
3135 else
3136 name = trans_function_name(&p, &is_global, eap->skip,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003137 TFN_NO_AUTOLOAD, &fudi, NULL, NULL);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003138 paren = (vim_strchr(p, '(') != NULL);
3139 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003140 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02003141 /*
3142 * Return on an invalid expression in braces, unless the expression
3143 * evaluation has been cancelled due to an aborting error, an
3144 * interrupt, or an exception.
3145 */
3146 if (!aborting())
3147 {
3148 if (!eap->skip && fudi.fd_newkey != NULL)
3149 semsg(_(e_dictkey), fudi.fd_newkey);
3150 vim_free(fudi.fd_newkey);
3151 return NULL;
3152 }
3153 else
3154 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003155 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003156 }
3157
Bram Moolenaare38eab22019-12-05 21:50:01 +01003158 // An error in a function call during evaluation of an expression in magic
3159 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003160 saved_did_emsg = did_emsg;
3161 did_emsg = FALSE;
3162
3163 /*
3164 * ":function func" with only function name: list function.
3165 */
3166 if (!paren)
3167 {
3168 if (!ends_excmd(*skipwhite(p)))
3169 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003170 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003171 goto ret_free;
3172 }
3173 eap->nextcmd = check_nextcmd(p);
3174 if (eap->nextcmd != NULL)
3175 *p = NUL;
3176 if (!eap->skip && !got_int)
3177 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003178 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003179 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
3180 {
3181 char_u *up = untrans_function_name(name);
3182
3183 // With Vim9 script the name was made script-local, if not
3184 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02003185 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003186 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003187 }
3188
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003189 if (fp != NULL)
3190 {
3191 list_func_head(fp, TRUE);
3192 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
3193 {
3194 if (FUNCLINE(fp, j) == NULL)
3195 continue;
3196 msg_putchar('\n');
3197 msg_outnum((long)(j + 1));
3198 if (j < 9)
3199 msg_putchar(' ');
3200 if (j < 99)
3201 msg_putchar(' ');
3202 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003203 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003204 ui_breakcheck();
3205 }
3206 if (!got_int)
3207 {
3208 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003209 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 msg_puts(" enddef");
3211 else
3212 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003213 }
3214 }
3215 else
Bram Moolenaara26b9702020-04-18 19:53:28 +02003216 emsg_funcname(N_("E123: Undefined function: %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003217 }
3218 goto ret_free;
3219 }
3220
3221 /*
3222 * ":function name(arg1, arg2)" Define function.
3223 */
3224 p = skipwhite(p);
3225 if (*p != '(')
3226 {
3227 if (!eap->skip)
3228 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003229 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003230 goto ret_free;
3231 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003232 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003233 if (vim_strchr(p, '(') != NULL)
3234 p = vim_strchr(p, '(');
3235 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003236
Bram Moolenaar4efd9942021-01-24 21:14:20 +01003237 if ((vim9script || eap->cmdidx == CMD_def) && VIM_ISWHITE(p[-1]))
3238 {
Bram Moolenaarba98fb52021-02-07 18:06:29 +01003239 semsg(_(e_no_white_space_allowed_before_str_str), "(", p - 1);
Bram Moolenaar4efd9942021-01-24 21:14:20 +01003240 goto ret_free;
3241 }
3242
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02003243 // In Vim9 script only global functions can be redefined.
3244 if (vim9script && eap->forceit && !is_global)
3245 {
3246 emsg(_(e_nobang));
3247 goto ret_free;
3248 }
3249
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003250 ga_init2(&newlines, (int)sizeof(char_u *), 3);
3251
Bram Moolenaar04b12692020-05-04 23:24:44 +02003252 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003253 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003254 // Check the name of the function. Unless it's a dictionary function
3255 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003256 if (name != NULL)
3257 arg = name;
3258 else
3259 arg = fudi.fd_newkey;
3260 if (arg != NULL && (fudi.fd_di == NULL
3261 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
3262 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
3263 {
3264 if (*arg == K_SPECIAL)
3265 j = 3;
3266 else
3267 j = 0;
3268 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
3269 : eval_isnamec(arg[j])))
3270 ++j;
3271 if (arg[j] != NUL)
3272 emsg_funcname((char *)e_invarg2, arg);
3273 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003274 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003275 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003276 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003277 }
3278
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003279 // This may get more lines and make the pointers into the first line
3280 // invalid.
Bram Moolenaarcef12702021-01-04 14:09:43 +01003281 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003283 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar057e84a2021-02-28 16:55:11 +01003284 NULL, &varargs, &default_args, eap->skip,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003285 eap, &line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003286 goto errret_2;
Bram Moolenaarcef12702021-01-04 14:09:43 +01003287 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003288
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003290 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 // find the return type: :def Func(): type
3292 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003293 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02003295 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003297 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003298 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaarcef12702021-01-04 14:09:43 +01003299 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003301 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003303 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003304 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02003305 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003306 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003307 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02003308 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003309 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 else
3311 // find extra arguments "range", "dict", "abort" and "closure"
3312 for (;;)
3313 {
Bram Moolenaarcef12702021-01-04 14:09:43 +01003314 whitep = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003315 p = skipwhite(p);
3316 if (STRNCMP(p, "range", 5) == 0)
3317 {
3318 flags |= FC_RANGE;
3319 p += 5;
3320 }
3321 else if (STRNCMP(p, "dict", 4) == 0)
3322 {
3323 flags |= FC_DICT;
3324 p += 4;
3325 }
3326 else if (STRNCMP(p, "abort", 5) == 0)
3327 {
3328 flags |= FC_ABORT;
3329 p += 5;
3330 }
3331 else if (STRNCMP(p, "closure", 7) == 0)
3332 {
3333 flags |= FC_CLOSURE;
3334 p += 7;
3335 if (current_funccal == NULL)
3336 {
3337 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
3338 name == NULL ? (char_u *)"" : name);
3339 goto erret;
3340 }
3341 }
3342 else
3343 break;
3344 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003345
Bram Moolenaare38eab22019-12-05 21:50:01 +01003346 // When there is a line break use what follows for the function body.
3347 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003348 if (*p == '\n')
3349 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003350 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02003351 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
3352 && eap->cmdidx != CMD_def)
Bram Moolenaarcef12702021-01-04 14:09:43 +01003353 && !(VIM_ISWHITE(*whitep) && *p == '#'
3354 && (vim9script || eap->cmdidx == CMD_def))
Bram Moolenaare7e48382020-07-22 18:17:08 +02003355 && !eap->skip
3356 && !did_emsg)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003357 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003358
3359 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
3361 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003362 */
3363 if (KeyTyped)
3364 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003365 // Check if the function already exists, don't let the user type the
3366 // whole function before telling him it doesn't work! For a script we
3367 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003368 if (!eap->skip && !eap->forceit)
3369 {
3370 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003371 emsg(_(e_funcdict));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003372 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003373 emsg_funcname(e_funcexts, name);
3374 }
3375
3376 if (!eap->skip && did_emsg)
3377 goto erret;
3378
Bram Moolenaare38eab22019-12-05 21:50:01 +01003379 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003380 cmdline_row = msg_row;
3381 }
3382
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003383 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003384 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003385
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02003386 // Detect having skipped over comment lines to find the return
3387 // type. Add NULL lines to keep the line count correct.
3388 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
3389 if (SOURCING_LNUM < sourcing_lnum_off)
3390 {
3391 sourcing_lnum_off -= SOURCING_LNUM;
3392 if (ga_grow(&newlines, sourcing_lnum_off) == FAIL)
3393 goto erret;
3394 while (sourcing_lnum_off-- > 0)
3395 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3396 }
3397
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003398 indent = 2;
3399 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01003401 getline_options = eap->cmdidx == CMD_def
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01003402 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003403 for (;;)
3404 {
3405 if (KeyTyped)
3406 {
3407 msg_scroll = TRUE;
3408 saved_wait_return = FALSE;
3409 }
3410 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003411
3412 if (line_arg != NULL)
3413 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003414 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003415 theline = line_arg;
3416 p = vim_strchr(theline, '\n');
3417 if (p == NULL)
3418 line_arg += STRLEN(line_arg);
3419 else
3420 {
3421 *p = NUL;
3422 line_arg = p + 1;
3423 }
3424 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003425 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02003426 {
3427 vim_free(line_to_free);
3428 if (eap->getline == NULL)
Bram Moolenaar66250c92020-08-20 15:02:42 +02003429 theline = getcmdline(':', 0L, indent, getline_options);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003430 else
Bram Moolenaar66250c92020-08-20 15:02:42 +02003431 theline = eap->getline(':', eap->cookie, indent,
3432 getline_options);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003433 line_to_free = theline;
3434 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003435 if (KeyTyped)
3436 lines_left = Rows - 1;
3437 if (theline == NULL)
3438 {
Bram Moolenaarb8ba9b92021-01-01 18:54:34 +01003439 // Use the start of the function for the line number.
3440 SOURCING_LNUM = sourcing_lnum_top;
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003441 if (skip_until != NULL)
3442 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
3443 else if (eap->cmdidx == CMD_def)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003444 emsg(_(e_missing_enddef));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 else
3446 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003447 goto erret;
3448 }
3449
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003450 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003451 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003452 if (SOURCING_LNUM < sourcing_lnum_off)
3453 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003454 else
3455 sourcing_lnum_off = 0;
3456
3457 if (skip_until != NULL)
3458 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003460 // * ":append" and "."
3461 // * ":python <<EOF" and "EOF"
3462 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
3463 if (heredoc_trimmed == NULL
3464 || (is_heredoc && skipwhite(theline) == theline)
3465 || STRNCMP(theline, heredoc_trimmed,
3466 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003467 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003468 if (heredoc_trimmed == NULL)
3469 p = theline;
3470 else if (is_heredoc)
3471 p = skipwhite(theline) == theline
3472 ? theline : theline + STRLEN(heredoc_trimmed);
3473 else
3474 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02003475 if (STRCMP(p, skip_until) == 0)
3476 {
3477 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003478 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01003479 getline_options = eap->cmdidx == CMD_def
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01003480 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003481 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003482 }
3483 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003484 }
3485 else
3486 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003487 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01003488 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003489 ;
3490
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 // Check for "endfunction" or "enddef".
Bram Moolenaar832ea892021-01-08 21:55:26 +01003492 // When a ":" follows it must be a dict key; "enddef: value,"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 if (checkforcmd(&p, nesting_def[nesting]
Bram Moolenaar832ea892021-01-08 21:55:26 +01003494 ? "enddef" : "endfunction", 4)
3495 && *p != ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003496 {
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003497 if (nesting-- == 0)
Bram Moolenaar53564f72017-06-24 14:48:11 +02003498 {
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003499 char_u *nextcmd = NULL;
3500
3501 if (*p == '|')
3502 nextcmd = p + 1;
3503 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
3504 nextcmd = line_arg;
3505 else if (*p != NUL && *p != '"' && p_verbose > 0)
3506 give_warning2(eap->cmdidx == CMD_def
3507 ? (char_u *)_("W1001: Text found after :enddef: %s")
3508 : (char_u *)_("W22: Text found after :endfunction: %s"),
3509 p, TRUE);
3510 if (nextcmd != NULL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02003511 {
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003512 // Another command follows. If the line came from "eap"
3513 // we can simply point into it, otherwise we need to
3514 // change "eap->cmdlinep".
3515 eap->nextcmd = nextcmd;
3516 if (line_to_free != NULL)
3517 {
3518 vim_free(*eap->cmdlinep);
3519 *eap->cmdlinep = line_to_free;
3520 line_to_free = NULL;
3521 }
Bram Moolenaar53564f72017-06-24 14:48:11 +02003522 }
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003523 break;
Bram Moolenaar53564f72017-06-24 14:48:11 +02003524 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003525 }
3526
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003527 // Check for mismatched "endfunc" or "enddef".
3528 // We don't check for "def" inside "func" thus we also can't check
3529 // for "enddef".
3530 // We continue to find the end of the function, although we might
3531 // not find it.
3532 else if (nesting_def[nesting])
3533 {
Bram Moolenaar832ea892021-01-08 21:55:26 +01003534 if (checkforcmd(&p, "endfunction", 4) && *p != ':')
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003535 emsg(_(e_mismatched_endfunction));
3536 }
3537 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
3538 emsg(_(e_mismatched_enddef));
3539
Bram Moolenaare38eab22019-12-05 21:50:01 +01003540 // Increase indent inside "if", "while", "for" and "try", decrease
3541 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003543 indent -= 2;
3544 else if (STRNCMP(p, "if", 2) == 0
3545 || STRNCMP(p, "wh", 2) == 0
3546 || STRNCMP(p, "for", 3) == 0
3547 || STRNCMP(p, "try", 3) == 0)
3548 indent += 2;
3549
Bram Moolenaare38eab22019-12-05 21:50:01 +01003550 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01003551 // Only recognize "def" inside "def", not inside "function",
3552 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 c = *p;
Bram Moolenaaradc8e442020-12-31 18:28:18 +01003554 if (is_function_cmd(&p)
Bram Moolenaar673660a2020-01-26 16:50:05 +01003555 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003556 {
3557 if (*p == '!')
3558 p = skipwhite(p + 1);
3559 p += eval_fname_script(p);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003560 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL,
3561 NULL, NULL));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003562 if (*skipwhite(p) == '(')
3563 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 if (nesting == MAX_FUNC_NESTING - 1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003565 emsg(_(e_function_nesting_too_deep));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 else
3567 {
3568 ++nesting;
3569 nesting_def[nesting] = (c == 'd');
3570 indent += 2;
3571 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003572 }
3573 }
3574
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003575 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003576 p = skip_range(p, FALSE, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003577 if (eap->cmdidx != CMD_def
3578 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01003579 || (p[0] == 'c'
3580 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
3581 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
3582 && (STRNCMP(&p[3], "nge", 3) != 0
3583 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003584 || (p[0] == 'i'
3585 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003586 && (!ASCII_ISALPHA(p[2])
3587 || (p[2] == 's'
3588 && (!ASCII_ISALPHA(p[3])
3589 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003590 skip_until = vim_strsave((char_u *)".");
3591
Bram Moolenaare38eab22019-12-05 21:50:01 +01003592 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003593 arg = skipwhite(skiptowhite(p));
3594 if (arg[0] == '<' && arg[1] =='<'
3595 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003596 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
3597 || ((p[2] == '3' || p[2] == 'x')
3598 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003599 || (p[0] == 'p' && p[1] == 'e'
3600 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
3601 || (p[0] == 't' && p[1] == 'c'
3602 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
3603 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
3604 && !ASCII_ISALPHA(p[3]))
3605 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
3606 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
3607 || (p[0] == 'm' && p[1] == 'z'
3608 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
3609 ))
3610 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003611 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003612 p = skipwhite(arg + 2);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003613 if (STRNCMP(p, "trim", 4) == 0)
3614 {
3615 // Ignore leading white space.
3616 p = skipwhite(p + 4);
3617 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003618 skipwhite(theline) - theline);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003619 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003620 if (*p == NUL)
3621 skip_until = vim_strsave((char_u *)".");
3622 else
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003623 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003624 getline_options = GETLINE_NONE;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003625 is_heredoc = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003626 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003627
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003628 // Check for ":cmd v =<< [trim] EOF"
3629 // and ":cmd [a, b] =<< [trim] EOF"
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003630 // and "lines =<< [trim] EOF" for Vim9
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003631 // Where "cmd" can be "let", "var", "final" or "const".
Bram Moolenaar8471e572019-05-19 21:37:18 +02003632 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003633 if (*arg == '[')
3634 arg = vim_strchr(arg, ']');
3635 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003636 {
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003637 int found = (eap->cmdidx == CMD_def && arg[0] == '='
3638 && arg[1] == '<' && arg[2] =='<');
3639
3640 if (!found)
3641 // skip over the argument after "cmd"
3642 arg = skipwhite(skiptowhite(arg));
3643 if (found || (arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003644 && (checkforcmd(&p, "let", 2)
3645 || checkforcmd(&p, "var", 3)
3646 || checkforcmd(&p, "final", 5)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003647 || checkforcmd(&p, "const", 5))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02003648 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003649 p = skipwhite(arg + 3);
3650 if (STRNCMP(p, "trim", 4) == 0)
3651 {
3652 // Ignore leading white space.
3653 p = skipwhite(p + 4);
3654 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003655 skipwhite(theline) - theline);
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003656 }
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003657 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003658 getline_options = GETLINE_NONE;
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003659 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003660 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003661 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003662 }
3663
Bram Moolenaare38eab22019-12-05 21:50:01 +01003664 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003665 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003666 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003667
Bram Moolenaare38eab22019-12-05 21:50:01 +01003668 // Copy the line to newly allocated memory. get_one_sourceline()
3669 // allocates 250 bytes per line, this saves 80% on average. The cost
3670 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003671 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003672 if (p == NULL)
3673 goto erret;
3674 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003675
Bram Moolenaare38eab22019-12-05 21:50:01 +01003676 // Add NULL lines for continuation lines, so that the line count is
3677 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003678 while (sourcing_lnum_off-- > 0)
3679 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3680
Bram Moolenaare38eab22019-12-05 21:50:01 +01003681 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003682 if (line_arg != NULL && *line_arg == NUL)
3683 line_arg = NULL;
3684 }
3685
Bram Moolenaare38eab22019-12-05 21:50:01 +01003686 // Don't define the function when skipping commands or when an error was
3687 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003688 if (eap->skip || did_emsg)
3689 goto erret;
3690
3691 /*
3692 * If there are no errors, add the function
3693 */
3694 if (fudi.fd_dict == NULL)
3695 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 hashtab_T *ht;
3697
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003698 v = find_var(name, &ht, TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003699 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
3700 {
3701 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
3702 name);
3703 goto erret;
3704 }
3705
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003706 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaareef21022020-08-01 22:16:43 +02003707 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003708 {
Bram Moolenaareef21022020-08-01 22:16:43 +02003709 char_u *uname = untrans_function_name(name);
3710
3711 import = find_imported(uname == NULL ? name : uname, 0, NULL);
3712 }
3713
3714 if (fp != NULL || import != NULL)
3715 {
3716 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003718 // Function can be replaced with "function!" and when sourcing the
3719 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02003720 // A name that is used by an import can not be overruled.
3721 if (import != NULL
3722 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003723 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02003724 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003725 {
Bram Moolenaareef21022020-08-01 22:16:43 +02003726 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003727 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02003728 else
3729 emsg_funcname(e_funcexts, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003730 goto erret;
3731 }
3732 if (fp->uf_calls > 0)
3733 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003734 emsg_funcname(
3735 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003736 name);
3737 goto erret;
3738 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003739 if (fp->uf_refcount > 1)
3740 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003741 // This function is referenced somewhere, don't redefine it but
3742 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003743 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003744 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003745 fp = NULL;
3746 overwrite = TRUE;
3747 }
3748 else
3749 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003750 char_u *exp_name = fp->uf_name_exp;
3751
3752 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01003753 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003754 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003755 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003756 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003758#ifdef FEAT_PROFILE
3759 fp->uf_profiling = FALSE;
3760 fp->uf_prof_initialized = FALSE;
3761#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01003762 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003763 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003764 }
3765 }
3766 else
3767 {
3768 char numbuf[20];
3769
3770 fp = NULL;
3771 if (fudi.fd_newkey == NULL && !eap->forceit)
3772 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003773 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003774 goto erret;
3775 }
3776 if (fudi.fd_di == NULL)
3777 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003778 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02003779 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003780 goto erret;
3781 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003782 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02003783 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003784 goto erret;
3785
Bram Moolenaare38eab22019-12-05 21:50:01 +01003786 // Give the function a sequential number. Can only be used with a
3787 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003788 vim_free(name);
3789 sprintf(numbuf, "%d", ++func_nr);
3790 name = vim_strsave((char_u *)numbuf);
3791 if (name == NULL)
3792 goto erret;
3793 }
3794
3795 if (fp == NULL)
3796 {
3797 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3798 {
3799 int slen, plen;
3800 char_u *scriptname;
3801
Bram Moolenaare38eab22019-12-05 21:50:01 +01003802 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003803 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003804 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003805 {
3806 scriptname = autoload_name(name);
3807 if (scriptname != NULL)
3808 {
3809 p = vim_strchr(scriptname, '/');
3810 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003811 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003812 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003813 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003814 j = OK;
3815 vim_free(scriptname);
3816 }
3817 }
3818 if (j == FAIL)
3819 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003820 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003821 goto erret;
3822 }
3823 }
3824
Bram Moolenaar47ed5532019-08-08 20:49:14 +02003825 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003826 if (fp == NULL)
3827 goto erret;
3828
3829 if (fudi.fd_dict != NULL)
3830 {
3831 if (fudi.fd_di == NULL)
3832 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003833 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003834 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
3835 if (fudi.fd_di == NULL)
3836 {
3837 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003838 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003839 goto erret;
3840 }
3841 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
3842 {
3843 vim_free(fudi.fd_di);
3844 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003845 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003846 goto erret;
3847 }
3848 }
3849 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003850 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003851 clear_tv(&fudi.fd_di->di_tv);
3852 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003853 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003854
Bram Moolenaare38eab22019-12-05 21:50:01 +01003855 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003856 flags |= FC_DICT;
3857 }
3858
Bram Moolenaare38eab22019-12-05 21:50:01 +01003859 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003860 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003861 if (overwrite)
3862 {
3863 hi = hash_find(&func_hashtab, name);
3864 hi->hi_key = UF2HIKEY(fp);
3865 }
3866 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003867 {
3868 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003869 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003870 goto erret;
3871 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003872 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003873 }
3874 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003875 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003877 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878
3879 if (eap->cmdidx == CMD_def)
3880 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003881 int lnum_save = SOURCING_LNUM;
3882 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003883
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003884 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003885
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003886 // error messages are for the first function line
3887 SOURCING_LNUM = sourcing_lnum_top;
3888
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003889 if (cstack != NULL && cstack->cs_idx >= 0)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003890 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003891 int count = cstack->cs_idx + 1;
3892 int i;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003893
3894 // The block context may be needed for script variables declared in
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003895 // a block that is visible now but not when the function is called
3896 // later.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003897 fp->uf_block_ids = ALLOC_MULT(int, count);
3898 if (fp->uf_block_ids != NULL)
3899 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003900 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003901 sizeof(int) * count);
3902 fp->uf_block_depth = count;
3903 }
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003904
3905 // Set flag in each block to indicate a function was defined. This
3906 // is used to keep the variable when leaving the block, see
3907 // hide_script_var().
3908 for (i = 0; i <= cstack->cs_idx; ++i)
3909 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003910 }
3911
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003912 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003914 SOURCING_LNUM = lnum_save;
3915 goto errret_2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003917 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918
3919 // parse the return type, if any
3920 if (ret_type == NULL)
3921 fp->uf_ret_type = &t_void;
3922 else
3923 {
3924 p = ret_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01003925 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaar648ea762021-01-15 19:04:32 +01003926 if (fp->uf_ret_type == NULL)
3927 {
3928 fp->uf_ret_type = &t_void;
3929 SOURCING_LNUM = lnum_save;
3930 goto erret;
3931 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02003933 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003935 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003936 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003938 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003939 if ((flags & FC_CLOSURE) != 0)
3940 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003941 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003942 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003943 }
3944 else
3945 fp->uf_scoped = NULL;
3946
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003947#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003948 if (prof_def_func())
3949 func_do_profile(fp);
3950#endif
3951 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003952 if (sandbox)
3953 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003954 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003955 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003956 fp->uf_flags = flags;
3957 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003958 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003959 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02003960 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003961 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962 if (is_export)
3963 {
3964 fp->uf_flags |= FC_EXPORT;
3965 // let ex_export() know the export worked.
3966 is_export = FALSE;
3967 }
3968
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003969 if (eap->cmdidx == CMD_def)
3970 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02003971 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
3972 // :func does not use Vim9 script syntax, even in a Vim9 script file
3973 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003974
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003975 goto ret_free;
3976
3977erret:
3978 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003979 ga_clear_strings(&default_args);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01003980 if (fp != NULL)
3981 {
3982 ga_init(&fp->uf_args);
3983 ga_init(&fp->uf_def_args);
3984 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003985errret_2:
3986 ga_clear_strings(&newlines);
Bram Moolenaar31842cd2021-02-12 22:10:21 +01003987 if (fp != NULL)
3988 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003989ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003990 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003991 vim_free(skip_until);
Bram Moolenaarfffdf472020-12-13 21:16:55 +01003992 vim_free(heredoc_trimmed);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003993 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003994 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003995 if (name != name_arg)
3996 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003997 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003998 did_emsg |= saved_did_emsg;
3999 need_wait_return |= saved_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02004000
4001 return fp;
4002}
4003
4004/*
4005 * ":function"
4006 */
4007 void
4008ex_function(exarg_T *eap)
4009{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004010 (void)define_function(eap, NULL);
Bram Moolenaar822ba242020-05-24 23:00:18 +02004011}
4012
4013/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02004014 * :defcompile - compile all :def functions in the current script that need to
Bram Moolenaarb2049902021-01-24 12:53:53 +01004015 * be compiled. Except dead functions. Doesn't do profiling.
Bram Moolenaar822ba242020-05-24 23:00:18 +02004016 */
4017 void
4018ex_defcompile(exarg_T *eap UNUSED)
4019{
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004020 long todo = (long)func_hashtab.ht_used;
4021 int changed = func_hashtab.ht_changed;
Bram Moolenaar822ba242020-05-24 23:00:18 +02004022 hashitem_T *hi;
4023 ufunc_T *ufunc;
4024
4025 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
4026 {
4027 if (!HASHITEM_EMPTY(hi))
4028 {
4029 --todo;
4030 ufunc = HI2UF(hi);
4031 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
Bram Moolenaar96f8f492020-09-09 17:08:51 +02004032 && ufunc->uf_def_status == UF_TO_BE_COMPILED
4033 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004034 {
Bram Moolenaardc167462021-02-20 20:26:16 +01004035 (void)compile_def_function(ufunc, FALSE, FALSE, NULL);
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004036
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004037 if (func_hashtab.ht_changed != changed)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004038 {
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02004039 // a function has been added or removed, need to start over
4040 todo = (long)func_hashtab.ht_used;
4041 changed = func_hashtab.ht_changed;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004042 hi = func_hashtab.ht_array;
Bram Moolenaar285b1892020-05-26 11:37:26 +02004043 --hi;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02004044 }
4045 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02004046 }
4047 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004048}
4049
4050/*
4051 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
4052 * Return 2 if "p" starts with "s:".
4053 * Return 0 otherwise.
4054 */
4055 int
4056eval_fname_script(char_u *p)
4057{
Bram Moolenaare38eab22019-12-05 21:50:01 +01004058 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
4059 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004060 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
4061 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
4062 return 5;
4063 if (p[0] == 's' && p[1] == ':')
4064 return 2;
4065 return 0;
4066}
4067
4068 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004069translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004070{
4071 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02004072 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004073 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074}
4075
4076/*
4077 * Return TRUE when "ufunc" has old-style "..." varargs
4078 * or named varargs "...name: type".
4079 */
4080 int
4081has_varargs(ufunc_T *ufunc)
4082{
4083 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004084}
4085
4086/*
4087 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004088 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004089 */
4090 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004091function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004092{
4093 char_u *nm = name;
4094 char_u *p;
4095 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004096 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004097 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004098
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02004099 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
4100 if (no_deref)
4101 flag |= TFN_NO_DEREF;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004102 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004103 nm = skipwhite(nm);
4104
Bram Moolenaare38eab22019-12-05 21:50:01 +01004105 // Only accept "funcname", "funcname ", "funcname (..." and
4106 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004107 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004108 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004109 vim_free(p);
4110 return n;
4111}
4112
Bram Moolenaar113e1072019-01-20 15:30:40 +01004113#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004114 char_u *
4115get_expanded_name(char_u *name, int check)
4116{
4117 char_u *nm = name;
4118 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004119 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004120
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004121 p = trans_function_name(&nm, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004122 TFN_INT|TFN_QUIET, NULL, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004123
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004124 if (p != NULL && *nm == NUL
4125 && (!check || translated_function_exists(p, is_global)))
4126 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004127
4128 vim_free(p);
4129 return NULL;
4130}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004131#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004132
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004133/*
4134 * Function given to ExpandGeneric() to obtain the list of user defined
4135 * function names.
4136 */
4137 char_u *
4138get_user_func_name(expand_T *xp, int idx)
4139{
4140 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004141 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004142 static hashitem_T *hi;
4143 ufunc_T *fp;
4144
4145 if (idx == 0)
4146 {
4147 done = 0;
4148 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004149 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004150 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004151 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004152 {
4153 if (done++ > 0)
4154 ++hi;
4155 while (HASHITEM_EMPTY(hi))
4156 ++hi;
4157 fp = HI2UF(hi);
4158
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 // don't show dead, dict and lambda functions
4160 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02004161 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004163
4164 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004165 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004166
4167 cat_func_name(IObuff, fp);
4168 if (xp->xp_context != EXPAND_USER_FUNC)
4169 {
4170 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004172 STRCAT(IObuff, ")");
4173 }
4174 return IObuff;
4175 }
4176 return NULL;
4177}
4178
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004179/*
4180 * ":delfunction {name}"
4181 */
4182 void
4183ex_delfunction(exarg_T *eap)
4184{
4185 ufunc_T *fp = NULL;
4186 char_u *p;
4187 char_u *name;
4188 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004189 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004190
4191 p = eap->arg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004192 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi,
4193 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004194 vim_free(fudi.fd_newkey);
4195 if (name == NULL)
4196 {
4197 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004198 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004199 return;
4200 }
4201 if (!ends_excmd(*skipwhite(p)))
4202 {
4203 vim_free(name);
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004204 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004205 return;
4206 }
4207 eap->nextcmd = check_nextcmd(p);
4208 if (eap->nextcmd != NULL)
4209 *p = NUL;
4210
4211 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004212 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004213 vim_free(name);
4214
4215 if (!eap->skip)
4216 {
4217 if (fp == NULL)
4218 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02004219 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004220 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004221 return;
4222 }
4223 if (fp->uf_calls > 0)
4224 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004225 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004226 return;
4227 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004228 if (fp->uf_flags & FC_VIM9)
4229 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004230 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004231 return;
4232 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004233
4234 if (fudi.fd_dict != NULL)
4235 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004236 // Delete the dict item that refers to the function, it will
4237 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004238 dictitem_remove(fudi.fd_dict, fudi.fd_di);
4239 }
4240 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004241 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004242 // A normal function (not a numbered function or lambda) has a
4243 // refcount of 1 for the entry in the hashtable. When deleting
4244 // it and the refcount is more than one, it should be kept.
4245 // A numbered function and lambda should be kept if the refcount is
4246 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004247 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004248 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004249 // Function is still referenced somewhere. Don't free it but
4250 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004251 if (func_remove(fp))
4252 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004253 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004254 }
4255 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004256 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004257 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004258 }
4259}
4260
4261/*
4262 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004263 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004264 */
4265 void
4266func_unref(char_u *name)
4267{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004268 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004269
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004270 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004271 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004272 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004273 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004274 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004275#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004276 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004277#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01004278 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004279 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004280 func_ptr_unref(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004281}
4282
4283/*
4284 * Unreference a Function: decrement the reference count and free it when it
4285 * becomes zero.
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004286 * Also when it becomes one and uf_partial points to the function.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004287 */
4288 void
4289func_ptr_unref(ufunc_T *fp)
4290{
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01004291 if (fp != NULL && (--fp->uf_refcount <= 0
4292 || (fp->uf_refcount == 1 && fp->uf_partial != NULL
4293 && fp->uf_partial->pt_refcount <= 1
4294 && fp->uf_partial->pt_func == fp)))
Bram Moolenaar97baee82016-07-26 20:46:08 +02004295 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004296 // Only delete it when it's not being used. Otherwise it's done
4297 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02004298 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004299 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004300 }
4301}
4302
4303/*
4304 * Count a reference to a Function.
4305 */
4306 void
4307func_ref(char_u *name)
4308{
4309 ufunc_T *fp;
4310
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004311 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004312 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004313 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004314 if (fp != NULL)
4315 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004316 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01004317 // Only give an error for a numbered function.
4318 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01004319 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004320}
4321
4322/*
4323 * Count a reference to a Function.
4324 */
4325 void
4326func_ptr_ref(ufunc_T *fp)
4327{
4328 if (fp != NULL)
4329 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004330}
4331
4332/*
4333 * Return TRUE if items in "fc" do not have "copyID". That means they are not
4334 * referenced from anywhere that is in use.
4335 */
4336 static int
4337can_free_funccal(funccall_T *fc, int copyID)
4338{
4339 return (fc->l_varlist.lv_copyID != copyID
4340 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004341 && fc->l_avars.dv_copyID != copyID
4342 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004343}
4344
4345/*
4346 * ":return [expr]"
4347 */
4348 void
4349ex_return(exarg_T *eap)
4350{
4351 char_u *arg = eap->arg;
4352 typval_T rettv;
4353 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004354 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004355
4356 if (current_funccal == NULL)
4357 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004358 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004359 return;
4360 }
4361
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004362 CLEAR_FIELD(evalarg);
4363 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
4364
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004365 if (eap->skip)
4366 ++emsg_skip;
4367
4368 eap->nextcmd = NULL;
4369 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004370 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004371 {
4372 if (!eap->skip)
4373 returning = do_return(eap, FALSE, TRUE, &rettv);
4374 else
4375 clear_tv(&rettv);
4376 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004377 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004378 else if (!eap->skip)
4379 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004380 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01004381 update_force_abort();
4382
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004383 /*
4384 * Return unless the expression evaluation has been cancelled due to an
4385 * aborting error, an interrupt, or an exception.
4386 */
4387 if (!aborting())
4388 returning = do_return(eap, FALSE, TRUE, NULL);
4389 }
4390
Bram Moolenaare38eab22019-12-05 21:50:01 +01004391 // When skipping or the return gets pending, advance to the next command
4392 // in this line (!returning). Otherwise, ignore the rest of the line.
4393 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004394 if (returning)
4395 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004396 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004397 eap->nextcmd = check_nextcmd(arg);
4398
4399 if (eap->skip)
4400 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02004401 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004402}
4403
4404/*
4405 * ":1,25call func(arg1, arg2)" function call.
4406 */
4407 void
4408ex_call(exarg_T *eap)
4409{
4410 char_u *arg = eap->arg;
4411 char_u *startarg;
4412 char_u *name;
4413 char_u *tofree;
4414 int len;
4415 typval_T rettv;
4416 linenr_T lnum;
4417 int doesrange;
4418 int failed = FALSE;
4419 funcdict_T fudi;
4420 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004421 evalarg_T evalarg;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004422 type_T *type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004423
Bram Moolenaare6b53242020-07-01 17:28:33 +02004424 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004425 if (eap->skip)
4426 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004427 // trans_function_name() doesn't work well when skipping, use eval0()
4428 // instead to skip to any following command, e.g. for:
4429 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004430 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004431 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004432 clear_tv(&rettv);
4433 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004434 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004435 return;
4436 }
4437
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004438 tofree = trans_function_name(&arg, NULL, eap->skip, TFN_INT,
4439 &fudi, &partial, in_vim9script() ? &type : NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004440 if (fudi.fd_newkey != NULL)
4441 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004442 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004443 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004444 vim_free(fudi.fd_newkey);
4445 }
4446 if (tofree == NULL)
4447 return;
4448
Bram Moolenaare38eab22019-12-05 21:50:01 +01004449 // Increase refcount on dictionary, it could get deleted when evaluating
4450 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004451 if (fudi.fd_dict != NULL)
4452 ++fudi.fd_dict->dv_refcount;
4453
Bram Moolenaare38eab22019-12-05 21:50:01 +01004454 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
4455 // contents. For VAR_PARTIAL get its partial, unless we already have one
4456 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004457 len = (int)STRLEN(tofree);
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004458 name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial,
4459 in_vim9script() && type == NULL ? &type : NULL, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004460
Bram Moolenaare38eab22019-12-05 21:50:01 +01004461 // Skip white space to allow ":call func ()". Not good, but required for
4462 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004463 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004464 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004465
4466 if (*startarg != '(')
4467 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004469 goto end;
4470 }
4471
4472 /*
4473 * When skipping, evaluate the function once, to find the end of the
4474 * arguments.
4475 * When the function takes a range, this is discovered after the first
4476 * call, and the loop is broken.
4477 */
4478 if (eap->skip)
4479 {
4480 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004481 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004482 }
4483 else
4484 lnum = eap->line1;
4485 for ( ; lnum <= eap->line2; ++lnum)
4486 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004487 funcexe_T funcexe;
4488
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004489 if (!eap->skip && eap->addr_count > 0)
4490 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004491 if (lnum > curbuf->b_ml.ml_line_count)
4492 {
4493 // If the function deleted lines or switched to another buffer
4494 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004495 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004496 break;
4497 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004498 curwin->w_cursor.lnum = lnum;
4499 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004500 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004501 }
4502 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004503
Bram Moolenaara80faa82020-04-12 19:37:17 +02004504 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004505 funcexe.firstline = eap->line1;
4506 funcexe.lastline = eap->line2;
4507 funcexe.doesrange = &doesrange;
4508 funcexe.evaluate = !eap->skip;
4509 funcexe.partial = partial;
4510 funcexe.selfdict = fudi.fd_dict;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01004511 funcexe.check_type = type;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004512 if (get_func_tv(name, -1, &rettv, &arg, &evalarg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004513 {
4514 failed = TRUE;
4515 break;
4516 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01004517 if (has_watchexpr())
4518 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004519
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004520 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004521 if (handle_subscript(&arg, &rettv,
4522 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004523 {
4524 failed = TRUE;
4525 break;
4526 }
4527
4528 clear_tv(&rettv);
4529 if (doesrange || eap->skip)
4530 break;
4531
Bram Moolenaare38eab22019-12-05 21:50:01 +01004532 // Stop when immediately aborting on error, or when an interrupt
4533 // occurred or an exception was thrown but not caught.
4534 // get_func_tv() returned OK, so that the check for trailing
4535 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004536 if (aborting())
4537 break;
4538 }
4539 if (eap->skip)
4540 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004541 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004542
Bram Moolenaare51bb172020-02-16 19:42:23 +01004543 // When inside :try we need to check for following "| catch".
4544 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004545 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004546 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02004547 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004548 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004549 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004550 if (!failed)
4551 {
4552 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004553 semsg(_(e_trailing_arg), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004554 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004555 }
4556 else
4557 eap->nextcmd = check_nextcmd(arg);
4558 }
4559
4560end:
4561 dict_unref(fudi.fd_dict);
4562 vim_free(tofree);
4563}
4564
4565/*
4566 * Return from a function. Possibly makes the return pending. Also called
4567 * for a pending return at the ":endtry" or after returning from an extra
4568 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
4569 * when called due to a ":return" command. "rettv" may point to a typval_T
4570 * with the return rettv. Returns TRUE when the return can be carried out,
4571 * FALSE when the return gets pending.
4572 */
4573 int
4574do_return(
4575 exarg_T *eap,
4576 int reanimate,
4577 int is_cmd,
4578 void *rettv)
4579{
4580 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01004581 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004582
4583 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004584 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004585 current_funccal->returned = FALSE;
4586
4587 /*
4588 * Cleanup (and inactivate) conditionals, but stop when a try conditional
4589 * not in its finally clause (which then is to be executed next) is found.
4590 * In this case, make the ":return" pending for execution at the ":endtry".
4591 * Otherwise, return normally.
4592 */
4593 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
4594 if (idx >= 0)
4595 {
4596 cstack->cs_pending[idx] = CSTP_RETURN;
4597
4598 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004599 // A pending return again gets pending. "rettv" points to an
4600 // allocated variable with the rettv of the original ":return"'s
4601 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004602 cstack->cs_rettv[idx] = rettv;
4603 else
4604 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004605 // When undoing a return in order to make it pending, get the stored
4606 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004607 if (reanimate)
4608 rettv = current_funccal->rettv;
4609
4610 if (rettv != NULL)
4611 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004612 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004613 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
4614 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
4615 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004616 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004617 }
4618 else
4619 cstack->cs_rettv[idx] = NULL;
4620
4621 if (reanimate)
4622 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004623 // The pending return value could be overwritten by a ":return"
4624 // without argument in a finally clause; reset the default
4625 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004626 current_funccal->rettv->v_type = VAR_NUMBER;
4627 current_funccal->rettv->vval.v_number = 0;
4628 }
4629 }
4630 report_make_pending(CSTP_RETURN, rettv);
4631 }
4632 else
4633 {
4634 current_funccal->returned = TRUE;
4635
Bram Moolenaare38eab22019-12-05 21:50:01 +01004636 // If the return is carried out now, store the return value. For
4637 // a return immediately after reanimation, the value is already
4638 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004639 if (!reanimate && rettv != NULL)
4640 {
4641 clear_tv(current_funccal->rettv);
4642 *current_funccal->rettv = *(typval_T *)rettv;
4643 if (!is_cmd)
4644 vim_free(rettv);
4645 }
4646 }
4647
4648 return idx < 0;
4649}
4650
4651/*
4652 * Free the variable with a pending return value.
4653 */
4654 void
4655discard_pending_return(void *rettv)
4656{
4657 free_tv((typval_T *)rettv);
4658}
4659
4660/*
4661 * Generate a return command for producing the value of "rettv". The result
4662 * is an allocated string. Used by report_pending() for verbose messages.
4663 */
4664 char_u *
4665get_return_cmd(void *rettv)
4666{
4667 char_u *s = NULL;
4668 char_u *tofree = NULL;
4669 char_u numbuf[NUMBUFLEN];
4670
4671 if (rettv != NULL)
4672 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
4673 if (s == NULL)
4674 s = (char_u *)"";
4675
4676 STRCPY(IObuff, ":return ");
4677 STRNCPY(IObuff + 8, s, IOSIZE - 8);
4678 if (STRLEN(s) + 8 >= IOSIZE)
4679 STRCPY(IObuff + IOSIZE - 4, "...");
4680 vim_free(tofree);
4681 return vim_strsave(IObuff);
4682}
4683
4684/*
4685 * Get next function line.
4686 * Called by do_cmdline() to get the next line.
4687 * Returns allocated string, or NULL for end of function.
4688 */
4689 char_u *
4690get_func_line(
4691 int c UNUSED,
4692 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02004693 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004694 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004695{
4696 funccall_T *fcp = (funccall_T *)cookie;
4697 ufunc_T *fp = fcp->func;
4698 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004699 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004700
Bram Moolenaare38eab22019-12-05 21:50:01 +01004701 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004702 if (fcp->dbg_tick != debug_tick)
4703 {
4704 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004705 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004706 fcp->dbg_tick = debug_tick;
4707 }
4708#ifdef FEAT_PROFILE
4709 if (do_profiling == PROF_YES)
4710 func_line_end(cookie);
4711#endif
4712
4713 gap = &fp->uf_lines;
4714 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4715 || fcp->returned)
4716 retval = NULL;
4717 else
4718 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004719 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004720 while (fcp->linenr < gap->ga_len
4721 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
4722 ++fcp->linenr;
4723 if (fcp->linenr >= gap->ga_len)
4724 retval = NULL;
4725 else
4726 {
4727 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004728 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004729#ifdef FEAT_PROFILE
4730 if (do_profiling == PROF_YES)
Bram Moolenaarb2049902021-01-24 12:53:53 +01004731 func_line_start(cookie, SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004732#endif
4733 }
4734 }
4735
Bram Moolenaare38eab22019-12-05 21:50:01 +01004736 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004737 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004738 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004739 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004740 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004741 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004742 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004743 fcp->dbg_tick = debug_tick;
4744 }
4745
4746 return retval;
4747}
4748
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004749/*
4750 * Return TRUE if the currently active function should be ended, because a
4751 * return was encountered or an error occurred. Used inside a ":while".
4752 */
4753 int
4754func_has_ended(void *cookie)
4755{
4756 funccall_T *fcp = (funccall_T *)cookie;
4757
Bram Moolenaare38eab22019-12-05 21:50:01 +01004758 // Ignore the "abort" flag if the abortion behavior has been changed due to
4759 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004760 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4761 || fcp->returned);
4762}
4763
4764/*
4765 * return TRUE if cookie indicates a function which "abort"s on errors.
4766 */
4767 int
4768func_has_abort(
4769 void *cookie)
4770{
4771 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
4772}
4773
4774
4775/*
4776 * Turn "dict.Func" into a partial for "Func" bound to "dict".
4777 * Don't do this when "Func" is already a partial that was bound
4778 * explicitly (pt_auto is FALSE).
4779 * Changes "rettv" in-place.
4780 * Returns the updated "selfdict_in".
4781 */
4782 dict_T *
4783make_partial(dict_T *selfdict_in, typval_T *rettv)
4784{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004785 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004786 char_u *tofree = NULL;
4787 ufunc_T *fp;
4788 char_u fname_buf[FLEN_FIXED + 1];
4789 int error;
4790 dict_T *selfdict = selfdict_in;
4791
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004792 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
4793 fp = rettv->vval.v_partial->pt_func;
4794 else
4795 {
4796 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
4797 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004798 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004799 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004800 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004801 vim_free(tofree);
4802 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004803
4804 if (fp != NULL && (fp->uf_flags & FC_DICT))
4805 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004806 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004807
4808 if (pt != NULL)
4809 {
4810 pt->pt_refcount = 1;
4811 pt->pt_dict = selfdict;
4812 pt->pt_auto = TRUE;
4813 selfdict = NULL;
4814 if (rettv->v_type == VAR_FUNC)
4815 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004816 // Just a function: Take over the function name and use
4817 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004818 pt->pt_name = rettv->vval.v_string;
4819 }
4820 else
4821 {
4822 partial_T *ret_pt = rettv->vval.v_partial;
4823 int i;
4824
Bram Moolenaare38eab22019-12-05 21:50:01 +01004825 // Partial: copy the function name, use selfdict and copy
4826 // args. Can't take over name or args, the partial might
4827 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004828 if (ret_pt->pt_name != NULL)
4829 {
4830 pt->pt_name = vim_strsave(ret_pt->pt_name);
4831 func_ref(pt->pt_name);
4832 }
4833 else
4834 {
4835 pt->pt_func = ret_pt->pt_func;
4836 func_ptr_ref(pt->pt_func);
4837 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004838 if (ret_pt->pt_argc > 0)
4839 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004840 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004841 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004842 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004843 pt->pt_argc = 0;
4844 else
4845 {
4846 pt->pt_argc = ret_pt->pt_argc;
4847 for (i = 0; i < pt->pt_argc; i++)
4848 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
4849 }
4850 }
4851 partial_unref(ret_pt);
4852 }
4853 rettv->v_type = VAR_PARTIAL;
4854 rettv->vval.v_partial = pt;
4855 }
4856 }
4857 return selfdict;
4858}
4859
4860/*
4861 * Return the name of the executed function.
4862 */
4863 char_u *
4864func_name(void *cookie)
4865{
4866 return ((funccall_T *)cookie)->func->uf_name;
4867}
4868
4869/*
4870 * Return the address holding the next breakpoint line for a funccall cookie.
4871 */
4872 linenr_T *
4873func_breakpoint(void *cookie)
4874{
4875 return &((funccall_T *)cookie)->breakpoint;
4876}
4877
4878/*
4879 * Return the address holding the debug tick for a funccall cookie.
4880 */
4881 int *
4882func_dbg_tick(void *cookie)
4883{
4884 return &((funccall_T *)cookie)->dbg_tick;
4885}
4886
4887/*
4888 * Return the nesting level for a funccall cookie.
4889 */
4890 int
4891func_level(void *cookie)
4892{
4893 return ((funccall_T *)cookie)->level;
4894}
4895
4896/*
4897 * Return TRUE when a function was ended by a ":return" command.
4898 */
4899 int
4900current_func_returned(void)
4901{
4902 return current_funccal->returned;
4903}
4904
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004905 int
4906free_unref_funccal(int copyID, int testing)
4907{
4908 int did_free = FALSE;
4909 int did_free_funccal = FALSE;
4910 funccall_T *fc, **pfc;
4911
4912 for (pfc = &previous_funccal; *pfc != NULL; )
4913 {
4914 if (can_free_funccal(*pfc, copyID))
4915 {
4916 fc = *pfc;
4917 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01004918 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004919 did_free = TRUE;
4920 did_free_funccal = TRUE;
4921 }
4922 else
4923 pfc = &(*pfc)->caller;
4924 }
4925 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004926 // When a funccal was freed some more items might be garbage
4927 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004928 (void)garbage_collect(testing);
4929
4930 return did_free;
4931}
4932
4933/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004934 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004935 */
4936 static funccall_T *
4937get_funccal(void)
4938{
4939 int i;
4940 funccall_T *funccal;
4941 funccall_T *temp_funccal;
4942
4943 funccal = current_funccal;
4944 if (debug_backtrace_level > 0)
4945 {
4946 for (i = 0; i < debug_backtrace_level; i++)
4947 {
4948 temp_funccal = funccal->caller;
4949 if (temp_funccal)
4950 funccal = temp_funccal;
4951 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004952 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004953 debug_backtrace_level = i;
4954 }
4955 }
4956 return funccal;
4957}
4958
4959/*
4960 * Return the hashtable used for local variables in the current funccal.
4961 * Return NULL if there is no current funccal.
4962 */
4963 hashtab_T *
4964get_funccal_local_ht()
4965{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004966 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004967 return NULL;
4968 return &get_funccal()->l_vars.dv_hashtab;
4969}
4970
4971/*
4972 * Return the l: scope variable.
4973 * Return NULL if there is no current funccal.
4974 */
4975 dictitem_T *
4976get_funccal_local_var()
4977{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004978 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004979 return NULL;
4980 return &get_funccal()->l_vars_var;
4981}
4982
4983/*
4984 * Return the hashtable used for argument in the current funccal.
4985 * Return NULL if there is no current funccal.
4986 */
4987 hashtab_T *
4988get_funccal_args_ht()
4989{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004990 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004991 return NULL;
4992 return &get_funccal()->l_avars.dv_hashtab;
4993}
4994
4995/*
4996 * Return the a: scope variable.
4997 * Return NULL if there is no current funccal.
4998 */
4999 dictitem_T *
5000get_funccal_args_var()
5001{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005003 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01005004 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005005}
5006
5007/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005008 * List function variables, if there is a function.
5009 */
5010 void
5011list_func_vars(int *first)
5012{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005014 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01005015 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005016}
5017
5018/*
5019 * If "ht" is the hashtable for local variables in the current funccal, return
5020 * the dict that contains it.
5021 * Otherwise return NULL.
5022 */
5023 dict_T *
5024get_current_funccal_dict(hashtab_T *ht)
5025{
5026 if (current_funccal != NULL
5027 && ht == &current_funccal->l_vars.dv_hashtab)
5028 return &current_funccal->l_vars;
5029 return NULL;
5030}
5031
5032/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005033 * Search hashitem in parent scope.
5034 */
5035 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005036find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005037{
5038 funccall_T *old_current_funccal = current_funccal;
5039 hashtab_T *ht;
5040 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005041 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005042
5043 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
5044 return NULL;
5045
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02005046 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005047 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02005048 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005049 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005050 ht = find_var_ht(name, &varname);
5051 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02005052 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005053 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02005054 if (!HASHITEM_EMPTY(hi))
5055 {
5056 *pht = ht;
5057 break;
5058 }
5059 }
5060 if (current_funccal == current_funccal->func->uf_scoped)
5061 break;
5062 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02005063 }
5064 current_funccal = old_current_funccal;
5065
5066 return hi;
5067}
5068
5069/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005070 * Search variable in parent scope.
5071 */
5072 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005073find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005074{
5075 dictitem_T *v = NULL;
5076 funccall_T *old_current_funccal = current_funccal;
5077 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005078 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005079
5080 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
5081 return NULL;
5082
Bram Moolenaare38eab22019-12-05 21:50:01 +01005083 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005084 current_funccal = current_funccal->func->uf_scoped;
5085 while (current_funccal)
5086 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005087 ht = find_var_ht(name, &varname);
5088 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005089 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02005090 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005091 if (v != NULL)
5092 break;
5093 }
5094 if (current_funccal == current_funccal->func->uf_scoped)
5095 break;
5096 current_funccal = current_funccal->func->uf_scoped;
5097 }
5098 current_funccal = old_current_funccal;
5099
5100 return v;
5101}
5102
5103/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005104 * Set "copyID + 1" in previous_funccal and callers.
5105 */
5106 int
5107set_ref_in_previous_funccal(int copyID)
5108{
5109 int abort = FALSE;
5110 funccall_T *fc;
5111
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005112 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005113 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005114 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005115 abort = abort
5116 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
5117 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005118 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005119 }
5120 return abort;
5121}
5122
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005123 static int
5124set_ref_in_funccal(funccall_T *fc, int copyID)
5125{
5126 int abort = FALSE;
5127
5128 if (fc->fc_copyID != copyID)
5129 {
5130 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005131 abort = abort
5132 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
5133 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005134 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005135 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005136 }
5137 return abort;
5138}
5139
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005140/*
5141 * Set "copyID" in all local vars and arguments in the call stack.
5142 */
5143 int
5144set_ref_in_call_stack(int copyID)
5145{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005146 int abort = FALSE;
5147 funccall_T *fc;
5148 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005149
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005150 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005151 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005152
5153 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005154 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
5155 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005156 abort = abort || set_ref_in_funccal(fc, copyID);
5157
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005158 return abort;
5159}
5160
5161/*
5162 * Set "copyID" in all functions available by name.
5163 */
5164 int
5165set_ref_in_functions(int copyID)
5166{
5167 int todo;
5168 hashitem_T *hi = NULL;
5169 int abort = FALSE;
5170 ufunc_T *fp;
5171
5172 todo = (int)func_hashtab.ht_used;
5173 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005174 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005175 if (!HASHITEM_EMPTY(hi))
5176 {
5177 --todo;
5178 fp = HI2UF(hi);
5179 if (!func_name_refcount(fp->uf_name))
5180 abort = abort || set_ref_in_func(NULL, fp, copyID);
5181 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005182 }
5183 return abort;
5184}
5185
5186/*
5187 * Set "copyID" in all function arguments.
5188 */
5189 int
5190set_ref_in_func_args(int copyID)
5191{
5192 int i;
5193 int abort = FALSE;
5194
5195 for (i = 0; i < funcargs.ga_len; ++i)
5196 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
5197 copyID, NULL, NULL);
5198 return abort;
5199}
5200
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005201/*
5202 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005203 * Returns TRUE if setting references failed somehow.
5204 */
5205 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005206set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005207{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005208 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005209 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01005210 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005211 char_u fname_buf[FLEN_FIXED + 1];
5212 char_u *tofree = NULL;
5213 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005214 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005215
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005216 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005217 return FALSE;
5218
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005219 if (fp_in == NULL)
5220 {
5221 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005222 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005223 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005224 if (fp != NULL)
5225 {
5226 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005227 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005228 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02005229
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005230 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005231 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005232}
5233
Bram Moolenaare38eab22019-12-05 21:50:01 +01005234#endif // FEAT_EVAL