blob: 2bae9ef90cda06fff70abf8bc1c507832c212246 [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 Moolenaarc754b4c2020-12-25 15:24:23 +0100577 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100578 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100579 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200580
Bram Moolenaare38eab22019-12-05 21:50:01 +0100581 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200582 if (evaluate)
583 eval_lavars_used = &eval_lavars;
584
Bram Moolenaar65c44152020-12-24 15:14:01 +0100585 *arg = skipwhite_and_linebreak(*arg, evalarg);
586
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100587 // Recognize "{" as the start of a function body.
588 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +0100589 {
590 // TODO: process the function body upto the "}".
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100591 // Return type is required then.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100592 emsg("Lambda function body not supported yet");
593 goto errret;
594 }
595
Bram Moolenaare38eab22019-12-05 21:50:01 +0100596 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200597 start = *arg;
598 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200599 if (ret == FAIL)
600 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200601 if (evalarg != NULL)
602 {
603 // avoid that the expression gets freed when another line break follows
604 tofree = evalarg->eval_tofree;
605 evalarg->eval_tofree = NULL;
606 }
607
Bram Moolenaar65c44152020-12-24 15:14:01 +0100608 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +0100609 {
Bram Moolenaar65c44152020-12-24 15:14:01 +0100610 *arg = skipwhite_and_linebreak(*arg, evalarg);
611 if (**arg != '}')
612 {
613 semsg(_("E451: Expected }: %s"), *arg);
614 goto errret;
615 }
616 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +0100617 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200618
619 if (evaluate)
620 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200621 int len;
622 int flags = 0;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200623 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200624 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200625
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200626 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200627 if (fp == NULL)
628 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200629 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200630 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200631 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200632 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200633
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200634 ga_init2(&newlines, (int)sizeof(char_u *), 1);
635 if (ga_grow(&newlines, 1) == FAIL)
636 goto errret;
637
Bram Moolenaare38eab22019-12-05 21:50:01 +0100638 // Add "return " before the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200639 len = 7 + (int)(end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200640 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200641 if (p == NULL)
642 goto errret;
643 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
644 STRCPY(p, "return ");
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200645 vim_strncpy(p + 7, start, end - start);
Bram Moolenaarf10806b2020-04-02 18:34:35 +0200646 if (strstr((char *)p + 7, "a:") == NULL)
647 // No a: variables are used for sure.
648 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200649
650 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100651 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200652 hash_add(&func_hashtab, UF2HIKEY(fp));
653 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200654 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100655 if (types_optional)
656 {
657 if (parse_argument_types(fp, &argtypes, FALSE) == FAIL)
658 goto errret;
659 if (ret_type != NULL)
660 {
661 fp->uf_ret_type = parse_type(&ret_type,
662 &fp->uf_type_list, TRUE);
663 if (fp->uf_ret_type == NULL)
664 goto errret;
665 }
666 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100667
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200668 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200669 if (current_funccal != NULL && eval_lavars)
670 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200671 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200672 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200673 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200674 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200675
676#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200677 if (prof_def_func())
678 func_do_profile(fp);
679#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200680 if (sandbox)
681 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200683 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200684 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200685 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200686 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100687 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200688
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200689 pt->pt_func = fp;
690 pt->pt_refcount = 1;
691 rettv->vval.v_partial = pt;
692 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200693 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200694
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200695 eval_lavars_used = old_eval_lavars;
Bram Moolenaarefaaaa62020-07-08 22:24:09 +0200696 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200697 evalarg->eval_tofree = tofree;
698 else
699 vim_free(tofree);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100700 if (types_optional)
701 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200702 return OK;
703
704errret:
705 ga_clear_strings(&newargs);
706 ga_clear_strings(&newlines);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100707 if (types_optional)
708 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200709 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100710 vim_free(pt);
Bram Moolenaarefaaaa62020-07-08 22:24:09 +0200711 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200712 evalarg->eval_tofree = tofree;
713 else
714 vim_free(tofree);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200715 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200716 return FAIL;
717}
718
719/*
720 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
721 * name it contains, otherwise return "name".
722 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
723 * "partialp".
724 */
725 char_u *
726deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
727{
728 dictitem_T *v;
729 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200730 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200731
732 if (partialp != NULL)
733 *partialp = NULL;
734
735 cc = name[*lenp];
736 name[*lenp] = NUL;
737 v = find_var(name, NULL, no_autoload);
738 name[*lenp] = cc;
739 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
740 {
741 if (v->di_tv.vval.v_string == NULL)
742 {
743 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100744 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200745 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200746 s = v->di_tv.vval.v_string;
747 *lenp = (int)STRLEN(s);
748 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200749 }
750
751 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
752 {
753 partial_T *pt = v->di_tv.vval.v_partial;
754
755 if (pt == NULL)
756 {
757 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100758 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200759 }
760 if (partialp != NULL)
761 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200762 s = partial_name(pt);
763 *lenp = (int)STRLEN(s);
764 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200765 }
766
767 return name;
768}
769
770/*
771 * Give an error message with a function name. Handle <SNR> things.
772 * "ermsg" is to be passed without translation, use N_() instead of _().
773 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100774 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200775emsg_funcname(char *ermsg, char_u *name)
776{
777 char_u *p;
778
779 if (*name == K_SPECIAL)
780 p = concat_str((char_u *)"<SNR>", name + 3);
781 else
782 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100783 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200784 if (p != name)
785 vim_free(p);
786}
787
788/*
789 * Allocate a variable for the result of a function.
790 * Return OK or FAIL.
791 */
792 int
793get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200794 char_u *name, // name of the function
795 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200796 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200797 char_u **arg, // argument, pointing to the '('
Bram Moolenaare6b53242020-07-01 17:28:33 +0200798 evalarg_T *evalarg, // for line continuation
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200799 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200800{
801 char_u *argp;
802 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100803 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
804 int argcount = 0; // number of arguments found
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200805 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200806
807 /*
808 * Get the arguments.
809 */
810 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200811 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
812 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200813 {
Bram Moolenaare6b53242020-07-01 17:28:33 +0200814 // skip the '(' or ',' and possibly line breaks
815 argp = skipwhite_and_linebreak(argp + 1, evalarg);
816
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200817 if (*argp == ')' || *argp == ',' || *argp == NUL)
818 break;
Bram Moolenaare6b53242020-07-01 17:28:33 +0200819 if (eval1(&argp, &argvars[argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200820 {
821 ret = FAIL;
822 break;
823 }
824 ++argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +0200825 // The comma should come right after the argument, but this wasn't
826 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200827 if (vim9script)
828 {
829 if (*argp != ',' && *skipwhite(argp) == ',')
830 {
831 semsg(_(e_no_white_space_allowed_before_str), ",");
832 ret = FAIL;
833 break;
834 }
835 }
836 else
Bram Moolenaar9d489562020-07-30 20:08:50 +0200837 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200838 if (*argp != ',')
839 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200840 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
841 {
842 semsg(_(e_white_space_required_after_str), ",");
843 ret = FAIL;
844 break;
845 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200846 }
Bram Moolenaare6b53242020-07-01 17:28:33 +0200847 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200848 if (*argp == ')')
849 ++argp;
850 else
851 ret = FAIL;
852
853 if (ret == OK)
854 {
855 int i = 0;
856
857 if (get_vim_var_nr(VV_TESTING))
858 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100859 // Prepare for calling test_garbagecollect_now(), need to know
860 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200861 if (funcargs.ga_itemsize == 0)
862 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
863 for (i = 0; i < argcount; ++i)
864 if (ga_grow(&funcargs, 1) == OK)
865 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
866 &argvars[i];
867 }
868
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200869 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200870
871 funcargs.ga_len -= i;
872 }
873 else if (!aborting())
874 {
875 if (argcount == MAX_FUNC_ARGS)
876 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
877 else
878 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
879 }
880
881 while (--argcount >= 0)
882 clear_tv(&argvars[argcount]);
883
Bram Moolenaar8294d492020-08-10 22:40:56 +0200884 if (in_vim9script())
885 *arg = argp;
886 else
887 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200888 return ret;
889}
890
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200891/*
892 * Return TRUE if "p" starts with "<SID>" or "s:".
893 * Only works if eval_fname_script() returned non-zero for "p"!
894 */
895 static int
896eval_fname_sid(char_u *p)
897{
898 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
899}
900
901/*
902 * In a script change <SID>name() and s:name() to K_SNR 123_name().
903 * Change <SNR>123_name() to K_SNR 123_name().
904 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
905 * (slow).
906 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100907 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200908fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
909{
910 int llen;
911 char_u *fname;
912 int i;
913
914 llen = eval_fname_script(name);
915 if (llen > 0)
916 {
917 fname_buf[0] = K_SPECIAL;
918 fname_buf[1] = KS_EXTRA;
919 fname_buf[2] = (int)KE_SNR;
920 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100921 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200922 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200923 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100924 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200925 else
926 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200927 sprintf((char *)fname_buf + 3, "%ld_",
928 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200929 i = (int)STRLEN(fname_buf);
930 }
931 }
932 if (i + STRLEN(name + llen) < FLEN_FIXED)
933 {
934 STRCPY(fname_buf + i, name + llen);
935 fname = fname_buf;
936 }
937 else
938 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200939 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200940 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +0100941 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200942 else
943 {
944 *tofree = fname;
945 mch_memmove(fname, fname_buf, (size_t)i);
946 STRCPY(fname + i, name + llen);
947 }
948 }
949 }
950 else
951 fname = name;
952 return fname;
953}
954
955/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956 * Find a function "name" in script "sid".
957 */
958 static ufunc_T *
959find_func_with_sid(char_u *name, int sid)
960{
961 hashitem_T *hi;
962 char_u buffer[200];
963
964 buffer[0] = K_SPECIAL;
965 buffer[1] = KS_EXTRA;
966 buffer[2] = (int)KE_SNR;
967 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
968 (long)sid, name);
969 hi = hash_find(&func_hashtab, buffer);
970 if (!HASHITEM_EMPTY(hi))
971 return HI2UF(hi);
972
973 return NULL;
974}
975
976/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200977 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200978 * When "is_global" is true don't find script-local or imported functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200979 * Return NULL for unknown function.
980 */
Bram Moolenaarce658352020-07-31 23:47:12 +0200981 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200982find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200983{
984 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 ufunc_T *func;
986 imported_T *imported;
987
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200988 if (!is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100989 {
Bram Moolenaar333894b2020-08-01 18:53:07 +0200990 char_u *after_script = NULL;
Bram Moolenaarefa94442020-08-08 22:16:00 +0200991 long sid = 0;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200992 int find_script_local = in_vim9script()
993 && eval_isnamec1(*name) && name[1] != ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100994
Bram Moolenaar95006e32020-08-29 17:47:08 +0200995 if (find_script_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100996 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200997 // Find script-local function before global one.
998 func = find_func_with_sid(name, current_sctx.sc_sid);
999 if (func != NULL)
1000 return func;
1001 }
1002
Bram Moolenaarefa94442020-08-08 22:16:00 +02001003 if (name[0] == K_SPECIAL
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001004 && name[1] == KS_EXTRA
1005 && name[2] == KE_SNR)
1006 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001007 // Caller changes s: to <SNR>99_name.
1008
1009 after_script = name + 3;
1010 sid = getdigits(&after_script);
Bram Moolenaarefa94442020-08-08 22:16:00 +02001011 if (*after_script == '_')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001012 ++after_script;
1013 else
1014 after_script = NULL;
1015 }
Bram Moolenaar95006e32020-08-29 17:47:08 +02001016 if (find_script_local || after_script != NULL)
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001017 {
1018 // Find imported function before global one.
Bram Moolenaarefa94442020-08-08 22:16:00 +02001019 if (after_script != NULL && sid != current_sctx.sc_sid)
1020 imported = find_imported_in_script(after_script, 0, sid);
1021 else
1022 imported = find_imported(after_script == NULL
1023 ? name : after_script, 0, cctx);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001024 if (imported != NULL && imported->imp_funcname != NULL)
1025 {
1026 hi = hash_find(&func_hashtab, imported->imp_funcname);
1027 if (!HASHITEM_EMPTY(hi))
1028 return HI2UF(hi);
1029 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030 }
1031 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001032
Bram Moolenaara26b9702020-04-18 19:53:28 +02001033 hi = hash_find(&func_hashtab,
1034 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001035 if (!HASHITEM_EMPTY(hi))
1036 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001037
1038 return NULL;
1039}
1040
1041/*
1042 * Find a function by name, return pointer to it in ufuncs.
1043 * "cctx" is passed in a :def function to find imported functions.
1044 * Return NULL for unknown or dead function.
1045 */
1046 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001047find_func(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001048{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001049 ufunc_T *fp = find_func_even_dead(name, is_global, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050
1051 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
1052 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001053 return NULL;
1054}
1055
1056/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02001057 * Return TRUE if "ufunc" is a global function.
1058 */
1059 int
1060func_is_global(ufunc_T *ufunc)
1061{
1062 return ufunc->uf_name[0] != K_SPECIAL;
1063}
1064
1065/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001066 * Copy the function name of "fp" to buffer "buf".
1067 * "buf" must be able to hold the function name plus three bytes.
1068 * Takes care of script-local function names.
1069 */
1070 static void
1071cat_func_name(char_u *buf, ufunc_T *fp)
1072{
Bram Moolenaar0f769812020-09-12 18:32:34 +02001073 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001074 {
1075 STRCPY(buf, "<SNR>");
1076 STRCAT(buf, fp->uf_name + 3);
1077 }
1078 else
1079 STRCPY(buf, fp->uf_name);
1080}
1081
1082/*
1083 * Add a number variable "name" to dict "dp" with value "nr".
1084 */
1085 static void
1086add_nr_var(
1087 dict_T *dp,
1088 dictitem_T *v,
1089 char *name,
1090 varnumber_T nr)
1091{
1092 STRCPY(v->di_key, name);
1093 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1094 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
1095 v->di_tv.v_type = VAR_NUMBER;
1096 v->di_tv.v_lock = VAR_FIXED;
1097 v->di_tv.vval.v_number = nr;
1098}
1099
1100/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001101 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001102 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001103 static void
1104free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001105{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001106 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001107
1108 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1109 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001110 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001111
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001112 // When garbage collecting a funccall_T may be freed before the
1113 // function that references it, clear its uf_scoped field.
1114 // The function may have been redefined and point to another
1115 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001116 if (fp != NULL && fp->uf_scoped == fc)
1117 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001118 }
Bram Moolenaar58016442016-07-31 18:30:22 +02001119 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001120
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001121 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001122 vim_free(fc);
1123}
1124
1125/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001126 * Free "fc" and what it contains.
1127 * Can be called only when "fc" is kept beyond the period of it called,
1128 * i.e. after cleanup_function_call(fc).
1129 */
1130 static void
1131free_funccal_contents(funccall_T *fc)
1132{
1133 listitem_T *li;
1134
1135 // Free all l: variables.
1136 vars_clear(&fc->l_vars.dv_hashtab);
1137
1138 // Free all a: variables.
1139 vars_clear(&fc->l_avars.dv_hashtab);
1140
1141 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001142 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001143 clear_tv(&li->li_tv);
1144
1145 free_funccal(fc);
1146}
1147
1148/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02001149 * Handle the last part of returning from a function: free the local hashtable.
1150 * Unless it is still in use by a closure.
1151 */
1152 static void
1153cleanup_function_call(funccall_T *fc)
1154{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001155 int may_free_fc = fc->fc_refcount <= 0;
1156 int free_fc = TRUE;
1157
Bram Moolenaar6914c642017-04-01 21:21:30 +02001158 current_funccal = fc->caller;
1159
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001160 // Free all l: variables if not referred.
1161 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
1162 vars_clear(&fc->l_vars.dv_hashtab);
1163 else
1164 free_fc = FALSE;
1165
1166 // If the a:000 list and the l: and a: dicts are not referenced and
1167 // there is no closure using it, we can free the funccall_T and what's
1168 // in it.
1169 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
1170 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02001171 else
1172 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001173 int todo;
1174 hashitem_T *hi;
1175 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02001176
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001177 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02001178
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001179 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001180 todo = (int)fc->l_avars.dv_hashtab.ht_used;
1181 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
1182 {
1183 if (!HASHITEM_EMPTY(hi))
1184 {
1185 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001186 di = HI2DI(hi);
1187 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02001188 }
1189 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001190 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02001191
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001192 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
1193 fc->l_varlist.lv_first = NULL;
1194 else
1195 {
1196 listitem_T *li;
1197
1198 free_fc = FALSE;
1199
1200 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001201 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02001202 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001203 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001204
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001205 if (free_fc)
1206 free_funccal(fc);
1207 else
1208 {
1209 static int made_copy = 0;
1210
1211 // "fc" is still in use. This can happen when returning "a:000",
1212 // assigning "l:" to a global variable or defining a closure.
1213 // Link "fc" in the list for garbage collection later.
1214 fc->caller = previous_funccal;
1215 previous_funccal = fc;
1216
1217 if (want_garbage_collect)
1218 // If garbage collector is ready, clear count.
1219 made_copy = 0;
1220 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001221 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001222 // We have made a lot of copies, worth 4 Mbyte. This can happen
1223 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01001224 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001225 // too much memory.
1226 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01001227 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001228 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02001229 }
1230}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001231
1232/*
1233 * There are two kinds of function names:
1234 * 1. ordinary names, function defined with :function or :def
1235 * 2. numbered functions and lambdas
1236 * For the first we only count the name stored in func_hashtab as a reference,
1237 * using function() does not count as a reference, because the function is
1238 * looked up by name.
1239 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02001240 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001241func_name_refcount(char_u *name)
1242{
1243 return isdigit(*name) || *name == '<';
1244}
1245
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001246/*
1247 * Unreference "fc": decrement the reference count and free it when it
1248 * becomes zero. "fp" is detached from "fc".
1249 * When "force" is TRUE we are exiting.
1250 */
1251 static void
1252funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
1253{
1254 funccall_T **pfc;
1255 int i;
1256
1257 if (fc == NULL)
1258 return;
1259
1260 if (--fc->fc_refcount <= 0 && (force || (
1261 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
1262 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
1263 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
1264 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
1265 {
1266 if (fc == *pfc)
1267 {
1268 *pfc = fc->caller;
1269 free_funccal_contents(fc);
1270 return;
1271 }
1272 }
1273 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1274 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
1275 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
1276}
1277
1278/*
1279 * Remove the function from the function hashtable. If the function was
1280 * deleted while it still has references this was already done.
1281 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
1282 */
1283 static int
1284func_remove(ufunc_T *fp)
1285{
1286 hashitem_T *hi;
1287
1288 // Return if it was already virtually deleted.
1289 if (fp->uf_flags & FC_DEAD)
1290 return FALSE;
1291
1292 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
1293 if (!HASHITEM_EMPTY(hi))
1294 {
1295 // When there is a def-function index do not actually remove the
1296 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001297 // Do remove it when it's a copy.
1298 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001299 fp->uf_flags |= FC_DEAD;
1300 else
1301 hash_remove(&func_hashtab, hi);
1302 return TRUE;
1303 }
1304 return FALSE;
1305}
1306
1307 static void
1308func_clear_items(ufunc_T *fp)
1309{
1310 ga_clear_strings(&(fp->uf_args));
1311 ga_clear_strings(&(fp->uf_def_args));
1312 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001314 VIM_CLEAR(fp->uf_def_arg_idx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001315 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001316 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02001317 clear_type_list(&fp->uf_type_list);
Bram Moolenaarf112f302020-12-20 17:47:52 +01001318 partial_unref(fp->uf_partial);
1319 fp->uf_partial = NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +02001320
1321#ifdef FEAT_LUA
1322 if (fp->uf_cb_free != NULL)
1323 {
1324 fp->uf_cb_free(fp->uf_cb_state);
1325 fp->uf_cb_free = NULL;
1326 }
1327
1328 fp->uf_cb_state = NULL;
1329 fp->uf_cb = NULL;
1330#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331#ifdef FEAT_PROFILE
1332 VIM_CLEAR(fp->uf_tml_count);
1333 VIM_CLEAR(fp->uf_tml_total);
1334 VIM_CLEAR(fp->uf_tml_self);
1335#endif
1336}
1337
1338/*
1339 * Free all things that a function contains. Does not free the function
1340 * itself, use func_free() for that.
1341 * When "force" is TRUE we are exiting.
1342 */
1343 static void
1344func_clear(ufunc_T *fp, int force)
1345{
1346 if (fp->uf_cleared)
1347 return;
1348 fp->uf_cleared = TRUE;
1349
1350 // clear this function
1351 func_clear_items(fp);
1352 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001353 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354}
1355
1356/*
1357 * Free a function and remove it from the list of functions. Does not free
1358 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001359 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02001360 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02001362 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001363func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364{
1365 // Only remove it when not done already, otherwise we would remove a newer
1366 // version of the function with the same name.
1367 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1368 func_remove(fp);
1369
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001370 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001371 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001372 if (fp->uf_dfunc_idx > 0)
1373 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001374 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001375 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02001376 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001377 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02001378 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379}
1380
1381/*
1382 * Free all things that a function contains and free the function itself.
1383 * When "force" is TRUE we are exiting.
1384 */
1385 static void
1386func_clear_free(ufunc_T *fp, int force)
1387{
1388 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001389 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
1390 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001391 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02001392 else
1393 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394}
1395
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001396/*
1397 * Copy already defined function "lambda" to a new function with name "global".
1398 * This is for when a compiled function defines a global function.
1399 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001400 int
1401copy_func(char_u *lambda, char_u *global, ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001402{
1403 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01001404 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001405
1406 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001407 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001408 semsg(_(e_lambda_function_not_found_str), lambda);
1409 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001410 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001411
1412 // TODO: handle ! to overwrite
1413 fp = find_func(global, TRUE, NULL);
1414 if (fp != NULL)
1415 {
1416 semsg(_(e_funcexts), global);
1417 return FAIL;
1418 }
1419
1420 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
1421 if (fp == NULL)
1422 return FAIL;
1423
1424 fp->uf_varargs = ufunc->uf_varargs;
1425 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
1426 fp->uf_def_status = ufunc->uf_def_status;
1427 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
1428 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
1429 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
1430 == FAIL
1431 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
1432 goto failed;
1433
1434 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
1435 : vim_strsave(ufunc->uf_name_exp);
1436 if (ufunc->uf_arg_types != NULL)
1437 {
1438 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
1439 if (fp->uf_arg_types == NULL)
1440 goto failed;
1441 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
1442 sizeof(type_T *) * fp->uf_args.ga_len);
1443 }
1444 if (ufunc->uf_def_arg_idx != NULL)
1445 {
1446 fp->uf_def_arg_idx = ALLOC_MULT(int, fp->uf_def_args.ga_len + 1);
1447 if (fp->uf_def_arg_idx == NULL)
1448 goto failed;
1449 mch_memmove(fp->uf_def_arg_idx, ufunc->uf_def_arg_idx,
1450 sizeof(int) * fp->uf_def_args.ga_len + 1);
1451 }
1452 if (ufunc->uf_va_name != NULL)
1453 {
1454 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
1455 if (fp->uf_va_name == NULL)
1456 goto failed;
1457 }
1458 fp->uf_ret_type = ufunc->uf_ret_type;
1459
1460 fp->uf_refcount = 1;
1461 STRCPY(fp->uf_name, global);
1462 hash_add(&func_hashtab, UF2HIKEY(fp));
1463
1464 // the referenced dfunc_T is now used one more time
1465 link_def_function(fp);
1466
1467 // Create a partial to store the context of the function, if not done
1468 // already.
1469 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
1470 {
1471 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
1472
1473 if (pt == NULL)
1474 goto failed;
1475 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
1476 goto failed;
1477 ufunc->uf_partial = pt;
1478 --pt->pt_refcount; // not referenced here yet
1479 }
1480 if (ufunc->uf_partial != NULL)
1481 {
1482 fp->uf_partial = ufunc->uf_partial;
1483 ++fp->uf_partial->pt_refcount;
1484 }
1485
1486 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001487
1488failed:
1489 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001490 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001491}
1492
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001493static int funcdepth = 0;
1494
1495/*
1496 * Increment the function call depth count.
1497 * Return FAIL when going over 'maxfuncdepth'.
1498 * Otherwise return OK, must call funcdepth_decrement() later!
1499 */
1500 int
1501funcdepth_increment(void)
1502{
1503 if (funcdepth >= p_mfd)
1504 {
1505 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
1506 return FAIL;
1507 }
1508 ++funcdepth;
1509 return OK;
1510}
1511
1512 void
1513funcdepth_decrement(void)
1514{
1515 --funcdepth;
1516}
1517
1518/*
1519 * Get the current function call depth.
1520 */
1521 int
1522funcdepth_get(void)
1523{
1524 return funcdepth;
1525}
1526
1527/*
1528 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001529 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001530 * times as funcdepth_increment().
1531 */
1532 void
1533funcdepth_restore(int depth)
1534{
1535 funcdepth = depth;
1536}
Bram Moolenaar6914c642017-04-01 21:21:30 +02001537
1538/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001539 * Call a user function.
1540 */
1541 static void
1542call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001543 ufunc_T *fp, // pointer to function
1544 int argcount, // nr of args
1545 typval_T *argvars, // arguments
1546 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001547 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01001548 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001549{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001550 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001551 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001552 funccall_T *fc;
1553 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001554 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001555 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001556 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001557 int i;
1558 int ai;
1559 int islambda = FALSE;
1560 char_u numbuf[NUMBUFLEN];
1561 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001562#ifdef FEAT_PROFILE
1563 proftime_T wait_start;
1564 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001565 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001566#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001567 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001568
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001569 // If depth of calling is getting too high, don't execute the function.
1570 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001571 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001572 rettv->v_type = VAR_NUMBER;
1573 rettv->vval.v_number = -1;
1574 return;
1575 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001576
Bram Moolenaare38eab22019-12-05 21:50:01 +01001577 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001578
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001579 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001580 if (fc == NULL)
1581 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001582 fc->caller = current_funccal;
1583 current_funccal = fc;
1584 fc->func = fp;
1585 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001586 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001587 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001588 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1589 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001590 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001591 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001592 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001593
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001594 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 {
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001596 // Execute the function, possibly compiling it first.
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001597 call_def_function(fp, argcount, argvars, funcexe->partial, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001598 funcdepth_decrement();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 current_funccal = fc->caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 free_funccal(fc);
1601 return;
1602 }
1603
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001604 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1605 islambda = TRUE;
1606
1607 /*
1608 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1609 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1610 * each argument variable and saves a lot of time.
1611 */
1612 /*
1613 * Init l: variables.
1614 */
1615 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1616 if (selfdict != NULL)
1617 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001618 // Set l:self to "selfdict". Use "name" to avoid a warning from
1619 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001620 v = &fc->fixvar[fixvar_idx++].var;
1621 name = v->di_key;
1622 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001623 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001624 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1625 v->di_tv.v_type = VAR_DICT;
1626 v->di_tv.v_lock = 0;
1627 v->di_tv.vval.v_dict = selfdict;
1628 ++selfdict->dv_refcount;
1629 }
1630
1631 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001632 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001633 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001634 * Set a:000 to a list with room for the "..." arguments.
1635 */
1636 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001637 if ((fp->uf_flags & FC_NOARGS) == 0)
1638 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001639 (varnumber_T)(argcount >= fp->uf_args.ga_len
1640 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001641 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001642 if ((fp->uf_flags & FC_NOARGS) == 0)
1643 {
1644 // Use "name" to avoid a warning from some compiler that checks the
1645 // destination size.
1646 v = &fc->fixvar[fixvar_idx++].var;
1647 name = v->di_key;
1648 STRCPY(name, "000");
1649 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1650 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1651 v->di_tv.v_type = VAR_LIST;
1652 v->di_tv.v_lock = VAR_FIXED;
1653 v->di_tv.vval.v_list = &fc->l_varlist;
1654 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001655 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001656 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1657 fc->l_varlist.lv_lock = VAR_FIXED;
1658
1659 /*
1660 * Set a:firstline to "firstline" and a:lastline to "lastline".
1661 * Set a:name to named arguments.
1662 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001663 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001664 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001665 if ((fp->uf_flags & FC_NOARGS) == 0)
1666 {
1667 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001668 (varnumber_T)funcexe->firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001669 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001670 (varnumber_T)funcexe->lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001671 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001672 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001673 {
1674 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001675 typval_T def_rettv;
1676 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001677
1678 ai = i - fp->uf_args.ga_len;
1679 if (ai < 0)
1680 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001681 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001682 name = FUNCARG(fp, i);
1683 if (islambda)
1684 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001685
1686 // evaluate named argument default expression
1687 isdefault = ai + fp->uf_def_args.ga_len >= 0
1688 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1689 && argvars[i].vval.v_number == VVAL_NONE));
1690 if (isdefault)
1691 {
1692 char_u *default_expr = NULL;
1693 def_rettv.v_type = VAR_NUMBER;
1694 def_rettv.vval.v_number = -1;
1695
1696 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1697 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001698 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001699 {
1700 default_arg_err = 1;
1701 break;
1702 }
1703 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001704 }
1705 else
1706 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001707 if ((fp->uf_flags & FC_NOARGS) != 0)
1708 // Bail out if no a: arguments used (in lambda).
1709 break;
1710
Bram Moolenaare38eab22019-12-05 21:50:01 +01001711 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001712 sprintf((char *)numbuf, "%d", ai + 1);
1713 name = numbuf;
1714 }
1715 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1716 {
1717 v = &fc->fixvar[fixvar_idx++].var;
1718 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001719 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001720 }
1721 else
1722 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001723 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001724 if (v == NULL)
1725 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001726 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001727 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001728
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001729 // Note: the values are copied directly to avoid alloc/free.
1730 // "argvars" must have VAR_FIXED for v_lock.
1731 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001732 v->di_tv.v_lock = VAR_FIXED;
1733
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001734 if (addlocal)
1735 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001736 // Named arguments should be accessed without the "a:" prefix in
1737 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001738 copy_tv(&v->di_tv, &v->di_tv);
1739 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001740 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001741 else
1742 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001743
1744 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1745 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001746 listitem_T *li = &fc->l_listitems[ai];
1747
1748 li->li_tv = argvars[i];
1749 li->li_tv.v_lock = VAR_FIXED;
1750 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001751 }
1752 }
1753
Bram Moolenaare38eab22019-12-05 21:50:01 +01001754 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001755 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001756
1757 if (fp->uf_flags & FC_SANDBOX)
1758 {
1759 using_sandbox = TRUE;
1760 ++sandbox;
1761 }
1762
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001763 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001764 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001765 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001766 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001767 ++no_wait_return;
1768 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001769
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001770 smsg(_("calling %s"), SOURCING_NAME);
1771 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001772 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001773 char_u buf[MSG_BUF_LEN];
1774 char_u numbuf2[NUMBUFLEN];
1775 char_u *tofree;
1776 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001777
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001778 msg_puts("(");
1779 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001780 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001781 if (i > 0)
1782 msg_puts(", ");
1783 if (argvars[i].v_type == VAR_NUMBER)
1784 msg_outnum((long)argvars[i].vval.v_number);
1785 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001786 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001787 // Do not want errors such as E724 here.
1788 ++emsg_off;
1789 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1790 --emsg_off;
1791 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001792 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001793 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001794 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001795 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1796 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001797 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001798 msg_puts((char *)s);
1799 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001800 }
1801 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001802 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001803 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001804 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001805 msg_puts("\n"); // don't overwrite this either
1806
1807 verbose_leave_scroll();
1808 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001809 }
1810#ifdef FEAT_PROFILE
1811 if (do_profiling == PROF_YES)
1812 {
1813 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001814 {
1815 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001816 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001817 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001818 if (fp->uf_profiling
1819 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1820 {
1821 ++fp->uf_tm_count;
1822 profile_start(&call_start);
1823 profile_zero(&fp->uf_tm_children);
1824 }
1825 script_prof_save(&wait_start);
1826 }
1827#endif
1828
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001829 save_current_sctx = current_sctx;
1830 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001831 save_did_emsg = did_emsg;
1832 did_emsg = FALSE;
1833
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001834 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1835 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001836 else if (islambda)
1837 {
1838 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
1839
1840 // A Lambda always has the command "return {expr}". It is much faster
1841 // to evaluate {expr} directly.
1842 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001843 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001844 --ex_nesting_level;
1845 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001846 else
1847 // call do_cmdline() to execute the lines
1848 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001849 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1850
1851 --RedrawingDisabled;
1852
Bram Moolenaare38eab22019-12-05 21:50:01 +01001853 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001854 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1855 {
1856 clear_tv(rettv);
1857 rettv->v_type = VAR_NUMBER;
1858 rettv->vval.v_number = -1;
1859 }
1860
1861#ifdef FEAT_PROFILE
1862 if (do_profiling == PROF_YES && (fp->uf_profiling
1863 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1864 {
1865 profile_end(&call_start);
1866 profile_sub_wait(&wait_start, &call_start);
1867 profile_add(&fp->uf_tm_total, &call_start);
1868 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1869 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1870 {
1871 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1872 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1873 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001874 if (started_profiling)
1875 // make a ":profdel func" stop profiling the function
1876 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001877 }
1878#endif
1879
Bram Moolenaare38eab22019-12-05 21:50:01 +01001880 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001881 if (p_verbose >= 12)
1882 {
1883 ++no_wait_return;
1884 verbose_enter_scroll();
1885
1886 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001887 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001888 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001889 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001890 (long)fc->rettv->vval.v_number);
1891 else
1892 {
1893 char_u buf[MSG_BUF_LEN];
1894 char_u numbuf2[NUMBUFLEN];
1895 char_u *tofree;
1896 char_u *s;
1897
Bram Moolenaare38eab22019-12-05 21:50:01 +01001898 // The value may be very long. Skip the middle part, so that we
1899 // have some idea how it starts and ends. smsg() would always
1900 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001901 ++emsg_off;
1902 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1903 --emsg_off;
1904 if (s != NULL)
1905 {
1906 if (vim_strsize(s) > MSG_BUF_CLEN)
1907 {
1908 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1909 s = buf;
1910 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001911 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001912 vim_free(tofree);
1913 }
1914 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001915 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001916
1917 verbose_leave_scroll();
1918 --no_wait_return;
1919 }
1920
Bram Moolenaare31ee862020-01-07 20:59:34 +01001921 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001922 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001923 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001924#ifdef FEAT_PROFILE
1925 if (do_profiling == PROF_YES)
1926 script_prof_restore(&wait_start);
1927#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001928 if (using_sandbox)
1929 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001930
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001931 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001932 {
1933 ++no_wait_return;
1934 verbose_enter_scroll();
1935
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001936 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001937 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001938
1939 verbose_leave_scroll();
1940 --no_wait_return;
1941 }
1942
1943 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001944 funcdepth_decrement();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001945 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001946}
1947
1948/*
Bram Moolenaar50824712020-12-20 21:10:17 +01001949 * Check the argument count for user function "fp".
1950 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
1951 */
1952 int
1953check_user_func_argcount(ufunc_T *fp, int argcount)
1954{
1955 int regular_args = fp->uf_args.ga_len;
1956
1957 if (argcount < regular_args - fp->uf_def_args.ga_len)
1958 return FCERR_TOOFEW;
1959 else if (!has_varargs(fp) && argcount > regular_args)
1960 return FCERR_TOOMANY;
1961 return FCERR_UNKNOWN;
1962}
1963
1964/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001966 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001967 int
1968call_user_func_check(
1969 ufunc_T *fp,
1970 int argcount,
1971 typval_T *argvars,
1972 typval_T *rettv,
1973 funcexe_T *funcexe,
1974 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001975{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001977
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1979 *funcexe->doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01001980 error = check_user_func_argcount(fp, argcount);
1981 if (error != FCERR_UNKNOWN)
1982 return error;
1983 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984 error = FCERR_DICT;
1985 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001986 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 int did_save_redo = FALSE;
1988 save_redo_T save_redo;
1989
1990 /*
1991 * Call the user function.
1992 * Save and restore search patterns, script variables and
1993 * redo buffer.
1994 */
1995 save_search_patterns();
1996 if (!ins_compl_active())
1997 {
1998 saveRedobuff(&save_redo);
1999 did_save_redo = TRUE;
2000 }
2001 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002002 call_user_func(fp, argcount, argvars, rettv, funcexe,
2003 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
2005 // Function was unreferenced while being used, free it now.
2006 func_clear_free(fp, FALSE);
2007 if (did_save_redo)
2008 restoreRedobuff(&save_redo);
2009 restore_search_patterns();
2010 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002011 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002012 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002013}
2014
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002015static funccal_entry_T *funccal_stack = NULL;
2016
2017/*
2018 * Save the current function call pointer, and set it to NULL.
2019 * Used when executing autocommands and for ":source".
2020 */
2021 void
2022save_funccal(funccal_entry_T *entry)
2023{
2024 entry->top_funccal = current_funccal;
2025 entry->next = funccal_stack;
2026 funccal_stack = entry;
2027 current_funccal = NULL;
2028}
2029
2030 void
2031restore_funccal(void)
2032{
2033 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002034 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002035 else
2036 {
2037 current_funccal = funccal_stack->top_funccal;
2038 funccal_stack = funccal_stack->next;
2039 }
2040}
2041
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02002042 funccall_T *
2043get_current_funccal(void)
2044{
2045 return current_funccal;
2046}
2047
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002048/*
2049 * Mark all functions of script "sid" as deleted.
2050 */
2051 void
2052delete_script_functions(int sid)
2053{
2054 hashitem_T *hi;
2055 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02002056 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002057 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002058 size_t len;
2059
2060 buf[0] = K_SPECIAL;
2061 buf[1] = KS_EXTRA;
2062 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002063 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002064 len = STRLEN(buf);
2065
Bram Moolenaarce658352020-07-31 23:47:12 +02002066 while (todo > 0)
2067 {
2068 todo = func_hashtab.ht_used;
2069 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2070 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002071 {
Bram Moolenaarce658352020-07-31 23:47:12 +02002072 fp = HI2UF(hi);
2073 if (STRNCMP(fp->uf_name, buf, len) == 0)
2074 {
2075 int changed = func_hashtab.ht_changed;
2076
2077 fp->uf_flags |= FC_DEAD;
2078 func_clear(fp, TRUE);
2079 // When clearing a function another function can be cleared
2080 // as a side effect. When that happens start over.
2081 if (changed != func_hashtab.ht_changed)
2082 break;
2083 }
2084 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002085 }
Bram Moolenaarce658352020-07-31 23:47:12 +02002086 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002087}
2088
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002089#if defined(EXITFREE) || defined(PROTO)
2090 void
2091free_all_functions(void)
2092{
2093 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002094 ufunc_T *fp;
2095 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002096 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002097 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002098
Bram Moolenaare38eab22019-12-05 21:50:01 +01002099 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02002100 while (current_funccal != NULL)
2101 {
2102 clear_tv(current_funccal->rettv);
2103 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002104 if (current_funccal == NULL && funccal_stack != NULL)
2105 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02002106 }
2107
Bram Moolenaare38eab22019-12-05 21:50:01 +01002108 // First clear what the functions contain. Since this may lower the
2109 // reference count of a function, it may also free a function and change
2110 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002111 while (todo > 0)
2112 {
2113 todo = func_hashtab.ht_used;
2114 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2115 if (!HASHITEM_EMPTY(hi))
2116 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002117 // clear the def function index now
2118 fp = HI2UF(hi);
2119 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002120 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121
Bram Moolenaare38eab22019-12-05 21:50:01 +01002122 // Only free functions that are not refcounted, those are
2123 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002124 if (func_name_refcount(fp->uf_name))
2125 ++skipped;
2126 else
2127 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002128 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002129 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002130 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002131 {
2132 skipped = 0;
2133 break;
2134 }
2135 }
2136 --todo;
2137 }
2138 }
2139
Bram Moolenaare38eab22019-12-05 21:50:01 +01002140 // Now actually free the functions. Need to start all over every time,
2141 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002142 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002143 while (func_hashtab.ht_used > skipped)
2144 {
2145 todo = func_hashtab.ht_used;
2146 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002147 if (!HASHITEM_EMPTY(hi))
2148 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02002149 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002150 // Only free functions that are not refcounted, those are
2151 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02002152 fp = HI2UF(hi);
2153 if (func_name_refcount(fp->uf_name))
2154 ++skipped;
2155 else
2156 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02002157 if (func_free(fp, FALSE) == OK)
2158 {
2159 skipped = 0;
2160 break;
2161 }
2162 // did not actually free it
2163 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002164 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002165 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02002166 }
2167 if (skipped == 0)
2168 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169
2170 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002171}
2172#endif
2173
2174/*
2175 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02002176 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002177 * "len" is the length of "name", or -1 for NUL terminated.
2178 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002179 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002180builtin_function(char_u *name, int len)
2181{
2182 char_u *p;
2183
Bram Moolenaara26b9702020-04-18 19:53:28 +02002184 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002185 return FALSE;
2186 p = vim_strchr(name, AUTOLOAD_CHAR);
2187 return p == NULL || (len > 0 && p > name + len);
2188}
2189
2190 int
2191func_call(
2192 char_u *name,
2193 typval_T *args,
2194 partial_T *partial,
2195 dict_T *selfdict,
2196 typval_T *rettv)
2197{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002198 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002199 listitem_T *item;
2200 typval_T argv[MAX_FUNC_ARGS + 1];
2201 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002202 int r = 0;
2203
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002204 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002205 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002206 {
2207 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
2208 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002209 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002210 break;
2211 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002212 // Make a copy of each argument. This is needed to be able to set
2213 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002214 copy_tv(&item->li_tv, &argv[argc++]);
2215 }
2216
2217 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002218 {
2219 funcexe_T funcexe;
2220
Bram Moolenaara80faa82020-04-12 19:37:17 +02002221 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002222 funcexe.firstline = curwin->w_cursor.lnum;
2223 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002224 funcexe.evaluate = TRUE;
2225 funcexe.partial = partial;
2226 funcexe.selfdict = selfdict;
2227 r = call_func(name, -1, rettv, argc, argv, &funcexe);
2228 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002229
Bram Moolenaare38eab22019-12-05 21:50:01 +01002230 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002231 while (argc > 0)
2232 clear_tv(&argv[--argc]);
2233
2234 return r;
2235}
2236
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002237static int callback_depth = 0;
2238
2239 int
2240get_callback_depth(void)
2241{
2242 return callback_depth;
2243}
2244
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002245/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002246 * Invoke call_func() with a callback.
2247 */
2248 int
2249call_callback(
2250 callback_T *callback,
2251 int len, // length of "name" or -1 to use strlen()
2252 typval_T *rettv, // return value goes here
2253 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002254 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002255 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002256{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002257 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002258 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002259
Bram Moolenaara80faa82020-04-12 19:37:17 +02002260 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002261 funcexe.evaluate = TRUE;
2262 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002263 ++callback_depth;
2264 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
2265 --callback_depth;
2266 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002267}
2268
2269/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270 * Give an error message for the result of a function.
2271 * Nothing if "error" is FCERR_NONE.
2272 */
2273 void
2274user_func_error(int error, char_u *name)
2275{
2276 switch (error)
2277 {
2278 case FCERR_UNKNOWN:
2279 emsg_funcname(e_unknownfunc, name);
2280 break;
2281 case FCERR_NOTMETHOD:
2282 emsg_funcname(
2283 N_("E276: Cannot use function as a method: %s"), name);
2284 break;
2285 case FCERR_DELETED:
2286 emsg_funcname(N_(e_func_deleted), name);
2287 break;
2288 case FCERR_TOOMANY:
2289 emsg_funcname((char *)e_toomanyarg, name);
2290 break;
2291 case FCERR_TOOFEW:
2292 emsg_funcname((char *)e_toofewarg, name);
2293 break;
2294 case FCERR_SCRIPT:
2295 emsg_funcname(
2296 N_("E120: Using <SID> not in a script context: %s"), name);
2297 break;
2298 case FCERR_DICT:
2299 emsg_funcname(
2300 N_("E725: Calling dict function without Dictionary: %s"),
2301 name);
2302 break;
2303 }
2304}
2305
2306/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002307 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002308 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002309 * Return FAIL when the function can't be called, OK otherwise.
2310 * Also returns OK when an error was encountered while executing the function.
2311 */
2312 int
2313call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02002314 char_u *funcname, // name of the function
2315 int len, // length of "name" or -1 to use strlen()
2316 typval_T *rettv, // return value goes here
2317 int argcount_in, // number of "argvars"
2318 typval_T *argvars_in, // vars for arguments, must have "argcount"
2319 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002320 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002321{
2322 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01002323 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002324 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002325 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002326 char_u fname_buf[FLEN_FIXED + 1];
2327 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002328 char_u *fname = NULL;
2329 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002330 int argcount = argcount_in;
2331 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002332 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002333 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
2334 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002335 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002336 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002337 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002338
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02002339 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
2340 // even when call_func() returns FAIL.
2341 rettv->v_type = VAR_UNKNOWN;
2342
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002343 if (partial != NULL)
2344 fp = partial->pt_func;
2345 if (fp == NULL)
2346 {
2347 // Make a copy of the name, if it comes from a funcref variable it
2348 // could be changed or deleted in the called function.
2349 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
2350 if (name == NULL)
2351 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002352
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002353 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
2354 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002355
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002356 if (funcexe->doesrange != NULL)
2357 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002358
2359 if (partial != NULL)
2360 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002361 // When the function has a partial with a dict and there is a dict
2362 // argument, use the dict argument. That is backwards compatible.
2363 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002364 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002365 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01002366 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002367 {
2368 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002369 {
2370 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
2371 {
Bram Moolenaaref140542019-12-31 21:27:13 +01002372 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002373 goto theend;
2374 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002375 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002376 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002377 for (i = 0; i < argcount_in; ++i)
2378 argv[i + argv_clear] = argvars_in[i];
2379 argvars = argv;
2380 argcount = partial->pt_argc + argcount_in;
2381 }
2382 }
2383
Bram Moolenaaref140542019-12-31 21:27:13 +01002384 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002385 {
2386 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02002387 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002388
Bram Moolenaar333894b2020-08-01 18:53:07 +02002389 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002390 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02002391 {
2392 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002393 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02002394 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002395
Bram Moolenaare38eab22019-12-05 21:50:01 +01002396 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002397 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01002398 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002399
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002400 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002401 {
2402 /*
2403 * User defined function.
2404 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002405 if (fp == NULL)
Bram Moolenaar333894b2020-08-01 18:53:07 +02002406 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002407
Bram Moolenaare38eab22019-12-05 21:50:01 +01002408 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002409 if (fp == NULL
2410 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002411 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002412 && !aborting())
2413 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002414 // executed an autocommand, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02002415 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002416 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002417 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002418 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
2419 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002420 // loaded a package, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02002421 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002422 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02002423 if (fp == NULL)
2424 {
2425 char_u *p = untrans_function_name(rfname);
2426
2427 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02002428 // Don't do this if the name starts with "s:".
2429 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaar333894b2020-08-01 18:53:07 +02002430 fp = find_func(p, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002431 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002432
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002433 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01002434 error = FCERR_DELETED;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002435#ifdef FEAT_LUA
2436 else if (fp != NULL && (fp->uf_flags & FC_CFUNC))
2437 {
2438 cfunc_T cb = fp->uf_cb;
2439
2440 error = (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
2441 }
2442#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002443 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002444 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002445 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01002446 // postponed filling in the arguments, do it now
2447 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002448 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002449
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002450 if (funcexe->basetv != NULL)
2451 {
2452 // Method call: base->Method()
2453 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
2454 argv[0] = *funcexe->basetv;
2455 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002456 argvars = argv;
2457 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002458 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002459
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002460 error = call_user_func_check(fp, argcount, argvars, rettv,
2461 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002462 }
2463 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002464 else if (funcexe->basetv != NULL)
2465 {
2466 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002467 * expr->method(): Find the method name in the table, call its
2468 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02002469 */
2470 error = call_internal_method(fname, argcount, argvars, rettv,
2471 funcexe->basetv);
2472 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002473 else
2474 {
2475 /*
2476 * Find the function name in the table, call its implementation.
2477 */
2478 error = call_internal_func(fname, argcount, argvars, rettv);
2479 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02002480
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002481 /*
2482 * The function call (or "FuncUndefined" autocommand sequence) might
2483 * have been aborted by an error, an interrupt, or an explicitly thrown
2484 * exception that has not been caught so far. This situation can be
2485 * tested for by calling aborting(). For an error in an internal
2486 * function or for the "E132" error in call_user_func(), however, the
2487 * throw point at which the "force_abort" flag (temporarily reset by
2488 * emsg()) is normally updated has not been reached yet. We need to
2489 * update that flag first to make aborting() reliable.
2490 */
2491 update_force_abort();
2492 }
Bram Moolenaaref140542019-12-31 21:27:13 +01002493 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002494 ret = OK;
2495
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002496theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002497 /*
2498 * Report an error unless the argument evaluation or function call has been
2499 * cancelled due to an aborting error, an interrupt, or an exception.
2500 */
2501 if (!aborting())
2502 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002503 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002504 }
2505
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002506 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002507 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002508 clear_tv(&argv[--argv_clear + argv_base]);
2509
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002510 vim_free(tofree);
2511 vim_free(name);
2512
2513 return ret;
2514}
2515
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002516 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517printable_func_name(ufunc_T *fp)
2518{
2519 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
2520}
2521
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002522/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002523 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002524 */
2525 static void
2526list_func_head(ufunc_T *fp, int indent)
2527{
2528 int j;
2529
2530 msg_start();
2531 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002532 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002533 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002534 msg_puts("def ");
2535 else
2536 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002538 msg_putchar('(');
2539 for (j = 0; j < fp->uf_args.ga_len; ++j)
2540 {
2541 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002542 msg_puts(", ");
2543 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 if (fp->uf_arg_types != NULL)
2545 {
2546 char *tofree;
2547
2548 msg_puts(": ");
2549 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
2550 vim_free(tofree);
2551 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002552 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
2553 {
2554 msg_puts(" = ");
2555 msg_puts(((char **)(fp->uf_def_args.ga_data))
2556 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
2557 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002558 }
2559 if (fp->uf_varargs)
2560 {
2561 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002562 msg_puts(", ");
2563 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002564 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002565 if (fp->uf_va_name != NULL)
2566 {
2567 if (j)
2568 msg_puts(", ");
2569 msg_puts("...");
2570 msg_puts((char *)fp->uf_va_name);
2571 if (fp->uf_va_type)
2572 {
2573 char *tofree;
2574
2575 msg_puts(": ");
2576 msg_puts(type_name(fp->uf_va_type, &tofree));
2577 vim_free(tofree);
2578 }
2579 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002580 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002581
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002582 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002583 {
2584 if (fp->uf_ret_type != &t_void)
2585 {
2586 char *tofree;
2587
2588 msg_puts(": ");
2589 msg_puts(type_name(fp->uf_ret_type, &tofree));
2590 vim_free(tofree);
2591 }
2592 }
2593 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002594 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002595 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002596 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002597 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002598 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002599 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002600 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002601 msg_clr_eos();
2602 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002603 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002604}
2605
2606/*
2607 * Get a function name, translating "<SID>" and "<SNR>".
2608 * Also handles a Funcref in a List or Dictionary.
2609 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002610 * Set "*is_global" to TRUE when the function must be global, unless
2611 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002612 * flags:
2613 * TFN_INT: internal function name OK
2614 * TFN_QUIET: be quiet
2615 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002616 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002617 * Advances "pp" to just after the function name (if no error).
2618 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002619 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002620trans_function_name(
2621 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002622 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002623 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002624 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002625 funcdict_T *fdp, // return: info about dictionary used
2626 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002627{
2628 char_u *name = NULL;
2629 char_u *start;
2630 char_u *end;
2631 int lead;
2632 char_u sid_buf[20];
2633 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002635 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636 int vim9script;
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002637 static char *e_function_name = N_("E129: Function name required");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002638
2639 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02002640 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002641 start = *pp;
2642
Bram Moolenaare38eab22019-12-05 21:50:01 +01002643 // Check for hard coded <SNR>: already translated function ID (from a user
2644 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002645 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2646 && (*pp)[2] == (int)KE_SNR)
2647 {
2648 *pp += 3;
2649 len = get_id_len(pp) + 3;
2650 return vim_strnsave(start, len);
2651 }
2652
Bram Moolenaare38eab22019-12-05 21:50:01 +01002653 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2654 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002655 lead = eval_fname_script(start);
2656 if (lead > 2)
2657 start += lead;
2658
Bram Moolenaare38eab22019-12-05 21:50:01 +01002659 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002660 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002661 lead > 2 ? 0 : FNE_CHECK_START);
2662 if (end == start)
2663 {
2664 if (!skip)
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002665 emsg(_(e_function_name));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002666 goto theend;
2667 }
2668 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2669 {
2670 /*
2671 * Report an invalid expression in braces, unless the expression
2672 * evaluation has been cancelled due to an aborting error, an
2673 * interrupt, or an exception.
2674 */
2675 if (!aborting())
2676 {
2677 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002678 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002679 }
2680 else
2681 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2682 goto theend;
2683 }
2684
2685 if (lv.ll_tv != NULL)
2686 {
2687 if (fdp != NULL)
2688 {
2689 fdp->fd_dict = lv.ll_dict;
2690 fdp->fd_newkey = lv.ll_newkey;
2691 lv.ll_newkey = NULL;
2692 fdp->fd_di = lv.ll_di;
2693 }
2694 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2695 {
2696 name = vim_strsave(lv.ll_tv->vval.v_string);
2697 *pp = end;
2698 }
2699 else if (lv.ll_tv->v_type == VAR_PARTIAL
2700 && lv.ll_tv->vval.v_partial != NULL)
2701 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002702 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002703 *pp = end;
2704 if (partial != NULL)
2705 *partial = lv.ll_tv->vval.v_partial;
2706 }
2707 else
2708 {
2709 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2710 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002711 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002712 else
2713 *pp = end;
2714 name = NULL;
2715 }
2716 goto theend;
2717 }
2718
2719 if (lv.ll_name == NULL)
2720 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002721 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002722 *pp = end;
2723 goto theend;
2724 }
2725
Bram Moolenaare38eab22019-12-05 21:50:01 +01002726 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002727 if (lv.ll_exp_name != NULL)
2728 {
2729 len = (int)STRLEN(lv.ll_exp_name);
2730 name = deref_func_name(lv.ll_exp_name, &len, partial,
2731 flags & TFN_NO_AUTOLOAD);
2732 if (name == lv.ll_exp_name)
2733 name = NULL;
2734 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002735 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002736 {
2737 len = (int)(end - *pp);
2738 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2739 if (name == *pp)
2740 name = NULL;
2741 }
2742 if (name != NULL)
2743 {
2744 name = vim_strsave(name);
2745 *pp = end;
2746 if (STRNCMP(name, "<SNR>", 5) == 0)
2747 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002748 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002749 name[0] = K_SPECIAL;
2750 name[1] = KS_EXTRA;
2751 name[2] = (int)KE_SNR;
2752 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2753 }
2754 goto theend;
2755 }
2756
2757 if (lv.ll_exp_name != NULL)
2758 {
2759 len = (int)STRLEN(lv.ll_exp_name);
2760 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2761 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2762 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002763 // When there was "s:" already or the name expanded to get a
2764 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002765 lv.ll_name += 2;
2766 len -= 2;
2767 lead = 2;
2768 }
2769 }
2770 else
2771 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002772 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002773 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002774 {
2775 if (is_global != NULL && lv.ll_name[0] == 'g')
2776 *is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002777 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002778 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002779 len = (int)(end - lv.ll_name);
2780 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002781 if (len <= 0)
2782 {
2783 if (!skip)
2784 emsg(_(e_function_name));
2785 goto theend;
2786 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002787
Bram Moolenaar17f700a2020-12-19 21:23:42 +01002788 // In Vim9 script a user function is script-local by default, unless it
2789 // starts with a lower case character: dict.func().
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002790 vim9script = ASCII_ISUPPER(*start) && in_vim9script();
Bram Moolenaar17f700a2020-12-19 21:23:42 +01002791 if (vim9script)
2792 {
2793 char_u *p;
2794
2795 // SomeScript#func() is a global function.
2796 for (p = start; *p != NUL && *p != '('; ++p)
2797 if (*p == AUTOLOAD_CHAR)
2798 vim9script = FALSE;
2799 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002801 /*
2802 * Copy the function name to allocated memory.
2803 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2804 * Accept <SNR>123_name() outside a script.
2805 */
2806 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002807 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002809 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810 if (!vim9script)
2811 lead = 3;
2812 if (vim9script || (lv.ll_exp_name != NULL
2813 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002814 || eval_fname_sid(*pp))
2815 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002817 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002818 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002819 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002820 goto theend;
2821 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002822 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002823 if (vim9script)
2824 extra = 3 + (int)STRLEN(sid_buf);
2825 else
2826 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002827 }
2828 }
2829 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2830 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002831 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002832 start);
2833 goto theend;
2834 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002835 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002836 {
2837 char_u *cp = vim_strchr(lv.ll_name, ':');
2838
2839 if (cp != NULL && cp < end)
2840 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002841 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002842 goto theend;
2843 }
2844 }
2845
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002847 if (name != NULL)
2848 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002849 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002850 {
2851 name[0] = K_SPECIAL;
2852 name[1] = KS_EXTRA;
2853 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002855 STRCPY(name + 3, sid_buf);
2856 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2858 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002859 }
2860 *pp = end;
2861
2862theend:
2863 clear_lval(&lv);
2864 return name;
2865}
2866
2867/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02002868 * Assuming "name" is the result of trans_function_name() and it was prefixed
2869 * to use the script-local name, return the unmodified name (points into
2870 * "name"). Otherwise return NULL.
2871 * This can be used to first search for a script-local function and fall back
2872 * to the global function if not found.
2873 */
2874 char_u *
2875untrans_function_name(char_u *name)
2876{
2877 char_u *p;
2878
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002879 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02002880 {
2881 p = vim_strchr(name, '_');
2882 if (p != NULL)
2883 return p + 1;
2884 }
2885 return NULL;
2886}
2887
2888/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002889 * List functions. When "regmatch" is NULL all of then.
2890 * Otherwise functions matching "regmatch".
2891 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002892 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002893list_functions(regmatch_T *regmatch)
2894{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002895 int changed = func_hashtab.ht_changed;
2896 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002897 hashitem_T *hi;
2898
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002899 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002900 {
2901 if (!HASHITEM_EMPTY(hi))
2902 {
2903 ufunc_T *fp = HI2UF(hi);
2904
2905 --todo;
2906 if ((fp->uf_flags & FC_DEAD) == 0
2907 && (regmatch == NULL
2908 ? !message_filtered(fp->uf_name)
2909 && !func_name_refcount(fp->uf_name)
2910 : !isdigit(*fp->uf_name)
2911 && vim_regexec(regmatch, fp->uf_name, 0)))
2912 {
2913 list_func_head(fp, FALSE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002914 if (changed != func_hashtab.ht_changed)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002915 {
2916 emsg(_("E454: function list was modified"));
2917 return;
2918 }
2919 }
2920 }
2921 }
2922}
2923
2924/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02002925 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002926 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
2927 * the function name.
Bram Moolenaar04b12692020-05-04 23:24:44 +02002928 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002929 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02002930 ufunc_T *
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002931define_function(exarg_T *eap, char_u *name_arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002932{
2933 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002934 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002935 int j;
2936 int c;
2937 int saved_did_emsg;
2938 int saved_wait_return = need_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002939 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002940 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002941 char_u *p;
2942 char_u *arg;
2943 char_u *line_arg = NULL;
2944 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002946 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002947 garray_T newlines;
2948 int varargs = FALSE;
2949 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002951 ufunc_T *fp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002952 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002953 int indent;
2954 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955#define MAX_FUNC_NESTING 50
2956 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002957 dictitem_T *v;
2958 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002959 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002960 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002961 hashitem_T *hi;
Bram Moolenaar66250c92020-08-20 15:02:42 +02002962 getline_opt_T getline_options = GETLINE_CONCAT_CONT;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002963 linenr_T sourcing_lnum_off;
2964 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002965 int is_heredoc = FALSE;
2966 char_u *skip_until = NULL;
2967 char_u *heredoc_trimmed = NULL;
Bram Moolenaare7e48382020-07-22 18:17:08 +02002968 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02002969 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002970
2971 /*
2972 * ":function" without argument: list functions.
2973 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002974 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002975 {
2976 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002977 list_functions(NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002978 eap->nextcmd = check_nextcmd(eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02002979 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002980 }
2981
2982 /*
2983 * ":function /pat": list functions matching pattern.
2984 */
2985 if (*eap->arg == '/')
2986 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002987 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002988 if (!eap->skip)
2989 {
2990 regmatch_T regmatch;
2991
2992 c = *p;
2993 *p = NUL;
2994 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2995 *p = c;
2996 if (regmatch.regprog != NULL)
2997 {
2998 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002999 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003000 vim_regfree(regmatch.regprog);
3001 }
3002 }
3003 if (*p == '/')
3004 ++p;
3005 eap->nextcmd = check_nextcmd(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003006 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003007 }
3008
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 ga_init(&newargs);
3010 ga_init(&argtypes);
3011 ga_init(&default_args);
3012
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003013 /*
3014 * Get the function name. There are these situations:
3015 * func normal function name
3016 * "name" == func, "fudi.fd_dict" == NULL
3017 * dict.func new dictionary entry
3018 * "name" == NULL, "fudi.fd_dict" set,
3019 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
3020 * dict.func existing dict entry with a Funcref
3021 * "name" == func, "fudi.fd_dict" set,
3022 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
3023 * dict.func existing dict entry that's not a Funcref
3024 * "name" == NULL, "fudi.fd_dict" set,
3025 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
3026 * s:func script-local function name
3027 * g:func global function name, same as "func"
3028 */
3029 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003030 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003031 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02003032 // nested function, argument is (args).
3033 paren = TRUE;
3034 CLEAR_FIELD(fudi);
3035 }
3036 else
3037 {
3038 name = trans_function_name(&p, &is_global, eap->skip,
3039 TFN_NO_AUTOLOAD, &fudi, NULL);
3040 paren = (vim_strchr(p, '(') != NULL);
3041 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003042 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02003043 /*
3044 * Return on an invalid expression in braces, unless the expression
3045 * evaluation has been cancelled due to an aborting error, an
3046 * interrupt, or an exception.
3047 */
3048 if (!aborting())
3049 {
3050 if (!eap->skip && fudi.fd_newkey != NULL)
3051 semsg(_(e_dictkey), fudi.fd_newkey);
3052 vim_free(fudi.fd_newkey);
3053 return NULL;
3054 }
3055 else
3056 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003057 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003058 }
3059
Bram Moolenaare38eab22019-12-05 21:50:01 +01003060 // An error in a function call during evaluation of an expression in magic
3061 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003062 saved_did_emsg = did_emsg;
3063 did_emsg = FALSE;
3064
3065 /*
3066 * ":function func" with only function name: list function.
3067 */
3068 if (!paren)
3069 {
3070 if (!ends_excmd(*skipwhite(p)))
3071 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003072 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003073 goto ret_free;
3074 }
3075 eap->nextcmd = check_nextcmd(p);
3076 if (eap->nextcmd != NULL)
3077 *p = NUL;
3078 if (!eap->skip && !got_int)
3079 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003080 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003081 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
3082 {
3083 char_u *up = untrans_function_name(name);
3084
3085 // With Vim9 script the name was made script-local, if not
3086 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02003087 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003088 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003089 }
3090
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003091 if (fp != NULL)
3092 {
3093 list_func_head(fp, TRUE);
3094 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
3095 {
3096 if (FUNCLINE(fp, j) == NULL)
3097 continue;
3098 msg_putchar('\n');
3099 msg_outnum((long)(j + 1));
3100 if (j < 9)
3101 msg_putchar(' ');
3102 if (j < 99)
3103 msg_putchar(' ');
3104 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003105 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003106 ui_breakcheck();
3107 }
3108 if (!got_int)
3109 {
3110 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003111 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 msg_puts(" enddef");
3113 else
3114 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003115 }
3116 }
3117 else
Bram Moolenaara26b9702020-04-18 19:53:28 +02003118 emsg_funcname(N_("E123: Undefined function: %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003119 }
3120 goto ret_free;
3121 }
3122
3123 /*
3124 * ":function name(arg1, arg2)" Define function.
3125 */
3126 p = skipwhite(p);
3127 if (*p != '(')
3128 {
3129 if (!eap->skip)
3130 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003131 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003132 goto ret_free;
3133 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003134 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003135 if (vim_strchr(p, '(') != NULL)
3136 p = vim_strchr(p, '(');
3137 }
3138 p = skipwhite(p + 1);
3139
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02003140 // In Vim9 script only global functions can be redefined.
3141 if (vim9script && eap->forceit && !is_global)
3142 {
3143 emsg(_(e_nobang));
3144 goto ret_free;
3145 }
3146
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003147 ga_init2(&newlines, (int)sizeof(char_u *), 3);
3148
Bram Moolenaar04b12692020-05-04 23:24:44 +02003149 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003150 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003151 // Check the name of the function. Unless it's a dictionary function
3152 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003153 if (name != NULL)
3154 arg = name;
3155 else
3156 arg = fudi.fd_newkey;
3157 if (arg != NULL && (fudi.fd_di == NULL
3158 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
3159 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
3160 {
3161 if (*arg == K_SPECIAL)
3162 j = 3;
3163 else
3164 j = 0;
3165 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
3166 : eval_isnamec(arg[j])))
3167 ++j;
3168 if (arg[j] != NUL)
3169 emsg_funcname((char *)e_invarg2, arg);
3170 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003171 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003172 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003173 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003174 }
3175
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003176 // This may get more lines and make the pointers into the first line
3177 // invalid.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003179 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003180 &varargs, &default_args, eap->skip,
3181 eap, &line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003182 goto errret_2;
3183
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003185 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186 // find the return type: :def Func(): type
3187 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003188 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02003190 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003192 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003193 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003195 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003197 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003198 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02003199 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003200 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003201 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02003202 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003203 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 else
3205 // find extra arguments "range", "dict", "abort" and "closure"
3206 for (;;)
3207 {
3208 p = skipwhite(p);
3209 if (STRNCMP(p, "range", 5) == 0)
3210 {
3211 flags |= FC_RANGE;
3212 p += 5;
3213 }
3214 else if (STRNCMP(p, "dict", 4) == 0)
3215 {
3216 flags |= FC_DICT;
3217 p += 4;
3218 }
3219 else if (STRNCMP(p, "abort", 5) == 0)
3220 {
3221 flags |= FC_ABORT;
3222 p += 5;
3223 }
3224 else if (STRNCMP(p, "closure", 7) == 0)
3225 {
3226 flags |= FC_CLOSURE;
3227 p += 7;
3228 if (current_funccal == NULL)
3229 {
3230 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
3231 name == NULL ? (char_u *)"" : name);
3232 goto erret;
3233 }
3234 }
3235 else
3236 break;
3237 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003238
Bram Moolenaare38eab22019-12-05 21:50:01 +01003239 // When there is a line break use what follows for the function body.
3240 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003241 if (*p == '\n')
3242 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003243 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02003244 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
3245 && eap->cmdidx != CMD_def)
Bram Moolenaare7e48382020-07-22 18:17:08 +02003246 && !(*p == '#' && (vim9script || eap->cmdidx == CMD_def))
3247 && !eap->skip
3248 && !did_emsg)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003249 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003250
3251 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
3253 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003254 */
3255 if (KeyTyped)
3256 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003257 // Check if the function already exists, don't let the user type the
3258 // whole function before telling him it doesn't work! For a script we
3259 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003260 if (!eap->skip && !eap->forceit)
3261 {
3262 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003263 emsg(_(e_funcdict));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003264 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003265 emsg_funcname(e_funcexts, name);
3266 }
3267
3268 if (!eap->skip && did_emsg)
3269 goto erret;
3270
Bram Moolenaare38eab22019-12-05 21:50:01 +01003271 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003272 cmdline_row = msg_row;
3273 }
3274
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003275 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003276 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003277
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02003278 // Detect having skipped over comment lines to find the return
3279 // type. Add NULL lines to keep the line count correct.
3280 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
3281 if (SOURCING_LNUM < sourcing_lnum_off)
3282 {
3283 sourcing_lnum_off -= SOURCING_LNUM;
3284 if (ga_grow(&newlines, sourcing_lnum_off) == FAIL)
3285 goto erret;
3286 while (sourcing_lnum_off-- > 0)
3287 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3288 }
3289
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003290 indent = 2;
3291 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003293 for (;;)
3294 {
3295 if (KeyTyped)
3296 {
3297 msg_scroll = TRUE;
3298 saved_wait_return = FALSE;
3299 }
3300 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003301
3302 if (line_arg != NULL)
3303 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003304 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003305 theline = line_arg;
3306 p = vim_strchr(theline, '\n');
3307 if (p == NULL)
3308 line_arg += STRLEN(line_arg);
3309 else
3310 {
3311 *p = NUL;
3312 line_arg = p + 1;
3313 }
3314 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003315 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02003316 {
3317 vim_free(line_to_free);
3318 if (eap->getline == NULL)
Bram Moolenaar66250c92020-08-20 15:02:42 +02003319 theline = getcmdline(':', 0L, indent, getline_options);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003320 else
Bram Moolenaar66250c92020-08-20 15:02:42 +02003321 theline = eap->getline(':', eap->cookie, indent,
3322 getline_options);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003323 line_to_free = theline;
3324 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003325 if (KeyTyped)
3326 lines_left = Rows - 1;
3327 if (theline == NULL)
3328 {
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003329 if (skip_until != NULL)
3330 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
3331 else if (eap->cmdidx == CMD_def)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003332 emsg(_(e_missing_enddef));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 else
3334 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003335 goto erret;
3336 }
3337
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003338 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003339 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003340 if (SOURCING_LNUM < sourcing_lnum_off)
3341 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003342 else
3343 sourcing_lnum_off = 0;
3344
3345 if (skip_until != NULL)
3346 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003348 // * ":append" and "."
3349 // * ":python <<EOF" and "EOF"
3350 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
3351 if (heredoc_trimmed == NULL
3352 || (is_heredoc && skipwhite(theline) == theline)
3353 || STRNCMP(theline, heredoc_trimmed,
3354 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003355 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003356 if (heredoc_trimmed == NULL)
3357 p = theline;
3358 else if (is_heredoc)
3359 p = skipwhite(theline) == theline
3360 ? theline : theline + STRLEN(heredoc_trimmed);
3361 else
3362 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02003363 if (STRCMP(p, skip_until) == 0)
3364 {
3365 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003366 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003367 getline_options = GETLINE_CONCAT_CONT;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003368 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003369 }
3370 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003371 }
3372 else
3373 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003374 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01003375 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003376 ;
3377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378 // Check for "endfunction" or "enddef".
3379 if (checkforcmd(&p, nesting_def[nesting]
3380 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003381 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02003382 char_u *nextcmd = NULL;
3383
Bram Moolenaar663bb232017-06-22 19:12:10 +02003384 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02003385 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02003386 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02003387 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02003388 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 give_warning2(eap->cmdidx == CMD_def
3390 ? (char_u *)_("W1001: Text found after :enddef: %s")
3391 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003392 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003393 if (nextcmd != NULL)
3394 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003395 // Another command follows. If the line came from "eap" we
3396 // can simply point into it, otherwise we need to change
3397 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02003398 eap->nextcmd = nextcmd;
3399 if (line_to_free != NULL)
3400 {
3401 vim_free(*eap->cmdlinep);
3402 *eap->cmdlinep = line_to_free;
3403 line_to_free = NULL;
3404 }
3405 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003406 break;
3407 }
3408
Bram Moolenaare38eab22019-12-05 21:50:01 +01003409 // Increase indent inside "if", "while", "for" and "try", decrease
3410 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003412 indent -= 2;
3413 else if (STRNCMP(p, "if", 2) == 0
3414 || STRNCMP(p, "wh", 2) == 0
3415 || STRNCMP(p, "for", 3) == 0
3416 || STRNCMP(p, "try", 3) == 0)
3417 indent += 2;
3418
Bram Moolenaare38eab22019-12-05 21:50:01 +01003419 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01003420 // Only recognize "def" inside "def", not inside "function",
3421 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 c = *p;
Bram Moolenaar673660a2020-01-26 16:50:05 +01003423 if (checkforcmd(&p, "function", 2)
3424 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003425 {
3426 if (*p == '!')
3427 p = skipwhite(p + 1);
3428 p += eval_fname_script(p);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003429 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL, NULL));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003430 if (*skipwhite(p) == '(')
3431 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 if (nesting == MAX_FUNC_NESTING - 1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003433 emsg(_(e_function_nesting_too_deep));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 else
3435 {
3436 ++nesting;
3437 nesting_def[nesting] = (c == 'd');
3438 indent += 2;
3439 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003440 }
3441 }
3442
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003443 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003444 p = skip_range(p, FALSE, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003445 if (eap->cmdidx != CMD_def
3446 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01003447 || (p[0] == 'c'
3448 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
3449 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
3450 && (STRNCMP(&p[3], "nge", 3) != 0
3451 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003452 || (p[0] == 'i'
3453 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003454 && (!ASCII_ISALPHA(p[2])
3455 || (p[2] == 's'
3456 && (!ASCII_ISALPHA(p[3])
3457 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003458 skip_until = vim_strsave((char_u *)".");
3459
Bram Moolenaare38eab22019-12-05 21:50:01 +01003460 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003461 arg = skipwhite(skiptowhite(p));
3462 if (arg[0] == '<' && arg[1] =='<'
3463 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003464 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
3465 || ((p[2] == '3' || p[2] == 'x')
3466 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003467 || (p[0] == 'p' && p[1] == 'e'
3468 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
3469 || (p[0] == 't' && p[1] == 'c'
3470 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
3471 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
3472 && !ASCII_ISALPHA(p[3]))
3473 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
3474 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
3475 || (p[0] == 'm' && p[1] == 'z'
3476 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
3477 ))
3478 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003479 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003480 p = skipwhite(arg + 2);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003481 if (STRNCMP(p, "trim", 4) == 0)
3482 {
3483 // Ignore leading white space.
3484 p = skipwhite(p + 4);
3485 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003486 skipwhite(theline) - theline);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003487 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003488 if (*p == NUL)
3489 skip_until = vim_strsave((char_u *)".");
3490 else
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003491 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003492 getline_options = GETLINE_NONE;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003493 is_heredoc = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003494 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003495
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003496 // Check for ":cmd v =<< [trim] EOF"
3497 // and ":cmd [a, b] =<< [trim] EOF"
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003498 // and "lines =<< [trim] EOF" for Vim9
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003499 // Where "cmd" can be "let", "var", "final" or "const".
Bram Moolenaar8471e572019-05-19 21:37:18 +02003500 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003501 if (*arg == '[')
3502 arg = vim_strchr(arg, ']');
3503 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003504 {
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003505 int found = (eap->cmdidx == CMD_def && arg[0] == '='
3506 && arg[1] == '<' && arg[2] =='<');
3507
3508 if (!found)
3509 // skip over the argument after "cmd"
3510 arg = skipwhite(skiptowhite(arg));
3511 if (found || (arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003512 && (checkforcmd(&p, "let", 2)
3513 || checkforcmd(&p, "var", 3)
3514 || checkforcmd(&p, "final", 5)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003515 || checkforcmd(&p, "const", 5))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02003516 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003517 p = skipwhite(arg + 3);
3518 if (STRNCMP(p, "trim", 4) == 0)
3519 {
3520 // Ignore leading white space.
3521 p = skipwhite(p + 4);
3522 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003523 skipwhite(theline) - theline);
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003524 }
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003525 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003526 getline_options = GETLINE_NONE;
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003527 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003528 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003529 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003530 }
3531
Bram Moolenaare38eab22019-12-05 21:50:01 +01003532 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003533 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003534 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003535
Bram Moolenaare38eab22019-12-05 21:50:01 +01003536 // Copy the line to newly allocated memory. get_one_sourceline()
3537 // allocates 250 bytes per line, this saves 80% on average. The cost
3538 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003539 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003540 if (p == NULL)
3541 goto erret;
3542 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003543
Bram Moolenaare38eab22019-12-05 21:50:01 +01003544 // Add NULL lines for continuation lines, so that the line count is
3545 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003546 while (sourcing_lnum_off-- > 0)
3547 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3548
Bram Moolenaare38eab22019-12-05 21:50:01 +01003549 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003550 if (line_arg != NULL && *line_arg == NUL)
3551 line_arg = NULL;
3552 }
3553
Bram Moolenaare38eab22019-12-05 21:50:01 +01003554 // Don't define the function when skipping commands or when an error was
3555 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003556 if (eap->skip || did_emsg)
3557 goto erret;
3558
3559 /*
3560 * If there are no errors, add the function
3561 */
3562 if (fudi.fd_dict == NULL)
3563 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 hashtab_T *ht;
3565
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003566 v = find_var(name, &ht, FALSE);
3567 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
3568 {
3569 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
3570 name);
3571 goto erret;
3572 }
3573
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003574 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaareef21022020-08-01 22:16:43 +02003575 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003576 {
Bram Moolenaareef21022020-08-01 22:16:43 +02003577 char_u *uname = untrans_function_name(name);
3578
3579 import = find_imported(uname == NULL ? name : uname, 0, NULL);
3580 }
3581
3582 if (fp != NULL || import != NULL)
3583 {
3584 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003586 // Function can be replaced with "function!" and when sourcing the
3587 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02003588 // A name that is used by an import can not be overruled.
3589 if (import != NULL
3590 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003591 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02003592 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003593 {
Bram Moolenaareef21022020-08-01 22:16:43 +02003594 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003595 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02003596 else
3597 emsg_funcname(e_funcexts, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003598 goto erret;
3599 }
3600 if (fp->uf_calls > 0)
3601 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003602 emsg_funcname(
3603 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003604 name);
3605 goto erret;
3606 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003607 if (fp->uf_refcount > 1)
3608 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003609 // This function is referenced somewhere, don't redefine it but
3610 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003611 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003612 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003613 fp = NULL;
3614 overwrite = TRUE;
3615 }
3616 else
3617 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003618 char_u *exp_name = fp->uf_name_exp;
3619
3620 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01003621 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003622 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003623 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003624 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003626#ifdef FEAT_PROFILE
3627 fp->uf_profiling = FALSE;
3628 fp->uf_prof_initialized = FALSE;
3629#endif
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003630 unlink_def_function(fp);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003631 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003632 }
3633 }
3634 else
3635 {
3636 char numbuf[20];
3637
3638 fp = NULL;
3639 if (fudi.fd_newkey == NULL && !eap->forceit)
3640 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003641 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003642 goto erret;
3643 }
3644 if (fudi.fd_di == NULL)
3645 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003646 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02003647 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003648 goto erret;
3649 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003650 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02003651 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003652 goto erret;
3653
Bram Moolenaare38eab22019-12-05 21:50:01 +01003654 // Give the function a sequential number. Can only be used with a
3655 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003656 vim_free(name);
3657 sprintf(numbuf, "%d", ++func_nr);
3658 name = vim_strsave((char_u *)numbuf);
3659 if (name == NULL)
3660 goto erret;
3661 }
3662
3663 if (fp == NULL)
3664 {
3665 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3666 {
3667 int slen, plen;
3668 char_u *scriptname;
3669
Bram Moolenaare38eab22019-12-05 21:50:01 +01003670 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003671 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003672 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003673 {
3674 scriptname = autoload_name(name);
3675 if (scriptname != NULL)
3676 {
3677 p = vim_strchr(scriptname, '/');
3678 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003679 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003680 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003681 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003682 j = OK;
3683 vim_free(scriptname);
3684 }
3685 }
3686 if (j == FAIL)
3687 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003688 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003689 goto erret;
3690 }
3691 }
3692
Bram Moolenaar47ed5532019-08-08 20:49:14 +02003693 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003694 if (fp == NULL)
3695 goto erret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003696 fp->uf_def_status = eap->cmdidx == CMD_def ? UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003697 : UF_NOT_COMPILED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003698
3699 if (fudi.fd_dict != NULL)
3700 {
3701 if (fudi.fd_di == NULL)
3702 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003703 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003704 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
3705 if (fudi.fd_di == NULL)
3706 {
3707 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003708 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003709 goto erret;
3710 }
3711 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
3712 {
3713 vim_free(fudi.fd_di);
3714 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003715 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003716 goto erret;
3717 }
3718 }
3719 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003720 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003721 clear_tv(&fudi.fd_di->di_tv);
3722 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003723 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003724
Bram Moolenaare38eab22019-12-05 21:50:01 +01003725 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003726 flags |= FC_DICT;
3727 }
3728
Bram Moolenaare38eab22019-12-05 21:50:01 +01003729 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003730 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003731 if (overwrite)
3732 {
3733 hi = hash_find(&func_hashtab, name);
3734 hi->hi_key = UF2HIKEY(fp);
3735 }
3736 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003737 {
3738 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003739 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003740 goto erret;
3741 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003742 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003743 }
3744 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003745 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003747 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748
3749 if (eap->cmdidx == CMD_def)
3750 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003751 int lnum_save = SOURCING_LNUM;
3752 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003753
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003754 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003755
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003756 // error messages are for the first function line
3757 SOURCING_LNUM = sourcing_lnum_top;
3758
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003759 if (cstack != NULL && cstack->cs_idx >= 0)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003760 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003761 int count = cstack->cs_idx + 1;
3762 int i;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003763
3764 // The block context may be needed for script variables declared in
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003765 // a block that is visible now but not when the function is called
3766 // later.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003767 fp->uf_block_ids = ALLOC_MULT(int, count);
3768 if (fp->uf_block_ids != NULL)
3769 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003770 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003771 sizeof(int) * count);
3772 fp->uf_block_depth = count;
3773 }
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003774
3775 // Set flag in each block to indicate a function was defined. This
3776 // is used to keep the variable when leaving the block, see
3777 // hide_script_var().
3778 for (i = 0; i <= cstack->cs_idx; ++i)
3779 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003780 }
3781
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003782 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003784 SOURCING_LNUM = lnum_save;
3785 goto errret_2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003787 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788
3789 // parse the return type, if any
3790 if (ret_type == NULL)
3791 fp->uf_ret_type = &t_void;
3792 else
3793 {
3794 p = ret_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01003795 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02003797 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003799 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003800 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003802 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003803 if ((flags & FC_CLOSURE) != 0)
3804 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003805 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003806 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003807 }
3808 else
3809 fp->uf_scoped = NULL;
3810
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003811#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003812 if (prof_def_func())
3813 func_do_profile(fp);
3814#endif
3815 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003816 if (sandbox)
3817 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003818 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003819 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003820 fp->uf_flags = flags;
3821 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003822 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003823 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02003824 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003825 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826 if (is_export)
3827 {
3828 fp->uf_flags |= FC_EXPORT;
3829 // let ex_export() know the export worked.
3830 is_export = FALSE;
3831 }
3832
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003833 if (eap->cmdidx == CMD_def)
3834 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02003835 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
3836 // :func does not use Vim9 script syntax, even in a Vim9 script file
3837 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003838
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003839 goto ret_free;
3840
3841erret:
3842 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003843 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003844errret_2:
3845 ga_clear_strings(&newlines);
3846ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003847 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003848 vim_free(skip_until);
Bram Moolenaarfffdf472020-12-13 21:16:55 +01003849 vim_free(heredoc_trimmed);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003850 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003851 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003852 if (name != name_arg)
3853 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003854 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003855 did_emsg |= saved_did_emsg;
3856 need_wait_return |= saved_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003857
3858 return fp;
3859}
3860
3861/*
3862 * ":function"
3863 */
3864 void
3865ex_function(exarg_T *eap)
3866{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003867 (void)define_function(eap, NULL);
Bram Moolenaar822ba242020-05-24 23:00:18 +02003868}
3869
3870/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02003871 * :defcompile - compile all :def functions in the current script that need to
3872 * be compiled. Except dead functions.
Bram Moolenaar822ba242020-05-24 23:00:18 +02003873 */
3874 void
3875ex_defcompile(exarg_T *eap UNUSED)
3876{
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003877 long todo = (long)func_hashtab.ht_used;
3878 int changed = func_hashtab.ht_changed;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003879 hashitem_T *hi;
3880 ufunc_T *ufunc;
3881
3882 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
3883 {
3884 if (!HASHITEM_EMPTY(hi))
3885 {
3886 --todo;
3887 ufunc = HI2UF(hi);
3888 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
Bram Moolenaar96f8f492020-09-09 17:08:51 +02003889 && ufunc->uf_def_status == UF_TO_BE_COMPILED
3890 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003891 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02003892 compile_def_function(ufunc, FALSE, NULL);
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003893
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003894 if (func_hashtab.ht_changed != changed)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003895 {
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003896 // a function has been added or removed, need to start over
3897 todo = (long)func_hashtab.ht_used;
3898 changed = func_hashtab.ht_changed;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003899 hi = func_hashtab.ht_array;
Bram Moolenaar285b1892020-05-26 11:37:26 +02003900 --hi;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003901 }
3902 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003903 }
3904 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003905}
3906
3907/*
3908 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3909 * Return 2 if "p" starts with "s:".
3910 * Return 0 otherwise.
3911 */
3912 int
3913eval_fname_script(char_u *p)
3914{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003915 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3916 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003917 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3918 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3919 return 5;
3920 if (p[0] == 's' && p[1] == ':')
3921 return 2;
3922 return 0;
3923}
3924
3925 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003926translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003927{
3928 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003929 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003930 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931}
3932
3933/*
3934 * Return TRUE when "ufunc" has old-style "..." varargs
3935 * or named varargs "...name: type".
3936 */
3937 int
3938has_varargs(ufunc_T *ufunc)
3939{
3940 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003941}
3942
3943/*
3944 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003945 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003946 */
3947 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003948function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003949{
3950 char_u *nm = name;
3951 char_u *p;
3952 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003953 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003954 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003955
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003956 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3957 if (no_deref)
3958 flag |= TFN_NO_DEREF;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003959 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003960 nm = skipwhite(nm);
3961
Bram Moolenaare38eab22019-12-05 21:50:01 +01003962 // Only accept "funcname", "funcname ", "funcname (..." and
3963 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003964 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003965 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003966 vim_free(p);
3967 return n;
3968}
3969
Bram Moolenaar113e1072019-01-20 15:30:40 +01003970#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003971 char_u *
3972get_expanded_name(char_u *name, int check)
3973{
3974 char_u *nm = name;
3975 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003976 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003977
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003978 p = trans_function_name(&nm, &is_global, FALSE,
3979 TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003980
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003981 if (p != NULL && *nm == NUL
3982 && (!check || translated_function_exists(p, is_global)))
3983 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003984
3985 vim_free(p);
3986 return NULL;
3987}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003988#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003989
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003990/*
3991 * Function given to ExpandGeneric() to obtain the list of user defined
3992 * function names.
3993 */
3994 char_u *
3995get_user_func_name(expand_T *xp, int idx)
3996{
3997 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02003998 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003999 static hashitem_T *hi;
4000 ufunc_T *fp;
4001
4002 if (idx == 0)
4003 {
4004 done = 0;
4005 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004006 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004007 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004008 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004009 {
4010 if (done++ > 0)
4011 ++hi;
4012 while (HASHITEM_EMPTY(hi))
4013 ++hi;
4014 fp = HI2UF(hi);
4015
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 // don't show dead, dict and lambda functions
4017 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02004018 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004020
4021 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004022 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004023
4024 cat_func_name(IObuff, fp);
4025 if (xp->xp_context != EXPAND_USER_FUNC)
4026 {
4027 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004029 STRCAT(IObuff, ")");
4030 }
4031 return IObuff;
4032 }
4033 return NULL;
4034}
4035
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004036/*
4037 * ":delfunction {name}"
4038 */
4039 void
4040ex_delfunction(exarg_T *eap)
4041{
4042 ufunc_T *fp = NULL;
4043 char_u *p;
4044 char_u *name;
4045 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004046 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004047
4048 p = eap->arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004049 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004050 vim_free(fudi.fd_newkey);
4051 if (name == NULL)
4052 {
4053 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004054 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004055 return;
4056 }
4057 if (!ends_excmd(*skipwhite(p)))
4058 {
4059 vim_free(name);
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004060 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004061 return;
4062 }
4063 eap->nextcmd = check_nextcmd(p);
4064 if (eap->nextcmd != NULL)
4065 *p = NUL;
4066
4067 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004068 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004069 vim_free(name);
4070
4071 if (!eap->skip)
4072 {
4073 if (fp == NULL)
4074 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02004075 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004076 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004077 return;
4078 }
4079 if (fp->uf_calls > 0)
4080 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004081 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004082 return;
4083 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004084 if (fp->uf_flags & FC_VIM9)
4085 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004086 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004087 return;
4088 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004089
4090 if (fudi.fd_dict != NULL)
4091 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004092 // Delete the dict item that refers to the function, it will
4093 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004094 dictitem_remove(fudi.fd_dict, fudi.fd_di);
4095 }
4096 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004097 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004098 // A normal function (not a numbered function or lambda) has a
4099 // refcount of 1 for the entry in the hashtable. When deleting
4100 // it and the refcount is more than one, it should be kept.
4101 // A numbered function and lambda should be kept if the refcount is
4102 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004103 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004104 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004105 // Function is still referenced somewhere. Don't free it but
4106 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004107 if (func_remove(fp))
4108 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004109 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004110 }
4111 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004112 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004113 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004114 }
4115}
4116
4117/*
4118 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004119 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004120 */
4121 void
4122func_unref(char_u *name)
4123{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004124 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004125
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004126 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004127 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004128 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004129 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004130 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004131#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004132 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004133#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01004134 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004135 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004136 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004137 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004138 // Only delete it when it's not being used. Otherwise it's done
4139 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004140 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004141 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02004142 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004143}
4144
4145/*
4146 * Unreference a Function: decrement the reference count and free it when it
4147 * becomes zero.
4148 */
4149 void
4150func_ptr_unref(ufunc_T *fp)
4151{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004152 if (fp != NULL && --fp->uf_refcount <= 0)
4153 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004154 // Only delete it when it's not being used. Otherwise it's done
4155 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02004156 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004157 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004158 }
4159}
4160
4161/*
4162 * Count a reference to a Function.
4163 */
4164 void
4165func_ref(char_u *name)
4166{
4167 ufunc_T *fp;
4168
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004169 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004170 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004171 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004172 if (fp != NULL)
4173 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004174 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01004175 // Only give an error for a numbered function.
4176 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01004177 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004178}
4179
4180/*
4181 * Count a reference to a Function.
4182 */
4183 void
4184func_ptr_ref(ufunc_T *fp)
4185{
4186 if (fp != NULL)
4187 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004188}
4189
4190/*
4191 * Return TRUE if items in "fc" do not have "copyID". That means they are not
4192 * referenced from anywhere that is in use.
4193 */
4194 static int
4195can_free_funccal(funccall_T *fc, int copyID)
4196{
4197 return (fc->l_varlist.lv_copyID != copyID
4198 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004199 && fc->l_avars.dv_copyID != copyID
4200 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004201}
4202
4203/*
4204 * ":return [expr]"
4205 */
4206 void
4207ex_return(exarg_T *eap)
4208{
4209 char_u *arg = eap->arg;
4210 typval_T rettv;
4211 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004212 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004213
4214 if (current_funccal == NULL)
4215 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004216 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004217 return;
4218 }
4219
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004220 CLEAR_FIELD(evalarg);
4221 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
4222
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004223 if (eap->skip)
4224 ++emsg_skip;
4225
4226 eap->nextcmd = NULL;
4227 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004228 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004229 {
4230 if (!eap->skip)
4231 returning = do_return(eap, FALSE, TRUE, &rettv);
4232 else
4233 clear_tv(&rettv);
4234 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004235 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004236 else if (!eap->skip)
4237 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004238 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01004239 update_force_abort();
4240
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004241 /*
4242 * Return unless the expression evaluation has been cancelled due to an
4243 * aborting error, an interrupt, or an exception.
4244 */
4245 if (!aborting())
4246 returning = do_return(eap, FALSE, TRUE, NULL);
4247 }
4248
Bram Moolenaare38eab22019-12-05 21:50:01 +01004249 // When skipping or the return gets pending, advance to the next command
4250 // in this line (!returning). Otherwise, ignore the rest of the line.
4251 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004252 if (returning)
4253 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004254 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004255 eap->nextcmd = check_nextcmd(arg);
4256
4257 if (eap->skip)
4258 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02004259 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004260}
4261
4262/*
4263 * ":1,25call func(arg1, arg2)" function call.
4264 */
4265 void
4266ex_call(exarg_T *eap)
4267{
4268 char_u *arg = eap->arg;
4269 char_u *startarg;
4270 char_u *name;
4271 char_u *tofree;
4272 int len;
4273 typval_T rettv;
4274 linenr_T lnum;
4275 int doesrange;
4276 int failed = FALSE;
4277 funcdict_T fudi;
4278 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004279 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004280
Bram Moolenaare6b53242020-07-01 17:28:33 +02004281 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004282 if (eap->skip)
4283 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004284 // trans_function_name() doesn't work well when skipping, use eval0()
4285 // instead to skip to any following command, e.g. for:
4286 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004287 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004288 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004289 clear_tv(&rettv);
4290 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004291 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004292 return;
4293 }
4294
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004295 tofree = trans_function_name(&arg, NULL, eap->skip,
4296 TFN_INT, &fudi, &partial);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004297 if (fudi.fd_newkey != NULL)
4298 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004299 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004300 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004301 vim_free(fudi.fd_newkey);
4302 }
4303 if (tofree == NULL)
4304 return;
4305
Bram Moolenaare38eab22019-12-05 21:50:01 +01004306 // Increase refcount on dictionary, it could get deleted when evaluating
4307 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004308 if (fudi.fd_dict != NULL)
4309 ++fudi.fd_dict->dv_refcount;
4310
Bram Moolenaare38eab22019-12-05 21:50:01 +01004311 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
4312 // contents. For VAR_PARTIAL get its partial, unless we already have one
4313 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004314 len = (int)STRLEN(tofree);
4315 name = deref_func_name(tofree, &len,
4316 partial != NULL ? NULL : &partial, FALSE);
4317
Bram Moolenaare38eab22019-12-05 21:50:01 +01004318 // Skip white space to allow ":call func ()". Not good, but required for
4319 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004320 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004321 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004322
4323 if (*startarg != '(')
4324 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004326 goto end;
4327 }
4328
4329 /*
4330 * When skipping, evaluate the function once, to find the end of the
4331 * arguments.
4332 * When the function takes a range, this is discovered after the first
4333 * call, and the loop is broken.
4334 */
4335 if (eap->skip)
4336 {
4337 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004338 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004339 }
4340 else
4341 lnum = eap->line1;
4342 for ( ; lnum <= eap->line2; ++lnum)
4343 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004344 funcexe_T funcexe;
4345
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004346 if (!eap->skip && eap->addr_count > 0)
4347 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004348 if (lnum > curbuf->b_ml.ml_line_count)
4349 {
4350 // If the function deleted lines or switched to another buffer
4351 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004352 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004353 break;
4354 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004355 curwin->w_cursor.lnum = lnum;
4356 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004357 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004358 }
4359 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004360
Bram Moolenaara80faa82020-04-12 19:37:17 +02004361 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004362 funcexe.firstline = eap->line1;
4363 funcexe.lastline = eap->line2;
4364 funcexe.doesrange = &doesrange;
4365 funcexe.evaluate = !eap->skip;
4366 funcexe.partial = partial;
4367 funcexe.selfdict = fudi.fd_dict;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004368 if (get_func_tv(name, -1, &rettv, &arg, &evalarg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004369 {
4370 failed = TRUE;
4371 break;
4372 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01004373 if (has_watchexpr())
4374 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004375
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004376 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004377 if (handle_subscript(&arg, &rettv,
4378 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004379 {
4380 failed = TRUE;
4381 break;
4382 }
4383
4384 clear_tv(&rettv);
4385 if (doesrange || eap->skip)
4386 break;
4387
Bram Moolenaare38eab22019-12-05 21:50:01 +01004388 // Stop when immediately aborting on error, or when an interrupt
4389 // occurred or an exception was thrown but not caught.
4390 // get_func_tv() returned OK, so that the check for trailing
4391 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004392 if (aborting())
4393 break;
4394 }
4395 if (eap->skip)
4396 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004397 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004398
Bram Moolenaare51bb172020-02-16 19:42:23 +01004399 // When inside :try we need to check for following "| catch".
4400 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004401 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004402 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02004403 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004404 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004405 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004406 if (!failed)
4407 {
4408 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004409 semsg(_(e_trailing_arg), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004410 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004411 }
4412 else
4413 eap->nextcmd = check_nextcmd(arg);
4414 }
4415
4416end:
4417 dict_unref(fudi.fd_dict);
4418 vim_free(tofree);
4419}
4420
4421/*
4422 * Return from a function. Possibly makes the return pending. Also called
4423 * for a pending return at the ":endtry" or after returning from an extra
4424 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
4425 * when called due to a ":return" command. "rettv" may point to a typval_T
4426 * with the return rettv. Returns TRUE when the return can be carried out,
4427 * FALSE when the return gets pending.
4428 */
4429 int
4430do_return(
4431 exarg_T *eap,
4432 int reanimate,
4433 int is_cmd,
4434 void *rettv)
4435{
4436 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01004437 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004438
4439 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004440 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004441 current_funccal->returned = FALSE;
4442
4443 /*
4444 * Cleanup (and inactivate) conditionals, but stop when a try conditional
4445 * not in its finally clause (which then is to be executed next) is found.
4446 * In this case, make the ":return" pending for execution at the ":endtry".
4447 * Otherwise, return normally.
4448 */
4449 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
4450 if (idx >= 0)
4451 {
4452 cstack->cs_pending[idx] = CSTP_RETURN;
4453
4454 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004455 // A pending return again gets pending. "rettv" points to an
4456 // allocated variable with the rettv of the original ":return"'s
4457 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004458 cstack->cs_rettv[idx] = rettv;
4459 else
4460 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004461 // When undoing a return in order to make it pending, get the stored
4462 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004463 if (reanimate)
4464 rettv = current_funccal->rettv;
4465
4466 if (rettv != NULL)
4467 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004468 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004469 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
4470 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
4471 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004472 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004473 }
4474 else
4475 cstack->cs_rettv[idx] = NULL;
4476
4477 if (reanimate)
4478 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004479 // The pending return value could be overwritten by a ":return"
4480 // without argument in a finally clause; reset the default
4481 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004482 current_funccal->rettv->v_type = VAR_NUMBER;
4483 current_funccal->rettv->vval.v_number = 0;
4484 }
4485 }
4486 report_make_pending(CSTP_RETURN, rettv);
4487 }
4488 else
4489 {
4490 current_funccal->returned = TRUE;
4491
Bram Moolenaare38eab22019-12-05 21:50:01 +01004492 // If the return is carried out now, store the return value. For
4493 // a return immediately after reanimation, the value is already
4494 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004495 if (!reanimate && rettv != NULL)
4496 {
4497 clear_tv(current_funccal->rettv);
4498 *current_funccal->rettv = *(typval_T *)rettv;
4499 if (!is_cmd)
4500 vim_free(rettv);
4501 }
4502 }
4503
4504 return idx < 0;
4505}
4506
4507/*
4508 * Free the variable with a pending return value.
4509 */
4510 void
4511discard_pending_return(void *rettv)
4512{
4513 free_tv((typval_T *)rettv);
4514}
4515
4516/*
4517 * Generate a return command for producing the value of "rettv". The result
4518 * is an allocated string. Used by report_pending() for verbose messages.
4519 */
4520 char_u *
4521get_return_cmd(void *rettv)
4522{
4523 char_u *s = NULL;
4524 char_u *tofree = NULL;
4525 char_u numbuf[NUMBUFLEN];
4526
4527 if (rettv != NULL)
4528 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
4529 if (s == NULL)
4530 s = (char_u *)"";
4531
4532 STRCPY(IObuff, ":return ");
4533 STRNCPY(IObuff + 8, s, IOSIZE - 8);
4534 if (STRLEN(s) + 8 >= IOSIZE)
4535 STRCPY(IObuff + IOSIZE - 4, "...");
4536 vim_free(tofree);
4537 return vim_strsave(IObuff);
4538}
4539
4540/*
4541 * Get next function line.
4542 * Called by do_cmdline() to get the next line.
4543 * Returns allocated string, or NULL for end of function.
4544 */
4545 char_u *
4546get_func_line(
4547 int c UNUSED,
4548 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02004549 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004550 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004551{
4552 funccall_T *fcp = (funccall_T *)cookie;
4553 ufunc_T *fp = fcp->func;
4554 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004555 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004556
Bram Moolenaare38eab22019-12-05 21:50:01 +01004557 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004558 if (fcp->dbg_tick != debug_tick)
4559 {
4560 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004561 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004562 fcp->dbg_tick = debug_tick;
4563 }
4564#ifdef FEAT_PROFILE
4565 if (do_profiling == PROF_YES)
4566 func_line_end(cookie);
4567#endif
4568
4569 gap = &fp->uf_lines;
4570 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4571 || fcp->returned)
4572 retval = NULL;
4573 else
4574 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004575 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004576 while (fcp->linenr < gap->ga_len
4577 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
4578 ++fcp->linenr;
4579 if (fcp->linenr >= gap->ga_len)
4580 retval = NULL;
4581 else
4582 {
4583 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004584 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004585#ifdef FEAT_PROFILE
4586 if (do_profiling == PROF_YES)
4587 func_line_start(cookie);
4588#endif
4589 }
4590 }
4591
Bram Moolenaare38eab22019-12-05 21:50:01 +01004592 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004593 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004594 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004595 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004596 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004597 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004598 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004599 fcp->dbg_tick = debug_tick;
4600 }
4601
4602 return retval;
4603}
4604
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004605/*
4606 * Return TRUE if the currently active function should be ended, because a
4607 * return was encountered or an error occurred. Used inside a ":while".
4608 */
4609 int
4610func_has_ended(void *cookie)
4611{
4612 funccall_T *fcp = (funccall_T *)cookie;
4613
Bram Moolenaare38eab22019-12-05 21:50:01 +01004614 // Ignore the "abort" flag if the abortion behavior has been changed due to
4615 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004616 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4617 || fcp->returned);
4618}
4619
4620/*
4621 * return TRUE if cookie indicates a function which "abort"s on errors.
4622 */
4623 int
4624func_has_abort(
4625 void *cookie)
4626{
4627 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
4628}
4629
4630
4631/*
4632 * Turn "dict.Func" into a partial for "Func" bound to "dict".
4633 * Don't do this when "Func" is already a partial that was bound
4634 * explicitly (pt_auto is FALSE).
4635 * Changes "rettv" in-place.
4636 * Returns the updated "selfdict_in".
4637 */
4638 dict_T *
4639make_partial(dict_T *selfdict_in, typval_T *rettv)
4640{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004641 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004642 char_u *tofree = NULL;
4643 ufunc_T *fp;
4644 char_u fname_buf[FLEN_FIXED + 1];
4645 int error;
4646 dict_T *selfdict = selfdict_in;
4647
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004648 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
4649 fp = rettv->vval.v_partial->pt_func;
4650 else
4651 {
4652 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
4653 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004654 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004655 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004656 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004657 vim_free(tofree);
4658 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004659
4660 if (fp != NULL && (fp->uf_flags & FC_DICT))
4661 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004662 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004663
4664 if (pt != NULL)
4665 {
4666 pt->pt_refcount = 1;
4667 pt->pt_dict = selfdict;
4668 pt->pt_auto = TRUE;
4669 selfdict = NULL;
4670 if (rettv->v_type == VAR_FUNC)
4671 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004672 // Just a function: Take over the function name and use
4673 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004674 pt->pt_name = rettv->vval.v_string;
4675 }
4676 else
4677 {
4678 partial_T *ret_pt = rettv->vval.v_partial;
4679 int i;
4680
Bram Moolenaare38eab22019-12-05 21:50:01 +01004681 // Partial: copy the function name, use selfdict and copy
4682 // args. Can't take over name or args, the partial might
4683 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004684 if (ret_pt->pt_name != NULL)
4685 {
4686 pt->pt_name = vim_strsave(ret_pt->pt_name);
4687 func_ref(pt->pt_name);
4688 }
4689 else
4690 {
4691 pt->pt_func = ret_pt->pt_func;
4692 func_ptr_ref(pt->pt_func);
4693 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004694 if (ret_pt->pt_argc > 0)
4695 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004696 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004697 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004698 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004699 pt->pt_argc = 0;
4700 else
4701 {
4702 pt->pt_argc = ret_pt->pt_argc;
4703 for (i = 0; i < pt->pt_argc; i++)
4704 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
4705 }
4706 }
4707 partial_unref(ret_pt);
4708 }
4709 rettv->v_type = VAR_PARTIAL;
4710 rettv->vval.v_partial = pt;
4711 }
4712 }
4713 return selfdict;
4714}
4715
4716/*
4717 * Return the name of the executed function.
4718 */
4719 char_u *
4720func_name(void *cookie)
4721{
4722 return ((funccall_T *)cookie)->func->uf_name;
4723}
4724
4725/*
4726 * Return the address holding the next breakpoint line for a funccall cookie.
4727 */
4728 linenr_T *
4729func_breakpoint(void *cookie)
4730{
4731 return &((funccall_T *)cookie)->breakpoint;
4732}
4733
4734/*
4735 * Return the address holding the debug tick for a funccall cookie.
4736 */
4737 int *
4738func_dbg_tick(void *cookie)
4739{
4740 return &((funccall_T *)cookie)->dbg_tick;
4741}
4742
4743/*
4744 * Return the nesting level for a funccall cookie.
4745 */
4746 int
4747func_level(void *cookie)
4748{
4749 return ((funccall_T *)cookie)->level;
4750}
4751
4752/*
4753 * Return TRUE when a function was ended by a ":return" command.
4754 */
4755 int
4756current_func_returned(void)
4757{
4758 return current_funccal->returned;
4759}
4760
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004761 int
4762free_unref_funccal(int copyID, int testing)
4763{
4764 int did_free = FALSE;
4765 int did_free_funccal = FALSE;
4766 funccall_T *fc, **pfc;
4767
4768 for (pfc = &previous_funccal; *pfc != NULL; )
4769 {
4770 if (can_free_funccal(*pfc, copyID))
4771 {
4772 fc = *pfc;
4773 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01004774 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004775 did_free = TRUE;
4776 did_free_funccal = TRUE;
4777 }
4778 else
4779 pfc = &(*pfc)->caller;
4780 }
4781 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004782 // When a funccal was freed some more items might be garbage
4783 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004784 (void)garbage_collect(testing);
4785
4786 return did_free;
4787}
4788
4789/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004790 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004791 */
4792 static funccall_T *
4793get_funccal(void)
4794{
4795 int i;
4796 funccall_T *funccal;
4797 funccall_T *temp_funccal;
4798
4799 funccal = current_funccal;
4800 if (debug_backtrace_level > 0)
4801 {
4802 for (i = 0; i < debug_backtrace_level; i++)
4803 {
4804 temp_funccal = funccal->caller;
4805 if (temp_funccal)
4806 funccal = temp_funccal;
4807 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004808 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004809 debug_backtrace_level = i;
4810 }
4811 }
4812 return funccal;
4813}
4814
4815/*
4816 * Return the hashtable used for local variables in the current funccal.
4817 * Return NULL if there is no current funccal.
4818 */
4819 hashtab_T *
4820get_funccal_local_ht()
4821{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004823 return NULL;
4824 return &get_funccal()->l_vars.dv_hashtab;
4825}
4826
4827/*
4828 * Return the l: scope variable.
4829 * Return NULL if there is no current funccal.
4830 */
4831 dictitem_T *
4832get_funccal_local_var()
4833{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004834 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004835 return NULL;
4836 return &get_funccal()->l_vars_var;
4837}
4838
4839/*
4840 * Return the hashtable used for argument in the current funccal.
4841 * Return NULL if there is no current funccal.
4842 */
4843 hashtab_T *
4844get_funccal_args_ht()
4845{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004847 return NULL;
4848 return &get_funccal()->l_avars.dv_hashtab;
4849}
4850
4851/*
4852 * Return the a: scope variable.
4853 * Return NULL if there is no current funccal.
4854 */
4855 dictitem_T *
4856get_funccal_args_var()
4857{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004858 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004859 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004860 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004861}
4862
4863/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004864 * List function variables, if there is a function.
4865 */
4866 void
4867list_func_vars(int *first)
4868{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004869 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004870 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004871 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004872}
4873
4874/*
4875 * If "ht" is the hashtable for local variables in the current funccal, return
4876 * the dict that contains it.
4877 * Otherwise return NULL.
4878 */
4879 dict_T *
4880get_current_funccal_dict(hashtab_T *ht)
4881{
4882 if (current_funccal != NULL
4883 && ht == &current_funccal->l_vars.dv_hashtab)
4884 return &current_funccal->l_vars;
4885 return NULL;
4886}
4887
4888/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004889 * Search hashitem in parent scope.
4890 */
4891 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004892find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004893{
4894 funccall_T *old_current_funccal = current_funccal;
4895 hashtab_T *ht;
4896 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004897 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004898
4899 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4900 return NULL;
4901
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004902 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004903 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004904 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004905 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004906 ht = find_var_ht(name, &varname);
4907 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004908 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004909 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004910 if (!HASHITEM_EMPTY(hi))
4911 {
4912 *pht = ht;
4913 break;
4914 }
4915 }
4916 if (current_funccal == current_funccal->func->uf_scoped)
4917 break;
4918 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004919 }
4920 current_funccal = old_current_funccal;
4921
4922 return hi;
4923}
4924
4925/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004926 * Search variable in parent scope.
4927 */
4928 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004929find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004930{
4931 dictitem_T *v = NULL;
4932 funccall_T *old_current_funccal = current_funccal;
4933 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004934 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004935
4936 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4937 return NULL;
4938
Bram Moolenaare38eab22019-12-05 21:50:01 +01004939 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004940 current_funccal = current_funccal->func->uf_scoped;
4941 while (current_funccal)
4942 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004943 ht = find_var_ht(name, &varname);
4944 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004945 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004946 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004947 if (v != NULL)
4948 break;
4949 }
4950 if (current_funccal == current_funccal->func->uf_scoped)
4951 break;
4952 current_funccal = current_funccal->func->uf_scoped;
4953 }
4954 current_funccal = old_current_funccal;
4955
4956 return v;
4957}
4958
4959/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004960 * Set "copyID + 1" in previous_funccal and callers.
4961 */
4962 int
4963set_ref_in_previous_funccal(int copyID)
4964{
4965 int abort = FALSE;
4966 funccall_T *fc;
4967
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004968 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004969 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004970 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004971 abort = abort
4972 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4973 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004974 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004975 }
4976 return abort;
4977}
4978
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004979 static int
4980set_ref_in_funccal(funccall_T *fc, int copyID)
4981{
4982 int abort = FALSE;
4983
4984 if (fc->fc_copyID != copyID)
4985 {
4986 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004987 abort = abort
4988 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
4989 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004990 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004991 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004992 }
4993 return abort;
4994}
4995
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004996/*
4997 * Set "copyID" in all local vars and arguments in the call stack.
4998 */
4999 int
5000set_ref_in_call_stack(int copyID)
5001{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005002 int abort = FALSE;
5003 funccall_T *fc;
5004 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005005
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005006 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005007 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005008
5009 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005010 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
5011 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005012 abort = abort || set_ref_in_funccal(fc, copyID);
5013
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005014 return abort;
5015}
5016
5017/*
5018 * Set "copyID" in all functions available by name.
5019 */
5020 int
5021set_ref_in_functions(int copyID)
5022{
5023 int todo;
5024 hashitem_T *hi = NULL;
5025 int abort = FALSE;
5026 ufunc_T *fp;
5027
5028 todo = (int)func_hashtab.ht_used;
5029 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005030 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005031 if (!HASHITEM_EMPTY(hi))
5032 {
5033 --todo;
5034 fp = HI2UF(hi);
5035 if (!func_name_refcount(fp->uf_name))
5036 abort = abort || set_ref_in_func(NULL, fp, copyID);
5037 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005038 }
5039 return abort;
5040}
5041
5042/*
5043 * Set "copyID" in all function arguments.
5044 */
5045 int
5046set_ref_in_func_args(int copyID)
5047{
5048 int i;
5049 int abort = FALSE;
5050
5051 for (i = 0; i < funcargs.ga_len; ++i)
5052 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
5053 copyID, NULL, NULL);
5054 return abort;
5055}
5056
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005057/*
5058 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005059 * Returns TRUE if setting references failed somehow.
5060 */
5061 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005062set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005063{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005064 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005065 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01005066 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005067 char_u fname_buf[FLEN_FIXED + 1];
5068 char_u *tofree = NULL;
5069 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005070 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005071
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005072 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005073 return FALSE;
5074
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005075 if (fp_in == NULL)
5076 {
5077 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005078 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005079 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005080 if (fp != NULL)
5081 {
5082 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005083 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005084 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02005085
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005086 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005087 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005088}
5089
Bram Moolenaare38eab22019-12-05 21:50:01 +01005090#endif // FEAT_EVAL