blob: 065425965c7920e46f3ba64eeadd35708b72181e [file] [log] [blame]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001/* vi:set ts=8 sts=4 sw=4:
2 *
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/*
11 * eval.c: User defined function support
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +020017/* function flags */
Bram Moolenaar8dd3a432016-08-01 20:46:25 +020018#define FC_ABORT 0x01 /* abort function on error */
19#define FC_RANGE 0x02 /* function accepts range */
20#define FC_DICT 0x04 /* Dict function, uses "self" */
21#define FC_CLOSURE 0x08 /* closure, uses outer scope variables */
22#define FC_DELETED 0x10 /* :delfunction used while uf_refcount > 0 */
23#define FC_REMOVED 0x20 /* function redefined while uf_refcount > 0 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +020024
25/* From user function to hashitem and back. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +020026#define UF2HIKEY(fp) ((fp)->uf_name)
Bram Moolenaar0a0f6412016-07-19 21:30:13 +020027#define HIKEY2UF(p) ((ufunc_T *)(p - offsetof(ufunc_T, uf_name)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +020028#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
29
30#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
31#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
32
Bram Moolenaara9b579f2016-07-17 18:29:19 +020033/*
34 * All user-defined functions are found in this hashtable.
35 */
36static hashtab_T func_hashtab;
37
38/* Used by get_func_tv() */
39static garray_T funcargs = GA_EMPTY;
40
41/* pointer to funccal for currently active function */
42funccall_T *current_funccal = NULL;
43
44/* pointer to list of previously used funccal, still around because some
45 * item in it is still being used. */
46funccall_T *previous_funccal = NULL;
47
48static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
49static char *e_funcdict = N_("E717: Dictionary entry already exists");
50static char *e_funcref = N_("E718: Funcref required");
51static char *e_nofunc = N_("E130: Unknown function: %s");
52
53#ifdef FEAT_PROFILE
54static void func_do_profile(ufunc_T *fp);
55static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
56static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
57static int
58# ifdef __BORLANDC__
59 _RTLENTRYF
60# endif
61 prof_total_cmp(const void *s1, const void *s2);
62static int
63# ifdef __BORLANDC__
64 _RTLENTRYF
65# endif
66 prof_self_cmp(const void *s1, const void *s2);
67#endif
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020068static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020069
70 void
71func_init()
72{
73 hash_init(&func_hashtab);
74}
75
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020076/*
77 * Get function arguments.
78 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +020079 static int
80get_function_args(
81 char_u **argp,
82 char_u endchar,
83 garray_T *newargs,
84 int *varargs,
85 int skip)
86{
87 int mustend = FALSE;
88 char_u *arg = *argp;
89 char_u *p = arg;
90 int c;
91 int i;
92
93 if (newargs != NULL)
94 ga_init2(newargs, (int)sizeof(char_u *), 3);
95
96 if (varargs != NULL)
97 *varargs = FALSE;
98
99 /*
100 * Isolate the arguments: "arg1, arg2, ...)"
101 */
102 while (*p != endchar)
103 {
104 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
105 {
106 if (varargs != NULL)
107 *varargs = TRUE;
108 p += 3;
109 mustend = TRUE;
110 }
111 else
112 {
113 arg = p;
114 while (ASCII_ISALNUM(*p) || *p == '_')
115 ++p;
116 if (arg == p || isdigit(*arg)
117 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
118 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
119 {
120 if (!skip)
121 EMSG2(_("E125: Illegal argument: %s"), arg);
122 break;
123 }
124 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
Bram Moolenaar19df5cc2016-07-20 22:11:06 +0200125 goto err_ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200126 if (newargs != NULL)
127 {
128 c = *p;
129 *p = NUL;
130 arg = vim_strsave(arg);
131 if (arg == NULL)
Bram Moolenaar19df5cc2016-07-20 22:11:06 +0200132 {
133 *p = c;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200134 goto err_ret;
Bram Moolenaar19df5cc2016-07-20 22:11:06 +0200135 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200136
137 /* Check for duplicate argument name. */
138 for (i = 0; i < newargs->ga_len; ++i)
139 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg) == 0)
140 {
141 EMSG2(_("E853: Duplicate argument name: %s"), arg);
142 vim_free(arg);
143 goto err_ret;
144 }
145 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg;
146 newargs->ga_len++;
147
148 *p = c;
149 }
150 if (*p == ',')
151 ++p;
152 else
153 mustend = TRUE;
154 }
155 p = skipwhite(p);
156 if (mustend && *p != endchar)
157 {
158 if (!skip)
159 EMSG2(_(e_invarg2), *argp);
160 break;
161 }
162 }
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200163 if (*p != endchar)
164 goto err_ret;
165 ++p; /* skip "endchar" */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200166
167 *argp = p;
168 return OK;
169
170err_ret:
171 if (newargs != NULL)
172 ga_clear_strings(newargs);
173 return FAIL;
174}
175
176/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200177 * Register function "fp" as using "current_funccal" as its scope.
178 */
179 static int
180register_closure(ufunc_T *fp)
181{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200182 if (fp->uf_scoped == current_funccal)
183 /* no change */
184 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200185 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200186 fp->uf_scoped = current_funccal;
187 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200188
Bram Moolenaar58016442016-07-31 18:30:22 +0200189 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
190 return FAIL;
191 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
192 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200193 return OK;
194}
195
196/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200197 * Parse a lambda expression and get a Funcref from "*arg".
198 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
199 */
200 int
201get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
202{
203 garray_T newargs;
204 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200205 garray_T *pnewargs;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200206 ufunc_T *fp = NULL;
207 int varargs;
208 int ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200209 char_u *start = skipwhite(*arg + 1);
210 char_u *s, *e;
211 static int lambda_no = 0;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200212 int *old_eval_lavars = eval_lavars_used;
213 int eval_lavars = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200214
215 ga_init(&newargs);
216 ga_init(&newlines);
217
218 /* First, check if this is a lambda expression. "->" must exist. */
219 ret = get_function_args(&start, '-', NULL, NULL, TRUE);
220 if (ret == FAIL || *start != '>')
221 return NOTDONE;
222
223 /* Parse the arguments again. */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200224 if (evaluate)
225 pnewargs = &newargs;
226 else
227 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200228 *arg = skipwhite(*arg + 1);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200229 ret = get_function_args(arg, '-', pnewargs, &varargs, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200230 if (ret == FAIL || **arg != '>')
231 goto errret;
232
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +0200233 /* Set up a flag for checking local variables and arguments. */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200234 if (evaluate)
235 eval_lavars_used = &eval_lavars;
236
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200237 /* Get the start and the end of the expression. */
238 *arg = skipwhite(*arg + 1);
239 s = *arg;
240 ret = skip_expr(arg);
241 if (ret == FAIL)
242 goto errret;
243 e = *arg;
244 *arg = skipwhite(*arg);
245 if (**arg != '}')
246 goto errret;
247 ++*arg;
248
249 if (evaluate)
250 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200251 int len, flags = 0;
252 char_u *p;
253 char_u name[20];
254 partial_T *pt;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200255
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200256 sprintf((char*)name, "<lambda>%d", ++lambda_no);
257
Bram Moolenaar58016442016-07-31 18:30:22 +0200258 fp = (ufunc_T *)alloc_clear((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200259 if (fp == NULL)
260 goto errret;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200261 pt = (partial_T *)alloc_clear((unsigned)sizeof(partial_T));
262 if (pt == NULL)
263 {
264 vim_free(fp);
265 goto errret;
266 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200267
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200268 ga_init2(&newlines, (int)sizeof(char_u *), 1);
269 if (ga_grow(&newlines, 1) == FAIL)
270 goto errret;
271
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200272 /* Add "return " before the expression. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200273 len = 7 + e - s + 1;
274 p = (char_u *)alloc(len);
275 if (p == NULL)
276 goto errret;
277 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
278 STRCPY(p, "return ");
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200279 vim_strncpy(p + 7, s, e - s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200280
281 fp->uf_refcount = 1;
282 STRCPY(fp->uf_name, name);
283 hash_add(&func_hashtab, UF2HIKEY(fp));
284 fp->uf_args = newargs;
285 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200286 if (current_funccal != NULL && eval_lavars)
287 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200288 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200289 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200290 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200291 }
292 else
293 fp->uf_scoped = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200294
295#ifdef FEAT_PROFILE
296 fp->uf_tml_count = NULL;
297 fp->uf_tml_total = NULL;
298 fp->uf_tml_self = NULL;
299 fp->uf_profiling = FALSE;
300 if (prof_def_func())
301 func_do_profile(fp);
302#endif
303 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200304 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200305 fp->uf_calls = 0;
306 fp->uf_script_ID = current_SID;
307
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200308 pt->pt_func = fp;
309 pt->pt_refcount = 1;
310 rettv->vval.v_partial = pt;
311 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200312 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200313
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200314 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200315 return OK;
316
317errret:
318 ga_clear_strings(&newargs);
319 ga_clear_strings(&newlines);
320 vim_free(fp);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200321 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200322 return FAIL;
323}
324
325/*
326 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
327 * name it contains, otherwise return "name".
328 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
329 * "partialp".
330 */
331 char_u *
332deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
333{
334 dictitem_T *v;
335 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200336 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200337
338 if (partialp != NULL)
339 *partialp = NULL;
340
341 cc = name[*lenp];
342 name[*lenp] = NUL;
343 v = find_var(name, NULL, no_autoload);
344 name[*lenp] = cc;
345 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
346 {
347 if (v->di_tv.vval.v_string == NULL)
348 {
349 *lenp = 0;
350 return (char_u *)""; /* just in case */
351 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200352 s = v->di_tv.vval.v_string;
353 *lenp = (int)STRLEN(s);
354 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200355 }
356
357 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
358 {
359 partial_T *pt = v->di_tv.vval.v_partial;
360
361 if (pt == NULL)
362 {
363 *lenp = 0;
364 return (char_u *)""; /* just in case */
365 }
366 if (partialp != NULL)
367 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200368 s = partial_name(pt);
369 *lenp = (int)STRLEN(s);
370 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200371 }
372
373 return name;
374}
375
376/*
377 * Give an error message with a function name. Handle <SNR> things.
378 * "ermsg" is to be passed without translation, use N_() instead of _().
379 */
380 static void
381emsg_funcname(char *ermsg, char_u *name)
382{
383 char_u *p;
384
385 if (*name == K_SPECIAL)
386 p = concat_str((char_u *)"<SNR>", name + 3);
387 else
388 p = name;
389 EMSG2(_(ermsg), p);
390 if (p != name)
391 vim_free(p);
392}
393
394/*
395 * Allocate a variable for the result of a function.
396 * Return OK or FAIL.
397 */
398 int
399get_func_tv(
400 char_u *name, /* name of the function */
401 int len, /* length of "name" */
402 typval_T *rettv,
403 char_u **arg, /* argument, pointing to the '(' */
404 linenr_T firstline, /* first line of range */
405 linenr_T lastline, /* last line of range */
406 int *doesrange, /* return: function handled range */
407 int evaluate,
408 partial_T *partial, /* for extra arguments */
409 dict_T *selfdict) /* Dictionary for "self" */
410{
411 char_u *argp;
412 int ret = OK;
413 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
414 int argcount = 0; /* number of arguments found */
415
416 /*
417 * Get the arguments.
418 */
419 argp = *arg;
420 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
421 {
422 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
423 if (*argp == ')' || *argp == ',' || *argp == NUL)
424 break;
425 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
426 {
427 ret = FAIL;
428 break;
429 }
430 ++argcount;
431 if (*argp != ',')
432 break;
433 }
434 if (*argp == ')')
435 ++argp;
436 else
437 ret = FAIL;
438
439 if (ret == OK)
440 {
441 int i = 0;
442
443 if (get_vim_var_nr(VV_TESTING))
444 {
445 /* Prepare for calling test_garbagecollect_now(), need to know
446 * what variables are used on the call stack. */
447 if (funcargs.ga_itemsize == 0)
448 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
449 for (i = 0; i < argcount; ++i)
450 if (ga_grow(&funcargs, 1) == OK)
451 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
452 &argvars[i];
453 }
454
Bram Moolenaardf48fb42016-07-22 21:50:18 +0200455 ret = call_func(name, len, rettv, argcount, argvars, NULL,
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200456 firstline, lastline, doesrange, evaluate, partial, selfdict);
457
458 funcargs.ga_len -= i;
459 }
460 else if (!aborting())
461 {
462 if (argcount == MAX_FUNC_ARGS)
463 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
464 else
465 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
466 }
467
468 while (--argcount >= 0)
469 clear_tv(&argvars[argcount]);
470
471 *arg = skipwhite(argp);
472 return ret;
473}
474
475#define FLEN_FIXED 40
476
477/*
478 * Return TRUE if "p" starts with "<SID>" or "s:".
479 * Only works if eval_fname_script() returned non-zero for "p"!
480 */
481 static int
482eval_fname_sid(char_u *p)
483{
484 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
485}
486
487/*
488 * In a script change <SID>name() and s:name() to K_SNR 123_name().
489 * Change <SNR>123_name() to K_SNR 123_name().
490 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
491 * (slow).
492 */
493 static char_u *
494fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
495{
496 int llen;
497 char_u *fname;
498 int i;
499
500 llen = eval_fname_script(name);
501 if (llen > 0)
502 {
503 fname_buf[0] = K_SPECIAL;
504 fname_buf[1] = KS_EXTRA;
505 fname_buf[2] = (int)KE_SNR;
506 i = 3;
507 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
508 {
509 if (current_SID <= 0)
510 *error = ERROR_SCRIPT;
511 else
512 {
513 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
514 i = (int)STRLEN(fname_buf);
515 }
516 }
517 if (i + STRLEN(name + llen) < FLEN_FIXED)
518 {
519 STRCPY(fname_buf + i, name + llen);
520 fname = fname_buf;
521 }
522 else
523 {
524 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
525 if (fname == NULL)
526 *error = ERROR_OTHER;
527 else
528 {
529 *tofree = fname;
530 mch_memmove(fname, fname_buf, (size_t)i);
531 STRCPY(fname + i, name + llen);
532 }
533 }
534 }
535 else
536 fname = name;
537 return fname;
538}
539
540/*
541 * Find a function by name, return pointer to it in ufuncs.
542 * Return NULL for unknown function.
543 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200544 ufunc_T *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200545find_func(char_u *name)
546{
547 hashitem_T *hi;
548
549 hi = hash_find(&func_hashtab, name);
550 if (!HASHITEM_EMPTY(hi))
551 return HI2UF(hi);
552 return NULL;
553}
554
555/*
556 * Copy the function name of "fp" to buffer "buf".
557 * "buf" must be able to hold the function name plus three bytes.
558 * Takes care of script-local function names.
559 */
560 static void
561cat_func_name(char_u *buf, ufunc_T *fp)
562{
563 if (fp->uf_name[0] == K_SPECIAL)
564 {
565 STRCPY(buf, "<SNR>");
566 STRCAT(buf, fp->uf_name + 3);
567 }
568 else
569 STRCPY(buf, fp->uf_name);
570}
571
572/*
573 * Add a number variable "name" to dict "dp" with value "nr".
574 */
575 static void
576add_nr_var(
577 dict_T *dp,
578 dictitem_T *v,
579 char *name,
580 varnumber_T nr)
581{
582 STRCPY(v->di_key, name);
583 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
584 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
585 v->di_tv.v_type = VAR_NUMBER;
586 v->di_tv.v_lock = VAR_FIXED;
587 v->di_tv.vval.v_number = nr;
588}
589
590/*
591 * Free "fc" and what it contains.
592 */
593 static void
594free_funccal(
595 funccall_T *fc,
596 int free_val) /* a: vars were allocated */
597{
598 listitem_T *li;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200599 int i;
600
601 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
602 {
603 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
604
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200605 /* When garbage collecting a funccall_T may be freed before the
606 * function that references it, clear its uf_scoped field.
607 * The function may have been redefined and point to another
608 * funccall_T, don't clear it then. */
609 if (fp != NULL && fp->uf_scoped == fc)
610 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200611 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200612 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200613
614 /* The a: variables typevals may not have been allocated, only free the
615 * allocated variables. */
616 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
617
618 /* free all l: variables */
619 vars_clear(&fc->l_vars.dv_hashtab);
620
621 /* Free the a:000 variables if they were allocated. */
622 if (free_val)
623 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
624 clear_tv(&li->li_tv);
625
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200626 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200627 vim_free(fc);
628}
629
630/*
631 * Call a user function.
632 */
633 static void
634call_user_func(
635 ufunc_T *fp, /* pointer to function */
636 int argcount, /* nr of args */
637 typval_T *argvars, /* arguments */
638 typval_T *rettv, /* return value */
639 linenr_T firstline, /* first line of range */
640 linenr_T lastline, /* last line of range */
641 dict_T *selfdict) /* Dictionary for "self" */
642{
643 char_u *save_sourcing_name;
644 linenr_T save_sourcing_lnum;
645 scid_T save_current_SID;
646 funccall_T *fc;
647 int save_did_emsg;
648 static int depth = 0;
649 dictitem_T *v;
650 int fixvar_idx = 0; /* index in fixvar[] */
651 int i;
652 int ai;
653 int islambda = FALSE;
654 char_u numbuf[NUMBUFLEN];
655 char_u *name;
656 size_t len;
657#ifdef FEAT_PROFILE
658 proftime_T wait_start;
659 proftime_T call_start;
660#endif
661
662 /* If depth of calling is getting too high, don't execute the function */
663 if (depth >= p_mfd)
664 {
665 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
666 rettv->v_type = VAR_NUMBER;
667 rettv->vval.v_number = -1;
668 return;
669 }
670 ++depth;
671
672 line_breakcheck(); /* check for CTRL-C hit */
673
674 fc = (funccall_T *)alloc(sizeof(funccall_T));
675 fc->caller = current_funccal;
676 current_funccal = fc;
677 fc->func = fp;
678 fc->rettv = rettv;
679 rettv->vval.v_number = 0;
680 fc->linenr = 0;
681 fc->returned = FALSE;
682 fc->level = ex_nesting_level;
683 /* Check if this function has a breakpoint. */
684 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
685 fc->dbg_tick = debug_tick;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200686 /* Set up fields for closure. */
687 fc->fc_refcount = 0;
688 fc->fc_copyID = 0;
689 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200690 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200691
692 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
693 islambda = TRUE;
694
695 /*
696 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
697 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
698 * each argument variable and saves a lot of time.
699 */
700 /*
701 * Init l: variables.
702 */
703 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
704 if (selfdict != NULL)
705 {
706 /* Set l:self to "selfdict". Use "name" to avoid a warning from
707 * some compiler that checks the destination size. */
708 v = &fc->fixvar[fixvar_idx++].var;
709 name = v->di_key;
710 STRCPY(name, "self");
711 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
712 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
713 v->di_tv.v_type = VAR_DICT;
714 v->di_tv.v_lock = 0;
715 v->di_tv.vval.v_dict = selfdict;
716 ++selfdict->dv_refcount;
717 }
718
719 /*
720 * Init a: variables.
721 * Set a:0 to "argcount".
722 * Set a:000 to a list with room for the "..." arguments.
723 */
724 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
725 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
726 (varnumber_T)(argcount - fp->uf_args.ga_len));
727 /* Use "name" to avoid a warning from some compiler that checks the
728 * destination size. */
729 v = &fc->fixvar[fixvar_idx++].var;
730 name = v->di_key;
731 STRCPY(name, "000");
732 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
733 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
734 v->di_tv.v_type = VAR_LIST;
735 v->di_tv.v_lock = VAR_FIXED;
736 v->di_tv.vval.v_list = &fc->l_varlist;
737 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
738 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
739 fc->l_varlist.lv_lock = VAR_FIXED;
740
741 /*
742 * Set a:firstline to "firstline" and a:lastline to "lastline".
743 * Set a:name to named arguments.
744 * Set a:N to the "..." arguments.
745 */
746 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
747 (varnumber_T)firstline);
748 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
749 (varnumber_T)lastline);
750 for (i = 0; i < argcount; ++i)
751 {
752 int addlocal = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200753
754 ai = i - fp->uf_args.ga_len;
755 if (ai < 0)
756 {
757 /* named argument a:name */
758 name = FUNCARG(fp, i);
759 if (islambda)
760 addlocal = TRUE;
761 }
762 else
763 {
764 /* "..." argument a:1, a:2, etc. */
765 sprintf((char *)numbuf, "%d", ai + 1);
766 name = numbuf;
767 }
768 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
769 {
770 v = &fc->fixvar[fixvar_idx++].var;
771 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200772 }
773 else
774 {
775 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
776 + STRLEN(name)));
777 if (v == NULL)
778 break;
779 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200780 }
781 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200782
783 /* Note: the values are copied directly to avoid alloc/free.
784 * "argvars" must have VAR_FIXED for v_lock. */
785 v->di_tv = argvars[i];
786 v->di_tv.v_lock = VAR_FIXED;
787
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200788 if (addlocal)
789 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200790 /* Named arguments should be accessed without the "a:" prefix in
791 * lambda expressions. Add to the l: dict. */
792 copy_tv(&v->di_tv, &v->di_tv);
793 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200794 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200795 else
796 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200797
798 if (ai >= 0 && ai < MAX_FUNC_ARGS)
799 {
800 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
801 fc->l_listitems[ai].li_tv = argvars[i];
802 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
803 }
804 }
805
806 /* Don't redraw while executing the function. */
807 ++RedrawingDisabled;
808 save_sourcing_name = sourcing_name;
809 save_sourcing_lnum = sourcing_lnum;
810 sourcing_lnum = 1;
811 /* need space for function name + ("function " + 3) or "[number]" */
812 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
813 + STRLEN(fp->uf_name) + 20;
814 sourcing_name = alloc((unsigned)len);
815 if (sourcing_name != NULL)
816 {
817 if (save_sourcing_name != NULL
818 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
819 sprintf((char *)sourcing_name, "%s[%d]..",
820 save_sourcing_name, (int)save_sourcing_lnum);
821 else
822 STRCPY(sourcing_name, "function ");
823 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
824
825 if (p_verbose >= 12)
826 {
827 ++no_wait_return;
828 verbose_enter_scroll();
829
830 smsg((char_u *)_("calling %s"), sourcing_name);
831 if (p_verbose >= 14)
832 {
833 char_u buf[MSG_BUF_LEN];
834 char_u numbuf2[NUMBUFLEN];
835 char_u *tofree;
836 char_u *s;
837
838 msg_puts((char_u *)"(");
839 for (i = 0; i < argcount; ++i)
840 {
841 if (i > 0)
842 msg_puts((char_u *)", ");
843 if (argvars[i].v_type == VAR_NUMBER)
844 msg_outnum((long)argvars[i].vval.v_number);
845 else
846 {
847 /* Do not want errors such as E724 here. */
848 ++emsg_off;
849 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
850 --emsg_off;
851 if (s != NULL)
852 {
853 if (vim_strsize(s) > MSG_BUF_CLEN)
854 {
855 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
856 s = buf;
857 }
858 msg_puts(s);
859 vim_free(tofree);
860 }
861 }
862 }
863 msg_puts((char_u *)")");
864 }
865 msg_puts((char_u *)"\n"); /* don't overwrite this either */
866
867 verbose_leave_scroll();
868 --no_wait_return;
869 }
870 }
871#ifdef FEAT_PROFILE
872 if (do_profiling == PROF_YES)
873 {
874 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
875 func_do_profile(fp);
876 if (fp->uf_profiling
877 || (fc->caller != NULL && fc->caller->func->uf_profiling))
878 {
879 ++fp->uf_tm_count;
880 profile_start(&call_start);
881 profile_zero(&fp->uf_tm_children);
882 }
883 script_prof_save(&wait_start);
884 }
885#endif
886
887 save_current_SID = current_SID;
888 current_SID = fp->uf_script_ID;
889 save_did_emsg = did_emsg;
890 did_emsg = FALSE;
891
892 /* call do_cmdline() to execute the lines */
893 do_cmdline(NULL, get_func_line, (void *)fc,
894 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
895
896 --RedrawingDisabled;
897
898 /* when the function was aborted because of an error, return -1 */
899 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
900 {
901 clear_tv(rettv);
902 rettv->v_type = VAR_NUMBER;
903 rettv->vval.v_number = -1;
904 }
905
906#ifdef FEAT_PROFILE
907 if (do_profiling == PROF_YES && (fp->uf_profiling
908 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
909 {
910 profile_end(&call_start);
911 profile_sub_wait(&wait_start, &call_start);
912 profile_add(&fp->uf_tm_total, &call_start);
913 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
914 if (fc->caller != NULL && fc->caller->func->uf_profiling)
915 {
916 profile_add(&fc->caller->func->uf_tm_children, &call_start);
917 profile_add(&fc->caller->func->uf_tml_children, &call_start);
918 }
919 }
920#endif
921
922 /* when being verbose, mention the return value */
923 if (p_verbose >= 12)
924 {
925 ++no_wait_return;
926 verbose_enter_scroll();
927
928 if (aborting())
929 smsg((char_u *)_("%s aborted"), sourcing_name);
930 else if (fc->rettv->v_type == VAR_NUMBER)
931 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
932 (long)fc->rettv->vval.v_number);
933 else
934 {
935 char_u buf[MSG_BUF_LEN];
936 char_u numbuf2[NUMBUFLEN];
937 char_u *tofree;
938 char_u *s;
939
940 /* The value may be very long. Skip the middle part, so that we
941 * have some idea how it starts and ends. smsg() would always
942 * truncate it at the end. Don't want errors such as E724 here. */
943 ++emsg_off;
944 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
945 --emsg_off;
946 if (s != NULL)
947 {
948 if (vim_strsize(s) > MSG_BUF_CLEN)
949 {
950 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
951 s = buf;
952 }
953 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
954 vim_free(tofree);
955 }
956 }
957 msg_puts((char_u *)"\n"); /* don't overwrite this either */
958
959 verbose_leave_scroll();
960 --no_wait_return;
961 }
962
963 vim_free(sourcing_name);
964 sourcing_name = save_sourcing_name;
965 sourcing_lnum = save_sourcing_lnum;
966 current_SID = save_current_SID;
967#ifdef FEAT_PROFILE
968 if (do_profiling == PROF_YES)
969 script_prof_restore(&wait_start);
970#endif
971
972 if (p_verbose >= 12 && sourcing_name != NULL)
973 {
974 ++no_wait_return;
975 verbose_enter_scroll();
976
977 smsg((char_u *)_("continuing in %s"), sourcing_name);
978 msg_puts((char_u *)"\n"); /* don't overwrite this either */
979
980 verbose_leave_scroll();
981 --no_wait_return;
982 }
983
984 did_emsg |= save_did_emsg;
985 current_funccal = fc->caller;
986 --depth;
987
Bram Moolenaar58016442016-07-31 18:30:22 +0200988 /* If the a:000 list and the l: and a: dicts are not referenced and there
989 * is no closure using it, we can free the funccall_T and what's in it. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200990 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
991 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200992 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT
993 && fc->fc_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200994 {
995 free_funccal(fc, FALSE);
996 }
997 else
998 {
999 hashitem_T *hi;
1000 listitem_T *li;
1001 int todo;
1002
Bram Moolenaar58016442016-07-31 18:30:22 +02001003 /* "fc" is still in use. This can happen when returning "a:000",
1004 * assigning "l:" to a global variable or defining a closure.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001005 * Link "fc" in the list for garbage collection later. */
1006 fc->caller = previous_funccal;
1007 previous_funccal = fc;
1008
1009 /* Make a copy of the a: variables, since we didn't do that above. */
1010 todo = (int)fc->l_avars.dv_hashtab.ht_used;
1011 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
1012 {
1013 if (!HASHITEM_EMPTY(hi))
1014 {
1015 --todo;
1016 v = HI2DI(hi);
1017 copy_tv(&v->di_tv, &v->di_tv);
1018 }
1019 }
1020
1021 /* Make a copy of the a:000 items, since we didn't do that above. */
1022 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
1023 copy_tv(&li->li_tv, &li->li_tv);
1024 }
1025}
1026
1027/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001028 * Unreference "fc": decrement the reference count and free it when it
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001029 * becomes zero. "fp" is detached from "fc".
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001030 * When "force" is TRUE we are exiting.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001031 */
1032 static void
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001033funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001034{
1035 funccall_T **pfc;
1036 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001037
1038 if (fc == NULL)
1039 return;
1040
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001041 if (--fc->fc_refcount <= 0 && (force || (
1042 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001043 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001044 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001045 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001046 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001047 if (fc == *pfc)
1048 {
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001049 *pfc = fc->caller;
1050 free_funccal(fc, TRUE);
1051 return;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001052 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001053 }
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001054 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001055 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001056 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001057}
1058
1059/*
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001060 * Remove the function from the function hashtable. If the function was
1061 * deleted while it still has references this was already done.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001062 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001063 */
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001064 static int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001065func_remove(ufunc_T *fp)
1066{
1067 hashitem_T *hi = hash_find(&func_hashtab, UF2HIKEY(fp));
1068
1069 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001070 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001071 hash_remove(&func_hashtab, hi);
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001072 return TRUE;
1073 }
1074 return FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001075}
1076
1077/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001078 * Free a function and remove it from the list of functions.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001079 * When "force" is TRUE we are exiting.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001080 */
1081 static void
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001082func_free(ufunc_T *fp, int force)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001083{
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001084 /* clear this function */
1085 ga_clear_strings(&(fp->uf_args));
1086 ga_clear_strings(&(fp->uf_lines));
1087#ifdef FEAT_PROFILE
1088 vim_free(fp->uf_tml_count);
1089 vim_free(fp->uf_tml_total);
1090 vim_free(fp->uf_tml_self);
1091#endif
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001092 /* only remove it when not done already, otherwise we would remove a newer
1093 * version of the function */
1094 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1095 func_remove(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001096
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001097 funccal_unref(fp->uf_scoped, fp, force);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001098
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001099 vim_free(fp);
1100}
1101
1102#if defined(EXITFREE) || defined(PROTO)
1103 void
1104free_all_functions(void)
1105{
1106 hashitem_T *hi;
1107
1108 /* Need to start all over every time, because func_free() may change the
1109 * hash table. */
1110 while (func_hashtab.ht_used > 0)
1111 for (hi = func_hashtab.ht_array; ; ++hi)
1112 if (!HASHITEM_EMPTY(hi))
1113 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001114 func_free(HI2UF(hi), TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001115 break;
1116 }
1117 hash_clear(&func_hashtab);
1118}
1119#endif
1120
1121/*
1122 * Return TRUE if "name" looks like a builtin function name: starts with a
1123 * lower case letter and doesn't contain AUTOLOAD_CHAR.
1124 * "len" is the length of "name", or -1 for NUL terminated.
1125 */
1126 static int
1127builtin_function(char_u *name, int len)
1128{
1129 char_u *p;
1130
1131 if (!ASCII_ISLOWER(name[0]))
1132 return FALSE;
1133 p = vim_strchr(name, AUTOLOAD_CHAR);
1134 return p == NULL || (len > 0 && p > name + len);
1135}
1136
1137 int
1138func_call(
1139 char_u *name,
1140 typval_T *args,
1141 partial_T *partial,
1142 dict_T *selfdict,
1143 typval_T *rettv)
1144{
1145 listitem_T *item;
1146 typval_T argv[MAX_FUNC_ARGS + 1];
1147 int argc = 0;
1148 int dummy;
1149 int r = 0;
1150
1151 for (item = args->vval.v_list->lv_first; item != NULL;
1152 item = item->li_next)
1153 {
1154 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1155 {
1156 EMSG(_("E699: Too many arguments"));
1157 break;
1158 }
1159 /* Make a copy of each argument. This is needed to be able to set
1160 * v_lock to VAR_FIXED in the copy without changing the original list.
1161 */
1162 copy_tv(&item->li_tv, &argv[argc++]);
1163 }
1164
1165 if (item == NULL)
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001166 r = call_func(name, (int)STRLEN(name), rettv, argc, argv, NULL,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001167 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
1168 &dummy, TRUE, partial, selfdict);
1169
1170 /* Free the arguments. */
1171 while (argc > 0)
1172 clear_tv(&argv[--argc]);
1173
1174 return r;
1175}
1176
1177/*
1178 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001179 *
1180 * "argv_func", when not NULL, can be used to fill in arguments only when the
1181 * invoked function uses them. It is called like this:
1182 * new_argcount = argv_func(current_argcount, argv, called_func_argcount)
1183 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001184 * Return FAIL when the function can't be called, OK otherwise.
1185 * Also returns OK when an error was encountered while executing the function.
1186 */
1187 int
1188call_func(
1189 char_u *funcname, /* name of the function */
1190 int len, /* length of "name" */
1191 typval_T *rettv, /* return value goes here */
1192 int argcount_in, /* number of "argvars" */
1193 typval_T *argvars_in, /* vars for arguments, must have "argcount"
1194 PLUS ONE elements! */
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001195 int (* argv_func)(int, typval_T *, int),
1196 /* function to fill in argvars */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001197 linenr_T firstline, /* first line of range */
1198 linenr_T lastline, /* last line of range */
1199 int *doesrange, /* return: function handled range */
1200 int evaluate,
1201 partial_T *partial, /* optional, can be NULL */
1202 dict_T *selfdict_in) /* Dictionary for "self" */
1203{
1204 int ret = FAIL;
1205 int error = ERROR_NONE;
1206 int i;
1207 ufunc_T *fp;
1208 char_u fname_buf[FLEN_FIXED + 1];
1209 char_u *tofree = NULL;
1210 char_u *fname;
1211 char_u *name;
1212 int argcount = argcount_in;
1213 typval_T *argvars = argvars_in;
1214 dict_T *selfdict = selfdict_in;
1215 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
1216 int argv_clear = 0;
1217
1218 /* Make a copy of the name, if it comes from a funcref variable it could
1219 * be changed or deleted in the called function. */
1220 name = vim_strnsave(funcname, len);
1221 if (name == NULL)
1222 return ret;
1223
1224 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1225
1226 *doesrange = FALSE;
1227
1228 if (partial != NULL)
1229 {
1230 /* When the function has a partial with a dict and there is a dict
1231 * argument, use the dict argument. That is backwards compatible.
1232 * When the dict was bound explicitly use the one from the partial. */
1233 if (partial->pt_dict != NULL
1234 && (selfdict_in == NULL || !partial->pt_auto))
1235 selfdict = partial->pt_dict;
1236 if (error == ERROR_NONE && partial->pt_argc > 0)
1237 {
1238 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
1239 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
1240 for (i = 0; i < argcount_in; ++i)
1241 argv[i + argv_clear] = argvars_in[i];
1242 argvars = argv;
1243 argcount = partial->pt_argc + argcount_in;
1244 }
1245 }
1246
1247
1248 /* execute the function if no errors detected and executing */
1249 if (evaluate && error == ERROR_NONE)
1250 {
1251 char_u *rfname = fname;
1252
1253 /* Ignore "g:" before a function name. */
1254 if (fname[0] == 'g' && fname[1] == ':')
1255 rfname = fname + 2;
1256
1257 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
1258 rettv->vval.v_number = 0;
1259 error = ERROR_UNKNOWN;
1260
1261 if (!builtin_function(rfname, -1))
1262 {
1263 /*
1264 * User defined function.
1265 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001266 if (partial != NULL && partial->pt_func != NULL)
1267 fp = partial->pt_func;
1268 else
1269 fp = find_func(rfname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001270
1271#ifdef FEAT_AUTOCMD
1272 /* Trigger FuncUndefined event, may load the function. */
1273 if (fp == NULL
1274 && apply_autocmds(EVENT_FUNCUNDEFINED,
1275 rfname, rfname, TRUE, NULL)
1276 && !aborting())
1277 {
1278 /* executed an autocommand, search for the function again */
1279 fp = find_func(rfname);
1280 }
1281#endif
1282 /* Try loading a package. */
1283 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
1284 {
1285 /* loaded a package, search for the function again */
1286 fp = find_func(rfname);
1287 }
1288
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001289 if (fp != NULL && (fp->uf_flags & FC_DELETED))
1290 error = ERROR_DELETED;
1291 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001292 {
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001293 if (argv_func != NULL)
1294 argcount = argv_func(argcount, argvars, fp->uf_args.ga_len);
1295
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001296 if (fp->uf_flags & FC_RANGE)
1297 *doesrange = TRUE;
1298 if (argcount < fp->uf_args.ga_len)
1299 error = ERROR_TOOFEW;
1300 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
1301 error = ERROR_TOOMANY;
1302 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1303 error = ERROR_DICT;
1304 else
1305 {
1306 int did_save_redo = FALSE;
1307
1308 /*
1309 * Call the user function.
1310 * Save and restore search patterns, script variables and
1311 * redo buffer.
1312 */
1313 save_search_patterns();
1314#ifdef FEAT_INS_EXPAND
1315 if (!ins_compl_active())
1316#endif
1317 {
1318 saveRedobuff();
1319 did_save_redo = TRUE;
1320 }
1321 ++fp->uf_calls;
1322 call_user_func(fp, argcount, argvars, rettv,
1323 firstline, lastline,
1324 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001325 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001326 /* Function was unreferenced while being used, free it
1327 * now. */
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001328 func_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001329 if (did_save_redo)
1330 restoreRedobuff();
1331 restore_search_patterns();
1332 error = ERROR_NONE;
1333 }
1334 }
1335 }
1336 else
1337 {
1338 /*
1339 * Find the function name in the table, call its implementation.
1340 */
1341 error = call_internal_func(fname, argcount, argvars, rettv);
1342 }
1343 /*
1344 * The function call (or "FuncUndefined" autocommand sequence) might
1345 * have been aborted by an error, an interrupt, or an explicitly thrown
1346 * exception that has not been caught so far. This situation can be
1347 * tested for by calling aborting(). For an error in an internal
1348 * function or for the "E132" error in call_user_func(), however, the
1349 * throw point at which the "force_abort" flag (temporarily reset by
1350 * emsg()) is normally updated has not been reached yet. We need to
1351 * update that flag first to make aborting() reliable.
1352 */
1353 update_force_abort();
1354 }
1355 if (error == ERROR_NONE)
1356 ret = OK;
1357
1358 /*
1359 * Report an error unless the argument evaluation or function call has been
1360 * cancelled due to an aborting error, an interrupt, or an exception.
1361 */
1362 if (!aborting())
1363 {
1364 switch (error)
1365 {
1366 case ERROR_UNKNOWN:
1367 emsg_funcname(N_("E117: Unknown function: %s"), name);
1368 break;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001369 case ERROR_DELETED:
1370 emsg_funcname(N_("E933: Function was deleted: %s"), name);
1371 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001372 case ERROR_TOOMANY:
1373 emsg_funcname((char *)e_toomanyarg, name);
1374 break;
1375 case ERROR_TOOFEW:
1376 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
1377 name);
1378 break;
1379 case ERROR_SCRIPT:
1380 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
1381 name);
1382 break;
1383 case ERROR_DICT:
1384 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
1385 name);
1386 break;
1387 }
1388 }
1389
1390 while (argv_clear > 0)
1391 clear_tv(&argv[--argv_clear]);
1392 vim_free(tofree);
1393 vim_free(name);
1394
1395 return ret;
1396}
1397
1398/*
1399 * List the head of the function: "name(arg1, arg2)".
1400 */
1401 static void
1402list_func_head(ufunc_T *fp, int indent)
1403{
1404 int j;
1405
1406 msg_start();
1407 if (indent)
1408 MSG_PUTS(" ");
1409 MSG_PUTS("function ");
1410 if (fp->uf_name[0] == K_SPECIAL)
1411 {
1412 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
1413 msg_puts(fp->uf_name + 3);
1414 }
1415 else
1416 msg_puts(fp->uf_name);
1417 msg_putchar('(');
1418 for (j = 0; j < fp->uf_args.ga_len; ++j)
1419 {
1420 if (j)
1421 MSG_PUTS(", ");
1422 msg_puts(FUNCARG(fp, j));
1423 }
1424 if (fp->uf_varargs)
1425 {
1426 if (j)
1427 MSG_PUTS(", ");
1428 MSG_PUTS("...");
1429 }
1430 msg_putchar(')');
1431 if (fp->uf_flags & FC_ABORT)
1432 MSG_PUTS(" abort");
1433 if (fp->uf_flags & FC_RANGE)
1434 MSG_PUTS(" range");
1435 if (fp->uf_flags & FC_DICT)
1436 MSG_PUTS(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001437 if (fp->uf_flags & FC_CLOSURE)
1438 MSG_PUTS(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001439 msg_clr_eos();
1440 if (p_verbose > 0)
1441 last_set_msg(fp->uf_script_ID);
1442}
1443
1444/*
1445 * Get a function name, translating "<SID>" and "<SNR>".
1446 * Also handles a Funcref in a List or Dictionary.
1447 * Returns the function name in allocated memory, or NULL for failure.
1448 * flags:
1449 * TFN_INT: internal function name OK
1450 * TFN_QUIET: be quiet
1451 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001452 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001453 * Advances "pp" to just after the function name (if no error).
1454 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001455 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001456trans_function_name(
1457 char_u **pp,
1458 int skip, /* only find the end, don't evaluate */
1459 int flags,
1460 funcdict_T *fdp, /* return: info about dictionary used */
1461 partial_T **partial) /* return: partial of a FuncRef */
1462{
1463 char_u *name = NULL;
1464 char_u *start;
1465 char_u *end;
1466 int lead;
1467 char_u sid_buf[20];
1468 int len;
1469 lval_T lv;
1470
1471 if (fdp != NULL)
1472 vim_memset(fdp, 0, sizeof(funcdict_T));
1473 start = *pp;
1474
1475 /* Check for hard coded <SNR>: already translated function ID (from a user
1476 * command). */
1477 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
1478 && (*pp)[2] == (int)KE_SNR)
1479 {
1480 *pp += 3;
1481 len = get_id_len(pp) + 3;
1482 return vim_strnsave(start, len);
1483 }
1484
1485 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
1486 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
1487 lead = eval_fname_script(start);
1488 if (lead > 2)
1489 start += lead;
1490
1491 /* Note that TFN_ flags use the same values as GLV_ flags. */
1492 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
1493 lead > 2 ? 0 : FNE_CHECK_START);
1494 if (end == start)
1495 {
1496 if (!skip)
1497 EMSG(_("E129: Function name required"));
1498 goto theend;
1499 }
1500 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
1501 {
1502 /*
1503 * Report an invalid expression in braces, unless the expression
1504 * evaluation has been cancelled due to an aborting error, an
1505 * interrupt, or an exception.
1506 */
1507 if (!aborting())
1508 {
1509 if (end != NULL)
1510 EMSG2(_(e_invarg2), start);
1511 }
1512 else
1513 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
1514 goto theend;
1515 }
1516
1517 if (lv.ll_tv != NULL)
1518 {
1519 if (fdp != NULL)
1520 {
1521 fdp->fd_dict = lv.ll_dict;
1522 fdp->fd_newkey = lv.ll_newkey;
1523 lv.ll_newkey = NULL;
1524 fdp->fd_di = lv.ll_di;
1525 }
1526 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
1527 {
1528 name = vim_strsave(lv.ll_tv->vval.v_string);
1529 *pp = end;
1530 }
1531 else if (lv.ll_tv->v_type == VAR_PARTIAL
1532 && lv.ll_tv->vval.v_partial != NULL)
1533 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001534 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001535 *pp = end;
1536 if (partial != NULL)
1537 *partial = lv.ll_tv->vval.v_partial;
1538 }
1539 else
1540 {
1541 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
1542 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
1543 EMSG(_(e_funcref));
1544 else
1545 *pp = end;
1546 name = NULL;
1547 }
1548 goto theend;
1549 }
1550
1551 if (lv.ll_name == NULL)
1552 {
1553 /* Error found, but continue after the function name. */
1554 *pp = end;
1555 goto theend;
1556 }
1557
1558 /* Check if the name is a Funcref. If so, use the value. */
1559 if (lv.ll_exp_name != NULL)
1560 {
1561 len = (int)STRLEN(lv.ll_exp_name);
1562 name = deref_func_name(lv.ll_exp_name, &len, partial,
1563 flags & TFN_NO_AUTOLOAD);
1564 if (name == lv.ll_exp_name)
1565 name = NULL;
1566 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001567 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001568 {
1569 len = (int)(end - *pp);
1570 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
1571 if (name == *pp)
1572 name = NULL;
1573 }
1574 if (name != NULL)
1575 {
1576 name = vim_strsave(name);
1577 *pp = end;
1578 if (STRNCMP(name, "<SNR>", 5) == 0)
1579 {
1580 /* Change "<SNR>" to the byte sequence. */
1581 name[0] = K_SPECIAL;
1582 name[1] = KS_EXTRA;
1583 name[2] = (int)KE_SNR;
1584 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
1585 }
1586 goto theend;
1587 }
1588
1589 if (lv.ll_exp_name != NULL)
1590 {
1591 len = (int)STRLEN(lv.ll_exp_name);
1592 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
1593 && STRNCMP(lv.ll_name, "s:", 2) == 0)
1594 {
1595 /* When there was "s:" already or the name expanded to get a
1596 * leading "s:" then remove it. */
1597 lv.ll_name += 2;
1598 len -= 2;
1599 lead = 2;
1600 }
1601 }
1602 else
1603 {
1604 /* skip over "s:" and "g:" */
1605 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
1606 lv.ll_name += 2;
1607 len = (int)(end - lv.ll_name);
1608 }
1609
1610 /*
1611 * Copy the function name to allocated memory.
1612 * Accept <SID>name() inside a script, translate into <SNR>123_name().
1613 * Accept <SNR>123_name() outside a script.
1614 */
1615 if (skip)
1616 lead = 0; /* do nothing */
1617 else if (lead > 0)
1618 {
1619 lead = 3;
1620 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
1621 || eval_fname_sid(*pp))
1622 {
1623 /* It's "s:" or "<SID>" */
1624 if (current_SID <= 0)
1625 {
1626 EMSG(_(e_usingsid));
1627 goto theend;
1628 }
1629 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
1630 lead += (int)STRLEN(sid_buf);
1631 }
1632 }
1633 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
1634 {
1635 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
1636 start);
1637 goto theend;
1638 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001639 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001640 {
1641 char_u *cp = vim_strchr(lv.ll_name, ':');
1642
1643 if (cp != NULL && cp < end)
1644 {
1645 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
1646 goto theend;
1647 }
1648 }
1649
1650 name = alloc((unsigned)(len + lead + 1));
1651 if (name != NULL)
1652 {
1653 if (lead > 0)
1654 {
1655 name[0] = K_SPECIAL;
1656 name[1] = KS_EXTRA;
1657 name[2] = (int)KE_SNR;
1658 if (lead > 3) /* If it's "<SID>" */
1659 STRCPY(name + 3, sid_buf);
1660 }
1661 mch_memmove(name + lead, lv.ll_name, (size_t)len);
1662 name[lead + len] = NUL;
1663 }
1664 *pp = end;
1665
1666theend:
1667 clear_lval(&lv);
1668 return name;
1669}
1670
1671/*
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001672 * There are two kinds of function names:
1673 * 1. ordinary names, function defined with :function
1674 * 2. numbered functions and lambdas
1675 * For the first we only count the name stored in func_hashtab as a reference,
1676 * using function() does not count as a reference, because the function is
1677 * looked up by name.
1678 */
1679 static int
1680func_name_refcount(char_u *name)
1681{
1682 return isdigit(*name) || *name == '<';
1683}
1684
1685/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001686 * ":function"
1687 */
1688 void
1689ex_function(exarg_T *eap)
1690{
1691 char_u *theline;
1692 int j;
1693 int c;
1694 int saved_did_emsg;
1695 int saved_wait_return = need_wait_return;
1696 char_u *name = NULL;
1697 char_u *p;
1698 char_u *arg;
1699 char_u *line_arg = NULL;
1700 garray_T newargs;
1701 garray_T newlines;
1702 int varargs = FALSE;
1703 int flags = 0;
1704 ufunc_T *fp;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001705 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001706 int indent;
1707 int nesting;
1708 char_u *skip_until = NULL;
1709 dictitem_T *v;
1710 funcdict_T fudi;
1711 static int func_nr = 0; /* number for nameless function */
1712 int paren;
1713 hashtab_T *ht;
1714 int todo;
1715 hashitem_T *hi;
1716 int sourcing_lnum_off;
1717
1718 /*
1719 * ":function" without argument: list functions.
1720 */
1721 if (ends_excmd(*eap->arg))
1722 {
1723 if (!eap->skip)
1724 {
1725 todo = (int)func_hashtab.ht_used;
1726 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
1727 {
1728 if (!HASHITEM_EMPTY(hi))
1729 {
1730 --todo;
1731 fp = HI2UF(hi);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001732 if (!func_name_refcount(fp->uf_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001733 list_func_head(fp, FALSE);
1734 }
1735 }
1736 }
1737 eap->nextcmd = check_nextcmd(eap->arg);
1738 return;
1739 }
1740
1741 /*
1742 * ":function /pat": list functions matching pattern.
1743 */
1744 if (*eap->arg == '/')
1745 {
1746 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
1747 if (!eap->skip)
1748 {
1749 regmatch_T regmatch;
1750
1751 c = *p;
1752 *p = NUL;
1753 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
1754 *p = c;
1755 if (regmatch.regprog != NULL)
1756 {
1757 regmatch.rm_ic = p_ic;
1758
1759 todo = (int)func_hashtab.ht_used;
1760 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
1761 {
1762 if (!HASHITEM_EMPTY(hi))
1763 {
1764 --todo;
1765 fp = HI2UF(hi);
1766 if (!isdigit(*fp->uf_name)
1767 && vim_regexec(&regmatch, fp->uf_name, 0))
1768 list_func_head(fp, FALSE);
1769 }
1770 }
1771 vim_regfree(regmatch.regprog);
1772 }
1773 }
1774 if (*p == '/')
1775 ++p;
1776 eap->nextcmd = check_nextcmd(p);
1777 return;
1778 }
1779
1780 /*
1781 * Get the function name. There are these situations:
1782 * func normal function name
1783 * "name" == func, "fudi.fd_dict" == NULL
1784 * dict.func new dictionary entry
1785 * "name" == NULL, "fudi.fd_dict" set,
1786 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
1787 * dict.func existing dict entry with a Funcref
1788 * "name" == func, "fudi.fd_dict" set,
1789 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
1790 * dict.func existing dict entry that's not a Funcref
1791 * "name" == NULL, "fudi.fd_dict" set,
1792 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
1793 * s:func script-local function name
1794 * g:func global function name, same as "func"
1795 */
1796 p = eap->arg;
1797 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
1798 paren = (vim_strchr(p, '(') != NULL);
1799 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
1800 {
1801 /*
1802 * Return on an invalid expression in braces, unless the expression
1803 * evaluation has been cancelled due to an aborting error, an
1804 * interrupt, or an exception.
1805 */
1806 if (!aborting())
1807 {
1808 if (!eap->skip && fudi.fd_newkey != NULL)
1809 EMSG2(_(e_dictkey), fudi.fd_newkey);
1810 vim_free(fudi.fd_newkey);
1811 return;
1812 }
1813 else
1814 eap->skip = TRUE;
1815 }
1816
1817 /* An error in a function call during evaluation of an expression in magic
1818 * braces should not cause the function not to be defined. */
1819 saved_did_emsg = did_emsg;
1820 did_emsg = FALSE;
1821
1822 /*
1823 * ":function func" with only function name: list function.
1824 */
1825 if (!paren)
1826 {
1827 if (!ends_excmd(*skipwhite(p)))
1828 {
1829 EMSG(_(e_trailing));
1830 goto ret_free;
1831 }
1832 eap->nextcmd = check_nextcmd(p);
1833 if (eap->nextcmd != NULL)
1834 *p = NUL;
1835 if (!eap->skip && !got_int)
1836 {
1837 fp = find_func(name);
1838 if (fp != NULL)
1839 {
1840 list_func_head(fp, TRUE);
1841 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
1842 {
1843 if (FUNCLINE(fp, j) == NULL)
1844 continue;
1845 msg_putchar('\n');
1846 msg_outnum((long)(j + 1));
1847 if (j < 9)
1848 msg_putchar(' ');
1849 if (j < 99)
1850 msg_putchar(' ');
1851 msg_prt_line(FUNCLINE(fp, j), FALSE);
1852 out_flush(); /* show a line at a time */
1853 ui_breakcheck();
1854 }
1855 if (!got_int)
1856 {
1857 msg_putchar('\n');
1858 msg_puts((char_u *)" endfunction");
1859 }
1860 }
1861 else
1862 emsg_funcname(N_("E123: Undefined function: %s"), name);
1863 }
1864 goto ret_free;
1865 }
1866
1867 /*
1868 * ":function name(arg1, arg2)" Define function.
1869 */
1870 p = skipwhite(p);
1871 if (*p != '(')
1872 {
1873 if (!eap->skip)
1874 {
1875 EMSG2(_("E124: Missing '(': %s"), eap->arg);
1876 goto ret_free;
1877 }
1878 /* attempt to continue by skipping some text */
1879 if (vim_strchr(p, '(') != NULL)
1880 p = vim_strchr(p, '(');
1881 }
1882 p = skipwhite(p + 1);
1883
1884 ga_init2(&newlines, (int)sizeof(char_u *), 3);
1885
1886 if (!eap->skip)
1887 {
1888 /* Check the name of the function. Unless it's a dictionary function
1889 * (that we are overwriting). */
1890 if (name != NULL)
1891 arg = name;
1892 else
1893 arg = fudi.fd_newkey;
1894 if (arg != NULL && (fudi.fd_di == NULL
1895 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
1896 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
1897 {
1898 if (*arg == K_SPECIAL)
1899 j = 3;
1900 else
1901 j = 0;
1902 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
1903 : eval_isnamec(arg[j])))
1904 ++j;
1905 if (arg[j] != NUL)
1906 emsg_funcname((char *)e_invarg2, arg);
1907 }
1908 /* Disallow using the g: dict. */
1909 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
1910 EMSG(_("E862: Cannot use g: here"));
1911 }
1912
1913 if (get_function_args(&p, ')', &newargs, &varargs, eap->skip) == FAIL)
1914 goto errret_2;
1915
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001916 /* find extra arguments "range", "dict", "abort" and "closure" */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001917 for (;;)
1918 {
1919 p = skipwhite(p);
1920 if (STRNCMP(p, "range", 5) == 0)
1921 {
1922 flags |= FC_RANGE;
1923 p += 5;
1924 }
1925 else if (STRNCMP(p, "dict", 4) == 0)
1926 {
1927 flags |= FC_DICT;
1928 p += 4;
1929 }
1930 else if (STRNCMP(p, "abort", 5) == 0)
1931 {
1932 flags |= FC_ABORT;
1933 p += 5;
1934 }
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001935 else if (STRNCMP(p, "closure", 7) == 0)
1936 {
1937 flags |= FC_CLOSURE;
1938 p += 7;
Bram Moolenaar58016442016-07-31 18:30:22 +02001939 if (current_funccal == NULL)
1940 {
1941 emsg_funcname(N_("E932 Closure function should not be at top level: %s"),
1942 name == NULL ? (char_u *)"" : name);
1943 goto erret;
1944 }
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001945 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001946 else
1947 break;
1948 }
1949
1950 /* When there is a line break use what follows for the function body.
1951 * Makes 'exe "func Test()\n...\nendfunc"' work. */
1952 if (*p == '\n')
1953 line_arg = p + 1;
1954 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
1955 EMSG(_(e_trailing));
1956
1957 /*
1958 * Read the body of the function, until ":endfunction" is found.
1959 */
1960 if (KeyTyped)
1961 {
1962 /* Check if the function already exists, don't let the user type the
1963 * whole function before telling him it doesn't work! For a script we
1964 * need to skip the body to be able to find what follows. */
1965 if (!eap->skip && !eap->forceit)
1966 {
1967 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
1968 EMSG(_(e_funcdict));
1969 else if (name != NULL && find_func(name) != NULL)
1970 emsg_funcname(e_funcexts, name);
1971 }
1972
1973 if (!eap->skip && did_emsg)
1974 goto erret;
1975
1976 msg_putchar('\n'); /* don't overwrite the function name */
1977 cmdline_row = msg_row;
1978 }
1979
1980 indent = 2;
1981 nesting = 0;
1982 for (;;)
1983 {
1984 if (KeyTyped)
1985 {
1986 msg_scroll = TRUE;
1987 saved_wait_return = FALSE;
1988 }
1989 need_wait_return = FALSE;
1990 sourcing_lnum_off = sourcing_lnum;
1991
1992 if (line_arg != NULL)
1993 {
1994 /* Use eap->arg, split up in parts by line breaks. */
1995 theline = line_arg;
1996 p = vim_strchr(theline, '\n');
1997 if (p == NULL)
1998 line_arg += STRLEN(line_arg);
1999 else
2000 {
2001 *p = NUL;
2002 line_arg = p + 1;
2003 }
2004 }
2005 else if (eap->getline == NULL)
2006 theline = getcmdline(':', 0L, indent);
2007 else
2008 theline = eap->getline(':', eap->cookie, indent);
2009 if (KeyTyped)
2010 lines_left = Rows - 1;
2011 if (theline == NULL)
2012 {
2013 EMSG(_("E126: Missing :endfunction"));
2014 goto erret;
2015 }
2016
2017 /* Detect line continuation: sourcing_lnum increased more than one. */
2018 if (sourcing_lnum > sourcing_lnum_off + 1)
2019 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
2020 else
2021 sourcing_lnum_off = 0;
2022
2023 if (skip_until != NULL)
2024 {
2025 /* between ":append" and "." and between ":python <<EOF" and "EOF"
2026 * don't check for ":endfunc". */
2027 if (STRCMP(theline, skip_until) == 0)
2028 {
2029 vim_free(skip_until);
2030 skip_until = NULL;
2031 }
2032 }
2033 else
2034 {
2035 /* skip ':' and blanks*/
2036 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
2037 ;
2038
2039 /* Check for "endfunction". */
2040 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
2041 {
2042 if (line_arg == NULL)
2043 vim_free(theline);
2044 break;
2045 }
2046
2047 /* Increase indent inside "if", "while", "for" and "try", decrease
2048 * at "end". */
2049 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
2050 indent -= 2;
2051 else if (STRNCMP(p, "if", 2) == 0
2052 || STRNCMP(p, "wh", 2) == 0
2053 || STRNCMP(p, "for", 3) == 0
2054 || STRNCMP(p, "try", 3) == 0)
2055 indent += 2;
2056
2057 /* Check for defining a function inside this function. */
2058 if (checkforcmd(&p, "function", 2))
2059 {
2060 if (*p == '!')
2061 p = skipwhite(p + 1);
2062 p += eval_fname_script(p);
2063 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
2064 if (*skipwhite(p) == '(')
2065 {
2066 ++nesting;
2067 indent += 2;
2068 }
2069 }
2070
2071 /* Check for ":append" or ":insert". */
2072 p = skip_range(p, NULL);
2073 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
2074 || (p[0] == 'i'
2075 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
2076 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
2077 skip_until = vim_strsave((char_u *)".");
2078
2079 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
2080 arg = skipwhite(skiptowhite(p));
2081 if (arg[0] == '<' && arg[1] =='<'
2082 && ((p[0] == 'p' && p[1] == 'y'
2083 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
2084 || (p[0] == 'p' && p[1] == 'e'
2085 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
2086 || (p[0] == 't' && p[1] == 'c'
2087 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
2088 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
2089 && !ASCII_ISALPHA(p[3]))
2090 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
2091 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
2092 || (p[0] == 'm' && p[1] == 'z'
2093 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
2094 ))
2095 {
2096 /* ":python <<" continues until a dot, like ":append" */
2097 p = skipwhite(arg + 2);
2098 if (*p == NUL)
2099 skip_until = vim_strsave((char_u *)".");
2100 else
2101 skip_until = vim_strsave(p);
2102 }
2103 }
2104
2105 /* Add the line to the function. */
2106 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
2107 {
2108 if (line_arg == NULL)
2109 vim_free(theline);
2110 goto erret;
2111 }
2112
2113 /* Copy the line to newly allocated memory. get_one_sourceline()
2114 * allocates 250 bytes per line, this saves 80% on average. The cost
2115 * is an extra alloc/free. */
2116 p = vim_strsave(theline);
2117 if (p != NULL)
2118 {
2119 if (line_arg == NULL)
2120 vim_free(theline);
2121 theline = p;
2122 }
2123
2124 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
2125
2126 /* Add NULL lines for continuation lines, so that the line count is
2127 * equal to the index in the growarray. */
2128 while (sourcing_lnum_off-- > 0)
2129 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
2130
2131 /* Check for end of eap->arg. */
2132 if (line_arg != NULL && *line_arg == NUL)
2133 line_arg = NULL;
2134 }
2135
2136 /* Don't define the function when skipping commands or when an error was
2137 * detected. */
2138 if (eap->skip || did_emsg)
2139 goto erret;
2140
2141 /*
2142 * If there are no errors, add the function
2143 */
2144 if (fudi.fd_dict == NULL)
2145 {
2146 v = find_var(name, &ht, FALSE);
2147 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
2148 {
2149 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
2150 name);
2151 goto erret;
2152 }
2153
2154 fp = find_func(name);
2155 if (fp != NULL)
2156 {
2157 if (!eap->forceit)
2158 {
2159 emsg_funcname(e_funcexts, name);
2160 goto erret;
2161 }
2162 if (fp->uf_calls > 0)
2163 {
2164 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
2165 name);
2166 goto erret;
2167 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002168 if (fp->uf_refcount > 1)
2169 {
2170 /* This function is referenced somewhere, don't redefine it but
2171 * create a new one. */
2172 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002173 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002174 fp = NULL;
2175 overwrite = TRUE;
2176 }
2177 else
2178 {
2179 /* redefine existing function */
2180 ga_clear_strings(&(fp->uf_args));
2181 ga_clear_strings(&(fp->uf_lines));
2182 vim_free(name);
2183 name = NULL;
2184 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002185 }
2186 }
2187 else
2188 {
2189 char numbuf[20];
2190
2191 fp = NULL;
2192 if (fudi.fd_newkey == NULL && !eap->forceit)
2193 {
2194 EMSG(_(e_funcdict));
2195 goto erret;
2196 }
2197 if (fudi.fd_di == NULL)
2198 {
2199 /* Can't add a function to a locked dictionary */
2200 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
2201 goto erret;
2202 }
2203 /* Can't change an existing function if it is locked */
2204 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
2205 goto erret;
2206
2207 /* Give the function a sequential number. Can only be used with a
2208 * Funcref! */
2209 vim_free(name);
2210 sprintf(numbuf, "%d", ++func_nr);
2211 name = vim_strsave((char_u *)numbuf);
2212 if (name == NULL)
2213 goto erret;
2214 }
2215
2216 if (fp == NULL)
2217 {
2218 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
2219 {
2220 int slen, plen;
2221 char_u *scriptname;
2222
2223 /* Check that the autoload name matches the script name. */
2224 j = FAIL;
2225 if (sourcing_name != NULL)
2226 {
2227 scriptname = autoload_name(name);
2228 if (scriptname != NULL)
2229 {
2230 p = vim_strchr(scriptname, '/');
2231 plen = (int)STRLEN(p);
2232 slen = (int)STRLEN(sourcing_name);
2233 if (slen > plen && fnamecmp(p,
2234 sourcing_name + slen - plen) == 0)
2235 j = OK;
2236 vim_free(scriptname);
2237 }
2238 }
2239 if (j == FAIL)
2240 {
2241 EMSG2(_("E746: Function name does not match script file name: %s"), name);
2242 goto erret;
2243 }
2244 }
2245
Bram Moolenaar58016442016-07-31 18:30:22 +02002246 fp = (ufunc_T *)alloc_clear((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002247 if (fp == NULL)
2248 goto erret;
2249
2250 if (fudi.fd_dict != NULL)
2251 {
2252 if (fudi.fd_di == NULL)
2253 {
2254 /* add new dict entry */
2255 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
2256 if (fudi.fd_di == NULL)
2257 {
2258 vim_free(fp);
2259 goto erret;
2260 }
2261 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
2262 {
2263 vim_free(fudi.fd_di);
2264 vim_free(fp);
2265 goto erret;
2266 }
2267 }
2268 else
2269 /* overwrite existing dict entry */
2270 clear_tv(&fudi.fd_di->di_tv);
2271 fudi.fd_di->di_tv.v_type = VAR_FUNC;
2272 fudi.fd_di->di_tv.v_lock = 0;
2273 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002274
2275 /* behave like "dict" was used */
2276 flags |= FC_DICT;
2277 }
2278
2279 /* insert the new function in the function list */
2280 STRCPY(fp->uf_name, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002281 if (overwrite)
2282 {
2283 hi = hash_find(&func_hashtab, name);
2284 hi->hi_key = UF2HIKEY(fp);
2285 }
2286 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002287 {
2288 vim_free(fp);
2289 goto erret;
2290 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002291 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002292 }
2293 fp->uf_args = newargs;
2294 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002295 if ((flags & FC_CLOSURE) != 0)
2296 {
Bram Moolenaar58016442016-07-31 18:30:22 +02002297 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002298 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002299 }
2300 else
2301 fp->uf_scoped = NULL;
2302
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002303#ifdef FEAT_PROFILE
2304 fp->uf_tml_count = NULL;
2305 fp->uf_tml_total = NULL;
2306 fp->uf_tml_self = NULL;
2307 fp->uf_profiling = FALSE;
2308 if (prof_def_func())
2309 func_do_profile(fp);
2310#endif
2311 fp->uf_varargs = varargs;
2312 fp->uf_flags = flags;
2313 fp->uf_calls = 0;
2314 fp->uf_script_ID = current_SID;
2315 goto ret_free;
2316
2317erret:
2318 ga_clear_strings(&newargs);
2319errret_2:
2320 ga_clear_strings(&newlines);
2321ret_free:
2322 vim_free(skip_until);
2323 vim_free(fudi.fd_newkey);
2324 vim_free(name);
2325 did_emsg |= saved_did_emsg;
2326 need_wait_return |= saved_wait_return;
2327}
2328
2329/*
2330 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
2331 * Return 2 if "p" starts with "s:".
2332 * Return 0 otherwise.
2333 */
2334 int
2335eval_fname_script(char_u *p)
2336{
2337 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
2338 * the standard library function. */
2339 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
2340 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
2341 return 5;
2342 if (p[0] == 's' && p[1] == ':')
2343 return 2;
2344 return 0;
2345}
2346
2347 int
2348translated_function_exists(char_u *name)
2349{
2350 if (builtin_function(name, -1))
2351 return find_internal_func(name) >= 0;
2352 return find_func(name) != NULL;
2353}
2354
2355/*
2356 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002357 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002358 */
2359 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002360function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002361{
2362 char_u *nm = name;
2363 char_u *p;
2364 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002365 int flag;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002366
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002367 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
2368 if (no_deref)
2369 flag |= TFN_NO_DEREF;
2370 p = trans_function_name(&nm, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002371 nm = skipwhite(nm);
2372
2373 /* Only accept "funcname", "funcname ", "funcname (..." and
2374 * "funcname(...", not "funcname!...". */
2375 if (p != NULL && (*nm == NUL || *nm == '('))
2376 n = translated_function_exists(p);
2377 vim_free(p);
2378 return n;
2379}
2380
2381 char_u *
2382get_expanded_name(char_u *name, int check)
2383{
2384 char_u *nm = name;
2385 char_u *p;
2386
2387 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
2388
2389 if (p != NULL && *nm == NUL)
2390 if (!check || translated_function_exists(p))
2391 return p;
2392
2393 vim_free(p);
2394 return NULL;
2395}
2396
2397#if defined(FEAT_PROFILE) || defined(PROTO)
2398/*
2399 * Start profiling function "fp".
2400 */
2401 static void
2402func_do_profile(ufunc_T *fp)
2403{
2404 int len = fp->uf_lines.ga_len;
2405
2406 if (len == 0)
2407 len = 1; /* avoid getting error for allocating zero bytes */
2408 fp->uf_tm_count = 0;
2409 profile_zero(&fp->uf_tm_self);
2410 profile_zero(&fp->uf_tm_total);
2411 if (fp->uf_tml_count == NULL)
2412 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
2413 if (fp->uf_tml_total == NULL)
2414 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
2415 (sizeof(proftime_T) * len));
2416 if (fp->uf_tml_self == NULL)
2417 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
2418 (sizeof(proftime_T) * len));
2419 fp->uf_tml_idx = -1;
2420 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
2421 || fp->uf_tml_self == NULL)
2422 return; /* out of memory */
2423
2424 fp->uf_profiling = TRUE;
2425}
2426
2427/*
2428 * Dump the profiling results for all functions in file "fd".
2429 */
2430 void
2431func_dump_profile(FILE *fd)
2432{
2433 hashitem_T *hi;
2434 int todo;
2435 ufunc_T *fp;
2436 int i;
2437 ufunc_T **sorttab;
2438 int st_len = 0;
2439
2440 todo = (int)func_hashtab.ht_used;
2441 if (todo == 0)
2442 return; /* nothing to dump */
2443
2444 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
2445
2446 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2447 {
2448 if (!HASHITEM_EMPTY(hi))
2449 {
2450 --todo;
2451 fp = HI2UF(hi);
2452 if (fp->uf_profiling)
2453 {
2454 if (sorttab != NULL)
2455 sorttab[st_len++] = fp;
2456
2457 if (fp->uf_name[0] == K_SPECIAL)
2458 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
2459 else
2460 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
2461 if (fp->uf_tm_count == 1)
2462 fprintf(fd, "Called 1 time\n");
2463 else
2464 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
2465 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
2466 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
2467 fprintf(fd, "\n");
2468 fprintf(fd, "count total (s) self (s)\n");
2469
2470 for (i = 0; i < fp->uf_lines.ga_len; ++i)
2471 {
2472 if (FUNCLINE(fp, i) == NULL)
2473 continue;
2474 prof_func_line(fd, fp->uf_tml_count[i],
2475 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
2476 fprintf(fd, "%s\n", FUNCLINE(fp, i));
2477 }
2478 fprintf(fd, "\n");
2479 }
2480 }
2481 }
2482
2483 if (sorttab != NULL && st_len > 0)
2484 {
2485 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
2486 prof_total_cmp);
2487 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
2488 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
2489 prof_self_cmp);
2490 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
2491 }
2492
2493 vim_free(sorttab);
2494}
2495
2496 static void
2497prof_sort_list(
2498 FILE *fd,
2499 ufunc_T **sorttab,
2500 int st_len,
2501 char *title,
2502 int prefer_self) /* when equal print only self time */
2503{
2504 int i;
2505 ufunc_T *fp;
2506
2507 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
2508 fprintf(fd, "count total (s) self (s) function\n");
2509 for (i = 0; i < 20 && i < st_len; ++i)
2510 {
2511 fp = sorttab[i];
2512 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
2513 prefer_self);
2514 if (fp->uf_name[0] == K_SPECIAL)
2515 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
2516 else
2517 fprintf(fd, " %s()\n", fp->uf_name);
2518 }
2519 fprintf(fd, "\n");
2520}
2521
2522/*
2523 * Print the count and times for one function or function line.
2524 */
2525 static void
2526prof_func_line(
2527 FILE *fd,
2528 int count,
2529 proftime_T *total,
2530 proftime_T *self,
2531 int prefer_self) /* when equal print only self time */
2532{
2533 if (count > 0)
2534 {
2535 fprintf(fd, "%5d ", count);
2536 if (prefer_self && profile_equal(total, self))
2537 fprintf(fd, " ");
2538 else
2539 fprintf(fd, "%s ", profile_msg(total));
2540 if (!prefer_self && profile_equal(total, self))
2541 fprintf(fd, " ");
2542 else
2543 fprintf(fd, "%s ", profile_msg(self));
2544 }
2545 else
2546 fprintf(fd, " ");
2547}
2548
2549/*
2550 * Compare function for total time sorting.
2551 */
2552 static int
2553#ifdef __BORLANDC__
2554_RTLENTRYF
2555#endif
2556prof_total_cmp(const void *s1, const void *s2)
2557{
2558 ufunc_T *p1, *p2;
2559
2560 p1 = *(ufunc_T **)s1;
2561 p2 = *(ufunc_T **)s2;
2562 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
2563}
2564
2565/*
2566 * Compare function for self time sorting.
2567 */
2568 static int
2569#ifdef __BORLANDC__
2570_RTLENTRYF
2571#endif
2572prof_self_cmp(const void *s1, const void *s2)
2573{
2574 ufunc_T *p1, *p2;
2575
2576 p1 = *(ufunc_T **)s1;
2577 p2 = *(ufunc_T **)s2;
2578 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
2579}
2580
2581/*
2582 * Prepare profiling for entering a child or something else that is not
2583 * counted for the script/function itself.
2584 * Should always be called in pair with prof_child_exit().
2585 */
2586 void
2587prof_child_enter(
2588 proftime_T *tm) /* place to store waittime */
2589{
2590 funccall_T *fc = current_funccal;
2591
2592 if (fc != NULL && fc->func->uf_profiling)
2593 profile_start(&fc->prof_child);
2594 script_prof_save(tm);
2595}
2596
2597/*
2598 * Take care of time spent in a child.
2599 * Should always be called after prof_child_enter().
2600 */
2601 void
2602prof_child_exit(
2603 proftime_T *tm) /* where waittime was stored */
2604{
2605 funccall_T *fc = current_funccal;
2606
2607 if (fc != NULL && fc->func->uf_profiling)
2608 {
2609 profile_end(&fc->prof_child);
2610 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
2611 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
2612 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
2613 }
2614 script_prof_restore(tm);
2615}
2616
2617#endif /* FEAT_PROFILE */
2618
2619#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2620
2621/*
2622 * Function given to ExpandGeneric() to obtain the list of user defined
2623 * function names.
2624 */
2625 char_u *
2626get_user_func_name(expand_T *xp, int idx)
2627{
2628 static long_u done;
2629 static hashitem_T *hi;
2630 ufunc_T *fp;
2631
2632 if (idx == 0)
2633 {
2634 done = 0;
2635 hi = func_hashtab.ht_array;
2636 }
2637 if (done < func_hashtab.ht_used)
2638 {
2639 if (done++ > 0)
2640 ++hi;
2641 while (HASHITEM_EMPTY(hi))
2642 ++hi;
2643 fp = HI2UF(hi);
2644
Bram Moolenaarb49edc12016-07-23 15:47:34 +02002645 if ((fp->uf_flags & FC_DICT)
2646 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
2647 return (char_u *)""; /* don't show dict and lambda functions */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002648
2649 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
2650 return fp->uf_name; /* prevents overflow */
2651
2652 cat_func_name(IObuff, fp);
2653 if (xp->xp_context != EXPAND_USER_FUNC)
2654 {
2655 STRCAT(IObuff, "(");
2656 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
2657 STRCAT(IObuff, ")");
2658 }
2659 return IObuff;
2660 }
2661 return NULL;
2662}
2663
2664#endif /* FEAT_CMDL_COMPL */
2665
2666/*
2667 * ":delfunction {name}"
2668 */
2669 void
2670ex_delfunction(exarg_T *eap)
2671{
2672 ufunc_T *fp = NULL;
2673 char_u *p;
2674 char_u *name;
2675 funcdict_T fudi;
2676
2677 p = eap->arg;
2678 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
2679 vim_free(fudi.fd_newkey);
2680 if (name == NULL)
2681 {
2682 if (fudi.fd_dict != NULL && !eap->skip)
2683 EMSG(_(e_funcref));
2684 return;
2685 }
2686 if (!ends_excmd(*skipwhite(p)))
2687 {
2688 vim_free(name);
2689 EMSG(_(e_trailing));
2690 return;
2691 }
2692 eap->nextcmd = check_nextcmd(p);
2693 if (eap->nextcmd != NULL)
2694 *p = NUL;
2695
2696 if (!eap->skip)
2697 fp = find_func(name);
2698 vim_free(name);
2699
2700 if (!eap->skip)
2701 {
2702 if (fp == NULL)
2703 {
2704 EMSG2(_(e_nofunc), eap->arg);
2705 return;
2706 }
2707 if (fp->uf_calls > 0)
2708 {
2709 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
2710 return;
2711 }
2712
2713 if (fudi.fd_dict != NULL)
2714 {
2715 /* Delete the dict item that refers to the function, it will
2716 * invoke func_unref() and possibly delete the function. */
2717 dictitem_remove(fudi.fd_dict, fudi.fd_di);
2718 }
2719 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002720 {
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002721 /* A normal function (not a numbered function or lambda) has a
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002722 * refcount of 1 for the entry in the hashtable. When deleting
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002723 * it and the refcount is more than one, it should be kept.
2724 * A numbered function and lambda snould be kept if the refcount is
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002725 * one or more. */
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002726 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002727 {
2728 /* Function is still referenced somewhere. Don't free it but
2729 * do remove it from the hashtable. */
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002730 if (func_remove(fp))
2731 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002732 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002733 }
2734 else
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002735 func_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002736 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002737 }
2738}
2739
2740/*
2741 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002742 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002743 */
2744 void
2745func_unref(char_u *name)
2746{
Bram Moolenaar97baee82016-07-26 20:46:08 +02002747 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002748
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002749 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002750 return;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002751 fp = find_func(name);
2752 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002753 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002754#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002755 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002756#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002757 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002758 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002759 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002760 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002761 /* Only delete it when it's not being used. Otherwise it's done
2762 * when "uf_calls" becomes zero. */
2763 if (fp->uf_calls == 0)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002764 func_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02002765 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002766}
2767
2768/*
2769 * Unreference a Function: decrement the reference count and free it when it
2770 * becomes zero.
2771 */
2772 void
2773func_ptr_unref(ufunc_T *fp)
2774{
Bram Moolenaar97baee82016-07-26 20:46:08 +02002775 if (fp != NULL && --fp->uf_refcount <= 0)
2776 {
2777 /* Only delete it when it's not being used. Otherwise it's done
2778 * when "uf_calls" becomes zero. */
2779 if (fp->uf_calls == 0)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002780 func_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002781 }
2782}
2783
2784/*
2785 * Count a reference to a Function.
2786 */
2787 void
2788func_ref(char_u *name)
2789{
2790 ufunc_T *fp;
2791
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002792 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002793 return;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002794 fp = find_func(name);
2795 if (fp != NULL)
2796 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002797 else if (isdigit(*name))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002798 /* Only give an error for a numbered function.
2799 * Fail silently, when named or lambda function isn't found. */
2800 EMSG2(_(e_intern2), "func_ref()");
2801}
2802
2803/*
2804 * Count a reference to a Function.
2805 */
2806 void
2807func_ptr_ref(ufunc_T *fp)
2808{
2809 if (fp != NULL)
2810 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002811}
2812
2813/*
2814 * Return TRUE if items in "fc" do not have "copyID". That means they are not
2815 * referenced from anywhere that is in use.
2816 */
2817 static int
2818can_free_funccal(funccall_T *fc, int copyID)
2819{
2820 return (fc->l_varlist.lv_copyID != copyID
2821 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002822 && fc->l_avars.dv_copyID != copyID
2823 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002824}
2825
2826/*
2827 * ":return [expr]"
2828 */
2829 void
2830ex_return(exarg_T *eap)
2831{
2832 char_u *arg = eap->arg;
2833 typval_T rettv;
2834 int returning = FALSE;
2835
2836 if (current_funccal == NULL)
2837 {
2838 EMSG(_("E133: :return not inside a function"));
2839 return;
2840 }
2841
2842 if (eap->skip)
2843 ++emsg_skip;
2844
2845 eap->nextcmd = NULL;
2846 if ((*arg != NUL && *arg != '|' && *arg != '\n')
2847 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
2848 {
2849 if (!eap->skip)
2850 returning = do_return(eap, FALSE, TRUE, &rettv);
2851 else
2852 clear_tv(&rettv);
2853 }
2854 /* It's safer to return also on error. */
2855 else if (!eap->skip)
2856 {
2857 /*
2858 * Return unless the expression evaluation has been cancelled due to an
2859 * aborting error, an interrupt, or an exception.
2860 */
2861 if (!aborting())
2862 returning = do_return(eap, FALSE, TRUE, NULL);
2863 }
2864
2865 /* When skipping or the return gets pending, advance to the next command
2866 * in this line (!returning). Otherwise, ignore the rest of the line.
2867 * Following lines will be ignored by get_func_line(). */
2868 if (returning)
2869 eap->nextcmd = NULL;
2870 else if (eap->nextcmd == NULL) /* no argument */
2871 eap->nextcmd = check_nextcmd(arg);
2872
2873 if (eap->skip)
2874 --emsg_skip;
2875}
2876
2877/*
2878 * ":1,25call func(arg1, arg2)" function call.
2879 */
2880 void
2881ex_call(exarg_T *eap)
2882{
2883 char_u *arg = eap->arg;
2884 char_u *startarg;
2885 char_u *name;
2886 char_u *tofree;
2887 int len;
2888 typval_T rettv;
2889 linenr_T lnum;
2890 int doesrange;
2891 int failed = FALSE;
2892 funcdict_T fudi;
2893 partial_T *partial = NULL;
2894
2895 if (eap->skip)
2896 {
2897 /* trans_function_name() doesn't work well when skipping, use eval0()
2898 * instead to skip to any following command, e.g. for:
2899 * :if 0 | call dict.foo().bar() | endif */
2900 ++emsg_skip;
2901 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
2902 clear_tv(&rettv);
2903 --emsg_skip;
2904 return;
2905 }
2906
2907 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
2908 if (fudi.fd_newkey != NULL)
2909 {
2910 /* Still need to give an error message for missing key. */
2911 EMSG2(_(e_dictkey), fudi.fd_newkey);
2912 vim_free(fudi.fd_newkey);
2913 }
2914 if (tofree == NULL)
2915 return;
2916
2917 /* Increase refcount on dictionary, it could get deleted when evaluating
2918 * the arguments. */
2919 if (fudi.fd_dict != NULL)
2920 ++fudi.fd_dict->dv_refcount;
2921
2922 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
2923 * contents. For VAR_PARTIAL get its partial, unless we already have one
2924 * from trans_function_name(). */
2925 len = (int)STRLEN(tofree);
2926 name = deref_func_name(tofree, &len,
2927 partial != NULL ? NULL : &partial, FALSE);
2928
2929 /* Skip white space to allow ":call func ()". Not good, but required for
2930 * backward compatibility. */
2931 startarg = skipwhite(arg);
2932 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
2933
2934 if (*startarg != '(')
2935 {
2936 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
2937 goto end;
2938 }
2939
2940 /*
2941 * When skipping, evaluate the function once, to find the end of the
2942 * arguments.
2943 * When the function takes a range, this is discovered after the first
2944 * call, and the loop is broken.
2945 */
2946 if (eap->skip)
2947 {
2948 ++emsg_skip;
2949 lnum = eap->line2; /* do it once, also with an invalid range */
2950 }
2951 else
2952 lnum = eap->line1;
2953 for ( ; lnum <= eap->line2; ++lnum)
2954 {
2955 if (!eap->skip && eap->addr_count > 0)
2956 {
2957 curwin->w_cursor.lnum = lnum;
2958 curwin->w_cursor.col = 0;
2959#ifdef FEAT_VIRTUALEDIT
2960 curwin->w_cursor.coladd = 0;
2961#endif
2962 }
2963 arg = startarg;
2964 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
2965 eap->line1, eap->line2, &doesrange,
2966 !eap->skip, partial, fudi.fd_dict) == FAIL)
2967 {
2968 failed = TRUE;
2969 break;
2970 }
2971
2972 /* Handle a function returning a Funcref, Dictionary or List. */
2973 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
2974 {
2975 failed = TRUE;
2976 break;
2977 }
2978
2979 clear_tv(&rettv);
2980 if (doesrange || eap->skip)
2981 break;
2982
2983 /* Stop when immediately aborting on error, or when an interrupt
2984 * occurred or an exception was thrown but not caught.
2985 * get_func_tv() returned OK, so that the check for trailing
2986 * characters below is executed. */
2987 if (aborting())
2988 break;
2989 }
2990 if (eap->skip)
2991 --emsg_skip;
2992
2993 if (!failed)
2994 {
2995 /* Check for trailing illegal characters and a following command. */
2996 if (!ends_excmd(*arg))
2997 {
2998 emsg_severe = TRUE;
2999 EMSG(_(e_trailing));
3000 }
3001 else
3002 eap->nextcmd = check_nextcmd(arg);
3003 }
3004
3005end:
3006 dict_unref(fudi.fd_dict);
3007 vim_free(tofree);
3008}
3009
3010/*
3011 * Return from a function. Possibly makes the return pending. Also called
3012 * for a pending return at the ":endtry" or after returning from an extra
3013 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3014 * when called due to a ":return" command. "rettv" may point to a typval_T
3015 * with the return rettv. Returns TRUE when the return can be carried out,
3016 * FALSE when the return gets pending.
3017 */
3018 int
3019do_return(
3020 exarg_T *eap,
3021 int reanimate,
3022 int is_cmd,
3023 void *rettv)
3024{
3025 int idx;
3026 struct condstack *cstack = eap->cstack;
3027
3028 if (reanimate)
3029 /* Undo the return. */
3030 current_funccal->returned = FALSE;
3031
3032 /*
3033 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3034 * not in its finally clause (which then is to be executed next) is found.
3035 * In this case, make the ":return" pending for execution at the ":endtry".
3036 * Otherwise, return normally.
3037 */
3038 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3039 if (idx >= 0)
3040 {
3041 cstack->cs_pending[idx] = CSTP_RETURN;
3042
3043 if (!is_cmd && !reanimate)
3044 /* A pending return again gets pending. "rettv" points to an
3045 * allocated variable with the rettv of the original ":return"'s
3046 * argument if present or is NULL else. */
3047 cstack->cs_rettv[idx] = rettv;
3048 else
3049 {
3050 /* When undoing a return in order to make it pending, get the stored
3051 * return rettv. */
3052 if (reanimate)
3053 rettv = current_funccal->rettv;
3054
3055 if (rettv != NULL)
3056 {
3057 /* Store the value of the pending return. */
3058 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
3059 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
3060 else
3061 EMSG(_(e_outofmem));
3062 }
3063 else
3064 cstack->cs_rettv[idx] = NULL;
3065
3066 if (reanimate)
3067 {
3068 /* The pending return value could be overwritten by a ":return"
3069 * without argument in a finally clause; reset the default
3070 * return value. */
3071 current_funccal->rettv->v_type = VAR_NUMBER;
3072 current_funccal->rettv->vval.v_number = 0;
3073 }
3074 }
3075 report_make_pending(CSTP_RETURN, rettv);
3076 }
3077 else
3078 {
3079 current_funccal->returned = TRUE;
3080
3081 /* If the return is carried out now, store the return value. For
3082 * a return immediately after reanimation, the value is already
3083 * there. */
3084 if (!reanimate && rettv != NULL)
3085 {
3086 clear_tv(current_funccal->rettv);
3087 *current_funccal->rettv = *(typval_T *)rettv;
3088 if (!is_cmd)
3089 vim_free(rettv);
3090 }
3091 }
3092
3093 return idx < 0;
3094}
3095
3096/*
3097 * Free the variable with a pending return value.
3098 */
3099 void
3100discard_pending_return(void *rettv)
3101{
3102 free_tv((typval_T *)rettv);
3103}
3104
3105/*
3106 * Generate a return command for producing the value of "rettv". The result
3107 * is an allocated string. Used by report_pending() for verbose messages.
3108 */
3109 char_u *
3110get_return_cmd(void *rettv)
3111{
3112 char_u *s = NULL;
3113 char_u *tofree = NULL;
3114 char_u numbuf[NUMBUFLEN];
3115
3116 if (rettv != NULL)
3117 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
3118 if (s == NULL)
3119 s = (char_u *)"";
3120
3121 STRCPY(IObuff, ":return ");
3122 STRNCPY(IObuff + 8, s, IOSIZE - 8);
3123 if (STRLEN(s) + 8 >= IOSIZE)
3124 STRCPY(IObuff + IOSIZE - 4, "...");
3125 vim_free(tofree);
3126 return vim_strsave(IObuff);
3127}
3128
3129/*
3130 * Get next function line.
3131 * Called by do_cmdline() to get the next line.
3132 * Returns allocated string, or NULL for end of function.
3133 */
3134 char_u *
3135get_func_line(
3136 int c UNUSED,
3137 void *cookie,
3138 int indent UNUSED)
3139{
3140 funccall_T *fcp = (funccall_T *)cookie;
3141 ufunc_T *fp = fcp->func;
3142 char_u *retval;
3143 garray_T *gap; /* growarray with function lines */
3144
3145 /* If breakpoints have been added/deleted need to check for it. */
3146 if (fcp->dbg_tick != debug_tick)
3147 {
3148 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
3149 sourcing_lnum);
3150 fcp->dbg_tick = debug_tick;
3151 }
3152#ifdef FEAT_PROFILE
3153 if (do_profiling == PROF_YES)
3154 func_line_end(cookie);
3155#endif
3156
3157 gap = &fp->uf_lines;
3158 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3159 || fcp->returned)
3160 retval = NULL;
3161 else
3162 {
3163 /* Skip NULL lines (continuation lines). */
3164 while (fcp->linenr < gap->ga_len
3165 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
3166 ++fcp->linenr;
3167 if (fcp->linenr >= gap->ga_len)
3168 retval = NULL;
3169 else
3170 {
3171 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
3172 sourcing_lnum = fcp->linenr;
3173#ifdef FEAT_PROFILE
3174 if (do_profiling == PROF_YES)
3175 func_line_start(cookie);
3176#endif
3177 }
3178 }
3179
3180 /* Did we encounter a breakpoint? */
3181 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
3182 {
3183 dbg_breakpoint(fp->uf_name, sourcing_lnum);
3184 /* Find next breakpoint. */
3185 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
3186 sourcing_lnum);
3187 fcp->dbg_tick = debug_tick;
3188 }
3189
3190 return retval;
3191}
3192
3193#if defined(FEAT_PROFILE) || defined(PROTO)
3194/*
3195 * Called when starting to read a function line.
3196 * "sourcing_lnum" must be correct!
3197 * When skipping lines it may not actually be executed, but we won't find out
3198 * until later and we need to store the time now.
3199 */
3200 void
3201func_line_start(void *cookie)
3202{
3203 funccall_T *fcp = (funccall_T *)cookie;
3204 ufunc_T *fp = fcp->func;
3205
3206 if (fp->uf_profiling && sourcing_lnum >= 1
3207 && sourcing_lnum <= fp->uf_lines.ga_len)
3208 {
3209 fp->uf_tml_idx = sourcing_lnum - 1;
3210 /* Skip continuation lines. */
3211 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
3212 --fp->uf_tml_idx;
3213 fp->uf_tml_execed = FALSE;
3214 profile_start(&fp->uf_tml_start);
3215 profile_zero(&fp->uf_tml_children);
3216 profile_get_wait(&fp->uf_tml_wait);
3217 }
3218}
3219
3220/*
3221 * Called when actually executing a function line.
3222 */
3223 void
3224func_line_exec(void *cookie)
3225{
3226 funccall_T *fcp = (funccall_T *)cookie;
3227 ufunc_T *fp = fcp->func;
3228
3229 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
3230 fp->uf_tml_execed = TRUE;
3231}
3232
3233/*
3234 * Called when done with a function line.
3235 */
3236 void
3237func_line_end(void *cookie)
3238{
3239 funccall_T *fcp = (funccall_T *)cookie;
3240 ufunc_T *fp = fcp->func;
3241
3242 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
3243 {
3244 if (fp->uf_tml_execed)
3245 {
3246 ++fp->uf_tml_count[fp->uf_tml_idx];
3247 profile_end(&fp->uf_tml_start);
3248 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
3249 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
3250 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
3251 &fp->uf_tml_children);
3252 }
3253 fp->uf_tml_idx = -1;
3254 }
3255}
3256#endif
3257
3258/*
3259 * Return TRUE if the currently active function should be ended, because a
3260 * return was encountered or an error occurred. Used inside a ":while".
3261 */
3262 int
3263func_has_ended(void *cookie)
3264{
3265 funccall_T *fcp = (funccall_T *)cookie;
3266
3267 /* Ignore the "abort" flag if the abortion behavior has been changed due to
3268 * an error inside a try conditional. */
3269 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3270 || fcp->returned);
3271}
3272
3273/*
3274 * return TRUE if cookie indicates a function which "abort"s on errors.
3275 */
3276 int
3277func_has_abort(
3278 void *cookie)
3279{
3280 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
3281}
3282
3283
3284/*
3285 * Turn "dict.Func" into a partial for "Func" bound to "dict".
3286 * Don't do this when "Func" is already a partial that was bound
3287 * explicitly (pt_auto is FALSE).
3288 * Changes "rettv" in-place.
3289 * Returns the updated "selfdict_in".
3290 */
3291 dict_T *
3292make_partial(dict_T *selfdict_in, typval_T *rettv)
3293{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003294 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003295 char_u *tofree = NULL;
3296 ufunc_T *fp;
3297 char_u fname_buf[FLEN_FIXED + 1];
3298 int error;
3299 dict_T *selfdict = selfdict_in;
3300
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003301 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
3302 fp = rettv->vval.v_partial->pt_func;
3303 else
3304 {
3305 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
3306 : rettv->vval.v_partial->pt_name;
3307 /* Translate "s:func" to the stored function name. */
3308 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
3309 fp = find_func(fname);
3310 vim_free(tofree);
3311 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003312
3313 if (fp != NULL && (fp->uf_flags & FC_DICT))
3314 {
3315 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3316
3317 if (pt != NULL)
3318 {
3319 pt->pt_refcount = 1;
3320 pt->pt_dict = selfdict;
3321 pt->pt_auto = TRUE;
3322 selfdict = NULL;
3323 if (rettv->v_type == VAR_FUNC)
3324 {
3325 /* Just a function: Take over the function name and use
3326 * selfdict. */
3327 pt->pt_name = rettv->vval.v_string;
3328 }
3329 else
3330 {
3331 partial_T *ret_pt = rettv->vval.v_partial;
3332 int i;
3333
3334 /* Partial: copy the function name, use selfdict and copy
3335 * args. Can't take over name or args, the partial might
3336 * be referenced elsewhere. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003337 if (ret_pt->pt_name != NULL)
3338 {
3339 pt->pt_name = vim_strsave(ret_pt->pt_name);
3340 func_ref(pt->pt_name);
3341 }
3342 else
3343 {
3344 pt->pt_func = ret_pt->pt_func;
3345 func_ptr_ref(pt->pt_func);
3346 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003347 if (ret_pt->pt_argc > 0)
3348 {
3349 pt->pt_argv = (typval_T *)alloc(
3350 sizeof(typval_T) * ret_pt->pt_argc);
3351 if (pt->pt_argv == NULL)
3352 /* out of memory: drop the arguments */
3353 pt->pt_argc = 0;
3354 else
3355 {
3356 pt->pt_argc = ret_pt->pt_argc;
3357 for (i = 0; i < pt->pt_argc; i++)
3358 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
3359 }
3360 }
3361 partial_unref(ret_pt);
3362 }
3363 rettv->v_type = VAR_PARTIAL;
3364 rettv->vval.v_partial = pt;
3365 }
3366 }
3367 return selfdict;
3368}
3369
3370/*
3371 * Return the name of the executed function.
3372 */
3373 char_u *
3374func_name(void *cookie)
3375{
3376 return ((funccall_T *)cookie)->func->uf_name;
3377}
3378
3379/*
3380 * Return the address holding the next breakpoint line for a funccall cookie.
3381 */
3382 linenr_T *
3383func_breakpoint(void *cookie)
3384{
3385 return &((funccall_T *)cookie)->breakpoint;
3386}
3387
3388/*
3389 * Return the address holding the debug tick for a funccall cookie.
3390 */
3391 int *
3392func_dbg_tick(void *cookie)
3393{
3394 return &((funccall_T *)cookie)->dbg_tick;
3395}
3396
3397/*
3398 * Return the nesting level for a funccall cookie.
3399 */
3400 int
3401func_level(void *cookie)
3402{
3403 return ((funccall_T *)cookie)->level;
3404}
3405
3406/*
3407 * Return TRUE when a function was ended by a ":return" command.
3408 */
3409 int
3410current_func_returned(void)
3411{
3412 return current_funccal->returned;
3413}
3414
3415/*
3416 * Save the current function call pointer, and set it to NULL.
3417 * Used when executing autocommands and for ":source".
3418 */
3419 void *
3420save_funccal(void)
3421{
3422 funccall_T *fc = current_funccal;
3423
3424 current_funccal = NULL;
3425 return (void *)fc;
3426}
3427
3428 void
3429restore_funccal(void *vfc)
3430{
3431 funccall_T *fc = (funccall_T *)vfc;
3432
3433 current_funccal = fc;
3434}
3435
3436 int
3437free_unref_funccal(int copyID, int testing)
3438{
3439 int did_free = FALSE;
3440 int did_free_funccal = FALSE;
3441 funccall_T *fc, **pfc;
3442
3443 for (pfc = &previous_funccal; *pfc != NULL; )
3444 {
3445 if (can_free_funccal(*pfc, copyID))
3446 {
3447 fc = *pfc;
3448 *pfc = fc->caller;
3449 free_funccal(fc, TRUE);
3450 did_free = TRUE;
3451 did_free_funccal = TRUE;
3452 }
3453 else
3454 pfc = &(*pfc)->caller;
3455 }
3456 if (did_free_funccal)
3457 /* When a funccal was freed some more items might be garbage
3458 * collected, so run again. */
3459 (void)garbage_collect(testing);
3460
3461 return did_free;
3462}
3463
3464/*
3465 * Get function call environment based on bactrace debug level
3466 */
3467 static funccall_T *
3468get_funccal(void)
3469{
3470 int i;
3471 funccall_T *funccal;
3472 funccall_T *temp_funccal;
3473
3474 funccal = current_funccal;
3475 if (debug_backtrace_level > 0)
3476 {
3477 for (i = 0; i < debug_backtrace_level; i++)
3478 {
3479 temp_funccal = funccal->caller;
3480 if (temp_funccal)
3481 funccal = temp_funccal;
3482 else
3483 /* backtrace level overflow. reset to max */
3484 debug_backtrace_level = i;
3485 }
3486 }
3487 return funccal;
3488}
3489
3490/*
3491 * Return the hashtable used for local variables in the current funccal.
3492 * Return NULL if there is no current funccal.
3493 */
3494 hashtab_T *
3495get_funccal_local_ht()
3496{
3497 if (current_funccal == NULL)
3498 return NULL;
3499 return &get_funccal()->l_vars.dv_hashtab;
3500}
3501
3502/*
3503 * Return the l: scope variable.
3504 * Return NULL if there is no current funccal.
3505 */
3506 dictitem_T *
3507get_funccal_local_var()
3508{
3509 if (current_funccal == NULL)
3510 return NULL;
3511 return &get_funccal()->l_vars_var;
3512}
3513
3514/*
3515 * Return the hashtable used for argument in the current funccal.
3516 * Return NULL if there is no current funccal.
3517 */
3518 hashtab_T *
3519get_funccal_args_ht()
3520{
3521 if (current_funccal == NULL)
3522 return NULL;
3523 return &get_funccal()->l_avars.dv_hashtab;
3524}
3525
3526/*
3527 * Return the a: scope variable.
3528 * Return NULL if there is no current funccal.
3529 */
3530 dictitem_T *
3531get_funccal_args_var()
3532{
3533 if (current_funccal == NULL)
3534 return NULL;
3535 return &current_funccal->l_avars_var;
3536}
3537
3538/*
3539 * Clear the current_funccal and return the old value.
3540 * Caller is expected to invoke restore_current_funccal().
3541 */
3542 void *
3543clear_current_funccal()
3544{
3545 funccall_T *f = current_funccal;
3546
3547 current_funccal = NULL;
3548 return f;
3549}
3550
3551 void
3552restore_current_funccal(void *f)
3553{
3554 current_funccal = f;
3555}
3556
3557/*
3558 * List function variables, if there is a function.
3559 */
3560 void
3561list_func_vars(int *first)
3562{
3563 if (current_funccal != NULL)
3564 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
3565 (char_u *)"l:", FALSE, first);
3566}
3567
3568/*
3569 * If "ht" is the hashtable for local variables in the current funccal, return
3570 * the dict that contains it.
3571 * Otherwise return NULL.
3572 */
3573 dict_T *
3574get_current_funccal_dict(hashtab_T *ht)
3575{
3576 if (current_funccal != NULL
3577 && ht == &current_funccal->l_vars.dv_hashtab)
3578 return &current_funccal->l_vars;
3579 return NULL;
3580}
3581
3582/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003583 * Search hashitem in parent scope.
3584 */
3585 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003586find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003587{
3588 funccall_T *old_current_funccal = current_funccal;
3589 hashtab_T *ht;
3590 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003591 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003592
3593 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
3594 return NULL;
3595
3596 /* Search in parent scope which is possible to reference from lambda */
3597 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02003598 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003599 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003600 ht = find_var_ht(name, &varname);
3601 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02003602 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003603 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02003604 if (!HASHITEM_EMPTY(hi))
3605 {
3606 *pht = ht;
3607 break;
3608 }
3609 }
3610 if (current_funccal == current_funccal->func->uf_scoped)
3611 break;
3612 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003613 }
3614 current_funccal = old_current_funccal;
3615
3616 return hi;
3617}
3618
3619/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003620 * Search variable in parent scope.
3621 */
3622 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003623find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003624{
3625 dictitem_T *v = NULL;
3626 funccall_T *old_current_funccal = current_funccal;
3627 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003628 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003629
3630 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
3631 return NULL;
3632
3633 /* Search in parent scope which is possible to reference from lambda */
3634 current_funccal = current_funccal->func->uf_scoped;
3635 while (current_funccal)
3636 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003637 ht = find_var_ht(name, &varname);
3638 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003639 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003640 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003641 if (v != NULL)
3642 break;
3643 }
3644 if (current_funccal == current_funccal->func->uf_scoped)
3645 break;
3646 current_funccal = current_funccal->func->uf_scoped;
3647 }
3648 current_funccal = old_current_funccal;
3649
3650 return v;
3651}
3652
3653/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003654 * Set "copyID + 1" in previous_funccal and callers.
3655 */
3656 int
3657set_ref_in_previous_funccal(int copyID)
3658{
3659 int abort = FALSE;
3660 funccall_T *fc;
3661
3662 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
3663 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003664 fc->fc_copyID = copyID + 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003665 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
3666 NULL);
3667 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
3668 NULL);
3669 }
3670 return abort;
3671}
3672
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003673 static int
3674set_ref_in_funccal(funccall_T *fc, int copyID)
3675{
3676 int abort = FALSE;
3677
3678 if (fc->fc_copyID != copyID)
3679 {
3680 fc->fc_copyID = copyID;
3681 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
3682 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
3683 abort = abort || set_ref_in_func(NULL, fc->func, copyID);
3684 }
3685 return abort;
3686}
3687
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003688/*
3689 * Set "copyID" in all local vars and arguments in the call stack.
3690 */
3691 int
3692set_ref_in_call_stack(int copyID)
3693{
3694 int abort = FALSE;
3695 funccall_T *fc;
3696
3697 for (fc = current_funccal; fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003698 abort = abort || set_ref_in_funccal(fc, copyID);
3699 return abort;
3700}
3701
3702/*
3703 * Set "copyID" in all functions available by name.
3704 */
3705 int
3706set_ref_in_functions(int copyID)
3707{
3708 int todo;
3709 hashitem_T *hi = NULL;
3710 int abort = FALSE;
3711 ufunc_T *fp;
3712
3713 todo = (int)func_hashtab.ht_used;
3714 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003715 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003716 if (!HASHITEM_EMPTY(hi))
3717 {
3718 --todo;
3719 fp = HI2UF(hi);
3720 if (!func_name_refcount(fp->uf_name))
3721 abort = abort || set_ref_in_func(NULL, fp, copyID);
3722 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003723 }
3724 return abort;
3725}
3726
3727/*
3728 * Set "copyID" in all function arguments.
3729 */
3730 int
3731set_ref_in_func_args(int copyID)
3732{
3733 int i;
3734 int abort = FALSE;
3735
3736 for (i = 0; i < funcargs.ga_len; ++i)
3737 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
3738 copyID, NULL, NULL);
3739 return abort;
3740}
3741
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003742/*
3743 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003744 * Returns TRUE if setting references failed somehow.
3745 */
3746 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003747set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003748{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003749 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003750 funccall_T *fc;
3751 int error = ERROR_NONE;
3752 char_u fname_buf[FLEN_FIXED + 1];
3753 char_u *tofree = NULL;
3754 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003755 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003756
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003757 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003758 return FALSE;
3759
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003760 if (fp_in == NULL)
3761 {
3762 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3763 fp = find_func(fname);
3764 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003765 if (fp != NULL)
3766 {
3767 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003768 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003769 }
3770 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003771 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003772}
3773
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003774#endif /* FEAT_EVAL */