blob: ef1d42d12b14ad08a25ea878177f7512e79dd242 [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
203 any_default = TRUE;
204 p = skipwhite(p) + 1;
205 p = skipwhite(p);
206 expr = p;
207 if (eval1(&p, &rettv, FALSE) != FAIL)
208 {
209 if (ga_grow(default_args, 1) == FAIL)
210 goto err_ret;
211
212 // trim trailing whitespace
213 while (p > expr && VIM_ISWHITE(p[-1]))
214 p--;
215 c = *p;
216 *p = NUL;
217 expr = vim_strsave(expr);
218 if (expr == NULL)
219 {
220 *p = c;
221 goto err_ret;
222 }
223 ((char_u **)(default_args->ga_data))
224 [default_args->ga_len] = expr;
225 default_args->ga_len++;
226 *p = c;
227 }
228 else
229 mustend = TRUE;
230 }
231 else if (any_default)
232 {
233 emsg(_("E989: Non-default argument follows default argument"));
234 mustend = TRUE;
235 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200236 if (*p == ',')
237 ++p;
238 else
239 mustend = TRUE;
240 }
241 p = skipwhite(p);
242 if (mustend && *p != endchar)
243 {
244 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100245 semsg(_(e_invarg2), *argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200246 break;
247 }
248 }
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200249 if (*p != endchar)
250 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100251 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200252
253 *argp = p;
254 return OK;
255
256err_ret:
257 if (newargs != NULL)
258 ga_clear_strings(newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200259 if (default_args != NULL)
260 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200261 return FAIL;
262}
263
264/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200265 * Register function "fp" as using "current_funccal" as its scope.
266 */
267 static int
268register_closure(ufunc_T *fp)
269{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200270 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100271 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200272 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200273 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200274 fp->uf_scoped = current_funccal;
275 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200276
Bram Moolenaar58016442016-07-31 18:30:22 +0200277 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
278 return FAIL;
279 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
280 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200281 return OK;
282}
283
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100284 static void
285set_ufunc_name(ufunc_T *fp, char_u *name)
286{
287 STRCPY(fp->uf_name, name);
288
289 if (name[0] == K_SPECIAL)
290 {
291 fp->uf_name_exp = alloc(STRLEN(name) + 3);
292 if (fp->uf_name_exp != NULL)
293 {
294 STRCPY(fp->uf_name_exp, "<SNR>");
295 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
296 }
297 }
298}
299
Bram Moolenaar58016442016-07-31 18:30:22 +0200300/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200301 * Parse a lambda expression and get a Funcref from "*arg".
302 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
303 */
304 int
305get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
306{
307 garray_T newargs;
308 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200309 garray_T *pnewargs;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200310 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100311 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200312 int varargs;
313 int ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200314 char_u *start = skipwhite(*arg + 1);
315 char_u *s, *e;
316 static int lambda_no = 0;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200317 int *old_eval_lavars = eval_lavars_used;
318 int eval_lavars = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200319
320 ga_init(&newargs);
321 ga_init(&newlines);
322
Bram Moolenaare38eab22019-12-05 21:50:01 +0100323 // First, check if this is a lambda expression. "->" must exist.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100324 ret = get_function_args(&start, '-', NULL, NULL, NULL, NULL, TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200325 if (ret == FAIL || *start != '>')
326 return NOTDONE;
327
Bram Moolenaare38eab22019-12-05 21:50:01 +0100328 // Parse the arguments again.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200329 if (evaluate)
330 pnewargs = &newargs;
331 else
332 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200333 *arg = skipwhite(*arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100334 // TODO: argument types
335 ret = get_function_args(arg, '-', pnewargs, NULL, &varargs, NULL, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200336 if (ret == FAIL || **arg != '>')
337 goto errret;
338
Bram Moolenaare38eab22019-12-05 21:50:01 +0100339 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200340 if (evaluate)
341 eval_lavars_used = &eval_lavars;
342
Bram Moolenaare38eab22019-12-05 21:50:01 +0100343 // Get the start and the end of the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200344 *arg = skipwhite(*arg + 1);
345 s = *arg;
346 ret = skip_expr(arg);
347 if (ret == FAIL)
348 goto errret;
349 e = *arg;
350 *arg = skipwhite(*arg);
351 if (**arg != '}')
352 goto errret;
353 ++*arg;
354
355 if (evaluate)
356 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200357 int len, flags = 0;
358 char_u *p;
359 char_u name[20];
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200360
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200361 sprintf((char*)name, "<lambda>%d", ++lambda_no);
362
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200363 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200364 if (fp == NULL)
365 goto errret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100366 fp->uf_dfunc_idx = -1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200367 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200368 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200369 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200370
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200371 ga_init2(&newlines, (int)sizeof(char_u *), 1);
372 if (ga_grow(&newlines, 1) == FAIL)
373 goto errret;
374
Bram Moolenaare38eab22019-12-05 21:50:01 +0100375 // Add "return " before the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200376 len = 7 + e - s + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200377 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200378 if (p == NULL)
379 goto errret;
380 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
381 STRCPY(p, "return ");
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200382 vim_strncpy(p + 7, s, e - s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200383
384 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100385 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200386 hash_add(&func_hashtab, UF2HIKEY(fp));
387 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200388 ga_init(&fp->uf_def_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200389 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200390 if (current_funccal != NULL && eval_lavars)
391 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200392 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200393 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200394 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200395 }
396 else
397 fp->uf_scoped = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200398
399#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200400 if (prof_def_func())
401 func_do_profile(fp);
402#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200403 if (sandbox)
404 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100405 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200406 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200407 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200408 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200409 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100410 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200411
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200412 pt->pt_func = fp;
413 pt->pt_refcount = 1;
414 rettv->vval.v_partial = pt;
415 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200416 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200417
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200418 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200419 return OK;
420
421errret:
422 ga_clear_strings(&newargs);
423 ga_clear_strings(&newlines);
424 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100425 vim_free(pt);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200426 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200427 return FAIL;
428}
429
430/*
431 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
432 * name it contains, otherwise return "name".
433 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
434 * "partialp".
435 */
436 char_u *
437deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
438{
439 dictitem_T *v;
440 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200441 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200442
443 if (partialp != NULL)
444 *partialp = NULL;
445
446 cc = name[*lenp];
447 name[*lenp] = NUL;
448 v = find_var(name, NULL, no_autoload);
449 name[*lenp] = cc;
450 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
451 {
452 if (v->di_tv.vval.v_string == NULL)
453 {
454 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100455 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200456 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200457 s = v->di_tv.vval.v_string;
458 *lenp = (int)STRLEN(s);
459 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200460 }
461
462 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
463 {
464 partial_T *pt = v->di_tv.vval.v_partial;
465
466 if (pt == NULL)
467 {
468 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100469 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200470 }
471 if (partialp != NULL)
472 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200473 s = partial_name(pt);
474 *lenp = (int)STRLEN(s);
475 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200476 }
477
478 return name;
479}
480
481/*
482 * Give an error message with a function name. Handle <SNR> things.
483 * "ermsg" is to be passed without translation, use N_() instead of _().
484 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100485 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200486emsg_funcname(char *ermsg, char_u *name)
487{
488 char_u *p;
489
490 if (*name == K_SPECIAL)
491 p = concat_str((char_u *)"<SNR>", name + 3);
492 else
493 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100494 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200495 if (p != name)
496 vim_free(p);
497}
498
499/*
500 * Allocate a variable for the result of a function.
501 * Return OK or FAIL.
502 */
503 int
504get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200505 char_u *name, // name of the function
506 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200507 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200508 char_u **arg, // argument, pointing to the '('
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200509 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200510{
511 char_u *argp;
512 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100513 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
514 int argcount = 0; // number of arguments found
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200515
516 /*
517 * Get the arguments.
518 */
519 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200520 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
521 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200522 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100523 argp = skipwhite(argp + 1); // skip the '(' or ','
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200524 if (*argp == ')' || *argp == ',' || *argp == NUL)
525 break;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200526 if (eval1(&argp, &argvars[argcount], funcexe->evaluate) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200527 {
528 ret = FAIL;
529 break;
530 }
531 ++argcount;
532 if (*argp != ',')
533 break;
534 }
535 if (*argp == ')')
536 ++argp;
537 else
538 ret = FAIL;
539
540 if (ret == OK)
541 {
542 int i = 0;
543
544 if (get_vim_var_nr(VV_TESTING))
545 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100546 // Prepare for calling test_garbagecollect_now(), need to know
547 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200548 if (funcargs.ga_itemsize == 0)
549 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
550 for (i = 0; i < argcount; ++i)
551 if (ga_grow(&funcargs, 1) == OK)
552 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
553 &argvars[i];
554 }
555
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200556 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200557
558 funcargs.ga_len -= i;
559 }
560 else if (!aborting())
561 {
562 if (argcount == MAX_FUNC_ARGS)
563 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
564 else
565 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
566 }
567
568 while (--argcount >= 0)
569 clear_tv(&argvars[argcount]);
570
571 *arg = skipwhite(argp);
572 return ret;
573}
574
575#define FLEN_FIXED 40
576
577/*
578 * Return TRUE if "p" starts with "<SID>" or "s:".
579 * Only works if eval_fname_script() returned non-zero for "p"!
580 */
581 static int
582eval_fname_sid(char_u *p)
583{
584 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
585}
586
587/*
588 * In a script change <SID>name() and s:name() to K_SNR 123_name().
589 * Change <SNR>123_name() to K_SNR 123_name().
590 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
591 * (slow).
592 */
593 static char_u *
594fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
595{
596 int llen;
597 char_u *fname;
598 int i;
599
600 llen = eval_fname_script(name);
601 if (llen > 0)
602 {
603 fname_buf[0] = K_SPECIAL;
604 fname_buf[1] = KS_EXTRA;
605 fname_buf[2] = (int)KE_SNR;
606 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100607 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200608 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200609 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100610 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200611 else
612 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200613 sprintf((char *)fname_buf + 3, "%ld_",
614 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200615 i = (int)STRLEN(fname_buf);
616 }
617 }
618 if (i + STRLEN(name + llen) < FLEN_FIXED)
619 {
620 STRCPY(fname_buf + i, name + llen);
621 fname = fname_buf;
622 }
623 else
624 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200625 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200626 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +0100627 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200628 else
629 {
630 *tofree = fname;
631 mch_memmove(fname, fname_buf, (size_t)i);
632 STRCPY(fname + i, name + llen);
633 }
634 }
635 }
636 else
637 fname = name;
638 return fname;
639}
640
641/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100642 * Find a function "name" in script "sid".
643 */
644 static ufunc_T *
645find_func_with_sid(char_u *name, int sid)
646{
647 hashitem_T *hi;
648 char_u buffer[200];
649
650 buffer[0] = K_SPECIAL;
651 buffer[1] = KS_EXTRA;
652 buffer[2] = (int)KE_SNR;
653 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
654 (long)sid, name);
655 hi = hash_find(&func_hashtab, buffer);
656 if (!HASHITEM_EMPTY(hi))
657 return HI2UF(hi);
658
659 return NULL;
660}
661
662/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200663 * Find a function by name, return pointer to it in ufuncs.
664 * Return NULL for unknown function.
665 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100666 static ufunc_T *
667find_func_even_dead(char_u *name, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200668{
669 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 ufunc_T *func;
671 imported_T *imported;
672
673 if (in_vim9script())
674 {
675 // Find script-local function before global one.
676 func = find_func_with_sid(name, current_sctx.sc_sid);
677 if (func != NULL)
678 return func;
679
680 // Find imported funcion before global one.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +0100681 imported = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682 if (imported != NULL && imported->imp_funcname != NULL)
683 {
684 hi = hash_find(&func_hashtab, imported->imp_funcname);
685 if (!HASHITEM_EMPTY(hi))
686 return HI2UF(hi);
687 }
688 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200689
690 hi = hash_find(&func_hashtab, name);
691 if (!HASHITEM_EMPTY(hi))
692 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100693
694 return NULL;
695}
696
697/*
698 * Find a function by name, return pointer to it in ufuncs.
699 * "cctx" is passed in a :def function to find imported functions.
700 * Return NULL for unknown or dead function.
701 */
702 ufunc_T *
703find_func(char_u *name, cctx_T *cctx)
704{
705 ufunc_T *fp = find_func_even_dead(name, cctx);
706
707 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
708 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200709 return NULL;
710}
711
712/*
713 * Copy the function name of "fp" to buffer "buf".
714 * "buf" must be able to hold the function name plus three bytes.
715 * Takes care of script-local function names.
716 */
717 static void
718cat_func_name(char_u *buf, ufunc_T *fp)
719{
720 if (fp->uf_name[0] == K_SPECIAL)
721 {
722 STRCPY(buf, "<SNR>");
723 STRCAT(buf, fp->uf_name + 3);
724 }
725 else
726 STRCPY(buf, fp->uf_name);
727}
728
729/*
730 * Add a number variable "name" to dict "dp" with value "nr".
731 */
732 static void
733add_nr_var(
734 dict_T *dp,
735 dictitem_T *v,
736 char *name,
737 varnumber_T nr)
738{
739 STRCPY(v->di_key, name);
740 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
741 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
742 v->di_tv.v_type = VAR_NUMBER;
743 v->di_tv.v_lock = VAR_FIXED;
744 v->di_tv.vval.v_number = nr;
745}
746
747/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100748 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200749 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100750 static void
751free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200752{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100753 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200754
755 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
756 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100757 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200758
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100759 // When garbage collecting a funccall_T may be freed before the
760 // function that references it, clear its uf_scoped field.
761 // The function may have been redefined and point to another
762 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200763 if (fp != NULL && fp->uf_scoped == fc)
764 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200765 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200766 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200767
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200768 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200769 vim_free(fc);
770}
771
772/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100773 * Free "fc" and what it contains.
774 * Can be called only when "fc" is kept beyond the period of it called,
775 * i.e. after cleanup_function_call(fc).
776 */
777 static void
778free_funccal_contents(funccall_T *fc)
779{
780 listitem_T *li;
781
782 // Free all l: variables.
783 vars_clear(&fc->l_vars.dv_hashtab);
784
785 // Free all a: variables.
786 vars_clear(&fc->l_avars.dv_hashtab);
787
788 // Free the a:000 variables.
789 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
790 clear_tv(&li->li_tv);
791
792 free_funccal(fc);
793}
794
795/*
Bram Moolenaar6914c642017-04-01 21:21:30 +0200796 * Handle the last part of returning from a function: free the local hashtable.
797 * Unless it is still in use by a closure.
798 */
799 static void
800cleanup_function_call(funccall_T *fc)
801{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100802 int may_free_fc = fc->fc_refcount <= 0;
803 int free_fc = TRUE;
804
Bram Moolenaar6914c642017-04-01 21:21:30 +0200805 current_funccal = fc->caller;
806
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100807 // Free all l: variables if not referred.
808 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
809 vars_clear(&fc->l_vars.dv_hashtab);
810 else
811 free_fc = FALSE;
812
813 // If the a:000 list and the l: and a: dicts are not referenced and
814 // there is no closure using it, we can free the funccall_T and what's
815 // in it.
816 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
817 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200818 else
819 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100820 int todo;
821 hashitem_T *hi;
822 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200823
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100824 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200825
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100826 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200827 todo = (int)fc->l_avars.dv_hashtab.ht_used;
828 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
829 {
830 if (!HASHITEM_EMPTY(hi))
831 {
832 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100833 di = HI2DI(hi);
834 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200835 }
836 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100837 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200838
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100839 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
840 fc->l_varlist.lv_first = NULL;
841 else
842 {
843 listitem_T *li;
844
845 free_fc = FALSE;
846
847 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200848 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
849 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100850 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100851
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100852 if (free_fc)
853 free_funccal(fc);
854 else
855 {
856 static int made_copy = 0;
857
858 // "fc" is still in use. This can happen when returning "a:000",
859 // assigning "l:" to a global variable or defining a closure.
860 // Link "fc" in the list for garbage collection later.
861 fc->caller = previous_funccal;
862 previous_funccal = fc;
863
864 if (want_garbage_collect)
865 // If garbage collector is ready, clear count.
866 made_copy = 0;
867 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100868 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100869 // We have made a lot of copies, worth 4 Mbyte. This can happen
870 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100871 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100872 // too much memory.
873 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100874 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100875 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200876 }
877}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878/*
879 * Unreference "fc": decrement the reference count and free it when it
880 * becomes zero. "fp" is detached from "fc".
881 * When "force" is TRUE we are exiting.
882 */
883 static void
884funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
885{
886 funccall_T **pfc;
887 int i;
888
889 if (fc == NULL)
890 return;
891
892 if (--fc->fc_refcount <= 0 && (force || (
893 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
894 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
895 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
896 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
897 {
898 if (fc == *pfc)
899 {
900 *pfc = fc->caller;
901 free_funccal_contents(fc);
902 return;
903 }
904 }
905 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
906 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
907 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
908}
909
910/*
911 * Remove the function from the function hashtable. If the function was
912 * deleted while it still has references this was already done.
913 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
914 */
915 static int
916func_remove(ufunc_T *fp)
917{
918 hashitem_T *hi;
919
920 // Return if it was already virtually deleted.
921 if (fp->uf_flags & FC_DEAD)
922 return FALSE;
923
924 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
925 if (!HASHITEM_EMPTY(hi))
926 {
927 // When there is a def-function index do not actually remove the
928 // function, so we can find the index when defining the function again.
929 if (fp->uf_dfunc_idx >= 0)
930 fp->uf_flags |= FC_DEAD;
931 else
932 hash_remove(&func_hashtab, hi);
933 return TRUE;
934 }
935 return FALSE;
936}
937
938 static void
939func_clear_items(ufunc_T *fp)
940{
941 ga_clear_strings(&(fp->uf_args));
942 ga_clear_strings(&(fp->uf_def_args));
943 ga_clear_strings(&(fp->uf_lines));
944 VIM_CLEAR(fp->uf_name_exp);
945 VIM_CLEAR(fp->uf_arg_types);
946 ga_clear(&fp->uf_type_list);
947#ifdef FEAT_PROFILE
948 VIM_CLEAR(fp->uf_tml_count);
949 VIM_CLEAR(fp->uf_tml_total);
950 VIM_CLEAR(fp->uf_tml_self);
951#endif
952}
953
954/*
955 * Free all things that a function contains. Does not free the function
956 * itself, use func_free() for that.
957 * When "force" is TRUE we are exiting.
958 */
959 static void
960func_clear(ufunc_T *fp, int force)
961{
962 if (fp->uf_cleared)
963 return;
964 fp->uf_cleared = TRUE;
965
966 // clear this function
967 func_clear_items(fp);
968 funccal_unref(fp->uf_scoped, fp, force);
969 delete_def_function(fp);
970}
971
972/*
973 * Free a function and remove it from the list of functions. Does not free
974 * what a function contains, call func_clear() first.
975 */
976 static void
977func_free(ufunc_T *fp)
978{
979 // Only remove it when not done already, otherwise we would remove a newer
980 // version of the function with the same name.
981 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
982 func_remove(fp);
983
984 if ((fp->uf_flags & FC_DEAD) == 0)
985 vim_free(fp);
986}
987
988/*
989 * Free all things that a function contains and free the function itself.
990 * When "force" is TRUE we are exiting.
991 */
992 static void
993func_clear_free(ufunc_T *fp, int force)
994{
995 func_clear(fp, force);
996 func_free(fp);
997}
998
Bram Moolenaar6914c642017-04-01 21:21:30 +0200999
1000/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001001 * Call a user function.
1002 */
1003 static void
1004call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001005 ufunc_T *fp, // pointer to function
1006 int argcount, // nr of args
1007 typval_T *argvars, // arguments
1008 typval_T *rettv, // return value
1009 linenr_T firstline, // first line of range
1010 linenr_T lastline, // last line of range
1011 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001012{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001013 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001014 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001015 funccall_T *fc;
1016 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001017 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001018 static int depth = 0;
1019 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001020 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001021 int i;
1022 int ai;
1023 int islambda = FALSE;
1024 char_u numbuf[NUMBUFLEN];
1025 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001026#ifdef FEAT_PROFILE
1027 proftime_T wait_start;
1028 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001029 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001030#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001031 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001032
Bram Moolenaare38eab22019-12-05 21:50:01 +01001033 // If depth of calling is getting too high, don't execute the function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001034 if (depth >= p_mfd)
1035 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001036 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001037 rettv->v_type = VAR_NUMBER;
1038 rettv->vval.v_number = -1;
1039 return;
1040 }
1041 ++depth;
1042
Bram Moolenaare38eab22019-12-05 21:50:01 +01001043 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001044
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001045 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001046 if (fc == NULL)
1047 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001048 fc->caller = current_funccal;
1049 current_funccal = fc;
1050 fc->func = fp;
1051 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001052 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001053 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001054 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1055 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001056 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001057 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001058 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001059
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001060 if (fp->uf_dfunc_idx >= 0)
1061 {
1062 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001063 save_current_sctx = current_sctx;
1064 current_sctx = fp->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001065
1066 // Execute the compiled function.
1067 call_def_function(fp, argcount, argvars, rettv);
1068 --depth;
1069 current_funccal = fc->caller;
1070
1071 estack_pop();
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001072 current_sctx = save_current_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001073 free_funccal(fc);
1074 return;
1075 }
1076
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001077 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1078 islambda = TRUE;
1079
1080 /*
1081 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1082 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1083 * each argument variable and saves a lot of time.
1084 */
1085 /*
1086 * Init l: variables.
1087 */
1088 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1089 if (selfdict != NULL)
1090 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001091 // Set l:self to "selfdict". Use "name" to avoid a warning from
1092 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001093 v = &fc->fixvar[fixvar_idx++].var;
1094 name = v->di_key;
1095 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001096 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001097 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1098 v->di_tv.v_type = VAR_DICT;
1099 v->di_tv.v_lock = 0;
1100 v->di_tv.vval.v_dict = selfdict;
1101 ++selfdict->dv_refcount;
1102 }
1103
1104 /*
1105 * Init a: variables.
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001106 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001107 * Set a:000 to a list with room for the "..." arguments.
1108 */
1109 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
1110 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001111 (varnumber_T)(argcount >= fp->uf_args.ga_len
1112 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001113 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001114 // Use "name" to avoid a warning from some compiler that checks the
1115 // destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001116 v = &fc->fixvar[fixvar_idx++].var;
1117 name = v->di_key;
1118 STRCPY(name, "000");
1119 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1120 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1121 v->di_tv.v_type = VAR_LIST;
1122 v->di_tv.v_lock = VAR_FIXED;
1123 v->di_tv.vval.v_list = &fc->l_varlist;
1124 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
1125 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1126 fc->l_varlist.lv_lock = VAR_FIXED;
1127
1128 /*
1129 * Set a:firstline to "firstline" and a:lastline to "lastline".
1130 * Set a:name to named arguments.
1131 * Set a:N to the "..." arguments.
1132 */
1133 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
1134 (varnumber_T)firstline);
1135 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
1136 (varnumber_T)lastline);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001137 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001138 {
1139 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001140 typval_T def_rettv;
1141 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001142
1143 ai = i - fp->uf_args.ga_len;
1144 if (ai < 0)
1145 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001146 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001147 name = FUNCARG(fp, i);
1148 if (islambda)
1149 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001150
1151 // evaluate named argument default expression
1152 isdefault = ai + fp->uf_def_args.ga_len >= 0
1153 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1154 && argvars[i].vval.v_number == VVAL_NONE));
1155 if (isdefault)
1156 {
1157 char_u *default_expr = NULL;
1158 def_rettv.v_type = VAR_NUMBER;
1159 def_rettv.vval.v_number = -1;
1160
1161 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1162 [ai + fp->uf_def_args.ga_len];
1163 if (eval1(&default_expr, &def_rettv, TRUE) == FAIL)
1164 {
1165 default_arg_err = 1;
1166 break;
1167 }
1168 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001169 }
1170 else
1171 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001172 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001173 sprintf((char *)numbuf, "%d", ai + 1);
1174 name = numbuf;
1175 }
1176 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1177 {
1178 v = &fc->fixvar[fixvar_idx++].var;
1179 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001180 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001181 }
1182 else
1183 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001184 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001185 if (v == NULL)
1186 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001187 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001188 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001189
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001190 // Note: the values are copied directly to avoid alloc/free.
1191 // "argvars" must have VAR_FIXED for v_lock.
1192 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001193 v->di_tv.v_lock = VAR_FIXED;
1194
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001195 if (addlocal)
1196 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001197 // Named arguments should be accessed without the "a:" prefix in
1198 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001199 copy_tv(&v->di_tv, &v->di_tv);
1200 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001201 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001202 else
1203 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001204
1205 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1206 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001207 listitem_T *li = &fc->l_listitems[ai];
1208
1209 li->li_tv = argvars[i];
1210 li->li_tv.v_lock = VAR_FIXED;
1211 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001212 }
1213 }
1214
Bram Moolenaare38eab22019-12-05 21:50:01 +01001215 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001216 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001217
1218 if (fp->uf_flags & FC_SANDBOX)
1219 {
1220 using_sandbox = TRUE;
1221 ++sandbox;
1222 }
1223
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001224 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001225 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001226 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001227 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001228 ++no_wait_return;
1229 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001230
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001231 smsg(_("calling %s"), SOURCING_NAME);
1232 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001233 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001234 char_u buf[MSG_BUF_LEN];
1235 char_u numbuf2[NUMBUFLEN];
1236 char_u *tofree;
1237 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001238
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001239 msg_puts("(");
1240 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001241 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001242 if (i > 0)
1243 msg_puts(", ");
1244 if (argvars[i].v_type == VAR_NUMBER)
1245 msg_outnum((long)argvars[i].vval.v_number);
1246 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001247 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001248 // Do not want errors such as E724 here.
1249 ++emsg_off;
1250 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1251 --emsg_off;
1252 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001253 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001254 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001255 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001256 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1257 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001258 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001259 msg_puts((char *)s);
1260 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001261 }
1262 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001263 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001264 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001265 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001266 msg_puts("\n"); // don't overwrite this either
1267
1268 verbose_leave_scroll();
1269 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001270 }
1271#ifdef FEAT_PROFILE
1272 if (do_profiling == PROF_YES)
1273 {
1274 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001275 {
1276 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001277 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001278 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001279 if (fp->uf_profiling
1280 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1281 {
1282 ++fp->uf_tm_count;
1283 profile_start(&call_start);
1284 profile_zero(&fp->uf_tm_children);
1285 }
1286 script_prof_save(&wait_start);
1287 }
1288#endif
1289
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001290 save_current_sctx = current_sctx;
1291 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001292 save_did_emsg = did_emsg;
1293 did_emsg = FALSE;
1294
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001295 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1296 did_emsg = TRUE;
1297 else
1298 // call do_cmdline() to execute the lines
1299 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001300 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1301
1302 --RedrawingDisabled;
1303
Bram Moolenaare38eab22019-12-05 21:50:01 +01001304 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001305 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1306 {
1307 clear_tv(rettv);
1308 rettv->v_type = VAR_NUMBER;
1309 rettv->vval.v_number = -1;
1310 }
1311
1312#ifdef FEAT_PROFILE
1313 if (do_profiling == PROF_YES && (fp->uf_profiling
1314 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1315 {
1316 profile_end(&call_start);
1317 profile_sub_wait(&wait_start, &call_start);
1318 profile_add(&fp->uf_tm_total, &call_start);
1319 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1320 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1321 {
1322 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1323 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1324 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001325 if (started_profiling)
1326 // make a ":profdel func" stop profiling the function
1327 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001328 }
1329#endif
1330
Bram Moolenaare38eab22019-12-05 21:50:01 +01001331 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001332 if (p_verbose >= 12)
1333 {
1334 ++no_wait_return;
1335 verbose_enter_scroll();
1336
1337 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001338 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001339 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001340 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001341 (long)fc->rettv->vval.v_number);
1342 else
1343 {
1344 char_u buf[MSG_BUF_LEN];
1345 char_u numbuf2[NUMBUFLEN];
1346 char_u *tofree;
1347 char_u *s;
1348
Bram Moolenaare38eab22019-12-05 21:50:01 +01001349 // The value may be very long. Skip the middle part, so that we
1350 // have some idea how it starts and ends. smsg() would always
1351 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001352 ++emsg_off;
1353 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1354 --emsg_off;
1355 if (s != NULL)
1356 {
1357 if (vim_strsize(s) > MSG_BUF_CLEN)
1358 {
1359 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1360 s = buf;
1361 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001362 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001363 vim_free(tofree);
1364 }
1365 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001366 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001367
1368 verbose_leave_scroll();
1369 --no_wait_return;
1370 }
1371
Bram Moolenaare31ee862020-01-07 20:59:34 +01001372 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001373 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001374 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001375#ifdef FEAT_PROFILE
1376 if (do_profiling == PROF_YES)
1377 script_prof_restore(&wait_start);
1378#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001379 if (using_sandbox)
1380 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001381
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001382 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001383 {
1384 ++no_wait_return;
1385 verbose_enter_scroll();
1386
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001387 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001388 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001389
1390 verbose_leave_scroll();
1391 --no_wait_return;
1392 }
1393
1394 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001395 --depth;
1396
Bram Moolenaar6914c642017-04-01 21:21:30 +02001397 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001398}
1399
1400/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001401 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001402 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403 int
1404call_user_func_check(
1405 ufunc_T *fp,
1406 int argcount,
1407 typval_T *argvars,
1408 typval_T *rettv,
1409 funcexe_T *funcexe,
1410 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001411{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 int error;
1413 int regular_args = fp->uf_args.ga_len;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001414
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1416 *funcexe->doesrange = TRUE;
1417 if (argcount < regular_args - fp->uf_def_args.ga_len)
1418 error = FCERR_TOOFEW;
1419 else if (!has_varargs(fp) && argcount > regular_args)
1420 error = FCERR_TOOMANY;
1421 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1422 error = FCERR_DICT;
1423 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001424 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 int did_save_redo = FALSE;
1426 save_redo_T save_redo;
1427
1428 /*
1429 * Call the user function.
1430 * Save and restore search patterns, script variables and
1431 * redo buffer.
1432 */
1433 save_search_patterns();
1434 if (!ins_compl_active())
1435 {
1436 saveRedobuff(&save_redo);
1437 did_save_redo = TRUE;
1438 }
1439 ++fp->uf_calls;
1440 call_user_func(fp, argcount, argvars, rettv,
1441 funcexe->firstline, funcexe->lastline,
1442 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
1443 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
1444 // Function was unreferenced while being used, free it now.
1445 func_clear_free(fp, FALSE);
1446 if (did_save_redo)
1447 restoreRedobuff(&save_redo);
1448 restore_search_patterns();
1449 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001450 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001452}
1453
1454/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001455 * There are two kinds of function names:
1456 * 1. ordinary names, function defined with :function
1457 * 2. numbered functions and lambdas
1458 * For the first we only count the name stored in func_hashtab as a reference,
1459 * using function() does not count as a reference, because the function is
1460 * looked up by name.
1461 */
1462 static int
1463func_name_refcount(char_u *name)
1464{
1465 return isdigit(*name) || *name == '<';
1466}
1467
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001468static funccal_entry_T *funccal_stack = NULL;
1469
1470/*
1471 * Save the current function call pointer, and set it to NULL.
1472 * Used when executing autocommands and for ":source".
1473 */
1474 void
1475save_funccal(funccal_entry_T *entry)
1476{
1477 entry->top_funccal = current_funccal;
1478 entry->next = funccal_stack;
1479 funccal_stack = entry;
1480 current_funccal = NULL;
1481}
1482
1483 void
1484restore_funccal(void)
1485{
1486 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001487 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001488 else
1489 {
1490 current_funccal = funccal_stack->top_funccal;
1491 funccal_stack = funccal_stack->next;
1492 }
1493}
1494
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001495 funccall_T *
1496get_current_funccal(void)
1497{
1498 return current_funccal;
1499}
1500
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001501#if defined(EXITFREE) || defined(PROTO)
1502 void
1503free_all_functions(void)
1504{
1505 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001506 ufunc_T *fp;
1507 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001508 long_u todo = 1;
1509 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001510
Bram Moolenaare38eab22019-12-05 21:50:01 +01001511 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001512 while (current_funccal != NULL)
1513 {
1514 clear_tv(current_funccal->rettv);
1515 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001516 if (current_funccal == NULL && funccal_stack != NULL)
1517 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001518 }
1519
Bram Moolenaare38eab22019-12-05 21:50:01 +01001520 // First clear what the functions contain. Since this may lower the
1521 // reference count of a function, it may also free a function and change
1522 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001523 while (todo > 0)
1524 {
1525 todo = func_hashtab.ht_used;
1526 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1527 if (!HASHITEM_EMPTY(hi))
1528 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001529 // clear the def function index now
1530 fp = HI2UF(hi);
1531 fp->uf_flags &= ~FC_DEAD;
1532 fp->uf_dfunc_idx = -1;
1533
Bram Moolenaare38eab22019-12-05 21:50:01 +01001534 // Only free functions that are not refcounted, those are
1535 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001536 if (func_name_refcount(fp->uf_name))
1537 ++skipped;
1538 else
1539 {
1540 used = func_hashtab.ht_used;
1541 func_clear(fp, TRUE);
1542 if (used != func_hashtab.ht_used)
1543 {
1544 skipped = 0;
1545 break;
1546 }
1547 }
1548 --todo;
1549 }
1550 }
1551
Bram Moolenaare38eab22019-12-05 21:50:01 +01001552 // Now actually free the functions. Need to start all over every time,
1553 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001554 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001555 while (func_hashtab.ht_used > skipped)
1556 {
1557 todo = func_hashtab.ht_used;
1558 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001559 if (!HASHITEM_EMPTY(hi))
1560 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001561 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001562 // Only free functions that are not refcounted, those are
1563 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02001564 fp = HI2UF(hi);
1565 if (func_name_refcount(fp->uf_name))
1566 ++skipped;
1567 else
1568 {
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001569 func_free(fp);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001570 skipped = 0;
1571 break;
1572 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001573 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001574 }
1575 if (skipped == 0)
1576 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577
1578 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001579}
1580#endif
1581
1582/*
1583 * Return TRUE if "name" looks like a builtin function name: starts with a
1584 * lower case letter and doesn't contain AUTOLOAD_CHAR.
1585 * "len" is the length of "name", or -1 for NUL terminated.
1586 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001588builtin_function(char_u *name, int len)
1589{
1590 char_u *p;
1591
1592 if (!ASCII_ISLOWER(name[0]))
1593 return FALSE;
1594 p = vim_strchr(name, AUTOLOAD_CHAR);
1595 return p == NULL || (len > 0 && p > name + len);
1596}
1597
1598 int
1599func_call(
1600 char_u *name,
1601 typval_T *args,
1602 partial_T *partial,
1603 dict_T *selfdict,
1604 typval_T *rettv)
1605{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001606 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001607 listitem_T *item;
1608 typval_T argv[MAX_FUNC_ARGS + 1];
1609 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001610 int r = 0;
1611
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001612 range_list_materialize(l);
1613 for (item = l->lv_first; item != NULL; item = item->li_next)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001614 {
1615 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1616 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001617 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001618 break;
1619 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001620 // Make a copy of each argument. This is needed to be able to set
1621 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001622 copy_tv(&item->li_tv, &argv[argc++]);
1623 }
1624
1625 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001626 {
1627 funcexe_T funcexe;
1628
Bram Moolenaarac92e252019-08-03 21:58:38 +02001629 vim_memset(&funcexe, 0, sizeof(funcexe));
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001630 funcexe.firstline = curwin->w_cursor.lnum;
1631 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001632 funcexe.evaluate = TRUE;
1633 funcexe.partial = partial;
1634 funcexe.selfdict = selfdict;
1635 r = call_func(name, -1, rettv, argc, argv, &funcexe);
1636 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001637
Bram Moolenaare38eab22019-12-05 21:50:01 +01001638 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001639 while (argc > 0)
1640 clear_tv(&argv[--argc]);
1641
1642 return r;
1643}
1644
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001645static int callback_depth = 0;
1646
1647 int
1648get_callback_depth(void)
1649{
1650 return callback_depth;
1651}
1652
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001653/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001654 * Invoke call_func() with a callback.
1655 */
1656 int
1657call_callback(
1658 callback_T *callback,
1659 int len, // length of "name" or -1 to use strlen()
1660 typval_T *rettv, // return value goes here
1661 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001662 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001663 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001664{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001665 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001666 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001667
1668 vim_memset(&funcexe, 0, sizeof(funcexe));
1669 funcexe.evaluate = TRUE;
1670 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001671 ++callback_depth;
1672 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
1673 --callback_depth;
1674 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001675}
1676
1677/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001678 * Give an error message for the result of a function.
1679 * Nothing if "error" is FCERR_NONE.
1680 */
1681 void
1682user_func_error(int error, char_u *name)
1683{
1684 switch (error)
1685 {
1686 case FCERR_UNKNOWN:
1687 emsg_funcname(e_unknownfunc, name);
1688 break;
1689 case FCERR_NOTMETHOD:
1690 emsg_funcname(
1691 N_("E276: Cannot use function as a method: %s"), name);
1692 break;
1693 case FCERR_DELETED:
1694 emsg_funcname(N_(e_func_deleted), name);
1695 break;
1696 case FCERR_TOOMANY:
1697 emsg_funcname((char *)e_toomanyarg, name);
1698 break;
1699 case FCERR_TOOFEW:
1700 emsg_funcname((char *)e_toofewarg, name);
1701 break;
1702 case FCERR_SCRIPT:
1703 emsg_funcname(
1704 N_("E120: Using <SID> not in a script context: %s"), name);
1705 break;
1706 case FCERR_DICT:
1707 emsg_funcname(
1708 N_("E725: Calling dict function without Dictionary: %s"),
1709 name);
1710 break;
1711 }
1712}
1713
1714/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001715 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001716 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001717 * Return FAIL when the function can't be called, OK otherwise.
1718 * Also returns OK when an error was encountered while executing the function.
1719 */
1720 int
1721call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001722 char_u *funcname, // name of the function
1723 int len, // length of "name" or -1 to use strlen()
1724 typval_T *rettv, // return value goes here
1725 int argcount_in, // number of "argvars"
1726 typval_T *argvars_in, // vars for arguments, must have "argcount"
1727 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001728 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001729{
1730 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01001731 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001732 int i;
1733 ufunc_T *fp;
1734 char_u fname_buf[FLEN_FIXED + 1];
1735 char_u *tofree = NULL;
1736 char_u *fname;
1737 char_u *name;
1738 int argcount = argcount_in;
1739 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001740 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001741 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
1742 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001743 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001744 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001745 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001746
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02001747 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
1748 // even when call_func() returns FAIL.
1749 rettv->v_type = VAR_UNKNOWN;
1750
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001751 // Make a copy of the name, if it comes from a funcref variable it could
1752 // be changed or deleted in the called function.
1753 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001754 if (name == NULL)
1755 return ret;
1756
1757 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1758
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001759 if (funcexe->doesrange != NULL)
1760 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001761
1762 if (partial != NULL)
1763 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001764 // When the function has a partial with a dict and there is a dict
1765 // argument, use the dict argument. That is backwards compatible.
1766 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001767 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001768 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01001769 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001770 {
1771 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001772 {
1773 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
1774 {
Bram Moolenaaref140542019-12-31 21:27:13 +01001775 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001776 goto theend;
1777 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001778 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001779 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001780 for (i = 0; i < argcount_in; ++i)
1781 argv[i + argv_clear] = argvars_in[i];
1782 argvars = argv;
1783 argcount = partial->pt_argc + argcount_in;
1784 }
1785 }
1786
Bram Moolenaaref140542019-12-31 21:27:13 +01001787 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001788 {
1789 char_u *rfname = fname;
1790
Bram Moolenaare38eab22019-12-05 21:50:01 +01001791 // Ignore "g:" before a function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001792 if (fname[0] == 'g' && fname[1] == ':')
1793 rfname = fname + 2;
1794
Bram Moolenaare38eab22019-12-05 21:50:01 +01001795 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001796 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01001797 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001798
1799 if (!builtin_function(rfname, -1))
1800 {
1801 /*
1802 * User defined function.
1803 */
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001804 if (partial != NULL && partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001805 fp = partial->pt_func;
1806 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001808
Bram Moolenaare38eab22019-12-05 21:50:01 +01001809 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001810 if (fp == NULL
1811 && apply_autocmds(EVENT_FUNCUNDEFINED,
1812 rfname, rfname, TRUE, NULL)
1813 && !aborting())
1814 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001815 // executed an autocommand, search for the function again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001817 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001818 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001819 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
1820 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001821 // loaded a package, search for the function again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001823 }
1824
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001825 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01001826 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001827 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001828 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001829 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01001830 // postponed filling in the arguments, do it now
1831 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001832 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001833
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001834 if (funcexe->basetv != NULL)
1835 {
1836 // Method call: base->Method()
1837 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
1838 argv[0] = *funcexe->basetv;
1839 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001840 argvars = argv;
1841 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001842 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001843
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001844 error = call_user_func_check(fp, argcount, argvars, rettv,
1845 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001846 }
1847 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001848 else if (funcexe->basetv != NULL)
1849 {
1850 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001851 * expr->method(): Find the method name in the table, call its
1852 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02001853 */
1854 error = call_internal_method(fname, argcount, argvars, rettv,
1855 funcexe->basetv);
1856 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001857 else
1858 {
1859 /*
1860 * Find the function name in the table, call its implementation.
1861 */
1862 error = call_internal_func(fname, argcount, argvars, rettv);
1863 }
1864 /*
1865 * The function call (or "FuncUndefined" autocommand sequence) might
1866 * have been aborted by an error, an interrupt, or an explicitly thrown
1867 * exception that has not been caught so far. This situation can be
1868 * tested for by calling aborting(). For an error in an internal
1869 * function or for the "E132" error in call_user_func(), however, the
1870 * throw point at which the "force_abort" flag (temporarily reset by
1871 * emsg()) is normally updated has not been reached yet. We need to
1872 * update that flag first to make aborting() reliable.
1873 */
1874 update_force_abort();
1875 }
Bram Moolenaaref140542019-12-31 21:27:13 +01001876 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001877 ret = OK;
1878
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001879theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001880 /*
1881 * Report an error unless the argument evaluation or function call has been
1882 * cancelled due to an aborting error, an interrupt, or an exception.
1883 */
1884 if (!aborting())
1885 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001886 user_func_error(error, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001887 }
1888
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001889 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001890 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001891 clear_tv(&argv[--argv_clear + argv_base]);
1892
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001893 vim_free(tofree);
1894 vim_free(name);
1895
1896 return ret;
1897}
1898
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899 static char_u *
1900printable_func_name(ufunc_T *fp)
1901{
1902 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
1903}
1904
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001905/*
1906 * List the head of the function: "name(arg1, arg2)".
1907 */
1908 static void
1909list_func_head(ufunc_T *fp, int indent)
1910{
1911 int j;
1912
1913 msg_start();
1914 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001915 msg_puts(" ");
1916 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001918 msg_putchar('(');
1919 for (j = 0; j < fp->uf_args.ga_len; ++j)
1920 {
1921 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001922 msg_puts(", ");
1923 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 if (fp->uf_arg_types != NULL)
1925 {
1926 char *tofree;
1927
1928 msg_puts(": ");
1929 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
1930 vim_free(tofree);
1931 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001932 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
1933 {
1934 msg_puts(" = ");
1935 msg_puts(((char **)(fp->uf_def_args.ga_data))
1936 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
1937 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001938 }
1939 if (fp->uf_varargs)
1940 {
1941 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001942 msg_puts(", ");
1943 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001944 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 if (fp->uf_va_name != NULL)
1946 {
1947 if (j)
1948 msg_puts(", ");
1949 msg_puts("...");
1950 msg_puts((char *)fp->uf_va_name);
1951 if (fp->uf_va_type)
1952 {
1953 char *tofree;
1954
1955 msg_puts(": ");
1956 msg_puts(type_name(fp->uf_va_type, &tofree));
1957 vim_free(tofree);
1958 }
1959 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001960 msg_putchar(')');
1961 if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001962 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001963 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001964 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001965 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001966 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001967 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001968 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001969 msg_clr_eos();
1970 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001971 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001972}
1973
1974/*
1975 * Get a function name, translating "<SID>" and "<SNR>".
1976 * Also handles a Funcref in a List or Dictionary.
1977 * Returns the function name in allocated memory, or NULL for failure.
1978 * flags:
1979 * TFN_INT: internal function name OK
1980 * TFN_QUIET: be quiet
1981 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001982 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001983 * Advances "pp" to just after the function name (if no error).
1984 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001985 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001986trans_function_name(
1987 char_u **pp,
Bram Moolenaare38eab22019-12-05 21:50:01 +01001988 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001989 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01001990 funcdict_T *fdp, // return: info about dictionary used
1991 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001992{
1993 char_u *name = NULL;
1994 char_u *start;
1995 char_u *end;
1996 int lead;
1997 char_u sid_buf[20];
1998 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002000 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002001 int vim9script;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002002
2003 if (fdp != NULL)
2004 vim_memset(fdp, 0, sizeof(funcdict_T));
2005 start = *pp;
2006
Bram Moolenaare38eab22019-12-05 21:50:01 +01002007 // Check for hard coded <SNR>: already translated function ID (from a user
2008 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002009 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2010 && (*pp)[2] == (int)KE_SNR)
2011 {
2012 *pp += 3;
2013 len = get_id_len(pp) + 3;
2014 return vim_strnsave(start, len);
2015 }
2016
Bram Moolenaare38eab22019-12-05 21:50:01 +01002017 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2018 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002019 lead = eval_fname_script(start);
2020 if (lead > 2)
2021 start += lead;
2022
Bram Moolenaare38eab22019-12-05 21:50:01 +01002023 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002024 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002025 lead > 2 ? 0 : FNE_CHECK_START);
2026 if (end == start)
2027 {
2028 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002029 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002030 goto theend;
2031 }
2032 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2033 {
2034 /*
2035 * Report an invalid expression in braces, unless the expression
2036 * evaluation has been cancelled due to an aborting error, an
2037 * interrupt, or an exception.
2038 */
2039 if (!aborting())
2040 {
2041 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002042 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002043 }
2044 else
2045 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2046 goto theend;
2047 }
2048
2049 if (lv.ll_tv != NULL)
2050 {
2051 if (fdp != NULL)
2052 {
2053 fdp->fd_dict = lv.ll_dict;
2054 fdp->fd_newkey = lv.ll_newkey;
2055 lv.ll_newkey = NULL;
2056 fdp->fd_di = lv.ll_di;
2057 }
2058 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2059 {
2060 name = vim_strsave(lv.ll_tv->vval.v_string);
2061 *pp = end;
2062 }
2063 else if (lv.ll_tv->v_type == VAR_PARTIAL
2064 && lv.ll_tv->vval.v_partial != NULL)
2065 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002066 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002067 *pp = end;
2068 if (partial != NULL)
2069 *partial = lv.ll_tv->vval.v_partial;
2070 }
2071 else
2072 {
2073 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2074 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002075 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002076 else
2077 *pp = end;
2078 name = NULL;
2079 }
2080 goto theend;
2081 }
2082
2083 if (lv.ll_name == NULL)
2084 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002085 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002086 *pp = end;
2087 goto theend;
2088 }
2089
Bram Moolenaare38eab22019-12-05 21:50:01 +01002090 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002091 if (lv.ll_exp_name != NULL)
2092 {
2093 len = (int)STRLEN(lv.ll_exp_name);
2094 name = deref_func_name(lv.ll_exp_name, &len, partial,
2095 flags & TFN_NO_AUTOLOAD);
2096 if (name == lv.ll_exp_name)
2097 name = NULL;
2098 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002099 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002100 {
2101 len = (int)(end - *pp);
2102 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2103 if (name == *pp)
2104 name = NULL;
2105 }
2106 if (name != NULL)
2107 {
2108 name = vim_strsave(name);
2109 *pp = end;
2110 if (STRNCMP(name, "<SNR>", 5) == 0)
2111 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002112 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002113 name[0] = K_SPECIAL;
2114 name[1] = KS_EXTRA;
2115 name[2] = (int)KE_SNR;
2116 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2117 }
2118 goto theend;
2119 }
2120
2121 if (lv.ll_exp_name != NULL)
2122 {
2123 len = (int)STRLEN(lv.ll_exp_name);
2124 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2125 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2126 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002127 // When there was "s:" already or the name expanded to get a
2128 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002129 lv.ll_name += 2;
2130 len -= 2;
2131 lead = 2;
2132 }
2133 }
2134 else
2135 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002136 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002137 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
2138 lv.ll_name += 2;
2139 len = (int)(end - lv.ll_name);
2140 }
2141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142 // In Vim9 script a user function is script-local by default.
2143 vim9script = ASCII_ISUPPER(*start)
2144 && current_sctx.sc_version == SCRIPT_VERSION_VIM9;
2145
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002146 /*
2147 * Copy the function name to allocated memory.
2148 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2149 * Accept <SNR>123_name() outside a script.
2150 */
2151 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002152 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002153 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002154 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155 if (!vim9script)
2156 lead = 3;
2157 if (vim9script || (lv.ll_exp_name != NULL
2158 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002159 || eval_fname_sid(*pp))
2160 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002161 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002162 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002163 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002164 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002165 goto theend;
2166 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002167 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168 if (vim9script)
2169 extra = 3 + (int)STRLEN(sid_buf);
2170 else
2171 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002172 }
2173 }
2174 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2175 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002176 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002177 start);
2178 goto theend;
2179 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002180 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002181 {
2182 char_u *cp = vim_strchr(lv.ll_name, ':');
2183
2184 if (cp != NULL && cp < end)
2185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002186 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002187 goto theend;
2188 }
2189 }
2190
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002192 if (name != NULL)
2193 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002194 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002195 {
2196 name[0] = K_SPECIAL;
2197 name[1] = KS_EXTRA;
2198 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002199 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002200 STRCPY(name + 3, sid_buf);
2201 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002202 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2203 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002204 }
2205 *pp = end;
2206
2207theend:
2208 clear_lval(&lv);
2209 return name;
2210}
2211
2212/*
2213 * ":function"
2214 */
2215 void
2216ex_function(exarg_T *eap)
2217{
2218 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002219 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002220 int j;
2221 int c;
2222 int saved_did_emsg;
2223 int saved_wait_return = need_wait_return;
2224 char_u *name = NULL;
2225 char_u *p;
2226 char_u *arg;
2227 char_u *line_arg = NULL;
2228 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002229 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002230 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002231 garray_T newlines;
2232 int varargs = FALSE;
2233 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002234 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002235 ufunc_T *fp;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002236 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002237 int indent;
2238 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002239#define MAX_FUNC_NESTING 50
2240 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002241 dictitem_T *v;
2242 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002243 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002244 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002245 int todo;
2246 hashitem_T *hi;
Bram Moolenaare96a2492019-06-25 04:12:16 +02002247 int do_concat = TRUE;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002248 linenr_T sourcing_lnum_off;
2249 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002250 int is_heredoc = FALSE;
2251 char_u *skip_until = NULL;
2252 char_u *heredoc_trimmed = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002253
2254 /*
2255 * ":function" without argument: list functions.
2256 */
2257 if (ends_excmd(*eap->arg))
2258 {
2259 if (!eap->skip)
2260 {
2261 todo = (int)func_hashtab.ht_used;
2262 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2263 {
2264 if (!HASHITEM_EMPTY(hi))
2265 {
2266 --todo;
2267 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268 if ((fp->uf_flags & FC_DEAD)
2269 || message_filtered(fp->uf_name))
Bram Moolenaarf86db782018-10-25 13:31:37 +02002270 continue;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002271 if (!func_name_refcount(fp->uf_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002272 list_func_head(fp, FALSE);
2273 }
2274 }
2275 }
2276 eap->nextcmd = check_nextcmd(eap->arg);
2277 return;
2278 }
2279
2280 /*
2281 * ":function /pat": list functions matching pattern.
2282 */
2283 if (*eap->arg == '/')
2284 {
2285 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
2286 if (!eap->skip)
2287 {
2288 regmatch_T regmatch;
2289
2290 c = *p;
2291 *p = NUL;
2292 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2293 *p = c;
2294 if (regmatch.regprog != NULL)
2295 {
2296 regmatch.rm_ic = p_ic;
2297
2298 todo = (int)func_hashtab.ht_used;
2299 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2300 {
2301 if (!HASHITEM_EMPTY(hi))
2302 {
2303 --todo;
2304 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 if ((fp->uf_flags & FC_DEAD) == 0
2306 && !isdigit(*fp->uf_name)
2307 && vim_regexec(&regmatch, fp->uf_name, 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002308 list_func_head(fp, FALSE);
2309 }
2310 }
2311 vim_regfree(regmatch.regprog);
2312 }
2313 }
2314 if (*p == '/')
2315 ++p;
2316 eap->nextcmd = check_nextcmd(p);
2317 return;
2318 }
2319
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320 ga_init(&newargs);
2321 ga_init(&argtypes);
2322 ga_init(&default_args);
2323
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002324 /*
2325 * Get the function name. There are these situations:
2326 * func normal function name
2327 * "name" == func, "fudi.fd_dict" == NULL
2328 * dict.func new dictionary entry
2329 * "name" == NULL, "fudi.fd_dict" set,
2330 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
2331 * dict.func existing dict entry with a Funcref
2332 * "name" == func, "fudi.fd_dict" set,
2333 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2334 * dict.func existing dict entry that's not a Funcref
2335 * "name" == NULL, "fudi.fd_dict" set,
2336 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2337 * s:func script-local function name
2338 * g:func global function name, same as "func"
2339 */
2340 p = eap->arg;
Bram Moolenaar3388d332017-12-07 22:23:04 +01002341 name = trans_function_name(&p, eap->skip, TFN_NO_AUTOLOAD, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002342 paren = (vim_strchr(p, '(') != NULL);
2343 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
2344 {
2345 /*
2346 * Return on an invalid expression in braces, unless the expression
2347 * evaluation has been cancelled due to an aborting error, an
2348 * interrupt, or an exception.
2349 */
2350 if (!aborting())
2351 {
2352 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002353 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002354 vim_free(fudi.fd_newkey);
2355 return;
2356 }
2357 else
2358 eap->skip = TRUE;
2359 }
2360
Bram Moolenaare38eab22019-12-05 21:50:01 +01002361 // An error in a function call during evaluation of an expression in magic
2362 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002363 saved_did_emsg = did_emsg;
2364 did_emsg = FALSE;
2365
2366 /*
2367 * ":function func" with only function name: list function.
2368 */
2369 if (!paren)
2370 {
2371 if (!ends_excmd(*skipwhite(p)))
2372 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002373 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002374 goto ret_free;
2375 }
2376 eap->nextcmd = check_nextcmd(p);
2377 if (eap->nextcmd != NULL)
2378 *p = NUL;
2379 if (!eap->skip && !got_int)
2380 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381 fp = find_func(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002382 if (fp != NULL)
2383 {
2384 list_func_head(fp, TRUE);
2385 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2386 {
2387 if (FUNCLINE(fp, j) == NULL)
2388 continue;
2389 msg_putchar('\n');
2390 msg_outnum((long)(j + 1));
2391 if (j < 9)
2392 msg_putchar(' ');
2393 if (j < 99)
2394 msg_putchar(' ');
2395 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002396 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002397 ui_breakcheck();
2398 }
2399 if (!got_int)
2400 {
2401 msg_putchar('\n');
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402 if (fp->uf_dfunc_idx >= 0)
2403 msg_puts(" enddef");
2404 else
2405 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002406 }
2407 }
2408 else
2409 emsg_funcname(N_("E123: Undefined function: %s"), name);
2410 }
2411 goto ret_free;
2412 }
2413
2414 /*
2415 * ":function name(arg1, arg2)" Define function.
2416 */
2417 p = skipwhite(p);
2418 if (*p != '(')
2419 {
2420 if (!eap->skip)
2421 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002422 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002423 goto ret_free;
2424 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002425 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002426 if (vim_strchr(p, '(') != NULL)
2427 p = vim_strchr(p, '(');
2428 }
2429 p = skipwhite(p + 1);
2430
2431 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2432
2433 if (!eap->skip)
2434 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002435 // Check the name of the function. Unless it's a dictionary function
2436 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002437 if (name != NULL)
2438 arg = name;
2439 else
2440 arg = fudi.fd_newkey;
2441 if (arg != NULL && (fudi.fd_di == NULL
2442 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2443 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2444 {
2445 if (*arg == K_SPECIAL)
2446 j = 3;
2447 else
2448 j = 0;
2449 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2450 : eval_isnamec(arg[j])))
2451 ++j;
2452 if (arg[j] != NUL)
2453 emsg_funcname((char *)e_invarg2, arg);
2454 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002455 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002456 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002457 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002458 }
2459
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002460 if (get_function_args(&p, ')', &newargs,
2461 eap->cmdidx == CMD_def ? &argtypes : NULL,
2462 &varargs, &default_args, eap->skip) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002463 goto errret_2;
2464
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002465 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002466 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 // find the return type: :def Func(): type
2468 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002469 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 ret_type = skipwhite(p + 1);
2471 p = skip_type(ret_type);
2472 if (p > ret_type)
2473 p = skipwhite(p);
2474 else
2475 semsg(_("E1056: expected a type: %s"), ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002476 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002477 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478 else
2479 // find extra arguments "range", "dict", "abort" and "closure"
2480 for (;;)
2481 {
2482 p = skipwhite(p);
2483 if (STRNCMP(p, "range", 5) == 0)
2484 {
2485 flags |= FC_RANGE;
2486 p += 5;
2487 }
2488 else if (STRNCMP(p, "dict", 4) == 0)
2489 {
2490 flags |= FC_DICT;
2491 p += 4;
2492 }
2493 else if (STRNCMP(p, "abort", 5) == 0)
2494 {
2495 flags |= FC_ABORT;
2496 p += 5;
2497 }
2498 else if (STRNCMP(p, "closure", 7) == 0)
2499 {
2500 flags |= FC_CLOSURE;
2501 p += 7;
2502 if (current_funccal == NULL)
2503 {
2504 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
2505 name == NULL ? (char_u *)"" : name);
2506 goto erret;
2507 }
2508 }
2509 else
2510 break;
2511 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002512
Bram Moolenaare38eab22019-12-05 21:50:01 +01002513 // When there is a line break use what follows for the function body.
2514 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002515 if (*p == '\n')
2516 line_arg = p + 1;
2517 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002518 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002519
2520 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
2522 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002523 */
2524 if (KeyTyped)
2525 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002526 // Check if the function already exists, don't let the user type the
2527 // whole function before telling him it doesn't work! For a script we
2528 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002529 if (!eap->skip && !eap->forceit)
2530 {
2531 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002532 emsg(_(e_funcdict));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 else if (name != NULL && find_func(name, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002534 emsg_funcname(e_funcexts, name);
2535 }
2536
2537 if (!eap->skip && did_emsg)
2538 goto erret;
2539
Bram Moolenaare38eab22019-12-05 21:50:01 +01002540 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002541 cmdline_row = msg_row;
2542 }
2543
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002544 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002545 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002546
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002547 indent = 2;
2548 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002550 for (;;)
2551 {
2552 if (KeyTyped)
2553 {
2554 msg_scroll = TRUE;
2555 saved_wait_return = FALSE;
2556 }
2557 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002558
2559 if (line_arg != NULL)
2560 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002561 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002562 theline = line_arg;
2563 p = vim_strchr(theline, '\n');
2564 if (p == NULL)
2565 line_arg += STRLEN(line_arg);
2566 else
2567 {
2568 *p = NUL;
2569 line_arg = p + 1;
2570 }
2571 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002572 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002573 {
2574 vim_free(line_to_free);
2575 if (eap->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02002576 theline = getcmdline(':', 0L, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002577 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02002578 theline = eap->getline(':', eap->cookie, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002579 line_to_free = theline;
2580 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002581 if (KeyTyped)
2582 lines_left = Rows - 1;
2583 if (theline == NULL)
2584 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 if (eap->cmdidx == CMD_def)
2586 emsg(_("E1057: Missing :enddef"));
2587 else
2588 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002589 goto erret;
2590 }
2591
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002592 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002593 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002594 if (SOURCING_LNUM < sourcing_lnum_off)
2595 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002596 else
2597 sourcing_lnum_off = 0;
2598
2599 if (skip_until != NULL)
2600 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002602 // * ":append" and "."
2603 // * ":python <<EOF" and "EOF"
2604 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
2605 if (heredoc_trimmed == NULL
2606 || (is_heredoc && skipwhite(theline) == theline)
2607 || STRNCMP(theline, heredoc_trimmed,
2608 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002609 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002610 if (heredoc_trimmed == NULL)
2611 p = theline;
2612 else if (is_heredoc)
2613 p = skipwhite(theline) == theline
2614 ? theline : theline + STRLEN(heredoc_trimmed);
2615 else
2616 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02002617 if (STRCMP(p, skip_until) == 0)
2618 {
2619 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002620 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaare96a2492019-06-25 04:12:16 +02002621 do_concat = TRUE;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002622 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002623 }
2624 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002625 }
2626 else
2627 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002628 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01002629 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002630 ;
2631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 // Check for "endfunction" or "enddef".
2633 if (checkforcmd(&p, nesting_def[nesting]
2634 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002635 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002636 char_u *nextcmd = NULL;
2637
Bram Moolenaar663bb232017-06-22 19:12:10 +02002638 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002639 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002640 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002641 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002642 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643 give_warning2(eap->cmdidx == CMD_def
2644 ? (char_u *)_("W1001: Text found after :enddef: %s")
2645 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002646 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002647 if (nextcmd != NULL)
2648 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002649 // Another command follows. If the line came from "eap" we
2650 // can simply point into it, otherwise we need to change
2651 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02002652 eap->nextcmd = nextcmd;
2653 if (line_to_free != NULL)
2654 {
2655 vim_free(*eap->cmdlinep);
2656 *eap->cmdlinep = line_to_free;
2657 line_to_free = NULL;
2658 }
2659 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002660 break;
2661 }
2662
Bram Moolenaare38eab22019-12-05 21:50:01 +01002663 // Increase indent inside "if", "while", "for" and "try", decrease
2664 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002666 indent -= 2;
2667 else if (STRNCMP(p, "if", 2) == 0
2668 || STRNCMP(p, "wh", 2) == 0
2669 || STRNCMP(p, "for", 3) == 0
2670 || STRNCMP(p, "try", 3) == 0)
2671 indent += 2;
2672
Bram Moolenaare38eab22019-12-05 21:50:01 +01002673 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01002674 // Only recognize "def" inside "def", not inside "function",
2675 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676 c = *p;
Bram Moolenaar673660a2020-01-26 16:50:05 +01002677 if (checkforcmd(&p, "function", 2)
2678 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002679 {
2680 if (*p == '!')
2681 p = skipwhite(p + 1);
2682 p += eval_fname_script(p);
2683 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
2684 if (*skipwhite(p) == '(')
2685 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686 if (nesting == MAX_FUNC_NESTING - 1)
2687 emsg(_("E1058: function nesting too deep"));
2688 else
2689 {
2690 ++nesting;
2691 nesting_def[nesting] = (c == 'd');
2692 indent += 2;
2693 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002694 }
2695 }
2696
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002697 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002698 p = skip_range(p, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002699 if (eap->cmdidx != CMD_def
2700 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002701 || (p[0] == 'c'
2702 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
2703 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
2704 && (STRNCMP(&p[3], "nge", 3) != 0
2705 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002706 || (p[0] == 'i'
2707 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002708 && (!ASCII_ISALPHA(p[2])
2709 || (p[2] == 's'
2710 && (!ASCII_ISALPHA(p[3])
2711 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002712 skip_until = vim_strsave((char_u *)".");
2713
Bram Moolenaare38eab22019-12-05 21:50:01 +01002714 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002715 arg = skipwhite(skiptowhite(p));
2716 if (arg[0] == '<' && arg[1] =='<'
2717 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01002718 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
2719 || ((p[2] == '3' || p[2] == 'x')
2720 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002721 || (p[0] == 'p' && p[1] == 'e'
2722 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
2723 || (p[0] == 't' && p[1] == 'c'
2724 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
2725 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
2726 && !ASCII_ISALPHA(p[3]))
2727 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
2728 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
2729 || (p[0] == 'm' && p[1] == 'z'
2730 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
2731 ))
2732 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002733 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002734 p = skipwhite(arg + 2);
2735 if (*p == NUL)
2736 skip_until = vim_strsave((char_u *)".");
2737 else
2738 skip_until = vim_strsave(p);
2739 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002740
2741 // Check for ":let v =<< [trim] EOF"
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002742 // and ":let [a, b] =<< [trim] EOF"
Bram Moolenaar8471e572019-05-19 21:37:18 +02002743 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002744 if (*arg == '[')
2745 arg = vim_strchr(arg, ']');
2746 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002747 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002748 arg = skipwhite(skiptowhite(arg));
2749 if ( arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
2750 && ((p[0] == 'l'
2751 && p[1] == 'e'
2752 && (!ASCII_ISALNUM(p[2])
2753 || (p[2] == 't' && !ASCII_ISALNUM(p[3]))))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02002754 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002755 p = skipwhite(arg + 3);
2756 if (STRNCMP(p, "trim", 4) == 0)
2757 {
2758 // Ignore leading white space.
2759 p = skipwhite(p + 4);
2760 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar8471e572019-05-19 21:37:18 +02002761 (int)(skipwhite(theline) - theline));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002762 }
2763 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p));
2764 do_concat = FALSE;
2765 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002766 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002767 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002768 }
2769
Bram Moolenaare38eab22019-12-05 21:50:01 +01002770 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002771 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002772 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002773
Bram Moolenaare38eab22019-12-05 21:50:01 +01002774 // Copy the line to newly allocated memory. get_one_sourceline()
2775 // allocates 250 bytes per line, this saves 80% on average. The cost
2776 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002777 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002778 if (p == NULL)
2779 goto erret;
2780 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002781
Bram Moolenaare38eab22019-12-05 21:50:01 +01002782 // Add NULL lines for continuation lines, so that the line count is
2783 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002784 while (sourcing_lnum_off-- > 0)
2785 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
2786
Bram Moolenaare38eab22019-12-05 21:50:01 +01002787 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002788 if (line_arg != NULL && *line_arg == NUL)
2789 line_arg = NULL;
2790 }
2791
Bram Moolenaare38eab22019-12-05 21:50:01 +01002792 // Don't define the function when skipping commands or when an error was
2793 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002794 if (eap->skip || did_emsg)
2795 goto erret;
2796
2797 /*
2798 * If there are no errors, add the function
2799 */
2800 if (fudi.fd_dict == NULL)
2801 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 hashtab_T *ht;
2803
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002804 v = find_var(name, &ht, FALSE);
2805 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
2806 {
2807 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
2808 name);
2809 goto erret;
2810 }
2811
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 fp = find_func_even_dead(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002813 if (fp != NULL)
2814 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 int dead = fp->uf_flags & FC_DEAD;
2816
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002817 // Function can be replaced with "function!" and when sourcing the
2818 // same script again, but only once.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 if (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002820 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
2821 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002822 {
2823 emsg_funcname(e_funcexts, name);
2824 goto erret;
2825 }
2826 if (fp->uf_calls > 0)
2827 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002828 emsg_funcname(
2829 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002830 name);
2831 goto erret;
2832 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002833 if (fp->uf_refcount > 1)
2834 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002835 // This function is referenced somewhere, don't redefine it but
2836 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002837 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002838 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002839 fp = NULL;
2840 overwrite = TRUE;
2841 }
2842 else
2843 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002844 char_u *exp_name = fp->uf_name_exp;
2845
2846 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01002847 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002848 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002849 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002850 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002852#ifdef FEAT_PROFILE
2853 fp->uf_profiling = FALSE;
2854 fp->uf_prof_initialized = FALSE;
2855#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002856 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002857 }
2858 }
2859 else
2860 {
2861 char numbuf[20];
2862
2863 fp = NULL;
2864 if (fudi.fd_newkey == NULL && !eap->forceit)
2865 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002866 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002867 goto erret;
2868 }
2869 if (fudi.fd_di == NULL)
2870 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002871 // Can't add a function to a locked dictionary
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002872 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002873 goto erret;
2874 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002875 // Can't change an existing function if it is locked
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002876 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002877 goto erret;
2878
Bram Moolenaare38eab22019-12-05 21:50:01 +01002879 // Give the function a sequential number. Can only be used with a
2880 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002881 vim_free(name);
2882 sprintf(numbuf, "%d", ++func_nr);
2883 name = vim_strsave((char_u *)numbuf);
2884 if (name == NULL)
2885 goto erret;
2886 }
2887
2888 if (fp == NULL)
2889 {
2890 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
2891 {
2892 int slen, plen;
2893 char_u *scriptname;
2894
Bram Moolenaare38eab22019-12-05 21:50:01 +01002895 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002896 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002897 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002898 {
2899 scriptname = autoload_name(name);
2900 if (scriptname != NULL)
2901 {
2902 p = vim_strchr(scriptname, '/');
2903 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002904 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002905 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002906 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002907 j = OK;
2908 vim_free(scriptname);
2909 }
2910 }
2911 if (j == FAIL)
2912 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002913 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002914 goto erret;
2915 }
2916 }
2917
Bram Moolenaar47ed5532019-08-08 20:49:14 +02002918 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002919 if (fp == NULL)
2920 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 fp->uf_dfunc_idx = -1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002922
2923 if (fudi.fd_dict != NULL)
2924 {
2925 if (fudi.fd_di == NULL)
2926 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002927 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002928 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
2929 if (fudi.fd_di == NULL)
2930 {
2931 vim_free(fp);
2932 goto erret;
2933 }
2934 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
2935 {
2936 vim_free(fudi.fd_di);
2937 vim_free(fp);
2938 goto erret;
2939 }
2940 }
2941 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01002942 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002943 clear_tv(&fudi.fd_di->di_tv);
2944 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002945 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002946
Bram Moolenaare38eab22019-12-05 21:50:01 +01002947 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002948 flags |= FC_DICT;
2949 }
2950
Bram Moolenaare38eab22019-12-05 21:50:01 +01002951 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002952 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002953 if (overwrite)
2954 {
2955 hi = hash_find(&func_hashtab, name);
2956 hi->hi_key = UF2HIKEY(fp);
2957 }
2958 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002959 {
2960 vim_free(fp);
2961 goto erret;
2962 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002963 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002964 }
2965 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002966 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 fp->uf_ret_type = &t_any;
2968
2969 if (eap->cmdidx == CMD_def)
2970 {
Bram Moolenaarbfe12042020-02-04 21:54:07 +01002971 int lnum_save = SOURCING_LNUM;
2972
2973 // error messages are for the first function line
2974 SOURCING_LNUM = sourcing_lnum_top;
2975
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976 // parse the argument types
2977 ga_init2(&fp->uf_type_list, sizeof(type_T), 5);
2978
2979 if (argtypes.ga_len > 0)
2980 {
2981 // When "varargs" is set the last name/type goes into uf_va_name
2982 // and uf_va_type.
2983 int len = argtypes.ga_len - (varargs ? 1 : 0);
2984
2985 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
2986 if (fp->uf_arg_types != NULL)
2987 {
Bram Moolenaarbfe12042020-02-04 21:54:07 +01002988 int i;
2989 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990
2991 for (i = 0; i < len; ++ i)
2992 {
2993 p = ((char_u **)argtypes.ga_data)[i];
2994 if (p == NULL)
2995 // todo: get type from default value
Bram Moolenaarbfe12042020-02-04 21:54:07 +01002996 type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 else
Bram Moolenaarbfe12042020-02-04 21:54:07 +01002998 type = parse_type(&p, &fp->uf_type_list);
2999 if (type == NULL)
3000 {
3001 SOURCING_LNUM = lnum_save;
3002 goto errret_2;
3003 }
3004 fp->uf_arg_types[i] = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 }
3006 }
3007 if (varargs)
3008 {
3009 // Move the last argument "...name: type" to uf_va_name and
3010 // uf_va_type.
3011 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
3012 [fp->uf_args.ga_len - 1];
3013 --fp->uf_args.ga_len;
3014 p = ((char_u **)argtypes.ga_data)[len];
3015 if (p == NULL)
3016 // todo: get type from default value
3017 fp->uf_va_type = &t_any;
3018 else
3019 fp->uf_va_type = parse_type(&p, &fp->uf_type_list);
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003020 if (fp->uf_va_type == NULL)
3021 {
3022 SOURCING_LNUM = lnum_save;
3023 goto errret_2;
3024 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 }
3026 varargs = FALSE;
3027 }
3028
3029 // parse the return type, if any
3030 if (ret_type == NULL)
3031 fp->uf_ret_type = &t_void;
3032 else
3033 {
3034 p = ret_type;
3035 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list);
3036 }
3037 }
3038
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003039 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003040 if ((flags & FC_CLOSURE) != 0)
3041 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003042 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003043 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003044 }
3045 else
3046 fp->uf_scoped = NULL;
3047
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003048#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003049 if (prof_def_func())
3050 func_do_profile(fp);
3051#endif
3052 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003053 if (sandbox)
3054 flags |= FC_SANDBOX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003055 fp->uf_flags = flags;
3056 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003057 fp->uf_script_ctx = current_sctx;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003058 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 if (is_export)
3060 {
3061 fp->uf_flags |= FC_EXPORT;
3062 // let ex_export() know the export worked.
3063 is_export = FALSE;
3064 }
3065
3066 // ":def Func()" needs to be compiled
3067 if (eap->cmdidx == CMD_def)
3068 compile_def_function(fp, FALSE);
3069
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003070 goto ret_free;
3071
3072erret:
3073 ga_clear_strings(&newargs);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 ga_clear_strings(&argtypes);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003075 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003076errret_2:
3077 ga_clear_strings(&newlines);
3078ret_free:
3079 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003080 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003081 vim_free(fudi.fd_newkey);
3082 vim_free(name);
3083 did_emsg |= saved_did_emsg;
3084 need_wait_return |= saved_wait_return;
3085}
3086
3087/*
3088 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3089 * Return 2 if "p" starts with "s:".
3090 * Return 0 otherwise.
3091 */
3092 int
3093eval_fname_script(char_u *p)
3094{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003095 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3096 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003097 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3098 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3099 return 5;
3100 if (p[0] == 's' && p[1] == ':')
3101 return 2;
3102 return 0;
3103}
3104
3105 int
3106translated_function_exists(char_u *name)
3107{
3108 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003109 return has_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 return find_func(name, NULL) != NULL;
3111}
3112
3113/*
3114 * Return TRUE when "ufunc" has old-style "..." varargs
3115 * or named varargs "...name: type".
3116 */
3117 int
3118has_varargs(ufunc_T *ufunc)
3119{
3120 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003121}
3122
3123/*
3124 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003125 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003126 */
3127 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003128function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003129{
3130 char_u *nm = name;
3131 char_u *p;
3132 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003133 int flag;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003134
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003135 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3136 if (no_deref)
3137 flag |= TFN_NO_DEREF;
3138 p = trans_function_name(&nm, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003139 nm = skipwhite(nm);
3140
Bram Moolenaare38eab22019-12-05 21:50:01 +01003141 // Only accept "funcname", "funcname ", "funcname (..." and
3142 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003143 if (p != NULL && (*nm == NUL || *nm == '('))
3144 n = translated_function_exists(p);
3145 vim_free(p);
3146 return n;
3147}
3148
Bram Moolenaar113e1072019-01-20 15:30:40 +01003149#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003150 char_u *
3151get_expanded_name(char_u *name, int check)
3152{
3153 char_u *nm = name;
3154 char_u *p;
3155
3156 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
3157
3158 if (p != NULL && *nm == NUL)
3159 if (!check || translated_function_exists(p))
3160 return p;
3161
3162 vim_free(p);
3163 return NULL;
3164}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003165#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003166
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003167/*
3168 * Function given to ExpandGeneric() to obtain the list of user defined
3169 * function names.
3170 */
3171 char_u *
3172get_user_func_name(expand_T *xp, int idx)
3173{
3174 static long_u done;
3175 static hashitem_T *hi;
3176 ufunc_T *fp;
3177
3178 if (idx == 0)
3179 {
3180 done = 0;
3181 hi = func_hashtab.ht_array;
3182 }
3183 if (done < func_hashtab.ht_used)
3184 {
3185 if (done++ > 0)
3186 ++hi;
3187 while (HASHITEM_EMPTY(hi))
3188 ++hi;
3189 fp = HI2UF(hi);
3190
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 // don't show dead, dict and lambda functions
3192 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02003193 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003195
3196 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003197 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003198
3199 cat_func_name(IObuff, fp);
3200 if (xp->xp_context != EXPAND_USER_FUNC)
3201 {
3202 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003204 STRCAT(IObuff, ")");
3205 }
3206 return IObuff;
3207 }
3208 return NULL;
3209}
3210
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003211/*
3212 * ":delfunction {name}"
3213 */
3214 void
3215ex_delfunction(exarg_T *eap)
3216{
3217 ufunc_T *fp = NULL;
3218 char_u *p;
3219 char_u *name;
3220 funcdict_T fudi;
3221
3222 p = eap->arg;
3223 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
3224 vim_free(fudi.fd_newkey);
3225 if (name == NULL)
3226 {
3227 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003228 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003229 return;
3230 }
3231 if (!ends_excmd(*skipwhite(p)))
3232 {
3233 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003234 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003235 return;
3236 }
3237 eap->nextcmd = check_nextcmd(p);
3238 if (eap->nextcmd != NULL)
3239 *p = NUL;
3240
3241 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 fp = find_func(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003243 vim_free(name);
3244
3245 if (!eap->skip)
3246 {
3247 if (fp == NULL)
3248 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02003249 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003250 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003251 return;
3252 }
3253 if (fp->uf_calls > 0)
3254 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003255 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003256 return;
3257 }
3258
3259 if (fudi.fd_dict != NULL)
3260 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003261 // Delete the dict item that refers to the function, it will
3262 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003263 dictitem_remove(fudi.fd_dict, fudi.fd_di);
3264 }
3265 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003266 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003267 // A normal function (not a numbered function or lambda) has a
3268 // refcount of 1 for the entry in the hashtable. When deleting
3269 // it and the refcount is more than one, it should be kept.
3270 // A numbered function and lambda should be kept if the refcount is
3271 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003272 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003273 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003274 // Function is still referenced somewhere. Don't free it but
3275 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003276 if (func_remove(fp))
3277 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003278 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003279 }
3280 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003281 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003282 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003283 }
3284}
3285
3286/*
3287 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003288 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003289 */
3290 void
3291func_unref(char_u *name)
3292{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003293 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003294
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003295 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003296 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 fp = find_func(name, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003298 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003299 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003300#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003301 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003302#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01003303 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003304 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003305 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003306 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003307 // Only delete it when it's not being used. Otherwise it's done
3308 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003309 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003310 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02003311 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003312}
3313
3314/*
3315 * Unreference a Function: decrement the reference count and free it when it
3316 * becomes zero.
3317 */
3318 void
3319func_ptr_unref(ufunc_T *fp)
3320{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003321 if (fp != NULL && --fp->uf_refcount <= 0)
3322 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003323 // Only delete it when it's not being used. Otherwise it's done
3324 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02003325 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003326 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003327 }
3328}
3329
3330/*
3331 * Count a reference to a Function.
3332 */
3333 void
3334func_ref(char_u *name)
3335{
3336 ufunc_T *fp;
3337
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003338 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003339 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 fp = find_func(name, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003341 if (fp != NULL)
3342 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003343 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01003344 // Only give an error for a numbered function.
3345 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01003346 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003347}
3348
3349/*
3350 * Count a reference to a Function.
3351 */
3352 void
3353func_ptr_ref(ufunc_T *fp)
3354{
3355 if (fp != NULL)
3356 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003357}
3358
3359/*
3360 * Return TRUE if items in "fc" do not have "copyID". That means they are not
3361 * referenced from anywhere that is in use.
3362 */
3363 static int
3364can_free_funccal(funccall_T *fc, int copyID)
3365{
3366 return (fc->l_varlist.lv_copyID != copyID
3367 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003368 && fc->l_avars.dv_copyID != copyID
3369 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003370}
3371
3372/*
3373 * ":return [expr]"
3374 */
3375 void
3376ex_return(exarg_T *eap)
3377{
3378 char_u *arg = eap->arg;
3379 typval_T rettv;
3380 int returning = FALSE;
3381
3382 if (current_funccal == NULL)
3383 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003384 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003385 return;
3386 }
3387
3388 if (eap->skip)
3389 ++emsg_skip;
3390
3391 eap->nextcmd = NULL;
3392 if ((*arg != NUL && *arg != '|' && *arg != '\n')
3393 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
3394 {
3395 if (!eap->skip)
3396 returning = do_return(eap, FALSE, TRUE, &rettv);
3397 else
3398 clear_tv(&rettv);
3399 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003400 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003401 else if (!eap->skip)
3402 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003403 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003404 update_force_abort();
3405
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003406 /*
3407 * Return unless the expression evaluation has been cancelled due to an
3408 * aborting error, an interrupt, or an exception.
3409 */
3410 if (!aborting())
3411 returning = do_return(eap, FALSE, TRUE, NULL);
3412 }
3413
Bram Moolenaare38eab22019-12-05 21:50:01 +01003414 // When skipping or the return gets pending, advance to the next command
3415 // in this line (!returning). Otherwise, ignore the rest of the line.
3416 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003417 if (returning)
3418 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003419 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003420 eap->nextcmd = check_nextcmd(arg);
3421
3422 if (eap->skip)
3423 --emsg_skip;
3424}
3425
3426/*
3427 * ":1,25call func(arg1, arg2)" function call.
3428 */
3429 void
3430ex_call(exarg_T *eap)
3431{
3432 char_u *arg = eap->arg;
3433 char_u *startarg;
3434 char_u *name;
3435 char_u *tofree;
3436 int len;
3437 typval_T rettv;
3438 linenr_T lnum;
3439 int doesrange;
3440 int failed = FALSE;
3441 funcdict_T fudi;
3442 partial_T *partial = NULL;
3443
3444 if (eap->skip)
3445 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003446 // trans_function_name() doesn't work well when skipping, use eval0()
3447 // instead to skip to any following command, e.g. for:
3448 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003449 ++emsg_skip;
3450 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3451 clear_tv(&rettv);
3452 --emsg_skip;
3453 return;
3454 }
3455
3456 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
3457 if (fudi.fd_newkey != NULL)
3458 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003459 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003460 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003461 vim_free(fudi.fd_newkey);
3462 }
3463 if (tofree == NULL)
3464 return;
3465
Bram Moolenaare38eab22019-12-05 21:50:01 +01003466 // Increase refcount on dictionary, it could get deleted when evaluating
3467 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003468 if (fudi.fd_dict != NULL)
3469 ++fudi.fd_dict->dv_refcount;
3470
Bram Moolenaare38eab22019-12-05 21:50:01 +01003471 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3472 // contents. For VAR_PARTIAL get its partial, unless we already have one
3473 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003474 len = (int)STRLEN(tofree);
3475 name = deref_func_name(tofree, &len,
3476 partial != NULL ? NULL : &partial, FALSE);
3477
Bram Moolenaare38eab22019-12-05 21:50:01 +01003478 // Skip white space to allow ":call func ()". Not good, but required for
3479 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003480 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003481 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003482
3483 if (*startarg != '(')
3484 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003486 goto end;
3487 }
3488
3489 /*
3490 * When skipping, evaluate the function once, to find the end of the
3491 * arguments.
3492 * When the function takes a range, this is discovered after the first
3493 * call, and the loop is broken.
3494 */
3495 if (eap->skip)
3496 {
3497 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003498 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003499 }
3500 else
3501 lnum = eap->line1;
3502 for ( ; lnum <= eap->line2; ++lnum)
3503 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003504 funcexe_T funcexe;
3505
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003506 if (!eap->skip && eap->addr_count > 0)
3507 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003508 if (lnum > curbuf->b_ml.ml_line_count)
3509 {
3510 // If the function deleted lines or switched to another buffer
3511 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003512 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003513 break;
3514 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003515 curwin->w_cursor.lnum = lnum;
3516 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003517 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003518 }
3519 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003520
Bram Moolenaarac92e252019-08-03 21:58:38 +02003521 vim_memset(&funcexe, 0, sizeof(funcexe));
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003522 funcexe.firstline = eap->line1;
3523 funcexe.lastline = eap->line2;
3524 funcexe.doesrange = &doesrange;
3525 funcexe.evaluate = !eap->skip;
3526 funcexe.partial = partial;
3527 funcexe.selfdict = fudi.fd_dict;
3528 if (get_func_tv(name, -1, &rettv, &arg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003529 {
3530 failed = TRUE;
3531 break;
3532 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003533 if (has_watchexpr())
3534 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003535
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003536 // Handle a function returning a Funcref, Dictionary or List.
3537 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE,
3538 name, &name) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003539 {
3540 failed = TRUE;
3541 break;
3542 }
3543
3544 clear_tv(&rettv);
3545 if (doesrange || eap->skip)
3546 break;
3547
Bram Moolenaare38eab22019-12-05 21:50:01 +01003548 // Stop when immediately aborting on error, or when an interrupt
3549 // occurred or an exception was thrown but not caught.
3550 // get_func_tv() returned OK, so that the check for trailing
3551 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003552 if (aborting())
3553 break;
3554 }
3555 if (eap->skip)
3556 --emsg_skip;
3557
3558 if (!failed)
3559 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003560 // Check for trailing illegal characters and a following command.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003561 if (!ends_excmd(*arg))
3562 {
3563 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003564 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003565 }
3566 else
3567 eap->nextcmd = check_nextcmd(arg);
3568 }
3569
3570end:
3571 dict_unref(fudi.fd_dict);
3572 vim_free(tofree);
3573}
3574
3575/*
3576 * Return from a function. Possibly makes the return pending. Also called
3577 * for a pending return at the ":endtry" or after returning from an extra
3578 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3579 * when called due to a ":return" command. "rettv" may point to a typval_T
3580 * with the return rettv. Returns TRUE when the return can be carried out,
3581 * FALSE when the return gets pending.
3582 */
3583 int
3584do_return(
3585 exarg_T *eap,
3586 int reanimate,
3587 int is_cmd,
3588 void *rettv)
3589{
3590 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01003591 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003592
3593 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003594 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003595 current_funccal->returned = FALSE;
3596
3597 /*
3598 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3599 * not in its finally clause (which then is to be executed next) is found.
3600 * In this case, make the ":return" pending for execution at the ":endtry".
3601 * Otherwise, return normally.
3602 */
3603 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3604 if (idx >= 0)
3605 {
3606 cstack->cs_pending[idx] = CSTP_RETURN;
3607
3608 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003609 // A pending return again gets pending. "rettv" points to an
3610 // allocated variable with the rettv of the original ":return"'s
3611 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003612 cstack->cs_rettv[idx] = rettv;
3613 else
3614 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003615 // When undoing a return in order to make it pending, get the stored
3616 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003617 if (reanimate)
3618 rettv = current_funccal->rettv;
3619
3620 if (rettv != NULL)
3621 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003622 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003623 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
3624 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
3625 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003626 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003627 }
3628 else
3629 cstack->cs_rettv[idx] = NULL;
3630
3631 if (reanimate)
3632 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003633 // The pending return value could be overwritten by a ":return"
3634 // without argument in a finally clause; reset the default
3635 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003636 current_funccal->rettv->v_type = VAR_NUMBER;
3637 current_funccal->rettv->vval.v_number = 0;
3638 }
3639 }
3640 report_make_pending(CSTP_RETURN, rettv);
3641 }
3642 else
3643 {
3644 current_funccal->returned = TRUE;
3645
Bram Moolenaare38eab22019-12-05 21:50:01 +01003646 // If the return is carried out now, store the return value. For
3647 // a return immediately after reanimation, the value is already
3648 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003649 if (!reanimate && rettv != NULL)
3650 {
3651 clear_tv(current_funccal->rettv);
3652 *current_funccal->rettv = *(typval_T *)rettv;
3653 if (!is_cmd)
3654 vim_free(rettv);
3655 }
3656 }
3657
3658 return idx < 0;
3659}
3660
3661/*
3662 * Free the variable with a pending return value.
3663 */
3664 void
3665discard_pending_return(void *rettv)
3666{
3667 free_tv((typval_T *)rettv);
3668}
3669
3670/*
3671 * Generate a return command for producing the value of "rettv". The result
3672 * is an allocated string. Used by report_pending() for verbose messages.
3673 */
3674 char_u *
3675get_return_cmd(void *rettv)
3676{
3677 char_u *s = NULL;
3678 char_u *tofree = NULL;
3679 char_u numbuf[NUMBUFLEN];
3680
3681 if (rettv != NULL)
3682 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
3683 if (s == NULL)
3684 s = (char_u *)"";
3685
3686 STRCPY(IObuff, ":return ");
3687 STRNCPY(IObuff + 8, s, IOSIZE - 8);
3688 if (STRLEN(s) + 8 >= IOSIZE)
3689 STRCPY(IObuff + IOSIZE - 4, "...");
3690 vim_free(tofree);
3691 return vim_strsave(IObuff);
3692}
3693
3694/*
3695 * Get next function line.
3696 * Called by do_cmdline() to get the next line.
3697 * Returns allocated string, or NULL for end of function.
3698 */
3699 char_u *
3700get_func_line(
3701 int c UNUSED,
3702 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003703 int indent UNUSED,
3704 int do_concat UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003705{
3706 funccall_T *fcp = (funccall_T *)cookie;
3707 ufunc_T *fp = fcp->func;
3708 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003709 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003710
Bram Moolenaare38eab22019-12-05 21:50:01 +01003711 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003712 if (fcp->dbg_tick != debug_tick)
3713 {
3714 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003715 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003716 fcp->dbg_tick = debug_tick;
3717 }
3718#ifdef FEAT_PROFILE
3719 if (do_profiling == PROF_YES)
3720 func_line_end(cookie);
3721#endif
3722
3723 gap = &fp->uf_lines;
3724 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3725 || fcp->returned)
3726 retval = NULL;
3727 else
3728 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003729 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003730 while (fcp->linenr < gap->ga_len
3731 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
3732 ++fcp->linenr;
3733 if (fcp->linenr >= gap->ga_len)
3734 retval = NULL;
3735 else
3736 {
3737 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003738 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003739#ifdef FEAT_PROFILE
3740 if (do_profiling == PROF_YES)
3741 func_line_start(cookie);
3742#endif
3743 }
3744 }
3745
Bram Moolenaare38eab22019-12-05 21:50:01 +01003746 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003747 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003748 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003749 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003750 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003751 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003752 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003753 fcp->dbg_tick = debug_tick;
3754 }
3755
3756 return retval;
3757}
3758
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003759/*
3760 * Return TRUE if the currently active function should be ended, because a
3761 * return was encountered or an error occurred. Used inside a ":while".
3762 */
3763 int
3764func_has_ended(void *cookie)
3765{
3766 funccall_T *fcp = (funccall_T *)cookie;
3767
Bram Moolenaare38eab22019-12-05 21:50:01 +01003768 // Ignore the "abort" flag if the abortion behavior has been changed due to
3769 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003770 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3771 || fcp->returned);
3772}
3773
3774/*
3775 * return TRUE if cookie indicates a function which "abort"s on errors.
3776 */
3777 int
3778func_has_abort(
3779 void *cookie)
3780{
3781 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
3782}
3783
3784
3785/*
3786 * Turn "dict.Func" into a partial for "Func" bound to "dict".
3787 * Don't do this when "Func" is already a partial that was bound
3788 * explicitly (pt_auto is FALSE).
3789 * Changes "rettv" in-place.
3790 * Returns the updated "selfdict_in".
3791 */
3792 dict_T *
3793make_partial(dict_T *selfdict_in, typval_T *rettv)
3794{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003795 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003796 char_u *tofree = NULL;
3797 ufunc_T *fp;
3798 char_u fname_buf[FLEN_FIXED + 1];
3799 int error;
3800 dict_T *selfdict = selfdict_in;
3801
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003802 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
3803 fp = rettv->vval.v_partial->pt_func;
3804 else
3805 {
3806 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
3807 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003808 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003809 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 fp = find_func(fname, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003811 vim_free(tofree);
3812 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003813
3814 if (fp != NULL && (fp->uf_flags & FC_DICT))
3815 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003816 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003817
3818 if (pt != NULL)
3819 {
3820 pt->pt_refcount = 1;
3821 pt->pt_dict = selfdict;
3822 pt->pt_auto = TRUE;
3823 selfdict = NULL;
3824 if (rettv->v_type == VAR_FUNC)
3825 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003826 // Just a function: Take over the function name and use
3827 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003828 pt->pt_name = rettv->vval.v_string;
3829 }
3830 else
3831 {
3832 partial_T *ret_pt = rettv->vval.v_partial;
3833 int i;
3834
Bram Moolenaare38eab22019-12-05 21:50:01 +01003835 // Partial: copy the function name, use selfdict and copy
3836 // args. Can't take over name or args, the partial might
3837 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003838 if (ret_pt->pt_name != NULL)
3839 {
3840 pt->pt_name = vim_strsave(ret_pt->pt_name);
3841 func_ref(pt->pt_name);
3842 }
3843 else
3844 {
3845 pt->pt_func = ret_pt->pt_func;
3846 func_ptr_ref(pt->pt_func);
3847 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003848 if (ret_pt->pt_argc > 0)
3849 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003850 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003851 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003852 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003853 pt->pt_argc = 0;
3854 else
3855 {
3856 pt->pt_argc = ret_pt->pt_argc;
3857 for (i = 0; i < pt->pt_argc; i++)
3858 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
3859 }
3860 }
3861 partial_unref(ret_pt);
3862 }
3863 rettv->v_type = VAR_PARTIAL;
3864 rettv->vval.v_partial = pt;
3865 }
3866 }
3867 return selfdict;
3868}
3869
3870/*
3871 * Return the name of the executed function.
3872 */
3873 char_u *
3874func_name(void *cookie)
3875{
3876 return ((funccall_T *)cookie)->func->uf_name;
3877}
3878
3879/*
3880 * Return the address holding the next breakpoint line for a funccall cookie.
3881 */
3882 linenr_T *
3883func_breakpoint(void *cookie)
3884{
3885 return &((funccall_T *)cookie)->breakpoint;
3886}
3887
3888/*
3889 * Return the address holding the debug tick for a funccall cookie.
3890 */
3891 int *
3892func_dbg_tick(void *cookie)
3893{
3894 return &((funccall_T *)cookie)->dbg_tick;
3895}
3896
3897/*
3898 * Return the nesting level for a funccall cookie.
3899 */
3900 int
3901func_level(void *cookie)
3902{
3903 return ((funccall_T *)cookie)->level;
3904}
3905
3906/*
3907 * Return TRUE when a function was ended by a ":return" command.
3908 */
3909 int
3910current_func_returned(void)
3911{
3912 return current_funccal->returned;
3913}
3914
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003915 int
3916free_unref_funccal(int copyID, int testing)
3917{
3918 int did_free = FALSE;
3919 int did_free_funccal = FALSE;
3920 funccall_T *fc, **pfc;
3921
3922 for (pfc = &previous_funccal; *pfc != NULL; )
3923 {
3924 if (can_free_funccal(*pfc, copyID))
3925 {
3926 fc = *pfc;
3927 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003928 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003929 did_free = TRUE;
3930 did_free_funccal = TRUE;
3931 }
3932 else
3933 pfc = &(*pfc)->caller;
3934 }
3935 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003936 // When a funccal was freed some more items might be garbage
3937 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003938 (void)garbage_collect(testing);
3939
3940 return did_free;
3941}
3942
3943/*
Bram Moolenaarba209902016-08-24 22:06:38 +02003944 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003945 */
3946 static funccall_T *
3947get_funccal(void)
3948{
3949 int i;
3950 funccall_T *funccal;
3951 funccall_T *temp_funccal;
3952
3953 funccal = current_funccal;
3954 if (debug_backtrace_level > 0)
3955 {
3956 for (i = 0; i < debug_backtrace_level; i++)
3957 {
3958 temp_funccal = funccal->caller;
3959 if (temp_funccal)
3960 funccal = temp_funccal;
3961 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003962 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003963 debug_backtrace_level = i;
3964 }
3965 }
3966 return funccal;
3967}
3968
3969/*
3970 * Return the hashtable used for local variables in the current funccal.
3971 * Return NULL if there is no current funccal.
3972 */
3973 hashtab_T *
3974get_funccal_local_ht()
3975{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003977 return NULL;
3978 return &get_funccal()->l_vars.dv_hashtab;
3979}
3980
3981/*
3982 * Return the l: scope variable.
3983 * Return NULL if there is no current funccal.
3984 */
3985 dictitem_T *
3986get_funccal_local_var()
3987{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003989 return NULL;
3990 return &get_funccal()->l_vars_var;
3991}
3992
3993/*
3994 * Return the hashtable used for argument in the current funccal.
3995 * Return NULL if there is no current funccal.
3996 */
3997 hashtab_T *
3998get_funccal_args_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_avars.dv_hashtab;
4003}
4004
4005/*
4006 * Return the a: scope variable.
4007 * Return NULL if there is no current funccal.
4008 */
4009 dictitem_T *
4010get_funccal_args_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;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004014 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004015}
4016
4017/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004018 * List function variables, if there is a function.
4019 */
4020 void
4021list_func_vars(int *first)
4022{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004024 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004025 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004026}
4027
4028/*
4029 * If "ht" is the hashtable for local variables in the current funccal, return
4030 * the dict that contains it.
4031 * Otherwise return NULL.
4032 */
4033 dict_T *
4034get_current_funccal_dict(hashtab_T *ht)
4035{
4036 if (current_funccal != NULL
4037 && ht == &current_funccal->l_vars.dv_hashtab)
4038 return &current_funccal->l_vars;
4039 return NULL;
4040}
4041
4042/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004043 * Search hashitem in parent scope.
4044 */
4045 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004046find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004047{
4048 funccall_T *old_current_funccal = current_funccal;
4049 hashtab_T *ht;
4050 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004051 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004052
4053 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4054 return NULL;
4055
Bram Moolenaare38eab22019-12-05 21:50:01 +01004056 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004057 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004058 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004059 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004060 ht = find_var_ht(name, &varname);
4061 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004062 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004063 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004064 if (!HASHITEM_EMPTY(hi))
4065 {
4066 *pht = ht;
4067 break;
4068 }
4069 }
4070 if (current_funccal == current_funccal->func->uf_scoped)
4071 break;
4072 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004073 }
4074 current_funccal = old_current_funccal;
4075
4076 return hi;
4077}
4078
4079/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004080 * Search variable in parent scope.
4081 */
4082 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004083find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004084{
4085 dictitem_T *v = NULL;
4086 funccall_T *old_current_funccal = current_funccal;
4087 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004088 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004089
4090 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4091 return NULL;
4092
Bram Moolenaare38eab22019-12-05 21:50:01 +01004093 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004094 current_funccal = current_funccal->func->uf_scoped;
4095 while (current_funccal)
4096 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004097 ht = find_var_ht(name, &varname);
4098 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004099 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004100 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004101 if (v != NULL)
4102 break;
4103 }
4104 if (current_funccal == current_funccal->func->uf_scoped)
4105 break;
4106 current_funccal = current_funccal->func->uf_scoped;
4107 }
4108 current_funccal = old_current_funccal;
4109
4110 return v;
4111}
4112
4113/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004114 * Set "copyID + 1" in previous_funccal and callers.
4115 */
4116 int
4117set_ref_in_previous_funccal(int copyID)
4118{
4119 int abort = FALSE;
4120 funccall_T *fc;
4121
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004122 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004123 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004124 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004125 abort = abort
4126 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4127 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004128 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004129 }
4130 return abort;
4131}
4132
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004133 static int
4134set_ref_in_funccal(funccall_T *fc, int copyID)
4135{
4136 int abort = FALSE;
4137
4138 if (fc->fc_copyID != copyID)
4139 {
4140 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004141 abort = abort
4142 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
4143 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004144 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004145 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004146 }
4147 return abort;
4148}
4149
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004150/*
4151 * Set "copyID" in all local vars and arguments in the call stack.
4152 */
4153 int
4154set_ref_in_call_stack(int copyID)
4155{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004156 int abort = FALSE;
4157 funccall_T *fc;
4158 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004159
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004160 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004161 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004162
4163 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004164 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
4165 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004166 abort = abort || set_ref_in_funccal(fc, copyID);
4167
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004168 return abort;
4169}
4170
4171/*
4172 * Set "copyID" in all functions available by name.
4173 */
4174 int
4175set_ref_in_functions(int copyID)
4176{
4177 int todo;
4178 hashitem_T *hi = NULL;
4179 int abort = FALSE;
4180 ufunc_T *fp;
4181
4182 todo = (int)func_hashtab.ht_used;
4183 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004184 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004185 if (!HASHITEM_EMPTY(hi))
4186 {
4187 --todo;
4188 fp = HI2UF(hi);
4189 if (!func_name_refcount(fp->uf_name))
4190 abort = abort || set_ref_in_func(NULL, fp, copyID);
4191 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004192 }
4193 return abort;
4194}
4195
4196/*
4197 * Set "copyID" in all function arguments.
4198 */
4199 int
4200set_ref_in_func_args(int copyID)
4201{
4202 int i;
4203 int abort = FALSE;
4204
4205 for (i = 0; i < funcargs.ga_len; ++i)
4206 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
4207 copyID, NULL, NULL);
4208 return abort;
4209}
4210
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004211/*
4212 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004213 * Returns TRUE if setting references failed somehow.
4214 */
4215 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004216set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004217{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004218 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004219 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01004220 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004221 char_u fname_buf[FLEN_FIXED + 1];
4222 char_u *tofree = NULL;
4223 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004224 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004225
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004226 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004227 return FALSE;
4228
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004229 if (fp_in == NULL)
4230 {
4231 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232 fp = find_func(fname, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004233 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004234 if (fp != NULL)
4235 {
4236 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004237 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004238 }
4239 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004240 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004241}
4242
Bram Moolenaare38eab22019-12-05 21:50:01 +01004243#endif // FEAT_EVAL