blob: f0877e1c3bd468cbc50f01e1f3dbc9cc059b2094 [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]
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003407 ? "enddef" : "endfunction", 4))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003408 {
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003409 if (nesting-- == 0)
Bram Moolenaar53564f72017-06-24 14:48:11 +02003410 {
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003411 char_u *nextcmd = NULL;
3412
3413 if (*p == '|')
3414 nextcmd = p + 1;
3415 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
3416 nextcmd = line_arg;
3417 else if (*p != NUL && *p != '"' && p_verbose > 0)
3418 give_warning2(eap->cmdidx == CMD_def
3419 ? (char_u *)_("W1001: Text found after :enddef: %s")
3420 : (char_u *)_("W22: Text found after :endfunction: %s"),
3421 p, TRUE);
3422 if (nextcmd != NULL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02003423 {
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003424 // Another command follows. If the line came from "eap"
3425 // we can simply point into it, otherwise we need to
3426 // change "eap->cmdlinep".
3427 eap->nextcmd = nextcmd;
3428 if (line_to_free != NULL)
3429 {
3430 vim_free(*eap->cmdlinep);
3431 *eap->cmdlinep = line_to_free;
3432 line_to_free = NULL;
3433 }
Bram Moolenaar53564f72017-06-24 14:48:11 +02003434 }
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003435 break;
Bram Moolenaar53564f72017-06-24 14:48:11 +02003436 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003437 }
3438
Bram Moolenaar5178b1b2021-01-01 18:43:51 +01003439 // Check for mismatched "endfunc" or "enddef".
3440 // We don't check for "def" inside "func" thus we also can't check
3441 // for "enddef".
3442 // We continue to find the end of the function, although we might
3443 // not find it.
3444 else if (nesting_def[nesting])
3445 {
3446 if (checkforcmd(&p, "endfunction", 4))
3447 emsg(_(e_mismatched_endfunction));
3448 }
3449 else if (eap->cmdidx == CMD_def && checkforcmd(&p, "enddef", 4))
3450 emsg(_(e_mismatched_enddef));
3451
Bram Moolenaare38eab22019-12-05 21:50:01 +01003452 // Increase indent inside "if", "while", "for" and "try", decrease
3453 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003455 indent -= 2;
3456 else if (STRNCMP(p, "if", 2) == 0
3457 || STRNCMP(p, "wh", 2) == 0
3458 || STRNCMP(p, "for", 3) == 0
3459 || STRNCMP(p, "try", 3) == 0)
3460 indent += 2;
3461
Bram Moolenaare38eab22019-12-05 21:50:01 +01003462 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01003463 // Only recognize "def" inside "def", not inside "function",
3464 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465 c = *p;
Bram Moolenaaradc8e442020-12-31 18:28:18 +01003466 if (is_function_cmd(&p)
Bram Moolenaar673660a2020-01-26 16:50:05 +01003467 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003468 {
3469 if (*p == '!')
3470 p = skipwhite(p + 1);
3471 p += eval_fname_script(p);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003472 vim_free(trans_function_name(&p, NULL, TRUE, 0, NULL, NULL));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003473 if (*skipwhite(p) == '(')
3474 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475 if (nesting == MAX_FUNC_NESTING - 1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003476 emsg(_(e_function_nesting_too_deep));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477 else
3478 {
3479 ++nesting;
3480 nesting_def[nesting] = (c == 'd');
3481 indent += 2;
3482 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003483 }
3484 }
3485
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003486 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003487 p = skip_range(p, FALSE, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003488 if (eap->cmdidx != CMD_def
3489 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01003490 || (p[0] == 'c'
3491 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
3492 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
3493 && (STRNCMP(&p[3], "nge", 3) != 0
3494 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003495 || (p[0] == 'i'
3496 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01003497 && (!ASCII_ISALPHA(p[2])
3498 || (p[2] == 's'
3499 && (!ASCII_ISALPHA(p[3])
3500 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003501 skip_until = vim_strsave((char_u *)".");
3502
Bram Moolenaare38eab22019-12-05 21:50:01 +01003503 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003504 arg = skipwhite(skiptowhite(p));
3505 if (arg[0] == '<' && arg[1] =='<'
3506 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01003507 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
3508 || ((p[2] == '3' || p[2] == 'x')
3509 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003510 || (p[0] == 'p' && p[1] == 'e'
3511 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
3512 || (p[0] == 't' && p[1] == 'c'
3513 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
3514 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
3515 && !ASCII_ISALPHA(p[3]))
3516 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
3517 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
3518 || (p[0] == 'm' && p[1] == 'z'
3519 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
3520 ))
3521 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003522 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003523 p = skipwhite(arg + 2);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003524 if (STRNCMP(p, "trim", 4) == 0)
3525 {
3526 // Ignore leading white space.
3527 p = skipwhite(p + 4);
3528 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003529 skipwhite(theline) - theline);
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003530 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003531 if (*p == NUL)
3532 skip_until = vim_strsave((char_u *)".");
3533 else
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003534 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003535 getline_options = GETLINE_NONE;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +02003536 is_heredoc = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003537 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003538
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003539 // Check for ":cmd v =<< [trim] EOF"
3540 // and ":cmd [a, b] =<< [trim] EOF"
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003541 // and "lines =<< [trim] EOF" for Vim9
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003542 // Where "cmd" can be "let", "var", "final" or "const".
Bram Moolenaar8471e572019-05-19 21:37:18 +02003543 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003544 if (*arg == '[')
3545 arg = vim_strchr(arg, ']');
3546 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02003547 {
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003548 int found = (eap->cmdidx == CMD_def && arg[0] == '='
3549 && arg[1] == '<' && arg[2] =='<');
3550
3551 if (!found)
3552 // skip over the argument after "cmd"
3553 arg = skipwhite(skiptowhite(arg));
3554 if (found || (arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02003555 && (checkforcmd(&p, "let", 2)
3556 || checkforcmd(&p, "var", 3)
3557 || checkforcmd(&p, "final", 5)
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003558 || checkforcmd(&p, "const", 5))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02003559 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003560 p = skipwhite(arg + 3);
3561 if (STRNCMP(p, "trim", 4) == 0)
3562 {
3563 // Ignore leading white space.
3564 p = skipwhite(p + 4);
3565 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003566 skipwhite(theline) - theline);
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003567 }
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003568 skip_until = vim_strnsave(p, skiptowhite(p) - p);
Bram Moolenaar66250c92020-08-20 15:02:42 +02003569 getline_options = GETLINE_NONE;
Bram Moolenaar1e673b92019-11-06 15:02:50 +01003570 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02003571 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02003572 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003573 }
3574
Bram Moolenaare38eab22019-12-05 21:50:01 +01003575 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003576 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003577 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003578
Bram Moolenaare38eab22019-12-05 21:50:01 +01003579 // Copy the line to newly allocated memory. get_one_sourceline()
3580 // allocates 250 bytes per line, this saves 80% on average. The cost
3581 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003582 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003583 if (p == NULL)
3584 goto erret;
3585 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003586
Bram Moolenaare38eab22019-12-05 21:50:01 +01003587 // Add NULL lines for continuation lines, so that the line count is
3588 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003589 while (sourcing_lnum_off-- > 0)
3590 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
3591
Bram Moolenaare38eab22019-12-05 21:50:01 +01003592 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003593 if (line_arg != NULL && *line_arg == NUL)
3594 line_arg = NULL;
3595 }
3596
Bram Moolenaare38eab22019-12-05 21:50:01 +01003597 // Don't define the function when skipping commands or when an error was
3598 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003599 if (eap->skip || did_emsg)
3600 goto erret;
3601
3602 /*
3603 * If there are no errors, add the function
3604 */
3605 if (fudi.fd_dict == NULL)
3606 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607 hashtab_T *ht;
3608
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003609 v = find_var(name, &ht, FALSE);
3610 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
3611 {
3612 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
3613 name);
3614 goto erret;
3615 }
3616
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003617 fp = find_func_even_dead(name, is_global, NULL);
Bram Moolenaareef21022020-08-01 22:16:43 +02003618 if (vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003619 {
Bram Moolenaareef21022020-08-01 22:16:43 +02003620 char_u *uname = untrans_function_name(name);
3621
3622 import = find_imported(uname == NULL ? name : uname, 0, NULL);
3623 }
3624
3625 if (fp != NULL || import != NULL)
3626 {
3627 int dead = fp != NULL && (fp->uf_flags & FC_DEAD);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003629 // Function can be replaced with "function!" and when sourcing the
3630 // same script again, but only once.
Bram Moolenaareef21022020-08-01 22:16:43 +02003631 // A name that is used by an import can not be overruled.
3632 if (import != NULL
3633 || (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003634 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
Bram Moolenaareef21022020-08-01 22:16:43 +02003635 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003636 {
Bram Moolenaareef21022020-08-01 22:16:43 +02003637 if (vim9script)
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02003638 emsg_funcname(e_name_already_defined_str, name);
Bram Moolenaareef21022020-08-01 22:16:43 +02003639 else
3640 emsg_funcname(e_funcexts, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003641 goto erret;
3642 }
3643 if (fp->uf_calls > 0)
3644 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003645 emsg_funcname(
3646 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003647 name);
3648 goto erret;
3649 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003650 if (fp->uf_refcount > 1)
3651 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003652 // This function is referenced somewhere, don't redefine it but
3653 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003654 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003655 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003656 fp = NULL;
3657 overwrite = TRUE;
3658 }
3659 else
3660 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003661 char_u *exp_name = fp->uf_name_exp;
3662
3663 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01003664 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003665 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003666 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01003667 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02003669#ifdef FEAT_PROFILE
3670 fp->uf_profiling = FALSE;
3671 fp->uf_prof_initialized = FALSE;
3672#endif
Bram Moolenaarcdc40c42020-12-26 17:43:08 +01003673 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003674 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003675 }
3676 }
3677 else
3678 {
3679 char numbuf[20];
3680
3681 fp = NULL;
3682 if (fudi.fd_newkey == NULL && !eap->forceit)
3683 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003684 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003685 goto erret;
3686 }
3687 if (fudi.fd_di == NULL)
3688 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003689 // Can't add a function to a locked dictionary
Bram Moolenaara187c432020-09-16 21:08:28 +02003690 if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003691 goto erret;
3692 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003693 // Can't change an existing function if it is locked
Bram Moolenaara187c432020-09-16 21:08:28 +02003694 else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003695 goto erret;
3696
Bram Moolenaare38eab22019-12-05 21:50:01 +01003697 // Give the function a sequential number. Can only be used with a
3698 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003699 vim_free(name);
3700 sprintf(numbuf, "%d", ++func_nr);
3701 name = vim_strsave((char_u *)numbuf);
3702 if (name == NULL)
3703 goto erret;
3704 }
3705
3706 if (fp == NULL)
3707 {
3708 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
3709 {
3710 int slen, plen;
3711 char_u *scriptname;
3712
Bram Moolenaare38eab22019-12-05 21:50:01 +01003713 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003714 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003715 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003716 {
3717 scriptname = autoload_name(name);
3718 if (scriptname != NULL)
3719 {
3720 p = vim_strchr(scriptname, '/');
3721 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003722 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003723 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003724 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003725 j = OK;
3726 vim_free(scriptname);
3727 }
3728 }
3729 if (j == FAIL)
3730 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003731 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003732 goto erret;
3733 }
3734 }
3735
Bram Moolenaar47ed5532019-08-08 20:49:14 +02003736 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003737 if (fp == NULL)
3738 goto erret;
3739
3740 if (fudi.fd_dict != NULL)
3741 {
3742 if (fudi.fd_di == NULL)
3743 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003744 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003745 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
3746 if (fudi.fd_di == NULL)
3747 {
3748 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003749 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003750 goto erret;
3751 }
3752 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
3753 {
3754 vim_free(fudi.fd_di);
3755 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003756 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003757 goto erret;
3758 }
3759 }
3760 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003761 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003762 clear_tv(&fudi.fd_di->di_tv);
3763 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003764 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003765
Bram Moolenaare38eab22019-12-05 21:50:01 +01003766 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003767 flags |= FC_DICT;
3768 }
3769
Bram Moolenaare38eab22019-12-05 21:50:01 +01003770 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003771 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003772 if (overwrite)
3773 {
3774 hi = hash_find(&func_hashtab, name);
3775 hi->hi_key = UF2HIKEY(fp);
3776 }
3777 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003778 {
3779 vim_free(fp);
Bram Moolenaara14e6972020-05-25 23:29:28 +02003780 fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003781 goto erret;
3782 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003783 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003784 }
3785 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003786 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003788 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789
3790 if (eap->cmdidx == CMD_def)
3791 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003792 int lnum_save = SOURCING_LNUM;
3793 cstack_T *cstack = eap->cstack;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003794
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003795 fp->uf_def_status = UF_TO_BE_COMPILED;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003796
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003797 // error messages are for the first function line
3798 SOURCING_LNUM = sourcing_lnum_top;
3799
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003800 if (cstack != NULL && cstack->cs_idx >= 0)
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003801 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003802 int count = cstack->cs_idx + 1;
3803 int i;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003804
3805 // The block context may be needed for script variables declared in
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003806 // a block that is visible now but not when the function is called
3807 // later.
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003808 fp->uf_block_ids = ALLOC_MULT(int, count);
3809 if (fp->uf_block_ids != NULL)
3810 {
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003811 mch_memmove(fp->uf_block_ids, cstack->cs_block_id,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003812 sizeof(int) * count);
3813 fp->uf_block_depth = count;
3814 }
Bram Moolenaar39ca4122020-10-20 14:25:07 +02003815
3816 // Set flag in each block to indicate a function was defined. This
3817 // is used to keep the variable when leaving the block, see
3818 // hide_script_var().
3819 for (i = 0; i <= cstack->cs_idx; ++i)
3820 cstack->cs_flags[i] |= CSF_FUNC_DEF;
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003821 }
3822
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003823 if (parse_argument_types(fp, &argtypes, varargs) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824 {
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003825 SOURCING_LNUM = lnum_save;
3826 goto errret_2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 }
Bram Moolenaarb4d16cb2020-11-05 18:45:46 +01003828 varargs = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829
3830 // parse the return type, if any
3831 if (ret_type == NULL)
3832 fp->uf_ret_type = &t_void;
3833 else
3834 {
3835 p = ret_type;
Bram Moolenaar9e68c322020-12-25 12:38:04 +01003836 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02003838 SOURCING_LNUM = lnum_save;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003840 else
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003841 fp->uf_def_status = UF_NOT_COMPILED;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003843 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003844 if ((flags & FC_CLOSURE) != 0)
3845 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003846 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003847 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003848 }
3849 else
3850 fp->uf_scoped = NULL;
3851
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003852#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003853 if (prof_def_func())
3854 func_do_profile(fp);
3855#endif
3856 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003857 if (sandbox)
3858 flags |= FC_SANDBOX;
Bram Moolenaare7e48382020-07-22 18:17:08 +02003859 if (vim9script && !ASCII_ISUPPER(*fp->uf_name))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003860 flags |= FC_VIM9;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003861 fp->uf_flags = flags;
3862 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003863 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003864 fp->uf_script_ctx = current_sctx;
Bram Moolenaarf9b2b492020-08-05 14:34:14 +02003865 fp->uf_script_ctx_version = current_sctx.sc_version;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003866 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 if (is_export)
3868 {
3869 fp->uf_flags |= FC_EXPORT;
3870 // let ex_export() know the export worked.
3871 is_export = FALSE;
3872 }
3873
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003874 if (eap->cmdidx == CMD_def)
3875 set_function_type(fp);
Bram Moolenaar67979662020-06-20 22:50:47 +02003876 else if (fp->uf_script_ctx.sc_version == SCRIPT_VERSION_VIM9)
3877 // :func does not use Vim9 script syntax, even in a Vim9 script file
3878 fp->uf_script_ctx.sc_version = SCRIPT_VERSION_MAX;
Bram Moolenaar6ff71d82020-05-24 23:45:24 +02003879
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003880 goto ret_free;
3881
3882erret:
3883 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003884 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003885errret_2:
3886 ga_clear_strings(&newlines);
3887ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003888 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003889 vim_free(skip_until);
Bram Moolenaarfffdf472020-12-13 21:16:55 +01003890 vim_free(heredoc_trimmed);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003891 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003892 vim_free(fudi.fd_newkey);
Bram Moolenaar04b12692020-05-04 23:24:44 +02003893 if (name != name_arg)
3894 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003895 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003896 did_emsg |= saved_did_emsg;
3897 need_wait_return |= saved_wait_return;
Bram Moolenaar04b12692020-05-04 23:24:44 +02003898
3899 return fp;
3900}
3901
3902/*
3903 * ":function"
3904 */
3905 void
3906ex_function(exarg_T *eap)
3907{
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003908 (void)define_function(eap, NULL);
Bram Moolenaar822ba242020-05-24 23:00:18 +02003909}
3910
3911/*
Bram Moolenaar96f8f492020-09-09 17:08:51 +02003912 * :defcompile - compile all :def functions in the current script that need to
3913 * be compiled. Except dead functions.
Bram Moolenaar822ba242020-05-24 23:00:18 +02003914 */
3915 void
3916ex_defcompile(exarg_T *eap UNUSED)
3917{
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003918 long todo = (long)func_hashtab.ht_used;
3919 int changed = func_hashtab.ht_changed;
Bram Moolenaar822ba242020-05-24 23:00:18 +02003920 hashitem_T *hi;
3921 ufunc_T *ufunc;
3922
3923 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
3924 {
3925 if (!HASHITEM_EMPTY(hi))
3926 {
3927 --todo;
3928 ufunc = HI2UF(hi);
3929 if (ufunc->uf_script_ctx.sc_sid == current_sctx.sc_sid
Bram Moolenaar96f8f492020-09-09 17:08:51 +02003930 && ufunc->uf_def_status == UF_TO_BE_COMPILED
3931 && (ufunc->uf_flags & FC_DEAD) == 0)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003932 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02003933 compile_def_function(ufunc, FALSE, NULL);
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003934
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003935 if (func_hashtab.ht_changed != changed)
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003936 {
Bram Moolenaar7ce85be2020-07-14 15:01:05 +02003937 // a function has been added or removed, need to start over
3938 todo = (long)func_hashtab.ht_used;
3939 changed = func_hashtab.ht_changed;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003940 hi = func_hashtab.ht_array;
Bram Moolenaar285b1892020-05-26 11:37:26 +02003941 --hi;
Bram Moolenaarebc3de62020-05-26 11:08:28 +02003942 }
3943 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02003944 }
3945 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003946}
3947
3948/*
3949 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3950 * Return 2 if "p" starts with "s:".
3951 * Return 0 otherwise.
3952 */
3953 int
3954eval_fname_script(char_u *p)
3955{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003956 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3957 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003958 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3959 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3960 return 5;
3961 if (p[0] == 's' && p[1] == ':')
3962 return 2;
3963 return 0;
3964}
3965
3966 int
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003967translated_function_exists(char_u *name, int is_global)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003968{
3969 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003970 return has_internal_func(name);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003971 return find_func(name, is_global, NULL) != NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972}
3973
3974/*
3975 * Return TRUE when "ufunc" has old-style "..." varargs
3976 * or named varargs "...name: type".
3977 */
3978 int
3979has_varargs(ufunc_T *ufunc)
3980{
3981 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003982}
3983
3984/*
3985 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003986 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003987 */
3988 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003989function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003990{
3991 char_u *nm = name;
3992 char_u *p;
3993 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003994 int flag;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003995 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003996
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003997 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3998 if (no_deref)
3999 flag |= TFN_NO_DEREF;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004000 p = trans_function_name(&nm, &is_global, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004001 nm = skipwhite(nm);
4002
Bram Moolenaare38eab22019-12-05 21:50:01 +01004003 // Only accept "funcname", "funcname ", "funcname (..." and
4004 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004005 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004006 n = translated_function_exists(p, is_global);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004007 vim_free(p);
4008 return n;
4009}
4010
Bram Moolenaar113e1072019-01-20 15:30:40 +01004011#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004012 char_u *
4013get_expanded_name(char_u *name, int check)
4014{
4015 char_u *nm = name;
4016 char_u *p;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004017 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004018
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004019 p = trans_function_name(&nm, &is_global, FALSE,
4020 TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004021
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004022 if (p != NULL && *nm == NUL
4023 && (!check || translated_function_exists(p, is_global)))
4024 return p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004025
4026 vim_free(p);
4027 return NULL;
4028}
Bram Moolenaar113e1072019-01-20 15:30:40 +01004029#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004030
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004031/*
4032 * Function given to ExpandGeneric() to obtain the list of user defined
4033 * function names.
4034 */
4035 char_u *
4036get_user_func_name(expand_T *xp, int idx)
4037{
4038 static long_u done;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004039 static int changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004040 static hashitem_T *hi;
4041 ufunc_T *fp;
4042
4043 if (idx == 0)
4044 {
4045 done = 0;
4046 hi = func_hashtab.ht_array;
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004047 changed = func_hashtab.ht_changed;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004048 }
Bram Moolenaar1f22cc52020-07-14 21:08:49 +02004049 if (changed == func_hashtab.ht_changed && done < func_hashtab.ht_used)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004050 {
4051 if (done++ > 0)
4052 ++hi;
4053 while (HASHITEM_EMPTY(hi))
4054 ++hi;
4055 fp = HI2UF(hi);
4056
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057 // don't show dead, dict and lambda functions
4058 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02004059 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004061
4062 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004063 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004064
4065 cat_func_name(IObuff, fp);
4066 if (xp->xp_context != EXPAND_USER_FUNC)
4067 {
4068 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004070 STRCAT(IObuff, ")");
4071 }
4072 return IObuff;
4073 }
4074 return NULL;
4075}
4076
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004077/*
4078 * ":delfunction {name}"
4079 */
4080 void
4081ex_delfunction(exarg_T *eap)
4082{
4083 ufunc_T *fp = NULL;
4084 char_u *p;
4085 char_u *name;
4086 funcdict_T fudi;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004087 int is_global = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004088
4089 p = eap->arg;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004090 name = trans_function_name(&p, &is_global, eap->skip, 0, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004091 vim_free(fudi.fd_newkey);
4092 if (name == NULL)
4093 {
4094 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004095 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004096 return;
4097 }
4098 if (!ends_excmd(*skipwhite(p)))
4099 {
4100 vim_free(name);
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004101 semsg(_(e_trailing_arg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004102 return;
4103 }
4104 eap->nextcmd = check_nextcmd(p);
4105 if (eap->nextcmd != NULL)
4106 *p = NUL;
4107
4108 if (!eap->skip)
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004109 fp = find_func(name, is_global, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004110 vim_free(name);
4111
4112 if (!eap->skip)
4113 {
4114 if (fp == NULL)
4115 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02004116 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004117 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004118 return;
4119 }
4120 if (fp->uf_calls > 0)
4121 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004122 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004123 return;
4124 }
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004125 if (fp->uf_flags & FC_VIM9)
4126 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004127 semsg(_(e_cannot_delete_vim9_script_function_str), eap->arg);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004128 return;
4129 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004130
4131 if (fudi.fd_dict != NULL)
4132 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004133 // Delete the dict item that refers to the function, it will
4134 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004135 dictitem_remove(fudi.fd_dict, fudi.fd_di);
4136 }
4137 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004138 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004139 // A normal function (not a numbered function or lambda) has a
4140 // refcount of 1 for the entry in the hashtable. When deleting
4141 // it and the refcount is more than one, it should be kept.
4142 // A numbered function and lambda should be kept if the refcount is
4143 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004144 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004145 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004146 // Function is still referenced somewhere. Don't free it but
4147 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004148 if (func_remove(fp))
4149 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004150 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004151 }
4152 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004153 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004154 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004155 }
4156}
4157
4158/*
4159 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004160 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004161 */
4162 void
4163func_unref(char_u *name)
4164{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004165 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004166
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004167 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004168 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004169 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004170 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004171 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004172#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004173 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004174#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01004175 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004176 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004177 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004178 {
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 Moolenaar437bafe2016-08-01 15:40:54 +02004181 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004182 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02004183 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004184}
4185
4186/*
4187 * Unreference a Function: decrement the reference count and free it when it
4188 * becomes zero.
4189 */
4190 void
4191func_ptr_unref(ufunc_T *fp)
4192{
Bram Moolenaar97baee82016-07-26 20:46:08 +02004193 if (fp != NULL && --fp->uf_refcount <= 0)
4194 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004195 // Only delete it when it's not being used. Otherwise it's done
4196 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02004197 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01004198 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004199 }
4200}
4201
4202/*
4203 * Count a reference to a Function.
4204 */
4205 void
4206func_ref(char_u *name)
4207{
4208 ufunc_T *fp;
4209
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02004210 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004211 return;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004212 fp = find_func(name, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004213 if (fp != NULL)
4214 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004215 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01004216 // Only give an error for a numbered function.
4217 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01004218 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004219}
4220
4221/*
4222 * Count a reference to a Function.
4223 */
4224 void
4225func_ptr_ref(ufunc_T *fp)
4226{
4227 if (fp != NULL)
4228 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004229}
4230
4231/*
4232 * Return TRUE if items in "fc" do not have "copyID". That means they are not
4233 * referenced from anywhere that is in use.
4234 */
4235 static int
4236can_free_funccal(funccall_T *fc, int copyID)
4237{
4238 return (fc->l_varlist.lv_copyID != copyID
4239 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004240 && fc->l_avars.dv_copyID != copyID
4241 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004242}
4243
4244/*
4245 * ":return [expr]"
4246 */
4247 void
4248ex_return(exarg_T *eap)
4249{
4250 char_u *arg = eap->arg;
4251 typval_T rettv;
4252 int returning = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004253 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004254
4255 if (current_funccal == NULL)
4256 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004257 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004258 return;
4259 }
4260
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004261 CLEAR_FIELD(evalarg);
4262 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
4263
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004264 if (eap->skip)
4265 ++emsg_skip;
4266
4267 eap->nextcmd = NULL;
4268 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004269 && eval0(arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004270 {
4271 if (!eap->skip)
4272 returning = do_return(eap, FALSE, TRUE, &rettv);
4273 else
4274 clear_tv(&rettv);
4275 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01004276 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004277 else if (!eap->skip)
4278 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004279 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01004280 update_force_abort();
4281
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004282 /*
4283 * Return unless the expression evaluation has been cancelled due to an
4284 * aborting error, an interrupt, or an exception.
4285 */
4286 if (!aborting())
4287 returning = do_return(eap, FALSE, TRUE, NULL);
4288 }
4289
Bram Moolenaare38eab22019-12-05 21:50:01 +01004290 // When skipping or the return gets pending, advance to the next command
4291 // in this line (!returning). Otherwise, ignore the rest of the line.
4292 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004293 if (returning)
4294 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004295 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004296 eap->nextcmd = check_nextcmd(arg);
4297
4298 if (eap->skip)
4299 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02004300 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004301}
4302
4303/*
4304 * ":1,25call func(arg1, arg2)" function call.
4305 */
4306 void
4307ex_call(exarg_T *eap)
4308{
4309 char_u *arg = eap->arg;
4310 char_u *startarg;
4311 char_u *name;
4312 char_u *tofree;
4313 int len;
4314 typval_T rettv;
4315 linenr_T lnum;
4316 int doesrange;
4317 int failed = FALSE;
4318 funcdict_T fudi;
4319 partial_T *partial = NULL;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004320 evalarg_T evalarg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004321
Bram Moolenaare6b53242020-07-01 17:28:33 +02004322 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004323 if (eap->skip)
4324 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004325 // trans_function_name() doesn't work well when skipping, use eval0()
4326 // instead to skip to any following command, e.g. for:
4327 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004328 ++emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004329 if (eval0(eap->arg, &rettv, eap, &evalarg) != FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004330 clear_tv(&rettv);
4331 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004332 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004333 return;
4334 }
4335
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004336 tofree = trans_function_name(&arg, NULL, eap->skip,
4337 TFN_INT, &fudi, &partial);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004338 if (fudi.fd_newkey != NULL)
4339 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004340 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004341 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004342 vim_free(fudi.fd_newkey);
4343 }
4344 if (tofree == NULL)
4345 return;
4346
Bram Moolenaare38eab22019-12-05 21:50:01 +01004347 // Increase refcount on dictionary, it could get deleted when evaluating
4348 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004349 if (fudi.fd_dict != NULL)
4350 ++fudi.fd_dict->dv_refcount;
4351
Bram Moolenaare38eab22019-12-05 21:50:01 +01004352 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
4353 // contents. For VAR_PARTIAL get its partial, unless we already have one
4354 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004355 len = (int)STRLEN(tofree);
4356 name = deref_func_name(tofree, &len,
4357 partial != NULL ? NULL : &partial, FALSE);
4358
Bram Moolenaare38eab22019-12-05 21:50:01 +01004359 // Skip white space to allow ":call func ()". Not good, but required for
4360 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004361 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004362 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004363
4364 if (*startarg != '(')
4365 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004367 goto end;
4368 }
4369
4370 /*
4371 * When skipping, evaluate the function once, to find the end of the
4372 * arguments.
4373 * When the function takes a range, this is discovered after the first
4374 * call, and the loop is broken.
4375 */
4376 if (eap->skip)
4377 {
4378 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004379 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004380 }
4381 else
4382 lnum = eap->line1;
4383 for ( ; lnum <= eap->line2; ++lnum)
4384 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004385 funcexe_T funcexe;
4386
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004387 if (!eap->skip && eap->addr_count > 0)
4388 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004389 if (lnum > curbuf->b_ml.ml_line_count)
4390 {
4391 // If the function deleted lines or switched to another buffer
4392 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004393 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01004394 break;
4395 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004396 curwin->w_cursor.lnum = lnum;
4397 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004398 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004399 }
4400 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004401
Bram Moolenaara80faa82020-04-12 19:37:17 +02004402 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02004403 funcexe.firstline = eap->line1;
4404 funcexe.lastline = eap->line2;
4405 funcexe.doesrange = &doesrange;
4406 funcexe.evaluate = !eap->skip;
4407 funcexe.partial = partial;
4408 funcexe.selfdict = fudi.fd_dict;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004409 if (get_func_tv(name, -1, &rettv, &arg, &evalarg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004410 {
4411 failed = TRUE;
4412 break;
4413 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01004414 if (has_watchexpr())
4415 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004416
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004417 // Handle a function returning a Funcref, Dictionary or List.
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004418 if (handle_subscript(&arg, &rettv,
4419 eap->skip ? NULL : &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004420 {
4421 failed = TRUE;
4422 break;
4423 }
4424
4425 clear_tv(&rettv);
4426 if (doesrange || eap->skip)
4427 break;
4428
Bram Moolenaare38eab22019-12-05 21:50:01 +01004429 // Stop when immediately aborting on error, or when an interrupt
4430 // occurred or an exception was thrown but not caught.
4431 // get_func_tv() returned OK, so that the check for trailing
4432 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004433 if (aborting())
4434 break;
4435 }
4436 if (eap->skip)
4437 --emsg_skip;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004438 clear_evalarg(&evalarg, eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004439
Bram Moolenaare51bb172020-02-16 19:42:23 +01004440 // When inside :try we need to check for following "| catch".
4441 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004442 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004443 // Check for trailing illegal characters and a following command.
Bram Moolenaar8294d492020-08-10 22:40:56 +02004444 arg = skipwhite(arg);
Bram Moolenaara72cfb82020-04-23 17:07:30 +02004445 if (!ends_excmd2(eap->arg, arg))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004446 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004447 if (!failed)
4448 {
4449 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02004450 semsg(_(e_trailing_arg), arg);
Bram Moolenaar40d9da22020-02-17 10:01:24 +01004451 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004452 }
4453 else
4454 eap->nextcmd = check_nextcmd(arg);
4455 }
4456
4457end:
4458 dict_unref(fudi.fd_dict);
4459 vim_free(tofree);
4460}
4461
4462/*
4463 * Return from a function. Possibly makes the return pending. Also called
4464 * for a pending return at the ":endtry" or after returning from an extra
4465 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
4466 * when called due to a ":return" command. "rettv" may point to a typval_T
4467 * with the return rettv. Returns TRUE when the return can be carried out,
4468 * FALSE when the return gets pending.
4469 */
4470 int
4471do_return(
4472 exarg_T *eap,
4473 int reanimate,
4474 int is_cmd,
4475 void *rettv)
4476{
4477 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01004478 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004479
4480 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004481 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004482 current_funccal->returned = FALSE;
4483
4484 /*
4485 * Cleanup (and inactivate) conditionals, but stop when a try conditional
4486 * not in its finally clause (which then is to be executed next) is found.
4487 * In this case, make the ":return" pending for execution at the ":endtry".
4488 * Otherwise, return normally.
4489 */
4490 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
4491 if (idx >= 0)
4492 {
4493 cstack->cs_pending[idx] = CSTP_RETURN;
4494
4495 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004496 // A pending return again gets pending. "rettv" points to an
4497 // allocated variable with the rettv of the original ":return"'s
4498 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004499 cstack->cs_rettv[idx] = rettv;
4500 else
4501 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004502 // When undoing a return in order to make it pending, get the stored
4503 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004504 if (reanimate)
4505 rettv = current_funccal->rettv;
4506
4507 if (rettv != NULL)
4508 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004509 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004510 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
4511 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
4512 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004513 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004514 }
4515 else
4516 cstack->cs_rettv[idx] = NULL;
4517
4518 if (reanimate)
4519 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004520 // The pending return value could be overwritten by a ":return"
4521 // without argument in a finally clause; reset the default
4522 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004523 current_funccal->rettv->v_type = VAR_NUMBER;
4524 current_funccal->rettv->vval.v_number = 0;
4525 }
4526 }
4527 report_make_pending(CSTP_RETURN, rettv);
4528 }
4529 else
4530 {
4531 current_funccal->returned = TRUE;
4532
Bram Moolenaare38eab22019-12-05 21:50:01 +01004533 // If the return is carried out now, store the return value. For
4534 // a return immediately after reanimation, the value is already
4535 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004536 if (!reanimate && rettv != NULL)
4537 {
4538 clear_tv(current_funccal->rettv);
4539 *current_funccal->rettv = *(typval_T *)rettv;
4540 if (!is_cmd)
4541 vim_free(rettv);
4542 }
4543 }
4544
4545 return idx < 0;
4546}
4547
4548/*
4549 * Free the variable with a pending return value.
4550 */
4551 void
4552discard_pending_return(void *rettv)
4553{
4554 free_tv((typval_T *)rettv);
4555}
4556
4557/*
4558 * Generate a return command for producing the value of "rettv". The result
4559 * is an allocated string. Used by report_pending() for verbose messages.
4560 */
4561 char_u *
4562get_return_cmd(void *rettv)
4563{
4564 char_u *s = NULL;
4565 char_u *tofree = NULL;
4566 char_u numbuf[NUMBUFLEN];
4567
4568 if (rettv != NULL)
4569 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
4570 if (s == NULL)
4571 s = (char_u *)"";
4572
4573 STRCPY(IObuff, ":return ");
4574 STRNCPY(IObuff + 8, s, IOSIZE - 8);
4575 if (STRLEN(s) + 8 >= IOSIZE)
4576 STRCPY(IObuff + IOSIZE - 4, "...");
4577 vim_free(tofree);
4578 return vim_strsave(IObuff);
4579}
4580
4581/*
4582 * Get next function line.
4583 * Called by do_cmdline() to get the next line.
4584 * Returns allocated string, or NULL for end of function.
4585 */
4586 char_u *
4587get_func_line(
4588 int c UNUSED,
4589 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02004590 int indent UNUSED,
Bram Moolenaar66250c92020-08-20 15:02:42 +02004591 getline_opt_T options UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004592{
4593 funccall_T *fcp = (funccall_T *)cookie;
4594 ufunc_T *fp = fcp->func;
4595 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004596 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004597
Bram Moolenaare38eab22019-12-05 21:50:01 +01004598 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004599 if (fcp->dbg_tick != debug_tick)
4600 {
4601 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004602 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004603 fcp->dbg_tick = debug_tick;
4604 }
4605#ifdef FEAT_PROFILE
4606 if (do_profiling == PROF_YES)
4607 func_line_end(cookie);
4608#endif
4609
4610 gap = &fp->uf_lines;
4611 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4612 || fcp->returned)
4613 retval = NULL;
4614 else
4615 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004616 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004617 while (fcp->linenr < gap->ga_len
4618 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
4619 ++fcp->linenr;
4620 if (fcp->linenr >= gap->ga_len)
4621 retval = NULL;
4622 else
4623 {
4624 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004625 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004626#ifdef FEAT_PROFILE
4627 if (do_profiling == PROF_YES)
4628 func_line_start(cookie);
4629#endif
4630 }
4631 }
4632
Bram Moolenaare38eab22019-12-05 21:50:01 +01004633 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004634 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004635 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004636 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01004637 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004638 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01004639 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004640 fcp->dbg_tick = debug_tick;
4641 }
4642
4643 return retval;
4644}
4645
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004646/*
4647 * Return TRUE if the currently active function should be ended, because a
4648 * return was encountered or an error occurred. Used inside a ":while".
4649 */
4650 int
4651func_has_ended(void *cookie)
4652{
4653 funccall_T *fcp = (funccall_T *)cookie;
4654
Bram Moolenaare38eab22019-12-05 21:50:01 +01004655 // Ignore the "abort" flag if the abortion behavior has been changed due to
4656 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004657 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
4658 || fcp->returned);
4659}
4660
4661/*
4662 * return TRUE if cookie indicates a function which "abort"s on errors.
4663 */
4664 int
4665func_has_abort(
4666 void *cookie)
4667{
4668 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
4669}
4670
4671
4672/*
4673 * Turn "dict.Func" into a partial for "Func" bound to "dict".
4674 * Don't do this when "Func" is already a partial that was bound
4675 * explicitly (pt_auto is FALSE).
4676 * Changes "rettv" in-place.
4677 * Returns the updated "selfdict_in".
4678 */
4679 dict_T *
4680make_partial(dict_T *selfdict_in, typval_T *rettv)
4681{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004682 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004683 char_u *tofree = NULL;
4684 ufunc_T *fp;
4685 char_u fname_buf[FLEN_FIXED + 1];
4686 int error;
4687 dict_T *selfdict = selfdict_in;
4688
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004689 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
4690 fp = rettv->vval.v_partial->pt_func;
4691 else
4692 {
4693 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
4694 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01004695 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004696 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02004697 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004698 vim_free(tofree);
4699 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004700
4701 if (fp != NULL && (fp->uf_flags & FC_DICT))
4702 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004703 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004704
4705 if (pt != NULL)
4706 {
4707 pt->pt_refcount = 1;
4708 pt->pt_dict = selfdict;
4709 pt->pt_auto = TRUE;
4710 selfdict = NULL;
4711 if (rettv->v_type == VAR_FUNC)
4712 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01004713 // Just a function: Take over the function name and use
4714 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004715 pt->pt_name = rettv->vval.v_string;
4716 }
4717 else
4718 {
4719 partial_T *ret_pt = rettv->vval.v_partial;
4720 int i;
4721
Bram Moolenaare38eab22019-12-05 21:50:01 +01004722 // Partial: copy the function name, use selfdict and copy
4723 // args. Can't take over name or args, the partial might
4724 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004725 if (ret_pt->pt_name != NULL)
4726 {
4727 pt->pt_name = vim_strsave(ret_pt->pt_name);
4728 func_ref(pt->pt_name);
4729 }
4730 else
4731 {
4732 pt->pt_func = ret_pt->pt_func;
4733 func_ptr_ref(pt->pt_func);
4734 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004735 if (ret_pt->pt_argc > 0)
4736 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004737 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004738 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004739 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004740 pt->pt_argc = 0;
4741 else
4742 {
4743 pt->pt_argc = ret_pt->pt_argc;
4744 for (i = 0; i < pt->pt_argc; i++)
4745 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
4746 }
4747 }
4748 partial_unref(ret_pt);
4749 }
4750 rettv->v_type = VAR_PARTIAL;
4751 rettv->vval.v_partial = pt;
4752 }
4753 }
4754 return selfdict;
4755}
4756
4757/*
4758 * Return the name of the executed function.
4759 */
4760 char_u *
4761func_name(void *cookie)
4762{
4763 return ((funccall_T *)cookie)->func->uf_name;
4764}
4765
4766/*
4767 * Return the address holding the next breakpoint line for a funccall cookie.
4768 */
4769 linenr_T *
4770func_breakpoint(void *cookie)
4771{
4772 return &((funccall_T *)cookie)->breakpoint;
4773}
4774
4775/*
4776 * Return the address holding the debug tick for a funccall cookie.
4777 */
4778 int *
4779func_dbg_tick(void *cookie)
4780{
4781 return &((funccall_T *)cookie)->dbg_tick;
4782}
4783
4784/*
4785 * Return the nesting level for a funccall cookie.
4786 */
4787 int
4788func_level(void *cookie)
4789{
4790 return ((funccall_T *)cookie)->level;
4791}
4792
4793/*
4794 * Return TRUE when a function was ended by a ":return" command.
4795 */
4796 int
4797current_func_returned(void)
4798{
4799 return current_funccal->returned;
4800}
4801
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004802 int
4803free_unref_funccal(int copyID, int testing)
4804{
4805 int did_free = FALSE;
4806 int did_free_funccal = FALSE;
4807 funccall_T *fc, **pfc;
4808
4809 for (pfc = &previous_funccal; *pfc != NULL; )
4810 {
4811 if (can_free_funccal(*pfc, copyID))
4812 {
4813 fc = *pfc;
4814 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01004815 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004816 did_free = TRUE;
4817 did_free_funccal = TRUE;
4818 }
4819 else
4820 pfc = &(*pfc)->caller;
4821 }
4822 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004823 // When a funccal was freed some more items might be garbage
4824 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004825 (void)garbage_collect(testing);
4826
4827 return did_free;
4828}
4829
4830/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004831 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004832 */
4833 static funccall_T *
4834get_funccal(void)
4835{
4836 int i;
4837 funccall_T *funccal;
4838 funccall_T *temp_funccal;
4839
4840 funccal = current_funccal;
4841 if (debug_backtrace_level > 0)
4842 {
4843 for (i = 0; i < debug_backtrace_level; i++)
4844 {
4845 temp_funccal = funccal->caller;
4846 if (temp_funccal)
4847 funccal = temp_funccal;
4848 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004849 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004850 debug_backtrace_level = i;
4851 }
4852 }
4853 return funccal;
4854}
4855
4856/*
4857 * Return the hashtable used for local variables in the current funccal.
4858 * Return NULL if there is no current funccal.
4859 */
4860 hashtab_T *
4861get_funccal_local_ht()
4862{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004864 return NULL;
4865 return &get_funccal()->l_vars.dv_hashtab;
4866}
4867
4868/*
4869 * Return the l: scope variable.
4870 * Return NULL if there is no current funccal.
4871 */
4872 dictitem_T *
4873get_funccal_local_var()
4874{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004876 return NULL;
4877 return &get_funccal()->l_vars_var;
4878}
4879
4880/*
4881 * Return the hashtable used for argument in the current funccal.
4882 * Return NULL if there is no current funccal.
4883 */
4884 hashtab_T *
4885get_funccal_args_ht()
4886{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004888 return NULL;
4889 return &get_funccal()->l_avars.dv_hashtab;
4890}
4891
4892/*
4893 * Return the a: scope variable.
4894 * Return NULL if there is no current funccal.
4895 */
4896 dictitem_T *
4897get_funccal_args_var()
4898{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004899 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004900 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004901 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004902}
4903
4904/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004905 * List function variables, if there is a function.
4906 */
4907 void
4908list_func_vars(int *first)
4909{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004911 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004912 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004913}
4914
4915/*
4916 * If "ht" is the hashtable for local variables in the current funccal, return
4917 * the dict that contains it.
4918 * Otherwise return NULL.
4919 */
4920 dict_T *
4921get_current_funccal_dict(hashtab_T *ht)
4922{
4923 if (current_funccal != NULL
4924 && ht == &current_funccal->l_vars.dv_hashtab)
4925 return &current_funccal->l_vars;
4926 return NULL;
4927}
4928
4929/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004930 * Search hashitem in parent scope.
4931 */
4932 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004933find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004934{
4935 funccall_T *old_current_funccal = current_funccal;
4936 hashtab_T *ht;
4937 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004938 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004939
4940 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4941 return NULL;
4942
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004943 // Search in parent scope, which can be referenced from a lambda.
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004944 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004945 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004946 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004947 ht = find_var_ht(name, &varname);
4948 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004949 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004950 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004951 if (!HASHITEM_EMPTY(hi))
4952 {
4953 *pht = ht;
4954 break;
4955 }
4956 }
4957 if (current_funccal == current_funccal->func->uf_scoped)
4958 break;
4959 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004960 }
4961 current_funccal = old_current_funccal;
4962
4963 return hi;
4964}
4965
4966/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004967 * Search variable in parent scope.
4968 */
4969 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004970find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004971{
4972 dictitem_T *v = NULL;
4973 funccall_T *old_current_funccal = current_funccal;
4974 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004975 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004976
4977 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4978 return NULL;
4979
Bram Moolenaare38eab22019-12-05 21:50:01 +01004980 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004981 current_funccal = current_funccal->func->uf_scoped;
4982 while (current_funccal)
4983 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004984 ht = find_var_ht(name, &varname);
4985 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004986 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004987 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004988 if (v != NULL)
4989 break;
4990 }
4991 if (current_funccal == current_funccal->func->uf_scoped)
4992 break;
4993 current_funccal = current_funccal->func->uf_scoped;
4994 }
4995 current_funccal = old_current_funccal;
4996
4997 return v;
4998}
4999
5000/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005001 * Set "copyID + 1" in previous_funccal and callers.
5002 */
5003 int
5004set_ref_in_previous_funccal(int copyID)
5005{
5006 int abort = FALSE;
5007 funccall_T *fc;
5008
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005009 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005010 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005011 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005012 abort = abort
5013 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
5014 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005015 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005016 }
5017 return abort;
5018}
5019
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005020 static int
5021set_ref_in_funccal(funccall_T *fc, int copyID)
5022{
5023 int abort = FALSE;
5024
5025 if (fc->fc_copyID != copyID)
5026 {
5027 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005028 abort = abort
5029 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
5030 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005031 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02005032 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005033 }
5034 return abort;
5035}
5036
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005037/*
5038 * Set "copyID" in all local vars and arguments in the call stack.
5039 */
5040 int
5041set_ref_in_call_stack(int copyID)
5042{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005043 int abort = FALSE;
5044 funccall_T *fc;
5045 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005046
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005047 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005048 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005049
5050 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005051 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
5052 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02005053 abort = abort || set_ref_in_funccal(fc, copyID);
5054
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005055 return abort;
5056}
5057
5058/*
5059 * Set "copyID" in all functions available by name.
5060 */
5061 int
5062set_ref_in_functions(int copyID)
5063{
5064 int todo;
5065 hashitem_T *hi = NULL;
5066 int abort = FALSE;
5067 ufunc_T *fp;
5068
5069 todo = (int)func_hashtab.ht_used;
5070 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005071 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005072 if (!HASHITEM_EMPTY(hi))
5073 {
5074 --todo;
5075 fp = HI2UF(hi);
5076 if (!func_name_refcount(fp->uf_name))
5077 abort = abort || set_ref_in_func(NULL, fp, copyID);
5078 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005079 }
5080 return abort;
5081}
5082
5083/*
5084 * Set "copyID" in all function arguments.
5085 */
5086 int
5087set_ref_in_func_args(int copyID)
5088{
5089 int i;
5090 int abort = FALSE;
5091
5092 for (i = 0; i < funcargs.ga_len; ++i)
5093 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
5094 copyID, NULL, NULL);
5095 return abort;
5096}
5097
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005098/*
5099 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005100 * Returns TRUE if setting references failed somehow.
5101 */
5102 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005103set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005104{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005105 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005106 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01005107 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005108 char_u fname_buf[FLEN_FIXED + 1];
5109 char_u *tofree = NULL;
5110 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005111 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005112
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005113 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005114 return FALSE;
5115
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005116 if (fp_in == NULL)
5117 {
5118 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005119 fp = find_func(fname, FALSE, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005120 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005121 if (fp != NULL)
5122 {
5123 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005124 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005125 }
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02005126
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005127 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005128 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005129}
5130
Bram Moolenaare38eab22019-12-05 21:50:01 +01005131#endif // FEAT_EVAL