blob: 05e5b7f56d911b61fd26dbb2e99d509e652cb402 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +020011 * userfunc.c: User defined function support
Bram Moolenaara9b579f2016-07-17 18:29:19 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar93343722018-07-10 19:39:18 +020017// flags used in uf_flags
18#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
24#define FC_SANDBOX 0x40 // function defined in the sandbox
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010025#define FC_DEAD 0x80 // function kept only for reference to dfunc
26#define FC_EXPORT 0x100 // "export def Func()"
Bram Moolenaara9b579f2016-07-17 18:29:19 +020027
Bram Moolenaara9b579f2016-07-17 18:29:19 +020028/*
29 * All user-defined functions are found in this hashtable.
30 */
31static hashtab_T func_hashtab;
32
Bram Moolenaare38eab22019-12-05 21:50:01 +010033// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020034static garray_T funcargs = GA_EMPTY;
35
Bram Moolenaar209b8e32019-03-14 13:43:24 +010036// pointer to funccal for currently active function
37static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020038
Bram Moolenaar209b8e32019-03-14 13:43:24 +010039// Pointer to list of previously used funccal, still around because some
40// item in it is still being used.
41static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020042
43static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
44static char *e_funcdict = N_("E717: Dictionary entry already exists");
45static char *e_funcref = N_("E718: Funcref required");
46static char *e_nofunc = N_("E130: Unknown function: %s");
47
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020048static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020049
50 void
51func_init()
52{
53 hash_init(&func_hashtab);
54}
55
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020056/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020057 * Return the function hash table
58 */
59 hashtab_T *
60func_tbl_get(void)
61{
62 return &func_hashtab;
63}
64
65/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010066 * Get one function argument and an optional type: "arg: type".
67 * Return a pointer to after the type.
68 * When something is wrong return "arg".
69 */
70 static char_u *
71one_function_arg(char_u *arg, garray_T *newargs, garray_T *argtypes, int skip)
72{
73 char_u *p = arg;
74
75 while (ASCII_ISALNUM(*p) || *p == '_')
76 ++p;
77 if (arg == p || isdigit(*arg)
78 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
79 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
80 {
81 if (!skip)
82 semsg(_("E125: Illegal argument: %s"), arg);
83 return arg;
84 }
85 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
86 return arg;
87 if (newargs != NULL)
88 {
89 char_u *arg_copy;
90 int c;
91 int i;
92
93 c = *p;
94 *p = NUL;
95 arg_copy = vim_strsave(arg);
96 if (arg_copy == NULL)
97 {
98 *p = c;
99 return arg;
100 }
101
102 // Check for duplicate argument name.
103 for (i = 0; i < newargs->ga_len; ++i)
104 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
105 {
106 semsg(_("E853: Duplicate argument name: %s"), arg_copy);
107 vim_free(arg_copy);
108 return arg;
109 }
110 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
111 newargs->ga_len++;
112
113 *p = c;
114 }
115
116 // get any type from "arg: type"
117 if (argtypes != NULL && ga_grow(argtypes, 1) == OK)
118 {
119 char_u *type = NULL;
120
121 if (*p == ':')
122 {
123 type = skipwhite(p + 1);
124 p = skip_type(type);
125 type = vim_strnsave(type, p - type);
126 }
127 else if (*skipwhite(p) == ':')
128 emsg(_("E1059: No white space allowed before :"));
129 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
130 }
131
132 return p;
133}
134
135/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200136 * Get function arguments.
137 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200139get_function_args(
140 char_u **argp,
141 char_u endchar,
142 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143 garray_T *argtypes, // NULL unless using :def
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200144 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200145 garray_T *default_args,
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200146 int skip)
147{
148 int mustend = FALSE;
149 char_u *arg = *argp;
150 char_u *p = arg;
151 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200152 int any_default = FALSE;
153 char_u *expr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200154
155 if (newargs != NULL)
156 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157 if (argtypes != NULL)
158 ga_init2(argtypes, (int)sizeof(char_u *), 3);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200159 if (default_args != NULL)
160 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200161
162 if (varargs != NULL)
163 *varargs = FALSE;
164
165 /*
166 * Isolate the arguments: "arg1, arg2, ...)"
167 */
168 while (*p != endchar)
169 {
170 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
171 {
172 if (varargs != NULL)
173 *varargs = TRUE;
174 p += 3;
175 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100176
177 if (argtypes != NULL)
178 {
179 // ...name: list<type>
180 if (!ASCII_ISALPHA(*p))
181 {
182 emsg(_("E1055: Missing name after ..."));
183 break;
184 }
185
186 arg = p;
187 p = one_function_arg(p, newargs, argtypes, skip);
188 if (p == arg)
189 break;
190 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200191 }
192 else
193 {
194 arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100195 p = one_function_arg(p, newargs, argtypes, skip);
196 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200197 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200198
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200199 if (*skipwhite(p) == '=' && default_args != NULL)
200 {
201 typval_T rettv;
202
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100203 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200204 any_default = TRUE;
205 p = skipwhite(p) + 1;
206 p = skipwhite(p);
207 expr = p;
208 if (eval1(&p, &rettv, FALSE) != FAIL)
209 {
210 if (ga_grow(default_args, 1) == FAIL)
211 goto err_ret;
212
213 // trim trailing whitespace
214 while (p > expr && VIM_ISWHITE(p[-1]))
215 p--;
216 c = *p;
217 *p = NUL;
218 expr = vim_strsave(expr);
219 if (expr == NULL)
220 {
221 *p = c;
222 goto err_ret;
223 }
224 ((char_u **)(default_args->ga_data))
225 [default_args->ga_len] = expr;
226 default_args->ga_len++;
227 *p = c;
228 }
229 else
230 mustend = TRUE;
231 }
232 else if (any_default)
233 {
234 emsg(_("E989: Non-default argument follows default argument"));
235 mustend = TRUE;
236 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200237 if (*p == ',')
238 ++p;
239 else
240 mustend = TRUE;
241 }
242 p = skipwhite(p);
243 if (mustend && *p != endchar)
244 {
245 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100246 semsg(_(e_invarg2), *argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200247 break;
248 }
249 }
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200250 if (*p != endchar)
251 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100252 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200253
254 *argp = p;
255 return OK;
256
257err_ret:
258 if (newargs != NULL)
259 ga_clear_strings(newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200260 if (default_args != NULL)
261 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200262 return FAIL;
263}
264
265/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200266 * Register function "fp" as using "current_funccal" as its scope.
267 */
268 static int
269register_closure(ufunc_T *fp)
270{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200271 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100272 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200273 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200274 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200275 fp->uf_scoped = current_funccal;
276 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200277
Bram Moolenaar58016442016-07-31 18:30:22 +0200278 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
279 return FAIL;
280 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
281 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200282 return OK;
283}
284
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100285 static void
286set_ufunc_name(ufunc_T *fp, char_u *name)
287{
288 STRCPY(fp->uf_name, name);
289
290 if (name[0] == K_SPECIAL)
291 {
292 fp->uf_name_exp = alloc(STRLEN(name) + 3);
293 if (fp->uf_name_exp != NULL)
294 {
295 STRCPY(fp->uf_name_exp, "<SNR>");
296 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
297 }
298 }
299}
300
Bram Moolenaar58016442016-07-31 18:30:22 +0200301/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200302 * Parse a lambda expression and get a Funcref from "*arg".
303 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
304 */
305 int
306get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
307{
308 garray_T newargs;
309 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200310 garray_T *pnewargs;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200311 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100312 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200313 int varargs;
314 int ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200315 char_u *start = skipwhite(*arg + 1);
316 char_u *s, *e;
317 static int lambda_no = 0;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200318 int *old_eval_lavars = eval_lavars_used;
319 int eval_lavars = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200320
321 ga_init(&newargs);
322 ga_init(&newlines);
323
Bram Moolenaare38eab22019-12-05 21:50:01 +0100324 // First, check if this is a lambda expression. "->" must exist.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100325 ret = get_function_args(&start, '-', NULL, NULL, NULL, NULL, TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200326 if (ret == FAIL || *start != '>')
327 return NOTDONE;
328
Bram Moolenaare38eab22019-12-05 21:50:01 +0100329 // Parse the arguments again.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200330 if (evaluate)
331 pnewargs = &newargs;
332 else
333 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200334 *arg = skipwhite(*arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100335 // TODO: argument types
336 ret = get_function_args(arg, '-', pnewargs, NULL, &varargs, NULL, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200337 if (ret == FAIL || **arg != '>')
338 goto errret;
339
Bram Moolenaare38eab22019-12-05 21:50:01 +0100340 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200341 if (evaluate)
342 eval_lavars_used = &eval_lavars;
343
Bram Moolenaare38eab22019-12-05 21:50:01 +0100344 // Get the start and the end of the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200345 *arg = skipwhite(*arg + 1);
346 s = *arg;
347 ret = skip_expr(arg);
348 if (ret == FAIL)
349 goto errret;
350 e = *arg;
351 *arg = skipwhite(*arg);
352 if (**arg != '}')
Bram Moolenaaree619e52020-03-28 21:38:06 +0100353 {
354 semsg(_("E451: Expected }: %s"), *arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200355 goto errret;
Bram Moolenaaree619e52020-03-28 21:38:06 +0100356 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200357 ++*arg;
358
359 if (evaluate)
360 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200361 int len, flags = 0;
362 char_u *p;
363 char_u name[20];
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200364
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200365 sprintf((char*)name, "<lambda>%d", ++lambda_no);
366
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200367 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200368 if (fp == NULL)
369 goto errret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100370 fp->uf_dfunc_idx = -1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200371 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200372 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200373 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200374
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200375 ga_init2(&newlines, (int)sizeof(char_u *), 1);
376 if (ga_grow(&newlines, 1) == FAIL)
377 goto errret;
378
Bram Moolenaare38eab22019-12-05 21:50:01 +0100379 // Add "return " before the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200380 len = 7 + e - s + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200381 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200382 if (p == NULL)
383 goto errret;
384 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
385 STRCPY(p, "return ");
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200386 vim_strncpy(p + 7, s, e - s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200387
388 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100389 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200390 hash_add(&func_hashtab, UF2HIKEY(fp));
391 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200392 ga_init(&fp->uf_def_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200393 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200394 if (current_funccal != NULL && eval_lavars)
395 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200396 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200397 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200398 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200399 }
400 else
401 fp->uf_scoped = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200402
403#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200404 if (prof_def_func())
405 func_do_profile(fp);
406#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200407 if (sandbox)
408 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100409 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200410 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200411 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200412 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200413 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100414 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200415
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200416 pt->pt_func = fp;
417 pt->pt_refcount = 1;
418 rettv->vval.v_partial = pt;
419 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200420 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200421
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200422 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200423 return OK;
424
425errret:
426 ga_clear_strings(&newargs);
427 ga_clear_strings(&newlines);
428 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100429 vim_free(pt);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200430 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200431 return FAIL;
432}
433
434/*
435 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
436 * name it contains, otherwise return "name".
437 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
438 * "partialp".
439 */
440 char_u *
441deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
442{
443 dictitem_T *v;
444 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200445 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200446
447 if (partialp != NULL)
448 *partialp = NULL;
449
450 cc = name[*lenp];
451 name[*lenp] = NUL;
452 v = find_var(name, NULL, no_autoload);
453 name[*lenp] = cc;
454 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
455 {
456 if (v->di_tv.vval.v_string == NULL)
457 {
458 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100459 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200460 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200461 s = v->di_tv.vval.v_string;
462 *lenp = (int)STRLEN(s);
463 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200464 }
465
466 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
467 {
468 partial_T *pt = v->di_tv.vval.v_partial;
469
470 if (pt == NULL)
471 {
472 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100473 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200474 }
475 if (partialp != NULL)
476 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200477 s = partial_name(pt);
478 *lenp = (int)STRLEN(s);
479 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200480 }
481
482 return name;
483}
484
485/*
486 * Give an error message with a function name. Handle <SNR> things.
487 * "ermsg" is to be passed without translation, use N_() instead of _().
488 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100489 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200490emsg_funcname(char *ermsg, char_u *name)
491{
492 char_u *p;
493
494 if (*name == K_SPECIAL)
495 p = concat_str((char_u *)"<SNR>", name + 3);
496 else
497 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100498 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200499 if (p != name)
500 vim_free(p);
501}
502
503/*
504 * Allocate a variable for the result of a function.
505 * Return OK or FAIL.
506 */
507 int
508get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200509 char_u *name, // name of the function
510 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200511 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200512 char_u **arg, // argument, pointing to the '('
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200513 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200514{
515 char_u *argp;
516 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100517 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
518 int argcount = 0; // number of arguments found
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200519
520 /*
521 * Get the arguments.
522 */
523 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200524 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
525 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200526 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100527 argp = skipwhite(argp + 1); // skip the '(' or ','
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200528 if (*argp == ')' || *argp == ',' || *argp == NUL)
529 break;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200530 if (eval1(&argp, &argvars[argcount], funcexe->evaluate) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200531 {
532 ret = FAIL;
533 break;
534 }
535 ++argcount;
536 if (*argp != ',')
537 break;
538 }
539 if (*argp == ')')
540 ++argp;
541 else
542 ret = FAIL;
543
544 if (ret == OK)
545 {
546 int i = 0;
547
548 if (get_vim_var_nr(VV_TESTING))
549 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100550 // Prepare for calling test_garbagecollect_now(), need to know
551 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200552 if (funcargs.ga_itemsize == 0)
553 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
554 for (i = 0; i < argcount; ++i)
555 if (ga_grow(&funcargs, 1) == OK)
556 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
557 &argvars[i];
558 }
559
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200560 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200561
562 funcargs.ga_len -= i;
563 }
564 else if (!aborting())
565 {
566 if (argcount == MAX_FUNC_ARGS)
567 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
568 else
569 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
570 }
571
572 while (--argcount >= 0)
573 clear_tv(&argvars[argcount]);
574
575 *arg = skipwhite(argp);
576 return ret;
577}
578
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200579/*
580 * Return TRUE if "p" starts with "<SID>" or "s:".
581 * Only works if eval_fname_script() returned non-zero for "p"!
582 */
583 static int
584eval_fname_sid(char_u *p)
585{
586 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
587}
588
589/*
590 * In a script change <SID>name() and s:name() to K_SNR 123_name().
591 * Change <SNR>123_name() to K_SNR 123_name().
592 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
593 * (slow).
594 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100595 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200596fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
597{
598 int llen;
599 char_u *fname;
600 int i;
601
602 llen = eval_fname_script(name);
603 if (llen > 0)
604 {
605 fname_buf[0] = K_SPECIAL;
606 fname_buf[1] = KS_EXTRA;
607 fname_buf[2] = (int)KE_SNR;
608 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100609 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200610 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200611 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100612 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200613 else
614 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200615 sprintf((char *)fname_buf + 3, "%ld_",
616 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200617 i = (int)STRLEN(fname_buf);
618 }
619 }
620 if (i + STRLEN(name + llen) < FLEN_FIXED)
621 {
622 STRCPY(fname_buf + i, name + llen);
623 fname = fname_buf;
624 }
625 else
626 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200627 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200628 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +0100629 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200630 else
631 {
632 *tofree = fname;
633 mch_memmove(fname, fname_buf, (size_t)i);
634 STRCPY(fname + i, name + llen);
635 }
636 }
637 }
638 else
639 fname = name;
640 return fname;
641}
642
643/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100644 * Find a function "name" in script "sid".
645 */
646 static ufunc_T *
647find_func_with_sid(char_u *name, int sid)
648{
649 hashitem_T *hi;
650 char_u buffer[200];
651
652 buffer[0] = K_SPECIAL;
653 buffer[1] = KS_EXTRA;
654 buffer[2] = (int)KE_SNR;
655 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
656 (long)sid, name);
657 hi = hash_find(&func_hashtab, buffer);
658 if (!HASHITEM_EMPTY(hi))
659 return HI2UF(hi);
660
661 return NULL;
662}
663
664/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200665 * Find a function by name, return pointer to it in ufuncs.
666 * Return NULL for unknown function.
667 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100668 static ufunc_T *
669find_func_even_dead(char_u *name, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200670{
671 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100672 ufunc_T *func;
673 imported_T *imported;
674
675 if (in_vim9script())
676 {
677 // Find script-local function before global one.
678 func = find_func_with_sid(name, current_sctx.sc_sid);
679 if (func != NULL)
680 return func;
681
682 // Find imported funcion before global one.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +0100683 imported = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100684 if (imported != NULL && imported->imp_funcname != NULL)
685 {
686 hi = hash_find(&func_hashtab, imported->imp_funcname);
687 if (!HASHITEM_EMPTY(hi))
688 return HI2UF(hi);
689 }
690 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200691
692 hi = hash_find(&func_hashtab, name);
693 if (!HASHITEM_EMPTY(hi))
694 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100695
696 return NULL;
697}
698
699/*
700 * Find a function by name, return pointer to it in ufuncs.
701 * "cctx" is passed in a :def function to find imported functions.
702 * Return NULL for unknown or dead function.
703 */
704 ufunc_T *
705find_func(char_u *name, cctx_T *cctx)
706{
707 ufunc_T *fp = find_func_even_dead(name, cctx);
708
709 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
710 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200711 return NULL;
712}
713
714/*
715 * Copy the function name of "fp" to buffer "buf".
716 * "buf" must be able to hold the function name plus three bytes.
717 * Takes care of script-local function names.
718 */
719 static void
720cat_func_name(char_u *buf, ufunc_T *fp)
721{
722 if (fp->uf_name[0] == K_SPECIAL)
723 {
724 STRCPY(buf, "<SNR>");
725 STRCAT(buf, fp->uf_name + 3);
726 }
727 else
728 STRCPY(buf, fp->uf_name);
729}
730
731/*
732 * Add a number variable "name" to dict "dp" with value "nr".
733 */
734 static void
735add_nr_var(
736 dict_T *dp,
737 dictitem_T *v,
738 char *name,
739 varnumber_T nr)
740{
741 STRCPY(v->di_key, name);
742 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
743 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
744 v->di_tv.v_type = VAR_NUMBER;
745 v->di_tv.v_lock = VAR_FIXED;
746 v->di_tv.vval.v_number = nr;
747}
748
749/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100750 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200751 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100752 static void
753free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200754{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100755 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200756
757 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
758 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100759 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200760
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100761 // When garbage collecting a funccall_T may be freed before the
762 // function that references it, clear its uf_scoped field.
763 // The function may have been redefined and point to another
764 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200765 if (fp != NULL && fp->uf_scoped == fc)
766 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200767 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200768 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200769
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200770 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200771 vim_free(fc);
772}
773
774/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100775 * Free "fc" and what it contains.
776 * Can be called only when "fc" is kept beyond the period of it called,
777 * i.e. after cleanup_function_call(fc).
778 */
779 static void
780free_funccal_contents(funccall_T *fc)
781{
782 listitem_T *li;
783
784 // Free all l: variables.
785 vars_clear(&fc->l_vars.dv_hashtab);
786
787 // Free all a: variables.
788 vars_clear(&fc->l_avars.dv_hashtab);
789
790 // Free the a:000 variables.
791 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
792 clear_tv(&li->li_tv);
793
794 free_funccal(fc);
795}
796
797/*
Bram Moolenaar6914c642017-04-01 21:21:30 +0200798 * Handle the last part of returning from a function: free the local hashtable.
799 * Unless it is still in use by a closure.
800 */
801 static void
802cleanup_function_call(funccall_T *fc)
803{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100804 int may_free_fc = fc->fc_refcount <= 0;
805 int free_fc = TRUE;
806
Bram Moolenaar6914c642017-04-01 21:21:30 +0200807 current_funccal = fc->caller;
808
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100809 // Free all l: variables if not referred.
810 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
811 vars_clear(&fc->l_vars.dv_hashtab);
812 else
813 free_fc = FALSE;
814
815 // If the a:000 list and the l: and a: dicts are not referenced and
816 // there is no closure using it, we can free the funccall_T and what's
817 // in it.
818 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
819 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200820 else
821 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100822 int todo;
823 hashitem_T *hi;
824 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200825
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100826 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200827
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100828 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200829 todo = (int)fc->l_avars.dv_hashtab.ht_used;
830 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
831 {
832 if (!HASHITEM_EMPTY(hi))
833 {
834 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100835 di = HI2DI(hi);
836 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200837 }
838 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100839 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200840
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100841 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
842 fc->l_varlist.lv_first = NULL;
843 else
844 {
845 listitem_T *li;
846
847 free_fc = FALSE;
848
849 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200850 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
851 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100852 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100853
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100854 if (free_fc)
855 free_funccal(fc);
856 else
857 {
858 static int made_copy = 0;
859
860 // "fc" is still in use. This can happen when returning "a:000",
861 // assigning "l:" to a global variable or defining a closure.
862 // Link "fc" in the list for garbage collection later.
863 fc->caller = previous_funccal;
864 previous_funccal = fc;
865
866 if (want_garbage_collect)
867 // If garbage collector is ready, clear count.
868 made_copy = 0;
869 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100870 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100871 // We have made a lot of copies, worth 4 Mbyte. This can happen
872 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100873 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100874 // too much memory.
875 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100876 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100877 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200878 }
879}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880/*
881 * Unreference "fc": decrement the reference count and free it when it
882 * becomes zero. "fp" is detached from "fc".
883 * When "force" is TRUE we are exiting.
884 */
885 static void
886funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
887{
888 funccall_T **pfc;
889 int i;
890
891 if (fc == NULL)
892 return;
893
894 if (--fc->fc_refcount <= 0 && (force || (
895 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
896 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
897 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
898 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
899 {
900 if (fc == *pfc)
901 {
902 *pfc = fc->caller;
903 free_funccal_contents(fc);
904 return;
905 }
906 }
907 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
908 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
909 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
910}
911
912/*
913 * Remove the function from the function hashtable. If the function was
914 * deleted while it still has references this was already done.
915 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
916 */
917 static int
918func_remove(ufunc_T *fp)
919{
920 hashitem_T *hi;
921
922 // Return if it was already virtually deleted.
923 if (fp->uf_flags & FC_DEAD)
924 return FALSE;
925
926 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
927 if (!HASHITEM_EMPTY(hi))
928 {
929 // When there is a def-function index do not actually remove the
930 // function, so we can find the index when defining the function again.
931 if (fp->uf_dfunc_idx >= 0)
932 fp->uf_flags |= FC_DEAD;
933 else
934 hash_remove(&func_hashtab, hi);
935 return TRUE;
936 }
937 return FALSE;
938}
939
940 static void
941func_clear_items(ufunc_T *fp)
942{
943 ga_clear_strings(&(fp->uf_args));
944 ga_clear_strings(&(fp->uf_def_args));
945 ga_clear_strings(&(fp->uf_lines));
946 VIM_CLEAR(fp->uf_name_exp);
947 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100948 VIM_CLEAR(fp->uf_def_arg_idx);
949 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950 ga_clear(&fp->uf_type_list);
951#ifdef FEAT_PROFILE
952 VIM_CLEAR(fp->uf_tml_count);
953 VIM_CLEAR(fp->uf_tml_total);
954 VIM_CLEAR(fp->uf_tml_self);
955#endif
956}
957
958/*
959 * Free all things that a function contains. Does not free the function
960 * itself, use func_free() for that.
961 * When "force" is TRUE we are exiting.
962 */
963 static void
964func_clear(ufunc_T *fp, int force)
965{
966 if (fp->uf_cleared)
967 return;
968 fp->uf_cleared = TRUE;
969
970 // clear this function
971 func_clear_items(fp);
972 funccal_unref(fp->uf_scoped, fp, force);
973 delete_def_function(fp);
974}
975
976/*
977 * Free a function and remove it from the list of functions. Does not free
978 * what a function contains, call func_clear() first.
979 */
980 static void
981func_free(ufunc_T *fp)
982{
983 // Only remove it when not done already, otherwise we would remove a newer
984 // version of the function with the same name.
985 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
986 func_remove(fp);
987
988 if ((fp->uf_flags & FC_DEAD) == 0)
989 vim_free(fp);
990}
991
992/*
993 * Free all things that a function contains and free the function itself.
994 * When "force" is TRUE we are exiting.
995 */
996 static void
997func_clear_free(ufunc_T *fp, int force)
998{
999 func_clear(fp, force);
1000 func_free(fp);
1001}
1002
Bram Moolenaar6914c642017-04-01 21:21:30 +02001003
1004/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001005 * Call a user function.
1006 */
1007 static void
1008call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001009 ufunc_T *fp, // pointer to function
1010 int argcount, // nr of args
1011 typval_T *argvars, // arguments
1012 typval_T *rettv, // return value
1013 linenr_T firstline, // first line of range
1014 linenr_T lastline, // last line of range
1015 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001016{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001017 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001018 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001019 funccall_T *fc;
1020 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001021 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001022 static int depth = 0;
1023 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001024 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001025 int i;
1026 int ai;
1027 int islambda = FALSE;
1028 char_u numbuf[NUMBUFLEN];
1029 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001030#ifdef FEAT_PROFILE
1031 proftime_T wait_start;
1032 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001033 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001034#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001035 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001036
Bram Moolenaare38eab22019-12-05 21:50:01 +01001037 // If depth of calling is getting too high, don't execute the function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001038 if (depth >= p_mfd)
1039 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001040 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001041 rettv->v_type = VAR_NUMBER;
1042 rettv->vval.v_number = -1;
1043 return;
1044 }
1045 ++depth;
1046
Bram Moolenaare38eab22019-12-05 21:50:01 +01001047 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001048
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001049 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001050 if (fc == NULL)
1051 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001052 fc->caller = current_funccal;
1053 current_funccal = fc;
1054 fc->func = fp;
1055 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001056 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001057 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001058 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1059 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001060 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001061 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001062 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001063
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001064 if (fp->uf_dfunc_idx >= 0)
1065 {
1066 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001067 save_current_sctx = current_sctx;
1068 current_sctx = fp->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001069
1070 // Execute the compiled function.
1071 call_def_function(fp, argcount, argvars, rettv);
1072 --depth;
1073 current_funccal = fc->caller;
1074
1075 estack_pop();
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001076 current_sctx = save_current_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077 free_funccal(fc);
1078 return;
1079 }
1080
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001081 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1082 islambda = TRUE;
1083
1084 /*
1085 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1086 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1087 * each argument variable and saves a lot of time.
1088 */
1089 /*
1090 * Init l: variables.
1091 */
1092 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1093 if (selfdict != NULL)
1094 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001095 // Set l:self to "selfdict". Use "name" to avoid a warning from
1096 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001097 v = &fc->fixvar[fixvar_idx++].var;
1098 name = v->di_key;
1099 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001100 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001101 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1102 v->di_tv.v_type = VAR_DICT;
1103 v->di_tv.v_lock = 0;
1104 v->di_tv.vval.v_dict = selfdict;
1105 ++selfdict->dv_refcount;
1106 }
1107
1108 /*
1109 * Init a: variables.
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001110 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001111 * Set a:000 to a list with room for the "..." arguments.
1112 */
1113 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
1114 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001115 (varnumber_T)(argcount >= fp->uf_args.ga_len
1116 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001117 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001118 // Use "name" to avoid a warning from some compiler that checks the
1119 // destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001120 v = &fc->fixvar[fixvar_idx++].var;
1121 name = v->di_key;
1122 STRCPY(name, "000");
1123 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1124 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1125 v->di_tv.v_type = VAR_LIST;
1126 v->di_tv.v_lock = VAR_FIXED;
1127 v->di_tv.vval.v_list = &fc->l_varlist;
1128 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
1129 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1130 fc->l_varlist.lv_lock = VAR_FIXED;
1131
1132 /*
1133 * Set a:firstline to "firstline" and a:lastline to "lastline".
1134 * Set a:name to named arguments.
1135 * Set a:N to the "..." arguments.
1136 */
1137 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
1138 (varnumber_T)firstline);
1139 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
1140 (varnumber_T)lastline);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001141 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001142 {
1143 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001144 typval_T def_rettv;
1145 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001146
1147 ai = i - fp->uf_args.ga_len;
1148 if (ai < 0)
1149 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001150 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001151 name = FUNCARG(fp, i);
1152 if (islambda)
1153 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001154
1155 // evaluate named argument default expression
1156 isdefault = ai + fp->uf_def_args.ga_len >= 0
1157 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1158 && argvars[i].vval.v_number == VVAL_NONE));
1159 if (isdefault)
1160 {
1161 char_u *default_expr = NULL;
1162 def_rettv.v_type = VAR_NUMBER;
1163 def_rettv.vval.v_number = -1;
1164
1165 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1166 [ai + fp->uf_def_args.ga_len];
1167 if (eval1(&default_expr, &def_rettv, TRUE) == FAIL)
1168 {
1169 default_arg_err = 1;
1170 break;
1171 }
1172 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001173 }
1174 else
1175 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001176 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001177 sprintf((char *)numbuf, "%d", ai + 1);
1178 name = numbuf;
1179 }
1180 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1181 {
1182 v = &fc->fixvar[fixvar_idx++].var;
1183 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001184 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001185 }
1186 else
1187 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001188 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001189 if (v == NULL)
1190 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001191 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001192 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001193
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001194 // Note: the values are copied directly to avoid alloc/free.
1195 // "argvars" must have VAR_FIXED for v_lock.
1196 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001197 v->di_tv.v_lock = VAR_FIXED;
1198
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001199 if (addlocal)
1200 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001201 // Named arguments should be accessed without the "a:" prefix in
1202 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001203 copy_tv(&v->di_tv, &v->di_tv);
1204 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001205 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001206 else
1207 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001208
1209 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1210 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001211 listitem_T *li = &fc->l_listitems[ai];
1212
1213 li->li_tv = argvars[i];
1214 li->li_tv.v_lock = VAR_FIXED;
1215 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001216 }
1217 }
1218
Bram Moolenaare38eab22019-12-05 21:50:01 +01001219 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001220 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001221
1222 if (fp->uf_flags & FC_SANDBOX)
1223 {
1224 using_sandbox = TRUE;
1225 ++sandbox;
1226 }
1227
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001228 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001229 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001230 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001231 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001232 ++no_wait_return;
1233 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001234
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001235 smsg(_("calling %s"), SOURCING_NAME);
1236 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001237 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001238 char_u buf[MSG_BUF_LEN];
1239 char_u numbuf2[NUMBUFLEN];
1240 char_u *tofree;
1241 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001242
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001243 msg_puts("(");
1244 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001245 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001246 if (i > 0)
1247 msg_puts(", ");
1248 if (argvars[i].v_type == VAR_NUMBER)
1249 msg_outnum((long)argvars[i].vval.v_number);
1250 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001251 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001252 // Do not want errors such as E724 here.
1253 ++emsg_off;
1254 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1255 --emsg_off;
1256 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001257 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001258 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001259 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001260 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1261 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001262 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001263 msg_puts((char *)s);
1264 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001265 }
1266 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001267 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001268 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001269 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001270 msg_puts("\n"); // don't overwrite this either
1271
1272 verbose_leave_scroll();
1273 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001274 }
1275#ifdef FEAT_PROFILE
1276 if (do_profiling == PROF_YES)
1277 {
1278 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001279 {
1280 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001281 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001282 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001283 if (fp->uf_profiling
1284 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1285 {
1286 ++fp->uf_tm_count;
1287 profile_start(&call_start);
1288 profile_zero(&fp->uf_tm_children);
1289 }
1290 script_prof_save(&wait_start);
1291 }
1292#endif
1293
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001294 save_current_sctx = current_sctx;
1295 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001296 save_did_emsg = did_emsg;
1297 did_emsg = FALSE;
1298
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001299 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1300 did_emsg = TRUE;
1301 else
1302 // call do_cmdline() to execute the lines
1303 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001304 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1305
1306 --RedrawingDisabled;
1307
Bram Moolenaare38eab22019-12-05 21:50:01 +01001308 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001309 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1310 {
1311 clear_tv(rettv);
1312 rettv->v_type = VAR_NUMBER;
1313 rettv->vval.v_number = -1;
1314 }
1315
1316#ifdef FEAT_PROFILE
1317 if (do_profiling == PROF_YES && (fp->uf_profiling
1318 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1319 {
1320 profile_end(&call_start);
1321 profile_sub_wait(&wait_start, &call_start);
1322 profile_add(&fp->uf_tm_total, &call_start);
1323 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1324 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1325 {
1326 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1327 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1328 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001329 if (started_profiling)
1330 // make a ":profdel func" stop profiling the function
1331 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001332 }
1333#endif
1334
Bram Moolenaare38eab22019-12-05 21:50:01 +01001335 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001336 if (p_verbose >= 12)
1337 {
1338 ++no_wait_return;
1339 verbose_enter_scroll();
1340
1341 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001342 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001343 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001344 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001345 (long)fc->rettv->vval.v_number);
1346 else
1347 {
1348 char_u buf[MSG_BUF_LEN];
1349 char_u numbuf2[NUMBUFLEN];
1350 char_u *tofree;
1351 char_u *s;
1352
Bram Moolenaare38eab22019-12-05 21:50:01 +01001353 // The value may be very long. Skip the middle part, so that we
1354 // have some idea how it starts and ends. smsg() would always
1355 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001356 ++emsg_off;
1357 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1358 --emsg_off;
1359 if (s != NULL)
1360 {
1361 if (vim_strsize(s) > MSG_BUF_CLEN)
1362 {
1363 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1364 s = buf;
1365 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001366 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001367 vim_free(tofree);
1368 }
1369 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001370 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001371
1372 verbose_leave_scroll();
1373 --no_wait_return;
1374 }
1375
Bram Moolenaare31ee862020-01-07 20:59:34 +01001376 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001377 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001378 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001379#ifdef FEAT_PROFILE
1380 if (do_profiling == PROF_YES)
1381 script_prof_restore(&wait_start);
1382#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001383 if (using_sandbox)
1384 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001385
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001386 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001387 {
1388 ++no_wait_return;
1389 verbose_enter_scroll();
1390
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001391 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001392 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001393
1394 verbose_leave_scroll();
1395 --no_wait_return;
1396 }
1397
1398 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001399 --depth;
1400
Bram Moolenaar6914c642017-04-01 21:21:30 +02001401 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001402}
1403
1404/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001406 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 int
1408call_user_func_check(
1409 ufunc_T *fp,
1410 int argcount,
1411 typval_T *argvars,
1412 typval_T *rettv,
1413 funcexe_T *funcexe,
1414 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001415{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416 int error;
1417 int regular_args = fp->uf_args.ga_len;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001419 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1420 *funcexe->doesrange = TRUE;
1421 if (argcount < regular_args - fp->uf_def_args.ga_len)
1422 error = FCERR_TOOFEW;
1423 else if (!has_varargs(fp) && argcount > regular_args)
1424 error = FCERR_TOOMANY;
1425 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1426 error = FCERR_DICT;
1427 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001428 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429 int did_save_redo = FALSE;
1430 save_redo_T save_redo;
1431
1432 /*
1433 * Call the user function.
1434 * Save and restore search patterns, script variables and
1435 * redo buffer.
1436 */
1437 save_search_patterns();
1438 if (!ins_compl_active())
1439 {
1440 saveRedobuff(&save_redo);
1441 did_save_redo = TRUE;
1442 }
1443 ++fp->uf_calls;
1444 call_user_func(fp, argcount, argvars, rettv,
1445 funcexe->firstline, funcexe->lastline,
1446 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
1447 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
1448 // Function was unreferenced while being used, free it now.
1449 func_clear_free(fp, FALSE);
1450 if (did_save_redo)
1451 restoreRedobuff(&save_redo);
1452 restore_search_patterns();
1453 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001454 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001456}
1457
1458/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001459 * There are two kinds of function names:
1460 * 1. ordinary names, function defined with :function
1461 * 2. numbered functions and lambdas
1462 * For the first we only count the name stored in func_hashtab as a reference,
1463 * using function() does not count as a reference, because the function is
1464 * looked up by name.
1465 */
1466 static int
1467func_name_refcount(char_u *name)
1468{
1469 return isdigit(*name) || *name == '<';
1470}
1471
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001472static funccal_entry_T *funccal_stack = NULL;
1473
1474/*
1475 * Save the current function call pointer, and set it to NULL.
1476 * Used when executing autocommands and for ":source".
1477 */
1478 void
1479save_funccal(funccal_entry_T *entry)
1480{
1481 entry->top_funccal = current_funccal;
1482 entry->next = funccal_stack;
1483 funccal_stack = entry;
1484 current_funccal = NULL;
1485}
1486
1487 void
1488restore_funccal(void)
1489{
1490 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001491 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001492 else
1493 {
1494 current_funccal = funccal_stack->top_funccal;
1495 funccal_stack = funccal_stack->next;
1496 }
1497}
1498
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001499 funccall_T *
1500get_current_funccal(void)
1501{
1502 return current_funccal;
1503}
1504
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001505#if defined(EXITFREE) || defined(PROTO)
1506 void
1507free_all_functions(void)
1508{
1509 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001510 ufunc_T *fp;
1511 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001512 long_u todo = 1;
1513 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001514
Bram Moolenaare38eab22019-12-05 21:50:01 +01001515 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001516 while (current_funccal != NULL)
1517 {
1518 clear_tv(current_funccal->rettv);
1519 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001520 if (current_funccal == NULL && funccal_stack != NULL)
1521 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001522 }
1523
Bram Moolenaare38eab22019-12-05 21:50:01 +01001524 // First clear what the functions contain. Since this may lower the
1525 // reference count of a function, it may also free a function and change
1526 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001527 while (todo > 0)
1528 {
1529 todo = func_hashtab.ht_used;
1530 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1531 if (!HASHITEM_EMPTY(hi))
1532 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001533 // clear the def function index now
1534 fp = HI2UF(hi);
1535 fp->uf_flags &= ~FC_DEAD;
1536 fp->uf_dfunc_idx = -1;
1537
Bram Moolenaare38eab22019-12-05 21:50:01 +01001538 // Only free functions that are not refcounted, those are
1539 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001540 if (func_name_refcount(fp->uf_name))
1541 ++skipped;
1542 else
1543 {
1544 used = func_hashtab.ht_used;
1545 func_clear(fp, TRUE);
1546 if (used != func_hashtab.ht_used)
1547 {
1548 skipped = 0;
1549 break;
1550 }
1551 }
1552 --todo;
1553 }
1554 }
1555
Bram Moolenaare38eab22019-12-05 21:50:01 +01001556 // Now actually free the functions. Need to start all over every time,
1557 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001558 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001559 while (func_hashtab.ht_used > skipped)
1560 {
1561 todo = func_hashtab.ht_used;
1562 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001563 if (!HASHITEM_EMPTY(hi))
1564 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001565 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001566 // Only free functions that are not refcounted, those are
1567 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02001568 fp = HI2UF(hi);
1569 if (func_name_refcount(fp->uf_name))
1570 ++skipped;
1571 else
1572 {
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001573 func_free(fp);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001574 skipped = 0;
1575 break;
1576 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001577 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001578 }
1579 if (skipped == 0)
1580 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581
1582 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001583}
1584#endif
1585
1586/*
1587 * Return TRUE if "name" looks like a builtin function name: starts with a
1588 * lower case letter and doesn't contain AUTOLOAD_CHAR.
1589 * "len" is the length of "name", or -1 for NUL terminated.
1590 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001592builtin_function(char_u *name, int len)
1593{
1594 char_u *p;
1595
1596 if (!ASCII_ISLOWER(name[0]))
1597 return FALSE;
1598 p = vim_strchr(name, AUTOLOAD_CHAR);
1599 return p == NULL || (len > 0 && p > name + len);
1600}
1601
1602 int
1603func_call(
1604 char_u *name,
1605 typval_T *args,
1606 partial_T *partial,
1607 dict_T *selfdict,
1608 typval_T *rettv)
1609{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001610 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001611 listitem_T *item;
1612 typval_T argv[MAX_FUNC_ARGS + 1];
1613 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001614 int r = 0;
1615
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001616 range_list_materialize(l);
1617 for (item = l->lv_first; item != NULL; item = item->li_next)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001618 {
1619 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1620 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001621 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001622 break;
1623 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001624 // Make a copy of each argument. This is needed to be able to set
1625 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001626 copy_tv(&item->li_tv, &argv[argc++]);
1627 }
1628
1629 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001630 {
1631 funcexe_T funcexe;
1632
Bram Moolenaarac92e252019-08-03 21:58:38 +02001633 vim_memset(&funcexe, 0, sizeof(funcexe));
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001634 funcexe.firstline = curwin->w_cursor.lnum;
1635 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001636 funcexe.evaluate = TRUE;
1637 funcexe.partial = partial;
1638 funcexe.selfdict = selfdict;
1639 r = call_func(name, -1, rettv, argc, argv, &funcexe);
1640 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001641
Bram Moolenaare38eab22019-12-05 21:50:01 +01001642 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001643 while (argc > 0)
1644 clear_tv(&argv[--argc]);
1645
1646 return r;
1647}
1648
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001649static int callback_depth = 0;
1650
1651 int
1652get_callback_depth(void)
1653{
1654 return callback_depth;
1655}
1656
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001657/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001658 * Invoke call_func() with a callback.
1659 */
1660 int
1661call_callback(
1662 callback_T *callback,
1663 int len, // length of "name" or -1 to use strlen()
1664 typval_T *rettv, // return value goes here
1665 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001666 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001667 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001668{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001669 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001670 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001671
1672 vim_memset(&funcexe, 0, sizeof(funcexe));
1673 funcexe.evaluate = TRUE;
1674 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001675 ++callback_depth;
1676 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
1677 --callback_depth;
1678 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001679}
1680
1681/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682 * Give an error message for the result of a function.
1683 * Nothing if "error" is FCERR_NONE.
1684 */
1685 void
1686user_func_error(int error, char_u *name)
1687{
1688 switch (error)
1689 {
1690 case FCERR_UNKNOWN:
1691 emsg_funcname(e_unknownfunc, name);
1692 break;
1693 case FCERR_NOTMETHOD:
1694 emsg_funcname(
1695 N_("E276: Cannot use function as a method: %s"), name);
1696 break;
1697 case FCERR_DELETED:
1698 emsg_funcname(N_(e_func_deleted), name);
1699 break;
1700 case FCERR_TOOMANY:
1701 emsg_funcname((char *)e_toomanyarg, name);
1702 break;
1703 case FCERR_TOOFEW:
1704 emsg_funcname((char *)e_toofewarg, name);
1705 break;
1706 case FCERR_SCRIPT:
1707 emsg_funcname(
1708 N_("E120: Using <SID> not in a script context: %s"), name);
1709 break;
1710 case FCERR_DICT:
1711 emsg_funcname(
1712 N_("E725: Calling dict function without Dictionary: %s"),
1713 name);
1714 break;
1715 }
1716}
1717
1718/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001719 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001720 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001721 * Return FAIL when the function can't be called, OK otherwise.
1722 * Also returns OK when an error was encountered while executing the function.
1723 */
1724 int
1725call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001726 char_u *funcname, // name of the function
1727 int len, // length of "name" or -1 to use strlen()
1728 typval_T *rettv, // return value goes here
1729 int argcount_in, // number of "argvars"
1730 typval_T *argvars_in, // vars for arguments, must have "argcount"
1731 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001732 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001733{
1734 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01001735 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001736 int i;
1737 ufunc_T *fp;
1738 char_u fname_buf[FLEN_FIXED + 1];
1739 char_u *tofree = NULL;
1740 char_u *fname;
1741 char_u *name;
1742 int argcount = argcount_in;
1743 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001744 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001745 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
1746 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001747 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001748 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001749 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001750
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02001751 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
1752 // even when call_func() returns FAIL.
1753 rettv->v_type = VAR_UNKNOWN;
1754
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001755 // Make a copy of the name, if it comes from a funcref variable it could
1756 // be changed or deleted in the called function.
1757 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001758 if (name == NULL)
1759 return ret;
1760
1761 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1762
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001763 if (funcexe->doesrange != NULL)
1764 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001765
1766 if (partial != NULL)
1767 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001768 // When the function has a partial with a dict and there is a dict
1769 // argument, use the dict argument. That is backwards compatible.
1770 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001771 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001772 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01001773 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001774 {
1775 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001776 {
1777 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
1778 {
Bram Moolenaaref140542019-12-31 21:27:13 +01001779 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001780 goto theend;
1781 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001782 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001783 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001784 for (i = 0; i < argcount_in; ++i)
1785 argv[i + argv_clear] = argvars_in[i];
1786 argvars = argv;
1787 argcount = partial->pt_argc + argcount_in;
1788 }
1789 }
1790
Bram Moolenaaref140542019-12-31 21:27:13 +01001791 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001792 {
1793 char_u *rfname = fname;
1794
Bram Moolenaare38eab22019-12-05 21:50:01 +01001795 // Ignore "g:" before a function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001796 if (fname[0] == 'g' && fname[1] == ':')
1797 rfname = fname + 2;
1798
Bram Moolenaare38eab22019-12-05 21:50:01 +01001799 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001800 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01001801 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001802
1803 if (!builtin_function(rfname, -1))
1804 {
1805 /*
1806 * User defined function.
1807 */
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001808 if (partial != NULL && partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001809 fp = partial->pt_func;
1810 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001812
Bram Moolenaare38eab22019-12-05 21:50:01 +01001813 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001814 if (fp == NULL
1815 && apply_autocmds(EVENT_FUNCUNDEFINED,
1816 rfname, rfname, TRUE, NULL)
1817 && !aborting())
1818 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001819 // executed an autocommand, search for the function again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001821 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001822 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001823 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
1824 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001825 // loaded a package, search for the function again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001827 }
1828
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001829 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01001830 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001831 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001832 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001833 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01001834 // postponed filling in the arguments, do it now
1835 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001836 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001837
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001838 if (funcexe->basetv != NULL)
1839 {
1840 // Method call: base->Method()
1841 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
1842 argv[0] = *funcexe->basetv;
1843 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001844 argvars = argv;
1845 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001846 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848 error = call_user_func_check(fp, argcount, argvars, rettv,
1849 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001850 }
1851 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001852 else if (funcexe->basetv != NULL)
1853 {
1854 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001855 * expr->method(): Find the method name in the table, call its
1856 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02001857 */
1858 error = call_internal_method(fname, argcount, argvars, rettv,
1859 funcexe->basetv);
1860 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001861 else
1862 {
1863 /*
1864 * Find the function name in the table, call its implementation.
1865 */
1866 error = call_internal_func(fname, argcount, argvars, rettv);
1867 }
1868 /*
1869 * The function call (or "FuncUndefined" autocommand sequence) might
1870 * have been aborted by an error, an interrupt, or an explicitly thrown
1871 * exception that has not been caught so far. This situation can be
1872 * tested for by calling aborting(). For an error in an internal
1873 * function or for the "E132" error in call_user_func(), however, the
1874 * throw point at which the "force_abort" flag (temporarily reset by
1875 * emsg()) is normally updated has not been reached yet. We need to
1876 * update that flag first to make aborting() reliable.
1877 */
1878 update_force_abort();
1879 }
Bram Moolenaaref140542019-12-31 21:27:13 +01001880 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001881 ret = OK;
1882
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001883theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001884 /*
1885 * Report an error unless the argument evaluation or function call has been
1886 * cancelled due to an aborting error, an interrupt, or an exception.
1887 */
1888 if (!aborting())
1889 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001890 user_func_error(error, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001891 }
1892
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001893 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001894 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001895 clear_tv(&argv[--argv_clear + argv_base]);
1896
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001897 vim_free(tofree);
1898 vim_free(name);
1899
1900 return ret;
1901}
1902
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001903 static char_u *
1904printable_func_name(ufunc_T *fp)
1905{
1906 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
1907}
1908
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001909/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001910 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001911 */
1912 static void
1913list_func_head(ufunc_T *fp, int indent)
1914{
1915 int j;
1916
1917 msg_start();
1918 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001919 msg_puts(" ");
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001920 if (fp->uf_dfunc_idx >= 0)
1921 msg_puts("def ");
1922 else
1923 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001925 msg_putchar('(');
1926 for (j = 0; j < fp->uf_args.ga_len; ++j)
1927 {
1928 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001929 msg_puts(", ");
1930 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931 if (fp->uf_arg_types != NULL)
1932 {
1933 char *tofree;
1934
1935 msg_puts(": ");
1936 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
1937 vim_free(tofree);
1938 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001939 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
1940 {
1941 msg_puts(" = ");
1942 msg_puts(((char **)(fp->uf_def_args.ga_data))
1943 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
1944 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001945 }
1946 if (fp->uf_varargs)
1947 {
1948 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001949 msg_puts(", ");
1950 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001951 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 if (fp->uf_va_name != NULL)
1953 {
1954 if (j)
1955 msg_puts(", ");
1956 msg_puts("...");
1957 msg_puts((char *)fp->uf_va_name);
1958 if (fp->uf_va_type)
1959 {
1960 char *tofree;
1961
1962 msg_puts(": ");
1963 msg_puts(type_name(fp->uf_va_type, &tofree));
1964 vim_free(tofree);
1965 }
1966 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001967 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001968
1969 if (fp->uf_dfunc_idx >= 0)
1970 {
1971 if (fp->uf_ret_type != &t_void)
1972 {
1973 char *tofree;
1974
1975 msg_puts(": ");
1976 msg_puts(type_name(fp->uf_ret_type, &tofree));
1977 vim_free(tofree);
1978 }
1979 }
1980 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001981 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001982 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001983 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001984 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001985 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001986 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001987 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001988 msg_clr_eos();
1989 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001990 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001991}
1992
1993/*
1994 * Get a function name, translating "<SID>" and "<SNR>".
1995 * Also handles a Funcref in a List or Dictionary.
1996 * Returns the function name in allocated memory, or NULL for failure.
1997 * flags:
1998 * TFN_INT: internal function name OK
1999 * TFN_QUIET: be quiet
2000 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002001 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002002 * Advances "pp" to just after the function name (if no error).
2003 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002004 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002005trans_function_name(
2006 char_u **pp,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002007 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002008 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002009 funcdict_T *fdp, // return: info about dictionary used
2010 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002011{
2012 char_u *name = NULL;
2013 char_u *start;
2014 char_u *end;
2015 int lead;
2016 char_u sid_buf[20];
2017 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002018 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002019 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020 int vim9script;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002021
2022 if (fdp != NULL)
2023 vim_memset(fdp, 0, sizeof(funcdict_T));
2024 start = *pp;
2025
Bram Moolenaare38eab22019-12-05 21:50:01 +01002026 // Check for hard coded <SNR>: already translated function ID (from a user
2027 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002028 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2029 && (*pp)[2] == (int)KE_SNR)
2030 {
2031 *pp += 3;
2032 len = get_id_len(pp) + 3;
2033 return vim_strnsave(start, len);
2034 }
2035
Bram Moolenaare38eab22019-12-05 21:50:01 +01002036 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2037 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002038 lead = eval_fname_script(start);
2039 if (lead > 2)
2040 start += lead;
2041
Bram Moolenaare38eab22019-12-05 21:50:01 +01002042 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002043 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002044 lead > 2 ? 0 : FNE_CHECK_START);
2045 if (end == start)
2046 {
2047 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002048 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002049 goto theend;
2050 }
2051 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2052 {
2053 /*
2054 * Report an invalid expression in braces, unless the expression
2055 * evaluation has been cancelled due to an aborting error, an
2056 * interrupt, or an exception.
2057 */
2058 if (!aborting())
2059 {
2060 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002061 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002062 }
2063 else
2064 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2065 goto theend;
2066 }
2067
2068 if (lv.ll_tv != NULL)
2069 {
2070 if (fdp != NULL)
2071 {
2072 fdp->fd_dict = lv.ll_dict;
2073 fdp->fd_newkey = lv.ll_newkey;
2074 lv.ll_newkey = NULL;
2075 fdp->fd_di = lv.ll_di;
2076 }
2077 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2078 {
2079 name = vim_strsave(lv.ll_tv->vval.v_string);
2080 *pp = end;
2081 }
2082 else if (lv.ll_tv->v_type == VAR_PARTIAL
2083 && lv.ll_tv->vval.v_partial != NULL)
2084 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002085 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002086 *pp = end;
2087 if (partial != NULL)
2088 *partial = lv.ll_tv->vval.v_partial;
2089 }
2090 else
2091 {
2092 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2093 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002094 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002095 else
2096 *pp = end;
2097 name = NULL;
2098 }
2099 goto theend;
2100 }
2101
2102 if (lv.ll_name == NULL)
2103 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002104 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002105 *pp = end;
2106 goto theend;
2107 }
2108
Bram Moolenaare38eab22019-12-05 21:50:01 +01002109 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002110 if (lv.ll_exp_name != NULL)
2111 {
2112 len = (int)STRLEN(lv.ll_exp_name);
2113 name = deref_func_name(lv.ll_exp_name, &len, partial,
2114 flags & TFN_NO_AUTOLOAD);
2115 if (name == lv.ll_exp_name)
2116 name = NULL;
2117 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002118 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002119 {
2120 len = (int)(end - *pp);
2121 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2122 if (name == *pp)
2123 name = NULL;
2124 }
2125 if (name != NULL)
2126 {
2127 name = vim_strsave(name);
2128 *pp = end;
2129 if (STRNCMP(name, "<SNR>", 5) == 0)
2130 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002131 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002132 name[0] = K_SPECIAL;
2133 name[1] = KS_EXTRA;
2134 name[2] = (int)KE_SNR;
2135 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2136 }
2137 goto theend;
2138 }
2139
2140 if (lv.ll_exp_name != NULL)
2141 {
2142 len = (int)STRLEN(lv.ll_exp_name);
2143 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2144 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2145 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002146 // When there was "s:" already or the name expanded to get a
2147 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002148 lv.ll_name += 2;
2149 len -= 2;
2150 lead = 2;
2151 }
2152 }
2153 else
2154 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002155 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002156 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
2157 lv.ll_name += 2;
2158 len = (int)(end - lv.ll_name);
2159 }
2160
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002161 // In Vim9 script a user function is script-local by default.
2162 vim9script = ASCII_ISUPPER(*start)
2163 && current_sctx.sc_version == SCRIPT_VERSION_VIM9;
2164
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002165 /*
2166 * Copy the function name to allocated memory.
2167 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2168 * Accept <SNR>123_name() outside a script.
2169 */
2170 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002171 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002172 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002173 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002174 if (!vim9script)
2175 lead = 3;
2176 if (vim9script || (lv.ll_exp_name != NULL
2177 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002178 || eval_fname_sid(*pp))
2179 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002181 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002182 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002183 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002184 goto theend;
2185 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002186 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 if (vim9script)
2188 extra = 3 + (int)STRLEN(sid_buf);
2189 else
2190 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002191 }
2192 }
2193 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2194 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002195 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002196 start);
2197 goto theend;
2198 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002199 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002200 {
2201 char_u *cp = vim_strchr(lv.ll_name, ':');
2202
2203 if (cp != NULL && cp < end)
2204 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002205 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002206 goto theend;
2207 }
2208 }
2209
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002210 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002211 if (name != NULL)
2212 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002213 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002214 {
2215 name[0] = K_SPECIAL;
2216 name[1] = KS_EXTRA;
2217 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002218 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002219 STRCPY(name + 3, sid_buf);
2220 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002221 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2222 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002223 }
2224 *pp = end;
2225
2226theend:
2227 clear_lval(&lv);
2228 return name;
2229}
2230
2231/*
2232 * ":function"
2233 */
2234 void
2235ex_function(exarg_T *eap)
2236{
2237 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002238 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002239 int j;
2240 int c;
2241 int saved_did_emsg;
2242 int saved_wait_return = need_wait_return;
2243 char_u *name = NULL;
2244 char_u *p;
2245 char_u *arg;
2246 char_u *line_arg = NULL;
2247 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002249 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002250 garray_T newlines;
2251 int varargs = FALSE;
2252 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002254 ufunc_T *fp;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002255 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002256 int indent;
2257 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258#define MAX_FUNC_NESTING 50
2259 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002260 dictitem_T *v;
2261 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002262 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002263 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002264 int todo;
2265 hashitem_T *hi;
Bram Moolenaare96a2492019-06-25 04:12:16 +02002266 int do_concat = TRUE;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002267 linenr_T sourcing_lnum_off;
2268 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002269 int is_heredoc = FALSE;
2270 char_u *skip_until = NULL;
2271 char_u *heredoc_trimmed = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002272
2273 /*
2274 * ":function" without argument: list functions.
2275 */
2276 if (ends_excmd(*eap->arg))
2277 {
2278 if (!eap->skip)
2279 {
2280 todo = (int)func_hashtab.ht_used;
2281 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2282 {
2283 if (!HASHITEM_EMPTY(hi))
2284 {
2285 --todo;
2286 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002287 if ((fp->uf_flags & FC_DEAD)
2288 || message_filtered(fp->uf_name))
Bram Moolenaarf86db782018-10-25 13:31:37 +02002289 continue;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002290 if (!func_name_refcount(fp->uf_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002291 list_func_head(fp, FALSE);
2292 }
2293 }
2294 }
2295 eap->nextcmd = check_nextcmd(eap->arg);
2296 return;
2297 }
2298
2299 /*
2300 * ":function /pat": list functions matching pattern.
2301 */
2302 if (*eap->arg == '/')
2303 {
2304 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
2305 if (!eap->skip)
2306 {
2307 regmatch_T regmatch;
2308
2309 c = *p;
2310 *p = NUL;
2311 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2312 *p = c;
2313 if (regmatch.regprog != NULL)
2314 {
2315 regmatch.rm_ic = p_ic;
2316
2317 todo = (int)func_hashtab.ht_used;
2318 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2319 {
2320 if (!HASHITEM_EMPTY(hi))
2321 {
2322 --todo;
2323 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002324 if ((fp->uf_flags & FC_DEAD) == 0
2325 && !isdigit(*fp->uf_name)
2326 && vim_regexec(&regmatch, fp->uf_name, 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002327 list_func_head(fp, FALSE);
2328 }
2329 }
2330 vim_regfree(regmatch.regprog);
2331 }
2332 }
2333 if (*p == '/')
2334 ++p;
2335 eap->nextcmd = check_nextcmd(p);
2336 return;
2337 }
2338
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339 ga_init(&newargs);
2340 ga_init(&argtypes);
2341 ga_init(&default_args);
2342
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002343 /*
2344 * Get the function name. There are these situations:
2345 * func normal function name
2346 * "name" == func, "fudi.fd_dict" == NULL
2347 * dict.func new dictionary entry
2348 * "name" == NULL, "fudi.fd_dict" set,
2349 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
2350 * dict.func existing dict entry with a Funcref
2351 * "name" == func, "fudi.fd_dict" set,
2352 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2353 * dict.func existing dict entry that's not a Funcref
2354 * "name" == NULL, "fudi.fd_dict" set,
2355 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2356 * s:func script-local function name
2357 * g:func global function name, same as "func"
2358 */
2359 p = eap->arg;
Bram Moolenaar3388d332017-12-07 22:23:04 +01002360 name = trans_function_name(&p, eap->skip, TFN_NO_AUTOLOAD, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002361 paren = (vim_strchr(p, '(') != NULL);
2362 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
2363 {
2364 /*
2365 * Return on an invalid expression in braces, unless the expression
2366 * evaluation has been cancelled due to an aborting error, an
2367 * interrupt, or an exception.
2368 */
2369 if (!aborting())
2370 {
2371 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002372 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002373 vim_free(fudi.fd_newkey);
2374 return;
2375 }
2376 else
2377 eap->skip = TRUE;
2378 }
2379
Bram Moolenaare38eab22019-12-05 21:50:01 +01002380 // An error in a function call during evaluation of an expression in magic
2381 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002382 saved_did_emsg = did_emsg;
2383 did_emsg = FALSE;
2384
2385 /*
2386 * ":function func" with only function name: list function.
2387 */
2388 if (!paren)
2389 {
2390 if (!ends_excmd(*skipwhite(p)))
2391 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002392 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002393 goto ret_free;
2394 }
2395 eap->nextcmd = check_nextcmd(p);
2396 if (eap->nextcmd != NULL)
2397 *p = NUL;
2398 if (!eap->skip && !got_int)
2399 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400 fp = find_func(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002401 if (fp != NULL)
2402 {
2403 list_func_head(fp, TRUE);
2404 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2405 {
2406 if (FUNCLINE(fp, j) == NULL)
2407 continue;
2408 msg_putchar('\n');
2409 msg_outnum((long)(j + 1));
2410 if (j < 9)
2411 msg_putchar(' ');
2412 if (j < 99)
2413 msg_putchar(' ');
2414 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002415 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002416 ui_breakcheck();
2417 }
2418 if (!got_int)
2419 {
2420 msg_putchar('\n');
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002421 if (fp->uf_dfunc_idx >= 0)
2422 msg_puts(" enddef");
2423 else
2424 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002425 }
2426 }
2427 else
2428 emsg_funcname(N_("E123: Undefined function: %s"), name);
2429 }
2430 goto ret_free;
2431 }
2432
2433 /*
2434 * ":function name(arg1, arg2)" Define function.
2435 */
2436 p = skipwhite(p);
2437 if (*p != '(')
2438 {
2439 if (!eap->skip)
2440 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002441 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002442 goto ret_free;
2443 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002444 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002445 if (vim_strchr(p, '(') != NULL)
2446 p = vim_strchr(p, '(');
2447 }
2448 p = skipwhite(p + 1);
2449
2450 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2451
2452 if (!eap->skip)
2453 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002454 // Check the name of the function. Unless it's a dictionary function
2455 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002456 if (name != NULL)
2457 arg = name;
2458 else
2459 arg = fudi.fd_newkey;
2460 if (arg != NULL && (fudi.fd_di == NULL
2461 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2462 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2463 {
2464 if (*arg == K_SPECIAL)
2465 j = 3;
2466 else
2467 j = 0;
2468 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2469 : eval_isnamec(arg[j])))
2470 ++j;
2471 if (arg[j] != NUL)
2472 emsg_funcname((char *)e_invarg2, arg);
2473 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002474 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002475 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002476 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002477 }
2478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 if (get_function_args(&p, ')', &newargs,
2480 eap->cmdidx == CMD_def ? &argtypes : NULL,
2481 &varargs, &default_args, eap->skip) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002482 goto errret_2;
2483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002485 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 // find the return type: :def Func(): type
2487 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002488 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489 ret_type = skipwhite(p + 1);
2490 p = skip_type(ret_type);
2491 if (p > ret_type)
2492 p = skipwhite(p);
2493 else
2494 semsg(_("E1056: expected a type: %s"), ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002495 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002496 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 else
2498 // find extra arguments "range", "dict", "abort" and "closure"
2499 for (;;)
2500 {
2501 p = skipwhite(p);
2502 if (STRNCMP(p, "range", 5) == 0)
2503 {
2504 flags |= FC_RANGE;
2505 p += 5;
2506 }
2507 else if (STRNCMP(p, "dict", 4) == 0)
2508 {
2509 flags |= FC_DICT;
2510 p += 4;
2511 }
2512 else if (STRNCMP(p, "abort", 5) == 0)
2513 {
2514 flags |= FC_ABORT;
2515 p += 5;
2516 }
2517 else if (STRNCMP(p, "closure", 7) == 0)
2518 {
2519 flags |= FC_CLOSURE;
2520 p += 7;
2521 if (current_funccal == NULL)
2522 {
2523 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
2524 name == NULL ? (char_u *)"" : name);
2525 goto erret;
2526 }
2527 }
2528 else
2529 break;
2530 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002531
Bram Moolenaare38eab22019-12-05 21:50:01 +01002532 // When there is a line break use what follows for the function body.
2533 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002534 if (*p == '\n')
2535 line_arg = p + 1;
2536 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002537 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002538
2539 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
2541 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002542 */
2543 if (KeyTyped)
2544 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002545 // Check if the function already exists, don't let the user type the
2546 // whole function before telling him it doesn't work! For a script we
2547 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002548 if (!eap->skip && !eap->forceit)
2549 {
2550 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002551 emsg(_(e_funcdict));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 else if (name != NULL && find_func(name, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002553 emsg_funcname(e_funcexts, name);
2554 }
2555
2556 if (!eap->skip && did_emsg)
2557 goto erret;
2558
Bram Moolenaare38eab22019-12-05 21:50:01 +01002559 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002560 cmdline_row = msg_row;
2561 }
2562
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002563 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002564 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002565
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002566 indent = 2;
2567 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002569 for (;;)
2570 {
2571 if (KeyTyped)
2572 {
2573 msg_scroll = TRUE;
2574 saved_wait_return = FALSE;
2575 }
2576 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002577
2578 if (line_arg != NULL)
2579 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002580 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002581 theline = line_arg;
2582 p = vim_strchr(theline, '\n');
2583 if (p == NULL)
2584 line_arg += STRLEN(line_arg);
2585 else
2586 {
2587 *p = NUL;
2588 line_arg = p + 1;
2589 }
2590 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002591 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002592 {
2593 vim_free(line_to_free);
2594 if (eap->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02002595 theline = getcmdline(':', 0L, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002596 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02002597 theline = eap->getline(':', eap->cookie, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002598 line_to_free = theline;
2599 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002600 if (KeyTyped)
2601 lines_left = Rows - 1;
2602 if (theline == NULL)
2603 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 if (eap->cmdidx == CMD_def)
2605 emsg(_("E1057: Missing :enddef"));
2606 else
2607 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002608 goto erret;
2609 }
2610
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002611 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002612 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002613 if (SOURCING_LNUM < sourcing_lnum_off)
2614 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002615 else
2616 sourcing_lnum_off = 0;
2617
2618 if (skip_until != NULL)
2619 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002620 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002621 // * ":append" and "."
2622 // * ":python <<EOF" and "EOF"
2623 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
2624 if (heredoc_trimmed == NULL
2625 || (is_heredoc && skipwhite(theline) == theline)
2626 || STRNCMP(theline, heredoc_trimmed,
2627 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002628 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002629 if (heredoc_trimmed == NULL)
2630 p = theline;
2631 else if (is_heredoc)
2632 p = skipwhite(theline) == theline
2633 ? theline : theline + STRLEN(heredoc_trimmed);
2634 else
2635 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02002636 if (STRCMP(p, skip_until) == 0)
2637 {
2638 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002639 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaare96a2492019-06-25 04:12:16 +02002640 do_concat = TRUE;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002641 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002642 }
2643 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002644 }
2645 else
2646 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002647 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01002648 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002649 ;
2650
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 // Check for "endfunction" or "enddef".
2652 if (checkforcmd(&p, nesting_def[nesting]
2653 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002654 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002655 char_u *nextcmd = NULL;
2656
Bram Moolenaar663bb232017-06-22 19:12:10 +02002657 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002658 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002659 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002660 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002661 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 give_warning2(eap->cmdidx == CMD_def
2663 ? (char_u *)_("W1001: Text found after :enddef: %s")
2664 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002665 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002666 if (nextcmd != NULL)
2667 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002668 // Another command follows. If the line came from "eap" we
2669 // can simply point into it, otherwise we need to change
2670 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02002671 eap->nextcmd = nextcmd;
2672 if (line_to_free != NULL)
2673 {
2674 vim_free(*eap->cmdlinep);
2675 *eap->cmdlinep = line_to_free;
2676 line_to_free = NULL;
2677 }
2678 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002679 break;
2680 }
2681
Bram Moolenaare38eab22019-12-05 21:50:01 +01002682 // Increase indent inside "if", "while", "for" and "try", decrease
2683 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002685 indent -= 2;
2686 else if (STRNCMP(p, "if", 2) == 0
2687 || STRNCMP(p, "wh", 2) == 0
2688 || STRNCMP(p, "for", 3) == 0
2689 || STRNCMP(p, "try", 3) == 0)
2690 indent += 2;
2691
Bram Moolenaare38eab22019-12-05 21:50:01 +01002692 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01002693 // Only recognize "def" inside "def", not inside "function",
2694 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695 c = *p;
Bram Moolenaar673660a2020-01-26 16:50:05 +01002696 if (checkforcmd(&p, "function", 2)
2697 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002698 {
2699 if (*p == '!')
2700 p = skipwhite(p + 1);
2701 p += eval_fname_script(p);
2702 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
2703 if (*skipwhite(p) == '(')
2704 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705 if (nesting == MAX_FUNC_NESTING - 1)
2706 emsg(_("E1058: function nesting too deep"));
2707 else
2708 {
2709 ++nesting;
2710 nesting_def[nesting] = (c == 'd');
2711 indent += 2;
2712 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002713 }
2714 }
2715
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002716 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002717 p = skip_range(p, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002718 if (eap->cmdidx != CMD_def
2719 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002720 || (p[0] == 'c'
2721 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
2722 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
2723 && (STRNCMP(&p[3], "nge", 3) != 0
2724 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002725 || (p[0] == 'i'
2726 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002727 && (!ASCII_ISALPHA(p[2])
2728 || (p[2] == 's'
2729 && (!ASCII_ISALPHA(p[3])
2730 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002731 skip_until = vim_strsave((char_u *)".");
2732
Bram Moolenaare38eab22019-12-05 21:50:01 +01002733 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002734 arg = skipwhite(skiptowhite(p));
2735 if (arg[0] == '<' && arg[1] =='<'
2736 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01002737 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
2738 || ((p[2] == '3' || p[2] == 'x')
2739 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002740 || (p[0] == 'p' && p[1] == 'e'
2741 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
2742 || (p[0] == 't' && p[1] == 'c'
2743 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
2744 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
2745 && !ASCII_ISALPHA(p[3]))
2746 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
2747 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
2748 || (p[0] == 'm' && p[1] == 'z'
2749 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
2750 ))
2751 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002752 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002753 p = skipwhite(arg + 2);
2754 if (*p == NUL)
2755 skip_until = vim_strsave((char_u *)".");
2756 else
2757 skip_until = vim_strsave(p);
2758 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002759
2760 // Check for ":let v =<< [trim] EOF"
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002761 // and ":let [a, b] =<< [trim] EOF"
Bram Moolenaar8471e572019-05-19 21:37:18 +02002762 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002763 if (*arg == '[')
2764 arg = vim_strchr(arg, ']');
2765 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002766 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002767 arg = skipwhite(skiptowhite(arg));
2768 if ( arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
2769 && ((p[0] == 'l'
2770 && p[1] == 'e'
2771 && (!ASCII_ISALNUM(p[2])
2772 || (p[2] == 't' && !ASCII_ISALNUM(p[3]))))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02002773 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002774 p = skipwhite(arg + 3);
2775 if (STRNCMP(p, "trim", 4) == 0)
2776 {
2777 // Ignore leading white space.
2778 p = skipwhite(p + 4);
2779 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar8471e572019-05-19 21:37:18 +02002780 (int)(skipwhite(theline) - theline));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002781 }
2782 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p));
2783 do_concat = FALSE;
2784 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002785 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002786 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002787 }
2788
Bram Moolenaare38eab22019-12-05 21:50:01 +01002789 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002790 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002791 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002792
Bram Moolenaare38eab22019-12-05 21:50:01 +01002793 // Copy the line to newly allocated memory. get_one_sourceline()
2794 // allocates 250 bytes per line, this saves 80% on average. The cost
2795 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002796 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002797 if (p == NULL)
2798 goto erret;
2799 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002800
Bram Moolenaare38eab22019-12-05 21:50:01 +01002801 // Add NULL lines for continuation lines, so that the line count is
2802 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002803 while (sourcing_lnum_off-- > 0)
2804 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
2805
Bram Moolenaare38eab22019-12-05 21:50:01 +01002806 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002807 if (line_arg != NULL && *line_arg == NUL)
2808 line_arg = NULL;
2809 }
2810
Bram Moolenaare38eab22019-12-05 21:50:01 +01002811 // Don't define the function when skipping commands or when an error was
2812 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002813 if (eap->skip || did_emsg)
2814 goto erret;
2815
2816 /*
2817 * If there are no errors, add the function
2818 */
2819 if (fudi.fd_dict == NULL)
2820 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 hashtab_T *ht;
2822
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002823 v = find_var(name, &ht, FALSE);
2824 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
2825 {
2826 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
2827 name);
2828 goto erret;
2829 }
2830
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 fp = find_func_even_dead(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002832 if (fp != NULL)
2833 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834 int dead = fp->uf_flags & FC_DEAD;
2835
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002836 // Function can be replaced with "function!" and when sourcing the
2837 // same script again, but only once.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 if (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002839 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
2840 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002841 {
2842 emsg_funcname(e_funcexts, name);
2843 goto erret;
2844 }
2845 if (fp->uf_calls > 0)
2846 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002847 emsg_funcname(
2848 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002849 name);
2850 goto erret;
2851 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002852 if (fp->uf_refcount > 1)
2853 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002854 // This function is referenced somewhere, don't redefine it but
2855 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002856 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002857 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002858 fp = NULL;
2859 overwrite = TRUE;
2860 }
2861 else
2862 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002863 char_u *exp_name = fp->uf_name_exp;
2864
2865 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01002866 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002867 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002868 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002869 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002871#ifdef FEAT_PROFILE
2872 fp->uf_profiling = FALSE;
2873 fp->uf_prof_initialized = FALSE;
2874#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002875 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002876 }
2877 }
2878 else
2879 {
2880 char numbuf[20];
2881
2882 fp = NULL;
2883 if (fudi.fd_newkey == NULL && !eap->forceit)
2884 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002885 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002886 goto erret;
2887 }
2888 if (fudi.fd_di == NULL)
2889 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002890 // Can't add a function to a locked dictionary
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002891 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002892 goto erret;
2893 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002894 // Can't change an existing function if it is locked
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002895 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002896 goto erret;
2897
Bram Moolenaare38eab22019-12-05 21:50:01 +01002898 // Give the function a sequential number. Can only be used with a
2899 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002900 vim_free(name);
2901 sprintf(numbuf, "%d", ++func_nr);
2902 name = vim_strsave((char_u *)numbuf);
2903 if (name == NULL)
2904 goto erret;
2905 }
2906
2907 if (fp == NULL)
2908 {
2909 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
2910 {
2911 int slen, plen;
2912 char_u *scriptname;
2913
Bram Moolenaare38eab22019-12-05 21:50:01 +01002914 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002915 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002916 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002917 {
2918 scriptname = autoload_name(name);
2919 if (scriptname != NULL)
2920 {
2921 p = vim_strchr(scriptname, '/');
2922 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002923 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002924 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002925 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002926 j = OK;
2927 vim_free(scriptname);
2928 }
2929 }
2930 if (j == FAIL)
2931 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002932 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002933 goto erret;
2934 }
2935 }
2936
Bram Moolenaar47ed5532019-08-08 20:49:14 +02002937 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002938 if (fp == NULL)
2939 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 fp->uf_dfunc_idx = -1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002941
2942 if (fudi.fd_dict != NULL)
2943 {
2944 if (fudi.fd_di == NULL)
2945 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002946 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002947 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
2948 if (fudi.fd_di == NULL)
2949 {
2950 vim_free(fp);
2951 goto erret;
2952 }
2953 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
2954 {
2955 vim_free(fudi.fd_di);
2956 vim_free(fp);
2957 goto erret;
2958 }
2959 }
2960 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01002961 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002962 clear_tv(&fudi.fd_di->di_tv);
2963 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002964 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002965
Bram Moolenaare38eab22019-12-05 21:50:01 +01002966 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002967 flags |= FC_DICT;
2968 }
2969
Bram Moolenaare38eab22019-12-05 21:50:01 +01002970 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002971 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002972 if (overwrite)
2973 {
2974 hi = hash_find(&func_hashtab, name);
2975 hi->hi_key = UF2HIKEY(fp);
2976 }
2977 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002978 {
2979 vim_free(fp);
2980 goto erret;
2981 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002982 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002983 }
2984 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002985 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 fp->uf_ret_type = &t_any;
2987
2988 if (eap->cmdidx == CMD_def)
2989 {
Bram Moolenaarbfe12042020-02-04 21:54:07 +01002990 int lnum_save = SOURCING_LNUM;
2991
2992 // error messages are for the first function line
2993 SOURCING_LNUM = sourcing_lnum_top;
2994
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 // parse the argument types
2996 ga_init2(&fp->uf_type_list, sizeof(type_T), 5);
2997
2998 if (argtypes.ga_len > 0)
2999 {
3000 // When "varargs" is set the last name/type goes into uf_va_name
3001 // and uf_va_type.
3002 int len = argtypes.ga_len - (varargs ? 1 : 0);
3003
3004 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
3005 if (fp->uf_arg_types != NULL)
3006 {
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003007 int i;
3008 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009
3010 for (i = 0; i < len; ++ i)
3011 {
3012 p = ((char_u **)argtypes.ga_data)[i];
3013 if (p == NULL)
3014 // todo: get type from default value
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003015 type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 else
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003017 type = parse_type(&p, &fp->uf_type_list);
3018 if (type == NULL)
3019 {
3020 SOURCING_LNUM = lnum_save;
3021 goto errret_2;
3022 }
3023 fp->uf_arg_types[i] = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 }
3025 }
3026 if (varargs)
3027 {
3028 // Move the last argument "...name: type" to uf_va_name and
3029 // uf_va_type.
3030 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
3031 [fp->uf_args.ga_len - 1];
3032 --fp->uf_args.ga_len;
3033 p = ((char_u **)argtypes.ga_data)[len];
3034 if (p == NULL)
3035 // todo: get type from default value
3036 fp->uf_va_type = &t_any;
3037 else
3038 fp->uf_va_type = parse_type(&p, &fp->uf_type_list);
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003039 if (fp->uf_va_type == NULL)
3040 {
3041 SOURCING_LNUM = lnum_save;
3042 goto errret_2;
3043 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 }
3045 varargs = FALSE;
3046 }
3047
3048 // parse the return type, if any
3049 if (ret_type == NULL)
3050 fp->uf_ret_type = &t_void;
3051 else
3052 {
3053 p = ret_type;
3054 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list);
3055 }
3056 }
3057
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003058 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003059 if ((flags & FC_CLOSURE) != 0)
3060 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003061 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003062 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003063 }
3064 else
3065 fp->uf_scoped = NULL;
3066
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003067#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003068 if (prof_def_func())
3069 func_do_profile(fp);
3070#endif
3071 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003072 if (sandbox)
3073 flags |= FC_SANDBOX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003074 fp->uf_flags = flags;
3075 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003076 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003077 fp->uf_script_ctx = current_sctx;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003078 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 if (is_export)
3080 {
3081 fp->uf_flags |= FC_EXPORT;
3082 // let ex_export() know the export worked.
3083 is_export = FALSE;
3084 }
3085
3086 // ":def Func()" needs to be compiled
3087 if (eap->cmdidx == CMD_def)
3088 compile_def_function(fp, FALSE);
3089
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003090 goto ret_free;
3091
3092erret:
3093 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003094 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003095errret_2:
3096 ga_clear_strings(&newlines);
3097ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003098 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003099 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003100 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003101 vim_free(fudi.fd_newkey);
3102 vim_free(name);
3103 did_emsg |= saved_did_emsg;
3104 need_wait_return |= saved_wait_return;
3105}
3106
3107/*
3108 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3109 * Return 2 if "p" starts with "s:".
3110 * Return 0 otherwise.
3111 */
3112 int
3113eval_fname_script(char_u *p)
3114{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003115 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3116 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003117 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3118 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3119 return 5;
3120 if (p[0] == 's' && p[1] == ':')
3121 return 2;
3122 return 0;
3123}
3124
3125 int
3126translated_function_exists(char_u *name)
3127{
3128 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003129 return has_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 return find_func(name, NULL) != NULL;
3131}
3132
3133/*
3134 * Return TRUE when "ufunc" has old-style "..." varargs
3135 * or named varargs "...name: type".
3136 */
3137 int
3138has_varargs(ufunc_T *ufunc)
3139{
3140 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003141}
3142
3143/*
3144 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003145 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003146 */
3147 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003148function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003149{
3150 char_u *nm = name;
3151 char_u *p;
3152 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003153 int flag;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003154
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003155 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3156 if (no_deref)
3157 flag |= TFN_NO_DEREF;
3158 p = trans_function_name(&nm, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003159 nm = skipwhite(nm);
3160
Bram Moolenaare38eab22019-12-05 21:50:01 +01003161 // Only accept "funcname", "funcname ", "funcname (..." and
3162 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003163 if (p != NULL && (*nm == NUL || *nm == '('))
3164 n = translated_function_exists(p);
3165 vim_free(p);
3166 return n;
3167}
3168
Bram Moolenaar113e1072019-01-20 15:30:40 +01003169#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003170 char_u *
3171get_expanded_name(char_u *name, int check)
3172{
3173 char_u *nm = name;
3174 char_u *p;
3175
3176 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
3177
3178 if (p != NULL && *nm == NUL)
3179 if (!check || translated_function_exists(p))
3180 return p;
3181
3182 vim_free(p);
3183 return NULL;
3184}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003185#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003186
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003187/*
3188 * Function given to ExpandGeneric() to obtain the list of user defined
3189 * function names.
3190 */
3191 char_u *
3192get_user_func_name(expand_T *xp, int idx)
3193{
3194 static long_u done;
3195 static hashitem_T *hi;
3196 ufunc_T *fp;
3197
3198 if (idx == 0)
3199 {
3200 done = 0;
3201 hi = func_hashtab.ht_array;
3202 }
3203 if (done < func_hashtab.ht_used)
3204 {
3205 if (done++ > 0)
3206 ++hi;
3207 while (HASHITEM_EMPTY(hi))
3208 ++hi;
3209 fp = HI2UF(hi);
3210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 // don't show dead, dict and lambda functions
3212 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02003213 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003215
3216 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003217 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003218
3219 cat_func_name(IObuff, fp);
3220 if (xp->xp_context != EXPAND_USER_FUNC)
3221 {
3222 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003224 STRCAT(IObuff, ")");
3225 }
3226 return IObuff;
3227 }
3228 return NULL;
3229}
3230
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003231/*
3232 * ":delfunction {name}"
3233 */
3234 void
3235ex_delfunction(exarg_T *eap)
3236{
3237 ufunc_T *fp = NULL;
3238 char_u *p;
3239 char_u *name;
3240 funcdict_T fudi;
3241
3242 p = eap->arg;
3243 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
3244 vim_free(fudi.fd_newkey);
3245 if (name == NULL)
3246 {
3247 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003248 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003249 return;
3250 }
3251 if (!ends_excmd(*skipwhite(p)))
3252 {
3253 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003254 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003255 return;
3256 }
3257 eap->nextcmd = check_nextcmd(p);
3258 if (eap->nextcmd != NULL)
3259 *p = NUL;
3260
3261 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 fp = find_func(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003263 vim_free(name);
3264
3265 if (!eap->skip)
3266 {
3267 if (fp == NULL)
3268 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02003269 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003270 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003271 return;
3272 }
3273 if (fp->uf_calls > 0)
3274 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003275 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003276 return;
3277 }
3278
3279 if (fudi.fd_dict != NULL)
3280 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003281 // Delete the dict item that refers to the function, it will
3282 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003283 dictitem_remove(fudi.fd_dict, fudi.fd_di);
3284 }
3285 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003286 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003287 // A normal function (not a numbered function or lambda) has a
3288 // refcount of 1 for the entry in the hashtable. When deleting
3289 // it and the refcount is more than one, it should be kept.
3290 // A numbered function and lambda should be kept if the refcount is
3291 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003292 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003293 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003294 // Function is still referenced somewhere. Don't free it but
3295 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003296 if (func_remove(fp))
3297 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003298 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003299 }
3300 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003301 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003302 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003303 }
3304}
3305
3306/*
3307 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003308 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003309 */
3310 void
3311func_unref(char_u *name)
3312{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003313 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003314
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003315 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003316 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 fp = find_func(name, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003318 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003319 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003320#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003321 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003322#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01003323 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003324 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003325 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003326 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003327 // Only delete it when it's not being used. Otherwise it's done
3328 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003329 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003330 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02003331 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003332}
3333
3334/*
3335 * Unreference a Function: decrement the reference count and free it when it
3336 * becomes zero.
3337 */
3338 void
3339func_ptr_unref(ufunc_T *fp)
3340{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003341 if (fp != NULL && --fp->uf_refcount <= 0)
3342 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003343 // Only delete it when it's not being used. Otherwise it's done
3344 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02003345 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003346 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003347 }
3348}
3349
3350/*
3351 * Count a reference to a Function.
3352 */
3353 void
3354func_ref(char_u *name)
3355{
3356 ufunc_T *fp;
3357
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003358 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003359 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 fp = find_func(name, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003361 if (fp != NULL)
3362 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003363 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01003364 // Only give an error for a numbered function.
3365 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01003366 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003367}
3368
3369/*
3370 * Count a reference to a Function.
3371 */
3372 void
3373func_ptr_ref(ufunc_T *fp)
3374{
3375 if (fp != NULL)
3376 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003377}
3378
3379/*
3380 * Return TRUE if items in "fc" do not have "copyID". That means they are not
3381 * referenced from anywhere that is in use.
3382 */
3383 static int
3384can_free_funccal(funccall_T *fc, int copyID)
3385{
3386 return (fc->l_varlist.lv_copyID != copyID
3387 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003388 && fc->l_avars.dv_copyID != copyID
3389 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003390}
3391
3392/*
3393 * ":return [expr]"
3394 */
3395 void
3396ex_return(exarg_T *eap)
3397{
3398 char_u *arg = eap->arg;
3399 typval_T rettv;
3400 int returning = FALSE;
3401
3402 if (current_funccal == NULL)
3403 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003404 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003405 return;
3406 }
3407
3408 if (eap->skip)
3409 ++emsg_skip;
3410
3411 eap->nextcmd = NULL;
3412 if ((*arg != NUL && *arg != '|' && *arg != '\n')
3413 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
3414 {
3415 if (!eap->skip)
3416 returning = do_return(eap, FALSE, TRUE, &rettv);
3417 else
3418 clear_tv(&rettv);
3419 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003420 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003421 else if (!eap->skip)
3422 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003423 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003424 update_force_abort();
3425
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003426 /*
3427 * Return unless the expression evaluation has been cancelled due to an
3428 * aborting error, an interrupt, or an exception.
3429 */
3430 if (!aborting())
3431 returning = do_return(eap, FALSE, TRUE, NULL);
3432 }
3433
Bram Moolenaare38eab22019-12-05 21:50:01 +01003434 // When skipping or the return gets pending, advance to the next command
3435 // in this line (!returning). Otherwise, ignore the rest of the line.
3436 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003437 if (returning)
3438 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003439 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003440 eap->nextcmd = check_nextcmd(arg);
3441
3442 if (eap->skip)
3443 --emsg_skip;
3444}
3445
3446/*
3447 * ":1,25call func(arg1, arg2)" function call.
3448 */
3449 void
3450ex_call(exarg_T *eap)
3451{
3452 char_u *arg = eap->arg;
3453 char_u *startarg;
3454 char_u *name;
3455 char_u *tofree;
3456 int len;
3457 typval_T rettv;
3458 linenr_T lnum;
3459 int doesrange;
3460 int failed = FALSE;
3461 funcdict_T fudi;
3462 partial_T *partial = NULL;
3463
3464 if (eap->skip)
3465 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003466 // trans_function_name() doesn't work well when skipping, use eval0()
3467 // instead to skip to any following command, e.g. for:
3468 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003469 ++emsg_skip;
3470 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3471 clear_tv(&rettv);
3472 --emsg_skip;
3473 return;
3474 }
3475
3476 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
3477 if (fudi.fd_newkey != NULL)
3478 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003479 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003480 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003481 vim_free(fudi.fd_newkey);
3482 }
3483 if (tofree == NULL)
3484 return;
3485
Bram Moolenaare38eab22019-12-05 21:50:01 +01003486 // Increase refcount on dictionary, it could get deleted when evaluating
3487 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003488 if (fudi.fd_dict != NULL)
3489 ++fudi.fd_dict->dv_refcount;
3490
Bram Moolenaare38eab22019-12-05 21:50:01 +01003491 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3492 // contents. For VAR_PARTIAL get its partial, unless we already have one
3493 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003494 len = (int)STRLEN(tofree);
3495 name = deref_func_name(tofree, &len,
3496 partial != NULL ? NULL : &partial, FALSE);
3497
Bram Moolenaare38eab22019-12-05 21:50:01 +01003498 // Skip white space to allow ":call func ()". Not good, but required for
3499 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003500 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003501 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003502
3503 if (*startarg != '(')
3504 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003505 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003506 goto end;
3507 }
3508
3509 /*
3510 * When skipping, evaluate the function once, to find the end of the
3511 * arguments.
3512 * When the function takes a range, this is discovered after the first
3513 * call, and the loop is broken.
3514 */
3515 if (eap->skip)
3516 {
3517 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003518 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003519 }
3520 else
3521 lnum = eap->line1;
3522 for ( ; lnum <= eap->line2; ++lnum)
3523 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003524 funcexe_T funcexe;
3525
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003526 if (!eap->skip && eap->addr_count > 0)
3527 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003528 if (lnum > curbuf->b_ml.ml_line_count)
3529 {
3530 // If the function deleted lines or switched to another buffer
3531 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003532 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003533 break;
3534 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003535 curwin->w_cursor.lnum = lnum;
3536 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003537 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003538 }
3539 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003540
Bram Moolenaarac92e252019-08-03 21:58:38 +02003541 vim_memset(&funcexe, 0, sizeof(funcexe));
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003542 funcexe.firstline = eap->line1;
3543 funcexe.lastline = eap->line2;
3544 funcexe.doesrange = &doesrange;
3545 funcexe.evaluate = !eap->skip;
3546 funcexe.partial = partial;
3547 funcexe.selfdict = fudi.fd_dict;
3548 if (get_func_tv(name, -1, &rettv, &arg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003549 {
3550 failed = TRUE;
3551 break;
3552 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003553 if (has_watchexpr())
3554 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003555
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003556 // Handle a function returning a Funcref, Dictionary or List.
3557 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE,
3558 name, &name) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003559 {
3560 failed = TRUE;
3561 break;
3562 }
3563
3564 clear_tv(&rettv);
3565 if (doesrange || eap->skip)
3566 break;
3567
Bram Moolenaare38eab22019-12-05 21:50:01 +01003568 // Stop when immediately aborting on error, or when an interrupt
3569 // occurred or an exception was thrown but not caught.
3570 // get_func_tv() returned OK, so that the check for trailing
3571 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003572 if (aborting())
3573 break;
3574 }
3575 if (eap->skip)
3576 --emsg_skip;
3577
Bram Moolenaare51bb172020-02-16 19:42:23 +01003578 // When inside :try we need to check for following "| catch".
3579 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003580 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003581 // Check for trailing illegal characters and a following command.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003582 if (!ends_excmd(*arg))
3583 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01003584 if (!failed)
3585 {
3586 emsg_severe = TRUE;
3587 emsg(_(e_trailing));
3588 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003589 }
3590 else
3591 eap->nextcmd = check_nextcmd(arg);
3592 }
3593
3594end:
3595 dict_unref(fudi.fd_dict);
3596 vim_free(tofree);
3597}
3598
3599/*
3600 * Return from a function. Possibly makes the return pending. Also called
3601 * for a pending return at the ":endtry" or after returning from an extra
3602 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3603 * when called due to a ":return" command. "rettv" may point to a typval_T
3604 * with the return rettv. Returns TRUE when the return can be carried out,
3605 * FALSE when the return gets pending.
3606 */
3607 int
3608do_return(
3609 exarg_T *eap,
3610 int reanimate,
3611 int is_cmd,
3612 void *rettv)
3613{
3614 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01003615 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003616
3617 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003618 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003619 current_funccal->returned = FALSE;
3620
3621 /*
3622 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3623 * not in its finally clause (which then is to be executed next) is found.
3624 * In this case, make the ":return" pending for execution at the ":endtry".
3625 * Otherwise, return normally.
3626 */
3627 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3628 if (idx >= 0)
3629 {
3630 cstack->cs_pending[idx] = CSTP_RETURN;
3631
3632 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003633 // A pending return again gets pending. "rettv" points to an
3634 // allocated variable with the rettv of the original ":return"'s
3635 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003636 cstack->cs_rettv[idx] = rettv;
3637 else
3638 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003639 // When undoing a return in order to make it pending, get the stored
3640 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003641 if (reanimate)
3642 rettv = current_funccal->rettv;
3643
3644 if (rettv != NULL)
3645 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003646 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003647 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
3648 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
3649 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003650 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003651 }
3652 else
3653 cstack->cs_rettv[idx] = NULL;
3654
3655 if (reanimate)
3656 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003657 // The pending return value could be overwritten by a ":return"
3658 // without argument in a finally clause; reset the default
3659 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003660 current_funccal->rettv->v_type = VAR_NUMBER;
3661 current_funccal->rettv->vval.v_number = 0;
3662 }
3663 }
3664 report_make_pending(CSTP_RETURN, rettv);
3665 }
3666 else
3667 {
3668 current_funccal->returned = TRUE;
3669
Bram Moolenaare38eab22019-12-05 21:50:01 +01003670 // If the return is carried out now, store the return value. For
3671 // a return immediately after reanimation, the value is already
3672 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003673 if (!reanimate && rettv != NULL)
3674 {
3675 clear_tv(current_funccal->rettv);
3676 *current_funccal->rettv = *(typval_T *)rettv;
3677 if (!is_cmd)
3678 vim_free(rettv);
3679 }
3680 }
3681
3682 return idx < 0;
3683}
3684
3685/*
3686 * Free the variable with a pending return value.
3687 */
3688 void
3689discard_pending_return(void *rettv)
3690{
3691 free_tv((typval_T *)rettv);
3692}
3693
3694/*
3695 * Generate a return command for producing the value of "rettv". The result
3696 * is an allocated string. Used by report_pending() for verbose messages.
3697 */
3698 char_u *
3699get_return_cmd(void *rettv)
3700{
3701 char_u *s = NULL;
3702 char_u *tofree = NULL;
3703 char_u numbuf[NUMBUFLEN];
3704
3705 if (rettv != NULL)
3706 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
3707 if (s == NULL)
3708 s = (char_u *)"";
3709
3710 STRCPY(IObuff, ":return ");
3711 STRNCPY(IObuff + 8, s, IOSIZE - 8);
3712 if (STRLEN(s) + 8 >= IOSIZE)
3713 STRCPY(IObuff + IOSIZE - 4, "...");
3714 vim_free(tofree);
3715 return vim_strsave(IObuff);
3716}
3717
3718/*
3719 * Get next function line.
3720 * Called by do_cmdline() to get the next line.
3721 * Returns allocated string, or NULL for end of function.
3722 */
3723 char_u *
3724get_func_line(
3725 int c UNUSED,
3726 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003727 int indent UNUSED,
3728 int do_concat UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003729{
3730 funccall_T *fcp = (funccall_T *)cookie;
3731 ufunc_T *fp = fcp->func;
3732 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003733 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003734
Bram Moolenaare38eab22019-12-05 21:50:01 +01003735 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003736 if (fcp->dbg_tick != debug_tick)
3737 {
3738 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003739 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003740 fcp->dbg_tick = debug_tick;
3741 }
3742#ifdef FEAT_PROFILE
3743 if (do_profiling == PROF_YES)
3744 func_line_end(cookie);
3745#endif
3746
3747 gap = &fp->uf_lines;
3748 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3749 || fcp->returned)
3750 retval = NULL;
3751 else
3752 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003753 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003754 while (fcp->linenr < gap->ga_len
3755 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
3756 ++fcp->linenr;
3757 if (fcp->linenr >= gap->ga_len)
3758 retval = NULL;
3759 else
3760 {
3761 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003762 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003763#ifdef FEAT_PROFILE
3764 if (do_profiling == PROF_YES)
3765 func_line_start(cookie);
3766#endif
3767 }
3768 }
3769
Bram Moolenaare38eab22019-12-05 21:50:01 +01003770 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003771 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003772 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003773 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003774 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003775 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003776 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003777 fcp->dbg_tick = debug_tick;
3778 }
3779
3780 return retval;
3781}
3782
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003783/*
3784 * Return TRUE if the currently active function should be ended, because a
3785 * return was encountered or an error occurred. Used inside a ":while".
3786 */
3787 int
3788func_has_ended(void *cookie)
3789{
3790 funccall_T *fcp = (funccall_T *)cookie;
3791
Bram Moolenaare38eab22019-12-05 21:50:01 +01003792 // Ignore the "abort" flag if the abortion behavior has been changed due to
3793 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003794 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3795 || fcp->returned);
3796}
3797
3798/*
3799 * return TRUE if cookie indicates a function which "abort"s on errors.
3800 */
3801 int
3802func_has_abort(
3803 void *cookie)
3804{
3805 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
3806}
3807
3808
3809/*
3810 * Turn "dict.Func" into a partial for "Func" bound to "dict".
3811 * Don't do this when "Func" is already a partial that was bound
3812 * explicitly (pt_auto is FALSE).
3813 * Changes "rettv" in-place.
3814 * Returns the updated "selfdict_in".
3815 */
3816 dict_T *
3817make_partial(dict_T *selfdict_in, typval_T *rettv)
3818{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003819 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003820 char_u *tofree = NULL;
3821 ufunc_T *fp;
3822 char_u fname_buf[FLEN_FIXED + 1];
3823 int error;
3824 dict_T *selfdict = selfdict_in;
3825
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003826 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
3827 fp = rettv->vval.v_partial->pt_func;
3828 else
3829 {
3830 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
3831 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003832 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003833 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 fp = find_func(fname, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003835 vim_free(tofree);
3836 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003837
3838 if (fp != NULL && (fp->uf_flags & FC_DICT))
3839 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003840 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003841
3842 if (pt != NULL)
3843 {
3844 pt->pt_refcount = 1;
3845 pt->pt_dict = selfdict;
3846 pt->pt_auto = TRUE;
3847 selfdict = NULL;
3848 if (rettv->v_type == VAR_FUNC)
3849 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003850 // Just a function: Take over the function name and use
3851 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003852 pt->pt_name = rettv->vval.v_string;
3853 }
3854 else
3855 {
3856 partial_T *ret_pt = rettv->vval.v_partial;
3857 int i;
3858
Bram Moolenaare38eab22019-12-05 21:50:01 +01003859 // Partial: copy the function name, use selfdict and copy
3860 // args. Can't take over name or args, the partial might
3861 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003862 if (ret_pt->pt_name != NULL)
3863 {
3864 pt->pt_name = vim_strsave(ret_pt->pt_name);
3865 func_ref(pt->pt_name);
3866 }
3867 else
3868 {
3869 pt->pt_func = ret_pt->pt_func;
3870 func_ptr_ref(pt->pt_func);
3871 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003872 if (ret_pt->pt_argc > 0)
3873 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003874 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003875 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003876 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003877 pt->pt_argc = 0;
3878 else
3879 {
3880 pt->pt_argc = ret_pt->pt_argc;
3881 for (i = 0; i < pt->pt_argc; i++)
3882 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
3883 }
3884 }
3885 partial_unref(ret_pt);
3886 }
3887 rettv->v_type = VAR_PARTIAL;
3888 rettv->vval.v_partial = pt;
3889 }
3890 }
3891 return selfdict;
3892}
3893
3894/*
3895 * Return the name of the executed function.
3896 */
3897 char_u *
3898func_name(void *cookie)
3899{
3900 return ((funccall_T *)cookie)->func->uf_name;
3901}
3902
3903/*
3904 * Return the address holding the next breakpoint line for a funccall cookie.
3905 */
3906 linenr_T *
3907func_breakpoint(void *cookie)
3908{
3909 return &((funccall_T *)cookie)->breakpoint;
3910}
3911
3912/*
3913 * Return the address holding the debug tick for a funccall cookie.
3914 */
3915 int *
3916func_dbg_tick(void *cookie)
3917{
3918 return &((funccall_T *)cookie)->dbg_tick;
3919}
3920
3921/*
3922 * Return the nesting level for a funccall cookie.
3923 */
3924 int
3925func_level(void *cookie)
3926{
3927 return ((funccall_T *)cookie)->level;
3928}
3929
3930/*
3931 * Return TRUE when a function was ended by a ":return" command.
3932 */
3933 int
3934current_func_returned(void)
3935{
3936 return current_funccal->returned;
3937}
3938
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003939 int
3940free_unref_funccal(int copyID, int testing)
3941{
3942 int did_free = FALSE;
3943 int did_free_funccal = FALSE;
3944 funccall_T *fc, **pfc;
3945
3946 for (pfc = &previous_funccal; *pfc != NULL; )
3947 {
3948 if (can_free_funccal(*pfc, copyID))
3949 {
3950 fc = *pfc;
3951 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003952 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003953 did_free = TRUE;
3954 did_free_funccal = TRUE;
3955 }
3956 else
3957 pfc = &(*pfc)->caller;
3958 }
3959 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003960 // When a funccal was freed some more items might be garbage
3961 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003962 (void)garbage_collect(testing);
3963
3964 return did_free;
3965}
3966
3967/*
Bram Moolenaarba209902016-08-24 22:06:38 +02003968 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003969 */
3970 static funccall_T *
3971get_funccal(void)
3972{
3973 int i;
3974 funccall_T *funccal;
3975 funccall_T *temp_funccal;
3976
3977 funccal = current_funccal;
3978 if (debug_backtrace_level > 0)
3979 {
3980 for (i = 0; i < debug_backtrace_level; i++)
3981 {
3982 temp_funccal = funccal->caller;
3983 if (temp_funccal)
3984 funccal = temp_funccal;
3985 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003986 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003987 debug_backtrace_level = i;
3988 }
3989 }
3990 return funccal;
3991}
3992
3993/*
3994 * Return the hashtable used for local variables in the current funccal.
3995 * Return NULL if there is no current funccal.
3996 */
3997 hashtab_T *
3998get_funccal_local_ht()
3999{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004001 return NULL;
4002 return &get_funccal()->l_vars.dv_hashtab;
4003}
4004
4005/*
4006 * Return the l: scope variable.
4007 * Return NULL if there is no current funccal.
4008 */
4009 dictitem_T *
4010get_funccal_local_var()
4011{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004013 return NULL;
4014 return &get_funccal()->l_vars_var;
4015}
4016
4017/*
4018 * Return the hashtable used for argument in the current funccal.
4019 * Return NULL if there is no current funccal.
4020 */
4021 hashtab_T *
4022get_funccal_args_ht()
4023{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004025 return NULL;
4026 return &get_funccal()->l_avars.dv_hashtab;
4027}
4028
4029/*
4030 * Return the a: scope variable.
4031 * Return NULL if there is no current funccal.
4032 */
4033 dictitem_T *
4034get_funccal_args_var()
4035{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004037 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004038 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004039}
4040
4041/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004042 * List function variables, if there is a function.
4043 */
4044 void
4045list_func_vars(int *first)
4046{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004048 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004049 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004050}
4051
4052/*
4053 * If "ht" is the hashtable for local variables in the current funccal, return
4054 * the dict that contains it.
4055 * Otherwise return NULL.
4056 */
4057 dict_T *
4058get_current_funccal_dict(hashtab_T *ht)
4059{
4060 if (current_funccal != NULL
4061 && ht == &current_funccal->l_vars.dv_hashtab)
4062 return &current_funccal->l_vars;
4063 return NULL;
4064}
4065
4066/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004067 * Search hashitem in parent scope.
4068 */
4069 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004070find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004071{
4072 funccall_T *old_current_funccal = current_funccal;
4073 hashtab_T *ht;
4074 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004075 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004076
4077 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4078 return NULL;
4079
Bram Moolenaare38eab22019-12-05 21:50:01 +01004080 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004081 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004082 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004083 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004084 ht = find_var_ht(name, &varname);
4085 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004086 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004087 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004088 if (!HASHITEM_EMPTY(hi))
4089 {
4090 *pht = ht;
4091 break;
4092 }
4093 }
4094 if (current_funccal == current_funccal->func->uf_scoped)
4095 break;
4096 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004097 }
4098 current_funccal = old_current_funccal;
4099
4100 return hi;
4101}
4102
4103/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004104 * Search variable in parent scope.
4105 */
4106 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004107find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004108{
4109 dictitem_T *v = NULL;
4110 funccall_T *old_current_funccal = current_funccal;
4111 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004112 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004113
4114 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4115 return NULL;
4116
Bram Moolenaare38eab22019-12-05 21:50:01 +01004117 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004118 current_funccal = current_funccal->func->uf_scoped;
4119 while (current_funccal)
4120 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004121 ht = find_var_ht(name, &varname);
4122 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004123 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004124 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004125 if (v != NULL)
4126 break;
4127 }
4128 if (current_funccal == current_funccal->func->uf_scoped)
4129 break;
4130 current_funccal = current_funccal->func->uf_scoped;
4131 }
4132 current_funccal = old_current_funccal;
4133
4134 return v;
4135}
4136
4137/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004138 * Set "copyID + 1" in previous_funccal and callers.
4139 */
4140 int
4141set_ref_in_previous_funccal(int copyID)
4142{
4143 int abort = FALSE;
4144 funccall_T *fc;
4145
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004146 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004147 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004148 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004149 abort = abort
4150 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4151 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004152 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004153 }
4154 return abort;
4155}
4156
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004157 static int
4158set_ref_in_funccal(funccall_T *fc, int copyID)
4159{
4160 int abort = FALSE;
4161
4162 if (fc->fc_copyID != copyID)
4163 {
4164 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004165 abort = abort
4166 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
4167 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004168 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004169 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004170 }
4171 return abort;
4172}
4173
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004174/*
4175 * Set "copyID" in all local vars and arguments in the call stack.
4176 */
4177 int
4178set_ref_in_call_stack(int copyID)
4179{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004180 int abort = FALSE;
4181 funccall_T *fc;
4182 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004183
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004184 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004185 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004186
4187 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004188 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
4189 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004190 abort = abort || set_ref_in_funccal(fc, copyID);
4191
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004192 return abort;
4193}
4194
4195/*
4196 * Set "copyID" in all functions available by name.
4197 */
4198 int
4199set_ref_in_functions(int copyID)
4200{
4201 int todo;
4202 hashitem_T *hi = NULL;
4203 int abort = FALSE;
4204 ufunc_T *fp;
4205
4206 todo = (int)func_hashtab.ht_used;
4207 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004208 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004209 if (!HASHITEM_EMPTY(hi))
4210 {
4211 --todo;
4212 fp = HI2UF(hi);
4213 if (!func_name_refcount(fp->uf_name))
4214 abort = abort || set_ref_in_func(NULL, fp, copyID);
4215 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004216 }
4217 return abort;
4218}
4219
4220/*
4221 * Set "copyID" in all function arguments.
4222 */
4223 int
4224set_ref_in_func_args(int copyID)
4225{
4226 int i;
4227 int abort = FALSE;
4228
4229 for (i = 0; i < funcargs.ga_len; ++i)
4230 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
4231 copyID, NULL, NULL);
4232 return abort;
4233}
4234
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004235/*
4236 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004237 * Returns TRUE if setting references failed somehow.
4238 */
4239 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004240set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004241{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004242 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004243 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01004244 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004245 char_u fname_buf[FLEN_FIXED + 1];
4246 char_u *tofree = NULL;
4247 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004248 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004249
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004250 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004251 return FALSE;
4252
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004253 if (fp_in == NULL)
4254 {
4255 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256 fp = find_func(fname, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004257 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004258 if (fp != NULL)
4259 {
4260 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004261 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004262 }
4263 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004264 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004265}
4266
Bram Moolenaare38eab22019-12-05 21:50:01 +01004267#endif // FEAT_EVAL