blob: bc3dd0e9ae383d735465119db5026ddfc9662ad4 [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 Moolenaaradc8e442020-12-31 18:28:18 +01002928 * Check if "*cmd" points to a function command and if so advance "*cmd" and
2929 * return TRUE.
2930 * Otherwise return FALSE;
2931 * Do not consider "function(" to be a command.
2932 */
2933 static int
2934is_function_cmd(char_u **cmd)
2935{
2936 char_u *p = *cmd;
2937
2938 if (checkforcmd(&p, "function", 2))
2939 {
2940 if (*p == '(')
2941 return FALSE;
2942 *cmd = p;
2943 return TRUE;
2944 }
2945 return FALSE;
2946}
2947
2948/*
Bram Moolenaar04b12692020-05-04 23:24:44 +02002949 * ":function" also supporting nested ":def".
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002950 * When "name_arg" is not NULL this is a nested function, using "name_arg" for
2951 * the function name.
Bram Moolenaar04b12692020-05-04 23:24:44 +02002952 * Returns a pointer to the function or NULL if no function defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002953 */
Bram Moolenaar04b12692020-05-04 23:24:44 +02002954 ufunc_T *
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02002955define_function(exarg_T *eap, char_u *name_arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002956{
2957 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002958 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002959 int j;
2960 int c;
2961 int saved_did_emsg;
2962 int saved_wait_return = need_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002963 char_u *name = name_arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002964 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002965 char_u *p;
2966 char_u *arg;
2967 char_u *line_arg = NULL;
2968 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002970 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002971 garray_T newlines;
2972 int varargs = FALSE;
2973 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 char_u *ret_type = NULL;
Bram Moolenaar04b12692020-05-04 23:24:44 +02002975 ufunc_T *fp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002976 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002977 int indent;
2978 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979#define MAX_FUNC_NESTING 50
2980 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002981 dictitem_T *v;
2982 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002983 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002984 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002985 hashitem_T *hi;
Bram Moolenaardcc58e02020-12-28 20:53:21 +01002986 getline_opt_T getline_options;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002987 linenr_T sourcing_lnum_off;
2988 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002989 int is_heredoc = FALSE;
2990 char_u *skip_until = NULL;
2991 char_u *heredoc_trimmed = NULL;
Bram Moolenaare7e48382020-07-22 18:17:08 +02002992 int vim9script = in_vim9script();
Bram Moolenaareef21022020-08-01 22:16:43 +02002993 imported_T *import = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002994
2995 /*
2996 * ":function" without argument: list functions.
2997 */
Bram Moolenaara72cfb82020-04-23 17:07:30 +02002998 if (ends_excmd2(eap->cmd, eap->arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002999 {
3000 if (!eap->skip)
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003001 list_functions(NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003002 eap->nextcmd = check_nextcmd(eap->arg);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003003 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003004 }
3005
3006 /*
3007 * ":function /pat": list functions matching pattern.
3008 */
3009 if (*eap->arg == '/')
3010 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003011 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003012 if (!eap->skip)
3013 {
3014 regmatch_T regmatch;
3015
3016 c = *p;
3017 *p = NUL;
3018 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
3019 *p = c;
3020 if (regmatch.regprog != NULL)
3021 {
3022 regmatch.rm_ic = p_ic;
Bram Moolenaar3fffa972020-06-05 21:06:10 +02003023 list_functions(&regmatch);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003024 vim_regfree(regmatch.regprog);
3025 }
3026 }
3027 if (*p == '/')
3028 ++p;
3029 eap->nextcmd = check_nextcmd(p);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003030 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003031 }
3032
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 ga_init(&newargs);
3034 ga_init(&argtypes);
3035 ga_init(&default_args);
3036
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003037 /*
3038 * Get the function name. There are these situations:
3039 * func normal function name
3040 * "name" == func, "fudi.fd_dict" == NULL
3041 * dict.func new dictionary entry
3042 * "name" == NULL, "fudi.fd_dict" set,
3043 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
3044 * dict.func existing dict entry with a Funcref
3045 * "name" == func, "fudi.fd_dict" set,
3046 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
3047 * dict.func existing dict entry that's not a Funcref
3048 * "name" == NULL, "fudi.fd_dict" set,
3049 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
3050 * s:func script-local function name
3051 * g:func global function name, same as "func"
3052 */
3053 p = eap->arg;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003054 if (name_arg != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003055 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02003056 // nested function, argument is (args).
3057 paren = TRUE;
3058 CLEAR_FIELD(fudi);
3059 }
3060 else
3061 {
3062 name = trans_function_name(&p, &is_global, eap->skip,
3063 TFN_NO_AUTOLOAD, &fudi, NULL);
3064 paren = (vim_strchr(p, '(') != NULL);
3065 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003066 {
Bram Moolenaar04b12692020-05-04 23:24:44 +02003067 /*
3068 * Return on an invalid expression in braces, unless the expression
3069 * evaluation has been cancelled due to an aborting error, an
3070 * interrupt, or an exception.
3071 */
3072 if (!aborting())
3073 {
3074 if (!eap->skip && fudi.fd_newkey != NULL)
3075 semsg(_(e_dictkey), fudi.fd_newkey);
3076 vim_free(fudi.fd_newkey);
3077 return NULL;
3078 }
3079 else
3080 eap->skip = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003081 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003082 }
3083
Bram Moolenaare38eab22019-12-05 21:50:01 +01003084 // An error in a function call during evaluation of an expression in magic
3085 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003086 saved_did_emsg = did_emsg;
3087 did_emsg = FALSE;
3088
3089 /*
3090 * ":function func" with only function name: list function.
3091 */
3092 if (!paren)
3093 {
3094 if (!ends_excmd(*skipwhite(p)))
3095 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003096 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003097 goto ret_free;
3098 }
3099 eap->nextcmd = check_nextcmd(p);
3100 if (eap->nextcmd != NULL)
3101 *p = NUL;
3102 if (!eap->skip && !got_int)
3103 {
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003104 fp = find_func(name, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003105 if (fp == NULL && ASCII_ISUPPER(*eap->arg))
3106 {
3107 char_u *up = untrans_function_name(name);
3108
3109 // With Vim9 script the name was made script-local, if not
3110 // found try again with the original name.
Bram Moolenaarec9749f2020-04-18 20:51:40 +02003111 if (up != NULL)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003112 fp = find_func(up, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003113 }
3114
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003115 if (fp != NULL)
3116 {
3117 list_func_head(fp, TRUE);
3118 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
3119 {
3120 if (FUNCLINE(fp, j) == NULL)
3121 continue;
3122 msg_putchar('\n');
3123 msg_outnum((long)(j + 1));
3124 if (j < 9)
3125 msg_putchar(' ');
3126 if (j < 99)
3127 msg_putchar(' ');
3128 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003129 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003130 ui_breakcheck();
3131 }
3132 if (!got_int)
3133 {
3134 msg_putchar('\n');
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003135 if (fp->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 msg_puts(" enddef");
3137 else
3138 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003139 }
3140 }
3141 else
Bram Moolenaara26b9702020-04-18 19:53:28 +02003142 emsg_funcname(N_("E123: Undefined function: %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003143 }
3144 goto ret_free;
3145 }
3146
3147 /*
3148 * ":function name(arg1, arg2)" Define function.
3149 */
3150 p = skipwhite(p);
3151 if (*p != '(')
3152 {
3153 if (!eap->skip)
3154 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003155 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003156 goto ret_free;
3157 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003158 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003159 if (vim_strchr(p, '(') != NULL)
3160 p = vim_strchr(p, '(');
3161 }
3162 p = skipwhite(p + 1);
3163
Bram Moolenaar925e9fd2020-07-25 15:41:11 +02003164 // In Vim9 script only global functions can be redefined.
3165 if (vim9script && eap->forceit && !is_global)
3166 {
3167 emsg(_(e_nobang));
3168 goto ret_free;
3169 }
3170
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003171 ga_init2(&newlines, (int)sizeof(char_u *), 3);
3172
Bram Moolenaar04b12692020-05-04 23:24:44 +02003173 if (!eap->skip && name_arg == NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003174 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003175 // Check the name of the function. Unless it's a dictionary function
3176 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003177 if (name != NULL)
3178 arg = name;
3179 else
3180 arg = fudi.fd_newkey;
3181 if (arg != NULL && (fudi.fd_di == NULL
3182 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
3183 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
3184 {
3185 if (*arg == K_SPECIAL)
3186 j = 3;
3187 else
3188 j = 0;
3189 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
3190 : eval_isnamec(arg[j])))
3191 ++j;
3192 if (arg[j] != NUL)
3193 emsg_funcname((char *)e_invarg2, arg);
3194 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003195 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003196 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003197 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003198 }
3199
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003200 // This may get more lines and make the pointers into the first line
3201 // invalid.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 if (get_function_args(&p, ')', &newargs,
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003203 eap->cmdidx == CMD_def ? &argtypes : NULL, FALSE,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003204 &varargs, &default_args, eap->skip,
3205 eap, &line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003206 goto errret_2;
3207
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003209 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 // find the return type: :def Func(): type
3211 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003212 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 ret_type = skipwhite(p + 1);
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02003214 p = skip_type(ret_type, FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003216 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003217 ret_type = vim_strnsave(ret_type, p - ret_type);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003219 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003221 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003222 semsg(_(e_expected_type_str), ret_type);
Bram Moolenaarb8ce6b02020-04-23 22:23:14 +02003223 ret_type = NULL;
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003224 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003225 }
Bram Moolenaare7e48382020-07-22 18:17:08 +02003226 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003227 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 else
3229 // find extra arguments "range", "dict", "abort" and "closure"
3230 for (;;)
3231 {
3232 p = skipwhite(p);
3233 if (STRNCMP(p, "range", 5) == 0)
3234 {
3235 flags |= FC_RANGE;
3236 p += 5;
3237 }
3238 else if (STRNCMP(p, "dict", 4) == 0)
3239 {
3240 flags |= FC_DICT;
3241 p += 4;
3242 }
3243 else if (STRNCMP(p, "abort", 5) == 0)
3244 {
3245 flags |= FC_ABORT;
3246 p += 5;
3247 }
3248 else if (STRNCMP(p, "closure", 7) == 0)
3249 {
3250 flags |= FC_CLOSURE;
3251 p += 7;
3252 if (current_funccal == NULL)
3253 {
3254 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
3255 name == NULL ? (char_u *)"" : name);
3256 goto erret;
3257 }
3258 }
3259 else
3260 break;
3261 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003262
Bram Moolenaare38eab22019-12-05 21:50:01 +01003263 // When there is a line break use what follows for the function body.
3264 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003265 if (*p == '\n')
3266 line_arg = p + 1;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003267 else if (*p != NUL
Bram Moolenaar98981072020-07-29 14:40:25 +02003268 && !(*p == '"' && (!vim9script || eap->cmdidx == CMD_function)
3269 && eap->cmdidx != CMD_def)
Bram Moolenaare7e48382020-07-22 18:17:08 +02003270 && !(*p == '#' && (vim9script || eap->cmdidx == CMD_def))
3271 && !eap->skip
3272 && !did_emsg)
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003273 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003274
3275 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
3277 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003278 */
3279 if (KeyTyped)
3280 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003281 // Check if the function already exists, don't let the user type the
3282 // whole function before telling him it doesn't work! For a script we
3283 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003284 if (!eap->skip && !eap->forceit)
3285 {
3286 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003287 emsg(_(e_funcdict));
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003288 else if (name != NULL && find_func(name, is_global, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003289 emsg_funcname(e_funcexts, name);
3290 }
3291
3292 if (!eap->skip && did_emsg)
3293 goto erret;
3294
Bram Moolenaare38eab22019-12-05 21:50:01 +01003295 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003296 cmdline_row = msg_row;
3297 }
3298
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003299 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003300 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003301
Bram Moolenaarbf8feb52020-08-08 14:26:31 +02003302 // Detect having skipped over comment lines to find the return
3303 // type. Add NULL lines to keep the line count correct.
3304 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
3305 if (SOURCING_LNUM < sourcing_lnum_off)
3306 {
3307 sourcing_lnum_off -= SOURCING_LNUM;
3308 if (ga_grow(&newlines, sourcing_lnum_off) == FAIL)
3309 goto erret;
3310 while (sourcing_lnum_off-- > 0)
3311 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3312 }
3313
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003314 indent = 2;
3315 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01003317 getline_options = eap->cmdidx == CMD_def
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01003318 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003319 for (;;)
3320 {
3321 if (KeyTyped)
3322 {
3323 msg_scroll = TRUE;
3324 saved_wait_return = FALSE;
3325 }
3326 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003327
3328 if (line_arg != NULL)
3329 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003330 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003331 theline = line_arg;
3332 p = vim_strchr(theline, '\n');
3333 if (p == NULL)
3334 line_arg += STRLEN(line_arg);
3335 else
3336 {
3337 *p = NUL;
3338 line_arg = p + 1;
3339 }
3340 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003341 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02003342 {
3343 vim_free(line_to_free);
3344 if (eap->getline == NULL)
Bram Moolenaar66250c92020-08-20 15:02:42 +02003345 theline = getcmdline(':', 0L, indent, getline_options);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003346 else
Bram Moolenaar66250c92020-08-20 15:02:42 +02003347 theline = eap->getline(':', eap->cookie, indent,
3348 getline_options);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003349 line_to_free = theline;
3350 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003351 if (KeyTyped)
3352 lines_left = Rows - 1;
3353 if (theline == NULL)
3354 {
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003355 if (skip_until != NULL)
3356 semsg(_(e_missing_heredoc_end_marker_str), skip_until);
3357 else if (eap->cmdidx == CMD_def)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003358 emsg(_(e_missing_enddef));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 else
3360 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003361 goto erret;
3362 }
3363
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003364 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003365 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003366 if (SOURCING_LNUM < sourcing_lnum_off)
3367 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003368 else
3369 sourcing_lnum_off = 0;
3370
3371 if (skip_until != NULL)
3372 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003374 // * ":append" and "."
3375 // * ":python <<EOF" and "EOF"
3376 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
3377 if (heredoc_trimmed == NULL
3378 || (is_heredoc && skipwhite(theline) == theline)
3379 || STRNCMP(theline, heredoc_trimmed,
3380 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003381 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003382 if (heredoc_trimmed == NULL)
3383 p = theline;
3384 else if (is_heredoc)
3385 p = skipwhite(theline) == theline
3386 ? theline : theline + STRLEN(heredoc_trimmed);
3387 else
3388 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02003389 if (STRCMP(p, skip_until) == 0)
3390 {
3391 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003392 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaardcc58e02020-12-28 20:53:21 +01003393 getline_options = eap->cmdidx == CMD_def
Bram Moolenaar8242ebb2020-12-29 11:15:01 +01003394 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02003395 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003396 }
3397 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003398 }
3399 else
3400 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003401 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01003402 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003403 ;
3404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 // Check for "endfunction" or "enddef".
3406 if (checkforcmd(&p, nesting_def[nesting]
3407 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003408 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02003409 char_u *nextcmd = NULL;
3410
Bram Moolenaar663bb232017-06-22 19:12:10 +02003411 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02003412 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02003413 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02003414 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02003415 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 give_warning2(eap->cmdidx == CMD_def
3417 ? (char_u *)_("W1001: Text found after :enddef: %s")
3418 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02003419 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003420 if (nextcmd != NULL)
3421 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003422 // Another command follows. If the line came from "eap" we
3423 // can simply point into it, otherwise we need to change
3424 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02003425 eap->nextcmd = nextcmd;
3426 if (line_to_free != NULL)
3427 {
3428 vim_free(*eap->cmdlinep);
3429 *eap->cmdlinep = line_to_free;
3430 line_to_free = NULL;
3431 }
3432 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003433 break;
3434 }
3435
Bram Moolenaare38eab22019-12-05 21:50:01 +01003436 // Increase indent inside "if", "while", "for" and "try", decrease
3437 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003439 indent -= 2;
3440 else if (STRNCMP(p, "if", 2) == 0
3441 || STRNCMP(p, "wh", 2) == 0
3442 || STRNCMP(p, "for", 3) == 0
3443 || STRNCMP(p, "try", 3) == 0)
3444 indent += 2;
3445
Bram Moolenaare38eab22019-12-05 21:50:01 +01003446 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01003447 // Only recognize "def" inside "def", not inside "function",
3448 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 c = *p;
Bram Moolenaaradc8e442020-12-31 18:28:18 +01003450 if (is_function_cmd(&p)
Bram Moolenaar673660a2020-01-26 16:50:05 +01003451 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003452 {
3453 if (*p == '!')
3454 p = skipwhite(p + 1);
3455 p += eval_fname_script(p);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003456 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL, NULL));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003457 if (*skipwhite(p) == '(')
3458 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459 if (nesting == MAX_FUNC_NESTING - 1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003460 emsg(_(e_function_nesting_too_deep));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 else
3462 {
3463 ++nesting;
3464 nesting_def[nesting] = (c == 'd');
3465 indent += 2;
3466 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003467 }
3468 }
3469
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003470 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003471 p = skip_range(p, FALSE, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003472 if (eap->cmdidx != CMD_def
3473 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01003474 || (p[0] == 'c'
3475 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
3476 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
3477 && (STRNCMP(&p[3], "nge", 3) != 0
3478 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003479 || (p[0] == 'i'
3480 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003481 && (!ASCII_ISALPHA(p[2])
3482 || (p[2] == 's'
3483 && (!ASCII_ISALPHA(p[3])
3484 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003485 skip_until = vim_strsave((char_u *)".");
3486
Bram Moolenaare38eab22019-12-05 21:50:01 +01003487 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003488 arg = skipwhite(skiptowhite(p));
3489 if (arg[0] == '<' && arg[1] =='<'
3490 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003491 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
3492 || ((p[2] == '3' || p[2] == 'x')
3493 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003494 || (p[0] == 'p' && p[1] == 'e'
3495 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
3496 || (p[0] == 't' && p[1] == 'c'
3497 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
3498 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
3499 && !ASCII_ISALPHA(p[3]))
3500 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
3501 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
3502 || (p[0] == 'm' && p[1] == 'z'
3503 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
3504 ))
3505 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003506 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003507 p = skipwhite(arg + 2);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003508 if (STRNCMP(p, "trim", 4) == 0)
3509 {
3510 // Ignore leading white space.
3511 p = skipwhite(p + 4);
3512 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003513 skipwhite(theline) - theline);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003514 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003515 if (*p == NUL)
3516 skip_until = vim_strsave((char_u *)".");
3517 else
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003518 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003519 getline_options = GETLINE_NONE;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003520 is_heredoc = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003521 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003522
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003523 // Check for ":cmd v =<< [trim] EOF"
3524 // and ":cmd [a, b] =<< [trim] EOF"
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003525 // and "lines =<< [trim] EOF" for Vim9
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003526 // Where "cmd" can be "let", "var", "final" or "const".
Bram Moolenaar8471e572019-05-19 21:37:18 +02003527 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003528 if (*arg == '[')
3529 arg = vim_strchr(arg, ']');
3530 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003531 {
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003532 int found = (eap->cmdidx == CMD_def && arg[0] == '='
3533 && arg[1] == '<' && arg[2] =='<');
3534
3535 if (!found)
3536 // skip over the argument after "cmd"
3537 arg = skipwhite(skiptowhite(arg));
3538 if (found || (arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003539 && (checkforcmd(&p, "let", 2)
3540 || checkforcmd(&p, "var", 3)
3541 || checkforcmd(&p, "final", 5)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003542 || checkforcmd(&p, "const", 5))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02003543 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003544 p = skipwhite(arg + 3);
3545 if (STRNCMP(p, "trim", 4) == 0)
3546 {
3547 // Ignore leading white space.
3548 p = skipwhite(p + 4);
3549 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003550 skipwhite(theline) - theline);
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003551 }
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003552 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003553 getline_options = GETLINE_NONE;
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003554 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003555 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003556 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003557 }
3558
Bram Moolenaare38eab22019-12-05 21:50:01 +01003559 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003560 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003561 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003562
Bram Moolenaare38eab22019-12-05 21:50:01 +01003563 // Copy the line to newly allocated memory. get_one_sourceline()
3564 // allocates 250 bytes per line, this saves 80% on average. The cost
3565 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003566 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003567 if (p == NULL)
3568 goto erret;
3569 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003570
Bram Moolenaare38eab22019-12-05 21:50:01 +01003571 // Add NULL lines for continuation lines, so that the line count is
3572 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003573 while (sourcing_lnum_off-- > 0)
3574 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3575
Bram Moolenaare38eab22019-12-05 21:50:01 +01003576 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003577 if (line_arg != NULL && *line_arg == NUL)
3578 line_arg = NULL;
3579 }
3580
Bram Moolenaare38eab22019-12-05 21:50:01 +01003581 // Don't define the function when skipping commands or when an error was
3582 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003583 if (eap->skip || did_emsg)
3584 goto erret;
3585
3586 /*
3587 * If there are no errors, add the function
3588 */
3589 if (fudi.fd_dict == NULL)
3590 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 hashtab_T *ht;
3592
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003593 v = find_var(name, &ht, FALSE);
3594 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
3595 {
3596 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
3597 name);
3598 goto erret;
3599 }
3600
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003601 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaareef21022020-08-01 22:16:43 +02003602 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003603 {
Bram Moolenaareef21022020-08-01 22:16:43 +02003604 char_u *uname = untrans_function_name(name);
3605
3606 import = find_imported(uname == NULL ? name : uname, 0, NULL);
3607 }
3608
3609 if (fp != NULL || import != NULL)
3610 {
3611 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003612
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003613 // Function can be replaced with "function!" and when sourcing the
3614 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02003615 // A name that is used by an import can not be overruled.
3616 if (import != NULL
3617 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003618 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02003619 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003620 {
Bram Moolenaareef21022020-08-01 22:16:43 +02003621 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003622 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02003623 else
3624 emsg_funcname(e_funcexts, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003625 goto erret;
3626 }
3627 if (fp->uf_calls > 0)
3628 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003629 emsg_funcname(
3630 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003631 name);
3632 goto erret;
3633 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003634 if (fp->uf_refcount > 1)
3635 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003636 // This function is referenced somewhere, don't redefine it but
3637 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003638 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003639 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003640 fp = NULL;
3641 overwrite = TRUE;
3642 }
3643 else
3644 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003645 char_u *exp_name = fp->uf_name_exp;
3646
3647 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01003648 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003649 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003650 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003651 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003653#ifdef FEAT_PROFILE
3654 fp->uf_profiling = FALSE;
3655 fp->uf_prof_initialized = FALSE;
3656#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01003657 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003658 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003659 }
3660 }
3661 else
3662 {
3663 char numbuf[20];
3664
3665 fp = NULL;
3666 if (fudi.fd_newkey == NULL && !eap->forceit)
3667 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003668 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003669 goto erret;
3670 }
3671 if (fudi.fd_di == NULL)
3672 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003673 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02003674 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003675 goto erret;
3676 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003677 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02003678 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003679 goto erret;
3680
Bram Moolenaare38eab22019-12-05 21:50:01 +01003681 // Give the function a sequential number. Can only be used with a
3682 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003683 vim_free(name);
3684 sprintf(numbuf, "%d", ++func_nr);
3685 name = vim_strsave((char_u *)numbuf);
3686 if (name == NULL)
3687 goto erret;
3688 }
3689
3690 if (fp == NULL)
3691 {
3692 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3693 {
3694 int slen, plen;
3695 char_u *scriptname;
3696
Bram Moolenaare38eab22019-12-05 21:50:01 +01003697 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003698 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003699 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003700 {
3701 scriptname = autoload_name(name);
3702 if (scriptname != NULL)
3703 {
3704 p = vim_strchr(scriptname, '/');
3705 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003706 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003707 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003708 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003709 j = OK;
3710 vim_free(scriptname);
3711 }
3712 }
3713 if (j == FAIL)
3714 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003715 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003716 goto erret;
3717 }
3718 }
3719
Bram Moolenaar47ed5532019-08-08 20:49:14 +02003720 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003721 if (fp == NULL)
3722 goto erret;
3723
3724 if (fudi.fd_dict != NULL)
3725 {
3726 if (fudi.fd_di == NULL)
3727 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003728 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003729 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
3730 if (fudi.fd_di == NULL)
3731 {
3732 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003733 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003734 goto erret;
3735 }
3736 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
3737 {
3738 vim_free(fudi.fd_di);
3739 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003740 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003741 goto erret;
3742 }
3743 }
3744 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003745 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003746 clear_tv(&fudi.fd_di->di_tv);
3747 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003748 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003749
Bram Moolenaare38eab22019-12-05 21:50:01 +01003750 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003751 flags |= FC_DICT;
3752 }
3753
Bram Moolenaare38eab22019-12-05 21:50:01 +01003754 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003755 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003756 if (overwrite)
3757 {
3758 hi = hash_find(&func_hashtab, name);
3759 hi->hi_key = UF2HIKEY(fp);
3760 }
3761 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003762 {
3763 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003764 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003765 goto erret;
3766 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003767 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003768 }
3769 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003770 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003771 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003772 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773
3774 if (eap->cmdidx == CMD_def)
3775 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003776 int lnum_save = SOURCING_LNUM;
3777 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003778
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003779 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003780
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003781 // error messages are for the first function line
3782 SOURCING_LNUM = sourcing_lnum_top;
3783
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003784 if (cstack != NULL && cstack->cs_idx >= 0)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003785 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003786 int count = cstack->cs_idx + 1;
3787 int i;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003788
3789 // The block context may be needed for script variables declared in
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003790 // a block that is visible now but not when the function is called
3791 // later.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003792 fp->uf_block_ids = ALLOC_MULT(int, count);
3793 if (fp->uf_block_ids != NULL)
3794 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003795 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003796 sizeof(int) * count);
3797 fp->uf_block_depth = count;
3798 }
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003799
3800 // Set flag in each block to indicate a function was defined. This
3801 // is used to keep the variable when leaving the block, see
3802 // hide_script_var().
3803 for (i = 0; i <= cstack->cs_idx; ++i)
3804 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003805 }
3806
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003807 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003809 SOURCING_LNUM = lnum_save;
3810 goto errret_2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003812 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813
3814 // parse the return type, if any
3815 if (ret_type == NULL)
3816 fp->uf_ret_type = &t_void;
3817 else
3818 {
3819 p = ret_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01003820 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02003822 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003824 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003825 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003826
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003827 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003828 if ((flags & FC_CLOSURE) != 0)
3829 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003830 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003831 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003832 }
3833 else
3834 fp->uf_scoped = NULL;
3835
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003836#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003837 if (prof_def_func())
3838 func_do_profile(fp);
3839#endif
3840 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003841 if (sandbox)
3842 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003843 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003844 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003845 fp->uf_flags = flags;
3846 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003847 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003848 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02003849 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003850 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 if (is_export)
3852 {
3853 fp->uf_flags |= FC_EXPORT;
3854 // let ex_export() know the export worked.
3855 is_export = FALSE;
3856 }
3857
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003858 if (eap->cmdidx == CMD_def)
3859 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02003860 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
3861 // :func does not use Vim9 script syntax, even in a Vim9 script file
3862 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003863
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003864 goto ret_free;
3865
3866erret:
3867 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003868 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003869errret_2:
3870 ga_clear_strings(&newlines);
3871ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003872 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003873 vim_free(skip_until);
Bram Moolenaarfffdf472020-12-13 21:16:55 +01003874 vim_free(heredoc_trimmed);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003875 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003876 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003877 if (name != name_arg)
3878 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003879 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003880 did_emsg |= saved_did_emsg;
3881 need_wait_return |= saved_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003882
3883 return fp;
3884}
3885
3886/*
3887 * ":function"
3888 */
3889 void
3890ex_function(exarg_T *eap)
3891{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003892 (void)define_function(eap, NULL);
Bram Moolenaar822ba242020-05-24 23:00:18 +02003893}
3894
3895/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02003896 * :defcompile - compile all :def functions in the current script that need to
3897 * be compiled. Except dead functions.
Bram Moolenaar822ba242020-05-24 23:00:18 +02003898 */
3899 void
3900ex_defcompile(exarg_T *eap UNUSED)
3901{
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003902 long todo = (long)func_hashtab.ht_used;
3903 int changed = func_hashtab.ht_changed;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003904 hashitem_T *hi;
3905 ufunc_T *ufunc;
3906
3907 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
3908 {
3909 if (!HASHITEM_EMPTY(hi))
3910 {
3911 --todo;
3912 ufunc = HI2UF(hi);
3913 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
Bram Moolenaar96f8f492020-09-09 17:08:51 +02003914 && ufunc->uf_def_status == UF_TO_BE_COMPILED
3915 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003916 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02003917 compile_def_function(ufunc, FALSE, NULL);
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003918
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003919 if (func_hashtab.ht_changed != changed)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003920 {
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003921 // a function has been added or removed, need to start over
3922 todo = (long)func_hashtab.ht_used;
3923 changed = func_hashtab.ht_changed;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003924 hi = func_hashtab.ht_array;
Bram Moolenaar285b1892020-05-26 11:37:26 +02003925 --hi;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003926 }
3927 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003928 }
3929 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003930}
3931
3932/*
3933 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3934 * Return 2 if "p" starts with "s:".
3935 * Return 0 otherwise.
3936 */
3937 int
3938eval_fname_script(char_u *p)
3939{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003940 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3941 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003942 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3943 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3944 return 5;
3945 if (p[0] == 's' && p[1] == ':')
3946 return 2;
3947 return 0;
3948}
3949
3950 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003951translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003952{
3953 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003954 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003955 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956}
3957
3958/*
3959 * Return TRUE when "ufunc" has old-style "..." varargs
3960 * or named varargs "...name: type".
3961 */
3962 int
3963has_varargs(ufunc_T *ufunc)
3964{
3965 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003966}
3967
3968/*
3969 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003970 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003971 */
3972 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003973function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003974{
3975 char_u *nm = name;
3976 char_u *p;
3977 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003978 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003979 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003980
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003981 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3982 if (no_deref)
3983 flag |= TFN_NO_DEREF;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003984 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003985 nm = skipwhite(nm);
3986
Bram Moolenaare38eab22019-12-05 21:50:01 +01003987 // Only accept "funcname", "funcname ", "funcname (..." and
3988 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003989 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003990 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003991 vim_free(p);
3992 return n;
3993}
3994
Bram Moolenaar113e1072019-01-20 15:30:40 +01003995#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003996 char_u *
3997get_expanded_name(char_u *name, int check)
3998{
3999 char_u *nm = name;
4000 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004001 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004002
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004003 p = trans_function_name(&nm, &is_global, FALSE,
4004 TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004005
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004006 if (p != NULL && *nm == NUL
4007 && (!check || translated_function_exists(p, is_global)))
4008 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004009
4010 vim_free(p);
4011 return NULL;
4012}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004013#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004014
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004015/*
4016 * Function given to ExpandGeneric() to obtain the list of user defined
4017 * function names.
4018 */
4019 char_u *
4020get_user_func_name(expand_T *xp, int idx)
4021{
4022 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004023 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004024 static hashitem_T *hi;
4025 ufunc_T *fp;
4026
4027 if (idx == 0)
4028 {
4029 done = 0;
4030 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004031 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004032 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004033 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004034 {
4035 if (done++ > 0)
4036 ++hi;
4037 while (HASHITEM_EMPTY(hi))
4038 ++hi;
4039 fp = HI2UF(hi);
4040
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004041 // don't show dead, dict and lambda functions
4042 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02004043 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004045
4046 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004047 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004048
4049 cat_func_name(IObuff, fp);
4050 if (xp->xp_context != EXPAND_USER_FUNC)
4051 {
4052 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004054 STRCAT(IObuff, ")");
4055 }
4056 return IObuff;
4057 }
4058 return NULL;
4059}
4060
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004061/*
4062 * ":delfunction {name}"
4063 */
4064 void
4065ex_delfunction(exarg_T *eap)
4066{
4067 ufunc_T *fp = NULL;
4068 char_u *p;
4069 char_u *name;
4070 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004071 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004072
4073 p = eap->arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004074 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004075 vim_free(fudi.fd_newkey);
4076 if (name == NULL)
4077 {
4078 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004079 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004080 return;
4081 }
4082 if (!ends_excmd(*skipwhite(p)))
4083 {
4084 vim_free(name);
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004085 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004086 return;
4087 }
4088 eap->nextcmd = check_nextcmd(p);
4089 if (eap->nextcmd != NULL)
4090 *p = NUL;
4091
4092 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004093 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004094 vim_free(name);
4095
4096 if (!eap->skip)
4097 {
4098 if (fp == NULL)
4099 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02004100 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004101 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004102 return;
4103 }
4104 if (fp->uf_calls > 0)
4105 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004106 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004107 return;
4108 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004109 if (fp->uf_flags & FC_VIM9)
4110 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004111 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004112 return;
4113 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004114
4115 if (fudi.fd_dict != NULL)
4116 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004117 // Delete the dict item that refers to the function, it will
4118 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004119 dictitem_remove(fudi.fd_dict, fudi.fd_di);
4120 }
4121 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004122 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004123 // A normal function (not a numbered function or lambda) has a
4124 // refcount of 1 for the entry in the hashtable. When deleting
4125 // it and the refcount is more than one, it should be kept.
4126 // A numbered function and lambda should be kept if the refcount is
4127 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004128 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004129 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004130 // Function is still referenced somewhere. Don't free it but
4131 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004132 if (func_remove(fp))
4133 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004134 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004135 }
4136 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004137 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004138 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004139 }
4140}
4141
4142/*
4143 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004144 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004145 */
4146 void
4147func_unref(char_u *name)
4148{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004149 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004150
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004151 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004152 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004153 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004154 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004155 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004156#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004157 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004158#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01004159 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004160 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004161 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004162 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004163 // Only delete it when it's not being used. Otherwise it's done
4164 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004165 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004166 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02004167 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004168}
4169
4170/*
4171 * Unreference a Function: decrement the reference count and free it when it
4172 * becomes zero.
4173 */
4174 void
4175func_ptr_unref(ufunc_T *fp)
4176{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004177 if (fp != NULL && --fp->uf_refcount <= 0)
4178 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004179 // Only delete it when it's not being used. Otherwise it's done
4180 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02004181 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004182 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004183 }
4184}
4185
4186/*
4187 * Count a reference to a Function.
4188 */
4189 void
4190func_ref(char_u *name)
4191{
4192 ufunc_T *fp;
4193
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004194 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004195 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004196 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004197 if (fp != NULL)
4198 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004199 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01004200 // Only give an error for a numbered function.
4201 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01004202 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004203}
4204
4205/*
4206 * Count a reference to a Function.
4207 */
4208 void
4209func_ptr_ref(ufunc_T *fp)
4210{
4211 if (fp != NULL)
4212 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004213}
4214
4215/*
4216 * Return TRUE if items in "fc" do not have "copyID". That means they are not
4217 * referenced from anywhere that is in use.
4218 */
4219 static int
4220can_free_funccal(funccall_T *fc, int copyID)
4221{
4222 return (fc->l_varlist.lv_copyID != copyID
4223 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004224 && fc->l_avars.dv_copyID != copyID
4225 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004226}
4227
4228/*
4229 * ":return [expr]"
4230 */
4231 void
4232ex_return(exarg_T *eap)
4233{
4234 char_u *arg = eap->arg;
4235 typval_T rettv;
4236 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004237 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004238
4239 if (current_funccal == NULL)
4240 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004241 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004242 return;
4243 }
4244
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004245 CLEAR_FIELD(evalarg);
4246 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
4247
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004248 if (eap->skip)
4249 ++emsg_skip;
4250
4251 eap->nextcmd = NULL;
4252 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004253 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004254 {
4255 if (!eap->skip)
4256 returning = do_return(eap, FALSE, TRUE, &rettv);
4257 else
4258 clear_tv(&rettv);
4259 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004260 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004261 else if (!eap->skip)
4262 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004263 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01004264 update_force_abort();
4265
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004266 /*
4267 * Return unless the expression evaluation has been cancelled due to an
4268 * aborting error, an interrupt, or an exception.
4269 */
4270 if (!aborting())
4271 returning = do_return(eap, FALSE, TRUE, NULL);
4272 }
4273
Bram Moolenaare38eab22019-12-05 21:50:01 +01004274 // When skipping or the return gets pending, advance to the next command
4275 // in this line (!returning). Otherwise, ignore the rest of the line.
4276 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004277 if (returning)
4278 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004279 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004280 eap->nextcmd = check_nextcmd(arg);
4281
4282 if (eap->skip)
4283 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02004284 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004285}
4286
4287/*
4288 * ":1,25call func(arg1, arg2)" function call.
4289 */
4290 void
4291ex_call(exarg_T *eap)
4292{
4293 char_u *arg = eap->arg;
4294 char_u *startarg;
4295 char_u *name;
4296 char_u *tofree;
4297 int len;
4298 typval_T rettv;
4299 linenr_T lnum;
4300 int doesrange;
4301 int failed = FALSE;
4302 funcdict_T fudi;
4303 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004304 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004305
Bram Moolenaare6b53242020-07-01 17:28:33 +02004306 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004307 if (eap->skip)
4308 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004309 // trans_function_name() doesn't work well when skipping, use eval0()
4310 // instead to skip to any following command, e.g. for:
4311 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004312 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004313 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004314 clear_tv(&rettv);
4315 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004316 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004317 return;
4318 }
4319
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004320 tofree = trans_function_name(&arg, NULL, eap->skip,
4321 TFN_INT, &fudi, &partial);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004322 if (fudi.fd_newkey != NULL)
4323 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004324 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004325 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004326 vim_free(fudi.fd_newkey);
4327 }
4328 if (tofree == NULL)
4329 return;
4330
Bram Moolenaare38eab22019-12-05 21:50:01 +01004331 // Increase refcount on dictionary, it could get deleted when evaluating
4332 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004333 if (fudi.fd_dict != NULL)
4334 ++fudi.fd_dict->dv_refcount;
4335
Bram Moolenaare38eab22019-12-05 21:50:01 +01004336 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
4337 // contents. For VAR_PARTIAL get its partial, unless we already have one
4338 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004339 len = (int)STRLEN(tofree);
4340 name = deref_func_name(tofree, &len,
4341 partial != NULL ? NULL : &partial, FALSE);
4342
Bram Moolenaare38eab22019-12-05 21:50:01 +01004343 // Skip white space to allow ":call func ()". Not good, but required for
4344 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004345 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004346 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004347
4348 if (*startarg != '(')
4349 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004351 goto end;
4352 }
4353
4354 /*
4355 * When skipping, evaluate the function once, to find the end of the
4356 * arguments.
4357 * When the function takes a range, this is discovered after the first
4358 * call, and the loop is broken.
4359 */
4360 if (eap->skip)
4361 {
4362 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004363 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004364 }
4365 else
4366 lnum = eap->line1;
4367 for ( ; lnum <= eap->line2; ++lnum)
4368 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004369 funcexe_T funcexe;
4370
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004371 if (!eap->skip && eap->addr_count > 0)
4372 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004373 if (lnum > curbuf->b_ml.ml_line_count)
4374 {
4375 // If the function deleted lines or switched to another buffer
4376 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004377 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004378 break;
4379 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004380 curwin->w_cursor.lnum = lnum;
4381 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004382 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004383 }
4384 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004385
Bram Moolenaara80faa82020-04-12 19:37:17 +02004386 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004387 funcexe.firstline = eap->line1;
4388 funcexe.lastline = eap->line2;
4389 funcexe.doesrange = &doesrange;
4390 funcexe.evaluate = !eap->skip;
4391 funcexe.partial = partial;
4392 funcexe.selfdict = fudi.fd_dict;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004393 if (get_func_tv(name, -1, &rettv, &arg, &evalarg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004394 {
4395 failed = TRUE;
4396 break;
4397 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01004398 if (has_watchexpr())
4399 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004400
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004401 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004402 if (handle_subscript(&arg, &rettv,
4403 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004404 {
4405 failed = TRUE;
4406 break;
4407 }
4408
4409 clear_tv(&rettv);
4410 if (doesrange || eap->skip)
4411 break;
4412
Bram Moolenaare38eab22019-12-05 21:50:01 +01004413 // Stop when immediately aborting on error, or when an interrupt
4414 // occurred or an exception was thrown but not caught.
4415 // get_func_tv() returned OK, so that the check for trailing
4416 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004417 if (aborting())
4418 break;
4419 }
4420 if (eap->skip)
4421 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004422 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004423
Bram Moolenaare51bb172020-02-16 19:42:23 +01004424 // When inside :try we need to check for following "| catch".
4425 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004426 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004427 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02004428 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004429 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004430 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004431 if (!failed)
4432 {
4433 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004434 semsg(_(e_trailing_arg), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004435 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004436 }
4437 else
4438 eap->nextcmd = check_nextcmd(arg);
4439 }
4440
4441end:
4442 dict_unref(fudi.fd_dict);
4443 vim_free(tofree);
4444}
4445
4446/*
4447 * Return from a function. Possibly makes the return pending. Also called
4448 * for a pending return at the ":endtry" or after returning from an extra
4449 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
4450 * when called due to a ":return" command. "rettv" may point to a typval_T
4451 * with the return rettv. Returns TRUE when the return can be carried out,
4452 * FALSE when the return gets pending.
4453 */
4454 int
4455do_return(
4456 exarg_T *eap,
4457 int reanimate,
4458 int is_cmd,
4459 void *rettv)
4460{
4461 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01004462 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004463
4464 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004465 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004466 current_funccal->returned = FALSE;
4467
4468 /*
4469 * Cleanup (and inactivate) conditionals, but stop when a try conditional
4470 * not in its finally clause (which then is to be executed next) is found.
4471 * In this case, make the ":return" pending for execution at the ":endtry".
4472 * Otherwise, return normally.
4473 */
4474 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
4475 if (idx >= 0)
4476 {
4477 cstack->cs_pending[idx] = CSTP_RETURN;
4478
4479 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004480 // A pending return again gets pending. "rettv" points to an
4481 // allocated variable with the rettv of the original ":return"'s
4482 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004483 cstack->cs_rettv[idx] = rettv;
4484 else
4485 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004486 // When undoing a return in order to make it pending, get the stored
4487 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004488 if (reanimate)
4489 rettv = current_funccal->rettv;
4490
4491 if (rettv != NULL)
4492 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004493 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004494 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
4495 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
4496 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004497 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004498 }
4499 else
4500 cstack->cs_rettv[idx] = NULL;
4501
4502 if (reanimate)
4503 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004504 // The pending return value could be overwritten by a ":return"
4505 // without argument in a finally clause; reset the default
4506 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004507 current_funccal->rettv->v_type = VAR_NUMBER;
4508 current_funccal->rettv->vval.v_number = 0;
4509 }
4510 }
4511 report_make_pending(CSTP_RETURN, rettv);
4512 }
4513 else
4514 {
4515 current_funccal->returned = TRUE;
4516
Bram Moolenaare38eab22019-12-05 21:50:01 +01004517 // If the return is carried out now, store the return value. For
4518 // a return immediately after reanimation, the value is already
4519 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004520 if (!reanimate && rettv != NULL)
4521 {
4522 clear_tv(current_funccal->rettv);
4523 *current_funccal->rettv = *(typval_T *)rettv;
4524 if (!is_cmd)
4525 vim_free(rettv);
4526 }
4527 }
4528
4529 return idx < 0;
4530}
4531
4532/*
4533 * Free the variable with a pending return value.
4534 */
4535 void
4536discard_pending_return(void *rettv)
4537{
4538 free_tv((typval_T *)rettv);
4539}
4540
4541/*
4542 * Generate a return command for producing the value of "rettv". The result
4543 * is an allocated string. Used by report_pending() for verbose messages.
4544 */
4545 char_u *
4546get_return_cmd(void *rettv)
4547{
4548 char_u *s = NULL;
4549 char_u *tofree = NULL;
4550 char_u numbuf[NUMBUFLEN];
4551
4552 if (rettv != NULL)
4553 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
4554 if (s == NULL)
4555 s = (char_u *)"";
4556
4557 STRCPY(IObuff, ":return ");
4558 STRNCPY(IObuff + 8, s, IOSIZE - 8);
4559 if (STRLEN(s) + 8 >= IOSIZE)
4560 STRCPY(IObuff + IOSIZE - 4, "...");
4561 vim_free(tofree);
4562 return vim_strsave(IObuff);
4563}
4564
4565/*
4566 * Get next function line.
4567 * Called by do_cmdline() to get the next line.
4568 * Returns allocated string, or NULL for end of function.
4569 */
4570 char_u *
4571get_func_line(
4572 int c UNUSED,
4573 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02004574 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004575 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004576{
4577 funccall_T *fcp = (funccall_T *)cookie;
4578 ufunc_T *fp = fcp->func;
4579 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004580 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004581
Bram Moolenaare38eab22019-12-05 21:50:01 +01004582 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004583 if (fcp->dbg_tick != debug_tick)
4584 {
4585 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004586 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004587 fcp->dbg_tick = debug_tick;
4588 }
4589#ifdef FEAT_PROFILE
4590 if (do_profiling == PROF_YES)
4591 func_line_end(cookie);
4592#endif
4593
4594 gap = &fp->uf_lines;
4595 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4596 || fcp->returned)
4597 retval = NULL;
4598 else
4599 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004600 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004601 while (fcp->linenr < gap->ga_len
4602 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
4603 ++fcp->linenr;
4604 if (fcp->linenr >= gap->ga_len)
4605 retval = NULL;
4606 else
4607 {
4608 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004609 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004610#ifdef FEAT_PROFILE
4611 if (do_profiling == PROF_YES)
4612 func_line_start(cookie);
4613#endif
4614 }
4615 }
4616
Bram Moolenaare38eab22019-12-05 21:50:01 +01004617 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004618 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004619 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004620 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004621 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004622 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004623 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004624 fcp->dbg_tick = debug_tick;
4625 }
4626
4627 return retval;
4628}
4629
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004630/*
4631 * Return TRUE if the currently active function should be ended, because a
4632 * return was encountered or an error occurred. Used inside a ":while".
4633 */
4634 int
4635func_has_ended(void *cookie)
4636{
4637 funccall_T *fcp = (funccall_T *)cookie;
4638
Bram Moolenaare38eab22019-12-05 21:50:01 +01004639 // Ignore the "abort" flag if the abortion behavior has been changed due to
4640 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004641 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4642 || fcp->returned);
4643}
4644
4645/*
4646 * return TRUE if cookie indicates a function which "abort"s on errors.
4647 */
4648 int
4649func_has_abort(
4650 void *cookie)
4651{
4652 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
4653}
4654
4655
4656/*
4657 * Turn "dict.Func" into a partial for "Func" bound to "dict".
4658 * Don't do this when "Func" is already a partial that was bound
4659 * explicitly (pt_auto is FALSE).
4660 * Changes "rettv" in-place.
4661 * Returns the updated "selfdict_in".
4662 */
4663 dict_T *
4664make_partial(dict_T *selfdict_in, typval_T *rettv)
4665{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004666 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004667 char_u *tofree = NULL;
4668 ufunc_T *fp;
4669 char_u fname_buf[FLEN_FIXED + 1];
4670 int error;
4671 dict_T *selfdict = selfdict_in;
4672
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004673 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
4674 fp = rettv->vval.v_partial->pt_func;
4675 else
4676 {
4677 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
4678 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004679 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004680 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004681 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004682 vim_free(tofree);
4683 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004684
4685 if (fp != NULL && (fp->uf_flags & FC_DICT))
4686 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004687 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004688
4689 if (pt != NULL)
4690 {
4691 pt->pt_refcount = 1;
4692 pt->pt_dict = selfdict;
4693 pt->pt_auto = TRUE;
4694 selfdict = NULL;
4695 if (rettv->v_type == VAR_FUNC)
4696 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004697 // Just a function: Take over the function name and use
4698 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004699 pt->pt_name = rettv->vval.v_string;
4700 }
4701 else
4702 {
4703 partial_T *ret_pt = rettv->vval.v_partial;
4704 int i;
4705
Bram Moolenaare38eab22019-12-05 21:50:01 +01004706 // Partial: copy the function name, use selfdict and copy
4707 // args. Can't take over name or args, the partial might
4708 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004709 if (ret_pt->pt_name != NULL)
4710 {
4711 pt->pt_name = vim_strsave(ret_pt->pt_name);
4712 func_ref(pt->pt_name);
4713 }
4714 else
4715 {
4716 pt->pt_func = ret_pt->pt_func;
4717 func_ptr_ref(pt->pt_func);
4718 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004719 if (ret_pt->pt_argc > 0)
4720 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004721 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004722 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004723 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004724 pt->pt_argc = 0;
4725 else
4726 {
4727 pt->pt_argc = ret_pt->pt_argc;
4728 for (i = 0; i < pt->pt_argc; i++)
4729 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
4730 }
4731 }
4732 partial_unref(ret_pt);
4733 }
4734 rettv->v_type = VAR_PARTIAL;
4735 rettv->vval.v_partial = pt;
4736 }
4737 }
4738 return selfdict;
4739}
4740
4741/*
4742 * Return the name of the executed function.
4743 */
4744 char_u *
4745func_name(void *cookie)
4746{
4747 return ((funccall_T *)cookie)->func->uf_name;
4748}
4749
4750/*
4751 * Return the address holding the next breakpoint line for a funccall cookie.
4752 */
4753 linenr_T *
4754func_breakpoint(void *cookie)
4755{
4756 return &((funccall_T *)cookie)->breakpoint;
4757}
4758
4759/*
4760 * Return the address holding the debug tick for a funccall cookie.
4761 */
4762 int *
4763func_dbg_tick(void *cookie)
4764{
4765 return &((funccall_T *)cookie)->dbg_tick;
4766}
4767
4768/*
4769 * Return the nesting level for a funccall cookie.
4770 */
4771 int
4772func_level(void *cookie)
4773{
4774 return ((funccall_T *)cookie)->level;
4775}
4776
4777/*
4778 * Return TRUE when a function was ended by a ":return" command.
4779 */
4780 int
4781current_func_returned(void)
4782{
4783 return current_funccal->returned;
4784}
4785
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004786 int
4787free_unref_funccal(int copyID, int testing)
4788{
4789 int did_free = FALSE;
4790 int did_free_funccal = FALSE;
4791 funccall_T *fc, **pfc;
4792
4793 for (pfc = &previous_funccal; *pfc != NULL; )
4794 {
4795 if (can_free_funccal(*pfc, copyID))
4796 {
4797 fc = *pfc;
4798 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01004799 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004800 did_free = TRUE;
4801 did_free_funccal = TRUE;
4802 }
4803 else
4804 pfc = &(*pfc)->caller;
4805 }
4806 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004807 // When a funccal was freed some more items might be garbage
4808 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004809 (void)garbage_collect(testing);
4810
4811 return did_free;
4812}
4813
4814/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004815 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004816 */
4817 static funccall_T *
4818get_funccal(void)
4819{
4820 int i;
4821 funccall_T *funccal;
4822 funccall_T *temp_funccal;
4823
4824 funccal = current_funccal;
4825 if (debug_backtrace_level > 0)
4826 {
4827 for (i = 0; i < debug_backtrace_level; i++)
4828 {
4829 temp_funccal = funccal->caller;
4830 if (temp_funccal)
4831 funccal = temp_funccal;
4832 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004833 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004834 debug_backtrace_level = i;
4835 }
4836 }
4837 return funccal;
4838}
4839
4840/*
4841 * Return the hashtable used for local variables in the current funccal.
4842 * Return NULL if there is no current funccal.
4843 */
4844 hashtab_T *
4845get_funccal_local_ht()
4846{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004847 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004848 return NULL;
4849 return &get_funccal()->l_vars.dv_hashtab;
4850}
4851
4852/*
4853 * Return the l: scope variable.
4854 * Return NULL if there is no current funccal.
4855 */
4856 dictitem_T *
4857get_funccal_local_var()
4858{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004859 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004860 return NULL;
4861 return &get_funccal()->l_vars_var;
4862}
4863
4864/*
4865 * Return the hashtable used for argument in the current funccal.
4866 * Return NULL if there is no current funccal.
4867 */
4868 hashtab_T *
4869get_funccal_args_ht()
4870{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004872 return NULL;
4873 return &get_funccal()->l_avars.dv_hashtab;
4874}
4875
4876/*
4877 * Return the a: scope variable.
4878 * Return NULL if there is no current funccal.
4879 */
4880 dictitem_T *
4881get_funccal_args_var()
4882{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004884 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004885 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004886}
4887
4888/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004889 * List function variables, if there is a function.
4890 */
4891 void
4892list_func_vars(int *first)
4893{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004895 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004896 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004897}
4898
4899/*
4900 * If "ht" is the hashtable for local variables in the current funccal, return
4901 * the dict that contains it.
4902 * Otherwise return NULL.
4903 */
4904 dict_T *
4905get_current_funccal_dict(hashtab_T *ht)
4906{
4907 if (current_funccal != NULL
4908 && ht == &current_funccal->l_vars.dv_hashtab)
4909 return &current_funccal->l_vars;
4910 return NULL;
4911}
4912
4913/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004914 * Search hashitem in parent scope.
4915 */
4916 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004917find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004918{
4919 funccall_T *old_current_funccal = current_funccal;
4920 hashtab_T *ht;
4921 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004922 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004923
4924 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4925 return NULL;
4926
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004927 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004928 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004929 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004930 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004931 ht = find_var_ht(name, &varname);
4932 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004933 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004934 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004935 if (!HASHITEM_EMPTY(hi))
4936 {
4937 *pht = ht;
4938 break;
4939 }
4940 }
4941 if (current_funccal == current_funccal->func->uf_scoped)
4942 break;
4943 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004944 }
4945 current_funccal = old_current_funccal;
4946
4947 return hi;
4948}
4949
4950/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004951 * Search variable in parent scope.
4952 */
4953 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004954find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004955{
4956 dictitem_T *v = NULL;
4957 funccall_T *old_current_funccal = current_funccal;
4958 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004959 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004960
4961 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4962 return NULL;
4963
Bram Moolenaare38eab22019-12-05 21:50:01 +01004964 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004965 current_funccal = current_funccal->func->uf_scoped;
4966 while (current_funccal)
4967 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004968 ht = find_var_ht(name, &varname);
4969 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004970 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004971 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004972 if (v != NULL)
4973 break;
4974 }
4975 if (current_funccal == current_funccal->func->uf_scoped)
4976 break;
4977 current_funccal = current_funccal->func->uf_scoped;
4978 }
4979 current_funccal = old_current_funccal;
4980
4981 return v;
4982}
4983
4984/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004985 * Set "copyID + 1" in previous_funccal and callers.
4986 */
4987 int
4988set_ref_in_previous_funccal(int copyID)
4989{
4990 int abort = FALSE;
4991 funccall_T *fc;
4992
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004993 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004994 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004995 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004996 abort = abort
4997 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4998 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004999 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005000 }
5001 return abort;
5002}
5003
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005004 static int
5005set_ref_in_funccal(funccall_T *fc, int copyID)
5006{
5007 int abort = FALSE;
5008
5009 if (fc->fc_copyID != copyID)
5010 {
5011 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005012 abort = abort
5013 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
5014 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005015 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005016 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005017 }
5018 return abort;
5019}
5020
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005021/*
5022 * Set "copyID" in all local vars and arguments in the call stack.
5023 */
5024 int
5025set_ref_in_call_stack(int copyID)
5026{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005027 int abort = FALSE;
5028 funccall_T *fc;
5029 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005030
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005031 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005032 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005033
5034 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005035 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
5036 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005037 abort = abort || set_ref_in_funccal(fc, copyID);
5038
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005039 return abort;
5040}
5041
5042/*
5043 * Set "copyID" in all functions available by name.
5044 */
5045 int
5046set_ref_in_functions(int copyID)
5047{
5048 int todo;
5049 hashitem_T *hi = NULL;
5050 int abort = FALSE;
5051 ufunc_T *fp;
5052
5053 todo = (int)func_hashtab.ht_used;
5054 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005055 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005056 if (!HASHITEM_EMPTY(hi))
5057 {
5058 --todo;
5059 fp = HI2UF(hi);
5060 if (!func_name_refcount(fp->uf_name))
5061 abort = abort || set_ref_in_func(NULL, fp, copyID);
5062 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005063 }
5064 return abort;
5065}
5066
5067/*
5068 * Set "copyID" in all function arguments.
5069 */
5070 int
5071set_ref_in_func_args(int copyID)
5072{
5073 int i;
5074 int abort = FALSE;
5075
5076 for (i = 0; i < funcargs.ga_len; ++i)
5077 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
5078 copyID, NULL, NULL);
5079 return abort;
5080}
5081
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005082/*
5083 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005084 * Returns TRUE if setting references failed somehow.
5085 */
5086 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005087set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005088{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005089 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005090 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01005091 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005092 char_u fname_buf[FLEN_FIXED + 1];
5093 char_u *tofree = NULL;
5094 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005095 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005096
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005097 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005098 return FALSE;
5099
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005100 if (fp_in == NULL)
5101 {
5102 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005103 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005104 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005105 if (fp != NULL)
5106 {
5107 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005108 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005109 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02005110
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005111 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005112 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005113}
5114
Bram Moolenaare38eab22019-12-05 21:50:01 +01005115#endif // FEAT_EVAL