blob: cc41eed7cb9403a48e7018b95e8055556747aa87 [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 Moolenaar8a7d6542020-01-26 15:56:19 +010058 * Return a pointer to after the type.
59 * When something is wrong return "arg".
60 */
61 static char_u *
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +010062one_function_arg(
63 char_u *arg,
64 garray_T *newargs,
65 garray_T *argtypes,
66 int types_optional,
67 int skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010068{
Bram Moolenaar6e949782020-04-13 17:21:00 +020069 char_u *p = arg;
70 char_u *arg_copy = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010071
72 while (ASCII_ISALNUM(*p) || *p == '_')
73 ++p;
74 if (arg == p || isdigit(*arg)
Bram Moolenaarb816dae2020-09-20 22:04:00 +020075 || (argtypes == NULL
76 && ((p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
77 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010078 {
79 if (!skip)
80 semsg(_("E125: Illegal argument: %s"), arg);
81 return arg;
82 }
83 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
84 return arg;
85 if (newargs != NULL)
86 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087 int c;
88 int i;
89
90 c = *p;
91 *p = NUL;
92 arg_copy = vim_strsave(arg);
93 if (arg_copy == NULL)
94 {
95 *p = c;
96 return arg;
97 }
98
99 // Check for duplicate argument name.
100 for (i = 0; i < newargs->ga_len; ++i)
101 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
102 {
103 semsg(_("E853: Duplicate argument name: %s"), arg_copy);
104 vim_free(arg_copy);
105 return arg;
106 }
107 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
108 newargs->ga_len++;
109
110 *p = c;
111 }
112
113 // get any type from "arg: type"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100114 if (argtypes != NULL && (skip || ga_grow(argtypes, 1) == OK))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115 {
116 char_u *type = NULL;
117
Bram Moolenaar6e949782020-04-13 17:21:00 +0200118 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
119 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200120 semsg(_(e_no_white_space_allowed_before_colon_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200121 arg_copy == NULL ? arg : arg_copy);
122 p = skipwhite(p);
123 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100124 if (*p == ':')
125 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200126 ++p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100127 if (!skip && !VIM_ISWHITE(*p))
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200128 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200129 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200130 return arg;
131 }
132 type = skipwhite(p);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +0200133 p = skip_type(type, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100134 if (!skip)
135 type = vim_strnsave(type, p - type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100136 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100137 else if (*skipwhite(p) != '=' && !types_optional)
Bram Moolenaar6e949782020-04-13 17:21:00 +0200138 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200139 semsg(_(e_missing_argument_type_for_str),
Bram Moolenaar6e949782020-04-13 17:21:00 +0200140 arg_copy == NULL ? arg : arg_copy);
141 return arg;
142 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100143 if (!skip)
144 {
145 if (type == NULL && types_optional)
146 // lambda arguments default to "any" type
147 type = vim_strsave((char_u *)"any");
148 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
149 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100150 }
151
152 return p;
153}
154
155/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200156 * Get function arguments.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100157 * "argp" is advanced just after "endchar".
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200158 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100159 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200160get_function_args(
161 char_u **argp,
162 char_u endchar,
163 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 garray_T *argtypes, // NULL unless using :def
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100165 int types_optional, // types optional if "argtypes" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200166 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200167 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200168 int skip,
169 exarg_T *eap,
170 char_u **line_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200171{
172 int mustend = FALSE;
173 char_u *arg = *argp;
174 char_u *p = arg;
175 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200176 int any_default = FALSE;
177 char_u *expr;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200178 char_u *whitep = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200179
180 if (newargs != NULL)
181 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 if (argtypes != NULL)
183 ga_init2(argtypes, (int)sizeof(char_u *), 3);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200184 if (default_args != NULL)
185 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200186
187 if (varargs != NULL)
188 *varargs = FALSE;
189
190 /*
191 * Isolate the arguments: "arg1, arg2, ...)"
192 */
193 while (*p != endchar)
194 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200195 while (eap != NULL && eap->getline != NULL
196 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200197 {
198 char_u *theline;
199
200 // End of the line, get the next one.
201 theline = eap->getline(':', eap->cookie, 0, TRUE);
202 if (theline == NULL)
203 break;
204 vim_free(*line_to_free);
205 *line_to_free = theline;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200206 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200207 p = skipwhite(theline);
208 }
209
210 if (mustend && *p != endchar)
211 {
212 if (!skip)
213 semsg(_(e_invarg2), *argp);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100214 goto err_ret;
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200215 }
216 if (*p == endchar)
217 break;
218
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200219 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
220 {
221 if (varargs != NULL)
222 *varargs = TRUE;
223 p += 3;
224 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100225
226 if (argtypes != NULL)
227 {
228 // ...name: list<type>
Bram Moolenaar28022722020-09-21 22:02:49 +0200229 if (!eval_isnamec1(*p))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100230 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100231 if (!skip)
232 emsg(_(e_missing_name_after_dots));
233 goto err_ret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100234 }
235
236 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100237 p = one_function_arg(p, newargs, argtypes, types_optional,
238 skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239 if (p == arg)
240 break;
241 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200242 }
243 else
244 {
245 arg = p;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100246 p = one_function_arg(p, newargs, argtypes, types_optional, skip);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100247 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200248 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200249
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200250 if (*skipwhite(p) == '=' && default_args != NULL)
251 {
252 typval_T rettv;
253
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100254 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200255 any_default = TRUE;
256 p = skipwhite(p) + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200257 whitep = p;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200258 p = skipwhite(p);
259 expr = p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200260 if (eval1(&p, &rettv, NULL) != FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200261 {
262 if (ga_grow(default_args, 1) == FAIL)
263 goto err_ret;
264
265 // trim trailing whitespace
266 while (p > expr && VIM_ISWHITE(p[-1]))
267 p--;
268 c = *p;
269 *p = NUL;
270 expr = vim_strsave(expr);
271 if (expr == NULL)
272 {
273 *p = c;
274 goto err_ret;
275 }
276 ((char_u **)(default_args->ga_data))
277 [default_args->ga_len] = expr;
278 default_args->ga_len++;
279 *p = c;
280 }
281 else
282 mustend = TRUE;
283 }
284 else if (any_default)
285 {
286 emsg(_("E989: Non-default argument follows default argument"));
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200287 goto err_ret;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200288 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200289 if (*p == ',')
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200290 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200291 ++p;
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200292 // Don't give this error when skipping, it makes the "->" not
293 // found in "{k,v -> x}" and give a confusing error.
294 if (!skip && in_vim9script()
295 && !IS_WHITE_OR_NUL(*p) && *p != endchar)
296 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200297 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar914e7ea2020-07-11 15:20:48 +0200298 goto err_ret;
299 }
300 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200301 else
302 mustend = TRUE;
303 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200304 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200305 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200306 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200307
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200308 if (*p != endchar)
309 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100310 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200311
312 *argp = p;
313 return OK;
314
315err_ret:
316 if (newargs != NULL)
317 ga_clear_strings(newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200318 if (default_args != NULL)
319 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200320 return FAIL;
321}
322
323/*
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100324 * Parse the argument types, filling "fp->uf_arg_types".
325 * Return OK or FAIL.
326 */
327 static int
328parse_argument_types(ufunc_T *fp, garray_T *argtypes, int varargs)
329{
330 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
331 if (argtypes->ga_len > 0)
332 {
333 // When "varargs" is set the last name/type goes into uf_va_name
334 // and uf_va_type.
335 int len = argtypes->ga_len - (varargs ? 1 : 0);
336
337 if (len > 0)
338 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
339 if (fp->uf_arg_types != NULL)
340 {
341 int i;
342 type_T *type;
343
344 for (i = 0; i < len; ++ i)
345 {
346 char_u *p = ((char_u **)argtypes->ga_data)[i];
347
348 if (p == NULL)
349 // will get the type from the default value
350 type = &t_unknown;
351 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100352 type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100353 if (type == NULL)
354 return FAIL;
355 fp->uf_arg_types[i] = type;
356 }
357 }
358 if (varargs)
359 {
360 char_u *p;
361
362 // Move the last argument "...name: type" to uf_va_name and
363 // uf_va_type.
364 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
365 [fp->uf_args.ga_len - 1];
366 --fp->uf_args.ga_len;
367 p = ((char_u **)argtypes->ga_data)[len];
368 if (p == NULL)
369 // todo: get type from default value
370 fp->uf_va_type = &t_any;
371 else
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100372 fp->uf_va_type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100373 if (fp->uf_va_type == NULL)
374 return FAIL;
375 }
376 }
377 return OK;
378}
379
380/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200381 * Register function "fp" as using "current_funccal" as its scope.
382 */
383 static int
384register_closure(ufunc_T *fp)
385{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200386 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100387 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200388 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200389 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200390 fp->uf_scoped = current_funccal;
391 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200392
Bram Moolenaar58016442016-07-31 18:30:22 +0200393 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
394 return FAIL;
395 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
396 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200397 return OK;
398}
399
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100400 static void
401set_ufunc_name(ufunc_T *fp, char_u *name)
402{
403 STRCPY(fp->uf_name, name);
404
405 if (name[0] == K_SPECIAL)
406 {
407 fp->uf_name_exp = alloc(STRLEN(name) + 3);
408 if (fp->uf_name_exp != NULL)
409 {
410 STRCPY(fp->uf_name_exp, "<SNR>");
411 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
412 }
413 }
414}
415
Bram Moolenaar58016442016-07-31 18:30:22 +0200416/*
Bram Moolenaar04b12692020-05-04 23:24:44 +0200417 * Get a name for a lambda. Returned in static memory.
418 */
419 char_u *
420get_lambda_name(void)
421{
422 static char_u name[30];
423 static int lambda_no = 0;
424
425 sprintf((char*)name, "<lambda>%d", ++lambda_no);
426 return name;
427}
428
Bram Moolenaar801ab062020-06-25 19:27:56 +0200429#if defined(FEAT_LUA) || defined(PROTO)
430/*
431 * Registers a native C callback which can be called from Vim script.
432 * Returns the name of the Vim script function.
433 */
434 char_u *
435register_cfunc(cfunc_T cb, cfunc_free_T cb_free, void *state)
436{
437 char_u *name = get_lambda_name();
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200438 ufunc_T *fp;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200439
440 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
441 if (fp == NULL)
Bram Moolenaar7d2ac922020-06-29 20:20:33 +0200442 return NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200443
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200444 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200445 fp->uf_refcount = 1;
446 fp->uf_varargs = TRUE;
447 fp->uf_flags = FC_CFUNC;
448 fp->uf_calls = 0;
449 fp->uf_script_ctx = current_sctx;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200450 fp->uf_cb = cb;
451 fp->uf_cb_free = cb_free;
452 fp->uf_cb_state = state;
453
454 set_ufunc_name(fp, name);
455 hash_add(&func_hashtab, UF2HIKEY(fp));
456
457 return name;
Bram Moolenaar801ab062020-06-25 19:27:56 +0200458}
459#endif
460
Bram Moolenaar04b12692020-05-04 23:24:44 +0200461/*
Bram Moolenaar65c44152020-12-24 15:14:01 +0100462 * Skip over "->" or "=>" after the arguments of a lambda.
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100463 * If ": type" is found make "ret_type" point to "type".
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100464 * If "white_error" is not NULL check for correct use of white space and set
465 * "white_error" to TRUE if there is an error.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100466 * Return NULL if no valid arrow found.
467 */
468 static char_u *
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100469skip_arrow(
470 char_u *start,
471 int equal_arrow,
472 char_u **ret_type,
473 int *white_error)
Bram Moolenaar65c44152020-12-24 15:14:01 +0100474{
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100475 char_u *s = start;
476 char_u *bef = start - 2; // "start" points to > of ->
Bram Moolenaar65c44152020-12-24 15:14:01 +0100477
478 if (equal_arrow)
479 {
480 if (*s == ':')
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100481 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100482 if (white_error != NULL && !VIM_ISWHITE(s[1]))
483 {
484 *white_error = TRUE;
485 semsg(_(e_white_space_required_after_str), ":");
486 return NULL;
487 }
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100488 s = skipwhite(s + 1);
489 *ret_type = s;
490 s = skip_type(s, TRUE);
491 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100492 bef = s;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100493 s = skipwhite(s);
494 if (*s != '=')
495 return NULL;
496 ++s;
497 }
498 if (*s != '>')
499 return NULL;
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100500 if (white_error != NULL && ((!VIM_ISWHITE(*bef) && *bef != '{')
501 || !IS_WHITE_OR_NUL(s[1])))
502 {
503 *white_error = TRUE;
504 semsg(_(e_white_space_required_before_and_after_str),
505 equal_arrow ? "=>" : "->");
506 return NULL;
507 }
Bram Moolenaar65c44152020-12-24 15:14:01 +0100508 return skipwhite(s + 1);
509}
510
511/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200512 * Parse a lambda expression and get a Funcref from "*arg".
Bram Moolenaar65c44152020-12-24 15:14:01 +0100513 * "arg" points to the { in "{arg -> expr}" or the ( in "(arg) => expr"
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100514 * When "types_optional" is TRUE optionally take argument types.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200515 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
516 */
517 int
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100518get_lambda_tv(
519 char_u **arg,
520 typval_T *rettv,
521 int types_optional,
522 evalarg_T *evalarg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200523{
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200524 int evaluate = evalarg != NULL
525 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200526 garray_T newargs;
527 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200528 garray_T *pnewargs;
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100529 garray_T argtypes;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200530 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100531 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200532 int varargs;
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100533 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200534 int ret;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200535 char_u *s;
536 char_u *start, *end;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200537 int *old_eval_lavars = eval_lavars_used;
538 int eval_lavars = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200539 char_u *tofree = NULL;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100540 int equal_arrow = **arg == '(';
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100541 int white_error = FALSE;
Bram Moolenaar65c44152020-12-24 15:14:01 +0100542
543 if (equal_arrow && !in_vim9script())
544 return NOTDONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200545
546 ga_init(&newargs);
547 ga_init(&newlines);
548
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100549 // First, check if this is really a lambda expression. "->" or "=>" must
550 // be found after the arguments.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200551 s = skipwhite(*arg + 1);
Bram Moolenaar65c44152020-12-24 15:14:01 +0100552 ret = get_function_args(&s, equal_arrow ? ')' : '-', NULL,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100553 types_optional ? &argtypes : NULL, types_optional,
554 NULL, NULL, TRUE, NULL, NULL);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100555 if (ret == FAIL || skip_arrow(s, equal_arrow, &ret_type, NULL) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100556 {
557 if (types_optional)
558 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200559 return NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100560 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200561
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100562 // Parse the arguments for real.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200563 if (evaluate)
564 pnewargs = &newargs;
565 else
566 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200567 *arg = skipwhite(*arg + 1);
Bram Moolenaar65c44152020-12-24 15:14:01 +0100568 ret = get_function_args(arg, equal_arrow ? ')' : '-', pnewargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100569 types_optional ? &argtypes : NULL, types_optional,
570 &varargs, NULL, FALSE, NULL, NULL);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100571 if (ret == FAIL
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100572 || (s = skip_arrow(*arg, equal_arrow, &ret_type,
Bram Moolenaar7cfcd0c2020-12-25 16:11:53 +0100573 equal_arrow ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100574 {
575 if (types_optional)
576 ga_clear_strings(&argtypes);
Bram Moolenaare5730bd2020-12-25 22:30:16 +0100577 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100578 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100579 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100580 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200581
Bram Moolenaare38eab22019-12-05 21:50:01 +0100582 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200583 if (evaluate)
584 eval_lavars_used = &eval_lavars;
585
Bram Moolenaar65c44152020-12-24 15:14:01 +0100586 *arg = skipwhite_and_linebreak(*arg, evalarg);
587
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100588 // Recognize "{" as the start of a function body.
589 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +0100590 {
591 // TODO: process the function body upto the "}".
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100592 // Return type is required then.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100593 emsg("Lambda function body not supported yet");
594 goto errret;
595 }
596
Bram Moolenaare38eab22019-12-05 21:50:01 +0100597 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200598 start = *arg;
599 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200600 if (ret == FAIL)
601 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200602 if (evalarg != NULL)
603 {
604 // avoid that the expression gets freed when another line break follows
605 tofree = evalarg->eval_tofree;
606 evalarg->eval_tofree = NULL;
607 }
608
Bram Moolenaar65c44152020-12-24 15:14:01 +0100609 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +0100610 {
Bram Moolenaar65c44152020-12-24 15:14:01 +0100611 *arg = skipwhite_and_linebreak(*arg, evalarg);
612 if (**arg != '}')
613 {
614 semsg(_("E451: Expected }: %s"), *arg);
615 goto errret;
616 }
617 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +0100618 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200619
620 if (evaluate)
621 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200622 int len;
623 int flags = 0;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200624 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200625 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200626
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200627 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200628 if (fp == NULL)
629 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200630 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200631 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200632 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200633 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200634
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200635 ga_init2(&newlines, (int)sizeof(char_u *), 1);
636 if (ga_grow(&newlines, 1) == FAIL)
637 goto errret;
638
Bram Moolenaare38eab22019-12-05 21:50:01 +0100639 // Add "return " before the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200640 len = 7 + (int)(end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200641 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200642 if (p == NULL)
643 goto errret;
644 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
645 STRCPY(p, "return ");
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200646 vim_strncpy(p + 7, start, end - start);
Bram Moolenaarf10806b2020-04-02 18:34:35 +0200647 if (strstr((char *)p + 7, "a:") == NULL)
648 // No a: variables are used for sure.
649 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200650
651 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100652 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200653 hash_add(&func_hashtab, UF2HIKEY(fp));
654 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200655 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100656 if (types_optional)
657 {
658 if (parse_argument_types(fp, &argtypes, FALSE) == FAIL)
659 goto errret;
660 if (ret_type != NULL)
661 {
662 fp->uf_ret_type = parse_type(&ret_type,
663 &fp->uf_type_list, TRUE);
664 if (fp->uf_ret_type == NULL)
665 goto errret;
666 }
667 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100668
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200669 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200670 if (current_funccal != NULL && eval_lavars)
671 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200672 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200673 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200674 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200675 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200676
677#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200678 if (prof_def_func())
679 func_do_profile(fp);
680#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200681 if (sandbox)
682 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200684 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200685 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200686 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200687 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100688 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200689
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200690 pt->pt_func = fp;
691 pt->pt_refcount = 1;
692 rettv->vval.v_partial = pt;
693 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200694 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200695
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200696 eval_lavars_used = old_eval_lavars;
Bram Moolenaarefaaaa62020-07-08 22:24:09 +0200697 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200698 evalarg->eval_tofree = tofree;
699 else
700 vim_free(tofree);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100701 if (types_optional)
702 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200703 return OK;
704
705errret:
706 ga_clear_strings(&newargs);
707 ga_clear_strings(&newlines);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100708 if (types_optional)
709 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200710 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100711 vim_free(pt);
Bram Moolenaarefaaaa62020-07-08 22:24:09 +0200712 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200713 evalarg->eval_tofree = tofree;
714 else
715 vim_free(tofree);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200716 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200717 return FAIL;
718}
719
720/*
721 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
722 * name it contains, otherwise return "name".
723 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
724 * "partialp".
725 */
726 char_u *
727deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
728{
729 dictitem_T *v;
730 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200731 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200732
733 if (partialp != NULL)
734 *partialp = NULL;
735
736 cc = name[*lenp];
737 name[*lenp] = NUL;
738 v = find_var(name, NULL, no_autoload);
739 name[*lenp] = cc;
740 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
741 {
742 if (v->di_tv.vval.v_string == NULL)
743 {
744 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100745 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200746 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200747 s = v->di_tv.vval.v_string;
748 *lenp = (int)STRLEN(s);
749 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200750 }
751
752 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
753 {
754 partial_T *pt = v->di_tv.vval.v_partial;
755
756 if (pt == NULL)
757 {
758 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100759 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200760 }
761 if (partialp != NULL)
762 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200763 s = partial_name(pt);
764 *lenp = (int)STRLEN(s);
765 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200766 }
767
768 return name;
769}
770
771/*
772 * Give an error message with a function name. Handle <SNR> things.
773 * "ermsg" is to be passed without translation, use N_() instead of _().
774 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100775 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200776emsg_funcname(char *ermsg, char_u *name)
777{
778 char_u *p;
779
780 if (*name == K_SPECIAL)
781 p = concat_str((char_u *)"<SNR>", name + 3);
782 else
783 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100784 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200785 if (p != name)
786 vim_free(p);
787}
788
789/*
790 * Allocate a variable for the result of a function.
791 * Return OK or FAIL.
792 */
793 int
794get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200795 char_u *name, // name of the function
796 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200797 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200798 char_u **arg, // argument, pointing to the '('
Bram Moolenaare6b53242020-07-01 17:28:33 +0200799 evalarg_T *evalarg, // for line continuation
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200800 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200801{
802 char_u *argp;
803 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100804 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
805 int argcount = 0; // number of arguments found
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200806 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200807
808 /*
809 * Get the arguments.
810 */
811 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200812 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
813 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200814 {
Bram Moolenaare6b53242020-07-01 17:28:33 +0200815 // skip the '(' or ',' and possibly line breaks
816 argp = skipwhite_and_linebreak(argp + 1, evalarg);
817
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200818 if (*argp == ')' || *argp == ',' || *argp == NUL)
819 break;
Bram Moolenaare6b53242020-07-01 17:28:33 +0200820 if (eval1(&argp, &argvars[argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200821 {
822 ret = FAIL;
823 break;
824 }
825 ++argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +0200826 // The comma should come right after the argument, but this wasn't
827 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200828 if (vim9script)
829 {
830 if (*argp != ',' && *skipwhite(argp) == ',')
831 {
832 semsg(_(e_no_white_space_allowed_before_str), ",");
833 ret = FAIL;
834 break;
835 }
836 }
837 else
Bram Moolenaar9d489562020-07-30 20:08:50 +0200838 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200839 if (*argp != ',')
840 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200841 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
842 {
843 semsg(_(e_white_space_required_after_str), ",");
844 ret = FAIL;
845 break;
846 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200847 }
Bram Moolenaare6b53242020-07-01 17:28:33 +0200848 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200849 if (*argp == ')')
850 ++argp;
851 else
852 ret = FAIL;
853
854 if (ret == OK)
855 {
856 int i = 0;
857
858 if (get_vim_var_nr(VV_TESTING))
859 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100860 // Prepare for calling test_garbagecollect_now(), need to know
861 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200862 if (funcargs.ga_itemsize == 0)
863 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
864 for (i = 0; i < argcount; ++i)
865 if (ga_grow(&funcargs, 1) == OK)
866 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
867 &argvars[i];
868 }
869
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200870 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200871
872 funcargs.ga_len -= i;
873 }
874 else if (!aborting())
875 {
876 if (argcount == MAX_FUNC_ARGS)
877 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
878 else
879 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
880 }
881
882 while (--argcount >= 0)
883 clear_tv(&argvars[argcount]);
884
Bram Moolenaar8294d492020-08-10 22:40:56 +0200885 if (in_vim9script())
886 *arg = argp;
887 else
888 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200889 return ret;
890}
891
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200892/*
893 * Return TRUE if "p" starts with "<SID>" or "s:".
894 * Only works if eval_fname_script() returned non-zero for "p"!
895 */
896 static int
897eval_fname_sid(char_u *p)
898{
899 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
900}
901
902/*
903 * In a script change <SID>name() and s:name() to K_SNR 123_name().
904 * Change <SNR>123_name() to K_SNR 123_name().
905 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
906 * (slow).
907 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100908 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200909fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
910{
911 int llen;
912 char_u *fname;
913 int i;
914
915 llen = eval_fname_script(name);
916 if (llen > 0)
917 {
918 fname_buf[0] = K_SPECIAL;
919 fname_buf[1] = KS_EXTRA;
920 fname_buf[2] = (int)KE_SNR;
921 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100922 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200923 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200924 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100925 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200926 else
927 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200928 sprintf((char *)fname_buf + 3, "%ld_",
929 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200930 i = (int)STRLEN(fname_buf);
931 }
932 }
933 if (i + STRLEN(name + llen) < FLEN_FIXED)
934 {
935 STRCPY(fname_buf + i, name + llen);
936 fname = fname_buf;
937 }
938 else
939 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200940 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200941 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +0100942 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200943 else
944 {
945 *tofree = fname;
946 mch_memmove(fname, fname_buf, (size_t)i);
947 STRCPY(fname + i, name + llen);
948 }
949 }
950 }
951 else
952 fname = name;
953 return fname;
954}
955
956/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100957 * Find a function "name" in script "sid".
958 */
959 static ufunc_T *
960find_func_with_sid(char_u *name, int sid)
961{
962 hashitem_T *hi;
963 char_u buffer[200];
964
965 buffer[0] = K_SPECIAL;
966 buffer[1] = KS_EXTRA;
967 buffer[2] = (int)KE_SNR;
968 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
969 (long)sid, name);
970 hi = hash_find(&func_hashtab, buffer);
971 if (!HASHITEM_EMPTY(hi))
972 return HI2UF(hi);
973
974 return NULL;
975}
976
977/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200978 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200979 * When "is_global" is true don't find script-local or imported functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200980 * Return NULL for unknown function.
981 */
Bram Moolenaarce658352020-07-31 23:47:12 +0200982 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200983find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200984{
985 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100986 ufunc_T *func;
987 imported_T *imported;
988
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200989 if (!is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990 {
Bram Moolenaar333894b2020-08-01 18:53:07 +0200991 char_u *after_script = NULL;
Bram Moolenaarefa94442020-08-08 22:16:00 +0200992 long sid = 0;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200993 int find_script_local = in_vim9script()
994 && eval_isnamec1(*name) && name[1] != ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995
Bram Moolenaar95006e32020-08-29 17:47:08 +0200996 if (find_script_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200998 // Find script-local function before global one.
999 func = find_func_with_sid(name, current_sctx.sc_sid);
1000 if (func != NULL)
1001 return func;
1002 }
1003
Bram Moolenaarefa94442020-08-08 22:16:00 +02001004 if (name[0] == K_SPECIAL
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001005 && name[1] == KS_EXTRA
1006 && name[2] == KE_SNR)
1007 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001008 // Caller changes s: to <SNR>99_name.
1009
1010 after_script = name + 3;
1011 sid = getdigits(&after_script);
Bram Moolenaarefa94442020-08-08 22:16:00 +02001012 if (*after_script == '_')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001013 ++after_script;
1014 else
1015 after_script = NULL;
1016 }
Bram Moolenaar95006e32020-08-29 17:47:08 +02001017 if (find_script_local || after_script != NULL)
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001018 {
1019 // Find imported function before global one.
Bram Moolenaarefa94442020-08-08 22:16:00 +02001020 if (after_script != NULL && sid != current_sctx.sc_sid)
1021 imported = find_imported_in_script(after_script, 0, sid);
1022 else
1023 imported = find_imported(after_script == NULL
1024 ? name : after_script, 0, cctx);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001025 if (imported != NULL && imported->imp_funcname != NULL)
1026 {
1027 hi = hash_find(&func_hashtab, imported->imp_funcname);
1028 if (!HASHITEM_EMPTY(hi))
1029 return HI2UF(hi);
1030 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001031 }
1032 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001033
Bram Moolenaara26b9702020-04-18 19:53:28 +02001034 hi = hash_find(&func_hashtab,
1035 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001036 if (!HASHITEM_EMPTY(hi))
1037 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001038
1039 return NULL;
1040}
1041
1042/*
1043 * Find a function by name, return pointer to it in ufuncs.
1044 * "cctx" is passed in a :def function to find imported functions.
1045 * Return NULL for unknown or dead function.
1046 */
1047 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001048find_func(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001049{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001050 ufunc_T *fp = find_func_even_dead(name, is_global, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051
1052 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
1053 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001054 return NULL;
1055}
1056
1057/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02001058 * Return TRUE if "ufunc" is a global function.
1059 */
1060 int
1061func_is_global(ufunc_T *ufunc)
1062{
1063 return ufunc->uf_name[0] != K_SPECIAL;
1064}
1065
1066/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001067 * Copy the function name of "fp" to buffer "buf".
1068 * "buf" must be able to hold the function name plus three bytes.
1069 * Takes care of script-local function names.
1070 */
1071 static void
1072cat_func_name(char_u *buf, ufunc_T *fp)
1073{
Bram Moolenaar0f769812020-09-12 18:32:34 +02001074 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001075 {
1076 STRCPY(buf, "<SNR>");
1077 STRCAT(buf, fp->uf_name + 3);
1078 }
1079 else
1080 STRCPY(buf, fp->uf_name);
1081}
1082
1083/*
1084 * Add a number variable "name" to dict "dp" with value "nr".
1085 */
1086 static void
1087add_nr_var(
1088 dict_T *dp,
1089 dictitem_T *v,
1090 char *name,
1091 varnumber_T nr)
1092{
1093 STRCPY(v->di_key, name);
1094 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1095 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
1096 v->di_tv.v_type = VAR_NUMBER;
1097 v->di_tv.v_lock = VAR_FIXED;
1098 v->di_tv.vval.v_number = nr;
1099}
1100
1101/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001102 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001103 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001104 static void
1105free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001106{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001107 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001108
1109 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1110 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001111 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001112
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001113 // When garbage collecting a funccall_T may be freed before the
1114 // function that references it, clear its uf_scoped field.
1115 // The function may have been redefined and point to another
1116 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001117 if (fp != NULL && fp->uf_scoped == fc)
1118 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001119 }
Bram Moolenaar58016442016-07-31 18:30:22 +02001120 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001121
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001122 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001123 vim_free(fc);
1124}
1125
1126/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001127 * Free "fc" and what it contains.
1128 * Can be called only when "fc" is kept beyond the period of it called,
1129 * i.e. after cleanup_function_call(fc).
1130 */
1131 static void
1132free_funccal_contents(funccall_T *fc)
1133{
1134 listitem_T *li;
1135
1136 // Free all l: variables.
1137 vars_clear(&fc->l_vars.dv_hashtab);
1138
1139 // Free all a: variables.
1140 vars_clear(&fc->l_avars.dv_hashtab);
1141
1142 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001143 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001144 clear_tv(&li->li_tv);
1145
1146 free_funccal(fc);
1147}
1148
1149/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02001150 * Handle the last part of returning from a function: free the local hashtable.
1151 * Unless it is still in use by a closure.
1152 */
1153 static void
1154cleanup_function_call(funccall_T *fc)
1155{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001156 int may_free_fc = fc->fc_refcount <= 0;
1157 int free_fc = TRUE;
1158
Bram Moolenaar6914c642017-04-01 21:21:30 +02001159 current_funccal = fc->caller;
1160
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001161 // Free all l: variables if not referred.
1162 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
1163 vars_clear(&fc->l_vars.dv_hashtab);
1164 else
1165 free_fc = FALSE;
1166
1167 // If the a:000 list and the l: and a: dicts are not referenced and
1168 // there is no closure using it, we can free the funccall_T and what's
1169 // in it.
1170 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
1171 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02001172 else
1173 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001174 int todo;
1175 hashitem_T *hi;
1176 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02001177
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001178 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02001179
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001180 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001181 todo = (int)fc->l_avars.dv_hashtab.ht_used;
1182 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
1183 {
1184 if (!HASHITEM_EMPTY(hi))
1185 {
1186 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001187 di = HI2DI(hi);
1188 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02001189 }
1190 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001191 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02001192
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001193 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
1194 fc->l_varlist.lv_first = NULL;
1195 else
1196 {
1197 listitem_T *li;
1198
1199 free_fc = FALSE;
1200
1201 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001202 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02001203 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001204 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001205
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001206 if (free_fc)
1207 free_funccal(fc);
1208 else
1209 {
1210 static int made_copy = 0;
1211
1212 // "fc" is still in use. This can happen when returning "a:000",
1213 // assigning "l:" to a global variable or defining a closure.
1214 // Link "fc" in the list for garbage collection later.
1215 fc->caller = previous_funccal;
1216 previous_funccal = fc;
1217
1218 if (want_garbage_collect)
1219 // If garbage collector is ready, clear count.
1220 made_copy = 0;
1221 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001222 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001223 // We have made a lot of copies, worth 4 Mbyte. This can happen
1224 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01001225 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001226 // too much memory.
1227 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01001228 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001229 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02001230 }
1231}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001232
1233/*
1234 * There are two kinds of function names:
1235 * 1. ordinary names, function defined with :function or :def
1236 * 2. numbered functions and lambdas
1237 * For the first we only count the name stored in func_hashtab as a reference,
1238 * using function() does not count as a reference, because the function is
1239 * looked up by name.
1240 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02001241 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001242func_name_refcount(char_u *name)
1243{
1244 return isdigit(*name) || *name == '<';
1245}
1246
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001247/*
1248 * Unreference "fc": decrement the reference count and free it when it
1249 * becomes zero. "fp" is detached from "fc".
1250 * When "force" is TRUE we are exiting.
1251 */
1252 static void
1253funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
1254{
1255 funccall_T **pfc;
1256 int i;
1257
1258 if (fc == NULL)
1259 return;
1260
1261 if (--fc->fc_refcount <= 0 && (force || (
1262 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
1263 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
1264 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
1265 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
1266 {
1267 if (fc == *pfc)
1268 {
1269 *pfc = fc->caller;
1270 free_funccal_contents(fc);
1271 return;
1272 }
1273 }
1274 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1275 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
1276 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
1277}
1278
1279/*
1280 * Remove the function from the function hashtable. If the function was
1281 * deleted while it still has references this was already done.
1282 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
1283 */
1284 static int
1285func_remove(ufunc_T *fp)
1286{
1287 hashitem_T *hi;
1288
1289 // Return if it was already virtually deleted.
1290 if (fp->uf_flags & FC_DEAD)
1291 return FALSE;
1292
1293 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
1294 if (!HASHITEM_EMPTY(hi))
1295 {
1296 // When there is a def-function index do not actually remove the
1297 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001298 // Do remove it when it's a copy.
1299 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001300 fp->uf_flags |= FC_DEAD;
1301 else
1302 hash_remove(&func_hashtab, hi);
1303 return TRUE;
1304 }
1305 return FALSE;
1306}
1307
1308 static void
1309func_clear_items(ufunc_T *fp)
1310{
1311 ga_clear_strings(&(fp->uf_args));
1312 ga_clear_strings(&(fp->uf_def_args));
1313 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001314 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001315 VIM_CLEAR(fp->uf_def_arg_idx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001316 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001317 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02001318 clear_type_list(&fp->uf_type_list);
Bram Moolenaarf112f302020-12-20 17:47:52 +01001319 partial_unref(fp->uf_partial);
1320 fp->uf_partial = NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +02001321
1322#ifdef FEAT_LUA
1323 if (fp->uf_cb_free != NULL)
1324 {
1325 fp->uf_cb_free(fp->uf_cb_state);
1326 fp->uf_cb_free = NULL;
1327 }
1328
1329 fp->uf_cb_state = NULL;
1330 fp->uf_cb = NULL;
1331#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332#ifdef FEAT_PROFILE
1333 VIM_CLEAR(fp->uf_tml_count);
1334 VIM_CLEAR(fp->uf_tml_total);
1335 VIM_CLEAR(fp->uf_tml_self);
1336#endif
1337}
1338
1339/*
1340 * Free all things that a function contains. Does not free the function
1341 * itself, use func_free() for that.
1342 * When "force" is TRUE we are exiting.
1343 */
1344 static void
1345func_clear(ufunc_T *fp, int force)
1346{
1347 if (fp->uf_cleared)
1348 return;
1349 fp->uf_cleared = TRUE;
1350
1351 // clear this function
1352 func_clear_items(fp);
1353 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001354 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355}
1356
1357/*
1358 * Free a function and remove it from the list of functions. Does not free
1359 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001360 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02001361 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02001363 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001364func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001365{
1366 // Only remove it when not done already, otherwise we would remove a newer
1367 // version of the function with the same name.
1368 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1369 func_remove(fp);
1370
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001371 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001372 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001373 if (fp->uf_dfunc_idx > 0)
1374 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001375 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02001377 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001378 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02001379 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380}
1381
1382/*
1383 * Free all things that a function contains and free the function itself.
1384 * When "force" is TRUE we are exiting.
1385 */
1386 static void
1387func_clear_free(ufunc_T *fp, int force)
1388{
1389 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001390 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
1391 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001392 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02001393 else
1394 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395}
1396
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001397/*
1398 * Copy already defined function "lambda" to a new function with name "global".
1399 * This is for when a compiled function defines a global function.
1400 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001401 int
1402copy_func(char_u *lambda, char_u *global, ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001403{
1404 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01001405 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001406
1407 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001408 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001409 semsg(_(e_lambda_function_not_found_str), lambda);
1410 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001411 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001412
1413 // TODO: handle ! to overwrite
1414 fp = find_func(global, TRUE, NULL);
1415 if (fp != NULL)
1416 {
1417 semsg(_(e_funcexts), global);
1418 return FAIL;
1419 }
1420
1421 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
1422 if (fp == NULL)
1423 return FAIL;
1424
1425 fp->uf_varargs = ufunc->uf_varargs;
1426 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
1427 fp->uf_def_status = ufunc->uf_def_status;
1428 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
1429 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
1430 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
1431 == FAIL
1432 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
1433 goto failed;
1434
1435 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
1436 : vim_strsave(ufunc->uf_name_exp);
1437 if (ufunc->uf_arg_types != NULL)
1438 {
1439 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
1440 if (fp->uf_arg_types == NULL)
1441 goto failed;
1442 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
1443 sizeof(type_T *) * fp->uf_args.ga_len);
1444 }
1445 if (ufunc->uf_def_arg_idx != NULL)
1446 {
1447 fp->uf_def_arg_idx = ALLOC_MULT(int, fp->uf_def_args.ga_len + 1);
1448 if (fp->uf_def_arg_idx == NULL)
1449 goto failed;
1450 mch_memmove(fp->uf_def_arg_idx, ufunc->uf_def_arg_idx,
1451 sizeof(int) * fp->uf_def_args.ga_len + 1);
1452 }
1453 if (ufunc->uf_va_name != NULL)
1454 {
1455 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
1456 if (fp->uf_va_name == NULL)
1457 goto failed;
1458 }
1459 fp->uf_ret_type = ufunc->uf_ret_type;
1460
1461 fp->uf_refcount = 1;
1462 STRCPY(fp->uf_name, global);
1463 hash_add(&func_hashtab, UF2HIKEY(fp));
1464
1465 // the referenced dfunc_T is now used one more time
1466 link_def_function(fp);
1467
1468 // Create a partial to store the context of the function, if not done
1469 // already.
1470 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
1471 {
1472 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
1473
1474 if (pt == NULL)
1475 goto failed;
1476 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
1477 goto failed;
1478 ufunc->uf_partial = pt;
1479 --pt->pt_refcount; // not referenced here yet
1480 }
1481 if (ufunc->uf_partial != NULL)
1482 {
1483 fp->uf_partial = ufunc->uf_partial;
1484 ++fp->uf_partial->pt_refcount;
1485 }
1486
1487 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001488
1489failed:
1490 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001491 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001492}
1493
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001494static int funcdepth = 0;
1495
1496/*
1497 * Increment the function call depth count.
1498 * Return FAIL when going over 'maxfuncdepth'.
1499 * Otherwise return OK, must call funcdepth_decrement() later!
1500 */
1501 int
1502funcdepth_increment(void)
1503{
1504 if (funcdepth >= p_mfd)
1505 {
1506 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
1507 return FAIL;
1508 }
1509 ++funcdepth;
1510 return OK;
1511}
1512
1513 void
1514funcdepth_decrement(void)
1515{
1516 --funcdepth;
1517}
1518
1519/*
1520 * Get the current function call depth.
1521 */
1522 int
1523funcdepth_get(void)
1524{
1525 return funcdepth;
1526}
1527
1528/*
1529 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001530 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001531 * times as funcdepth_increment().
1532 */
1533 void
1534funcdepth_restore(int depth)
1535{
1536 funcdepth = depth;
1537}
Bram Moolenaar6914c642017-04-01 21:21:30 +02001538
1539/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001540 * Call a user function.
1541 */
1542 static void
1543call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001544 ufunc_T *fp, // pointer to function
1545 int argcount, // nr of args
1546 typval_T *argvars, // arguments
1547 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001548 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01001549 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001550{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001551 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001552 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001553 funccall_T *fc;
1554 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001555 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001556 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001557 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001558 int i;
1559 int ai;
1560 int islambda = FALSE;
1561 char_u numbuf[NUMBUFLEN];
1562 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001563#ifdef FEAT_PROFILE
1564 proftime_T wait_start;
1565 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001566 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001567#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001568 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001569
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001570 // If depth of calling is getting too high, don't execute the function.
1571 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001572 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001573 rettv->v_type = VAR_NUMBER;
1574 rettv->vval.v_number = -1;
1575 return;
1576 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001577
Bram Moolenaare38eab22019-12-05 21:50:01 +01001578 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001579
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001580 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001581 if (fc == NULL)
1582 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001583 fc->caller = current_funccal;
1584 current_funccal = fc;
1585 fc->func = fp;
1586 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001587 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001588 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001589 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1590 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001591 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001592 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001593 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001594
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001595 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 {
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001597 // Execute the function, possibly compiling it first.
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001598 call_def_function(fp, argcount, argvars, funcexe->partial, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001599 funcdepth_decrement();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 current_funccal = fc->caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 free_funccal(fc);
1602 return;
1603 }
1604
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001605 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1606 islambda = TRUE;
1607
1608 /*
1609 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1610 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1611 * each argument variable and saves a lot of time.
1612 */
1613 /*
1614 * Init l: variables.
1615 */
1616 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1617 if (selfdict != NULL)
1618 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001619 // Set l:self to "selfdict". Use "name" to avoid a warning from
1620 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001621 v = &fc->fixvar[fixvar_idx++].var;
1622 name = v->di_key;
1623 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001624 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001625 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1626 v->di_tv.v_type = VAR_DICT;
1627 v->di_tv.v_lock = 0;
1628 v->di_tv.vval.v_dict = selfdict;
1629 ++selfdict->dv_refcount;
1630 }
1631
1632 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001633 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001634 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001635 * Set a:000 to a list with room for the "..." arguments.
1636 */
1637 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001638 if ((fp->uf_flags & FC_NOARGS) == 0)
1639 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001640 (varnumber_T)(argcount >= fp->uf_args.ga_len
1641 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001642 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001643 if ((fp->uf_flags & FC_NOARGS) == 0)
1644 {
1645 // Use "name" to avoid a warning from some compiler that checks the
1646 // destination size.
1647 v = &fc->fixvar[fixvar_idx++].var;
1648 name = v->di_key;
1649 STRCPY(name, "000");
1650 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1651 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1652 v->di_tv.v_type = VAR_LIST;
1653 v->di_tv.v_lock = VAR_FIXED;
1654 v->di_tv.vval.v_list = &fc->l_varlist;
1655 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001656 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001657 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1658 fc->l_varlist.lv_lock = VAR_FIXED;
1659
1660 /*
1661 * Set a:firstline to "firstline" and a:lastline to "lastline".
1662 * Set a:name to named arguments.
1663 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001664 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001665 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001666 if ((fp->uf_flags & FC_NOARGS) == 0)
1667 {
1668 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001669 (varnumber_T)funcexe->firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001670 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001671 (varnumber_T)funcexe->lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001672 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001673 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001674 {
1675 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001676 typval_T def_rettv;
1677 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001678
1679 ai = i - fp->uf_args.ga_len;
1680 if (ai < 0)
1681 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001682 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001683 name = FUNCARG(fp, i);
1684 if (islambda)
1685 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001686
1687 // evaluate named argument default expression
1688 isdefault = ai + fp->uf_def_args.ga_len >= 0
1689 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1690 && argvars[i].vval.v_number == VVAL_NONE));
1691 if (isdefault)
1692 {
1693 char_u *default_expr = NULL;
1694 def_rettv.v_type = VAR_NUMBER;
1695 def_rettv.vval.v_number = -1;
1696
1697 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1698 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001699 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001700 {
1701 default_arg_err = 1;
1702 break;
1703 }
1704 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001705 }
1706 else
1707 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001708 if ((fp->uf_flags & FC_NOARGS) != 0)
1709 // Bail out if no a: arguments used (in lambda).
1710 break;
1711
Bram Moolenaare38eab22019-12-05 21:50:01 +01001712 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001713 sprintf((char *)numbuf, "%d", ai + 1);
1714 name = numbuf;
1715 }
1716 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1717 {
1718 v = &fc->fixvar[fixvar_idx++].var;
1719 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001720 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001721 }
1722 else
1723 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001724 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001725 if (v == NULL)
1726 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001727 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001728 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001729
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001730 // Note: the values are copied directly to avoid alloc/free.
1731 // "argvars" must have VAR_FIXED for v_lock.
1732 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001733 v->di_tv.v_lock = VAR_FIXED;
1734
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001735 if (addlocal)
1736 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001737 // Named arguments should be accessed without the "a:" prefix in
1738 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001739 copy_tv(&v->di_tv, &v->di_tv);
1740 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001741 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001742 else
1743 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001744
1745 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1746 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001747 listitem_T *li = &fc->l_listitems[ai];
1748
1749 li->li_tv = argvars[i];
1750 li->li_tv.v_lock = VAR_FIXED;
1751 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001752 }
1753 }
1754
Bram Moolenaare38eab22019-12-05 21:50:01 +01001755 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001756 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001757
1758 if (fp->uf_flags & FC_SANDBOX)
1759 {
1760 using_sandbox = TRUE;
1761 ++sandbox;
1762 }
1763
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001764 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001765 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001766 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001767 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001768 ++no_wait_return;
1769 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001770
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001771 smsg(_("calling %s"), SOURCING_NAME);
1772 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001773 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001774 char_u buf[MSG_BUF_LEN];
1775 char_u numbuf2[NUMBUFLEN];
1776 char_u *tofree;
1777 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001778
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001779 msg_puts("(");
1780 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001781 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001782 if (i > 0)
1783 msg_puts(", ");
1784 if (argvars[i].v_type == VAR_NUMBER)
1785 msg_outnum((long)argvars[i].vval.v_number);
1786 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001787 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001788 // Do not want errors such as E724 here.
1789 ++emsg_off;
1790 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1791 --emsg_off;
1792 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001793 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001794 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001795 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001796 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1797 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001798 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001799 msg_puts((char *)s);
1800 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001801 }
1802 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001803 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001804 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001805 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001806 msg_puts("\n"); // don't overwrite this either
1807
1808 verbose_leave_scroll();
1809 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001810 }
1811#ifdef FEAT_PROFILE
1812 if (do_profiling == PROF_YES)
1813 {
1814 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001815 {
1816 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001817 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001818 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001819 if (fp->uf_profiling
1820 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1821 {
1822 ++fp->uf_tm_count;
1823 profile_start(&call_start);
1824 profile_zero(&fp->uf_tm_children);
1825 }
1826 script_prof_save(&wait_start);
1827 }
1828#endif
1829
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001830 save_current_sctx = current_sctx;
1831 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001832 save_did_emsg = did_emsg;
1833 did_emsg = FALSE;
1834
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001835 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1836 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001837 else if (islambda)
1838 {
1839 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
1840
1841 // A Lambda always has the command "return {expr}". It is much faster
1842 // to evaluate {expr} directly.
1843 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001844 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001845 --ex_nesting_level;
1846 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001847 else
1848 // call do_cmdline() to execute the lines
1849 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001850 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1851
1852 --RedrawingDisabled;
1853
Bram Moolenaare38eab22019-12-05 21:50:01 +01001854 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001855 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1856 {
1857 clear_tv(rettv);
1858 rettv->v_type = VAR_NUMBER;
1859 rettv->vval.v_number = -1;
1860 }
1861
1862#ifdef FEAT_PROFILE
1863 if (do_profiling == PROF_YES && (fp->uf_profiling
1864 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1865 {
1866 profile_end(&call_start);
1867 profile_sub_wait(&wait_start, &call_start);
1868 profile_add(&fp->uf_tm_total, &call_start);
1869 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1870 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1871 {
1872 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1873 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1874 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001875 if (started_profiling)
1876 // make a ":profdel func" stop profiling the function
1877 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001878 }
1879#endif
1880
Bram Moolenaare38eab22019-12-05 21:50:01 +01001881 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001882 if (p_verbose >= 12)
1883 {
1884 ++no_wait_return;
1885 verbose_enter_scroll();
1886
1887 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001888 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001889 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001890 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001891 (long)fc->rettv->vval.v_number);
1892 else
1893 {
1894 char_u buf[MSG_BUF_LEN];
1895 char_u numbuf2[NUMBUFLEN];
1896 char_u *tofree;
1897 char_u *s;
1898
Bram Moolenaare38eab22019-12-05 21:50:01 +01001899 // The value may be very long. Skip the middle part, so that we
1900 // have some idea how it starts and ends. smsg() would always
1901 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001902 ++emsg_off;
1903 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1904 --emsg_off;
1905 if (s != NULL)
1906 {
1907 if (vim_strsize(s) > MSG_BUF_CLEN)
1908 {
1909 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1910 s = buf;
1911 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001912 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001913 vim_free(tofree);
1914 }
1915 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001916 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001917
1918 verbose_leave_scroll();
1919 --no_wait_return;
1920 }
1921
Bram Moolenaare31ee862020-01-07 20:59:34 +01001922 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001923 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001924 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001925#ifdef FEAT_PROFILE
1926 if (do_profiling == PROF_YES)
1927 script_prof_restore(&wait_start);
1928#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001929 if (using_sandbox)
1930 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001931
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001932 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001933 {
1934 ++no_wait_return;
1935 verbose_enter_scroll();
1936
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001937 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001938 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001939
1940 verbose_leave_scroll();
1941 --no_wait_return;
1942 }
1943
1944 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001945 funcdepth_decrement();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001946 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001947}
1948
1949/*
Bram Moolenaar50824712020-12-20 21:10:17 +01001950 * Check the argument count for user function "fp".
1951 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
1952 */
1953 int
1954check_user_func_argcount(ufunc_T *fp, int argcount)
1955{
1956 int regular_args = fp->uf_args.ga_len;
1957
1958 if (argcount < regular_args - fp->uf_def_args.ga_len)
1959 return FCERR_TOOFEW;
1960 else if (!has_varargs(fp) && argcount > regular_args)
1961 return FCERR_TOOMANY;
1962 return FCERR_UNKNOWN;
1963}
1964
1965/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001967 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 int
1969call_user_func_check(
1970 ufunc_T *fp,
1971 int argcount,
1972 typval_T *argvars,
1973 typval_T *rettv,
1974 funcexe_T *funcexe,
1975 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001976{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001978
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1980 *funcexe->doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01001981 error = check_user_func_argcount(fp, argcount);
1982 if (error != FCERR_UNKNOWN)
1983 return error;
1984 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 error = FCERR_DICT;
1986 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001987 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 int did_save_redo = FALSE;
1989 save_redo_T save_redo;
1990
1991 /*
1992 * Call the user function.
1993 * Save and restore search patterns, script variables and
1994 * redo buffer.
1995 */
1996 save_search_patterns();
1997 if (!ins_compl_active())
1998 {
1999 saveRedobuff(&save_redo);
2000 did_save_redo = TRUE;
2001 }
2002 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002003 call_user_func(fp, argcount, argvars, rettv, funcexe,
2004 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
2006 // Function was unreferenced while being used, free it now.
2007 func_clear_free(fp, FALSE);
2008 if (did_save_redo)
2009 restoreRedobuff(&save_redo);
2010 restore_search_patterns();
2011 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002012 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002014}
2015
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002016static funccal_entry_T *funccal_stack = NULL;
2017
2018/*
2019 * Save the current function call pointer, and set it to NULL.
2020 * Used when executing autocommands and for ":source".
2021 */
2022 void
2023save_funccal(funccal_entry_T *entry)
2024{
2025 entry->top_funccal = current_funccal;
2026 entry->next = funccal_stack;
2027 funccal_stack = entry;
2028 current_funccal = NULL;
2029}
2030
2031 void
2032restore_funccal(void)
2033{
2034 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002035 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002036 else
2037 {
2038 current_funccal = funccal_stack->top_funccal;
2039 funccal_stack = funccal_stack->next;
2040 }
2041}
2042
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02002043 funccall_T *
2044get_current_funccal(void)
2045{
2046 return current_funccal;
2047}
2048
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002049/*
2050 * Mark all functions of script "sid" as deleted.
2051 */
2052 void
2053delete_script_functions(int sid)
2054{
2055 hashitem_T *hi;
2056 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02002057 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002058 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002059 size_t len;
2060
2061 buf[0] = K_SPECIAL;
2062 buf[1] = KS_EXTRA;
2063 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002064 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002065 len = STRLEN(buf);
2066
Bram Moolenaarce658352020-07-31 23:47:12 +02002067 while (todo > 0)
2068 {
2069 todo = func_hashtab.ht_used;
2070 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2071 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002072 {
Bram Moolenaarce658352020-07-31 23:47:12 +02002073 fp = HI2UF(hi);
2074 if (STRNCMP(fp->uf_name, buf, len) == 0)
2075 {
2076 int changed = func_hashtab.ht_changed;
2077
2078 fp->uf_flags |= FC_DEAD;
2079 func_clear(fp, TRUE);
2080 // When clearing a function another function can be cleared
2081 // as a side effect. When that happens start over.
2082 if (changed != func_hashtab.ht_changed)
2083 break;
2084 }
2085 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002086 }
Bram Moolenaarce658352020-07-31 23:47:12 +02002087 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002088}
2089
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002090#if defined(EXITFREE) || defined(PROTO)
2091 void
2092free_all_functions(void)
2093{
2094 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002095 ufunc_T *fp;
2096 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002097 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002098 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002099
Bram Moolenaare38eab22019-12-05 21:50:01 +01002100 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02002101 while (current_funccal != NULL)
2102 {
2103 clear_tv(current_funccal->rettv);
2104 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002105 if (current_funccal == NULL && funccal_stack != NULL)
2106 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02002107 }
2108
Bram Moolenaare38eab22019-12-05 21:50:01 +01002109 // First clear what the functions contain. Since this may lower the
2110 // reference count of a function, it may also free a function and change
2111 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002112 while (todo > 0)
2113 {
2114 todo = func_hashtab.ht_used;
2115 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2116 if (!HASHITEM_EMPTY(hi))
2117 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118 // clear the def function index now
2119 fp = HI2UF(hi);
2120 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002121 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122
Bram Moolenaare38eab22019-12-05 21:50:01 +01002123 // Only free functions that are not refcounted, those are
2124 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002125 if (func_name_refcount(fp->uf_name))
2126 ++skipped;
2127 else
2128 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002129 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002130 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002131 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002132 {
2133 skipped = 0;
2134 break;
2135 }
2136 }
2137 --todo;
2138 }
2139 }
2140
Bram Moolenaare38eab22019-12-05 21:50:01 +01002141 // Now actually free the functions. Need to start all over every time,
2142 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002143 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002144 while (func_hashtab.ht_used > skipped)
2145 {
2146 todo = func_hashtab.ht_used;
2147 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002148 if (!HASHITEM_EMPTY(hi))
2149 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02002150 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002151 // Only free functions that are not refcounted, those are
2152 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02002153 fp = HI2UF(hi);
2154 if (func_name_refcount(fp->uf_name))
2155 ++skipped;
2156 else
2157 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02002158 if (func_free(fp, FALSE) == OK)
2159 {
2160 skipped = 0;
2161 break;
2162 }
2163 // did not actually free it
2164 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002165 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002166 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02002167 }
2168 if (skipped == 0)
2169 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002170
2171 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002172}
2173#endif
2174
2175/*
2176 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02002177 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002178 * "len" is the length of "name", or -1 for NUL terminated.
2179 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002181builtin_function(char_u *name, int len)
2182{
2183 char_u *p;
2184
Bram Moolenaara26b9702020-04-18 19:53:28 +02002185 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002186 return FALSE;
2187 p = vim_strchr(name, AUTOLOAD_CHAR);
2188 return p == NULL || (len > 0 && p > name + len);
2189}
2190
2191 int
2192func_call(
2193 char_u *name,
2194 typval_T *args,
2195 partial_T *partial,
2196 dict_T *selfdict,
2197 typval_T *rettv)
2198{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002199 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002200 listitem_T *item;
2201 typval_T argv[MAX_FUNC_ARGS + 1];
2202 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002203 int r = 0;
2204
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002205 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002206 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002207 {
2208 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
2209 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002210 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002211 break;
2212 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002213 // Make a copy of each argument. This is needed to be able to set
2214 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002215 copy_tv(&item->li_tv, &argv[argc++]);
2216 }
2217
2218 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002219 {
2220 funcexe_T funcexe;
2221
Bram Moolenaara80faa82020-04-12 19:37:17 +02002222 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002223 funcexe.firstline = curwin->w_cursor.lnum;
2224 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002225 funcexe.evaluate = TRUE;
2226 funcexe.partial = partial;
2227 funcexe.selfdict = selfdict;
2228 r = call_func(name, -1, rettv, argc, argv, &funcexe);
2229 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002230
Bram Moolenaare38eab22019-12-05 21:50:01 +01002231 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002232 while (argc > 0)
2233 clear_tv(&argv[--argc]);
2234
2235 return r;
2236}
2237
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002238static int callback_depth = 0;
2239
2240 int
2241get_callback_depth(void)
2242{
2243 return callback_depth;
2244}
2245
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002246/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002247 * Invoke call_func() with a callback.
2248 */
2249 int
2250call_callback(
2251 callback_T *callback,
2252 int len, // length of "name" or -1 to use strlen()
2253 typval_T *rettv, // return value goes here
2254 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002255 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002256 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002257{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002258 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002259 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002260
Bram Moolenaara80faa82020-04-12 19:37:17 +02002261 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002262 funcexe.evaluate = TRUE;
2263 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002264 ++callback_depth;
2265 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
2266 --callback_depth;
2267 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002268}
2269
2270/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 * Give an error message for the result of a function.
2272 * Nothing if "error" is FCERR_NONE.
2273 */
2274 void
2275user_func_error(int error, char_u *name)
2276{
2277 switch (error)
2278 {
2279 case FCERR_UNKNOWN:
2280 emsg_funcname(e_unknownfunc, name);
2281 break;
2282 case FCERR_NOTMETHOD:
2283 emsg_funcname(
2284 N_("E276: Cannot use function as a method: %s"), name);
2285 break;
2286 case FCERR_DELETED:
2287 emsg_funcname(N_(e_func_deleted), name);
2288 break;
2289 case FCERR_TOOMANY:
2290 emsg_funcname((char *)e_toomanyarg, name);
2291 break;
2292 case FCERR_TOOFEW:
2293 emsg_funcname((char *)e_toofewarg, name);
2294 break;
2295 case FCERR_SCRIPT:
2296 emsg_funcname(
2297 N_("E120: Using <SID> not in a script context: %s"), name);
2298 break;
2299 case FCERR_DICT:
2300 emsg_funcname(
2301 N_("E725: Calling dict function without Dictionary: %s"),
2302 name);
2303 break;
2304 }
2305}
2306
2307/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002308 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002309 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002310 * Return FAIL when the function can't be called, OK otherwise.
2311 * Also returns OK when an error was encountered while executing the function.
2312 */
2313 int
2314call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02002315 char_u *funcname, // name of the function
2316 int len, // length of "name" or -1 to use strlen()
2317 typval_T *rettv, // return value goes here
2318 int argcount_in, // number of "argvars"
2319 typval_T *argvars_in, // vars for arguments, must have "argcount"
2320 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002321 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002322{
2323 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01002324 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002325 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002326 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002327 char_u fname_buf[FLEN_FIXED + 1];
2328 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002329 char_u *fname = NULL;
2330 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002331 int argcount = argcount_in;
2332 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002333 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002334 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
2335 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002336 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002337 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002338 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002339
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02002340 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
2341 // even when call_func() returns FAIL.
2342 rettv->v_type = VAR_UNKNOWN;
2343
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002344 if (partial != NULL)
2345 fp = partial->pt_func;
2346 if (fp == NULL)
2347 {
2348 // Make a copy of the name, if it comes from a funcref variable it
2349 // could be changed or deleted in the called function.
2350 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
2351 if (name == NULL)
2352 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002353
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002354 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
2355 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002356
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002357 if (funcexe->doesrange != NULL)
2358 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002359
2360 if (partial != NULL)
2361 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002362 // When the function has a partial with a dict and there is a dict
2363 // argument, use the dict argument. That is backwards compatible.
2364 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002365 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002366 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01002367 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002368 {
2369 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002370 {
2371 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
2372 {
Bram Moolenaaref140542019-12-31 21:27:13 +01002373 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002374 goto theend;
2375 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002376 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002377 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002378 for (i = 0; i < argcount_in; ++i)
2379 argv[i + argv_clear] = argvars_in[i];
2380 argvars = argv;
2381 argcount = partial->pt_argc + argcount_in;
2382 }
2383 }
2384
Bram Moolenaaref140542019-12-31 21:27:13 +01002385 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002386 {
2387 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02002388 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002389
Bram Moolenaar333894b2020-08-01 18:53:07 +02002390 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002391 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02002392 {
2393 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002394 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02002395 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002396
Bram Moolenaare38eab22019-12-05 21:50:01 +01002397 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002398 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01002399 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002400
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002401 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002402 {
2403 /*
2404 * User defined function.
2405 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002406 if (fp == NULL)
Bram Moolenaar333894b2020-08-01 18:53:07 +02002407 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002408
Bram Moolenaare38eab22019-12-05 21:50:01 +01002409 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002410 if (fp == NULL
2411 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002412 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002413 && !aborting())
2414 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002415 // executed an autocommand, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02002416 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002417 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002418 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002419 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
2420 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002421 // loaded a package, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02002422 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002423 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02002424 if (fp == NULL)
2425 {
2426 char_u *p = untrans_function_name(rfname);
2427
2428 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02002429 // Don't do this if the name starts with "s:".
2430 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaar333894b2020-08-01 18:53:07 +02002431 fp = find_func(p, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002432 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002433
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002434 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01002435 error = FCERR_DELETED;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002436#ifdef FEAT_LUA
2437 else if (fp != NULL && (fp->uf_flags & FC_CFUNC))
2438 {
2439 cfunc_T cb = fp->uf_cb;
2440
2441 error = (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
2442 }
2443#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002444 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002445 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002446 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01002447 // postponed filling in the arguments, do it now
2448 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002449 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002450
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002451 if (funcexe->basetv != NULL)
2452 {
2453 // Method call: base->Method()
2454 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
2455 argv[0] = *funcexe->basetv;
2456 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002457 argvars = argv;
2458 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002459 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002461 error = call_user_func_check(fp, argcount, argvars, rettv,
2462 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002463 }
2464 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002465 else if (funcexe->basetv != NULL)
2466 {
2467 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002468 * expr->method(): Find the method name in the table, call its
2469 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02002470 */
2471 error = call_internal_method(fname, argcount, argvars, rettv,
2472 funcexe->basetv);
2473 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002474 else
2475 {
2476 /*
2477 * Find the function name in the table, call its implementation.
2478 */
2479 error = call_internal_func(fname, argcount, argvars, rettv);
2480 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02002481
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002482 /*
2483 * The function call (or "FuncUndefined" autocommand sequence) might
2484 * have been aborted by an error, an interrupt, or an explicitly thrown
2485 * exception that has not been caught so far. This situation can be
2486 * tested for by calling aborting(). For an error in an internal
2487 * function or for the "E132" error in call_user_func(), however, the
2488 * throw point at which the "force_abort" flag (temporarily reset by
2489 * emsg()) is normally updated has not been reached yet. We need to
2490 * update that flag first to make aborting() reliable.
2491 */
2492 update_force_abort();
2493 }
Bram Moolenaaref140542019-12-31 21:27:13 +01002494 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002495 ret = OK;
2496
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002497theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002498 /*
2499 * Report an error unless the argument evaluation or function call has been
2500 * cancelled due to an aborting error, an interrupt, or an exception.
2501 */
2502 if (!aborting())
2503 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002504 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002505 }
2506
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002507 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002508 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002509 clear_tv(&argv[--argv_clear + argv_base]);
2510
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002511 vim_free(tofree);
2512 vim_free(name);
2513
2514 return ret;
2515}
2516
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002517 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518printable_func_name(ufunc_T *fp)
2519{
2520 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
2521}
2522
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002523/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002524 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002525 */
2526 static void
2527list_func_head(ufunc_T *fp, int indent)
2528{
2529 int j;
2530
2531 msg_start();
2532 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002533 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002534 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002535 msg_puts("def ");
2536 else
2537 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002539 msg_putchar('(');
2540 for (j = 0; j < fp->uf_args.ga_len; ++j)
2541 {
2542 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002543 msg_puts(", ");
2544 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 if (fp->uf_arg_types != NULL)
2546 {
2547 char *tofree;
2548
2549 msg_puts(": ");
2550 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
2551 vim_free(tofree);
2552 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002553 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
2554 {
2555 msg_puts(" = ");
2556 msg_puts(((char **)(fp->uf_def_args.ga_data))
2557 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
2558 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002559 }
2560 if (fp->uf_varargs)
2561 {
2562 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002563 msg_puts(", ");
2564 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002565 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566 if (fp->uf_va_name != NULL)
2567 {
2568 if (j)
2569 msg_puts(", ");
2570 msg_puts("...");
2571 msg_puts((char *)fp->uf_va_name);
2572 if (fp->uf_va_type)
2573 {
2574 char *tofree;
2575
2576 msg_puts(": ");
2577 msg_puts(type_name(fp->uf_va_type, &tofree));
2578 vim_free(tofree);
2579 }
2580 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002581 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002582
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002583 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002584 {
2585 if (fp->uf_ret_type != &t_void)
2586 {
2587 char *tofree;
2588
2589 msg_puts(": ");
2590 msg_puts(type_name(fp->uf_ret_type, &tofree));
2591 vim_free(tofree);
2592 }
2593 }
2594 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002595 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002596 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002597 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002598 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002599 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002600 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002601 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002602 msg_clr_eos();
2603 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002604 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002605}
2606
2607/*
2608 * Get a function name, translating "<SID>" and "<SNR>".
2609 * Also handles a Funcref in a List or Dictionary.
2610 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002611 * Set "*is_global" to TRUE when the function must be global, unless
2612 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002613 * flags:
2614 * TFN_INT: internal function name OK
2615 * TFN_QUIET: be quiet
2616 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002617 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002618 * Advances "pp" to just after the function name (if no error).
2619 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002620 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002621trans_function_name(
2622 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002623 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002624 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002625 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002626 funcdict_T *fdp, // return: info about dictionary used
2627 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002628{
2629 char_u *name = NULL;
2630 char_u *start;
2631 char_u *end;
2632 int lead;
2633 char_u sid_buf[20];
2634 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002636 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 int vim9script;
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002638 static char *e_function_name = N_("E129: Function name required");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002639
2640 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02002641 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002642 start = *pp;
2643
Bram Moolenaare38eab22019-12-05 21:50:01 +01002644 // Check for hard coded <SNR>: already translated function ID (from a user
2645 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002646 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2647 && (*pp)[2] == (int)KE_SNR)
2648 {
2649 *pp += 3;
2650 len = get_id_len(pp) + 3;
2651 return vim_strnsave(start, len);
2652 }
2653
Bram Moolenaare38eab22019-12-05 21:50:01 +01002654 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2655 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002656 lead = eval_fname_script(start);
2657 if (lead > 2)
2658 start += lead;
2659
Bram Moolenaare38eab22019-12-05 21:50:01 +01002660 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002661 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002662 lead > 2 ? 0 : FNE_CHECK_START);
2663 if (end == start)
2664 {
2665 if (!skip)
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002666 emsg(_(e_function_name));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002667 goto theend;
2668 }
2669 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2670 {
2671 /*
2672 * Report an invalid expression in braces, unless the expression
2673 * evaluation has been cancelled due to an aborting error, an
2674 * interrupt, or an exception.
2675 */
2676 if (!aborting())
2677 {
2678 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002679 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002680 }
2681 else
2682 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2683 goto theend;
2684 }
2685
2686 if (lv.ll_tv != NULL)
2687 {
2688 if (fdp != NULL)
2689 {
2690 fdp->fd_dict = lv.ll_dict;
2691 fdp->fd_newkey = lv.ll_newkey;
2692 lv.ll_newkey = NULL;
2693 fdp->fd_di = lv.ll_di;
2694 }
2695 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2696 {
2697 name = vim_strsave(lv.ll_tv->vval.v_string);
2698 *pp = end;
2699 }
2700 else if (lv.ll_tv->v_type == VAR_PARTIAL
2701 && lv.ll_tv->vval.v_partial != NULL)
2702 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002703 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002704 *pp = end;
2705 if (partial != NULL)
2706 *partial = lv.ll_tv->vval.v_partial;
2707 }
2708 else
2709 {
2710 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2711 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002712 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002713 else
2714 *pp = end;
2715 name = NULL;
2716 }
2717 goto theend;
2718 }
2719
2720 if (lv.ll_name == NULL)
2721 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002722 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002723 *pp = end;
2724 goto theend;
2725 }
2726
Bram Moolenaare38eab22019-12-05 21:50:01 +01002727 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002728 if (lv.ll_exp_name != NULL)
2729 {
2730 len = (int)STRLEN(lv.ll_exp_name);
2731 name = deref_func_name(lv.ll_exp_name, &len, partial,
2732 flags & TFN_NO_AUTOLOAD);
2733 if (name == lv.ll_exp_name)
2734 name = NULL;
2735 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002736 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002737 {
2738 len = (int)(end - *pp);
2739 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2740 if (name == *pp)
2741 name = NULL;
2742 }
2743 if (name != NULL)
2744 {
2745 name = vim_strsave(name);
2746 *pp = end;
2747 if (STRNCMP(name, "<SNR>", 5) == 0)
2748 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002749 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002750 name[0] = K_SPECIAL;
2751 name[1] = KS_EXTRA;
2752 name[2] = (int)KE_SNR;
2753 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2754 }
2755 goto theend;
2756 }
2757
2758 if (lv.ll_exp_name != NULL)
2759 {
2760 len = (int)STRLEN(lv.ll_exp_name);
2761 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2762 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2763 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002764 // When there was "s:" already or the name expanded to get a
2765 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002766 lv.ll_name += 2;
2767 len -= 2;
2768 lead = 2;
2769 }
2770 }
2771 else
2772 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002773 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002774 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002775 {
2776 if (is_global != NULL && lv.ll_name[0] == 'g')
2777 *is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002778 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002779 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002780 len = (int)(end - lv.ll_name);
2781 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002782 if (len <= 0)
2783 {
2784 if (!skip)
2785 emsg(_(e_function_name));
2786 goto theend;
2787 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002788
Bram Moolenaar17f700a2020-12-19 21:23:42 +01002789 // In Vim9 script a user function is script-local by default, unless it
2790 // starts with a lower case character: dict.func().
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002791 vim9script = ASCII_ISUPPER(*start) && in_vim9script();
Bram Moolenaar17f700a2020-12-19 21:23:42 +01002792 if (vim9script)
2793 {
2794 char_u *p;
2795
2796 // SomeScript#func() is a global function.
2797 for (p = start; *p != NUL && *p != '('; ++p)
2798 if (*p == AUTOLOAD_CHAR)
2799 vim9script = FALSE;
2800 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002802 /*
2803 * Copy the function name to allocated memory.
2804 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2805 * Accept <SNR>123_name() outside a script.
2806 */
2807 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002808 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002809 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002810 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 if (!vim9script)
2812 lead = 3;
2813 if (vim9script || (lv.ll_exp_name != NULL
2814 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002815 || eval_fname_sid(*pp))
2816 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002818 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002819 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002820 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002821 goto theend;
2822 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002823 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 if (vim9script)
2825 extra = 3 + (int)STRLEN(sid_buf);
2826 else
2827 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002828 }
2829 }
2830 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2831 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002832 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002833 start);
2834 goto theend;
2835 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002836 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002837 {
2838 char_u *cp = vim_strchr(lv.ll_name, ':');
2839
2840 if (cp != NULL && cp < end)
2841 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002842 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002843 goto theend;
2844 }
2845 }
2846
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002848 if (name != NULL)
2849 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002850 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002851 {
2852 name[0] = K_SPECIAL;
2853 name[1] = KS_EXTRA;
2854 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002856 STRCPY(name + 3, sid_buf);
2857 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2859 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002860 }
2861 *pp = end;
2862
2863theend:
2864 clear_lval(&lv);
2865 return name;
2866}
2867
2868/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02002869 * Assuming "name" is the result of trans_function_name() and it was prefixed
2870 * to use the script-local name, return the unmodified name (points into
2871 * "name"). Otherwise return NULL.
2872 * This can be used to first search for a script-local function and fall back
2873 * to the global function if not found.
2874 */
2875 char_u *
2876untrans_function_name(char_u *name)
2877{
2878 char_u *p;
2879
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002880 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02002881 {
2882 p = vim_strchr(name, '_');
2883 if (p != NULL)
2884 return p + 1;
2885 }
2886 return NULL;
2887}
2888
2889/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002890 * List functions. When "regmatch" is NULL all of then.
2891 * Otherwise functions matching "regmatch".
2892 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002893 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002894list_functions(regmatch_T *regmatch)
2895{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002896 int changed = func_hashtab.ht_changed;
2897 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002898 hashitem_T *hi;
2899
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002900 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002901 {
2902 if (!HASHITEM_EMPTY(hi))
2903 {
2904 ufunc_T *fp = HI2UF(hi);
2905
2906 --todo;
2907 if ((fp->uf_flags & FC_DEAD) == 0
2908 && (regmatch == NULL
2909 ? !message_filtered(fp->uf_name)
2910 && !func_name_refcount(fp->uf_name)
2911 : !isdigit(*fp->uf_name)
2912 && vim_regexec(regmatch, fp->uf_name, 0)))
2913 {
2914 list_func_head(fp, FALSE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002915 if (changed != func_hashtab.ht_changed)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002916 {
2917 emsg(_("E454: function list was modified"));
2918 return;
2919 }
2920 }
2921 }
2922 }
2923}
2924
2925/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02002926 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002927 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
2928 * the function name.
Bram Moolenaar04b12692020-05-04 23:24:44 +02002929 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002930 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02002931 ufunc_T *
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002932define_function(exarg_T *eap, char_u *name_arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002933{
2934 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002935 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002936 int j;
2937 int c;
2938 int saved_did_emsg;
2939 int saved_wait_return = need_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002940 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002941 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002942 char_u *p;
2943 char_u *arg;
2944 char_u *line_arg = NULL;
2945 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002947 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002948 garray_T newlines;
2949 int varargs = FALSE;
2950 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002952 ufunc_T *fp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002953 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002954 int indent;
2955 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956#define MAX_FUNC_NESTING 50
2957 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002958 dictitem_T *v;
2959 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002960 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002961 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002962 hashitem_T *hi;
Bram Moolenaar66250c92020-08-20 15:02:42 +02002963 getline_opt_T getline_options = GETLINE_CONCAT_CONT;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002964 linenr_T sourcing_lnum_off;
2965 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002966 int is_heredoc = FALSE;
2967 char_u *skip_until = NULL;
2968 char_u *heredoc_trimmed = NULL;
Bram Moolenaare7e48382020-07-22 18:17:08 +02002969 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02002970 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002971
2972 /*
2973 * ":function" without argument: list functions.
2974 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002975 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002976 {
2977 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002978 list_functions(NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002979 eap->nextcmd = check_nextcmd(eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02002980 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002981 }
2982
2983 /*
2984 * ":function /pat": list functions matching pattern.
2985 */
2986 if (*eap->arg == '/')
2987 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002988 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002989 if (!eap->skip)
2990 {
2991 regmatch_T regmatch;
2992
2993 c = *p;
2994 *p = NUL;
2995 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2996 *p = c;
2997 if (regmatch.regprog != NULL)
2998 {
2999 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003000 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003001 vim_regfree(regmatch.regprog);
3002 }
3003 }
3004 if (*p == '/')
3005 ++p;
3006 eap->nextcmd = check_nextcmd(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003007 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003008 }
3009
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 ga_init(&newargs);
3011 ga_init(&argtypes);
3012 ga_init(&default_args);
3013
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003014 /*
3015 * Get the function name. There are these situations:
3016 * func normal function name
3017 * "name" == func, "fudi.fd_dict" == NULL
3018 * dict.func new dictionary entry
3019 * "name" == NULL, "fudi.fd_dict" set,
3020 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
3021 * dict.func existing dict entry with a Funcref
3022 * "name" == func, "fudi.fd_dict" set,
3023 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
3024 * dict.func existing dict entry that's not a Funcref
3025 * "name" == NULL, "fudi.fd_dict" set,
3026 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
3027 * s:func script-local function name
3028 * g:func global function name, same as "func"
3029 */
3030 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003031 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003032 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02003033 // nested function, argument is (args).
3034 paren = TRUE;
3035 CLEAR_FIELD(fudi);
3036 }
3037 else
3038 {
3039 name = trans_function_name(&p, &is_global, eap->skip,
3040 TFN_NO_AUTOLOAD, &fudi, NULL);
3041 paren = (vim_strchr(p, '(') != NULL);
3042 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003043 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02003044 /*
3045 * Return on an invalid expression in braces, unless the expression
3046 * evaluation has been cancelled due to an aborting error, an
3047 * interrupt, or an exception.
3048 */
3049 if (!aborting())
3050 {
3051 if (!eap->skip && fudi.fd_newkey != NULL)
3052 semsg(_(e_dictkey), fudi.fd_newkey);
3053 vim_free(fudi.fd_newkey);
3054 return NULL;
3055 }
3056 else
3057 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003058 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003059 }
3060
Bram Moolenaare38eab22019-12-05 21:50:01 +01003061 // An error in a function call during evaluation of an expression in magic
3062 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003063 saved_did_emsg = did_emsg;
3064 did_emsg = FALSE;
3065
3066 /*
3067 * ":function func" with only function name: list function.
3068 */
3069 if (!paren)
3070 {
3071 if (!ends_excmd(*skipwhite(p)))
3072 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003073 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003074 goto ret_free;
3075 }
3076 eap->nextcmd = check_nextcmd(p);
3077 if (eap->nextcmd != NULL)
3078 *p = NUL;
3079 if (!eap->skip && !got_int)
3080 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003081 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003082 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
3083 {
3084 char_u *up = untrans_function_name(name);
3085
3086 // With Vim9 script the name was made script-local, if not
3087 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02003088 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003089 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003090 }
3091
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003092 if (fp != NULL)
3093 {
3094 list_func_head(fp, TRUE);
3095 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
3096 {
3097 if (FUNCLINE(fp, j) == NULL)
3098 continue;
3099 msg_putchar('\n');
3100 msg_outnum((long)(j + 1));
3101 if (j < 9)
3102 msg_putchar(' ');
3103 if (j < 99)
3104 msg_putchar(' ');
3105 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003106 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003107 ui_breakcheck();
3108 }
3109 if (!got_int)
3110 {
3111 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003112 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 msg_puts(" enddef");
3114 else
3115 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003116 }
3117 }
3118 else
Bram Moolenaara26b9702020-04-18 19:53:28 +02003119 emsg_funcname(N_("E123: Undefined function: %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003120 }
3121 goto ret_free;
3122 }
3123
3124 /*
3125 * ":function name(arg1, arg2)" Define function.
3126 */
3127 p = skipwhite(p);
3128 if (*p != '(')
3129 {
3130 if (!eap->skip)
3131 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003132 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003133 goto ret_free;
3134 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003135 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003136 if (vim_strchr(p, '(') != NULL)
3137 p = vim_strchr(p, '(');
3138 }
3139 p = skipwhite(p + 1);
3140
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02003141 // In Vim9 script only global functions can be redefined.
3142 if (vim9script && eap->forceit && !is_global)
3143 {
3144 emsg(_(e_nobang));
3145 goto ret_free;
3146 }
3147
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003148 ga_init2(&newlines, (int)sizeof(char_u *), 3);
3149
Bram Moolenaar04b12692020-05-04 23:24:44 +02003150 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003151 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003152 // Check the name of the function. Unless it's a dictionary function
3153 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003154 if (name != NULL)
3155 arg = name;
3156 else
3157 arg = fudi.fd_newkey;
3158 if (arg != NULL && (fudi.fd_di == NULL
3159 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
3160 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
3161 {
3162 if (*arg == K_SPECIAL)
3163 j = 3;
3164 else
3165 j = 0;
3166 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
3167 : eval_isnamec(arg[j])))
3168 ++j;
3169 if (arg[j] != NUL)
3170 emsg_funcname((char *)e_invarg2, arg);
3171 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003172 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003173 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003174 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003175 }
3176
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003177 // This may get more lines and make the pointers into the first line
3178 // invalid.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003180 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003181 &varargs, &default_args, eap->skip,
3182 eap, &line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003183 goto errret_2;
3184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003186 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 // find the return type: :def Func(): type
3188 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003189 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02003191 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003193 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003194 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003196 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003198 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003199 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02003200 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003201 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003202 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02003203 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003204 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 else
3206 // find extra arguments "range", "dict", "abort" and "closure"
3207 for (;;)
3208 {
3209 p = skipwhite(p);
3210 if (STRNCMP(p, "range", 5) == 0)
3211 {
3212 flags |= FC_RANGE;
3213 p += 5;
3214 }
3215 else if (STRNCMP(p, "dict", 4) == 0)
3216 {
3217 flags |= FC_DICT;
3218 p += 4;
3219 }
3220 else if (STRNCMP(p, "abort", 5) == 0)
3221 {
3222 flags |= FC_ABORT;
3223 p += 5;
3224 }
3225 else if (STRNCMP(p, "closure", 7) == 0)
3226 {
3227 flags |= FC_CLOSURE;
3228 p += 7;
3229 if (current_funccal == NULL)
3230 {
3231 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
3232 name == NULL ? (char_u *)"" : name);
3233 goto erret;
3234 }
3235 }
3236 else
3237 break;
3238 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003239
Bram Moolenaare38eab22019-12-05 21:50:01 +01003240 // When there is a line break use what follows for the function body.
3241 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003242 if (*p == '\n')
3243 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003244 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02003245 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
3246 && eap->cmdidx != CMD_def)
Bram Moolenaare7e48382020-07-22 18:17:08 +02003247 && !(*p == '#' && (vim9script || eap->cmdidx == CMD_def))
3248 && !eap->skip
3249 && !did_emsg)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003250 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003251
3252 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
3254 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003255 */
3256 if (KeyTyped)
3257 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003258 // Check if the function already exists, don't let the user type the
3259 // whole function before telling him it doesn't work! For a script we
3260 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003261 if (!eap->skip && !eap->forceit)
3262 {
3263 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003264 emsg(_(e_funcdict));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003265 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003266 emsg_funcname(e_funcexts, name);
3267 }
3268
3269 if (!eap->skip && did_emsg)
3270 goto erret;
3271
Bram Moolenaare38eab22019-12-05 21:50:01 +01003272 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003273 cmdline_row = msg_row;
3274 }
3275
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003276 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003277 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003278
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02003279 // Detect having skipped over comment lines to find the return
3280 // type. Add NULL lines to keep the line count correct.
3281 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
3282 if (SOURCING_LNUM < sourcing_lnum_off)
3283 {
3284 sourcing_lnum_off -= SOURCING_LNUM;
3285 if (ga_grow(&newlines, sourcing_lnum_off) == FAIL)
3286 goto erret;
3287 while (sourcing_lnum_off-- > 0)
3288 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3289 }
3290
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003291 indent = 2;
3292 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003294 for (;;)
3295 {
3296 if (KeyTyped)
3297 {
3298 msg_scroll = TRUE;
3299 saved_wait_return = FALSE;
3300 }
3301 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003302
3303 if (line_arg != NULL)
3304 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003305 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003306 theline = line_arg;
3307 p = vim_strchr(theline, '\n');
3308 if (p == NULL)
3309 line_arg += STRLEN(line_arg);
3310 else
3311 {
3312 *p = NUL;
3313 line_arg = p + 1;
3314 }
3315 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003316 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02003317 {
3318 vim_free(line_to_free);
3319 if (eap->getline == NULL)
Bram Moolenaar66250c92020-08-20 15:02:42 +02003320 theline = getcmdline(':', 0L, indent, getline_options);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003321 else
Bram Moolenaar66250c92020-08-20 15:02:42 +02003322 theline = eap->getline(':', eap->cookie, indent,
3323 getline_options);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003324 line_to_free = theline;
3325 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003326 if (KeyTyped)
3327 lines_left = Rows - 1;
3328 if (theline == NULL)
3329 {
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003330 if (skip_until != NULL)
3331 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
3332 else if (eap->cmdidx == CMD_def)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003333 emsg(_(e_missing_enddef));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 else
3335 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003336 goto erret;
3337 }
3338
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003339 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003340 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003341 if (SOURCING_LNUM < sourcing_lnum_off)
3342 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003343 else
3344 sourcing_lnum_off = 0;
3345
3346 if (skip_until != NULL)
3347 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003349 // * ":append" and "."
3350 // * ":python <<EOF" and "EOF"
3351 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
3352 if (heredoc_trimmed == NULL
3353 || (is_heredoc && skipwhite(theline) == theline)
3354 || STRNCMP(theline, heredoc_trimmed,
3355 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003356 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003357 if (heredoc_trimmed == NULL)
3358 p = theline;
3359 else if (is_heredoc)
3360 p = skipwhite(theline) == theline
3361 ? theline : theline + STRLEN(heredoc_trimmed);
3362 else
3363 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02003364 if (STRCMP(p, skip_until) == 0)
3365 {
3366 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003367 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003368 getline_options = GETLINE_CONCAT_CONT;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003369 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003370 }
3371 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003372 }
3373 else
3374 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003375 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01003376 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003377 ;
3378
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 // Check for "endfunction" or "enddef".
3380 if (checkforcmd(&p, nesting_def[nesting]
3381 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003382 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02003383 char_u *nextcmd = NULL;
3384
Bram Moolenaar663bb232017-06-22 19:12:10 +02003385 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02003386 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02003387 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02003388 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02003389 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 give_warning2(eap->cmdidx == CMD_def
3391 ? (char_u *)_("W1001: Text found after :enddef: %s")
3392 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003393 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003394 if (nextcmd != NULL)
3395 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003396 // Another command follows. If the line came from "eap" we
3397 // can simply point into it, otherwise we need to change
3398 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02003399 eap->nextcmd = nextcmd;
3400 if (line_to_free != NULL)
3401 {
3402 vim_free(*eap->cmdlinep);
3403 *eap->cmdlinep = line_to_free;
3404 line_to_free = NULL;
3405 }
3406 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003407 break;
3408 }
3409
Bram Moolenaare38eab22019-12-05 21:50:01 +01003410 // Increase indent inside "if", "while", "for" and "try", decrease
3411 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003413 indent -= 2;
3414 else if (STRNCMP(p, "if", 2) == 0
3415 || STRNCMP(p, "wh", 2) == 0
3416 || STRNCMP(p, "for", 3) == 0
3417 || STRNCMP(p, "try", 3) == 0)
3418 indent += 2;
3419
Bram Moolenaare38eab22019-12-05 21:50:01 +01003420 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01003421 // Only recognize "def" inside "def", not inside "function",
3422 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 c = *p;
Bram Moolenaar673660a2020-01-26 16:50:05 +01003424 if (checkforcmd(&p, "function", 2)
3425 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003426 {
3427 if (*p == '!')
3428 p = skipwhite(p + 1);
3429 p += eval_fname_script(p);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003430 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL, NULL));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003431 if (*skipwhite(p) == '(')
3432 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433 if (nesting == MAX_FUNC_NESTING - 1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003434 emsg(_(e_function_nesting_too_deep));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 else
3436 {
3437 ++nesting;
3438 nesting_def[nesting] = (c == 'd');
3439 indent += 2;
3440 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003441 }
3442 }
3443
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003444 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003445 p = skip_range(p, FALSE, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003446 if (eap->cmdidx != CMD_def
3447 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01003448 || (p[0] == 'c'
3449 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
3450 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
3451 && (STRNCMP(&p[3], "nge", 3) != 0
3452 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003453 || (p[0] == 'i'
3454 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003455 && (!ASCII_ISALPHA(p[2])
3456 || (p[2] == 's'
3457 && (!ASCII_ISALPHA(p[3])
3458 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003459 skip_until = vim_strsave((char_u *)".");
3460
Bram Moolenaare38eab22019-12-05 21:50:01 +01003461 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003462 arg = skipwhite(skiptowhite(p));
3463 if (arg[0] == '<' && arg[1] =='<'
3464 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003465 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
3466 || ((p[2] == '3' || p[2] == 'x')
3467 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003468 || (p[0] == 'p' && p[1] == 'e'
3469 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
3470 || (p[0] == 't' && p[1] == 'c'
3471 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
3472 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
3473 && !ASCII_ISALPHA(p[3]))
3474 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
3475 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
3476 || (p[0] == 'm' && p[1] == 'z'
3477 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
3478 ))
3479 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003480 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003481 p = skipwhite(arg + 2);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003482 if (STRNCMP(p, "trim", 4) == 0)
3483 {
3484 // Ignore leading white space.
3485 p = skipwhite(p + 4);
3486 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003487 skipwhite(theline) - theline);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003488 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003489 if (*p == NUL)
3490 skip_until = vim_strsave((char_u *)".");
3491 else
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003492 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003493 getline_options = GETLINE_NONE;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003494 is_heredoc = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003495 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003496
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003497 // Check for ":cmd v =<< [trim] EOF"
3498 // and ":cmd [a, b] =<< [trim] EOF"
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003499 // and "lines =<< [trim] EOF" for Vim9
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003500 // Where "cmd" can be "let", "var", "final" or "const".
Bram Moolenaar8471e572019-05-19 21:37:18 +02003501 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003502 if (*arg == '[')
3503 arg = vim_strchr(arg, ']');
3504 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003505 {
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003506 int found = (eap->cmdidx == CMD_def && arg[0] == '='
3507 && arg[1] == '<' && arg[2] =='<');
3508
3509 if (!found)
3510 // skip over the argument after "cmd"
3511 arg = skipwhite(skiptowhite(arg));
3512 if (found || (arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003513 && (checkforcmd(&p, "let", 2)
3514 || checkforcmd(&p, "var", 3)
3515 || checkforcmd(&p, "final", 5)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003516 || checkforcmd(&p, "const", 5))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02003517 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003518 p = skipwhite(arg + 3);
3519 if (STRNCMP(p, "trim", 4) == 0)
3520 {
3521 // Ignore leading white space.
3522 p = skipwhite(p + 4);
3523 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003524 skipwhite(theline) - theline);
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003525 }
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003526 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003527 getline_options = GETLINE_NONE;
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003528 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003529 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003530 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003531 }
3532
Bram Moolenaare38eab22019-12-05 21:50:01 +01003533 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003534 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003535 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003536
Bram Moolenaare38eab22019-12-05 21:50:01 +01003537 // Copy the line to newly allocated memory. get_one_sourceline()
3538 // allocates 250 bytes per line, this saves 80% on average. The cost
3539 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003540 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003541 if (p == NULL)
3542 goto erret;
3543 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003544
Bram Moolenaare38eab22019-12-05 21:50:01 +01003545 // Add NULL lines for continuation lines, so that the line count is
3546 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003547 while (sourcing_lnum_off-- > 0)
3548 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3549
Bram Moolenaare38eab22019-12-05 21:50:01 +01003550 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003551 if (line_arg != NULL && *line_arg == NUL)
3552 line_arg = NULL;
3553 }
3554
Bram Moolenaare38eab22019-12-05 21:50:01 +01003555 // Don't define the function when skipping commands or when an error was
3556 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003557 if (eap->skip || did_emsg)
3558 goto erret;
3559
3560 /*
3561 * If there are no errors, add the function
3562 */
3563 if (fudi.fd_dict == NULL)
3564 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 hashtab_T *ht;
3566
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003567 v = find_var(name, &ht, FALSE);
3568 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
3569 {
3570 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
3571 name);
3572 goto erret;
3573 }
3574
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003575 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaareef21022020-08-01 22:16:43 +02003576 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003577 {
Bram Moolenaareef21022020-08-01 22:16:43 +02003578 char_u *uname = untrans_function_name(name);
3579
3580 import = find_imported(uname == NULL ? name : uname, 0, NULL);
3581 }
3582
3583 if (fp != NULL || import != NULL)
3584 {
3585 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003587 // Function can be replaced with "function!" and when sourcing the
3588 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02003589 // A name that is used by an import can not be overruled.
3590 if (import != NULL
3591 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003592 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02003593 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003594 {
Bram Moolenaareef21022020-08-01 22:16:43 +02003595 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003596 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02003597 else
3598 emsg_funcname(e_funcexts, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003599 goto erret;
3600 }
3601 if (fp->uf_calls > 0)
3602 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003603 emsg_funcname(
3604 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003605 name);
3606 goto erret;
3607 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003608 if (fp->uf_refcount > 1)
3609 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003610 // This function is referenced somewhere, don't redefine it but
3611 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003612 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003613 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003614 fp = NULL;
3615 overwrite = TRUE;
3616 }
3617 else
3618 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003619 char_u *exp_name = fp->uf_name_exp;
3620
3621 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01003622 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003623 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003624 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003625 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003627#ifdef FEAT_PROFILE
3628 fp->uf_profiling = FALSE;
3629 fp->uf_prof_initialized = FALSE;
3630#endif
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003631 unlink_def_function(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003632 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003633 }
3634 }
3635 else
3636 {
3637 char numbuf[20];
3638
3639 fp = NULL;
3640 if (fudi.fd_newkey == NULL && !eap->forceit)
3641 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003642 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003643 goto erret;
3644 }
3645 if (fudi.fd_di == NULL)
3646 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003647 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02003648 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003649 goto erret;
3650 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003651 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02003652 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003653 goto erret;
3654
Bram Moolenaare38eab22019-12-05 21:50:01 +01003655 // Give the function a sequential number. Can only be used with a
3656 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003657 vim_free(name);
3658 sprintf(numbuf, "%d", ++func_nr);
3659 name = vim_strsave((char_u *)numbuf);
3660 if (name == NULL)
3661 goto erret;
3662 }
3663
3664 if (fp == NULL)
3665 {
3666 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3667 {
3668 int slen, plen;
3669 char_u *scriptname;
3670
Bram Moolenaare38eab22019-12-05 21:50:01 +01003671 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003672 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003673 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003674 {
3675 scriptname = autoload_name(name);
3676 if (scriptname != NULL)
3677 {
3678 p = vim_strchr(scriptname, '/');
3679 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003680 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003681 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003682 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003683 j = OK;
3684 vim_free(scriptname);
3685 }
3686 }
3687 if (j == FAIL)
3688 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003689 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003690 goto erret;
3691 }
3692 }
3693
Bram Moolenaar47ed5532019-08-08 20:49:14 +02003694 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003695 if (fp == NULL)
3696 goto erret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003697 fp->uf_def_status = eap->cmdidx == CMD_def ? UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003698 : UF_NOT_COMPILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003699
3700 if (fudi.fd_dict != NULL)
3701 {
3702 if (fudi.fd_di == NULL)
3703 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003704 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003705 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
3706 if (fudi.fd_di == NULL)
3707 {
3708 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003709 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003710 goto erret;
3711 }
3712 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
3713 {
3714 vim_free(fudi.fd_di);
3715 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003716 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003717 goto erret;
3718 }
3719 }
3720 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003721 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003722 clear_tv(&fudi.fd_di->di_tv);
3723 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003724 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003725
Bram Moolenaare38eab22019-12-05 21:50:01 +01003726 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003727 flags |= FC_DICT;
3728 }
3729
Bram Moolenaare38eab22019-12-05 21:50:01 +01003730 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003731 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003732 if (overwrite)
3733 {
3734 hi = hash_find(&func_hashtab, name);
3735 hi->hi_key = UF2HIKEY(fp);
3736 }
3737 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003738 {
3739 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003740 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003741 goto erret;
3742 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003743 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003744 }
3745 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003746 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003748 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749
3750 if (eap->cmdidx == CMD_def)
3751 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003752 int lnum_save = SOURCING_LNUM;
3753 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003754
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003755 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003756
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003757 // error messages are for the first function line
3758 SOURCING_LNUM = sourcing_lnum_top;
3759
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003760 if (cstack != NULL && cstack->cs_idx >= 0)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003761 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003762 int count = cstack->cs_idx + 1;
3763 int i;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003764
3765 // The block context may be needed for script variables declared in
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003766 // a block that is visible now but not when the function is called
3767 // later.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003768 fp->uf_block_ids = ALLOC_MULT(int, count);
3769 if (fp->uf_block_ids != NULL)
3770 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003771 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003772 sizeof(int) * count);
3773 fp->uf_block_depth = count;
3774 }
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003775
3776 // Set flag in each block to indicate a function was defined. This
3777 // is used to keep the variable when leaving the block, see
3778 // hide_script_var().
3779 for (i = 0; i <= cstack->cs_idx; ++i)
3780 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003781 }
3782
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003783 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003785 SOURCING_LNUM = lnum_save;
3786 goto errret_2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003788 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789
3790 // parse the return type, if any
3791 if (ret_type == NULL)
3792 fp->uf_ret_type = &t_void;
3793 else
3794 {
3795 p = ret_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01003796 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02003798 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003800 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003801 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003803 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003804 if ((flags & FC_CLOSURE) != 0)
3805 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003806 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003807 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003808 }
3809 else
3810 fp->uf_scoped = NULL;
3811
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003812#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003813 if (prof_def_func())
3814 func_do_profile(fp);
3815#endif
3816 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003817 if (sandbox)
3818 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003819 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003820 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003821 fp->uf_flags = flags;
3822 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003823 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003824 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02003825 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003826 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 if (is_export)
3828 {
3829 fp->uf_flags |= FC_EXPORT;
3830 // let ex_export() know the export worked.
3831 is_export = FALSE;
3832 }
3833
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003834 if (eap->cmdidx == CMD_def)
3835 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02003836 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
3837 // :func does not use Vim9 script syntax, even in a Vim9 script file
3838 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003839
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003840 goto ret_free;
3841
3842erret:
3843 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003844 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003845errret_2:
3846 ga_clear_strings(&newlines);
3847ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003848 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003849 vim_free(skip_until);
Bram Moolenaarfffdf472020-12-13 21:16:55 +01003850 vim_free(heredoc_trimmed);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003851 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003852 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003853 if (name != name_arg)
3854 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003855 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003856 did_emsg |= saved_did_emsg;
3857 need_wait_return |= saved_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003858
3859 return fp;
3860}
3861
3862/*
3863 * ":function"
3864 */
3865 void
3866ex_function(exarg_T *eap)
3867{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003868 (void)define_function(eap, NULL);
Bram Moolenaar822ba242020-05-24 23:00:18 +02003869}
3870
3871/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02003872 * :defcompile - compile all :def functions in the current script that need to
3873 * be compiled. Except dead functions.
Bram Moolenaar822ba242020-05-24 23:00:18 +02003874 */
3875 void
3876ex_defcompile(exarg_T *eap UNUSED)
3877{
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003878 long todo = (long)func_hashtab.ht_used;
3879 int changed = func_hashtab.ht_changed;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003880 hashitem_T *hi;
3881 ufunc_T *ufunc;
3882
3883 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
3884 {
3885 if (!HASHITEM_EMPTY(hi))
3886 {
3887 --todo;
3888 ufunc = HI2UF(hi);
3889 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
Bram Moolenaar96f8f492020-09-09 17:08:51 +02003890 && ufunc->uf_def_status == UF_TO_BE_COMPILED
3891 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003892 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02003893 compile_def_function(ufunc, FALSE, NULL);
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003894
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003895 if (func_hashtab.ht_changed != changed)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003896 {
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003897 // a function has been added or removed, need to start over
3898 todo = (long)func_hashtab.ht_used;
3899 changed = func_hashtab.ht_changed;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003900 hi = func_hashtab.ht_array;
Bram Moolenaar285b1892020-05-26 11:37:26 +02003901 --hi;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003902 }
3903 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003904 }
3905 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003906}
3907
3908/*
3909 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3910 * Return 2 if "p" starts with "s:".
3911 * Return 0 otherwise.
3912 */
3913 int
3914eval_fname_script(char_u *p)
3915{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003916 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3917 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003918 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3919 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3920 return 5;
3921 if (p[0] == 's' && p[1] == ':')
3922 return 2;
3923 return 0;
3924}
3925
3926 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003927translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003928{
3929 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003930 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003931 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932}
3933
3934/*
3935 * Return TRUE when "ufunc" has old-style "..." varargs
3936 * or named varargs "...name: type".
3937 */
3938 int
3939has_varargs(ufunc_T *ufunc)
3940{
3941 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003942}
3943
3944/*
3945 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003946 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003947 */
3948 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003949function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003950{
3951 char_u *nm = name;
3952 char_u *p;
3953 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003954 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003955 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003956
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003957 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3958 if (no_deref)
3959 flag |= TFN_NO_DEREF;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003960 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003961 nm = skipwhite(nm);
3962
Bram Moolenaare38eab22019-12-05 21:50:01 +01003963 // Only accept "funcname", "funcname ", "funcname (..." and
3964 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003965 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003966 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003967 vim_free(p);
3968 return n;
3969}
3970
Bram Moolenaar113e1072019-01-20 15:30:40 +01003971#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003972 char_u *
3973get_expanded_name(char_u *name, int check)
3974{
3975 char_u *nm = name;
3976 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003977 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003978
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003979 p = trans_function_name(&nm, &is_global, FALSE,
3980 TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003981
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003982 if (p != NULL && *nm == NUL
3983 && (!check || translated_function_exists(p, is_global)))
3984 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003985
3986 vim_free(p);
3987 return NULL;
3988}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003989#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003990
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003991/*
3992 * Function given to ExpandGeneric() to obtain the list of user defined
3993 * function names.
3994 */
3995 char_u *
3996get_user_func_name(expand_T *xp, int idx)
3997{
3998 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003999 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004000 static hashitem_T *hi;
4001 ufunc_T *fp;
4002
4003 if (idx == 0)
4004 {
4005 done = 0;
4006 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004007 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004008 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004009 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004010 {
4011 if (done++ > 0)
4012 ++hi;
4013 while (HASHITEM_EMPTY(hi))
4014 ++hi;
4015 fp = HI2UF(hi);
4016
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 // don't show dead, dict and lambda functions
4018 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02004019 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004021
4022 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004023 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004024
4025 cat_func_name(IObuff, fp);
4026 if (xp->xp_context != EXPAND_USER_FUNC)
4027 {
4028 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004030 STRCAT(IObuff, ")");
4031 }
4032 return IObuff;
4033 }
4034 return NULL;
4035}
4036
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004037/*
4038 * ":delfunction {name}"
4039 */
4040 void
4041ex_delfunction(exarg_T *eap)
4042{
4043 ufunc_T *fp = NULL;
4044 char_u *p;
4045 char_u *name;
4046 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004047 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004048
4049 p = eap->arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004050 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004051 vim_free(fudi.fd_newkey);
4052 if (name == NULL)
4053 {
4054 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004055 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004056 return;
4057 }
4058 if (!ends_excmd(*skipwhite(p)))
4059 {
4060 vim_free(name);
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004061 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004062 return;
4063 }
4064 eap->nextcmd = check_nextcmd(p);
4065 if (eap->nextcmd != NULL)
4066 *p = NUL;
4067
4068 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004069 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004070 vim_free(name);
4071
4072 if (!eap->skip)
4073 {
4074 if (fp == NULL)
4075 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02004076 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004077 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004078 return;
4079 }
4080 if (fp->uf_calls > 0)
4081 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004082 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004083 return;
4084 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004085 if (fp->uf_flags & FC_VIM9)
4086 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004087 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004088 return;
4089 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004090
4091 if (fudi.fd_dict != NULL)
4092 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004093 // Delete the dict item that refers to the function, it will
4094 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004095 dictitem_remove(fudi.fd_dict, fudi.fd_di);
4096 }
4097 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004098 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004099 // A normal function (not a numbered function or lambda) has a
4100 // refcount of 1 for the entry in the hashtable. When deleting
4101 // it and the refcount is more than one, it should be kept.
4102 // A numbered function and lambda should be kept if the refcount is
4103 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004104 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004105 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004106 // Function is still referenced somewhere. Don't free it but
4107 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004108 if (func_remove(fp))
4109 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004110 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004111 }
4112 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004113 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004114 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004115 }
4116}
4117
4118/*
4119 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004120 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004121 */
4122 void
4123func_unref(char_u *name)
4124{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004125 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004126
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004127 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004128 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004129 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004130 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004131 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004132#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004133 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004134#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01004135 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004136 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004137 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004138 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004139 // Only delete it when it's not being used. Otherwise it's done
4140 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004141 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004142 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02004143 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004144}
4145
4146/*
4147 * Unreference a Function: decrement the reference count and free it when it
4148 * becomes zero.
4149 */
4150 void
4151func_ptr_unref(ufunc_T *fp)
4152{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004153 if (fp != NULL && --fp->uf_refcount <= 0)
4154 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004155 // Only delete it when it's not being used. Otherwise it's done
4156 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02004157 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004158 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004159 }
4160}
4161
4162/*
4163 * Count a reference to a Function.
4164 */
4165 void
4166func_ref(char_u *name)
4167{
4168 ufunc_T *fp;
4169
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004170 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004171 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004172 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004173 if (fp != NULL)
4174 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004175 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01004176 // Only give an error for a numbered function.
4177 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01004178 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004179}
4180
4181/*
4182 * Count a reference to a Function.
4183 */
4184 void
4185func_ptr_ref(ufunc_T *fp)
4186{
4187 if (fp != NULL)
4188 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004189}
4190
4191/*
4192 * Return TRUE if items in "fc" do not have "copyID". That means they are not
4193 * referenced from anywhere that is in use.
4194 */
4195 static int
4196can_free_funccal(funccall_T *fc, int copyID)
4197{
4198 return (fc->l_varlist.lv_copyID != copyID
4199 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004200 && fc->l_avars.dv_copyID != copyID
4201 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004202}
4203
4204/*
4205 * ":return [expr]"
4206 */
4207 void
4208ex_return(exarg_T *eap)
4209{
4210 char_u *arg = eap->arg;
4211 typval_T rettv;
4212 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004213 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004214
4215 if (current_funccal == NULL)
4216 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004217 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004218 return;
4219 }
4220
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004221 CLEAR_FIELD(evalarg);
4222 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
4223
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004224 if (eap->skip)
4225 ++emsg_skip;
4226
4227 eap->nextcmd = NULL;
4228 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004229 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004230 {
4231 if (!eap->skip)
4232 returning = do_return(eap, FALSE, TRUE, &rettv);
4233 else
4234 clear_tv(&rettv);
4235 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004236 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004237 else if (!eap->skip)
4238 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004239 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01004240 update_force_abort();
4241
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004242 /*
4243 * Return unless the expression evaluation has been cancelled due to an
4244 * aborting error, an interrupt, or an exception.
4245 */
4246 if (!aborting())
4247 returning = do_return(eap, FALSE, TRUE, NULL);
4248 }
4249
Bram Moolenaare38eab22019-12-05 21:50:01 +01004250 // When skipping or the return gets pending, advance to the next command
4251 // in this line (!returning). Otherwise, ignore the rest of the line.
4252 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004253 if (returning)
4254 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004255 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004256 eap->nextcmd = check_nextcmd(arg);
4257
4258 if (eap->skip)
4259 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02004260 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004261}
4262
4263/*
4264 * ":1,25call func(arg1, arg2)" function call.
4265 */
4266 void
4267ex_call(exarg_T *eap)
4268{
4269 char_u *arg = eap->arg;
4270 char_u *startarg;
4271 char_u *name;
4272 char_u *tofree;
4273 int len;
4274 typval_T rettv;
4275 linenr_T lnum;
4276 int doesrange;
4277 int failed = FALSE;
4278 funcdict_T fudi;
4279 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004280 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004281
Bram Moolenaare6b53242020-07-01 17:28:33 +02004282 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004283 if (eap->skip)
4284 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004285 // trans_function_name() doesn't work well when skipping, use eval0()
4286 // instead to skip to any following command, e.g. for:
4287 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004288 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004289 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004290 clear_tv(&rettv);
4291 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004292 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004293 return;
4294 }
4295
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004296 tofree = trans_function_name(&arg, NULL, eap->skip,
4297 TFN_INT, &fudi, &partial);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004298 if (fudi.fd_newkey != NULL)
4299 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004300 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004301 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004302 vim_free(fudi.fd_newkey);
4303 }
4304 if (tofree == NULL)
4305 return;
4306
Bram Moolenaare38eab22019-12-05 21:50:01 +01004307 // Increase refcount on dictionary, it could get deleted when evaluating
4308 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004309 if (fudi.fd_dict != NULL)
4310 ++fudi.fd_dict->dv_refcount;
4311
Bram Moolenaare38eab22019-12-05 21:50:01 +01004312 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
4313 // contents. For VAR_PARTIAL get its partial, unless we already have one
4314 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004315 len = (int)STRLEN(tofree);
4316 name = deref_func_name(tofree, &len,
4317 partial != NULL ? NULL : &partial, FALSE);
4318
Bram Moolenaare38eab22019-12-05 21:50:01 +01004319 // Skip white space to allow ":call func ()". Not good, but required for
4320 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004321 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004322 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004323
4324 if (*startarg != '(')
4325 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004327 goto end;
4328 }
4329
4330 /*
4331 * When skipping, evaluate the function once, to find the end of the
4332 * arguments.
4333 * When the function takes a range, this is discovered after the first
4334 * call, and the loop is broken.
4335 */
4336 if (eap->skip)
4337 {
4338 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004339 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004340 }
4341 else
4342 lnum = eap->line1;
4343 for ( ; lnum <= eap->line2; ++lnum)
4344 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004345 funcexe_T funcexe;
4346
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004347 if (!eap->skip && eap->addr_count > 0)
4348 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004349 if (lnum > curbuf->b_ml.ml_line_count)
4350 {
4351 // If the function deleted lines or switched to another buffer
4352 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004353 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004354 break;
4355 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004356 curwin->w_cursor.lnum = lnum;
4357 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004358 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004359 }
4360 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004361
Bram Moolenaara80faa82020-04-12 19:37:17 +02004362 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004363 funcexe.firstline = eap->line1;
4364 funcexe.lastline = eap->line2;
4365 funcexe.doesrange = &doesrange;
4366 funcexe.evaluate = !eap->skip;
4367 funcexe.partial = partial;
4368 funcexe.selfdict = fudi.fd_dict;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004369 if (get_func_tv(name, -1, &rettv, &arg, &evalarg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004370 {
4371 failed = TRUE;
4372 break;
4373 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01004374 if (has_watchexpr())
4375 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004376
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004377 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004378 if (handle_subscript(&arg, &rettv,
4379 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004380 {
4381 failed = TRUE;
4382 break;
4383 }
4384
4385 clear_tv(&rettv);
4386 if (doesrange || eap->skip)
4387 break;
4388
Bram Moolenaare38eab22019-12-05 21:50:01 +01004389 // Stop when immediately aborting on error, or when an interrupt
4390 // occurred or an exception was thrown but not caught.
4391 // get_func_tv() returned OK, so that the check for trailing
4392 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004393 if (aborting())
4394 break;
4395 }
4396 if (eap->skip)
4397 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004398 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004399
Bram Moolenaare51bb172020-02-16 19:42:23 +01004400 // When inside :try we need to check for following "| catch".
4401 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004402 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004403 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02004404 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004405 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004406 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004407 if (!failed)
4408 {
4409 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004410 semsg(_(e_trailing_arg), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004411 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004412 }
4413 else
4414 eap->nextcmd = check_nextcmd(arg);
4415 }
4416
4417end:
4418 dict_unref(fudi.fd_dict);
4419 vim_free(tofree);
4420}
4421
4422/*
4423 * Return from a function. Possibly makes the return pending. Also called
4424 * for a pending return at the ":endtry" or after returning from an extra
4425 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
4426 * when called due to a ":return" command. "rettv" may point to a typval_T
4427 * with the return rettv. Returns TRUE when the return can be carried out,
4428 * FALSE when the return gets pending.
4429 */
4430 int
4431do_return(
4432 exarg_T *eap,
4433 int reanimate,
4434 int is_cmd,
4435 void *rettv)
4436{
4437 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01004438 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004439
4440 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004441 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004442 current_funccal->returned = FALSE;
4443
4444 /*
4445 * Cleanup (and inactivate) conditionals, but stop when a try conditional
4446 * not in its finally clause (which then is to be executed next) is found.
4447 * In this case, make the ":return" pending for execution at the ":endtry".
4448 * Otherwise, return normally.
4449 */
4450 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
4451 if (idx >= 0)
4452 {
4453 cstack->cs_pending[idx] = CSTP_RETURN;
4454
4455 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004456 // A pending return again gets pending. "rettv" points to an
4457 // allocated variable with the rettv of the original ":return"'s
4458 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004459 cstack->cs_rettv[idx] = rettv;
4460 else
4461 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004462 // When undoing a return in order to make it pending, get the stored
4463 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004464 if (reanimate)
4465 rettv = current_funccal->rettv;
4466
4467 if (rettv != NULL)
4468 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004469 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004470 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
4471 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
4472 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004473 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004474 }
4475 else
4476 cstack->cs_rettv[idx] = NULL;
4477
4478 if (reanimate)
4479 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004480 // The pending return value could be overwritten by a ":return"
4481 // without argument in a finally clause; reset the default
4482 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004483 current_funccal->rettv->v_type = VAR_NUMBER;
4484 current_funccal->rettv->vval.v_number = 0;
4485 }
4486 }
4487 report_make_pending(CSTP_RETURN, rettv);
4488 }
4489 else
4490 {
4491 current_funccal->returned = TRUE;
4492
Bram Moolenaare38eab22019-12-05 21:50:01 +01004493 // If the return is carried out now, store the return value. For
4494 // a return immediately after reanimation, the value is already
4495 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004496 if (!reanimate && rettv != NULL)
4497 {
4498 clear_tv(current_funccal->rettv);
4499 *current_funccal->rettv = *(typval_T *)rettv;
4500 if (!is_cmd)
4501 vim_free(rettv);
4502 }
4503 }
4504
4505 return idx < 0;
4506}
4507
4508/*
4509 * Free the variable with a pending return value.
4510 */
4511 void
4512discard_pending_return(void *rettv)
4513{
4514 free_tv((typval_T *)rettv);
4515}
4516
4517/*
4518 * Generate a return command for producing the value of "rettv". The result
4519 * is an allocated string. Used by report_pending() for verbose messages.
4520 */
4521 char_u *
4522get_return_cmd(void *rettv)
4523{
4524 char_u *s = NULL;
4525 char_u *tofree = NULL;
4526 char_u numbuf[NUMBUFLEN];
4527
4528 if (rettv != NULL)
4529 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
4530 if (s == NULL)
4531 s = (char_u *)"";
4532
4533 STRCPY(IObuff, ":return ");
4534 STRNCPY(IObuff + 8, s, IOSIZE - 8);
4535 if (STRLEN(s) + 8 >= IOSIZE)
4536 STRCPY(IObuff + IOSIZE - 4, "...");
4537 vim_free(tofree);
4538 return vim_strsave(IObuff);
4539}
4540
4541/*
4542 * Get next function line.
4543 * Called by do_cmdline() to get the next line.
4544 * Returns allocated string, or NULL for end of function.
4545 */
4546 char_u *
4547get_func_line(
4548 int c UNUSED,
4549 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02004550 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004551 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004552{
4553 funccall_T *fcp = (funccall_T *)cookie;
4554 ufunc_T *fp = fcp->func;
4555 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004556 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004557
Bram Moolenaare38eab22019-12-05 21:50:01 +01004558 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004559 if (fcp->dbg_tick != debug_tick)
4560 {
4561 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004562 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004563 fcp->dbg_tick = debug_tick;
4564 }
4565#ifdef FEAT_PROFILE
4566 if (do_profiling == PROF_YES)
4567 func_line_end(cookie);
4568#endif
4569
4570 gap = &fp->uf_lines;
4571 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4572 || fcp->returned)
4573 retval = NULL;
4574 else
4575 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004576 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004577 while (fcp->linenr < gap->ga_len
4578 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
4579 ++fcp->linenr;
4580 if (fcp->linenr >= gap->ga_len)
4581 retval = NULL;
4582 else
4583 {
4584 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004585 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004586#ifdef FEAT_PROFILE
4587 if (do_profiling == PROF_YES)
4588 func_line_start(cookie);
4589#endif
4590 }
4591 }
4592
Bram Moolenaare38eab22019-12-05 21:50:01 +01004593 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004594 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004595 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004596 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004597 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004598 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004599 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004600 fcp->dbg_tick = debug_tick;
4601 }
4602
4603 return retval;
4604}
4605
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004606/*
4607 * Return TRUE if the currently active function should be ended, because a
4608 * return was encountered or an error occurred. Used inside a ":while".
4609 */
4610 int
4611func_has_ended(void *cookie)
4612{
4613 funccall_T *fcp = (funccall_T *)cookie;
4614
Bram Moolenaare38eab22019-12-05 21:50:01 +01004615 // Ignore the "abort" flag if the abortion behavior has been changed due to
4616 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004617 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4618 || fcp->returned);
4619}
4620
4621/*
4622 * return TRUE if cookie indicates a function which "abort"s on errors.
4623 */
4624 int
4625func_has_abort(
4626 void *cookie)
4627{
4628 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
4629}
4630
4631
4632/*
4633 * Turn "dict.Func" into a partial for "Func" bound to "dict".
4634 * Don't do this when "Func" is already a partial that was bound
4635 * explicitly (pt_auto is FALSE).
4636 * Changes "rettv" in-place.
4637 * Returns the updated "selfdict_in".
4638 */
4639 dict_T *
4640make_partial(dict_T *selfdict_in, typval_T *rettv)
4641{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004642 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004643 char_u *tofree = NULL;
4644 ufunc_T *fp;
4645 char_u fname_buf[FLEN_FIXED + 1];
4646 int error;
4647 dict_T *selfdict = selfdict_in;
4648
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004649 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
4650 fp = rettv->vval.v_partial->pt_func;
4651 else
4652 {
4653 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
4654 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004655 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004656 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004657 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004658 vim_free(tofree);
4659 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004660
4661 if (fp != NULL && (fp->uf_flags & FC_DICT))
4662 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004663 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004664
4665 if (pt != NULL)
4666 {
4667 pt->pt_refcount = 1;
4668 pt->pt_dict = selfdict;
4669 pt->pt_auto = TRUE;
4670 selfdict = NULL;
4671 if (rettv->v_type == VAR_FUNC)
4672 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004673 // Just a function: Take over the function name and use
4674 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004675 pt->pt_name = rettv->vval.v_string;
4676 }
4677 else
4678 {
4679 partial_T *ret_pt = rettv->vval.v_partial;
4680 int i;
4681
Bram Moolenaare38eab22019-12-05 21:50:01 +01004682 // Partial: copy the function name, use selfdict and copy
4683 // args. Can't take over name or args, the partial might
4684 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004685 if (ret_pt->pt_name != NULL)
4686 {
4687 pt->pt_name = vim_strsave(ret_pt->pt_name);
4688 func_ref(pt->pt_name);
4689 }
4690 else
4691 {
4692 pt->pt_func = ret_pt->pt_func;
4693 func_ptr_ref(pt->pt_func);
4694 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004695 if (ret_pt->pt_argc > 0)
4696 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004697 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004698 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004699 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004700 pt->pt_argc = 0;
4701 else
4702 {
4703 pt->pt_argc = ret_pt->pt_argc;
4704 for (i = 0; i < pt->pt_argc; i++)
4705 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
4706 }
4707 }
4708 partial_unref(ret_pt);
4709 }
4710 rettv->v_type = VAR_PARTIAL;
4711 rettv->vval.v_partial = pt;
4712 }
4713 }
4714 return selfdict;
4715}
4716
4717/*
4718 * Return the name of the executed function.
4719 */
4720 char_u *
4721func_name(void *cookie)
4722{
4723 return ((funccall_T *)cookie)->func->uf_name;
4724}
4725
4726/*
4727 * Return the address holding the next breakpoint line for a funccall cookie.
4728 */
4729 linenr_T *
4730func_breakpoint(void *cookie)
4731{
4732 return &((funccall_T *)cookie)->breakpoint;
4733}
4734
4735/*
4736 * Return the address holding the debug tick for a funccall cookie.
4737 */
4738 int *
4739func_dbg_tick(void *cookie)
4740{
4741 return &((funccall_T *)cookie)->dbg_tick;
4742}
4743
4744/*
4745 * Return the nesting level for a funccall cookie.
4746 */
4747 int
4748func_level(void *cookie)
4749{
4750 return ((funccall_T *)cookie)->level;
4751}
4752
4753/*
4754 * Return TRUE when a function was ended by a ":return" command.
4755 */
4756 int
4757current_func_returned(void)
4758{
4759 return current_funccal->returned;
4760}
4761
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004762 int
4763free_unref_funccal(int copyID, int testing)
4764{
4765 int did_free = FALSE;
4766 int did_free_funccal = FALSE;
4767 funccall_T *fc, **pfc;
4768
4769 for (pfc = &previous_funccal; *pfc != NULL; )
4770 {
4771 if (can_free_funccal(*pfc, copyID))
4772 {
4773 fc = *pfc;
4774 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01004775 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004776 did_free = TRUE;
4777 did_free_funccal = TRUE;
4778 }
4779 else
4780 pfc = &(*pfc)->caller;
4781 }
4782 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004783 // When a funccal was freed some more items might be garbage
4784 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004785 (void)garbage_collect(testing);
4786
4787 return did_free;
4788}
4789
4790/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004791 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004792 */
4793 static funccall_T *
4794get_funccal(void)
4795{
4796 int i;
4797 funccall_T *funccal;
4798 funccall_T *temp_funccal;
4799
4800 funccal = current_funccal;
4801 if (debug_backtrace_level > 0)
4802 {
4803 for (i = 0; i < debug_backtrace_level; i++)
4804 {
4805 temp_funccal = funccal->caller;
4806 if (temp_funccal)
4807 funccal = temp_funccal;
4808 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004809 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004810 debug_backtrace_level = i;
4811 }
4812 }
4813 return funccal;
4814}
4815
4816/*
4817 * Return the hashtable used for local variables in the current funccal.
4818 * Return NULL if there is no current funccal.
4819 */
4820 hashtab_T *
4821get_funccal_local_ht()
4822{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004823 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004824 return NULL;
4825 return &get_funccal()->l_vars.dv_hashtab;
4826}
4827
4828/*
4829 * Return the l: scope variable.
4830 * Return NULL if there is no current funccal.
4831 */
4832 dictitem_T *
4833get_funccal_local_var()
4834{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004835 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004836 return NULL;
4837 return &get_funccal()->l_vars_var;
4838}
4839
4840/*
4841 * Return the hashtable used for argument in the current funccal.
4842 * Return NULL if there is no current funccal.
4843 */
4844 hashtab_T *
4845get_funccal_args_ht()
4846{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004847 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004848 return NULL;
4849 return &get_funccal()->l_avars.dv_hashtab;
4850}
4851
4852/*
4853 * Return the a: scope variable.
4854 * Return NULL if there is no current funccal.
4855 */
4856 dictitem_T *
4857get_funccal_args_var()
4858{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004859 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004860 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004861 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004862}
4863
4864/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004865 * List function variables, if there is a function.
4866 */
4867 void
4868list_func_vars(int *first)
4869{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004870 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004871 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004872 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004873}
4874
4875/*
4876 * If "ht" is the hashtable for local variables in the current funccal, return
4877 * the dict that contains it.
4878 * Otherwise return NULL.
4879 */
4880 dict_T *
4881get_current_funccal_dict(hashtab_T *ht)
4882{
4883 if (current_funccal != NULL
4884 && ht == &current_funccal->l_vars.dv_hashtab)
4885 return &current_funccal->l_vars;
4886 return NULL;
4887}
4888
4889/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004890 * Search hashitem in parent scope.
4891 */
4892 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004893find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004894{
4895 funccall_T *old_current_funccal = current_funccal;
4896 hashtab_T *ht;
4897 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004898 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004899
4900 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4901 return NULL;
4902
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004903 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004904 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004905 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004906 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004907 ht = find_var_ht(name, &varname);
4908 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004909 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004910 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004911 if (!HASHITEM_EMPTY(hi))
4912 {
4913 *pht = ht;
4914 break;
4915 }
4916 }
4917 if (current_funccal == current_funccal->func->uf_scoped)
4918 break;
4919 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004920 }
4921 current_funccal = old_current_funccal;
4922
4923 return hi;
4924}
4925
4926/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004927 * Search variable in parent scope.
4928 */
4929 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004930find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004931{
4932 dictitem_T *v = NULL;
4933 funccall_T *old_current_funccal = current_funccal;
4934 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004935 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004936
4937 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4938 return NULL;
4939
Bram Moolenaare38eab22019-12-05 21:50:01 +01004940 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004941 current_funccal = current_funccal->func->uf_scoped;
4942 while (current_funccal)
4943 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004944 ht = find_var_ht(name, &varname);
4945 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004946 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004947 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004948 if (v != NULL)
4949 break;
4950 }
4951 if (current_funccal == current_funccal->func->uf_scoped)
4952 break;
4953 current_funccal = current_funccal->func->uf_scoped;
4954 }
4955 current_funccal = old_current_funccal;
4956
4957 return v;
4958}
4959
4960/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004961 * Set "copyID + 1" in previous_funccal and callers.
4962 */
4963 int
4964set_ref_in_previous_funccal(int copyID)
4965{
4966 int abort = FALSE;
4967 funccall_T *fc;
4968
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004969 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004970 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004971 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004972 abort = abort
4973 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4974 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004975 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004976 }
4977 return abort;
4978}
4979
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004980 static int
4981set_ref_in_funccal(funccall_T *fc, int copyID)
4982{
4983 int abort = FALSE;
4984
4985 if (fc->fc_copyID != copyID)
4986 {
4987 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004988 abort = abort
4989 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
4990 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004991 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004992 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004993 }
4994 return abort;
4995}
4996
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004997/*
4998 * Set "copyID" in all local vars and arguments in the call stack.
4999 */
5000 int
5001set_ref_in_call_stack(int copyID)
5002{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005003 int abort = FALSE;
5004 funccall_T *fc;
5005 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005006
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005007 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005008 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005009
5010 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005011 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
5012 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005013 abort = abort || set_ref_in_funccal(fc, copyID);
5014
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005015 return abort;
5016}
5017
5018/*
5019 * Set "copyID" in all functions available by name.
5020 */
5021 int
5022set_ref_in_functions(int copyID)
5023{
5024 int todo;
5025 hashitem_T *hi = NULL;
5026 int abort = FALSE;
5027 ufunc_T *fp;
5028
5029 todo = (int)func_hashtab.ht_used;
5030 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005031 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005032 if (!HASHITEM_EMPTY(hi))
5033 {
5034 --todo;
5035 fp = HI2UF(hi);
5036 if (!func_name_refcount(fp->uf_name))
5037 abort = abort || set_ref_in_func(NULL, fp, copyID);
5038 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005039 }
5040 return abort;
5041}
5042
5043/*
5044 * Set "copyID" in all function arguments.
5045 */
5046 int
5047set_ref_in_func_args(int copyID)
5048{
5049 int i;
5050 int abort = FALSE;
5051
5052 for (i = 0; i < funcargs.ga_len; ++i)
5053 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
5054 copyID, NULL, NULL);
5055 return abort;
5056}
5057
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005058/*
5059 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005060 * Returns TRUE if setting references failed somehow.
5061 */
5062 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005063set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005064{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005065 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005066 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01005067 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005068 char_u fname_buf[FLEN_FIXED + 1];
5069 char_u *tofree = NULL;
5070 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005071 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005072
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005073 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005074 return FALSE;
5075
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005076 if (fp_in == NULL)
5077 {
5078 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005079 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005080 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005081 if (fp != NULL)
5082 {
5083 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005084 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005085 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02005086
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005087 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005088 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005089}
5090
Bram Moolenaare38eab22019-12-05 21:50:01 +01005091#endif // FEAT_EVAL