blob: 9393b62cc1210aba0740b33bf1864d0daa32ca11 [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 Moolenaare462f522020-12-27 14:43:30 +0100573 equal_arrow || in_vim9script() ? &white_error : NULL)) == NULL)
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100574 {
575 if (types_optional)
576 ga_clear_strings(&argtypes);
Bram Moolenaare5730bd2020-12-25 22:30:16 +0100577 ga_clear_strings(&newargs);
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100578 return white_error ? FAIL : NOTDONE;
Bram Moolenaar6dd41b12020-12-24 16:06:00 +0100579 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +0100580 *arg = s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200581
Bram Moolenaare38eab22019-12-05 21:50:01 +0100582 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200583 if (evaluate)
584 eval_lavars_used = &eval_lavars;
585
Bram Moolenaar65c44152020-12-24 15:14:01 +0100586 *arg = skipwhite_and_linebreak(*arg, evalarg);
587
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100588 // Recognize "{" as the start of a function body.
589 if (equal_arrow && **arg == '{')
Bram Moolenaar65c44152020-12-24 15:14:01 +0100590 {
591 // TODO: process the function body upto the "}".
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100592 // Return type is required then.
Bram Moolenaar65c44152020-12-24 15:14:01 +0100593 emsg("Lambda function body not supported yet");
594 goto errret;
595 }
596
Bram Moolenaare38eab22019-12-05 21:50:01 +0100597 // Get the start and the end of the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200598 start = *arg;
599 ret = skip_expr_concatenate(arg, &start, &end, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200600 if (ret == FAIL)
601 goto errret;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200602 if (evalarg != NULL)
603 {
604 // avoid that the expression gets freed when another line break follows
605 tofree = evalarg->eval_tofree;
606 evalarg->eval_tofree = NULL;
607 }
608
Bram Moolenaar65c44152020-12-24 15:14:01 +0100609 if (!equal_arrow)
Bram Moolenaaree619e52020-03-28 21:38:06 +0100610 {
Bram Moolenaar65c44152020-12-24 15:14:01 +0100611 *arg = skipwhite_and_linebreak(*arg, evalarg);
612 if (**arg != '}')
613 {
614 semsg(_("E451: Expected }: %s"), *arg);
615 goto errret;
616 }
617 ++*arg;
Bram Moolenaaree619e52020-03-28 21:38:06 +0100618 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200619
620 if (evaluate)
621 {
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200622 int len;
623 int flags = 0;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200624 char_u *p;
Bram Moolenaar04b12692020-05-04 23:24:44 +0200625 char_u *name = get_lambda_name();
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200626
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200627 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200628 if (fp == NULL)
629 goto errret;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200630 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200631 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200632 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200633 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200634
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200635 ga_init2(&newlines, (int)sizeof(char_u *), 1);
636 if (ga_grow(&newlines, 1) == FAIL)
637 goto errret;
638
Bram Moolenaare38eab22019-12-05 21:50:01 +0100639 // Add "return " before the expression.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200640 len = 7 + (int)(end - start) + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200641 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200642 if (p == NULL)
643 goto errret;
644 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
645 STRCPY(p, "return ");
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200646 vim_strncpy(p + 7, start, end - start);
Bram Moolenaarf10806b2020-04-02 18:34:35 +0200647 if (strstr((char *)p + 7, "a:") == NULL)
648 // No a: variables are used for sure.
649 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200650
651 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100652 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200653 hash_add(&func_hashtab, UF2HIKEY(fp));
654 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200655 ga_init(&fp->uf_def_args);
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100656 if (types_optional)
657 {
658 if (parse_argument_types(fp, &argtypes, FALSE) == FAIL)
659 goto errret;
660 if (ret_type != NULL)
661 {
662 fp->uf_ret_type = parse_type(&ret_type,
663 &fp->uf_type_list, TRUE);
664 if (fp->uf_ret_type == NULL)
665 goto errret;
666 }
667 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100668
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200669 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200670 if (current_funccal != NULL && eval_lavars)
671 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200672 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200673 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200674 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200675 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200676
677#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200678 if (prof_def_func())
679 func_do_profile(fp);
680#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200681 if (sandbox)
682 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200684 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200685 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200686 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200687 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100688 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200689
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200690 pt->pt_func = fp;
691 pt->pt_refcount = 1;
692 rettv->vval.v_partial = pt;
693 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200694 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200695
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200696 eval_lavars_used = old_eval_lavars;
Bram Moolenaarefaaaa62020-07-08 22:24:09 +0200697 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200698 evalarg->eval_tofree = tofree;
699 else
700 vim_free(tofree);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100701 if (types_optional)
702 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200703 return OK;
704
705errret:
706 ga_clear_strings(&newargs);
707 ga_clear_strings(&newlines);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100708 if (types_optional)
709 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200710 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100711 vim_free(pt);
Bram Moolenaarefaaaa62020-07-08 22:24:09 +0200712 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200713 evalarg->eval_tofree = tofree;
714 else
715 vim_free(tofree);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200716 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200717 return FAIL;
718}
719
720/*
721 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
722 * name it contains, otherwise return "name".
723 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
724 * "partialp".
725 */
726 char_u *
727deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
728{
729 dictitem_T *v;
730 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200731 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200732
733 if (partialp != NULL)
734 *partialp = NULL;
735
736 cc = name[*lenp];
737 name[*lenp] = NUL;
738 v = find_var(name, NULL, no_autoload);
739 name[*lenp] = cc;
740 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
741 {
742 if (v->di_tv.vval.v_string == NULL)
743 {
744 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100745 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200746 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200747 s = v->di_tv.vval.v_string;
748 *lenp = (int)STRLEN(s);
749 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200750 }
751
752 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
753 {
754 partial_T *pt = v->di_tv.vval.v_partial;
755
756 if (pt == NULL)
757 {
758 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100759 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200760 }
761 if (partialp != NULL)
762 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200763 s = partial_name(pt);
764 *lenp = (int)STRLEN(s);
765 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200766 }
767
768 return name;
769}
770
771/*
772 * Give an error message with a function name. Handle <SNR> things.
773 * "ermsg" is to be passed without translation, use N_() instead of _().
774 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100775 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200776emsg_funcname(char *ermsg, char_u *name)
777{
778 char_u *p;
779
780 if (*name == K_SPECIAL)
781 p = concat_str((char_u *)"<SNR>", name + 3);
782 else
783 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100784 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200785 if (p != name)
786 vim_free(p);
787}
788
789/*
790 * Allocate a variable for the result of a function.
791 * Return OK or FAIL.
792 */
793 int
794get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200795 char_u *name, // name of the function
796 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200797 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200798 char_u **arg, // argument, pointing to the '('
Bram Moolenaare6b53242020-07-01 17:28:33 +0200799 evalarg_T *evalarg, // for line continuation
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200800 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200801{
802 char_u *argp;
803 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100804 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
805 int argcount = 0; // number of arguments found
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200806 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200807
808 /*
809 * Get the arguments.
810 */
811 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200812 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
813 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200814 {
Bram Moolenaare6b53242020-07-01 17:28:33 +0200815 // skip the '(' or ',' and possibly line breaks
816 argp = skipwhite_and_linebreak(argp + 1, evalarg);
817
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200818 if (*argp == ')' || *argp == ',' || *argp == NUL)
819 break;
Bram Moolenaare6b53242020-07-01 17:28:33 +0200820 if (eval1(&argp, &argvars[argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200821 {
822 ret = FAIL;
823 break;
824 }
825 ++argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +0200826 // The comma should come right after the argument, but this wasn't
827 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200828 if (vim9script)
829 {
830 if (*argp != ',' && *skipwhite(argp) == ',')
831 {
832 semsg(_(e_no_white_space_allowed_before_str), ",");
833 ret = FAIL;
834 break;
835 }
836 }
837 else
Bram Moolenaar9d489562020-07-30 20:08:50 +0200838 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200839 if (*argp != ',')
840 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200841 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
842 {
843 semsg(_(e_white_space_required_after_str), ",");
844 ret = FAIL;
845 break;
846 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200847 }
Bram Moolenaare6b53242020-07-01 17:28:33 +0200848 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200849 if (*argp == ')')
850 ++argp;
851 else
852 ret = FAIL;
853
854 if (ret == OK)
855 {
856 int i = 0;
857
858 if (get_vim_var_nr(VV_TESTING))
859 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100860 // Prepare for calling test_garbagecollect_now(), need to know
861 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200862 if (funcargs.ga_itemsize == 0)
863 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
864 for (i = 0; i < argcount; ++i)
865 if (ga_grow(&funcargs, 1) == OK)
866 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
867 &argvars[i];
868 }
869
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200870 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200871
872 funcargs.ga_len -= i;
873 }
874 else if (!aborting())
875 {
876 if (argcount == MAX_FUNC_ARGS)
877 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
878 else
879 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
880 }
881
882 while (--argcount >= 0)
883 clear_tv(&argvars[argcount]);
884
Bram Moolenaar8294d492020-08-10 22:40:56 +0200885 if (in_vim9script())
886 *arg = argp;
887 else
888 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200889 return ret;
890}
891
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200892/*
893 * Return TRUE if "p" starts with "<SID>" or "s:".
894 * Only works if eval_fname_script() returned non-zero for "p"!
895 */
896 static int
897eval_fname_sid(char_u *p)
898{
899 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
900}
901
902/*
903 * In a script change <SID>name() and s:name() to K_SNR 123_name().
904 * Change <SNR>123_name() to K_SNR 123_name().
905 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
906 * (slow).
907 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100908 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200909fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
910{
911 int llen;
912 char_u *fname;
913 int i;
914
915 llen = eval_fname_script(name);
916 if (llen > 0)
917 {
918 fname_buf[0] = K_SPECIAL;
919 fname_buf[1] = KS_EXTRA;
920 fname_buf[2] = (int)KE_SNR;
921 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100922 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200923 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200924 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100925 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200926 else
927 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200928 sprintf((char *)fname_buf + 3, "%ld_",
929 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200930 i = (int)STRLEN(fname_buf);
931 }
932 }
933 if (i + STRLEN(name + llen) < FLEN_FIXED)
934 {
935 STRCPY(fname_buf + i, name + llen);
936 fname = fname_buf;
937 }
938 else
939 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200940 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200941 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +0100942 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200943 else
944 {
945 *tofree = fname;
946 mch_memmove(fname, fname_buf, (size_t)i);
947 STRCPY(fname + i, name + llen);
948 }
949 }
950 }
951 else
952 fname = name;
953 return fname;
954}
955
956/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100957 * Find a function "name" in script "sid".
958 */
959 static ufunc_T *
960find_func_with_sid(char_u *name, int sid)
961{
962 hashitem_T *hi;
963 char_u buffer[200];
964
965 buffer[0] = K_SPECIAL;
966 buffer[1] = KS_EXTRA;
967 buffer[2] = (int)KE_SNR;
968 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
969 (long)sid, name);
970 hi = hash_find(&func_hashtab, buffer);
971 if (!HASHITEM_EMPTY(hi))
972 return HI2UF(hi);
973
974 return NULL;
975}
976
977/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200978 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200979 * When "is_global" is true don't find script-local or imported functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200980 * Return NULL for unknown function.
981 */
Bram Moolenaarce658352020-07-31 23:47:12 +0200982 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200983find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200984{
985 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100986 ufunc_T *func;
987 imported_T *imported;
988
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200989 if (!is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990 {
Bram Moolenaar333894b2020-08-01 18:53:07 +0200991 char_u *after_script = NULL;
Bram Moolenaarefa94442020-08-08 22:16:00 +0200992 long sid = 0;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200993 int find_script_local = in_vim9script()
994 && eval_isnamec1(*name) && name[1] != ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995
Bram Moolenaar95006e32020-08-29 17:47:08 +0200996 if (find_script_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200998 // Find script-local function before global one.
999 func = find_func_with_sid(name, current_sctx.sc_sid);
1000 if (func != NULL)
1001 return func;
1002 }
1003
Bram Moolenaarefa94442020-08-08 22:16:00 +02001004 if (name[0] == K_SPECIAL
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001005 && name[1] == KS_EXTRA
1006 && name[2] == KE_SNR)
1007 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001008 // Caller changes s: to <SNR>99_name.
1009
1010 after_script = name + 3;
1011 sid = getdigits(&after_script);
Bram Moolenaarefa94442020-08-08 22:16:00 +02001012 if (*after_script == '_')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001013 ++after_script;
1014 else
1015 after_script = NULL;
1016 }
Bram Moolenaar95006e32020-08-29 17:47:08 +02001017 if (find_script_local || after_script != NULL)
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001018 {
1019 // Find imported function before global one.
Bram Moolenaarefa94442020-08-08 22:16:00 +02001020 if (after_script != NULL && sid != current_sctx.sc_sid)
1021 imported = find_imported_in_script(after_script, 0, sid);
1022 else
1023 imported = find_imported(after_script == NULL
1024 ? name : after_script, 0, cctx);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001025 if (imported != NULL && imported->imp_funcname != NULL)
1026 {
1027 hi = hash_find(&func_hashtab, imported->imp_funcname);
1028 if (!HASHITEM_EMPTY(hi))
1029 return HI2UF(hi);
1030 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001031 }
1032 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001033
Bram Moolenaara26b9702020-04-18 19:53:28 +02001034 hi = hash_find(&func_hashtab,
1035 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001036 if (!HASHITEM_EMPTY(hi))
1037 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001038
1039 return NULL;
1040}
1041
1042/*
1043 * Find a function by name, return pointer to it in ufuncs.
1044 * "cctx" is passed in a :def function to find imported functions.
1045 * Return NULL for unknown or dead function.
1046 */
1047 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001048find_func(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001049{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001050 ufunc_T *fp = find_func_even_dead(name, is_global, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051
1052 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
1053 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001054 return NULL;
1055}
1056
1057/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02001058 * Return TRUE if "ufunc" is a global function.
1059 */
1060 int
1061func_is_global(ufunc_T *ufunc)
1062{
1063 return ufunc->uf_name[0] != K_SPECIAL;
1064}
1065
1066/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001067 * Copy the function name of "fp" to buffer "buf".
1068 * "buf" must be able to hold the function name plus three bytes.
1069 * Takes care of script-local function names.
1070 */
1071 static void
1072cat_func_name(char_u *buf, ufunc_T *fp)
1073{
Bram Moolenaar0f769812020-09-12 18:32:34 +02001074 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001075 {
1076 STRCPY(buf, "<SNR>");
1077 STRCAT(buf, fp->uf_name + 3);
1078 }
1079 else
1080 STRCPY(buf, fp->uf_name);
1081}
1082
1083/*
1084 * Add a number variable "name" to dict "dp" with value "nr".
1085 */
1086 static void
1087add_nr_var(
1088 dict_T *dp,
1089 dictitem_T *v,
1090 char *name,
1091 varnumber_T nr)
1092{
1093 STRCPY(v->di_key, name);
1094 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1095 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
1096 v->di_tv.v_type = VAR_NUMBER;
1097 v->di_tv.v_lock = VAR_FIXED;
1098 v->di_tv.vval.v_number = nr;
1099}
1100
1101/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001102 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001103 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001104 static void
1105free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001106{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001107 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001108
1109 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1110 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001111 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001112
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001113 // When garbage collecting a funccall_T may be freed before the
1114 // function that references it, clear its uf_scoped field.
1115 // The function may have been redefined and point to another
1116 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001117 if (fp != NULL && fp->uf_scoped == fc)
1118 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001119 }
Bram Moolenaar58016442016-07-31 18:30:22 +02001120 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001121
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001122 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001123 vim_free(fc);
1124}
1125
1126/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001127 * Free "fc" and what it contains.
1128 * Can be called only when "fc" is kept beyond the period of it called,
1129 * i.e. after cleanup_function_call(fc).
1130 */
1131 static void
1132free_funccal_contents(funccall_T *fc)
1133{
1134 listitem_T *li;
1135
1136 // Free all l: variables.
1137 vars_clear(&fc->l_vars.dv_hashtab);
1138
1139 // Free all a: variables.
1140 vars_clear(&fc->l_avars.dv_hashtab);
1141
1142 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001143 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001144 clear_tv(&li->li_tv);
1145
1146 free_funccal(fc);
1147}
1148
1149/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02001150 * Handle the last part of returning from a function: free the local hashtable.
1151 * Unless it is still in use by a closure.
1152 */
1153 static void
1154cleanup_function_call(funccall_T *fc)
1155{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001156 int may_free_fc = fc->fc_refcount <= 0;
1157 int free_fc = TRUE;
1158
Bram Moolenaar6914c642017-04-01 21:21:30 +02001159 current_funccal = fc->caller;
1160
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001161 // Free all l: variables if not referred.
1162 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
1163 vars_clear(&fc->l_vars.dv_hashtab);
1164 else
1165 free_fc = FALSE;
1166
1167 // If the a:000 list and the l: and a: dicts are not referenced and
1168 // there is no closure using it, we can free the funccall_T and what's
1169 // in it.
1170 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
1171 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02001172 else
1173 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001174 int todo;
1175 hashitem_T *hi;
1176 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02001177
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001178 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02001179
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001180 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001181 todo = (int)fc->l_avars.dv_hashtab.ht_used;
1182 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
1183 {
1184 if (!HASHITEM_EMPTY(hi))
1185 {
1186 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001187 di = HI2DI(hi);
1188 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02001189 }
1190 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001191 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02001192
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001193 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
1194 fc->l_varlist.lv_first = NULL;
1195 else
1196 {
1197 listitem_T *li;
1198
1199 free_fc = FALSE;
1200
1201 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001202 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02001203 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001204 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001205
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001206 if (free_fc)
1207 free_funccal(fc);
1208 else
1209 {
1210 static int made_copy = 0;
1211
1212 // "fc" is still in use. This can happen when returning "a:000",
1213 // assigning "l:" to a global variable or defining a closure.
1214 // Link "fc" in the list for garbage collection later.
1215 fc->caller = previous_funccal;
1216 previous_funccal = fc;
1217
1218 if (want_garbage_collect)
1219 // If garbage collector is ready, clear count.
1220 made_copy = 0;
1221 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001222 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001223 // We have made a lot of copies, worth 4 Mbyte. This can happen
1224 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01001225 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001226 // too much memory.
1227 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01001228 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001229 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02001230 }
1231}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001232
1233/*
1234 * There are two kinds of function names:
1235 * 1. ordinary names, function defined with :function or :def
1236 * 2. numbered functions and lambdas
1237 * For the first we only count the name stored in func_hashtab as a reference,
1238 * using function() does not count as a reference, because the function is
1239 * looked up by name.
1240 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02001241 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001242func_name_refcount(char_u *name)
1243{
1244 return isdigit(*name) || *name == '<';
1245}
1246
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001247/*
1248 * Unreference "fc": decrement the reference count and free it when it
1249 * becomes zero. "fp" is detached from "fc".
1250 * When "force" is TRUE we are exiting.
1251 */
1252 static void
1253funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
1254{
1255 funccall_T **pfc;
1256 int i;
1257
1258 if (fc == NULL)
1259 return;
1260
1261 if (--fc->fc_refcount <= 0 && (force || (
1262 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
1263 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
1264 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
1265 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
1266 {
1267 if (fc == *pfc)
1268 {
1269 *pfc = fc->caller;
1270 free_funccal_contents(fc);
1271 return;
1272 }
1273 }
1274 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1275 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
1276 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
1277}
1278
1279/*
1280 * Remove the function from the function hashtable. If the function was
1281 * deleted while it still has references this was already done.
1282 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
1283 */
1284 static int
1285func_remove(ufunc_T *fp)
1286{
1287 hashitem_T *hi;
1288
1289 // Return if it was already virtually deleted.
1290 if (fp->uf_flags & FC_DEAD)
1291 return FALSE;
1292
1293 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
1294 if (!HASHITEM_EMPTY(hi))
1295 {
1296 // When there is a def-function index do not actually remove the
1297 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001298 // Do remove it when it's a copy.
1299 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001300 fp->uf_flags |= FC_DEAD;
1301 else
1302 hash_remove(&func_hashtab, hi);
1303 return TRUE;
1304 }
1305 return FALSE;
1306}
1307
1308 static void
1309func_clear_items(ufunc_T *fp)
1310{
1311 ga_clear_strings(&(fp->uf_args));
1312 ga_clear_strings(&(fp->uf_def_args));
1313 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001314 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001315 VIM_CLEAR(fp->uf_def_arg_idx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001316 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001317 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02001318 clear_type_list(&fp->uf_type_list);
Bram Moolenaarf112f302020-12-20 17:47:52 +01001319 partial_unref(fp->uf_partial);
1320 fp->uf_partial = NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +02001321
1322#ifdef FEAT_LUA
1323 if (fp->uf_cb_free != NULL)
1324 {
1325 fp->uf_cb_free(fp->uf_cb_state);
1326 fp->uf_cb_free = NULL;
1327 }
1328
1329 fp->uf_cb_state = NULL;
1330 fp->uf_cb = NULL;
1331#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332#ifdef FEAT_PROFILE
1333 VIM_CLEAR(fp->uf_tml_count);
1334 VIM_CLEAR(fp->uf_tml_total);
1335 VIM_CLEAR(fp->uf_tml_self);
1336#endif
1337}
1338
1339/*
1340 * Free all things that a function contains. Does not free the function
1341 * itself, use func_free() for that.
1342 * When "force" is TRUE we are exiting.
1343 */
1344 static void
1345func_clear(ufunc_T *fp, int force)
1346{
1347 if (fp->uf_cleared)
1348 return;
1349 fp->uf_cleared = TRUE;
1350
1351 // clear this function
1352 func_clear_items(fp);
1353 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001354 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355}
1356
1357/*
1358 * Free a function and remove it from the list of functions. Does not free
1359 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001360 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02001361 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02001363 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001364func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001365{
1366 // Only remove it when not done already, otherwise we would remove a newer
1367 // version of the function with the same name.
1368 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1369 func_remove(fp);
1370
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001371 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001372 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001373 if (fp->uf_dfunc_idx > 0)
1374 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001375 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02001377 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001378 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02001379 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380}
1381
1382/*
1383 * Free all things that a function contains and free the function itself.
1384 * When "force" is TRUE we are exiting.
1385 */
1386 static void
1387func_clear_free(ufunc_T *fp, int force)
1388{
1389 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001390 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
1391 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001392 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02001393 else
1394 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395}
1396
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001397/*
1398 * Copy already defined function "lambda" to a new function with name "global".
1399 * This is for when a compiled function defines a global function.
1400 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001401 int
1402copy_func(char_u *lambda, char_u *global, ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001403{
1404 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01001405 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001406
1407 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001408 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001409 semsg(_(e_lambda_function_not_found_str), lambda);
1410 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001411 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001412
1413 // TODO: handle ! to overwrite
1414 fp = find_func(global, TRUE, NULL);
1415 if (fp != NULL)
1416 {
1417 semsg(_(e_funcexts), global);
1418 return FAIL;
1419 }
1420
1421 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
1422 if (fp == NULL)
1423 return FAIL;
1424
1425 fp->uf_varargs = ufunc->uf_varargs;
1426 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
1427 fp->uf_def_status = ufunc->uf_def_status;
1428 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
1429 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
1430 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
1431 == FAIL
1432 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
1433 goto failed;
1434
1435 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
1436 : vim_strsave(ufunc->uf_name_exp);
1437 if (ufunc->uf_arg_types != NULL)
1438 {
1439 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
1440 if (fp->uf_arg_types == NULL)
1441 goto failed;
1442 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
1443 sizeof(type_T *) * fp->uf_args.ga_len);
1444 }
1445 if (ufunc->uf_def_arg_idx != NULL)
1446 {
1447 fp->uf_def_arg_idx = ALLOC_MULT(int, fp->uf_def_args.ga_len + 1);
1448 if (fp->uf_def_arg_idx == NULL)
1449 goto failed;
1450 mch_memmove(fp->uf_def_arg_idx, ufunc->uf_def_arg_idx,
1451 sizeof(int) * fp->uf_def_args.ga_len + 1);
1452 }
1453 if (ufunc->uf_va_name != NULL)
1454 {
1455 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
1456 if (fp->uf_va_name == NULL)
1457 goto failed;
1458 }
1459 fp->uf_ret_type = ufunc->uf_ret_type;
1460
1461 fp->uf_refcount = 1;
1462 STRCPY(fp->uf_name, global);
1463 hash_add(&func_hashtab, UF2HIKEY(fp));
1464
1465 // the referenced dfunc_T is now used one more time
1466 link_def_function(fp);
1467
1468 // Create a partial to store the context of the function, if not done
1469 // already.
1470 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
1471 {
1472 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
1473
1474 if (pt == NULL)
1475 goto failed;
1476 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
1477 goto failed;
1478 ufunc->uf_partial = pt;
1479 --pt->pt_refcount; // not referenced here yet
1480 }
1481 if (ufunc->uf_partial != NULL)
1482 {
1483 fp->uf_partial = ufunc->uf_partial;
1484 ++fp->uf_partial->pt_refcount;
1485 }
1486
1487 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001488
1489failed:
1490 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001491 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001492}
1493
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001494static int funcdepth = 0;
1495
1496/*
1497 * Increment the function call depth count.
1498 * Return FAIL when going over 'maxfuncdepth'.
1499 * Otherwise return OK, must call funcdepth_decrement() later!
1500 */
1501 int
1502funcdepth_increment(void)
1503{
1504 if (funcdepth >= p_mfd)
1505 {
1506 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
1507 return FAIL;
1508 }
1509 ++funcdepth;
1510 return OK;
1511}
1512
1513 void
1514funcdepth_decrement(void)
1515{
1516 --funcdepth;
1517}
1518
1519/*
1520 * Get the current function call depth.
1521 */
1522 int
1523funcdepth_get(void)
1524{
1525 return funcdepth;
1526}
1527
1528/*
1529 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001530 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001531 * times as funcdepth_increment().
1532 */
1533 void
1534funcdepth_restore(int depth)
1535{
1536 funcdepth = depth;
1537}
Bram Moolenaar6914c642017-04-01 21:21:30 +02001538
1539/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001540 * Call a user function.
1541 */
1542 static void
1543call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001544 ufunc_T *fp, // pointer to function
1545 int argcount, // nr of args
1546 typval_T *argvars, // arguments
1547 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001548 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01001549 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001550{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001551 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001552 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001553 funccall_T *fc;
1554 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001555 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001556 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001557 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001558 int i;
1559 int ai;
1560 int islambda = FALSE;
1561 char_u numbuf[NUMBUFLEN];
1562 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001563#ifdef FEAT_PROFILE
1564 proftime_T wait_start;
1565 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001566 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001567#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001568 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001569
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001570 // If depth of calling is getting too high, don't execute the function.
1571 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001572 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001573 rettv->v_type = VAR_NUMBER;
1574 rettv->vval.v_number = -1;
1575 return;
1576 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001577
Bram Moolenaare38eab22019-12-05 21:50:01 +01001578 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001579
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001580 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001581 if (fc == NULL)
1582 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001583 fc->caller = current_funccal;
1584 current_funccal = fc;
1585 fc->func = fp;
1586 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001587 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001588 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001589 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1590 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001591 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001592 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001593 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001594
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001595 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 {
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001597 // Execute the function, possibly compiling it first.
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001598 call_def_function(fp, argcount, argvars, funcexe->partial, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001599 funcdepth_decrement();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 current_funccal = fc->caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 free_funccal(fc);
1602 return;
1603 }
1604
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001605 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1606 islambda = TRUE;
1607
1608 /*
1609 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1610 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1611 * each argument variable and saves a lot of time.
1612 */
1613 /*
1614 * Init l: variables.
1615 */
1616 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1617 if (selfdict != NULL)
1618 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001619 // Set l:self to "selfdict". Use "name" to avoid a warning from
1620 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001621 v = &fc->fixvar[fixvar_idx++].var;
1622 name = v->di_key;
1623 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001624 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001625 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1626 v->di_tv.v_type = VAR_DICT;
1627 v->di_tv.v_lock = 0;
1628 v->di_tv.vval.v_dict = selfdict;
1629 ++selfdict->dv_refcount;
1630 }
1631
1632 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001633 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001634 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001635 * Set a:000 to a list with room for the "..." arguments.
1636 */
1637 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001638 if ((fp->uf_flags & FC_NOARGS) == 0)
1639 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001640 (varnumber_T)(argcount >= fp->uf_args.ga_len
1641 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001642 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001643 if ((fp->uf_flags & FC_NOARGS) == 0)
1644 {
1645 // Use "name" to avoid a warning from some compiler that checks the
1646 // destination size.
1647 v = &fc->fixvar[fixvar_idx++].var;
1648 name = v->di_key;
1649 STRCPY(name, "000");
1650 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1651 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1652 v->di_tv.v_type = VAR_LIST;
1653 v->di_tv.v_lock = VAR_FIXED;
1654 v->di_tv.vval.v_list = &fc->l_varlist;
1655 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001656 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001657 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1658 fc->l_varlist.lv_lock = VAR_FIXED;
1659
1660 /*
1661 * Set a:firstline to "firstline" and a:lastline to "lastline".
1662 * Set a:name to named arguments.
1663 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001664 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001665 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001666 if ((fp->uf_flags & FC_NOARGS) == 0)
1667 {
1668 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001669 (varnumber_T)funcexe->firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001670 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001671 (varnumber_T)funcexe->lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001672 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001673 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001674 {
1675 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001676 typval_T def_rettv;
1677 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001678
1679 ai = i - fp->uf_args.ga_len;
1680 if (ai < 0)
1681 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001682 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001683 name = FUNCARG(fp, i);
1684 if (islambda)
1685 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001686
1687 // evaluate named argument default expression
1688 isdefault = ai + fp->uf_def_args.ga_len >= 0
1689 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1690 && argvars[i].vval.v_number == VVAL_NONE));
1691 if (isdefault)
1692 {
1693 char_u *default_expr = NULL;
1694 def_rettv.v_type = VAR_NUMBER;
1695 def_rettv.vval.v_number = -1;
1696
1697 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1698 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001699 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001700 {
1701 default_arg_err = 1;
1702 break;
1703 }
1704 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001705 }
1706 else
1707 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001708 if ((fp->uf_flags & FC_NOARGS) != 0)
1709 // Bail out if no a: arguments used (in lambda).
1710 break;
1711
Bram Moolenaare38eab22019-12-05 21:50:01 +01001712 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001713 sprintf((char *)numbuf, "%d", ai + 1);
1714 name = numbuf;
1715 }
1716 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1717 {
1718 v = &fc->fixvar[fixvar_idx++].var;
1719 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001720 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001721 }
1722 else
1723 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001724 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001725 if (v == NULL)
1726 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001727 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001728 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001729
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001730 // Note: the values are copied directly to avoid alloc/free.
1731 // "argvars" must have VAR_FIXED for v_lock.
1732 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001733 v->di_tv.v_lock = VAR_FIXED;
1734
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001735 if (addlocal)
1736 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001737 // Named arguments should be accessed without the "a:" prefix in
1738 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001739 copy_tv(&v->di_tv, &v->di_tv);
1740 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001741 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001742 else
1743 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001744
1745 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1746 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001747 listitem_T *li = &fc->l_listitems[ai];
1748
1749 li->li_tv = argvars[i];
1750 li->li_tv.v_lock = VAR_FIXED;
1751 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001752 }
1753 }
1754
Bram Moolenaare38eab22019-12-05 21:50:01 +01001755 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001756 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001757
1758 if (fp->uf_flags & FC_SANDBOX)
1759 {
1760 using_sandbox = TRUE;
1761 ++sandbox;
1762 }
1763
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001764 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001765 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001766 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001767 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001768 ++no_wait_return;
1769 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001770
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001771 smsg(_("calling %s"), SOURCING_NAME);
1772 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001773 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001774 char_u buf[MSG_BUF_LEN];
1775 char_u numbuf2[NUMBUFLEN];
1776 char_u *tofree;
1777 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001778
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001779 msg_puts("(");
1780 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001781 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001782 if (i > 0)
1783 msg_puts(", ");
1784 if (argvars[i].v_type == VAR_NUMBER)
1785 msg_outnum((long)argvars[i].vval.v_number);
1786 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001787 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001788 // Do not want errors such as E724 here.
1789 ++emsg_off;
1790 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1791 --emsg_off;
1792 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001793 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001794 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001795 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001796 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1797 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001798 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001799 msg_puts((char *)s);
1800 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001801 }
1802 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001803 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001804 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001805 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001806 msg_puts("\n"); // don't overwrite this either
1807
1808 verbose_leave_scroll();
1809 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001810 }
1811#ifdef FEAT_PROFILE
1812 if (do_profiling == PROF_YES)
1813 {
1814 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001815 {
1816 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001817 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001818 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001819 if (fp->uf_profiling
1820 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1821 {
1822 ++fp->uf_tm_count;
1823 profile_start(&call_start);
1824 profile_zero(&fp->uf_tm_children);
1825 }
1826 script_prof_save(&wait_start);
1827 }
1828#endif
1829
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001830 save_current_sctx = current_sctx;
1831 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001832 save_did_emsg = did_emsg;
1833 did_emsg = FALSE;
1834
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001835 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1836 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001837 else if (islambda)
1838 {
1839 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
1840
1841 // A Lambda always has the command "return {expr}". It is much faster
1842 // to evaluate {expr} directly.
1843 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001844 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001845 --ex_nesting_level;
1846 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001847 else
1848 // call do_cmdline() to execute the lines
1849 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001850 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1851
1852 --RedrawingDisabled;
1853
Bram Moolenaare38eab22019-12-05 21:50:01 +01001854 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001855 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1856 {
1857 clear_tv(rettv);
1858 rettv->v_type = VAR_NUMBER;
1859 rettv->vval.v_number = -1;
1860 }
1861
1862#ifdef FEAT_PROFILE
1863 if (do_profiling == PROF_YES && (fp->uf_profiling
1864 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1865 {
1866 profile_end(&call_start);
1867 profile_sub_wait(&wait_start, &call_start);
1868 profile_add(&fp->uf_tm_total, &call_start);
1869 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1870 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1871 {
1872 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1873 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1874 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001875 if (started_profiling)
1876 // make a ":profdel func" stop profiling the function
1877 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001878 }
1879#endif
1880
Bram Moolenaare38eab22019-12-05 21:50:01 +01001881 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001882 if (p_verbose >= 12)
1883 {
1884 ++no_wait_return;
1885 verbose_enter_scroll();
1886
1887 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001888 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001889 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001890 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001891 (long)fc->rettv->vval.v_number);
1892 else
1893 {
1894 char_u buf[MSG_BUF_LEN];
1895 char_u numbuf2[NUMBUFLEN];
1896 char_u *tofree;
1897 char_u *s;
1898
Bram Moolenaare38eab22019-12-05 21:50:01 +01001899 // The value may be very long. Skip the middle part, so that we
1900 // have some idea how it starts and ends. smsg() would always
1901 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001902 ++emsg_off;
1903 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1904 --emsg_off;
1905 if (s != NULL)
1906 {
1907 if (vim_strsize(s) > MSG_BUF_CLEN)
1908 {
1909 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1910 s = buf;
1911 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001912 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001913 vim_free(tofree);
1914 }
1915 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001916 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001917
1918 verbose_leave_scroll();
1919 --no_wait_return;
1920 }
1921
Bram Moolenaare31ee862020-01-07 20:59:34 +01001922 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001923 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001924 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001925#ifdef FEAT_PROFILE
1926 if (do_profiling == PROF_YES)
1927 script_prof_restore(&wait_start);
1928#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001929 if (using_sandbox)
1930 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001931
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001932 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001933 {
1934 ++no_wait_return;
1935 verbose_enter_scroll();
1936
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001937 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001938 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001939
1940 verbose_leave_scroll();
1941 --no_wait_return;
1942 }
1943
1944 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001945 funcdepth_decrement();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001946 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001947}
1948
1949/*
Bram Moolenaar50824712020-12-20 21:10:17 +01001950 * Check the argument count for user function "fp".
1951 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
1952 */
1953 int
1954check_user_func_argcount(ufunc_T *fp, int argcount)
1955{
1956 int regular_args = fp->uf_args.ga_len;
1957
1958 if (argcount < regular_args - fp->uf_def_args.ga_len)
1959 return FCERR_TOOFEW;
1960 else if (!has_varargs(fp) && argcount > regular_args)
1961 return FCERR_TOOMANY;
1962 return FCERR_UNKNOWN;
1963}
1964
1965/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001967 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 int
1969call_user_func_check(
1970 ufunc_T *fp,
1971 int argcount,
1972 typval_T *argvars,
1973 typval_T *rettv,
1974 funcexe_T *funcexe,
1975 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001976{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001978
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1980 *funcexe->doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01001981 error = check_user_func_argcount(fp, argcount);
1982 if (error != FCERR_UNKNOWN)
1983 return error;
1984 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 error = FCERR_DICT;
1986 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001987 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 int did_save_redo = FALSE;
1989 save_redo_T save_redo;
1990
1991 /*
1992 * Call the user function.
1993 * Save and restore search patterns, script variables and
1994 * redo buffer.
1995 */
1996 save_search_patterns();
1997 if (!ins_compl_active())
1998 {
1999 saveRedobuff(&save_redo);
2000 did_save_redo = TRUE;
2001 }
2002 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002003 call_user_func(fp, argcount, argvars, rettv, funcexe,
2004 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
2006 // Function was unreferenced while being used, free it now.
2007 func_clear_free(fp, FALSE);
2008 if (did_save_redo)
2009 restoreRedobuff(&save_redo);
2010 restore_search_patterns();
2011 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002012 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002014}
2015
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002016static funccal_entry_T *funccal_stack = NULL;
2017
2018/*
2019 * Save the current function call pointer, and set it to NULL.
2020 * Used when executing autocommands and for ":source".
2021 */
2022 void
2023save_funccal(funccal_entry_T *entry)
2024{
2025 entry->top_funccal = current_funccal;
2026 entry->next = funccal_stack;
2027 funccal_stack = entry;
2028 current_funccal = NULL;
2029}
2030
2031 void
2032restore_funccal(void)
2033{
2034 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002035 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002036 else
2037 {
2038 current_funccal = funccal_stack->top_funccal;
2039 funccal_stack = funccal_stack->next;
2040 }
2041}
2042
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02002043 funccall_T *
2044get_current_funccal(void)
2045{
2046 return current_funccal;
2047}
2048
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002049/*
2050 * Mark all functions of script "sid" as deleted.
2051 */
2052 void
2053delete_script_functions(int sid)
2054{
2055 hashitem_T *hi;
2056 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02002057 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002058 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002059 size_t len;
2060
2061 buf[0] = K_SPECIAL;
2062 buf[1] = KS_EXTRA;
2063 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002064 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002065 len = STRLEN(buf);
2066
Bram Moolenaarce658352020-07-31 23:47:12 +02002067 while (todo > 0)
2068 {
2069 todo = func_hashtab.ht_used;
2070 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2071 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002072 {
Bram Moolenaarce658352020-07-31 23:47:12 +02002073 fp = HI2UF(hi);
2074 if (STRNCMP(fp->uf_name, buf, len) == 0)
2075 {
2076 int changed = func_hashtab.ht_changed;
2077
2078 fp->uf_flags |= FC_DEAD;
2079 func_clear(fp, TRUE);
2080 // When clearing a function another function can be cleared
2081 // as a side effect. When that happens start over.
2082 if (changed != func_hashtab.ht_changed)
2083 break;
2084 }
2085 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002086 }
Bram Moolenaarce658352020-07-31 23:47:12 +02002087 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002088}
2089
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002090#if defined(EXITFREE) || defined(PROTO)
2091 void
2092free_all_functions(void)
2093{
2094 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002095 ufunc_T *fp;
2096 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002097 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002098 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002099
Bram Moolenaare38eab22019-12-05 21:50:01 +01002100 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02002101 while (current_funccal != NULL)
2102 {
2103 clear_tv(current_funccal->rettv);
2104 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002105 if (current_funccal == NULL && funccal_stack != NULL)
2106 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02002107 }
2108
Bram Moolenaare38eab22019-12-05 21:50:01 +01002109 // First clear what the functions contain. Since this may lower the
2110 // reference count of a function, it may also free a function and change
2111 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002112 while (todo > 0)
2113 {
2114 todo = func_hashtab.ht_used;
2115 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2116 if (!HASHITEM_EMPTY(hi))
2117 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118 // clear the def function index now
2119 fp = HI2UF(hi);
2120 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002121 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122
Bram Moolenaare38eab22019-12-05 21:50:01 +01002123 // Only free functions that are not refcounted, those are
2124 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002125 if (func_name_refcount(fp->uf_name))
2126 ++skipped;
2127 else
2128 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002129 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002130 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002131 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002132 {
2133 skipped = 0;
2134 break;
2135 }
2136 }
2137 --todo;
2138 }
2139 }
2140
Bram Moolenaare38eab22019-12-05 21:50:01 +01002141 // Now actually free the functions. Need to start all over every time,
2142 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002143 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002144 while (func_hashtab.ht_used > skipped)
2145 {
2146 todo = func_hashtab.ht_used;
2147 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002148 if (!HASHITEM_EMPTY(hi))
2149 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02002150 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002151 // Only free functions that are not refcounted, those are
2152 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02002153 fp = HI2UF(hi);
2154 if (func_name_refcount(fp->uf_name))
2155 ++skipped;
2156 else
2157 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02002158 if (func_free(fp, FALSE) == OK)
2159 {
2160 skipped = 0;
2161 break;
2162 }
2163 // did not actually free it
2164 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002165 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002166 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02002167 }
2168 if (skipped == 0)
2169 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002170
2171 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002172}
2173#endif
2174
2175/*
2176 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02002177 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002178 * "len" is the length of "name", or -1 for NUL terminated.
2179 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002181builtin_function(char_u *name, int len)
2182{
2183 char_u *p;
2184
Bram Moolenaara26b9702020-04-18 19:53:28 +02002185 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002186 return FALSE;
2187 p = vim_strchr(name, AUTOLOAD_CHAR);
2188 return p == NULL || (len > 0 && p > name + len);
2189}
2190
2191 int
2192func_call(
2193 char_u *name,
2194 typval_T *args,
2195 partial_T *partial,
2196 dict_T *selfdict,
2197 typval_T *rettv)
2198{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002199 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002200 listitem_T *item;
2201 typval_T argv[MAX_FUNC_ARGS + 1];
2202 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002203 int r = 0;
2204
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002205 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002206 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002207 {
2208 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
2209 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002210 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002211 break;
2212 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002213 // Make a copy of each argument. This is needed to be able to set
2214 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002215 copy_tv(&item->li_tv, &argv[argc++]);
2216 }
2217
2218 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002219 {
2220 funcexe_T funcexe;
2221
Bram Moolenaara80faa82020-04-12 19:37:17 +02002222 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002223 funcexe.firstline = curwin->w_cursor.lnum;
2224 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002225 funcexe.evaluate = TRUE;
2226 funcexe.partial = partial;
2227 funcexe.selfdict = selfdict;
2228 r = call_func(name, -1, rettv, argc, argv, &funcexe);
2229 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002230
Bram Moolenaare38eab22019-12-05 21:50:01 +01002231 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002232 while (argc > 0)
2233 clear_tv(&argv[--argc]);
2234
2235 return r;
2236}
2237
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002238static int callback_depth = 0;
2239
2240 int
2241get_callback_depth(void)
2242{
2243 return callback_depth;
2244}
2245
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002246/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002247 * Invoke call_func() with a callback.
2248 */
2249 int
2250call_callback(
2251 callback_T *callback,
2252 int len, // length of "name" or -1 to use strlen()
2253 typval_T *rettv, // return value goes here
2254 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002255 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002256 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002257{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002258 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002259 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002260
Bram Moolenaara80faa82020-04-12 19:37:17 +02002261 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002262 funcexe.evaluate = TRUE;
2263 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002264 ++callback_depth;
2265 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
2266 --callback_depth;
2267 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002268}
2269
2270/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 * Give an error message for the result of a function.
2272 * Nothing if "error" is FCERR_NONE.
2273 */
2274 void
2275user_func_error(int error, char_u *name)
2276{
2277 switch (error)
2278 {
2279 case FCERR_UNKNOWN:
2280 emsg_funcname(e_unknownfunc, name);
2281 break;
2282 case FCERR_NOTMETHOD:
2283 emsg_funcname(
2284 N_("E276: Cannot use function as a method: %s"), name);
2285 break;
2286 case FCERR_DELETED:
2287 emsg_funcname(N_(e_func_deleted), name);
2288 break;
2289 case FCERR_TOOMANY:
2290 emsg_funcname((char *)e_toomanyarg, name);
2291 break;
2292 case FCERR_TOOFEW:
2293 emsg_funcname((char *)e_toofewarg, name);
2294 break;
2295 case FCERR_SCRIPT:
2296 emsg_funcname(
2297 N_("E120: Using <SID> not in a script context: %s"), name);
2298 break;
2299 case FCERR_DICT:
2300 emsg_funcname(
2301 N_("E725: Calling dict function without Dictionary: %s"),
2302 name);
2303 break;
2304 }
2305}
2306
2307/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002308 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002309 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002310 * Return FAIL when the function can't be called, OK otherwise.
2311 * Also returns OK when an error was encountered while executing the function.
2312 */
2313 int
2314call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02002315 char_u *funcname, // name of the function
2316 int len, // length of "name" or -1 to use strlen()
2317 typval_T *rettv, // return value goes here
2318 int argcount_in, // number of "argvars"
2319 typval_T *argvars_in, // vars for arguments, must have "argcount"
2320 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002321 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002322{
2323 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01002324 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002325 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002326 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002327 char_u fname_buf[FLEN_FIXED + 1];
2328 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002329 char_u *fname = NULL;
2330 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002331 int argcount = argcount_in;
2332 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002333 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002334 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
2335 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002336 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002337 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002338 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002339
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02002340 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
2341 // even when call_func() returns FAIL.
2342 rettv->v_type = VAR_UNKNOWN;
2343
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002344 if (partial != NULL)
2345 fp = partial->pt_func;
2346 if (fp == NULL)
2347 {
2348 // Make a copy of the name, if it comes from a funcref variable it
2349 // could be changed or deleted in the called function.
2350 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
2351 if (name == NULL)
2352 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002353
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002354 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
2355 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002356
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002357 if (funcexe->doesrange != NULL)
2358 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002359
2360 if (partial != NULL)
2361 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002362 // When the function has a partial with a dict and there is a dict
2363 // argument, use the dict argument. That is backwards compatible.
2364 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002365 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002366 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01002367 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002368 {
2369 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002370 {
2371 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
2372 {
Bram Moolenaaref140542019-12-31 21:27:13 +01002373 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002374 goto theend;
2375 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002376 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002377 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002378 for (i = 0; i < argcount_in; ++i)
2379 argv[i + argv_clear] = argvars_in[i];
2380 argvars = argv;
2381 argcount = partial->pt_argc + argcount_in;
2382 }
2383 }
2384
Bram Moolenaaref140542019-12-31 21:27:13 +01002385 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002386 {
2387 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02002388 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002389
Bram Moolenaar333894b2020-08-01 18:53:07 +02002390 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002391 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02002392 {
2393 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002394 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02002395 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002396
Bram Moolenaare38eab22019-12-05 21:50:01 +01002397 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002398 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01002399 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002400
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002401 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002402 {
2403 /*
2404 * User defined function.
2405 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002406 if (fp == NULL)
Bram Moolenaar333894b2020-08-01 18:53:07 +02002407 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002408
Bram Moolenaare38eab22019-12-05 21:50:01 +01002409 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002410 if (fp == NULL
2411 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002412 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002413 && !aborting())
2414 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002415 // executed an autocommand, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02002416 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002417 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002418 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002419 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
2420 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002421 // loaded a package, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02002422 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002423 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02002424 if (fp == NULL)
2425 {
2426 char_u *p = untrans_function_name(rfname);
2427
2428 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02002429 // Don't do this if the name starts with "s:".
2430 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaar333894b2020-08-01 18:53:07 +02002431 fp = find_func(p, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002432 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002433
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002434 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01002435 error = FCERR_DELETED;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002436#ifdef FEAT_LUA
2437 else if (fp != NULL && (fp->uf_flags & FC_CFUNC))
2438 {
2439 cfunc_T cb = fp->uf_cb;
2440
2441 error = (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
2442 }
2443#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002444 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002445 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002446 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01002447 // postponed filling in the arguments, do it now
2448 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002449 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002450
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002451 if (funcexe->basetv != NULL)
2452 {
2453 // Method call: base->Method()
2454 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
2455 argv[0] = *funcexe->basetv;
2456 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002457 argvars = argv;
2458 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002459 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002461 error = call_user_func_check(fp, argcount, argvars, rettv,
2462 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002463 }
2464 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002465 else if (funcexe->basetv != NULL)
2466 {
2467 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002468 * expr->method(): Find the method name in the table, call its
2469 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02002470 */
2471 error = call_internal_method(fname, argcount, argvars, rettv,
2472 funcexe->basetv);
2473 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002474 else
2475 {
2476 /*
2477 * Find the function name in the table, call its implementation.
2478 */
2479 error = call_internal_func(fname, argcount, argvars, rettv);
2480 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02002481
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002482 /*
2483 * The function call (or "FuncUndefined" autocommand sequence) might
2484 * have been aborted by an error, an interrupt, or an explicitly thrown
2485 * exception that has not been caught so far. This situation can be
2486 * tested for by calling aborting(). For an error in an internal
2487 * function or for the "E132" error in call_user_func(), however, the
2488 * throw point at which the "force_abort" flag (temporarily reset by
2489 * emsg()) is normally updated has not been reached yet. We need to
2490 * update that flag first to make aborting() reliable.
2491 */
2492 update_force_abort();
2493 }
Bram Moolenaaref140542019-12-31 21:27:13 +01002494 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002495 ret = OK;
2496
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002497theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002498 /*
2499 * Report an error unless the argument evaluation or function call has been
2500 * cancelled due to an aborting error, an interrupt, or an exception.
2501 */
2502 if (!aborting())
2503 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002504 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002505 }
2506
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002507 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002508 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002509 clear_tv(&argv[--argv_clear + argv_base]);
2510
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002511 vim_free(tofree);
2512 vim_free(name);
2513
2514 return ret;
2515}
2516
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002517 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518printable_func_name(ufunc_T *fp)
2519{
2520 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
2521}
2522
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002523/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002524 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002525 */
2526 static void
2527list_func_head(ufunc_T *fp, int indent)
2528{
2529 int j;
2530
2531 msg_start();
2532 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002533 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002534 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002535 msg_puts("def ");
2536 else
2537 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002539 msg_putchar('(');
2540 for (j = 0; j < fp->uf_args.ga_len; ++j)
2541 {
2542 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002543 msg_puts(", ");
2544 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 if (fp->uf_arg_types != NULL)
2546 {
2547 char *tofree;
2548
2549 msg_puts(": ");
2550 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
2551 vim_free(tofree);
2552 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002553 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
2554 {
2555 msg_puts(" = ");
2556 msg_puts(((char **)(fp->uf_def_args.ga_data))
2557 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
2558 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002559 }
2560 if (fp->uf_varargs)
2561 {
2562 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002563 msg_puts(", ");
2564 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002565 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566 if (fp->uf_va_name != NULL)
2567 {
2568 if (j)
2569 msg_puts(", ");
2570 msg_puts("...");
2571 msg_puts((char *)fp->uf_va_name);
2572 if (fp->uf_va_type)
2573 {
2574 char *tofree;
2575
2576 msg_puts(": ");
2577 msg_puts(type_name(fp->uf_va_type, &tofree));
2578 vim_free(tofree);
2579 }
2580 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002581 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002582
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002583 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002584 {
2585 if (fp->uf_ret_type != &t_void)
2586 {
2587 char *tofree;
2588
2589 msg_puts(": ");
2590 msg_puts(type_name(fp->uf_ret_type, &tofree));
2591 vim_free(tofree);
2592 }
2593 }
2594 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002595 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002596 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002597 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002598 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002599 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002600 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002601 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002602 msg_clr_eos();
2603 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002604 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002605}
2606
2607/*
2608 * Get a function name, translating "<SID>" and "<SNR>".
2609 * Also handles a Funcref in a List or Dictionary.
2610 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002611 * Set "*is_global" to TRUE when the function must be global, unless
2612 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002613 * flags:
2614 * TFN_INT: internal function name OK
2615 * TFN_QUIET: be quiet
2616 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002617 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002618 * Advances "pp" to just after the function name (if no error).
2619 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002620 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002621trans_function_name(
2622 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002623 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002624 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002625 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002626 funcdict_T *fdp, // return: info about dictionary used
2627 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002628{
2629 char_u *name = NULL;
2630 char_u *start;
2631 char_u *end;
2632 int lead;
2633 char_u sid_buf[20];
2634 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002636 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 int vim9script;
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002638 static char *e_function_name = N_("E129: Function name required");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002639
2640 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02002641 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002642 start = *pp;
2643
Bram Moolenaare38eab22019-12-05 21:50:01 +01002644 // Check for hard coded <SNR>: already translated function ID (from a user
2645 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002646 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2647 && (*pp)[2] == (int)KE_SNR)
2648 {
2649 *pp += 3;
2650 len = get_id_len(pp) + 3;
2651 return vim_strnsave(start, len);
2652 }
2653
Bram Moolenaare38eab22019-12-05 21:50:01 +01002654 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2655 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002656 lead = eval_fname_script(start);
2657 if (lead > 2)
2658 start += lead;
2659
Bram Moolenaare38eab22019-12-05 21:50:01 +01002660 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002661 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002662 lead > 2 ? 0 : FNE_CHECK_START);
2663 if (end == start)
2664 {
2665 if (!skip)
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002666 emsg(_(e_function_name));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002667 goto theend;
2668 }
2669 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2670 {
2671 /*
2672 * Report an invalid expression in braces, unless the expression
2673 * evaluation has been cancelled due to an aborting error, an
2674 * interrupt, or an exception.
2675 */
2676 if (!aborting())
2677 {
2678 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002679 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002680 }
2681 else
2682 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2683 goto theend;
2684 }
2685
2686 if (lv.ll_tv != NULL)
2687 {
2688 if (fdp != NULL)
2689 {
2690 fdp->fd_dict = lv.ll_dict;
2691 fdp->fd_newkey = lv.ll_newkey;
2692 lv.ll_newkey = NULL;
2693 fdp->fd_di = lv.ll_di;
2694 }
2695 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2696 {
2697 name = vim_strsave(lv.ll_tv->vval.v_string);
2698 *pp = end;
2699 }
2700 else if (lv.ll_tv->v_type == VAR_PARTIAL
2701 && lv.ll_tv->vval.v_partial != NULL)
2702 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002703 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002704 *pp = end;
2705 if (partial != NULL)
2706 *partial = lv.ll_tv->vval.v_partial;
2707 }
2708 else
2709 {
2710 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2711 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002712 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002713 else
2714 *pp = end;
2715 name = NULL;
2716 }
2717 goto theend;
2718 }
2719
2720 if (lv.ll_name == NULL)
2721 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002722 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002723 *pp = end;
2724 goto theend;
2725 }
2726
Bram Moolenaare38eab22019-12-05 21:50:01 +01002727 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002728 if (lv.ll_exp_name != NULL)
2729 {
2730 len = (int)STRLEN(lv.ll_exp_name);
2731 name = deref_func_name(lv.ll_exp_name, &len, partial,
2732 flags & TFN_NO_AUTOLOAD);
2733 if (name == lv.ll_exp_name)
2734 name = NULL;
2735 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002736 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002737 {
2738 len = (int)(end - *pp);
2739 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2740 if (name == *pp)
2741 name = NULL;
2742 }
2743 if (name != NULL)
2744 {
2745 name = vim_strsave(name);
2746 *pp = end;
2747 if (STRNCMP(name, "<SNR>", 5) == 0)
2748 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002749 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002750 name[0] = K_SPECIAL;
2751 name[1] = KS_EXTRA;
2752 name[2] = (int)KE_SNR;
2753 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2754 }
2755 goto theend;
2756 }
2757
2758 if (lv.ll_exp_name != NULL)
2759 {
2760 len = (int)STRLEN(lv.ll_exp_name);
2761 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2762 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2763 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002764 // When there was "s:" already or the name expanded to get a
2765 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002766 lv.ll_name += 2;
2767 len -= 2;
2768 lead = 2;
2769 }
2770 }
2771 else
2772 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002773 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002774 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002775 {
2776 if (is_global != NULL && lv.ll_name[0] == 'g')
2777 *is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002778 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002779 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002780 len = (int)(end - lv.ll_name);
2781 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002782 if (len <= 0)
2783 {
2784 if (!skip)
2785 emsg(_(e_function_name));
2786 goto theend;
2787 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002788
Bram Moolenaar17f700a2020-12-19 21:23:42 +01002789 // In Vim9 script a user function is script-local by default, unless it
2790 // starts with a lower case character: dict.func().
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002791 vim9script = ASCII_ISUPPER(*start) && in_vim9script();
Bram Moolenaar17f700a2020-12-19 21:23:42 +01002792 if (vim9script)
2793 {
2794 char_u *p;
2795
2796 // SomeScript#func() is a global function.
2797 for (p = start; *p != NUL && *p != '('; ++p)
2798 if (*p == AUTOLOAD_CHAR)
2799 vim9script = FALSE;
2800 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002802 /*
2803 * Copy the function name to allocated memory.
2804 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2805 * Accept <SNR>123_name() outside a script.
2806 */
2807 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002808 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002809 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002810 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 if (!vim9script)
2812 lead = 3;
2813 if (vim9script || (lv.ll_exp_name != NULL
2814 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002815 || eval_fname_sid(*pp))
2816 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002818 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002819 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002820 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002821 goto theend;
2822 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002823 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 if (vim9script)
2825 extra = 3 + (int)STRLEN(sid_buf);
2826 else
2827 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002828 }
2829 }
2830 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2831 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002832 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002833 start);
2834 goto theend;
2835 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002836 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002837 {
2838 char_u *cp = vim_strchr(lv.ll_name, ':');
2839
2840 if (cp != NULL && cp < end)
2841 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002842 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002843 goto theend;
2844 }
2845 }
2846
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002848 if (name != NULL)
2849 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002850 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002851 {
2852 name[0] = K_SPECIAL;
2853 name[1] = KS_EXTRA;
2854 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002856 STRCPY(name + 3, sid_buf);
2857 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2859 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002860 }
2861 *pp = end;
2862
2863theend:
2864 clear_lval(&lv);
2865 return name;
2866}
2867
2868/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02002869 * Assuming "name" is the result of trans_function_name() and it was prefixed
2870 * to use the script-local name, return the unmodified name (points into
2871 * "name"). Otherwise return NULL.
2872 * This can be used to first search for a script-local function and fall back
2873 * to the global function if not found.
2874 */
2875 char_u *
2876untrans_function_name(char_u *name)
2877{
2878 char_u *p;
2879
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002880 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02002881 {
2882 p = vim_strchr(name, '_');
2883 if (p != NULL)
2884 return p + 1;
2885 }
2886 return NULL;
2887}
2888
2889/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002890 * List functions. When "regmatch" is NULL all of then.
2891 * Otherwise functions matching "regmatch".
2892 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002893 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002894list_functions(regmatch_T *regmatch)
2895{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002896 int changed = func_hashtab.ht_changed;
2897 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002898 hashitem_T *hi;
2899
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002900 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002901 {
2902 if (!HASHITEM_EMPTY(hi))
2903 {
2904 ufunc_T *fp = HI2UF(hi);
2905
2906 --todo;
2907 if ((fp->uf_flags & FC_DEAD) == 0
2908 && (regmatch == NULL
2909 ? !message_filtered(fp->uf_name)
2910 && !func_name_refcount(fp->uf_name)
2911 : !isdigit(*fp->uf_name)
2912 && vim_regexec(regmatch, fp->uf_name, 0)))
2913 {
2914 list_func_head(fp, FALSE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002915 if (changed != func_hashtab.ht_changed)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002916 {
2917 emsg(_("E454: function list was modified"));
2918 return;
2919 }
2920 }
2921 }
2922 }
2923}
2924
2925/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02002926 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002927 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
2928 * the function name.
Bram Moolenaar04b12692020-05-04 23:24:44 +02002929 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002930 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02002931 ufunc_T *
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002932define_function(exarg_T *eap, char_u *name_arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002933{
2934 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002935 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002936 int j;
2937 int c;
2938 int saved_did_emsg;
2939 int saved_wait_return = need_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002940 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002941 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002942 char_u *p;
2943 char_u *arg;
2944 char_u *line_arg = NULL;
2945 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002947 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002948 garray_T newlines;
2949 int varargs = FALSE;
2950 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002952 ufunc_T *fp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002953 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002954 int indent;
2955 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956#define MAX_FUNC_NESTING 50
2957 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002958 dictitem_T *v;
2959 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002960 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002961 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002962 hashitem_T *hi;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002963 getline_opt_T getline_options;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002964 linenr_T sourcing_lnum_off;
2965 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002966 int is_heredoc = FALSE;
2967 char_u *skip_until = NULL;
2968 char_u *heredoc_trimmed = NULL;
Bram Moolenaare7e48382020-07-22 18:17:08 +02002969 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02002970 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002971
2972 /*
2973 * ":function" without argument: list functions.
2974 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002975 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002976 {
2977 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002978 list_functions(NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002979 eap->nextcmd = check_nextcmd(eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02002980 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002981 }
2982
2983 /*
2984 * ":function /pat": list functions matching pattern.
2985 */
2986 if (*eap->arg == '/')
2987 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002988 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002989 if (!eap->skip)
2990 {
2991 regmatch_T regmatch;
2992
2993 c = *p;
2994 *p = NUL;
2995 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2996 *p = c;
2997 if (regmatch.regprog != NULL)
2998 {
2999 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003000 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003001 vim_regfree(regmatch.regprog);
3002 }
3003 }
3004 if (*p == '/')
3005 ++p;
3006 eap->nextcmd = check_nextcmd(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003007 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003008 }
3009
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 ga_init(&newargs);
3011 ga_init(&argtypes);
3012 ga_init(&default_args);
3013
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003014 /*
3015 * Get the function name. There are these situations:
3016 * func normal function name
3017 * "name" == func, "fudi.fd_dict" == NULL
3018 * dict.func new dictionary entry
3019 * "name" == NULL, "fudi.fd_dict" set,
3020 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
3021 * dict.func existing dict entry with a Funcref
3022 * "name" == func, "fudi.fd_dict" set,
3023 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
3024 * dict.func existing dict entry that's not a Funcref
3025 * "name" == NULL, "fudi.fd_dict" set,
3026 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
3027 * s:func script-local function name
3028 * g:func global function name, same as "func"
3029 */
3030 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003031 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003032 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02003033 // nested function, argument is (args).
3034 paren = TRUE;
3035 CLEAR_FIELD(fudi);
3036 }
3037 else
3038 {
3039 name = trans_function_name(&p, &is_global, eap->skip,
3040 TFN_NO_AUTOLOAD, &fudi, NULL);
3041 paren = (vim_strchr(p, '(') != NULL);
3042 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003043 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02003044 /*
3045 * Return on an invalid expression in braces, unless the expression
3046 * evaluation has been cancelled due to an aborting error, an
3047 * interrupt, or an exception.
3048 */
3049 if (!aborting())
3050 {
3051 if (!eap->skip && fudi.fd_newkey != NULL)
3052 semsg(_(e_dictkey), fudi.fd_newkey);
3053 vim_free(fudi.fd_newkey);
3054 return NULL;
3055 }
3056 else
3057 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003058 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003059 }
3060
Bram Moolenaare38eab22019-12-05 21:50:01 +01003061 // An error in a function call during evaluation of an expression in magic
3062 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003063 saved_did_emsg = did_emsg;
3064 did_emsg = FALSE;
3065
3066 /*
3067 * ":function func" with only function name: list function.
3068 */
3069 if (!paren)
3070 {
3071 if (!ends_excmd(*skipwhite(p)))
3072 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003073 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003074 goto ret_free;
3075 }
3076 eap->nextcmd = check_nextcmd(p);
3077 if (eap->nextcmd != NULL)
3078 *p = NUL;
3079 if (!eap->skip && !got_int)
3080 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003081 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003082 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
3083 {
3084 char_u *up = untrans_function_name(name);
3085
3086 // With Vim9 script the name was made script-local, if not
3087 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02003088 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003089 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003090 }
3091
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003092 if (fp != NULL)
3093 {
3094 list_func_head(fp, TRUE);
3095 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
3096 {
3097 if (FUNCLINE(fp, j) == NULL)
3098 continue;
3099 msg_putchar('\n');
3100 msg_outnum((long)(j + 1));
3101 if (j < 9)
3102 msg_putchar(' ');
3103 if (j < 99)
3104 msg_putchar(' ');
3105 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003106 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003107 ui_breakcheck();
3108 }
3109 if (!got_int)
3110 {
3111 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003112 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 msg_puts(" enddef");
3114 else
3115 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003116 }
3117 }
3118 else
Bram Moolenaara26b9702020-04-18 19:53:28 +02003119 emsg_funcname(N_("E123: Undefined function: %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003120 }
3121 goto ret_free;
3122 }
3123
3124 /*
3125 * ":function name(arg1, arg2)" Define function.
3126 */
3127 p = skipwhite(p);
3128 if (*p != '(')
3129 {
3130 if (!eap->skip)
3131 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003132 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003133 goto ret_free;
3134 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003135 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003136 if (vim_strchr(p, '(') != NULL)
3137 p = vim_strchr(p, '(');
3138 }
3139 p = skipwhite(p + 1);
3140
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02003141 // In Vim9 script only global functions can be redefined.
3142 if (vim9script && eap->forceit && !is_global)
3143 {
3144 emsg(_(e_nobang));
3145 goto ret_free;
3146 }
3147
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003148 ga_init2(&newlines, (int)sizeof(char_u *), 3);
3149
Bram Moolenaar04b12692020-05-04 23:24:44 +02003150 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003151 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003152 // Check the name of the function. Unless it's a dictionary function
3153 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003154 if (name != NULL)
3155 arg = name;
3156 else
3157 arg = fudi.fd_newkey;
3158 if (arg != NULL && (fudi.fd_di == NULL
3159 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
3160 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
3161 {
3162 if (*arg == K_SPECIAL)
3163 j = 3;
3164 else
3165 j = 0;
3166 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
3167 : eval_isnamec(arg[j])))
3168 ++j;
3169 if (arg[j] != NUL)
3170 emsg_funcname((char *)e_invarg2, arg);
3171 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003172 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003173 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003174 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003175 }
3176
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003177 // This may get more lines and make the pointers into the first line
3178 // invalid.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003180 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003181 &varargs, &default_args, eap->skip,
3182 eap, &line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003183 goto errret_2;
3184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003186 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 // find the return type: :def Func(): type
3188 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003189 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02003191 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003193 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003194 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003196 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003198 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003199 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02003200 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003201 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003202 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02003203 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003204 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 else
3206 // find extra arguments "range", "dict", "abort" and "closure"
3207 for (;;)
3208 {
3209 p = skipwhite(p);
3210 if (STRNCMP(p, "range", 5) == 0)
3211 {
3212 flags |= FC_RANGE;
3213 p += 5;
3214 }
3215 else if (STRNCMP(p, "dict", 4) == 0)
3216 {
3217 flags |= FC_DICT;
3218 p += 4;
3219 }
3220 else if (STRNCMP(p, "abort", 5) == 0)
3221 {
3222 flags |= FC_ABORT;
3223 p += 5;
3224 }
3225 else if (STRNCMP(p, "closure", 7) == 0)
3226 {
3227 flags |= FC_CLOSURE;
3228 p += 7;
3229 if (current_funccal == NULL)
3230 {
3231 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
3232 name == NULL ? (char_u *)"" : name);
3233 goto erret;
3234 }
3235 }
3236 else
3237 break;
3238 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003239
Bram Moolenaare38eab22019-12-05 21:50:01 +01003240 // When there is a line break use what follows for the function body.
3241 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003242 if (*p == '\n')
3243 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003244 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02003245 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
3246 && eap->cmdidx != CMD_def)
Bram Moolenaare7e48382020-07-22 18:17:08 +02003247 && !(*p == '#' && (vim9script || eap->cmdidx == CMD_def))
3248 && !eap->skip
3249 && !did_emsg)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003250 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003251
3252 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
3254 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003255 */
3256 if (KeyTyped)
3257 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003258 // Check if the function already exists, don't let the user type the
3259 // whole function before telling him it doesn't work! For a script we
3260 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003261 if (!eap->skip && !eap->forceit)
3262 {
3263 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003264 emsg(_(e_funcdict));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003265 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003266 emsg_funcname(e_funcexts, name);
3267 }
3268
3269 if (!eap->skip && did_emsg)
3270 goto erret;
3271
Bram Moolenaare38eab22019-12-05 21:50:01 +01003272 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003273 cmdline_row = msg_row;
3274 }
3275
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003276 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003277 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003278
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02003279 // Detect having skipped over comment lines to find the return
3280 // type. Add NULL lines to keep the line count correct.
3281 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
3282 if (SOURCING_LNUM < sourcing_lnum_off)
3283 {
3284 sourcing_lnum_off -= SOURCING_LNUM;
3285 if (ga_grow(&newlines, sourcing_lnum_off) == FAIL)
3286 goto erret;
3287 while (sourcing_lnum_off-- > 0)
3288 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3289 }
3290
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003291 indent = 2;
3292 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01003294 getline_options = eap->cmdidx == CMD_def
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01003295 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003296 for (;;)
3297 {
3298 if (KeyTyped)
3299 {
3300 msg_scroll = TRUE;
3301 saved_wait_return = FALSE;
3302 }
3303 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003304
3305 if (line_arg != NULL)
3306 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003307 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003308 theline = line_arg;
3309 p = vim_strchr(theline, '\n');
3310 if (p == NULL)
3311 line_arg += STRLEN(line_arg);
3312 else
3313 {
3314 *p = NUL;
3315 line_arg = p + 1;
3316 }
3317 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003318 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02003319 {
3320 vim_free(line_to_free);
3321 if (eap->getline == NULL)
Bram Moolenaar66250c92020-08-20 15:02:42 +02003322 theline = getcmdline(':', 0L, indent, getline_options);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003323 else
Bram Moolenaar66250c92020-08-20 15:02:42 +02003324 theline = eap->getline(':', eap->cookie, indent,
3325 getline_options);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003326 line_to_free = theline;
3327 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003328 if (KeyTyped)
3329 lines_left = Rows - 1;
3330 if (theline == NULL)
3331 {
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003332 if (skip_until != NULL)
3333 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
3334 else if (eap->cmdidx == CMD_def)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003335 emsg(_(e_missing_enddef));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336 else
3337 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003338 goto erret;
3339 }
3340
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003341 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003342 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003343 if (SOURCING_LNUM < sourcing_lnum_off)
3344 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003345 else
3346 sourcing_lnum_off = 0;
3347
3348 if (skip_until != NULL)
3349 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003350 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003351 // * ":append" and "."
3352 // * ":python <<EOF" and "EOF"
3353 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
3354 if (heredoc_trimmed == NULL
3355 || (is_heredoc && skipwhite(theline) == theline)
3356 || STRNCMP(theline, heredoc_trimmed,
3357 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003358 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003359 if (heredoc_trimmed == NULL)
3360 p = theline;
3361 else if (is_heredoc)
3362 p = skipwhite(theline) == theline
3363 ? theline : theline + STRLEN(heredoc_trimmed);
3364 else
3365 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02003366 if (STRCMP(p, skip_until) == 0)
3367 {
3368 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003369 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01003370 getline_options = eap->cmdidx == CMD_def
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01003371 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003372 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003373 }
3374 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003375 }
3376 else
3377 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003378 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01003379 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003380 ;
3381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 // Check for "endfunction" or "enddef".
3383 if (checkforcmd(&p, nesting_def[nesting]
3384 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003385 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02003386 char_u *nextcmd = NULL;
3387
Bram Moolenaar663bb232017-06-22 19:12:10 +02003388 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02003389 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02003390 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02003391 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02003392 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393 give_warning2(eap->cmdidx == CMD_def
3394 ? (char_u *)_("W1001: Text found after :enddef: %s")
3395 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003396 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003397 if (nextcmd != NULL)
3398 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003399 // Another command follows. If the line came from "eap" we
3400 // can simply point into it, otherwise we need to change
3401 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02003402 eap->nextcmd = nextcmd;
3403 if (line_to_free != NULL)
3404 {
3405 vim_free(*eap->cmdlinep);
3406 *eap->cmdlinep = line_to_free;
3407 line_to_free = NULL;
3408 }
3409 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003410 break;
3411 }
3412
Bram Moolenaare38eab22019-12-05 21:50:01 +01003413 // Increase indent inside "if", "while", "for" and "try", decrease
3414 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003416 indent -= 2;
3417 else if (STRNCMP(p, "if", 2) == 0
3418 || STRNCMP(p, "wh", 2) == 0
3419 || STRNCMP(p, "for", 3) == 0
3420 || STRNCMP(p, "try", 3) == 0)
3421 indent += 2;
3422
Bram Moolenaare38eab22019-12-05 21:50:01 +01003423 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01003424 // Only recognize "def" inside "def", not inside "function",
3425 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 c = *p;
Bram Moolenaar673660a2020-01-26 16:50:05 +01003427 if (checkforcmd(&p, "function", 2)
3428 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003429 {
3430 if (*p == '!')
3431 p = skipwhite(p + 1);
3432 p += eval_fname_script(p);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003433 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL, NULL));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003434 if (*skipwhite(p) == '(')
3435 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 if (nesting == MAX_FUNC_NESTING - 1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003437 emsg(_(e_function_nesting_too_deep));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438 else
3439 {
3440 ++nesting;
3441 nesting_def[nesting] = (c == 'd');
3442 indent += 2;
3443 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003444 }
3445 }
3446
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003447 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003448 p = skip_range(p, FALSE, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003449 if (eap->cmdidx != CMD_def
3450 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01003451 || (p[0] == 'c'
3452 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
3453 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
3454 && (STRNCMP(&p[3], "nge", 3) != 0
3455 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003456 || (p[0] == 'i'
3457 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003458 && (!ASCII_ISALPHA(p[2])
3459 || (p[2] == 's'
3460 && (!ASCII_ISALPHA(p[3])
3461 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003462 skip_until = vim_strsave((char_u *)".");
3463
Bram Moolenaare38eab22019-12-05 21:50:01 +01003464 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003465 arg = skipwhite(skiptowhite(p));
3466 if (arg[0] == '<' && arg[1] =='<'
3467 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003468 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
3469 || ((p[2] == '3' || p[2] == 'x')
3470 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003471 || (p[0] == 'p' && p[1] == 'e'
3472 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
3473 || (p[0] == 't' && p[1] == 'c'
3474 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
3475 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
3476 && !ASCII_ISALPHA(p[3]))
3477 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
3478 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
3479 || (p[0] == 'm' && p[1] == 'z'
3480 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
3481 ))
3482 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003483 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003484 p = skipwhite(arg + 2);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003485 if (STRNCMP(p, "trim", 4) == 0)
3486 {
3487 // Ignore leading white space.
3488 p = skipwhite(p + 4);
3489 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003490 skipwhite(theline) - theline);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003491 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003492 if (*p == NUL)
3493 skip_until = vim_strsave((char_u *)".");
3494 else
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003495 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003496 getline_options = GETLINE_NONE;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003497 is_heredoc = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003498 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003499
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003500 // Check for ":cmd v =<< [trim] EOF"
3501 // and ":cmd [a, b] =<< [trim] EOF"
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003502 // and "lines =<< [trim] EOF" for Vim9
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003503 // Where "cmd" can be "let", "var", "final" or "const".
Bram Moolenaar8471e572019-05-19 21:37:18 +02003504 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003505 if (*arg == '[')
3506 arg = vim_strchr(arg, ']');
3507 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003508 {
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003509 int found = (eap->cmdidx == CMD_def && arg[0] == '='
3510 && arg[1] == '<' && arg[2] =='<');
3511
3512 if (!found)
3513 // skip over the argument after "cmd"
3514 arg = skipwhite(skiptowhite(arg));
3515 if (found || (arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003516 && (checkforcmd(&p, "let", 2)
3517 || checkforcmd(&p, "var", 3)
3518 || checkforcmd(&p, "final", 5)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003519 || checkforcmd(&p, "const", 5))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02003520 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003521 p = skipwhite(arg + 3);
3522 if (STRNCMP(p, "trim", 4) == 0)
3523 {
3524 // Ignore leading white space.
3525 p = skipwhite(p + 4);
3526 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003527 skipwhite(theline) - theline);
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003528 }
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003529 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003530 getline_options = GETLINE_NONE;
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003531 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003532 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003533 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003534 }
3535
Bram Moolenaare38eab22019-12-05 21:50:01 +01003536 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003537 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003538 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003539
Bram Moolenaare38eab22019-12-05 21:50:01 +01003540 // Copy the line to newly allocated memory. get_one_sourceline()
3541 // allocates 250 bytes per line, this saves 80% on average. The cost
3542 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003543 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003544 if (p == NULL)
3545 goto erret;
3546 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003547
Bram Moolenaare38eab22019-12-05 21:50:01 +01003548 // Add NULL lines for continuation lines, so that the line count is
3549 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003550 while (sourcing_lnum_off-- > 0)
3551 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3552
Bram Moolenaare38eab22019-12-05 21:50:01 +01003553 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003554 if (line_arg != NULL && *line_arg == NUL)
3555 line_arg = NULL;
3556 }
3557
Bram Moolenaare38eab22019-12-05 21:50:01 +01003558 // Don't define the function when skipping commands or when an error was
3559 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003560 if (eap->skip || did_emsg)
3561 goto erret;
3562
3563 /*
3564 * If there are no errors, add the function
3565 */
3566 if (fudi.fd_dict == NULL)
3567 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568 hashtab_T *ht;
3569
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003570 v = find_var(name, &ht, FALSE);
3571 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
3572 {
3573 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
3574 name);
3575 goto erret;
3576 }
3577
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003578 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaareef21022020-08-01 22:16:43 +02003579 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003580 {
Bram Moolenaareef21022020-08-01 22:16:43 +02003581 char_u *uname = untrans_function_name(name);
3582
3583 import = find_imported(uname == NULL ? name : uname, 0, NULL);
3584 }
3585
3586 if (fp != NULL || import != NULL)
3587 {
3588 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003590 // Function can be replaced with "function!" and when sourcing the
3591 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02003592 // A name that is used by an import can not be overruled.
3593 if (import != NULL
3594 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003595 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02003596 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003597 {
Bram Moolenaareef21022020-08-01 22:16:43 +02003598 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003599 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02003600 else
3601 emsg_funcname(e_funcexts, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003602 goto erret;
3603 }
3604 if (fp->uf_calls > 0)
3605 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003606 emsg_funcname(
3607 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003608 name);
3609 goto erret;
3610 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003611 if (fp->uf_refcount > 1)
3612 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003613 // This function is referenced somewhere, don't redefine it but
3614 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003615 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003616 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003617 fp = NULL;
3618 overwrite = TRUE;
3619 }
3620 else
3621 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003622 char_u *exp_name = fp->uf_name_exp;
3623
3624 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01003625 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003626 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003627 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003628 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003630#ifdef FEAT_PROFILE
3631 fp->uf_profiling = FALSE;
3632 fp->uf_prof_initialized = FALSE;
3633#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01003634 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003635 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003636 }
3637 }
3638 else
3639 {
3640 char numbuf[20];
3641
3642 fp = NULL;
3643 if (fudi.fd_newkey == NULL && !eap->forceit)
3644 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003645 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003646 goto erret;
3647 }
3648 if (fudi.fd_di == NULL)
3649 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003650 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02003651 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003652 goto erret;
3653 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003654 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02003655 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003656 goto erret;
3657
Bram Moolenaare38eab22019-12-05 21:50:01 +01003658 // Give the function a sequential number. Can only be used with a
3659 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003660 vim_free(name);
3661 sprintf(numbuf, "%d", ++func_nr);
3662 name = vim_strsave((char_u *)numbuf);
3663 if (name == NULL)
3664 goto erret;
3665 }
3666
3667 if (fp == NULL)
3668 {
3669 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3670 {
3671 int slen, plen;
3672 char_u *scriptname;
3673
Bram Moolenaare38eab22019-12-05 21:50:01 +01003674 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003675 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003676 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003677 {
3678 scriptname = autoload_name(name);
3679 if (scriptname != NULL)
3680 {
3681 p = vim_strchr(scriptname, '/');
3682 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003683 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003684 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003685 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003686 j = OK;
3687 vim_free(scriptname);
3688 }
3689 }
3690 if (j == FAIL)
3691 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003692 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003693 goto erret;
3694 }
3695 }
3696
Bram Moolenaar47ed5532019-08-08 20:49:14 +02003697 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003698 if (fp == NULL)
3699 goto erret;
3700
3701 if (fudi.fd_dict != NULL)
3702 {
3703 if (fudi.fd_di == NULL)
3704 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003705 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003706 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
3707 if (fudi.fd_di == NULL)
3708 {
3709 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003710 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003711 goto erret;
3712 }
3713 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
3714 {
3715 vim_free(fudi.fd_di);
3716 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003717 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003718 goto erret;
3719 }
3720 }
3721 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003722 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003723 clear_tv(&fudi.fd_di->di_tv);
3724 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003725 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003726
Bram Moolenaare38eab22019-12-05 21:50:01 +01003727 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003728 flags |= FC_DICT;
3729 }
3730
Bram Moolenaare38eab22019-12-05 21:50:01 +01003731 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003732 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003733 if (overwrite)
3734 {
3735 hi = hash_find(&func_hashtab, name);
3736 hi->hi_key = UF2HIKEY(fp);
3737 }
3738 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003739 {
3740 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003741 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003742 goto erret;
3743 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003744 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003745 }
3746 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003747 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003749 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003750
3751 if (eap->cmdidx == CMD_def)
3752 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003753 int lnum_save = SOURCING_LNUM;
3754 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003755
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003756 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003757
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003758 // error messages are for the first function line
3759 SOURCING_LNUM = sourcing_lnum_top;
3760
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003761 if (cstack != NULL && cstack->cs_idx >= 0)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003762 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003763 int count = cstack->cs_idx + 1;
3764 int i;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003765
3766 // The block context may be needed for script variables declared in
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003767 // a block that is visible now but not when the function is called
3768 // later.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003769 fp->uf_block_ids = ALLOC_MULT(int, count);
3770 if (fp->uf_block_ids != NULL)
3771 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003772 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003773 sizeof(int) * count);
3774 fp->uf_block_depth = count;
3775 }
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003776
3777 // Set flag in each block to indicate a function was defined. This
3778 // is used to keep the variable when leaving the block, see
3779 // hide_script_var().
3780 for (i = 0; i <= cstack->cs_idx; ++i)
3781 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003782 }
3783
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003784 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003786 SOURCING_LNUM = lnum_save;
3787 goto errret_2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003789 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790
3791 // parse the return type, if any
3792 if (ret_type == NULL)
3793 fp->uf_ret_type = &t_void;
3794 else
3795 {
3796 p = ret_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01003797 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02003799 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003801 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003802 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003804 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003805 if ((flags & FC_CLOSURE) != 0)
3806 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003807 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003808 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003809 }
3810 else
3811 fp->uf_scoped = NULL;
3812
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003813#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003814 if (prof_def_func())
3815 func_do_profile(fp);
3816#endif
3817 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003818 if (sandbox)
3819 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003820 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003821 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003822 fp->uf_flags = flags;
3823 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003824 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003825 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02003826 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003827 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 if (is_export)
3829 {
3830 fp->uf_flags |= FC_EXPORT;
3831 // let ex_export() know the export worked.
3832 is_export = FALSE;
3833 }
3834
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003835 if (eap->cmdidx == CMD_def)
3836 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02003837 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
3838 // :func does not use Vim9 script syntax, even in a Vim9 script file
3839 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003840
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003841 goto ret_free;
3842
3843erret:
3844 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003845 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003846errret_2:
3847 ga_clear_strings(&newlines);
3848ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003849 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003850 vim_free(skip_until);
Bram Moolenaarfffdf472020-12-13 21:16:55 +01003851 vim_free(heredoc_trimmed);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003852 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003853 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003854 if (name != name_arg)
3855 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003856 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003857 did_emsg |= saved_did_emsg;
3858 need_wait_return |= saved_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003859
3860 return fp;
3861}
3862
3863/*
3864 * ":function"
3865 */
3866 void
3867ex_function(exarg_T *eap)
3868{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003869 (void)define_function(eap, NULL);
Bram Moolenaar822ba242020-05-24 23:00:18 +02003870}
3871
3872/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02003873 * :defcompile - compile all :def functions in the current script that need to
3874 * be compiled. Except dead functions.
Bram Moolenaar822ba242020-05-24 23:00:18 +02003875 */
3876 void
3877ex_defcompile(exarg_T *eap UNUSED)
3878{
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003879 long todo = (long)func_hashtab.ht_used;
3880 int changed = func_hashtab.ht_changed;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003881 hashitem_T *hi;
3882 ufunc_T *ufunc;
3883
3884 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
3885 {
3886 if (!HASHITEM_EMPTY(hi))
3887 {
3888 --todo;
3889 ufunc = HI2UF(hi);
3890 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
Bram Moolenaar96f8f492020-09-09 17:08:51 +02003891 && ufunc->uf_def_status == UF_TO_BE_COMPILED
3892 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003893 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02003894 compile_def_function(ufunc, FALSE, NULL);
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003895
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003896 if (func_hashtab.ht_changed != changed)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003897 {
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003898 // a function has been added or removed, need to start over
3899 todo = (long)func_hashtab.ht_used;
3900 changed = func_hashtab.ht_changed;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003901 hi = func_hashtab.ht_array;
Bram Moolenaar285b1892020-05-26 11:37:26 +02003902 --hi;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003903 }
3904 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003905 }
3906 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003907}
3908
3909/*
3910 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3911 * Return 2 if "p" starts with "s:".
3912 * Return 0 otherwise.
3913 */
3914 int
3915eval_fname_script(char_u *p)
3916{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003917 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3918 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003919 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3920 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3921 return 5;
3922 if (p[0] == 's' && p[1] == ':')
3923 return 2;
3924 return 0;
3925}
3926
3927 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003928translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003929{
3930 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003931 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003932 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933}
3934
3935/*
3936 * Return TRUE when "ufunc" has old-style "..." varargs
3937 * or named varargs "...name: type".
3938 */
3939 int
3940has_varargs(ufunc_T *ufunc)
3941{
3942 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003943}
3944
3945/*
3946 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003947 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003948 */
3949 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003950function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003951{
3952 char_u *nm = name;
3953 char_u *p;
3954 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003955 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003956 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003957
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003958 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3959 if (no_deref)
3960 flag |= TFN_NO_DEREF;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003961 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003962 nm = skipwhite(nm);
3963
Bram Moolenaare38eab22019-12-05 21:50:01 +01003964 // Only accept "funcname", "funcname ", "funcname (..." and
3965 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003966 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003967 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003968 vim_free(p);
3969 return n;
3970}
3971
Bram Moolenaar113e1072019-01-20 15:30:40 +01003972#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003973 char_u *
3974get_expanded_name(char_u *name, int check)
3975{
3976 char_u *nm = name;
3977 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003978 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003979
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003980 p = trans_function_name(&nm, &is_global, FALSE,
3981 TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003982
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003983 if (p != NULL && *nm == NUL
3984 && (!check || translated_function_exists(p, is_global)))
3985 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003986
3987 vim_free(p);
3988 return NULL;
3989}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003990#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003991
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003992/*
3993 * Function given to ExpandGeneric() to obtain the list of user defined
3994 * function names.
3995 */
3996 char_u *
3997get_user_func_name(expand_T *xp, int idx)
3998{
3999 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004000 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004001 static hashitem_T *hi;
4002 ufunc_T *fp;
4003
4004 if (idx == 0)
4005 {
4006 done = 0;
4007 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004008 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004009 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004010 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004011 {
4012 if (done++ > 0)
4013 ++hi;
4014 while (HASHITEM_EMPTY(hi))
4015 ++hi;
4016 fp = HI2UF(hi);
4017
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018 // don't show dead, dict and lambda functions
4019 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02004020 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004022
4023 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004024 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004025
4026 cat_func_name(IObuff, fp);
4027 if (xp->xp_context != EXPAND_USER_FUNC)
4028 {
4029 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004031 STRCAT(IObuff, ")");
4032 }
4033 return IObuff;
4034 }
4035 return NULL;
4036}
4037
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004038/*
4039 * ":delfunction {name}"
4040 */
4041 void
4042ex_delfunction(exarg_T *eap)
4043{
4044 ufunc_T *fp = NULL;
4045 char_u *p;
4046 char_u *name;
4047 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004048 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004049
4050 p = eap->arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004051 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004052 vim_free(fudi.fd_newkey);
4053 if (name == NULL)
4054 {
4055 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004056 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004057 return;
4058 }
4059 if (!ends_excmd(*skipwhite(p)))
4060 {
4061 vim_free(name);
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004062 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004063 return;
4064 }
4065 eap->nextcmd = check_nextcmd(p);
4066 if (eap->nextcmd != NULL)
4067 *p = NUL;
4068
4069 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004070 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004071 vim_free(name);
4072
4073 if (!eap->skip)
4074 {
4075 if (fp == NULL)
4076 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02004077 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004078 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004079 return;
4080 }
4081 if (fp->uf_calls > 0)
4082 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004083 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004084 return;
4085 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004086 if (fp->uf_flags & FC_VIM9)
4087 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004088 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004089 return;
4090 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004091
4092 if (fudi.fd_dict != NULL)
4093 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004094 // Delete the dict item that refers to the function, it will
4095 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004096 dictitem_remove(fudi.fd_dict, fudi.fd_di);
4097 }
4098 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004099 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004100 // A normal function (not a numbered function or lambda) has a
4101 // refcount of 1 for the entry in the hashtable. When deleting
4102 // it and the refcount is more than one, it should be kept.
4103 // A numbered function and lambda should be kept if the refcount is
4104 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004105 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004106 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004107 // Function is still referenced somewhere. Don't free it but
4108 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004109 if (func_remove(fp))
4110 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004111 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004112 }
4113 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004114 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004115 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004116 }
4117}
4118
4119/*
4120 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004121 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004122 */
4123 void
4124func_unref(char_u *name)
4125{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004126 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004127
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004128 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004129 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004130 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004131 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004132 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004133#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004134 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004135#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01004136 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004137 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004138 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004139 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004140 // Only delete it when it's not being used. Otherwise it's done
4141 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004142 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004143 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02004144 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004145}
4146
4147/*
4148 * Unreference a Function: decrement the reference count and free it when it
4149 * becomes zero.
4150 */
4151 void
4152func_ptr_unref(ufunc_T *fp)
4153{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004154 if (fp != NULL && --fp->uf_refcount <= 0)
4155 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004156 // Only delete it when it's not being used. Otherwise it's done
4157 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02004158 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004159 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004160 }
4161}
4162
4163/*
4164 * Count a reference to a Function.
4165 */
4166 void
4167func_ref(char_u *name)
4168{
4169 ufunc_T *fp;
4170
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004171 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004172 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004173 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004174 if (fp != NULL)
4175 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004176 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01004177 // Only give an error for a numbered function.
4178 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01004179 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004180}
4181
4182/*
4183 * Count a reference to a Function.
4184 */
4185 void
4186func_ptr_ref(ufunc_T *fp)
4187{
4188 if (fp != NULL)
4189 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004190}
4191
4192/*
4193 * Return TRUE if items in "fc" do not have "copyID". That means they are not
4194 * referenced from anywhere that is in use.
4195 */
4196 static int
4197can_free_funccal(funccall_T *fc, int copyID)
4198{
4199 return (fc->l_varlist.lv_copyID != copyID
4200 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004201 && fc->l_avars.dv_copyID != copyID
4202 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004203}
4204
4205/*
4206 * ":return [expr]"
4207 */
4208 void
4209ex_return(exarg_T *eap)
4210{
4211 char_u *arg = eap->arg;
4212 typval_T rettv;
4213 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004214 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004215
4216 if (current_funccal == NULL)
4217 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004218 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004219 return;
4220 }
4221
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004222 CLEAR_FIELD(evalarg);
4223 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
4224
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004225 if (eap->skip)
4226 ++emsg_skip;
4227
4228 eap->nextcmd = NULL;
4229 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004230 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004231 {
4232 if (!eap->skip)
4233 returning = do_return(eap, FALSE, TRUE, &rettv);
4234 else
4235 clear_tv(&rettv);
4236 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004237 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004238 else if (!eap->skip)
4239 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004240 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01004241 update_force_abort();
4242
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004243 /*
4244 * Return unless the expression evaluation has been cancelled due to an
4245 * aborting error, an interrupt, or an exception.
4246 */
4247 if (!aborting())
4248 returning = do_return(eap, FALSE, TRUE, NULL);
4249 }
4250
Bram Moolenaare38eab22019-12-05 21:50:01 +01004251 // When skipping or the return gets pending, advance to the next command
4252 // in this line (!returning). Otherwise, ignore the rest of the line.
4253 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004254 if (returning)
4255 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004256 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004257 eap->nextcmd = check_nextcmd(arg);
4258
4259 if (eap->skip)
4260 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02004261 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004262}
4263
4264/*
4265 * ":1,25call func(arg1, arg2)" function call.
4266 */
4267 void
4268ex_call(exarg_T *eap)
4269{
4270 char_u *arg = eap->arg;
4271 char_u *startarg;
4272 char_u *name;
4273 char_u *tofree;
4274 int len;
4275 typval_T rettv;
4276 linenr_T lnum;
4277 int doesrange;
4278 int failed = FALSE;
4279 funcdict_T fudi;
4280 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004281 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004282
Bram Moolenaare6b53242020-07-01 17:28:33 +02004283 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004284 if (eap->skip)
4285 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004286 // trans_function_name() doesn't work well when skipping, use eval0()
4287 // instead to skip to any following command, e.g. for:
4288 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004289 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004290 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004291 clear_tv(&rettv);
4292 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004293 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004294 return;
4295 }
4296
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004297 tofree = trans_function_name(&arg, NULL, eap->skip,
4298 TFN_INT, &fudi, &partial);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004299 if (fudi.fd_newkey != NULL)
4300 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004301 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004302 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004303 vim_free(fudi.fd_newkey);
4304 }
4305 if (tofree == NULL)
4306 return;
4307
Bram Moolenaare38eab22019-12-05 21:50:01 +01004308 // Increase refcount on dictionary, it could get deleted when evaluating
4309 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004310 if (fudi.fd_dict != NULL)
4311 ++fudi.fd_dict->dv_refcount;
4312
Bram Moolenaare38eab22019-12-05 21:50:01 +01004313 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
4314 // contents. For VAR_PARTIAL get its partial, unless we already have one
4315 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004316 len = (int)STRLEN(tofree);
4317 name = deref_func_name(tofree, &len,
4318 partial != NULL ? NULL : &partial, FALSE);
4319
Bram Moolenaare38eab22019-12-05 21:50:01 +01004320 // Skip white space to allow ":call func ()". Not good, but required for
4321 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004322 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004323 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004324
4325 if (*startarg != '(')
4326 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004328 goto end;
4329 }
4330
4331 /*
4332 * When skipping, evaluate the function once, to find the end of the
4333 * arguments.
4334 * When the function takes a range, this is discovered after the first
4335 * call, and the loop is broken.
4336 */
4337 if (eap->skip)
4338 {
4339 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004340 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004341 }
4342 else
4343 lnum = eap->line1;
4344 for ( ; lnum <= eap->line2; ++lnum)
4345 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004346 funcexe_T funcexe;
4347
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004348 if (!eap->skip && eap->addr_count > 0)
4349 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004350 if (lnum > curbuf->b_ml.ml_line_count)
4351 {
4352 // If the function deleted lines or switched to another buffer
4353 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004354 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004355 break;
4356 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004357 curwin->w_cursor.lnum = lnum;
4358 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004359 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004360 }
4361 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004362
Bram Moolenaara80faa82020-04-12 19:37:17 +02004363 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004364 funcexe.firstline = eap->line1;
4365 funcexe.lastline = eap->line2;
4366 funcexe.doesrange = &doesrange;
4367 funcexe.evaluate = !eap->skip;
4368 funcexe.partial = partial;
4369 funcexe.selfdict = fudi.fd_dict;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004370 if (get_func_tv(name, -1, &rettv, &arg, &evalarg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004371 {
4372 failed = TRUE;
4373 break;
4374 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01004375 if (has_watchexpr())
4376 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004377
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004378 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004379 if (handle_subscript(&arg, &rettv,
4380 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004381 {
4382 failed = TRUE;
4383 break;
4384 }
4385
4386 clear_tv(&rettv);
4387 if (doesrange || eap->skip)
4388 break;
4389
Bram Moolenaare38eab22019-12-05 21:50:01 +01004390 // Stop when immediately aborting on error, or when an interrupt
4391 // occurred or an exception was thrown but not caught.
4392 // get_func_tv() returned OK, so that the check for trailing
4393 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004394 if (aborting())
4395 break;
4396 }
4397 if (eap->skip)
4398 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004399 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004400
Bram Moolenaare51bb172020-02-16 19:42:23 +01004401 // When inside :try we need to check for following "| catch".
4402 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004403 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004404 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02004405 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004406 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004407 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004408 if (!failed)
4409 {
4410 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004411 semsg(_(e_trailing_arg), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004412 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004413 }
4414 else
4415 eap->nextcmd = check_nextcmd(arg);
4416 }
4417
4418end:
4419 dict_unref(fudi.fd_dict);
4420 vim_free(tofree);
4421}
4422
4423/*
4424 * Return from a function. Possibly makes the return pending. Also called
4425 * for a pending return at the ":endtry" or after returning from an extra
4426 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
4427 * when called due to a ":return" command. "rettv" may point to a typval_T
4428 * with the return rettv. Returns TRUE when the return can be carried out,
4429 * FALSE when the return gets pending.
4430 */
4431 int
4432do_return(
4433 exarg_T *eap,
4434 int reanimate,
4435 int is_cmd,
4436 void *rettv)
4437{
4438 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01004439 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004440
4441 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004442 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004443 current_funccal->returned = FALSE;
4444
4445 /*
4446 * Cleanup (and inactivate) conditionals, but stop when a try conditional
4447 * not in its finally clause (which then is to be executed next) is found.
4448 * In this case, make the ":return" pending for execution at the ":endtry".
4449 * Otherwise, return normally.
4450 */
4451 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
4452 if (idx >= 0)
4453 {
4454 cstack->cs_pending[idx] = CSTP_RETURN;
4455
4456 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004457 // A pending return again gets pending. "rettv" points to an
4458 // allocated variable with the rettv of the original ":return"'s
4459 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004460 cstack->cs_rettv[idx] = rettv;
4461 else
4462 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004463 // When undoing a return in order to make it pending, get the stored
4464 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004465 if (reanimate)
4466 rettv = current_funccal->rettv;
4467
4468 if (rettv != NULL)
4469 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004470 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004471 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
4472 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
4473 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004474 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004475 }
4476 else
4477 cstack->cs_rettv[idx] = NULL;
4478
4479 if (reanimate)
4480 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004481 // The pending return value could be overwritten by a ":return"
4482 // without argument in a finally clause; reset the default
4483 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004484 current_funccal->rettv->v_type = VAR_NUMBER;
4485 current_funccal->rettv->vval.v_number = 0;
4486 }
4487 }
4488 report_make_pending(CSTP_RETURN, rettv);
4489 }
4490 else
4491 {
4492 current_funccal->returned = TRUE;
4493
Bram Moolenaare38eab22019-12-05 21:50:01 +01004494 // If the return is carried out now, store the return value. For
4495 // a return immediately after reanimation, the value is already
4496 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004497 if (!reanimate && rettv != NULL)
4498 {
4499 clear_tv(current_funccal->rettv);
4500 *current_funccal->rettv = *(typval_T *)rettv;
4501 if (!is_cmd)
4502 vim_free(rettv);
4503 }
4504 }
4505
4506 return idx < 0;
4507}
4508
4509/*
4510 * Free the variable with a pending return value.
4511 */
4512 void
4513discard_pending_return(void *rettv)
4514{
4515 free_tv((typval_T *)rettv);
4516}
4517
4518/*
4519 * Generate a return command for producing the value of "rettv". The result
4520 * is an allocated string. Used by report_pending() for verbose messages.
4521 */
4522 char_u *
4523get_return_cmd(void *rettv)
4524{
4525 char_u *s = NULL;
4526 char_u *tofree = NULL;
4527 char_u numbuf[NUMBUFLEN];
4528
4529 if (rettv != NULL)
4530 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
4531 if (s == NULL)
4532 s = (char_u *)"";
4533
4534 STRCPY(IObuff, ":return ");
4535 STRNCPY(IObuff + 8, s, IOSIZE - 8);
4536 if (STRLEN(s) + 8 >= IOSIZE)
4537 STRCPY(IObuff + IOSIZE - 4, "...");
4538 vim_free(tofree);
4539 return vim_strsave(IObuff);
4540}
4541
4542/*
4543 * Get next function line.
4544 * Called by do_cmdline() to get the next line.
4545 * Returns allocated string, or NULL for end of function.
4546 */
4547 char_u *
4548get_func_line(
4549 int c UNUSED,
4550 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02004551 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004552 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004553{
4554 funccall_T *fcp = (funccall_T *)cookie;
4555 ufunc_T *fp = fcp->func;
4556 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004557 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004558
Bram Moolenaare38eab22019-12-05 21:50:01 +01004559 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004560 if (fcp->dbg_tick != debug_tick)
4561 {
4562 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004563 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004564 fcp->dbg_tick = debug_tick;
4565 }
4566#ifdef FEAT_PROFILE
4567 if (do_profiling == PROF_YES)
4568 func_line_end(cookie);
4569#endif
4570
4571 gap = &fp->uf_lines;
4572 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4573 || fcp->returned)
4574 retval = NULL;
4575 else
4576 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004577 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004578 while (fcp->linenr < gap->ga_len
4579 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
4580 ++fcp->linenr;
4581 if (fcp->linenr >= gap->ga_len)
4582 retval = NULL;
4583 else
4584 {
4585 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004586 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004587#ifdef FEAT_PROFILE
4588 if (do_profiling == PROF_YES)
4589 func_line_start(cookie);
4590#endif
4591 }
4592 }
4593
Bram Moolenaare38eab22019-12-05 21:50:01 +01004594 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004595 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004596 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004597 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004598 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004599 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004600 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004601 fcp->dbg_tick = debug_tick;
4602 }
4603
4604 return retval;
4605}
4606
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004607/*
4608 * Return TRUE if the currently active function should be ended, because a
4609 * return was encountered or an error occurred. Used inside a ":while".
4610 */
4611 int
4612func_has_ended(void *cookie)
4613{
4614 funccall_T *fcp = (funccall_T *)cookie;
4615
Bram Moolenaare38eab22019-12-05 21:50:01 +01004616 // Ignore the "abort" flag if the abortion behavior has been changed due to
4617 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004618 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4619 || fcp->returned);
4620}
4621
4622/*
4623 * return TRUE if cookie indicates a function which "abort"s on errors.
4624 */
4625 int
4626func_has_abort(
4627 void *cookie)
4628{
4629 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
4630}
4631
4632
4633/*
4634 * Turn "dict.Func" into a partial for "Func" bound to "dict".
4635 * Don't do this when "Func" is already a partial that was bound
4636 * explicitly (pt_auto is FALSE).
4637 * Changes "rettv" in-place.
4638 * Returns the updated "selfdict_in".
4639 */
4640 dict_T *
4641make_partial(dict_T *selfdict_in, typval_T *rettv)
4642{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004643 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004644 char_u *tofree = NULL;
4645 ufunc_T *fp;
4646 char_u fname_buf[FLEN_FIXED + 1];
4647 int error;
4648 dict_T *selfdict = selfdict_in;
4649
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004650 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
4651 fp = rettv->vval.v_partial->pt_func;
4652 else
4653 {
4654 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
4655 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004656 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004657 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004658 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004659 vim_free(tofree);
4660 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004661
4662 if (fp != NULL && (fp->uf_flags & FC_DICT))
4663 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004664 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004665
4666 if (pt != NULL)
4667 {
4668 pt->pt_refcount = 1;
4669 pt->pt_dict = selfdict;
4670 pt->pt_auto = TRUE;
4671 selfdict = NULL;
4672 if (rettv->v_type == VAR_FUNC)
4673 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004674 // Just a function: Take over the function name and use
4675 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004676 pt->pt_name = rettv->vval.v_string;
4677 }
4678 else
4679 {
4680 partial_T *ret_pt = rettv->vval.v_partial;
4681 int i;
4682
Bram Moolenaare38eab22019-12-05 21:50:01 +01004683 // Partial: copy the function name, use selfdict and copy
4684 // args. Can't take over name or args, the partial might
4685 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004686 if (ret_pt->pt_name != NULL)
4687 {
4688 pt->pt_name = vim_strsave(ret_pt->pt_name);
4689 func_ref(pt->pt_name);
4690 }
4691 else
4692 {
4693 pt->pt_func = ret_pt->pt_func;
4694 func_ptr_ref(pt->pt_func);
4695 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004696 if (ret_pt->pt_argc > 0)
4697 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004698 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004699 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004700 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004701 pt->pt_argc = 0;
4702 else
4703 {
4704 pt->pt_argc = ret_pt->pt_argc;
4705 for (i = 0; i < pt->pt_argc; i++)
4706 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
4707 }
4708 }
4709 partial_unref(ret_pt);
4710 }
4711 rettv->v_type = VAR_PARTIAL;
4712 rettv->vval.v_partial = pt;
4713 }
4714 }
4715 return selfdict;
4716}
4717
4718/*
4719 * Return the name of the executed function.
4720 */
4721 char_u *
4722func_name(void *cookie)
4723{
4724 return ((funccall_T *)cookie)->func->uf_name;
4725}
4726
4727/*
4728 * Return the address holding the next breakpoint line for a funccall cookie.
4729 */
4730 linenr_T *
4731func_breakpoint(void *cookie)
4732{
4733 return &((funccall_T *)cookie)->breakpoint;
4734}
4735
4736/*
4737 * Return the address holding the debug tick for a funccall cookie.
4738 */
4739 int *
4740func_dbg_tick(void *cookie)
4741{
4742 return &((funccall_T *)cookie)->dbg_tick;
4743}
4744
4745/*
4746 * Return the nesting level for a funccall cookie.
4747 */
4748 int
4749func_level(void *cookie)
4750{
4751 return ((funccall_T *)cookie)->level;
4752}
4753
4754/*
4755 * Return TRUE when a function was ended by a ":return" command.
4756 */
4757 int
4758current_func_returned(void)
4759{
4760 return current_funccal->returned;
4761}
4762
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004763 int
4764free_unref_funccal(int copyID, int testing)
4765{
4766 int did_free = FALSE;
4767 int did_free_funccal = FALSE;
4768 funccall_T *fc, **pfc;
4769
4770 for (pfc = &previous_funccal; *pfc != NULL; )
4771 {
4772 if (can_free_funccal(*pfc, copyID))
4773 {
4774 fc = *pfc;
4775 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01004776 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004777 did_free = TRUE;
4778 did_free_funccal = TRUE;
4779 }
4780 else
4781 pfc = &(*pfc)->caller;
4782 }
4783 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004784 // When a funccal was freed some more items might be garbage
4785 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004786 (void)garbage_collect(testing);
4787
4788 return did_free;
4789}
4790
4791/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004792 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004793 */
4794 static funccall_T *
4795get_funccal(void)
4796{
4797 int i;
4798 funccall_T *funccal;
4799 funccall_T *temp_funccal;
4800
4801 funccal = current_funccal;
4802 if (debug_backtrace_level > 0)
4803 {
4804 for (i = 0; i < debug_backtrace_level; i++)
4805 {
4806 temp_funccal = funccal->caller;
4807 if (temp_funccal)
4808 funccal = temp_funccal;
4809 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004810 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004811 debug_backtrace_level = i;
4812 }
4813 }
4814 return funccal;
4815}
4816
4817/*
4818 * Return the hashtable used for local variables in the current funccal.
4819 * Return NULL if there is no current funccal.
4820 */
4821 hashtab_T *
4822get_funccal_local_ht()
4823{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004824 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004825 return NULL;
4826 return &get_funccal()->l_vars.dv_hashtab;
4827}
4828
4829/*
4830 * Return the l: scope variable.
4831 * Return NULL if there is no current funccal.
4832 */
4833 dictitem_T *
4834get_funccal_local_var()
4835{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004836 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004837 return NULL;
4838 return &get_funccal()->l_vars_var;
4839}
4840
4841/*
4842 * Return the hashtable used for argument in the current funccal.
4843 * Return NULL if there is no current funccal.
4844 */
4845 hashtab_T *
4846get_funccal_args_ht()
4847{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004848 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004849 return NULL;
4850 return &get_funccal()->l_avars.dv_hashtab;
4851}
4852
4853/*
4854 * Return the a: scope variable.
4855 * Return NULL if there is no current funccal.
4856 */
4857 dictitem_T *
4858get_funccal_args_var()
4859{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004861 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004862 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004863}
4864
4865/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004866 * List function variables, if there is a function.
4867 */
4868 void
4869list_func_vars(int *first)
4870{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004872 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004873 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004874}
4875
4876/*
4877 * If "ht" is the hashtable for local variables in the current funccal, return
4878 * the dict that contains it.
4879 * Otherwise return NULL.
4880 */
4881 dict_T *
4882get_current_funccal_dict(hashtab_T *ht)
4883{
4884 if (current_funccal != NULL
4885 && ht == &current_funccal->l_vars.dv_hashtab)
4886 return &current_funccal->l_vars;
4887 return NULL;
4888}
4889
4890/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004891 * Search hashitem in parent scope.
4892 */
4893 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004894find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004895{
4896 funccall_T *old_current_funccal = current_funccal;
4897 hashtab_T *ht;
4898 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004899 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004900
4901 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4902 return NULL;
4903
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004904 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004905 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004906 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004907 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004908 ht = find_var_ht(name, &varname);
4909 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004910 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004911 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004912 if (!HASHITEM_EMPTY(hi))
4913 {
4914 *pht = ht;
4915 break;
4916 }
4917 }
4918 if (current_funccal == current_funccal->func->uf_scoped)
4919 break;
4920 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004921 }
4922 current_funccal = old_current_funccal;
4923
4924 return hi;
4925}
4926
4927/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004928 * Search variable in parent scope.
4929 */
4930 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004931find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004932{
4933 dictitem_T *v = NULL;
4934 funccall_T *old_current_funccal = current_funccal;
4935 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004936 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004937
4938 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4939 return NULL;
4940
Bram Moolenaare38eab22019-12-05 21:50:01 +01004941 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004942 current_funccal = current_funccal->func->uf_scoped;
4943 while (current_funccal)
4944 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004945 ht = find_var_ht(name, &varname);
4946 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004947 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004948 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004949 if (v != NULL)
4950 break;
4951 }
4952 if (current_funccal == current_funccal->func->uf_scoped)
4953 break;
4954 current_funccal = current_funccal->func->uf_scoped;
4955 }
4956 current_funccal = old_current_funccal;
4957
4958 return v;
4959}
4960
4961/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004962 * Set "copyID + 1" in previous_funccal and callers.
4963 */
4964 int
4965set_ref_in_previous_funccal(int copyID)
4966{
4967 int abort = FALSE;
4968 funccall_T *fc;
4969
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004970 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004971 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004972 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004973 abort = abort
4974 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4975 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004976 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004977 }
4978 return abort;
4979}
4980
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004981 static int
4982set_ref_in_funccal(funccall_T *fc, int copyID)
4983{
4984 int abort = FALSE;
4985
4986 if (fc->fc_copyID != copyID)
4987 {
4988 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004989 abort = abort
4990 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
4991 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004992 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004993 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004994 }
4995 return abort;
4996}
4997
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004998/*
4999 * Set "copyID" in all local vars and arguments in the call stack.
5000 */
5001 int
5002set_ref_in_call_stack(int copyID)
5003{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005004 int abort = FALSE;
5005 funccall_T *fc;
5006 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005007
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005008 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005009 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005010
5011 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005012 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
5013 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005014 abort = abort || set_ref_in_funccal(fc, copyID);
5015
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005016 return abort;
5017}
5018
5019/*
5020 * Set "copyID" in all functions available by name.
5021 */
5022 int
5023set_ref_in_functions(int copyID)
5024{
5025 int todo;
5026 hashitem_T *hi = NULL;
5027 int abort = FALSE;
5028 ufunc_T *fp;
5029
5030 todo = (int)func_hashtab.ht_used;
5031 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005032 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005033 if (!HASHITEM_EMPTY(hi))
5034 {
5035 --todo;
5036 fp = HI2UF(hi);
5037 if (!func_name_refcount(fp->uf_name))
5038 abort = abort || set_ref_in_func(NULL, fp, copyID);
5039 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005040 }
5041 return abort;
5042}
5043
5044/*
5045 * Set "copyID" in all function arguments.
5046 */
5047 int
5048set_ref_in_func_args(int copyID)
5049{
5050 int i;
5051 int abort = FALSE;
5052
5053 for (i = 0; i < funcargs.ga_len; ++i)
5054 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
5055 copyID, NULL, NULL);
5056 return abort;
5057}
5058
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005059/*
5060 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005061 * Returns TRUE if setting references failed somehow.
5062 */
5063 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005064set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005065{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005066 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005067 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01005068 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005069 char_u fname_buf[FLEN_FIXED + 1];
5070 char_u *tofree = NULL;
5071 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005072 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005073
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005074 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005075 return FALSE;
5076
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005077 if (fp_in == NULL)
5078 {
5079 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005080 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005081 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005082 if (fp != NULL)
5083 {
5084 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005085 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005086 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02005087
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005088 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005089 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005090}
5091
Bram Moolenaare38eab22019-12-05 21:50:01 +01005092#endif // FEAT_EVAL