blob: c6c6cec3cb955928758370573f4541d5f13a4dd8 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +020011 * userfunc.c: User defined function support
Bram Moolenaara9b579f2016-07-17 18:29:19 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar93343722018-07-10 19:39:18 +020017// flags used in uf_flags
18#define FC_ABORT 0x01 // abort function on error
19#define FC_RANGE 0x02 // function accepts range
20#define FC_DICT 0x04 // Dict function, uses "self"
21#define FC_CLOSURE 0x08 // closure, uses outer scope variables
22#define FC_DELETED 0x10 // :delfunction used while uf_refcount > 0
23#define FC_REMOVED 0x20 // function redefined while uf_refcount > 0
24#define FC_SANDBOX 0x40 // function defined in the sandbox
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010025#define FC_DEAD 0x80 // function kept only for reference to dfunc
26#define FC_EXPORT 0x100 // "export def Func()"
Bram Moolenaara9b579f2016-07-17 18:29:19 +020027
Bram Moolenaara9b579f2016-07-17 18:29:19 +020028/*
29 * All user-defined functions are found in this hashtable.
30 */
31static hashtab_T func_hashtab;
32
Bram Moolenaare38eab22019-12-05 21:50:01 +010033// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020034static garray_T funcargs = GA_EMPTY;
35
Bram Moolenaar209b8e32019-03-14 13:43:24 +010036// pointer to funccal for currently active function
37static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020038
Bram Moolenaar209b8e32019-03-14 13:43:24 +010039// Pointer to list of previously used funccal, still around because some
40// item in it is still being used.
41static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020042
43static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
44static char *e_funcdict = N_("E717: Dictionary entry already exists");
45static char *e_funcref = N_("E718: Funcref required");
46static char *e_nofunc = N_("E130: Unknown function: %s");
47
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020048static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020049
50 void
51func_init()
52{
53 hash_init(&func_hashtab);
54}
55
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020056/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020057 * Return the function hash table
58 */
59 hashtab_T *
60func_tbl_get(void)
61{
62 return &func_hashtab;
63}
64
65/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010066 * Get one function argument and an optional type: "arg: type".
67 * Return a pointer to after the type.
68 * When something is wrong return "arg".
69 */
70 static char_u *
71one_function_arg(char_u *arg, garray_T *newargs, garray_T *argtypes, int skip)
72{
73 char_u *p = arg;
74
75 while (ASCII_ISALNUM(*p) || *p == '_')
76 ++p;
77 if (arg == p || isdigit(*arg)
78 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
79 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
80 {
81 if (!skip)
82 semsg(_("E125: Illegal argument: %s"), arg);
83 return arg;
84 }
85 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
86 return arg;
87 if (newargs != NULL)
88 {
89 char_u *arg_copy;
90 int c;
91 int i;
92
93 c = *p;
94 *p = NUL;
95 arg_copy = vim_strsave(arg);
96 if (arg_copy == NULL)
97 {
98 *p = c;
99 return arg;
100 }
101
102 // Check for duplicate argument name.
103 for (i = 0; i < newargs->ga_len; ++i)
104 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
105 {
106 semsg(_("E853: Duplicate argument name: %s"), arg_copy);
107 vim_free(arg_copy);
108 return arg;
109 }
110 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
111 newargs->ga_len++;
112
113 *p = c;
114 }
115
116 // get any type from "arg: type"
117 if (argtypes != NULL && ga_grow(argtypes, 1) == OK)
118 {
119 char_u *type = NULL;
120
121 if (*p == ':')
122 {
123 type = skipwhite(p + 1);
124 p = skip_type(type);
125 type = vim_strnsave(type, p - type);
126 }
127 else if (*skipwhite(p) == ':')
128 emsg(_("E1059: No white space allowed before :"));
129 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
130 }
131
132 return p;
133}
134
135/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200136 * Get function arguments.
137 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100138 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200139get_function_args(
140 char_u **argp,
141 char_u endchar,
142 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143 garray_T *argtypes, // NULL unless using :def
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200144 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200145 garray_T *default_args,
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200146 int skip)
147{
148 int mustend = FALSE;
149 char_u *arg = *argp;
150 char_u *p = arg;
151 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200152 int any_default = FALSE;
153 char_u *expr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200154
155 if (newargs != NULL)
156 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100157 if (argtypes != NULL)
158 ga_init2(argtypes, (int)sizeof(char_u *), 3);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200159 if (default_args != NULL)
160 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200161
162 if (varargs != NULL)
163 *varargs = FALSE;
164
165 /*
166 * Isolate the arguments: "arg1, arg2, ...)"
167 */
168 while (*p != endchar)
169 {
170 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
171 {
172 if (varargs != NULL)
173 *varargs = TRUE;
174 p += 3;
175 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100176
177 if (argtypes != NULL)
178 {
179 // ...name: list<type>
180 if (!ASCII_ISALPHA(*p))
181 {
182 emsg(_("E1055: Missing name after ..."));
183 break;
184 }
185
186 arg = p;
187 p = one_function_arg(p, newargs, argtypes, skip);
188 if (p == arg)
189 break;
190 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200191 }
192 else
193 {
194 arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100195 p = one_function_arg(p, newargs, argtypes, skip);
196 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200197 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200198
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200199 if (*skipwhite(p) == '=' && default_args != NULL)
200 {
201 typval_T rettv;
202
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100203 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200204 any_default = TRUE;
205 p = skipwhite(p) + 1;
206 p = skipwhite(p);
207 expr = p;
208 if (eval1(&p, &rettv, FALSE) != FAIL)
209 {
210 if (ga_grow(default_args, 1) == FAIL)
211 goto err_ret;
212
213 // trim trailing whitespace
214 while (p > expr && VIM_ISWHITE(p[-1]))
215 p--;
216 c = *p;
217 *p = NUL;
218 expr = vim_strsave(expr);
219 if (expr == NULL)
220 {
221 *p = c;
222 goto err_ret;
223 }
224 ((char_u **)(default_args->ga_data))
225 [default_args->ga_len] = expr;
226 default_args->ga_len++;
227 *p = c;
228 }
229 else
230 mustend = TRUE;
231 }
232 else if (any_default)
233 {
234 emsg(_("E989: Non-default argument follows default argument"));
235 mustend = TRUE;
236 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200237 if (*p == ',')
238 ++p;
239 else
240 mustend = TRUE;
241 }
242 p = skipwhite(p);
243 if (mustend && *p != endchar)
244 {
245 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100246 semsg(_(e_invarg2), *argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200247 break;
248 }
249 }
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200250 if (*p != endchar)
251 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100252 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200253
254 *argp = p;
255 return OK;
256
257err_ret:
258 if (newargs != NULL)
259 ga_clear_strings(newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200260 if (default_args != NULL)
261 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200262 return FAIL;
263}
264
265/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200266 * Register function "fp" as using "current_funccal" as its scope.
267 */
268 static int
269register_closure(ufunc_T *fp)
270{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200271 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100272 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200273 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200274 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200275 fp->uf_scoped = current_funccal;
276 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200277
Bram Moolenaar58016442016-07-31 18:30:22 +0200278 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
279 return FAIL;
280 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
281 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200282 return OK;
283}
284
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100285 static void
286set_ufunc_name(ufunc_T *fp, char_u *name)
287{
288 STRCPY(fp->uf_name, name);
289
290 if (name[0] == K_SPECIAL)
291 {
292 fp->uf_name_exp = alloc(STRLEN(name) + 3);
293 if (fp->uf_name_exp != NULL)
294 {
295 STRCPY(fp->uf_name_exp, "<SNR>");
296 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
297 }
298 }
299}
300
Bram Moolenaar58016442016-07-31 18:30:22 +0200301/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200302 * Parse a lambda expression and get a Funcref from "*arg".
303 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
304 */
305 int
306get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
307{
308 garray_T newargs;
309 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200310 garray_T *pnewargs;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200311 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100312 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200313 int varargs;
314 int ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200315 char_u *start = skipwhite(*arg + 1);
316 char_u *s, *e;
317 static int lambda_no = 0;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200318 int *old_eval_lavars = eval_lavars_used;
319 int eval_lavars = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200320
321 ga_init(&newargs);
322 ga_init(&newlines);
323
Bram Moolenaare38eab22019-12-05 21:50:01 +0100324 // First, check if this is a lambda expression. "->" must exist.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100325 ret = get_function_args(&start, '-', NULL, NULL, NULL, NULL, TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200326 if (ret == FAIL || *start != '>')
327 return NOTDONE;
328
Bram Moolenaare38eab22019-12-05 21:50:01 +0100329 // Parse the arguments again.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200330 if (evaluate)
331 pnewargs = &newargs;
332 else
333 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200334 *arg = skipwhite(*arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100335 // TODO: argument types
336 ret = get_function_args(arg, '-', pnewargs, NULL, &varargs, NULL, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200337 if (ret == FAIL || **arg != '>')
338 goto errret;
339
Bram Moolenaare38eab22019-12-05 21:50:01 +0100340 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200341 if (evaluate)
342 eval_lavars_used = &eval_lavars;
343
Bram Moolenaare38eab22019-12-05 21:50:01 +0100344 // Get the start and the end of the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200345 *arg = skipwhite(*arg + 1);
346 s = *arg;
347 ret = skip_expr(arg);
348 if (ret == FAIL)
349 goto errret;
350 e = *arg;
351 *arg = skipwhite(*arg);
352 if (**arg != '}')
353 goto errret;
354 ++*arg;
355
356 if (evaluate)
357 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200358 int len, flags = 0;
359 char_u *p;
360 char_u name[20];
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200361
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200362 sprintf((char*)name, "<lambda>%d", ++lambda_no);
363
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200364 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200365 if (fp == NULL)
366 goto errret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100367 fp->uf_dfunc_idx = -1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200368 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200369 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200370 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200371
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200372 ga_init2(&newlines, (int)sizeof(char_u *), 1);
373 if (ga_grow(&newlines, 1) == FAIL)
374 goto errret;
375
Bram Moolenaare38eab22019-12-05 21:50:01 +0100376 // Add "return " before the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200377 len = 7 + e - s + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200378 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200379 if (p == NULL)
380 goto errret;
381 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
382 STRCPY(p, "return ");
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200383 vim_strncpy(p + 7, s, e - s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200384
385 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100386 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200387 hash_add(&func_hashtab, UF2HIKEY(fp));
388 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200389 ga_init(&fp->uf_def_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200390 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200391 if (current_funccal != NULL && eval_lavars)
392 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200393 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200394 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200395 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200396 }
397 else
398 fp->uf_scoped = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200399
400#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200401 if (prof_def_func())
402 func_do_profile(fp);
403#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200404 if (sandbox)
405 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100406 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200407 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200408 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200409 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200410 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100411 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200412
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200413 pt->pt_func = fp;
414 pt->pt_refcount = 1;
415 rettv->vval.v_partial = pt;
416 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200417 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200418
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200419 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200420 return OK;
421
422errret:
423 ga_clear_strings(&newargs);
424 ga_clear_strings(&newlines);
425 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100426 vim_free(pt);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200427 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200428 return FAIL;
429}
430
431/*
432 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
433 * name it contains, otherwise return "name".
434 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
435 * "partialp".
436 */
437 char_u *
438deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
439{
440 dictitem_T *v;
441 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200442 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200443
444 if (partialp != NULL)
445 *partialp = NULL;
446
447 cc = name[*lenp];
448 name[*lenp] = NUL;
449 v = find_var(name, NULL, no_autoload);
450 name[*lenp] = cc;
451 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
452 {
453 if (v->di_tv.vval.v_string == NULL)
454 {
455 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100456 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200457 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200458 s = v->di_tv.vval.v_string;
459 *lenp = (int)STRLEN(s);
460 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200461 }
462
463 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
464 {
465 partial_T *pt = v->di_tv.vval.v_partial;
466
467 if (pt == NULL)
468 {
469 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100470 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200471 }
472 if (partialp != NULL)
473 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200474 s = partial_name(pt);
475 *lenp = (int)STRLEN(s);
476 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200477 }
478
479 return name;
480}
481
482/*
483 * Give an error message with a function name. Handle <SNR> things.
484 * "ermsg" is to be passed without translation, use N_() instead of _().
485 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100486 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200487emsg_funcname(char *ermsg, char_u *name)
488{
489 char_u *p;
490
491 if (*name == K_SPECIAL)
492 p = concat_str((char_u *)"<SNR>", name + 3);
493 else
494 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100495 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200496 if (p != name)
497 vim_free(p);
498}
499
500/*
501 * Allocate a variable for the result of a function.
502 * Return OK or FAIL.
503 */
504 int
505get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200506 char_u *name, // name of the function
507 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200508 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200509 char_u **arg, // argument, pointing to the '('
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200510 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200511{
512 char_u *argp;
513 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100514 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
515 int argcount = 0; // number of arguments found
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200516
517 /*
518 * Get the arguments.
519 */
520 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200521 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
522 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200523 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100524 argp = skipwhite(argp + 1); // skip the '(' or ','
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200525 if (*argp == ')' || *argp == ',' || *argp == NUL)
526 break;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200527 if (eval1(&argp, &argvars[argcount], funcexe->evaluate) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200528 {
529 ret = FAIL;
530 break;
531 }
532 ++argcount;
533 if (*argp != ',')
534 break;
535 }
536 if (*argp == ')')
537 ++argp;
538 else
539 ret = FAIL;
540
541 if (ret == OK)
542 {
543 int i = 0;
544
545 if (get_vim_var_nr(VV_TESTING))
546 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100547 // Prepare for calling test_garbagecollect_now(), need to know
548 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200549 if (funcargs.ga_itemsize == 0)
550 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
551 for (i = 0; i < argcount; ++i)
552 if (ga_grow(&funcargs, 1) == OK)
553 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
554 &argvars[i];
555 }
556
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200557 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200558
559 funcargs.ga_len -= i;
560 }
561 else if (!aborting())
562 {
563 if (argcount == MAX_FUNC_ARGS)
564 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
565 else
566 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
567 }
568
569 while (--argcount >= 0)
570 clear_tv(&argvars[argcount]);
571
572 *arg = skipwhite(argp);
573 return ret;
574}
575
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200576/*
577 * Return TRUE if "p" starts with "<SID>" or "s:".
578 * Only works if eval_fname_script() returned non-zero for "p"!
579 */
580 static int
581eval_fname_sid(char_u *p)
582{
583 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
584}
585
586/*
587 * In a script change <SID>name() and s:name() to K_SNR 123_name().
588 * Change <SNR>123_name() to K_SNR 123_name().
589 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
590 * (slow).
591 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100592 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200593fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
594{
595 int llen;
596 char_u *fname;
597 int i;
598
599 llen = eval_fname_script(name);
600 if (llen > 0)
601 {
602 fname_buf[0] = K_SPECIAL;
603 fname_buf[1] = KS_EXTRA;
604 fname_buf[2] = (int)KE_SNR;
605 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100606 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200607 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200608 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100609 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200610 else
611 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200612 sprintf((char *)fname_buf + 3, "%ld_",
613 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200614 i = (int)STRLEN(fname_buf);
615 }
616 }
617 if (i + STRLEN(name + llen) < FLEN_FIXED)
618 {
619 STRCPY(fname_buf + i, name + llen);
620 fname = fname_buf;
621 }
622 else
623 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200624 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200625 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +0100626 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200627 else
628 {
629 *tofree = fname;
630 mch_memmove(fname, fname_buf, (size_t)i);
631 STRCPY(fname + i, name + llen);
632 }
633 }
634 }
635 else
636 fname = name;
637 return fname;
638}
639
640/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641 * Find a function "name" in script "sid".
642 */
643 static ufunc_T *
644find_func_with_sid(char_u *name, int sid)
645{
646 hashitem_T *hi;
647 char_u buffer[200];
648
649 buffer[0] = K_SPECIAL;
650 buffer[1] = KS_EXTRA;
651 buffer[2] = (int)KE_SNR;
652 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
653 (long)sid, name);
654 hi = hash_find(&func_hashtab, buffer);
655 if (!HASHITEM_EMPTY(hi))
656 return HI2UF(hi);
657
658 return NULL;
659}
660
661/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200662 * Find a function by name, return pointer to it in ufuncs.
663 * Return NULL for unknown function.
664 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665 static ufunc_T *
666find_func_even_dead(char_u *name, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200667{
668 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669 ufunc_T *func;
670 imported_T *imported;
671
672 if (in_vim9script())
673 {
674 // Find script-local function before global one.
675 func = find_func_with_sid(name, current_sctx.sc_sid);
676 if (func != NULL)
677 return func;
678
679 // Find imported funcion before global one.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +0100680 imported = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 if (imported != NULL && imported->imp_funcname != NULL)
682 {
683 hi = hash_find(&func_hashtab, imported->imp_funcname);
684 if (!HASHITEM_EMPTY(hi))
685 return HI2UF(hi);
686 }
687 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200688
689 hi = hash_find(&func_hashtab, name);
690 if (!HASHITEM_EMPTY(hi))
691 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100692
693 return NULL;
694}
695
696/*
697 * Find a function by name, return pointer to it in ufuncs.
698 * "cctx" is passed in a :def function to find imported functions.
699 * Return NULL for unknown or dead function.
700 */
701 ufunc_T *
702find_func(char_u *name, cctx_T *cctx)
703{
704 ufunc_T *fp = find_func_even_dead(name, cctx);
705
706 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
707 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200708 return NULL;
709}
710
711/*
712 * Copy the function name of "fp" to buffer "buf".
713 * "buf" must be able to hold the function name plus three bytes.
714 * Takes care of script-local function names.
715 */
716 static void
717cat_func_name(char_u *buf, ufunc_T *fp)
718{
719 if (fp->uf_name[0] == K_SPECIAL)
720 {
721 STRCPY(buf, "<SNR>");
722 STRCAT(buf, fp->uf_name + 3);
723 }
724 else
725 STRCPY(buf, fp->uf_name);
726}
727
728/*
729 * Add a number variable "name" to dict "dp" with value "nr".
730 */
731 static void
732add_nr_var(
733 dict_T *dp,
734 dictitem_T *v,
735 char *name,
736 varnumber_T nr)
737{
738 STRCPY(v->di_key, name);
739 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
740 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
741 v->di_tv.v_type = VAR_NUMBER;
742 v->di_tv.v_lock = VAR_FIXED;
743 v->di_tv.vval.v_number = nr;
744}
745
746/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100747 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200748 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100749 static void
750free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200751{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100752 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200753
754 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
755 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100756 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200757
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100758 // When garbage collecting a funccall_T may be freed before the
759 // function that references it, clear its uf_scoped field.
760 // The function may have been redefined and point to another
761 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200762 if (fp != NULL && fp->uf_scoped == fc)
763 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200764 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200765 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200766
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200767 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200768 vim_free(fc);
769}
770
771/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100772 * Free "fc" and what it contains.
773 * Can be called only when "fc" is kept beyond the period of it called,
774 * i.e. after cleanup_function_call(fc).
775 */
776 static void
777free_funccal_contents(funccall_T *fc)
778{
779 listitem_T *li;
780
781 // Free all l: variables.
782 vars_clear(&fc->l_vars.dv_hashtab);
783
784 // Free all a: variables.
785 vars_clear(&fc->l_avars.dv_hashtab);
786
787 // Free the a:000 variables.
788 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
789 clear_tv(&li->li_tv);
790
791 free_funccal(fc);
792}
793
794/*
Bram Moolenaar6914c642017-04-01 21:21:30 +0200795 * Handle the last part of returning from a function: free the local hashtable.
796 * Unless it is still in use by a closure.
797 */
798 static void
799cleanup_function_call(funccall_T *fc)
800{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100801 int may_free_fc = fc->fc_refcount <= 0;
802 int free_fc = TRUE;
803
Bram Moolenaar6914c642017-04-01 21:21:30 +0200804 current_funccal = fc->caller;
805
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100806 // Free all l: variables if not referred.
807 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
808 vars_clear(&fc->l_vars.dv_hashtab);
809 else
810 free_fc = FALSE;
811
812 // If the a:000 list and the l: and a: dicts are not referenced and
813 // there is no closure using it, we can free the funccall_T and what's
814 // in it.
815 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
816 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200817 else
818 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100819 int todo;
820 hashitem_T *hi;
821 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200822
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100823 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200824
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100825 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200826 todo = (int)fc->l_avars.dv_hashtab.ht_used;
827 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
828 {
829 if (!HASHITEM_EMPTY(hi))
830 {
831 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100832 di = HI2DI(hi);
833 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200834 }
835 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100836 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200837
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100838 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
839 fc->l_varlist.lv_first = NULL;
840 else
841 {
842 listitem_T *li;
843
844 free_fc = FALSE;
845
846 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200847 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
848 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100849 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100850
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100851 if (free_fc)
852 free_funccal(fc);
853 else
854 {
855 static int made_copy = 0;
856
857 // "fc" is still in use. This can happen when returning "a:000",
858 // assigning "l:" to a global variable or defining a closure.
859 // Link "fc" in the list for garbage collection later.
860 fc->caller = previous_funccal;
861 previous_funccal = fc;
862
863 if (want_garbage_collect)
864 // If garbage collector is ready, clear count.
865 made_copy = 0;
866 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100867 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100868 // We have made a lot of copies, worth 4 Mbyte. This can happen
869 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100870 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100871 // too much memory.
872 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100873 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100874 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200875 }
876}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100877/*
878 * Unreference "fc": decrement the reference count and free it when it
879 * becomes zero. "fp" is detached from "fc".
880 * When "force" is TRUE we are exiting.
881 */
882 static void
883funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
884{
885 funccall_T **pfc;
886 int i;
887
888 if (fc == NULL)
889 return;
890
891 if (--fc->fc_refcount <= 0 && (force || (
892 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
893 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
894 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
895 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
896 {
897 if (fc == *pfc)
898 {
899 *pfc = fc->caller;
900 free_funccal_contents(fc);
901 return;
902 }
903 }
904 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
905 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
906 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
907}
908
909/*
910 * Remove the function from the function hashtable. If the function was
911 * deleted while it still has references this was already done.
912 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
913 */
914 static int
915func_remove(ufunc_T *fp)
916{
917 hashitem_T *hi;
918
919 // Return if it was already virtually deleted.
920 if (fp->uf_flags & FC_DEAD)
921 return FALSE;
922
923 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
924 if (!HASHITEM_EMPTY(hi))
925 {
926 // When there is a def-function index do not actually remove the
927 // function, so we can find the index when defining the function again.
928 if (fp->uf_dfunc_idx >= 0)
929 fp->uf_flags |= FC_DEAD;
930 else
931 hash_remove(&func_hashtab, hi);
932 return TRUE;
933 }
934 return FALSE;
935}
936
937 static void
938func_clear_items(ufunc_T *fp)
939{
940 ga_clear_strings(&(fp->uf_args));
941 ga_clear_strings(&(fp->uf_def_args));
942 ga_clear_strings(&(fp->uf_lines));
943 VIM_CLEAR(fp->uf_name_exp);
944 VIM_CLEAR(fp->uf_arg_types);
945 ga_clear(&fp->uf_type_list);
946#ifdef FEAT_PROFILE
947 VIM_CLEAR(fp->uf_tml_count);
948 VIM_CLEAR(fp->uf_tml_total);
949 VIM_CLEAR(fp->uf_tml_self);
950#endif
951}
952
953/*
954 * Free all things that a function contains. Does not free the function
955 * itself, use func_free() for that.
956 * When "force" is TRUE we are exiting.
957 */
958 static void
959func_clear(ufunc_T *fp, int force)
960{
961 if (fp->uf_cleared)
962 return;
963 fp->uf_cleared = TRUE;
964
965 // clear this function
966 func_clear_items(fp);
967 funccal_unref(fp->uf_scoped, fp, force);
968 delete_def_function(fp);
969}
970
971/*
972 * Free a function and remove it from the list of functions. Does not free
973 * what a function contains, call func_clear() first.
974 */
975 static void
976func_free(ufunc_T *fp)
977{
978 // Only remove it when not done already, otherwise we would remove a newer
979 // version of the function with the same name.
980 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
981 func_remove(fp);
982
983 if ((fp->uf_flags & FC_DEAD) == 0)
984 vim_free(fp);
985}
986
987/*
988 * Free all things that a function contains and free the function itself.
989 * When "force" is TRUE we are exiting.
990 */
991 static void
992func_clear_free(ufunc_T *fp, int force)
993{
994 func_clear(fp, force);
995 func_free(fp);
996}
997
Bram Moolenaar6914c642017-04-01 21:21:30 +0200998
999/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001000 * Call a user function.
1001 */
1002 static void
1003call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001004 ufunc_T *fp, // pointer to function
1005 int argcount, // nr of args
1006 typval_T *argvars, // arguments
1007 typval_T *rettv, // return value
1008 linenr_T firstline, // first line of range
1009 linenr_T lastline, // last line of range
1010 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001011{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001012 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001013 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001014 funccall_T *fc;
1015 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001016 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001017 static int depth = 0;
1018 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001019 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001020 int i;
1021 int ai;
1022 int islambda = FALSE;
1023 char_u numbuf[NUMBUFLEN];
1024 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001025#ifdef FEAT_PROFILE
1026 proftime_T wait_start;
1027 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001028 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001029#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001030 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001031
Bram Moolenaare38eab22019-12-05 21:50:01 +01001032 // If depth of calling is getting too high, don't execute the function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001033 if (depth >= p_mfd)
1034 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001035 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001036 rettv->v_type = VAR_NUMBER;
1037 rettv->vval.v_number = -1;
1038 return;
1039 }
1040 ++depth;
1041
Bram Moolenaare38eab22019-12-05 21:50:01 +01001042 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001043
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001044 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001045 if (fc == NULL)
1046 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001047 fc->caller = current_funccal;
1048 current_funccal = fc;
1049 fc->func = fp;
1050 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001051 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001052 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001053 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1054 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001055 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001056 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001057 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001058
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 if (fp->uf_dfunc_idx >= 0)
1060 {
1061 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001062 save_current_sctx = current_sctx;
1063 current_sctx = fp->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001064
1065 // Execute the compiled function.
1066 call_def_function(fp, argcount, argvars, rettv);
1067 --depth;
1068 current_funccal = fc->caller;
1069
1070 estack_pop();
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001071 current_sctx = save_current_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001072 free_funccal(fc);
1073 return;
1074 }
1075
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001076 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1077 islambda = TRUE;
1078
1079 /*
1080 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1081 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1082 * each argument variable and saves a lot of time.
1083 */
1084 /*
1085 * Init l: variables.
1086 */
1087 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1088 if (selfdict != NULL)
1089 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001090 // Set l:self to "selfdict". Use "name" to avoid a warning from
1091 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001092 v = &fc->fixvar[fixvar_idx++].var;
1093 name = v->di_key;
1094 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001095 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001096 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1097 v->di_tv.v_type = VAR_DICT;
1098 v->di_tv.v_lock = 0;
1099 v->di_tv.vval.v_dict = selfdict;
1100 ++selfdict->dv_refcount;
1101 }
1102
1103 /*
1104 * Init a: variables.
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001105 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001106 * Set a:000 to a list with room for the "..." arguments.
1107 */
1108 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
1109 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001110 (varnumber_T)(argcount >= fp->uf_args.ga_len
1111 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001112 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001113 // Use "name" to avoid a warning from some compiler that checks the
1114 // destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001115 v = &fc->fixvar[fixvar_idx++].var;
1116 name = v->di_key;
1117 STRCPY(name, "000");
1118 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1119 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1120 v->di_tv.v_type = VAR_LIST;
1121 v->di_tv.v_lock = VAR_FIXED;
1122 v->di_tv.vval.v_list = &fc->l_varlist;
1123 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
1124 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1125 fc->l_varlist.lv_lock = VAR_FIXED;
1126
1127 /*
1128 * Set a:firstline to "firstline" and a:lastline to "lastline".
1129 * Set a:name to named arguments.
1130 * Set a:N to the "..." arguments.
1131 */
1132 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
1133 (varnumber_T)firstline);
1134 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
1135 (varnumber_T)lastline);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001136 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001137 {
1138 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001139 typval_T def_rettv;
1140 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001141
1142 ai = i - fp->uf_args.ga_len;
1143 if (ai < 0)
1144 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001145 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001146 name = FUNCARG(fp, i);
1147 if (islambda)
1148 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001149
1150 // evaluate named argument default expression
1151 isdefault = ai + fp->uf_def_args.ga_len >= 0
1152 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1153 && argvars[i].vval.v_number == VVAL_NONE));
1154 if (isdefault)
1155 {
1156 char_u *default_expr = NULL;
1157 def_rettv.v_type = VAR_NUMBER;
1158 def_rettv.vval.v_number = -1;
1159
1160 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1161 [ai + fp->uf_def_args.ga_len];
1162 if (eval1(&default_expr, &def_rettv, TRUE) == FAIL)
1163 {
1164 default_arg_err = 1;
1165 break;
1166 }
1167 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001168 }
1169 else
1170 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001171 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001172 sprintf((char *)numbuf, "%d", ai + 1);
1173 name = numbuf;
1174 }
1175 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1176 {
1177 v = &fc->fixvar[fixvar_idx++].var;
1178 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001179 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001180 }
1181 else
1182 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001183 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001184 if (v == NULL)
1185 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001186 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001187 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001188
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001189 // Note: the values are copied directly to avoid alloc/free.
1190 // "argvars" must have VAR_FIXED for v_lock.
1191 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001192 v->di_tv.v_lock = VAR_FIXED;
1193
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001194 if (addlocal)
1195 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001196 // Named arguments should be accessed without the "a:" prefix in
1197 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001198 copy_tv(&v->di_tv, &v->di_tv);
1199 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001200 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001201 else
1202 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001203
1204 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1205 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001206 listitem_T *li = &fc->l_listitems[ai];
1207
1208 li->li_tv = argvars[i];
1209 li->li_tv.v_lock = VAR_FIXED;
1210 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001211 }
1212 }
1213
Bram Moolenaare38eab22019-12-05 21:50:01 +01001214 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001215 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001216
1217 if (fp->uf_flags & FC_SANDBOX)
1218 {
1219 using_sandbox = TRUE;
1220 ++sandbox;
1221 }
1222
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001223 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001224 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001225 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001226 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001227 ++no_wait_return;
1228 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001229
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001230 smsg(_("calling %s"), SOURCING_NAME);
1231 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001232 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001233 char_u buf[MSG_BUF_LEN];
1234 char_u numbuf2[NUMBUFLEN];
1235 char_u *tofree;
1236 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001237
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001238 msg_puts("(");
1239 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001240 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001241 if (i > 0)
1242 msg_puts(", ");
1243 if (argvars[i].v_type == VAR_NUMBER)
1244 msg_outnum((long)argvars[i].vval.v_number);
1245 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001246 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001247 // Do not want errors such as E724 here.
1248 ++emsg_off;
1249 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1250 --emsg_off;
1251 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001252 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001253 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001254 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001255 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1256 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001257 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001258 msg_puts((char *)s);
1259 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001260 }
1261 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001262 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001263 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001264 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001265 msg_puts("\n"); // don't overwrite this either
1266
1267 verbose_leave_scroll();
1268 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001269 }
1270#ifdef FEAT_PROFILE
1271 if (do_profiling == PROF_YES)
1272 {
1273 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001274 {
1275 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001276 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001277 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001278 if (fp->uf_profiling
1279 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1280 {
1281 ++fp->uf_tm_count;
1282 profile_start(&call_start);
1283 profile_zero(&fp->uf_tm_children);
1284 }
1285 script_prof_save(&wait_start);
1286 }
1287#endif
1288
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001289 save_current_sctx = current_sctx;
1290 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001291 save_did_emsg = did_emsg;
1292 did_emsg = FALSE;
1293
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001294 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1295 did_emsg = TRUE;
1296 else
1297 // call do_cmdline() to execute the lines
1298 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001299 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1300
1301 --RedrawingDisabled;
1302
Bram Moolenaare38eab22019-12-05 21:50:01 +01001303 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001304 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1305 {
1306 clear_tv(rettv);
1307 rettv->v_type = VAR_NUMBER;
1308 rettv->vval.v_number = -1;
1309 }
1310
1311#ifdef FEAT_PROFILE
1312 if (do_profiling == PROF_YES && (fp->uf_profiling
1313 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1314 {
1315 profile_end(&call_start);
1316 profile_sub_wait(&wait_start, &call_start);
1317 profile_add(&fp->uf_tm_total, &call_start);
1318 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1319 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1320 {
1321 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1322 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1323 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001324 if (started_profiling)
1325 // make a ":profdel func" stop profiling the function
1326 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001327 }
1328#endif
1329
Bram Moolenaare38eab22019-12-05 21:50:01 +01001330 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001331 if (p_verbose >= 12)
1332 {
1333 ++no_wait_return;
1334 verbose_enter_scroll();
1335
1336 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001337 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001338 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001339 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001340 (long)fc->rettv->vval.v_number);
1341 else
1342 {
1343 char_u buf[MSG_BUF_LEN];
1344 char_u numbuf2[NUMBUFLEN];
1345 char_u *tofree;
1346 char_u *s;
1347
Bram Moolenaare38eab22019-12-05 21:50:01 +01001348 // The value may be very long. Skip the middle part, so that we
1349 // have some idea how it starts and ends. smsg() would always
1350 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001351 ++emsg_off;
1352 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1353 --emsg_off;
1354 if (s != NULL)
1355 {
1356 if (vim_strsize(s) > MSG_BUF_CLEN)
1357 {
1358 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1359 s = buf;
1360 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001361 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001362 vim_free(tofree);
1363 }
1364 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001365 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001366
1367 verbose_leave_scroll();
1368 --no_wait_return;
1369 }
1370
Bram Moolenaare31ee862020-01-07 20:59:34 +01001371 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001372 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001373 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001374#ifdef FEAT_PROFILE
1375 if (do_profiling == PROF_YES)
1376 script_prof_restore(&wait_start);
1377#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001378 if (using_sandbox)
1379 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001380
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001381 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001382 {
1383 ++no_wait_return;
1384 verbose_enter_scroll();
1385
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001386 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001387 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001388
1389 verbose_leave_scroll();
1390 --no_wait_return;
1391 }
1392
1393 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001394 --depth;
1395
Bram Moolenaar6914c642017-04-01 21:21:30 +02001396 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001397}
1398
1399/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001401 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402 int
1403call_user_func_check(
1404 ufunc_T *fp,
1405 int argcount,
1406 typval_T *argvars,
1407 typval_T *rettv,
1408 funcexe_T *funcexe,
1409 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001410{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 int error;
1412 int regular_args = fp->uf_args.ga_len;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001413
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001414 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1415 *funcexe->doesrange = TRUE;
1416 if (argcount < regular_args - fp->uf_def_args.ga_len)
1417 error = FCERR_TOOFEW;
1418 else if (!has_varargs(fp) && argcount > regular_args)
1419 error = FCERR_TOOMANY;
1420 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1421 error = FCERR_DICT;
1422 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001423 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 int did_save_redo = FALSE;
1425 save_redo_T save_redo;
1426
1427 /*
1428 * Call the user function.
1429 * Save and restore search patterns, script variables and
1430 * redo buffer.
1431 */
1432 save_search_patterns();
1433 if (!ins_compl_active())
1434 {
1435 saveRedobuff(&save_redo);
1436 did_save_redo = TRUE;
1437 }
1438 ++fp->uf_calls;
1439 call_user_func(fp, argcount, argvars, rettv,
1440 funcexe->firstline, funcexe->lastline,
1441 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
1442 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
1443 // Function was unreferenced while being used, free it now.
1444 func_clear_free(fp, FALSE);
1445 if (did_save_redo)
1446 restoreRedobuff(&save_redo);
1447 restore_search_patterns();
1448 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001449 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001451}
1452
1453/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001454 * There are two kinds of function names:
1455 * 1. ordinary names, function defined with :function
1456 * 2. numbered functions and lambdas
1457 * For the first we only count the name stored in func_hashtab as a reference,
1458 * using function() does not count as a reference, because the function is
1459 * looked up by name.
1460 */
1461 static int
1462func_name_refcount(char_u *name)
1463{
1464 return isdigit(*name) || *name == '<';
1465}
1466
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001467static funccal_entry_T *funccal_stack = NULL;
1468
1469/*
1470 * Save the current function call pointer, and set it to NULL.
1471 * Used when executing autocommands and for ":source".
1472 */
1473 void
1474save_funccal(funccal_entry_T *entry)
1475{
1476 entry->top_funccal = current_funccal;
1477 entry->next = funccal_stack;
1478 funccal_stack = entry;
1479 current_funccal = NULL;
1480}
1481
1482 void
1483restore_funccal(void)
1484{
1485 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001486 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001487 else
1488 {
1489 current_funccal = funccal_stack->top_funccal;
1490 funccal_stack = funccal_stack->next;
1491 }
1492}
1493
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001494 funccall_T *
1495get_current_funccal(void)
1496{
1497 return current_funccal;
1498}
1499
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001500#if defined(EXITFREE) || defined(PROTO)
1501 void
1502free_all_functions(void)
1503{
1504 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001505 ufunc_T *fp;
1506 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001507 long_u todo = 1;
1508 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001509
Bram Moolenaare38eab22019-12-05 21:50:01 +01001510 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001511 while (current_funccal != NULL)
1512 {
1513 clear_tv(current_funccal->rettv);
1514 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001515 if (current_funccal == NULL && funccal_stack != NULL)
1516 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001517 }
1518
Bram Moolenaare38eab22019-12-05 21:50:01 +01001519 // First clear what the functions contain. Since this may lower the
1520 // reference count of a function, it may also free a function and change
1521 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001522 while (todo > 0)
1523 {
1524 todo = func_hashtab.ht_used;
1525 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1526 if (!HASHITEM_EMPTY(hi))
1527 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 // clear the def function index now
1529 fp = HI2UF(hi);
1530 fp->uf_flags &= ~FC_DEAD;
1531 fp->uf_dfunc_idx = -1;
1532
Bram Moolenaare38eab22019-12-05 21:50:01 +01001533 // Only free functions that are not refcounted, those are
1534 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001535 if (func_name_refcount(fp->uf_name))
1536 ++skipped;
1537 else
1538 {
1539 used = func_hashtab.ht_used;
1540 func_clear(fp, TRUE);
1541 if (used != func_hashtab.ht_used)
1542 {
1543 skipped = 0;
1544 break;
1545 }
1546 }
1547 --todo;
1548 }
1549 }
1550
Bram Moolenaare38eab22019-12-05 21:50:01 +01001551 // Now actually free the functions. Need to start all over every time,
1552 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001553 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001554 while (func_hashtab.ht_used > skipped)
1555 {
1556 todo = func_hashtab.ht_used;
1557 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001558 if (!HASHITEM_EMPTY(hi))
1559 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001560 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001561 // Only free functions that are not refcounted, those are
1562 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02001563 fp = HI2UF(hi);
1564 if (func_name_refcount(fp->uf_name))
1565 ++skipped;
1566 else
1567 {
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001568 func_free(fp);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001569 skipped = 0;
1570 break;
1571 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001572 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001573 }
1574 if (skipped == 0)
1575 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576
1577 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001578}
1579#endif
1580
1581/*
1582 * Return TRUE if "name" looks like a builtin function name: starts with a
1583 * lower case letter and doesn't contain AUTOLOAD_CHAR.
1584 * "len" is the length of "name", or -1 for NUL terminated.
1585 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001587builtin_function(char_u *name, int len)
1588{
1589 char_u *p;
1590
1591 if (!ASCII_ISLOWER(name[0]))
1592 return FALSE;
1593 p = vim_strchr(name, AUTOLOAD_CHAR);
1594 return p == NULL || (len > 0 && p > name + len);
1595}
1596
1597 int
1598func_call(
1599 char_u *name,
1600 typval_T *args,
1601 partial_T *partial,
1602 dict_T *selfdict,
1603 typval_T *rettv)
1604{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001605 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001606 listitem_T *item;
1607 typval_T argv[MAX_FUNC_ARGS + 1];
1608 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001609 int r = 0;
1610
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001611 range_list_materialize(l);
1612 for (item = l->lv_first; item != NULL; item = item->li_next)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001613 {
1614 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1615 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001616 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001617 break;
1618 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001619 // Make a copy of each argument. This is needed to be able to set
1620 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001621 copy_tv(&item->li_tv, &argv[argc++]);
1622 }
1623
1624 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001625 {
1626 funcexe_T funcexe;
1627
Bram Moolenaarac92e252019-08-03 21:58:38 +02001628 vim_memset(&funcexe, 0, sizeof(funcexe));
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001629 funcexe.firstline = curwin->w_cursor.lnum;
1630 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001631 funcexe.evaluate = TRUE;
1632 funcexe.partial = partial;
1633 funcexe.selfdict = selfdict;
1634 r = call_func(name, -1, rettv, argc, argv, &funcexe);
1635 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001636
Bram Moolenaare38eab22019-12-05 21:50:01 +01001637 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001638 while (argc > 0)
1639 clear_tv(&argv[--argc]);
1640
1641 return r;
1642}
1643
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001644static int callback_depth = 0;
1645
1646 int
1647get_callback_depth(void)
1648{
1649 return callback_depth;
1650}
1651
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001652/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001653 * Invoke call_func() with a callback.
1654 */
1655 int
1656call_callback(
1657 callback_T *callback,
1658 int len, // length of "name" or -1 to use strlen()
1659 typval_T *rettv, // return value goes here
1660 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001661 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001662 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001663{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001664 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001665 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001666
1667 vim_memset(&funcexe, 0, sizeof(funcexe));
1668 funcexe.evaluate = TRUE;
1669 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001670 ++callback_depth;
1671 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
1672 --callback_depth;
1673 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001674}
1675
1676/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677 * Give an error message for the result of a function.
1678 * Nothing if "error" is FCERR_NONE.
1679 */
1680 void
1681user_func_error(int error, char_u *name)
1682{
1683 switch (error)
1684 {
1685 case FCERR_UNKNOWN:
1686 emsg_funcname(e_unknownfunc, name);
1687 break;
1688 case FCERR_NOTMETHOD:
1689 emsg_funcname(
1690 N_("E276: Cannot use function as a method: %s"), name);
1691 break;
1692 case FCERR_DELETED:
1693 emsg_funcname(N_(e_func_deleted), name);
1694 break;
1695 case FCERR_TOOMANY:
1696 emsg_funcname((char *)e_toomanyarg, name);
1697 break;
1698 case FCERR_TOOFEW:
1699 emsg_funcname((char *)e_toofewarg, name);
1700 break;
1701 case FCERR_SCRIPT:
1702 emsg_funcname(
1703 N_("E120: Using <SID> not in a script context: %s"), name);
1704 break;
1705 case FCERR_DICT:
1706 emsg_funcname(
1707 N_("E725: Calling dict function without Dictionary: %s"),
1708 name);
1709 break;
1710 }
1711}
1712
1713/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001714 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001715 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001716 * Return FAIL when the function can't be called, OK otherwise.
1717 * Also returns OK when an error was encountered while executing the function.
1718 */
1719 int
1720call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001721 char_u *funcname, // name of the function
1722 int len, // length of "name" or -1 to use strlen()
1723 typval_T *rettv, // return value goes here
1724 int argcount_in, // number of "argvars"
1725 typval_T *argvars_in, // vars for arguments, must have "argcount"
1726 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001727 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001728{
1729 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01001730 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001731 int i;
1732 ufunc_T *fp;
1733 char_u fname_buf[FLEN_FIXED + 1];
1734 char_u *tofree = NULL;
1735 char_u *fname;
1736 char_u *name;
1737 int argcount = argcount_in;
1738 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001739 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001740 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
1741 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001742 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001743 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001744 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001745
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02001746 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
1747 // even when call_func() returns FAIL.
1748 rettv->v_type = VAR_UNKNOWN;
1749
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001750 // Make a copy of the name, if it comes from a funcref variable it could
1751 // be changed or deleted in the called function.
1752 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001753 if (name == NULL)
1754 return ret;
1755
1756 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1757
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001758 if (funcexe->doesrange != NULL)
1759 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001760
1761 if (partial != NULL)
1762 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001763 // When the function has a partial with a dict and there is a dict
1764 // argument, use the dict argument. That is backwards compatible.
1765 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001766 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001767 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01001768 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001769 {
1770 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001771 {
1772 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
1773 {
Bram Moolenaaref140542019-12-31 21:27:13 +01001774 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001775 goto theend;
1776 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001777 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001778 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001779 for (i = 0; i < argcount_in; ++i)
1780 argv[i + argv_clear] = argvars_in[i];
1781 argvars = argv;
1782 argcount = partial->pt_argc + argcount_in;
1783 }
1784 }
1785
Bram Moolenaaref140542019-12-31 21:27:13 +01001786 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001787 {
1788 char_u *rfname = fname;
1789
Bram Moolenaare38eab22019-12-05 21:50:01 +01001790 // Ignore "g:" before a function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001791 if (fname[0] == 'g' && fname[1] == ':')
1792 rfname = fname + 2;
1793
Bram Moolenaare38eab22019-12-05 21:50:01 +01001794 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001795 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01001796 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001797
1798 if (!builtin_function(rfname, -1))
1799 {
1800 /*
1801 * User defined function.
1802 */
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001803 if (partial != NULL && partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001804 fp = partial->pt_func;
1805 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001807
Bram Moolenaare38eab22019-12-05 21:50:01 +01001808 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001809 if (fp == NULL
1810 && apply_autocmds(EVENT_FUNCUNDEFINED,
1811 rfname, rfname, TRUE, NULL)
1812 && !aborting())
1813 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001814 // executed an autocommand, search for the function again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001816 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001817 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001818 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
1819 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001820 // loaded a package, search for the function again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001822 }
1823
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001824 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01001825 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001826 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001827 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001828 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01001829 // postponed filling in the arguments, do it now
1830 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001831 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001832
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001833 if (funcexe->basetv != NULL)
1834 {
1835 // Method call: base->Method()
1836 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
1837 argv[0] = *funcexe->basetv;
1838 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001839 argvars = argv;
1840 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001841 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 error = call_user_func_check(fp, argcount, argvars, rettv,
1844 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001845 }
1846 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001847 else if (funcexe->basetv != NULL)
1848 {
1849 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001850 * expr->method(): Find the method name in the table, call its
1851 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02001852 */
1853 error = call_internal_method(fname, argcount, argvars, rettv,
1854 funcexe->basetv);
1855 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001856 else
1857 {
1858 /*
1859 * Find the function name in the table, call its implementation.
1860 */
1861 error = call_internal_func(fname, argcount, argvars, rettv);
1862 }
1863 /*
1864 * The function call (or "FuncUndefined" autocommand sequence) might
1865 * have been aborted by an error, an interrupt, or an explicitly thrown
1866 * exception that has not been caught so far. This situation can be
1867 * tested for by calling aborting(). For an error in an internal
1868 * function or for the "E132" error in call_user_func(), however, the
1869 * throw point at which the "force_abort" flag (temporarily reset by
1870 * emsg()) is normally updated has not been reached yet. We need to
1871 * update that flag first to make aborting() reliable.
1872 */
1873 update_force_abort();
1874 }
Bram Moolenaaref140542019-12-31 21:27:13 +01001875 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001876 ret = OK;
1877
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001878theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001879 /*
1880 * Report an error unless the argument evaluation or function call has been
1881 * cancelled due to an aborting error, an interrupt, or an exception.
1882 */
1883 if (!aborting())
1884 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001885 user_func_error(error, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001886 }
1887
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001888 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001889 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001890 clear_tv(&argv[--argv_clear + argv_base]);
1891
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001892 vim_free(tofree);
1893 vim_free(name);
1894
1895 return ret;
1896}
1897
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898 static char_u *
1899printable_func_name(ufunc_T *fp)
1900{
1901 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
1902}
1903
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001904/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001905 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001906 */
1907 static void
1908list_func_head(ufunc_T *fp, int indent)
1909{
1910 int j;
1911
1912 msg_start();
1913 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001914 msg_puts(" ");
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001915 if (fp->uf_dfunc_idx >= 0)
1916 msg_puts("def ");
1917 else
1918 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001919 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001920 msg_putchar('(');
1921 for (j = 0; j < fp->uf_args.ga_len; ++j)
1922 {
1923 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001924 msg_puts(", ");
1925 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926 if (fp->uf_arg_types != NULL)
1927 {
1928 char *tofree;
1929
1930 msg_puts(": ");
1931 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
1932 vim_free(tofree);
1933 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001934 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
1935 {
1936 msg_puts(" = ");
1937 msg_puts(((char **)(fp->uf_def_args.ga_data))
1938 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
1939 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001940 }
1941 if (fp->uf_varargs)
1942 {
1943 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001944 msg_puts(", ");
1945 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001946 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947 if (fp->uf_va_name != NULL)
1948 {
1949 if (j)
1950 msg_puts(", ");
1951 msg_puts("...");
1952 msg_puts((char *)fp->uf_va_name);
1953 if (fp->uf_va_type)
1954 {
1955 char *tofree;
1956
1957 msg_puts(": ");
1958 msg_puts(type_name(fp->uf_va_type, &tofree));
1959 vim_free(tofree);
1960 }
1961 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001962 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001963
1964 if (fp->uf_dfunc_idx >= 0)
1965 {
1966 if (fp->uf_ret_type != &t_void)
1967 {
1968 char *tofree;
1969
1970 msg_puts(": ");
1971 msg_puts(type_name(fp->uf_ret_type, &tofree));
1972 vim_free(tofree);
1973 }
1974 }
1975 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001976 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001977 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001978 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001979 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001980 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001981 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001982 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001983 msg_clr_eos();
1984 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001985 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001986}
1987
1988/*
1989 * Get a function name, translating "<SID>" and "<SNR>".
1990 * Also handles a Funcref in a List or Dictionary.
1991 * Returns the function name in allocated memory, or NULL for failure.
1992 * flags:
1993 * TFN_INT: internal function name OK
1994 * TFN_QUIET: be quiet
1995 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001996 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001997 * Advances "pp" to just after the function name (if no error).
1998 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001999 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002000trans_function_name(
2001 char_u **pp,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002002 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002003 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002004 funcdict_T *fdp, // return: info about dictionary used
2005 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002006{
2007 char_u *name = NULL;
2008 char_u *start;
2009 char_u *end;
2010 int lead;
2011 char_u sid_buf[20];
2012 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002014 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015 int vim9script;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002016
2017 if (fdp != NULL)
2018 vim_memset(fdp, 0, sizeof(funcdict_T));
2019 start = *pp;
2020
Bram Moolenaare38eab22019-12-05 21:50:01 +01002021 // Check for hard coded <SNR>: already translated function ID (from a user
2022 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002023 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2024 && (*pp)[2] == (int)KE_SNR)
2025 {
2026 *pp += 3;
2027 len = get_id_len(pp) + 3;
2028 return vim_strnsave(start, len);
2029 }
2030
Bram Moolenaare38eab22019-12-05 21:50:01 +01002031 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2032 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002033 lead = eval_fname_script(start);
2034 if (lead > 2)
2035 start += lead;
2036
Bram Moolenaare38eab22019-12-05 21:50:01 +01002037 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002038 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002039 lead > 2 ? 0 : FNE_CHECK_START);
2040 if (end == start)
2041 {
2042 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002043 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002044 goto theend;
2045 }
2046 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2047 {
2048 /*
2049 * Report an invalid expression in braces, unless the expression
2050 * evaluation has been cancelled due to an aborting error, an
2051 * interrupt, or an exception.
2052 */
2053 if (!aborting())
2054 {
2055 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002056 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002057 }
2058 else
2059 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2060 goto theend;
2061 }
2062
2063 if (lv.ll_tv != NULL)
2064 {
2065 if (fdp != NULL)
2066 {
2067 fdp->fd_dict = lv.ll_dict;
2068 fdp->fd_newkey = lv.ll_newkey;
2069 lv.ll_newkey = NULL;
2070 fdp->fd_di = lv.ll_di;
2071 }
2072 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2073 {
2074 name = vim_strsave(lv.ll_tv->vval.v_string);
2075 *pp = end;
2076 }
2077 else if (lv.ll_tv->v_type == VAR_PARTIAL
2078 && lv.ll_tv->vval.v_partial != NULL)
2079 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002080 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002081 *pp = end;
2082 if (partial != NULL)
2083 *partial = lv.ll_tv->vval.v_partial;
2084 }
2085 else
2086 {
2087 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2088 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002089 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002090 else
2091 *pp = end;
2092 name = NULL;
2093 }
2094 goto theend;
2095 }
2096
2097 if (lv.ll_name == NULL)
2098 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002099 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002100 *pp = end;
2101 goto theend;
2102 }
2103
Bram Moolenaare38eab22019-12-05 21:50:01 +01002104 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002105 if (lv.ll_exp_name != NULL)
2106 {
2107 len = (int)STRLEN(lv.ll_exp_name);
2108 name = deref_func_name(lv.ll_exp_name, &len, partial,
2109 flags & TFN_NO_AUTOLOAD);
2110 if (name == lv.ll_exp_name)
2111 name = NULL;
2112 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002113 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002114 {
2115 len = (int)(end - *pp);
2116 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2117 if (name == *pp)
2118 name = NULL;
2119 }
2120 if (name != NULL)
2121 {
2122 name = vim_strsave(name);
2123 *pp = end;
2124 if (STRNCMP(name, "<SNR>", 5) == 0)
2125 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002126 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002127 name[0] = K_SPECIAL;
2128 name[1] = KS_EXTRA;
2129 name[2] = (int)KE_SNR;
2130 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2131 }
2132 goto theend;
2133 }
2134
2135 if (lv.ll_exp_name != NULL)
2136 {
2137 len = (int)STRLEN(lv.ll_exp_name);
2138 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2139 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2140 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002141 // When there was "s:" already or the name expanded to get a
2142 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002143 lv.ll_name += 2;
2144 len -= 2;
2145 lead = 2;
2146 }
2147 }
2148 else
2149 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002150 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002151 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
2152 lv.ll_name += 2;
2153 len = (int)(end - lv.ll_name);
2154 }
2155
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156 // In Vim9 script a user function is script-local by default.
2157 vim9script = ASCII_ISUPPER(*start)
2158 && current_sctx.sc_version == SCRIPT_VERSION_VIM9;
2159
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002160 /*
2161 * Copy the function name to allocated memory.
2162 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2163 * Accept <SNR>123_name() outside a script.
2164 */
2165 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002166 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002167 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002168 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 if (!vim9script)
2170 lead = 3;
2171 if (vim9script || (lv.ll_exp_name != NULL
2172 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002173 || eval_fname_sid(*pp))
2174 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002176 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002177 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002178 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002179 goto theend;
2180 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002181 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002182 if (vim9script)
2183 extra = 3 + (int)STRLEN(sid_buf);
2184 else
2185 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002186 }
2187 }
2188 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2189 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002190 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002191 start);
2192 goto theend;
2193 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002194 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002195 {
2196 char_u *cp = vim_strchr(lv.ll_name, ':');
2197
2198 if (cp != NULL && cp < end)
2199 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002200 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002201 goto theend;
2202 }
2203 }
2204
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002206 if (name != NULL)
2207 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002208 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002209 {
2210 name[0] = K_SPECIAL;
2211 name[1] = KS_EXTRA;
2212 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002213 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002214 STRCPY(name + 3, sid_buf);
2215 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002216 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2217 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002218 }
2219 *pp = end;
2220
2221theend:
2222 clear_lval(&lv);
2223 return name;
2224}
2225
2226/*
2227 * ":function"
2228 */
2229 void
2230ex_function(exarg_T *eap)
2231{
2232 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002233 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002234 int j;
2235 int c;
2236 int saved_did_emsg;
2237 int saved_wait_return = need_wait_return;
2238 char_u *name = NULL;
2239 char_u *p;
2240 char_u *arg;
2241 char_u *line_arg = NULL;
2242 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002243 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002244 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002245 garray_T newlines;
2246 int varargs = FALSE;
2247 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002249 ufunc_T *fp;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002250 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002251 int indent;
2252 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253#define MAX_FUNC_NESTING 50
2254 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002255 dictitem_T *v;
2256 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002257 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002258 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002259 int todo;
2260 hashitem_T *hi;
Bram Moolenaare96a2492019-06-25 04:12:16 +02002261 int do_concat = TRUE;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002262 linenr_T sourcing_lnum_off;
2263 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002264 int is_heredoc = FALSE;
2265 char_u *skip_until = NULL;
2266 char_u *heredoc_trimmed = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002267
2268 /*
2269 * ":function" without argument: list functions.
2270 */
2271 if (ends_excmd(*eap->arg))
2272 {
2273 if (!eap->skip)
2274 {
2275 todo = (int)func_hashtab.ht_used;
2276 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2277 {
2278 if (!HASHITEM_EMPTY(hi))
2279 {
2280 --todo;
2281 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002282 if ((fp->uf_flags & FC_DEAD)
2283 || message_filtered(fp->uf_name))
Bram Moolenaarf86db782018-10-25 13:31:37 +02002284 continue;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002285 if (!func_name_refcount(fp->uf_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002286 list_func_head(fp, FALSE);
2287 }
2288 }
2289 }
2290 eap->nextcmd = check_nextcmd(eap->arg);
2291 return;
2292 }
2293
2294 /*
2295 * ":function /pat": list functions matching pattern.
2296 */
2297 if (*eap->arg == '/')
2298 {
2299 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
2300 if (!eap->skip)
2301 {
2302 regmatch_T regmatch;
2303
2304 c = *p;
2305 *p = NUL;
2306 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2307 *p = c;
2308 if (regmatch.regprog != NULL)
2309 {
2310 regmatch.rm_ic = p_ic;
2311
2312 todo = (int)func_hashtab.ht_used;
2313 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2314 {
2315 if (!HASHITEM_EMPTY(hi))
2316 {
2317 --todo;
2318 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002319 if ((fp->uf_flags & FC_DEAD) == 0
2320 && !isdigit(*fp->uf_name)
2321 && vim_regexec(&regmatch, fp->uf_name, 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002322 list_func_head(fp, FALSE);
2323 }
2324 }
2325 vim_regfree(regmatch.regprog);
2326 }
2327 }
2328 if (*p == '/')
2329 ++p;
2330 eap->nextcmd = check_nextcmd(p);
2331 return;
2332 }
2333
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334 ga_init(&newargs);
2335 ga_init(&argtypes);
2336 ga_init(&default_args);
2337
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002338 /*
2339 * Get the function name. There are these situations:
2340 * func normal function name
2341 * "name" == func, "fudi.fd_dict" == NULL
2342 * dict.func new dictionary entry
2343 * "name" == NULL, "fudi.fd_dict" set,
2344 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
2345 * dict.func existing dict entry with a Funcref
2346 * "name" == func, "fudi.fd_dict" set,
2347 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2348 * dict.func existing dict entry that's not a Funcref
2349 * "name" == NULL, "fudi.fd_dict" set,
2350 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2351 * s:func script-local function name
2352 * g:func global function name, same as "func"
2353 */
2354 p = eap->arg;
Bram Moolenaar3388d332017-12-07 22:23:04 +01002355 name = trans_function_name(&p, eap->skip, TFN_NO_AUTOLOAD, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002356 paren = (vim_strchr(p, '(') != NULL);
2357 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
2358 {
2359 /*
2360 * Return on an invalid expression in braces, unless the expression
2361 * evaluation has been cancelled due to an aborting error, an
2362 * interrupt, or an exception.
2363 */
2364 if (!aborting())
2365 {
2366 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002367 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002368 vim_free(fudi.fd_newkey);
2369 return;
2370 }
2371 else
2372 eap->skip = TRUE;
2373 }
2374
Bram Moolenaare38eab22019-12-05 21:50:01 +01002375 // An error in a function call during evaluation of an expression in magic
2376 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002377 saved_did_emsg = did_emsg;
2378 did_emsg = FALSE;
2379
2380 /*
2381 * ":function func" with only function name: list function.
2382 */
2383 if (!paren)
2384 {
2385 if (!ends_excmd(*skipwhite(p)))
2386 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002387 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002388 goto ret_free;
2389 }
2390 eap->nextcmd = check_nextcmd(p);
2391 if (eap->nextcmd != NULL)
2392 *p = NUL;
2393 if (!eap->skip && !got_int)
2394 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 fp = find_func(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002396 if (fp != NULL)
2397 {
2398 list_func_head(fp, TRUE);
2399 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2400 {
2401 if (FUNCLINE(fp, j) == NULL)
2402 continue;
2403 msg_putchar('\n');
2404 msg_outnum((long)(j + 1));
2405 if (j < 9)
2406 msg_putchar(' ');
2407 if (j < 99)
2408 msg_putchar(' ');
2409 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002410 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002411 ui_breakcheck();
2412 }
2413 if (!got_int)
2414 {
2415 msg_putchar('\n');
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 if (fp->uf_dfunc_idx >= 0)
2417 msg_puts(" enddef");
2418 else
2419 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002420 }
2421 }
2422 else
2423 emsg_funcname(N_("E123: Undefined function: %s"), name);
2424 }
2425 goto ret_free;
2426 }
2427
2428 /*
2429 * ":function name(arg1, arg2)" Define function.
2430 */
2431 p = skipwhite(p);
2432 if (*p != '(')
2433 {
2434 if (!eap->skip)
2435 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002436 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002437 goto ret_free;
2438 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002439 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002440 if (vim_strchr(p, '(') != NULL)
2441 p = vim_strchr(p, '(');
2442 }
2443 p = skipwhite(p + 1);
2444
2445 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2446
2447 if (!eap->skip)
2448 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002449 // Check the name of the function. Unless it's a dictionary function
2450 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002451 if (name != NULL)
2452 arg = name;
2453 else
2454 arg = fudi.fd_newkey;
2455 if (arg != NULL && (fudi.fd_di == NULL
2456 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2457 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2458 {
2459 if (*arg == K_SPECIAL)
2460 j = 3;
2461 else
2462 j = 0;
2463 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2464 : eval_isnamec(arg[j])))
2465 ++j;
2466 if (arg[j] != NUL)
2467 emsg_funcname((char *)e_invarg2, arg);
2468 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002469 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002470 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002471 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002472 }
2473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474 if (get_function_args(&p, ')', &newargs,
2475 eap->cmdidx == CMD_def ? &argtypes : NULL,
2476 &varargs, &default_args, eap->skip) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002477 goto errret_2;
2478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002480 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 // find the return type: :def Func(): type
2482 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002483 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484 ret_type = skipwhite(p + 1);
2485 p = skip_type(ret_type);
2486 if (p > ret_type)
2487 p = skipwhite(p);
2488 else
2489 semsg(_("E1056: expected a type: %s"), ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002490 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002491 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 else
2493 // find extra arguments "range", "dict", "abort" and "closure"
2494 for (;;)
2495 {
2496 p = skipwhite(p);
2497 if (STRNCMP(p, "range", 5) == 0)
2498 {
2499 flags |= FC_RANGE;
2500 p += 5;
2501 }
2502 else if (STRNCMP(p, "dict", 4) == 0)
2503 {
2504 flags |= FC_DICT;
2505 p += 4;
2506 }
2507 else if (STRNCMP(p, "abort", 5) == 0)
2508 {
2509 flags |= FC_ABORT;
2510 p += 5;
2511 }
2512 else if (STRNCMP(p, "closure", 7) == 0)
2513 {
2514 flags |= FC_CLOSURE;
2515 p += 7;
2516 if (current_funccal == NULL)
2517 {
2518 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
2519 name == NULL ? (char_u *)"" : name);
2520 goto erret;
2521 }
2522 }
2523 else
2524 break;
2525 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002526
Bram Moolenaare38eab22019-12-05 21:50:01 +01002527 // When there is a line break use what follows for the function body.
2528 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002529 if (*p == '\n')
2530 line_arg = p + 1;
2531 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002532 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002533
2534 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
2536 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002537 */
2538 if (KeyTyped)
2539 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002540 // Check if the function already exists, don't let the user type the
2541 // whole function before telling him it doesn't work! For a script we
2542 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002543 if (!eap->skip && !eap->forceit)
2544 {
2545 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002546 emsg(_(e_funcdict));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547 else if (name != NULL && find_func(name, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002548 emsg_funcname(e_funcexts, name);
2549 }
2550
2551 if (!eap->skip && did_emsg)
2552 goto erret;
2553
Bram Moolenaare38eab22019-12-05 21:50:01 +01002554 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002555 cmdline_row = msg_row;
2556 }
2557
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002558 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002559 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002560
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002561 indent = 2;
2562 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002563 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002564 for (;;)
2565 {
2566 if (KeyTyped)
2567 {
2568 msg_scroll = TRUE;
2569 saved_wait_return = FALSE;
2570 }
2571 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002572
2573 if (line_arg != NULL)
2574 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002575 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002576 theline = line_arg;
2577 p = vim_strchr(theline, '\n');
2578 if (p == NULL)
2579 line_arg += STRLEN(line_arg);
2580 else
2581 {
2582 *p = NUL;
2583 line_arg = p + 1;
2584 }
2585 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002586 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002587 {
2588 vim_free(line_to_free);
2589 if (eap->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02002590 theline = getcmdline(':', 0L, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002591 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02002592 theline = eap->getline(':', eap->cookie, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002593 line_to_free = theline;
2594 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002595 if (KeyTyped)
2596 lines_left = Rows - 1;
2597 if (theline == NULL)
2598 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 if (eap->cmdidx == CMD_def)
2600 emsg(_("E1057: Missing :enddef"));
2601 else
2602 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002603 goto erret;
2604 }
2605
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002606 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002607 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002608 if (SOURCING_LNUM < sourcing_lnum_off)
2609 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002610 else
2611 sourcing_lnum_off = 0;
2612
2613 if (skip_until != NULL)
2614 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002616 // * ":append" and "."
2617 // * ":python <<EOF" and "EOF"
2618 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
2619 if (heredoc_trimmed == NULL
2620 || (is_heredoc && skipwhite(theline) == theline)
2621 || STRNCMP(theline, heredoc_trimmed,
2622 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002623 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002624 if (heredoc_trimmed == NULL)
2625 p = theline;
2626 else if (is_heredoc)
2627 p = skipwhite(theline) == theline
2628 ? theline : theline + STRLEN(heredoc_trimmed);
2629 else
2630 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02002631 if (STRCMP(p, skip_until) == 0)
2632 {
2633 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002634 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaare96a2492019-06-25 04:12:16 +02002635 do_concat = TRUE;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002636 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002637 }
2638 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002639 }
2640 else
2641 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002642 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01002643 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002644 ;
2645
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646 // Check for "endfunction" or "enddef".
2647 if (checkforcmd(&p, nesting_def[nesting]
2648 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002649 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002650 char_u *nextcmd = NULL;
2651
Bram Moolenaar663bb232017-06-22 19:12:10 +02002652 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002653 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002654 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002655 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002656 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 give_warning2(eap->cmdidx == CMD_def
2658 ? (char_u *)_("W1001: Text found after :enddef: %s")
2659 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002660 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002661 if (nextcmd != NULL)
2662 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002663 // Another command follows. If the line came from "eap" we
2664 // can simply point into it, otherwise we need to change
2665 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02002666 eap->nextcmd = nextcmd;
2667 if (line_to_free != NULL)
2668 {
2669 vim_free(*eap->cmdlinep);
2670 *eap->cmdlinep = line_to_free;
2671 line_to_free = NULL;
2672 }
2673 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002674 break;
2675 }
2676
Bram Moolenaare38eab22019-12-05 21:50:01 +01002677 // Increase indent inside "if", "while", "for" and "try", decrease
2678 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002680 indent -= 2;
2681 else if (STRNCMP(p, "if", 2) == 0
2682 || STRNCMP(p, "wh", 2) == 0
2683 || STRNCMP(p, "for", 3) == 0
2684 || STRNCMP(p, "try", 3) == 0)
2685 indent += 2;
2686
Bram Moolenaare38eab22019-12-05 21:50:01 +01002687 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01002688 // Only recognize "def" inside "def", not inside "function",
2689 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690 c = *p;
Bram Moolenaar673660a2020-01-26 16:50:05 +01002691 if (checkforcmd(&p, "function", 2)
2692 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002693 {
2694 if (*p == '!')
2695 p = skipwhite(p + 1);
2696 p += eval_fname_script(p);
2697 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
2698 if (*skipwhite(p) == '(')
2699 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002700 if (nesting == MAX_FUNC_NESTING - 1)
2701 emsg(_("E1058: function nesting too deep"));
2702 else
2703 {
2704 ++nesting;
2705 nesting_def[nesting] = (c == 'd');
2706 indent += 2;
2707 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002708 }
2709 }
2710
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002711 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002712 p = skip_range(p, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002713 if (eap->cmdidx != CMD_def
2714 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002715 || (p[0] == 'c'
2716 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
2717 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
2718 && (STRNCMP(&p[3], "nge", 3) != 0
2719 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002720 || (p[0] == 'i'
2721 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002722 && (!ASCII_ISALPHA(p[2])
2723 || (p[2] == 's'
2724 && (!ASCII_ISALPHA(p[3])
2725 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002726 skip_until = vim_strsave((char_u *)".");
2727
Bram Moolenaare38eab22019-12-05 21:50:01 +01002728 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002729 arg = skipwhite(skiptowhite(p));
2730 if (arg[0] == '<' && arg[1] =='<'
2731 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01002732 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
2733 || ((p[2] == '3' || p[2] == 'x')
2734 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002735 || (p[0] == 'p' && p[1] == 'e'
2736 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
2737 || (p[0] == 't' && p[1] == 'c'
2738 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
2739 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
2740 && !ASCII_ISALPHA(p[3]))
2741 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
2742 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
2743 || (p[0] == 'm' && p[1] == 'z'
2744 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
2745 ))
2746 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002747 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002748 p = skipwhite(arg + 2);
2749 if (*p == NUL)
2750 skip_until = vim_strsave((char_u *)".");
2751 else
2752 skip_until = vim_strsave(p);
2753 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002754
2755 // Check for ":let v =<< [trim] EOF"
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002756 // and ":let [a, b] =<< [trim] EOF"
Bram Moolenaar8471e572019-05-19 21:37:18 +02002757 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002758 if (*arg == '[')
2759 arg = vim_strchr(arg, ']');
2760 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002761 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002762 arg = skipwhite(skiptowhite(arg));
2763 if ( arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
2764 && ((p[0] == 'l'
2765 && p[1] == 'e'
2766 && (!ASCII_ISALNUM(p[2])
2767 || (p[2] == 't' && !ASCII_ISALNUM(p[3]))))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02002768 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002769 p = skipwhite(arg + 3);
2770 if (STRNCMP(p, "trim", 4) == 0)
2771 {
2772 // Ignore leading white space.
2773 p = skipwhite(p + 4);
2774 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar8471e572019-05-19 21:37:18 +02002775 (int)(skipwhite(theline) - theline));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002776 }
2777 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p));
2778 do_concat = FALSE;
2779 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002780 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002781 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002782 }
2783
Bram Moolenaare38eab22019-12-05 21:50:01 +01002784 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002785 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002786 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002787
Bram Moolenaare38eab22019-12-05 21:50:01 +01002788 // Copy the line to newly allocated memory. get_one_sourceline()
2789 // allocates 250 bytes per line, this saves 80% on average. The cost
2790 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002791 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002792 if (p == NULL)
2793 goto erret;
2794 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002795
Bram Moolenaare38eab22019-12-05 21:50:01 +01002796 // Add NULL lines for continuation lines, so that the line count is
2797 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002798 while (sourcing_lnum_off-- > 0)
2799 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
2800
Bram Moolenaare38eab22019-12-05 21:50:01 +01002801 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002802 if (line_arg != NULL && *line_arg == NUL)
2803 line_arg = NULL;
2804 }
2805
Bram Moolenaare38eab22019-12-05 21:50:01 +01002806 // Don't define the function when skipping commands or when an error was
2807 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002808 if (eap->skip || did_emsg)
2809 goto erret;
2810
2811 /*
2812 * If there are no errors, add the function
2813 */
2814 if (fudi.fd_dict == NULL)
2815 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 hashtab_T *ht;
2817
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002818 v = find_var(name, &ht, FALSE);
2819 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
2820 {
2821 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
2822 name);
2823 goto erret;
2824 }
2825
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826 fp = find_func_even_dead(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002827 if (fp != NULL)
2828 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 int dead = fp->uf_flags & FC_DEAD;
2830
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002831 // Function can be replaced with "function!" and when sourcing the
2832 // same script again, but only once.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833 if (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002834 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
2835 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002836 {
2837 emsg_funcname(e_funcexts, name);
2838 goto erret;
2839 }
2840 if (fp->uf_calls > 0)
2841 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002842 emsg_funcname(
2843 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002844 name);
2845 goto erret;
2846 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002847 if (fp->uf_refcount > 1)
2848 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002849 // This function is referenced somewhere, don't redefine it but
2850 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002851 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002852 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002853 fp = NULL;
2854 overwrite = TRUE;
2855 }
2856 else
2857 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002858 char_u *exp_name = fp->uf_name_exp;
2859
2860 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01002861 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002862 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002863 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002864 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002866#ifdef FEAT_PROFILE
2867 fp->uf_profiling = FALSE;
2868 fp->uf_prof_initialized = FALSE;
2869#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002870 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002871 }
2872 }
2873 else
2874 {
2875 char numbuf[20];
2876
2877 fp = NULL;
2878 if (fudi.fd_newkey == NULL && !eap->forceit)
2879 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002880 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002881 goto erret;
2882 }
2883 if (fudi.fd_di == NULL)
2884 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002885 // Can't add a function to a locked dictionary
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002886 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002887 goto erret;
2888 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002889 // Can't change an existing function if it is locked
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002890 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002891 goto erret;
2892
Bram Moolenaare38eab22019-12-05 21:50:01 +01002893 // Give the function a sequential number. Can only be used with a
2894 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002895 vim_free(name);
2896 sprintf(numbuf, "%d", ++func_nr);
2897 name = vim_strsave((char_u *)numbuf);
2898 if (name == NULL)
2899 goto erret;
2900 }
2901
2902 if (fp == NULL)
2903 {
2904 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
2905 {
2906 int slen, plen;
2907 char_u *scriptname;
2908
Bram Moolenaare38eab22019-12-05 21:50:01 +01002909 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002910 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002911 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002912 {
2913 scriptname = autoload_name(name);
2914 if (scriptname != NULL)
2915 {
2916 p = vim_strchr(scriptname, '/');
2917 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002918 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002919 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002920 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002921 j = OK;
2922 vim_free(scriptname);
2923 }
2924 }
2925 if (j == FAIL)
2926 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002927 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002928 goto erret;
2929 }
2930 }
2931
Bram Moolenaar47ed5532019-08-08 20:49:14 +02002932 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002933 if (fp == NULL)
2934 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 fp->uf_dfunc_idx = -1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002936
2937 if (fudi.fd_dict != NULL)
2938 {
2939 if (fudi.fd_di == NULL)
2940 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002941 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002942 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
2943 if (fudi.fd_di == NULL)
2944 {
2945 vim_free(fp);
2946 goto erret;
2947 }
2948 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
2949 {
2950 vim_free(fudi.fd_di);
2951 vim_free(fp);
2952 goto erret;
2953 }
2954 }
2955 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01002956 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002957 clear_tv(&fudi.fd_di->di_tv);
2958 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002959 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002960
Bram Moolenaare38eab22019-12-05 21:50:01 +01002961 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002962 flags |= FC_DICT;
2963 }
2964
Bram Moolenaare38eab22019-12-05 21:50:01 +01002965 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002966 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002967 if (overwrite)
2968 {
2969 hi = hash_find(&func_hashtab, name);
2970 hi->hi_key = UF2HIKEY(fp);
2971 }
2972 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002973 {
2974 vim_free(fp);
2975 goto erret;
2976 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002977 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002978 }
2979 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002980 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 fp->uf_ret_type = &t_any;
2982
2983 if (eap->cmdidx == CMD_def)
2984 {
Bram Moolenaarbfe12042020-02-04 21:54:07 +01002985 int lnum_save = SOURCING_LNUM;
2986
2987 // error messages are for the first function line
2988 SOURCING_LNUM = sourcing_lnum_top;
2989
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 // parse the argument types
2991 ga_init2(&fp->uf_type_list, sizeof(type_T), 5);
2992
2993 if (argtypes.ga_len > 0)
2994 {
2995 // When "varargs" is set the last name/type goes into uf_va_name
2996 // and uf_va_type.
2997 int len = argtypes.ga_len - (varargs ? 1 : 0);
2998
2999 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
3000 if (fp->uf_arg_types != NULL)
3001 {
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003002 int i;
3003 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004
3005 for (i = 0; i < len; ++ i)
3006 {
3007 p = ((char_u **)argtypes.ga_data)[i];
3008 if (p == NULL)
3009 // todo: get type from default value
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003010 type = &t_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 else
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003012 type = parse_type(&p, &fp->uf_type_list);
3013 if (type == NULL)
3014 {
3015 SOURCING_LNUM = lnum_save;
3016 goto errret_2;
3017 }
3018 fp->uf_arg_types[i] = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 }
3020 }
3021 if (varargs)
3022 {
3023 // Move the last argument "...name: type" to uf_va_name and
3024 // uf_va_type.
3025 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
3026 [fp->uf_args.ga_len - 1];
3027 --fp->uf_args.ga_len;
3028 p = ((char_u **)argtypes.ga_data)[len];
3029 if (p == NULL)
3030 // todo: get type from default value
3031 fp->uf_va_type = &t_any;
3032 else
3033 fp->uf_va_type = parse_type(&p, &fp->uf_type_list);
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003034 if (fp->uf_va_type == NULL)
3035 {
3036 SOURCING_LNUM = lnum_save;
3037 goto errret_2;
3038 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 }
3040 varargs = FALSE;
3041 }
3042
3043 // parse the return type, if any
3044 if (ret_type == NULL)
3045 fp->uf_ret_type = &t_void;
3046 else
3047 {
3048 p = ret_type;
3049 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list);
3050 }
3051 }
3052
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003053 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003054 if ((flags & FC_CLOSURE) != 0)
3055 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003056 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003057 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003058 }
3059 else
3060 fp->uf_scoped = NULL;
3061
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003062#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003063 if (prof_def_func())
3064 func_do_profile(fp);
3065#endif
3066 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003067 if (sandbox)
3068 flags |= FC_SANDBOX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003069 fp->uf_flags = flags;
3070 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003071 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003072 fp->uf_script_ctx = current_sctx;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003073 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 if (is_export)
3075 {
3076 fp->uf_flags |= FC_EXPORT;
3077 // let ex_export() know the export worked.
3078 is_export = FALSE;
3079 }
3080
3081 // ":def Func()" needs to be compiled
3082 if (eap->cmdidx == CMD_def)
3083 compile_def_function(fp, FALSE);
3084
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003085 goto ret_free;
3086
3087erret:
3088 ga_clear_strings(&newargs);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 ga_clear_strings(&argtypes);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003090 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003091errret_2:
3092 ga_clear_strings(&newlines);
3093ret_free:
3094 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003095 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003096 vim_free(fudi.fd_newkey);
3097 vim_free(name);
3098 did_emsg |= saved_did_emsg;
3099 need_wait_return |= saved_wait_return;
3100}
3101
3102/*
3103 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3104 * Return 2 if "p" starts with "s:".
3105 * Return 0 otherwise.
3106 */
3107 int
3108eval_fname_script(char_u *p)
3109{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003110 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3111 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003112 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3113 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3114 return 5;
3115 if (p[0] == 's' && p[1] == ':')
3116 return 2;
3117 return 0;
3118}
3119
3120 int
3121translated_function_exists(char_u *name)
3122{
3123 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003124 return has_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 return find_func(name, NULL) != NULL;
3126}
3127
3128/*
3129 * Return TRUE when "ufunc" has old-style "..." varargs
3130 * or named varargs "...name: type".
3131 */
3132 int
3133has_varargs(ufunc_T *ufunc)
3134{
3135 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003136}
3137
3138/*
3139 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003140 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003141 */
3142 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003143function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003144{
3145 char_u *nm = name;
3146 char_u *p;
3147 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003148 int flag;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003149
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003150 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3151 if (no_deref)
3152 flag |= TFN_NO_DEREF;
3153 p = trans_function_name(&nm, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003154 nm = skipwhite(nm);
3155
Bram Moolenaare38eab22019-12-05 21:50:01 +01003156 // Only accept "funcname", "funcname ", "funcname (..." and
3157 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003158 if (p != NULL && (*nm == NUL || *nm == '('))
3159 n = translated_function_exists(p);
3160 vim_free(p);
3161 return n;
3162}
3163
Bram Moolenaar113e1072019-01-20 15:30:40 +01003164#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003165 char_u *
3166get_expanded_name(char_u *name, int check)
3167{
3168 char_u *nm = name;
3169 char_u *p;
3170
3171 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
3172
3173 if (p != NULL && *nm == NUL)
3174 if (!check || translated_function_exists(p))
3175 return p;
3176
3177 vim_free(p);
3178 return NULL;
3179}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003180#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003181
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003182/*
3183 * Function given to ExpandGeneric() to obtain the list of user defined
3184 * function names.
3185 */
3186 char_u *
3187get_user_func_name(expand_T *xp, int idx)
3188{
3189 static long_u done;
3190 static hashitem_T *hi;
3191 ufunc_T *fp;
3192
3193 if (idx == 0)
3194 {
3195 done = 0;
3196 hi = func_hashtab.ht_array;
3197 }
3198 if (done < func_hashtab.ht_used)
3199 {
3200 if (done++ > 0)
3201 ++hi;
3202 while (HASHITEM_EMPTY(hi))
3203 ++hi;
3204 fp = HI2UF(hi);
3205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 // don't show dead, dict and lambda functions
3207 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02003208 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003210
3211 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003212 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003213
3214 cat_func_name(IObuff, fp);
3215 if (xp->xp_context != EXPAND_USER_FUNC)
3216 {
3217 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003219 STRCAT(IObuff, ")");
3220 }
3221 return IObuff;
3222 }
3223 return NULL;
3224}
3225
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003226/*
3227 * ":delfunction {name}"
3228 */
3229 void
3230ex_delfunction(exarg_T *eap)
3231{
3232 ufunc_T *fp = NULL;
3233 char_u *p;
3234 char_u *name;
3235 funcdict_T fudi;
3236
3237 p = eap->arg;
3238 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
3239 vim_free(fudi.fd_newkey);
3240 if (name == NULL)
3241 {
3242 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003243 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003244 return;
3245 }
3246 if (!ends_excmd(*skipwhite(p)))
3247 {
3248 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003249 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003250 return;
3251 }
3252 eap->nextcmd = check_nextcmd(p);
3253 if (eap->nextcmd != NULL)
3254 *p = NUL;
3255
3256 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 fp = find_func(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003258 vim_free(name);
3259
3260 if (!eap->skip)
3261 {
3262 if (fp == NULL)
3263 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02003264 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003265 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003266 return;
3267 }
3268 if (fp->uf_calls > 0)
3269 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003270 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003271 return;
3272 }
3273
3274 if (fudi.fd_dict != NULL)
3275 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003276 // Delete the dict item that refers to the function, it will
3277 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003278 dictitem_remove(fudi.fd_dict, fudi.fd_di);
3279 }
3280 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003281 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003282 // A normal function (not a numbered function or lambda) has a
3283 // refcount of 1 for the entry in the hashtable. When deleting
3284 // it and the refcount is more than one, it should be kept.
3285 // A numbered function and lambda should be kept if the refcount is
3286 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003287 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003288 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003289 // Function is still referenced somewhere. Don't free it but
3290 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003291 if (func_remove(fp))
3292 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003293 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003294 }
3295 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003296 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003297 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003298 }
3299}
3300
3301/*
3302 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003303 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003304 */
3305 void
3306func_unref(char_u *name)
3307{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003308 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003309
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003310 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003311 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 fp = find_func(name, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003313 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003314 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003315#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003316 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003317#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01003318 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003319 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003320 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003321 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003322 // Only delete it when it's not being used. Otherwise it's done
3323 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003324 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003325 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02003326 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003327}
3328
3329/*
3330 * Unreference a Function: decrement the reference count and free it when it
3331 * becomes zero.
3332 */
3333 void
3334func_ptr_unref(ufunc_T *fp)
3335{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003336 if (fp != NULL && --fp->uf_refcount <= 0)
3337 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003338 // Only delete it when it's not being used. Otherwise it's done
3339 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02003340 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003341 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003342 }
3343}
3344
3345/*
3346 * Count a reference to a Function.
3347 */
3348 void
3349func_ref(char_u *name)
3350{
3351 ufunc_T *fp;
3352
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003353 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003354 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 fp = find_func(name, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003356 if (fp != NULL)
3357 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003358 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01003359 // Only give an error for a numbered function.
3360 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01003361 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003362}
3363
3364/*
3365 * Count a reference to a Function.
3366 */
3367 void
3368func_ptr_ref(ufunc_T *fp)
3369{
3370 if (fp != NULL)
3371 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003372}
3373
3374/*
3375 * Return TRUE if items in "fc" do not have "copyID". That means they are not
3376 * referenced from anywhere that is in use.
3377 */
3378 static int
3379can_free_funccal(funccall_T *fc, int copyID)
3380{
3381 return (fc->l_varlist.lv_copyID != copyID
3382 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003383 && fc->l_avars.dv_copyID != copyID
3384 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003385}
3386
3387/*
3388 * ":return [expr]"
3389 */
3390 void
3391ex_return(exarg_T *eap)
3392{
3393 char_u *arg = eap->arg;
3394 typval_T rettv;
3395 int returning = FALSE;
3396
3397 if (current_funccal == NULL)
3398 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003399 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003400 return;
3401 }
3402
3403 if (eap->skip)
3404 ++emsg_skip;
3405
3406 eap->nextcmd = NULL;
3407 if ((*arg != NUL && *arg != '|' && *arg != '\n')
3408 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
3409 {
3410 if (!eap->skip)
3411 returning = do_return(eap, FALSE, TRUE, &rettv);
3412 else
3413 clear_tv(&rettv);
3414 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003415 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003416 else if (!eap->skip)
3417 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003418 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003419 update_force_abort();
3420
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003421 /*
3422 * Return unless the expression evaluation has been cancelled due to an
3423 * aborting error, an interrupt, or an exception.
3424 */
3425 if (!aborting())
3426 returning = do_return(eap, FALSE, TRUE, NULL);
3427 }
3428
Bram Moolenaare38eab22019-12-05 21:50:01 +01003429 // When skipping or the return gets pending, advance to the next command
3430 // in this line (!returning). Otherwise, ignore the rest of the line.
3431 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003432 if (returning)
3433 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003434 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003435 eap->nextcmd = check_nextcmd(arg);
3436
3437 if (eap->skip)
3438 --emsg_skip;
3439}
3440
3441/*
3442 * ":1,25call func(arg1, arg2)" function call.
3443 */
3444 void
3445ex_call(exarg_T *eap)
3446{
3447 char_u *arg = eap->arg;
3448 char_u *startarg;
3449 char_u *name;
3450 char_u *tofree;
3451 int len;
3452 typval_T rettv;
3453 linenr_T lnum;
3454 int doesrange;
3455 int failed = FALSE;
3456 funcdict_T fudi;
3457 partial_T *partial = NULL;
3458
3459 if (eap->skip)
3460 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003461 // trans_function_name() doesn't work well when skipping, use eval0()
3462 // instead to skip to any following command, e.g. for:
3463 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003464 ++emsg_skip;
3465 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3466 clear_tv(&rettv);
3467 --emsg_skip;
3468 return;
3469 }
3470
3471 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
3472 if (fudi.fd_newkey != NULL)
3473 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003474 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003475 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003476 vim_free(fudi.fd_newkey);
3477 }
3478 if (tofree == NULL)
3479 return;
3480
Bram Moolenaare38eab22019-12-05 21:50:01 +01003481 // Increase refcount on dictionary, it could get deleted when evaluating
3482 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003483 if (fudi.fd_dict != NULL)
3484 ++fudi.fd_dict->dv_refcount;
3485
Bram Moolenaare38eab22019-12-05 21:50:01 +01003486 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3487 // contents. For VAR_PARTIAL get its partial, unless we already have one
3488 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003489 len = (int)STRLEN(tofree);
3490 name = deref_func_name(tofree, &len,
3491 partial != NULL ? NULL : &partial, FALSE);
3492
Bram Moolenaare38eab22019-12-05 21:50:01 +01003493 // Skip white space to allow ":call func ()". Not good, but required for
3494 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003495 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003496 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003497
3498 if (*startarg != '(')
3499 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003501 goto end;
3502 }
3503
3504 /*
3505 * When skipping, evaluate the function once, to find the end of the
3506 * arguments.
3507 * When the function takes a range, this is discovered after the first
3508 * call, and the loop is broken.
3509 */
3510 if (eap->skip)
3511 {
3512 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003513 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003514 }
3515 else
3516 lnum = eap->line1;
3517 for ( ; lnum <= eap->line2; ++lnum)
3518 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003519 funcexe_T funcexe;
3520
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003521 if (!eap->skip && eap->addr_count > 0)
3522 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003523 if (lnum > curbuf->b_ml.ml_line_count)
3524 {
3525 // If the function deleted lines or switched to another buffer
3526 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003527 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003528 break;
3529 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003530 curwin->w_cursor.lnum = lnum;
3531 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003532 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003533 }
3534 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003535
Bram Moolenaarac92e252019-08-03 21:58:38 +02003536 vim_memset(&funcexe, 0, sizeof(funcexe));
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003537 funcexe.firstline = eap->line1;
3538 funcexe.lastline = eap->line2;
3539 funcexe.doesrange = &doesrange;
3540 funcexe.evaluate = !eap->skip;
3541 funcexe.partial = partial;
3542 funcexe.selfdict = fudi.fd_dict;
3543 if (get_func_tv(name, -1, &rettv, &arg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003544 {
3545 failed = TRUE;
3546 break;
3547 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003548 if (has_watchexpr())
3549 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003550
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003551 // Handle a function returning a Funcref, Dictionary or List.
3552 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE,
3553 name, &name) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003554 {
3555 failed = TRUE;
3556 break;
3557 }
3558
3559 clear_tv(&rettv);
3560 if (doesrange || eap->skip)
3561 break;
3562
Bram Moolenaare38eab22019-12-05 21:50:01 +01003563 // Stop when immediately aborting on error, or when an interrupt
3564 // occurred or an exception was thrown but not caught.
3565 // get_func_tv() returned OK, so that the check for trailing
3566 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003567 if (aborting())
3568 break;
3569 }
3570 if (eap->skip)
3571 --emsg_skip;
3572
Bram Moolenaare51bb172020-02-16 19:42:23 +01003573 // When inside :try we need to check for following "| catch".
3574 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003575 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003576 // Check for trailing illegal characters and a following command.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003577 if (!ends_excmd(*arg))
3578 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01003579 if (!failed)
3580 {
3581 emsg_severe = TRUE;
3582 emsg(_(e_trailing));
3583 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003584 }
3585 else
3586 eap->nextcmd = check_nextcmd(arg);
3587 }
3588
3589end:
3590 dict_unref(fudi.fd_dict);
3591 vim_free(tofree);
3592}
3593
3594/*
3595 * Return from a function. Possibly makes the return pending. Also called
3596 * for a pending return at the ":endtry" or after returning from an extra
3597 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3598 * when called due to a ":return" command. "rettv" may point to a typval_T
3599 * with the return rettv. Returns TRUE when the return can be carried out,
3600 * FALSE when the return gets pending.
3601 */
3602 int
3603do_return(
3604 exarg_T *eap,
3605 int reanimate,
3606 int is_cmd,
3607 void *rettv)
3608{
3609 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01003610 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003611
3612 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003613 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003614 current_funccal->returned = FALSE;
3615
3616 /*
3617 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3618 * not in its finally clause (which then is to be executed next) is found.
3619 * In this case, make the ":return" pending for execution at the ":endtry".
3620 * Otherwise, return normally.
3621 */
3622 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3623 if (idx >= 0)
3624 {
3625 cstack->cs_pending[idx] = CSTP_RETURN;
3626
3627 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003628 // A pending return again gets pending. "rettv" points to an
3629 // allocated variable with the rettv of the original ":return"'s
3630 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003631 cstack->cs_rettv[idx] = rettv;
3632 else
3633 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003634 // When undoing a return in order to make it pending, get the stored
3635 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003636 if (reanimate)
3637 rettv = current_funccal->rettv;
3638
3639 if (rettv != NULL)
3640 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003641 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003642 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
3643 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
3644 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003645 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003646 }
3647 else
3648 cstack->cs_rettv[idx] = NULL;
3649
3650 if (reanimate)
3651 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003652 // The pending return value could be overwritten by a ":return"
3653 // without argument in a finally clause; reset the default
3654 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003655 current_funccal->rettv->v_type = VAR_NUMBER;
3656 current_funccal->rettv->vval.v_number = 0;
3657 }
3658 }
3659 report_make_pending(CSTP_RETURN, rettv);
3660 }
3661 else
3662 {
3663 current_funccal->returned = TRUE;
3664
Bram Moolenaare38eab22019-12-05 21:50:01 +01003665 // If the return is carried out now, store the return value. For
3666 // a return immediately after reanimation, the value is already
3667 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003668 if (!reanimate && rettv != NULL)
3669 {
3670 clear_tv(current_funccal->rettv);
3671 *current_funccal->rettv = *(typval_T *)rettv;
3672 if (!is_cmd)
3673 vim_free(rettv);
3674 }
3675 }
3676
3677 return idx < 0;
3678}
3679
3680/*
3681 * Free the variable with a pending return value.
3682 */
3683 void
3684discard_pending_return(void *rettv)
3685{
3686 free_tv((typval_T *)rettv);
3687}
3688
3689/*
3690 * Generate a return command for producing the value of "rettv". The result
3691 * is an allocated string. Used by report_pending() for verbose messages.
3692 */
3693 char_u *
3694get_return_cmd(void *rettv)
3695{
3696 char_u *s = NULL;
3697 char_u *tofree = NULL;
3698 char_u numbuf[NUMBUFLEN];
3699
3700 if (rettv != NULL)
3701 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
3702 if (s == NULL)
3703 s = (char_u *)"";
3704
3705 STRCPY(IObuff, ":return ");
3706 STRNCPY(IObuff + 8, s, IOSIZE - 8);
3707 if (STRLEN(s) + 8 >= IOSIZE)
3708 STRCPY(IObuff + IOSIZE - 4, "...");
3709 vim_free(tofree);
3710 return vim_strsave(IObuff);
3711}
3712
3713/*
3714 * Get next function line.
3715 * Called by do_cmdline() to get the next line.
3716 * Returns allocated string, or NULL for end of function.
3717 */
3718 char_u *
3719get_func_line(
3720 int c UNUSED,
3721 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003722 int indent UNUSED,
3723 int do_concat UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003724{
3725 funccall_T *fcp = (funccall_T *)cookie;
3726 ufunc_T *fp = fcp->func;
3727 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003728 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003729
Bram Moolenaare38eab22019-12-05 21:50:01 +01003730 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003731 if (fcp->dbg_tick != debug_tick)
3732 {
3733 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003734 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003735 fcp->dbg_tick = debug_tick;
3736 }
3737#ifdef FEAT_PROFILE
3738 if (do_profiling == PROF_YES)
3739 func_line_end(cookie);
3740#endif
3741
3742 gap = &fp->uf_lines;
3743 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3744 || fcp->returned)
3745 retval = NULL;
3746 else
3747 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003748 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003749 while (fcp->linenr < gap->ga_len
3750 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
3751 ++fcp->linenr;
3752 if (fcp->linenr >= gap->ga_len)
3753 retval = NULL;
3754 else
3755 {
3756 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003757 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003758#ifdef FEAT_PROFILE
3759 if (do_profiling == PROF_YES)
3760 func_line_start(cookie);
3761#endif
3762 }
3763 }
3764
Bram Moolenaare38eab22019-12-05 21:50:01 +01003765 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003766 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003767 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003768 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003769 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003770 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003771 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003772 fcp->dbg_tick = debug_tick;
3773 }
3774
3775 return retval;
3776}
3777
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003778/*
3779 * Return TRUE if the currently active function should be ended, because a
3780 * return was encountered or an error occurred. Used inside a ":while".
3781 */
3782 int
3783func_has_ended(void *cookie)
3784{
3785 funccall_T *fcp = (funccall_T *)cookie;
3786
Bram Moolenaare38eab22019-12-05 21:50:01 +01003787 // Ignore the "abort" flag if the abortion behavior has been changed due to
3788 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003789 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3790 || fcp->returned);
3791}
3792
3793/*
3794 * return TRUE if cookie indicates a function which "abort"s on errors.
3795 */
3796 int
3797func_has_abort(
3798 void *cookie)
3799{
3800 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
3801}
3802
3803
3804/*
3805 * Turn "dict.Func" into a partial for "Func" bound to "dict".
3806 * Don't do this when "Func" is already a partial that was bound
3807 * explicitly (pt_auto is FALSE).
3808 * Changes "rettv" in-place.
3809 * Returns the updated "selfdict_in".
3810 */
3811 dict_T *
3812make_partial(dict_T *selfdict_in, typval_T *rettv)
3813{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003814 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003815 char_u *tofree = NULL;
3816 ufunc_T *fp;
3817 char_u fname_buf[FLEN_FIXED + 1];
3818 int error;
3819 dict_T *selfdict = selfdict_in;
3820
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003821 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
3822 fp = rettv->vval.v_partial->pt_func;
3823 else
3824 {
3825 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
3826 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003827 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003828 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 fp = find_func(fname, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003830 vim_free(tofree);
3831 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003832
3833 if (fp != NULL && (fp->uf_flags & FC_DICT))
3834 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003835 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003836
3837 if (pt != NULL)
3838 {
3839 pt->pt_refcount = 1;
3840 pt->pt_dict = selfdict;
3841 pt->pt_auto = TRUE;
3842 selfdict = NULL;
3843 if (rettv->v_type == VAR_FUNC)
3844 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003845 // Just a function: Take over the function name and use
3846 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003847 pt->pt_name = rettv->vval.v_string;
3848 }
3849 else
3850 {
3851 partial_T *ret_pt = rettv->vval.v_partial;
3852 int i;
3853
Bram Moolenaare38eab22019-12-05 21:50:01 +01003854 // Partial: copy the function name, use selfdict and copy
3855 // args. Can't take over name or args, the partial might
3856 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003857 if (ret_pt->pt_name != NULL)
3858 {
3859 pt->pt_name = vim_strsave(ret_pt->pt_name);
3860 func_ref(pt->pt_name);
3861 }
3862 else
3863 {
3864 pt->pt_func = ret_pt->pt_func;
3865 func_ptr_ref(pt->pt_func);
3866 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003867 if (ret_pt->pt_argc > 0)
3868 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003869 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003870 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003871 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003872 pt->pt_argc = 0;
3873 else
3874 {
3875 pt->pt_argc = ret_pt->pt_argc;
3876 for (i = 0; i < pt->pt_argc; i++)
3877 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
3878 }
3879 }
3880 partial_unref(ret_pt);
3881 }
3882 rettv->v_type = VAR_PARTIAL;
3883 rettv->vval.v_partial = pt;
3884 }
3885 }
3886 return selfdict;
3887}
3888
3889/*
3890 * Return the name of the executed function.
3891 */
3892 char_u *
3893func_name(void *cookie)
3894{
3895 return ((funccall_T *)cookie)->func->uf_name;
3896}
3897
3898/*
3899 * Return the address holding the next breakpoint line for a funccall cookie.
3900 */
3901 linenr_T *
3902func_breakpoint(void *cookie)
3903{
3904 return &((funccall_T *)cookie)->breakpoint;
3905}
3906
3907/*
3908 * Return the address holding the debug tick for a funccall cookie.
3909 */
3910 int *
3911func_dbg_tick(void *cookie)
3912{
3913 return &((funccall_T *)cookie)->dbg_tick;
3914}
3915
3916/*
3917 * Return the nesting level for a funccall cookie.
3918 */
3919 int
3920func_level(void *cookie)
3921{
3922 return ((funccall_T *)cookie)->level;
3923}
3924
3925/*
3926 * Return TRUE when a function was ended by a ":return" command.
3927 */
3928 int
3929current_func_returned(void)
3930{
3931 return current_funccal->returned;
3932}
3933
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003934 int
3935free_unref_funccal(int copyID, int testing)
3936{
3937 int did_free = FALSE;
3938 int did_free_funccal = FALSE;
3939 funccall_T *fc, **pfc;
3940
3941 for (pfc = &previous_funccal; *pfc != NULL; )
3942 {
3943 if (can_free_funccal(*pfc, copyID))
3944 {
3945 fc = *pfc;
3946 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003947 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003948 did_free = TRUE;
3949 did_free_funccal = TRUE;
3950 }
3951 else
3952 pfc = &(*pfc)->caller;
3953 }
3954 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003955 // When a funccal was freed some more items might be garbage
3956 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003957 (void)garbage_collect(testing);
3958
3959 return did_free;
3960}
3961
3962/*
Bram Moolenaarba209902016-08-24 22:06:38 +02003963 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003964 */
3965 static funccall_T *
3966get_funccal(void)
3967{
3968 int i;
3969 funccall_T *funccal;
3970 funccall_T *temp_funccal;
3971
3972 funccal = current_funccal;
3973 if (debug_backtrace_level > 0)
3974 {
3975 for (i = 0; i < debug_backtrace_level; i++)
3976 {
3977 temp_funccal = funccal->caller;
3978 if (temp_funccal)
3979 funccal = temp_funccal;
3980 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003981 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003982 debug_backtrace_level = i;
3983 }
3984 }
3985 return funccal;
3986}
3987
3988/*
3989 * Return the hashtable used for local variables in the current funccal.
3990 * Return NULL if there is no current funccal.
3991 */
3992 hashtab_T *
3993get_funccal_local_ht()
3994{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003996 return NULL;
3997 return &get_funccal()->l_vars.dv_hashtab;
3998}
3999
4000/*
4001 * Return the l: scope variable.
4002 * Return NULL if there is no current funccal.
4003 */
4004 dictitem_T *
4005get_funccal_local_var()
4006{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004008 return NULL;
4009 return &get_funccal()->l_vars_var;
4010}
4011
4012/*
4013 * Return the hashtable used for argument in the current funccal.
4014 * Return NULL if there is no current funccal.
4015 */
4016 hashtab_T *
4017get_funccal_args_ht()
4018{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004020 return NULL;
4021 return &get_funccal()->l_avars.dv_hashtab;
4022}
4023
4024/*
4025 * Return the a: scope variable.
4026 * Return NULL if there is no current funccal.
4027 */
4028 dictitem_T *
4029get_funccal_args_var()
4030{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004032 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004033 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004034}
4035
4036/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004037 * List function variables, if there is a function.
4038 */
4039 void
4040list_func_vars(int *first)
4041{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004043 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004044 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004045}
4046
4047/*
4048 * If "ht" is the hashtable for local variables in the current funccal, return
4049 * the dict that contains it.
4050 * Otherwise return NULL.
4051 */
4052 dict_T *
4053get_current_funccal_dict(hashtab_T *ht)
4054{
4055 if (current_funccal != NULL
4056 && ht == &current_funccal->l_vars.dv_hashtab)
4057 return &current_funccal->l_vars;
4058 return NULL;
4059}
4060
4061/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004062 * Search hashitem in parent scope.
4063 */
4064 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004065find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004066{
4067 funccall_T *old_current_funccal = current_funccal;
4068 hashtab_T *ht;
4069 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004070 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004071
4072 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4073 return NULL;
4074
Bram Moolenaare38eab22019-12-05 21:50:01 +01004075 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004076 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004077 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004078 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004079 ht = find_var_ht(name, &varname);
4080 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004081 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004082 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004083 if (!HASHITEM_EMPTY(hi))
4084 {
4085 *pht = ht;
4086 break;
4087 }
4088 }
4089 if (current_funccal == current_funccal->func->uf_scoped)
4090 break;
4091 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004092 }
4093 current_funccal = old_current_funccal;
4094
4095 return hi;
4096}
4097
4098/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004099 * Search variable in parent scope.
4100 */
4101 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004102find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004103{
4104 dictitem_T *v = NULL;
4105 funccall_T *old_current_funccal = current_funccal;
4106 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004107 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004108
4109 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4110 return NULL;
4111
Bram Moolenaare38eab22019-12-05 21:50:01 +01004112 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004113 current_funccal = current_funccal->func->uf_scoped;
4114 while (current_funccal)
4115 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004116 ht = find_var_ht(name, &varname);
4117 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004118 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004119 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004120 if (v != NULL)
4121 break;
4122 }
4123 if (current_funccal == current_funccal->func->uf_scoped)
4124 break;
4125 current_funccal = current_funccal->func->uf_scoped;
4126 }
4127 current_funccal = old_current_funccal;
4128
4129 return v;
4130}
4131
4132/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004133 * Set "copyID + 1" in previous_funccal and callers.
4134 */
4135 int
4136set_ref_in_previous_funccal(int copyID)
4137{
4138 int abort = FALSE;
4139 funccall_T *fc;
4140
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004141 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004142 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004143 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004144 abort = abort
4145 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4146 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004147 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004148 }
4149 return abort;
4150}
4151
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004152 static int
4153set_ref_in_funccal(funccall_T *fc, int copyID)
4154{
4155 int abort = FALSE;
4156
4157 if (fc->fc_copyID != copyID)
4158 {
4159 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004160 abort = abort
4161 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
4162 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004163 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004164 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004165 }
4166 return abort;
4167}
4168
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004169/*
4170 * Set "copyID" in all local vars and arguments in the call stack.
4171 */
4172 int
4173set_ref_in_call_stack(int copyID)
4174{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004175 int abort = FALSE;
4176 funccall_T *fc;
4177 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004178
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004179 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004180 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004181
4182 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004183 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
4184 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004185 abort = abort || set_ref_in_funccal(fc, copyID);
4186
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004187 return abort;
4188}
4189
4190/*
4191 * Set "copyID" in all functions available by name.
4192 */
4193 int
4194set_ref_in_functions(int copyID)
4195{
4196 int todo;
4197 hashitem_T *hi = NULL;
4198 int abort = FALSE;
4199 ufunc_T *fp;
4200
4201 todo = (int)func_hashtab.ht_used;
4202 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004203 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004204 if (!HASHITEM_EMPTY(hi))
4205 {
4206 --todo;
4207 fp = HI2UF(hi);
4208 if (!func_name_refcount(fp->uf_name))
4209 abort = abort || set_ref_in_func(NULL, fp, copyID);
4210 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004211 }
4212 return abort;
4213}
4214
4215/*
4216 * Set "copyID" in all function arguments.
4217 */
4218 int
4219set_ref_in_func_args(int copyID)
4220{
4221 int i;
4222 int abort = FALSE;
4223
4224 for (i = 0; i < funcargs.ga_len; ++i)
4225 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
4226 copyID, NULL, NULL);
4227 return abort;
4228}
4229
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004230/*
4231 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004232 * Returns TRUE if setting references failed somehow.
4233 */
4234 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004235set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004236{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004237 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004238 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01004239 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004240 char_u fname_buf[FLEN_FIXED + 1];
4241 char_u *tofree = NULL;
4242 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004243 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004244
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004245 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004246 return FALSE;
4247
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004248 if (fp_in == NULL)
4249 {
4250 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251 fp = find_func(fname, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004252 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004253 if (fp != NULL)
4254 {
4255 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004256 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004257 }
4258 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004259 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004260}
4261
Bram Moolenaare38eab22019-12-05 21:50:01 +01004262#endif // FEAT_EVAL