blob: a70870baae6cc6f90fc8c438e1eb806075252fc1 [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 }
Bram Moolenaarca2f7e72020-12-31 13:39:54 +0100667 else
668 fp->uf_ret_type = &t_unknown;
Bram Moolenaar9e68c322020-12-25 12:38:04 +0100669 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100670
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200671 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200672 if (current_funccal != NULL && eval_lavars)
673 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200674 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200675 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200676 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200677 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200678
679#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200680 if (prof_def_func())
681 func_do_profile(fp);
682#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200683 if (sandbox)
684 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200686 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200687 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200688 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200689 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100690 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200691
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200692 pt->pt_func = fp;
693 pt->pt_refcount = 1;
694 rettv->vval.v_partial = pt;
695 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200696 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200697
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200698 eval_lavars_used = old_eval_lavars;
Bram Moolenaarefaaaa62020-07-08 22:24:09 +0200699 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200700 evalarg->eval_tofree = tofree;
701 else
702 vim_free(tofree);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100703 if (types_optional)
704 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200705 return OK;
706
707errret:
708 ga_clear_strings(&newargs);
709 ga_clear_strings(&newlines);
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +0100710 if (types_optional)
711 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200712 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100713 vim_free(pt);
Bram Moolenaarefaaaa62020-07-08 22:24:09 +0200714 if (evalarg != NULL && evalarg->eval_tofree == NULL)
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200715 evalarg->eval_tofree = tofree;
716 else
717 vim_free(tofree);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200718 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200719 return FAIL;
720}
721
722/*
723 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
724 * name it contains, otherwise return "name".
725 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
726 * "partialp".
727 */
728 char_u *
729deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
730{
731 dictitem_T *v;
732 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200733 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200734
735 if (partialp != NULL)
736 *partialp = NULL;
737
738 cc = name[*lenp];
739 name[*lenp] = NUL;
740 v = find_var(name, NULL, no_autoload);
741 name[*lenp] = cc;
742 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
743 {
744 if (v->di_tv.vval.v_string == NULL)
745 {
746 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100747 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200748 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200749 s = v->di_tv.vval.v_string;
750 *lenp = (int)STRLEN(s);
751 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200752 }
753
754 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
755 {
756 partial_T *pt = v->di_tv.vval.v_partial;
757
758 if (pt == NULL)
759 {
760 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100761 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200762 }
763 if (partialp != NULL)
764 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200765 s = partial_name(pt);
766 *lenp = (int)STRLEN(s);
767 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200768 }
769
770 return name;
771}
772
773/*
774 * Give an error message with a function name. Handle <SNR> things.
775 * "ermsg" is to be passed without translation, use N_() instead of _().
776 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100777 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200778emsg_funcname(char *ermsg, char_u *name)
779{
780 char_u *p;
781
782 if (*name == K_SPECIAL)
783 p = concat_str((char_u *)"<SNR>", name + 3);
784 else
785 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100786 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200787 if (p != name)
788 vim_free(p);
789}
790
791/*
792 * Allocate a variable for the result of a function.
793 * Return OK or FAIL.
794 */
795 int
796get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200797 char_u *name, // name of the function
798 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200799 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200800 char_u **arg, // argument, pointing to the '('
Bram Moolenaare6b53242020-07-01 17:28:33 +0200801 evalarg_T *evalarg, // for line continuation
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200802 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200803{
804 char_u *argp;
805 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100806 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
807 int argcount = 0; // number of arguments found
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200808 int vim9script = in_vim9script();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200809
810 /*
811 * Get the arguments.
812 */
813 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200814 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
815 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200816 {
Bram Moolenaare6b53242020-07-01 17:28:33 +0200817 // skip the '(' or ',' and possibly line breaks
818 argp = skipwhite_and_linebreak(argp + 1, evalarg);
819
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200820 if (*argp == ')' || *argp == ',' || *argp == NUL)
821 break;
Bram Moolenaare6b53242020-07-01 17:28:33 +0200822 if (eval1(&argp, &argvars[argcount], evalarg) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200823 {
824 ret = FAIL;
825 break;
826 }
827 ++argcount;
Bram Moolenaar9d489562020-07-30 20:08:50 +0200828 // The comma should come right after the argument, but this wasn't
829 // checked previously, thus only enforce it in Vim9 script.
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200830 if (vim9script)
831 {
832 if (*argp != ',' && *skipwhite(argp) == ',')
833 {
834 semsg(_(e_no_white_space_allowed_before_str), ",");
835 ret = FAIL;
836 break;
837 }
838 }
839 else
Bram Moolenaar9d489562020-07-30 20:08:50 +0200840 argp = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200841 if (*argp != ',')
842 break;
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200843 if (vim9script && !IS_WHITE_OR_NUL(argp[1]))
844 {
845 semsg(_(e_white_space_required_after_str), ",");
846 ret = FAIL;
847 break;
848 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200849 }
Bram Moolenaare6b53242020-07-01 17:28:33 +0200850 argp = skipwhite_and_linebreak(argp, evalarg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200851 if (*argp == ')')
852 ++argp;
853 else
854 ret = FAIL;
855
856 if (ret == OK)
857 {
858 int i = 0;
859
860 if (get_vim_var_nr(VV_TESTING))
861 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100862 // Prepare for calling test_garbagecollect_now(), need to know
863 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200864 if (funcargs.ga_itemsize == 0)
865 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
866 for (i = 0; i < argcount; ++i)
867 if (ga_grow(&funcargs, 1) == OK)
868 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
869 &argvars[i];
870 }
871
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200872 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200873
874 funcargs.ga_len -= i;
875 }
876 else if (!aborting())
877 {
878 if (argcount == MAX_FUNC_ARGS)
879 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
880 else
881 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
882 }
883
884 while (--argcount >= 0)
885 clear_tv(&argvars[argcount]);
886
Bram Moolenaar8294d492020-08-10 22:40:56 +0200887 if (in_vim9script())
888 *arg = argp;
889 else
890 *arg = skipwhite(argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200891 return ret;
892}
893
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200894/*
895 * Return TRUE if "p" starts with "<SID>" or "s:".
896 * Only works if eval_fname_script() returned non-zero for "p"!
897 */
898 static int
899eval_fname_sid(char_u *p)
900{
901 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
902}
903
904/*
905 * In a script change <SID>name() and s:name() to K_SNR 123_name().
906 * Change <SNR>123_name() to K_SNR 123_name().
907 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
908 * (slow).
909 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100910 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200911fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
912{
913 int llen;
914 char_u *fname;
915 int i;
916
917 llen = eval_fname_script(name);
918 if (llen > 0)
919 {
920 fname_buf[0] = K_SPECIAL;
921 fname_buf[1] = KS_EXTRA;
922 fname_buf[2] = (int)KE_SNR;
923 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100924 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200925 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200926 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100927 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200928 else
929 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200930 sprintf((char *)fname_buf + 3, "%ld_",
931 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200932 i = (int)STRLEN(fname_buf);
933 }
934 }
935 if (i + STRLEN(name + llen) < FLEN_FIXED)
936 {
937 STRCPY(fname_buf + i, name + llen);
938 fname = fname_buf;
939 }
940 else
941 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200942 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200943 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +0100944 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200945 else
946 {
947 *tofree = fname;
948 mch_memmove(fname, fname_buf, (size_t)i);
949 STRCPY(fname + i, name + llen);
950 }
951 }
952 }
953 else
954 fname = name;
955 return fname;
956}
957
958/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959 * Find a function "name" in script "sid".
960 */
961 static ufunc_T *
962find_func_with_sid(char_u *name, int sid)
963{
964 hashitem_T *hi;
965 char_u buffer[200];
966
967 buffer[0] = K_SPECIAL;
968 buffer[1] = KS_EXTRA;
969 buffer[2] = (int)KE_SNR;
970 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
971 (long)sid, name);
972 hi = hash_find(&func_hashtab, buffer);
973 if (!HASHITEM_EMPTY(hi))
974 return HI2UF(hi);
975
976 return NULL;
977}
978
979/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200980 * Find a function by name, return pointer to it in ufuncs.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200981 * When "is_global" is true don't find script-local or imported functions.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200982 * Return NULL for unknown function.
983 */
Bram Moolenaarce658352020-07-31 23:47:12 +0200984 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200985find_func_even_dead(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200986{
987 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100988 ufunc_T *func;
989 imported_T *imported;
990
Bram Moolenaar9721fb42020-06-11 23:10:46 +0200991 if (!is_global)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100992 {
Bram Moolenaar333894b2020-08-01 18:53:07 +0200993 char_u *after_script = NULL;
Bram Moolenaarefa94442020-08-08 22:16:00 +0200994 long sid = 0;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200995 int find_script_local = in_vim9script()
996 && eval_isnamec1(*name) && name[1] != ':';
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997
Bram Moolenaar95006e32020-08-29 17:47:08 +0200998 if (find_script_local)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100999 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001000 // Find script-local function before global one.
1001 func = find_func_with_sid(name, current_sctx.sc_sid);
1002 if (func != NULL)
1003 return func;
1004 }
1005
Bram Moolenaarefa94442020-08-08 22:16:00 +02001006 if (name[0] == K_SPECIAL
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001007 && name[1] == KS_EXTRA
1008 && name[2] == KE_SNR)
1009 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001010 // Caller changes s: to <SNR>99_name.
1011
1012 after_script = name + 3;
1013 sid = getdigits(&after_script);
Bram Moolenaarefa94442020-08-08 22:16:00 +02001014 if (*after_script == '_')
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001015 ++after_script;
1016 else
1017 after_script = NULL;
1018 }
Bram Moolenaar95006e32020-08-29 17:47:08 +02001019 if (find_script_local || after_script != NULL)
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001020 {
1021 // Find imported function before global one.
Bram Moolenaarefa94442020-08-08 22:16:00 +02001022 if (after_script != NULL && sid != current_sctx.sc_sid)
1023 imported = find_imported_in_script(after_script, 0, sid);
1024 else
1025 imported = find_imported(after_script == NULL
1026 ? name : after_script, 0, cctx);
Bram Moolenaar9721fb42020-06-11 23:10:46 +02001027 if (imported != NULL && imported->imp_funcname != NULL)
1028 {
1029 hi = hash_find(&func_hashtab, imported->imp_funcname);
1030 if (!HASHITEM_EMPTY(hi))
1031 return HI2UF(hi);
1032 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 }
1034 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001035
Bram Moolenaara26b9702020-04-18 19:53:28 +02001036 hi = hash_find(&func_hashtab,
1037 STRNCMP(name, "g:", 2) == 0 ? name + 2 : name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001038 if (!HASHITEM_EMPTY(hi))
1039 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001040
1041 return NULL;
1042}
1043
1044/*
1045 * Find a function by name, return pointer to it in ufuncs.
1046 * "cctx" is passed in a :def function to find imported functions.
1047 * Return NULL for unknown or dead function.
1048 */
1049 ufunc_T *
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001050find_func(char_u *name, int is_global, cctx_T *cctx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051{
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001052 ufunc_T *fp = find_func_even_dead(name, is_global, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053
1054 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
1055 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001056 return NULL;
1057}
1058
1059/*
Bram Moolenaar0f769812020-09-12 18:32:34 +02001060 * Return TRUE if "ufunc" is a global function.
1061 */
1062 int
1063func_is_global(ufunc_T *ufunc)
1064{
1065 return ufunc->uf_name[0] != K_SPECIAL;
1066}
1067
1068/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001069 * Copy the function name of "fp" to buffer "buf".
1070 * "buf" must be able to hold the function name plus three bytes.
1071 * Takes care of script-local function names.
1072 */
1073 static void
1074cat_func_name(char_u *buf, ufunc_T *fp)
1075{
Bram Moolenaar0f769812020-09-12 18:32:34 +02001076 if (!func_is_global(fp))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001077 {
1078 STRCPY(buf, "<SNR>");
1079 STRCAT(buf, fp->uf_name + 3);
1080 }
1081 else
1082 STRCPY(buf, fp->uf_name);
1083}
1084
1085/*
1086 * Add a number variable "name" to dict "dp" with value "nr".
1087 */
1088 static void
1089add_nr_var(
1090 dict_T *dp,
1091 dictitem_T *v,
1092 char *name,
1093 varnumber_T nr)
1094{
1095 STRCPY(v->di_key, name);
1096 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1097 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
1098 v->di_tv.v_type = VAR_NUMBER;
1099 v->di_tv.v_lock = VAR_FIXED;
1100 v->di_tv.vval.v_number = nr;
1101}
1102
1103/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001104 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001105 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001106 static void
1107free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001108{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001109 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001110
1111 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1112 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001113 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001114
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001115 // When garbage collecting a funccall_T may be freed before the
1116 // function that references it, clear its uf_scoped field.
1117 // The function may have been redefined and point to another
1118 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001119 if (fp != NULL && fp->uf_scoped == fc)
1120 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001121 }
Bram Moolenaar58016442016-07-31 18:30:22 +02001122 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001123
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001124 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001125 vim_free(fc);
1126}
1127
1128/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001129 * Free "fc" and what it contains.
1130 * Can be called only when "fc" is kept beyond the period of it called,
1131 * i.e. after cleanup_function_call(fc).
1132 */
1133 static void
1134free_funccal_contents(funccall_T *fc)
1135{
1136 listitem_T *li;
1137
1138 // Free all l: variables.
1139 vars_clear(&fc->l_vars.dv_hashtab);
1140
1141 // Free all a: variables.
1142 vars_clear(&fc->l_avars.dv_hashtab);
1143
1144 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001145 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001146 clear_tv(&li->li_tv);
1147
1148 free_funccal(fc);
1149}
1150
1151/*
Bram Moolenaar6914c642017-04-01 21:21:30 +02001152 * Handle the last part of returning from a function: free the local hashtable.
1153 * Unless it is still in use by a closure.
1154 */
1155 static void
1156cleanup_function_call(funccall_T *fc)
1157{
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001158 int may_free_fc = fc->fc_refcount <= 0;
1159 int free_fc = TRUE;
1160
Bram Moolenaar6914c642017-04-01 21:21:30 +02001161 current_funccal = fc->caller;
1162
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001163 // Free all l: variables if not referred.
1164 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
1165 vars_clear(&fc->l_vars.dv_hashtab);
1166 else
1167 free_fc = FALSE;
1168
1169 // If the a:000 list and the l: and a: dicts are not referenced and
1170 // there is no closure using it, we can free the funccall_T and what's
1171 // in it.
1172 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
1173 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +02001174 else
1175 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001176 int todo;
1177 hashitem_T *hi;
1178 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +02001179
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001180 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +02001181
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001182 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001183 todo = (int)fc->l_avars.dv_hashtab.ht_used;
1184 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
1185 {
1186 if (!HASHITEM_EMPTY(hi))
1187 {
1188 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001189 di = HI2DI(hi);
1190 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +02001191 }
1192 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001193 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02001194
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001195 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
1196 fc->l_varlist.lv_first = NULL;
1197 else
1198 {
1199 listitem_T *li;
1200
1201 free_fc = FALSE;
1202
1203 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001204 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +02001205 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001206 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001207
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001208 if (free_fc)
1209 free_funccal(fc);
1210 else
1211 {
1212 static int made_copy = 0;
1213
1214 // "fc" is still in use. This can happen when returning "a:000",
1215 // assigning "l:" to a global variable or defining a closure.
1216 // Link "fc" in the list for garbage collection later.
1217 fc->caller = previous_funccal;
1218 previous_funccal = fc;
1219
1220 if (want_garbage_collect)
1221 // If garbage collector is ready, clear count.
1222 made_copy = 0;
1223 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001224 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001225 // We have made a lot of copies, worth 4 Mbyte. This can happen
1226 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +01001227 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001228 // too much memory.
1229 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +01001230 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001231 }
Bram Moolenaar6914c642017-04-01 21:21:30 +02001232 }
1233}
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001234
1235/*
1236 * There are two kinds of function names:
1237 * 1. ordinary names, function defined with :function or :def
1238 * 2. numbered functions and lambdas
1239 * For the first we only count the name stored in func_hashtab as a reference,
1240 * using function() does not count as a reference, because the function is
1241 * looked up by name.
1242 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02001243 int
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001244func_name_refcount(char_u *name)
1245{
1246 return isdigit(*name) || *name == '<';
1247}
1248
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249/*
1250 * Unreference "fc": decrement the reference count and free it when it
1251 * becomes zero. "fp" is detached from "fc".
1252 * When "force" is TRUE we are exiting.
1253 */
1254 static void
1255funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
1256{
1257 funccall_T **pfc;
1258 int i;
1259
1260 if (fc == NULL)
1261 return;
1262
1263 if (--fc->fc_refcount <= 0 && (force || (
1264 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
1265 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
1266 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
1267 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
1268 {
1269 if (fc == *pfc)
1270 {
1271 *pfc = fc->caller;
1272 free_funccal_contents(fc);
1273 return;
1274 }
1275 }
1276 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
1277 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
1278 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
1279}
1280
1281/*
1282 * Remove the function from the function hashtable. If the function was
1283 * deleted while it still has references this was already done.
1284 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
1285 */
1286 static int
1287func_remove(ufunc_T *fp)
1288{
1289 hashitem_T *hi;
1290
1291 // Return if it was already virtually deleted.
1292 if (fp->uf_flags & FC_DEAD)
1293 return FALSE;
1294
1295 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
1296 if (!HASHITEM_EMPTY(hi))
1297 {
1298 // When there is a def-function index do not actually remove the
1299 // function, so we can find the index when defining the function again.
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001300 // Do remove it when it's a copy.
1301 if (fp->uf_def_status == UF_COMPILED && (fp->uf_flags & FC_COPY) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001302 fp->uf_flags |= FC_DEAD;
1303 else
1304 hash_remove(&func_hashtab, hi);
1305 return TRUE;
1306 }
1307 return FALSE;
1308}
1309
1310 static void
1311func_clear_items(ufunc_T *fp)
1312{
1313 ga_clear_strings(&(fp->uf_args));
1314 ga_clear_strings(&(fp->uf_def_args));
1315 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001317 VIM_CLEAR(fp->uf_def_arg_idx);
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02001318 VIM_CLEAR(fp->uf_block_ids);
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001319 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar6110e792020-07-08 19:35:21 +02001320 clear_type_list(&fp->uf_type_list);
Bram Moolenaarf112f302020-12-20 17:47:52 +01001321 partial_unref(fp->uf_partial);
1322 fp->uf_partial = NULL;
Bram Moolenaar801ab062020-06-25 19:27:56 +02001323
1324#ifdef FEAT_LUA
1325 if (fp->uf_cb_free != NULL)
1326 {
1327 fp->uf_cb_free(fp->uf_cb_state);
1328 fp->uf_cb_free = NULL;
1329 }
1330
1331 fp->uf_cb_state = NULL;
1332 fp->uf_cb = NULL;
1333#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001334#ifdef FEAT_PROFILE
1335 VIM_CLEAR(fp->uf_tml_count);
1336 VIM_CLEAR(fp->uf_tml_total);
1337 VIM_CLEAR(fp->uf_tml_self);
1338#endif
1339}
1340
1341/*
1342 * Free all things that a function contains. Does not free the function
1343 * itself, use func_free() for that.
1344 * When "force" is TRUE we are exiting.
1345 */
1346 static void
1347func_clear(ufunc_T *fp, int force)
1348{
1349 if (fp->uf_cleared)
1350 return;
1351 fp->uf_cleared = TRUE;
1352
1353 // clear this function
1354 func_clear_items(fp);
1355 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001356 unlink_def_function(fp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357}
1358
1359/*
1360 * Free a function and remove it from the list of functions. Does not free
1361 * what a function contains, call func_clear() first.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001362 * When "force" is TRUE we are exiting.
Bram Moolenaara05e5242020-09-19 18:19:19 +02001363 * Returns OK when the function was actually freed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364 */
Bram Moolenaara05e5242020-09-19 18:19:19 +02001365 static int
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001366func_free(ufunc_T *fp, int force)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001367{
1368 // Only remove it when not done already, otherwise we would remove a newer
1369 // version of the function with the same name.
1370 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1371 func_remove(fp);
1372
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001373 if ((fp->uf_flags & FC_DEAD) == 0 || force)
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001374 {
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001375 if (fp->uf_dfunc_idx > 0)
1376 unlink_def_function(fp);
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001377 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 vim_free(fp);
Bram Moolenaara05e5242020-09-19 18:19:19 +02001379 return OK;
Bram Moolenaarc4ce36d2020-08-14 17:08:15 +02001380 }
Bram Moolenaara05e5242020-09-19 18:19:19 +02001381 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382}
1383
1384/*
1385 * Free all things that a function contains and free the function itself.
1386 * When "force" is TRUE we are exiting.
1387 */
1388 static void
1389func_clear_free(ufunc_T *fp, int force)
1390{
1391 func_clear(fp, force);
Bram Moolenaarfdeab652020-09-19 15:16:50 +02001392 if (force || fp->uf_dfunc_idx == 0 || func_name_refcount(fp->uf_name)
1393 || (fp->uf_flags & FC_COPY))
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001394 func_free(fp, force);
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02001395 else
1396 fp->uf_flags |= FC_DEAD;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397}
1398
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001399/*
1400 * Copy already defined function "lambda" to a new function with name "global".
1401 * This is for when a compiled function defines a global function.
1402 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001403 int
1404copy_func(char_u *lambda, char_u *global, ectx_T *ectx)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001405{
1406 ufunc_T *ufunc = find_func_even_dead(lambda, TRUE, NULL);
Bram Moolenaarf112f302020-12-20 17:47:52 +01001407 ufunc_T *fp = NULL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001408
1409 if (ufunc == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001410 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001411 semsg(_(e_lambda_function_not_found_str), lambda);
1412 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001413 }
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001414
1415 // TODO: handle ! to overwrite
1416 fp = find_func(global, TRUE, NULL);
1417 if (fp != NULL)
1418 {
1419 semsg(_(e_funcexts), global);
1420 return FAIL;
1421 }
1422
1423 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(global) + 1);
1424 if (fp == NULL)
1425 return FAIL;
1426
1427 fp->uf_varargs = ufunc->uf_varargs;
1428 fp->uf_flags = (ufunc->uf_flags & ~FC_VIM9) | FC_COPY;
1429 fp->uf_def_status = ufunc->uf_def_status;
1430 fp->uf_dfunc_idx = ufunc->uf_dfunc_idx;
1431 if (ga_copy_strings(&ufunc->uf_args, &fp->uf_args) == FAIL
1432 || ga_copy_strings(&ufunc->uf_def_args, &fp->uf_def_args)
1433 == FAIL
1434 || ga_copy_strings(&ufunc->uf_lines, &fp->uf_lines) == FAIL)
1435 goto failed;
1436
1437 fp->uf_name_exp = ufunc->uf_name_exp == NULL ? NULL
1438 : vim_strsave(ufunc->uf_name_exp);
1439 if (ufunc->uf_arg_types != NULL)
1440 {
1441 fp->uf_arg_types = ALLOC_MULT(type_T *, fp->uf_args.ga_len);
1442 if (fp->uf_arg_types == NULL)
1443 goto failed;
1444 mch_memmove(fp->uf_arg_types, ufunc->uf_arg_types,
1445 sizeof(type_T *) * fp->uf_args.ga_len);
1446 }
1447 if (ufunc->uf_def_arg_idx != NULL)
1448 {
1449 fp->uf_def_arg_idx = ALLOC_MULT(int, fp->uf_def_args.ga_len + 1);
1450 if (fp->uf_def_arg_idx == NULL)
1451 goto failed;
1452 mch_memmove(fp->uf_def_arg_idx, ufunc->uf_def_arg_idx,
1453 sizeof(int) * fp->uf_def_args.ga_len + 1);
1454 }
1455 if (ufunc->uf_va_name != NULL)
1456 {
1457 fp->uf_va_name = vim_strsave(ufunc->uf_va_name);
1458 if (fp->uf_va_name == NULL)
1459 goto failed;
1460 }
1461 fp->uf_ret_type = ufunc->uf_ret_type;
1462
1463 fp->uf_refcount = 1;
1464 STRCPY(fp->uf_name, global);
1465 hash_add(&func_hashtab, UF2HIKEY(fp));
1466
1467 // the referenced dfunc_T is now used one more time
1468 link_def_function(fp);
1469
1470 // Create a partial to store the context of the function, if not done
1471 // already.
1472 if ((ufunc->uf_flags & FC_CLOSURE) && ufunc->uf_partial == NULL)
1473 {
1474 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
1475
1476 if (pt == NULL)
1477 goto failed;
1478 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
1479 goto failed;
1480 ufunc->uf_partial = pt;
1481 --pt->pt_refcount; // not referenced here yet
1482 }
1483 if (ufunc->uf_partial != NULL)
1484 {
1485 fp->uf_partial = ufunc->uf_partial;
1486 ++fp->uf_partial->pt_refcount;
1487 }
1488
1489 return OK;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001490
1491failed:
1492 func_clear_free(fp, TRUE);
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001493 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001494}
1495
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001496static int funcdepth = 0;
1497
1498/*
1499 * Increment the function call depth count.
1500 * Return FAIL when going over 'maxfuncdepth'.
1501 * Otherwise return OK, must call funcdepth_decrement() later!
1502 */
1503 int
1504funcdepth_increment(void)
1505{
1506 if (funcdepth >= p_mfd)
1507 {
1508 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
1509 return FAIL;
1510 }
1511 ++funcdepth;
1512 return OK;
1513}
1514
1515 void
1516funcdepth_decrement(void)
1517{
1518 --funcdepth;
1519}
1520
1521/*
1522 * Get the current function call depth.
1523 */
1524 int
1525funcdepth_get(void)
1526{
1527 return funcdepth;
1528}
1529
1530/*
1531 * Restore the function call depth. This is for cases where there is no
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01001532 * guarantee funcdepth_decrement() can be called exactly the same number of
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001533 * times as funcdepth_increment().
1534 */
1535 void
1536funcdepth_restore(int depth)
1537{
1538 funcdepth = depth;
1539}
Bram Moolenaar6914c642017-04-01 21:21:30 +02001540
1541/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001542 * Call a user function.
1543 */
1544 static void
1545call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001546 ufunc_T *fp, // pointer to function
1547 int argcount, // nr of args
1548 typval_T *argvars, // arguments
1549 typval_T *rettv, // return value
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001550 funcexe_T *funcexe, // context
Bram Moolenaare38eab22019-12-05 21:50:01 +01001551 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001552{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001553 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001554 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001555 funccall_T *fc;
1556 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001557 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001558 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001559 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001560 int i;
1561 int ai;
1562 int islambda = FALSE;
1563 char_u numbuf[NUMBUFLEN];
1564 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001565#ifdef FEAT_PROFILE
1566 proftime_T wait_start;
1567 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001568 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001569#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001570 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001571
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001572 // If depth of calling is getting too high, don't execute the function.
1573 if (funcdepth_increment() == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001574 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001575 rettv->v_type = VAR_NUMBER;
1576 rettv->vval.v_number = -1;
1577 return;
1578 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001579
Bram Moolenaare38eab22019-12-05 21:50:01 +01001580 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001581
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001582 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001583 if (fc == NULL)
1584 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001585 fc->caller = current_funccal;
1586 current_funccal = fc;
1587 fc->func = fp;
1588 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001589 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001590 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001591 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1592 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001593 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001594 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001595 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001596
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001597 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598 {
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001599 // Execute the function, possibly compiling it first.
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001600 call_def_function(fp, argcount, argvars, funcexe->partial, rettv);
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001601 funcdepth_decrement();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602 current_funccal = fc->caller;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 free_funccal(fc);
1604 return;
1605 }
1606
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001607 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1608 islambda = TRUE;
1609
1610 /*
1611 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1612 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1613 * each argument variable and saves a lot of time.
1614 */
1615 /*
1616 * Init l: variables.
1617 */
1618 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1619 if (selfdict != NULL)
1620 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001621 // Set l:self to "selfdict". Use "name" to avoid a warning from
1622 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001623 v = &fc->fixvar[fixvar_idx++].var;
1624 name = v->di_key;
1625 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001626 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001627 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1628 v->di_tv.v_type = VAR_DICT;
1629 v->di_tv.v_lock = 0;
1630 v->di_tv.vval.v_dict = selfdict;
1631 ++selfdict->dv_refcount;
1632 }
1633
1634 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001635 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001636 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001637 * Set a:000 to a list with room for the "..." arguments.
1638 */
1639 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001640 if ((fp->uf_flags & FC_NOARGS) == 0)
1641 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001642 (varnumber_T)(argcount >= fp->uf_args.ga_len
1643 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001644 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001645 if ((fp->uf_flags & FC_NOARGS) == 0)
1646 {
1647 // Use "name" to avoid a warning from some compiler that checks the
1648 // destination size.
1649 v = &fc->fixvar[fixvar_idx++].var;
1650 name = v->di_key;
1651 STRCPY(name, "000");
1652 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1653 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1654 v->di_tv.v_type = VAR_LIST;
1655 v->di_tv.v_lock = VAR_FIXED;
1656 v->di_tv.vval.v_list = &fc->l_varlist;
1657 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001658 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001659 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1660 fc->l_varlist.lv_lock = VAR_FIXED;
1661
1662 /*
1663 * Set a:firstline to "firstline" and a:lastline to "lastline".
1664 * Set a:name to named arguments.
1665 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001666 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001667 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001668 if ((fp->uf_flags & FC_NOARGS) == 0)
1669 {
1670 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001671 (varnumber_T)funcexe->firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001672 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001673 (varnumber_T)funcexe->lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001674 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001675 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001676 {
1677 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001678 typval_T def_rettv;
1679 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001680
1681 ai = i - fp->uf_args.ga_len;
1682 if (ai < 0)
1683 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001684 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001685 name = FUNCARG(fp, i);
1686 if (islambda)
1687 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001688
1689 // evaluate named argument default expression
1690 isdefault = ai + fp->uf_def_args.ga_len >= 0
1691 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1692 && argvars[i].vval.v_number == VVAL_NONE));
1693 if (isdefault)
1694 {
1695 char_u *default_expr = NULL;
1696 def_rettv.v_type = VAR_NUMBER;
1697 def_rettv.vval.v_number = -1;
1698
1699 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1700 [ai + fp->uf_def_args.ga_len];
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001701 if (eval1(&default_expr, &def_rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001702 {
1703 default_arg_err = 1;
1704 break;
1705 }
1706 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001707 }
1708 else
1709 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001710 if ((fp->uf_flags & FC_NOARGS) != 0)
1711 // Bail out if no a: arguments used (in lambda).
1712 break;
1713
Bram Moolenaare38eab22019-12-05 21:50:01 +01001714 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001715 sprintf((char *)numbuf, "%d", ai + 1);
1716 name = numbuf;
1717 }
1718 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1719 {
1720 v = &fc->fixvar[fixvar_idx++].var;
1721 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001722 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001723 }
1724 else
1725 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001726 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001727 if (v == NULL)
1728 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001729 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001730 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001731
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001732 // Note: the values are copied directly to avoid alloc/free.
1733 // "argvars" must have VAR_FIXED for v_lock.
1734 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001735 v->di_tv.v_lock = VAR_FIXED;
1736
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001737 if (addlocal)
1738 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001739 // Named arguments should be accessed without the "a:" prefix in
1740 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001741 copy_tv(&v->di_tv, &v->di_tv);
1742 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001743 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001744 else
1745 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001746
1747 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1748 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001749 listitem_T *li = &fc->l_listitems[ai];
1750
1751 li->li_tv = argvars[i];
1752 li->li_tv.v_lock = VAR_FIXED;
1753 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001754 }
1755 }
1756
Bram Moolenaare38eab22019-12-05 21:50:01 +01001757 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001758 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001759
1760 if (fp->uf_flags & FC_SANDBOX)
1761 {
1762 using_sandbox = TRUE;
1763 ++sandbox;
1764 }
1765
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001766 estack_push_ufunc(fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001767 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001768 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001769 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001770 ++no_wait_return;
1771 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001772
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001773 smsg(_("calling %s"), SOURCING_NAME);
1774 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001775 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001776 char_u buf[MSG_BUF_LEN];
1777 char_u numbuf2[NUMBUFLEN];
1778 char_u *tofree;
1779 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001780
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001781 msg_puts("(");
1782 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001783 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001784 if (i > 0)
1785 msg_puts(", ");
1786 if (argvars[i].v_type == VAR_NUMBER)
1787 msg_outnum((long)argvars[i].vval.v_number);
1788 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001789 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001790 // Do not want errors such as E724 here.
1791 ++emsg_off;
1792 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1793 --emsg_off;
1794 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001795 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001796 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001797 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001798 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1799 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001800 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001801 msg_puts((char *)s);
1802 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001803 }
1804 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001805 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001806 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001807 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001808 msg_puts("\n"); // don't overwrite this either
1809
1810 verbose_leave_scroll();
1811 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001812 }
1813#ifdef FEAT_PROFILE
1814 if (do_profiling == PROF_YES)
1815 {
1816 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001817 {
1818 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001819 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001820 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001821 if (fp->uf_profiling
1822 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1823 {
1824 ++fp->uf_tm_count;
1825 profile_start(&call_start);
1826 profile_zero(&fp->uf_tm_children);
1827 }
1828 script_prof_save(&wait_start);
1829 }
1830#endif
1831
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001832 save_current_sctx = current_sctx;
1833 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001834 save_did_emsg = did_emsg;
1835 did_emsg = FALSE;
1836
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001837 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1838 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001839 else if (islambda)
1840 {
1841 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
1842
1843 // A Lambda always has the command "return {expr}". It is much faster
1844 // to evaluate {expr} directly.
1845 ++ex_nesting_level;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001846 (void)eval1(&p, rettv, &EVALARG_EVALUATE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001847 --ex_nesting_level;
1848 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001849 else
1850 // call do_cmdline() to execute the lines
1851 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001852 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1853
1854 --RedrawingDisabled;
1855
Bram Moolenaare38eab22019-12-05 21:50:01 +01001856 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001857 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1858 {
1859 clear_tv(rettv);
1860 rettv->v_type = VAR_NUMBER;
1861 rettv->vval.v_number = -1;
1862 }
1863
1864#ifdef FEAT_PROFILE
1865 if (do_profiling == PROF_YES && (fp->uf_profiling
1866 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1867 {
1868 profile_end(&call_start);
1869 profile_sub_wait(&wait_start, &call_start);
1870 profile_add(&fp->uf_tm_total, &call_start);
1871 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1872 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1873 {
1874 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1875 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1876 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001877 if (started_profiling)
1878 // make a ":profdel func" stop profiling the function
1879 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001880 }
1881#endif
1882
Bram Moolenaare38eab22019-12-05 21:50:01 +01001883 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001884 if (p_verbose >= 12)
1885 {
1886 ++no_wait_return;
1887 verbose_enter_scroll();
1888
1889 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001890 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001891 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001892 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001893 (long)fc->rettv->vval.v_number);
1894 else
1895 {
1896 char_u buf[MSG_BUF_LEN];
1897 char_u numbuf2[NUMBUFLEN];
1898 char_u *tofree;
1899 char_u *s;
1900
Bram Moolenaare38eab22019-12-05 21:50:01 +01001901 // The value may be very long. Skip the middle part, so that we
1902 // have some idea how it starts and ends. smsg() would always
1903 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001904 ++emsg_off;
1905 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1906 --emsg_off;
1907 if (s != NULL)
1908 {
1909 if (vim_strsize(s) > MSG_BUF_CLEN)
1910 {
1911 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1912 s = buf;
1913 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001914 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001915 vim_free(tofree);
1916 }
1917 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001918 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001919
1920 verbose_leave_scroll();
1921 --no_wait_return;
1922 }
1923
Bram Moolenaare31ee862020-01-07 20:59:34 +01001924 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001925 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001926 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001927#ifdef FEAT_PROFILE
1928 if (do_profiling == PROF_YES)
1929 script_prof_restore(&wait_start);
1930#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001931 if (using_sandbox)
1932 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001933
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001934 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001935 {
1936 ++no_wait_return;
1937 verbose_enter_scroll();
1938
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001939 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001940 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001941
1942 verbose_leave_scroll();
1943 --no_wait_return;
1944 }
1945
1946 did_emsg |= save_did_emsg;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001947 funcdepth_decrement();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001948 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001949}
1950
1951/*
Bram Moolenaar50824712020-12-20 21:10:17 +01001952 * Check the argument count for user function "fp".
1953 * Return FCERR_UNKNOWN if OK, FCERR_TOOFEW or FCERR_TOOMANY otherwise.
1954 */
1955 int
1956check_user_func_argcount(ufunc_T *fp, int argcount)
1957{
1958 int regular_args = fp->uf_args.ga_len;
1959
1960 if (argcount < regular_args - fp->uf_def_args.ga_len)
1961 return FCERR_TOOFEW;
1962 else if (!has_varargs(fp) && argcount > regular_args)
1963 return FCERR_TOOMANY;
1964 return FCERR_UNKNOWN;
1965}
1966
1967/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001969 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970 int
1971call_user_func_check(
1972 ufunc_T *fp,
1973 int argcount,
1974 typval_T *argvars,
1975 typval_T *rettv,
1976 funcexe_T *funcexe,
1977 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001978{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 int error;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001980
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1982 *funcexe->doesrange = TRUE;
Bram Moolenaar50824712020-12-20 21:10:17 +01001983 error = check_user_func_argcount(fp, argcount);
1984 if (error != FCERR_UNKNOWN)
1985 return error;
1986 if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 error = FCERR_DICT;
1988 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001989 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 int did_save_redo = FALSE;
1991 save_redo_T save_redo;
1992
1993 /*
1994 * Call the user function.
1995 * Save and restore search patterns, script variables and
1996 * redo buffer.
1997 */
1998 save_search_patterns();
1999 if (!ins_compl_active())
2000 {
2001 saveRedobuff(&save_redo);
2002 did_save_redo = TRUE;
2003 }
2004 ++fp->uf_calls;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002005 call_user_func(fp, argcount, argvars, rettv, funcexe,
2006 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
2008 // Function was unreferenced while being used, free it now.
2009 func_clear_free(fp, FALSE);
2010 if (did_save_redo)
2011 restoreRedobuff(&save_redo);
2012 restore_search_patterns();
2013 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002014 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002016}
2017
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002018static funccal_entry_T *funccal_stack = NULL;
2019
2020/*
2021 * Save the current function call pointer, and set it to NULL.
2022 * Used when executing autocommands and for ":source".
2023 */
2024 void
2025save_funccal(funccal_entry_T *entry)
2026{
2027 entry->top_funccal = current_funccal;
2028 entry->next = funccal_stack;
2029 funccal_stack = entry;
2030 current_funccal = NULL;
2031}
2032
2033 void
2034restore_funccal(void)
2035{
2036 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002037 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002038 else
2039 {
2040 current_funccal = funccal_stack->top_funccal;
2041 funccal_stack = funccal_stack->next;
2042 }
2043}
2044
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02002045 funccall_T *
2046get_current_funccal(void)
2047{
2048 return current_funccal;
2049}
2050
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002051/*
2052 * Mark all functions of script "sid" as deleted.
2053 */
2054 void
2055delete_script_functions(int sid)
2056{
2057 hashitem_T *hi;
2058 ufunc_T *fp;
Bram Moolenaarce658352020-07-31 23:47:12 +02002059 long_u todo = 1;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002060 char_u buf[30];
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002061 size_t len;
2062
2063 buf[0] = K_SPECIAL;
2064 buf[1] = KS_EXTRA;
2065 buf[2] = (int)KE_SNR;
Bram Moolenaar909ed7e2020-04-27 23:16:41 +02002066 sprintf((char *)buf + 3, "%d_", sid);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002067 len = STRLEN(buf);
2068
Bram Moolenaarce658352020-07-31 23:47:12 +02002069 while (todo > 0)
2070 {
2071 todo = func_hashtab.ht_used;
2072 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2073 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002074 {
Bram Moolenaarce658352020-07-31 23:47:12 +02002075 fp = HI2UF(hi);
2076 if (STRNCMP(fp->uf_name, buf, len) == 0)
2077 {
2078 int changed = func_hashtab.ht_changed;
2079
2080 fp->uf_flags |= FC_DEAD;
2081 func_clear(fp, TRUE);
2082 // When clearing a function another function can be cleared
2083 // as a side effect. When that happens start over.
2084 if (changed != func_hashtab.ht_changed)
2085 break;
2086 }
2087 --todo;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002088 }
Bram Moolenaarce658352020-07-31 23:47:12 +02002089 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002090}
2091
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002092#if defined(EXITFREE) || defined(PROTO)
2093 void
2094free_all_functions(void)
2095{
2096 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002097 ufunc_T *fp;
2098 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002099 long_u todo = 1;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002100 int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002101
Bram Moolenaare38eab22019-12-05 21:50:01 +01002102 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02002103 while (current_funccal != NULL)
2104 {
2105 clear_tv(current_funccal->rettv);
2106 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002107 if (current_funccal == NULL && funccal_stack != NULL)
2108 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02002109 }
2110
Bram Moolenaare38eab22019-12-05 21:50:01 +01002111 // First clear what the functions contain. Since this may lower the
2112 // reference count of a function, it may also free a function and change
2113 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002114 while (todo > 0)
2115 {
2116 todo = func_hashtab.ht_used;
2117 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2118 if (!HASHITEM_EMPTY(hi))
2119 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002120 // clear the def function index now
2121 fp = HI2UF(hi);
2122 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002123 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002124
Bram Moolenaare38eab22019-12-05 21:50:01 +01002125 // Only free functions that are not refcounted, those are
2126 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002127 if (func_name_refcount(fp->uf_name))
2128 ++skipped;
2129 else
2130 {
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002131 changed = func_hashtab.ht_changed;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002132 func_clear(fp, TRUE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002133 if (changed != func_hashtab.ht_changed)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002134 {
2135 skipped = 0;
2136 break;
2137 }
2138 }
2139 --todo;
2140 }
2141 }
2142
Bram Moolenaare38eab22019-12-05 21:50:01 +01002143 // Now actually free the functions. Need to start all over every time,
2144 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002145 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002146 while (func_hashtab.ht_used > skipped)
2147 {
2148 todo = func_hashtab.ht_used;
2149 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002150 if (!HASHITEM_EMPTY(hi))
2151 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02002152 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002153 // Only free functions that are not refcounted, those are
2154 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02002155 fp = HI2UF(hi);
2156 if (func_name_refcount(fp->uf_name))
2157 ++skipped;
2158 else
2159 {
Bram Moolenaara05e5242020-09-19 18:19:19 +02002160 if (func_free(fp, FALSE) == OK)
2161 {
2162 skipped = 0;
2163 break;
2164 }
2165 // did not actually free it
2166 ++skipped;
Bram Moolenaarc2574872016-08-11 22:51:05 +02002167 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002168 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02002169 }
2170 if (skipped == 0)
2171 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002172
2173 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002174}
2175#endif
2176
2177/*
2178 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaara26b9702020-04-18 19:53:28 +02002179 * lower case letter and doesn't contain AUTOLOAD_CHAR or ':'.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002180 * "len" is the length of "name", or -1 for NUL terminated.
2181 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002182 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002183builtin_function(char_u *name, int len)
2184{
2185 char_u *p;
2186
Bram Moolenaara26b9702020-04-18 19:53:28 +02002187 if (!ASCII_ISLOWER(name[0]) || name[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002188 return FALSE;
2189 p = vim_strchr(name, AUTOLOAD_CHAR);
2190 return p == NULL || (len > 0 && p > name + len);
2191}
2192
2193 int
2194func_call(
2195 char_u *name,
2196 typval_T *args,
2197 partial_T *partial,
2198 dict_T *selfdict,
2199 typval_T *rettv)
2200{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002201 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002202 listitem_T *item;
2203 typval_T argv[MAX_FUNC_ARGS + 1];
2204 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002205 int r = 0;
2206
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02002207 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02002208 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002209 {
2210 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
2211 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002212 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002213 break;
2214 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002215 // Make a copy of each argument. This is needed to be able to set
2216 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002217 copy_tv(&item->li_tv, &argv[argc++]);
2218 }
2219
2220 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002221 {
2222 funcexe_T funcexe;
2223
Bram Moolenaara80faa82020-04-12 19:37:17 +02002224 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002225 funcexe.firstline = curwin->w_cursor.lnum;
2226 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002227 funcexe.evaluate = TRUE;
2228 funcexe.partial = partial;
2229 funcexe.selfdict = selfdict;
2230 r = call_func(name, -1, rettv, argc, argv, &funcexe);
2231 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002232
Bram Moolenaare38eab22019-12-05 21:50:01 +01002233 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002234 while (argc > 0)
2235 clear_tv(&argv[--argc]);
2236
2237 return r;
2238}
2239
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002240static int callback_depth = 0;
2241
2242 int
2243get_callback_depth(void)
2244{
2245 return callback_depth;
2246}
2247
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002248/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002249 * Invoke call_func() with a callback.
2250 */
2251 int
2252call_callback(
2253 callback_T *callback,
2254 int len, // length of "name" or -1 to use strlen()
2255 typval_T *rettv, // return value goes here
2256 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002257 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002258 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002259{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002260 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002261 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002262
Bram Moolenaara80faa82020-04-12 19:37:17 +02002263 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002264 funcexe.evaluate = TRUE;
2265 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02002266 ++callback_depth;
2267 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
2268 --callback_depth;
2269 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02002270}
2271
2272/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273 * Give an error message for the result of a function.
2274 * Nothing if "error" is FCERR_NONE.
2275 */
2276 void
2277user_func_error(int error, char_u *name)
2278{
2279 switch (error)
2280 {
2281 case FCERR_UNKNOWN:
2282 emsg_funcname(e_unknownfunc, name);
2283 break;
2284 case FCERR_NOTMETHOD:
2285 emsg_funcname(
2286 N_("E276: Cannot use function as a method: %s"), name);
2287 break;
2288 case FCERR_DELETED:
2289 emsg_funcname(N_(e_func_deleted), name);
2290 break;
2291 case FCERR_TOOMANY:
2292 emsg_funcname((char *)e_toomanyarg, name);
2293 break;
2294 case FCERR_TOOFEW:
2295 emsg_funcname((char *)e_toofewarg, name);
2296 break;
2297 case FCERR_SCRIPT:
2298 emsg_funcname(
2299 N_("E120: Using <SID> not in a script context: %s"), name);
2300 break;
2301 case FCERR_DICT:
2302 emsg_funcname(
2303 N_("E725: Calling dict function without Dictionary: %s"),
2304 name);
2305 break;
2306 }
2307}
2308
2309/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002310 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002311 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002312 * Return FAIL when the function can't be called, OK otherwise.
2313 * Also returns OK when an error was encountered while executing the function.
2314 */
2315 int
2316call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02002317 char_u *funcname, // name of the function
2318 int len, // length of "name" or -1 to use strlen()
2319 typval_T *rettv, // return value goes here
2320 int argcount_in, // number of "argvars"
2321 typval_T *argvars_in, // vars for arguments, must have "argcount"
2322 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002323 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002324{
2325 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01002326 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002327 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002328 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002329 char_u fname_buf[FLEN_FIXED + 1];
2330 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002331 char_u *fname = NULL;
2332 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002333 int argcount = argcount_in;
2334 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002335 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002336 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
2337 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002338 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002339 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002340 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002341
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02002342 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
2343 // even when call_func() returns FAIL.
2344 rettv->v_type = VAR_UNKNOWN;
2345
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002346 if (partial != NULL)
2347 fp = partial->pt_func;
2348 if (fp == NULL)
2349 {
2350 // Make a copy of the name, if it comes from a funcref variable it
2351 // could be changed or deleted in the called function.
2352 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
2353 if (name == NULL)
2354 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002355
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002356 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
2357 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002358
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002359 if (funcexe->doesrange != NULL)
2360 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002361
2362 if (partial != NULL)
2363 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002364 // When the function has a partial with a dict and there is a dict
2365 // argument, use the dict argument. That is backwards compatible.
2366 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002367 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002368 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01002369 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002370 {
2371 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002372 {
2373 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
2374 {
Bram Moolenaaref140542019-12-31 21:27:13 +01002375 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002376 goto theend;
2377 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002378 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002379 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002380 for (i = 0; i < argcount_in; ++i)
2381 argv[i + argv_clear] = argvars_in[i];
2382 argvars = argv;
2383 argcount = partial->pt_argc + argcount_in;
2384 }
2385 }
2386
Bram Moolenaaref140542019-12-31 21:27:13 +01002387 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002388 {
2389 char_u *rfname = fname;
Bram Moolenaar333894b2020-08-01 18:53:07 +02002390 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002391
Bram Moolenaar333894b2020-08-01 18:53:07 +02002392 // Skip "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002393 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaar333894b2020-08-01 18:53:07 +02002394 {
2395 is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002396 rfname = fname + 2;
Bram Moolenaar333894b2020-08-01 18:53:07 +02002397 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002398
Bram Moolenaare38eab22019-12-05 21:50:01 +01002399 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002400 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01002401 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002402
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002403 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002404 {
2405 /*
2406 * User defined function.
2407 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002408 if (fp == NULL)
Bram Moolenaar333894b2020-08-01 18:53:07 +02002409 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002410
Bram Moolenaare38eab22019-12-05 21:50:01 +01002411 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002412 if (fp == NULL
2413 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002414 rfname, rfname, TRUE, NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002415 && !aborting())
2416 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002417 // executed an autocommand, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02002418 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002419 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002420 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002421 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
2422 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002423 // loaded a package, search for the function again
Bram Moolenaar333894b2020-08-01 18:53:07 +02002424 fp = find_func(rfname, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002425 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02002426 if (fp == NULL)
2427 {
2428 char_u *p = untrans_function_name(rfname);
2429
2430 // If using Vim9 script try not local to the script.
Bram Moolenaar035d6e92020-08-11 22:30:42 +02002431 // Don't do this if the name starts with "s:".
2432 if (p != NULL && (funcname[0] != 's' || funcname[1] != ':'))
Bram Moolenaar333894b2020-08-01 18:53:07 +02002433 fp = find_func(p, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002434 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002435
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002436 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01002437 error = FCERR_DELETED;
Bram Moolenaar801ab062020-06-25 19:27:56 +02002438#ifdef FEAT_LUA
2439 else if (fp != NULL && (fp->uf_flags & FC_CFUNC))
2440 {
2441 cfunc_T cb = fp->uf_cb;
2442
2443 error = (*cb)(argcount, argvars, rettv, fp->uf_cb_state);
2444 }
2445#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002446 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002447 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002448 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01002449 // postponed filling in the arguments, do it now
2450 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02002451 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02002452
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002453 if (funcexe->basetv != NULL)
2454 {
2455 // Method call: base->Method()
2456 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
2457 argv[0] = *funcexe->basetv;
2458 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002459 argvars = argv;
2460 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002461 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002462
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 error = call_user_func_check(fp, argcount, argvars, rettv,
2464 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002465 }
2466 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002467 else if (funcexe->basetv != NULL)
2468 {
2469 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02002470 * expr->method(): Find the method name in the table, call its
2471 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02002472 */
2473 error = call_internal_method(fname, argcount, argvars, rettv,
2474 funcexe->basetv);
2475 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002476 else
2477 {
2478 /*
2479 * Find the function name in the table, call its implementation.
2480 */
2481 error = call_internal_func(fname, argcount, argvars, rettv);
2482 }
Bram Moolenaar333894b2020-08-01 18:53:07 +02002483
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002484 /*
2485 * The function call (or "FuncUndefined" autocommand sequence) might
2486 * have been aborted by an error, an interrupt, or an explicitly thrown
2487 * exception that has not been caught so far. This situation can be
2488 * tested for by calling aborting(). For an error in an internal
2489 * function or for the "E132" error in call_user_func(), however, the
2490 * throw point at which the "force_abort" flag (temporarily reset by
2491 * emsg()) is normally updated has not been reached yet. We need to
2492 * update that flag first to make aborting() reliable.
2493 */
2494 update_force_abort();
2495 }
Bram Moolenaaref140542019-12-31 21:27:13 +01002496 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002497 ret = OK;
2498
Bram Moolenaar4c054e92019-11-10 00:13:50 +01002499theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002500 /*
2501 * Report an error unless the argument evaluation or function call has been
2502 * cancelled due to an aborting error, an interrupt, or an exception.
2503 */
2504 if (!aborting())
2505 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02002506 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002507 }
2508
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002509 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002510 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002511 clear_tv(&argv[--argv_clear + argv_base]);
2512
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002513 vim_free(tofree);
2514 vim_free(name);
2515
2516 return ret;
2517}
2518
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002519 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520printable_func_name(ufunc_T *fp)
2521{
2522 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
2523}
2524
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002525/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002526 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002527 */
2528 static void
2529list_func_head(ufunc_T *fp, int indent)
2530{
2531 int j;
2532
2533 msg_start();
2534 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002535 msg_puts(" ");
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002536 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002537 msg_puts("def ");
2538 else
2539 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002541 msg_putchar('(');
2542 for (j = 0; j < fp->uf_args.ga_len; ++j)
2543 {
2544 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002545 msg_puts(", ");
2546 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547 if (fp->uf_arg_types != NULL)
2548 {
2549 char *tofree;
2550
2551 msg_puts(": ");
2552 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
2553 vim_free(tofree);
2554 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002555 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
2556 {
2557 msg_puts(" = ");
2558 msg_puts(((char **)(fp->uf_def_args.ga_data))
2559 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
2560 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002561 }
2562 if (fp->uf_varargs)
2563 {
2564 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002565 msg_puts(", ");
2566 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002567 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 if (fp->uf_va_name != NULL)
2569 {
2570 if (j)
2571 msg_puts(", ");
2572 msg_puts("...");
2573 msg_puts((char *)fp->uf_va_name);
2574 if (fp->uf_va_type)
2575 {
2576 char *tofree;
2577
2578 msg_puts(": ");
2579 msg_puts(type_name(fp->uf_va_type, &tofree));
2580 vim_free(tofree);
2581 }
2582 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002583 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002584
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002585 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002586 {
2587 if (fp->uf_ret_type != &t_void)
2588 {
2589 char *tofree;
2590
2591 msg_puts(": ");
2592 msg_puts(type_name(fp->uf_ret_type, &tofree));
2593 vim_free(tofree);
2594 }
2595 }
2596 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002597 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002598 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002599 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002600 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002601 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002602 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002603 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002604 msg_clr_eos();
2605 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002606 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002607}
2608
2609/*
2610 * Get a function name, translating "<SID>" and "<SNR>".
2611 * Also handles a Funcref in a List or Dictionary.
2612 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002613 * Set "*is_global" to TRUE when the function must be global, unless
2614 * "is_global" is NULL.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002615 * flags:
2616 * TFN_INT: internal function name OK
2617 * TFN_QUIET: be quiet
2618 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002619 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002620 * Advances "pp" to just after the function name (if no error).
2621 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002622 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002623trans_function_name(
2624 char_u **pp,
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002625 int *is_global,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002626 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002627 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002628 funcdict_T *fdp, // return: info about dictionary used
2629 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002630{
2631 char_u *name = NULL;
2632 char_u *start;
2633 char_u *end;
2634 int lead;
2635 char_u sid_buf[20];
2636 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002638 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639 int vim9script;
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002640 static char *e_function_name = N_("E129: Function name required");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002641
2642 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02002643 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002644 start = *pp;
2645
Bram Moolenaare38eab22019-12-05 21:50:01 +01002646 // Check for hard coded <SNR>: already translated function ID (from a user
2647 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002648 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2649 && (*pp)[2] == (int)KE_SNR)
2650 {
2651 *pp += 3;
2652 len = get_id_len(pp) + 3;
2653 return vim_strnsave(start, len);
2654 }
2655
Bram Moolenaare38eab22019-12-05 21:50:01 +01002656 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2657 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002658 lead = eval_fname_script(start);
2659 if (lead > 2)
2660 start += lead;
2661
Bram Moolenaare38eab22019-12-05 21:50:01 +01002662 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002663 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002664 lead > 2 ? 0 : FNE_CHECK_START);
2665 if (end == start)
2666 {
2667 if (!skip)
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002668 emsg(_(e_function_name));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002669 goto theend;
2670 }
2671 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2672 {
2673 /*
2674 * Report an invalid expression in braces, unless the expression
2675 * evaluation has been cancelled due to an aborting error, an
2676 * interrupt, or an exception.
2677 */
2678 if (!aborting())
2679 {
2680 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002681 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002682 }
2683 else
2684 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2685 goto theend;
2686 }
2687
2688 if (lv.ll_tv != NULL)
2689 {
2690 if (fdp != NULL)
2691 {
2692 fdp->fd_dict = lv.ll_dict;
2693 fdp->fd_newkey = lv.ll_newkey;
2694 lv.ll_newkey = NULL;
2695 fdp->fd_di = lv.ll_di;
2696 }
2697 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2698 {
2699 name = vim_strsave(lv.ll_tv->vval.v_string);
2700 *pp = end;
2701 }
2702 else if (lv.ll_tv->v_type == VAR_PARTIAL
2703 && lv.ll_tv->vval.v_partial != NULL)
2704 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002705 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002706 *pp = end;
2707 if (partial != NULL)
2708 *partial = lv.ll_tv->vval.v_partial;
2709 }
2710 else
2711 {
2712 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2713 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002714 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002715 else
2716 *pp = end;
2717 name = NULL;
2718 }
2719 goto theend;
2720 }
2721
2722 if (lv.ll_name == NULL)
2723 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002724 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002725 *pp = end;
2726 goto theend;
2727 }
2728
Bram Moolenaare38eab22019-12-05 21:50:01 +01002729 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002730 if (lv.ll_exp_name != NULL)
2731 {
2732 len = (int)STRLEN(lv.ll_exp_name);
2733 name = deref_func_name(lv.ll_exp_name, &len, partial,
2734 flags & TFN_NO_AUTOLOAD);
2735 if (name == lv.ll_exp_name)
2736 name = NULL;
2737 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002738 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002739 {
2740 len = (int)(end - *pp);
2741 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2742 if (name == *pp)
2743 name = NULL;
2744 }
2745 if (name != NULL)
2746 {
2747 name = vim_strsave(name);
2748 *pp = end;
2749 if (STRNCMP(name, "<SNR>", 5) == 0)
2750 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002751 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002752 name[0] = K_SPECIAL;
2753 name[1] = KS_EXTRA;
2754 name[2] = (int)KE_SNR;
2755 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2756 }
2757 goto theend;
2758 }
2759
2760 if (lv.ll_exp_name != NULL)
2761 {
2762 len = (int)STRLEN(lv.ll_exp_name);
2763 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2764 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2765 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002766 // When there was "s:" already or the name expanded to get a
2767 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002768 lv.ll_name += 2;
2769 len -= 2;
2770 lead = 2;
2771 }
2772 }
2773 else
2774 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002775 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002776 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002777 {
2778 if (is_global != NULL && lv.ll_name[0] == 'g')
2779 *is_global = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002780 lv.ll_name += 2;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002781 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002782 len = (int)(end - lv.ll_name);
2783 }
Bram Moolenaar7b5d5442020-10-04 13:42:34 +02002784 if (len <= 0)
2785 {
2786 if (!skip)
2787 emsg(_(e_function_name));
2788 goto theend;
2789 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002790
Bram Moolenaar17f700a2020-12-19 21:23:42 +01002791 // In Vim9 script a user function is script-local by default, unless it
2792 // starts with a lower case character: dict.func().
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002793 vim9script = ASCII_ISUPPER(*start) && in_vim9script();
Bram Moolenaar17f700a2020-12-19 21:23:42 +01002794 if (vim9script)
2795 {
2796 char_u *p;
2797
2798 // SomeScript#func() is a global function.
2799 for (p = start; *p != NUL && *p != '('; ++p)
2800 if (*p == AUTOLOAD_CHAR)
2801 vim9script = FALSE;
2802 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002804 /*
2805 * Copy the function name to allocated memory.
2806 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2807 * Accept <SNR>123_name() outside a script.
2808 */
2809 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002810 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002812 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 if (!vim9script)
2814 lead = 3;
2815 if (vim9script || (lv.ll_exp_name != NULL
2816 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002817 || eval_fname_sid(*pp))
2818 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002820 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002821 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002822 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002823 goto theend;
2824 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002825 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826 if (vim9script)
2827 extra = 3 + (int)STRLEN(sid_buf);
2828 else
2829 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002830 }
2831 }
2832 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2833 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002834 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002835 start);
2836 goto theend;
2837 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002838 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002839 {
2840 char_u *cp = vim_strchr(lv.ll_name, ':');
2841
2842 if (cp != NULL && cp < end)
2843 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002844 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002845 goto theend;
2846 }
2847 }
2848
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002850 if (name != NULL)
2851 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002852 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002853 {
2854 name[0] = K_SPECIAL;
2855 name[1] = KS_EXTRA;
2856 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002858 STRCPY(name + 3, sid_buf);
2859 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2861 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002862 }
2863 *pp = end;
2864
2865theend:
2866 clear_lval(&lv);
2867 return name;
2868}
2869
2870/*
Bram Moolenaara26b9702020-04-18 19:53:28 +02002871 * Assuming "name" is the result of trans_function_name() and it was prefixed
2872 * to use the script-local name, return the unmodified name (points into
2873 * "name"). Otherwise return NULL.
2874 * This can be used to first search for a script-local function and fall back
2875 * to the global function if not found.
2876 */
2877 char_u *
2878untrans_function_name(char_u *name)
2879{
2880 char_u *p;
2881
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002882 if (*name == K_SPECIAL && in_vim9script())
Bram Moolenaara26b9702020-04-18 19:53:28 +02002883 {
2884 p = vim_strchr(name, '_');
2885 if (p != NULL)
2886 return p + 1;
2887 }
2888 return NULL;
2889}
2890
2891/*
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002892 * List functions. When "regmatch" is NULL all of then.
2893 * Otherwise functions matching "regmatch".
2894 */
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002895 void
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002896list_functions(regmatch_T *regmatch)
2897{
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002898 int changed = func_hashtab.ht_changed;
2899 long_u todo = func_hashtab.ht_used;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002900 hashitem_T *hi;
2901
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002902 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002903 {
2904 if (!HASHITEM_EMPTY(hi))
2905 {
2906 ufunc_T *fp = HI2UF(hi);
2907
2908 --todo;
2909 if ((fp->uf_flags & FC_DEAD) == 0
2910 && (regmatch == NULL
2911 ? !message_filtered(fp->uf_name)
2912 && !func_name_refcount(fp->uf_name)
2913 : !isdigit(*fp->uf_name)
2914 && vim_regexec(regmatch, fp->uf_name, 0)))
2915 {
2916 list_func_head(fp, FALSE);
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02002917 if (changed != func_hashtab.ht_changed)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002918 {
2919 emsg(_("E454: function list was modified"));
2920 return;
2921 }
2922 }
2923 }
2924 }
2925}
2926
2927/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02002928 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002929 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
2930 * the function name.
Bram Moolenaar04b12692020-05-04 23:24:44 +02002931 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002932 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02002933 ufunc_T *
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002934define_function(exarg_T *eap, char_u *name_arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002935{
2936 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002937 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002938 int j;
2939 int c;
2940 int saved_did_emsg;
2941 int saved_wait_return = need_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002942 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002943 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002944 char_u *p;
2945 char_u *arg;
2946 char_u *line_arg = NULL;
2947 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002949 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002950 garray_T newlines;
2951 int varargs = FALSE;
2952 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002954 ufunc_T *fp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002955 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002956 int indent;
2957 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958#define MAX_FUNC_NESTING 50
2959 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002960 dictitem_T *v;
2961 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002962 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002963 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002964 hashitem_T *hi;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002965 getline_opt_T getline_options;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002966 linenr_T sourcing_lnum_off;
2967 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002968 int is_heredoc = FALSE;
2969 char_u *skip_until = NULL;
2970 char_u *heredoc_trimmed = NULL;
Bram Moolenaare7e48382020-07-22 18:17:08 +02002971 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02002972 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002973
2974 /*
2975 * ":function" without argument: list functions.
2976 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002977 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002978 {
2979 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02002980 list_functions(NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002981 eap->nextcmd = check_nextcmd(eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02002982 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002983 }
2984
2985 /*
2986 * ":function /pat": list functions matching pattern.
2987 */
2988 if (*eap->arg == '/')
2989 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002990 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002991 if (!eap->skip)
2992 {
2993 regmatch_T regmatch;
2994
2995 c = *p;
2996 *p = NUL;
2997 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2998 *p = c;
2999 if (regmatch.regprog != NULL)
3000 {
3001 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003002 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003003 vim_regfree(regmatch.regprog);
3004 }
3005 }
3006 if (*p == '/')
3007 ++p;
3008 eap->nextcmd = check_nextcmd(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003009 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003010 }
3011
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 ga_init(&newargs);
3013 ga_init(&argtypes);
3014 ga_init(&default_args);
3015
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003016 /*
3017 * Get the function name. There are these situations:
3018 * func normal function name
3019 * "name" == func, "fudi.fd_dict" == NULL
3020 * dict.func new dictionary entry
3021 * "name" == NULL, "fudi.fd_dict" set,
3022 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
3023 * dict.func existing dict entry with a Funcref
3024 * "name" == func, "fudi.fd_dict" set,
3025 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
3026 * dict.func existing dict entry that's not a Funcref
3027 * "name" == NULL, "fudi.fd_dict" set,
3028 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
3029 * s:func script-local function name
3030 * g:func global function name, same as "func"
3031 */
3032 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003033 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003034 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02003035 // nested function, argument is (args).
3036 paren = TRUE;
3037 CLEAR_FIELD(fudi);
3038 }
3039 else
3040 {
3041 name = trans_function_name(&p, &is_global, eap->skip,
3042 TFN_NO_AUTOLOAD, &fudi, NULL);
3043 paren = (vim_strchr(p, '(') != NULL);
3044 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003045 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02003046 /*
3047 * Return on an invalid expression in braces, unless the expression
3048 * evaluation has been cancelled due to an aborting error, an
3049 * interrupt, or an exception.
3050 */
3051 if (!aborting())
3052 {
3053 if (!eap->skip && fudi.fd_newkey != NULL)
3054 semsg(_(e_dictkey), fudi.fd_newkey);
3055 vim_free(fudi.fd_newkey);
3056 return NULL;
3057 }
3058 else
3059 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003060 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003061 }
3062
Bram Moolenaare38eab22019-12-05 21:50:01 +01003063 // An error in a function call during evaluation of an expression in magic
3064 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003065 saved_did_emsg = did_emsg;
3066 did_emsg = FALSE;
3067
3068 /*
3069 * ":function func" with only function name: list function.
3070 */
3071 if (!paren)
3072 {
3073 if (!ends_excmd(*skipwhite(p)))
3074 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003075 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003076 goto ret_free;
3077 }
3078 eap->nextcmd = check_nextcmd(p);
3079 if (eap->nextcmd != NULL)
3080 *p = NUL;
3081 if (!eap->skip && !got_int)
3082 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003083 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003084 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
3085 {
3086 char_u *up = untrans_function_name(name);
3087
3088 // With Vim9 script the name was made script-local, if not
3089 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02003090 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003091 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003092 }
3093
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003094 if (fp != NULL)
3095 {
3096 list_func_head(fp, TRUE);
3097 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
3098 {
3099 if (FUNCLINE(fp, j) == NULL)
3100 continue;
3101 msg_putchar('\n');
3102 msg_outnum((long)(j + 1));
3103 if (j < 9)
3104 msg_putchar(' ');
3105 if (j < 99)
3106 msg_putchar(' ');
3107 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003108 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003109 ui_breakcheck();
3110 }
3111 if (!got_int)
3112 {
3113 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003114 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 msg_puts(" enddef");
3116 else
3117 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003118 }
3119 }
3120 else
Bram Moolenaara26b9702020-04-18 19:53:28 +02003121 emsg_funcname(N_("E123: Undefined function: %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003122 }
3123 goto ret_free;
3124 }
3125
3126 /*
3127 * ":function name(arg1, arg2)" Define function.
3128 */
3129 p = skipwhite(p);
3130 if (*p != '(')
3131 {
3132 if (!eap->skip)
3133 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003134 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003135 goto ret_free;
3136 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003137 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003138 if (vim_strchr(p, '(') != NULL)
3139 p = vim_strchr(p, '(');
3140 }
3141 p = skipwhite(p + 1);
3142
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02003143 // In Vim9 script only global functions can be redefined.
3144 if (vim9script && eap->forceit && !is_global)
3145 {
3146 emsg(_(e_nobang));
3147 goto ret_free;
3148 }
3149
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003150 ga_init2(&newlines, (int)sizeof(char_u *), 3);
3151
Bram Moolenaar04b12692020-05-04 23:24:44 +02003152 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003153 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003154 // Check the name of the function. Unless it's a dictionary function
3155 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003156 if (name != NULL)
3157 arg = name;
3158 else
3159 arg = fudi.fd_newkey;
3160 if (arg != NULL && (fudi.fd_di == NULL
3161 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
3162 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
3163 {
3164 if (*arg == K_SPECIAL)
3165 j = 3;
3166 else
3167 j = 0;
3168 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
3169 : eval_isnamec(arg[j])))
3170 ++j;
3171 if (arg[j] != NUL)
3172 emsg_funcname((char *)e_invarg2, arg);
3173 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003174 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003175 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003176 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003177 }
3178
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003179 // This may get more lines and make the pointers into the first line
3180 // invalid.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003182 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003183 &varargs, &default_args, eap->skip,
3184 eap, &line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003185 goto errret_2;
3186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003188 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 // find the return type: :def Func(): type
3190 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003191 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02003193 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003195 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003196 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003198 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003200 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003201 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02003202 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003203 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003204 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02003205 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003206 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 else
3208 // find extra arguments "range", "dict", "abort" and "closure"
3209 for (;;)
3210 {
3211 p = skipwhite(p);
3212 if (STRNCMP(p, "range", 5) == 0)
3213 {
3214 flags |= FC_RANGE;
3215 p += 5;
3216 }
3217 else if (STRNCMP(p, "dict", 4) == 0)
3218 {
3219 flags |= FC_DICT;
3220 p += 4;
3221 }
3222 else if (STRNCMP(p, "abort", 5) == 0)
3223 {
3224 flags |= FC_ABORT;
3225 p += 5;
3226 }
3227 else if (STRNCMP(p, "closure", 7) == 0)
3228 {
3229 flags |= FC_CLOSURE;
3230 p += 7;
3231 if (current_funccal == NULL)
3232 {
3233 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
3234 name == NULL ? (char_u *)"" : name);
3235 goto erret;
3236 }
3237 }
3238 else
3239 break;
3240 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003241
Bram Moolenaare38eab22019-12-05 21:50:01 +01003242 // When there is a line break use what follows for the function body.
3243 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003244 if (*p == '\n')
3245 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003246 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02003247 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
3248 && eap->cmdidx != CMD_def)
Bram Moolenaare7e48382020-07-22 18:17:08 +02003249 && !(*p == '#' && (vim9script || eap->cmdidx == CMD_def))
3250 && !eap->skip
3251 && !did_emsg)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003252 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003253
3254 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
3256 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003257 */
3258 if (KeyTyped)
3259 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003260 // Check if the function already exists, don't let the user type the
3261 // whole function before telling him it doesn't work! For a script we
3262 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003263 if (!eap->skip && !eap->forceit)
3264 {
3265 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003266 emsg(_(e_funcdict));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003267 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003268 emsg_funcname(e_funcexts, name);
3269 }
3270
3271 if (!eap->skip && did_emsg)
3272 goto erret;
3273
Bram Moolenaare38eab22019-12-05 21:50:01 +01003274 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003275 cmdline_row = msg_row;
3276 }
3277
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003278 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003279 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003280
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02003281 // Detect having skipped over comment lines to find the return
3282 // type. Add NULL lines to keep the line count correct.
3283 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
3284 if (SOURCING_LNUM < sourcing_lnum_off)
3285 {
3286 sourcing_lnum_off -= SOURCING_LNUM;
3287 if (ga_grow(&newlines, sourcing_lnum_off) == FAIL)
3288 goto erret;
3289 while (sourcing_lnum_off-- > 0)
3290 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3291 }
3292
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003293 indent = 2;
3294 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01003296 getline_options = eap->cmdidx == CMD_def
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01003297 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003298 for (;;)
3299 {
3300 if (KeyTyped)
3301 {
3302 msg_scroll = TRUE;
3303 saved_wait_return = FALSE;
3304 }
3305 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003306
3307 if (line_arg != NULL)
3308 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003309 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003310 theline = line_arg;
3311 p = vim_strchr(theline, '\n');
3312 if (p == NULL)
3313 line_arg += STRLEN(line_arg);
3314 else
3315 {
3316 *p = NUL;
3317 line_arg = p + 1;
3318 }
3319 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003320 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02003321 {
3322 vim_free(line_to_free);
3323 if (eap->getline == NULL)
Bram Moolenaar66250c92020-08-20 15:02:42 +02003324 theline = getcmdline(':', 0L, indent, getline_options);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003325 else
Bram Moolenaar66250c92020-08-20 15:02:42 +02003326 theline = eap->getline(':', eap->cookie, indent,
3327 getline_options);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003328 line_to_free = theline;
3329 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003330 if (KeyTyped)
3331 lines_left = Rows - 1;
3332 if (theline == NULL)
3333 {
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003334 if (skip_until != NULL)
3335 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
3336 else if (eap->cmdidx == CMD_def)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003337 emsg(_(e_missing_enddef));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338 else
3339 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003340 goto erret;
3341 }
3342
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003343 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003344 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003345 if (SOURCING_LNUM < sourcing_lnum_off)
3346 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003347 else
3348 sourcing_lnum_off = 0;
3349
3350 if (skip_until != NULL)
3351 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003353 // * ":append" and "."
3354 // * ":python <<EOF" and "EOF"
3355 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
3356 if (heredoc_trimmed == NULL
3357 || (is_heredoc && skipwhite(theline) == theline)
3358 || STRNCMP(theline, heredoc_trimmed,
3359 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003360 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003361 if (heredoc_trimmed == NULL)
3362 p = theline;
3363 else if (is_heredoc)
3364 p = skipwhite(theline) == theline
3365 ? theline : theline + STRLEN(heredoc_trimmed);
3366 else
3367 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02003368 if (STRCMP(p, skip_until) == 0)
3369 {
3370 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003371 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01003372 getline_options = eap->cmdidx == CMD_def
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01003373 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003374 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003375 }
3376 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003377 }
3378 else
3379 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003380 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01003381 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003382 ;
3383
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 // Check for "endfunction" or "enddef".
3385 if (checkforcmd(&p, nesting_def[nesting]
3386 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003387 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02003388 char_u *nextcmd = NULL;
3389
Bram Moolenaar663bb232017-06-22 19:12:10 +02003390 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02003391 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02003392 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02003393 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02003394 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003395 give_warning2(eap->cmdidx == CMD_def
3396 ? (char_u *)_("W1001: Text found after :enddef: %s")
3397 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003398 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003399 if (nextcmd != NULL)
3400 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003401 // Another command follows. If the line came from "eap" we
3402 // can simply point into it, otherwise we need to change
3403 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02003404 eap->nextcmd = nextcmd;
3405 if (line_to_free != NULL)
3406 {
3407 vim_free(*eap->cmdlinep);
3408 *eap->cmdlinep = line_to_free;
3409 line_to_free = NULL;
3410 }
3411 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003412 break;
3413 }
3414
Bram Moolenaare38eab22019-12-05 21:50:01 +01003415 // Increase indent inside "if", "while", "for" and "try", decrease
3416 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003418 indent -= 2;
3419 else if (STRNCMP(p, "if", 2) == 0
3420 || STRNCMP(p, "wh", 2) == 0
3421 || STRNCMP(p, "for", 3) == 0
3422 || STRNCMP(p, "try", 3) == 0)
3423 indent += 2;
3424
Bram Moolenaare38eab22019-12-05 21:50:01 +01003425 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01003426 // Only recognize "def" inside "def", not inside "function",
3427 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 c = *p;
Bram Moolenaar673660a2020-01-26 16:50:05 +01003429 if (checkforcmd(&p, "function", 2)
3430 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003431 {
3432 if (*p == '!')
3433 p = skipwhite(p + 1);
3434 p += eval_fname_script(p);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003435 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL, NULL));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003436 if (*skipwhite(p) == '(')
3437 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438 if (nesting == MAX_FUNC_NESTING - 1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003439 emsg(_(e_function_nesting_too_deep));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 else
3441 {
3442 ++nesting;
3443 nesting_def[nesting] = (c == 'd');
3444 indent += 2;
3445 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003446 }
3447 }
3448
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003449 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003450 p = skip_range(p, FALSE, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003451 if (eap->cmdidx != CMD_def
3452 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01003453 || (p[0] == 'c'
3454 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
3455 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
3456 && (STRNCMP(&p[3], "nge", 3) != 0
3457 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003458 || (p[0] == 'i'
3459 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003460 && (!ASCII_ISALPHA(p[2])
3461 || (p[2] == 's'
3462 && (!ASCII_ISALPHA(p[3])
3463 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003464 skip_until = vim_strsave((char_u *)".");
3465
Bram Moolenaare38eab22019-12-05 21:50:01 +01003466 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003467 arg = skipwhite(skiptowhite(p));
3468 if (arg[0] == '<' && arg[1] =='<'
3469 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003470 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
3471 || ((p[2] == '3' || p[2] == 'x')
3472 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003473 || (p[0] == 'p' && p[1] == 'e'
3474 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
3475 || (p[0] == 't' && p[1] == 'c'
3476 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
3477 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
3478 && !ASCII_ISALPHA(p[3]))
3479 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
3480 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
3481 || (p[0] == 'm' && p[1] == 'z'
3482 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
3483 ))
3484 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003485 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003486 p = skipwhite(arg + 2);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003487 if (STRNCMP(p, "trim", 4) == 0)
3488 {
3489 // Ignore leading white space.
3490 p = skipwhite(p + 4);
3491 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003492 skipwhite(theline) - theline);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003493 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003494 if (*p == NUL)
3495 skip_until = vim_strsave((char_u *)".");
3496 else
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003497 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003498 getline_options = GETLINE_NONE;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003499 is_heredoc = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003500 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003501
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003502 // Check for ":cmd v =<< [trim] EOF"
3503 // and ":cmd [a, b] =<< [trim] EOF"
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003504 // and "lines =<< [trim] EOF" for Vim9
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003505 // Where "cmd" can be "let", "var", "final" or "const".
Bram Moolenaar8471e572019-05-19 21:37:18 +02003506 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003507 if (*arg == '[')
3508 arg = vim_strchr(arg, ']');
3509 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003510 {
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003511 int found = (eap->cmdidx == CMD_def && arg[0] == '='
3512 && arg[1] == '<' && arg[2] =='<');
3513
3514 if (!found)
3515 // skip over the argument after "cmd"
3516 arg = skipwhite(skiptowhite(arg));
3517 if (found || (arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003518 && (checkforcmd(&p, "let", 2)
3519 || checkforcmd(&p, "var", 3)
3520 || checkforcmd(&p, "final", 5)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003521 || checkforcmd(&p, "const", 5))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02003522 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003523 p = skipwhite(arg + 3);
3524 if (STRNCMP(p, "trim", 4) == 0)
3525 {
3526 // Ignore leading white space.
3527 p = skipwhite(p + 4);
3528 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003529 skipwhite(theline) - theline);
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003530 }
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003531 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003532 getline_options = GETLINE_NONE;
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003533 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003534 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003535 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003536 }
3537
Bram Moolenaare38eab22019-12-05 21:50:01 +01003538 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003539 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003540 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003541
Bram Moolenaare38eab22019-12-05 21:50:01 +01003542 // Copy the line to newly allocated memory. get_one_sourceline()
3543 // allocates 250 bytes per line, this saves 80% on average. The cost
3544 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003545 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003546 if (p == NULL)
3547 goto erret;
3548 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003549
Bram Moolenaare38eab22019-12-05 21:50:01 +01003550 // Add NULL lines for continuation lines, so that the line count is
3551 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003552 while (sourcing_lnum_off-- > 0)
3553 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3554
Bram Moolenaare38eab22019-12-05 21:50:01 +01003555 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003556 if (line_arg != NULL && *line_arg == NUL)
3557 line_arg = NULL;
3558 }
3559
Bram Moolenaare38eab22019-12-05 21:50:01 +01003560 // Don't define the function when skipping commands or when an error was
3561 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003562 if (eap->skip || did_emsg)
3563 goto erret;
3564
3565 /*
3566 * If there are no errors, add the function
3567 */
3568 if (fudi.fd_dict == NULL)
3569 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570 hashtab_T *ht;
3571
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003572 v = find_var(name, &ht, FALSE);
3573 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
3574 {
3575 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
3576 name);
3577 goto erret;
3578 }
3579
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003580 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaareef21022020-08-01 22:16:43 +02003581 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003582 {
Bram Moolenaareef21022020-08-01 22:16:43 +02003583 char_u *uname = untrans_function_name(name);
3584
3585 import = find_imported(uname == NULL ? name : uname, 0, NULL);
3586 }
3587
3588 if (fp != NULL || import != NULL)
3589 {
3590 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003592 // Function can be replaced with "function!" and when sourcing the
3593 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02003594 // A name that is used by an import can not be overruled.
3595 if (import != NULL
3596 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003597 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02003598 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003599 {
Bram Moolenaareef21022020-08-01 22:16:43 +02003600 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003601 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02003602 else
3603 emsg_funcname(e_funcexts, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003604 goto erret;
3605 }
3606 if (fp->uf_calls > 0)
3607 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003608 emsg_funcname(
3609 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003610 name);
3611 goto erret;
3612 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003613 if (fp->uf_refcount > 1)
3614 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003615 // This function is referenced somewhere, don't redefine it but
3616 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003617 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003618 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003619 fp = NULL;
3620 overwrite = TRUE;
3621 }
3622 else
3623 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003624 char_u *exp_name = fp->uf_name_exp;
3625
3626 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01003627 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003628 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003629 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003630 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003632#ifdef FEAT_PROFILE
3633 fp->uf_profiling = FALSE;
3634 fp->uf_prof_initialized = FALSE;
3635#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01003636 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003637 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003638 }
3639 }
3640 else
3641 {
3642 char numbuf[20];
3643
3644 fp = NULL;
3645 if (fudi.fd_newkey == NULL && !eap->forceit)
3646 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003647 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003648 goto erret;
3649 }
3650 if (fudi.fd_di == NULL)
3651 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003652 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02003653 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003654 goto erret;
3655 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003656 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02003657 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003658 goto erret;
3659
Bram Moolenaare38eab22019-12-05 21:50:01 +01003660 // Give the function a sequential number. Can only be used with a
3661 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003662 vim_free(name);
3663 sprintf(numbuf, "%d", ++func_nr);
3664 name = vim_strsave((char_u *)numbuf);
3665 if (name == NULL)
3666 goto erret;
3667 }
3668
3669 if (fp == NULL)
3670 {
3671 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3672 {
3673 int slen, plen;
3674 char_u *scriptname;
3675
Bram Moolenaare38eab22019-12-05 21:50:01 +01003676 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003677 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003678 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003679 {
3680 scriptname = autoload_name(name);
3681 if (scriptname != NULL)
3682 {
3683 p = vim_strchr(scriptname, '/');
3684 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003685 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003686 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003687 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003688 j = OK;
3689 vim_free(scriptname);
3690 }
3691 }
3692 if (j == FAIL)
3693 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003694 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003695 goto erret;
3696 }
3697 }
3698
Bram Moolenaar47ed5532019-08-08 20:49:14 +02003699 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003700 if (fp == NULL)
3701 goto erret;
3702
3703 if (fudi.fd_dict != NULL)
3704 {
3705 if (fudi.fd_di == NULL)
3706 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003707 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003708 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
3709 if (fudi.fd_di == NULL)
3710 {
3711 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003712 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003713 goto erret;
3714 }
3715 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
3716 {
3717 vim_free(fudi.fd_di);
3718 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003719 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003720 goto erret;
3721 }
3722 }
3723 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003724 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003725 clear_tv(&fudi.fd_di->di_tv);
3726 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003727 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003728
Bram Moolenaare38eab22019-12-05 21:50:01 +01003729 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003730 flags |= FC_DICT;
3731 }
3732
Bram Moolenaare38eab22019-12-05 21:50:01 +01003733 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003734 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003735 if (overwrite)
3736 {
3737 hi = hash_find(&func_hashtab, name);
3738 hi->hi_key = UF2HIKEY(fp);
3739 }
3740 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003741 {
3742 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003743 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003744 goto erret;
3745 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003746 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003747 }
3748 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003749 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003750 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003751 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752
3753 if (eap->cmdidx == CMD_def)
3754 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003755 int lnum_save = SOURCING_LNUM;
3756 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003757
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003758 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003759
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003760 // error messages are for the first function line
3761 SOURCING_LNUM = sourcing_lnum_top;
3762
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003763 if (cstack != NULL && cstack->cs_idx >= 0)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003764 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003765 int count = cstack->cs_idx + 1;
3766 int i;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003767
3768 // The block context may be needed for script variables declared in
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003769 // a block that is visible now but not when the function is called
3770 // later.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003771 fp->uf_block_ids = ALLOC_MULT(int, count);
3772 if (fp->uf_block_ids != NULL)
3773 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003774 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003775 sizeof(int) * count);
3776 fp->uf_block_depth = count;
3777 }
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003778
3779 // Set flag in each block to indicate a function was defined. This
3780 // is used to keep the variable when leaving the block, see
3781 // hide_script_var().
3782 for (i = 0; i <= cstack->cs_idx; ++i)
3783 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003784 }
3785
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003786 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003788 SOURCING_LNUM = lnum_save;
3789 goto errret_2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003791 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792
3793 // parse the return type, if any
3794 if (ret_type == NULL)
3795 fp->uf_ret_type = &t_void;
3796 else
3797 {
3798 p = ret_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01003799 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02003801 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003803 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003804 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003805
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003806 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003807 if ((flags & FC_CLOSURE) != 0)
3808 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003809 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003810 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003811 }
3812 else
3813 fp->uf_scoped = NULL;
3814
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003815#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003816 if (prof_def_func())
3817 func_do_profile(fp);
3818#endif
3819 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003820 if (sandbox)
3821 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003822 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003823 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003824 fp->uf_flags = flags;
3825 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003826 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003827 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02003828 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003829 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830 if (is_export)
3831 {
3832 fp->uf_flags |= FC_EXPORT;
3833 // let ex_export() know the export worked.
3834 is_export = FALSE;
3835 }
3836
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003837 if (eap->cmdidx == CMD_def)
3838 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02003839 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
3840 // :func does not use Vim9 script syntax, even in a Vim9 script file
3841 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003842
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003843 goto ret_free;
3844
3845erret:
3846 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003847 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003848errret_2:
3849 ga_clear_strings(&newlines);
3850ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003851 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003852 vim_free(skip_until);
Bram Moolenaarfffdf472020-12-13 21:16:55 +01003853 vim_free(heredoc_trimmed);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003854 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003855 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003856 if (name != name_arg)
3857 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003858 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003859 did_emsg |= saved_did_emsg;
3860 need_wait_return |= saved_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003861
3862 return fp;
3863}
3864
3865/*
3866 * ":function"
3867 */
3868 void
3869ex_function(exarg_T *eap)
3870{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003871 (void)define_function(eap, NULL);
Bram Moolenaar822ba242020-05-24 23:00:18 +02003872}
3873
3874/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02003875 * :defcompile - compile all :def functions in the current script that need to
3876 * be compiled. Except dead functions.
Bram Moolenaar822ba242020-05-24 23:00:18 +02003877 */
3878 void
3879ex_defcompile(exarg_T *eap UNUSED)
3880{
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003881 long todo = (long)func_hashtab.ht_used;
3882 int changed = func_hashtab.ht_changed;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003883 hashitem_T *hi;
3884 ufunc_T *ufunc;
3885
3886 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
3887 {
3888 if (!HASHITEM_EMPTY(hi))
3889 {
3890 --todo;
3891 ufunc = HI2UF(hi);
3892 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
Bram Moolenaar96f8f492020-09-09 17:08:51 +02003893 && ufunc->uf_def_status == UF_TO_BE_COMPILED
3894 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003895 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02003896 compile_def_function(ufunc, FALSE, NULL);
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003897
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003898 if (func_hashtab.ht_changed != changed)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003899 {
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003900 // a function has been added or removed, need to start over
3901 todo = (long)func_hashtab.ht_used;
3902 changed = func_hashtab.ht_changed;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003903 hi = func_hashtab.ht_array;
Bram Moolenaar285b1892020-05-26 11:37:26 +02003904 --hi;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003905 }
3906 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003907 }
3908 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003909}
3910
3911/*
3912 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3913 * Return 2 if "p" starts with "s:".
3914 * Return 0 otherwise.
3915 */
3916 int
3917eval_fname_script(char_u *p)
3918{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003919 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3920 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003921 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3922 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3923 return 5;
3924 if (p[0] == 's' && p[1] == ':')
3925 return 2;
3926 return 0;
3927}
3928
3929 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003930translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003931{
3932 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003933 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003934 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003935}
3936
3937/*
3938 * Return TRUE when "ufunc" has old-style "..." varargs
3939 * or named varargs "...name: type".
3940 */
3941 int
3942has_varargs(ufunc_T *ufunc)
3943{
3944 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003945}
3946
3947/*
3948 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003949 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003950 */
3951 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003952function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003953{
3954 char_u *nm = name;
3955 char_u *p;
3956 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003957 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003958 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003959
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003960 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3961 if (no_deref)
3962 flag |= TFN_NO_DEREF;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003963 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003964 nm = skipwhite(nm);
3965
Bram Moolenaare38eab22019-12-05 21:50:01 +01003966 // Only accept "funcname", "funcname ", "funcname (..." and
3967 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003968 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003969 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003970 vim_free(p);
3971 return n;
3972}
3973
Bram Moolenaar113e1072019-01-20 15:30:40 +01003974#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003975 char_u *
3976get_expanded_name(char_u *name, int check)
3977{
3978 char_u *nm = name;
3979 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003980 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003981
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003982 p = trans_function_name(&nm, &is_global, FALSE,
3983 TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003984
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003985 if (p != NULL && *nm == NUL
3986 && (!check || translated_function_exists(p, is_global)))
3987 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003988
3989 vim_free(p);
3990 return NULL;
3991}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003992#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003993
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003994/*
3995 * Function given to ExpandGeneric() to obtain the list of user defined
3996 * function names.
3997 */
3998 char_u *
3999get_user_func_name(expand_T *xp, int idx)
4000{
4001 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004002 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004003 static hashitem_T *hi;
4004 ufunc_T *fp;
4005
4006 if (idx == 0)
4007 {
4008 done = 0;
4009 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004010 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004011 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004012 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004013 {
4014 if (done++ > 0)
4015 ++hi;
4016 while (HASHITEM_EMPTY(hi))
4017 ++hi;
4018 fp = HI2UF(hi);
4019
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 // don't show dead, dict and lambda functions
4021 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02004022 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004024
4025 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004026 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004027
4028 cat_func_name(IObuff, fp);
4029 if (xp->xp_context != EXPAND_USER_FUNC)
4030 {
4031 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004033 STRCAT(IObuff, ")");
4034 }
4035 return IObuff;
4036 }
4037 return NULL;
4038}
4039
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004040/*
4041 * ":delfunction {name}"
4042 */
4043 void
4044ex_delfunction(exarg_T *eap)
4045{
4046 ufunc_T *fp = NULL;
4047 char_u *p;
4048 char_u *name;
4049 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004050 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004051
4052 p = eap->arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004053 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004054 vim_free(fudi.fd_newkey);
4055 if (name == NULL)
4056 {
4057 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004058 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004059 return;
4060 }
4061 if (!ends_excmd(*skipwhite(p)))
4062 {
4063 vim_free(name);
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004064 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004065 return;
4066 }
4067 eap->nextcmd = check_nextcmd(p);
4068 if (eap->nextcmd != NULL)
4069 *p = NUL;
4070
4071 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004072 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004073 vim_free(name);
4074
4075 if (!eap->skip)
4076 {
4077 if (fp == NULL)
4078 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02004079 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004080 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004081 return;
4082 }
4083 if (fp->uf_calls > 0)
4084 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004085 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004086 return;
4087 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004088 if (fp->uf_flags & FC_VIM9)
4089 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004090 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004091 return;
4092 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004093
4094 if (fudi.fd_dict != NULL)
4095 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004096 // Delete the dict item that refers to the function, it will
4097 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004098 dictitem_remove(fudi.fd_dict, fudi.fd_di);
4099 }
4100 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004101 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004102 // A normal function (not a numbered function or lambda) has a
4103 // refcount of 1 for the entry in the hashtable. When deleting
4104 // it and the refcount is more than one, it should be kept.
4105 // A numbered function and lambda should be kept if the refcount is
4106 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004107 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004108 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004109 // Function is still referenced somewhere. Don't free it but
4110 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004111 if (func_remove(fp))
4112 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004113 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004114 }
4115 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004116 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004117 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004118 }
4119}
4120
4121/*
4122 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004123 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004124 */
4125 void
4126func_unref(char_u *name)
4127{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004128 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004129
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004130 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004131 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004132 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004133 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004134 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004135#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004136 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004137#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01004138 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004139 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004140 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004141 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004142 // Only delete it when it's not being used. Otherwise it's done
4143 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004144 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004145 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02004146 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004147}
4148
4149/*
4150 * Unreference a Function: decrement the reference count and free it when it
4151 * becomes zero.
4152 */
4153 void
4154func_ptr_unref(ufunc_T *fp)
4155{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004156 if (fp != NULL && --fp->uf_refcount <= 0)
4157 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004158 // Only delete it when it's not being used. Otherwise it's done
4159 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02004160 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004161 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004162 }
4163}
4164
4165/*
4166 * Count a reference to a Function.
4167 */
4168 void
4169func_ref(char_u *name)
4170{
4171 ufunc_T *fp;
4172
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004173 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004174 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004175 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004176 if (fp != NULL)
4177 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004178 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01004179 // Only give an error for a numbered function.
4180 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01004181 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004182}
4183
4184/*
4185 * Count a reference to a Function.
4186 */
4187 void
4188func_ptr_ref(ufunc_T *fp)
4189{
4190 if (fp != NULL)
4191 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004192}
4193
4194/*
4195 * Return TRUE if items in "fc" do not have "copyID". That means they are not
4196 * referenced from anywhere that is in use.
4197 */
4198 static int
4199can_free_funccal(funccall_T *fc, int copyID)
4200{
4201 return (fc->l_varlist.lv_copyID != copyID
4202 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004203 && fc->l_avars.dv_copyID != copyID
4204 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004205}
4206
4207/*
4208 * ":return [expr]"
4209 */
4210 void
4211ex_return(exarg_T *eap)
4212{
4213 char_u *arg = eap->arg;
4214 typval_T rettv;
4215 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004216 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004217
4218 if (current_funccal == NULL)
4219 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004220 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004221 return;
4222 }
4223
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004224 CLEAR_FIELD(evalarg);
4225 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
4226
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004227 if (eap->skip)
4228 ++emsg_skip;
4229
4230 eap->nextcmd = NULL;
4231 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004232 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004233 {
4234 if (!eap->skip)
4235 returning = do_return(eap, FALSE, TRUE, &rettv);
4236 else
4237 clear_tv(&rettv);
4238 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004239 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004240 else if (!eap->skip)
4241 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004242 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01004243 update_force_abort();
4244
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004245 /*
4246 * Return unless the expression evaluation has been cancelled due to an
4247 * aborting error, an interrupt, or an exception.
4248 */
4249 if (!aborting())
4250 returning = do_return(eap, FALSE, TRUE, NULL);
4251 }
4252
Bram Moolenaare38eab22019-12-05 21:50:01 +01004253 // When skipping or the return gets pending, advance to the next command
4254 // in this line (!returning). Otherwise, ignore the rest of the line.
4255 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004256 if (returning)
4257 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004258 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004259 eap->nextcmd = check_nextcmd(arg);
4260
4261 if (eap->skip)
4262 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02004263 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004264}
4265
4266/*
4267 * ":1,25call func(arg1, arg2)" function call.
4268 */
4269 void
4270ex_call(exarg_T *eap)
4271{
4272 char_u *arg = eap->arg;
4273 char_u *startarg;
4274 char_u *name;
4275 char_u *tofree;
4276 int len;
4277 typval_T rettv;
4278 linenr_T lnum;
4279 int doesrange;
4280 int failed = FALSE;
4281 funcdict_T fudi;
4282 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004283 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004284
Bram Moolenaare6b53242020-07-01 17:28:33 +02004285 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004286 if (eap->skip)
4287 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004288 // trans_function_name() doesn't work well when skipping, use eval0()
4289 // instead to skip to any following command, e.g. for:
4290 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004291 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004292 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004293 clear_tv(&rettv);
4294 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004295 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004296 return;
4297 }
4298
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004299 tofree = trans_function_name(&arg, NULL, eap->skip,
4300 TFN_INT, &fudi, &partial);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004301 if (fudi.fd_newkey != NULL)
4302 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004303 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004304 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004305 vim_free(fudi.fd_newkey);
4306 }
4307 if (tofree == NULL)
4308 return;
4309
Bram Moolenaare38eab22019-12-05 21:50:01 +01004310 // Increase refcount on dictionary, it could get deleted when evaluating
4311 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004312 if (fudi.fd_dict != NULL)
4313 ++fudi.fd_dict->dv_refcount;
4314
Bram Moolenaare38eab22019-12-05 21:50:01 +01004315 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
4316 // contents. For VAR_PARTIAL get its partial, unless we already have one
4317 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004318 len = (int)STRLEN(tofree);
4319 name = deref_func_name(tofree, &len,
4320 partial != NULL ? NULL : &partial, FALSE);
4321
Bram Moolenaare38eab22019-12-05 21:50:01 +01004322 // Skip white space to allow ":call func ()". Not good, but required for
4323 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004324 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004325 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004326
4327 if (*startarg != '(')
4328 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004330 goto end;
4331 }
4332
4333 /*
4334 * When skipping, evaluate the function once, to find the end of the
4335 * arguments.
4336 * When the function takes a range, this is discovered after the first
4337 * call, and the loop is broken.
4338 */
4339 if (eap->skip)
4340 {
4341 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004342 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004343 }
4344 else
4345 lnum = eap->line1;
4346 for ( ; lnum <= eap->line2; ++lnum)
4347 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004348 funcexe_T funcexe;
4349
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004350 if (!eap->skip && eap->addr_count > 0)
4351 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004352 if (lnum > curbuf->b_ml.ml_line_count)
4353 {
4354 // If the function deleted lines or switched to another buffer
4355 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004356 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004357 break;
4358 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004359 curwin->w_cursor.lnum = lnum;
4360 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004361 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004362 }
4363 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004364
Bram Moolenaara80faa82020-04-12 19:37:17 +02004365 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004366 funcexe.firstline = eap->line1;
4367 funcexe.lastline = eap->line2;
4368 funcexe.doesrange = &doesrange;
4369 funcexe.evaluate = !eap->skip;
4370 funcexe.partial = partial;
4371 funcexe.selfdict = fudi.fd_dict;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004372 if (get_func_tv(name, -1, &rettv, &arg, &evalarg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004373 {
4374 failed = TRUE;
4375 break;
4376 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01004377 if (has_watchexpr())
4378 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004379
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004380 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004381 if (handle_subscript(&arg, &rettv,
4382 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004383 {
4384 failed = TRUE;
4385 break;
4386 }
4387
4388 clear_tv(&rettv);
4389 if (doesrange || eap->skip)
4390 break;
4391
Bram Moolenaare38eab22019-12-05 21:50:01 +01004392 // Stop when immediately aborting on error, or when an interrupt
4393 // occurred or an exception was thrown but not caught.
4394 // get_func_tv() returned OK, so that the check for trailing
4395 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004396 if (aborting())
4397 break;
4398 }
4399 if (eap->skip)
4400 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004401 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004402
Bram Moolenaare51bb172020-02-16 19:42:23 +01004403 // When inside :try we need to check for following "| catch".
4404 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004405 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004406 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02004407 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004408 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004409 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004410 if (!failed)
4411 {
4412 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004413 semsg(_(e_trailing_arg), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004414 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004415 }
4416 else
4417 eap->nextcmd = check_nextcmd(arg);
4418 }
4419
4420end:
4421 dict_unref(fudi.fd_dict);
4422 vim_free(tofree);
4423}
4424
4425/*
4426 * Return from a function. Possibly makes the return pending. Also called
4427 * for a pending return at the ":endtry" or after returning from an extra
4428 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
4429 * when called due to a ":return" command. "rettv" may point to a typval_T
4430 * with the return rettv. Returns TRUE when the return can be carried out,
4431 * FALSE when the return gets pending.
4432 */
4433 int
4434do_return(
4435 exarg_T *eap,
4436 int reanimate,
4437 int is_cmd,
4438 void *rettv)
4439{
4440 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01004441 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004442
4443 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004444 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004445 current_funccal->returned = FALSE;
4446
4447 /*
4448 * Cleanup (and inactivate) conditionals, but stop when a try conditional
4449 * not in its finally clause (which then is to be executed next) is found.
4450 * In this case, make the ":return" pending for execution at the ":endtry".
4451 * Otherwise, return normally.
4452 */
4453 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
4454 if (idx >= 0)
4455 {
4456 cstack->cs_pending[idx] = CSTP_RETURN;
4457
4458 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004459 // A pending return again gets pending. "rettv" points to an
4460 // allocated variable with the rettv of the original ":return"'s
4461 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004462 cstack->cs_rettv[idx] = rettv;
4463 else
4464 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004465 // When undoing a return in order to make it pending, get the stored
4466 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004467 if (reanimate)
4468 rettv = current_funccal->rettv;
4469
4470 if (rettv != NULL)
4471 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004472 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004473 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
4474 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
4475 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004476 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004477 }
4478 else
4479 cstack->cs_rettv[idx] = NULL;
4480
4481 if (reanimate)
4482 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004483 // The pending return value could be overwritten by a ":return"
4484 // without argument in a finally clause; reset the default
4485 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004486 current_funccal->rettv->v_type = VAR_NUMBER;
4487 current_funccal->rettv->vval.v_number = 0;
4488 }
4489 }
4490 report_make_pending(CSTP_RETURN, rettv);
4491 }
4492 else
4493 {
4494 current_funccal->returned = TRUE;
4495
Bram Moolenaare38eab22019-12-05 21:50:01 +01004496 // If the return is carried out now, store the return value. For
4497 // a return immediately after reanimation, the value is already
4498 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004499 if (!reanimate && rettv != NULL)
4500 {
4501 clear_tv(current_funccal->rettv);
4502 *current_funccal->rettv = *(typval_T *)rettv;
4503 if (!is_cmd)
4504 vim_free(rettv);
4505 }
4506 }
4507
4508 return idx < 0;
4509}
4510
4511/*
4512 * Free the variable with a pending return value.
4513 */
4514 void
4515discard_pending_return(void *rettv)
4516{
4517 free_tv((typval_T *)rettv);
4518}
4519
4520/*
4521 * Generate a return command for producing the value of "rettv". The result
4522 * is an allocated string. Used by report_pending() for verbose messages.
4523 */
4524 char_u *
4525get_return_cmd(void *rettv)
4526{
4527 char_u *s = NULL;
4528 char_u *tofree = NULL;
4529 char_u numbuf[NUMBUFLEN];
4530
4531 if (rettv != NULL)
4532 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
4533 if (s == NULL)
4534 s = (char_u *)"";
4535
4536 STRCPY(IObuff, ":return ");
4537 STRNCPY(IObuff + 8, s, IOSIZE - 8);
4538 if (STRLEN(s) + 8 >= IOSIZE)
4539 STRCPY(IObuff + IOSIZE - 4, "...");
4540 vim_free(tofree);
4541 return vim_strsave(IObuff);
4542}
4543
4544/*
4545 * Get next function line.
4546 * Called by do_cmdline() to get the next line.
4547 * Returns allocated string, or NULL for end of function.
4548 */
4549 char_u *
4550get_func_line(
4551 int c UNUSED,
4552 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02004553 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004554 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004555{
4556 funccall_T *fcp = (funccall_T *)cookie;
4557 ufunc_T *fp = fcp->func;
4558 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004559 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004560
Bram Moolenaare38eab22019-12-05 21:50:01 +01004561 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004562 if (fcp->dbg_tick != debug_tick)
4563 {
4564 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004565 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004566 fcp->dbg_tick = debug_tick;
4567 }
4568#ifdef FEAT_PROFILE
4569 if (do_profiling == PROF_YES)
4570 func_line_end(cookie);
4571#endif
4572
4573 gap = &fp->uf_lines;
4574 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4575 || fcp->returned)
4576 retval = NULL;
4577 else
4578 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004579 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004580 while (fcp->linenr < gap->ga_len
4581 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
4582 ++fcp->linenr;
4583 if (fcp->linenr >= gap->ga_len)
4584 retval = NULL;
4585 else
4586 {
4587 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004588 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004589#ifdef FEAT_PROFILE
4590 if (do_profiling == PROF_YES)
4591 func_line_start(cookie);
4592#endif
4593 }
4594 }
4595
Bram Moolenaare38eab22019-12-05 21:50:01 +01004596 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004597 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004598 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004599 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004600 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004601 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004602 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004603 fcp->dbg_tick = debug_tick;
4604 }
4605
4606 return retval;
4607}
4608
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004609/*
4610 * Return TRUE if the currently active function should be ended, because a
4611 * return was encountered or an error occurred. Used inside a ":while".
4612 */
4613 int
4614func_has_ended(void *cookie)
4615{
4616 funccall_T *fcp = (funccall_T *)cookie;
4617
Bram Moolenaare38eab22019-12-05 21:50:01 +01004618 // Ignore the "abort" flag if the abortion behavior has been changed due to
4619 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004620 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4621 || fcp->returned);
4622}
4623
4624/*
4625 * return TRUE if cookie indicates a function which "abort"s on errors.
4626 */
4627 int
4628func_has_abort(
4629 void *cookie)
4630{
4631 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
4632}
4633
4634
4635/*
4636 * Turn "dict.Func" into a partial for "Func" bound to "dict".
4637 * Don't do this when "Func" is already a partial that was bound
4638 * explicitly (pt_auto is FALSE).
4639 * Changes "rettv" in-place.
4640 * Returns the updated "selfdict_in".
4641 */
4642 dict_T *
4643make_partial(dict_T *selfdict_in, typval_T *rettv)
4644{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004645 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004646 char_u *tofree = NULL;
4647 ufunc_T *fp;
4648 char_u fname_buf[FLEN_FIXED + 1];
4649 int error;
4650 dict_T *selfdict = selfdict_in;
4651
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004652 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
4653 fp = rettv->vval.v_partial->pt_func;
4654 else
4655 {
4656 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
4657 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004658 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004659 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004660 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004661 vim_free(tofree);
4662 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004663
4664 if (fp != NULL && (fp->uf_flags & FC_DICT))
4665 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004666 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004667
4668 if (pt != NULL)
4669 {
4670 pt->pt_refcount = 1;
4671 pt->pt_dict = selfdict;
4672 pt->pt_auto = TRUE;
4673 selfdict = NULL;
4674 if (rettv->v_type == VAR_FUNC)
4675 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004676 // Just a function: Take over the function name and use
4677 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004678 pt->pt_name = rettv->vval.v_string;
4679 }
4680 else
4681 {
4682 partial_T *ret_pt = rettv->vval.v_partial;
4683 int i;
4684
Bram Moolenaare38eab22019-12-05 21:50:01 +01004685 // Partial: copy the function name, use selfdict and copy
4686 // args. Can't take over name or args, the partial might
4687 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004688 if (ret_pt->pt_name != NULL)
4689 {
4690 pt->pt_name = vim_strsave(ret_pt->pt_name);
4691 func_ref(pt->pt_name);
4692 }
4693 else
4694 {
4695 pt->pt_func = ret_pt->pt_func;
4696 func_ptr_ref(pt->pt_func);
4697 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004698 if (ret_pt->pt_argc > 0)
4699 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004700 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004701 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004702 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004703 pt->pt_argc = 0;
4704 else
4705 {
4706 pt->pt_argc = ret_pt->pt_argc;
4707 for (i = 0; i < pt->pt_argc; i++)
4708 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
4709 }
4710 }
4711 partial_unref(ret_pt);
4712 }
4713 rettv->v_type = VAR_PARTIAL;
4714 rettv->vval.v_partial = pt;
4715 }
4716 }
4717 return selfdict;
4718}
4719
4720/*
4721 * Return the name of the executed function.
4722 */
4723 char_u *
4724func_name(void *cookie)
4725{
4726 return ((funccall_T *)cookie)->func->uf_name;
4727}
4728
4729/*
4730 * Return the address holding the next breakpoint line for a funccall cookie.
4731 */
4732 linenr_T *
4733func_breakpoint(void *cookie)
4734{
4735 return &((funccall_T *)cookie)->breakpoint;
4736}
4737
4738/*
4739 * Return the address holding the debug tick for a funccall cookie.
4740 */
4741 int *
4742func_dbg_tick(void *cookie)
4743{
4744 return &((funccall_T *)cookie)->dbg_tick;
4745}
4746
4747/*
4748 * Return the nesting level for a funccall cookie.
4749 */
4750 int
4751func_level(void *cookie)
4752{
4753 return ((funccall_T *)cookie)->level;
4754}
4755
4756/*
4757 * Return TRUE when a function was ended by a ":return" command.
4758 */
4759 int
4760current_func_returned(void)
4761{
4762 return current_funccal->returned;
4763}
4764
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004765 int
4766free_unref_funccal(int copyID, int testing)
4767{
4768 int did_free = FALSE;
4769 int did_free_funccal = FALSE;
4770 funccall_T *fc, **pfc;
4771
4772 for (pfc = &previous_funccal; *pfc != NULL; )
4773 {
4774 if (can_free_funccal(*pfc, copyID))
4775 {
4776 fc = *pfc;
4777 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01004778 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004779 did_free = TRUE;
4780 did_free_funccal = TRUE;
4781 }
4782 else
4783 pfc = &(*pfc)->caller;
4784 }
4785 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004786 // When a funccal was freed some more items might be garbage
4787 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004788 (void)garbage_collect(testing);
4789
4790 return did_free;
4791}
4792
4793/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004794 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004795 */
4796 static funccall_T *
4797get_funccal(void)
4798{
4799 int i;
4800 funccall_T *funccal;
4801 funccall_T *temp_funccal;
4802
4803 funccal = current_funccal;
4804 if (debug_backtrace_level > 0)
4805 {
4806 for (i = 0; i < debug_backtrace_level; i++)
4807 {
4808 temp_funccal = funccal->caller;
4809 if (temp_funccal)
4810 funccal = temp_funccal;
4811 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004812 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004813 debug_backtrace_level = i;
4814 }
4815 }
4816 return funccal;
4817}
4818
4819/*
4820 * Return the hashtable used for local variables in the current funccal.
4821 * Return NULL if there is no current funccal.
4822 */
4823 hashtab_T *
4824get_funccal_local_ht()
4825{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004826 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004827 return NULL;
4828 return &get_funccal()->l_vars.dv_hashtab;
4829}
4830
4831/*
4832 * Return the l: scope variable.
4833 * Return NULL if there is no current funccal.
4834 */
4835 dictitem_T *
4836get_funccal_local_var()
4837{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004838 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004839 return NULL;
4840 return &get_funccal()->l_vars_var;
4841}
4842
4843/*
4844 * Return the hashtable used for argument in the current funccal.
4845 * Return NULL if there is no current funccal.
4846 */
4847 hashtab_T *
4848get_funccal_args_ht()
4849{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004851 return NULL;
4852 return &get_funccal()->l_avars.dv_hashtab;
4853}
4854
4855/*
4856 * Return the a: scope variable.
4857 * Return NULL if there is no current funccal.
4858 */
4859 dictitem_T *
4860get_funccal_args_var()
4861{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004862 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004863 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004864 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004865}
4866
4867/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004868 * List function variables, if there is a function.
4869 */
4870 void
4871list_func_vars(int *first)
4872{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004873 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004874 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004875 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004876}
4877
4878/*
4879 * If "ht" is the hashtable for local variables in the current funccal, return
4880 * the dict that contains it.
4881 * Otherwise return NULL.
4882 */
4883 dict_T *
4884get_current_funccal_dict(hashtab_T *ht)
4885{
4886 if (current_funccal != NULL
4887 && ht == &current_funccal->l_vars.dv_hashtab)
4888 return &current_funccal->l_vars;
4889 return NULL;
4890}
4891
4892/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004893 * Search hashitem in parent scope.
4894 */
4895 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004896find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004897{
4898 funccall_T *old_current_funccal = current_funccal;
4899 hashtab_T *ht;
4900 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004901 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004902
4903 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4904 return NULL;
4905
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004906 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004907 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004908 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004909 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004910 ht = find_var_ht(name, &varname);
4911 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004912 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004913 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004914 if (!HASHITEM_EMPTY(hi))
4915 {
4916 *pht = ht;
4917 break;
4918 }
4919 }
4920 if (current_funccal == current_funccal->func->uf_scoped)
4921 break;
4922 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004923 }
4924 current_funccal = old_current_funccal;
4925
4926 return hi;
4927}
4928
4929/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004930 * Search variable in parent scope.
4931 */
4932 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004933find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004934{
4935 dictitem_T *v = NULL;
4936 funccall_T *old_current_funccal = current_funccal;
4937 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004938 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004939
4940 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4941 return NULL;
4942
Bram Moolenaare38eab22019-12-05 21:50:01 +01004943 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004944 current_funccal = current_funccal->func->uf_scoped;
4945 while (current_funccal)
4946 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004947 ht = find_var_ht(name, &varname);
4948 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004949 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004950 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004951 if (v != NULL)
4952 break;
4953 }
4954 if (current_funccal == current_funccal->func->uf_scoped)
4955 break;
4956 current_funccal = current_funccal->func->uf_scoped;
4957 }
4958 current_funccal = old_current_funccal;
4959
4960 return v;
4961}
4962
4963/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004964 * Set "copyID + 1" in previous_funccal and callers.
4965 */
4966 int
4967set_ref_in_previous_funccal(int copyID)
4968{
4969 int abort = FALSE;
4970 funccall_T *fc;
4971
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004972 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004973 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004974 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004975 abort = abort
4976 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4977 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004978 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004979 }
4980 return abort;
4981}
4982
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004983 static int
4984set_ref_in_funccal(funccall_T *fc, int copyID)
4985{
4986 int abort = FALSE;
4987
4988 if (fc->fc_copyID != copyID)
4989 {
4990 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004991 abort = abort
4992 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
4993 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004994 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004995 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004996 }
4997 return abort;
4998}
4999
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005000/*
5001 * Set "copyID" in all local vars and arguments in the call stack.
5002 */
5003 int
5004set_ref_in_call_stack(int copyID)
5005{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005006 int abort = FALSE;
5007 funccall_T *fc;
5008 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005009
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005010 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005011 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005012
5013 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005014 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
5015 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005016 abort = abort || set_ref_in_funccal(fc, copyID);
5017
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005018 return abort;
5019}
5020
5021/*
5022 * Set "copyID" in all functions available by name.
5023 */
5024 int
5025set_ref_in_functions(int copyID)
5026{
5027 int todo;
5028 hashitem_T *hi = NULL;
5029 int abort = FALSE;
5030 ufunc_T *fp;
5031
5032 todo = (int)func_hashtab.ht_used;
5033 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005034 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005035 if (!HASHITEM_EMPTY(hi))
5036 {
5037 --todo;
5038 fp = HI2UF(hi);
5039 if (!func_name_refcount(fp->uf_name))
5040 abort = abort || set_ref_in_func(NULL, fp, copyID);
5041 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005042 }
5043 return abort;
5044}
5045
5046/*
5047 * Set "copyID" in all function arguments.
5048 */
5049 int
5050set_ref_in_func_args(int copyID)
5051{
5052 int i;
5053 int abort = FALSE;
5054
5055 for (i = 0; i < funcargs.ga_len; ++i)
5056 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
5057 copyID, NULL, NULL);
5058 return abort;
5059}
5060
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005061/*
5062 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005063 * Returns TRUE if setting references failed somehow.
5064 */
5065 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005066set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005067{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005068 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005069 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01005070 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005071 char_u fname_buf[FLEN_FIXED + 1];
5072 char_u *tofree = NULL;
5073 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005074 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005075
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005076 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005077 return FALSE;
5078
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005079 if (fp_in == NULL)
5080 {
5081 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005082 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005083 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005084 if (fp != NULL)
5085 {
5086 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005087 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005088 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02005089
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005090 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005091 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005092}
5093
Bram Moolenaare38eab22019-12-05 21:50:01 +01005094#endif // FEAT_EVAL