blob: 6ca89806861c9db69497532a59236bbbefe53caa [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 Moolenaarf10806b2020-04-02 18:34:35 +020027#define FC_NOARGS 0x200 // no a: variables in lambda
Bram Moolenaara9b579f2016-07-17 18:29:19 +020028
Bram Moolenaara9b579f2016-07-17 18:29:19 +020029/*
30 * All user-defined functions are found in this hashtable.
31 */
32static hashtab_T func_hashtab;
33
Bram Moolenaare38eab22019-12-05 21:50:01 +010034// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020035static garray_T funcargs = GA_EMPTY;
36
Bram Moolenaar209b8e32019-03-14 13:43:24 +010037// pointer to funccal for currently active function
38static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020039
Bram Moolenaar209b8e32019-03-14 13:43:24 +010040// Pointer to list of previously used funccal, still around because some
41// item in it is still being used.
42static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020043
44static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
45static char *e_funcdict = N_("E717: Dictionary entry already exists");
46static char *e_funcref = N_("E718: Funcref required");
47static char *e_nofunc = N_("E130: Unknown function: %s");
48
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020049static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020050
51 void
52func_init()
53{
54 hash_init(&func_hashtab);
55}
56
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020057/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020058 * Return the function hash table
59 */
60 hashtab_T *
61func_tbl_get(void)
62{
63 return &func_hashtab;
64}
65
66/*
Bram Moolenaar6e949782020-04-13 17:21:00 +020067 * Get one function argument.
68 * If "argtypes" is not NULL also get the type: "arg: type".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010069 * Return a pointer to after the type.
70 * When something is wrong return "arg".
71 */
72 static char_u *
73one_function_arg(char_u *arg, garray_T *newargs, garray_T *argtypes, int skip)
74{
Bram Moolenaar6e949782020-04-13 17:21:00 +020075 char_u *p = arg;
76 char_u *arg_copy = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077
78 while (ASCII_ISALNUM(*p) || *p == '_')
79 ++p;
80 if (arg == p || isdigit(*arg)
81 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
82 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
83 {
84 if (!skip)
85 semsg(_("E125: Illegal argument: %s"), arg);
86 return arg;
87 }
88 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
89 return arg;
90 if (newargs != NULL)
91 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010092 int c;
93 int i;
94
95 c = *p;
96 *p = NUL;
97 arg_copy = vim_strsave(arg);
98 if (arg_copy == NULL)
99 {
100 *p = c;
101 return arg;
102 }
103
104 // Check for duplicate argument name.
105 for (i = 0; i < newargs->ga_len; ++i)
106 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg_copy) == 0)
107 {
108 semsg(_("E853: Duplicate argument name: %s"), arg_copy);
109 vim_free(arg_copy);
110 return arg;
111 }
112 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg_copy;
113 newargs->ga_len++;
114
115 *p = c;
116 }
117
118 // get any type from "arg: type"
119 if (argtypes != NULL && ga_grow(argtypes, 1) == OK)
120 {
121 char_u *type = NULL;
122
Bram Moolenaar6e949782020-04-13 17:21:00 +0200123 if (VIM_ISWHITE(*p) && *skipwhite(p) == ':')
124 {
125 semsg(_("E1059: No white space allowed before colon: %s"),
126 arg_copy == NULL ? arg : arg_copy);
127 p = skipwhite(p);
128 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100129 if (*p == ':')
130 {
131 type = skipwhite(p + 1);
132 p = skip_type(type);
133 type = vim_strnsave(type, p - type);
134 }
Bram Moolenaar6e949782020-04-13 17:21:00 +0200135 else if (*skipwhite(p) != '=')
136 {
137 semsg(_("E1077: Missing argument type for %s"),
138 arg_copy == NULL ? arg : arg_copy);
139 return arg;
140 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100141 ((char_u **)argtypes->ga_data)[argtypes->ga_len++] = type;
142 }
143
144 return p;
145}
146
147/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200148 * Get function arguments.
149 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100150 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200151get_function_args(
152 char_u **argp,
153 char_u endchar,
154 garray_T *newargs,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155 garray_T *argtypes, // NULL unless using :def
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200156 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200157 garray_T *default_args,
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200158 int skip,
159 exarg_T *eap,
160 char_u **line_to_free)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200161{
162 int mustend = FALSE;
163 char_u *arg = *argp;
164 char_u *p = arg;
165 int c;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200166 int any_default = FALSE;
167 char_u *expr;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200168 char_u *whitep = arg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200169
170 if (newargs != NULL)
171 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100172 if (argtypes != NULL)
173 ga_init2(argtypes, (int)sizeof(char_u *), 3);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200174 if (default_args != NULL)
175 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200176
177 if (varargs != NULL)
178 *varargs = FALSE;
179
180 /*
181 * Isolate the arguments: "arg1, arg2, ...)"
182 */
183 while (*p != endchar)
184 {
Bram Moolenaar2c330432020-04-13 14:41:35 +0200185 while (eap != NULL && eap->getline != NULL
186 && (*p == NUL || (VIM_ISWHITE(*whitep) && *p == '#')))
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200187 {
188 char_u *theline;
189
190 // End of the line, get the next one.
191 theline = eap->getline(':', eap->cookie, 0, TRUE);
192 if (theline == NULL)
193 break;
194 vim_free(*line_to_free);
195 *line_to_free = theline;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200196 whitep = (char_u *)" ";
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200197 p = skipwhite(theline);
198 }
199
200 if (mustend && *p != endchar)
201 {
202 if (!skip)
203 semsg(_(e_invarg2), *argp);
204 break;
205 }
206 if (*p == endchar)
207 break;
208
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200209 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
210 {
211 if (varargs != NULL)
212 *varargs = TRUE;
213 p += 3;
214 mustend = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100215
216 if (argtypes != NULL)
217 {
218 // ...name: list<type>
219 if (!ASCII_ISALPHA(*p))
220 {
221 emsg(_("E1055: Missing name after ..."));
222 break;
223 }
224
225 arg = p;
226 p = one_function_arg(p, newargs, argtypes, skip);
227 if (p == arg)
228 break;
229 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200230 }
231 else
232 {
233 arg = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100234 p = one_function_arg(p, newargs, argtypes, skip);
235 if (p == arg)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200236 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200237
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200238 if (*skipwhite(p) == '=' && default_args != NULL)
239 {
240 typval_T rettv;
241
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100242 // find the end of the expression (doesn't evaluate it)
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200243 any_default = TRUE;
244 p = skipwhite(p) + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +0200245 whitep = p;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200246 p = skipwhite(p);
247 expr = p;
248 if (eval1(&p, &rettv, FALSE) != FAIL)
249 {
250 if (ga_grow(default_args, 1) == FAIL)
251 goto err_ret;
252
253 // trim trailing whitespace
254 while (p > expr && VIM_ISWHITE(p[-1]))
255 p--;
256 c = *p;
257 *p = NUL;
258 expr = vim_strsave(expr);
259 if (expr == NULL)
260 {
261 *p = c;
262 goto err_ret;
263 }
264 ((char_u **)(default_args->ga_data))
265 [default_args->ga_len] = expr;
266 default_args->ga_len++;
267 *p = c;
268 }
269 else
270 mustend = TRUE;
271 }
272 else if (any_default)
273 {
274 emsg(_("E989: Non-default argument follows default argument"));
275 mustend = TRUE;
276 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200277 if (*p == ',')
278 ++p;
279 else
280 mustend = TRUE;
281 }
Bram Moolenaar2c330432020-04-13 14:41:35 +0200282 whitep = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200283 p = skipwhite(p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200284 }
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200285
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200286 if (*p != endchar)
287 goto err_ret;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100288 ++p; // skip "endchar"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200289
290 *argp = p;
291 return OK;
292
293err_ret:
294 if (newargs != NULL)
295 ga_clear_strings(newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200296 if (default_args != NULL)
297 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200298 return FAIL;
299}
300
301/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200302 * Register function "fp" as using "current_funccal" as its scope.
303 */
304 static int
305register_closure(ufunc_T *fp)
306{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200307 if (fp->uf_scoped == current_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100308 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200309 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200310 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200311 fp->uf_scoped = current_funccal;
312 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200313
Bram Moolenaar58016442016-07-31 18:30:22 +0200314 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
315 return FAIL;
316 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
317 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200318 return OK;
319}
320
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100321 static void
322set_ufunc_name(ufunc_T *fp, char_u *name)
323{
324 STRCPY(fp->uf_name, name);
325
326 if (name[0] == K_SPECIAL)
327 {
328 fp->uf_name_exp = alloc(STRLEN(name) + 3);
329 if (fp->uf_name_exp != NULL)
330 {
331 STRCPY(fp->uf_name_exp, "<SNR>");
332 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
333 }
334 }
335}
336
Bram Moolenaar58016442016-07-31 18:30:22 +0200337/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200338 * Parse a lambda expression and get a Funcref from "*arg".
339 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
340 */
341 int
342get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
343{
344 garray_T newargs;
345 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200346 garray_T *pnewargs;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200347 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100348 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200349 int varargs;
350 int ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200351 char_u *start = skipwhite(*arg + 1);
352 char_u *s, *e;
353 static int lambda_no = 0;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200354 int *old_eval_lavars = eval_lavars_used;
355 int eval_lavars = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200356
357 ga_init(&newargs);
358 ga_init(&newlines);
359
Bram Moolenaare38eab22019-12-05 21:50:01 +0100360 // First, check if this is a lambda expression. "->" must exist.
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200361 ret = get_function_args(&start, '-', NULL, NULL, NULL, NULL, TRUE,
362 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200363 if (ret == FAIL || *start != '>')
364 return NOTDONE;
365
Bram Moolenaare38eab22019-12-05 21:50:01 +0100366 // Parse the arguments again.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200367 if (evaluate)
368 pnewargs = &newargs;
369 else
370 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200371 *arg = skipwhite(*arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100372 // TODO: argument types
Bram Moolenaar5e774c72020-04-12 21:53:00 +0200373 ret = get_function_args(arg, '-', pnewargs, NULL, &varargs, NULL, FALSE,
374 NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200375 if (ret == FAIL || **arg != '>')
376 goto errret;
377
Bram Moolenaare38eab22019-12-05 21:50:01 +0100378 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200379 if (evaluate)
380 eval_lavars_used = &eval_lavars;
381
Bram Moolenaare38eab22019-12-05 21:50:01 +0100382 // Get the start and the end of the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200383 *arg = skipwhite(*arg + 1);
384 s = *arg;
385 ret = skip_expr(arg);
386 if (ret == FAIL)
387 goto errret;
388 e = *arg;
389 *arg = skipwhite(*arg);
390 if (**arg != '}')
Bram Moolenaaree619e52020-03-28 21:38:06 +0100391 {
392 semsg(_("E451: Expected }: %s"), *arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200393 goto errret;
Bram Moolenaaree619e52020-03-28 21:38:06 +0100394 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200395 ++*arg;
396
397 if (evaluate)
398 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200399 int len, flags = 0;
400 char_u *p;
401 char_u name[20];
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200402
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200403 sprintf((char*)name, "<lambda>%d", ++lambda_no);
404
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200405 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200406 if (fp == NULL)
407 goto errret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100408 fp->uf_dfunc_idx = -1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200409 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200410 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200411 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200412
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200413 ga_init2(&newlines, (int)sizeof(char_u *), 1);
414 if (ga_grow(&newlines, 1) == FAIL)
415 goto errret;
416
Bram Moolenaare38eab22019-12-05 21:50:01 +0100417 // Add "return " before the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200418 len = 7 + e - s + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200419 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200420 if (p == NULL)
421 goto errret;
422 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
423 STRCPY(p, "return ");
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200424 vim_strncpy(p + 7, s, e - s);
Bram Moolenaarf10806b2020-04-02 18:34:35 +0200425 if (strstr((char *)p + 7, "a:") == NULL)
426 // No a: variables are used for sure.
427 flags |= FC_NOARGS;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200428
429 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100430 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200431 hash_add(&func_hashtab, UF2HIKEY(fp));
432 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200433 ga_init(&fp->uf_def_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200434 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200435 if (current_funccal != NULL && eval_lavars)
436 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200437 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200438 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200439 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200440 }
441 else
442 fp->uf_scoped = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200443
444#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200445 if (prof_def_func())
446 func_do_profile(fp);
447#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200448 if (sandbox)
449 flags |= FC_SANDBOX;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100450 // can be called with more args than uf_args.ga_len
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200451 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200452 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200453 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200454 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100455 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200456
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200457 pt->pt_func = fp;
458 pt->pt_refcount = 1;
459 rettv->vval.v_partial = pt;
460 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200461 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200462
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200463 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200464 return OK;
465
466errret:
467 ga_clear_strings(&newargs);
468 ga_clear_strings(&newlines);
469 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100470 vim_free(pt);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200471 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200472 return FAIL;
473}
474
475/*
476 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
477 * name it contains, otherwise return "name".
478 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
479 * "partialp".
480 */
481 char_u *
482deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
483{
484 dictitem_T *v;
485 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200486 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200487
488 if (partialp != NULL)
489 *partialp = NULL;
490
491 cc = name[*lenp];
492 name[*lenp] = NUL;
493 v = find_var(name, NULL, no_autoload);
494 name[*lenp] = cc;
495 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
496 {
497 if (v->di_tv.vval.v_string == NULL)
498 {
499 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100500 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200501 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200502 s = v->di_tv.vval.v_string;
503 *lenp = (int)STRLEN(s);
504 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200505 }
506
507 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
508 {
509 partial_T *pt = v->di_tv.vval.v_partial;
510
511 if (pt == NULL)
512 {
513 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100514 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200515 }
516 if (partialp != NULL)
517 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200518 s = partial_name(pt);
519 *lenp = (int)STRLEN(s);
520 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200521 }
522
523 return name;
524}
525
526/*
527 * Give an error message with a function name. Handle <SNR> things.
528 * "ermsg" is to be passed without translation, use N_() instead of _().
529 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100530 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200531emsg_funcname(char *ermsg, char_u *name)
532{
533 char_u *p;
534
535 if (*name == K_SPECIAL)
536 p = concat_str((char_u *)"<SNR>", name + 3);
537 else
538 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100539 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200540 if (p != name)
541 vim_free(p);
542}
543
544/*
545 * Allocate a variable for the result of a function.
546 * Return OK or FAIL.
547 */
548 int
549get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200550 char_u *name, // name of the function
551 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200552 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200553 char_u **arg, // argument, pointing to the '('
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200554 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200555{
556 char_u *argp;
557 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100558 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
559 int argcount = 0; // number of arguments found
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200560
561 /*
562 * Get the arguments.
563 */
564 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200565 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
566 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200567 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100568 argp = skipwhite(argp + 1); // skip the '(' or ','
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200569 if (*argp == ')' || *argp == ',' || *argp == NUL)
570 break;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200571 if (eval1(&argp, &argvars[argcount], funcexe->evaluate) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200572 {
573 ret = FAIL;
574 break;
575 }
576 ++argcount;
577 if (*argp != ',')
578 break;
579 }
580 if (*argp == ')')
581 ++argp;
582 else
583 ret = FAIL;
584
585 if (ret == OK)
586 {
587 int i = 0;
588
589 if (get_vim_var_nr(VV_TESTING))
590 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100591 // Prepare for calling test_garbagecollect_now(), need to know
592 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200593 if (funcargs.ga_itemsize == 0)
594 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
595 for (i = 0; i < argcount; ++i)
596 if (ga_grow(&funcargs, 1) == OK)
597 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
598 &argvars[i];
599 }
600
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200601 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200602
603 funcargs.ga_len -= i;
604 }
605 else if (!aborting())
606 {
607 if (argcount == MAX_FUNC_ARGS)
608 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
609 else
610 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
611 }
612
613 while (--argcount >= 0)
614 clear_tv(&argvars[argcount]);
615
616 *arg = skipwhite(argp);
617 return ret;
618}
619
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200620/*
621 * Return TRUE if "p" starts with "<SID>" or "s:".
622 * Only works if eval_fname_script() returned non-zero for "p"!
623 */
624 static int
625eval_fname_sid(char_u *p)
626{
627 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
628}
629
630/*
631 * In a script change <SID>name() and s:name() to K_SNR 123_name().
632 * Change <SNR>123_name() to K_SNR 123_name().
633 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
634 * (slow).
635 */
Bram Moolenaar5cab73f2020-02-06 19:25:19 +0100636 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200637fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
638{
639 int llen;
640 char_u *fname;
641 int i;
642
643 llen = eval_fname_script(name);
644 if (llen > 0)
645 {
646 fname_buf[0] = K_SPECIAL;
647 fname_buf[1] = KS_EXTRA;
648 fname_buf[2] = (int)KE_SNR;
649 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100650 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200651 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200652 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100653 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200654 else
655 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200656 sprintf((char *)fname_buf + 3, "%ld_",
657 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200658 i = (int)STRLEN(fname_buf);
659 }
660 }
661 if (i + STRLEN(name + llen) < FLEN_FIXED)
662 {
663 STRCPY(fname_buf + i, name + llen);
664 fname = fname_buf;
665 }
666 else
667 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200668 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200669 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +0100670 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200671 else
672 {
673 *tofree = fname;
674 mch_memmove(fname, fname_buf, (size_t)i);
675 STRCPY(fname + i, name + llen);
676 }
677 }
678 }
679 else
680 fname = name;
681 return fname;
682}
683
684/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685 * Find a function "name" in script "sid".
686 */
687 static ufunc_T *
688find_func_with_sid(char_u *name, int sid)
689{
690 hashitem_T *hi;
691 char_u buffer[200];
692
693 buffer[0] = K_SPECIAL;
694 buffer[1] = KS_EXTRA;
695 buffer[2] = (int)KE_SNR;
696 vim_snprintf((char *)buffer + 3, sizeof(buffer) - 3, "%ld_%s",
697 (long)sid, name);
698 hi = hash_find(&func_hashtab, buffer);
699 if (!HASHITEM_EMPTY(hi))
700 return HI2UF(hi);
701
702 return NULL;
703}
704
705/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200706 * Find a function by name, return pointer to it in ufuncs.
707 * Return NULL for unknown function.
708 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709 static ufunc_T *
710find_func_even_dead(char_u *name, cctx_T *cctx)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200711{
712 hashitem_T *hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100713 ufunc_T *func;
714 imported_T *imported;
715
716 if (in_vim9script())
717 {
718 // Find script-local function before global one.
719 func = find_func_with_sid(name, current_sctx.sc_sid);
720 if (func != NULL)
721 return func;
722
723 // Find imported funcion before global one.
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +0100724 imported = find_imported(name, 0, cctx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100725 if (imported != NULL && imported->imp_funcname != NULL)
726 {
727 hi = hash_find(&func_hashtab, imported->imp_funcname);
728 if (!HASHITEM_EMPTY(hi))
729 return HI2UF(hi);
730 }
731 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200732
733 hi = hash_find(&func_hashtab, name);
734 if (!HASHITEM_EMPTY(hi))
735 return HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100736
737 return NULL;
738}
739
740/*
741 * Find a function by name, return pointer to it in ufuncs.
742 * "cctx" is passed in a :def function to find imported functions.
743 * Return NULL for unknown or dead function.
744 */
745 ufunc_T *
746find_func(char_u *name, cctx_T *cctx)
747{
748 ufunc_T *fp = find_func_even_dead(name, cctx);
749
750 if (fp != NULL && (fp->uf_flags & FC_DEAD) == 0)
751 return fp;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200752 return NULL;
753}
754
755/*
756 * Copy the function name of "fp" to buffer "buf".
757 * "buf" must be able to hold the function name plus three bytes.
758 * Takes care of script-local function names.
759 */
760 static void
761cat_func_name(char_u *buf, ufunc_T *fp)
762{
763 if (fp->uf_name[0] == K_SPECIAL)
764 {
765 STRCPY(buf, "<SNR>");
766 STRCAT(buf, fp->uf_name + 3);
767 }
768 else
769 STRCPY(buf, fp->uf_name);
770}
771
772/*
773 * Add a number variable "name" to dict "dp" with value "nr".
774 */
775 static void
776add_nr_var(
777 dict_T *dp,
778 dictitem_T *v,
779 char *name,
780 varnumber_T nr)
781{
782 STRCPY(v->di_key, name);
783 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
784 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
785 v->di_tv.v_type = VAR_NUMBER;
786 v->di_tv.v_lock = VAR_FIXED;
787 v->di_tv.vval.v_number = nr;
788}
789
790/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100791 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200792 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100793 static void
794free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200795{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100796 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200797
798 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
799 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100800 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200801
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100802 // When garbage collecting a funccall_T may be freed before the
803 // function that references it, clear its uf_scoped field.
804 // The function may have been redefined and point to another
805 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200806 if (fp != NULL && fp->uf_scoped == fc)
807 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200808 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200809 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200810
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200811 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200812 vim_free(fc);
813}
814
815/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100816 * Free "fc" and what it contains.
817 * Can be called only when "fc" is kept beyond the period of it called,
818 * i.e. after cleanup_function_call(fc).
819 */
820 static void
821free_funccal_contents(funccall_T *fc)
822{
823 listitem_T *li;
824
825 // Free all l: variables.
826 vars_clear(&fc->l_vars.dv_hashtab);
827
828 // Free all a: variables.
829 vars_clear(&fc->l_avars.dv_hashtab);
830
831 // Free the a:000 variables.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200832 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100833 clear_tv(&li->li_tv);
834
835 free_funccal(fc);
836}
837
838/*
Bram Moolenaar6914c642017-04-01 21:21:30 +0200839 * Handle the last part of returning from a function: free the local hashtable.
840 * Unless it is still in use by a closure.
841 */
842 static void
843cleanup_function_call(funccall_T *fc)
844{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100845 int may_free_fc = fc->fc_refcount <= 0;
846 int free_fc = TRUE;
847
Bram Moolenaar6914c642017-04-01 21:21:30 +0200848 current_funccal = fc->caller;
849
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100850 // Free all l: variables if not referred.
851 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
852 vars_clear(&fc->l_vars.dv_hashtab);
853 else
854 free_fc = FALSE;
855
856 // If the a:000 list and the l: and a: dicts are not referenced and
857 // there is no closure using it, we can free the funccall_T and what's
858 // in it.
859 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
860 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200861 else
862 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100863 int todo;
864 hashitem_T *hi;
865 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200866
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100867 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200868
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100869 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200870 todo = (int)fc->l_avars.dv_hashtab.ht_used;
871 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
872 {
873 if (!HASHITEM_EMPTY(hi))
874 {
875 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100876 di = HI2DI(hi);
877 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200878 }
879 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100880 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200881
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100882 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
883 fc->l_varlist.lv_first = NULL;
884 else
885 {
886 listitem_T *li;
887
888 free_fc = FALSE;
889
890 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200891 FOR_ALL_LIST_ITEMS(&fc->l_varlist, li)
Bram Moolenaar6914c642017-04-01 21:21:30 +0200892 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100893 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100894
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100895 if (free_fc)
896 free_funccal(fc);
897 else
898 {
899 static int made_copy = 0;
900
901 // "fc" is still in use. This can happen when returning "a:000",
902 // assigning "l:" to a global variable or defining a closure.
903 // Link "fc" in the list for garbage collection later.
904 fc->caller = previous_funccal;
905 previous_funccal = fc;
906
907 if (want_garbage_collect)
908 // If garbage collector is ready, clear count.
909 made_copy = 0;
910 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100911 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100912 // We have made a lot of copies, worth 4 Mbyte. This can happen
913 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100914 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100915 // too much memory.
916 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100917 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100918 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200919 }
920}
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100921/*
922 * Unreference "fc": decrement the reference count and free it when it
923 * becomes zero. "fp" is detached from "fc".
924 * When "force" is TRUE we are exiting.
925 */
926 static void
927funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
928{
929 funccall_T **pfc;
930 int i;
931
932 if (fc == NULL)
933 return;
934
935 if (--fc->fc_refcount <= 0 && (force || (
936 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
937 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
938 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
939 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
940 {
941 if (fc == *pfc)
942 {
943 *pfc = fc->caller;
944 free_funccal_contents(fc);
945 return;
946 }
947 }
948 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
949 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
950 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
951}
952
953/*
954 * Remove the function from the function hashtable. If the function was
955 * deleted while it still has references this was already done.
956 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
957 */
958 static int
959func_remove(ufunc_T *fp)
960{
961 hashitem_T *hi;
962
963 // Return if it was already virtually deleted.
964 if (fp->uf_flags & FC_DEAD)
965 return FALSE;
966
967 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
968 if (!HASHITEM_EMPTY(hi))
969 {
970 // When there is a def-function index do not actually remove the
971 // function, so we can find the index when defining the function again.
972 if (fp->uf_dfunc_idx >= 0)
973 fp->uf_flags |= FC_DEAD;
974 else
975 hash_remove(&func_hashtab, hi);
976 return TRUE;
977 }
978 return FALSE;
979}
980
981 static void
982func_clear_items(ufunc_T *fp)
983{
984 ga_clear_strings(&(fp->uf_args));
985 ga_clear_strings(&(fp->uf_def_args));
986 ga_clear_strings(&(fp->uf_lines));
987 VIM_CLEAR(fp->uf_name_exp);
988 VIM_CLEAR(fp->uf_arg_types);
Bram Moolenaar292b90d2020-03-18 15:23:16 +0100989 VIM_CLEAR(fp->uf_def_arg_idx);
990 VIM_CLEAR(fp->uf_va_name);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200991 while (fp->uf_type_list.ga_len > 0)
992 vim_free(((type_T **)fp->uf_type_list.ga_data)
993 [--fp->uf_type_list.ga_len]);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100994 ga_clear(&fp->uf_type_list);
995#ifdef FEAT_PROFILE
996 VIM_CLEAR(fp->uf_tml_count);
997 VIM_CLEAR(fp->uf_tml_total);
998 VIM_CLEAR(fp->uf_tml_self);
999#endif
1000}
1001
1002/*
1003 * Free all things that a function contains. Does not free the function
1004 * itself, use func_free() for that.
1005 * When "force" is TRUE we are exiting.
1006 */
1007 static void
1008func_clear(ufunc_T *fp, int force)
1009{
1010 if (fp->uf_cleared)
1011 return;
1012 fp->uf_cleared = TRUE;
1013
1014 // clear this function
1015 func_clear_items(fp);
1016 funccal_unref(fp->uf_scoped, fp, force);
1017 delete_def_function(fp);
1018}
1019
1020/*
1021 * Free a function and remove it from the list of functions. Does not free
1022 * what a function contains, call func_clear() first.
1023 */
1024 static void
1025func_free(ufunc_T *fp)
1026{
1027 // Only remove it when not done already, otherwise we would remove a newer
1028 // version of the function with the same name.
1029 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1030 func_remove(fp);
1031
1032 if ((fp->uf_flags & FC_DEAD) == 0)
1033 vim_free(fp);
1034}
1035
1036/*
1037 * Free all things that a function contains and free the function itself.
1038 * When "force" is TRUE we are exiting.
1039 */
1040 static void
1041func_clear_free(ufunc_T *fp, int force)
1042{
1043 func_clear(fp, force);
1044 func_free(fp);
1045}
1046
Bram Moolenaar6914c642017-04-01 21:21:30 +02001047
1048/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001049 * Call a user function.
1050 */
1051 static void
1052call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +01001053 ufunc_T *fp, // pointer to function
1054 int argcount, // nr of args
1055 typval_T *argvars, // arguments
1056 typval_T *rettv, // return value
1057 linenr_T firstline, // first line of range
1058 linenr_T lastline, // last line of range
1059 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001060{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001061 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +02001062 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001063 funccall_T *fc;
1064 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001065 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001066 static int depth = 0;
1067 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001068 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001069 int i;
1070 int ai;
1071 int islambda = FALSE;
1072 char_u numbuf[NUMBUFLEN];
1073 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001074#ifdef FEAT_PROFILE
1075 proftime_T wait_start;
1076 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +02001077 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001078#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01001079 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001080
Bram Moolenaare38eab22019-12-05 21:50:01 +01001081 // If depth of calling is getting too high, don't execute the function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001082 if (depth >= p_mfd)
1083 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001084 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001085 rettv->v_type = VAR_NUMBER;
1086 rettv->vval.v_number = -1;
1087 return;
1088 }
1089 ++depth;
1090
Bram Moolenaare38eab22019-12-05 21:50:01 +01001091 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001092
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001093 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +01001094 if (fc == NULL)
1095 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001096 fc->caller = current_funccal;
1097 current_funccal = fc;
1098 fc->func = fp;
1099 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001100 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001101 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001102 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
1103 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001104 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001105 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001106 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001108 if (fp->uf_dfunc_idx >= 0)
1109 {
1110 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001111 save_current_sctx = current_sctx;
1112 current_sctx = fp->uf_script_ctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001113
1114 // Execute the compiled function.
1115 call_def_function(fp, argcount, argvars, rettv);
1116 --depth;
1117 current_funccal = fc->caller;
1118
1119 estack_pop();
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001120 current_sctx = save_current_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121 free_funccal(fc);
1122 return;
1123 }
1124
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001125 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
1126 islambda = TRUE;
1127
1128 /*
1129 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
1130 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
1131 * each argument variable and saves a lot of time.
1132 */
1133 /*
1134 * Init l: variables.
1135 */
1136 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
1137 if (selfdict != NULL)
1138 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001139 // Set l:self to "selfdict". Use "name" to avoid a warning from
1140 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001141 v = &fc->fixvar[fixvar_idx++].var;
1142 name = v->di_key;
1143 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +01001144 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001145 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
1146 v->di_tv.v_type = VAR_DICT;
1147 v->di_tv.v_lock = 0;
1148 v->di_tv.vval.v_dict = selfdict;
1149 ++selfdict->dv_refcount;
1150 }
1151
1152 /*
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001153 * Init a: variables, unless none found (in lambda).
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001154 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001155 * Set a:000 to a list with room for the "..." arguments.
1156 */
1157 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001158 if ((fp->uf_flags & FC_NOARGS) == 0)
1159 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001160 (varnumber_T)(argcount >= fp->uf_args.ga_len
1161 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +01001162 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001163 if ((fp->uf_flags & FC_NOARGS) == 0)
1164 {
1165 // Use "name" to avoid a warning from some compiler that checks the
1166 // destination size.
1167 v = &fc->fixvar[fixvar_idx++].var;
1168 name = v->di_key;
1169 STRCPY(name, "000");
1170 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
1171 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
1172 v->di_tv.v_type = VAR_LIST;
1173 v->di_tv.v_lock = VAR_FIXED;
1174 v->di_tv.vval.v_list = &fc->l_varlist;
1175 }
Bram Moolenaara80faa82020-04-12 19:37:17 +02001176 CLEAR_FIELD(fc->l_varlist);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001177 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
1178 fc->l_varlist.lv_lock = VAR_FIXED;
1179
1180 /*
1181 * Set a:firstline to "firstline" and a:lastline to "lastline".
1182 * Set a:name to named arguments.
1183 * Set a:N to the "..." arguments.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001184 * Skipped when no a: variables used (in lambda).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001185 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001186 if ((fp->uf_flags & FC_NOARGS) == 0)
1187 {
1188 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001189 (varnumber_T)firstline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001190 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001191 (varnumber_T)lastline);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001192 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001193 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001194 {
1195 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001196 typval_T def_rettv;
1197 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001198
1199 ai = i - fp->uf_args.ga_len;
1200 if (ai < 0)
1201 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001202 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001203 name = FUNCARG(fp, i);
1204 if (islambda)
1205 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001206
1207 // evaluate named argument default expression
1208 isdefault = ai + fp->uf_def_args.ga_len >= 0
1209 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
1210 && argvars[i].vval.v_number == VVAL_NONE));
1211 if (isdefault)
1212 {
1213 char_u *default_expr = NULL;
1214 def_rettv.v_type = VAR_NUMBER;
1215 def_rettv.vval.v_number = -1;
1216
1217 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
1218 [ai + fp->uf_def_args.ga_len];
1219 if (eval1(&default_expr, &def_rettv, TRUE) == FAIL)
1220 {
1221 default_arg_err = 1;
1222 break;
1223 }
1224 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001225 }
1226 else
1227 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001228 if ((fp->uf_flags & FC_NOARGS) != 0)
1229 // Bail out if no a: arguments used (in lambda).
1230 break;
1231
Bram Moolenaare38eab22019-12-05 21:50:01 +01001232 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001233 sprintf((char *)numbuf, "%d", ai + 1);
1234 name = numbuf;
1235 }
1236 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
1237 {
1238 v = &fc->fixvar[fixvar_idx++].var;
1239 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001240 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001241 }
1242 else
1243 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001244 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001245 if (v == NULL)
1246 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001247 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001248 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001249
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02001250 // Note: the values are copied directly to avoid alloc/free.
1251 // "argvars" must have VAR_FIXED for v_lock.
1252 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001253 v->di_tv.v_lock = VAR_FIXED;
1254
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001255 if (addlocal)
1256 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001257 // Named arguments should be accessed without the "a:" prefix in
1258 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001259 copy_tv(&v->di_tv, &v->di_tv);
1260 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001261 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001262 else
1263 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001264
1265 if (ai >= 0 && ai < MAX_FUNC_ARGS)
1266 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001267 listitem_T *li = &fc->l_listitems[ai];
1268
1269 li->li_tv = argvars[i];
1270 li->li_tv.v_lock = VAR_FIXED;
1271 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001272 }
1273 }
1274
Bram Moolenaare38eab22019-12-05 21:50:01 +01001275 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001276 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +02001277
1278 if (fp->uf_flags & FC_SANDBOX)
1279 {
1280 using_sandbox = TRUE;
1281 ++sandbox;
1282 }
1283
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001284 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001285 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001286 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001287 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001288 ++no_wait_return;
1289 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001290
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001291 smsg(_("calling %s"), SOURCING_NAME);
1292 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001293 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001294 char_u buf[MSG_BUF_LEN];
1295 char_u numbuf2[NUMBUFLEN];
1296 char_u *tofree;
1297 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001298
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001299 msg_puts("(");
1300 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001301 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001302 if (i > 0)
1303 msg_puts(", ");
1304 if (argvars[i].v_type == VAR_NUMBER)
1305 msg_outnum((long)argvars[i].vval.v_number);
1306 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001307 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001308 // Do not want errors such as E724 here.
1309 ++emsg_off;
1310 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1311 --emsg_off;
1312 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001313 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001314 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001315 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001316 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1317 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001318 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001319 msg_puts((char *)s);
1320 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001321 }
1322 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001323 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001324 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001325 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001326 msg_puts("\n"); // don't overwrite this either
1327
1328 verbose_leave_scroll();
1329 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001330 }
1331#ifdef FEAT_PROFILE
1332 if (do_profiling == PROF_YES)
1333 {
1334 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001335 {
1336 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001337 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001338 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001339 if (fp->uf_profiling
1340 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1341 {
1342 ++fp->uf_tm_count;
1343 profile_start(&call_start);
1344 profile_zero(&fp->uf_tm_children);
1345 }
1346 script_prof_save(&wait_start);
1347 }
1348#endif
1349
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001350 save_current_sctx = current_sctx;
1351 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001352 save_did_emsg = did_emsg;
1353 did_emsg = FALSE;
1354
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001355 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1356 did_emsg = TRUE;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001357 else if (islambda)
1358 {
1359 char_u *p = *(char_u **)fp->uf_lines.ga_data + 7;
1360
1361 // A Lambda always has the command "return {expr}". It is much faster
1362 // to evaluate {expr} directly.
1363 ++ex_nesting_level;
Bram Moolenaard1e9dc22020-04-03 18:13:57 +02001364 (void)eval1(&p, rettv, TRUE);
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001365 --ex_nesting_level;
1366 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001367 else
1368 // call do_cmdline() to execute the lines
1369 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001370 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1371
1372 --RedrawingDisabled;
1373
Bram Moolenaare38eab22019-12-05 21:50:01 +01001374 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001375 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1376 {
1377 clear_tv(rettv);
1378 rettv->v_type = VAR_NUMBER;
1379 rettv->vval.v_number = -1;
1380 }
1381
1382#ifdef FEAT_PROFILE
1383 if (do_profiling == PROF_YES && (fp->uf_profiling
1384 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1385 {
1386 profile_end(&call_start);
1387 profile_sub_wait(&wait_start, &call_start);
1388 profile_add(&fp->uf_tm_total, &call_start);
1389 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1390 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1391 {
1392 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1393 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1394 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001395 if (started_profiling)
1396 // make a ":profdel func" stop profiling the function
1397 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001398 }
1399#endif
1400
Bram Moolenaare38eab22019-12-05 21:50:01 +01001401 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001402 if (p_verbose >= 12)
1403 {
1404 ++no_wait_return;
1405 verbose_enter_scroll();
1406
1407 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001408 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001409 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001410 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001411 (long)fc->rettv->vval.v_number);
1412 else
1413 {
1414 char_u buf[MSG_BUF_LEN];
1415 char_u numbuf2[NUMBUFLEN];
1416 char_u *tofree;
1417 char_u *s;
1418
Bram Moolenaare38eab22019-12-05 21:50:01 +01001419 // The value may be very long. Skip the middle part, so that we
1420 // have some idea how it starts and ends. smsg() would always
1421 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001422 ++emsg_off;
1423 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1424 --emsg_off;
1425 if (s != NULL)
1426 {
1427 if (vim_strsize(s) > MSG_BUF_CLEN)
1428 {
1429 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1430 s = buf;
1431 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001432 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001433 vim_free(tofree);
1434 }
1435 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001436 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001437
1438 verbose_leave_scroll();
1439 --no_wait_return;
1440 }
1441
Bram Moolenaare31ee862020-01-07 20:59:34 +01001442 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001443 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001444 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001445#ifdef FEAT_PROFILE
1446 if (do_profiling == PROF_YES)
1447 script_prof_restore(&wait_start);
1448#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001449 if (using_sandbox)
1450 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001451
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001452 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001453 {
1454 ++no_wait_return;
1455 verbose_enter_scroll();
1456
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001457 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001458 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001459
1460 verbose_leave_scroll();
1461 --no_wait_return;
1462 }
1463
1464 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001465 --depth;
1466
Bram Moolenaar6914c642017-04-01 21:21:30 +02001467 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001468}
1469
1470/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 * Call a user function after checking the arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001472 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 int
1474call_user_func_check(
1475 ufunc_T *fp,
1476 int argcount,
1477 typval_T *argvars,
1478 typval_T *rettv,
1479 funcexe_T *funcexe,
1480 dict_T *selfdict)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001481{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 int error;
1483 int regular_args = fp->uf_args.ga_len;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001484
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1486 *funcexe->doesrange = TRUE;
1487 if (argcount < regular_args - fp->uf_def_args.ga_len)
1488 error = FCERR_TOOFEW;
1489 else if (!has_varargs(fp) && argcount > regular_args)
1490 error = FCERR_TOOMANY;
1491 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1492 error = FCERR_DICT;
1493 else
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001494 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495 int did_save_redo = FALSE;
1496 save_redo_T save_redo;
1497
1498 /*
1499 * Call the user function.
1500 * Save and restore search patterns, script variables and
1501 * redo buffer.
1502 */
1503 save_search_patterns();
1504 if (!ins_compl_active())
1505 {
1506 saveRedobuff(&save_redo);
1507 did_save_redo = TRUE;
1508 }
1509 ++fp->uf_calls;
1510 call_user_func(fp, argcount, argvars, rettv,
1511 funcexe->firstline, funcexe->lastline,
1512 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
1513 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
1514 // Function was unreferenced while being used, free it now.
1515 func_clear_free(fp, FALSE);
1516 if (did_save_redo)
1517 restoreRedobuff(&save_redo);
1518 restore_search_patterns();
1519 error = FCERR_NONE;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001520 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521 return error;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001522}
1523
1524/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001525 * There are two kinds of function names:
1526 * 1. ordinary names, function defined with :function
1527 * 2. numbered functions and lambdas
1528 * For the first we only count the name stored in func_hashtab as a reference,
1529 * using function() does not count as a reference, because the function is
1530 * looked up by name.
1531 */
1532 static int
1533func_name_refcount(char_u *name)
1534{
1535 return isdigit(*name) || *name == '<';
1536}
1537
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001538static funccal_entry_T *funccal_stack = NULL;
1539
1540/*
1541 * Save the current function call pointer, and set it to NULL.
1542 * Used when executing autocommands and for ":source".
1543 */
1544 void
1545save_funccal(funccal_entry_T *entry)
1546{
1547 entry->top_funccal = current_funccal;
1548 entry->next = funccal_stack;
1549 funccal_stack = entry;
1550 current_funccal = NULL;
1551}
1552
1553 void
1554restore_funccal(void)
1555{
1556 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001557 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001558 else
1559 {
1560 current_funccal = funccal_stack->top_funccal;
1561 funccal_stack = funccal_stack->next;
1562 }
1563}
1564
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001565 funccall_T *
1566get_current_funccal(void)
1567{
1568 return current_funccal;
1569}
1570
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001571#if defined(EXITFREE) || defined(PROTO)
1572 void
1573free_all_functions(void)
1574{
1575 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001576 ufunc_T *fp;
1577 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001578 long_u todo = 1;
1579 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001580
Bram Moolenaare38eab22019-12-05 21:50:01 +01001581 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001582 while (current_funccal != NULL)
1583 {
1584 clear_tv(current_funccal->rettv);
1585 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001586 if (current_funccal == NULL && funccal_stack != NULL)
1587 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001588 }
1589
Bram Moolenaare38eab22019-12-05 21:50:01 +01001590 // First clear what the functions contain. Since this may lower the
1591 // reference count of a function, it may also free a function and change
1592 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001593 while (todo > 0)
1594 {
1595 todo = func_hashtab.ht_used;
1596 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1597 if (!HASHITEM_EMPTY(hi))
1598 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 // clear the def function index now
1600 fp = HI2UF(hi);
1601 fp->uf_flags &= ~FC_DEAD;
1602 fp->uf_dfunc_idx = -1;
1603
Bram Moolenaare38eab22019-12-05 21:50:01 +01001604 // Only free functions that are not refcounted, those are
1605 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001606 if (func_name_refcount(fp->uf_name))
1607 ++skipped;
1608 else
1609 {
1610 used = func_hashtab.ht_used;
1611 func_clear(fp, TRUE);
1612 if (used != func_hashtab.ht_used)
1613 {
1614 skipped = 0;
1615 break;
1616 }
1617 }
1618 --todo;
1619 }
1620 }
1621
Bram Moolenaare38eab22019-12-05 21:50:01 +01001622 // Now actually free the functions. Need to start all over every time,
1623 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001624 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001625 while (func_hashtab.ht_used > skipped)
1626 {
1627 todo = func_hashtab.ht_used;
1628 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001629 if (!HASHITEM_EMPTY(hi))
1630 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001631 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001632 // Only free functions that are not refcounted, those are
1633 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02001634 fp = HI2UF(hi);
1635 if (func_name_refcount(fp->uf_name))
1636 ++skipped;
1637 else
1638 {
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001639 func_free(fp);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001640 skipped = 0;
1641 break;
1642 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001643 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001644 }
1645 if (skipped == 0)
1646 hash_clear(&func_hashtab);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647
1648 free_def_functions();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001649}
1650#endif
1651
1652/*
1653 * Return TRUE if "name" looks like a builtin function name: starts with a
1654 * lower case letter and doesn't contain AUTOLOAD_CHAR.
1655 * "len" is the length of "name", or -1 for NUL terminated.
1656 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 int
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001658builtin_function(char_u *name, int len)
1659{
1660 char_u *p;
1661
1662 if (!ASCII_ISLOWER(name[0]))
1663 return FALSE;
1664 p = vim_strchr(name, AUTOLOAD_CHAR);
1665 return p == NULL || (len > 0 && p > name + len);
1666}
1667
1668 int
1669func_call(
1670 char_u *name,
1671 typval_T *args,
1672 partial_T *partial,
1673 dict_T *selfdict,
1674 typval_T *rettv)
1675{
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001676 list_T *l = args->vval.v_list;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001677 listitem_T *item;
1678 typval_T argv[MAX_FUNC_ARGS + 1];
1679 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001680 int r = 0;
1681
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001682 range_list_materialize(l);
Bram Moolenaar00d253e2020-04-06 22:13:01 +02001683 FOR_ALL_LIST_ITEMS(l, item)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001684 {
1685 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1686 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001687 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001688 break;
1689 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001690 // Make a copy of each argument. This is needed to be able to set
1691 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001692 copy_tv(&item->li_tv, &argv[argc++]);
1693 }
1694
1695 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001696 {
1697 funcexe_T funcexe;
1698
Bram Moolenaara80faa82020-04-12 19:37:17 +02001699 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001700 funcexe.firstline = curwin->w_cursor.lnum;
1701 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001702 funcexe.evaluate = TRUE;
1703 funcexe.partial = partial;
1704 funcexe.selfdict = selfdict;
1705 r = call_func(name, -1, rettv, argc, argv, &funcexe);
1706 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001707
Bram Moolenaare38eab22019-12-05 21:50:01 +01001708 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001709 while (argc > 0)
1710 clear_tv(&argv[--argc]);
1711
1712 return r;
1713}
1714
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001715static int callback_depth = 0;
1716
1717 int
1718get_callback_depth(void)
1719{
1720 return callback_depth;
1721}
1722
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001723/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001724 * Invoke call_func() with a callback.
1725 */
1726 int
1727call_callback(
1728 callback_T *callback,
1729 int len, // length of "name" or -1 to use strlen()
1730 typval_T *rettv, // return value goes here
1731 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001732 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001733 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001734{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001735 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001736 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001737
Bram Moolenaara80faa82020-04-12 19:37:17 +02001738 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001739 funcexe.evaluate = TRUE;
1740 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001741 ++callback_depth;
1742 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
1743 --callback_depth;
1744 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001745}
1746
1747/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748 * Give an error message for the result of a function.
1749 * Nothing if "error" is FCERR_NONE.
1750 */
1751 void
1752user_func_error(int error, char_u *name)
1753{
1754 switch (error)
1755 {
1756 case FCERR_UNKNOWN:
1757 emsg_funcname(e_unknownfunc, name);
1758 break;
1759 case FCERR_NOTMETHOD:
1760 emsg_funcname(
1761 N_("E276: Cannot use function as a method: %s"), name);
1762 break;
1763 case FCERR_DELETED:
1764 emsg_funcname(N_(e_func_deleted), name);
1765 break;
1766 case FCERR_TOOMANY:
1767 emsg_funcname((char *)e_toomanyarg, name);
1768 break;
1769 case FCERR_TOOFEW:
1770 emsg_funcname((char *)e_toofewarg, name);
1771 break;
1772 case FCERR_SCRIPT:
1773 emsg_funcname(
1774 N_("E120: Using <SID> not in a script context: %s"), name);
1775 break;
1776 case FCERR_DICT:
1777 emsg_funcname(
1778 N_("E725: Calling dict function without Dictionary: %s"),
1779 name);
1780 break;
1781 }
1782}
1783
1784/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001785 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001786 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001787 * Return FAIL when the function can't be called, OK otherwise.
1788 * Also returns OK when an error was encountered while executing the function.
1789 */
1790 int
1791call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001792 char_u *funcname, // name of the function
1793 int len, // length of "name" or -1 to use strlen()
1794 typval_T *rettv, // return value goes here
1795 int argcount_in, // number of "argvars"
1796 typval_T *argvars_in, // vars for arguments, must have "argcount"
1797 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001798 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001799{
1800 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01001801 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001802 int i;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001803 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001804 char_u fname_buf[FLEN_FIXED + 1];
1805 char_u *tofree = NULL;
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001806 char_u *fname = NULL;
1807 char_u *name = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001808 int argcount = argcount_in;
1809 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001810 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001811 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
1812 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001813 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001814 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001815 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001816
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02001817 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
1818 // even when call_func() returns FAIL.
1819 rettv->v_type = VAR_UNKNOWN;
1820
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001821 if (partial != NULL)
1822 fp = partial->pt_func;
1823 if (fp == NULL)
1824 {
1825 // Make a copy of the name, if it comes from a funcref variable it
1826 // could be changed or deleted in the called function.
1827 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
1828 if (name == NULL)
1829 return ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001830
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001831 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1832 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001833
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001834 if (funcexe->doesrange != NULL)
1835 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001836
1837 if (partial != NULL)
1838 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001839 // When the function has a partial with a dict and there is a dict
1840 // argument, use the dict argument. That is backwards compatible.
1841 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001842 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001843 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01001844 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001845 {
1846 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001847 {
1848 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
1849 {
Bram Moolenaaref140542019-12-31 21:27:13 +01001850 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001851 goto theend;
1852 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001853 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001854 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001855 for (i = 0; i < argcount_in; ++i)
1856 argv[i + argv_clear] = argvars_in[i];
1857 argvars = argv;
1858 argcount = partial->pt_argc + argcount_in;
1859 }
1860 }
1861
Bram Moolenaaref140542019-12-31 21:27:13 +01001862 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001863 {
1864 char_u *rfname = fname;
1865
Bram Moolenaare38eab22019-12-05 21:50:01 +01001866 // Ignore "g:" before a function name.
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001867 if (fp == NULL && fname[0] == 'g' && fname[1] == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001868 rfname = fname + 2;
1869
Bram Moolenaare38eab22019-12-05 21:50:01 +01001870 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001871 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01001872 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001873
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001874 if (fp != NULL || !builtin_function(rfname, -1))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001875 {
1876 /*
1877 * User defined function.
1878 */
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001879 if (fp == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001881
Bram Moolenaare38eab22019-12-05 21:50:01 +01001882 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001883 if (fp == NULL
1884 && apply_autocmds(EVENT_FUNCUNDEFINED,
1885 rfname, rfname, TRUE, NULL)
1886 && !aborting())
1887 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001888 // executed an autocommand, search for the function again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001890 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001891 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001892 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
1893 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001894 // loaded a package, search for the function again
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001895 fp = find_func(rfname, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001896 }
1897
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001898 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01001899 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001900 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001901 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001902 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01001903 // postponed filling in the arguments, do it now
1904 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001905 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001906
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001907 if (funcexe->basetv != NULL)
1908 {
1909 // Method call: base->Method()
1910 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
1911 argv[0] = *funcexe->basetv;
1912 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001913 argvars = argv;
1914 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001915 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001916
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 error = call_user_func_check(fp, argcount, argvars, rettv,
1918 funcexe, selfdict);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001919 }
1920 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001921 else if (funcexe->basetv != NULL)
1922 {
1923 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001924 * expr->method(): Find the method name in the table, call its
1925 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02001926 */
1927 error = call_internal_method(fname, argcount, argvars, rettv,
1928 funcexe->basetv);
1929 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001930 else
1931 {
1932 /*
1933 * Find the function name in the table, call its implementation.
1934 */
1935 error = call_internal_func(fname, argcount, argvars, rettv);
1936 }
1937 /*
1938 * The function call (or "FuncUndefined" autocommand sequence) might
1939 * have been aborted by an error, an interrupt, or an explicitly thrown
1940 * exception that has not been caught so far. This situation can be
1941 * tested for by calling aborting(). For an error in an internal
1942 * function or for the "E132" error in call_user_func(), however, the
1943 * throw point at which the "force_abort" flag (temporarily reset by
1944 * emsg()) is normally updated has not been reached yet. We need to
1945 * update that flag first to make aborting() reliable.
1946 */
1947 update_force_abort();
1948 }
Bram Moolenaaref140542019-12-31 21:27:13 +01001949 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001950 ret = OK;
1951
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001952theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001953 /*
1954 * Report an error unless the argument evaluation or function call has been
1955 * cancelled due to an aborting error, an interrupt, or an exception.
1956 */
1957 if (!aborting())
1958 {
Bram Moolenaarf10806b2020-04-02 18:34:35 +02001959 user_func_error(error, (name != NULL) ? name : funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001960 }
1961
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001962 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001963 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001964 clear_tv(&argv[--argv_clear + argv_base]);
1965
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001966 vim_free(tofree);
1967 vim_free(name);
1968
1969 return ret;
1970}
1971
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001972 static char_u *
1973printable_func_name(ufunc_T *fp)
1974{
1975 return fp->uf_name_exp != NULL ? fp->uf_name_exp : fp->uf_name;
1976}
1977
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001978/*
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001979 * List the head of the function: "function name(arg1, arg2)".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001980 */
1981 static void
1982list_func_head(ufunc_T *fp, int indent)
1983{
1984 int j;
1985
1986 msg_start();
1987 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001988 msg_puts(" ");
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01001989 if (fp->uf_dfunc_idx >= 0)
1990 msg_puts("def ");
1991 else
1992 msg_puts("function ");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 msg_puts((char *)printable_func_name(fp));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001994 msg_putchar('(');
1995 for (j = 0; j < fp->uf_args.ga_len; ++j)
1996 {
1997 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001998 msg_puts(", ");
1999 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 if (fp->uf_arg_types != NULL)
2001 {
2002 char *tofree;
2003
2004 msg_puts(": ");
2005 msg_puts(type_name(fp->uf_arg_types[j], &tofree));
2006 vim_free(tofree);
2007 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002008 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
2009 {
2010 msg_puts(" = ");
2011 msg_puts(((char **)(fp->uf_def_args.ga_data))
2012 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
2013 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002014 }
2015 if (fp->uf_varargs)
2016 {
2017 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002018 msg_puts(", ");
2019 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002020 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 if (fp->uf_va_name != NULL)
2022 {
2023 if (j)
2024 msg_puts(", ");
2025 msg_puts("...");
2026 msg_puts((char *)fp->uf_va_name);
2027 if (fp->uf_va_type)
2028 {
2029 char *tofree;
2030
2031 msg_puts(": ");
2032 msg_puts(type_name(fp->uf_va_type, &tofree));
2033 vim_free(tofree);
2034 }
2035 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002036 msg_putchar(')');
Bram Moolenaar61a6d4e2020-03-01 23:32:25 +01002037
2038 if (fp->uf_dfunc_idx >= 0)
2039 {
2040 if (fp->uf_ret_type != &t_void)
2041 {
2042 char *tofree;
2043
2044 msg_puts(": ");
2045 msg_puts(type_name(fp->uf_ret_type, &tofree));
2046 vim_free(tofree);
2047 }
2048 }
2049 else if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002050 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002051 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002052 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002053 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002054 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002055 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01002056 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002057 msg_clr_eos();
2058 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002059 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002060}
2061
2062/*
2063 * Get a function name, translating "<SID>" and "<SNR>".
2064 * Also handles a Funcref in a List or Dictionary.
2065 * Returns the function name in allocated memory, or NULL for failure.
2066 * flags:
2067 * TFN_INT: internal function name OK
2068 * TFN_QUIET: be quiet
2069 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002070 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002071 * Advances "pp" to just after the function name (if no error).
2072 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002073 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002074trans_function_name(
2075 char_u **pp,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002076 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002077 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01002078 funcdict_T *fdp, // return: info about dictionary used
2079 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002080{
2081 char_u *name = NULL;
2082 char_u *start;
2083 char_u *end;
2084 int lead;
2085 char_u sid_buf[20];
2086 int len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 int extra = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002088 lval_T lv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 int vim9script;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002090
2091 if (fdp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02002092 CLEAR_POINTER(fdp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002093 start = *pp;
2094
Bram Moolenaare38eab22019-12-05 21:50:01 +01002095 // Check for hard coded <SNR>: already translated function ID (from a user
2096 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002097 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
2098 && (*pp)[2] == (int)KE_SNR)
2099 {
2100 *pp += 3;
2101 len = get_id_len(pp) + 3;
2102 return vim_strnsave(start, len);
2103 }
2104
Bram Moolenaare38eab22019-12-05 21:50:01 +01002105 // A name starting with "<SID>" or "<SNR>" is local to a script. But
2106 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002107 lead = eval_fname_script(start);
2108 if (lead > 2)
2109 start += lead;
2110
Bram Moolenaare38eab22019-12-05 21:50:01 +01002111 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01002112 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002113 lead > 2 ? 0 : FNE_CHECK_START);
2114 if (end == start)
2115 {
2116 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002117 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002118 goto theend;
2119 }
2120 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
2121 {
2122 /*
2123 * Report an invalid expression in braces, unless the expression
2124 * evaluation has been cancelled due to an aborting error, an
2125 * interrupt, or an exception.
2126 */
2127 if (!aborting())
2128 {
2129 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002130 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002131 }
2132 else
2133 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
2134 goto theend;
2135 }
2136
2137 if (lv.ll_tv != NULL)
2138 {
2139 if (fdp != NULL)
2140 {
2141 fdp->fd_dict = lv.ll_dict;
2142 fdp->fd_newkey = lv.ll_newkey;
2143 lv.ll_newkey = NULL;
2144 fdp->fd_di = lv.ll_di;
2145 }
2146 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
2147 {
2148 name = vim_strsave(lv.ll_tv->vval.v_string);
2149 *pp = end;
2150 }
2151 else if (lv.ll_tv->v_type == VAR_PARTIAL
2152 && lv.ll_tv->vval.v_partial != NULL)
2153 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002154 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002155 *pp = end;
2156 if (partial != NULL)
2157 *partial = lv.ll_tv->vval.v_partial;
2158 }
2159 else
2160 {
2161 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
2162 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002163 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002164 else
2165 *pp = end;
2166 name = NULL;
2167 }
2168 goto theend;
2169 }
2170
2171 if (lv.ll_name == NULL)
2172 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002173 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002174 *pp = end;
2175 goto theend;
2176 }
2177
Bram Moolenaare38eab22019-12-05 21:50:01 +01002178 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002179 if (lv.ll_exp_name != NULL)
2180 {
2181 len = (int)STRLEN(lv.ll_exp_name);
2182 name = deref_func_name(lv.ll_exp_name, &len, partial,
2183 flags & TFN_NO_AUTOLOAD);
2184 if (name == lv.ll_exp_name)
2185 name = NULL;
2186 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002187 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002188 {
2189 len = (int)(end - *pp);
2190 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
2191 if (name == *pp)
2192 name = NULL;
2193 }
2194 if (name != NULL)
2195 {
2196 name = vim_strsave(name);
2197 *pp = end;
2198 if (STRNCMP(name, "<SNR>", 5) == 0)
2199 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002200 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002201 name[0] = K_SPECIAL;
2202 name[1] = KS_EXTRA;
2203 name[2] = (int)KE_SNR;
2204 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
2205 }
2206 goto theend;
2207 }
2208
2209 if (lv.ll_exp_name != NULL)
2210 {
2211 len = (int)STRLEN(lv.ll_exp_name);
2212 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
2213 && STRNCMP(lv.ll_name, "s:", 2) == 0)
2214 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002215 // When there was "s:" already or the name expanded to get a
2216 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002217 lv.ll_name += 2;
2218 len -= 2;
2219 lead = 2;
2220 }
2221 }
2222 else
2223 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002224 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002225 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
2226 lv.ll_name += 2;
2227 len = (int)(end - lv.ll_name);
2228 }
2229
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 // In Vim9 script a user function is script-local by default.
2231 vim9script = ASCII_ISUPPER(*start)
2232 && current_sctx.sc_version == SCRIPT_VERSION_VIM9;
2233
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002234 /*
2235 * Copy the function name to allocated memory.
2236 * Accept <SID>name() inside a script, translate into <SNR>123_name().
2237 * Accept <SNR>123_name() outside a script.
2238 */
2239 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002240 lead = 0; // do nothing
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002241 else if (lead > 0 || vim9script)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002242 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002243 if (!vim9script)
2244 lead = 3;
2245 if (vim9script || (lv.ll_exp_name != NULL
2246 && eval_fname_sid(lv.ll_exp_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002247 || eval_fname_sid(*pp))
2248 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002249 // It's script-local, "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002250 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002251 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002252 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002253 goto theend;
2254 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002255 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256 if (vim9script)
2257 extra = 3 + (int)STRLEN(sid_buf);
2258 else
2259 lead += (int)STRLEN(sid_buf);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002260 }
2261 }
2262 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
2263 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002264 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002265 start);
2266 goto theend;
2267 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002268 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002269 {
2270 char_u *cp = vim_strchr(lv.ll_name, ':');
2271
2272 if (cp != NULL && cp < end)
2273 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002274 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002275 goto theend;
2276 }
2277 }
2278
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279 name = alloc(len + lead + extra + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002280 if (name != NULL)
2281 {
Bram Moolenaar9a5e5a32020-01-28 23:09:23 +01002282 if (!skip && (lead > 0 || vim9script))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002283 {
2284 name[0] = K_SPECIAL;
2285 name[1] = KS_EXTRA;
2286 name[2] = (int)KE_SNR;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002287 if (vim9script || lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002288 STRCPY(name + 3, sid_buf);
2289 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290 mch_memmove(name + lead + extra, lv.ll_name, (size_t)len);
2291 name[lead + extra + len] = NUL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002292 }
2293 *pp = end;
2294
2295theend:
2296 clear_lval(&lv);
2297 return name;
2298}
2299
2300/*
2301 * ":function"
2302 */
2303 void
2304ex_function(exarg_T *eap)
2305{
2306 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002307 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002308 int j;
2309 int c;
2310 int saved_did_emsg;
2311 int saved_wait_return = need_wait_return;
2312 char_u *name = NULL;
2313 char_u *p;
2314 char_u *arg;
2315 char_u *line_arg = NULL;
2316 garray_T newargs;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002317 garray_T argtypes;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002318 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002319 garray_T newlines;
2320 int varargs = FALSE;
2321 int flags = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 char_u *ret_type = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002323 ufunc_T *fp;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002324 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002325 int indent;
2326 int nesting;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002327#define MAX_FUNC_NESTING 50
2328 char nesting_def[MAX_FUNC_NESTING];
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002329 dictitem_T *v;
2330 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002331 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002332 int paren;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002333 int todo;
2334 hashitem_T *hi;
Bram Moolenaare96a2492019-06-25 04:12:16 +02002335 int do_concat = TRUE;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002336 linenr_T sourcing_lnum_off;
2337 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002338 int is_heredoc = FALSE;
2339 char_u *skip_until = NULL;
2340 char_u *heredoc_trimmed = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002341
2342 /*
2343 * ":function" without argument: list functions.
2344 */
2345 if (ends_excmd(*eap->arg))
2346 {
2347 if (!eap->skip)
2348 {
2349 todo = (int)func_hashtab.ht_used;
2350 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2351 {
2352 if (!HASHITEM_EMPTY(hi))
2353 {
2354 --todo;
2355 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002356 if ((fp->uf_flags & FC_DEAD)
2357 || message_filtered(fp->uf_name))
Bram Moolenaarf86db782018-10-25 13:31:37 +02002358 continue;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002359 if (!func_name_refcount(fp->uf_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002360 list_func_head(fp, FALSE);
2361 }
2362 }
2363 }
2364 eap->nextcmd = check_nextcmd(eap->arg);
2365 return;
2366 }
2367
2368 /*
2369 * ":function /pat": list functions matching pattern.
2370 */
2371 if (*eap->arg == '/')
2372 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02002373 p = skip_regexp(eap->arg + 1, '/', TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002374 if (!eap->skip)
2375 {
2376 regmatch_T regmatch;
2377
2378 c = *p;
2379 *p = NUL;
2380 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2381 *p = c;
2382 if (regmatch.regprog != NULL)
2383 {
2384 regmatch.rm_ic = p_ic;
2385
2386 todo = (int)func_hashtab.ht_used;
2387 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2388 {
2389 if (!HASHITEM_EMPTY(hi))
2390 {
2391 --todo;
2392 fp = HI2UF(hi);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 if ((fp->uf_flags & FC_DEAD) == 0
2394 && !isdigit(*fp->uf_name)
2395 && vim_regexec(&regmatch, fp->uf_name, 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002396 list_func_head(fp, FALSE);
2397 }
2398 }
2399 vim_regfree(regmatch.regprog);
2400 }
2401 }
2402 if (*p == '/')
2403 ++p;
2404 eap->nextcmd = check_nextcmd(p);
2405 return;
2406 }
2407
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408 ga_init(&newargs);
2409 ga_init(&argtypes);
2410 ga_init(&default_args);
2411
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002412 /*
2413 * Get the function name. There are these situations:
2414 * func normal function name
2415 * "name" == func, "fudi.fd_dict" == NULL
2416 * dict.func new dictionary entry
2417 * "name" == NULL, "fudi.fd_dict" set,
2418 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
2419 * dict.func existing dict entry with a Funcref
2420 * "name" == func, "fudi.fd_dict" set,
2421 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2422 * dict.func existing dict entry that's not a Funcref
2423 * "name" == NULL, "fudi.fd_dict" set,
2424 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2425 * s:func script-local function name
2426 * g:func global function name, same as "func"
2427 */
2428 p = eap->arg;
Bram Moolenaar3388d332017-12-07 22:23:04 +01002429 name = trans_function_name(&p, eap->skip, TFN_NO_AUTOLOAD, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002430 paren = (vim_strchr(p, '(') != NULL);
2431 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
2432 {
2433 /*
2434 * Return on an invalid expression in braces, unless the expression
2435 * evaluation has been cancelled due to an aborting error, an
2436 * interrupt, or an exception.
2437 */
2438 if (!aborting())
2439 {
2440 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002441 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002442 vim_free(fudi.fd_newkey);
2443 return;
2444 }
2445 else
2446 eap->skip = TRUE;
2447 }
2448
Bram Moolenaare38eab22019-12-05 21:50:01 +01002449 // An error in a function call during evaluation of an expression in magic
2450 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002451 saved_did_emsg = did_emsg;
2452 did_emsg = FALSE;
2453
2454 /*
2455 * ":function func" with only function name: list function.
2456 */
2457 if (!paren)
2458 {
2459 if (!ends_excmd(*skipwhite(p)))
2460 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002461 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002462 goto ret_free;
2463 }
2464 eap->nextcmd = check_nextcmd(p);
2465 if (eap->nextcmd != NULL)
2466 *p = NUL;
2467 if (!eap->skip && !got_int)
2468 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002469 fp = find_func(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002470 if (fp != NULL)
2471 {
2472 list_func_head(fp, TRUE);
2473 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2474 {
2475 if (FUNCLINE(fp, j) == NULL)
2476 continue;
2477 msg_putchar('\n');
2478 msg_outnum((long)(j + 1));
2479 if (j < 9)
2480 msg_putchar(' ');
2481 if (j < 99)
2482 msg_putchar(' ');
2483 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002484 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002485 ui_breakcheck();
2486 }
2487 if (!got_int)
2488 {
2489 msg_putchar('\n');
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 if (fp->uf_dfunc_idx >= 0)
2491 msg_puts(" enddef");
2492 else
2493 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002494 }
2495 }
2496 else
2497 emsg_funcname(N_("E123: Undefined function: %s"), name);
2498 }
2499 goto ret_free;
2500 }
2501
2502 /*
2503 * ":function name(arg1, arg2)" Define function.
2504 */
2505 p = skipwhite(p);
2506 if (*p != '(')
2507 {
2508 if (!eap->skip)
2509 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002510 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002511 goto ret_free;
2512 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002513 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002514 if (vim_strchr(p, '(') != NULL)
2515 p = vim_strchr(p, '(');
2516 }
2517 p = skipwhite(p + 1);
2518
2519 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2520
2521 if (!eap->skip)
2522 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002523 // Check the name of the function. Unless it's a dictionary function
2524 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002525 if (name != NULL)
2526 arg = name;
2527 else
2528 arg = fudi.fd_newkey;
2529 if (arg != NULL && (fudi.fd_di == NULL
2530 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2531 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2532 {
2533 if (*arg == K_SPECIAL)
2534 j = 3;
2535 else
2536 j = 0;
2537 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2538 : eval_isnamec(arg[j])))
2539 ++j;
2540 if (arg[j] != NUL)
2541 emsg_funcname((char *)e_invarg2, arg);
2542 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002543 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002544 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002545 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002546 }
2547
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002548 // This may get more lines and make the pointers into the first line
2549 // invalid.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 if (get_function_args(&p, ')', &newargs,
2551 eap->cmdidx == CMD_def ? &argtypes : NULL,
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002552 &varargs, &default_args, eap->skip,
2553 eap, &line_to_free) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002554 goto errret_2;
2555
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002556 if (eap->cmdidx == CMD_def)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002557 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 // find the return type: :def Func(): type
2559 if (*p == ':')
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002560 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 ret_type = skipwhite(p + 1);
2562 p = skip_type(ret_type);
2563 if (p > ret_type)
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002564 {
2565 ret_type = vim_strnsave(ret_type, (int)(p - ret_type));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566 p = skipwhite(p);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002567 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 else
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002569 {
2570 ret_type = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 semsg(_("E1056: expected a type: %s"), ret_type);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02002572 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002573 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002574 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 else
2576 // find extra arguments "range", "dict", "abort" and "closure"
2577 for (;;)
2578 {
2579 p = skipwhite(p);
2580 if (STRNCMP(p, "range", 5) == 0)
2581 {
2582 flags |= FC_RANGE;
2583 p += 5;
2584 }
2585 else if (STRNCMP(p, "dict", 4) == 0)
2586 {
2587 flags |= FC_DICT;
2588 p += 4;
2589 }
2590 else if (STRNCMP(p, "abort", 5) == 0)
2591 {
2592 flags |= FC_ABORT;
2593 p += 5;
2594 }
2595 else if (STRNCMP(p, "closure", 7) == 0)
2596 {
2597 flags |= FC_CLOSURE;
2598 p += 7;
2599 if (current_funccal == NULL)
2600 {
2601 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
2602 name == NULL ? (char_u *)"" : name);
2603 goto erret;
2604 }
2605 }
2606 else
2607 break;
2608 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002609
Bram Moolenaare38eab22019-12-05 21:50:01 +01002610 // When there is a line break use what follows for the function body.
2611 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002612 if (*p == '\n')
2613 line_arg = p + 1;
Bram Moolenaar2c330432020-04-13 14:41:35 +02002614 else if (*p != NUL && *p != '"' && !(eap->cmdidx == CMD_def && *p == '#')
2615 && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002616 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002617
2618 /*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002619 * Read the body of the function, until "}", ":endfunction" or ":enddef" is
2620 * found.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002621 */
2622 if (KeyTyped)
2623 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002624 // Check if the function already exists, don't let the user type the
2625 // whole function before telling him it doesn't work! For a script we
2626 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002627 if (!eap->skip && !eap->forceit)
2628 {
2629 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002630 emsg(_(e_funcdict));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631 else if (name != NULL && find_func(name, NULL) != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002632 emsg_funcname(e_funcexts, name);
2633 }
2634
2635 if (!eap->skip && did_emsg)
2636 goto erret;
2637
Bram Moolenaare38eab22019-12-05 21:50:01 +01002638 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002639 cmdline_row = msg_row;
2640 }
2641
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002642 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002643 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002644
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002645 indent = 2;
2646 nesting = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647 nesting_def[nesting] = (eap->cmdidx == CMD_def);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002648 for (;;)
2649 {
2650 if (KeyTyped)
2651 {
2652 msg_scroll = TRUE;
2653 saved_wait_return = FALSE;
2654 }
2655 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002656
2657 if (line_arg != NULL)
2658 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002659 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002660 theline = line_arg;
2661 p = vim_strchr(theline, '\n');
2662 if (p == NULL)
2663 line_arg += STRLEN(line_arg);
2664 else
2665 {
2666 *p = NUL;
2667 line_arg = p + 1;
2668 }
2669 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002670 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002671 {
2672 vim_free(line_to_free);
2673 if (eap->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02002674 theline = getcmdline(':', 0L, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002675 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02002676 theline = eap->getline(':', eap->cookie, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002677 line_to_free = theline;
2678 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002679 if (KeyTyped)
2680 lines_left = Rows - 1;
2681 if (theline == NULL)
2682 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683 if (eap->cmdidx == CMD_def)
2684 emsg(_("E1057: Missing :enddef"));
2685 else
2686 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002687 goto erret;
2688 }
2689
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002690 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002691 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002692 if (SOURCING_LNUM < sourcing_lnum_off)
2693 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002694 else
2695 sourcing_lnum_off = 0;
2696
2697 if (skip_until != NULL)
2698 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 // Don't check for ":endfunc"/":enddef" between
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002700 // * ":append" and "."
2701 // * ":python <<EOF" and "EOF"
2702 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
2703 if (heredoc_trimmed == NULL
2704 || (is_heredoc && skipwhite(theline) == theline)
2705 || STRNCMP(theline, heredoc_trimmed,
2706 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002707 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002708 if (heredoc_trimmed == NULL)
2709 p = theline;
2710 else if (is_heredoc)
2711 p = skipwhite(theline) == theline
2712 ? theline : theline + STRLEN(heredoc_trimmed);
2713 else
2714 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02002715 if (STRCMP(p, skip_until) == 0)
2716 {
2717 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002718 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaare96a2492019-06-25 04:12:16 +02002719 do_concat = TRUE;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002720 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002721 }
2722 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002723 }
2724 else
2725 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002726 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01002727 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002728 ;
2729
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730 // Check for "endfunction" or "enddef".
2731 if (checkforcmd(&p, nesting_def[nesting]
2732 ? "enddef" : "endfunction", 4) && nesting-- == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002733 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002734 char_u *nextcmd = NULL;
2735
Bram Moolenaar663bb232017-06-22 19:12:10 +02002736 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002737 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002738 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002739 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002740 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 give_warning2(eap->cmdidx == CMD_def
2742 ? (char_u *)_("W1001: Text found after :enddef: %s")
2743 : (char_u *)_("W22: Text found after :endfunction: %s"),
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002744 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002745 if (nextcmd != NULL)
2746 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002747 // Another command follows. If the line came from "eap" we
2748 // can simply point into it, otherwise we need to change
2749 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02002750 eap->nextcmd = nextcmd;
2751 if (line_to_free != NULL)
2752 {
2753 vim_free(*eap->cmdlinep);
2754 *eap->cmdlinep = line_to_free;
2755 line_to_free = NULL;
2756 }
2757 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002758 break;
2759 }
2760
Bram Moolenaare38eab22019-12-05 21:50:01 +01002761 // Increase indent inside "if", "while", "for" and "try", decrease
2762 // at "end".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763 if (indent > 2 && (*p == '}' || STRNCMP(p, "end", 3) == 0))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002764 indent -= 2;
2765 else if (STRNCMP(p, "if", 2) == 0
2766 || STRNCMP(p, "wh", 2) == 0
2767 || STRNCMP(p, "for", 3) == 0
2768 || STRNCMP(p, "try", 3) == 0)
2769 indent += 2;
2770
Bram Moolenaare38eab22019-12-05 21:50:01 +01002771 // Check for defining a function inside this function.
Bram Moolenaar673660a2020-01-26 16:50:05 +01002772 // Only recognize "def" inside "def", not inside "function",
2773 // For backwards compatibility, see Test_function_python().
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 c = *p;
Bram Moolenaar673660a2020-01-26 16:50:05 +01002775 if (checkforcmd(&p, "function", 2)
2776 || (eap->cmdidx == CMD_def && checkforcmd(&p, "def", 3)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002777 {
2778 if (*p == '!')
2779 p = skipwhite(p + 1);
2780 p += eval_fname_script(p);
2781 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
2782 if (*skipwhite(p) == '(')
2783 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784 if (nesting == MAX_FUNC_NESTING - 1)
2785 emsg(_("E1058: function nesting too deep"));
2786 else
2787 {
2788 ++nesting;
2789 nesting_def[nesting] = (c == 'd');
2790 indent += 2;
2791 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002792 }
2793 }
2794
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002795 // Check for ":append", ":change", ":insert". Not for :def.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002796 p = skip_range(p, NULL);
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002797 if (eap->cmdidx != CMD_def
2798 && ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002799 || (p[0] == 'c'
2800 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
2801 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
2802 && (STRNCMP(&p[3], "nge", 3) != 0
2803 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002804 || (p[0] == 'i'
2805 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
Bram Moolenaara259d8d2020-01-31 20:10:50 +01002806 && (!ASCII_ISALPHA(p[2])
2807 || (p[2] == 's'
2808 && (!ASCII_ISALPHA(p[3])
2809 || p[3] == 'e'))))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002810 skip_until = vim_strsave((char_u *)".");
2811
Bram Moolenaare38eab22019-12-05 21:50:01 +01002812 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002813 arg = skipwhite(skiptowhite(p));
2814 if (arg[0] == '<' && arg[1] =='<'
2815 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01002816 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
2817 || ((p[2] == '3' || p[2] == 'x')
2818 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002819 || (p[0] == 'p' && p[1] == 'e'
2820 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
2821 || (p[0] == 't' && p[1] == 'c'
2822 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
2823 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
2824 && !ASCII_ISALPHA(p[3]))
2825 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
2826 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
2827 || (p[0] == 'm' && p[1] == 'z'
2828 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
2829 ))
2830 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002831 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002832 p = skipwhite(arg + 2);
2833 if (*p == NUL)
2834 skip_until = vim_strsave((char_u *)".");
2835 else
2836 skip_until = vim_strsave(p);
2837 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002838
2839 // Check for ":let v =<< [trim] EOF"
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002840 // and ":let [a, b] =<< [trim] EOF"
Bram Moolenaar8471e572019-05-19 21:37:18 +02002841 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002842 if (*arg == '[')
2843 arg = vim_strchr(arg, ']');
2844 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002845 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002846 arg = skipwhite(skiptowhite(arg));
2847 if ( arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
2848 && ((p[0] == 'l'
2849 && p[1] == 'e'
2850 && (!ASCII_ISALNUM(p[2])
2851 || (p[2] == 't' && !ASCII_ISALNUM(p[3]))))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02002852 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002853 p = skipwhite(arg + 3);
2854 if (STRNCMP(p, "trim", 4) == 0)
2855 {
2856 // Ignore leading white space.
2857 p = skipwhite(p + 4);
2858 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar8471e572019-05-19 21:37:18 +02002859 (int)(skipwhite(theline) - theline));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002860 }
2861 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p));
2862 do_concat = FALSE;
2863 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002864 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002865 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002866 }
2867
Bram Moolenaare38eab22019-12-05 21:50:01 +01002868 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002869 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002870 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002871
Bram Moolenaare38eab22019-12-05 21:50:01 +01002872 // Copy the line to newly allocated memory. get_one_sourceline()
2873 // allocates 250 bytes per line, this saves 80% on average. The cost
2874 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002875 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002876 if (p == NULL)
2877 goto erret;
2878 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002879
Bram Moolenaare38eab22019-12-05 21:50:01 +01002880 // Add NULL lines for continuation lines, so that the line count is
2881 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002882 while (sourcing_lnum_off-- > 0)
2883 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
2884
Bram Moolenaare38eab22019-12-05 21:50:01 +01002885 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002886 if (line_arg != NULL && *line_arg == NUL)
2887 line_arg = NULL;
2888 }
2889
Bram Moolenaare38eab22019-12-05 21:50:01 +01002890 // Don't define the function when skipping commands or when an error was
2891 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002892 if (eap->skip || did_emsg)
2893 goto erret;
2894
2895 /*
2896 * If there are no errors, add the function
2897 */
2898 if (fudi.fd_dict == NULL)
2899 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 hashtab_T *ht;
2901
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002902 v = find_var(name, &ht, FALSE);
2903 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
2904 {
2905 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
2906 name);
2907 goto erret;
2908 }
2909
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 fp = find_func_even_dead(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002911 if (fp != NULL)
2912 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 int dead = fp->uf_flags & FC_DEAD;
2914
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002915 // Function can be replaced with "function!" and when sourcing the
2916 // same script again, but only once.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 if (!dead && !eap->forceit
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002918 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
2919 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002920 {
2921 emsg_funcname(e_funcexts, name);
2922 goto erret;
2923 }
2924 if (fp->uf_calls > 0)
2925 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002926 emsg_funcname(
2927 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002928 name);
2929 goto erret;
2930 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002931 if (fp->uf_refcount > 1)
2932 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002933 // This function is referenced somewhere, don't redefine it but
2934 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002935 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002936 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002937 fp = NULL;
2938 overwrite = TRUE;
2939 }
2940 else
2941 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002942 char_u *exp_name = fp->uf_name_exp;
2943
2944 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01002945 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002946 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002947 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002948 fp->uf_name_exp = exp_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949 fp->uf_flags &= ~FC_DEAD;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002950#ifdef FEAT_PROFILE
2951 fp->uf_profiling = FALSE;
2952 fp->uf_prof_initialized = FALSE;
2953#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002954 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002955 }
2956 }
2957 else
2958 {
2959 char numbuf[20];
2960
2961 fp = NULL;
2962 if (fudi.fd_newkey == NULL && !eap->forceit)
2963 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002964 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002965 goto erret;
2966 }
2967 if (fudi.fd_di == NULL)
2968 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002969 // Can't add a function to a locked dictionary
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002970 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002971 goto erret;
2972 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002973 // Can't change an existing function if it is locked
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002974 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002975 goto erret;
2976
Bram Moolenaare38eab22019-12-05 21:50:01 +01002977 // Give the function a sequential number. Can only be used with a
2978 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002979 vim_free(name);
2980 sprintf(numbuf, "%d", ++func_nr);
2981 name = vim_strsave((char_u *)numbuf);
2982 if (name == NULL)
2983 goto erret;
2984 }
2985
2986 if (fp == NULL)
2987 {
2988 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
2989 {
2990 int slen, plen;
2991 char_u *scriptname;
2992
Bram Moolenaare38eab22019-12-05 21:50:01 +01002993 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002994 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002995 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002996 {
2997 scriptname = autoload_name(name);
2998 if (scriptname != NULL)
2999 {
3000 p = vim_strchr(scriptname, '/');
3001 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003002 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003003 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003004 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003005 j = OK;
3006 vim_free(scriptname);
3007 }
3008 }
3009 if (j == FAIL)
3010 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003011 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003012 goto erret;
3013 }
3014 }
3015
Bram Moolenaar47ed5532019-08-08 20:49:14 +02003016 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003017 if (fp == NULL)
3018 goto erret;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 fp->uf_dfunc_idx = -1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003020
3021 if (fudi.fd_dict != NULL)
3022 {
3023 if (fudi.fd_di == NULL)
3024 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003025 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003026 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
3027 if (fudi.fd_di == NULL)
3028 {
3029 vim_free(fp);
3030 goto erret;
3031 }
3032 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
3033 {
3034 vim_free(fudi.fd_di);
3035 vim_free(fp);
3036 goto erret;
3037 }
3038 }
3039 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003040 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003041 clear_tv(&fudi.fd_di->di_tv);
3042 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003043 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003044
Bram Moolenaare38eab22019-12-05 21:50:01 +01003045 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003046 flags |= FC_DICT;
3047 }
3048
Bram Moolenaare38eab22019-12-05 21:50:01 +01003049 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003050 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003051 if (overwrite)
3052 {
3053 hi = hash_find(&func_hashtab, name);
3054 hi->hi_key = UF2HIKEY(fp);
3055 }
3056 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003057 {
3058 vim_free(fp);
3059 goto erret;
3060 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003061 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003062 }
3063 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003064 fp->uf_def_args = default_args;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 fp->uf_ret_type = &t_any;
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003066 fp->uf_func_type = &t_func_any;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067
3068 if (eap->cmdidx == CMD_def)
3069 {
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003070 int lnum_save = SOURCING_LNUM;
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003071
3072 // error messages are for the first function line
3073 SOURCING_LNUM = sourcing_lnum_top;
3074
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 // parse the argument types
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003076 ga_init2(&fp->uf_type_list, sizeof(type_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003077
3078 if (argtypes.ga_len > 0)
3079 {
3080 // When "varargs" is set the last name/type goes into uf_va_name
3081 // and uf_va_type.
3082 int len = argtypes.ga_len - (varargs ? 1 : 0);
3083
Bram Moolenaar0b76b422020-04-07 22:05:08 +02003084 if (len > 0)
3085 fp->uf_arg_types = ALLOC_CLEAR_MULT(type_T *, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 if (fp->uf_arg_types != NULL)
3087 {
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003088 int i;
3089 type_T *type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090
3091 for (i = 0; i < len; ++ i)
3092 {
3093 p = ((char_u **)argtypes.ga_data)[i];
3094 if (p == NULL)
Bram Moolenaar49cf7cc2020-04-07 22:45:00 +02003095 // will get the type from the default value
3096 type = &t_unknown;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 else
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003098 type = parse_type(&p, &fp->uf_type_list);
3099 if (type == NULL)
3100 {
3101 SOURCING_LNUM = lnum_save;
3102 goto errret_2;
3103 }
3104 fp->uf_arg_types[i] = type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 }
3106 }
3107 if (varargs)
3108 {
3109 // Move the last argument "...name: type" to uf_va_name and
3110 // uf_va_type.
3111 fp->uf_va_name = ((char_u **)fp->uf_args.ga_data)
3112 [fp->uf_args.ga_len - 1];
3113 --fp->uf_args.ga_len;
3114 p = ((char_u **)argtypes.ga_data)[len];
3115 if (p == NULL)
3116 // todo: get type from default value
3117 fp->uf_va_type = &t_any;
3118 else
3119 fp->uf_va_type = parse_type(&p, &fp->uf_type_list);
Bram Moolenaarbfe12042020-02-04 21:54:07 +01003120 if (fp->uf_va_type == NULL)
3121 {
3122 SOURCING_LNUM = lnum_save;
3123 goto errret_2;
3124 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 }
3126 varargs = FALSE;
3127 }
3128
3129 // parse the return type, if any
3130 if (ret_type == NULL)
3131 fp->uf_ret_type = &t_void;
3132 else
3133 {
3134 p = ret_type;
3135 fp->uf_ret_type = parse_type(&p, &fp->uf_type_list);
3136 }
3137 }
3138
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003139 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003140 if ((flags & FC_CLOSURE) != 0)
3141 {
Bram Moolenaar58016442016-07-31 18:30:22 +02003142 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003143 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003144 }
3145 else
3146 fp->uf_scoped = NULL;
3147
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003148#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003149 if (prof_def_func())
3150 func_do_profile(fp);
3151#endif
3152 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02003153 if (sandbox)
3154 flags |= FC_SANDBOX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003155 fp->uf_flags = flags;
3156 fp->uf_calls = 0;
Bram Moolenaar63ce4842020-02-19 15:46:48 +01003157 fp->uf_cleared = FALSE;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003158 fp->uf_script_ctx = current_sctx;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02003159 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 if (is_export)
3161 {
3162 fp->uf_flags |= FC_EXPORT;
3163 // let ex_export() know the export worked.
3164 is_export = FALSE;
3165 }
3166
3167 // ":def Func()" needs to be compiled
3168 if (eap->cmdidx == CMD_def)
3169 compile_def_function(fp, FALSE);
3170
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003171 goto ret_free;
3172
3173erret:
3174 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02003175 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003176errret_2:
3177 ga_clear_strings(&newlines);
3178ret_free:
Bram Moolenaar292b90d2020-03-18 15:23:16 +01003179 ga_clear_strings(&argtypes);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003180 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02003181 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003182 vim_free(fudi.fd_newkey);
3183 vim_free(name);
Bram Moolenaar5e774c72020-04-12 21:53:00 +02003184 vim_free(ret_type);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003185 did_emsg |= saved_did_emsg;
3186 need_wait_return |= saved_wait_return;
3187}
3188
3189/*
3190 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
3191 * Return 2 if "p" starts with "s:".
3192 * Return 0 otherwise.
3193 */
3194 int
3195eval_fname_script(char_u *p)
3196{
Bram Moolenaare38eab22019-12-05 21:50:01 +01003197 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
3198 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003199 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
3200 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
3201 return 5;
3202 if (p[0] == 's' && p[1] == ':')
3203 return 2;
3204 return 0;
3205}
3206
3207 int
3208translated_function_exists(char_u *name)
3209{
3210 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02003211 return has_internal_func(name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 return find_func(name, NULL) != NULL;
3213}
3214
3215/*
3216 * Return TRUE when "ufunc" has old-style "..." varargs
3217 * or named varargs "...name: type".
3218 */
3219 int
3220has_varargs(ufunc_T *ufunc)
3221{
3222 return ufunc->uf_varargs || ufunc->uf_va_name != NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003223}
3224
3225/*
3226 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003227 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003228 */
3229 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003230function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003231{
3232 char_u *nm = name;
3233 char_u *p;
3234 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003235 int flag;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003236
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003237 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
3238 if (no_deref)
3239 flag |= TFN_NO_DEREF;
3240 p = trans_function_name(&nm, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003241 nm = skipwhite(nm);
3242
Bram Moolenaare38eab22019-12-05 21:50:01 +01003243 // Only accept "funcname", "funcname ", "funcname (..." and
3244 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003245 if (p != NULL && (*nm == NUL || *nm == '('))
3246 n = translated_function_exists(p);
3247 vim_free(p);
3248 return n;
3249}
3250
Bram Moolenaar113e1072019-01-20 15:30:40 +01003251#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003252 char_u *
3253get_expanded_name(char_u *name, int check)
3254{
3255 char_u *nm = name;
3256 char_u *p;
3257
3258 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
3259
3260 if (p != NULL && *nm == NUL)
3261 if (!check || translated_function_exists(p))
3262 return p;
3263
3264 vim_free(p);
3265 return NULL;
3266}
Bram Moolenaar113e1072019-01-20 15:30:40 +01003267#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003268
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003269/*
3270 * Function given to ExpandGeneric() to obtain the list of user defined
3271 * function names.
3272 */
3273 char_u *
3274get_user_func_name(expand_T *xp, int idx)
3275{
3276 static long_u done;
3277 static hashitem_T *hi;
3278 ufunc_T *fp;
3279
3280 if (idx == 0)
3281 {
3282 done = 0;
3283 hi = func_hashtab.ht_array;
3284 }
3285 if (done < func_hashtab.ht_used)
3286 {
3287 if (done++ > 0)
3288 ++hi;
3289 while (HASHITEM_EMPTY(hi))
3290 ++hi;
3291 fp = HI2UF(hi);
3292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 // don't show dead, dict and lambda functions
3294 if ((fp->uf_flags & FC_DEAD) || (fp->uf_flags & FC_DICT)
Bram Moolenaarb49edc12016-07-23 15:47:34 +02003295 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 return (char_u *)"";
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003297
3298 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003299 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003300
3301 cat_func_name(IObuff, fp);
3302 if (xp->xp_context != EXPAND_USER_FUNC)
3303 {
3304 STRCAT(IObuff, "(");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 if (!has_varargs(fp) && fp->uf_args.ga_len == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003306 STRCAT(IObuff, ")");
3307 }
3308 return IObuff;
3309 }
3310 return NULL;
3311}
3312
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003313/*
3314 * ":delfunction {name}"
3315 */
3316 void
3317ex_delfunction(exarg_T *eap)
3318{
3319 ufunc_T *fp = NULL;
3320 char_u *p;
3321 char_u *name;
3322 funcdict_T fudi;
3323
3324 p = eap->arg;
3325 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
3326 vim_free(fudi.fd_newkey);
3327 if (name == NULL)
3328 {
3329 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003330 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003331 return;
3332 }
3333 if (!ends_excmd(*skipwhite(p)))
3334 {
3335 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003336 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003337 return;
3338 }
3339 eap->nextcmd = check_nextcmd(p);
3340 if (eap->nextcmd != NULL)
3341 *p = NUL;
3342
3343 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344 fp = find_func(name, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003345 vim_free(name);
3346
3347 if (!eap->skip)
3348 {
3349 if (fp == NULL)
3350 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02003351 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003352 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003353 return;
3354 }
3355 if (fp->uf_calls > 0)
3356 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003357 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003358 return;
3359 }
3360
3361 if (fudi.fd_dict != NULL)
3362 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003363 // Delete the dict item that refers to the function, it will
3364 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003365 dictitem_remove(fudi.fd_dict, fudi.fd_di);
3366 }
3367 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003368 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003369 // A normal function (not a numbered function or lambda) has a
3370 // refcount of 1 for the entry in the hashtable. When deleting
3371 // it and the refcount is more than one, it should be kept.
3372 // A numbered function and lambda should be kept if the refcount is
3373 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003374 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003375 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003376 // Function is still referenced somewhere. Don't free it but
3377 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003378 if (func_remove(fp))
3379 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003380 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003381 }
3382 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003383 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003384 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003385 }
3386}
3387
3388/*
3389 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003390 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003391 */
3392 void
3393func_unref(char_u *name)
3394{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003395 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003396
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003397 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003398 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003399 fp = find_func(name, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003400 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003401 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003402#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003403 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003404#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01003405 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003406 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003407 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003408 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003409 // Only delete it when it's not being used. Otherwise it's done
3410 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003411 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003412 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02003413 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003414}
3415
3416/*
3417 * Unreference a Function: decrement the reference count and free it when it
3418 * becomes zero.
3419 */
3420 void
3421func_ptr_unref(ufunc_T *fp)
3422{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003423 if (fp != NULL && --fp->uf_refcount <= 0)
3424 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003425 // Only delete it when it's not being used. Otherwise it's done
3426 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02003427 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003428 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003429 }
3430}
3431
3432/*
3433 * Count a reference to a Function.
3434 */
3435 void
3436func_ref(char_u *name)
3437{
3438 ufunc_T *fp;
3439
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003440 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003441 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 fp = find_func(name, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003443 if (fp != NULL)
3444 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003445 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01003446 // Only give an error for a numbered function.
3447 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01003448 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003449}
3450
3451/*
3452 * Count a reference to a Function.
3453 */
3454 void
3455func_ptr_ref(ufunc_T *fp)
3456{
3457 if (fp != NULL)
3458 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003459}
3460
3461/*
3462 * Return TRUE if items in "fc" do not have "copyID". That means they are not
3463 * referenced from anywhere that is in use.
3464 */
3465 static int
3466can_free_funccal(funccall_T *fc, int copyID)
3467{
3468 return (fc->l_varlist.lv_copyID != copyID
3469 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003470 && fc->l_avars.dv_copyID != copyID
3471 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003472}
3473
3474/*
3475 * ":return [expr]"
3476 */
3477 void
3478ex_return(exarg_T *eap)
3479{
3480 char_u *arg = eap->arg;
3481 typval_T rettv;
3482 int returning = FALSE;
3483
3484 if (current_funccal == NULL)
3485 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003486 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003487 return;
3488 }
3489
3490 if (eap->skip)
3491 ++emsg_skip;
3492
3493 eap->nextcmd = NULL;
3494 if ((*arg != NUL && *arg != '|' && *arg != '\n')
3495 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
3496 {
3497 if (!eap->skip)
3498 returning = do_return(eap, FALSE, TRUE, &rettv);
3499 else
3500 clear_tv(&rettv);
3501 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003502 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003503 else if (!eap->skip)
3504 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003505 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003506 update_force_abort();
3507
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003508 /*
3509 * Return unless the expression evaluation has been cancelled due to an
3510 * aborting error, an interrupt, or an exception.
3511 */
3512 if (!aborting())
3513 returning = do_return(eap, FALSE, TRUE, NULL);
3514 }
3515
Bram Moolenaare38eab22019-12-05 21:50:01 +01003516 // When skipping or the return gets pending, advance to the next command
3517 // in this line (!returning). Otherwise, ignore the rest of the line.
3518 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003519 if (returning)
3520 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003521 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003522 eap->nextcmd = check_nextcmd(arg);
3523
3524 if (eap->skip)
3525 --emsg_skip;
3526}
3527
3528/*
3529 * ":1,25call func(arg1, arg2)" function call.
3530 */
3531 void
3532ex_call(exarg_T *eap)
3533{
3534 char_u *arg = eap->arg;
3535 char_u *startarg;
3536 char_u *name;
3537 char_u *tofree;
3538 int len;
3539 typval_T rettv;
3540 linenr_T lnum;
3541 int doesrange;
3542 int failed = FALSE;
3543 funcdict_T fudi;
3544 partial_T *partial = NULL;
3545
3546 if (eap->skip)
3547 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003548 // trans_function_name() doesn't work well when skipping, use eval0()
3549 // instead to skip to any following command, e.g. for:
3550 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003551 ++emsg_skip;
3552 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3553 clear_tv(&rettv);
3554 --emsg_skip;
3555 return;
3556 }
3557
3558 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
3559 if (fudi.fd_newkey != NULL)
3560 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003561 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003562 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003563 vim_free(fudi.fd_newkey);
3564 }
3565 if (tofree == NULL)
3566 return;
3567
Bram Moolenaare38eab22019-12-05 21:50:01 +01003568 // Increase refcount on dictionary, it could get deleted when evaluating
3569 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003570 if (fudi.fd_dict != NULL)
3571 ++fudi.fd_dict->dv_refcount;
3572
Bram Moolenaare38eab22019-12-05 21:50:01 +01003573 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3574 // contents. For VAR_PARTIAL get its partial, unless we already have one
3575 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003576 len = (int)STRLEN(tofree);
3577 name = deref_func_name(tofree, &len,
3578 partial != NULL ? NULL : &partial, FALSE);
3579
Bram Moolenaare38eab22019-12-05 21:50:01 +01003580 // Skip white space to allow ":call func ()". Not good, but required for
3581 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003582 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003583 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003584
3585 if (*startarg != '(')
3586 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587 semsg(_(e_missing_paren), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003588 goto end;
3589 }
3590
3591 /*
3592 * When skipping, evaluate the function once, to find the end of the
3593 * arguments.
3594 * When the function takes a range, this is discovered after the first
3595 * call, and the loop is broken.
3596 */
3597 if (eap->skip)
3598 {
3599 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003600 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003601 }
3602 else
3603 lnum = eap->line1;
3604 for ( ; lnum <= eap->line2; ++lnum)
3605 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003606 funcexe_T funcexe;
3607
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003608 if (!eap->skip && eap->addr_count > 0)
3609 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003610 if (lnum > curbuf->b_ml.ml_line_count)
3611 {
3612 // If the function deleted lines or switched to another buffer
3613 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003614 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003615 break;
3616 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003617 curwin->w_cursor.lnum = lnum;
3618 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003619 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003620 }
3621 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003622
Bram Moolenaara80faa82020-04-12 19:37:17 +02003623 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003624 funcexe.firstline = eap->line1;
3625 funcexe.lastline = eap->line2;
3626 funcexe.doesrange = &doesrange;
3627 funcexe.evaluate = !eap->skip;
3628 funcexe.partial = partial;
3629 funcexe.selfdict = fudi.fd_dict;
3630 if (get_func_tv(name, -1, &rettv, &arg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003631 {
3632 failed = TRUE;
3633 break;
3634 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003635 if (has_watchexpr())
3636 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003637
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003638 // Handle a function returning a Funcref, Dictionary or List.
3639 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE,
3640 name, &name) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003641 {
3642 failed = TRUE;
3643 break;
3644 }
3645
3646 clear_tv(&rettv);
3647 if (doesrange || eap->skip)
3648 break;
3649
Bram Moolenaare38eab22019-12-05 21:50:01 +01003650 // Stop when immediately aborting on error, or when an interrupt
3651 // occurred or an exception was thrown but not caught.
3652 // get_func_tv() returned OK, so that the check for trailing
3653 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003654 if (aborting())
3655 break;
3656 }
3657 if (eap->skip)
3658 --emsg_skip;
3659
Bram Moolenaare51bb172020-02-16 19:42:23 +01003660 // When inside :try we need to check for following "| catch".
3661 if (!failed || eap->cstack->cs_trylevel > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003662 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003663 // Check for trailing illegal characters and a following command.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003664 if (!ends_excmd(*arg))
3665 {
Bram Moolenaar40d9da22020-02-17 10:01:24 +01003666 if (!failed)
3667 {
3668 emsg_severe = TRUE;
3669 emsg(_(e_trailing));
3670 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003671 }
3672 else
3673 eap->nextcmd = check_nextcmd(arg);
3674 }
3675
3676end:
3677 dict_unref(fudi.fd_dict);
3678 vim_free(tofree);
3679}
3680
3681/*
3682 * Return from a function. Possibly makes the return pending. Also called
3683 * for a pending return at the ":endtry" or after returning from an extra
3684 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3685 * when called due to a ":return" command. "rettv" may point to a typval_T
3686 * with the return rettv. Returns TRUE when the return can be carried out,
3687 * FALSE when the return gets pending.
3688 */
3689 int
3690do_return(
3691 exarg_T *eap,
3692 int reanimate,
3693 int is_cmd,
3694 void *rettv)
3695{
3696 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01003697 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003698
3699 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003700 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003701 current_funccal->returned = FALSE;
3702
3703 /*
3704 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3705 * not in its finally clause (which then is to be executed next) is found.
3706 * In this case, make the ":return" pending for execution at the ":endtry".
3707 * Otherwise, return normally.
3708 */
3709 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3710 if (idx >= 0)
3711 {
3712 cstack->cs_pending[idx] = CSTP_RETURN;
3713
3714 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003715 // A pending return again gets pending. "rettv" points to an
3716 // allocated variable with the rettv of the original ":return"'s
3717 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003718 cstack->cs_rettv[idx] = rettv;
3719 else
3720 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003721 // When undoing a return in order to make it pending, get the stored
3722 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003723 if (reanimate)
3724 rettv = current_funccal->rettv;
3725
3726 if (rettv != NULL)
3727 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003728 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003729 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
3730 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
3731 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003732 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003733 }
3734 else
3735 cstack->cs_rettv[idx] = NULL;
3736
3737 if (reanimate)
3738 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003739 // The pending return value could be overwritten by a ":return"
3740 // without argument in a finally clause; reset the default
3741 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003742 current_funccal->rettv->v_type = VAR_NUMBER;
3743 current_funccal->rettv->vval.v_number = 0;
3744 }
3745 }
3746 report_make_pending(CSTP_RETURN, rettv);
3747 }
3748 else
3749 {
3750 current_funccal->returned = TRUE;
3751
Bram Moolenaare38eab22019-12-05 21:50:01 +01003752 // If the return is carried out now, store the return value. For
3753 // a return immediately after reanimation, the value is already
3754 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003755 if (!reanimate && rettv != NULL)
3756 {
3757 clear_tv(current_funccal->rettv);
3758 *current_funccal->rettv = *(typval_T *)rettv;
3759 if (!is_cmd)
3760 vim_free(rettv);
3761 }
3762 }
3763
3764 return idx < 0;
3765}
3766
3767/*
3768 * Free the variable with a pending return value.
3769 */
3770 void
3771discard_pending_return(void *rettv)
3772{
3773 free_tv((typval_T *)rettv);
3774}
3775
3776/*
3777 * Generate a return command for producing the value of "rettv". The result
3778 * is an allocated string. Used by report_pending() for verbose messages.
3779 */
3780 char_u *
3781get_return_cmd(void *rettv)
3782{
3783 char_u *s = NULL;
3784 char_u *tofree = NULL;
3785 char_u numbuf[NUMBUFLEN];
3786
3787 if (rettv != NULL)
3788 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
3789 if (s == NULL)
3790 s = (char_u *)"";
3791
3792 STRCPY(IObuff, ":return ");
3793 STRNCPY(IObuff + 8, s, IOSIZE - 8);
3794 if (STRLEN(s) + 8 >= IOSIZE)
3795 STRCPY(IObuff + IOSIZE - 4, "...");
3796 vim_free(tofree);
3797 return vim_strsave(IObuff);
3798}
3799
3800/*
3801 * Get next function line.
3802 * Called by do_cmdline() to get the next line.
3803 * Returns allocated string, or NULL for end of function.
3804 */
3805 char_u *
3806get_func_line(
3807 int c UNUSED,
3808 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003809 int indent UNUSED,
3810 int do_concat UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003811{
3812 funccall_T *fcp = (funccall_T *)cookie;
3813 ufunc_T *fp = fcp->func;
3814 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003815 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003816
Bram Moolenaare38eab22019-12-05 21:50:01 +01003817 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003818 if (fcp->dbg_tick != debug_tick)
3819 {
3820 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003821 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003822 fcp->dbg_tick = debug_tick;
3823 }
3824#ifdef FEAT_PROFILE
3825 if (do_profiling == PROF_YES)
3826 func_line_end(cookie);
3827#endif
3828
3829 gap = &fp->uf_lines;
3830 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3831 || fcp->returned)
3832 retval = NULL;
3833 else
3834 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003835 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003836 while (fcp->linenr < gap->ga_len
3837 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
3838 ++fcp->linenr;
3839 if (fcp->linenr >= gap->ga_len)
3840 retval = NULL;
3841 else
3842 {
3843 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003844 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003845#ifdef FEAT_PROFILE
3846 if (do_profiling == PROF_YES)
3847 func_line_start(cookie);
3848#endif
3849 }
3850 }
3851
Bram Moolenaare38eab22019-12-05 21:50:01 +01003852 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003853 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003854 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003855 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003856 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003857 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003858 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003859 fcp->dbg_tick = debug_tick;
3860 }
3861
3862 return retval;
3863}
3864
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003865/*
3866 * Return TRUE if the currently active function should be ended, because a
3867 * return was encountered or an error occurred. Used inside a ":while".
3868 */
3869 int
3870func_has_ended(void *cookie)
3871{
3872 funccall_T *fcp = (funccall_T *)cookie;
3873
Bram Moolenaare38eab22019-12-05 21:50:01 +01003874 // Ignore the "abort" flag if the abortion behavior has been changed due to
3875 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003876 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3877 || fcp->returned);
3878}
3879
3880/*
3881 * return TRUE if cookie indicates a function which "abort"s on errors.
3882 */
3883 int
3884func_has_abort(
3885 void *cookie)
3886{
3887 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
3888}
3889
3890
3891/*
3892 * Turn "dict.Func" into a partial for "Func" bound to "dict".
3893 * Don't do this when "Func" is already a partial that was bound
3894 * explicitly (pt_auto is FALSE).
3895 * Changes "rettv" in-place.
3896 * Returns the updated "selfdict_in".
3897 */
3898 dict_T *
3899make_partial(dict_T *selfdict_in, typval_T *rettv)
3900{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003901 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003902 char_u *tofree = NULL;
3903 ufunc_T *fp;
3904 char_u fname_buf[FLEN_FIXED + 1];
3905 int error;
3906 dict_T *selfdict = selfdict_in;
3907
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003908 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
3909 fp = rettv->vval.v_partial->pt_func;
3910 else
3911 {
3912 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
3913 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003914 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003915 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 fp = find_func(fname, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003917 vim_free(tofree);
3918 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003919
3920 if (fp != NULL && (fp->uf_flags & FC_DICT))
3921 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003922 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003923
3924 if (pt != NULL)
3925 {
3926 pt->pt_refcount = 1;
3927 pt->pt_dict = selfdict;
3928 pt->pt_auto = TRUE;
3929 selfdict = NULL;
3930 if (rettv->v_type == VAR_FUNC)
3931 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003932 // Just a function: Take over the function name and use
3933 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003934 pt->pt_name = rettv->vval.v_string;
3935 }
3936 else
3937 {
3938 partial_T *ret_pt = rettv->vval.v_partial;
3939 int i;
3940
Bram Moolenaare38eab22019-12-05 21:50:01 +01003941 // Partial: copy the function name, use selfdict and copy
3942 // args. Can't take over name or args, the partial might
3943 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003944 if (ret_pt->pt_name != NULL)
3945 {
3946 pt->pt_name = vim_strsave(ret_pt->pt_name);
3947 func_ref(pt->pt_name);
3948 }
3949 else
3950 {
3951 pt->pt_func = ret_pt->pt_func;
3952 func_ptr_ref(pt->pt_func);
3953 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003954 if (ret_pt->pt_argc > 0)
3955 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003956 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003957 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003958 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003959 pt->pt_argc = 0;
3960 else
3961 {
3962 pt->pt_argc = ret_pt->pt_argc;
3963 for (i = 0; i < pt->pt_argc; i++)
3964 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
3965 }
3966 }
3967 partial_unref(ret_pt);
3968 }
3969 rettv->v_type = VAR_PARTIAL;
3970 rettv->vval.v_partial = pt;
3971 }
3972 }
3973 return selfdict;
3974}
3975
3976/*
3977 * Return the name of the executed function.
3978 */
3979 char_u *
3980func_name(void *cookie)
3981{
3982 return ((funccall_T *)cookie)->func->uf_name;
3983}
3984
3985/*
3986 * Return the address holding the next breakpoint line for a funccall cookie.
3987 */
3988 linenr_T *
3989func_breakpoint(void *cookie)
3990{
3991 return &((funccall_T *)cookie)->breakpoint;
3992}
3993
3994/*
3995 * Return the address holding the debug tick for a funccall cookie.
3996 */
3997 int *
3998func_dbg_tick(void *cookie)
3999{
4000 return &((funccall_T *)cookie)->dbg_tick;
4001}
4002
4003/*
4004 * Return the nesting level for a funccall cookie.
4005 */
4006 int
4007func_level(void *cookie)
4008{
4009 return ((funccall_T *)cookie)->level;
4010}
4011
4012/*
4013 * Return TRUE when a function was ended by a ":return" command.
4014 */
4015 int
4016current_func_returned(void)
4017{
4018 return current_funccal->returned;
4019}
4020
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004021 int
4022free_unref_funccal(int copyID, int testing)
4023{
4024 int did_free = FALSE;
4025 int did_free_funccal = FALSE;
4026 funccall_T *fc, **pfc;
4027
4028 for (pfc = &previous_funccal; *pfc != NULL; )
4029 {
4030 if (can_free_funccal(*pfc, copyID))
4031 {
4032 fc = *pfc;
4033 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01004034 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004035 did_free = TRUE;
4036 did_free_funccal = TRUE;
4037 }
4038 else
4039 pfc = &(*pfc)->caller;
4040 }
4041 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01004042 // When a funccal was freed some more items might be garbage
4043 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004044 (void)garbage_collect(testing);
4045
4046 return did_free;
4047}
4048
4049/*
Bram Moolenaarba209902016-08-24 22:06:38 +02004050 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004051 */
4052 static funccall_T *
4053get_funccal(void)
4054{
4055 int i;
4056 funccall_T *funccal;
4057 funccall_T *temp_funccal;
4058
4059 funccal = current_funccal;
4060 if (debug_backtrace_level > 0)
4061 {
4062 for (i = 0; i < debug_backtrace_level; i++)
4063 {
4064 temp_funccal = funccal->caller;
4065 if (temp_funccal)
4066 funccal = temp_funccal;
4067 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01004068 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004069 debug_backtrace_level = i;
4070 }
4071 }
4072 return funccal;
4073}
4074
4075/*
4076 * Return the hashtable used for local variables in the current funccal.
4077 * Return NULL if there is no current funccal.
4078 */
4079 hashtab_T *
4080get_funccal_local_ht()
4081{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004083 return NULL;
4084 return &get_funccal()->l_vars.dv_hashtab;
4085}
4086
4087/*
4088 * Return the l: scope variable.
4089 * Return NULL if there is no current funccal.
4090 */
4091 dictitem_T *
4092get_funccal_local_var()
4093{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004094 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004095 return NULL;
4096 return &get_funccal()->l_vars_var;
4097}
4098
4099/*
4100 * Return the hashtable used for argument in the current funccal.
4101 * Return NULL if there is no current funccal.
4102 */
4103 hashtab_T *
4104get_funccal_args_ht()
4105{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004107 return NULL;
4108 return &get_funccal()->l_avars.dv_hashtab;
4109}
4110
4111/*
4112 * Return the a: scope variable.
4113 * Return NULL if there is no current funccal.
4114 */
4115 dictitem_T *
4116get_funccal_args_var()
4117{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 if (current_funccal == NULL || current_funccal->l_vars.dv_refcount == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004119 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01004120 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004121}
4122
4123/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004124 * List function variables, if there is a function.
4125 */
4126 void
4127list_func_vars(int *first)
4128{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 if (current_funccal != NULL && current_funccal->l_vars.dv_refcount > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004130 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01004131 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004132}
4133
4134/*
4135 * If "ht" is the hashtable for local variables in the current funccal, return
4136 * the dict that contains it.
4137 * Otherwise return NULL.
4138 */
4139 dict_T *
4140get_current_funccal_dict(hashtab_T *ht)
4141{
4142 if (current_funccal != NULL
4143 && ht == &current_funccal->l_vars.dv_hashtab)
4144 return &current_funccal->l_vars;
4145 return NULL;
4146}
4147
4148/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004149 * Search hashitem in parent scope.
4150 */
4151 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004152find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004153{
4154 funccall_T *old_current_funccal = current_funccal;
4155 hashtab_T *ht;
4156 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004157 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004158
4159 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4160 return NULL;
4161
Bram Moolenaare38eab22019-12-05 21:50:01 +01004162 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004163 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02004164 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004165 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004166 ht = find_var_ht(name, &varname);
4167 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02004168 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004169 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02004170 if (!HASHITEM_EMPTY(hi))
4171 {
4172 *pht = ht;
4173 break;
4174 }
4175 }
4176 if (current_funccal == current_funccal->func->uf_scoped)
4177 break;
4178 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02004179 }
4180 current_funccal = old_current_funccal;
4181
4182 return hi;
4183}
4184
4185/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004186 * Search variable in parent scope.
4187 */
4188 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004189find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004190{
4191 dictitem_T *v = NULL;
4192 funccall_T *old_current_funccal = current_funccal;
4193 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004194 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004195
4196 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
4197 return NULL;
4198
Bram Moolenaare38eab22019-12-05 21:50:01 +01004199 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004200 current_funccal = current_funccal->func->uf_scoped;
4201 while (current_funccal)
4202 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004203 ht = find_var_ht(name, &varname);
4204 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004205 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02004206 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004207 if (v != NULL)
4208 break;
4209 }
4210 if (current_funccal == current_funccal->func->uf_scoped)
4211 break;
4212 current_funccal = current_funccal->func->uf_scoped;
4213 }
4214 current_funccal = old_current_funccal;
4215
4216 return v;
4217}
4218
4219/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004220 * Set "copyID + 1" in previous_funccal and callers.
4221 */
4222 int
4223set_ref_in_previous_funccal(int copyID)
4224{
4225 int abort = FALSE;
4226 funccall_T *fc;
4227
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004228 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004229 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004230 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004231 abort = abort
4232 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
4233 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004234 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004235 }
4236 return abort;
4237}
4238
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004239 static int
4240set_ref_in_funccal(funccall_T *fc, int copyID)
4241{
4242 int abort = FALSE;
4243
4244 if (fc->fc_copyID != copyID)
4245 {
4246 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004247 abort = abort
4248 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
4249 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004250 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02004251 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004252 }
4253 return abort;
4254}
4255
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004256/*
4257 * Set "copyID" in all local vars and arguments in the call stack.
4258 */
4259 int
4260set_ref_in_call_stack(int copyID)
4261{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004262 int abort = FALSE;
4263 funccall_T *fc;
4264 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004265
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004266 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004267 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004268
4269 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004270 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
4271 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02004272 abort = abort || set_ref_in_funccal(fc, copyID);
4273
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004274 return abort;
4275}
4276
4277/*
4278 * Set "copyID" in all functions available by name.
4279 */
4280 int
4281set_ref_in_functions(int copyID)
4282{
4283 int todo;
4284 hashitem_T *hi = NULL;
4285 int abort = FALSE;
4286 ufunc_T *fp;
4287
4288 todo = (int)func_hashtab.ht_used;
4289 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004290 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004291 if (!HASHITEM_EMPTY(hi))
4292 {
4293 --todo;
4294 fp = HI2UF(hi);
4295 if (!func_name_refcount(fp->uf_name))
4296 abort = abort || set_ref_in_func(NULL, fp, copyID);
4297 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004298 }
4299 return abort;
4300}
4301
4302/*
4303 * Set "copyID" in all function arguments.
4304 */
4305 int
4306set_ref_in_func_args(int copyID)
4307{
4308 int i;
4309 int abort = FALSE;
4310
4311 for (i = 0; i < funcargs.ga_len; ++i)
4312 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
4313 copyID, NULL, NULL);
4314 return abort;
4315}
4316
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004317/*
4318 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004319 * Returns TRUE if setting references failed somehow.
4320 */
4321 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004322set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004323{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004324 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004325 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01004326 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004327 char_u fname_buf[FLEN_FIXED + 1];
4328 char_u *tofree = NULL;
4329 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004330 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004331
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004332 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004333 return FALSE;
4334
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004335 if (fp_in == NULL)
4336 {
4337 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 fp = find_func(fname, NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004339 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004340 if (fp != NULL)
4341 {
4342 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004343 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004344 }
4345 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004346 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004347}
4348
Bram Moolenaare38eab22019-12-05 21:50:01 +01004349#endif // FEAT_EVAL